summaryrefslogtreecommitdiffstats
path: root/macosx/ttkMacOSXTheme.c
blob: e2bd709d217234defa57abc88dcbaa947b8b03cb (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
/*
 * $Id: ttkMacOSXTheme.c,v 1.5 2006/12/30 23:23:26 cc_benny Exp $
 *
 * Tk theme engine for Mac OSX, using the Appearance Manager API.
 *
 * Copyright (c) 2004 Joe English
 * Copyright (c) 2005 Neil Madden
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * See also:
 *
 * <URL: http://developer.apple.com/documentation/Carbon/Reference/
 * 	Appearance_Manager/appearance_manager/APIIndex.html >
 *
 * Notes:
 * 	"Active" means different things in Mac and Tk terminology --
 *	On Aqua, widgets are "Active" if they belong to the foreground window,
 *	"Inactive" if they are in a background window.
 *	Tk/ttk uses the term "active" to mean that the mouse cursor
 *	is over a widget; aka "hover", "prelight", or "hot-tracked".
 *	(Aqua doesn't use this kind of feedback).
 *
 * 	The QuickDraw/Carbon coordinate system is relative to the
 *	top-level window, *not* to the Tk_Window.  However,
 *	since we're drawing into an off-screen port (Tk "Pixmap),
 *	we don't need to account for this.
 */

#include "tkMacOSXInt.h"
#include "ttk/ttkTheme.h"

/* Define a constant that was renamed for Mac OS X 10.4 */
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1040
#define kThemeDisclosureTriangle kThemeDisclosureButton 
#endif

/*----------------------------------------------------------------------
 * +++ Utilities.
 */

static
Rect BoxToRect(Ttk_Box b)
{
    Rect rect;
    rect.top	= b.y;
    rect.left	= b.x;
    rect.bottom	= b.y + b.height;
    rect.right	= b.x + b.width;
    return rect;
}

/* DontErase --
 * 	No-op ThemeEraseProc, can be passed to DrawThemeButton &c.
 */
static void DontErase(
    const Rect *bounds, UInt32 eraseData, SInt16 depth, Boolean isColorDev)
{  }

#define BEGIN_DRAWING(d) { 				\
    	CGrafPtr saveWorld; GDHandle saveDevice; 	\
	GetGWorld(&saveWorld, &saveDevice);		\
	SetGWorld(TkMacOSXGetDrawablePort(d), 0)	;
#define END_DRAWING 					\
	SetGWorld(saveWorld,saveDevice); 		}

/* Table mapping Tk states to Appearance manager ThemeStates
 */

static Ttk_StateTable ThemeStateTable[] = {
    {kThemeStateUnavailable, TTK_STATE_DISABLED, 0},
    {kThemeStatePressed, TTK_STATE_PRESSED, 0},
    {kThemeStateInactive, TTK_STATE_BACKGROUND, 0},
    {kThemeStateActive, 0, 0}
/* Others: Not sure what these are supposed to mean.
   Up/Down have something to do with "little arrow" increment controls...
   Dunno what a "Rollover" is.
   NEM: Rollover is TTK_STATE_ACTIVE... but we don't handle that yet, by the
   looks of things
    {kThemeStateRollover, 0, 0},
    {kThemeStateUnavailableInactive, 0, 0}
    {kThemeStatePressedUp, 0, 0},
    {kThemeStatePressedDown, 0, 0}
*/
};

/*----------------------------------------------------------------------
 * +++ Button element:  Used for elements drawn with DrawThemeButton.
 */

/* Extra margins to account for drop shadow.
 */
static Ttk_Padding ButtonMargins = {2,2,2,2};

#define NoThemeMetric 0xFFFFFFFF

typedef struct {
    ThemeButtonKind	kind;
    ThemeMetric 	heightMetric;
} ThemeButtonParms;

static ThemeButtonParms
    PushButtonParms = { kThemePushButton, NoThemeMetric },
    CheckBoxParms = { kThemeCheckBox, kThemeMetricCheckBoxHeight },
    RadioButtonParms = { kThemeRadioButton, kThemeMetricRadioButtonHeight },
    BevelButtonParms = { kThemeBevelButton, NoThemeMetric },
    PopupButtonParms = { kThemePopupButton, NoThemeMetric },
    DisclosureParms = { kThemeDisclosureButton, kThemeMetricDisclosureTriangleHeight },
    ListHeaderParms = { kThemeListHeaderButton, kThemeMetricListHeaderHeight };

