summaryrefslogtreecommitdiff
path: root/svx/source/fmcomp/fmgridcl.cxx
blob: 91c90af0f1ba5fd84d61271b4e1611d5beecaa98 (plain)
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
/*************************************************************************
 *
 *  $RCSfile: fmgridcl.cxx,v $
 *
 *  $Revision: 1.17 $
 *
 *  last change: $Author: fs $ $Date: 2001-03-26 15:08:15 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#pragma hdrstop

#ifndef _SVX_FMGRIDCL_HXX
#include "fmgridcl.hxx"
#endif
#ifndef _SVX_GRIDCELL_HXX
#include "gridcell.hxx"
#endif
#ifndef _SVX_FMURL_HXX
#include "fmurl.hxx"
#endif

#ifndef _SVX_FMPROP_HXX
#include "fmprop.hxx"
#endif

#ifndef _FMTFIELD_HXX_
#include <svtools/fmtfield.hxx>
#endif

#include <math.h>
#ifndef _SVX_FMPROP_HRC
#include "fmprop.hrc"
#endif
#ifndef _SVX_DBEXCH_HRC
#include "dbexch.hrc"
#endif

#ifndef _SFXVIEWFRM_HXX
#include <sfx2/viewfrm.hxx>
#endif

#ifndef _COM_SUN_STAR_UNO_XNAMINGSERVICE_HPP_
#include <com/sun/star/uno/XNamingService.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBCX_XDELETEROWS_HPP_
#include <com/sun/star/sdbcx/XDeleteRows.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
#include <com/sun/star/sdb/CommandType.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
#include <com/sun/star/sdbc/DataType.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_XPREPAREDSTATEMENT_HPP_
#include <com/sun/star/sdbc/XPreparedStatement.hpp>
#endif
#ifndef _COM_SUN_STAR_SDDB_XCOLUMNSSUPPLIER_HPP_
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#endif
#ifndef _COM_SUN_STAR_SDDB_XTABLESSUPPLIER_HPP_
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#endif
#ifndef _COM_SUN_STAR_VIEW_XSELECTIONSUPPLIER_HPP_
#include <com/sun/star/view/XSelectionSupplier.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_ROWCHANGEACTION_HPP_
#include <com/sun/star/sdb/RowChangeAction.hpp>
#endif
#ifndef _COM_SUN_STAR_FORM_XCONFIRMDELETELISTENER_HPP_
#include <com/sun/star/form/XConfirmDeleteListener.hpp>
#endif
#ifndef _COM_SUN_STAR_FORM_XGRIDCOLUMNFACTORY_HPP_
#include <com/sun/star/form/XGridColumnFactory.hpp>
#endif
#ifndef _COM_SUN_STAR_FORM_XFORMCOMPONENT_HPP_
#include <com/sun/star/form/XFormComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATSSUPPLIER_HPP_
#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
#endif
#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATS_HPP_
#include <com/sun/star/util/XNumberFormats.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XPERSISTOBJECT_HPP_
#include <com/sun/star/io/XPersistObject.hpp>
#endif

#ifndef _SV_EXCHANGE_HXX //autogen
#include <vcl/exchange.hxx>
#endif

#ifndef _SV_HELP_HXX //autogen
#include <vcl/help.hxx>
#endif

#ifndef _DTRANS_HXX //autogen
#include <so3/dtrans.hxx>
#endif

#ifndef _SFXDISPATCH_HXX //autogen
#include <sfx2/dispatch.hxx>
#endif

#ifndef _SFXENUMITEM_HXX //autogen
#include <svtools/eitem.hxx>
#endif

#ifndef _SV_MENU_HXX //autogen
#include <vcl/menu.hxx>
#endif

#ifndef _SV_IMAGE_HXX //autogen
#include <vcl/image.hxx>
#endif

#ifndef _LONGCURR_HXX
#include <vcl/longcurr.hxx>
#endif

#ifndef _SVX_FMSERVS_HXX
#include "fmservs.hxx"
#endif

#ifndef _SVX_FMITEMS_HXX
#include "fmitems.hxx"
#endif

#ifndef _SVX_FMRESIDS_HRC
#include "fmresids.hrc"
#endif

#ifndef _SVX_SVXIDS_HRC
#include "svxids.hrc"
#endif

#ifndef _SHL_HXX
#include <tools/shl.hxx>
#endif

#ifndef _SVX_DIALMGR_HXX
#include "dialmgr.hxx"
#endif

#ifndef _SVX_GRIDCOLS_HXX
#include "gridcols.hxx"
#endif

#ifndef _SVSTDARR_STRINGSDTOR
#define _SVSTDARR_STRINGSDTOR
#define _SVSTDARR_ULONGS
#include <svtools/svstdarr.hxx>
#endif

#ifndef _NUMUNO_HXX //autogen
#include <svtools/numuno.hxx>
#endif

#ifndef _SVX_FMGRIDIF_HXX
#include "fmgridif.hxx"
#endif

#ifndef _SVX_SHOWCOLS_HXX
#include "showcols.hxx"
#endif

#ifndef _COMPHELPER_EXTRACT_HXX_
#include <comphelper/extract.hxx>
#endif

#ifndef _CONNECTIVITY_DBTOOLS_HXX_
#include <connectivity/dbtools.hxx>
#endif
#ifndef _COMPHELPER_PROPERTY_HXX_
#include <comphelper/property.hxx>
#endif
#ifndef _COMPHELPER_NUMBERS_HXX_
#include <comphelper/numbers.hxx>
#endif
#ifndef _ISOLANG_HXX
#include <tools/isolang.hxx>
#endif

#ifndef _TRACE_HXX_
#include "trace.hxx"
#endif

#ifndef _SVX_DBAEXCHANGE_HXX_
#include "dbaexchange.hxx"
#endif

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::form;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::container;
using namespace ::cppu;
using namespace ::svxform;
using namespace ::svx;

//==============================================================================
//------------------------------------------------------------------------------
::rtl::OUString FieldServiceFromId(sal_Int32 nID)
{
    switch (nID)
    {
        case SID_FM_EDIT            : return FM_COL_TEXTFIELD;
        case SID_FM_COMBOBOX        : return FM_COL_COMBOBOX;
        case SID_FM_LISTBOX         : return FM_COL_LISTBOX;
        case SID_FM_CHECKBOX        : return FM_COL_CHECKBOX;
        case SID_FM_DATEFIELD       : return FM_COL_DATEFIELD;
        case SID_FM_TIMEFIELD       : return FM_COL_TIMEFIELD;
        case SID_FM_NUMERICFIELD    : return FM_COL_NUMERICFIELD;
        case SID_FM_CURRENCYFIELD   : return FM_COL_CURRENCYFIELD;
        case SID_FM_PATTERNFIELD    : return FM_COL_PATTERNFIELD;
        case SID_FM_FORMATTEDFIELD  : return FM_COL_FORMATTEDFIELD;
    }
    return ::rtl::OUString();
}

//==============================================================================
//------------------------------------------------------------------------------
const sal_Int16 nChangeTypeOffset = 1000;
void SetMenuItem(const ImageList& rList, sal_uInt16 nID, Menu* pMenu, Menu& rNewMenu, sal_Bool bDesignMode = sal_True, sal_Int16 nOffset = nChangeTypeOffset)
{
    pMenu->SetItemImage(nID, rList.GetImage(nID));
    pMenu->EnableItem(nID, bDesignMode);
    rNewMenu.InsertItem(nID + nOffset, pMenu->GetItemText(nID));
    rNewMenu.SetItemImage(nID + nOffset, rList.GetImage(nID));
    rNewMenu.SetHelpId(nID + nOffset, pMenu->GetHelpId(nID));
    rNewMenu.EnableItem(nID + nOffset, bDesignMode);
}

//------------------------------------------------------------------------------
FmGridHeader::FmGridHeader( BrowseBox* pParent, WinBits nWinBits)
        :DbBrowseHeader(pParent, nWinBits)
        ,DropTargetHelper(this)
{
    EnableDrop(sal_True);
}

//------------------------------------------------------------------------------
sal_uInt16 FmGridHeader::GetModelColumnPos(sal_uInt16 nId) const
{
    return static_cast<FmGridControl*>(GetParent())->GetModelColumnPos(nId);
}

//------------------------------------------------------------------------------
void FmGridHeader::Select()
{
    DbBrowseHeader::Select();
    if (static_cast<FmGridControl*>(GetParent())->IsDesignMode())
    {
        sal_uInt16 nPos = GetModelColumnPos(GetCurItemId());

        Reference< ::com::sun::star::container::XIndexContainer >  xColumns(static_cast<FmGridControl*>(GetParent())->GetPeer()->getColumns());
        Reference< ::com::sun::star::beans::XPropertySet> xColumn;
        ::cppu::extractInterface(xColumn, xColumns->getByIndex(nPos));
        Reference< ::com::sun::star::view::XSelectionSupplier >  xSelSupplier(xColumns, UNO_QUERY);
        if (xSelSupplier.is())
            xSelSupplier->select(makeAny(xColumn));
    }
}

//------------------------------------------------------------------------------
void FmGridHeader::RequestHelp( const HelpEvent& rHEvt )
{
    sal_uInt16 nItemId = GetItemId( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ) );
    if ( nItemId )
    {
        if ( rHEvt.GetMode() & (HELPMODE_QUICK | HELPMODE_BALLOON) )
        {
            Rectangle aItemRect = GetItemRect( nItemId );
            Point aPt = OutputToScreenPixel( aItemRect.TopLeft() );
            aItemRect.Left()   = aPt.X();
            aItemRect.Top()    = aPt.Y();
            aPt = OutputToScreenPixel( aItemRect.BottomRight() );
            aItemRect.Right()  = aPt.X();
            aItemRect.Bottom() = aPt.Y();

            sal_uInt16 nPos = GetModelColumnPos(nItemId);
            Reference< ::com::sun::star::container::XIndexContainer >  xColumns(static_cast<FmGridControl*>(GetParent())->GetPeer()->getColumns());
            try
            {
                Reference< ::com::sun::star::beans::XPropertySet >  xColumn;
                ::cppu::extractInterface(xColumn, xColumns->getByIndex(nPos));
                ::rtl::OUString aHelpText;
                xColumn->getPropertyValue(FM_PROP_HELPTEXT) >>= aHelpText;
                if ( aHelpText.len() )
                {
                    if ( rHEvt.GetMode() & HELPMODE_BALLOON )
                        Help::ShowBalloon( this, aItemRect.Center(), aItemRect, aHelpText );
                    else
                        Help::ShowQuickHelp( this, aItemRect, aHelpText );
                    return;
                }
            }
            catch(Exception&)
            {
                return;
            }
        }
    }
    DbBrowseHeader::RequestHelp( rHEvt );
}

//------------------------------------------------------------------------------
sal_Int8 FmGridHeader::AcceptDrop( const AcceptDropEvent& rEvt )
{
    // drop allowed in design mode only
    if (!static_cast<FmGridControl*>(GetParent())->IsDesignMode())
        return DND_ACTION_NONE;

    // search for recognized formats
    const DataFlavorExVector& rFlavors = GetDataFlavorExVector();
    if (OColumnTransferable::canExtractColumnDescriptor(rFlavors, CTF_FIELD_DESCRIPTOR))
        return DND_ACTION_COPY;

    return DND_ACTION_NONE;
}

//------------------------------------------------------------------------------
sal_Int8 FmGridHeader::ExecuteDrop( const ExecuteDropEvent& _rEvt )
{
    if (!static_cast<FmGridControl*>(GetParent())->IsDesignMode())
        return DND_ACTION_NONE;

    TransferableDataHelper aDroppedData(_rEvt.maDropEvent.Transferable);

    ::rtl::OUString sDatasouce, sCommand, sFieldName;
    sal_Int32       nCommandType = CommandType::COMMAND;
    if (!OColumnTransferable::extractColumnDescriptor(aDroppedData, sDatasouce, nCommandType, sCommand, sFieldName))
    {
        DBG_ERROR("FmGridHeader::ExecuteDrop: should never have reached this (no extractable format)!");
        return DND_ACTION_NONE;
    }

    if (!sFieldName.getLength() || !sCommand.getLength() || !sDatasouce.getLength())
        return DND_ACTION_NONE;


    Reference< XConnection >            xConnection;
    Reference< XPreparedStatement >     xStatement;

    try
    {
        xConnection = dbtools::getConnection(sDatasouce, ::rtl::OUString(), ::rtl::OUString(), static_cast<FmGridControl*>(GetParent())->getServiceManager());
    }
    catch(NoSuchElementException&)
    {   // allowed, means sDatasouce isn't a valid data source name ....
    }
    catch(Exception&)
    {
        DBG_ERROR("FmGridHeader::ExecuteDrop: could not retrieve the database access object !");
    }
    if (!xConnection.is())
    {
        DBG_ERROR("FmGridHeader::ExecuteDrop: could not retrieve the database access object !");
        return sal_False;
    }
    try
    {

#if DBG_UTIL
        Reference< XServiceInfo >  xServiceInfo(xConnection, UNO_QUERY);
        DBG_ASSERT(xServiceInfo.is() && xServiceInfo->supportsService(SRV_SDB_CONNECTION), "FmGridHeader::ExecuteDrop: invalid connection (no database access connection !)");
#endif

        // Festellen des Feldes
        Reference< XNameAccess >    xFields;
        Reference< XPropertySet >       xField;
        switch (nCommandType)
        {
            case CommandType::TABLE:
            {
                Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
                Reference< XColumnsSupplier >  xSupplyColumns;
                xSupplyTables->getTables()->getByName(sCommand) >>= xSupplyColumns;
                xFields = xSupplyColumns->getColumns();
            }
            break;
            case CommandType::QUERY:
            {
                Reference< XQueriesSupplier > xSupplyQueries(xConnection, UNO_QUERY);
                Reference< XColumnsSupplier > xSupplyColumns;
                xSupplyQueries->getQueries()->getByName(sCommand) >>= xSupplyColumns;
                xFields  = xSupplyColumns->getColumns();
            }
            break;
            default:
            {
                xStatement = xConnection->prepareStatement(sCommand);
                // not interested in any results

                Reference< XPropertySet > xStatProps(xStatement,UNO_QUERY);
                xStatProps->setPropertyValue(rtl::OUString::createFromAscii("MaxRows"), makeAny(sal_Int32(0)));

                Reference< XColumnsSupplier >  xSupplyCols(xStatement->executeQuery(), UNO_QUERY);
                if (xSupplyCols.is())
                    xFields = xSupplyCols->getColumns();
            }
        }

        if (xFields.is() && xFields->hasByName(sFieldName))
            xFields->getByName(sFieldName) >>= xField;

        Reference< XNumberFormatsSupplier >  xSupplier = ::dbtools::getNumberFormats(xConnection, sal_True);

        if (!xSupplier.is() || !xField.is())
        {
            ::comphelper::disposeComponent(xStatement);
            return sal_False;
        }

        Reference< XNumberFormats >  xNumberFormats(xSupplier->getNumberFormats());
        if (!xNumberFormats.is())
        {
            ::comphelper::disposeComponent(xStatement);
            return sal_False;
        }

        // Vom Feld werden nun zwei Informationen benoetigt:
        // a.) Name des Feldes fuer Label und ControlSource
        // b.) FormatKey, um festzustellen, welches Feld erzeugt werden soll
        sal_Int32 nDataType;
        xField->getPropertyValue(FM_PROP_FIELDTYPE) >>= nDataType;
        // diese Datentypen koennen im Gridcontrol nicht verarbeitet werden
        switch (nDataType)
        {
            case DataType::LONGVARBINARY:
            case DataType::BINARY:
            case DataType::VARBINARY:
            case DataType::OTHER:
                ::comphelper::disposeComponent(xStatement);
                return sal_False;
        }

        sal_Int32 nFormatKey;
        xField->getPropertyValue(FM_PROP_FORMATKEY) >>= nFormatKey;

        // Erstellen der Column
        Reference< XIndexContainer >  xCols(static_cast<FmGridControl*>(GetParent())->GetPeer()->getColumns());
        Reference< XGridColumnFactory >  xFactory(xCols, UNO_QUERY);

        Point aPos  = OutputToScreenPixel(_rEvt.maPosPixel);
        sal_uInt16 nColId = GetItemId(_rEvt.maPosPixel);
        // EinfuegePosition, immer vor der aktuellen Spalte
        sal_uInt16 nPos = GetModelColumnPos(nColId);
        Reference< XPropertySet >  xCol, xSecondCol;

        // erzeugen der Column in abhaengigkeit vom type, default textfeld
        SvULongs aPossibleTypes;
        switch (nDataType)
        {
            case DataType::BIT:
                aPossibleTypes.Insert(SID_FM_CHECKBOX, aPossibleTypes.Count());
                break;
            case DataType::TINYINT:
            case DataType::SMALLINT:
            case DataType::INTEGER:
                aPossibleTypes.Insert(SID_FM_NUMERICFIELD, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_FORMATTEDFIELD, aPossibleTypes.Count());
                break;
            case DataType::REAL:
            case DataType::DOUBLE:
            case DataType::NUMERIC:
            case DataType::DECIMAL:
                aPossibleTypes.Insert(SID_FM_FORMATTEDFIELD, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_NUMERICFIELD, aPossibleTypes.Count());
                break;
            case DataType::TIMESTAMP:
                aPossibleTypes.Insert(SID_FM_TWOFIELDS_DATE_N_TIME, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_DATEFIELD, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_TIMEFIELD, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_FORMATTEDFIELD, aPossibleTypes.Count());
                break;
            case DataType::DATE:
                aPossibleTypes.Insert(SID_FM_DATEFIELD, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_FORMATTEDFIELD, aPossibleTypes.Count());
                break;
            case DataType::TIME:
                aPossibleTypes.Insert(SID_FM_TIMEFIELD, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_FORMATTEDFIELD, aPossibleTypes.Count());
                break;
            case DataType::CHAR:
            case DataType::VARCHAR:
            case DataType::LONGVARCHAR:
            default:
                aPossibleTypes.Insert(SID_FM_EDIT, aPossibleTypes.Count());
                aPossibleTypes.Insert(SID_FM_FORMATTEDFIELD, aPossibleTypes.Count());
                break;
        }
        // if it's a currency field, a a "currency field" option
        try
        {
            if  (   ::comphelper::hasProperty(FM_PROP_ISCURRENCY, xField)
                &&  ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_ISCURRENCY)))
                aPossibleTypes.Insert(SID_FM_CURRENCYFIELD, 0);
        }
        catch(Exception&)
        {
            DBG_ERROR("FmGridHeader::ExecuteDrop: Exception occured!");
        }

        sal_Int32 nPreferedType = -1;
        sal_Bool bDateNTimeCol = sal_False;
        if (aPossibleTypes.Count() != 0)
        {
            nPreferedType = aPossibleTypes[0];
            if ((_rEvt.mnAction == DND_ACTION_LINK) && (aPossibleTypes.Count() > 1))
            {
                ImageList aImageList( SVX_RES(RID_SVXIMGLIST_FMEXPL) );

                PopupMenu aInsertMenu(SVX_RES(RID_SVXMNU_COLS));
                PopupMenu aTypeMenu;
                PopupMenu* pMenu = aInsertMenu.GetPopupMenu(SID_FM_INSERTCOL);
                for (sal_uInt32 i=0; i<aPossibleTypes.Count(); ++i)
                    SetMenuItem(aImageList, sal_uInt16(aPossibleTypes[(sal_uInt16)i]), pMenu, aTypeMenu, sal_True, 0);
                nPreferedType = aTypeMenu.Execute(this, _rEvt.maPosPixel);
            }

            bDateNTimeCol = nPreferedType == SID_FM_TWOFIELDS_DATE_N_TIME;
            sal_uInt16 nColCount = bDateNTimeCol ? 2 : 1;
            ::rtl::OUString sFieldService;
            while (nColCount--)
            {
                if (bDateNTimeCol)
                    nPreferedType = nColCount ? SID_FM_DATEFIELD : SID_FM_TIMEFIELD;

                sFieldService = FieldServiceFromId(nPreferedType);
                Reference< XPropertySet >  xThisRoundCol;
                if (sFieldService.len())
                {
                    xThisRoundCol = xFactory->createColumn(sFieldService);
                    if (xThisRoundCol.is() && ::comphelper::hasProperty(FM_PROP_STRICTFORMAT, xThisRoundCol))
                        xThisRoundCol->setPropertyValue(FM_PROP_STRICTFORMAT, ::cppu::bool2any(sal_False));
                }
                if (nColCount)
                    xSecondCol = xThisRoundCol;
                else
                    xCol = xThisRoundCol;
            }
        }

        if (!xCol.is() || (bDateNTimeCol && !xSecondCol.is()))
        {
            ::comphelper::disposeComponent(xCol);   // in case only the creation of the second column failed
            ::comphelper::disposeComponent(xStatement);
            return sal_False;
        }

        if (bDateNTimeCol)
        {
            String sPostfix(SVX_RES(RID_STR_DATETIME_LABELPOSTFIX));
            xCol->setPropertyValue(FM_PROP_LABEL, makeAny(sFieldName + ::rtl::OUString(sPostfix.GetToken(1, ';'))));
            xSecondCol->setPropertyValue(FM_PROP_LABEL, makeAny(sFieldName + ::rtl::OUString(sPostfix.GetToken(0, ';'))));
        }
        else
            xCol->setPropertyValue(FM_PROP_LABEL, makeAny(sFieldName));

        if (nPreferedType == SID_FM_NUMERICFIELD)
        {
            // set properties for numerix field
            {
                Any aScaleVal(::comphelper::getNumberFormatDecimals(xNumberFormats, nFormatKey));
                xCol->setPropertyValue(FM_PROP_DECIMAL_ACCURACY,aScaleVal);
            }

            // set the max and min value for this field
            sal_Int32 nMinValue = 0, nMaxValue = 1000000000;
            switch (nDataType)
            {
                case DataType::TINYINT  : nMinValue = 0;            nMaxValue = 255; break;
                case DataType::SMALLINT : nMinValue = -32768;       nMaxValue = 32767; break;
                case DataType::INTEGER  : nMinValue = 0x80000000;   nMaxValue = 0x7FFFFFFF; break;
                    // um die doubles/singles kuemmere ich mich nicht, da es ein wenig sinnlos ist
                    // double and singles are ignored
            }
            xCol->setPropertyValue(FM_PROP_VALUEMIN,makeAny((double)nMinValue));
            xCol->setPropertyValue(FM_PROP_VALUEMAX,makeAny((double)nMaxValue));

            // format checking for numeric fields is default sal_True
            xCol->setPropertyValue(FM_PROP_STRICTFORMAT, bool2any(sal_True));
        }

        xCol->setPropertyValue(FM_PROP_CONTROLSOURCE, makeAny(sFieldName));
        if (bDateNTimeCol)
            xSecondCol->setPropertyValue(FM_PROP_CONTROLSOURCE, makeAny(sFieldName));

        if (bDateNTimeCol)
        {
            String sRealName,sPurePostfix;
            String sPostfix(SVX_RES(RID_STR_DATETIME_LABELPOSTFIX));

            for (xub_StrLen i=0; i<2; ++i)
            {
                sPurePostfix = sPostfix.GetToken(i, ';');
                sPurePostfix.EraseLeadingChars(' ');
                sPurePostfix.EraseLeadingChars('(');
                sPurePostfix.EraseTrailingChars(')');
                sRealName = sFieldName;
                sRealName += '_';
                sRealName += sPurePostfix;
                if (i)
                    xSecondCol->setPropertyValue(FM_PROP_NAME, makeAny(::rtl::OUString(sRealName)));
                else
                    xCol->setPropertyValue(FM_PROP_NAME, makeAny(::rtl::OUString(sRealName)));
            }
        }
        else
            xCol->setPropertyValue(FM_PROP_NAME, makeAny(sFieldName));

        // jetzt einfuegen
        Any aElement;
        aElement <<= xCol;
        xCols->insertByIndex(nPos, aElement);

        if (bDateNTimeCol)
        {
            aElement <<= xSecondCol;
            xCols->insertByIndex(nPos == (sal_uInt16)-1 ? nPos : ++nPos, aElement);
        }

        // ist die component::Form an die Datenbankangebunden?
        Reference< XFormComponent >  xFormCp(xCols, UNO_QUERY);
        Reference< XPropertySet >  xForm(xFormCp->getParent(), UNO_QUERY);
        if (xForm.is())
        {
            if (!::comphelper::getString(xForm->getPropertyValue(FM_PROP_DATASOURCE)).getLength())
                xForm->setPropertyValue(FM_PROP_DATASOURCE, makeAny(sDatasouce));

            if (!::comphelper::getString(xForm->getPropertyValue(FM_PROP_COMMAND)).getLength())
            {
                xForm->setPropertyValue(FM_PROP_COMMAND, makeAny(sCommand));
                Any aCommandType;
                switch (nCommandType)
                {
                    case CommandType::TABLE:
                        aCommandType <<= (sal_Int32)CommandType::TABLE;
                        break;
                    case CommandType::QUERY:
                        aCommandType <<= (sal_Int32)CommandType::QUERY;
                        break;
                    default:
                        aCommandType <<= (sal_Int32)CommandType::COMMAND;
                        xForm->setPropertyValue(FM_PROP_ESCAPE_PROCESSING, bool2any((sal_Bool)(2 == nCommandType)));
                        break;
                }
                xForm->setPropertyValue(FM_PROP_COMMANDTYPE, aCommandType);
            }
        }
    }
    catch (Exception&)
    {
        DBG_ERROR("FmGridHeader::ExecuteDrop: caught an exception while creatin' the column !");
        ::comphelper::disposeComponent(xStatement);
        return sal_False;
    }

    return DND_ACTION_COPY;
}

//------------------------------------------------------------------------------
void FmGridHeader::PreExecuteColumnContextMenu(sal_uInt16 nColId, PopupMenu& rMenu)
{
    sal_Bool bDesignMode = static_cast<FmGridControl*>(GetParent())->IsDesignMode();

    Reference< ::com::sun::star::container::XIndexContainer >  xCols(static_cast<FmGridControl*>(GetParent())->GetPeer()->getColumns());
    // Aufbau des Insert Menues
    // mark the column if nColId != HEADERBAR_ITEM_NOTFOUND
    if(nColId > 0)
    {
        sal_uInt16 nPos2 = GetModelColumnPos(nColId);

        Reference< ::com::sun::star::container::XIndexContainer >  xColumns(static_cast<FmGridControl*>(GetParent())->GetPeer()->getColumns());
        Reference< ::com::sun::star::beans::XPropertySet> xColumn;
        ::cppu::extractInterface(xColumn, xColumns->getByIndex(nPos2));
        Reference< ::com::sun::star::view::XSelectionSupplier >  xSelSupplier(xColumns, UNO_QUERY);
        if (xSelSupplier.is())
            xSelSupplier->select(makeAny(xColumn));
    }

    // EinfuegePosition, immer vor der aktuellen Spalte
    sal_uInt16 nPos = GetModelColumnPos(nColId);
    sal_Bool bMarked = nColId && static_cast<FmGridControl*>(GetParent())->isColumnMarked(nColId);

    ImageList aImageList( SVX_RES(RID_SVXIMGLIST_FMEXPL) );
    PopupMenu* pControlMenu = new PopupMenu;

    PopupMenu* pMenu = rMenu.GetPopupMenu(SID_FM_INSERTCOL);
    if (pMenu)
    {
        SetMenuItem(aImageList, SID_FM_EDIT, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_CHECKBOX, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_COMBOBOX, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_LISTBOX, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_DATEFIELD, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_TIMEFIELD, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_NUMERICFIELD, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_CURRENCYFIELD, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_PATTERNFIELD, pMenu, *pControlMenu, bDesignMode);
        SetMenuItem(aImageList, SID_FM_FORMATTEDFIELD, pMenu, *pControlMenu, bDesignMode);
    }

    if (pMenu && xCols.is() && nColId)
    {
        Reference< ::com::sun::star::beans::XPropertySet > xSet;
        ::cppu::extractInterface(xSet, xCols->getByIndex(nPos));
        sal_Int16 nClassId;
        xSet->getPropertyValue(FM_PROP_CLASSID) >>= nClassId;

        Reference< ::com::sun::star::io::XPersistObject >  xServiceQuestion(xSet, UNO_QUERY);
        sal_Int32 nColType = xServiceQuestion.is() ? getColumnTypeByModelName(xServiceQuestion->getServiceName()) : 0;
        if (nColType == TYPE_TEXTFIELD)
        {   // edit fields and formatted fields have the same service name, thus getColumnTypeByModelName returns TYPE_TEXTFIELD
            // in both cases. And as columns don't have an ::com::sun::star::lang::XServiceInfo interface, we have to distinguish both
            // types via the existence of special properties
            Reference< ::com::sun::star::beans::XPropertySet >  xProps(xSet, UNO_QUERY);
            if (xProps.is())
            {
                Reference< ::com::sun::star::beans::XPropertySetInfo >  xPropsInfo = xProps->getPropertySetInfo();
                if (xPropsInfo.is() && xPropsInfo->hasPropertyByName(FM_PROP_FORMATSSUPPLIER))
                    nColType = TYPE_FORMATTEDFIELD;
            }
        }

        pControlMenu->EnableItem(SID_FM_EDIT + nChangeTypeOffset, bDesignMode && (nColType != TYPE_TEXTFIELD));
        pControlMenu->EnableItem(SID_FM_COMBOBOX + nChangeTypeOffset, bDesignMode && (nColType != TYPE_COMBOBOX));
        pControlMenu->EnableItem(SID_FM_LISTBOX + nChangeTypeOffset, bDesignMode && (nColType != TYPE_LISTBOX));
        pControlMenu->EnableItem(SID_FM_CHECKBOX + nChangeTypeOffset, bDesignMode && (nColType != TYPE_CHECKBOX));
        pControlMenu->EnableItem(SID_FM_DATEFIELD + nChangeTypeOffset, bDesignMode && (nColType != TYPE_DATEFIELD));
        pControlMenu->EnableItem(SID_FM_NUMERICFIELD + nChangeTypeOffset, bDesignMode && (nColType != TYPE_NUMERICFIELD));
        pControlMenu->EnableItem(SID_FM_TIMEFIELD + nChangeTypeOffset, bDesignMode && (nColType != TYPE_TIMEFIELD));
        pControlMenu->EnableItem(SID_FM_CURRENCYFIELD + nChangeTypeOffset, bDesignMode && (nColType != TYPE_CURRENCYFIELD));
        pControlMenu->EnableItem(SID_FM_PATTERNFIELD + nChangeTypeOffset, bDesignMode && (nColType != TYPE_PATTERNFIELD));
        pControlMenu->EnableItem(SID_FM_FORMATTEDFIELD + nChangeTypeOffset, bDesignMode && (nColType != TYPE_FORMATTEDFIELD));
        rMenu.SetPopupMenu(SID_FM_CHANGECOL, pControlMenu);
    }

    rMenu.EnableItem(SID_FM_INSERTCOL, bDesignMode && xCols.is());
    rMenu.EnableItem(SID_FM_DELETECOL, bDesignMode && bMarked && xCols.is());
    rMenu.EnableItem(SID_FM_CHANGECOL, bDesignMode && bMarked && xCols.is());
    rMenu.EnableItem(SID_FM_SHOW_PROPERTY_BROWSER, bDesignMode && bMarked && xCols.is());

    PopupMenu* pShowColsMenu = rMenu.GetPopupMenu(SID_FM_SHOWCOLS);
    sal_uInt16 nHiddenCols = 0;
    if (pShowColsMenu)
    {
        if (xCols.is())
        {
            // check for hidden cols
            Reference< ::com::sun::star::beans::XPropertySet >  xCurCol;
            Any aHidden,aName;
            for (sal_uInt16 i=0; i<xCols->getCount(); ++i)
            {
                ::cppu::extractInterface(xCurCol, xCols->getByIndex(i));
                DBG_ASSERT(xCurCol.is(), "FmGridHeader::PreExecuteColumnContextMenu : the Peer has invalid columns !");
                aHidden = xCurCol->getPropertyValue(FM_PROP_HIDDEN);
                DBG_ASSERT(aHidden.getValueType().getTypeClass() == TypeClass_BOOLEAN,
                    "FmGridHeader::PreExecuteColumnContextMenu : the property 'hidden' should be boolean !");
                if (::comphelper::getBOOL(aHidden))
                {
                    // put the column name into the 'show col' menu
                    if (nHiddenCols < 16)
                    {   // (only the first 16 items to keep the menu rather small)
                        aName = xCurCol->getPropertyValue(FM_PROP_LABEL);
                        pShowColsMenu->InsertItem(nHiddenCols + 1, ::comphelper::getString(aName), 0, nHiddenCols);
                            // the ID is arbitrary, but should be unique within the whole menu
                    }
                    ++nHiddenCols;
                }
            }
        }
        pShowColsMenu->EnableItem(SID_FM_SHOWCOLS_MORE, xCols.is() && (nHiddenCols > 16));
        pShowColsMenu->EnableItem(SID_FM_SHOWALLCOLS, xCols.is() && (nHiddenCols > 0));
    }

    // allow the 'hide column' item ?
    sal_Bool bAllowHide = bMarked;                                          // a column is marked
    bAllowHide = bAllowHide || (!bDesignMode && (nPos != (sal_uInt16)-1));  // OR we are in alive mode and have hit a column
    bAllowHide = bAllowHide && xCols.is();                              // AND we have a column container
    bAllowHide = bAllowHide && (xCols->getCount()-nHiddenCols > 1);     // AND there are at least two visible columns
    rMenu.EnableItem(SID_FM_HIDECOL,  bAllowHide);

    sal_Bool bChecked = sal_False;
    if (bMarked)
    {
        SfxPoolItem* pItem = NULL;

        SfxViewFrame* pCurrentFrame = SfxViewFrame::Current();
        SfxItemState eState = SFX_ITEM_UNKNOWN;
        // ask the bindings of the current view frame (which should be the one we're residing in) for the state
        if (pCurrentFrame)
            eState = pCurrentFrame->GetBindings().QueryState(SID_FM_CTL_PROPERTIES, pItem);
        else
            DBG_ERROR("FmGridHeader::PreExecuteColumnContextMenu : no current view frame -> no bindings !");

        if (eState >= SFX_ITEM_AVAILABLE)
        {
            if (pItem)
            {
                bChecked = pItem->ISA(SfxBoolItem) && ((SfxBoolItem*)pItem)->GetValue();
                rMenu.CheckItem(SID_FM_SHOW_PROPERTY_BROWSER,bChecked);
            }
        }
        delete pItem;
    }
}

//------------------------------------------------------------------------------
void FmGridHeader::PostExecuteColumnContextMenu(sal_uInt16 nColId, const PopupMenu& rMenu, sal_uInt16 nExecutionResult)
{
    Reference< ::com::sun::star::container::XIndexContainer >  xCols(static_cast<FmGridControl*>(GetParent())->GetPeer()->getColumns());
    sal_uInt16 nPos = GetModelColumnPos(nColId);

    // remove and delet the menu we inserted in PreExecuteColumnContextMenu
    PopupMenu* pControlMenu = rMenu.GetPopupMenu(SID_FM_CHANGECOL);
    delete pControlMenu;

    ::rtl::OUString aFieldType;
    sal_Bool    bReplace = sal_False;
    switch (nExecutionResult)
    {
        case SID_FM_DELETECOL:
        {
            Reference< XInterface >  xCol;
            ::cppu::extractInterface(xCol, xCols->getByIndex(nPos));
            xCols->removeByIndex(nPos);
            ::comphelper::disposeComponent(xCol);
        }   break;
        case SID_FM_SHOW_PROPERTY_BROWSER:
        {
            Reference< XInterface >  xCol;
            ::cppu::extractInterface(xCol, xCols->getByIndex(nPos));
            FmInterfaceItem aIFaceItem(SID_FM_SHOW_PROPERTY_BROWSER, xCol);
            SfxBoolItem aShowItem(SID_FM_SHOW_PROPERTIES, !rMenu.IsItemChecked(SID_FM_SHOW_PROPERTY_BROWSER));

            // execute the slot, use the dispatcher of the current view frame (which should be the one we're residing in)
            SfxViewFrame* pCurrentFrame = SfxViewFrame::Current();
            if (pCurrentFrame)
                pCurrentFrame->GetBindings().GetDispatcher()->Execute( SID_FM_SHOW_PROPERTY_BROWSER, SFX_CALLMODE_ASYNCHRON,
                                          &aIFaceItem, &aShowItem, 0L );
            else
                DBG_ERROR("FmGridHeader::PostExecuteColumnContextMenu : no current view frame -> no bindings !");
        }   break;
        case SID_FM_EDIT + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_EDIT:
            aFieldType = FM_COL_TEXTFIELD;
            break;
        case SID_FM_COMBOBOX + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_COMBOBOX:
            aFieldType = FM_COL_COMBOBOX;
            break;
        case SID_FM_LISTBOX + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_LISTBOX:
            aFieldType = FM_COL_LISTBOX;
            break;
        case SID_FM_CHECKBOX + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_CHECKBOX:
            aFieldType = FM_COL_CHECKBOX;
            break;
        case SID_FM_DATEFIELD + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_DATEFIELD:
            aFieldType = FM_COL_DATEFIELD;
            break;
        case SID_FM_TIMEFIELD + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_TIMEFIELD:
            aFieldType = FM_COL_TIMEFIELD;
            break;
        case SID_FM_NUMERICFIELD + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_NUMERICFIELD:
            aFieldType = FM_COL_NUMERICFIELD;
            break;
        case SID_FM_CURRENCYFIELD + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_CURRENCYFIELD:
            aFieldType = FM_COL_CURRENCYFIELD;
            break;
        case SID_FM_PATTERNFIELD + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_PATTERNFIELD:
            aFieldType = FM_COL_PATTERNFIELD;
            break;
        case SID_FM_FORMATTEDFIELD + nChangeTypeOffset:
            bReplace = sal_True;
        case SID_FM_FORMATTEDFIELD:
            aFieldType = FM_COL_FORMATTEDFIELD;
            break;
        case SID_FM_HIDECOL:
        {
            Reference< ::com::sun::star::beans::XPropertySet >  xCurCol;
            ::cppu::extractInterface(xCurCol, xCols->getByIndex(nPos));
            xCurCol->setPropertyValue(FM_PROP_HIDDEN, makeAny((sal_Bool)sal_True));
        }
        break;
        case SID_FM_SHOWCOLS_MORE:
        {
            FmShowColsDialog dlg(NULL);
            dlg.SetColumns(xCols);
            dlg.Execute();
        }
        break;
        case SID_FM_SHOWALLCOLS:
        {
            // just iterate through all the cols ...
            Reference< ::com::sun::star::beans::XPropertySet >  xCurCol;
            for (sal_uInt16 i=0; i<xCols->getCount(); ++i)
            {
                ::cppu::extractInterface(xCurCol, xCols->getByIndex(i));
                xCurCol->setPropertyValue(FM_PROP_HIDDEN, makeAny((sal_Bool)sal_False));
            }
            // TODO : there must be a more clever way to do this ....
            // with the above the view is updated after every single model update ...
        }
        break;
        default:
            if (nExecutionResult>0 && nExecutionResult<=16)
            {   // it was a "show column/<colname>" command (there are at most 16 such items)
                // search the nExecutionResult'th hidden col
                Reference< ::com::sun::star::beans::XPropertySet >  xCurCol;
                for (sal_uInt16 i=0; i<xCols->getCount() && nExecutionResult; ++i)
                {
                    ::cppu::extractInterface(xCurCol, xCols->getByIndex(i));
                    Any aHidden = xCurCol->getPropertyValue(FM_PROP_HIDDEN);
                    if (::comphelper::getBOOL(aHidden))
                        if (!--nExecutionResult)
                        {
                            xCurCol->setPropertyValue(FM_PROP_HIDDEN, makeAny((sal_Bool)sal_False));
                            break;
                        }
                }
            }
            break;
    }

    if (aFieldType.len())
    {
        Reference< ::com::sun::star::form::XGridColumnFactory >  xFactory(xCols, UNO_QUERY);
        Reference< ::com::sun::star::beans::XPropertySet >  xCol = xFactory->createColumn(aFieldType);
        if (xCol.is())
        {
            Any aNew;
            aNew <<= xCol;
            if (bReplace)
            {
                // ein paar Properties hinueberretten
                Reference< ::com::sun::star::beans::XPropertySet >  xReplaced;
                ::cppu::extractInterface(xReplaced, xCols->getByIndex(nPos));

                // the application locale
                String sLanguage, sCountry;
                ConvertLanguageToIsoNames(Application::GetAppInternational().GetLanguage(), sLanguage, sCountry);
                ::com::sun::star::lang::Locale aAppLocale(sLanguage, sCountry, ::rtl::OUString());

                ::dbtools::TransferFormComponentProperties(xReplaced, xCol, aAppLocale);

                xCols->replaceByIndex(nPos, aNew);
                ::comphelper::disposeComponent(xReplaced);
            }
            else
            {
                // Standardlabel setzen
                ::rtl::OUString sLabelBase(SVX_RES(RID_STR_COLUMN));
                // disambiguate the name
                Reference< XNameAccess > xColNames(xCols, UNO_QUERY);
                ::rtl::OUString sLabel;
                for (sal_Int32 i=1; i<65535; ++i)
                {
                    sLabel = sLabelBase;
                    sLabel += ::rtl::OUString::valueOf((sal_Int32)i);
                    if (!xColNames->hasByName(sLabel))
                        break;
                }
                // no fallback in case the name is not unique (which is rather improbable) ....
                xCol->setPropertyValue(FM_PROP_LABEL, makeAny(sLabel));
                xCol->setPropertyValue(FM_PROP_NAME, makeAny(sLabel));
                xCols->insertByIndex(nPos, aNew);
            }

        }
    }
}

//------------------------------------------------------------------------------
void FmGridHeader::Command(const CommandEvent& rEvt)
{
    switch (rEvt.GetCommand())
    {
        case COMMAND_CONTEXTMENU:
        {
            if (!rEvt.IsMouseEvent())
                return;

            sal_uInt16 nColId = GetItemId(rEvt.GetMousePosPixel());
            PopupMenu aContextMenu(SVX_RES(RID_SVXMNU_COLS));

            PreExecuteColumnContextMenu(nColId, aContextMenu);
            aContextMenu.RemoveDisabledEntries(sal_True, sal_True);
            PostExecuteColumnContextMenu(nColId, aContextMenu, aContextMenu.Execute(this, rEvt.GetMousePosPixel()));
        }
        break;
        default:
            DbBrowseHeader::Command(rEvt);
    }
}

//------------------------------------------------------------------------------
FmGridControl::FmGridControl(
                Reference< ::com::sun::star::lang::XMultiServiceFactory > _rxFactory,
                Window* pParent,
                FmXGridPeer* _pPeer,
                WinBits nBits)
        :DbGridControl(_rxFactory, pParent, nBits)
        ,m_bInColumnMove(sal_False)
        ,m_nMarkedColumnId(BROWSER_INVALIDID)
        ,m_pPeer(_pPeer)
{
}

// ::com::sun::star::beans::XPropertyChangeListener
//------------------------------------------------------------------------------
void FmGridControl::propertyChange(const ::com::sun::star::beans::PropertyChangeEvent& evt)
{
    if (evt.PropertyName == FM_PROP_ROWCOUNT)
    {
        // if we're not in the main thread call AdjustRows asynchronously
        implAdjustInSolarThread(sal_True);
        return;
    }

    const DbGridRowRef& xRow = GetCurrentRow();
    // waehrend Positionierung wird kein abgleich  der Properties vorgenommen
    Reference<XPropertySet> xSet(evt.Source,UNO_QUERY);
    if (xRow.Is() && (::cppu::any2bool(xSet->getPropertyValue(FM_PROP_ISNEW))|| CompareBookmark(getDataSource()->getBookmark(), xRow->GetBookmark())))
    {
        if (evt.PropertyName == FM_PROP_ISMODIFIED)
        {
            // modified or clean ?
            GridRowStatus eStatus = ::comphelper::getBOOL(evt.NewValue) ? GRS_MODIFIED : GRS_CLEAN;
            if (eStatus != xRow->GetStatus())
            {
                xRow->SetStatus(eStatus);
                vos::OGuard aGuard( Application::GetSolarMutex() );
                RowModified(GetCurrentPos());
            }
        }
    }
}

//------------------------------------------------------------------------------
void FmGridControl::SetDesignMode(sal_Bool bMode)
{
    sal_Bool bOldMode = IsDesignMode();
    DbGridControl::SetDesignMode(bMode);
    if (bOldMode != bMode)
        if (!bMode)
        {
            // selection aufheben
            markColumn(USHRT_MAX);
        }
        else
        {
            Reference< ::com::sun::star::container::XIndexContainer >  xColumns(GetPeer()->getColumns());
            Reference< ::com::sun::star::view::XSelectionSupplier >  xSelSupplier(xColumns, UNO_QUERY);
            if (xSelSupplier.is())
            {
                Any aSelection = xSelSupplier->getSelection();
                Reference< ::com::sun::star::beans::XPropertySet >  xColumn;
                if (aSelection.getValueType().getTypeClass() == TypeClass_INTERFACE)
                    ::cppu::extractInterface(xColumn, aSelection);
                Reference< XInterface >  xCurrent;
                for (sal_uInt16 i=0; i<xColumns->getCount(); ++i)
                {
                    ::cppu::extractInterface(xCurrent, xColumns->getByIndex(i));
                    if (xCurrent == xColumn)
                    {
                        markColumn(GetColumnIdFromModelPos(i));
                        break;
                    }
                }
            }
        }
}

//------------------------------------------------------------------------------
void FmGridControl::DeleteSelectedRows()
{
    if (!m_pSeekCursor)
        return;

    // how many rows are selected?
    sal_Int32 nSelectedRows = GetSelectRowCount();
    // is the insert row selected
    if (GetEmptyRow().Is() && IsRowSelected(GetRowCount() - 1))
        nSelectedRows -= 1;

    // nothing to do
    if (nSelectedRows <= 0)
        return;

    // try to confirm the delete
    Reference< ::com::sun::star::frame::XDispatchProvider >  xDispatcher = (::com::sun::star::frame::XDispatchProvider*)GetPeer();
    if (xDispatcher.is())
    {
        ::com::sun::star::util::URL aUrl;
        aUrl.Complete = FMURL_CONFIRM_DELETION;
        Reference< ::com::sun::star::frame::XDispatch >  xDispatch = xDispatcher->queryDispatch(aUrl, rtl::OUString(), 0);
        Reference< ::com::sun::star::form::XConfirmDeleteListener >  xConfirm(xDispatch, UNO_QUERY);
        if (xConfirm.is())
        {
            ::com::sun::star::sdb::RowChangeEvent aEvent;
            aEvent.Source = (Reference< XInterface > )(*getDataSource());
            aEvent.Rows = nSelectedRows;
            aEvent.Action = ::com::sun::star::sdb::RowChangeAction::DELETE;
            if (!xConfirm->confirmDelete(aEvent))
                return;
        }
    }

    Reference< ::com::sun::star::sdbcx::XDeleteRows >  xDeleteThem((Reference< XInterface >)*getDataSource(), UNO_QUERY);

    // colect the bookmarks of the selected rows
    Sequence < Any> aBookmarks = getSelectionBookmarks();

    // determine the next row to position after deletion
    Any aBookmark;
    sal_Bool bNewPos = sal_False;
    // if the current row isn't selected we take the row as row after deletion
    if (!IsRowSelected(GetCurrentPos()) && !IsCurrentAppending())
    {
        aBookmark = GetCurrentRow()->GetBookmark();
        bNewPos   = sal_True;
    }
    else
    {
        // we look for the first row after the selected block for selection
        long nIdx = LastSelectedRow() + 1;
        if (nIdx < GetRowCount() - 1)
        {
            // there is a next row to position on
            if (SeekCursor(nIdx))
            {
                GetSeekRow()->SetState(m_pSeekCursor, sal_True);

                bNewPos = sal_True;
                // if it's not the row for inserting we keep the bookmark
                if (!IsEmptyRow(nIdx))
                    aBookmark = m_pSeekCursor->getBookmark();
            }
        }
        else
        {
            // we look for the first row before the selected block for selection after deletion
            nIdx = FirstSelectedRow() - 1;
            if (nIdx >= 0 && SeekCursor(nIdx))
            {
                GetSeekRow()->SetState(m_pSeekCursor, sal_True);

                bNewPos = sal_True;
                aBookmark = m_pSeekCursor->getBookmark();
            }
        }
    }

    // Sind alle Zeilen Selectiert
    // Zweite bedingung falls keine einguegeZeile existiert
    sal_Bool bAllSelected = GetTotalCount() == nSelectedRows || GetRowCount() == nSelectedRows;

    BeginCursorAction();

    // now delete the row
    Sequence <sal_Int32> aDeletedRows;
    try
    {
        aDeletedRows = xDeleteThem->deleteRows(aBookmarks);
    }
    catch(SQLException&)
    {
    }

    // how many rows are deleted?
    sal_Int32 nDeletedRows = 0;
    const sal_Int32* pSuccess = aDeletedRows.getConstArray();
    for (sal_Int32 i = 0; i < aDeletedRows.getLength(); i++)
    {
        if (pSuccess[i])
            nDeletedRows++;
    }

    // sind Zeilen geloescht worden?
    if (nDeletedRows)
    {
        SetUpdateMode(sal_False);
        SetNoSelection();
        try
        {
            // did we delete all the rows than try to move to the next possible row
            if (nDeletedRows == aDeletedRows.getLength())
            {
                // there exists a new position to move on
                if (bNewPos)
                {
                    if (aBookmark.hasValue())
                        getDataSource()->moveToBookmark(aBookmark);
                    // no valid bookmark so move to the insert row
                    else
                    {
                        Reference< XResultSetUpdate >  xUpdateCursor((Reference< XInterface >)*getDataSource(), UNO_QUERY);
                        xUpdateCursor->moveToInsertRow();
                    }
                }
                else
                {
                    Reference< ::com::sun::star::beans::XPropertySet >  xSet((Reference< XInterface >)*m_pDataCursor, UNO_QUERY);
                    sal_Int32 nRecordCount;
                    xSet->getPropertyValue(FM_PROP_ROWCOUNT) >>= nRecordCount;
                    // there are no rows left and we have an insert row
                    if (!nRecordCount && GetEmptyRow().Is())
                    {
                        Reference< XResultSetUpdate >  xUpdateCursor((Reference< XInterface >)*getDataSource(), UNO_QUERY);
                        xUpdateCursor->moveToInsertRow();
                    }
                    else if (nRecordCount)
                        // move to the first row
                        getDataSource()->first();
                }
            }
            // not all the rows where deleted, so move to the first row which remained in the resultset
            else
            {
                for (sal_Int32 i = 0; i < aDeletedRows.getLength(); i++)
                {
                    if (!pSuccess[i])
                    {
                        getDataSource()->moveToBookmark(aBookmarks.getConstArray()[i]);
                        break;
                    }
                }
            }
        }
        catch(...)
        {
            try
            {
                // positioning went wrong so try to move to the first row
                getDataSource()->first();
            }
            catch(...)
            {
            }
        }

        // An den DatenCursor anpassen
        AdjustDataSource(sal_True);

        // es konnten nicht alle Zeilen geloescht werden
        // da nie nicht geloeschten wieder selektieren
        if (nDeletedRows < nSelectedRows)
        {
            // waren alle selektiert
            if (bAllSelected)
            {
                SelectAll();
                if (IsEmptyRow(GetRowCount() - 1))  // einfuegeZeile nicht
                    SelectRow(GetRowCount() - 1, sal_False);
            }
            else
            {
                // select the remaining rows
                for (sal_Int32 i = 0; i < aDeletedRows.getLength(); i++)
                {
                    try
                    {
                        if (!pSuccess[i])
                        {
                            m_pSeekCursor->moveToBookmark(m_pDataCursor->getBookmark());
                            SetSeekPos(m_pSeekCursor->getRow() - 1);
                            SelectRow(GetSeekPos());
                        }
                    }
                    catch(...)
                    {
                        // keep the seekpos in all cases
                        SetSeekPos(m_pSeekCursor->getRow() - 1);
                    }
                }
            }
        }

        EndCursorAction();
        SetUpdateMode(sal_True);
    }
    else // Zeile konnte nicht geloescht werden
    {
        EndCursorAction();
        try
        {
            // currentrow is the insert row?
            if (!IsCurrentAppending())
                getDataSource()->refreshRow();
        }
        catch(...)
        {
        }
    }

    // if there is no selection anymore we can start editing
    if (!GetSelectRowCount())
        ActivateCell();
}


// XCurrentRecordListener
//------------------------------------------------------------------------------
void FmGridControl::positioned(const ::com::sun::star::lang::EventObject& rEvent)
{
    TRACE_RANGE("FmGridControl::positioned");
    // position on the data source (force it to be done in the main thread)
    implAdjustInSolarThread(sal_False);
}

//------------------------------------------------------------------------------
sal_Bool FmGridControl::commit()
{
    // Commit nur ausfuehren, wenn nicht bereits ein Update vom ::com::sun::star::form::component::GridControl ausgefuehrt
    // wird
    if (!IsUpdating())
    {
        if (Controller().Is() && Controller()->IsModified())
        {
            if (!SaveModified())
                return sal_False;
        }
    }
    return sal_True;
}

//------------------------------------------------------------------------------
void FmGridControl::inserted(const ::com::sun::star::lang::EventObject& rEvent)
{
    const DbGridRowRef& xRow = GetCurrentRow();
    if (!xRow.Is())
        return;

    // Zeile ist eingefuegt worden, dann den status und mode zuruecksetzen
    xRow->SetState(m_pDataCursor, sal_False);
    xRow->SetNew(sal_False);

}

// XCancelUpdateRecordListener
//------------------------------------------------------------------------------
void FmGridControl::restored(const ::com::sun::star::lang::EventObject& rEvent)
{
    if (!GetCurrentRow().Is())
        return;

    sal_Bool bAppending = GetCurrentRow()->IsNew();
    sal_Bool bDirty     = GetCurrentRow()->IsModified();
    if (bAppending && (DbBrowseBox::IsModified() || bDirty))
    {
        if (Controller().Is())
            Controller()->ClearModified();

        // jetzt die Zeile herausnehmen
        RowRemoved(GetRowCount() - 1, 1, sal_True);
        GetNavigationBar().InvalidateAll();
    }

    positioned(rEvent);
}

//------------------------------------------------------------------------------
BrowserHeader* FmGridControl::imp_CreateHeaderBar(BrowseBox* pParent)
{
    return new FmGridHeader(this);
}

//------------------------------------------------------------------------------
void FmGridControl::markColumn(sal_uInt16 nId)
{
    if (GetHeaderBar() && m_nMarkedColumnId != nId)
    {
        // deselektieren
        if (m_nMarkedColumnId != BROWSER_INVALIDID)
        {
            HeaderBarItemBits aBits = GetHeaderBar()->GetItemBits(m_nMarkedColumnId) & ~HIB_FLAT;
            GetHeaderBar()->SetItemBits(m_nMarkedColumnId, aBits);
        }


        if (nId != BROWSER_INVALIDID)
        {
            HeaderBarItemBits aBits = GetHeaderBar()->GetItemBits(nId) | HIB_FLAT;
            GetHeaderBar()->SetItemBits(nId, aBits);
        }
        m_nMarkedColumnId = nId;
    }
}

//------------------------------------------------------------------------------
sal_Bool FmGridControl::isColumnMarked(sal_uInt16 nId) const
{
    return m_nMarkedColumnId == nId;
}

//------------------------------------------------------------------------------
void FmGridControl::ColumnResized(sal_uInt16 nId)
{
    DbGridControl::ColumnResized(nId);

    // Wert ans model uebergeben
    DbGridColumn* pCol = DbGridControl::GetColumns().GetObject(GetModelColumnPos(nId));
    Reference< ::com::sun::star::beans::XPropertySet >  xColModel(pCol->getModel());
    if (xColModel.is())
    {
        Any aWidth;
        sal_Int32 nColumnWidth = GetColumnWidth(nId);
        nColumnWidth = CalcReverseZoom(nColumnWidth);
        // Umrechnen in 10THMM
        aWidth <<= (sal_Int32)PixelToLogic(Point(nColumnWidth,0),MAP_10TH_MM).X();
        xColModel->setPropertyValue(FM_PROP_WIDTH, aWidth);
    }
}

//------------------------------------------------------------------------------
void FmGridControl::CellModified()
{
    DbGridControl::CellModified();
    GetPeer()->CellModified();
}

//------------------------------------------------------------------------------
void FmGridControl::BeginCursorAction()
{
    DbGridControl::BeginCursorAction();
    m_pPeer->stopCursorListening();
}

//------------------------------------------------------------------------------
void FmGridControl::EndCursorAction()
{
    m_pPeer->startCursorListening();
    DbGridControl::EndCursorAction();
}

//------------------------------------------------------------------------------
void FmGridControl::ColumnMoved(sal_uInt16 nId)
{
    m_bInColumnMove = sal_True;

    DbGridControl::ColumnMoved(nId);
    Reference< ::com::sun::star::container::XIndexContainer >  xColumns(GetPeer()->getColumns());

    if (xColumns.is())
    {
        // suchen der Spalte und verschieben im Model
        // ColumnPos holen
        DbGridColumn* pCol = DbGridControl::GetColumns().GetObject(GetModelColumnPos(nId));
        Reference< ::com::sun::star::beans::XPropertySet >  xCol;

        // Einfuegen mu sich an den Column Positionen orientieren
        sal_Int32 i;
        Reference< XInterface > xCurrent;
        for (i = 0; !xCol.is() && i < xColumns->getCount(); i++)
        {
            ::cppu::extractInterface(xCurrent, xColumns->getByIndex(i));
            if (xCurrent == pCol->getModel())
            {
                xCol = pCol->getModel();
                break;
            }
        }

        DBG_ASSERT(i < xColumns->getCount(), "Falscher ::com::sun::star::sdbcx::Index");
        xColumns->removeByIndex(i);
        Any aElement;
        aElement <<= xCol;
        xColumns->insertByIndex(GetModelColumnPos(nId), aElement);
        pCol->setModel(xCol);
    }

    m_bInColumnMove = sal_False;
}

//------------------------------------------------------------------------------
void FmGridControl::InitColumnsByModels(const Reference< ::com::sun::star::container::XIndexContainer >& xColumns)
{
    // Spalten wieder neu setzen
    // wenn es nur eine HandleColumn gibt, dann nicht
    if (GetModelColCount())
    {
        RemoveColumns();
        InsertHandleColumn();
    }

    if (!xColumns.is())
        return;

    SetUpdateMode(sal_False);

    // Einfuegen mu sich an den Column Positionen orientieren
    sal_Int32 i;
    String aName;
    Any aWidth;
    for (i = 0; i < xColumns->getCount(); ++i)
    {
        Reference< ::com::sun::star::beans::XPropertySet > xCol;
        ::cppu::extractInterface(xCol, xColumns->getByIndex(i));

        Reference< XPropertySetInfo > xPropsInfo = xCol->getPropertySetInfo();
        sal_Bool bHas = xPropsInfo->hasPropertyByName(FM_PROP_LABEL);

        aName  = (const sal_Unicode*)::comphelper::getString(xCol->getPropertyValue(FM_PROP_LABEL));

        aWidth = xCol->getPropertyValue(FM_PROP_WIDTH);
        sal_Int32 nWidth = 0;
        if (aWidth >>= nWidth)
            nWidth = LogicToPixel(Point(nWidth,0),MAP_10TH_MM).X();

        AppendColumn(aName, (sal_uInt16)nWidth);
        DbGridColumn* pCol = DbGridControl::GetColumns().GetObject(i);
        pCol->setModel(xCol);
    }

    // und jetzt noch die hidden columns rausnehmen
    // (wir haben das nicht gleich in der oberen Schleife gemacht, da wir dann Probleme mit den
    // IDs der Spalten bekommen haetten : AppendColumn vergibt die automatisch, die Spalte _nach_
    // einer versteckten braucht aber eine um eine erhoehte ID ....
    Any aHidden;
    for (i = 0; i < xColumns->getCount(); ++i)
    {
        Reference< ::com::sun::star::beans::XPropertySet > xCol;
        ::cppu::extractInterface(xCol, xColumns->getByIndex(i));
        aHidden = xCol->getPropertyValue(FM_PROP_HIDDEN);
        if (::comphelper::getBOOL(aHidden))
            HideColumn(GetColumnIdFromModelPos((sal_uInt16)i));
    }

    SetUpdateMode(sal_True);
}

//------------------------------------------------------------------------------
void FmGridControl::InitColumnsByFields(const Reference< ::com::sun::star::container::XIndexAccess >& xFields)
{
    if (!xFields.is())
        return;

    // Spalten initialisieren
    Reference< ::com::sun::star::container::XIndexContainer >  xColumns(GetPeer()->getColumns());
    Reference< ::com::sun::star::container::XNameAccess >  xFieldsAsNames(xFields, UNO_QUERY);
    sal_Int32 nFieldCount = xFields->getCount();

    // Einfuegen mu sich an den Column Positionen orientieren
    ::rtl::OUString aFieldName;
    for (sal_Int32 i = 0; i < xColumns->getCount(); i++)
    {
        DbGridColumn*   pCol = GetColumns().GetObject(i);
        Reference< ::com::sun::star::beans::XPropertySet > xCol;
        ::cppu::extractInterface(xCol, xColumns->getByIndex(i));
        DbCellControl*  pCellControl  = NULL;

        // suchen des Feldes, das zur Controlsource gehoert
        xCol->getPropertyValue(FM_PROP_CONTROLSOURCE) >>= aFieldName;
        Reference< ::com::sun::star::beans::XPropertySet >  xField;

        if (aFieldName.len() && xFieldsAsNames->hasByName(aFieldName))
        {
            ::cppu::extractInterface(xField, xFieldsAsNames->getByName(aFieldName));
        }

        // feststellen der Feldposition
        sal_Int32 nFieldPos = -1;
        if (xField.is())
        {
            Reference< ::com::sun::star::beans::XPropertySet > xCheck;
            for (sal_Int32 i = 0; i < nFieldCount; i++)
            {
                ::cppu::extractInterface(xCheck, xFields->getByIndex(i));
                if (xField == xCheck)
                {
                    nFieldPos = i;
                    break;
                }
            }
        }

        if (xField.is() && nFieldPos >= 0)
        {
            // Datenfelder mit folgenden Datentypen knnen nicht verwendet werden
            sal_Int32 nDataType;
            xField->getPropertyValue(FM_PROP_FIELDTYPE) >>= nDataType;
            sal_Bool bIllegalType(sal_False);
            switch (nDataType)
            {
                case DataType::LONGVARBINARY:
                case DataType::BINARY:
                case DataType::VARBINARY:
                case DataType::OTHER:
                    bIllegalType = sal_True;
            }

            if (bIllegalType)
            {
                pCol->SetObject((sal_Int16)nFieldPos);
                continue;
            }
            else
            {
                // Feststellen ob ReadOnly
                sal_Bool bReadOnly = ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_ISREADONLY));
                pCol->SetReadOnly(bReadOnly);
            }
        }

        // anhand des ServiceNamens wird das Control bestimmt
        ::rtl::OUString sPropColumnServiceName = ::rtl::OUString::createFromAscii("ColumnServiceName");
        if (!::comphelper::hasProperty(sPropColumnServiceName, xCol))
            return;

        pCol->setModel(xCol);

        sal_Int32 nTypeId = getColumnTypeByModelName(::comphelper::getString(xCol->getPropertyValue(sPropColumnServiceName)));
        pCol->CreateControl(nFieldPos, xField, nTypeId);
    }
}

//------------------------------------------------------------------------------
void FmGridControl::HideColumn(sal_uInt16 nId)
{
    DbGridControl::HideColumn(nId);

    sal_uInt16 nPos = GetModelColumnPos(nId);
    if (nPos == (sal_uInt16)-1)
        return;

    DbGridColumn* pColumn = GetColumns().GetObject(nPos);
    if (pColumn->IsHidden())
        GetPeer()->columnHidden(pColumn);

    if (nId == m_nMarkedColumnId)
        m_nMarkedColumnId = (sal_uInt16)-1;
}

//------------------------------------------------------------------------------
void FmGridControl::ShowColumn(sal_uInt16 nId)
{
    DbGridControl::ShowColumn(nId);

    sal_uInt16 nPos = GetModelColumnPos(nId);
    if (nPos == (sal_uInt16)-1)
        return;

    DbGridColumn* pColumn = GetColumns().GetObject(nPos);
    if (!pColumn->IsHidden())
        GetPeer()->columnVisible(pColumn);

    // if the column which is shown here is selected ...
    Reference< ::com::sun::star::view::XSelectionSupplier >  xSelSupplier(GetPeer()->getColumns(), UNO_QUERY);
    if (xSelSupplier.is())
    {
        Reference< ::com::sun::star::beans::XPropertySet >  xColumn;
        ::cppu::extractInterface(xColumn, xSelSupplier->getSelection());
        if (xColumn.get() == pColumn->getModel().get())
            // ... -> mark it
            markColumn(nId);
    }
}

//------------------------------------------------------------------------------
Sequence< Any> FmGridControl::getSelectionBookmarks()
{
    sal_Int32 nSelectedRows = GetSelectRowCount(), i = 0;
    Sequence< Any> aBookmarks(nSelectedRows);
    Any* pBookmarks = (Any*)aBookmarks.getArray();

    // lock our update so no paint-triggered seeks interfere ...
    SetUpdateMode(sal_False);
    // (I'm not sure if the problem isn't deeper : The szenario : a large table displayed by a grid with a
    // thread-safe cursor (dBase). On loading the sdb-cursor started a counting thread. While this counting progress
    // was running, I tried do delete 3 records from within the grid. Deletion caused a SeekCursor, which did a
    // m_pSeekCursor->moveRelative and a m_pSeekCursor->getPosition.
    // Unfortunally the first call caused a propertyChanged(RECORDCOUNT) which resulted in a repaint of the
    // navigation bar and the grid. The latter itself will result in SeekRow calls. So after (successfully) returning
    // from the moveRelative the getPosition returns an invalid value. And so the SeekCursor fails.
    // In the consequence ALL parts of code where two calls to the seek cursor are done, while the second call _relys_ on
    // the first one, should be secured against recursion, with a broad-minded interpretion of "recursion" : if any of these
    // code parts is executed, no other should be accessible. But this sounds very difficult to achieve ....
    // )

    // The next problem caused by the same behaviuor (SeekCursor causes a propertyChanged) : when adjusting rows we implicitly
    // change our selection. So a "FirstSelected(); SeekCursor(); NextSelected();" may produce unpredictable results.
    // That's why we _first_ collect the indicies of the selected rows and _then_ their bookmarks.
    long nIdx = FirstSelectedRow();
    while (nIdx >= 0)
    {
        // (we misuse the bookmarks array for this ...)
        pBookmarks[i++] <<= (sal_Int32)nIdx;
        nIdx = NextSelectedRow();
    }
    DBG_ASSERT(i == nSelectedRows, "FmGridControl::DeleteSelectedRows : could not collect the row indicies !");

    for (i=0; i<nSelectedRows; ++i)
    {
        nIdx = ::comphelper::getINT32(pBookmarks[i]);
        if (IsEmptyRow(nIdx))
        {
            // leerzeile nicht loeschen
            aBookmarks.realloc(--nSelectedRows);
            SelectRow(nIdx,sal_False);          // selection aufheben fuer leerzeile
            break;
        }

        // Zunaechst den DatenCursor auf den selektierten Satz pos.
        if (SeekCursor(nIdx))
        {
            GetSeekRow()->SetState(m_pSeekCursor, sal_True);

            pBookmarks[i] = m_pSeekCursor->getBookmark();
        }
#if DBG_UTIL
        else
            DBG_ERROR("FmGridControl::DeleteSelectedRows : a bookmark could not be determined !");
#endif
    }
    SetUpdateMode(sal_True);

    // if one of the SeekCursor-calls failed ....
    aBookmarks.realloc(i);

    // (the alternative : while collecting the bookmarks lock our propertyChanged, this should resolve both our problems.
    // but this would be incompatible as we need a locking flag, then ...)

    return aBookmarks;
}