summaryrefslogtreecommitdiffstats
path: root/tdeui/tdetoolbar.cpp
blob: 5154c78f791d48fd1ef76d4cc2533379a4709f9a (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
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
/* This file is part of the KDE libraries
    Copyright
    (C) 2000 Reginald Stadlbauer ([email protected])
    (C) 1997, 1998 Stephan Kulow ([email protected])
    (C) 1997, 1998 Mark Donohoe ([email protected])
    (C) 1997, 1998 Sven Radej ([email protected])
    (C) 1997, 1998 Matthias Ettrich ([email protected])
    (C) 1999 Chris Schlaeger ([email protected])
    (C) 1999 Kurt Granroth ([email protected])

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <config.h>

#ifdef KDE_USE_FINAL
#undef Always
#include <tqdockwindow.h>
#endif

#include <string.h>

#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqdrawutil.h>
#include <tqstring.h>
#include <tqrect.h>
#include <tqobjectlist.h>
#include <tqtimer.h>
#include <tqstyle.h>
#include <tqlayout.h>

#include <tdetoolbar.h>
#include <tdemainwindow.h>
#include <klineedit.h>
#include <kseparator.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include <tdeaction.h>
#include <kstdaction.h>
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <kiconloader.h>
#include <kcombobox.h>
#include <tdepopupmenu.h>
#include <kanimwidget.h>
#include <kedittoolbar.h>
#include <kipc.h>
#include <twin.h>
#include <kdebug.h>
#include <tdetoolbarbutton.h>

enum {
    CONTEXT_TOP = 0,
    CONTEXT_LEFT = 1,
    CONTEXT_RIGHT = 2,
    CONTEXT_BOTTOM = 3,
    CONTEXT_FLOAT = 4,
    CONTEXT_FLAT = 5,
    CONTEXT_ICONS = 6,
    CONTEXT_TEXT = 7,
    CONTEXT_TEXTRIGHT = 8,
    CONTEXT_TEXTUNDER = 9,
    CONTEXT_ICONSIZES = 50 // starting point for the icon size list, put everything else before
};

class TDEToolBarPrivate
{
public:
    TDEToolBarPrivate() {
        m_iconSize     = 0;
        m_iconText     = TDEToolBar::IconOnly;
        m_highlight    = true;
        m_transparent  = true;
        m_honorStyle   = false;

        m_enableContext  = true;

        m_parent         = NULL;
        m_xmlguiClient   = 0;

        oldPos = TQt::DockUnmanaged;

        modified = m_isHorizontal = positioned = false;

        IconSizeDefault = 0;
        IconTextDefault = "IconOnly";

        NewLineDefault = false;
        OffsetDefault = 0;
        PositionDefault = "Top";
	HiddenDefault = false;
        idleButtons.setAutoDelete(true);
    }

    int m_iconSize;
    TDEToolBar::IconText m_iconText;
    bool m_highlight : 1;
    bool m_transparent : 1;
    bool m_honorStyle : 1;
    bool m_isHorizontal : 1;
    bool m_enableContext : 1;
    bool modified : 1;
    bool positioned : 1;

    TQWidget *m_parent;

    TQMainWindow::ToolBarDock oldPos;

    KXMLGUIClient *m_xmlguiClient;

    struct ToolBarInfo
    {
        ToolBarInfo() : index( -1 ), offset( -1 ), newline( false ), dock( TQt::DockTop ) {}
        ToolBarInfo( TQt::Dock d, int i, bool n, int o ) : index( i ), offset( o ), newline( n ), dock( d ) {}
        int index, offset;
        bool newline;
        TQt::Dock dock;
    };

    ToolBarInfo toolBarInfo;
    TQValueList<int> iconSizes;
    TQTimer repaintTimer;

  // Default Values.
  bool HiddenDefault;
  int IconSizeDefault;
  TQString IconTextDefault;
  bool NewLineDefault;
  int OffsetDefault;
  TQString PositionDefault;

   TQPtrList<TQWidget> idleButtons;
};

TDEToolBarSeparator::TDEToolBarSeparator(Orientation o , bool l, TQToolBar *parent,
                                     const char* name )
    :TQFrame( parent, name ), line( l )
{
    connect( parent, TQT_SIGNAL(orientationChanged(Orientation)),
             this, TQT_SLOT(setOrientation(Orientation)) );
    setOrientation( o );
    setBackgroundMode( parent->backgroundMode() );
    setBackgroundOrigin( ParentOrigin );
}

void TDEToolBarSeparator::setOrientation( Orientation o )
{
    orient = o;
    setFrameStyle( NoFrame );
}

void TDEToolBarSeparator::drawContents( TQPainter* p )
{
    if ( line ) {
        TQStyle::SFlags flags = TQStyle::Style_Default;

        if ( orientation() == Qt::Horizontal )
            flags = flags | TQStyle::Style_Horizontal;

        style().tqdrawPrimitive(TQStyle::PE_DockWindowSeparator, p,
                              contentsRect(), colorGroup(), flags);
    } else {
        TQFrame::drawContents(p);
    }
}

void TDEToolBarSeparator::styleChange( TQStyle& )
{
    setOrientation( orient );
}

TQSize TDEToolBarSeparator::sizeHint() const
{
    int dim = style().pixelMetric( TQStyle::PM_DockWindowSeparatorExtent, this );
    return orientation() == Qt::Vertical ? TQSize( 0, dim ) : TQSize( dim, 0 );
}

TQSizePolicy TDEToolBarSeparator::sizePolicy() const
{
    return TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
}

TDEToolBar::TDEToolBar( TQWidget *parent, const char *name, bool honorStyle, bool readConfig )
    : TQToolBar( TQString::fromLatin1( name ),
      tqt_dynamic_cast<TQMainWindow*>(parent),
      parent, false,
      name ? name : "mainToolBar")
{
    init( readConfig, honorStyle );
}

TDEToolBar::TDEToolBar( TQMainWindow *parentWindow, TQMainWindow::ToolBarDock dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
    : TQToolBar( TQString::fromLatin1( name ),
      parentWindow, dock, newLine,
      name ? name : "mainToolBar")
{
    init( readConfig, honorStyle );
}

TDEToolBar::TDEToolBar( TQMainWindow *parentWindow, TQWidget *dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
    : TQToolBar( TQString::fromLatin1( name ),
      parentWindow, dock, newLine,
      name ? name : "mainToolBar")
{
    init( readConfig, honorStyle );
}

TDEToolBar::~TDEToolBar()
{
    emit toolbarDestroyed();
    delete d;
}

void TDEToolBar::init( bool readConfig, bool honorStyle )
{
    d = new TDEToolBarPrivate();

    setFullSize( true );
    d->m_honorStyle = honorStyle;
    context = 0;
    layoutTimer = new TQTimer( this );
    connect( layoutTimer, TQT_SIGNAL( timeout() ),
             this, TQT_SLOT( rebuildLayout() ) );
    connect( &(d->repaintTimer), TQT_SIGNAL( timeout() ),
             this, TQT_SLOT( slotRepaint() ) );

    if ( kapp ) { // may be null when started inside designer
        connect(kapp, TQT_SIGNAL(toolbarAppearanceChanged(int)), this, TQT_SLOT(slotAppearanceChanged()));
        // request notification of changes in icon style
        kapp->addKipcEventMask(KIPC::IconChanged);
        connect(kapp, TQT_SIGNAL(iconChanged(int)), this, TQT_SLOT(slotIconChanged(int)));
    }

    // finally, read in our configurable settings
    if ( readConfig )
        slotReadConfig();

    if ( mainWindow() )
        connect( mainWindow(), TQT_SIGNAL( toolBarPositionChanged( TQToolBar * ) ),
                 this, TQT_SLOT( toolBarPosChanged( TQToolBar * ) ) );

    // Hack to make sure we recalculate our size when we dock.
    connect( this, TQT_SIGNAL(placeChanged(TQDockWindow::Place)), TQT_SLOT(rebuildLayout()) );
}

int TDEToolBar::insertButton(const TQString& icon, int id, bool enabled,
                            const TQString& text, int index, TDEInstance *_instance )
{
    TDEToolBarButton *button = new TDEToolBarButton( icon, id, this, 0, text, _instance );

    insertWidgetInternal( button, index, id );
    button->setEnabled( enabled );
    doConnections( button );
    return index;
}


int TDEToolBar::insertButton(const TQString& icon, int id, const char *signal,
                            const TQObject *receiver, const char *slot,
                            bool enabled, const TQString& text, int index, TDEInstance *_instance )
{
    TDEToolBarButton *button = new TDEToolBarButton( icon, id, this, 0, text, _instance);
    insertWidgetInternal( button, index, id );
    button->setEnabled( enabled );
    connect( button, signal, receiver, slot );
    doConnections( button );
    return index;
}


int TDEToolBar::insertButton(const TQPixmap& pixmap, int id, bool enabled,
                            const TQString& text, int index )
{
    TDEToolBarButton *button = new TDEToolBarButton( pixmap, id, this, 0, text);
    insertWidgetInternal( button, index, id );
    button->setEnabled( enabled );
    doConnections( button );
    return index;
}


int TDEToolBar::insertButton(const TQPixmap& pixmap, int id, const char *signal,
                            const TQObject *receiver, const char *slot,
                            bool enabled, const TQString& text,
                            int index )
{
    TDEToolBarButton *button = new TDEToolBarButton( pixmap, id, this, 0, text);
    insertWidgetInternal( button, index, id );
    button->setEnabled( enabled );
    connect( button, signal, receiver, slot );
    doConnections( button );
    return index;
}


int TDEToolBar::insertButton(const TQString& icon, int id, TQPopupMenu *popup,
                            bool enabled, const TQString &text, int index )
{
    TDEToolBarButton *button = new TDEToolBarButton( icon, id, this, 0, text );
    insertWidgetInternal( button, index, id );
    button->setEnabled( enabled );
    button->setPopup( popup );
    doConnections( button );
    return index;
}


int TDEToolBar::insertButton(const TQPixmap& pixmap, int id, TQPopupMenu *popup,
                            bool enabled, const TQString &text, int index )
{
    TDEToolBarButton *button = new TDEToolBarButton( pixmap, id, this, 0, text );
    insertWidgetInternal( button, index, id );
    button->setEnabled( enabled );
    button->setPopup( popup );
    doConnections( button );
    return index;
}


int TDEToolBar::insertLined (const TQString& text, int id,
                            const char *signal,
                            const TQObject *receiver, const char *slot,
                            bool enabled ,
                            const TQString& toolTipText,
                            int size, int index )
{
    KLineEdit *lined = new KLineEdit ( this, 0 );
    if ( !toolTipText.isEmpty() )
        TQToolTip::add( lined, toolTipText );
    if ( size > 0 )
        lined->setMinimumWidth( size );
    insertWidgetInternal( lined, index, id );
    connect( lined, signal, receiver, slot );
    lined->setText(text);
    lined->setEnabled( enabled );
    return index;
}

int TDEToolBar::insertCombo (const TQStringList &list, int id, bool writable,
                            const char *signal, const TQObject *receiver,
                            const char *slot, bool enabled,
                            const TQString& tooltiptext,
                            int size, int index,
                            TQComboBox::Policy policy )
{
    KComboBox *combo = new KComboBox ( writable, this );

    insertWidgetInternal( combo, index, id );
    combo->insertStringList (list);
    combo->setInsertionPolicy(policy);
    combo->setEnabled( enabled );
    if ( size > 0 )
        combo->setMinimumWidth( size );
    if (!tooltiptext.isNull())
        TQToolTip::add( combo, tooltiptext );

    if ( signal && receiver && slot )
        connect ( combo, signal, receiver, slot );
    return index;
}


int TDEToolBar::insertCombo (const TQString& text, int id, bool writable,
                            const char *signal, TQObject *receiver,
                            const char *slot, bool enabled,
                            const TQString& tooltiptext,
                            int size, int index,
                            TQComboBox::Policy policy )
{
    KComboBox *combo = new KComboBox ( writable, this );
    insertWidgetInternal( combo, index, id );
    combo->insertItem (text);
    combo->setInsertionPolicy(policy);
    combo->setEnabled( enabled );
    if ( size > 0 )
        combo->setMinimumWidth( size );
    if (!tooltiptext.isNull())
        TQToolTip::add( combo, tooltiptext );
    connect (combo, signal, receiver, slot);
    return index;
}

int TDEToolBar::insertSeparator(int index, int id)
{
    TQWidget *w = new TDEToolBarSeparator( orientation(), false, this, "tool bar separator" );
    insertWidgetInternal( w, index, id );
    return index;
}

int TDEToolBar::insertLineSeparator(int index, int id)
{
    TQWidget *w = new TDEToolBarSeparator( orientation(), true, this, "tool bar separator" );
    insertWidgetInternal( w, index, id );
    return index;
}


int TDEToolBar::insertWidget(int id, int /*width*/, TQWidget *widget, int index)
{
    removeWidgetInternal( widget ); // in case we already have it ?
    insertWidgetInternal( widget, index, id );
    return index;
}

