1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
|
/*
* Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2010 David King <davidk@openismus.com>
* Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <glib/gi18n-lib.h>
#include <string.h>
#include <gdk/gdkkeysyms.h>
#ifdef HAVE_GTKSOURCEVIEW
#ifdef GTK_DISABLE_SINGLE_INCLUDES
#undef GTK_DISABLE_SINGLE_INCLUDES
#endif
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
#include <gtksourceview/gtksourcebuffer.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
#include <gtksourceview/gtksourcestylescheme.h>
#endif
#include "query-editor.h"
#include <binreloc/gda-binreloc.h>
#include "../browser-connection.h"
#include "../browser-window.h"
#include <libgda-ui/internal/popup-container.h>
#include "../support.h"
#include "../common/widget-overlay.h"
#define QUERY_EDITOR_LANGUAGE_SQL "gda-sql"
#define COLOR_ALTER_FACTOR 1.8
#define MAX_HISTORY_BATCH_ITEMS 20
#define STATES_ARRAY_SIZE 32
static void query_editor_history_batch_add_item (QueryEditorHistoryBatch *qib,
QueryEditorHistoryItem *qih);
static void query_editor_history_batch_del_item (QueryEditor *editor, QueryEditorHistoryBatch *qib,
QueryEditorHistoryItem *qih);
typedef void (* CreateTagsFunc) (QueryEditor *editor, const gchar *language);
typedef struct {
QueryEditorHistoryBatch *batch; /* ref held here */
QueryEditorHistoryItem *item; /* ref held here */
GtkTextTag *tag; /* ref held here */
GtkTextMark *start_mark; /* ref NOT held here */
GtkTextMark *end_mark; /* ref NOT held here */
gint ref_count;
} HistItemData;
static HistItemData *hist_item_data_new (void);
static HistItemData *hist_item_data_ref (HistItemData *hdata);
static void hist_item_data_unref (HistItemData *hdata);
struct _QueryEditorPrivate {
QueryEditorMode mode;
GtkWidget *scrolled_window;
GtkWidget *text;
GtkTextTag *indent_tag;
GArray *states; /* array of strings, pos 0 => oldest state */
gint current_state;
gchar *current_state_text;
/* HISTORY mode */
guint ts_timeout_id;
GSList *batches_list; /* list of QueryEditorHistoryBatch, in reverse order, refs held here */
GHashTable *hash; /* crossed references:
* QueryEditorHistoryBatch --> HistItemData on @batch
* QueryEditorHistoryItem --> HistItemData on @item
* GtkTextTag --> HistItemData on @tag
*
* hash table holds references to all HistItemData
*/
QueryEditorHistoryBatch *insert_into_batch; /* hold ref here */
HistItemData *hist_focus; /* ref held here */
/* completion popup */
GtkWidget *completion_popup;
GtkTreeView *completion_treeview;
GtkCellRenderer *completion_renderer;
GtkWidget *completion_sw;
/* tooltip */
GtkWidget *ovl;
GtkWidget *tooltip_widget;
};
static void query_editor_class_init (QueryEditorClass *klass);
static void query_editor_init (QueryEditor *editor, QueryEditorClass *klass);
static void query_editor_finalize (GObject *object);
static void query_editor_map (GtkWidget *widget);
static void query_editor_grab_focus (GtkWidget *widget);
static GObjectClass *parent_class = NULL;
static GHashTable *supported_languages = NULL;
static gint number_of_objects = 0;
static void focus_on_hist_data (QueryEditor *editor, HistItemData *hdata);
static HistItemData *get_next_hist_data (QueryEditor *editor, HistItemData *hdata);
static HistItemData *get_prev_hist_data (QueryEditor *editor, HistItemData *hdata);
static gboolean timestamps_update_cb (QueryEditor *editor);
/* signals */
enum
{
CHANGED,
HISTORY_ITEM_REMOVED,
HISTORY_CLEARED,
EXECUTE_REQUEST,
LAST_SIGNAL
};
static gint query_editor_signals[LAST_SIGNAL] = { 0, 0, 0, 0 };
/*
* Private functions
*/
static void
create_tags_for_sql (QueryEditor *editor, const gchar *language)
{
#ifdef HAVE_GTKSOURCEVIEW
GtkTextBuffer *buffer;
GtkSourceLanguageManager *mgr;
GtkSourceLanguage *lang;
gchar ** current_search_path;
gint len;
gchar ** search_path;
GtkSourceStyleSchemeManager* sch_mgr;
GtkSourceStyleScheme *sch;
#endif
g_return_if_fail (language != NULL);
g_return_if_fail (!strcmp (language, QUERY_EDITOR_LANGUAGE_SQL));
#ifdef HAVE_GTKSOURCEVIEW
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
mgr = gtk_source_language_manager_new ();
/* alter search path */
current_search_path = (gchar **) gtk_source_language_manager_get_search_path (mgr);
len = g_strv_length (current_search_path);
search_path = g_new0 (gchar*, len + 2);
memcpy (search_path, current_search_path, sizeof (gchar*) * len);
search_path [len] = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, "language-specs", NULL);
gtk_source_language_manager_set_search_path (mgr, search_path);
g_free (search_path [len]);
g_free (search_path);
lang = gtk_source_language_manager_get_language (mgr, "gda-sql");
if (!lang) {
gchar *tmp;
tmp = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, "language-spec", NULL);
g_print ("Could not find the gda-sql.lang file in %s,\nusing the default SQL highlighting rules.\n",
tmp);
g_free (tmp);
lang = gtk_source_language_manager_get_language (mgr, "sql");
}
if (lang)
gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (buffer), lang);
g_object_unref (mgr);
sch_mgr = gtk_source_style_scheme_manager_get_default ();
sch = gtk_source_style_scheme_manager_get_scheme (sch_mgr, "tango");
if (sch)
gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (buffer), sch);
#endif
}
/*
* QueryEditor class implementation
*/
static void
query_editor_class_init (QueryEditorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
query_editor_signals[CHANGED] =
g_signal_new ("changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (QueryEditorClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
query_editor_signals[EXECUTE_REQUEST] =
g_signal_new ("execute-request",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (QueryEditorClass, execute_request),
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
query_editor_signals[HISTORY_ITEM_REMOVED] =
g_signal_new ("history-item-removed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (QueryEditorClass, history_item_removed),
NULL, NULL,
g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
query_editor_signals[HISTORY_CLEARED] =
g_signal_new ("history-cleared",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (QueryEditorClass, history_cleared),
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
object_class->finalize = query_editor_finalize;
GTK_WIDGET_CLASS (object_class)->map = query_editor_map;
GTK_WIDGET_CLASS (object_class)->grab_focus = query_editor_grab_focus;
}
static void
text_buffer_changed_cb (G_GNUC_UNUSED GtkTextBuffer *buffer, QueryEditor *editor)
{
if (editor->priv->mode != QUERY_EDITOR_HISTORY)
g_signal_emit (editor, query_editor_signals[CHANGED], 0);
}
static void
popup_container_position_func (PopupContainer *cont, gint *out_x, gint *out_y)
{
QueryEditor *editor;
GdkWindow *wind;
gint x, y, ex, ey;
GtkTextBuffer *buffer;
GtkTextIter iter;
GdkRectangle rect;
GtkTextView *textview;
editor = g_object_get_data (G_OBJECT (cont), "editor");
textview = GTK_TEXT_VIEW (editor->priv->text);
buffer = gtk_text_view_get_buffer (textview);
gtk_text_buffer_get_iter_at_mark (buffer, &iter, gtk_text_buffer_get_insert (buffer));
gtk_text_view_get_iter_location (textview, &iter, &rect);
gtk_text_view_buffer_to_window_coords (textview, GTK_TEXT_WINDOW_WIDGET,
rect.x, rect.y, &ex, &ey);
wind = gtk_text_view_get_window (textview, GTK_TEXT_WINDOW_WIDGET);
gdk_window_get_origin (wind, &x, &y);
x += ex + rect.width;
y += ey + rect.height;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
*out_x = x;
*out_y = y;
}
static gchar *
get_string_to_complete (QueryEditor *editor, gchar **out_start_pos)
{
GtkTextBuffer *buffer;
GtkTextIter start, end;
GtkTextMark *mark;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &end, mark);
gtk_text_buffer_get_start_iter (buffer, &start);
gchar *str, *ptr;
str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
if (!*str) {
g_free (str);
*out_start_pos = NULL;
return NULL;
}
for (ptr = str + strlen (str) - 1; ptr > str; ptr--) {
if (! g_ascii_isalnum (*ptr) &&
(*ptr != '_') && (*ptr != '.')) {
ptr++;
break;
}
}
if ((ptr == str) &&
! g_ascii_isalnum (*ptr) &&
(*ptr != '_'))
ptr++;
/*g_print ("completing [%s]\n", ptr);*/
*out_start_pos = ptr;
return str;
}
static void
completion_row_activated_cb (G_GNUC_UNUSED GtkTreeView *treeview, GtkTreePath *path,
G_GNUC_UNUSED GtkTreeViewColumn *column, QueryEditor *editor)
{
GtkTreeModel *model;
GtkTreeIter iter;
gtk_widget_hide (editor->priv->completion_popup);
model = gtk_tree_view_get_model (editor->priv->completion_treeview);
if (gtk_tree_model_get_iter (model, &iter, path)) {
gchar *compl;
gchar *str, *ptr;
str = get_string_to_complete (editor, &ptr);
if (!str)
return;
gtk_tree_model_get (model, &iter, 0, &compl, -1);
GtkTextBuffer *buffer;
GtkTextIter start, end;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
gtk_text_buffer_get_iter_at_mark (buffer, &end, gtk_text_buffer_get_insert (buffer));
start = end;
if (gtk_text_iter_backward_chars (&start, strlen (ptr))) {
gtk_text_buffer_delete (buffer, &start, &end);
gtk_text_buffer_insert (buffer, &end, compl, -1);
gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
}
g_free (str);
g_free (compl);
}
}
static void
display_completions (QueryEditor *editor)
{
gchar *str, *ptr;
str = get_string_to_complete (editor, &ptr);
if (!str)
return;
BrowserConnection *bcnc;
gchar **compl;
bcnc = browser_window_get_connection ((BrowserWindow*) gtk_widget_get_toplevel ((GtkWidget*) editor));
compl = browser_connection_get_completions (bcnc, str, ptr - str, strlen (str));
g_free (str);
if (compl) {
GtkListStore *model;
if (! editor->priv->completion_popup) {
GtkWidget *treeview;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkWidget *popup, *sw;
model = gtk_list_store_new (1, G_TYPE_STRING);
treeview = browser_make_tree_view (GTK_TREE_MODEL (model));
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
gtk_tree_view_set_grid_lines (GTK_TREE_VIEW (treeview), GTK_TREE_VIEW_GRID_LINES_NONE);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
GTK_SELECTION_BROWSE);
g_object_unref (model);
renderer = gtk_cell_renderer_text_new ();
g_object_set (G_OBJECT (renderer), "scale", 0.8,
"background", "#ffff82", NULL);
column = gtk_tree_view_column_new_with_attributes ("", renderer,
"text", 0, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
GTK_SHADOW_NONE);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (sw), treeview);
popup = popup_container_new_with_func (popup_container_position_func);
g_object_set_data ((GObject*) popup, "editor", editor);
gtk_container_set_border_width (GTK_CONTAINER (popup), 0);
gtk_container_add (GTK_CONTAINER (popup), sw);
editor->priv->completion_popup = popup;
editor->priv->completion_treeview = GTK_TREE_VIEW (treeview);
editor->priv->completion_renderer = renderer;
editor->priv->completion_sw = sw;
g_signal_connect (treeview, "row-activated",
G_CALLBACK (completion_row_activated_cb), editor);
}
else {
model = GTK_LIST_STORE (gtk_tree_view_get_model (editor->priv->completion_treeview));
gtk_list_store_clear (model);
}
/* fill in the model */
GtkTreeIter iter;
gint w, width = 0, h, height = 0;
gint i;
for (i = 0; ; i++) {
if (! compl[i])
break;
/*g_print ("==> [%s]\n", compl[i]);*/
gtk_list_store_append (model, &iter);
gtk_list_store_set (model, &iter, 0, compl[i], -1);
if (i == 0)
gtk_tree_selection_select_iter (gtk_tree_view_get_selection (editor->priv->completion_treeview),
&iter);
GtkRequisition nat_req;
g_object_set ((GObject*) editor->priv->completion_renderer, "text", compl[i], NULL);
gtk_cell_renderer_get_preferred_size (editor->priv->completion_renderer,
(GtkWidget*) editor->priv->completion_treeview,
NULL, &nat_req);
width = MAX (width, nat_req.width);
height += nat_req.height + 2;
}
g_strfreev (compl);
gtk_widget_set_size_request (editor->priv->completion_sw,
MIN (width + 30, 400), MIN (height, 400));
gtk_widget_show_all (editor->priv->completion_popup);
}
}
/*
* Returns: -1 if none focussed
*/
static gboolean
event (GtkWidget *text_view, GdkEvent *ev, QueryEditor *editor)
{
GtkTextIter start, end, iter;
GtkTextBuffer *buffer;
GdkEventButton *event;
gint x, y;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
if ((editor->priv->mode == QUERY_EDITOR_HISTORY) &&
(ev->type == GDK_BUTTON_RELEASE)) {
event = (GdkEventButton *)ev;
if (event->button != 1)
return FALSE;
/* we shouldn't follow a link if the user has selected something */
gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end))
return FALSE;
gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (text_view),
GTK_TEXT_WINDOW_WIDGET,
event->x, event->y, &x, &y);
gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (text_view), &iter, x, y);
/* go through tags */
GSList *tags = NULL, *tagp = NULL;
HistItemData *hist_focus = NULL;
tags = gtk_text_iter_get_tags (&iter);
for (tagp = tags; tagp; tagp = tagp->next) {
hist_focus = g_hash_table_lookup (editor->priv->hash, tagp->data);
if (hist_focus)
break;
}
focus_on_hist_data (editor, hist_focus);
if (tags)
g_slist_free (tags);
}
else if (ev->type == GDK_KEY_PRESS) {
GdkEventKey *evkey = ((GdkEventKey*) ev);
if ((editor->priv->mode == QUERY_EDITOR_HISTORY) &&
((evkey->keyval == GDK_KEY_Up) || (evkey->keyval == GDK_KEY_Down))) {
HistItemData *nfocus = NULL;
if (editor->priv->hist_focus) {
if (((GdkEventKey*) ev)->keyval == GDK_KEY_Up)
nfocus = get_prev_hist_data (editor, editor->priv->hist_focus);
else
nfocus = get_next_hist_data (editor, editor->priv->hist_focus);
if (!nfocus)
nfocus = editor->priv->hist_focus;
}
focus_on_hist_data (editor, nfocus);
return TRUE;
}
else if ((editor->priv->mode == QUERY_EDITOR_HISTORY) &&
((evkey->keyval == GDK_KEY_Delete) && editor->priv->hist_focus)) {
if (editor->priv->hist_focus->item)
query_editor_del_current_history_item (editor);
else if (editor->priv->hist_focus->batch)
query_editor_del_history_batch (editor, editor->priv->hist_focus->batch);
return TRUE;
}
else if ((editor->priv->mode == QUERY_EDITOR_READWRITE) &&
(evkey->state & GDK_CONTROL_MASK) &&
((evkey->keyval == GDK_KEY_L) || (evkey->keyval == GDK_KEY_l))) {
GtkTextIter start, end;
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_delete (buffer, &start, &end);
return TRUE;
}
else if ((editor->priv->mode == QUERY_EDITOR_READWRITE) &&
(evkey->state & GDK_CONTROL_MASK) &&
(evkey->keyval == GDK_KEY_Return)) {
g_signal_emit (editor, query_editor_signals[EXECUTE_REQUEST], 0);
return TRUE;
}
else if ((editor->priv->mode == QUERY_EDITOR_READWRITE) &&
(evkey->state & GDK_CONTROL_MASK) &&
(evkey->keyval == GDK_KEY_Up) &&
editor->priv->states) {
if (editor->priv->states->len > 0) {
gint i = -1;
if (editor->priv->current_state == G_MAXINT) {
i = editor->priv->states->len - 1;
g_free (editor->priv->current_state_text);
editor->priv->current_state_text = query_editor_get_all_text (editor);
}
else if (editor->priv->current_state >= (gint)editor->priv->states->len)
i = editor->priv->states->len - 1; /* last stored state */
else if (editor->priv->current_state > 0)
i = editor->priv->current_state - 1;
gchar *ctext;
ctext = query_editor_get_all_text (editor);
while (i >= 0) {
gchar *tmp;
editor->priv->current_state = i;
tmp = g_array_index (editor->priv->states, gchar*, i);
if (strcmp (tmp, ctext)) {
query_editor_set_text (editor, tmp);
break;
}
i--;
}
g_free (ctext);
}
return TRUE;
}
else if ((editor->priv->mode == QUERY_EDITOR_READWRITE) &&
(evkey->state & GDK_CONTROL_MASK) &&
(evkey->keyval == GDK_KEY_Down) &&
editor->priv->states) {
if (editor->priv->states->len > 0) {
if (editor->priv->current_state < (gint)editor->priv->states->len - 1) {
gchar *tmp;
editor->priv->current_state ++;
tmp = g_array_index (editor->priv->states, gchar*, editor->priv->current_state);
query_editor_set_text (editor, tmp);
}
else if (editor->priv->current_state_text) {
editor->priv->current_state = G_MAXINT;
query_editor_set_text (editor, editor->priv->current_state_text);
g_free (editor->priv->current_state_text);
editor->priv->current_state_text = NULL;
}
}
return TRUE;
}
else if ((editor->priv->mode == QUERY_EDITOR_READWRITE) &&
(evkey->state & GDK_CONTROL_MASK) &&
(evkey->keyval == GDK_KEY_space)) {
display_completions (editor);
return TRUE;
}
}
else
return FALSE;
return FALSE;
}
static gboolean
text_view_draw (GtkTextView *tv, cairo_t *cr, QueryEditor *editor)
{
if (!editor->priv->hist_focus)
return FALSE;
GdkRectangle visible_rect;
GdkRectangle redraw_rect;
GtkTextIter cur;
gint y, ye;
gint height, heighte;
gint win_y;
gint margin;
GtkTextBuffer *tbuffer;
tbuffer = gtk_text_view_get_buffer (tv);
gtk_text_buffer_get_iter_at_mark (tbuffer, &cur, editor->priv->hist_focus->start_mark);
gtk_text_view_get_line_yrange (tv, &cur, &y, &height);
gtk_text_buffer_get_iter_at_mark (tbuffer, &cur, editor->priv->hist_focus->end_mark);
gtk_text_view_get_line_yrange (tv, &cur, &ye, &heighte);
height = ye - y;
if (!editor->priv->hist_focus->item) {
GSList *list;
HistItemData *hdata;
list = g_slist_last (editor->priv->hist_focus->batch->hist_items);
if (list) {
hdata = g_hash_table_lookup (editor->priv->hash, list->data);
gtk_text_buffer_get_iter_at_mark (tbuffer, &cur, hdata->end_mark);
gtk_text_view_get_line_yrange (tv, &cur, &ye, &heighte);
height = ye - y;
}
}
gtk_text_view_get_visible_rect (tv, &visible_rect);
gtk_text_view_buffer_to_window_coords (tv,
GTK_TEXT_WINDOW_TEXT,
visible_rect.x,
visible_rect.y,
&redraw_rect.x,
&redraw_rect.y);
gtk_text_view_buffer_to_window_coords (tv,
GTK_TEXT_WINDOW_TEXT,
0,
y,
NULL,
&win_y);
redraw_rect.width = visible_rect.width;
redraw_rect.height = visible_rect.height;
GdkRectangle rect;
margin = gtk_text_view_get_left_margin (tv);
rect.x = redraw_rect.x + MAX (0, margin - 1);
rect.y = win_y;
rect.width = redraw_rect.width;
rect.height = height;
cairo_set_source_rgba (cr, .5, .5, .5, .3);
gdk_cairo_rectangle (cr, &rect);
cairo_fill (cr);
return FALSE;
}
static void
copy_all_in_signle_line_cb (GtkMenuItem *mitem, QueryEditor *editor)
{
gchar *all, *ptr;
GString *string;
GtkClipboard *clipboard;
all = query_editor_get_all_text (editor);
if (!all)
return;
string = g_string_new ("");
for (ptr = all; *ptr; ptr++) {
if (*ptr == '\n') {
if ((ptr > all) && (ptr[-1] != ' '))
g_string_append_c (string, ' ');
}
else {
if ((*ptr == '-') && (ptr[1] == '-')) {
/* comment => skip till end of line */
for (; *ptr && *ptr != '\n'; ptr++);
}
else
g_string_append_c (string, *ptr);
}
}
g_free (all);
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_text (clipboard, string->str, -1);
g_string_free (string, TRUE);
}
static void
text_view_populate_popup_cb (GtkTextView *entry, GtkMenu *menu, QueryEditor *editor)
{
GtkWidget *mitem;
mitem = gtk_menu_item_new_with_label (_("Copy all in a single line"));
g_signal_connect (G_OBJECT (mitem), "activate", G_CALLBACK (copy_all_in_signle_line_cb),
editor);
gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), mitem);
gtk_widget_show (mitem);
}
static void
query_editor_init (QueryEditor *editor, G_GNUC_UNUSED QueryEditorClass *klass)
{
int tab = 8;
gboolean highlight = TRUE;
gboolean showlinesno = FALSE;
GtkWidget *ovl, *wid;
g_return_if_fail (QUERY_IS_EDITOR (editor));
gtk_orientable_set_orientation (GTK_ORIENTABLE (editor), GTK_ORIENTATION_VERTICAL);
/* allocate private structure */
editor->priv = g_new0 (QueryEditorPrivate, 1);
editor->priv->mode = QUERY_EDITOR_READWRITE;
editor->priv->batches_list = NULL;
editor->priv->hash = NULL;
editor->priv->hist_focus = NULL;
editor->priv->states = NULL;
editor->priv->current_state = G_MAXINT;
editor->priv->current_state_text = NULL;
editor->priv->completion_popup = NULL;
/* set up widgets */
ovl = widget_overlay_new ();
editor->priv->ovl = ovl;
gtk_box_pack_start (GTK_BOX (editor), ovl, TRUE, TRUE, 2);
editor->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (editor->priv->scrolled_window),
GTK_SHADOW_ETCHED_OUT);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (editor->priv->scrolled_window),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (ovl), editor->priv->scrolled_window);
widget_overlay_set_child_props (WIDGET_OVERLAY (ovl), editor->priv->scrolled_window,
WIDGET_OVERLAY_CHILD_HALIGN, WIDGET_OVERLAY_ALIGN_FILL,
WIDGET_OVERLAY_CHILD_VALIGN, WIDGET_OVERLAY_ALIGN_FILL,
-1);
wid = gtk_label_new ("");
editor->priv->tooltip_widget = wid;
gtk_label_set_markup (GTK_LABEL (wid), QUERY_EDITOR_TOOLTIP);
gtk_container_add (GTK_CONTAINER (ovl), wid);
widget_overlay_set_child_props (WIDGET_OVERLAY (ovl), wid,
WIDGET_OVERLAY_CHILD_HALIGN, WIDGET_OVERLAY_ALIGN_CENTER,
WIDGET_OVERLAY_CHILD_VALIGN, WIDGET_OVERLAY_ALIGN_CENTER,
WIDGET_OVERLAY_CHILD_ALPHA, .8,
WIDGET_OVERLAY_CHILD_TOOLTIP, TRUE,
-1);
#ifdef HAVE_GTKSOURCEVIEW
editor->priv->text = gtk_source_view_new ();
gtk_source_buffer_set_highlight_syntax (GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text))),
highlight);
gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW (editor->priv->text), showlinesno);
gtk_source_view_set_tab_width (GTK_SOURCE_VIEW (editor->priv->text), tab);
#else
editor->priv->text = gtk_text_view_new ();
#endif
gtk_container_add (GTK_CONTAINER (editor->priv->scrolled_window), editor->priv->text);
g_signal_connect (editor->priv->text, "event",
G_CALLBACK (event), editor);
g_signal_connect (gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text)), "changed",
G_CALLBACK (text_buffer_changed_cb), editor);
g_signal_connect (editor->priv->text, "draw",
G_CALLBACK (text_view_draw), editor);
g_signal_connect (editor->priv->text, "populate-popup",
G_CALLBACK (text_view_populate_popup_cb), editor);
/* create some tags */
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
gtk_text_buffer_create_tag (buffer, "h0",
"foreground", "#474A8F",
"weight", PANGO_WEIGHT_BOLD,
"variant", PANGO_VARIANT_SMALL_CAPS,
"scale", PANGO_SCALE_LARGE,
"underline", PANGO_UNDERLINE_SINGLE,
NULL);
gtk_text_buffer_create_tag (buffer, "note",
"left-margin", 50,
"foreground", "black",
"weight", PANGO_WEIGHT_NORMAL,
"style", PANGO_STYLE_ITALIC,
NULL);
/* initialize common data */
number_of_objects++;
if (!supported_languages) {
supported_languages = g_hash_table_new (g_str_hash, g_str_equal);
/* add the built-in languages */
g_hash_table_insert (supported_languages, QUERY_EDITOR_LANGUAGE_SQL, create_tags_for_sql);
}
create_tags_for_sql (editor, QUERY_EDITOR_LANGUAGE_SQL);
gtk_widget_show_all (ovl);
/* timeout function to update timestamps */
editor->priv->ts_timeout_id = 0;
}
static void
query_editor_map (GtkWidget *widget)
{
GTK_WIDGET_CLASS (parent_class)->map (widget);
if (QUERY_EDITOR (widget)->priv->mode == QUERY_EDITOR_HISTORY) {
GtkStyleContext* style_context = gtk_widget_get_style_context (widget);
GdkRGBA color;
gtk_style_context_get_background_color (style_context, GTK_STATE_FLAG_NORMAL, &color);
color.red += (1.0 - color.red) / COLOR_ALTER_FACTOR;
color.green += (1.0 - color.green) / COLOR_ALTER_FACTOR;
color.blue += (1.0 - color.blue) / COLOR_ALTER_FACTOR;
gtk_widget_override_background_color (QUERY_EDITOR (widget)->priv->text, GTK_STATE_FLAG_NORMAL, &color);
}
}
static void
query_editor_grab_focus (GtkWidget *widget)
{
gtk_widget_grab_focus (QUERY_EDITOR (widget)->priv->text);
}
static void
hist_data_free_all (QueryEditor *editor)
{
if (editor->priv->hist_focus) {
hist_item_data_unref (editor->priv->hist_focus);
editor->priv->hist_focus = NULL;
}
if (editor->priv->ts_timeout_id) {
g_source_remove (editor->priv->ts_timeout_id);
editor->priv->ts_timeout_id = 0;
}
if (editor->priv->hash) {
g_hash_table_destroy (editor->priv->hash);
editor->priv->hash = NULL;
}
if (editor->priv->insert_into_batch) {
query_editor_history_batch_unref (editor->priv->insert_into_batch);
editor->priv->insert_into_batch = NULL;
}
if (editor->priv->batches_list) {
g_slist_foreach (editor->priv->batches_list, (GFunc) query_editor_history_batch_unref, NULL);
g_slist_free (editor->priv->batches_list);
editor->priv->batches_list = NULL;
}
}
static void
query_editor_finalize (GObject *object)
{
QueryEditor *editor = (QueryEditor *) object;
g_return_if_fail (QUERY_IS_EDITOR (editor));
/* free memory */
hist_data_free_all (editor);
if (editor->priv->states) {
gsize i;
for (i = 0; i < editor->priv->states->len; i++) {
gchar *str;
str = g_array_index (editor->priv->states, gchar*, i);
g_free (str);
}
g_array_free (editor->priv->states, TRUE);
}
g_free (editor->priv->current_state_text);
if (editor->priv->completion_popup)
gtk_widget_destroy (editor->priv->completion_popup);
g_free (editor->priv);
editor->priv = NULL;
parent_class->finalize (object);
/* update common data */
number_of_objects--;
if (number_of_objects == 0) {
g_hash_table_destroy (supported_languages);
supported_languages = NULL;
}
}
GType
query_editor_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static const GTypeInfo info = {
sizeof (QueryEditorClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) query_editor_class_init,
NULL,
NULL,
sizeof (QueryEditor),
0,
(GInstanceInitFunc) query_editor_init,
0
};
type = g_type_register_static (GTK_TYPE_BOX, "QueryEditor", &info, 0);
}
return type;
}
/**
* query_editor_new
*
* Create a new #QueryEditor widget, which is a multiline text widget
* with support for several languages used to write database stored
* procedures and functions. If libgnomedb was compiled with gtksourceview
* in, this widget will support syntax highlighting for all supported
* languages.
*
* Returns: the newly created widget.
*/
GtkWidget *
query_editor_new (void)
{
QueryEditor *editor;
editor = g_object_new (QUERY_TYPE_EDITOR, NULL);
return GTK_WIDGET (editor);
}
/**
* query_editor_get_mode
* @editor: a #QueryEditor widget.
*
* Get @editor's mode
*
* Returns: @editor's mode
*/
QueryEditorMode
query_editor_get_mode (QueryEditor *editor)
{
g_return_val_if_fail (QUERY_IS_EDITOR (editor), 0);
return editor->priv->mode;
}
/**
* query_editor_set_mode
* @editor: a #QueryEditor widget.
* @mode: new mode
*
* A mode change will empty the buffer.
*/
void
query_editor_set_mode (QueryEditor *editor, QueryEditorMode mode)
{
GtkTextBuffer *buffer;
gboolean clean = TRUE;
g_return_if_fail (QUERY_IS_EDITOR (editor));
if (editor->priv->mode == mode)
return;
if (((editor->priv->mode == QUERY_EDITOR_READWRITE) && (mode == QUERY_EDITOR_READONLY)) ||
((editor->priv->mode == QUERY_EDITOR_READONLY) && (mode == QUERY_EDITOR_READWRITE)))
clean = FALSE;
else if (editor->priv->mode == QUERY_EDITOR_HISTORY)
hist_data_free_all (editor);
editor->priv->mode = mode;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
if (clean) {
GtkTextIter start, end;
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_delete (buffer, &start, &end);
}
switch (mode) {
case QUERY_EDITOR_READWRITE:
gtk_widget_set_tooltip_markup (editor->priv->text, QUERY_EDITOR_TOOLTIP);
gtk_text_view_set_editable (GTK_TEXT_VIEW (editor->priv->text), TRUE);
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (editor->priv->text), TRUE);
break;
case QUERY_EDITOR_READONLY:
case QUERY_EDITOR_HISTORY:
gtk_widget_set_tooltip_markup (editor->priv->text, NULL);
gtk_text_view_set_editable (GTK_TEXT_VIEW (editor->priv->text), FALSE);
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (editor->priv->text), FALSE);
gtk_widget_destroy (editor->priv->tooltip_widget);
editor->priv->tooltip_widget = NULL;
break;
default:
g_assert_not_reached ();
}
if (mode == QUERY_EDITOR_HISTORY) {
GtkStyleContext *style_context = gtk_widget_get_style_context (GTK_WIDGET (editor));
GdkRGBA color;
gtk_style_context_get_background_color (style_context, GTK_STATE_FLAG_NORMAL, &color);
color.red += (1.0 - color.red) / COLOR_ALTER_FACTOR;
color.green += (1.0 - color.green) / COLOR_ALTER_FACTOR;
color.blue += (1.0 - color.blue) / COLOR_ALTER_FACTOR;
gtk_widget_override_background_color (editor->priv->text, GTK_STATE_FLAG_NORMAL, &color);
editor->priv->hash = g_hash_table_new_full (NULL, NULL, NULL,
(GDestroyNotify) hist_item_data_unref);
}
else {
gtk_widget_override_background_color (editor->priv->text,
GTK_STATE_FLAG_NORMAL, NULL);
}
}
/**
* query_editor_set_text
* @editor: a #QueryEditor widget.
* @text: text to display in the editor.
*
* Set @editor's text, removing any previous one
*/
void
query_editor_set_text (QueryEditor *editor, const gchar *text)
{
GtkTextBuffer *buffer;
GtkTextIter start, end;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode != QUERY_EDITOR_HISTORY);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_delete (buffer, &start, &end);
if (text) {
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_insert (buffer, &end, text, -1);
}
}
/**
* query_editor_append_text
* @editor: a #QueryEditor widget.
* @text: text to display in the editor.
*
* Appends some text to @editor.
*/
void
query_editor_append_text (QueryEditor *editor, const gchar *text)
{
GtkTextBuffer *buffer;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode != QUERY_EDITOR_HISTORY);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
if (text) {
GtkTextIter end;
gint len;
len = strlen (text);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_insert (buffer, &end, text, -1);
if ((len >= 1) && (text [len - 1] != '\n'))
gtk_text_buffer_insert (buffer, &end, "\n", 1);
}
}
/**
* query_editor_keep_current_state
* @editor: #QueryEditor widget.
*
* Makes @editor keep the current text as a past state, which can be brought back
* using CTRL-Up or Gtrl-Down
*/
void
query_editor_keep_current_state (QueryEditor *editor)
{
gchar *text, *tmp;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode != QUERY_EDITOR_HISTORY);
if (!editor->priv->states)
editor->priv->states = g_array_sized_new (FALSE, FALSE, sizeof (gchar*), STATES_ARRAY_SIZE);
editor->priv->current_state = G_MAXINT;
text = query_editor_get_all_text (editor);
if (editor->priv->states->len > 0) {
tmp = g_array_index (editor->priv->states, gchar *, editor->priv->states->len-1);
if (!strcmp (tmp, text)) {
g_free (text);
return;
}
}
if (editor->priv->states->len == STATES_ARRAY_SIZE) {
tmp = g_array_index (editor->priv->states, gchar *, 0);
g_free (tmp);
g_array_remove_index (editor->priv->states, 0);
}
g_array_append_val (editor->priv->states, text);
}
/**
* query_editor_append_note
* @editor: a #QueryEditor widget.
* @text: text to display in the editor.
* @level: 0 for header, 1 for text with marging and in italics
*
* Appends some text to @editor.
*/
void
query_editor_append_note (QueryEditor *editor, const gchar *text, gint level)
{
GtkTextBuffer *buffer;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode != QUERY_EDITOR_HISTORY);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
if (text) {
GtkTextIter end;
gchar *str;
switch (level) {
case 0:
str = g_strdup_printf ("%s\n", text);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_insert_with_tags_by_name (buffer, &end,
str, -1,
"h0", NULL);
g_free (str);
break;
case 1:
str = g_strdup_printf ("%s\n", text);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_insert_with_tags_by_name (buffer, &end,
str, -1,
"note", NULL);
g_free (str);
break;
default:
g_assert_not_reached ();
}
}
}
/**
* query_editor_show_tooltip
* @editor: a #QueryEditor
* @show_tooltip:
*
* Defines if tooltip can be shown or not
*/
void
query_editor_show_tooltip (QueryEditor *editor, gboolean show_tooltip)
{
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode == QUERY_EDITOR_READWRITE);
if (show_tooltip)
gtk_widget_set_tooltip_markup (editor->priv->text, QUERY_EDITOR_TOOLTIP);
else
gtk_widget_set_tooltip_markup (editor->priv->text, NULL);
}
static void
focus_on_hist_data (QueryEditor *editor, HistItemData *hdata)
{
if (editor->priv->hist_focus) {
if (editor->priv->hist_focus == hdata)
return;
if (editor->priv->hist_focus->item)
g_object_set (G_OBJECT (editor->priv->hist_focus->tag),
"foreground-set", TRUE, NULL);
else {
/* un-highlight all the batch */
GSList *list;
for (list = editor->priv->hist_focus->batch->hist_items; list; list = list->next) {
HistItemData *hd;
hd = g_hash_table_lookup (editor->priv->hash, list->data);
g_object_set (G_OBJECT (hd->tag),
"foreground-set", TRUE, NULL);
}
}
hist_item_data_unref (editor->priv->hist_focus);
editor->priv->hist_focus = NULL;
}
if (hdata) {
GtkTextBuffer *buffer;
GtkTextIter iter;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
if (editor->priv->hist_focus)
hist_item_data_unref (editor->priv->hist_focus);
editor->priv->hist_focus = hist_item_data_ref (hdata);
if (hdata->item)
g_object_set (G_OBJECT (hdata->tag),
"foreground-set", FALSE, NULL);
else {
/* highlight all the batch */
GSList *list;
for (list = hdata->batch->hist_items; list; list = list->next) {
HistItemData *hd;
hd = g_hash_table_lookup (editor->priv->hash, list->data);
g_object_set (G_OBJECT (hd->tag),
"foreground-set", FALSE, NULL);
}
}
gtk_text_buffer_get_iter_at_mark (buffer, &iter, hdata->start_mark);
gtk_text_buffer_place_cursor (buffer, &iter);
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (editor->priv->text), hdata->start_mark);
}
g_signal_emit (editor, query_editor_signals[CHANGED], 0);
}
const char *
get_date_format (time_t time)
{
static char timebuf[200];
GTimeVal now;
unsigned long diff, tmp;
g_get_current_time (&now);
if (now.tv_sec < time)
return _("In the future:\n");
diff = now.tv_sec - time;
if (diff < 60)
return _("Less than a minute ago:\n");
/* Turn it into minutes */
tmp = diff / 60;
if (tmp < 60) {
snprintf (timebuf, sizeof(timebuf), ngettext ("%lu minute ago:\n",
"%lu minutes ago:\n", tmp), tmp);
return timebuf;
}
/* Turn it into hours */
tmp = diff / 3600;
if (tmp < 24) {
snprintf (timebuf, sizeof(timebuf), ngettext ("%lu hour ago\n",
"%lu hours ago\n", tmp), tmp);
return timebuf;
}
/* Turn it into days */
tmp = diff / 86400;
snprintf (timebuf, sizeof(timebuf), ngettext ("%lu day ago\n",
"%lu days ago\n", tmp), tmp);
return timebuf;
}
/**
* query_editor_start_history_batch
* @editor: a #QueryEditor widget.
* @hist_batch: (allow-none): a #QueryEditorHistoryBatch to add, or %NULL
*
* @hist_hash ref is _NOT_ stolen here
*/
void
query_editor_start_history_batch (QueryEditor *editor, QueryEditorHistoryBatch *hist_batch)
{
GtkTextIter iter;
GtkTextBuffer *buffer;
GtkTextTag *tag;
HistItemData *hdata;
gboolean empty = FALSE;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY);
if (!hist_batch) {
GTimeVal run_time = {0, 0};
empty = TRUE;
hist_batch = query_editor_history_batch_new (run_time, NULL);
}
else {
if (g_slist_find (editor->priv->batches_list, hist_batch))
return;
}
/* update editor->priv->insert_into_batch */
if (editor->priv->insert_into_batch)
query_editor_history_batch_unref (editor->priv->insert_into_batch);
editor->priv->insert_into_batch = query_editor_history_batch_ref (hist_batch);
editor->priv->batches_list = g_slist_prepend (editor->priv->batches_list,
query_editor_history_batch_ref (hist_batch));
/* new HistItemData */
hdata = hist_item_data_new ();
hdata->batch = query_editor_history_batch_ref (hist_batch);
hdata->item = NULL;
g_hash_table_insert (editor->priv->hash, hist_batch, hdata);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
gtk_text_buffer_get_end_iter (buffer, &iter);
/* mark start of insertion */
hdata->start_mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, TRUE);
/* new tag */
tag = gtk_text_buffer_create_tag (buffer, NULL,
"pixels-below-lines", 3,
"foreground", "black",
"scale", PANGO_SCALE_SMALL,
"weight", PANGO_WEIGHT_BOLD, NULL);
hdata->tag = g_object_ref (tag);
g_hash_table_insert (editor->priv->hash, tag, hist_item_data_ref (hdata));
if (!empty) {
/* insert text */
gtk_text_buffer_insert_with_tags (buffer, &iter,
get_date_format (hist_batch->run_time.tv_sec),
-1, tag, NULL);
}
/* mark end of insertion */
hdata->end_mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, TRUE);
if (empty)
query_editor_history_batch_unref (hist_batch);
/* add timout to 1 sec. */
if (editor->priv->ts_timeout_id == 0)
editor->priv->ts_timeout_id = g_timeout_add_seconds (60,
(GSourceFunc) timestamps_update_cb,
editor);
/* remove too old batches */
if (g_slist_length (editor->priv->batches_list) > MAX_HISTORY_BATCH_ITEMS) {
QueryEditorHistoryBatch *obatch;
obatch = g_slist_last (editor->priv->batches_list)->data;
query_editor_del_history_batch (editor, obatch);
}
}
static gboolean
timestamps_update_cb (QueryEditor *editor)
{
GSList *list;
GtkTextBuffer *buffer;
GtkTextIter start, end;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
for (list = editor->priv->batches_list; list; list = list->next) {
QueryEditorHistoryBatch *batch;
batch = (QueryEditorHistoryBatch*) list->data;
if (batch->run_time.tv_sec != 0) {
HistItemData *hdata;
hdata = g_hash_table_lookup (editor->priv->hash, batch);
/* delete current text */
gtk_text_buffer_get_iter_at_mark (buffer, &start, hdata->start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, &end, hdata->end_mark);
gtk_text_buffer_delete (buffer, &start, &end);
/* insert text */
gtk_text_buffer_get_iter_at_mark (buffer, &start, hdata->start_mark);
gtk_text_buffer_insert_with_tags (buffer, &start,
get_date_format (batch->run_time.tv_sec),
-1, hdata->tag, NULL);
gtk_text_buffer_delete_mark (buffer, hdata->end_mark);
hdata->end_mark = gtk_text_buffer_create_mark (buffer, NULL, &start, TRUE);
}
}
return TRUE; /* don't remove timeout */
}
/**
* query_editor_add_history_item
* @editor: a #QueryEditor widget.
* @hist_item: (allow-none): a #QueryEditorHistoryItem to add, or %NULL
*
* Adds some text. @text_data is useful only if @editor's mode is HISTORY, it will be ignored
* otherwise.
*
* Returns: the position of the added text chunk, or %0 if mode is not HISTORY
*/
void
query_editor_add_history_item (QueryEditor *editor, QueryEditorHistoryItem *hist_item)
{
GtkTextIter iter;
GtkTextBuffer *buffer;
GtkTextTag *tag;
HistItemData *hdata;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY);
g_return_if_fail (hist_item);
g_return_if_fail (hist_item->sql);
/* new HistItemData */
hdata = hist_item_data_new ();
hdata->batch = NULL;
hdata->item = query_editor_history_item_ref (hist_item);
g_hash_table_insert (editor->priv->hash, hist_item, hdata);
/* remove leading and trailing spaces */
g_strstrip (hist_item->sql);
if (!editor->priv->insert_into_batch)
query_editor_start_history_batch (editor, NULL);
query_editor_history_batch_add_item (editor->priv->insert_into_batch, hist_item);
hdata->batch = query_editor_history_batch_ref (editor->priv->insert_into_batch);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
gtk_text_buffer_get_end_iter (buffer, &iter);
tag = gtk_text_buffer_create_tag (buffer, NULL,
"scale", 0.75,
"foreground", "gray",
"foreground-set", TRUE, NULL);
if (hist_item->within_transaction)
g_object_set (G_OBJECT (tag), "left-margin", 15, NULL);
hdata->tag = g_object_ref (tag);
g_hash_table_insert (editor->priv->hash, tag, hist_item_data_ref (hdata));
/* mark start of insertion */
hdata->start_mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, TRUE);
/* insert text */
gchar *sql, *tmp1, *tmp2;
sql = g_strdup (hist_item->sql);
for (tmp1 = sql; (*tmp1 == ' ') || (*tmp1 == '\n') || (*tmp1 == '\t'); tmp1 ++);
for (tmp2 = sql + (strlen (sql) - 1);
(tmp2 > tmp1) && ((*tmp2 == ' ') || (*tmp2 == '\n') || (*tmp2 == '\t'));
tmp2--)
*tmp2 = 0;
gtk_text_buffer_insert_with_tags (buffer, &iter, tmp1, -1, tag, NULL);
gtk_text_buffer_insert_with_tags (buffer, &iter, "\n", 1, tag, NULL);
g_free (sql);
/* mark end of insertion */
gtk_text_buffer_get_end_iter (buffer, &iter);
hdata->end_mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, TRUE);
focus_on_hist_data (editor, hdata);
gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (editor->priv->text),
&iter, 0., FALSE, 0., 0.);
}
/**
* query_editor_get_current_history_item
* @editor: a #QueryEditor widget.
* @out_in_batch: (allow-none): a pointer to store the #QueryEditorHistoryBatch the returned item is in, or %NULL
*
* Get the current selected #QueryEditorHistoryItem
* passed to query_editor_add_history_item().
*
* Returns: a #QueryEditorHistoryItem pointer, or %NULL
*/
QueryEditorHistoryItem *
query_editor_get_current_history_item (QueryEditor *editor, QueryEditorHistoryBatch **out_in_batch)
{
g_return_val_if_fail (QUERY_IS_EDITOR (editor), NULL);
g_return_val_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY, NULL);
if (out_in_batch)
*out_in_batch = NULL;
if (editor->priv->hist_focus) {
if (out_in_batch)
*out_in_batch = editor->priv->hist_focus->batch;
return editor->priv->hist_focus->item;
}
else
return NULL;
}
/**
* query_editor_get_current_history_batch
* @editor: a #QueryEditor widget.
*
* Get the current selected #QueryEditorHistoryBatch if the selection is not on
* a #QueryEditorHistoryItem, but on the #QueryEditorHistoryBatch which was last
* set by a call to query_editor_start_history_batch().
*
* Returns: a #QueryEditorHistoryBatch pointer, or %NULL
*/
QueryEditorHistoryBatch *
query_editor_get_current_history_batch (QueryEditor *editor)
{
g_return_val_if_fail (QUERY_IS_EDITOR (editor), NULL);
g_return_val_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY, NULL);
if (editor->priv->hist_focus && !editor->priv->hist_focus->item)
return editor->priv->hist_focus->batch;
else
return NULL;
}
/**
* query_editor_history_is_empty
* @editor: a #QueryEditor widget.
*
* Returns: %TRUE if @editor does not have any history item
*/
gboolean
query_editor_history_is_empty (QueryEditor *editor)
{
g_return_val_if_fail (QUERY_IS_EDITOR (editor), FALSE);
g_return_val_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY, FALSE);
return editor->priv->batches_list ? FALSE : TRUE;
}
/**
* query_editor_del_current_history_item
* @editor: a #QueryEditor widget.
*
* Deletes the text associated to the current selection, useful only if @editor's mode is HISTORY
*/
void
query_editor_del_current_history_item (QueryEditor *editor)
{
HistItemData *hdata, *focus;
GtkTextBuffer *buffer;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY);
if (!editor->priv->hist_focus)
return;
hdata = editor->priv->hist_focus;
if (!hdata->item)
return;
focus = get_next_hist_data (editor, hdata);
if (focus)
focus_on_hist_data (editor, focus);
else
focus_on_hist_data (editor, get_prev_hist_data (editor, hdata));
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
/* handle GtkTextBuffer's deletion */
GtkTextIter start, end;
gtk_text_buffer_get_iter_at_mark (buffer, &start, hdata->start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, &end, hdata->end_mark);
gtk_text_buffer_delete (buffer, &start, &end);
gtk_text_buffer_delete_mark (buffer, hdata->start_mark);
gtk_text_buffer_delete_mark (buffer, hdata->end_mark);
hist_item_data_ref (hdata);
g_hash_table_remove (editor->priv->hash, hdata->item);
g_hash_table_remove (editor->priv->hash, hdata->tag);
g_assert (hdata->batch);
query_editor_history_batch_del_item (editor, hdata->batch, hdata->item);
if (! hdata->batch->hist_items) {
/* remove hdata->batch */
HistItemData *remhdata;
editor->priv->batches_list = g_slist_remove (editor->priv->batches_list, hdata->batch);
query_editor_history_batch_unref (hdata->batch);
remhdata = g_hash_table_lookup (editor->priv->hash, hdata->batch);
gtk_text_buffer_get_iter_at_mark (buffer, &start, remhdata->start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, &end, remhdata->end_mark);
gtk_text_buffer_delete (buffer, &start, &end);
gtk_text_buffer_delete_mark (buffer, remhdata->start_mark);
gtk_text_buffer_delete_mark (buffer, remhdata->end_mark);
g_hash_table_remove (editor->priv->hash, remhdata->batch);
g_hash_table_remove (editor->priv->hash, remhdata->tag);
if (editor->priv->insert_into_batch == hdata->batch) {
query_editor_history_batch_unref (editor->priv->insert_into_batch);
editor->priv->insert_into_batch = NULL;
}
}
hist_item_data_unref (hdata);
}
/**
* query_editor_del_all_history_items
* @editor: a #QueryEditor
*
* Clears all the history
*/
void
query_editor_del_all_history_items (QueryEditor *editor)
{
GtkTextBuffer *buffer;
GtkTextIter start, end;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
hist_data_free_all (editor);
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_delete (buffer, &start, &end);
editor->priv->hash = g_hash_table_new_full (NULL, NULL, NULL,
(GDestroyNotify) hist_item_data_unref);
g_signal_emit (editor, query_editor_signals[CHANGED], 0);
g_signal_emit (editor, query_editor_signals[HISTORY_CLEARED], 0);
}
/**
* query_editor_del_history_batch
* @editor: a #QueryEditor
* @batch: a #QueryEditorHistoryBatch
*
* Deletes all regarding @batch.
*/
void
query_editor_del_history_batch (QueryEditor *editor, QueryEditorHistoryBatch *batch)
{
GtkTextBuffer *buffer;
GSList *list;
HistItemData *hdata;
GtkTextIter start, end;
HistItemData *focus;
gint i;
g_return_if_fail (QUERY_IS_EDITOR (editor));
g_return_if_fail (editor->priv->mode == QUERY_EDITOR_HISTORY);
g_return_if_fail (batch);
i = g_slist_index (editor->priv->batches_list, batch);
g_return_if_fail (i >= 0);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
/* compute new focus */
focus = NULL;
if (i > 0) {
list = g_slist_nth (editor->priv->batches_list, i - 1);
focus = g_hash_table_lookup (editor->priv->hash, list->data);
}
focus_on_hist_data (editor, focus);
/* remove all history items */
for (list = batch->hist_items; list; list = batch->hist_items) {
hdata = g_hash_table_lookup (editor->priv->hash, list->data);
g_assert (hdata);
gtk_text_buffer_get_iter_at_mark (buffer, &start, hdata->start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, &end, hdata->end_mark);
gtk_text_buffer_delete (buffer, &start, &end);
gtk_text_buffer_delete_mark (buffer, hdata->start_mark);
gtk_text_buffer_delete_mark (buffer, hdata->end_mark);
g_hash_table_remove (editor->priv->hash, hdata->item);
g_hash_table_remove (editor->priv->hash, hdata->tag);
query_editor_history_batch_del_item (editor, batch, (QueryEditorHistoryItem*) list->data);
}
/* remove batch */
editor->priv->batches_list = g_slist_remove (editor->priv->batches_list, batch);
query_editor_history_batch_unref (batch);
hdata = g_hash_table_lookup (editor->priv->hash, batch);
gtk_text_buffer_get_iter_at_mark (buffer, &start, hdata->start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, &end, hdata->end_mark);
gtk_text_buffer_delete (buffer, &start, &end);
gtk_text_buffer_delete_mark (buffer, hdata->start_mark);
gtk_text_buffer_delete_mark (buffer, hdata->end_mark);
if (editor->priv->insert_into_batch == batch) {
query_editor_history_batch_unref (editor->priv->insert_into_batch);
editor->priv->insert_into_batch = NULL;
}
g_hash_table_remove (editor->priv->hash, hdata->batch);
g_hash_table_remove (editor->priv->hash, hdata->tag);
}
static HistItemData *
get_next_hist_data (QueryEditor *editor, HistItemData *hdata)
{
GSList *node;
g_return_val_if_fail (hdata, NULL);
g_assert (hdata->batch);
if (hdata->item) {
node = g_slist_find (hdata->batch->hist_items, hdata->item);
g_assert (node);
node = node->next;
if (node)
return g_hash_table_lookup (editor->priv->hash, node->data);
}
else
if (hdata->batch->hist_items)
return g_hash_table_lookup (editor->priv->hash, hdata->batch->hist_items->data);
/* move on to the next batch if any */
gint i;
i = g_slist_index (editor->priv->batches_list, hdata->batch);
if (i > 0) {
node = g_slist_nth (editor->priv->batches_list, i-1);
hdata = g_hash_table_lookup (editor->priv->hash, node->data);
return get_next_hist_data (editor, hdata);
}
return NULL;
}
static HistItemData *
get_prev_hist_data (QueryEditor *editor, HistItemData *hdata)
{
GSList *node;
gint i;
g_return_val_if_fail (hdata, NULL);
g_assert (hdata->batch);
if (hdata->item) {
node = g_slist_find (hdata->batch->hist_items, hdata->item);
g_assert (node);
i = g_slist_position (hdata->batch->hist_items, node);
if (i > 0) {
node = g_slist_nth (hdata->batch->hist_items, i - 1);
return g_hash_table_lookup (editor->priv->hash, node->data);
}
}
/* move to the previous batch, if any */
node = g_slist_find (editor->priv->batches_list, hdata->batch);
node = node->next;
while (node) {
QueryEditorHistoryBatch *b;
b = (QueryEditorHistoryBatch*) node->data;
GSList *l;
l = g_slist_last (b->hist_items);
if (l)
return g_hash_table_lookup (editor->priv->hash, l->data);
node = node->next;
}
return NULL;
}
/**
* query_editor_get_all_text
* @editor: a #QueryEditor widget.
*
* Retrieve the full contents of the given editor widget.
*
* Returns: the current contents of the editor buffer. You must free
* the returned value when no longer needed.
*/
gchar *
query_editor_get_all_text (QueryEditor *editor)
{
g_return_val_if_fail (QUERY_IS_EDITOR (editor), NULL);
GtkTextBuffer *buffer;
GtkTextIter start;
GtkTextIter end;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text));
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
return gtk_text_buffer_get_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text)),
&start, &end, FALSE);
}
/**
* query_editor_load_from_file
* @editor: a #QueryEditor widget.
* @filename: the file to be loaded.
*
* Load the given filename into the editor widget.
*
* Returns: TRUE if successful, FALSE otherwise.
*/
gboolean
query_editor_load_from_file (QueryEditor *editor, const gchar *filename)
{
gboolean retval = TRUE;
gchar *contents;
g_return_val_if_fail (QUERY_IS_EDITOR (editor), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
query_editor_set_text (editor, contents);
g_free (contents);
}
else
retval = FALSE;
return retval;
}
/**
* query_editor_save_to_file
* @editor: a #QueryEditor widget.
* @filename: the file to save to.
*
* Save the current editor contents to the given file.
*
* Returns: TRUE if successful, FALSE otherwise.
*/
gboolean
query_editor_save_to_file (QueryEditor *editor, const gchar *filename)
{
gchar *contents;
gboolean retval = TRUE;
g_return_val_if_fail (QUERY_IS_EDITOR (editor), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
contents = query_editor_get_all_text (editor);
if (!g_file_set_contents (filename, contents, strlen (contents), NULL))
retval = FALSE;
g_free (contents);
return retval;
}
/**
* query_editor_copy_clipboard
* @editor: a #QueryEditor widget.
*
* Copy currently selected text in the given editor widget to the clipboard.
*/
void
query_editor_copy_clipboard (QueryEditor *editor)
{
g_return_if_fail (QUERY_IS_EDITOR (editor));
gtk_text_buffer_copy_clipboard (gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text)),
gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
}
/**
* query_editor_cut_clipboard
* @editor: a #QueryEditor widget.
*
* Moves currently selected text in the given editor widget to the clipboard.
*/
void
query_editor_cut_clipboard (QueryEditor *editor)
{
g_return_if_fail (QUERY_IS_EDITOR (editor));
gtk_text_buffer_cut_clipboard (gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text)),
gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
gtk_text_view_get_editable (GTK_TEXT_VIEW (editor->priv->text)));
}
/**
* query_editor_paste_clipboard
* @editor: a #QueryEditor widget.
*
* Paste clipboard contents into editor widget, at the current position.
*/
void
query_editor_paste_clipboard (QueryEditor *editor)
{
g_return_if_fail (QUERY_IS_EDITOR (editor));
gtk_text_buffer_paste_clipboard (gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->priv->text)),
gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
NULL,
gtk_text_view_get_editable (GTK_TEXT_VIEW (editor->priv->text)));
}
/*
* QueryEditorHistoryBatch
*/
QueryEditorHistoryBatch *
query_editor_history_batch_new (GTimeVal run_time, GdaSet *params)
{
QueryEditorHistoryBatch *qib;
qib = g_new0 (QueryEditorHistoryBatch, 1);
qib->ref_count = 1;
qib->run_time = run_time;
if (params)
qib->params = gda_set_copy (params);
#ifdef QUERY_EDITOR_DEBUG
g_print ("\t%s() => %p\n", __FUNCTION__, qib);
#endif
return qib;
}
QueryEditorHistoryBatch *
query_editor_history_batch_ref (QueryEditorHistoryBatch *qib)
{
g_return_val_if_fail (qib, NULL);
qib->ref_count++;
#ifdef QUERY_EDITOR_DEBUG
g_print ("%s (%p) ref count:%d\n", __FUNCTION__, qib, qib->ref_count);
#endif
return qib;
}
void
query_editor_history_batch_unref (QueryEditorHistoryBatch *qib)
{
g_return_if_fail (qib);
qib->ref_count--;
#ifdef QUERY_EDITOR_DEBUG
g_print ("%s (%p) ref count:%d\n", __FUNCTION__, qib, qib->ref_count);
#endif
if (qib->ref_count <= 0) {
if (qib->hist_items) {
g_slist_foreach (qib->hist_items, (GFunc) query_editor_history_item_unref, NULL);
g_slist_free (qib->hist_items);
}
if (qib->params)
g_object_unref (qib->params);
g_free (qib);
#ifdef QUERY_EDITOR_DEBUG
g_print ("\t%s() => %p\n", __FUNCTION__, qib);
#endif
}
#ifdef QUERY_EDITOR_DEBUG
else
g_print ("\tFAILED %s() => %p:%d\n", __FUNCTION__, qib, qib->ref_count);
#endif
}
static void
query_editor_history_batch_add_item (QueryEditorHistoryBatch *qib, QueryEditorHistoryItem *qih)
{
g_return_if_fail (qib);
g_return_if_fail (qih);
qib->hist_items = g_slist_append (qib->hist_items, query_editor_history_item_ref (qih));
}
static void
query_editor_history_batch_del_item (QueryEditor *editor, QueryEditorHistoryBatch *qib, QueryEditorHistoryItem *qih)
{
g_return_if_fail (qib);
g_return_if_fail (qih);
qib->hist_items = g_slist_remove (qib->hist_items, qih);
g_signal_emit (editor, query_editor_signals[HISTORY_ITEM_REMOVED], 0, qih);
query_editor_history_item_unref (qih);
}
/*
* QueryEditorHistoryItem
*/
QueryEditorHistoryItem *
query_editor_history_item_new (const gchar *sql, GObject *result, GError *error)
{
QueryEditorHistoryItem *qih;
g_return_val_if_fail (sql, NULL);
qih = g_new0 (QueryEditorHistoryItem, 1);
qih->ref_count = 1;
qih->sql = g_strdup (sql);
if (result)
qih->result = g_object_ref (result);
if (error)
qih->exec_error = g_error_copy (error);
#ifdef QUERY_EDITOR_DEBUG
g_print ("\t%s() => %p\n", __FUNCTION__, qih);
#endif
return qih;
}
QueryEditorHistoryItem *
query_editor_history_item_ref (QueryEditorHistoryItem *qih)
{
g_return_val_if_fail (qih, NULL);
qih->ref_count++;
#ifdef QUERY_EDITOR_DEBUG
g_print ("%s (%p) ref count:%d\n", __FUNCTION__, qih, qih->ref_count);
#endif
return qih;
}
void
query_editor_history_item_unref (QueryEditorHistoryItem *qih)
{
g_return_if_fail (qih);
qih->ref_count--;
#ifdef QUERY_EDITOR_DEBUG
g_print ("%s (%p) ref count:%d\n", __FUNCTION__, qih, qih->ref_count);
#endif
if (qih->ref_count <= 0) {
g_free (qih->sql);
if (qih->result)
g_object_unref (qih->result);
if (qih->exec_error)
g_error_free (qih->exec_error);
g_free (qih);
#ifdef QUERY_EDITOR_DEBUG
g_print ("\t%s() => %p\n", __FUNCTION__, qih);
#endif
}
#ifdef QUERY_EDITOR_DEBUG
else
g_print ("\tFAILED %s() => %p:%d\n", __FUNCTION__, qih, qih->ref_count);
#endif
}
/*
* HistItemData
*/
static HistItemData *
hist_item_data_new (void)
{
HistItemData *hdata;
hdata = g_new0 (HistItemData, 1);
hdata->ref_count = 1;
return hdata;
}
static HistItemData *
hist_item_data_ref (HistItemData *hdata)
{
g_return_val_if_fail (hdata, NULL);
hdata->ref_count ++;
#ifdef QUERY_EDITOR_DEBUG
g_print ("%s (%p) ref count:%d\n", __FUNCTION__, hdata, hdata->ref_count);
#endif
return hdata;
}
static void
hist_item_data_unref (HistItemData *hdata)
{
g_return_if_fail (hdata);
hdata->ref_count --;
#ifdef QUERY_EDITOR_DEBUG
g_print ("%s (%p) ref count:%d\n", __FUNCTION__, hdata, hdata->ref_count);
#endif
if (hdata->ref_count <= 0) {
if (hdata->batch)
query_editor_history_batch_unref (hdata->batch);
if (hdata->item)
query_editor_history_item_unref (hdata->item);
if (hdata->tag)
g_object_unref (hdata->tag);
g_free (hdata);
}
}
|