summaryrefslogtreecommitdiffstats
path: root/src/dialogs/qcolordialog.cpp
blob: df042437689de7bb7ef3d7a0709fd0cf8a7f8ec6 (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
/****************************************************************************
**
** Implementation of TQColorDialog class
**
** Created : 990222
**
** Copyright (C) 1999-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the dialogs module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "ntqcolordialog.h"

#ifndef TQT_NO_COLORDIALOG

#include "ntqpainter.h"
#include "ntqlayout.h"
#include "ntqlabel.h"
#include "ntqpushbutton.h"
#include "ntqlineedit.h"
#include "ntqimage.h"
#include "ntqpixmap.h"
#include "ntqdrawutil.h"
#include "ntqvalidator.h"
#include "ntqdragobject.h"
#include "ntqgridview.h"
#include "ntqapplication.h"
#include "ntqstyle.h"
#include "ntqsettings.h"
#include "ntqpopupmenu.h"

#ifdef TQ_WS_MAC
TQRgb macGetRgba( TQRgb initial, bool *ok, TQWidget *parent, const char* name );
TQColor macGetColor( const TQColor& initial, TQWidget *parent, const char *name );
#endif

#ifdef TQ_WS_X11
#include "private/tqttdeintegration_x11_p.h"
#endif

//////////// TQWellArray BEGIN

struct TQWellArrayData;

class TQWellArray : public TQGridView
{
    TQ_OBJECT
    TQ_PROPERTY( int selectedColumn READ selectedColumn )
    TQ_PROPERTY( int selectedRow READ selectedRow )

public:
    TQWellArray( TQWidget* parent=0, const char* name=0, bool popup = FALSE );

    ~TQWellArray() {}
    TQString cellContent( int row, int col ) const;
    // ### Paul !!! virtual void setCellContent( int row, int col, const TQString &);

    int selectedColumn() const { return selCol; }
    int selectedRow() const { return selRow; }

    virtual void setCurrent( int row, int col );
    virtual void setSelected( int row, int col );

    TQSize sizeHint() const;

    virtual void setCellBrush( int row, int col, const TQBrush & );
    TQBrush cellBrush( int row, int col );

signals:
    void selected( int row, int col );

protected:
    void dimensionChange( int oldRows, int oldCols );

    virtual void paintCell( TQPainter *, int row, int col );
    virtual void paintCellContents( TQPainter *, int row, int col, const TQRect& );

    void mousePressEvent( TQMouseEvent* );
    void mouseReleaseEvent( TQMouseEvent* );
    void mouseMoveEvent( TQMouseEvent* );
    void keyPressEvent( TQKeyEvent* );
    void focusInEvent( TQFocusEvent* );
    void focusOutEvent( TQFocusEvent* );

private:
    int curRow;
    int curCol;
    int selRow;
    int selCol;
    bool smallStyle;
    TQWellArrayData *d;

private:	// Disabled copy constructor and operator=
#if defined(TQ_DISABLE_COPY)
    TQWellArray( const TQWellArray & );
    TQWellArray& operator=( const TQWellArray & );
#endif
};



// non-interface ...



struct TQWellArrayData {
    TQBrush *brush;
};

/*!
    \internal
    \class TQWellArray qwellarray_p.h
    \brief The TQWellArray class provides a well array.


    \ingroup advanced
*/

TQWellArray::TQWellArray( TQWidget *parent, const char * name, bool popup )
    : TQGridView( parent, name,
		 (popup ? (WStyle_Customize|WStyle_Tool|WStyle_NoBorder) : 0 ) )
{
    d = 0;
    setFocusPolicy( StrongFocus );
    setVScrollBarMode(AlwaysOff);
    setHScrollBarMode(AlwaysOff);
    viewport()->setBackgroundMode( PaletteBackground );
    setNumCols( 7 );
    setNumRows( 7 );
    setCellWidth( 24 );
    setCellHeight( 21 );
    smallStyle = popup;

    if ( popup ) {
	setCellWidth( 18 );
	setCellHeight( 18 );
	setFrameStyle(TQFrame::StyledPanel | TQFrame::Raised);
	setMargin( 1 );
	setLineWidth( 2 );
    } else {
	setFrameStyle( TQFrame::NoFrame );
    }
    curCol = 0;
    curRow = 0;
    selCol = -1;
    selRow = -1;

    if ( smallStyle )
	setMouseTracking( TRUE );
}


TQSize TQWellArray::sizeHint() const
{
    constPolish();
    TQSize s = gridSize().boundedTo( TQSize(640, 480 ) );
    return TQSize( s.width() + 2*frameWidth(), s.height() + 2*frameWidth() );
}


void TQWellArray::paintCell( TQPainter* p, int row, int col )
{
    int w = cellWidth();			// width of cell in pixels
    int h = cellHeight();			// height of cell in pixels
    int b = 1;

    if ( !smallStyle )
	b = 3;

    const TQColorGroup & g = colorGroup();
    p->setPen( TQPen( black, 0, SolidLine ) );
    if ( !smallStyle && row ==selRow && col == selCol &&
	 style().styleHint(TQStyle::SH_GUIStyle) != MotifStyle) {
	int n = 2;
	p->drawRect( n, n, w-2*n, h-2*n );
    }

    style().drawPrimitive(TQStyle::PE_Panel, p, TQRect(b, b, w-2*b, h-2*b), g,
			  TQStyle::Style_Enabled | TQStyle::Style_Sunken);

    int t = 0;
    if (style().styleHint(TQStyle::SH_GUIStyle) == MotifStyle)
	t = ( row == selRow && col == selCol ) ? 2 : 0;
    b += 2 + t;

    if ( (row == curRow) && (col == curCol) ) {
	if ( smallStyle ) {
	    p->setPen ( white );
	    p->drawRect( 1, 1, w-2, h-2 );
	    p->setPen ( black );
	    p->drawRect( 0, 0, w, h );
	    p->drawRect( 2, 2, w-4, h-4 );
	    b = 3;
	} else if ( hasFocus() ) {
	    style().drawPrimitive(TQStyle::PE_FocusRect, p, TQRect(0, 0, w, h), g);
	}
    }
    paintCellContents( p, row, col, TQRect(b, b, w - 2*b, h - 2*b) );
}

/*!
  Reimplement this function to change the contents of the well array.
 */
void TQWellArray::paintCellContents( TQPainter *p, int row, int col, const TQRect &r )
{

    if ( d ) {
	p->fillRect( r, d->brush[row*numCols()+col] );
    } else {
	p->fillRect( r, white );
	p->setPen( black );
	p->drawLine( r.topLeft(), r.bottomRight() );
	p->drawLine( r.topRight(), r.bottomLeft() );
    }
}


/*\reimp
*/
void TQWellArray::mousePressEvent( TQMouseEvent* e )
{
    // The current cell marker is set to the cell the mouse is pressed
    // in.
    TQPoint pos = e->pos();
    setCurrent( rowAt( pos.y() ), columnAt( pos.x() ) );
}

/*\reimp
*/
void TQWellArray::mouseReleaseEvent( TQMouseEvent* )
{
    // The current cell marker is set to the cell the mouse is clicked
    // in.
    setSelected( curRow, curCol );
}


/*\reimp
*/
void TQWellArray::mouseMoveEvent( TQMouseEvent* e )
{
    //   The current cell marker is set to the cell the mouse is
    //   clicked in.
    if ( smallStyle ) {
	TQPoint pos = e->pos();
	setCurrent( rowAt( pos.y() ), columnAt( pos.x() ) );
    }
}

/*
  Sets the cell currently having the focus. This is not necessarily
  the same as the currently selected cell.
*/

void TQWellArray::setCurrent( int row, int col )
{

    if ( (curRow == row) && (curCol == col) )
	return;

    if ( row < 0 || col < 0 )
	row = col = -1;

    int oldRow = curRow;
    int oldCol = curCol;

    curRow = row;
    curCol = col;

    updateCell( oldRow, oldCol );
    updateCell( curRow, curCol );
}


/*!
  Sets the currently selected cell to \a row, \a col.  If \a row or \a
  col are less than zero, the current cell is unselected.

  Does not set the position of the focus indicator.
*/

void TQWellArray::setSelected( int row, int col )
{
    if ( (selRow == row) && (selCol == col) )
	return;

    int oldRow = selRow;
    int oldCol = selCol;

    if ( row < 0 || col < 0 )
	row = col = -1;

    selCol = col;
    selRow = row;

    updateCell( oldRow, oldCol );
    updateCell( selRow, selCol );
    if ( row >= 0 )
	emit selected( row, col );

    if ( isVisible() && ::tqt_cast<TQPopupMenu*>(parentWidget()) )
	parentWidget()->close();
}



/*!\reimp
*/
void TQWellArray::focusInEvent( TQFocusEvent* )
{
    updateCell( curRow, curCol );
}


/*!
  Sets the size of the well array to be \a rows cells by \a cols.
  Resets any brush information set by setCellBrush().
 */
void TQWellArray::dimensionChange( int, int )
{
    if ( d ) {
	if ( d->brush )
	    delete[] d->brush;
	delete d;
	d = 0;
    }
}

void TQWellArray::setCellBrush( int row, int col, const TQBrush &b )
{
    if ( !d ) {
	d = new TQWellArrayData;
	int i = numRows()*numCols();
	d->brush = new TQBrush[i];
    }
    if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() )
	d->brush[row*numCols()+col] = b;
#ifdef QT_CHECK_RANGE
    else
	tqWarning( "TQWellArray::setCellBrush( %d, %d ) out of range", row, col );
#endif
}