int TDEToolBar::insertAnimatedWidget(int id, TQObject *receiver, const char *slot,
                                    const TQString& icons, int index )
{
    KAnimWidget *anim = new KAnimWidget( icons, d->m_iconSize, this );
    insertWidgetInternal( anim, index, id );

    if ( receiver )
        connect( anim, TQT_SIGNAL(clicked()), receiver, slot);

    return index;
}

KAnimWidget *TDEToolBar::animatedWidget( int id )
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
        return 0;
    KAnimWidget *aw = tqt_dynamic_cast<KAnimWidget *>(*it);
    if ( aw )
        return aw;
    TQObjectList *l = queryList( "KAnimWidget" );
    if ( !l || !l->first() ) {
        delete l;
        return 0;
    }

    for ( TQObject *o = l->first(); o; o = l->next() ) {
        KAnimWidget *aw = tqt_dynamic_cast<KAnimWidget *>(o);
        if ( aw )
        {
            delete l;
            return aw;
        }
    }

    delete l;
    return 0;
}


void TDEToolBar::addConnection (int id, const char *signal,
                               const TQObject *receiver, const char *slot)
{
    TQWidget* w = getWidget( id );
    if ( w )
        connect( w, signal, receiver, slot );
}

void TDEToolBar::setItemEnabled( int id, bool enabled )
{
    TQWidget* w = getWidget( id );
    if ( w )
        w->setEnabled( enabled );
}


void TDEToolBar::setButtonPixmap( int id, const TQPixmap& _pixmap )
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->setPixmap( _pixmap );
}


void TDEToolBar::setButtonIcon( int id, const TQString& _icon )
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->setIcon( _icon );
}

void TDEToolBar::setButtonIconSet( int id, const TQIconSet& iconset )
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->setIconSet( iconset );
}


void TDEToolBar::setDelayedPopup (int id , TQPopupMenu *_popup, bool toggle )
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->setDelayedPopup( _popup, toggle );
}


void TDEToolBar::setAutoRepeat (int id, bool flag)
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->setAutoRepeat( flag );
}


void TDEToolBar::setToggle (int id, bool flag )
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->setToggle( flag );
}


void TDEToolBar::toggleButton (int id)
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->toggle();
}


void TDEToolBar::setButton (int id, bool flag)
{
    TDEToolBarButton * button = getButton( id );
    if ( button )
        button->on( flag );
}


bool TDEToolBar::isButtonOn (int id) const
{
    TDEToolBarButton * button = const_cast<TDEToolBar*>( this )->getButton( id );
    return button ? button->isOn() : false;
}


void TDEToolBar::setLinedText (int id, const TQString& text)
{
    KLineEdit * lineEdit = getLined( id );
    if ( lineEdit )
        lineEdit->setText( text );
}


TQString TDEToolBar::getLinedText (int id) const
{
    KLineEdit * lineEdit = const_cast<TDEToolBar*>( this )->getLined( id );
    return lineEdit ? lineEdit->text() : TQString::null;
}


void TDEToolBar::insertComboItem (int id, const TQString& text, int index)
{
    KComboBox * comboBox = getCombo( id );
    if (comboBox)
        comboBox->insertItem( text, index );
}

void TDEToolBar::insertComboList (int id, const TQStringList &list, int index)
{
    KComboBox * comboBox = getCombo( id );
    if (comboBox)
        comboBox->insertStringList( list, index );
}


void TDEToolBar::removeComboItem (int id, int index)
{
    KComboBox * comboBox = getCombo( id );
    if (comboBox)
        comboBox->removeItem( index );
}


void TDEToolBar::setCurrentComboItem (int id, int index)
{
    KComboBox * comboBox = getCombo( id );
    if (comboBox)
        comboBox->setCurrentItem( index );
}


void TDEToolBar::changeComboItem  (int id, const TQString& text, int index)
{
    KComboBox * comboBox = getCombo( id );
    if (comboBox)
        comboBox->changeItem( text, index );
}


void TDEToolBar::clearCombo (int id)
{
    KComboBox * comboBox = getCombo( id );
    if (comboBox)
        comboBox->clear();
}


TQString TDEToolBar::getComboItem (int id, int index) const
{
    KComboBox * comboBox = const_cast<TDEToolBar*>( this )->getCombo( id );
    return comboBox ? comboBox->text( index ) : TQString::null;
}


KComboBox * TDEToolBar::getCombo(int id)
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
        return 0;
    return tqt_dynamic_cast<KComboBox *>( *it );
}


KLineEdit * TDEToolBar::getLined (int id)
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
        return 0;
    return tqt_dynamic_cast<KLineEdit *>( *it );
}


TDEToolBarButton * TDEToolBar::getButton (int id)
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
        return 0;
    return tqt_dynamic_cast<TDEToolBarButton *>( *it );
}