static Ttk_StateTable ButtonValueTable[] = {
    { kThemeButtonMixed, TTK_STATE_ALTERNATE, 0 },
    { kThemeButtonOn, TTK_STATE_SELECTED, 0 },
    { kThemeButtonOff, 0, 0 }
/* Others: kThemeDisclosureRight, kThemeDisclosureDown, kThemeDisclosureLeft */
};

static Ttk_StateTable ButtonAdornmentTable[] = {
    { kThemeAdornmentDefault, TTK_STATE_ALTERNATE, 0 },
    { kThemeAdornmentFocus, TTK_STATE_FOCUS, 0 },
    { kThemeAdornmentNone, 0, 0 }
};

/*
 * computeButtonDrawInfo --
 *	Fill in an appearance manager ThemeButtonDrawInfo record.
 */
static ThemeButtonDrawInfo computeButtonDrawInfo(
	ThemeButtonParms *parms, Ttk_State state)
{
    ThemeButtonDrawInfo info;
    info.state = Ttk_StateTableLookup(ThemeStateTable, state);
    info.value = Ttk_StateTableLookup(ButtonValueTable, state);
    info.adornment = Ttk_StateTableLookup(ButtonAdornmentTable, state);
    return info;
}

static void ButtonElementGeometryNoPadding(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    ThemeButtonParms *parms = clientData;

    if (parms->heightMetric != NoThemeMetric) {
	SInt32 gratuitouslyOverspecifiedType;
	GetThemeMetric(parms->heightMetric, &gratuitouslyOverspecifiedType);
	*heightPtr = gratuitouslyOverspecifiedType;
    }
}

static void ButtonElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    ThemeButtonParms *parms = clientData;
    ThemeButtonDrawInfo drawInfo = computeButtonDrawInfo(parms, 0);
    Rect scratchRect, contentsRect;
    const int scratchSize = 100;

    ButtonElementGeometryNoPadding(
	clientData, elementRecord, tkwin,
	widthPtr, heightPtr, paddingPtr);

    /* To compute internal padding, query the appearance manager
     * for the content bounds of a dummy rectangle, then use
     * the difference as the padding.
     */
    scratchRect.top = scratchRect.left = 0;
    scratchRect.bottom = scratchRect.right = scratchSize;

    GetThemeButtonContentBounds(
	&scratchRect, parms->kind, &drawInfo, &contentsRect);

    paddingPtr->left = contentsRect.left;
    paddingPtr->top = contentsRect.top;
    paddingPtr->bottom = scratchSize - contentsRect.bottom;
    paddingPtr->right = scratchSize - contentsRect.right;

    /* Now add a little extra padding to account for drop shadows.
     * @@@ SHOULD: call GetThemeButtonBackgroundBounds() instead.
     */

    *paddingPtr = Ttk_AddPadding(*paddingPtr, ButtonMargins);
}

static void ButtonElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(Ttk_PadBox(b, ButtonMargins));
    ThemeButtonParms *parms = clientData;
    ThemeButtonDrawInfo info = computeButtonDrawInfo(parms, state);

    BEGIN_DRAWING(d)
    DrawThemeButton(&bounds, parms->kind, &info,
	NULL/*prevInfo*/,NULL/*eraseProc*/,NULL/*labelProc*/,0/*userData*/);
    END_DRAWING
}

static Ttk_ElementSpec ButtonElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    ButtonElementGeometry,
    ButtonElementDraw
};

/*----------------------------------------------------------------------
 * +++ Notebook elements.
 */

static Ttk_StateTable TabStyleTable[] = {
    { kThemeTabFrontInactive, TTK_STATE_SELECTED|TTK_STATE_BACKGROUND, 0 },
    { kThemeTabNonFrontInactive, TTK_STATE_BACKGROUND, 0 },
    { kThemeTabFrontUnavailable,  TTK_STATE_DISABLED|TTK_STATE_SELECTED, 0 },
    { kThemeTabNonFrontUnavailable, TTK_STATE_DISABLED, 0 },
    { kThemeTabFront, TTK_STATE_SELECTED, 0 },
    { kThemeTabNonFrontPressed, TTK_STATE_PRESSED, 0 },
    { kThemeTabNonFront, 0,0 }
};

/* Quoth DrawThemeTab() reference manual:
 * "Small tabs have a height of 16 pixels large tabs have a height of
 * 21 pixels. (The widths of tabs are variable.) Additionally, the
 * distance that the tab overlaps the pane must be included in the tab
 * rectangle this overlap distance is always 3 pixels, although the
 * 3-pixel overlap is only drawn for the front tab."
 */
static const int TAB_HEIGHT = 21;
static const int TAB_OVERLAP = 3;

static void TabElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    *heightPtr = TAB_HEIGHT + TAB_OVERLAP - 1;
}