/*!
  Returns the brush set for the cell at \a row, \a col. If no brush is set,
  \c NoBrush is returned.
*/

TQBrush TQWellArray::cellBrush( int row, int col )
{
    if ( d && row >= 0 && row < numRows() && col >= 0 && col < numCols() )
	return d->brush[row*numCols()+col];
    return NoBrush;
}



/*!\reimp
*/

void TQWellArray::focusOutEvent( TQFocusEvent* )
{
    updateCell( curRow, curCol );
}

/*\reimp
*/
void TQWellArray::keyPressEvent( TQKeyEvent* e )
{
    switch( e->key() ) {			// Look at the key code
    case Key_Left:				// If 'left arrow'-key,
	if( curCol > 0 )			// and cr't not in leftmost col
	    setCurrent( curRow, curCol - 1);	// set cr't to next left column
	break;
    case Key_Right:				// Correspondingly...
	if( curCol < numCols()-1 )
	    setCurrent( curRow, curCol + 1);
	break;
    case Key_Up:
	if( curRow > 0 )
	    setCurrent( curRow - 1, curCol);
	else if ( smallStyle )
	    focusNextPrevChild( FALSE );
	break;
    case Key_Down:
	if( curRow < numRows()-1 )
	    setCurrent( curRow + 1, curCol);
	else if ( smallStyle )
	    focusNextPrevChild( TRUE );
	break;
    case Key_Space:
    case Key_Return:
    case Key_Enter:
	setSelected( curRow, curCol );
	break;
    default:				// If not an interesting key,
	e->ignore();			// we don't accept the event
	return;
    }

}

//////////// TQWellArray END

static bool initrgb = FALSE;
static TQRgb stdrgb[6*8];
static TQRgb cusrgb[2*8];
static bool customSet = FALSE;


static void initRGB()
{
    if ( initrgb )
	return;
    initrgb = TRUE;
    int i = 0;
    for ( int g = 0; g < 4; g++ )
	for ( int r = 0;  r < 4; r++ )
	    for ( int b = 0; b < 3; b++ )
		stdrgb[i++] = tqRgb( r*255/3, g*255/3, b*255/2 );

    for ( i = 0; i < 2*8; i++ )
	cusrgb[i] = tqRgb(0xff,0xff,0xff);
}

/*!
    Returns the number of custom colors supported by TQColorDialog. All
    color dialogs share the same custom colors.
*/
int TQColorDialog::customCount()
{
    return 2*8;
}

/*!
    Returns custom color number \a i as a TQRgb.
*/
TQRgb TQColorDialog::customColor( int i )
{
    initRGB();
    if ( i < 0 || i >= customCount() ) {
#ifdef QT_CHECK_RANGE
	tqWarning( "TQColorDialog::customColor() index %d out of range", i );
#endif
	i = 0;
    }
    return cusrgb[i];
}

