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 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
  
     | 
    
      /* mg-work-grid.c
 *
 * Copyright (C) 2002 - 2004 Vivien Malerba
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <libgnomedb/libgnomedb.h>
#include "marshal.h"
#include "mg-server.h"
#include "mg-server-data-type.h"
#include "mg-data-handler.h"
#include "mg-work-grid.h"
#include "mg-work-core.h"
#include "mg-work-widget.h"
#include "mg-query.h"
#include "mg-target.h"
#include "mg-entity.h"
#include "mg-renderer.h"
#include "mg-result-set.h"
#include "mg-form.h"
#include "mg-parameter.h"
#include "mg-context.h"
#include "mg-field.h"
#include "mg-qfield.h"
#include "handlers/mg-data-cell-renderer-info.h"
#include "handlers/mg-data-cell-renderer-combo.h"
#include "mg-data-entry.h" /* only the MG_DATA_ENTRY_* enum is used here */
#include "utility.h"
#include "mg-util.h"
#ifdef debug
#include "mg-graphviz.h"
#endif
static void mg_work_grid_class_init (MgWorkGridClass * class);
static void mg_work_grid_init (MgWorkGrid * wid);
static void mg_work_grid_dispose (GObject   * object);
static void mg_work_grid_set_property (GObject              *object,
				       guint                 param_id,
				       const GValue         *value,
				       GParamSpec           *pspec);
static void mg_work_grid_get_property (GObject              *object,
				       guint                 param_id,
				       GValue               *value,
				       GParamSpec           *pspec);
static void mg_work_grid_initialize (MgWorkGrid *grid);
static void nullified_core_cb (MgWorkCore *core, MgWorkGrid *grid);
static GtkWidget *modif_buttons_make (MgWorkGrid *grid);
static void       modif_buttons_update (MgWorkGrid *grid);
static void       modif_actions_real_do (MgWorkGrid *grid, gchar action);
static void refresh_all_rows (MgWorkGrid *grid, gboolean keep_old_modifications);
static void update_simple_grid (MgWorkGrid *grid, gboolean keep_user_modifs);
static void arg_param_changed_cb (MgParameter *param, MgWorkGrid *grid);
static void work_param_changed_cb (MgParameter *param, MgWorkGrid *grid);
/* MgWorkWidget interface */
static void            mg_work_grid_widget_init         (MgWorkWidgetIface *iface);
static void            mg_work_grid_run                 (MgWorkWidget *iface, guint mode);
static void            mg_work_grid_set_mode            (MgWorkWidget *iface, guint mode);
static void            mg_work_grid_set_entry_editable  (MgWorkWidget *iface, MgQfield *field, gboolean editable);
static void            mg_work_grid_show_entry_actions  (MgWorkWidget *iface, MgQfield *field, gboolean show_actions);
static void            mg_work_grid_show_global_actions (MgWorkWidget *iface, gboolean show_actions);
static MgParameter    *mg_work_grid_get_param_for_field (MgWorkWidget *iface, MgQfield *field, const gchar *field_name, 
							 gboolean in_exec_context);
static gboolean        mg_work_grid_has_been_changed    (MgWorkWidget *iface);
static MgContext      *mg_work_grid_get_exec_context    (MgWorkWidget *iface);
static GtkActionGroup *mg_work_grid_get_actions_group   (MgWorkWidget *iface);
typedef struct {
	MgContextNode   *node;
	GtkCellRenderer *data_cell;
	GtkCellRenderer *info_cell;
	gboolean         info_shown;
	gboolean         data_locked;
} ColumnData;
#define COLUMN_DATA(x) ((ColumnData *)(x))
struct _MgWorkGridPriv
{
	MgWorkCore        *core;
	gboolean           has_run;
	GSList            *modified_rows; /* list of ModelUserModifiedRow, all data's memory is handled there */
	guint              mode;
	GtkTooltips       *tooltips;
	gboolean           default_show_info_cell;
	GSList            *columns_data; /* list of ColumnData */
	gboolean           internal_params_change;
	gint               sample_first_row;
	gint               sample_last_row;
	gint               sample_size;
	GtkWidget         *title;
	GtkWidget         *scroll;
	GtkWidget         *treeview;
	GtkUIManager      *uimanager;
	GtkActionGroup    *actions_group;
	GtkWidget         *modif_all;
	GtkWidget         *nav_current_sample;
	gchar             *current_selection; /* Selection as a GtkTreePath string representation */
};
/* get a pointer to the parents to be able to call their destructor */
static GObjectClass *parent_class = NULL;
/* signals */
enum
{
        SELECTION_CHANGED,
        LAST_SIGNAL
};
                                                                                                                                                                              
static gint mg_work_grid_signals[LAST_SIGNAL] = { 0 };
/* properties */
enum
{
        PROP_0,
        PROP_TITLE_VISIBLE,
	PROP_TITLE_STRING,
	PROP_ACTIONS_VISIBLE,
	PROP_INFO_CELL_VISIBLE
};
guint
mg_work_grid_get_type (void)
{
	static GType type = 0;
	if (!type) {
		static const GTypeInfo info = {
			sizeof (MgWorkGridClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) mg_work_grid_class_init,
			NULL,
			NULL,
			sizeof (MgWorkGrid),
			0,
			(GInstanceInitFunc) mg_work_grid_init
		};		
		static const GInterfaceInfo work_widget_info = {
                        (GInterfaceInitFunc) mg_work_grid_widget_init,
                        NULL,
                        NULL
                };
		
		type = g_type_register_static (GTK_TYPE_VBOX, "MgWorkGrid", &info, 0);
		g_type_add_interface_static (type, MG_WORK_WIDGET_TYPE, &work_widget_info);
	}
	return type;
}
static void
mg_work_grid_widget_init (MgWorkWidgetIface *iface)
{
	iface->run = mg_work_grid_run;
	iface->set_mode = mg_work_grid_set_mode;
	iface->set_entry_editable = mg_work_grid_set_entry_editable;
	iface->show_entry_actions = mg_work_grid_show_entry_actions;
	iface->show_global_actions = mg_work_grid_show_global_actions;
	iface->get_param_for_field = mg_work_grid_get_param_for_field;
        iface->has_been_changed = mg_work_grid_has_been_changed;
	iface->get_exec_context = mg_work_grid_get_exec_context;
	iface->get_actions_group = mg_work_grid_get_actions_group;
}
static void
mg_work_grid_class_init (MgWorkGridClass * class)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (class);
	
	parent_class = g_type_class_peek_parent (class);
	mg_work_grid_signals[SELECTION_CHANGED] = 
		g_signal_new ("selection_changed",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET (MgWorkGridClass, selection_changed),
                              NULL, NULL,
                              marshal_VOID__BOOLEAN, G_TYPE_NONE,
                              1, G_TYPE_BOOLEAN);
	object_class->dispose = mg_work_grid_dispose;
	/* Properties */
        object_class->set_property = mg_work_grid_set_property;
        object_class->get_property = mg_work_grid_get_property;
	g_object_class_install_property (object_class, PROP_TITLE_VISIBLE,
                                         g_param_spec_boolean ("title_visible", NULL, NULL, FALSE,
                                                               G_PARAM_WRITABLE));
	g_object_class_install_property (object_class, PROP_TITLE_STRING,
                                         g_param_spec_string ("title_string", NULL, NULL, NULL,
							      G_PARAM_WRITABLE));
	g_object_class_install_property (object_class, PROP_ACTIONS_VISIBLE,
                                         g_param_spec_boolean ("actions_visible", NULL, NULL, FALSE,
                                                               G_PARAM_WRITABLE));
	g_object_class_install_property (object_class, PROP_INFO_CELL_VISIBLE,
                                         g_param_spec_boolean ("info_cell_visible", NULL, NULL, FALSE,
                                                               G_PARAM_WRITABLE));
}
static void
mg_work_grid_init (MgWorkGrid * wid)
{
	wid->priv = g_new0 (MgWorkGridPriv, 1);
	wid->priv->core = NULL;
	wid->priv->has_run = FALSE;
	wid->priv->modified_rows = NULL;
	wid->priv->default_show_info_cell = TRUE;
	wid->priv->columns_data = NULL;
	wid->priv->internal_params_change = FALSE;
	wid->priv->sample_first_row = 0;
	wid->priv->sample_last_row = 0;
	wid->priv->sample_size = 100;
	wid->priv->treeview = NULL;
	wid->priv->mode = 0;
	wid->priv->tooltips = NULL;
	wid->priv->current_selection = NULL;
}
/**
 * mg_work_grid_new
 * @query: a #MgQuery object
 * @modified: a #MgTarget object, or %NULL
 *
 * Creates a new #MgWorkGrid widget.
 *
 * @query must be a SELECT query (no union, etc selection query)
 *
 * The @modified target must belong to @query and represent
 * modifiable entity (a #MgDbTable for example). If @modified is %NULL then
 * no modification will be allowed.
 *
 * Returns: the new widget
 */
GtkWidget *
mg_work_grid_new (MgQuery *query, MgTarget *modified)
{
	GObject *obj;
	MgWorkGrid *grid;
	g_return_val_if_fail (query && IS_MG_QUERY (query), NULL);
	g_return_val_if_fail (mg_query_get_query_type (query) == MG_QUERY_TYPE_SELECT, NULL);
	
	if (modified) {
		g_return_val_if_fail (IS_MG_TARGET (modified), NULL);
		g_return_val_if_fail (mg_target_get_query (modified) == query, NULL);
		g_return_val_if_fail (mg_entity_is_writable (mg_target_get_represented_entity (modified)), NULL);
	}
	obj = g_object_new (MG_WORK_GRID_TYPE, NULL);
	grid = MG_WORK_GRID (obj);
	grid->priv->core = MG_WORK_CORE (mg_work_core_new (query, modified));
	g_signal_connect (G_OBJECT (grid->priv->core), "nullified",
			  G_CALLBACK (nullified_core_cb), grid);
	mg_work_grid_initialize (grid);
	return GTK_WIDGET (obj);
}
static void
nullified_core_cb (MgWorkCore *core, MgWorkGrid *grid)
{
	g_signal_handlers_disconnect_by_func (G_OBJECT (core),
					      G_CALLBACK (nullified_core_cb), grid);
	
	if (grid->priv->core->work_context) {
		GSList *list = grid->priv->core->work_context->parameters;
		while (list) {
			g_signal_handlers_disconnect_by_func (G_OBJECT (list->data),
							      G_CALLBACK (work_param_changed_cb), grid);
			list = g_slist_next (list);
		}
	}
	if (grid->priv->has_run && grid->priv->core->args_context)
		g_signal_handlers_disconnect_by_func (G_OBJECT (grid->priv->core->args_context),
						      G_CALLBACK (arg_param_changed_cb), grid);
	
	g_object_unref (G_OBJECT (grid->priv->core));
	grid->priv->core = NULL;
	gtk_widget_set_sensitive (GTK_WIDGET (grid), FALSE);
}
static void clean_user_modif_rows (MgWorkGrid *grid);
static void user_modifs_row_free (ModelUserModifiedRow *user_modifs);
static void
mg_work_grid_dispose (GObject *object)
{
	MgWorkGrid *grid;
	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_WORK_GRID (object));
	grid = MG_WORK_GRID (object);
	if (grid->priv) {
		if (grid->priv->current_selection)
			g_free (grid->priv->current_selection);
		clean_user_modif_rows (grid);
		/* info cells */
		if (grid->priv->columns_data) {
			GSList *list = grid->priv->columns_data;
			while (list) {
				g_free (list->data);
				list = g_slist_next (list);
			}
			g_slist_free (grid->priv->columns_data);
			grid->priv->columns_data = NULL;
		}
		/* core */
		if (grid->priv->core)
			nullified_core_cb (grid->priv->core, grid);
		/* tooltips */
		if (grid->priv->tooltips) {
			gtk_object_destroy (GTK_OBJECT (grid->priv->tooltips));
			grid->priv->tooltips = NULL;
		}
		/* UI */
		if (grid->priv->actions_group) 
			g_object_unref (G_OBJECT (grid->priv->actions_group));
		if (grid->priv->uimanager)
			g_object_unref (G_OBJECT (grid->priv->uimanager));
		
		/* the private area itself */
		g_free (grid->priv);
		grid->priv = NULL;
	}
	/* for the parent class */
	parent_class->dispose (object);
}
/*
 * Free any memory associated with the user modified rows, as nothing is
 * managed by the data model
 */ 