static void TabElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(b);
    bounds.bottom += TAB_OVERLAP;
    BEGIN_DRAWING(d)
    DrawThemeTab(
    	&bounds, Ttk_StateTableLookup(TabStyleTable, state), kThemeTabNorth,
	0/*labelProc*/,0/*userData*/);
    END_DRAWING
}

static Ttk_ElementSpec TabElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    TabElementGeometry,
    TabElementDraw
};

/* Notebook panes:
 */
static void PaneElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    /* Padding determined by trial-and-error */
    *paddingPtr = Ttk_MakePadding(2,8,2,2);
}

static void PaneElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(b);
    BEGIN_DRAWING(d)
    DrawThemeTabPane(
    	&bounds, Ttk_StateTableLookup(ThemeStateTable, state));
    END_DRAWING
}

static Ttk_ElementSpec PaneElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    PaneElementGeometry,
    PaneElementDraw
};

/* Labelframe borders:
 * Use "primary group box ..."
 * Quoth DrawThemePrimaryGroup reference:
 * "The primary group box frame is drawn inside the specified
 * rectangle and is a maximum of 2 pixels thick."
 *
 * "Maximum of 2 pixels thick" is apparently a lie;
 * looks more like 4 to me with shading.
 */
static void GroupElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    *paddingPtr = Ttk_UniformPadding(4);
}

static void GroupElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(b);
    BEGIN_DRAWING(d)
    DrawThemePrimaryGroup(
    	&bounds, Ttk_StateTableLookup(ThemeStateTable, state));
    END_DRAWING
}

static Ttk_ElementSpec GroupElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    GroupElementGeometry,
    GroupElementDraw
};

/*----------------------------------------------------------------------
 * +++ Entry element -- 
 * 	3 pixels padding for focus rectangle
 *	2 pixels padding for EditTextFrame 
 */

typedef struct {
    Tcl_Obj	*backgroundObj;
} EntryElement;

static Ttk_ElementOptionSpec EntryElementOptions[] = {
    { "-background", TK_OPTION_BORDER,
	    Tk_Offset(EntryElement,backgroundObj), "white" },
    {0}
};

static void EntryElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    *paddingPtr = Ttk_UniformPadding(5);
}

static void EntryElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    EntryElement *e = elementRecord;
    Tk_3DBorder backgroundPtr = Tk_Get3DBorderFromObj(tkwin,e->backgroundObj);
    Ttk_Box inner = Ttk_PadBox(b, Ttk_UniformPadding(3));
    Rect bounds = BoxToRect(inner);

    BEGIN_DRAWING(d)

    /* Erase w/background color:
    */
    XFillRectangle(Tk_Display(tkwin), d,
	Tk_3DBorderGC(tkwin, backgroundPtr, TK_3D_FLAT_GC),
	inner.x,inner.y, inner.width, inner.height);

    /* Draw border:
    */
    DrawThemeEditTextFrame(
    	&bounds, Ttk_StateTableLookup(ThemeStateTable, state));

    /* Draw focus highlight:
    */
    if (state & TTK_STATE_FOCUS)
	DrawThemeFocusRect(&bounds, 1);

    END_DRAWING
}

static Ttk_ElementSpec EntryElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(EntryElement),
    EntryElementOptions,
    EntryElementGeometry,
    EntryElementDraw
};

/*----------------------------------------------------------------------
 * +++ Pop-up arrow (for comboboxes)
 * NOTE: This isn't right at all, but I can't find the correct
 * function in the Appearance Manager reference.
 */

static void PopupArrowElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin, 
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    *widthPtr = 12;	/* wild-assed guess */
    *heightPtr = 12;	/* wild-assed guess */
}

static void PopupArrowElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin, 
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(b);

    ThemeButtonParms *parms = clientData;
    ThemeButtonDrawInfo info = computeButtonDrawInfo(parms, state);

    bounds.left -= 6;
    bounds.top -= 3;
    bounds.right -= 6;
    bounds.bottom -= 2;

    BEGIN_DRAWING(d)
    DrawThemeButton(&bounds, kThemeArrowButton, &info,
	NULL/*prevInfo*/,NULL/*eraseProc*/,NULL/*labelProc*/,0/*userData*/);

    bounds = BoxToRect(Ttk_PadBox(b, ButtonMargins));
    bounds.top += 2;
    bounds.bottom += 2;
    bounds.left -= 2;
    bounds.right -= 2;

    DrawThemePopupArrow(&bounds,
    	kThemeArrowDown, 
	kThemeArrow9pt,		/* ??? */
	Ttk_StateTableLookup(ThemeStateTable, state),
	NULL /*eraseProc*/,0/*eraseData*/);
    END_DRAWING
}