/*!
    Sets custom color number \a i to the TQRgb value \a c.
*/
void TQColorDialog::setCustomColor( int i, TQRgb c )
{
    initRGB();
    if ( i < 0 || i >= customCount() ) {
#ifdef QT_CHECK_RANGE
	tqWarning( "TQColorDialog::setCustomColor() index %d out of range", i );
#endif
	return;
    }
    customSet = TRUE;
    cusrgb[i] = c;
}

/*!
    Sets standard color number \a i to the TQRgb value \a c.
*/

void TQColorDialog::setStandardColor( int i, TQRgb c )
{
    initRGB();
    if ( i < 0 || i >= 6*8 ) {
#ifdef QT_CHECK_RANGE
	tqWarning( "TQColorDialog::setStandardColor() index %d out of range", i );
#endif
	return;
    }
    stdrgb[i] = c;
}

static inline void rgb2hsv( TQRgb rgb, int&h, int&s, int&v )
{
    TQColor c;
    c.setRgb( rgb );
    c.getHsv(h,s,v);
}

class TQColorWell : public TQWellArray
{
public:
    TQColorWell( TQWidget *parent, int r, int c, TQRgb *vals )
	:TQWellArray( parent, "" ), values( vals ), mousePressed( FALSE ), oldCurrent( -1, -1 )
    { setNumRows(r), setNumCols(c); setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum) ); }

protected:
    void paintCellContents( TQPainter *, int row, int col, const TQRect& );
    void mousePressEvent( TQMouseEvent *e );
    void mouseMoveEvent( TQMouseEvent *e );
    void mouseReleaseEvent( TQMouseEvent *e );
#ifndef TQT_NO_DRAGANDDROP
    void dragEnterEvent( TQDragEnterEvent *e );
    void dragLeaveEvent( TQDragLeaveEvent *e );
    void dragMoveEvent( TQDragMoveEvent *e );
    void dropEvent( TQDropEvent *e );
#endif

private:
    TQRgb *values;
    bool mousePressed;
    TQPoint pressPos;
    TQPoint oldCurrent;

};

void TQColorWell::paintCellContents( TQPainter *p, int row, int col, const TQRect &r )
{
    int i = row + col*numRows();
    p->fillRect( r, TQColor( values[i] ) );
}

void TQColorWell::mousePressEvent( TQMouseEvent *e )
{
    oldCurrent = TQPoint( selectedRow(), selectedColumn() );
    TQWellArray::mousePressEvent( e );
    mousePressed = TRUE;
    pressPos = e->pos();
}

void TQColorWell::mouseMoveEvent( TQMouseEvent *e )
{
    TQWellArray::mouseMoveEvent( e );
#ifndef TQT_NO_DRAGANDDROP
    if ( !mousePressed )
	return;
    if ( ( pressPos - e->pos() ).manhattanLength() > TQApplication::startDragDistance() ) {
	setCurrent( oldCurrent.x(), oldCurrent.y() );
	int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
	TQColor col( values[ i ] );
	TQColorDrag *drg = new TQColorDrag( col, this );
	TQPixmap pix( cellWidth(), cellHeight() );
	pix.fill( col );
	TQPainter p( &pix );
	p.drawRect( 0, 0, pix.width(), pix.height() );
	p.end();
	drg->setPixmap( pix );
	mousePressed = FALSE;
	drg->dragCopy();
    }
#endif
}

#ifndef TQT_NO_DRAGANDDROP
void TQColorWell::dragEnterEvent( TQDragEnterEvent *e )
{
    setFocus();
    if ( TQColorDrag::canDecode( e ) )
	e->accept();
    else
	e->ignore();
}

void TQColorWell::dragLeaveEvent( TQDragLeaveEvent * )
{
    if ( hasFocus() )
	parentWidget()->setFocus();
}

void TQColorWell::dragMoveEvent( TQDragMoveEvent *e )
{
    if ( TQColorDrag::canDecode( e ) ) {
	setCurrent( rowAt( e->pos().y() ), columnAt( e->pos().x() ) );
	e->accept();
    } else
	e->ignore();
}

void TQColorWell::dropEvent( TQDropEvent *e )
{
    if ( TQColorDrag::canDecode( e ) ) {
	int i = rowAt( e->pos().y() ) + columnAt( e->pos().x() ) * numRows();
	TQColor col;
	TQColorDrag::decode( e, col );
	values[ i ] = col.rgb();
	repaintContents( FALSE );
	e->accept();
    } else {
	e->ignore();
    }
}

#endif // TQT_NO_DRAGANDDROP

void TQColorWell::mouseReleaseEvent( TQMouseEvent *e )
{
    if ( !mousePressed )
	return;
    TQWellArray::mouseReleaseEvent( e );
    mousePressed = FALSE;
}

class TQColorPicker : public TQFrame
{
    TQ_OBJECT
public:
    TQColorPicker(TQWidget* parent=0, const char* name=0);
    ~TQColorPicker();

public slots:
    void setCol( int h, int s );

signals:
    void newCol( int h, int s );

protected:
    TQSize sizeHint() const;
    void drawContents(TQPainter* p);
    void mouseMoveEvent( TQMouseEvent * );
    void mousePressEvent( TQMouseEvent * );

private:
    int hue;
    int sat;

    TQPoint colPt();
    int huePt( const TQPoint &pt );
    int satPt( const TQPoint &pt );
    void setCol( const TQPoint &pt );

    TQPixmap *pix;
};

static int pWidth = 200;
static int pHeight = 200;

class TQColorLuminancePicker : public TQWidget
{
    TQ_OBJECT
public:
    TQColorLuminancePicker(TQWidget* parent=0, const char* name=0);
    ~TQColorLuminancePicker();

public slots:
    void setCol( int h, int s, int v );
    void setCol( int h, int s );

signals:
    void newHsv( int h, int s, int v );

protected:
    void paintEvent( TQPaintEvent*);
    void mouseMoveEvent( TQMouseEvent * );
    void mousePressEvent( TQMouseEvent * );

private:
    enum { foff = 3, coff = 4 }; //frame and contents offset
    int val;
    int hue;
    int sat;

    int y2val( int y );
    int val2y( int val );
    void setVal( int v );

    TQPixmap *pix;
};


int TQColorLuminancePicker::y2val( int y )
{
    int d = height() - 2*coff - 1;
    return 255 - (y - coff)*255/d;
}