static void
clean_user_modif_rows (MgWorkGrid *grid)
{
	if (grid->priv->modified_rows) {
		GSList *list;
		
		list = grid->priv->modified_rows;
		while (list) {
			user_modifs_row_free (MODEL_USER_MODIFIED_ROW (list->data));
			list = g_slist_next (list);
		}
		g_slist_free (grid->priv->modified_rows);
		grid->priv->modified_rows = NULL;
	}
}
static void
user_modifs_row_free (ModelUserModifiedRow *user_modifs)
{
	GSList *list;
			
	/* pkey values */
	if (user_modifs->pkey) {
		list = user_modifs->pkey;
		while (list) {
			if (list->data)
				gda_value_free ((GdaValue *) list->data);
			list = g_slist_next (list);
		}
		g_slist_free (user_modifs->pkey);
	}
	
	/* user modified values */
	if (user_modifs->user_values) {
		list = user_modifs->user_values;
		while (list) {
			if (MODEL_USER_MODIFIED_VALUE (list->data)->value)
				gda_value_free (MODEL_USER_MODIFIED_VALUE (list->data)->value);
			g_free (list->data);
			list = g_slist_next (list);
		}
		g_slist_free (user_modifs->user_values);
	}
	g_free (user_modifs);
}
static void
mg_work_grid_set_property (GObject              *object,
			   guint                 param_id,
			   const GValue         *value,
			   GParamSpec           *pspec)
{
	MgWorkGrid *grid;
        grid = MG_WORK_GRID (object);
        if (grid->priv) {
                switch (param_id) {
                case PROP_TITLE_VISIBLE:
			if (g_value_get_boolean (value))
				gtk_widget_show (grid->priv->title);
			else
				gtk_widget_hide (grid->priv->title);
                        break;
                case PROP_TITLE_STRING:
			gnome_db_gray_bar_set_text (GNOME_DB_GRAY_BAR (grid->priv->title), g_value_get_string (value));
			gtk_widget_show (grid->priv->title);
                        break;
		case PROP_ACTIONS_VISIBLE:
			if (g_value_get_boolean (value))
				gtk_widget_show (grid->priv->modif_all);
			else
				gtk_widget_hide (grid->priv->modif_all);
			break;
		case PROP_INFO_CELL_VISIBLE: 
		{
			GSList *list = grid->priv->columns_data;
			gboolean show = g_value_get_boolean (value);
			grid->priv->default_show_info_cell = show;
			while (list) {
				COLUMN_DATA (list->data)->info_shown = show;
				g_object_set (G_OBJECT (COLUMN_DATA (list->data)->info_cell), "visible", show, NULL);
				list = g_slist_next (list);
			}
		}
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
			break;
                }
        }
}
static void
mg_work_grid_get_property (GObject              *object,
			   guint                 param_id,
			   GValue               *value,
			   GParamSpec           *pspec)
{
	MgWorkGrid *grid;
        grid = MG_WORK_GRID (object);
        if (grid->priv) {
                switch (param_id) {
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
			break;
                }
        }	
}
/*
 * this function (re)computes the SELECT query and (re)runs it, resulting
 * in a change in the displayed values
 */
static void
refresh_all_rows (MgWorkGrid *grid, gboolean keep_old_modifications)
{
	GError *error = NULL;
	if (mg_work_core_run_select_query (grid->priv->core, &error))
		update_simple_grid (grid, keep_old_modifications);
	else {
		/* we don't want non relevant error messages so we check that the args context is valid before */
		if ((grid->priv->mode & MG_ACTION_REPORT_ERROR) && mg_context_is_valid (grid->priv->core->args_context)) {
			GtkWidget *dlg;
			gchar *message;
			GtkWidget *parent;
			
			/* find the top level window of the grid */
			parent = gtk_widget_get_parent (GTK_WIDGET (grid));
			while (parent && !GTK_IS_WINDOW (parent)) 
				parent = gtk_widget_get_parent (parent);
			
			if (error) {
				message = g_strdup (error->message);
				g_error_free (error);
			}
			else
				message = g_strdup_printf (_("An unknown error occurred while executing the query."));
			
			dlg = gtk_message_dialog_new (GTK_WINDOW (parent), 0,
						      GTK_MESSAGE_ERROR,
						      GTK_BUTTONS_CLOSE,
						      message);
			g_free (message);
			gtk_dialog_run (GTK_DIALOG (dlg));
			gtk_widget_destroy (dlg);
		}
		update_simple_grid (grid, FALSE);
	}
}
static void
arg_param_changed_cb (MgParameter *param, MgWorkGrid *grid)
{
	refresh_all_rows (grid, FALSE);
}
static ModelUserModifiedRow *grid_model_make_user_modifs (MgWorkGrid *grid, gint row);
static ModelUserModifiedValue *grid_model_get_user_modifs (MgWorkGrid *grid, GtkTreeModel *tree_model, GtkTreeIter *iter,
							   MgContextNode *context_node, gboolean create_if_needed);
static void grid_set_user_modifs_to_default_if_notnull (MgWorkGrid *grid, GtkTreeModel *tree_model, GtkTreeIter *iter, 
							MgWorkCore *core);
static void grid_modif_struct_set_value (MgWorkGrid *grid, ModelUserModifiedValue *user_modif, 
					 GtkTreeModel *tree_model, GtkTreeIter *iter,
					 MgWorkCore *core, const GdaValue *value);
static void grid_clean_data_model_row (MgWorkGrid *grid, GtkTreeModel *tree_model, GtkTreeIter *iter);
static void grid_modif_struct_set_status (MgWorkGrid *grid, ModelUserModifiedValue *user_modif,
					  GtkTreeModel *tree_model, GtkTreeIter *iter,
					  MgWorkCore *core, MgDataEntryAttribute requested_status);
static void
work_param_changed_cb (MgParameter *param, MgWorkGrid *grid)
{
	GSList *list;
	GtkTreeSelection *selection;
	GtkTreeModel *model;
	GtkTreeIter iter;
	/* take care of param dependencies */
	if (!grid->priv->treeview)
		return;
	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (grid->priv->treeview));
	if (!grid->priv->internal_params_change && gtk_tree_selection_get_selected (selection, &model, &iter)) {
		GtkTreePath *path;
		path = gtk_tree_model_get_path (model, &iter);
		list = grid->priv->core->work_context->parameters;
		while (list) {
			GSList *depend = mg_parameter_get_dependencies (MG_PARAMETER (list->data));
			while (depend) {
				if (depend->data == (gpointer) param) {
					ModelUserModifiedValue *user_modif;
					MgContextNode *context_node;
					
					context_node = mg_context_find_node_for_param (grid->priv->core->work_context, 
										       MG_PARAMETER (list->data));
					user_modif = grid_model_get_user_modifs (grid, model, &iter, context_node, TRUE);
					
					grid_modif_struct_set_status (grid, user_modif, model, &iter, grid->priv->core, 
								      MG_DATA_ENTRY_IS_NULL);
				}
				depend = g_slist_next (depend);
			}
			list = g_slist_next (list);
		}
		gtk_tree_path_free (path);
	}
}
/*
 * Real initialization
 */
static void 
mg_work_grid_initialize (MgWorkGrid *grid)
{
	GtkWidget *group, *sw;
	/* title */
	grid->priv->title = gnome_db_gray_bar_new (_("No title"));
        gtk_box_pack_start (GTK_BOX (grid), grid->priv->title, FALSE, TRUE, 2);
	gtk_widget_show (grid->priv->title);
	/*
	 * TreeView preparation
	 */
	sw = gtk_scrolled_window_new (NULL, NULL);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
        gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
	gtk_box_pack_start (GTK_BOX (grid), sw, TRUE, TRUE, 0);
	gtk_widget_show (sw);
	grid->priv->scroll = sw;
	/* signals connecting */
	if (grid->priv->core->work_context) {
		GSList *list = grid->priv->core->work_context->parameters;
		while (list) {
			g_signal_connect (G_OBJECT (list->data), "changed",
					  G_CALLBACK (work_param_changed_cb), grid);
			list = g_slist_next (list);
		}
	}
	/* 
	 * the grid's attributes
	 */
	if (mg_base_get_name (MG_BASE (grid->priv->core->query_select)))
		gnome_db_gray_bar_set_text (GNOME_DB_GRAY_BAR (grid->priv->title),
					    mg_base_get_name (MG_BASE (grid->priv->core->query_select)));
	else
		gtk_widget_hide (grid->priv->title);
	/*
	 * the actions part
	 */
	group = modif_buttons_make (grid);
	gtk_box_pack_start (GTK_BOX (grid), group, FALSE, FALSE, 0);
	gtk_widget_show (group);
	/* tooltips */
	grid->priv->tooltips = gtk_tooltips_new ();
}
static void
treeview_show_param_column (MgWorkGrid *grid, MgParameter *param, gboolean showit)
{
	gint pos = -1;
	GtkTreeViewColumn *viewcol;
	MgContextNode *cnode;
	cnode = mg_context_find_node_for_param (grid->priv->core->work_context, param);
	
	pos = g_slist_index (grid->priv->core->work_context->nodes, cnode);	
	g_assert (pos >= 0);
	viewcol = gtk_tree_view_get_column (GTK_TREE_VIEW (grid->priv->treeview), pos);
	/* Sets the column's visibility */
	gtk_tree_view_column_set_visible (viewcol, showit);
}
static void compute_row_boundaries (MgWorkGrid *grid);
static void tree_view_selection_changed_cb (GtkTreeSelection *selection, MgWorkGrid *grid);
static void create_new_tree_view (MgWorkGrid *grid, GtkTreeModel *tree_model);
static void tree_model_row_changed_cb (GtkTreeModel *treemodel, GtkTreePath *arg1, GtkTreeIter *arg2, MgWorkGrid *grid);
static gboolean compare_pk_values (GSList *list1, GSList *list2);
/*
 * Creates a GtkTreeView if necessary, and adds or update the data in grid->priv->core->data_rs in it.
 * if @keep_user_modifs is TRUE, then the update process tries to keep data as modified by the user
 */
