summaryrefslogtreecommitdiffstats
path: root/src/kernel/qpalette.cpp
blob: 77ddf8a8b069efe0cc2d95b65b9a0f90c9a029cf (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
/****************************************************************************
**
** Implementation of TQColorGroup and TQPalette classes
**
** Created : 950323
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the kernel 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 retquirements 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 sales@trolltech.com.
**
** 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 "qpalette.h"

#ifndef QT_NO_PALETTE
#include "qdatastream.h"
#include "qcleanuphandler.h"

/*****************************************************************************
  TQColorGroup member functions
 *****************************************************************************/

/*!
    \class TQColorGroup qpalette.h
    \brief The TQColorGroup class contains a group of widget colors.

    \ingroup appearance
    \ingroup graphics
    \ingroup images

    A color group contains a group of colors used by widgets for
    drawing themselves. We recommend that widgets use color group
    roles such as "foreground" and "base" rather than literal colors
    like "red" or "turquoise". The color roles are enumerated and
    defined in the \l ColorRole documentation.

    The most common use of TQColorGroup is like this:

    \code
	TQPainter p;
	...
	p.setPen( colorGroup().foreground() );
	p.drawLine( ... )
    \endcode

    It is also possible to modify color groups or create new color
    groups from scratch.

    The color group class can be created using three different
    constructors or by modifying one supplied by TQt. The default
    constructor creates an all-black color group, which can then be
    modified using set functions; there's also a constructor for
    specifying all the color group colors. And there is also a copy
    constructor.

    We strongly recommend using a system-supplied color group and
    modifying that as necessary.

    You modify a color group by calling the access functions
    setColor() and setBrush(), depending on whether you want a pure
    color or a pixmap pattern.

    There are also corresponding color() and brush() getters, and a
    commonly used convenience function to get each ColorRole:
    background(), foreground(), base(), etc.

    \sa TQColor TQPalette TQWidget::colorGroup()
*/


/*!
    \enum TQColorGroup::ColorRole

    The ColorRole enum defines the different symbolic color roles used
    in current GUIs.

    The central roles are:

    \value Background  general background color.

    \value Foreground  general foreground color.

    \value Base  used as background color for text entry widgets, for example;
    usually white or another light color.

    \value Text  the foreground color used with \c Base. Usually this
    is the same as the \c Foreground, in which case it must provide good
    contrast with \c Background and \c Base.

    \value Button  general button background color in which buttons need a
    background different from \c Background, as in the Macintosh style.

    \value ButtonText  a foreground color used with the \c Button color.

    There are some color roles used mostly for 3D bevel and shadow
    effects:

    \value Light  lighter than \c Button color.

    \value Midlight  between \c Button and \c Light.

    \value Dark  darker than \c Button.

    \value Mid  between \c Button and \c Dark.

    \value Shadow  a very dark color.
    By default, the shadow color is \c TQt::black.

    All of these are normally derived from \c Background and used in
    ways that depend on that relationship. For example, buttons depend
    on it to make the bevels look attractive, and Motif scroll bars
    depend on \c Mid to be slightly different from \c Background.

    Selected (marked) items have two roles:

    \value Highlight   a color to indicate a selected item or the
    current item. By default, the highlight color is \c TQt::darkBlue.

    \value HighlightedText  a text color that contrasts with \c Highlight.
    By default, the highlighted text color is \c TQt::white.

    Finally, there is a special role for text that needs to be
    drawn where \c Text or \c Foreground would give poor contrast,
    such as on pressed push buttons:

    \value BrightText a text color that is very different from \c
    Foreground and contrasts well with e.g. \c Dark.

    \value Link a text color used for unvisited hyperlinks.
    By default, the link color is \c TQt::blue.

    \value LinkVisited a text color used for already visited hyperlinks.
    By default, the linkvisited color is \c TQt::magenta.

    \value NColorRoles Internal.

    Note that text colors can be used for things other than just
    words; text colors are \e usually used for text, but it's quite
    common to use the text color roles for lines, icons, etc.

    This image shows most of the color roles in use:
    \img palette.png Color Roles
*/


class TQColorGroupPrivate : public TQShared
{
public:
    TQBrush br[TQColorGroup::NColorRoles];
    TQColorGroupPrivate* detach() {
	if ( count > 1 ) {
	    deref();
	    TQColorGroupPrivate* d = new TQColorGroupPrivate;
	    for (int i=0; i<TQColorGroup::NColorRoles; i++)
		d->br[i] = br[i];
	    return d;
	}
	return this;
    }
};

/*!
    Constructs a color group with all colors set to black.
*/

TQColorGroup::TQColorGroup()
{
    static TQColorGroupPrivate* defColorGroupData = 0;
    if ( !defColorGroupData ) {
	static TQSharedCleanupHandler<TQColorGroupPrivate> defColorGroupCleanup;
	defColorGroupData = new TQColorGroupPrivate;
	defColorGroupCleanup.set( &defColorGroupData );
    }
    d = defColorGroupData;
    br = d->br;
    d->ref();
}

/*!
    Constructs a color group that is an independent copy of \a other.
*/
TQColorGroup::TQColorGroup( const TQColorGroup& other )
{
    d = other.d;
    d->ref();
    br = d->br;
}

/*!
    Copies the colors of \a other to this color group.
*/
TQColorGroup& TQColorGroup::operator =(const TQColorGroup& other)
{
    if ( d != other.d ) {
	if ( d->deref() )
	    delete d;
	d = other.d;
	br = d->br;
	d->ref();
    }
    return *this;
}

static TQColor qt_mix_colors( TQColor a, TQColor b)
{
    return TQColor( (a.red() + b.red()) / 2, (a.green() + b.green()) / 2, (a.blue() + b.blue()) / 2 );
}


/*!
    Constructs a color group. You can pass either brushes, pixmaps or
    plain colors for \a foreground, \a button, \a light, \a dark, \a
    mid, \a text, \a bright_text, \a base and \a background.

    \sa TQBrush
*/
 TQColorGroup::TQColorGroup( const TQBrush &foreground, const TQBrush &button,
			   const TQBrush &light, const TQBrush &dark,
			   const TQBrush &mid, const TQBrush &text,
			   const TQBrush &bright_text, const TQBrush &base,
			   const TQBrush &background)
{
    d = new TQColorGroupPrivate;
    br = d->br;
    br[Foreground]      = foreground;
    br[Button]		= button;
    br[Light]		= light;
    br[Dark]		= dark;
    br[Mid]		= mid;
    br[Text]		= text;
    br[BrightText]	= bright_text;
    br[ButtonText]	= text;
    br[Base]		= base;
    br[Background]	= background;
    br[Midlight]	= qt_mix_colors( br[Button].color(), br[Light].color() );
    br[Shadow]          = TQt::black;
    br[Highlight]       = TQt::darkBlue;
    br[HighlightedText] = TQt::white;
    br[Link]            = TQt::blue;
    br[LinkVisited]     = TQt::magenta;
}


/*!\obsolete

  Constructs a color group with the specified colors. The button
  color will be set to the background color.
*/

TQColorGroup::TQColorGroup( const TQColor &foreground, const TQColor &background,
			  const TQColor &light, const TQColor &dark,
			  const TQColor &mid,
			  const TQColor &text, const TQColor &base )
{
    d = new TQColorGroupPrivate;
    br = d->br;
    br[Foreground]      = TQBrush(foreground);
    br[Button]          = TQBrush(background);
    br[Light]           = TQBrush(light);
    br[Dark]            = TQBrush(dark);
    br[Mid]             = TQBrush(mid);
    br[Text]            = TQBrush(text);
    br[BrightText]      = br[Light];
    br[ButtonText]      = br[Text];
    br[Base]            = TQBrush(base);
    br[Background]      = TQBrush(background);
    br[Midlight]	= qt_mix_colors( br[Button].color(), br[Light].color() );
    br[Shadow]          = TQt::black;
    br[Highlight]       = TQt::darkBlue;
    br[HighlightedText] = TQt::white;
    br[Link]            = TQt::blue;
    br[LinkVisited]     = TQt::magenta;
}

/*!
    Destroys the color group.
*/

TQColorGroup::~TQColorGroup()
{
    if ( d->deref() )
	delete d;
}

/*!
    Returns the color that has been set for color role \a r.

    \sa brush() ColorRole
 */
const TQColor &TQColorGroup::color( ColorRole r ) const
{
    return br[r].color();
}

/*!
    Returns the brush that has been set for color role \a r.

    \sa color() setBrush() ColorRole
*/
const TQBrush &TQColorGroup::brush( ColorRole r ) const
{
    return br[r];
}

/*!
    Sets the brush used for color role \a r to a solid color \a c.

    \sa brush() setColor() ColorRole
*/
void TQColorGroup::setColor( ColorRole r, const TQColor &c )
{
    setBrush( r, TQBrush(c) );
}

/*!
    Sets the brush used for color role \a r to \a b.

    \sa brush() setColor() ColorRole
*/
void TQColorGroup::setBrush( ColorRole r, const TQBrush &b )
{
    d = d->detach();
    br = d->br;
    br[r] = b;
}


/*!
    \fn const TQColor & TQColorGroup::foreground() const

    Returns the foreground color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::button() const

    Returns the button color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::light() const

    Returns the light color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor& TQColorGroup::midlight() const

    Returns the midlight color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::dark() const

    Returns the dark color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::mid() const

    Returns the mid color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::text() const

    Returns the text foreground color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::brightText() const

    Returns the bright text foreground color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::buttonText() const

    Returns the button text foreground color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::base() const

    Returns the base color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::background() const

    Returns the background color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::shadow() const

    Returns the shadow color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::highlight() const

    Returns the highlight color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::highlightedText() const

    Returns the highlighted text color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::link() const

    Returns the unvisited link text color of the color group.

    \sa ColorRole
*/

/*!
    \fn const TQColor & TQColorGroup::linkVisited() const

    Returns the visited link text color of the color group.

    \sa ColorRole
*/

/*!
    \fn bool TQColorGroup::operator!=( const TQColorGroup &g ) const

    Returns TRUE if this color group is different from \a g; otherwise
    returns  FALSE.

    \sa operator!=()
*/

/*!
    Returns TRUE if this color group is equal to \a g; otherwise
    returns FALSE.

    \sa operator==()
*/

bool TQColorGroup::operator==( const TQColorGroup &g ) const
{
    if ( d == g.d )
	return TRUE;
    for( int r = 0 ; r < NColorRoles ; r++ )
	if ( br[r] != g.br[r] )
	    return FALSE;
    return TRUE;
}


/*****************************************************************************
  TQPalette member functions
 *****************************************************************************/

/*!
    \class TQPalette qpalette.h

    \brief The TQPalette class contains color groups for each widget state.

    \ingroup appearance
    \ingroup shared
    \ingroup graphics
    \ingroup images
    \mainclass

    A palette consists of three color groups: \e active, \e disabled,
    and \e inactive. All widgets contain a palette, and all widgets in
    TQt use their palette to draw themselves. This makes the user
    interface easily configurable and easier to keep consistent.

    If you create a new widget we strongly recommend that you use the
    colors in the palette rather than hard-coding specific colors.

    The color groups:
    \list
    \i The active() group is used for the window that has keyboard focus.
    \i The inactive() group is used for other windows.
    \i The disabled() group is used for widgets (not windows) that are
    disabled for some reason.
    \endlist

    Both active and inactive windows can contain disabled widgets.
    (Disabled widgets are often called \e inaccessible or \e{grayed
    out}.)

    In Motif style, active() and inactive() look the same. In Windows
    2000 style and Macintosh Platinum style, the two styles look
    slightly different.

    There are setActive(), setInactive(), and setDisabled() functions
    to modify the palette. (TQt also supports a normal() group; this is
    an obsolete alias for active(), supported for backwards
    compatibility.)

    Colors and brushes can be set for particular roles in any of a
    palette's color groups with setColor() and setBrush().

    You can copy a palette using the copy constructor and test to see
    if two palettes are \e identical using isCopyOf().

    \sa TQApplication::setPalette(), TQWidget::setPalette(), TQColorGroup, TQColor
*/

/*!
    \enum TQPalette::ColorGroup

    \value Disabled
    \value Active
    \value Inactive
    \value NColorGroups
    \value Normal synonym for Active
*/

/*!
    \obsolete

    \fn const TQColorGroup &TQPalette::normal() const

    Returns the active color group. Use active() instead.

    \sa setActive() active()
*/

/*!
    \obsolete

    \fn void TQPalette::setNormal( const TQColorGroup & cg )

    Sets the active color group to \a cg. Use setActive() instead.

    \sa setActive() active()
*/


static int palette_count = 1;

/*!
    Constructs a palette that consists of color groups with only black
    colors.
*/

TQPalette::TQPalette()
{
    static TQPalData *defPalData = 0;
    if ( !defPalData ) {                // create common palette data
	defPalData = new TQPalData;      //   for the default palette
	static TQSharedCleanupHandler<TQPalData> defPalCleanup;
	defPalCleanup.set( &defPalData );
	defPalData->ser_no = palette_count++;
    }
    data = defPalData;
    data->ref();
}

/*!\obsolete
  Constructs a palette from the \a button color. The other colors are
  automatically calculated, based on this color. Background will be
  the button color as well.
*/

TQPalette::TQPalette( const TQColor &button )
{
    data = new TQPalData;
    Q_CHECK_PTR( data );
    data->ser_no = palette_count++;
    TQColor bg = button, btn = button, fg, base, disfg;
    int h, s, v;
    bg.hsv( &h, &s, &v );
    if ( v > 128 ) {				// light background
	fg   = TQt::black;
	base = TQt::white;
	disfg = TQt::darkGray;
    } else {					// dark background
	fg   = TQt::white;
	base = TQt::black;
	disfg = TQt::darkGray;
    }
    data->active   = TQColorGroup( fg, btn, btn.light(150), btn.dark(),
				  btn.dark(150), fg, TQt::white, base, bg );
    data->disabled = TQColorGroup( disfg, btn, btn.light(150), btn.dark(),
				  btn.dark(150), disfg, TQt::white, base, bg );
    data->inactive = data->active;
}

/*!
    Constructs a palette from a \a button color and a \a background.
    The other colors are automatically calculated, based on these
    colors.
*/

TQPalette::TQPalette( const TQColor &button, const TQColor &background )
{
    data = new TQPalData;
    Q_CHECK_PTR( data );
    data->ser_no = palette_count++;
    TQColor bg = background, btn = button, fg, base, disfg;
    int h, s, v;
    bg.hsv( &h, &s, &v );
    if ( v > 128 ) {				// light background
	fg   = TQt::black;
	base = TQt::white;
	disfg = TQt::darkGray;
    } else {					// dark background
	fg   = TQt::white;
	base = TQt::black;
	disfg = TQt::darkGray;
    }
    data->active   = TQColorGroup( fg, btn, btn.light(150), btn.dark(),
				  btn.dark(150), fg, TQt::white, base, bg );
    data->disabled = TQColorGroup( disfg, btn, btn.light(150), btn.dark(),
				  btn.dark(150), disfg, TQt::white, base, bg );
    data->inactive = data->active;
}

/*!
    Constructs a palette that consists of the three color groups \a
    active, \a disabled and \a inactive. See the \link #details
    Detailed Description\endlink for definitions of the color groups
    and \l TQColorGroup::ColorRole for definitions of each color role
    in the three groups.

    \sa TQColorGroup TQColorGroup::ColorRole TQPalette
*/

TQPalette::TQPalette( const TQColorGroup &active, const TQColorGroup &disabled,
		    const TQColorGroup &inactive )
{
    data = new TQPalData;
    Q_CHECK_PTR( data );
    data->ser_no = palette_count++;
    data->active = active;
    data->disabled = disabled;
    data->inactive = inactive;
}

/*!
    Constructs a copy of \a p.

    This constructor is fast (it uses copy-on-write).
*/

TQPalette::TQPalette( const TQPalette &p )
{
    data = p.data;
    data->ref();
}

/*!
    Destroys the palette.
*/

TQPalette::~TQPalette()
{
    if ( data->deref() )
	delete data;
}

/*!
    Assigns \a p to this palette and returns a reference to this
    palette.

    This is fast (it uses copy-on-write).

    \sa copy()
*/

TQPalette &TQPalette::operator=( const TQPalette &p )
{
    p.data->ref();
    if ( data->deref() )
	delete data;
    data = p.data;
    return *this;
}


/*!
    Returns the color in color group \a gr, used for color role \a r.

    \sa brush() setColor() TQColorGroup::ColorRole
*/
const TQColor &TQPalette::color( ColorGroup gr, TQColorGroup::ColorRole r ) const
{
    return directBrush( gr, r ).color();
}

/*!
    Returns the brush in color group \a gr, used for color role \a r.

    \sa color() setBrush() TQColorGroup::ColorRole
*/
const TQBrush &TQPalette::brush( ColorGroup gr, TQColorGroup::ColorRole r ) const
{
    return directBrush( gr, r );
}

/*!
    Sets the brush in color group \a gr, used for color role \a r, to
    the solid color \a c.

    \sa setBrush() color() TQColorGroup::ColorRole
*/
void TQPalette::setColor( ColorGroup gr, TQColorGroup::ColorRole r,
			 const TQColor &c)
{
    setBrush( gr, r, TQBrush(c) );
}

/*!
    Sets the brush in color group \a gr, used for color role \a r, to
    \a b.

    \sa brush() setColor() TQColorGroup::ColorRole
*/
void TQPalette::setBrush( ColorGroup gr, TQColorGroup::ColorRole r,
			 const TQBrush &b)
{
    detach();
    data->ser_no = palette_count++;
    directSetBrush( gr, r, b);
}

/*!
    \overload

    Sets the brush color used for color role \a r to color \a c in all
    three color groups.

    \sa color() setBrush() TQColorGroup::ColorRole
*/
void TQPalette::setColor( TQColorGroup::ColorRole r, const TQColor &c )
{
    setBrush( r, TQBrush(c) );
}

/*!
    \overload

    Sets the brush in for color role \a r in all three color groups to
    \a b.

    \sa brush() setColor() TQColorGroup::ColorRole active() inactive() disabled()
*/
void TQPalette::setBrush( TQColorGroup::ColorRole r, const TQBrush &b )
{
    detach();
    data->ser_no = palette_count++;
    directSetBrush( Active, r, b );
    directSetBrush( Disabled, r, b );
    directSetBrush( Inactive, r, b );
}


/*!
    Returns a deep copy of this palette.

    \warning This is slower than the copy constructor and assignment
    operator and offers no benefits.
*/

TQPalette TQPalette::copy() const
{
    TQPalette p( data->active, data->disabled, data->inactive );
    return p;
}


/*!
    Detaches this palette from any other TQPalette objects with which
    it might implicitly share TQColorGroup objects. In essence, does
    the copying part of copy-on-write.

    Calling this should generally not be necessary; TQPalette calls it
    itself when necessary.
*/

void TQPalette::detach()
{
    if ( data->count != 1 )
	*this = copy();
}

/*!
    \fn const TQColorGroup & TQPalette::disabled() const

    Returns the disabled color group of this palette.

    \sa TQColorGroup, setDisabled(), active(), inactive()
*/

/*!
    Sets the \c Disabled color group to \a g.

    \sa disabled() setActive() setInactive()
*/

void TQPalette::setDisabled( const TQColorGroup &g )
{
    detach();
    data->ser_no = palette_count++;
    data->disabled = g;
}

/*!
    \fn const TQColorGroup & TQPalette::active() const

    Returns the active color group of this palette.

    \sa TQColorGroup, setActive(), inactive(), disabled()
*/

/*!
    Sets the \c Active color group to \a g.

    \sa active() setDisabled() setInactive() TQColorGroup
*/

void TQPalette::setActive( const TQColorGroup &g )
{
    detach();
    data->ser_no = palette_count++;
    data->active = g;
}

/*!
    \fn const TQColorGroup & TQPalette::inactive() const

    Returns the inactive color group of this palette.

    \sa TQColorGroup,  setInactive(), active(), disabled()
*/

/*!
    Sets the \c Inactive color group to \a g.

    \sa active() setDisabled() setActive() TQColorGroup
*/

void TQPalette::setInactive( const TQColorGroup &g )
{
    detach();
    data->ser_no = palette_count++;
    data->inactive = g;
}


/*!
    \fn bool TQPalette::operator!=( const TQPalette &p ) const

    Returns TRUE (slowly) if this palette is different from \a p;
    otherwise returns FALSE (usually tquickly).
*/

/*!
    Returns TRUE (usually tquickly) if this palette is equal to \a p;
    otherwise returns FALSE (slowly).
*/

bool TQPalette::operator==( const TQPalette &p ) const
{
    return data->active == p.data->active &&
	   data->disabled == p.data->disabled &&
	   data->inactive == p.data->inactive;
}


/*!
    \fn int TQPalette::serialNumber() const

    Returns a number that uniquely identifies this TQPalette object.
    The serial number is intended for caching. Its value may not be
    used for anything other than equality testing.

    Note that TQPalette uses copy-on-write, and the serial number
    changes during the lazy copy operation (detach()), not during a
    shallow copy (copy constructor or assignment).

    \sa TQPixmap TQPixmapCache TQCache
*/


/*****************************************************************************
  TQColorGroup/TQPalette stream functions
 *****************************************************************************/

#ifndef QT_NO_DATASTREAM
/*!
    \relates TQColorGroup

    Writes color group, \a g to the stream \a s.

    \sa \link datastreamformat.html Format of the TQDataStream operators \endlink
*/

TQDataStream &operator<<( TQDataStream &s, const TQColorGroup &g )
{
    if ( s.version() == 1 ) {
	// TQt 1.x
	s << g.foreground()
	  << g.background()
	  << g.light()
	  << g.dark()
	  << g.mid()
	  << g.text()
	  << g.base();
    } else {
	int max = TQColorGroup::NColorRoles;
	if ( s.version() <= 3) // TQt 2.x
	    max = 14;

	for( int r = 0 ; r < max ; r++ )
	    s << g.brush( (TQColorGroup::ColorRole)r);
    }
    return s;
}

/*!
    \related TQColorGroup

    Reads a color group from the stream.

    \sa \link datastreamformat.html Format of the TQDataStream operators \endlink
*/

TQDataStream &operator>>( TQDataStream &s, TQColorGroup &g )
{
    if ( s.version() == 1 ) {
	// TQt 1.x
	TQColor fg, bg, light, dark, mid, text, base;
	s >> fg >> bg >> light >> dark >> mid >> text >> base;
	TQPalette p( bg );
	TQColorGroup n( p.active() );
	n.setColor( TQColorGroup::Foreground, fg );
	n.setColor( TQColorGroup::Light, light );
	n.setColor( TQColorGroup::Dark, dark );
	n.setColor( TQColorGroup::Mid, mid );
	n.setColor( TQColorGroup::Text, text );
	n.setColor( TQColorGroup::Base, base );
	g = n;
    } else {
	int max = TQColorGroup::NColorRoles;
	if (s.version() <= 3) // TQt 2.x
	    max = 14;

	TQBrush tmp;
	for( int r = 0 ; r < max; r++ ) {
	    s >> tmp;
	    g.setBrush( (TQColorGroup::ColorRole)r, tmp);
	}
    }
    return s;
}


/*!
    \relates TQPalette

    Writes the palette, \a p to the stream \a s and returns a
    reference to the stream.

    \sa \link datastreamformat.html Format of the TQDataStream operators \endlink
*/

TQDataStream &operator<<( TQDataStream &s, const TQPalette &p )
{
    return s << p.active()
	     << p.disabled()
	     << p.inactive();
}


static void readV1ColorGroup( TQDataStream &s, TQColorGroup &g,
		       TQPalette::ColorGroup r )
{
    TQColor fg, bg, light, dark, mid, text, base;
    s >> fg >> bg >> light >> dark >> mid >> text >> base;
    TQPalette p( bg );
    TQColorGroup n;
    switch ( r ) {
	case TQPalette::Disabled:
	    n = p.disabled();
	    break;
	case TQPalette::Inactive:
	    n = p.inactive();
	    break;
	default:
	    n = p.active();
	    break;
    }
    n.setColor( TQColorGroup::Foreground, fg );
    n.setColor( TQColorGroup::Light, light );
    n.setColor( TQColorGroup::Dark, dark );
    n.setColor( TQColorGroup::Mid, mid );
    n.setColor( TQColorGroup::Text, text );
    n.setColor( TQColorGroup::Base, base );
    g = n;
}


/*!
    \relates TQPalette

    Reads a palette from the stream, \a s into the palette \a p, and
    returns a reference to the stream.

    \sa \link datastreamformat.html Format of the TQDataStream operators \endlink
*/

TQDataStream &operator>>( TQDataStream &s, TQPalette &p )
{
    TQColorGroup active, disabled, inactive;
    if ( s.version() == 1 ) {
	readV1ColorGroup( s, active, TQPalette::Active );
	readV1ColorGroup( s, disabled, TQPalette::Disabled );
	readV1ColorGroup( s, inactive, TQPalette::Inactive );
    } else {
	s >> active >> disabled >> inactive;
    }
    TQPalette newpal( active, disabled, inactive );
    p = newpal;
    return s;
}
#endif //QT_NO_DATASTREAM

/*!
    Returns TRUE if this palette and \a p are copies of each other,
    i.e. one of them was created as a copy of the other and neither
    was subsequently modified; otherwise returns FALSE. This is much
    stricter than equality.

    \sa operator=() operator==()
*/

bool TQPalette::isCopyOf( const TQPalette & p )
{
    return data && data == p.data;
}

const TQBrush &TQPalette::directBrush( ColorGroup gr, TQColorGroup::ColorRole r ) const
{
    if ( (uint)gr > (uint)TQPalette::NColorGroups ) {
#if defined(QT_CHECK_RANGE)
	qWarning( "TQPalette::directBrush: colorGroup(%i) out of range", gr );
#endif
	return data->active.br[TQColorGroup::Foreground];
    }
    if ( (uint)r >= (uint)TQColorGroup::NColorRoles ) {
#if defined(QT_CHECK_RANGE)
	qWarning( "TQPalette::directBrush: colorRole(%i) out of range", r );
#endif
	return data->active.br[TQColorGroup::Foreground];
    }
    switch( gr ) {
    case Active:
	return data->active.br[r];
	//break;
    case Disabled:
	return data->disabled.br[r];
	//break;
    case Inactive:
	return data->inactive.br[r];
	//break;
    default:
	break;
    }
#if defined(QT_CHECK_RANGE)
    qWarning( "TQPalette::directBrush: colorGroup(%i) internal error", gr );
#endif
    return data->active.br[TQColorGroup::Foreground]; // Satisfy compiler
}

void TQPalette::directSetBrush( ColorGroup gr, TQColorGroup::ColorRole r, const TQBrush& b)
{
    if ( (uint)gr > (uint)TQPalette::NColorGroups ) {
#if defined(QT_CHECK_RANGE)
	qWarning( "TQPalette::directBrush: colorGroup(%i) out of range", gr );
#endif
	return;
    }
    if ( (uint)r >= (uint)TQColorGroup::NColorRoles ) {
#if defined(QT_CHECK_RANGE)
	qWarning( "TQPalette::directBrush: colorRole(%i) out of range", r );
#endif
	return;
    }
    switch( gr ) {
    case Active:
	data->active.setBrush(r,b);
	break;
    case Disabled:
	data->disabled.setBrush(r,b);
	break;
    case Inactive:
	data->inactive.setBrush(r,b);
	break;
    default:
#if defined(QT_CHECK_RANGE)
	qWarning( "TQPalette::directBrush: colorGroup(%i) internal error", gr );
#endif
	break;
    }
}


/*!\internal*/
TQColorGroup::ColorRole TQPalette::foregroundRoleFromMode( TQt::BackgroundMode mode )
{
    switch (mode) {
    case TQt::PaletteButton:
	return TQColorGroup::ButtonText;
    case TQt::PaletteBase:
	return TQColorGroup::Text;
    case TQt::PaletteDark:
    case TQt::PaletteShadow:
	return TQColorGroup::Light;
    case TQt::PaletteHighlight:
	return TQColorGroup::HighlightedText;
    case TQt::PaletteBackground:
    default:
	return TQColorGroup::Foreground;
    }
}

/*!\internal*/
TQColorGroup::ColorRole TQPalette::backgroundRoleFromMode( TQt::BackgroundMode mode)
{
    switch (mode) {
    case TQt::PaletteForeground:
	return TQColorGroup::Foreground;
    case TQt::PaletteButton:
	return TQColorGroup::Button;
    case TQt::PaletteLight:
	return TQColorGroup::Light;
    case TQt::PaletteMidlight:
	return TQColorGroup::Midlight;
    case TQt::PaletteDark:
	return TQColorGroup::Dark;
    case TQt::PaletteMid:
	return TQColorGroup::Mid;
    case TQt::PaletteText:
	return TQColorGroup::Text;
    case TQt::PaletteBrightText:
	return TQColorGroup::BrightText;
    case TQt::PaletteButtonText:
	return TQColorGroup::ButtonText;
    case TQt::PaletteBase:
	return TQColorGroup::Base;
    case TQt::PaletteShadow:
	return TQColorGroup::Shadow;
    case TQt::PaletteHighlight:
	return TQColorGroup::Highlight;
    case TQt::PaletteHighlightedText:
	return TQColorGroup::HighlightedText;
    case TQt::PaletteLink:
	return TQColorGroup::Link;
    case TQt::PaletteLinkVisited:
	return TQColorGroup::LinkVisited;
    case TQt::PaletteBackground:
    default:
	return TQColorGroup::Background;
    }
}

#endif // QT_NO_PALETTE