int TQColorLuminancePicker::val2y( int v )
{
    int d = height() - 2*coff - 1;
    return coff + (255-v)*d/255;
}

TQColorLuminancePicker::TQColorLuminancePicker(TQWidget* parent,
						  const char* name)
    :TQWidget( parent, name )
{
    hue = 100; val = 100; sat = 100;
    pix = 0;
    //    setBackgroundMode( NoBackground );
}

TQColorLuminancePicker::~TQColorLuminancePicker()
{
    delete pix;
}

void TQColorLuminancePicker::mouseMoveEvent( TQMouseEvent *m )
{
    setVal( y2val(m->y()) );
}
void TQColorLuminancePicker::mousePressEvent( TQMouseEvent *m )
{
    setVal( y2val(m->y()) );
}

void TQColorLuminancePicker::setVal( int v )
{
    if ( val == v )
	return;
    val = TQMAX( 0, TQMIN(v,255));
    delete pix; pix=0;
    repaint( FALSE ); //###
    emit newHsv( hue, sat, val );
}

//receives from a hue,sat chooser and relays.
void TQColorLuminancePicker::setCol( int h, int s )
{
    setCol( h, s, val );
    emit newHsv( h, s, val );
}

void TQColorLuminancePicker::paintEvent( TQPaintEvent * )
{
    int w = width() - 5;

    TQRect r( 0, foff, w, height() - 2*foff );
    int wi = r.width() - 2;
    int hi = r.height() - 2;
    if ( !pix || pix->height() != hi || pix->width() != wi ) {
	delete pix;
	TQImage img( wi, hi, 32 );
	int y;
	for ( y = 0; y < hi; y++ ) {
	    TQColor c( hue, sat, y2val(y+coff), TQColor::Hsv );
	    TQRgb r = c.rgb();
	    int x;
	    for ( x = 0; x < wi; x++ )
		img.setPixel( x, y, r );
	}
	pix = new TQPixmap;
	pix->convertFromImage(img);
    }
    TQPainter p(this);
    p.drawPixmap( 1, coff, *pix );
    const TQColorGroup &g = colorGroup();
    qDrawShadePanel( &p, r, g, TRUE );
    p.setPen( g.foreground() );
    p.setBrush( g.foreground() );
    TQPointArray a;
    int y = val2y(val);
    a.setPoints( 3, w, y, w+5, y+5, w+5, y-5 );
    erase( w, 0, 5, height() );
    p.drawPolygon( a );
}

void TQColorLuminancePicker::setCol( int h, int s , int v )
{
    val = v;
    hue = h;
    sat = s;
    delete pix; pix=0;
    repaint( FALSE );//####
}

TQPoint TQColorPicker::colPt()
{ return TQPoint( (360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255 ); }
int TQColorPicker::huePt( const TQPoint &pt )
{ return 360 - pt.x()*360/(pWidth-1); }
int TQColorPicker::satPt( const TQPoint &pt )
{ return 255 - pt.y()*255/(pHeight-1) ; }
void TQColorPicker::setCol( const TQPoint &pt )
{ setCol( huePt(pt), satPt(pt) ); }

TQColorPicker::TQColorPicker(TQWidget* parent, const char* name )
    : TQFrame( parent, name )
{
    hue = 0; sat = 0;
    setCol( 150, 255 );

    TQImage img( pWidth, pHeight, 32 );
    int x,y;
    for ( y = 0; y < pHeight; y++ )
	for ( x = 0; x < pWidth; x++ ) {
	    TQPoint p( x, y );
	    img.setPixel( x, y, TQColor(huePt(p), satPt(p),
				       200, TQColor::Hsv).rgb() );
	}
    pix = new TQPixmap;
    pix->convertFromImage(img);
    setBackgroundMode( NoBackground );
    setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed )  );
}

TQColorPicker::~TQColorPicker()
{
    delete pix;
}

TQSize TQColorPicker::sizeHint() const
{
    return TQSize( pWidth + 2*frameWidth(), pHeight + 2*frameWidth() );
}

void TQColorPicker::setCol( int h, int s )
{
    int nhue = TQMIN( TQMAX(0,h), 360 );
    int nsat = TQMIN( TQMAX(0,s), 255);
    if ( nhue == hue && nsat == sat )
	return;
    TQRect r( colPt(), TQSize(20,20) );
    hue = nhue; sat = nsat;
    r = r.unite( TQRect( colPt(), TQSize(20,20) ) );
    r.moveBy( contentsRect().x()-9, contentsRect().y()-9 );
    //    update( r );
    repaint( r, FALSE );
}

void TQColorPicker::mouseMoveEvent( TQMouseEvent *m )
{
    TQPoint p = m->pos() - contentsRect().topLeft();
    setCol( p );
    emit newCol( hue, sat );
}

void TQColorPicker::mousePressEvent( TQMouseEvent *m )
{
    TQPoint p = m->pos() - contentsRect().topLeft();
    setCol( p );
    emit newCol( hue, sat );
}

void TQColorPicker::drawContents(TQPainter* p)
{
    TQRect r = contentsRect();

    p->drawPixmap( r.topLeft(), *pix );
    TQPoint pt = colPt() + r.topLeft();
    p->setPen( TQPen(black) );

    p->fillRect( pt.x()-9, pt.y(), 20, 2, black );
    p->fillRect( pt.x(), pt.y()-9, 2, 20, black );

}

class TQColorShowLabel;



class TQColIntValidator: public TQIntValidator
{
public:
    TQColIntValidator( int bottom, int top,
		   TQWidget * parent, const char *name = 0 )
	:TQIntValidator( bottom, top, parent, name ) {}

    TQValidator::State validate( TQString &, int & ) const;
};

TQValidator::State TQColIntValidator::validate( TQString &s, int &pos ) const
{
    State state = TQIntValidator::validate(s,pos);
    if ( state == Valid ) {
	long int val = s.toLong();
	// This is not a general solution, assumes that top() > 0 and
	// bottom >= 0
	if ( val < 0 ) {
	    s = "0";
	    pos = 1;
	} else if ( val > top() ) {
	    s.setNum( top() );
	    pos = s.length();
	}
    }
    return state;
}