static void
update_simple_grid (MgWorkGrid *grid, gboolean keep_user_modifs)
{
	GtkTreeModel *tree_model;
	/* REM: if a treeview already exists, then we don't need to create a new one as the
	 * SELECT query will have returned the same columns (number and type), we just need
	 * to update the model.
	 */
	/* new treeview if necessary */
	if (!grid->priv->treeview) {
		/* Model creation: 
		 * --> 1 column which refers to the row number to be displayed at the selected row
		 *     (or -1 for a new row, or -2 as a temporary value for a row which will soon be deleted)
		 * --> 1 column which holds the changes the user has made to the model; the original data
		 *     is not modified. This column is a ModelUserModifiedRow structure (all
		 *     the data in each of these structures is allocated on demand, and managed in priv->modified_rows).
		 */
		tree_model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_INT, G_TYPE_POINTER));
		create_new_tree_view (grid, tree_model);
		g_object_unref (G_OBJECT (tree_model));
		gtk_container_add (GTK_CONTAINER (grid->priv->scroll), grid->priv->treeview);
		gtk_widget_show (grid->priv->treeview);
		
	}
	else
		tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (grid->priv->treeview));
	g_object_set_data (G_OBJECT (tree_model), "grid", grid);
	/* temporary disabling signal to update the buttons status */
	g_signal_handlers_block_by_func (G_OBJECT (tree_model),
					 G_CALLBACK (tree_model_row_changed_cb), grid);
	/* Filling or updating the data model */
	compute_row_boundaries (grid);
	if (grid->priv->core->data_rs) {
		GtkTreeIter current_iter;
		gint current_row;
		gint row_count;
		gboolean mode_new_data = TRUE; /* TRUE if there is not yet any data */
		GSList *kept_user_modif_rows = NULL;
		GSList *query_pk_fields = NULL;
		if (keep_user_modifs) 
			query_pk_fields = mg_query_get_target_pkfields (grid->priv->core->query_select, 
									grid->priv->core->modif_target);
		
		/* setting the current pointers (in the model and in the resultset) */
		row_count = mg_resultset_get_nbtuples (grid->priv->core->data_rs);
		current_row = grid->priv->sample_first_row;
#ifdef debug
		g_print ("-> %d row(s)\n", row_count);
#endif
		
		while (current_row < grid->priv->sample_last_row) {
			gboolean model_row = -1;
			
			/* set the iter position for this round, while keeping the new rows (created by the user) */
			while (model_row == -1) {
				if (current_row == grid->priv->sample_first_row) 
					mode_new_data = !gtk_tree_model_get_iter_first (tree_model, ¤t_iter);
				else
					mode_new_data = !gtk_tree_model_iter_next (tree_model, ¤t_iter);
				if (mode_new_data)
					gtk_list_store_append (GTK_LIST_STORE (tree_model), ¤t_iter);
				
				if (mode_new_data || !keep_user_modifs)
					model_row = current_row;
				else 
					gtk_tree_model_get (tree_model, ¤t_iter, COLUMN_ROW_NUM, &model_row, -1);
				if (model_row == -1) {
					ModelUserModifiedRow *stored_row;
					gtk_tree_model_get (tree_model, ¤t_iter, COLUMN_USER_MODIFS_ROW, &stored_row, -1);
					if (stored_row) {
						kept_user_modif_rows = g_slist_append (kept_user_modif_rows, stored_row);
						grid->priv->modified_rows = g_slist_remove (grid->priv->modified_rows, 
											    stored_row);
					}
				}
			}
			/* set model's columns */
			gtk_list_store_set (GTK_LIST_STORE (tree_model), ¤t_iter, 
					    COLUMN_ROW_NUM, current_row, COLUMN_USER_MODIFS_ROW, NULL, -1);
			
			/* see if we have a user modified value for that row that we want to keep */
			if (keep_user_modifs && !mode_new_data) {
				GSList *stored_list = grid->priv->modified_rows;
				ModelUserModifiedRow *stored_row = NULL;
				while (stored_list && !stored_row) {
					ModelUserModifiedRow *tmp = MODEL_USER_MODIFIED_ROW (stored_list->data);
					gboolean equal = TRUE;
					
					if (tmp->pkey) {
						GSList *pk_values = NULL, *list;
						list = query_pk_fields;
						while (list) {
							const GdaValue *value;
							gint col = mg_entity_get_field_index (MG_ENTITY (grid->priv->core->query_select),
											      MG_FIELD (list->data));
							value = mg_resultset_get_gdavalue (grid->priv->core->data_rs, current_row, col);
							pk_values = g_slist_append (pk_values, value);
							list = g_slist_next (list);
						}
						
						equal = compare_pk_values (tmp->pkey, pk_values);
						g_slist_free (pk_values);
					}
					else
						equal = FALSE;
					if (equal) {
						/* keep that ModelUserModifiedRow */
						stored_row = tmp;
						kept_user_modif_rows = g_slist_append (kept_user_modif_rows, stored_row);
						stored_list = g_slist_next (stored_list);
						grid->priv->modified_rows = g_slist_remove (grid->priv->modified_rows, 
											    stored_row);
						gtk_list_store_set (GTK_LIST_STORE (tree_model), ¤t_iter, 
								    COLUMN_USER_MODIFS_ROW, stored_row, -1);
					}
					else 
						stored_list = g_slist_next (stored_list);
				}
			}
			current_row ++;
		}
		
		/* remove extra remaining rows */
		if (row_count == 0)
			gtk_list_store_clear (GTK_LIST_STORE (tree_model));
		else {
			mode_new_data = !gtk_tree_model_iter_next (tree_model, ¤t_iter);			
			if (!mode_new_data) 
				while (gtk_list_store_remove (GTK_LIST_STORE (tree_model), ¤t_iter));
		}
		clean_user_modif_rows (grid);
		grid->priv->modified_rows = kept_user_modif_rows;
		if (keep_user_modifs) 
			g_slist_free (query_pk_fields);
	}
	else {
		clean_user_modif_rows (grid);
		gtk_list_store_clear (GTK_LIST_STORE (tree_model));
	}
	/* re-enabling signal to update the buttons status */
	g_signal_handlers_unblock_by_func (G_OBJECT (tree_model),
					   G_CALLBACK (tree_model_row_changed_cb), grid);
	/* make sure the new selected row is taken into account */
	if (grid->priv->core->data_rs) {
		GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (grid->priv->treeview));
		tree_view_selection_changed_cb (selection, grid);
	}
	modif_buttons_update (grid);
}
static void
compute_row_boundaries (MgWorkGrid *grid)
{
	/* Note: sample_first_row and sample_last_row evolve by complete truncks of size sample_size if sample_size != 0 */
	if (grid->priv->core->data_rs) {
		gint row_count;
		row_count = mg_resultset_get_nbtuples (grid->priv->core->data_rs);
		if (grid->priv->sample_size) { /* sample size is set */
			if (grid->priv->sample_first_row > row_count) {
				while (grid->priv->sample_first_row > row_count) {
					grid->priv->sample_first_row -= grid->priv->sample_size;
					if (grid->priv->sample_first_row <= 0)
						grid->priv->sample_first_row = 0;
				}
				
			}
			grid->priv->sample_last_row = grid->priv->sample_first_row + grid->priv->sample_size;
			if (grid->priv->sample_last_row > row_count)
				grid->priv->sample_last_row = row_count;
		}
		else { /* sample size is not set */
			grid->priv->sample_first_row = 0;
			grid->priv->sample_last_row = row_count - 1;
		}
	}
	else {
		grid->priv->sample_first_row = 0;
		grid->priv->sample_last_row = 0;
	}
}
static gboolean
compare_pk_values (GSList *list1, GSList *list2)
{
	gboolean equal = TRUE;
	GSList *l1, *l2;
	g_return_val_if_fail ((list1 && list2) || (!list1 && !list2), FALSE);
	if (!list1)
		return TRUE;
	l1 = list1;
	l2 = list2;
	while (l1 && l2 && equal) {
		if ((!l1->data || gda_value_is_null ((GdaValue *)l1->data)) &&
		    l2->data && !gda_value_is_null ((GdaValue *)l2->data))
			equal = FALSE;
		if ((!l2->data || gda_value_is_null ((GdaValue *)l2->data)) &&
		    l1->data && !gda_value_is_null ((GdaValue *)l1->data))
			equal = FALSE;
		if (l1->data && l2->data && 
		    (gda_value_get_type ((GdaValue *)l1->data) == gda_value_get_type ((GdaValue *)l2->data)))
			equal = !gda_value_compare ((GdaValue *)l1->data, (GdaValue *)l2->data);
		l1 = g_slist_next (l1);
		l2 = g_slist_next (l2);
	}
	g_return_val_if_fail ((list1 && list2) || (!list1 && !list2), FALSE);
	return equal;
}
/*
 * creates a new string where underscores '_' are replaced by double underscores '__'
 * WARNING: the old string is free'ed so it is possible to do "str=double_underscores(str);"
 */
static gchar *
replace_double_underscores (gchar *str)
{
        gchar **arr;
        gchar *ret;
	
        arr = g_strsplit (str, "_", 0);
        ret = g_strjoinv ("__", arr);
        g_strfreev (arr);
	g_free (str);
	
        return ret;
}
static ColumnData *get_column_data (MgWorkGrid *grid, MgContextNode *node);
static gboolean tree_view_event_cb (GtkWidget *treeview, GdkEvent *event, MgWorkGrid *grid);
static void data_cell_value_changed (GtkCellRenderer *renderer, const gchar *path, const GdaValue *new_value,
				     MgWorkGrid *grid);
static void data_cell_status_changed (GtkCellRenderer *renderer, const gchar *path, 
				      MgDataEntryAttribute requested_action, MgWorkGrid *grid);
static void cell_render_set_attributes (GtkTreeViewColumn *tree_column,
					GtkCellRenderer *cell,
					GtkTreeModel *tree_model,
					GtkTreeIter *iter, MgWorkGrid *grid);