void TDEToolBar::alignItemRight (int id, bool right )
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
        return;
    if ( rightAligned && !right && (*it) == rightAligned )
        rightAligned = 0;
    if ( (*it) && right )
        rightAligned = (*it);
}


TQWidget *TDEToolBar::getWidget (int id)
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    return ( it == id2widget.end() ) ? 0 : (*it);
}


void TDEToolBar::setItemAutoSized (int id, bool yes )
{
    TQWidget *w = getWidget(id);
    if ( w && yes )
        setStretchableWidget( w );
}


void TDEToolBar::clear ()
{
    /* Delete any idle buttons, so TQToolBar doesn't delete them itself, making a mess */
    for(TQWidget *w=d->idleButtons.first(); w; w=d->idleButtons.next())
       w->blockSignals(false);    
    d->idleButtons.clear();
     
    TQToolBar::clear();
    widget2id.clear();
    id2widget.clear();
}


void TDEToolBar::removeItem(int id)
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
    {
        kdDebug(220) << name() << " TDEToolBar::removeItem item " << id << " not found" << endl;
        return;
    }
    TQWidget * w = (*it);
    id2widget.remove( id );
    widget2id.remove( w );
    widgets.removeRef( w );
    delete w;
}


void TDEToolBar::removeItemDelayed(int id)
{
    Id2WidgetMap::Iterator it = id2widget.find( id );
    if ( it == id2widget.end() )
    {
        kdDebug(220) << name() << " TDEToolBar::removeItem item " << id << " not found" << endl;
        return;
    }
    TQWidget * w = (*it);
    id2widget.remove( id );
    widget2id.remove( w );
    widgets.removeRef( w );

    w->blockSignals(true);
    d->idleButtons.append(w);
    layoutTimer->start( 50, true );
}


void TDEToolBar::hideItem (int id)
{
    TQWidget *w = getWidget(id);
    if ( w )
        w->hide();
}


void TDEToolBar::showItem (int id)
{
    TQWidget *w = getWidget(id);
    if ( w )
        w->show();
}


int TDEToolBar::itemIndex (int id)
{
    TQWidget *w = getWidget(id);
    return w ? widgets.findRef(w) : -1;
}

int TDEToolBar::idAt (int index)
{
    TQWidget *w = widgets.at(index);
    return widget2id[w];
}

void TDEToolBar::setFullSize(bool flag )
{
    setHorizontalStretchable( flag );
    setVerticalStretchable( flag );
}


bool TDEToolBar::fullSize() const
{
    return isHorizontalStretchable() || isVerticalStretchable();
}


void TDEToolBar::enableMoving(bool flag )
{
    setMovingEnabled(flag);
}


void TDEToolBar::setBarPos (BarPosition bpos)
{
    if ( !mainWindow() )
        return;
    mainWindow()->moveDockWindow( this, (Dock)bpos );
    //kdDebug(220) << name() << " setBarPos dockWindowIndex=" << dockWindowIndex() << endl;
}


TDEToolBar::BarPosition TDEToolBar::barPos() const
{
    if ( !this->mainWindow() )
        return place() == TQDockWindow::InDock ? TDEToolBar::Top : TDEToolBar::Floating;
    Dock dock;
    int dm1, dm2;
    bool dm3;
    this->mainWindow()->getLocation( (TQToolBar*)this, dock, dm1, dm3, dm2 );
    if ( dock == DockUnmanaged ) {
        return (TDEToolBar::BarPosition)DockTop;
    }
    return (BarPosition)dock;
}


bool TDEToolBar::enable(BarStatus stat)
{
    bool mystat = isVisible();

    if ( (stat == Toggle && mystat) || stat == Hide )
        hide();
    else
        show();

    return isVisible() == mystat;
}


void TDEToolBar::setMaxHeight ( int h )
{
    setMaximumHeight( h );
}

int TDEToolBar::maxHeight()
{
    return maximumHeight();
}


void TDEToolBar::setMaxWidth (int dw)
{
    setMaximumWidth( dw );
}


int TDEToolBar::maxWidth()
{
    return maximumWidth();
}


void TDEToolBar::setTitle (const TQString& _title)
{
    setLabel( _title );
}


void TDEToolBar::enableFloating (bool )
{
}


void TDEToolBar::setIconText(IconText it)
{
    setIconText( it, true );
}


void TDEToolBar::setIconText(IconText icontext, bool update)
{
    bool doUpdate=false;

    if (icontext != d->m_iconText) {
        d->m_iconText = icontext;
        doUpdate=true;
        //kdDebug(220) << name() << "  icontext has changed, doUpdate=true" << endl;
    }
    else {
        //kdDebug(220) << name() << "  icontext hasn't changed, doUpdate=false" << endl;
    }

    if (!update)
        return;

    if (doUpdate)
        doModeChange(); // tell buttons what happened

    // ugly hack to force a TQMainWindow::triggerLayout( true )
    TQMainWindow *mw = mainWindow();
    if ( mw ) {
        mw->setUpdatesEnabled( false );
        mw->setToolBarsMovable( !mw->toolBarsMovable() );
        mw->setToolBarsMovable( !mw->toolBarsMovable() );
        mw->setUpdatesEnabled( true );
    }
}


TDEToolBar::IconText TDEToolBar::iconText() const
{
    return d->m_iconText;
}


void TDEToolBar::setIconSize(int size)
{
    setIconSize( size, true );
}

void TDEToolBar::setIconSize(int size, bool update)
{
    bool doUpdate=false;

    if ( size != d->m_iconSize ) {
            d->m_iconSize = size;
            doUpdate=true;
    }

    if (!update)
        return;

    if (doUpdate)
        doModeChange(); // tell buttons what happened

    // ugly hack to force a TQMainWindow::triggerLayout( true )
    if ( mainWindow() ) {
        TQMainWindow *mw = mainWindow();
        mw->setUpdatesEnabled( false );
        mw->setToolBarsMovable( !mw->toolBarsMovable() );
        mw->setToolBarsMovable( !mw->toolBarsMovable() );
        mw->setUpdatesEnabled( true );
    }
}

int TDEToolBar::iconSize() const
{
    if ( !d->m_iconSize ) // default value?
		return iconSizeDefault();

	return d->m_iconSize;
}

int TDEToolBar::iconSizeDefault() const
{
	if (!::qstrcmp(name(), "mainToolBar"))
		return TDEGlobal::iconLoader()->currentSize(TDEIcon::MainToolbar);

	return TDEGlobal::iconLoader()->currentSize(TDEIcon::Toolbar);
}

void TDEToolBar::setEnableContextMenu(bool enable )
{
    d->m_enableContext = enable;
}


bool TDEToolBar::contextMenuEnabled() const
{
    return d->m_enableContext;
}


void TDEToolBar::setItemNoStyle(int id, bool no_style )
{
    TDEToolBarButton * button = getButton( id );
    if (button)
        button->setNoStyle( no_style );
}


void TDEToolBar::setFlat (bool flag)
{
    if ( !mainWindow() )
        return;
    if ( flag )
        mainWindow()->moveDockWindow( this, DockMinimized );
    else
        mainWindow()->moveDockWindow( this, DockTop );
    // And remember to save the new look later
    TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mainWindow());
    if ( kmw )
        kmw->setSettingsDirty();
}


int TDEToolBar::count() const
{
    return id2widget.count();
}


void TDEToolBar::saveState()
{
    // first, try to save to the xml file
    if ( d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty() ) {
        //kdDebug(220) << name() << " saveState: saving to " << d->m_xmlguiClient->xmlFile() << endl;
        TQString barname(!::qstrcmp(name(), "unnamed") ? "mainToolBar" : name());
        // try to find our toolbar
        d->modified = false;
        // go down one level to get to the right tags
        TQDomElement current;
        for( TQDomNode n = d->m_xmlguiClient->domDocument().documentElement().firstChild();
             !n.isNull(); n = n.nextSibling()) {
            current = n.toElement();

            if ( current.tagName().lower() != "toolbar" )
                continue;

            TQString curname(current.attribute( "name" ));

            if ( curname == barname ) {
                saveState( current );
                break;
            }
        }
        // if we didn't make changes, then just return
        if ( !d->modified )
            return;

        // now we load in the (non-merged) local file
        TQString local_xml(KXMLGUIFactory::readConfigFile(d->m_xmlguiClient->xmlFile(), true, d->m_xmlguiClient->instance()));
        TQDomDocument local;
        local.setContent(local_xml);

        // make sure we don't append if this toolbar already exists locally
        bool just_append = true;

        for( TQDomNode n = local.documentElement().firstChild();
             !n.isNull(); n = n.nextSibling()) {
            TQDomElement elem = n.toElement();

            if ( elem.tagName().lower() != "toolbar" )
                continue;

            TQString curname(elem.attribute( "name" ));

            if ( curname == barname ) {
                just_append = false;
                local.documentElement().replaceChild( current, elem );
                break;
            }
        }

        if (just_append)
            local.documentElement().appendChild( current );

        KXMLGUIFactory::saveConfigFile(local, d->m_xmlguiClient->localXMLFile(), d->m_xmlguiClient->instance() );

        return;
    }

    // if that didn't work, we save to the config file
    TDEConfig *config = TDEGlobal::config();
    saveSettings(config, TQString::null);
    config->sync();
}