class TQColNumLineEdit : public TQLineEdit
{
public:
    TQColNumLineEdit( TQWidget *parent, const char* name=0 )
	: TQLineEdit( parent, name ) { setMaxLength( 3 );}
    TQSize sizeHint() const {
	return TQSize( fontMetrics().width( "999" ) + 2 * ( margin() + frameWidth() ),
		      TQLineEdit::sizeHint().height() ); }
    void setNum( int i ) {
	TQString s;
	s.setNum(i);
	bool block = signalsBlocked();
	blockSignals(TRUE);
	setText( s );
	blockSignals(block);
    }
    int val() const { return text().toInt(); }
};


class TQColorShower : public TQWidget
{
    TQ_OBJECT
public:
    TQColorShower( TQWidget *parent, const char *name=0 );

    //things that don't emit signals
    void setHsv( int h, int s, int v );

    int currentAlpha() const { return alphaEd->val(); }
    void setCurrentAlpha( int a ) { alphaEd->setNum( a ); }
    void showAlpha( bool b );


    TQRgb currentColor() const { return curCol; }

public slots:
    void setRgb( TQRgb rgb );

signals:
    void newCol( TQRgb rgb );
private slots:
    void rgbEd();
    void hsvEd();
private:
    void showCurrentColor();
    int hue, sat, val;
    TQRgb curCol;
    TQColNumLineEdit *hEd;
    TQColNumLineEdit *sEd;
    TQColNumLineEdit *vEd;
    TQColNumLineEdit *rEd;
    TQColNumLineEdit *gEd;
    TQColNumLineEdit *bEd;
    TQColNumLineEdit *alphaEd;
    TQLabel *alphaLab;
    TQColorShowLabel *lab;
    bool rgbOriginal;
};

class TQColorShowLabel : public TQFrame
{
    TQ_OBJECT

public:
    TQColorShowLabel( TQWidget *parent ) : TQFrame( parent, "qt_colorshow_lbl" ) {
	setFrameStyle( TQFrame::Panel|TQFrame::Sunken );
	setBackgroundMode( PaletteBackground );
	setAcceptDrops( TRUE );
	mousePressed = FALSE;
    }
    void setColor( TQColor c ) { col = c; }

signals:
    void colorDropped( TQRgb );

protected:
    void drawContents( TQPainter *p );
    void mousePressEvent( TQMouseEvent *e );
    void mouseMoveEvent( TQMouseEvent *e );
    void mouseReleaseEvent( TQMouseEvent *e );
#ifndef TQT_NO_DRAGANDDROP
    void dragEnterEvent( TQDragEnterEvent *e );
    void dragLeaveEvent( TQDragLeaveEvent *e );
    void dropEvent( TQDropEvent *e );
#endif

private:
    TQColor col;
    bool mousePressed;
    TQPoint pressPos;

};

void TQColorShowLabel::drawContents( TQPainter *p )
{
    p->fillRect( contentsRect(), col );
}

void TQColorShower::showAlpha( bool b )
{
    if ( b ) {
	alphaLab->show();
	alphaEd->show();
    } else {
	alphaLab->hide();
	alphaEd->hide();
    }
}

void TQColorShowLabel::mousePressEvent( TQMouseEvent *e )
{
    mousePressed = TRUE;
    pressPos = e->pos();
}

void TQColorShowLabel::mouseMoveEvent( TQMouseEvent *e )
{
#ifndef TQT_NO_DRAGANDDROP
    if ( !mousePressed )
	return;
    if ( ( pressPos - e->pos() ).manhattanLength() > TQApplication::startDragDistance() ) {
	TQColorDrag *drg = new TQColorDrag( col, this );
	TQPixmap pix( 30, 20 );
	pix.fill( col );
	TQPainter p( &pix );
	p.drawRect( 0, 0, pix.width(), pix.height() );
	p.end();
	drg->setPixmap( pix );
	mousePressed = FALSE;
	drg->dragCopy();
    }
#endif
}

#ifndef TQT_NO_DRAGANDDROP
void TQColorShowLabel::dragEnterEvent( TQDragEnterEvent *e )
{
    if ( TQColorDrag::canDecode( e ) )
	e->accept();
    else
	e->ignore();
}

void TQColorShowLabel::dragLeaveEvent( TQDragLeaveEvent * )
{
}

void TQColorShowLabel::dropEvent( TQDropEvent *e )
{
    if ( TQColorDrag::canDecode( e ) ) {
	TQColorDrag::decode( e, col );
	repaint( FALSE );
	emit colorDropped( col.rgb() );
	e->accept();
    } else {
	e->ignore();
    }
}
#endif // TQT_NO_DRAGANDDROP

void TQColorShowLabel::mouseReleaseEvent( TQMouseEvent * )
{
    if ( !mousePressed )
	return;
    mousePressed = FALSE;
}