static gint tree_sortable_sort_values (GtkTreeModel *model, GtkTreeIter *itera, GtkTreeIter *iterb, MgContextNode *node);
static void
create_new_tree_view (MgWorkGrid *grid, GtkTreeModel *tree_model)
{
	gint i;
	GtkTreeView *tree_view;
	GtkTreeSelection *selection;
	GSList *list;
	/* Actual GtkTreeView widget creation */
	tree_view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (tree_model));
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
	gtk_tree_view_set_enable_search (GTK_TREE_VIEW (tree_view), TRUE);
	g_assert (!grid->priv->treeview);
	grid->priv->treeview = GTK_WIDGET (tree_view);
	g_signal_connect (G_OBJECT (tree_view), "event",
			  G_CALLBACK (tree_view_event_cb), grid);
	/* selection and signal handling */
	selection = gtk_tree_view_get_selection (tree_view);
	g_signal_connect (G_OBJECT (selection), "changed",
			  G_CALLBACK (tree_view_selection_changed_cb), grid);
	g_signal_connect (G_OBJECT (tree_model), "row_changed",
			  G_CALLBACK (tree_model_row_changed_cb), grid);
	/* Creation of the columns in the treeview, to fit the parameters in the context */
	list = grid->priv->core->work_context->nodes;
	i = 0;
	while (list) {
		MgParameter *param;
		MgContextNode *node;
		GtkTreeViewColumn *column;
		GtkCellRenderer *renderer;
		ColumnData *column_data;
		node = MG_CONTEXT_NODE (list->data);
		/* update the list of columns data */
		column_data = get_column_data (grid, node);
		if (!column_data) {
			column_data = g_new0 (ColumnData, 1);
			column_data->node = node;
			column_data->info_shown = grid->priv->default_show_info_cell;
			column_data->data_locked = grid->priv->core->modif_target ? FALSE : TRUE;
			grid->priv->columns_data = g_slist_append (grid->priv->columns_data, column_data);
		}
		/* create renderers */
		if ((param = node->param)) { /* single direct parameter */
			MgConf *conf;
			MgServerDataType *type;
			MgDataHandler *dh = NULL;
			gchar *plugin = NULL;
			gchar *title;
			
			type = mg_parameter_get_data_type (param);
			g_assert (type);
			title = replace_double_underscores (g_strdup (mg_base_get_name (MG_BASE (param))));
			if (!title)
				title = g_strdup (_("No title"));
			conf = mg_base_get_conf (MG_BASE (type));
			g_object_get (G_OBJECT (param), "handler_plugin", &plugin, NULL);
			if (plugin) {
				dh = mg_server_get_handler_by_name (mg_conf_get_server (conf), plugin);
				/* test if plugin can handle the parameter's data type */
				if (!mg_data_handler_accepts_gda_type (dh, mg_server_data_type_get_gda_type (type)))
					dh = NULL;
			}
			if (!dh)
				dh = mg_server_data_type_get_handler (type);
						
			renderer = mg_data_handler_get_cell_renderer (dh, mg_server_data_type_get_gda_type (type));
			column_data->data_cell = renderer;
			gtk_tree_view_insert_column_with_data_func (tree_view, i, title, renderer,
								    (GtkTreeCellDataFunc) cell_render_set_attributes, 
								    grid, NULL);
			column = gtk_tree_view_get_column (tree_view, i);
			g_free (title);
		}
		else { /* parameters depending on a sub query */
			gchar *title = NULL;
			
			title = replace_double_underscores (g_strdup (mg_base_get_name (MG_BASE (node->query))));
			/* FIXME: find a better label */
			if (!title)
				title = g_strdup (_("Value"));
			renderer = mg_data_cell_renderer_combo_new (mg_base_get_conf (MG_BASE (grid->priv->core)),
								    grid->priv->core->work_context, node);
			column_data->data_cell = renderer;
			gtk_tree_view_insert_column_with_data_func (tree_view, i, title, renderer,
								    (GtkTreeCellDataFunc) cell_render_set_attributes, 
								    grid, NULL);
			column = gtk_tree_view_get_column (tree_view, i);
			g_free (title);
		}
		g_object_set_data (G_OBJECT (column), "data_renderer", renderer);
		/* settings and signals */
		g_signal_connect (G_OBJECT (renderer), "changed", 
				  G_CALLBACK (data_cell_value_changed), grid);
		g_object_set (G_OBJECT (renderer), "editable", !column_data->data_locked, NULL);
		if (g_object_class_find_property (G_OBJECT_GET_CLASS (renderer), "set_default_if_invalid"))
			g_object_set (G_OBJECT (renderer), "set_default_if_invalid", TRUE, NULL);
		g_object_set_data (G_OBJECT (renderer), "context-node", node);
		g_object_set_data (G_OBJECT (renderer), "tree-model", tree_model);
		g_object_set_data (G_OBJECT (renderer), "work-core", grid->priv->core);
		g_object_set_data (G_OBJECT (column), "context-node", node);
		/* Adding the GdaValue's information cell as another GtkCellRenderer */
		renderer = mg_data_cell_renderer_info_new ();
		column_data->info_cell = renderer;
		gtk_tree_view_column_pack_end (column, renderer, FALSE);
		gtk_tree_view_column_set_cell_data_func (column, renderer, 
							 (GtkTreeCellDataFunc) cell_render_set_attributes, 
							 grid, NULL);
		g_signal_connect (G_OBJECT (renderer), "status_changed",
				  G_CALLBACK (data_cell_status_changed), grid);
		g_object_set (G_OBJECT (renderer), "visible", column_data->info_shown, NULL);
		g_object_set_data (G_OBJECT (renderer), "context-node", node);
		g_object_set_data (G_OBJECT (renderer), "tree-model", tree_model);
		g_object_set_data (G_OBJECT (renderer), "work-core", grid->priv->core);
		/* Sorting data */
		gtk_tree_view_column_set_sort_column_id (column, i);
		gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (tree_model),
                                                 i, (GtkTreeIterCompareFunc) tree_sortable_sort_values, node, NULL);
		list = g_slist_next (list);
		i++;
	}
	/*
	 * Hiding some entries of the grid for which correspond to alias parameters
	 */
	list = grid->priv->core->no_show_params;
	while (list) {
		treeview_show_param_column (grid, MG_PARAMETER (list->data), FALSE);
		
		list = g_slist_next (list);
	}
}
static gboolean 
tree_view_event_cb (GtkWidget *treeview, GdkEvent *event, MgWorkGrid *grid)
{
	gboolean done = FALSE;
	if (event->type == GDK_KEY_PRESS) {
		GdkEventKey *ekey = (GdkEventKey *) event;
		guint modifiers = gtk_accelerator_get_default_mod_mask ();		
		/* Tab to move one column left or right */
		if (ekey->keyval == GDK_Tab) {
			GtkTreeViewColumn *column;
			GtkTreePath *path;
			/* FIXME: if a column is currently edited, then make sure the editing of that cell is not cancelled */
			gtk_tree_view_get_cursor (GTK_TREE_VIEW (treeview), &path, &column);
			if (column && path) {
				GList *columns = gtk_tree_view_get_columns (GTK_TREE_VIEW (treeview));
				GList *col;
				GtkCellRenderer *renderer;
				/* change column */
				col = g_list_find (columns, column);
				g_return_val_if_fail (col, FALSE);
				if (((ekey->state & modifiers) == GDK_SHIFT_MASK) || ((ekey->state & modifiers) == GDK_CONTROL_MASK))
					col = g_list_previous (col); /* going to previous column */
				else 
					col = g_list_next (col); /* going to next column */
				if (col) {
					renderer = g_object_get_data (G_OBJECT (col->data), "data_renderer");
					gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (treeview), path, 
									  GTK_TREE_VIEW_COLUMN (col->data), 
									  renderer, FALSE);
					gtk_widget_grab_focus (treeview);
					done = TRUE;
				}
				g_list_free (columns);
			}
			if (path)
				gtk_tree_path_free (path);
		}
		
		/* DELETE to delete the selected row */
		if (ekey->keyval == GDK_Delete) {
			if (((ekey->state & modifiers) == GDK_SHIFT_MASK) || ((ekey->state & modifiers) == GDK_CONTROL_MASK))
				modif_actions_real_do (grid, 'u');
			else
				modif_actions_real_do (grid, 'd');
			done = TRUE;
		}
	}
	return done;
}
static gint
tree_sortable_sort_values (GtkTreeModel *model, GtkTreeIter *itera, GtkTreeIter *iterb, MgContextNode *node)
{
        GdaValue *valuea, *valueb;
        gint retval = 1;
	MgWorkGrid *grid;
	grid = 	g_object_get_data (G_OBJECT (model), "grid");
	valuea = utility_grid_model_get_value (model, itera, grid->priv->core, node, FALSE, NULL);
	valueb = utility_grid_model_get_value (model, iterb, grid->priv->core, node, FALSE, NULL);
	if (gda_value_get_type (valuea) == gda_value_get_type (valueb)) {
		if (gda_value_get_type (valuea) == GDA_VALUE_TYPE_LIST) {
			const GList *lista, *listb;
			lista = gda_value_get_list (valuea);
			listb = gda_value_get_list (valueb);
			retval = 0;
			g_assert (g_list_length (lista) == g_list_length (listb));
			while (lista && listb && !retval) {
				GdaValue *vala = (GdaValue*) lista->data, *valb = (GdaValue*) listb->data;
				if (gda_value_is_null (vala)) {
					if (gda_value_is_null (valb))
						retval = 0;
					else
						retval = 1;
				}
				else {
					if (gda_value_is_null (valb))
						retval = -1;
					else
						retval = gda_value_compare (vala, valb);;
				}
				lista = g_list_next (lista);
				listb = g_list_next (listb);
			}
		}
		else
			retval = gda_value_compare (valuea, valueb);
	}
	else {
		if (gda_value_is_null (valuea))
			retval = -1;
		else
			retval = 1;
	}
	gda_value_free (valuea);
	gda_value_free (valueb);
        return retval;
}
static ColumnData *
get_column_data (MgWorkGrid *grid, MgContextNode *node)
{
	ColumnData *retval = NULL;
	GSList *list = grid->priv->columns_data;
	while (list && !retval) {
		if (COLUMN_DATA (list->data)->node == node)
			retval = COLUMN_DATA (list->data);
		list = g_slist_next (list);
	}
	return retval;
}
static void data_cell_update_context_params (MgContextNode *node, GdaValue *value);
static void
data_cell_value_changed (GtkCellRenderer *renderer, const gchar *path, const GdaValue *new_value, MgWorkGrid *grid)
{
	ModelUserModifiedValue *user_modif;
	GtkTreePath *treepath;
	GtkTreeIter iter;
	MgContextNode *context_node;
	GtkTreeModel *tree_model;
	MgWorkCore *core;
	
	context_node = g_object_get_data (G_OBJECT (renderer), "context-node");
	tree_model = g_object_get_data (G_OBJECT (renderer), "tree-model");
	if (path) {
		treepath = gtk_tree_path_new_from_string (path);
		if (! gtk_tree_model_get_iter (tree_model, &iter, treepath)) {
			gtk_tree_path_free (treepath);
			g_warning ("Can't get iter for path %s", path);
			return;
		}
		gtk_tree_path_free (treepath);
	}
	else {
		GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (grid->priv->treeview));
		if (!gtk_tree_selection_get_selected (selection, NULL, &iter)) {
			g_warning ("Can't get iter for current selection");
			return;
		}
	}
	core = g_object_get_data (G_OBJECT (renderer), "work-core");
	user_modif = grid_model_get_user_modifs (grid, tree_model, &iter, context_node, TRUE);
	if (!new_value || gda_value_is_null (new_value)) 
		grid_modif_struct_set_status (grid, user_modif, tree_model, &iter, core, MG_DATA_ENTRY_IS_NULL);
	else 
		grid_modif_struct_set_value (grid, user_modif, tree_model, &iter, core, new_value);
	data_cell_update_context_params (context_node, new_value);
}
static void
data_cell_update_context_params (MgContextNode *node, GdaValue *value)
{
	if (node->param)
		mg_parameter_set_value (node->param, value);
	else {
		const GList *values = NULL;
		GSList *params = node->params;
		
		if (value && !gda_value_is_null (value)) {
			g_assert (gda_value_isa (value, GDA_VALUE_TYPE_LIST));
			values = gda_value_get_list (value);
			g_assert (g_list_length (values) >= g_slist_length (params));
		}
		while (params) {
			if (values) 
				mg_parameter_set_value (MG_PARAMETER (params->data),
							g_list_nth_data (values, 
									 GPOINTER_TO_INT (g_hash_table_lookup 
											  (node->params_pos_in_query, params->data))));
			else
				mg_parameter_set_value (MG_PARAMETER (params->data), NULL);
			params = g_slist_next (params);
		}
	}
}
static void
data_cell_status_changed (GtkCellRenderer *renderer, const gchar *path, MgDataEntryAttribute requested_action,
			  MgWorkGrid *grid)
{
	ModelUserModifiedValue *user_modif;
	GtkTreePath *treepath;
	GtkTreeIter iter;
	MgContextNode *context_node;
	GtkTreeModel *tree_model;
	MgWorkCore *core;
	GdaValue *new_value;
	context_node = g_object_get_data (G_OBJECT (renderer), "context-node");
	tree_model = g_object_get_data (G_OBJECT (renderer), "tree-model");
	treepath = gtk_tree_path_new_from_string (path);
	if (! gtk_tree_model_get_iter (tree_model, &iter, treepath)) {
		gtk_tree_path_free (treepath);
		g_warning ("Can't get iter for path %s", path);
		return;
	}
	gtk_tree_path_free (treepath);
	core = g_object_get_data (G_OBJECT (renderer), "work-core");
	user_modif = grid_model_get_user_modifs (grid, tree_model, &iter, context_node, TRUE);
	grid_modif_struct_set_status (grid, user_modif, tree_model, &iter, core, requested_action);
	/* updating the parameters */
	new_value = utility_grid_model_get_value (tree_model, &iter, core, context_node, FALSE, NULL);
	data_cell_update_context_params (context_node, new_value);
	gda_value_free (new_value);
}
static void
cell_render_set_attributes (GtkTreeViewColumn *tree_column,
			    GtkCellRenderer *cell,
			    GtkTreeModel *tree_model,
			    GtkTreeIter *iter, MgWorkGrid *grid)
{
	MgContextNode *node;
	ModelUserModifiedRow *user_modifs;
	guint attributes;
	GdaValue *value;
	gboolean to_be_deleted = FALSE;
	ColumnData *column_data;
		
	if (!grid->priv->core->data_rs) 
		return;
	gtk_tree_model_get (tree_model, iter, COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
	if (user_modifs)
		to_be_deleted = user_modifs->to_be_deleted;
	node = g_object_get_data (G_OBJECT (tree_column), "context-node");
	column_data = get_column_data (grid, node);
	
	value = utility_grid_model_get_value (tree_model, iter, grid->priv->core, node, FALSE, &attributes);
	if (node->param) { /* single parameter */
		g_object_set (G_OBJECT (cell), 
			      "value", value,
			      "editable", !column_data->data_locked,
			      "value_attributes", attributes,
			      "cell_background", MG_COLOR_NORMAL_MODIF,
			      "cell_background-set", user_modifs ? TRUE : FALSE,
			      "to_be_deleted", to_be_deleted, 
			      NULL);
	}
	else { /* multiple parameters depending on a query */
		const GList *values = NULL;
		if (!gda_value_is_null (value))
			values = (GList *) gda_value_get_list (value);
		g_object_set (G_OBJECT (cell), 
			      "values_complete", values,
			      "editable", !column_data->data_locked,
			      "value_attributes", attributes,
			      "cell_background", MG_COLOR_NORMAL_MODIF,
			      "cell_background-set", user_modifs ? TRUE : FALSE,
			      "to_be_deleted", to_be_deleted, 
			      NULL);
	}
	gda_value_free (value);
}
static gboolean commit_do_work (MgWorkGrid *grid, GtkTreeIter *iter);
static void mg_work_grid_set_params_from_iter (MgWorkGrid *grid, GtkTreeIter *iter);
static void
tree_view_selection_changed_cb (GtkTreeSelection *selection, MgWorkGrid *grid)
{
	GtkTreeIter iter;
	GtkTreeModel *model;
	gboolean row_selected = FALSE;
	gboolean has_selection = TRUE;
	/* set all the parameters from the values of the current row */
	if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
		GtkTreePath *path;
		gchar *str;
		path = gtk_tree_model_get_path (model, &iter);
		str = gtk_tree_path_to_string (path);
		gtk_tree_path_free (path);
		if ((grid->priv->mode & MG_ACTION_MODIF_AUTO_COMMIT) &&
		    grid->priv->current_selection && 
		    (strcmp (str, grid->priv->current_selection))) {
			GtkTreeIter olditer;
			path = gtk_tree_path_new_from_string (grid->priv->current_selection);
			if (gtk_tree_model_get_iter (model, &olditer, path)) {
				/* save the previously modified row */
				if (commit_do_work (grid, &olditer)) {
					refresh_all_rows (grid, FALSE);
					has_selection = gtk_tree_selection_get_selected (selection, NULL, &iter);
				}
			}
			gtk_tree_path_free (path);
		}
		g_free (str);
		
		if (has_selection) {
			path = gtk_tree_model_get_path (model, &iter);
			if (grid->priv->current_selection)
				g_free (grid->priv->current_selection);
			grid->priv->current_selection = gtk_tree_path_to_string (path);
			gtk_tree_path_free (path);
			grid->priv->internal_params_change = TRUE;
			mg_work_grid_set_params_from_iter (grid, &iter);
			grid->priv->internal_params_change = FALSE;
			row_selected = TRUE;
		}
	}
	else {
		if (grid->priv->current_selection) {
			g_free (grid->priv->current_selection);
			grid->priv->current_selection = NULL;
		}
		has_selection = FALSE;
	}
	if (!has_selection) {
		/* render all the parameters invalid */
		GSList *list = grid->priv->core->work_context->parameters;
		while (list) {
			MgParameter *tmp;
			g_object_get (G_OBJECT (list->data), "full_bind", &tmp, NULL);
			if (! tmp) 
				mg_parameter_declare_invalid (MG_PARAMETER (list->data));
			list = g_slist_next (list);
		}
	}
	/* update the actions buttons */
	modif_buttons_update (grid);
#ifdef debug_signal
	g_print (">> 'SELECTION_CHANGED' from %s %p\n", G_OBJECT_TYPE_NAME (grid), grid);
#endif
	g_signal_emit (G_OBJECT (grid), mg_work_grid_signals[SELECTION_CHANGED], 0, row_selected);
#ifdef debug_signal
	g_print ("<< 'SELECTION_CHANGED' from %s %p\n", G_OBJECT_TYPE_NAME (grid), grid);
#endif
}
static void
tree_model_row_changed_cb (GtkTreeModel *treemodel, GtkTreePath *arg1, GtkTreeIter *arg2, MgWorkGrid *grid)
{
	/* update the actions buttons */
	modif_buttons_update (grid);
}
/*
 * mg_work_grid_set_params_from_iter
 * @grid:
 * @iter:
 *
 * Sets the parameters in 'core->work_context' from the values stored for the row provided by @iter.
 */