TQString TDEToolBar::settingsGroup() const
{
    TQString configGroup;
    if (!::qstrcmp(name(), "unnamed") || !::qstrcmp(name(), "mainToolBar"))
        configGroup = "Toolbar style";
    else
        configGroup = TQString(name()) + " Toolbar style";
    if ( this->mainWindow() )
    {
        configGroup.prepend(" ");
        configGroup.prepend( this->mainWindow()->name() );
    }
    return configGroup;
}

void TDEToolBar::saveSettings(TDEConfig *config, const TQString &_configGroup)
{
    TQString configGroup = _configGroup;
    if (configGroup.isEmpty())
        configGroup = settingsGroup();
    //kdDebug(220) << name() << " saveSettings() group=" << _configGroup << " -> " << configGroup << endl;

    TQString position, icontext;
    int index;
    getAttributes( position, icontext, index );

    //kdDebug(220) << name() << "                position=" << position << " index=" << index << " offset=" << offset() << " newLine=" << newLine() << endl;

    TDEConfigGroupSaver saver(config, configGroup);

    if(!config->hasDefault("Position") && position == d->PositionDefault )
      config->revertToDefault("Position");
    else
      config->writeEntry("Position", position);

    //kdDebug(220) << name() << "                icontext=" << icontext << " hasDefault:" << config->hasDefault( "IconText" ) << " d->IconTextDefault=" << d->IconTextDefault << endl;

    if(d->m_honorStyle && icontext == d->IconTextDefault && !config->hasDefault("IconText") )
    {
      //kdDebug(220) << name() << "                reverting icontext to default" << endl;
      config->revertToDefault("IconText");
    }
    else
    {
      //kdDebug(220) << name() << "                writing icontext " << icontext << endl;
      config->writeEntry("IconText", icontext);
    }

    if(!config->hasDefault("IconSize") && iconSize() == iconSizeDefault() )
      config->revertToDefault("IconSize");
    else
      config->writeEntry("IconSize", iconSize());

    if(!config->hasDefault("Hidden") && isHidden() == d->HiddenDefault )
      config->revertToDefault("Hidden");
    else
      config->writeEntry("Hidden", isHidden());

    // Note that index, unlike the other settings, depends on the other toolbars
    // So on the first run with a clean local config file, even the usual
    // hasDefault/==IndexDefault test would save the toolbar indexes
    // (IndexDefault was 0, whereas index is the real index in the GUI)
    //
    // Saving the whole set of indexes is necessary though. When moving only
    // one toolbar, if we only saved the changed indexes, the toolbars wouldn't
    // reappear at the same position the next time.
    // The whole set of indexes has to be saved.
    //kdDebug(220) << name() << "                writing index " << index << endl;
    TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mainWindow());
    // don't save if there's only one toolbar

    // Don't use kmw->toolBarIterator() because you might
    // mess up someone else's iterator.  Make the list on your own
    TQPtrList<TDEToolBar> toolbarList;
    TQPtrList<TQToolBar> lst;
    for ( int i = (int)TQMainWindow::DockUnmanaged; i <= (int)DockMinimized; ++i ) {
        lst = kmw->toolBars( (ToolBarDock)i );
        for ( TQToolBar *tb = lst.first(); tb; tb = lst.next() ) {
            if ( !tb->inherits( "TDEToolBar" ) )
                continue;
            toolbarList.append( (TDEToolBar*)tb );
        }
    }
    TQPtrListIterator<TDEToolBar> toolbarIterator( toolbarList );
    if ( !kmw || toolbarIterator.count() > 1 )
        config->writeEntry("Index", index);
    else
        config->revertToDefault("Index");

    if(!config->hasDefault("Offset") && offset() == d->OffsetDefault )
      config->revertToDefault("Offset");
    else
      config->writeEntry("Offset", offset());

    if(!config->hasDefault("NewLine") && newLine() == d->NewLineDefault )
      config->revertToDefault("NewLine");
    else
      config->writeEntry("NewLine", newLine());
}


void TDEToolBar::setXMLGUIClient( KXMLGUIClient *client )
{
    d->m_xmlguiClient = client;
}

void TDEToolBar::setText( const TQString & txt )
{
    setLabel( txt + " (" + kapp->caption() + ") " );
}


TQString TDEToolBar::text() const
{
    return label();
}


void TDEToolBar::doConnections( TDEToolBarButton *button )
{
    connect(button, TQT_SIGNAL(clicked(int)), this, TQT_SIGNAL( clicked( int ) ) );
    connect(button, TQT_SIGNAL(doubleClicked(int)), this, TQT_SIGNAL( doubleClicked( int ) ) );
    connect(button, TQT_SIGNAL(released(int)), this, TQT_SIGNAL( released( int ) ) );
    connect(button, TQT_SIGNAL(pressed(int)), this, TQT_SIGNAL( pressed( int ) ) );
    connect(button, TQT_SIGNAL(toggled(int)), this, TQT_SIGNAL( toggled( int ) ) );
    connect(button, TQT_SIGNAL(highlighted(int, bool)), this, TQT_SIGNAL( highlighted( int, bool ) ) );
}

void TDEToolBar::mousePressEvent ( TQMouseEvent *m )
{
    if ( !mainWindow() )
        return;
    TQMainWindow *mw = mainWindow();
    if ( mw->toolBarsMovable() && d->m_enableContext ) {
        if ( m->button() == Qt::RightButton ) {
	    TQGuardedPtr<TDEToolBar> guard( this );
            int i = contextMenu()->exec( m->globalPos(), 0 );
	    // "Configure Toolbars" recreates toolbars, so we might not exist anymore.
	    if ( guard )
                slotContextAboutToHide();
            switch ( i ) {
            case -1:
                return; // popup canceled
            case CONTEXT_LEFT:
                mw->moveDockWindow( this, DockLeft );
                break;
            case CONTEXT_RIGHT:
                mw->moveDockWindow( this, DockRight );
                break;
            case CONTEXT_TOP:
                mw->moveDockWindow( this, DockTop );
                break;
            case CONTEXT_BOTTOM:
                mw->moveDockWindow( this, DockBottom );
                break;
            case CONTEXT_FLOAT:
                mw->moveDockWindow( this, DockTornOff );
                break;
            case CONTEXT_FLAT:
                mw->moveDockWindow( this, DockMinimized );
                break;
            case CONTEXT_ICONS:
                setIconText( IconOnly );
                break;
            case CONTEXT_TEXTRIGHT:
                setIconText( IconTextRight );
                break;
            case CONTEXT_TEXT:
                setIconText( TextOnly );
                break;
            case CONTEXT_TEXTUNDER:
                setIconText( IconTextBottom );
                break;
            default:
                if ( i >= CONTEXT_ICONSIZES )
					setIconSize( i - CONTEXT_ICONSIZES );
                else
                    return; // assume this was an action handled elsewhere, no need for setSettingsDirty()
            }
            TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mw);
            if ( kmw )
                kmw->setSettingsDirty();
        }
    }
}

void TDEToolBar::doModeChange()
{
    for(TQWidget *w=d->idleButtons.first(); w; w=d->idleButtons.next())
       w->blockSignals(false);
    d->idleButtons.clear();

    emit modechange();
}

void TDEToolBar::rebuildLayout()
{ 
    for(TQWidget *w=d->idleButtons.first(); w; w=d->idleButtons.next())
       w->blockSignals(false);
    d->idleButtons.clear();

    layoutTimer->stop();
    TQApplication::sendPostedEvents( this, TQEvent::ChildInserted );
    TQBoxLayout *l = boxLayout();

    // clear the old layout
    TQLayoutIterator it = l->iterator();
    while ( it.current() )
        it.deleteCurrent();

    for ( TQWidget *w = widgets.first(); w; w = widgets.next() ) {
        if ( w == rightAligned )
            continue;
        TDEToolBarSeparator *ktbs = tqt_dynamic_cast<TDEToolBarSeparator *>(w);
        if ( ktbs && !ktbs->showLine() ) {
            l->addSpacing( orientation() == Qt::Vertical ? w->sizeHint().height() : w->sizeHint().width() );
            w->hide();
            continue;
        }
        if ( tqt_dynamic_cast<TQPopupMenu *>(w) ) // w is a QPopupMenu?
            continue;
        l->addWidget( w );
        w->show();
        if ((orientation() == Qt::Horizontal) && tqt_dynamic_cast<TQLineEdit *>(w)) // w is TQLineEdit ?
            l->addSpacing(2); // A little bit extra spacing behind it.
    }
    if ( rightAligned ) {
        l->addStretch();
        l->addWidget( rightAligned );
        rightAligned->show();
    }

    if ( fullSize() ) {
        if ( !rightAligned )
            l->addStretch();
        if ( stretchableWidget )
            l->setStretchFactor( stretchableWidget, 10 );
    }
    l->invalidate();
    TQApplication::postEvent( this, new TQEvent( TQEvent::LayoutHint ) );
}