TQColorShower::TQColorShower( TQWidget *parent, const char *name )
    :TQWidget( parent, name)
{
    curCol = tqRgb( -1, -1, -1 );
    TQColIntValidator *val256 = new TQColIntValidator( 0, 255, this );
    TQColIntValidator *val360 = new TQColIntValidator( 0, 360, this );

    TQGridLayout *gl = new TQGridLayout( this, 1, 1, 6 );
    lab = new TQColorShowLabel( this );
    lab->setMinimumWidth( 60 ); //###
    gl->addMultiCellWidget(lab, 0,-1,0,0);
    connect( lab, TQ_SIGNAL( colorDropped(TQRgb) ),
	     this, TQ_SIGNAL( newCol(TQRgb) ) );
    connect( lab, TQ_SIGNAL( colorDropped(TQRgb) ),
	     this, TQ_SLOT( setRgb(TQRgb) ) );

    hEd = new TQColNumLineEdit( this, "qt_hue_edit" );
    hEd->setValidator( val360 );
    TQLabel *l = new TQLabel( hEd, TQColorDialog::tr("Hu&e:"), this, "qt_hue_lbl" );
    l->setAlignment( AlignRight|AlignVCenter );
    gl->addWidget( l, 0, 1 );
    gl->addWidget( hEd, 0, 2 );

    sEd = new TQColNumLineEdit( this, "qt_sat_edit" );
    sEd->setValidator( val256 );
    l = new TQLabel( sEd, TQColorDialog::tr("&Sat:"), this, "qt_sat_lbl" );
    l->setAlignment( AlignRight|AlignVCenter );
    gl->addWidget( l, 1, 1 );
    gl->addWidget( sEd, 1, 2 );

    vEd = new TQColNumLineEdit( this, "qt_val_edit" );
    vEd->setValidator( val256 );
    l = new TQLabel( vEd, TQColorDialog::tr("&Val:"), this, "qt_val_lbl" );
    l->setAlignment( AlignRight|AlignVCenter );
    gl->addWidget( l, 2, 1 );
    gl->addWidget( vEd, 2, 2 );

    rEd = new TQColNumLineEdit( this, "qt_red_edit" );
    rEd->setValidator( val256 );
    l = new TQLabel( rEd, TQColorDialog::tr("&Red:"), this, "qt_red_lbl" );
    l->setAlignment( AlignRight|AlignVCenter );
    gl->addWidget( l, 0, 3 );
    gl->addWidget( rEd, 0, 4 );

    gEd = new TQColNumLineEdit( this, "qt_grn_edit" );
    gEd->setValidator( val256 );
    l = new TQLabel( gEd, TQColorDialog::tr("&Green:"), this, "qt_grn_lbl" );
    l->setAlignment( AlignRight|AlignVCenter );
    gl->addWidget( l, 1, 3 );
    gl->addWidget( gEd, 1, 4 );

    bEd = new TQColNumLineEdit( this, "qt_blue_edit" );
    bEd->setValidator( val256 );
    l = new TQLabel( bEd, TQColorDialog::tr("Bl&ue:"), this, "qt_blue_lbl" );
    l->setAlignment( AlignRight|AlignVCenter );
    gl->addWidget( l, 2, 3 );
    gl->addWidget( bEd, 2, 4 );

    alphaEd = new TQColNumLineEdit( this, "qt_aplha_edit" );
    alphaEd->setValidator( val256 );
    alphaLab = new TQLabel( alphaEd, TQColorDialog::tr("A&lpha channel:"), this, "qt_alpha_lbl" );
    alphaLab->setAlignment( AlignRight|AlignVCenter );
    gl->addMultiCellWidget( alphaLab, 3, 3, 1, 3 );
    gl->addWidget( alphaEd, 3, 4 );
    alphaEd->hide();
    alphaLab->hide();

    connect( hEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(hsvEd()) );
    connect( sEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(hsvEd()) );
    connect( vEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(hsvEd()) );

    connect( rEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(rgbEd()) );
    connect( gEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(rgbEd()) );
    connect( bEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(rgbEd()) );
    connect( alphaEd, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(rgbEd()) );
}

void TQColorShower::showCurrentColor()
{
    lab->setColor( currentColor() );
    lab->repaint(FALSE); //###
}

void TQColorShower::rgbEd()
{
    rgbOriginal = TRUE;
    if ( alphaEd->isVisible() )
	curCol = tqRgba( rEd->val(), gEd->val(), bEd->val(), currentAlpha() );
    else
	curCol = tqRgb( rEd->val(), gEd->val(), bEd->val() );

    rgb2hsv(currentColor(), hue, sat, val );

    hEd->setNum( hue );
    sEd->setNum( sat );
    vEd->setNum( val );

    showCurrentColor();
    emit newCol( currentColor() );
}

void TQColorShower::hsvEd()
{
    rgbOriginal = FALSE;
    hue = hEd->val();
    sat = sEd->val();
    val = vEd->val();

    curCol = TQColor( hue, sat, val, TQColor::Hsv ).rgb();

    rEd->setNum( tqRed(currentColor()) );
    gEd->setNum( tqGreen(currentColor()) );
    bEd->setNum( tqBlue(currentColor()) );

    showCurrentColor();
    emit newCol( currentColor() );
}

void TQColorShower::setRgb( TQRgb rgb )
{
    rgbOriginal = TRUE;
    curCol = rgb;

    rgb2hsv( currentColor(), hue, sat, val );

    hEd->setNum( hue );
    sEd->setNum( sat );
    vEd->setNum( val );

    rEd->setNum( tqRed(currentColor()) );
    gEd->setNum( tqGreen(currentColor()) );
    bEd->setNum( tqBlue(currentColor()) );

    showCurrentColor();
}

void TQColorShower::setHsv( int h, int s, int v )
{
    rgbOriginal = FALSE;
    hue = h; val = v; sat = s; //Range check###
    curCol = TQColor( hue, sat, val, TQColor::Hsv ).rgb();

    hEd->setNum( hue );
    sEd->setNum( sat );
    vEd->setNum( val );

    rEd->setNum( tqRed(currentColor()) );
    gEd->setNum( tqGreen(currentColor()) );
    bEd->setNum( tqBlue(currentColor()) );

    showCurrentColor();
}

class TQColorDialogPrivate : public TQObject
{
TQ_OBJECT
public:
    TQColorDialogPrivate( TQColorDialog *p );
    TQRgb currentColor() const { return cs->currentColor(); }
    void setCurrentColor( TQRgb rgb );

    int currentAlpha() const { return cs->currentAlpha(); }
    void setCurrentAlpha( int a ) { cs->setCurrentAlpha( a ); }
    void showAlpha( bool b ) { cs->showAlpha( b ); }

public slots:
    void addCustom();

    void newHsv( int h, int s, int v );
    void newColorTypedIn( TQRgb rgb );
    void newCustom( int, int );
    void newStandard( int, int );
public:
    TQWellArray *custom;
    TQWellArray *standard;

    TQColorPicker *cp;
    TQColorLuminancePicker *lp;
    TQColorShower *cs;
    int nextCust;
    bool compact;
};

//sets all widgets to display h,s,v
void TQColorDialogPrivate::newHsv( int h, int s, int v )
{
    cs->setHsv( h, s, v );
    cp->setCol( h, s );
    lp->setCol( h, s, v );
}

//sets all widgets to display rgb
void TQColorDialogPrivate::setCurrentColor( TQRgb rgb )
{
    cs->setRgb( rgb );
    newColorTypedIn( rgb );
}

//sets all widgets exept cs to display rgb
void TQColorDialogPrivate::newColorTypedIn( TQRgb rgb )
{
    int h, s, v;
    rgb2hsv(rgb, h, s, v );
    cp->setCol( h, s );
    lp->setCol( h, s, v);
}

void TQColorDialogPrivate::newCustom( int r, int c )
{
    int i = r+2*c;
    setCurrentColor( cusrgb[i] );
    nextCust = i;
    if (standard)
        standard->setSelected(-1,-1);
}