static Ttk_ElementSpec PopupArrowElementSpec = 
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    PopupArrowElementGeometry,
    PopupArrowElementDraw
};

/*----------------------------------------------------------------------
 * +++ DrawThemeTrack-based elements --
 * Progress bars and scales.  (See also: <<NOTE-TRACKS>>)
 */

static Ttk_StateTable ThemeTrackEnableTable[] = {
    { kThemeTrackDisabled, TTK_STATE_DISABLED, 0 },
    { kThemeTrackInactive, TTK_STATE_BACKGROUND, 0 },
    { kThemeTrackActive, 0, 0 }
    /* { kThemeTrackNothingToScroll, ?, ? }, */
};

typedef struct {	/* TrackElement client data */
    ThemeTrackKind	kind;
    SInt32		thicknessMetric;
} TrackElementData;

static TrackElementData ScaleData =
    { kThemeSlider, kThemeMetricHSliderHeight };

typedef struct {
    Tcl_Obj *fromObj;		/* minimum value */
    Tcl_Obj *toObj;		/* maximum value */
    Tcl_Obj *valueObj;		/* current value */
    Tcl_Obj *orientObj; 	/* horizontal / vertical */
} TrackElement;

static Ttk_ElementOptionSpec TrackElementOptions[] = {
    { "-from", TK_OPTION_DOUBLE, Tk_Offset(TrackElement,fromObj) },
    { "-to", TK_OPTION_DOUBLE, Tk_Offset(TrackElement,toObj) },
    { "-value", TK_OPTION_DOUBLE, Tk_Offset(TrackElement,valueObj) },
    { "-orient", TK_OPTION_STRING, Tk_Offset(TrackElement,orientObj) },
    {0,0,0}
};

static void TrackElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    TrackElementData *data = clientData;
    SInt32 size = 24;	/* reasonable default ... */
    GetThemeMetric(data->thicknessMetric, &size);
    *widthPtr = *heightPtr = size;
}

static void TrackElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    TrackElementData *data = clientData;
    TrackElement *elem = elementRecord;
    double from = 0, to = 100, value = 0;
    int orientation = TTK_ORIENT_HORIZONTAL;
    ThemeTrackDrawInfo drawInfo;

    Tcl_GetDoubleFromObj(NULL, elem->fromObj, &from);
    Tcl_GetDoubleFromObj(NULL, elem->toObj, &to);
    Tcl_GetDoubleFromObj(NULL, elem->valueObj, &value);
    Ttk_GetOrientFromObj(NULL, elem->orientObj, &orientation);

    /* @@@ BUG: min, max, and value should account for resolution:
     * @@@ if finer than 1.0, conversion to int breaks.
     */
    drawInfo.kind = data->kind;
    drawInfo.bounds = BoxToRect(b);
    drawInfo.min = (int)from;		/* @@@ */
    drawInfo.max = (int)to;		/* @@@ */
    drawInfo.value = (int)value;	/* @@@ */

    drawInfo.attributes = orientation == TTK_ORIENT_HORIZONTAL
	? kThemeTrackHorizontal : 0;
    drawInfo.attributes |= kThemeTrackShowThumb;
    drawInfo.enableState = Ttk_StateTableLookup(ThemeTrackEnableTable, state);

    switch (data->kind) {
    case kThemeProgressBar:
	drawInfo.trackInfo.progress.phase = 0;	/* 1-4: animation phase */
	break;
    case kThemeSlider:
    	drawInfo.trackInfo.slider.pressState = 0; /* @@@ fill this in */
    	drawInfo.trackInfo.slider.thumbDir =  kThemeThumbPlain;
	    /* kThemeThumbUpward, kThemeThumbDownward, kThemeThumbPlain  */
	break;
    }

    BEGIN_DRAWING(d)
    DrawThemeTrack(&drawInfo,
    	NULL/*rgnGhost*/,NULL/*eraseProc*/,0/*eraseData*/);
    END_DRAWING
}

static Ttk_ElementSpec TrackElementSpec = {
    TK_STYLE_VERSION_2,
    sizeof(TrackElement),
    TrackElementOptions,
    TrackElementGeometry,
    TrackElementDraw
};


/* Slider element -- <<NOTE-TRACKS>>
 * Has geometry only.  The Scale widget adjusts the position of this element,
 * and uses it for hit detection.  In the Aqua theme, the slider is actually
 * drawn as part of the trough element.
 *
 * Also buggy: The geometry here is a Wild-Assed-Guess; I can't
 * figure out how to get the Appearance Manager to tell me the
 * slider size.
 */