static void
mg_work_grid_set_params_from_iter (MgWorkGrid *grid, GtkTreeIter *iter)
{
	GSList *list;
	list = grid->priv->core->work_context->nodes;
	while (list) {
		MgContextNode *node;
		GdaValue *value;
		GtkTreeModel *model;
		guint attributes;
		model = gtk_tree_view_get_model (GTK_TREE_VIEW (grid->priv->treeview));
		node = MG_CONTEXT_NODE (list->data);
		value = utility_grid_model_get_value (model, iter, grid->priv->core, node, TRUE, &attributes);
		if (node->param)  /* single direct parameter */ {
			MgParameter *tmp;
			g_object_get (G_OBJECT (node->param), "full_bind", &tmp, NULL);
			if (! tmp) {
				mg_parameter_set_value (node->param, value);
				g_object_set (G_OBJECT (node->param), "use_default_value", 
					      attributes & MG_DATA_ENTRY_IS_DEFAULT ? TRUE : FALSE, NULL);
			}
		}
		else { /* parameters depending on a sub query */
			GSList *plist;
			GList *vlist = NULL;
			g_return_if_fail (!value || gda_value_is_null (value) || 
								       gda_value_isa (value, GDA_VALUE_TYPE_LIST));
			
			plist = node->params;
			if (value && !gda_value_is_null (value)) {
				vlist = (GList *) gda_value_get_list (value);
				g_assert (g_slist_length (plist) == g_list_length (vlist));
			}
			while (plist && (!value || gda_value_is_null (value) || vlist)) {
				if (value && !gda_value_is_null (value)) {
					mg_parameter_set_value (MG_PARAMETER (plist->data), (GdaValue *)(vlist->data));
					if (attributes & MG_DATA_ENTRY_IS_DEFAULT)
						g_object_set (G_OBJECT (plist->data), "use_default_value", TRUE, NULL);
					vlist = g_list_next (vlist);
				}
				else {
					mg_parameter_set_value (MG_PARAMETER (plist->data), NULL);
					if (attributes & MG_DATA_ENTRY_IS_DEFAULT)
						g_object_set (G_OBJECT (plist->data), "use_default_value", TRUE, NULL);
				}
				plist = g_slist_next (plist);
			}
		}
		gda_value_free (value);
		list = g_slist_next (list);
	}
}
/*
 *
 * Modification buttons (Commit changes, Reset grid, New entry, Delete)
 *
 */