void TQColorDialogPrivate::newStandard( int r, int c )
{
    setCurrentColor( stdrgb[r+c*6] );
    if (custom)
        custom->setSelected(-1,-1);
}

TQColorDialogPrivate::TQColorDialogPrivate( TQColorDialog *dialog ) :
    TQObject(dialog)
{
    compact = FALSE;
    // small displays (e.g. PDAs cannot fit the full color dialog,
    // so just use the color picker.
    if ( tqApp->desktop()->width() < 480 || tqApp->desktop()->height() < 350 )
	compact = TRUE;

    nextCust = 0;
    const int lumSpace = 3;
    int border = 12;
    if ( compact )
	border = 6;
    TQHBoxLayout *topLay = new TQHBoxLayout( dialog, border, 6 );
    TQVBoxLayout *leftLay = 0;

    if ( !compact )
	leftLay = new TQVBoxLayout( topLay );

    initRGB();

    if ( !compact ) {
	standard = new TQColorWell( dialog, 6, 8, stdrgb );
	standard->setCellWidth( 28 );
	standard->setCellHeight( 24 );
	TQLabel * lab = new TQLabel( standard,
				TQColorDialog::tr( "&Basic colors"), dialog, "qt_basiccolors_lbl" );
	connect( standard, TQ_SIGNAL(selected(int,int)), TQ_SLOT(newStandard(int,int)));
	leftLay->addWidget( lab );
	leftLay->addWidget( standard );


	leftLay->addStretch();

	custom = new TQColorWell( dialog, 2, 8, cusrgb );
	custom->setCellWidth( 28 );
	custom->setCellHeight( 24 );
	custom->setAcceptDrops( TRUE );

	connect( custom, TQ_SIGNAL(selected(int,int)), TQ_SLOT(newCustom(int,int)));
	lab = new TQLabel( custom, TQColorDialog::tr( "&Custom colors") , dialog, "qt_custcolors_lbl" );
	leftLay->addWidget( lab );
	leftLay->addWidget( custom );

	TQPushButton *custbut =
	    new TQPushButton( TQColorDialog::tr("&Define Custom Colors >>"),
						dialog, "qt_def_custcolors_lbl" );
	custbut->setEnabled( FALSE );
	leftLay->addWidget( custbut );
    } else {
	// better color picker size for small displays
	pWidth = 150;
	pHeight = 100;

        custom = 0;
        standard = 0;
    }

    TQVBoxLayout *rightLay = new TQVBoxLayout( topLay );

    TQHBoxLayout *pickLay = new TQHBoxLayout( rightLay );


    TQVBoxLayout *cLay = new TQVBoxLayout( pickLay );
    cp = new TQColorPicker( dialog, "qt_colorpicker" );
    cp->setFrameStyle( TQFrame::Panel + TQFrame::Sunken );
    cLay->addSpacing( lumSpace );
    cLay->addWidget( cp );
    cLay->addSpacing( lumSpace );

    lp = new TQColorLuminancePicker( dialog, "qt_luminance_picker" );
    lp->setFixedWidth( 20 ); //###
    pickLay->addWidget( lp );

    connect( cp, TQ_SIGNAL(newCol(int,int)), lp, TQ_SLOT(setCol(int,int)) );
    connect( lp, TQ_SIGNAL(newHsv(int,int,int)), this, TQ_SLOT(newHsv(int,int,int)) );

    rightLay->addStretch();

    cs = new TQColorShower( dialog, "qt_colorshower" );
    connect( cs, TQ_SIGNAL(newCol(TQRgb)), this, TQ_SLOT(newColorTypedIn(TQRgb)));
    rightLay->addWidget( cs );

    TQHBoxLayout *buttons;
    if ( compact )
	buttons = new TQHBoxLayout( rightLay );
    else
	buttons = new TQHBoxLayout( leftLay );

    TQPushButton *ok, *cancel;
    ok = new TQPushButton( TQColorDialog::tr("OK"), dialog, "qt_ok_btn" );
    connect( ok, TQ_SIGNAL(clicked()), dialog, TQ_SLOT(accept()) );
    ok->setDefault(TRUE);
    cancel = new TQPushButton( TQColorDialog::tr("Cancel"), dialog, "qt_cancel_btn" );
    connect( cancel, TQ_SIGNAL(clicked()), dialog, TQ_SLOT(reject()) );
    buttons->addWidget( ok );
    buttons->addWidget( cancel );
    buttons->addStretch();

    if ( !compact ) {
	TQPushButton *addCusBt = new TQPushButton(
					TQColorDialog::tr("&Add to Custom Colors"),
						 dialog, "qt_add_btn" );
	rightLay->addWidget( addCusBt );
	connect( addCusBt, TQ_SIGNAL(clicked()), this, TQ_SLOT(addCustom()) );
    }
}

void TQColorDialogPrivate::addCustom()
{
    cusrgb[nextCust] = cs->currentColor();
    if (custom)
        custom->repaintContents( FALSE );
    nextCust = (nextCust+1) % 16;
}


/*!
    \class TQColorDialog ntqcolordialog.h
    \brief The TQColorDialog class provides a dialog widget for specifying colors.
    \mainclass
    \ingroup dialogs
    \ingroup graphics

    The color dialog's function is to allow users to choose colors.
    For example, you might use this in a drawing program to allow the
    user to set the brush color.

    The static functions provide modal color dialogs.
    \omit
    If you require a modeless dialog, use the TQColorDialog constructor.
    \endomit

    The static getColor() function shows the dialog and allows the
    user to specify a color. The getRgba() function does the same but
    also allows the user to specify a color with an alpha channel
    (transparency) value.

    The user can store customCount() different custom colors. The
    custom colors are shared by all color dialogs, and remembered
    during the execution of the program. Use setCustomColor() to set
    the custom colors, and use customColor() to get them.

    \img qcolordlg-w.png
*/

/*!
    Constructs a default color dialog with parent \a parent and called
    \a name. If \a modal is TRUE the dialog will be modal. Use
    setColor() to set an initial value.

    \sa getColor()
*/