static void SliderElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    *widthPtr = *heightPtr = 24;
}

static Ttk_ElementSpec SliderElementSpec = {
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    SliderElementGeometry,
    TtkNullElementDraw
};

/*----------------------------------------------------------------------
 * +++ Progress bar element (new):
 * 
 * @@@ NOTE: According to an older revision of the Aqua reference docs,
 * @@@ the 'phase' field is between 0 and 4.  Newer revisions say
 * @@@ that it can be any UInt8 value.
 */

typedef struct {
    Tcl_Obj *orientObj; 	/* horizontal / vertical */
    Tcl_Obj *valueObj;		/* current value */
    Tcl_Obj *maximumObj;	/* maximum value */
    Tcl_Obj *phaseObj;		/* animation phase */
    Tcl_Obj *modeObj;		/* progress bar mode */
} PbarElement;

static Ttk_ElementOptionSpec PbarElementOptions[] = {
    { "-orient", TK_OPTION_STRING, 
	Tk_Offset(PbarElement,orientObj), "horizontal" },
    { "-value", TK_OPTION_DOUBLE,
    	Tk_Offset(PbarElement,valueObj), "0" },
    { "-maximum", TK_OPTION_DOUBLE,
    	Tk_Offset(PbarElement,maximumObj), "100" },
    { "-phase", TK_OPTION_INT,
    	Tk_Offset(PbarElement,phaseObj), "0" },
    { "-mode", TK_OPTION_STRING, 
    	Tk_Offset(PbarElement,modeObj), "determinate" },
    {0,0,0,0}
};

static void PbarElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    SInt32 size = 24;	/* @@@ Check HIG for correct default */
    GetThemeMetric(kThemeMetricLargeProgressBarThickness, &size);
    *widthPtr = *heightPtr = size;
}

static void PbarElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    PbarElement *pbar = elementRecord;
    int orientation = TTK_ORIENT_HORIZONTAL;
    double value = 0, maximum = 100;
    int phase = 0;
    ThemeTrackDrawInfo drawInfo;

    Ttk_GetOrientFromObj(NULL, pbar->orientObj, &orientation);
    Tcl_GetDoubleFromObj(NULL, pbar->valueObj, &value);
    Tcl_GetDoubleFromObj(NULL, pbar->maximumObj, &maximum);
    Tcl_GetIntFromObj(NULL, pbar->phaseObj, &phase);

    if (!strcmp("indeterminate", Tcl_GetString(pbar->modeObj)) && value) {
	drawInfo.kind = kThemeIndeterminateBar;
    } else {
	drawInfo.kind = kThemeProgressBar;
    }
    drawInfo.bounds = BoxToRect(b);
    drawInfo.min = 0;
    drawInfo.max = (int)maximum;	/* @@@ See note above */
    drawInfo.value = (int)value;
    drawInfo.attributes = orientation == TTK_ORIENT_HORIZONTAL
	? kThemeTrackHorizontal : 0;
    drawInfo.attributes |= kThemeTrackShowThumb;
    drawInfo.enableState = Ttk_StateTableLookup(ThemeTrackEnableTable, state);
    drawInfo.trackInfo.progress.phase = phase;

    BEGIN_DRAWING(d)
    DrawThemeTrack(&drawInfo,
    	NULL/*rgnGhost*/,NULL/*eraseProc*/,0/*eraseData*/);
    END_DRAWING
}

static Ttk_ElementSpec PbarElementSpec = {
    TK_STYLE_VERSION_2,
    sizeof(PbarElement),
    PbarElementOptions,
    PbarElementGeometry,
    PbarElementDraw
};

/*----------------------------------------------------------------------
 * +++ Separator element.
 *
 * DrawThemeSeparator() guesses the orientation of the line from
 * the width and height of the rectangle, so the same element can
 * can be used for horizontal, vertical, and general separators.
 */

static void SeparatorElementSize(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    *widthPtr = *heightPtr = 2;
}

static void SeparatorElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, unsigned int state)
{
    Rect bounds = BoxToRect(b);

    /* DrawThemeSeparator only supports kThemeStateActive / kThemeStateInactive
    */
    state &= TTK_STATE_BACKGROUND;
    BEGIN_DRAWING(d)
    DrawThemeSeparator(&bounds, Ttk_StateTableLookup(ThemeStateTable, state));
    END_DRAWING
}

static Ttk_ElementSpec SeparatorElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    SeparatorElementSize,
    SeparatorElementDraw
};

/*----------------------------------------------------------------------
 * +++ Size grip element.
 */
static const ThemeGrowDirection sizegripGrowDirection 
	= kThemeGrowRight|kThemeGrowDown;