static void action_new_cb (GtkAction *action, MgWorkGrid *grid);
static void action_delete_cb (GtkAction *action, MgWorkGrid *grid);
static void action_undelete_cb (GtkAction *action, MgWorkGrid *grid);
static void action_commit_cb (GtkAction *action, MgWorkGrid *grid);
static void action_reset_cb (GtkAction *action, MgWorkGrid *grid);
static void action_first_chunck_cb (GtkAction *action, MgWorkGrid *grid);
static void action_prev_chunck_cb (GtkAction *action, MgWorkGrid *grid);
static void action_next_chunck_cb (GtkAction *action, MgWorkGrid *grid);
static void action_last_chunck_cb (GtkAction *action, MgWorkGrid *grid);
static GtkActionEntry ui_actions[] = {
	{ "WorkWidgetNew", GTK_STOCK_NEW, "_New", NULL, "Create a new data entry", G_CALLBACK (action_new_cb)},
	{ "WorkWidgetDelete", GTK_STOCK_DELETE, "_Delete", NULL, "Delete the selected entry", G_CALLBACK (action_delete_cb)},
	{ "WorkWidgetUndelete", GTK_STOCK_UNDELETE, "_Undelete", NULL, "Cancels the deletion of the selected entry", 
	  G_CALLBACK (action_undelete_cb)},
	{ "WorkWidgetCommit", GTK_STOCK_SAVE, "_Commit", NULL, "Commit the latest changes", G_CALLBACK (action_commit_cb)},
	{ "WorkWidgetReset", GTK_STOCK_REFRESH, "_Reset", NULL, "Reset the data", G_CALLBACK (action_reset_cb)},
	{ "WorkWidgetFirstChunck", GTK_STOCK_GOTO_FIRST, "_First chunck", NULL, "Go to first chunck of records", 
	  G_CALLBACK (action_first_chunck_cb)},
	{ "WorkWidgetLastChunck", GTK_STOCK_GOTO_LAST, "_Last chunck", NULL, "Go to last chunck of records", 
	  G_CALLBACK (action_last_chunck_cb)},
	{ "WorkWidgetPrevChunck", GTK_STOCK_GO_BACK, "_Previous chunck", NULL, "Go to previous chunck of records", 
	  G_CALLBACK (action_prev_chunck_cb)},
	{ "WorkWidgetNextChunck", GTK_STOCK_GO_FORWARD, "Ne_xt chunck", NULL, "Go to next chunck of records",
	  G_CALLBACK (action_next_chunck_cb)}
};
static const gchar *ui_actions_info =
"<ui>"
"  <toolbar  name='ToolBar'>"
"    <toolitem action='WorkWidgetNew'/>"
"    <toolitem action='WorkWidgetDelete'/>"
"    <toolitem action='WorkWidgetUndelete'/>"
"    <toolitem action='WorkWidgetCommit'/>"
"    <toolitem action='WorkWidgetReset'/>"
"    <separator/>"
"    <toolitem action='WorkWidgetFirstChunck'/>"
"    <toolitem action='WorkWidgetPrevChunck'/>"
"    <toolitem action='WorkWidgetNextChunck'/>"
"    <toolitem action='WorkWidgetLastChunck'/>"
"  </toolbar>"
"</ui>";
static GtkWidget *
modif_buttons_make (MgWorkGrid *grid)
{
	GtkActionGroup *actions;
	GtkUIManager *ui;
	GtkWidget *table, *wid;
	table = gtk_table_new (1, 2, FALSE);
	/* action buttons */
	actions = gtk_action_group_new ("Actions");
	grid->priv->actions_group = actions;
	gtk_action_group_add_actions (actions, ui_actions, G_N_ELEMENTS (ui_actions), grid);
	ui = gtk_ui_manager_new ();
	gtk_ui_manager_insert_action_group (ui, actions, 0);
	gtk_ui_manager_add_ui_from_string (ui, ui_actions_info, -1, NULL);
	grid->priv->uimanager = ui;
	grid->priv->modif_all = gtk_ui_manager_get_widget (ui, "/ToolBar");
	gtk_table_attach_defaults (GTK_TABLE (table), grid->priv->modif_all, 0, 1, 0, 1);
	gtk_widget_show (grid->priv->modif_all);
	/* samples counter */
	wid = gtk_label_new ("? - ? / ?");
	gtk_widget_show (wid);
	grid->priv->nav_current_sample = wid;
	gtk_table_attach (GTK_TABLE (table), wid, 1, 2, 0, 1, 0, 0, 5, 0);
	return table;
}
static void
action_new_cb (GtkAction *action, MgWorkGrid *grid)
{
	modif_actions_real_do (grid, 'i');
}
static void
action_delete_cb (GtkAction *action, MgWorkGrid *grid)
{
	modif_actions_real_do (grid, 'd');
}
static void
action_undelete_cb (GtkAction *action, MgWorkGrid *grid)
{
	modif_actions_real_do (grid, 'u');
}
static void
action_commit_cb (GtkAction *action, MgWorkGrid *grid)
{
	modif_actions_real_do (grid, 'c');
}
static void
action_reset_cb (GtkAction *action, MgWorkGrid *grid)
{
	modif_actions_real_do (grid, 'r');
}
static void
action_first_chunck_cb (GtkAction *action, MgWorkGrid *grid)
{
	mg_work_grid_set_sample_start (grid, 0);	
}
static void
action_prev_chunck_cb (GtkAction *action, MgWorkGrid *grid)
{
	mg_work_grid_set_sample_start (grid, grid->priv->sample_first_row - grid->priv->sample_size);
}
static void
action_next_chunck_cb (GtkAction *action, MgWorkGrid *grid)
{
	mg_work_grid_set_sample_start (grid, grid->priv->sample_first_row + grid->priv->sample_size);
}
static void
action_last_chunck_cb (GtkAction *action, MgWorkGrid *grid)
{
	mg_work_grid_set_sample_start (grid, G_MAXINT - grid->priv->sample_size);
}
static void
modif_buttons_update (MgWorkGrid *grid)
{
	GtkTreeSelection *select;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gboolean changed=FALSE;
	gboolean has_selection;
	gchar *str;
	GtkAction *action;
	gint row_count = 0;
	if (!grid->priv->treeview || !grid->priv->core) {
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetDelete");
		g_object_set (G_OBJECT (action), "visible", TRUE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetUndelete");
		g_object_set (G_OBJECT (action), "visible", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetCommit");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetReset");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetNew");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetDelete");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		
		grid->priv->sample_first_row = 0;
		grid->priv->sample_last_row = 0;
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetFirstChunck");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetPrevChunck");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetNextChunck");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetLastChunck");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		gtk_label_set_text (GTK_LABEL (grid->priv->nav_current_sample), "? - ? / ?");
		gtk_widget_set_sensitive (grid->priv->nav_current_sample, FALSE);
		return;
	}
	changed = mg_work_grid_has_been_changed (MG_WORK_WIDGET (grid));
	select = gtk_tree_view_get_selection (GTK_TREE_VIEW (grid->priv->treeview));
	has_selection = gtk_tree_selection_get_selected (select, &model, &iter);
	/* FIXME: if the working mode is to allow the edition of one line at a time, then 
	 * we should check that the corresponding INSERT query is valid (or UPDATE, if INSERT is valid then
	 * UPDATE will also be valid, by construction) */
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetCommit");
	g_object_set (G_OBJECT (action), "sensitive", changed ? TRUE : FALSE, NULL);
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetReset");
	g_object_set (G_OBJECT (action), "sensitive", TRUE, NULL);
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetNew");
	g_object_set (G_OBJECT (action), "sensitive", 
		      grid->priv->core->modif_target && 
		      mg_context_is_valid (grid->priv->core->args_context) ? TRUE : FALSE, NULL);
	if (has_selection && grid->priv->core->modif_target) {
		gboolean to_be_deleted = FALSE;
		ModelUserModifiedRow *user_modifs;
		gtk_tree_model_get (model, &iter, COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
		if (user_modifs)
			to_be_deleted = user_modifs->to_be_deleted;
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetDelete");
		g_object_set (G_OBJECT (action), "sensitive", !to_be_deleted, NULL);
		
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetUndelete");
		g_object_set (G_OBJECT (action), "sensitive", to_be_deleted, NULL);
	}
	else {
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetDelete");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
		action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetUndelete");
		g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
	}
	if (grid->priv->sample_size) 
		row_count = grid->priv->core->data_rs ? mg_resultset_get_nbtuples (grid->priv->core->data_rs) : 0;
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetFirstChunck");
	g_object_set (G_OBJECT (action), "sensitive", 
		      grid->priv->sample_size ? (grid->priv->sample_first_row > 0 ? TRUE : FALSE) : FALSE, 
		      "visible", grid->priv->mode & MG_ACTION_NAVIGATION_ARROWS ? TRUE : FALSE, NULL);
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetPrevChunck");
	g_object_set (G_OBJECT (action), "sensitive", 
		      grid->priv->sample_size ? (grid->priv->sample_first_row > 0 ? TRUE : FALSE) : FALSE,  
		      "visible", grid->priv->mode & MG_ACTION_NAVIGATION_ARROWS ? TRUE : FALSE, NULL);
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetNextChunck");
	g_object_set (G_OBJECT (action), "sensitive", 
		      grid->priv->sample_size ? (grid->priv->sample_last_row < row_count ? TRUE : FALSE) : FALSE,  
		      "visible", grid->priv->mode & MG_ACTION_NAVIGATION_ARROWS ? TRUE : FALSE, NULL);
	action = gtk_ui_manager_get_action (grid->priv->uimanager, "/ToolBar/WorkWidgetLastChunck");
	g_object_set (G_OBJECT (action), "sensitive", 
		      grid->priv->sample_size ? (grid->priv->sample_last_row < row_count ? TRUE : FALSE) : FALSE,  
		      "visible", grid->priv->mode & MG_ACTION_NAVIGATION_ARROWS ? TRUE : FALSE, NULL);
	
	if (grid->priv->sample_last_row == 0)
		str = g_strdup_printf (_("? - ? / ?"));
	else
		str = g_strdup_printf ("%d - %d / %d", 
				       grid->priv->sample_first_row + 1, grid->priv->sample_last_row, row_count);
	gtk_label_set_text (GTK_LABEL (grid->priv->nav_current_sample), str);
	g_free (str);
	gtk_widget_set_sensitive (grid->priv->nav_current_sample, TRUE);
	if (grid->priv->mode & MG_ACTION_NAVIGATION_ARROWS)
		gtk_widget_show (grid->priv->nav_current_sample);
	else
		gtk_widget_hide (grid->priv->nav_current_sample);
	gtk_ui_manager_ensure_update (grid->priv->uimanager); 
}
static void     params_set_block_changed (MgWorkGrid *grid, gboolean block);
static gboolean commit_do_run_query (MgWorkGrid *grid, MgQuery *query);
static void
modif_actions_real_do (MgWorkGrid *grid, gchar action)
{
	GtkTreeModel *model;
	GtkTreeIter iter, sel_iter;
	GtkTreeSelection *select;
	gboolean select_ok;
	GtkTreePath *path;
	select = gtk_tree_view_get_selection (GTK_TREE_VIEW (grid->priv->treeview));
	select_ok = gtk_tree_selection_get_selected (select, &model, &sel_iter);
	switch (action) {
	case 'c': /* COMMIT changes */
		if (grid->priv->core->data_rs) {
			GtkTreeIter iter;
			GtkTreeModel *model;
			GtkTreeSelection *selection;
			
			model = gtk_tree_view_get_model (GTK_TREE_VIEW (grid->priv->treeview));
			/* FIRST row */
			if (!gtk_tree_model_get_iter_first (model, &iter))
				return;
			commit_do_work (grid, &iter);
						
			/* OTHER rows */
			while (gtk_tree_model_iter_next (model, &iter)) 
				commit_do_work (grid, &iter);
			/* reset the parameters to their correct value, or render them invalid */
			selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (grid->priv->treeview));
			if (gtk_tree_selection_get_selected (selection, NULL, &iter)) 
				mg_work_grid_set_params_from_iter (grid, &iter);
			else {
				/* render all the parameters invalid */
				GSList *list = grid->priv->core->work_context->parameters;
				while (list) {
					MgParameter *tmp;
					g_object_get (G_OBJECT (list->data), "full_bind", &tmp, NULL);
					if (! tmp) 
						mg_parameter_declare_invalid (MG_PARAMETER (list->data));
					
					list = g_slist_next (list);
				}
			}
			
			refresh_all_rows (grid, TRUE);
		}
		break;
	case 'r': /* RESET grid */
		refresh_all_rows (grid, FALSE);
		break;
	case 'i': /* INSERT */
		/* Add a row to the model with -1 as the row number, if possible */
		if (select_ok)
			gtk_list_store_insert_after (GTK_LIST_STORE (model), &iter, &sel_iter);
		else
			gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model), &iter, COLUMN_ROW_NUM, -1, -1);
		gtk_tree_selection_select_iter (select, &iter);
		path = gtk_tree_model_get_path (model, &iter);
		gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (grid->priv->treeview), path, NULL, FALSE, 0, 0);
		gtk_tree_path_free (path);
		/* for params which will be invalid for this row, set them to the default value */
		grid_set_user_modifs_to_default_if_notnull (grid, model, &iter, grid->priv->core);
		break;
	case 'd': /* DELETE */
		if (select_ok) {
			gint row;
			ModelUserModifiedRow *user_modifs;
			gtk_tree_model_get (model, &sel_iter, COLUMN_ROW_NUM, &row, 
					    COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
			if (row >= 0) {
				if (!user_modifs) {
					user_modifs = grid_model_make_user_modifs (grid, row);
					user_modifs->to_be_deleted = TRUE;
					gtk_list_store_set (GTK_LIST_STORE (model), &sel_iter, 
							    COLUMN_USER_MODIFS_ROW, user_modifs, -1);
				}
				else {
					GtkTreePath *path = gtk_tree_model_get_path (model, &sel_iter);
					user_modifs->to_be_deleted = TRUE;
					gtk_tree_model_row_changed (model, path, &sel_iter);
					gtk_tree_path_free (path);
				}
			}
			else {
				grid_clean_data_model_row (grid, model, &sel_iter);
				gtk_list_store_remove (GTK_LIST_STORE (model), &sel_iter);
			}
		}
		break;
	case 'u': /* UNDELETE */
		if (select_ok) {
			gint row;
			ModelUserModifiedRow *user_modifs;
			gtk_tree_model_get (model, &sel_iter, COLUMN_ROW_NUM, &row, 
					    COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
			if (row >= 0) {
				if (user_modifs) {
					if (user_modifs->user_values) {
						GtkTreePath *path = gtk_tree_model_get_path (model, &sel_iter);
						user_modifs->to_be_deleted = FALSE;
						gtk_tree_model_row_changed (model, path, &sel_iter);
						gtk_tree_path_free (path);
					}
					else {
						grid->priv->modified_rows = g_slist_remove (grid->priv->modified_rows, 
											    user_modifs);
						user_modifs_row_free (user_modifs);
						gtk_list_store_set (GTK_LIST_STORE (model), &sel_iter, 
								    COLUMN_USER_MODIFS_ROW, NULL, -1);
					}
				}
			}
			else
				g_assert_not_reached ();
		}
		break;
	default:
		g_assert_not_reached ();
		break;
	}
}
static void
params_set_block_changed (MgWorkGrid *grid, gboolean block)
{
	GSList *list;
	list = grid->priv->core->work_context->nodes;
	while (list) {
		MgContextNode *node;
		node = MG_CONTEXT_NODE (list->data);
		if (node->param)
			if (block)
				mg_base_block_changed (MG_BASE (node->param));
			else
				mg_base_unblock_changed (MG_BASE (node->param));
		else {
			GSList *plist = node->params;
			while (plist) {
				if (block)
					mg_base_block_changed (MG_BASE (plist->data));
				else
					mg_base_unblock_changed (MG_BASE (plist->data));
				plist = g_slist_next (plist);
			}
		}
		list = g_slist_next (list);
	}
}
static gboolean
commit_do_work (MgWorkGrid *grid, GtkTreeIter *iter)
{
	GtkTreeModel *model;
	gboolean to_be_deleted = FALSE;
	MgQuery *query = NULL;
	gint row;
	ModelUserModifiedRow *user_modifs;
	model = gtk_tree_view_get_model (GTK_TREE_VIEW (grid->priv->treeview));
	gtk_tree_model_get (model, iter, 
			    COLUMN_ROW_NUM, &row,
			    COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
	if (user_modifs)
		to_be_deleted = user_modifs->to_be_deleted;
	if (to_be_deleted)
		query = grid->priv->core->query_delete;
	else {
		if (row < 0)
			query = grid->priv->core->query_insert;
		else {
			if (user_modifs)
				query = grid->priv->core->query_update;
		}
	}
	
	if (query) {
		params_set_block_changed (grid, TRUE);
		mg_work_grid_set_params_from_iter (grid, iter);
		if (commit_do_run_query (grid, query)) 
			grid_clean_data_model_row (grid, model, iter);
		params_set_block_changed (grid, FALSE);
	}
	return query ? TRUE : FALSE;
}
/*
 * Runs the provided query which is part of the priv->core's modif queries
 */
static gboolean
commit_do_run_query (MgWorkGrid *grid, MgQuery *query)
{
	return mg_util_query_execute_modif (query, grid->priv->core->work_context,
					    grid->priv->mode & MG_ACTION_ASK_CONFIRM_INSERT,
					    grid->priv->mode & MG_ACTION_ASK_CONFIRM_UPDATE,
					    grid->priv->mode & MG_ACTION_ASK_CONFIRM_DELETE,
					    GTK_WIDGET (grid), NULL, NULL);
}
/*
 * grid_model_make_user_modifs
 *
 * Creates a new ModelUserModifiedRow, and fills the pkey part with the 
 * PK fields of the corresponding @row in the grid->priv->core->data_rs if @row >= 0
 */
static ModelUserModifiedRow *
grid_model_make_user_modifs (MgWorkGrid *grid, gint row)
{
	ModelUserModifiedRow *user_modifs;
	user_modifs = g_new0 (ModelUserModifiedRow, 1);
	grid->priv->modified_rows = g_slist_append (grid->priv->modified_rows, user_modifs);
	
	if (row >= 0) {
		/* store a copy of all the pk fields' values into the ModelUserModifiedRow structure */
		GSList *pk_fields = mg_query_get_target_pkfields (grid->priv->core->query_select, 
								  grid->priv->core->modif_target);
		if (pk_fields) {
			GSList *flist = pk_fields;
			GSList *pk_values = NULL;
			while (flist) {
				const GdaValue *value;
				gint col = mg_entity_get_field_index (MG_ENTITY (grid->priv->core->query_select),
								      MG_FIELD (flist->data));
				value = mg_resultset_get_gdavalue (grid->priv->core->data_rs, row, col);
				pk_values = g_slist_append (pk_values, gda_value_copy (value));
				
				flist = g_slist_next (flist);
			}
			g_slist_free (pk_fields);
			
			user_modifs->pkey = pk_values;
		}
	}
	return user_modifs;
}
/*
 * grid_model_get_user_modifs
 * From the data model @tree_model, tries to find a ModelUserModifiedValue structure which
 * represents the data as modified by the user, for the provided @context_node.
 *
 * If @create_if_needed is TRUE, the a new ModelUserModifiedValue structure will be created
 * and returned.
 *
 * Returns: a ModelUserModifiedValue structure, or %NULL
 */
static ModelUserModifiedValue *
grid_model_get_user_modifs (MgWorkGrid *grid, GtkTreeModel *tree_model, GtkTreeIter *iter,
			    MgContextNode *context_node, gboolean create_if_needed)
{
	ModelUserModifiedRow *user_modifs;
	ModelUserModifiedValue *user_value = NULL;
	gint row;
	
	gtk_tree_model_get (tree_model, iter, COLUMN_ROW_NUM, &row, COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
	if (user_modifs) {
		GSList *list;
		list = user_modifs->user_values;
		while (list && !user_value) {
			if (MODEL_USER_MODIFIED_VALUE (list->data)->context_node == context_node)
				user_value = MODEL_USER_MODIFIED_VALUE (list->data);
			list = g_slist_next (list);
		}		
	}
	if (!user_value && create_if_needed) {
		if (!user_modifs) 
			user_modifs = grid_model_make_user_modifs (grid, row);
		
		user_value = g_new0 (ModelUserModifiedValue, 1);
		user_modifs->user_values = g_slist_append (user_modifs->user_values, user_value);
		user_value->context_node = context_node;
		user_value->attributes = 0;
		user_value->value = NULL;
		gtk_list_store_set (GTK_LIST_STORE (tree_model), iter, COLUMN_USER_MODIFS_ROW, user_modifs, -1);
	}
	return user_value;
}
/*
 * grid_set_user_modifs_to_default_if_notnull
 * If core->work_context has some 'proposed' values for some parameters, then set them.
 *
 * Also, if some of the parameters in core->work_context are invalid, and if some default values are
 * available, creates ModelUserModifiedValue structures to tell that the default value is required
 * (as if it had been done by the user).
 */
static void
grid_set_user_modifs_to_default_if_notnull (MgWorkGrid *grid, GtkTreeModel *tree_model, GtkTreeIter *iter, 
					    MgWorkCore *core)
{
	GSList *list = core->params_modif_queries_value_providers;
	MgParameter *param;
	MgContextNode *node;
	const GdaValue *value;
	ModelUserModifiedValue *modval;
	while (list) {
		param = MG_PARAMETER (list->data);
		/* 'proposed' value in the context */ 
		value = mg_context_get_param_default_value (core->work_context, param);
		if (value) {
			node = mg_context_find_node_for_param (core->work_context, param);
			modval = grid_model_get_user_modifs (grid, tree_model, iter, node, TRUE);
			grid_modif_struct_set_value (grid, modval, tree_model, iter, core, value);
		}
		else {
			/* Available default value */
			value = mg_parameter_get_default_value (param);
			if (mg_parameter_get_not_null (param) && value && !gda_value_is_null (value)) {
				node = mg_context_find_node_for_param (core->work_context, param);
				g_assert (node);
				modval = grid_model_get_user_modifs (grid, tree_model, iter, node, TRUE);
				grid_modif_struct_set_status (grid, modval, tree_model, iter, core, 
							      MG_DATA_ENTRY_IS_DEFAULT);
			}
			else {
				/* If we have a MgDataCellRendererCombo then ask for the first record 
				 * of choice, if available */
				node = mg_context_find_node_for_param (core->work_context, param);
				
				if (! node->param) {
					GdaValue *first_value;
					ColumnData *column_data;
					column_data = get_column_data (grid, node);
					first_value = mg_data_cell_renderer_combo_get_first_value_available 
						(MG_DATA_CELL_RENDERER_COMBO (column_data->data_cell));
					if (first_value) {
						modval = grid_model_get_user_modifs (grid, tree_model, 
										     iter, node, TRUE);
						grid_modif_struct_set_value (grid, modval, tree_model,
									     iter, core, first_value);
						gda_value_free (first_value);
					}
				}
			}
		}
		
		list = g_slist_next (list);
	}
}
static const GdaValue *
get_value_from_recordset (GtkTreeModel *tree_model, GtkTreeIter *iter, MgWorkCore *core, MgParameter *context_param)
{
	gint col;
	MgWorkCoreNode *cnode;
	gint row;
	
	g_return_val_if_fail (context_param, NULL);
	gtk_tree_model_get (tree_model, iter, COLUMN_ROW_NUM, &row, -1);
	cnode = mg_work_core_find_core_node (core, context_param);
	g_assert (cnode);
	col = cnode->position;
	g_assert (col >= 0);
		
	return mg_resultset_get_gdavalue (core->data_rs, row, col);	
}
static void
remove_user_modif (MgWorkGrid *grid, ModelUserModifiedValue *user_value, GtkTreeModel *tree_model, GtkTreeIter *iter)
{
	ModelUserModifiedRow *user_modifs;
	
	gtk_tree_model_get (tree_model, iter, COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
	g_return_if_fail (user_modifs);
	g_return_if_fail (g_slist_find (user_modifs->user_values, user_value));
	user_modifs->user_values = g_slist_remove (user_modifs->user_values, user_value);
	if (user_value->value)
		gda_value_free (user_value->value);
	g_free (user_value);
	/* if there is not anymore any user modified values, then get rid of the ModelUserModifiedRow */
	if (!user_modifs->user_values && !user_modifs->to_be_deleted) {
		grid->priv->modified_rows = g_slist_remove (grid->priv->modified_rows, user_modifs);
		user_modifs_row_free (user_modifs);
		gtk_list_store_set (GTK_LIST_STORE (tree_model), iter, COLUMN_USER_MODIFS_ROW, NULL, -1);
	}
}
/*
 * grid_modif_struct_set_value
 * Updates @user_modif with @value, and delete it if @value is the same as the one it already has.
 */
static void
grid_modif_struct_set_value (MgWorkGrid *grid, ModelUserModifiedValue *user_modif, 
			     GtkTreeModel *tree_model, GtkTreeIter *iter,
			     MgWorkCore *core, const GdaValue *value)
{
	guint attrs = 0;
	gint row;
	GtkTreePath *path;
	/* see if the value is the same as the original one, and if that is the case, 
	 * get rid of the 'user_modif' and return.
	 * row == -1 if a new row has been added (one which is not in the core->resultset) */
	gtk_tree_model_get (tree_model, iter, COLUMN_ROW_NUM, &row, -1);
	if (row > -1) {
		if (user_modif->context_node->param) { /* one single param */
			const GdaValue *value_orig;
			
			value_orig = get_value_from_recordset (tree_model, iter, core, user_modif->context_node->param);
			g_assert (value_orig);
			if (((!value || gda_value_is_null (value)) && (!value_orig || gda_value_is_null (value_orig))) ||
			    (value && value_orig && (gda_value_get_type (value) == gda_value_get_type (value_orig)) &&
			     !gda_value_compare (value, value_orig))) {
				remove_user_modif (grid, user_modif, tree_model, iter);
				return;
			}
		}
		else { /* multiple parameters */
			gboolean same_as_orig = TRUE;
			GSList *params;
			GList *user_modifs;
			
			g_assert (gda_value_isa (value, GDA_VALUE_TYPE_LIST));
			
			params = user_modif->context_node->params;
			user_modifs = (GList *) gda_value_get_list (value);
			while (params && same_as_orig) {
				const GdaValue *value = NULL;
				const GdaValue *orig_value;
				
				value = g_list_nth_data (user_modifs, 
							 GPOINTER_TO_INT (g_hash_table_lookup 
									  (user_modif->context_node->params_pos_in_query, 
									   params->data)));
				orig_value = get_value_from_recordset (tree_model, iter, core, MG_PARAMETER (params->data));
				
				if (((!value || gda_value_is_null (value)) && 
				     (!orig_value || gda_value_is_null (orig_value))) ||
				    (value && orig_value && 
				     (gda_value_get_type (value) == gda_value_get_type (orig_value)) &&
				     !gda_value_compare (value, orig_value)))
					same_as_orig = TRUE;
				else
					same_as_orig = FALSE;
				
				params = g_slist_next (params);
			}
			if (same_as_orig) {
				remove_user_modif (grid, user_modif, tree_model, iter);
				return;
			}
		}
	}
	/* at this point the value is different from the original one, or there is no original value */
	if (user_modif->value)
		gda_value_free (user_modif->value);
	
	user_modif->value = gda_value_copy (value);
	attrs = user_modif->attributes;
	/* resetting some attributes */
	attrs = attrs & ~MG_DATA_ENTRY_IS_NULL;
	attrs = attrs & ~MG_DATA_ENTRY_IS_UNCHANGED;
	attrs = attrs & ~MG_DATA_ENTRY_IS_DEFAULT;
	/* setting new attributes */
	if (!value || gda_value_is_null (value))
		attrs = attrs | MG_DATA_ENTRY_IS_NULL;
	user_modif->attributes = attrs;
	/* signal the model for data update */
	path = gtk_tree_model_get_path (tree_model, iter);
	gtk_tree_model_row_changed (tree_model, path, iter);
	gtk_tree_path_free (path);
}
/*
 * grid_clean_data_model_row
 *
 * Cleans the data allocated inside @tree_model (ModelUserModifiedValue structures), at row pointed
 * by @iter; and sets the COLUMN_ROW_NUM to -2.
 */
static void 
grid_clean_data_model_row (MgWorkGrid *grid, GtkTreeModel *tree_model, GtkTreeIter *iter)
{
	ModelUserModifiedRow *user_modifs;
	gtk_tree_model_get (tree_model, iter, COLUMN_USER_MODIFS_ROW, &user_modifs, -1);
	/* REM: user_modifs can be NULL if the user has not changed anything, and no safe default was set */
	if (!user_modifs)
		return;
	grid->priv->modified_rows = g_slist_remove (grid->priv->modified_rows, user_modifs);
	user_modifs_row_free (user_modifs);
	gtk_list_store_set (GTK_LIST_STORE (tree_model), iter, COLUMN_ROW_NUM, -2, COLUMN_USER_MODIFS_ROW, NULL, -1);
}
static void
grid_modif_struct_set_status (MgWorkGrid *grid, ModelUserModifiedValue *user_modif,
			      GtkTreeModel *tree_model, GtkTreeIter *iter,
			      MgWorkCore *core, MgDataEntryAttribute requested_status)
{
	g_return_if_fail (user_modif);
	guint attrs;
	GtkTreePath *path;
	gint row;
	switch (requested_status) {
	case MG_DATA_ENTRY_IS_NULL:
		/* see if  the original value is NULL
		 * to get rid of the 'user_modif' and return. */
		gtk_tree_model_get (tree_model, iter, COLUMN_ROW_NUM, &row, -1);
		if (row > -1) {
			if (user_modif->context_node->param) { /* one single param */
				const GdaValue *value_orig;
				
				value_orig = get_value_from_recordset (tree_model, iter, core, user_modif->context_node->param);
				g_assert (value_orig);
				
				if (!value_orig || gda_value_is_null (value_orig)) {
					remove_user_modif (grid, user_modif, tree_model, iter);
					return;
				}
			}
			else { /* multiple parameters */
				GSList *params;
				gboolean allnull = TRUE;
				const GdaValue *orig_value;
				
				params = user_modif->context_node->params;
				while (params && allnull) {
					orig_value = get_value_from_recordset (tree_model, iter, core, 
									       MG_PARAMETER (params->data));
					
					if (orig_value && !gda_value_is_null (orig_value))
						allnull = FALSE;
					
					params = g_slist_next (params);
				}
				
				if (allnull) {
					remove_user_modif (grid, user_modif, tree_model, iter);
					return;
				}
			}
		}
		if (user_modif->value) {
			gda_value_free (user_modif->value);
			user_modif->value = NULL;
		}
		user_modif->value = gda_value_new_null ();
		user_modif->attributes = user_modif->attributes | MG_DATA_ENTRY_IS_NULL;
		/* signal the model for data update */
		path = gtk_tree_model_get_path (tree_model, iter);
		gtk_tree_model_row_changed (tree_model, path, iter);
		gtk_tree_path_free (path);
		break;
	case MG_DATA_ENTRY_IS_DEFAULT:
		attrs = user_modif->attributes;
		if (user_modif->context_node->param) { /* single parameter */
			const GdaValue *value = mg_parameter_get_default_value (user_modif->context_node->param);
			if (user_modif->value) {
				gda_value_free (user_modif->value);
				user_modif->value = NULL;
			}
			if (value && 
			    (gda_value_get_type (value) == mg_server_data_type_get_gda_type (mg_parameter_get_data_type 
			     (user_modif->context_node->param)))) {
				user_modif->value = gda_value_copy (value);
			}
			if (! user_modif->value)
				user_modif->value = gda_value_new_null ();
			attrs = attrs | MG_DATA_ENTRY_IS_DEFAULT;
		}
		else { /* several parameters constrained by a query */
			TO_IMPLEMENT;
			attrs = attrs | MG_DATA_ENTRY_IS_DEFAULT;
		}
		user_modif->attributes = attrs;
		/* signal the model for data update */
		path = gtk_tree_model_get_path (tree_model, iter);
		gtk_tree_model_row_changed (tree_model, path, iter);
		gtk_tree_path_free (path);
		break;
	case MG_DATA_ENTRY_IS_UNCHANGED:
		remove_user_modif (grid, user_modif, tree_model, iter);
		break;
	default:
		g_assert_not_reached ();
		break;
	}
}
/*
 * MgWorkWidget interface implementation
 */
static void
mg_work_grid_run (MgWorkWidget *iface, guint mode)
{
	MgWorkGrid *grid;
	g_return_if_fail (iface && IS_MG_WORK_GRID (iface));
	grid = MG_WORK_GRID (iface);
	g_return_if_fail (grid->priv);
	g_return_if_fail (grid->priv->core->query_select);
	/* REM : we don't check for missing parameters: the user must do it himself
	 * using the mg_work_widget_get_exec_context() function and mg_context_is_valid() */
	/*
	 * Signals connecting
	 */
	if (grid->priv->core->args_context)
		g_signal_connect (G_OBJECT (grid->priv->core->args_context), "changed",
				  G_CALLBACK (arg_param_changed_cb), grid);
	grid->priv->has_run = TRUE;
	/*
	 * Actual start
	 */
	if (mode)
		grid->priv->mode = mode;
	refresh_all_rows (grid, FALSE);
}
static void
mg_work_grid_set_mode (MgWorkWidget *iface, guint mode)
{
	MgWorkGrid *grid;
	g_return_if_fail (iface && IS_MG_WORK_GRID (iface));
	grid = MG_WORK_GRID (iface);
	g_return_if_fail (grid->priv);
	grid->priv->mode = mode;
	/* updating the various possible actions */
	modif_buttons_update (grid);
}
static void
mg_work_grid_set_entry_editable (MgWorkWidget *iface, MgQfield *field, gboolean editable)
{
	MgWorkGrid *grid;
	MgParameter *param;
	ColumnData *column_data;
	MgContextNode *node;
	g_return_if_fail (iface && IS_MG_WORK_GRID (iface));
	grid = MG_WORK_GRID (iface);
	g_return_if_fail (grid->priv);
	
	param = mg_work_grid_get_param_for_field (iface, field, NULL, FALSE);
	node = mg_context_find_node_for_param (grid->priv->core->work_context, param);
	g_return_if_fail (node);
	column_data = get_column_data (grid, node);
	g_return_if_fail (column_data);
	if (editable)
		column_data->data_locked = grid->priv->core->modif_target ? FALSE : TRUE;
	else
		column_data->data_locked = TRUE;
	g_object_set (G_OBJECT (column_data->data_cell), "editable", !column_data->data_locked, NULL);
}
static void
mg_work_grid_show_entry_actions (MgWorkWidget *iface, MgQfield *field, gboolean show_actions)
{
	MgParameter *param;
	MgContextNode *cnode;
	ColumnData *cdata;
	MgWorkGrid *grid;
	g_return_if_fail (iface && IS_MG_WORK_GRID (iface));
	grid = MG_WORK_GRID (iface);
	g_return_if_fail (grid->priv);
	if (!field)
		/* TODO: if @field is NULL, then apply to all the fields */
		TO_IMPLEMENT;
	
	param = mg_work_grid_get_param_for_field (iface, field, NULL, FALSE);
	g_return_if_fail (param);
	
	cnode = mg_context_find_node_for_param (grid->priv->core->work_context, param);
	g_return_if_fail (cnode);
	cdata = get_column_data (grid, cnode);
	g_return_if_fail (cdata);
	if (show_actions != cdata->info_shown) {
		cdata->info_shown = show_actions;
		g_object_set (G_OBJECT (cdata->info_cell), "visible", cdata->info_shown, NULL);
	}
}
static void
mg_work_grid_show_global_actions (MgWorkWidget *iface, gboolean show_actions)
{
	MgWorkGrid *grid;
	g_return_if_fail (iface && IS_MG_WORK_GRID (iface));
	grid = MG_WORK_GRID (iface);
	g_return_if_fail (grid->priv);
	if (show_actions)
		gtk_widget_show (grid->priv->modif_all);
	else
		gtk_widget_hide (grid->priv->modif_all);
}
static MgParameter *
mg_work_grid_get_param_for_field (MgWorkWidget *iface, MgQfield *field, const gchar *field_name, gboolean in_exec_context)
{
	MgWorkGrid *grid;
	MgParameter *param = NULL;
	g_return_val_if_fail (iface && IS_MG_WORK_GRID (iface), NULL);
	grid = MG_WORK_GRID (iface);
	g_return_val_if_fail (grid->priv, NULL);
	g_return_val_if_fail (field || (field_name && *field_name), NULL);
	if (field) {
		/* use the 'field' argument */
		g_return_val_if_fail (field && IS_MG_QFIELD (field), NULL);
		param = mg_work_core_find_param (grid->priv->core, field, in_exec_context);
	}
	else {
		/* use the 'field_name' argument */
		MgField *f = mg_entity_get_field_by_name (MG_ENTITY (grid->priv->core->query_select), field_name);
		if (f)
			param = mg_work_core_find_param (grid->priv->core, MG_QFIELD (f), in_exec_context);	
	}
	return param;
}
static gboolean
mg_work_grid_has_been_changed (MgWorkWidget *iface)
{
	MgWorkGrid *grid;
	g_return_val_if_fail (iface && IS_MG_WORK_GRID (iface), FALSE);
	grid = MG_WORK_GRID (iface);
	g_return_val_if_fail (grid->priv, FALSE);
	return grid->priv->modified_rows ? TRUE : FALSE;
}
static MgContext *
mg_work_grid_get_exec_context (MgWorkWidget *iface)
{
	MgWorkGrid *grid;
	g_return_val_if_fail (iface && IS_MG_WORK_GRID (iface), NULL);
	grid = MG_WORK_GRID (iface);
	g_return_val_if_fail (grid->priv, NULL);
	
	return grid->priv->core->args_context;
}
static GtkActionGroup *
mg_work_grid_get_actions_group (MgWorkWidget *iface)
{
	MgWorkGrid *grid;
	
	g_return_val_if_fail (iface && IS_MG_WORK_GRID (iface), NULL);
	grid = MG_WORK_GRID (iface);
	g_return_val_if_fail (grid->priv, NULL);
	return grid->priv->actions_group;
}
/**
 * mg_work_grid_set_sample_size
 * @grid:
 * @sample_size:
 *
 */
void
mg_work_grid_set_sample_size (MgWorkGrid *grid, gint sample_size)
{
	g_return_if_fail (grid && IS_MG_WORK_GRID (grid));
	g_return_if_fail (grid->priv);
	if (sample_size <0)
		grid->priv->sample_size = 0;
	else
		grid->priv->sample_size = sample_size;
	update_simple_grid (grid, FALSE);
}
/**
 * mg_work_grid_set_sample_start
 * @grid:
 * @sample_start:
 *
 */
void
mg_work_grid_set_sample_start (MgWorkGrid *grid, gint sample_start)
{
	g_return_if_fail (grid && IS_MG_WORK_GRID (grid));
	g_return_if_fail (grid->priv);
	
	if (sample_start <0)
		grid->priv->sample_first_row = 0;
	else
		grid->priv->sample_first_row = sample_start;
	update_simple_grid (grid, FALSE);
}
 
     |