void TDEToolBar::childEvent( TQChildEvent *e )
{
    if ( e->child()->isWidgetType() ) {
        TQWidget * w = tqt_dynamic_cast<TQWidget *>(e->child());
        if (!w || !(::qstrcmp( "qt_dockwidget_internal", w->name())))
        {
           TQToolBar::childEvent( e );
           return;
        }
        if ( e->type() == TQEvent::ChildInserted ) {
            if ( !tqt_dynamic_cast<TQPopupMenu *>(w)) { // e->child() is not a QPopupMenu
                // prevent items that have been explicitly inserted by insert*() from
                // being inserted again
                if ( !widget2id.contains( w ) )
                {
                    int dummy = -1;
                    insertWidgetInternal( w, dummy, -1 );
                }
            }
        } else {
            removeWidgetInternal( w );
        }
        if ( isVisibleTo( 0 ) )
        {
            layoutTimer->start( 50, true );
            TQBoxLayout *l = boxLayout();

            // clear the old layout so that we don't get unnecessary layout
            // changes until we have rebuilt the thing
            TQLayoutIterator it = l->iterator();
            while ( it.current() )
               it.deleteCurrent();
        }
    }
    TQToolBar::childEvent( e );
}

void TDEToolBar::insertWidgetInternal( TQWidget *w, int &index, int id )
{
    // we can't have it in widgets, or something is really wrong
    //widgets.removeRef( w );

    connect( w, TQT_SIGNAL( destroyed() ),
             this, TQT_SLOT( widgetDestroyed() ) );
    if ( index == -1 || index > (int)widgets.count() ) {
        index = (int)widgets.count();
        widgets.append( w );
    }
    else
        widgets.insert( index, w );
    if ( id == -1 )
        id = id2widget.count();
    id2widget.insert( id, w );
    widget2id.insert( w, id );
}

void TDEToolBar::showEvent( TQShowEvent *e )
{
    TQToolBar::showEvent( e );
    rebuildLayout();
}

void TDEToolBar::setStretchableWidget( TQWidget *w )
{
    TQToolBar::setStretchableWidget( w );
    stretchableWidget = w;
}

TQSizePolicy TDEToolBar::sizePolicy() const
{
    if ( orientation() == Qt::Horizontal )
        return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
    else
        return TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Expanding );
}

TQSize TDEToolBar::sizeHint() const
{
    TQSize minSize(0,0);
    TDEToolBar *ncThis = const_cast<TDEToolBar *>(this);

    ncThis->polish();

    int margin = static_cast<TQWidget*>(ncThis)->layout()->margin() + frameWidth();
    switch( barPos() )
    {
     case TDEToolBar::Top:
     case TDEToolBar::Bottom:
       for ( TQWidget *w = ncThis->widgets.first(); w; w = ncThis->widgets.next() )
       {
          TQSize sh = w->sizeHint();
          if ( w->sizePolicy().horData() == TQSizePolicy::Ignored )
             sh.setWidth( 1 );
          if ( w->sizePolicy().verData() == TQSizePolicy::Ignored )
             sh.setHeight( 1 );
          sh = sh.boundedTo( w->maximumSize() )
                 .expandedTo( w->minimumSize() ).expandedTo( TQSize(1, 1) );

          minSize = minSize.expandedTo(TQSize(0, sh.height()));
          minSize += TQSize(sh.width()+1, 0);
          if (tqt_dynamic_cast<TQLineEdit *>(w)) // w is a TQLineEdit ?
             minSize += TQSize(2, 0); // A little bit extra spacing behind it.
       }

       minSize += TQSize(TQApplication::style().pixelMetric( TQStyle::PM_DockWindowHandleExtent ), 0);
       minSize += TQSize(margin*2, margin*2);
       break;

     case TDEToolBar::Left:
     case TDEToolBar::Right:
       for ( TQWidget *w = ncThis->widgets.first(); w; w = ncThis->widgets.next() )
       {
          TQSize sh = w->sizeHint();
          if ( w->sizePolicy().horData() == TQSizePolicy::Ignored )
             sh.setWidth( 1 );
          if ( w->sizePolicy().verData() == TQSizePolicy::Ignored )
             sh.setHeight( 1 );
          sh = sh.boundedTo( w->maximumSize() )
                 .expandedTo( w->minimumSize() ).expandedTo( TQSize(1, 1) );

          minSize = minSize.expandedTo(TQSize(sh.width(), 0));
          minSize += TQSize(0, sh.height()+1);
       }
       minSize += TQSize(0, TQApplication::style().pixelMetric( TQStyle::PM_DockWindowHandleExtent ));
       minSize += TQSize(margin*2, margin*2);
       break;

     default:
       minSize = TQToolBar::sizeHint();
       break;
    }
    return minSize;
}

TQSize TDEToolBar::minimumSize() const
{
    return minimumSizeHint();
}

TQSize TDEToolBar::minimumSizeHint() const
{
    return sizeHint();
}

bool TDEToolBar::highlight() const
{
    return d->m_highlight;
}

void TDEToolBar::hide()
{
    TQToolBar::hide();
}

void TDEToolBar::show()
{
    TQToolBar::show();
}

void TDEToolBar::resizeEvent( TQResizeEvent *e )
{
    bool b = isUpdatesEnabled();
    setUpdatesEnabled( false );
    TQToolBar::resizeEvent( e );
    if (b)
    {
      if (layoutTimer->isActive())
      {
         // Wait with repainting till layout is complete.
         d->repaintTimer.start( 100, true );
      }
      else
      {
         // Repaint now
         slotRepaint();
      }
    }
//     else {
//         printf("[WARNING] In TDEToolBar::resizeEvent, but this code block should not be executing.  Preventing toolbar lockup.  [Code 0045]\n");
//         setUpdatesEnabled( true );
//     }
}

void TDEToolBar::slotIconChanged(int group)
{
    if ((group != TDEIcon::Toolbar) && (group != TDEIcon::MainToolbar))
        return;
    if ((group == TDEIcon::MainToolbar) != !::qstrcmp(name(), "mainToolBar"))
        return;

    doModeChange();

    if (isVisible())
        updateGeometry();
}

void TDEToolBar::slotReadConfig()
{
    //kdDebug(220) << name() << " slotReadConfig" << endl;
    // Read appearance settings (hmm, we used to do both here,
    // but a well behaved application will call applyMainWindowSettings
    // anyway, right ?)
    applyAppearanceSettings(TDEGlobal::config(), TQString::null );
}

void TDEToolBar::slotAppearanceChanged()
{
    // Read appearance settings from global file.
    applyAppearanceSettings(TDEGlobal::config(), TQString::null, true /* lose local settings */ );

    // And remember to save the new look later
    TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mainWindow());
    if ( kmw )
        kmw->setSettingsDirty();
}

//static
bool TDEToolBar::highlightSetting()
{
    TQString grpToolbar(TQString::fromLatin1("Toolbar style"));
    TDEConfigGroupSaver saver(TDEGlobal::config(), grpToolbar);
    return TDEGlobal::config()->readBoolEntry(TQString::fromLatin1("Highlighting"),true);
}

//static
bool TDEToolBar::transparentSetting()
{
    TQString grpToolbar(TQString::fromLatin1("Toolbar style"));
    TDEConfigGroupSaver saver(TDEGlobal::config(), grpToolbar);
    return TDEGlobal::config()->readBoolEntry(TQString::fromLatin1("TransparentMoving"),true);
}

//static
TDEToolBar::IconText TDEToolBar::iconTextSetting()
{
    TQString grpToolbar(TQString::fromLatin1("Toolbar style"));
    TDEConfigGroupSaver saver(TDEGlobal::config(), grpToolbar);
    TQString icontext = TDEGlobal::config()->readEntry(TQString::fromLatin1("IconText"),TQString::fromLatin1("IconOnly"));
    if ( icontext == "IconTextRight" )
        return IconTextRight;
    else if ( icontext == "IconTextBottom" )
        return IconTextBottom;
    else if ( icontext == "TextOnly" )
        return TextOnly;
    else
        return IconOnly;
}