static void SizegripElementSize(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    Point origin;
    Rect bounds;

    origin.h = origin.v = 0;
    GetThemeStandaloneGrowBoxBounds(
    	origin, sizegripGrowDirection, false, &bounds);
    *widthPtr = bounds.right - bounds.left;
    *heightPtr = bounds.bottom - bounds.top;
}

static void SizegripElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, unsigned int state)
{
    Point origin;
    origin.h = b.x; origin.v = b.y;

    /* Grow box only supports kThemeStateActive, kThemeStateInactive */
    state &= TTK_STATE_BACKGROUND;

    BEGIN_DRAWING(d)
    DrawThemeStandaloneGrowBox(
    	origin, sizegripGrowDirection, false,
	Ttk_StateTableLookup(ThemeStateTable, state));
    END_DRAWING
}

static Ttk_ElementSpec SizegripElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    SizegripElementSize,
    SizegripElementDraw
};


/*----------------------------------------------------------------------
 * +++ Background element -- an experiment.
 *
 *	This isn't quite right: In Aqua, the correct background for
 *	a control depends on what kind of container it belongs to,
 *	and the type of the top-level window.
 *
 *	Also: patterned backgrounds should be aligned with the coordinate
 *	system of the top-level window.  Since we're drawing into an
 * 	off-screen graphics port with its own coordinate system,
 *	this leads to alignment glitches.
 *
 *	Available kTheme constants:
 *  	kThemeBackgroundTabPane,
 *  	kThemeBackgroundPlacard,
 *  	kThemeBackgroundWindowHeader,
 *  	kThemeBackgroundListViewWindowHeader,
 *  	kThemeBackgroundSecondaryGroupBox,
 *
 * 	GetThemeBrush() and SetThemeBackground() offer more choices.
 *
 */

static void BackgroundElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    ThemeBackgroundKind kind = kThemeBackgroundWindowHeader;
    Rect bounds;
    SInt32 depth = 32;	/* ??? */
    Boolean inColor = true;
    Point origin;

    /* Avoid kThemeStatePressed, which seems to give bad results
     * for ApplyThemeBackground:
     */
    state &= ~TTK_STATE_PRESSED;

    TkMacOSXWinBounds((TkWindow *) tkwin, &bounds);
    origin.v = -bounds.top;
    origin.h = -bounds.left;

    bounds.top = bounds.left = 0;
    bounds.right = Tk_Width(tkwin);
    bounds.bottom = Tk_Height(tkwin);

    BEGIN_DRAWING(d)
    ApplyThemeBackground(kind, &bounds,
    	Ttk_StateTableLookup(ThemeStateTable, state),
	depth, inColor);
    QDSetPatternOrigin(origin);
    EraseRect(&bounds);
    END_DRAWING
}

static Ttk_ElementSpec BackgroundElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    TtkNullElementGeometry,
    BackgroundElementDraw
};

/*----------------------------------------------------------------------
 * +++ ToolbarBackground element -- toolbar style for frames.
 *
 *      This is very similar to the normal background element, but uses a
 *      different ThemeBrush in order to get the lighter pinstripe effect
 *      used in toolbars. We use SetThemeBackground() rather than
 *      ApplyThemeBackground() in order to get the right style.
 *
 * <URL: http://developer.apple.com/documentation/Carbon/Reference/
 *      Appearance_Manager/appearance_manager/constant_7.html#/
 *      /apple_ref/doc/uid/TP30000243/C005321>
 *
 */
static void ToolbarBackgroundElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    ThemeBrush brush = kThemeBrushToolbarBackground;
    Rect bounds;
    SInt32 depth = 32;		/* ??? */
    Boolean inColor = true;

    bounds.top = bounds.left = 0;
    bounds.right = Tk_Width(tkwin);
    bounds.bottom = Tk_Height(tkwin);

    BEGIN_DRAWING(d)
    SetThemeBackground(brush,
	depth, inColor);
    EraseRect(&bounds);
    END_DRAWING
}

static Ttk_ElementSpec ToolbarBackgroundElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    TtkNullElementGeometry,
    ToolbarBackgroundElementDraw
};

/*----------------------------------------------------------------------
 * +++ Treeview header
 *	Redefine the header to use a kThemeListHeaderButton.
 */

static Ttk_StateTable TreeHeaderAdornmentTable[] = {
    { kThemeAdornmentHeaderButtonSortUp, TTK_STATE_ALTERNATE, 0 },
    { kThemeAdornmentFocus, TTK_STATE_FOCUS, 0 },
    { kThemeAdornmentNone, 0, 0 }
};