TQColorDialog::TQColorDialog(TQWidget* parent, const char* name, bool modal) :
    TQDialog(parent, name, modal, (  WType_Dialog | WStyle_Customize | WStyle_Title |
                                    WStyle_DialogBorder | WStyle_SysMenu ) )
{
    setSizeGripEnabled( FALSE );
    d = new TQColorDialogPrivate( this );

#ifndef TQT_NO_SETTINGS
    if ( !customSet ) {
	TQSettings settings;
	settings.insertSearchPath( TQSettings::Windows, "/Trolltech" );
	for ( int i = 0; i < 2*8; ++i ) {
	    bool ok = FALSE;
	    TQRgb rgb = (TQRgb)settings.readNumEntry( "/TQt/customColors/" + TQString::number( i ), 0, &ok );
	    if ( ok )
		cusrgb[i] = rgb;
	}
    }
#endif
}

/*!
    Pops up a modal color dialog, lets the user choose a color, and
    returns that color. The color is initially set to \a initial. The
    dialog is a child of \a parent and is called \a name. It returns
    an invalid (see TQColor::isValid()) color if the user cancels the
    dialog. All colors allocated by the dialog will be deallocated
    before this function returns.
*/

TQColor TQColorDialog::getColor( const TQColor& initial, TQWidget *parent,
			       const char *name )
{
#if defined(TQ_WS_X11)
    if( TQTDEIntegration::enabled())
        return TQTDEIntegration::getColor( initial, parent, name );
#elif defined(TQ_WS_MAC)
    return macGetColor(initial, parent, name);
#endif

    int allocContext = TQColor::enterAllocContext();
    TQColorDialog *dlg = new TQColorDialog( parent, name, TRUE );  //modal
#ifndef TQT_NO_WIDGET_TOPEXTRA
    dlg->setCaption( TQColorDialog::tr( "Select color" ) );
#endif
    dlg->setColor( initial );
    dlg->selectColor( initial );
    int resultCode = dlg->exec();
    TQColor::leaveAllocContext();
    TQColor result;
    if ( resultCode == TQDialog::Accepted )
	result = dlg->color();
    TQColor::destroyAllocContext(allocContext);
    delete dlg;
    return result;
}


/*!
    Pops up a modal color dialog to allow the user to choose a color
    and an alpha channel (transparency) value. The color+alpha is
    initially set to \a initial. The dialog is a child of \a parent
    and called \a name.

    If \a ok is non-null, \e *\a ok is set to TRUE if the user clicked
    OK, and to FALSE if the user clicked Cancel.

    If the user clicks Cancel, the \a initial value is returned.
*/

TQRgb TQColorDialog::getRgba( TQRgb initial, bool *ok,
			    TQWidget *parent, const char* name )
{
#if defined(TQ_WS_MAC)
    if( TQTDEIntegration::enabled()) {
        TQColor color = TQTDEIntegration::getColor( TQColor( initial ), parent, name );
        if( ok )
            *ok = color.isValid();
        return color.rgba();
    }
#elif defined(TQ_WS_MAC)
    return macGetRgba(initial, ok, parent, name);
#endif

    int allocContext = TQColor::enterAllocContext();
    TQColorDialog *dlg = new TQColorDialog( parent, name, TRUE );  //modal

    TQ_CHECK_PTR( dlg );
#ifndef TQT_NO_WIDGET_TOPEXTRA
    dlg->setCaption( TQColorDialog::tr( "Select color" ) );
#endif
    dlg->setColor( initial );
    dlg->selectColor( initial );
    dlg->setSelectedAlpha( tqAlpha(initial) );
    int resultCode = dlg->exec();
    TQColor::leaveAllocContext();
    TQRgb result = initial;
    if ( resultCode == TQDialog::Accepted ) {
	TQRgb c = dlg->color().rgb();
	int alpha = dlg->selectedAlpha();
	result = tqRgba( tqRed(c), tqGreen(c), tqBlue(c), alpha );
    }
    if ( ok )
	*ok = resultCode == TQDialog::Accepted;

    TQColor::destroyAllocContext(allocContext);
    delete dlg;
    return result;
}





/*!
    Returns the color currently selected in the dialog.

    \sa setColor()
*/

TQColor TQColorDialog::color() const
{
    return TQColor(d->currentColor());
}


/*!
    Destroys the dialog and frees any memory it allocated.
*/

TQColorDialog::~TQColorDialog()
{
#ifndef TQT_NO_SETTINGS
    if ( !customSet ) {
	TQSettings settings;
	settings.insertSearchPath( TQSettings::Windows, "/Trolltech" );
	for ( int i = 0; i < 2*8; ++i )
	    settings.writeEntry( "/TQt/customColors/" + TQString::number( i ), (int)cusrgb[i] );
    }
#endif
}


/*!
    Sets the color shown in the dialog to \a c.

    \sa color()
*/

void TQColorDialog::setColor( const TQColor& c )
{
    d->setCurrentColor( c.rgb() );
}




/*!
    Sets the initial alpha channel value to \a a, and shows the alpha
    channel entry box.
*/

void TQColorDialog::setSelectedAlpha( int a )
{
    d->showAlpha( TRUE );
    d->setCurrentAlpha( a );
}


/*!
    Returns the value selected for the alpha channel.
*/

int TQColorDialog::selectedAlpha() const
{
    return d->currentAlpha();
}

/*!
    Sets focus to the corresponding button, if any.
*/
bool TQColorDialog::selectColor( const TQColor& col )
{
    TQRgb color = col.rgb();
    int i = 0, j = 0;
    // Check standard colors
    if (d->standard) {
        for ( i = 0; i < 6; i++ ) {
            for ( j = 0; j < 8; j++ ) {
                if ( color == stdrgb[i + j*6] ) {
                    d->newStandard( i, j );
                    d->standard->setCurrent( i, j );
                    d->standard->setSelected( i, j );
                    d->standard->setFocus();
                    return TRUE;
                }
            }
        }
    }
    // Check custom colors
    if (d->custom) {
        for ( i = 0; i < 2; i++ ) {
            for ( j = 0; j < 8; j++ ) {
                if ( color == cusrgb[i + j*2] ) {
                    d->newCustom( i, j );
                    d->custom->setCurrent( i, j );
                    d->custom->setSelected( i, j );
                    d->custom->setFocus();
                    return TRUE;
                }
            }
        }
    }
    return FALSE;
}

#include "qcolordialog.moc"

#endif