void TDEToolBar::applyAppearanceSettings(TDEConfig *config, const TQString &_configGroup, bool forceGlobal)
{
    TQString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
    //kdDebug(220) << name() << " applyAppearanceSettings: configGroup=" << configGroup << " forceGlobal=" << forceGlobal << endl;

    // If we have application-specific settings in the XML file,
    // and nothing in the application's config file, then
    // we don't apply the global defaults, the XML ones are preferred
    // (see applySettings for a full explanation)
    // This is the reason for the xmlgui tests below.
    bool xmlgui = d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty();

    TDEConfig *gconfig = TDEGlobal::config();

    static const TQString &attrIconText  = TDEGlobal::staticQString("IconText");
    static const TQString &attrHighlight = TDEGlobal::staticQString("Highlighting");
    static const TQString &attrTrans     = TDEGlobal::staticQString("TransparentMoving");
    static const TQString &attrIconSize  = TDEGlobal::staticQString("IconSize");

    // we actually do this in two steps.
    // First, we read in the global styles [Toolbar style] (from the KControl module).
    // Then, if the toolbar is NOT 'mainToolBar', we will also try to read in [barname Toolbar style]
    bool highlight;
    int transparent;
    bool applyIconText = !xmlgui; // if xmlgui is used, global defaults won't apply
    bool applyIconSize = !xmlgui;

    int iconSize = d->IconSizeDefault;
    TQString iconText = d->IconTextDefault;

    // this is the first iteration
    TQString grpToolbar(TQString::fromLatin1("Toolbar style"));
    { // start block for TDEConfigGroupSaver
        TDEConfigGroupSaver saver(gconfig, grpToolbar);

        // first, get the generic settings
        highlight   = gconfig->readBoolEntry(attrHighlight, true);
        transparent = gconfig->readBoolEntry(attrTrans, true);

        // we read in the IconText property *only* if we intend on actually
        // honoring it
        if (d->m_honorStyle)
            d->IconTextDefault = gconfig->readEntry(attrIconText, d->IconTextDefault);
        else
            d->IconTextDefault = "IconOnly";

        // Use the default icon size for toolbar icons.
        d->IconSizeDefault = gconfig->readNumEntry(attrIconSize, d->IconSizeDefault);

        iconSize = d->IconSizeDefault;
        iconText = d->IconTextDefault;

        if ( !forceGlobal && config->hasGroup(configGroup) )
        {
            config->setGroup(configGroup);

            // first, get the generic settings
            highlight   = config->readBoolEntry(attrHighlight, highlight);
            transparent = config->readBoolEntry(attrTrans, transparent);

            // read in the IconText property
            if ( config->hasKey( attrIconText ) ) {
                iconText = config->readEntry(attrIconText);
                applyIconText = true;
                //kdDebug(220) << name() << " read icontext=" << d->IconTextDefault << ", that will be the default" << endl;
            }

            // now get the size
            if ( config->hasKey( attrIconSize ) ) {
                iconSize = config->readNumEntry(attrIconSize);
                applyIconSize = true;
            }
        }

        // revert back to the old group
    } // end block for TDEConfigGroupSaver

    bool doUpdate = false;

    IconText icon_text;
    if ( iconText == "IconTextRight" )
        icon_text = IconTextRight;
    else if ( iconText == "IconTextBottom" )
        icon_text = IconTextBottom;
    else if ( iconText == "TextOnly" )
        icon_text = TextOnly;
    else
        icon_text = IconOnly;

    // check if the icon/text has changed
    if (icon_text != d->m_iconText && applyIconText) {
        //kdDebug(220) << name() << " applyAppearanceSettings setIconText " << icon_text << endl;
        setIconText(icon_text, false);
        doUpdate = true;
    }

    // ...and check if the icon size has changed
    if (iconSize != d->m_iconSize && applyIconSize) {
        setIconSize(iconSize, false);
        doUpdate = true;
    }

    TQMainWindow *mw = mainWindow();

    // ...and if we should highlight
    if ( highlight != d->m_highlight ) {
        d->m_highlight = highlight;
        doUpdate = true;
    }

    // ...and if we should move transparently
    if ( mw && transparent != (!mw->opaqueMoving()) ) {
        mw->setOpaqueMoving( !transparent );
    }

    if (doUpdate)
        doModeChange(); // tell buttons what happened

    if (isVisible ())
        updateGeometry();
}

void TDEToolBar::applySettings(TDEConfig *config, const TQString &_configGroup)
{
    return applySettings(config,_configGroup,false);
}

void TDEToolBar::applySettings(TDEConfig *config, const TQString &_configGroup, bool force)
{
    //kdDebug(220) << name() << " applySettings group=" << _configGroup << endl;

    TQString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;

    /*
      Let's explain this a bit more in details.
      The order in which we apply settings is :
       Global config / <appnamerc> user settings                        if no XMLGUI is used
       Global config / App-XML attributes / <appnamerc> user settings   if XMLGUI is used

      So in the first case, we simply read everything from TDEConfig as below,
      but in the second case we don't do anything here if there is no app-specific config,
      and the XMLGUI-related code (loadState()) uses the static methods of this class
      to get the global defaults.

      Global config doesn't include position (index, offset, newline and hidden/shown).
    */

    // First the appearance stuff - the one which has a global config
    applyAppearanceSettings( config, configGroup );

    // ...and now the position stuff
    if ( config->hasGroup(configGroup) || force )
    {
        TDEConfigGroupSaver cgs(config, configGroup);

        static const TQString &attrPosition  = TDEGlobal::staticQString("Position");
        static const TQString &attrIndex  = TDEGlobal::staticQString("Index");
        static const TQString &attrOffset  = TDEGlobal::staticQString("Offset");
        static const TQString &attrNewLine  = TDEGlobal::staticQString("NewLine");
        static const TQString &attrHidden  = TDEGlobal::staticQString("Hidden");

        TQString position = config->readEntry(attrPosition, d->PositionDefault);
        int index = config->readNumEntry(attrIndex, -1);
        int offset = config->readNumEntry(attrOffset, d->OffsetDefault);
        bool newLine = config->readBoolEntry(attrNewLine, d->NewLineDefault);
        bool hidden = config->readBoolEntry(attrHidden, d->HiddenDefault);

        Dock pos(DockTop);
        if ( position == "Top" )
            pos = DockTop;
        else if ( position == "Bottom" )
            pos = DockBottom;
        else if ( position == "Left" )
            pos = DockLeft;
        else if ( position == "Right" )
            pos = DockRight;
        else if ( position == "Floating" )
            pos = DockTornOff;
        else if ( position == "Flat" )
            pos = DockMinimized;

        //kdDebug(220) << name() << " applySettings hidden=" << hidden << endl;
        if (hidden)
            hide();
        else
            show();

        if ( mainWindow() )
        {
            //kdDebug(220) << name() << " applySettings updating ToolbarInfo" << endl;
            d->toolBarInfo = TDEToolBarPrivate::ToolBarInfo( pos, index, newLine, offset );
            positionYourself( true );
        }
        if (isVisible ())
            updateGeometry();
    }
}

bool TDEToolBar::event( TQEvent *e )
{
    if ( (e->type() == TQEvent::LayoutHint) && isUpdatesEnabled() )
       d->repaintTimer.start( 100, true );

    if (e->type() == TQEvent::ChildInserted )
    {
       // Bypass TQToolBar::event,
       // it will show() the inserted child and we don't want to
       // do that until we have rebuilt the layout.
       childEvent((TQChildEvent *)e);
       return true;
    }

    return TQToolBar::event( e );
}

void TDEToolBar::slotRepaint()
{
    setUpdatesEnabled( false );
    // Send a resizeEvent to update the "toolbar extension arrow"
    // (The button you get when your toolbar-items don't fit in
    // the available space)
    TQResizeEvent ev(size(), size());
    resizeEvent(&ev);
    TQApplication::sendPostedEvents( this, TQEvent::LayoutHint );
    setUpdatesEnabled( true );
    repaint( true );
}

void TDEToolBar::toolBarPosChanged( TQToolBar *tb )
{
    if ( tb != this )
        return;
    if ( d->oldPos == DockMinimized )
        rebuildLayout();
    d->oldPos = (TQMainWindow::ToolBarDock)barPos();
    TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mainWindow());
    if ( kmw )
        kmw->setSettingsDirty();
}

static TDEToolBar::Dock stringToDock( const TQString& attrPosition )
{
    TDEToolBar::Dock dock = TDEToolBar::DockTop;
    if ( !attrPosition.isEmpty() ) {
        if ( attrPosition == "top" )
            dock = TDEToolBar::DockTop;
        else if ( attrPosition == "left" )
            dock = TDEToolBar::DockLeft;
        else if ( attrPosition == "right" )
            dock = TDEToolBar::DockRight;
        else if ( attrPosition == "bottom" )
            dock = TDEToolBar::DockBottom;
        else if ( attrPosition == "floating" )
            dock = TDEToolBar::DockTornOff;
        else if ( attrPosition == "flat" )
            dock = TDEToolBar::DockMinimized;
    }
    return dock;
}