static void TreeHeaderElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(b);
    ThemeButtonParms *parms = clientData;
    ThemeButtonDrawInfo info;

    info.state = Ttk_StateTableLookup(ThemeStateTable, state);
    info.value = Ttk_StateTableLookup(ButtonValueTable, state);
    info.adornment = Ttk_StateTableLookup(TreeHeaderAdornmentTable, state);

    BEGIN_DRAWING(d)
    DrawThemeButton(&bounds, parms->kind, &info,
	NULL/*prevInfo*/,NULL/*eraseProc*/,NULL/*labelProc*/,0/*userData*/);
    END_DRAWING
}

static Ttk_ElementSpec TreeHeaderElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    ButtonElementGeometryNoPadding,
    TreeHeaderElementDraw
};

/* Disclosure triangle:
 */
#define TTK_TREEVIEW_STATE_OPEN TTK_STATE_USER1
#define TTK_TREEVIEW_STATE_LEAF TTK_STATE_USER2
static Ttk_StateTable DisclosureValueTable[] = {
    { kThemeDisclosureDown, TTK_TREEVIEW_STATE_OPEN, 0 },
    { kThemeDisclosureRight, 0, 0 },
};

static void DisclosureElementSize(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    SInt32 s;
    GetThemeMetric(kThemeMetricDisclosureTriangleWidth, &s); *widthPtr = s;
    GetThemeMetric(kThemeMetricDisclosureTriangleHeight, &s); *heightPtr = s;
}

static void DisclosureElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, Ttk_State state)
{
    Rect bounds = BoxToRect(b);
    ThemeButtonDrawInfo info;

    if (state & TTK_TREEVIEW_STATE_LEAF)
	return;

    info.state = Ttk_StateTableLookup(ThemeStateTable, state);
    info.value = Ttk_StateTableLookup(DisclosureValueTable, state);
    info.adornment = kThemeAdornmentDrawIndicatorOnly;

    BEGIN_DRAWING(d)
    DrawThemeButton(&bounds, kThemeDisclosureTriangle, &info,
	NULL/*prevInfo*/,DontErase,NULL/*labelProc*/,0/*userData*/);
    END_DRAWING
}

static Ttk_ElementSpec DisclosureElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    TtkNullElementOptions,
    DisclosureElementSize,
    DisclosureElementDraw
};

/*----------------------------------------------------------------------
 * +++ Widget layouts.
 */
TTK_BEGIN_LAYOUT(ToolbarLayout)
    TTK_NODE("Toolbar.background", TTK_FILL_BOTH)
TTK_END_LAYOUT

TTK_BEGIN_LAYOUT(ButtonLayout)
    TTK_GROUP("Button.button", TTK_FILL_BOTH,
	TTK_GROUP("Button.padding", TTK_FILL_BOTH,
	    TTK_NODE("Button.label", TTK_FILL_BOTH)))
TTK_END_LAYOUT

TTK_BEGIN_LAYOUT(RadiobuttonLayout)
    TTK_GROUP("Radiobutton.button", TTK_FILL_BOTH,
	TTK_GROUP("Radiobutton.padding", TTK_FILL_BOTH,
	    TTK_NODE("Radiobutton.label", TTK_PACK_LEFT)))
TTK_END_LAYOUT

TTK_BEGIN_LAYOUT(CheckbuttonLayout)
    TTK_GROUP("Checkbutton.button", TTK_FILL_BOTH,
	TTK_GROUP("Checkbutton.padding", TTK_FILL_BOTH,
	    TTK_NODE("Checkbutton.label", TTK_PACK_LEFT)))
TTK_END_LAYOUT

TTK_BEGIN_LAYOUT(MenubuttonLayout)
    TTK_GROUP("Menubutton.button", TTK_FILL_BOTH,
	TTK_GROUP("Menubutton.padding", TTK_FILL_BOTH,
	    TTK_NODE("Menubutton.label", TTK_PACK_LEFT)))
TTK_END_LAYOUT

/* Notebook tabs -- no focus ring */
TTK_BEGIN_LAYOUT(TabLayout)
    TTK_GROUP("Notebook.tab", TTK_FILL_BOTH,
	TTK_GROUP("Notebook.padding", TTK_EXPAND|TTK_FILL_BOTH,
	    TTK_NODE("Notebook.label", TTK_EXPAND|TTK_FILL_BOTH)))
TTK_END_LAYOUT

/* Progress bars -- track only */
TTK_BEGIN_LAYOUT(ProgressbarLayout)
    TTK_NODE("Progressbar.track", TTK_EXPAND|TTK_FILL_BOTH)
TTK_END_LAYOUT