void TDEToolBar::loadState( const TQDomElement &element )
{
    TQMainWindow *mw = mainWindow();

    if ( !mw )
        return;

    {
        TQCString text = element.namedItem( "text" ).toElement().text().utf8();
        if ( text.isEmpty() )
            text = element.namedItem( "Text" ).toElement().text().utf8();
        if ( !text.isEmpty() )
            setText( i18n( text ) );
    }

    {
        TQCString attrFullWidth = element.attribute( "fullWidth" ).lower().latin1();
        if ( !attrFullWidth.isEmpty() )
            setFullSize( attrFullWidth == "true" );
    }

    /*
      This method is called in order to load toolbar settings from XML.
      However this can be used in two rather different cases:
      - for the initial loading of the app's XML. In that case the settings
        are only the defaults, the user's TDEConfig settings will override them
        (KDE4 TODO: how about saving those user settings into the local XML file instead?
        Then this whole thing would be simpler, no TDEConfig settings to apply afterwards.
        OTOH we'd have to migrate those settings when the .rc version increases,
        like we do for shortcuts)

      - for later re-loading when switching between parts in KXMLGUIFactory.
        In that case the XML contains the final settings, not the defaults.
        We do need the defaults, and the toolbar might have been completely
        deleted and recreated meanwhile. So we store the app-default settings
        into the XML.
     */
    bool loadingAppDefaults = true;
    if ( element.hasAttribute( "offsetDefault" ) )
    {
        // this isn't the first time, so the defaults have been saved into the (in-memory) XML
        loadingAppDefaults = false;
        d->OffsetDefault = element.attribute( "offsetDefault" ).toInt();
        d->NewLineDefault = element.attribute( "newlineDefault" ) == "true";
        d->HiddenDefault = element.attribute( "hiddenDefault" ) == "true";
        d->IconSizeDefault = element.attribute( "iconSizeDefault" ).toInt();
        d->PositionDefault = element.attribute( "positionDefault" );
        d->IconTextDefault = element.attribute( "iconTextDefault" );
    }
    //kdDebug(220) << name() << " loadState loadingAppDefaults=" << loadingAppDefaults << endl;

    Dock dock = stringToDock( element.attribute( "position" ).lower() );

    {
        TQCString attrIconText = element.attribute( "iconText" ).lower().latin1();
        if ( !attrIconText.isEmpty() ) {
            //kdDebug(220) << name() << " loadState attrIconText=" << attrIconText << endl;
            if ( attrIconText == "icontextright" )
                setIconText( TDEToolBar::IconTextRight );
            else if ( attrIconText == "textonly" )
                setIconText( TDEToolBar::TextOnly );
            else if ( attrIconText == "icontextbottom" )
                setIconText( TDEToolBar::IconTextBottom );
            else if ( attrIconText == "icononly" )
                setIconText( TDEToolBar::IconOnly );
        } else
	{
	    //kdDebug(220) << name() << " loadState no iconText attribute in XML, using iconTextSetting=" << iconTextSetting() << endl;
            // Use global setting
            if (d->m_honorStyle)
                setIconText( iconTextSetting() );
            else
                setIconText( d->IconTextDefault );
	}
    }

    TQString attrIconSize = element.attribute( "iconSize" ).lower();
    int iconSize = d->IconSizeDefault;
    if ( !attrIconSize.isEmpty() )
        iconSize = attrIconSize.toInt();
    setIconSize( iconSize );

    int index = -1; // append by default. This is very important, otherwise
    // with all 0 indexes, we keep reversing the toolbars.
    {
        TQString attrIndex = element.attribute( "index" ).lower();
        if ( !attrIndex.isEmpty() )
            index = attrIndex.toInt();
    }

    int offset = d->OffsetDefault;
    bool newLine = d->NewLineDefault;
    bool hidden = d->HiddenDefault;

    {
        TQString attrOffset = element.attribute( "offset" );
        if ( !attrOffset.isEmpty() )
            offset = attrOffset.toInt();
    }

    {
        TQString attrNewLine = element.attribute( "newline" ).lower();
        if ( !attrNewLine.isEmpty() )
            newLine = attrNewLine == "true";
    }

    {
        TQString attrHidden = element.attribute( "hidden" ).lower();
        if ( !attrHidden.isEmpty() ) {
            hidden = attrHidden  == "true";
        }
    }

    d->toolBarInfo = TDEToolBarPrivate::ToolBarInfo( dock, index, newLine, offset );
    mw->addDockWindow( this, dock, newLine );
    mw->moveDockWindow( this, dock, newLine, index, offset );

    // Apply the highlight button setting
    d->m_highlight = highlightSetting();

    if ( hidden )
        hide();
    else
        show();

    if ( loadingAppDefaults )
    {
        getAttributes( d->PositionDefault, d->IconTextDefault, index );
        //kdDebug(220) << name() << " loadState IconTextDefault=" << d->IconTextDefault << endl;
        d->OffsetDefault = offset;
        d->NewLineDefault = newLine;
        d->HiddenDefault = hidden;
        d->IconSizeDefault = iconSize;
    }
    //kdDebug(220) << name() << " loadState hidden=" << hidden << endl;

    // Apply transparent-toolbar-moving setting (ok, this is global to the mainwindow,
    // but we do it only if there are toolbars...)
    // KDE4: move to TDEMainWindow
    if ( transparentSetting() != !mw->opaqueMoving() ) {
        mw->setOpaqueMoving( !transparentSetting() );
    }
}

int TDEToolBar::dockWindowIndex()
{
    int index = 0;
    Q_ASSERT( mainWindow() );
    if ( mainWindow() ) {
        TQMainWindow::ToolBarDock dock;
        bool newLine;
        int offset;
        mainWindow()->getLocation( this, dock, index, newLine, offset );
    }
    return index;
}

void TDEToolBar::getAttributes( TQString &position, TQString &icontext, int &index )
{
    // get all of the stuff to save
    switch ( barPos() ) {
    case TDEToolBar::Flat:
        position = "Flat";
        break;
    case TDEToolBar::Bottom:
        position = "Bottom";
        break;
    case TDEToolBar::Left:
        position = "Left";
        break;
    case TDEToolBar::Right:
        position = "Right";
        break;
    case TDEToolBar::Floating:
        position = "Floating";
        break;
    case TDEToolBar::Top:
    default:
        position = "Top";
        break;
    }

    index = dockWindowIndex();

    switch (d->m_iconText) {
    case TDEToolBar::IconTextRight:
        icontext = "IconTextRight";
        break;
    case TDEToolBar::IconTextBottom:
        icontext = "IconTextBottom";
        break;
    case TDEToolBar::TextOnly:
        icontext = "TextOnly";
        break;
    case TDEToolBar::IconOnly:
    default:
        icontext = "IconOnly";
        break;
    }
    //kdDebug(220) << name() << " getAttributes: icontext=" << icontext << endl;
}

void TDEToolBar::saveState( TQDomElement &current )
{
    Q_ASSERT( !current.isNull() );
    TQString position, icontext;
    int index = -1;
    getAttributes( position, icontext, index );

    current.setAttribute( "noMerge", "1" );
    current.setAttribute( "position", position );
    current.setAttribute( "iconText", icontext );
    current.setAttribute( "index", index );
    current.setAttribute( "offset", offset() );
    current.setAttribute( "newline", newLine() );
    if ( isHidden() )
        current.setAttribute( "hidden", "true" );
    d->modified = true;

    // TODO if this method is used by more than KXMLGUIBuilder, e.g. to save XML settings to *disk*,
    // then the stuff below shouldn't always be done.
    current.setAttribute( "offsetDefault", d->OffsetDefault );
    current.setAttribute( "newlineDefault", d->NewLineDefault );
    current.setAttribute( "hiddenDefault", d->HiddenDefault ? "true" : "false" );
    current.setAttribute( "iconSizeDefault", d->IconSizeDefault );
    current.setAttribute( "positionDefault", d->PositionDefault );
    current.setAttribute( "iconTextDefault", d->IconTextDefault );

    //kdDebug(220) << name() << " saveState: saving index=" << index << " iconText=" << icontext << " hidden=" << isHidden() << endl;
}

// Called by TDEMainWindow::finalizeGUI
void TDEToolBar::positionYourself( bool force )
{
    if (force)
        d->positioned = false;

    if ( d->positioned || !mainWindow() )
    {
        //kdDebug(220) << name() << " positionYourself d->positioned=true  ALREADY DONE" << endl;
        return;
    }
    // we can't test for ForceHide after moveDockWindow because QDockArea
    // does a reparent() with showIt == true
    bool hidden = isHidden();
    //kdDebug(220) << name() << " positionYourself  dock=" << d->toolBarInfo.dock << " newLine=" << d->toolBarInfo.newline << " index=" << d->toolBarInfo.index << " offset=" << d->toolBarInfo.offset << endl;
    mainWindow()->moveDockWindow( this, d->toolBarInfo.dock,
                                  d->toolBarInfo.newline,
                                  d->toolBarInfo.index,
                                  d->toolBarInfo.offset );

    //kdDebug(220) << name() << " positionYourself dockWindowIndex=" << dockWindowIndex() << endl;
    if ( hidden )
        hide();
    else
        show();
    // This method can only have an effect once - unless force is set
    d->positioned = true;
}

TDEPopupMenu *TDEToolBar::contextMenu()
{
  if ( context )
    return context;
  // Construct our context popup menu. Name it qt_dockwidget_internal so it
  // won't be deleted by TQToolBar::clear().
  context = new TDEPopupMenu( this, "qt_dockwidget_internal" );
  context->insertTitle(i18n("Toolbar Menu"));

  TDEPopupMenu *orient = new TDEPopupMenu( context, "orient" );
  orient->insertItem( i18n("toolbar position string","Top"),  CONTEXT_TOP );
  orient->insertItem( i18n("toolbar position string","Left"), CONTEXT_LEFT );
  orient->insertItem( i18n("toolbar position string","Right"), CONTEXT_RIGHT );
  orient->insertItem( i18n("toolbar position string","Bottom"), CONTEXT_BOTTOM );
  orient->insertSeparator(-1);
  orient->insertItem( i18n("toolbar position string","Floating"), CONTEXT_FLOAT );
  orient->insertItem( i18n("min toolbar", "Flat"), CONTEXT_FLAT );

  TDEPopupMenu *mode = new TDEPopupMenu( context, "mode" );
  mode->insertItem( i18n("Icons Only"), CONTEXT_ICONS );
  mode->insertItem( i18n("Text Only"), CONTEXT_TEXT );
  mode->insertItem( i18n("Text Alongside Icons"), CONTEXT_TEXTRIGHT );
  mode->insertItem( i18n("Text Under Icons"), CONTEXT_TEXTUNDER );

  TDEPopupMenu *size = new TDEPopupMenu( context, "size" );
  size->insertItem( i18n("Default"), CONTEXT_ICONSIZES );
  // Query the current theme for available sizes
  TDEIconTheme *theme = TDEGlobal::instance()->iconLoader()->theme();
  TQValueList<int> avSizes;
  if (theme)
  {
      if (!::qstrcmp(name(), "mainToolBar"))
          avSizes = theme->querySizes( TDEIcon::MainToolbar);
      else
          avSizes = theme->querySizes( TDEIcon::Toolbar);
  }

  d->iconSizes = avSizes;
  qHeapSort(avSizes);

  TQValueList<int>::Iterator it;
  if (avSizes.count() < 10) {
      // Fixed or threshold type icons
	  TQValueList<int>::Iterator end(avSizes.end());
      for (it=avSizes.begin(); it!=end; ++it) {
          TQString text;
          if ( *it < 19 )
              text = i18n("Small (%1x%2)").arg(*it).arg(*it);
          else if (*it < 25)
              text = i18n("Medium (%1x%2)").arg(*it).arg(*it);
          else if (*it < 35)
              text = i18n("Large (%1x%2)").arg(*it).arg(*it);
          else
              text = i18n("Huge (%1x%2)").arg(*it).arg(*it);
          //we use the size as an id, with an offset
          size->insertItem( text, CONTEXT_ICONSIZES + *it );
      }
  }
  else {
      // Scalable icons.
      const int progression[] = {16, 22, 32, 48, 64, 96, 128, 192, 256};

      it = avSizes.begin();
      for (uint i = 0; i < 9; i++) {
          while (it++ != avSizes.end()) {
              if (*it >= progression[i]) {
                  TQString text;
                  if ( *it < 19 )
                      text = i18n("Small (%1x%2)").arg(*it).arg(*it);
                  else if (*it < 25)
                      text = i18n("Medium (%1x%2)").arg(*it).arg(*it);
                  else if (*it < 35)
                      text = i18n("Large (%1x%2)").arg(*it).arg(*it);
                  else
                      text = i18n("Huge (%1x%2)").arg(*it).arg(*it);
                  //we use the size as an id, with an offset
                  size->insertItem( text, CONTEXT_ICONSIZES + *it );
                  break;
              }
          }
      }
  }

  context->insertItem( i18n("Orientation"), orient );
  orient->setItemChecked(CONTEXT_TOP, true);
  context->insertItem( i18n("Text Position"), mode );
  context->setItemChecked(CONTEXT_ICONS, true);
  context->insertItem( i18n("Icon Size"), size );

  connect( context, TQT_SIGNAL( aboutToShow() ), this, TQT_SLOT( slotContextAboutToShow() ) );
  // Unplugging a submenu from abouttohide leads to the popupmenu floating around
  // So better simply call that code from after exec() returns (DF)
  //connect( context, TQT_SIGNAL( aboutToHide() ), this, TQT_SLOT( slotContextAboutToHide() ) );
  return context;
}

void TDEToolBar::slotContextAboutToShow()
{
  // The idea here is to reuse the "static" part of the menu to save time.
  // But the "Toolbars" action is dynamic (can be a single action or a submenu)
  // and ToolBarHandler::setupActions() deletes it, so better not keep it around.
  // So we currently plug/unplug the last two actions of the menu.
  // Another way would be to keep around the actions and plug them all into a (new each time) popupmenu.
  TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mainWindow());
  if ( kmw ) {
      kmw->setupToolbarMenuActions();
      // Only allow hiding a toolbar if the action is also plugged somewhere else (e.g. menubar)
      TDEAction *tbAction = kmw->toolBarMenuAction();
      if ( tbAction && tbAction->containerCount() > 0 )
          tbAction->plug(context);
  }

  // try to find "configure toolbars" action
  TDEAction *configureAction = 0;
  const char* actionName = KStdAction::name(KStdAction::ConfigureToolbars);
  if ( d->m_xmlguiClient )
    configureAction = d->m_xmlguiClient->actionCollection()->action(actionName);
  if ( !configureAction && kmw )
    configureAction = kmw->actionCollection()->action(actionName);
  if ( configureAction )
    configureAction->plug(context);
  KEditToolbar::setDefaultToolbar(name());

  for(int i = CONTEXT_ICONS; i <= CONTEXT_TEXTUNDER; ++i)
    context->setItemChecked(i, false);

  switch( d->m_iconText )
  {
        case IconOnly:
        default:
            context->setItemChecked(CONTEXT_ICONS, true);
            break;
        case IconTextRight:
            context->setItemChecked(CONTEXT_TEXTRIGHT, true);
            break;
        case TextOnly:
            context->setItemChecked(CONTEXT_TEXT, true);
            break;
        case IconTextBottom:
            context->setItemChecked(CONTEXT_TEXTUNDER, true);
            break;
  }

  TQValueList<int>::ConstIterator iIt = d->iconSizes.begin();
  TQValueList<int>::ConstIterator iEnd = d->iconSizes.end();
  for (; iIt != iEnd; ++iIt )
      context->setItemChecked( CONTEXT_ICONSIZES + *iIt, false );

  context->setItemChecked( CONTEXT_ICONSIZES, false );

  context->setItemChecked( CONTEXT_ICONSIZES + d->m_iconSize, true );

  for ( int i = CONTEXT_TOP; i <= CONTEXT_FLAT; ++i )
      context->setItemChecked( i, false );

  switch ( barPos() )
  {
  case TDEToolBar::Flat:
      context->setItemChecked( CONTEXT_FLAT, true );
      break;
  case TDEToolBar::Bottom:
      context->setItemChecked( CONTEXT_BOTTOM, true );
      break;
  case TDEToolBar::Left:
      context->setItemChecked( CONTEXT_LEFT, true );
      break;
  case TDEToolBar::Right:
      context->setItemChecked( CONTEXT_RIGHT, true );
      break;
  case TDEToolBar::Floating:
      context->setItemChecked( CONTEXT_FLOAT, true );
      break;
  case TDEToolBar::Top:
      context->setItemChecked( CONTEXT_TOP, true );
      break;
  default: break;
  }
}

void TDEToolBar::slotContextAboutToHide()
{
  // We have to unplug whatever slotContextAboutToShow plugged into the menu.
  // Unplug the toolbar menu action
  TDEMainWindow *kmw = tqt_dynamic_cast<TDEMainWindow *>(mainWindow());
  if ( kmw && kmw->toolBarMenuAction() )
    if ( kmw->toolBarMenuAction()->containerCount() > 1 )
      kmw->toolBarMenuAction()->unplug(context);

  // Unplug the configure toolbars action too, since it's afterwards anyway
  TDEAction *configureAction = 0;
  const char* actionName = KStdAction::name(KStdAction::ConfigureToolbars);
  if ( d->m_xmlguiClient )
    configureAction = d->m_xmlguiClient->actionCollection()->action(actionName);
  if ( !configureAction && kmw )
    configureAction = kmw->actionCollection()->action(actionName);
  if ( configureAction )
    configureAction->unplug(context);

  TQPtrListIterator<TQWidget> it( widgets );
  TQWidget *wdg;
  while ( ( wdg = it.current() ) != 0 ) {
    if ( wdg->inherits( "TQToolButton" ) )
      static_cast<TQToolButton*>( wdg )->setDown( false );
    ++it;
  }
}

void TDEToolBar::widgetDestroyed()
{
    removeWidgetInternal( (TQWidget*)sender() );
}

void TDEToolBar::removeWidgetInternal( TQWidget * w )
{
    widgets.removeRef( w );
    TQMap< TQWidget*, int >::Iterator it = widget2id.find( w );
    if ( it == widget2id.end() )
        return;
    id2widget.remove( *it );
    widget2id.remove( it );
}

void TDEToolBar::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "tdetoolbar.moc"