/* Tree heading -- no border, fixed height */
TTK_BEGIN_LAYOUT(TreeHeadingLayout)
    TTK_NODE("Treeheading.cell", TTK_FILL_X)
    TTK_NODE("Treeheading.image", TTK_PACK_RIGHT)
    TTK_NODE("Treeheading.text", 0)
TTK_END_LAYOUT

/*----------------------------------------------------------------------
 * +++ Initialization.
 */

static int AquaTheme_Init(Tcl_Interp *interp)
{
    Ttk_Theme themePtr = Ttk_CreateTheme(interp, "aqua", NULL);

    if (!themePtr) {
	return TCL_ERROR;
    }

    /* Elements:
     */
    Ttk_RegisterElementSpec(themePtr,"background",&BackgroundElementSpec,0);
    Ttk_RegisterElementSpec(themePtr,"Toolbar.background",
	&ToolbarBackgroundElementSpec, 0);

    Ttk_RegisterElementSpec(themePtr, "Button.button",
    	&ButtonElementSpec, &PushButtonParms);
    Ttk_RegisterElementSpec(themePtr, "Checkbutton.button",
    	&ButtonElementSpec, &CheckBoxParms);
    Ttk_RegisterElementSpec(themePtr, "Radiobutton.button",
    	&ButtonElementSpec, &RadioButtonParms);
    Ttk_RegisterElementSpec(themePtr, "Toolbutton.border",
    	&ButtonElementSpec, &BevelButtonParms);
    Ttk_RegisterElementSpec(themePtr, "Menubutton.button",
    	&ButtonElementSpec, &PopupButtonParms);
    Ttk_RegisterElementSpec(themePtr, "Treeitem.indicator",
    	&DisclosureElementSpec, &DisclosureParms);
    Ttk_RegisterElementSpec(themePtr, "Treeheading.cell",
	&TreeHeaderElementSpec, &ListHeaderParms); 

    Ttk_RegisterElementSpec(themePtr, "Notebook.tab", &TabElementSpec, 0);
    Ttk_RegisterElementSpec(themePtr, "Notebook.client", &PaneElementSpec, 0);

    Ttk_RegisterElementSpec(themePtr, "Labelframe.border",&GroupElementSpec,0);
    Ttk_RegisterElementSpec(themePtr, "Entry.field",&EntryElementSpec,0);

    Ttk_RegisterElementSpec(themePtr, "Combobox.field",&EntryElementSpec,0);
    Ttk_RegisterElementSpec(themePtr, "Combobox.downarrow",
    	&PopupArrowElementSpec, 0);

    Ttk_RegisterElementSpec(themePtr, "separator",&SeparatorElementSpec,0);
    Ttk_RegisterElementSpec(themePtr, "hseparator",&SeparatorElementSpec,0);
    Ttk_RegisterElementSpec(themePtr, "vseparator",&SeparatorElementSpec,0);

    Ttk_RegisterElementSpec(themePtr, "sizegrip",&SizegripElementSpec,0);

    /* <<NOTE-TRACKS>>
     * The Progressbar widget adjusts the size of the pbar element.
     * In the Aqua theme, the appearance manager computes the bar geometry;
     * we do all the drawing in the ".track" element and leave the .pbar out.
     */
    Ttk_RegisterElementSpec(themePtr,"Scale.trough",
    	&TrackElementSpec, &ScaleData);
    Ttk_RegisterElementSpec(themePtr,"Scale.slider",&SliderElementSpec,0);
    Ttk_RegisterElementSpec(themePtr,"Progressbar.track", &PbarElementSpec, 0);

    /* Layouts:
     */
    Ttk_RegisterLayout(themePtr, "Toolbar", ToolbarLayout);
    Ttk_RegisterLayout(themePtr, "TButton", ButtonLayout);
    Ttk_RegisterLayout(themePtr, "TCheckbutton", CheckbuttonLayout);
    Ttk_RegisterLayout(themePtr, "TRadiobutton", RadiobuttonLayout);
    Ttk_RegisterLayout(themePtr, "TMenubutton", MenubuttonLayout);
    Ttk_RegisterLayout(themePtr, "TProgressbar", ProgressbarLayout);
    Ttk_RegisterLayout(themePtr, "TNotebook.Tab", TabLayout);
    Ttk_RegisterLayout(themePtr, "Heading", TreeHeadingLayout);

    Tcl_PkgProvide(interp, "ttk::theme::aqua", TTK_VERSION);
    return TCL_OK;
}

MODULE_SCOPE int Ttk_MacOSXPlatformInit(Tcl_Interp *interp)
{
    return AquaTheme_Init(interp);
}