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
|
/* $Id: ttkFrame.c,v 1.6 2007/06/09 21:45:44 jenglish Exp $
* Copyright (c) 2004, Joe English
*
* ttk::frame and ttk::labelframe widgets.
*/
#include <tk.h>
#include "ttkTheme.h"
#include "ttkWidget.h"
#include "ttkManager.h"
/* ======================================================================
* +++ Frame widget:
*/
typedef struct
{
Tcl_Obj *borderWidthObj;
Tcl_Obj *paddingObj;
Tcl_Obj *reliefObj;
Tcl_Obj *widthObj;
Tcl_Obj *heightObj;
} FramePart;
typedef struct
{
WidgetCore core;
FramePart frame;
} Frame;
static Tk_OptionSpec FrameOptionSpecs[] =
{
{TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth", NULL,
Tk_Offset(Frame,frame.borderWidthObj), -1,
TK_OPTION_NULL_OK,0,GEOMETRY_CHANGED },
{TK_OPTION_STRING, "-padding", "padding", "Pad", NULL,
Tk_Offset(Frame,frame.paddingObj), -1,
TK_OPTION_NULL_OK,0,GEOMETRY_CHANGED },
{TK_OPTION_RELIEF, "-relief", "relief", "Relief", NULL,
Tk_Offset(Frame,frame.reliefObj), -1,
TK_OPTION_NULL_OK,0,0 },
{TK_OPTION_PIXELS, "-width", "width", "Width", "0",
Tk_Offset(Frame,frame.widthObj), -1,
0,0,GEOMETRY_CHANGED },
{TK_OPTION_PIXELS, "-height", "height", "Height", "0",
Tk_Offset(Frame,frame.heightObj), -1,
0,0,GEOMETRY_CHANGED },
WIDGET_INHERIT_OPTIONS(ttkCoreOptionSpecs)
};
static WidgetCommandSpec FrameCommands[] =
{
{ "configure", TtkWidgetConfigureCommand },
{ "cget", TtkWidgetCgetCommand },
{ "instate", TtkWidgetInstateCommand },
{ "state", TtkWidgetStateCommand },
{ "identify", TtkWidgetIdentifyCommand },
{ NULL, NULL }
};
/*
* FrameMargins --
* Compute internal margins for a frame widget.
* This includes the -borderWidth, plus any additional -padding.
*/
static Ttk_Padding FrameMargins(Frame *framePtr)
{
Ttk_Padding margins = Ttk_UniformPadding(0);
/* Check -padding:
*/
if (framePtr->frame.paddingObj) {
Ttk_GetPaddingFromObj(NULL,
framePtr->core.tkwin, framePtr->frame.paddingObj, &margins);
}
/* Add padding for border:
*/
if (framePtr->frame.borderWidthObj) {
int border = 0;
Tk_GetPixelsFromObj(NULL,
framePtr->core.tkwin, framePtr->frame.borderWidthObj, &border);
margins = Ttk_AddPadding(margins, Ttk_UniformPadding((short)border));
}
return margins;
}
/* FrameSize procedure --
* The frame doesn't request a size of its own by default,
* but it does have an internal border. See also <<NOTE-SIZE>>
*/
static int FrameSize(void *recordPtr, int *widthPtr, int *heightPtr)
{
Frame *framePtr = recordPtr;
Ttk_SetMargins(framePtr->core.tkwin, FrameMargins(framePtr));
return 0;
}
/*
* FrameConfigure -- configure hook.
* <<NOTE-SIZE>> Usually the size of a frame is controlled by
* a geometry manager (pack, grid); the -width and -height
* options are only effective if geometry propagation is turned
* off or if the [place] GM is used for child widgets.
*
* To avoid geometry blinking, we issue a geometry request
* in the Configure hook instead of the Size hook, and only
* if -width and/or -height is nonzero and one of them
* or the other size-related options (-borderwidth, -padding)
* has been changed.
*/
static int FrameConfigure(Tcl_Interp *interp, void *recordPtr, int mask)
{
Frame *framePtr = recordPtr;
int width, height;
/*
* Make sure -padding resource, if present, is correct:
*/
if (framePtr->frame.paddingObj) {
Ttk_Padding unused;
if (Ttk_GetPaddingFromObj(interp,
framePtr->core.tkwin,
framePtr->frame.paddingObj,
&unused) != TCL_OK) {
return TCL_ERROR;
}
}
/* See <<NOTE-SIZE>>
*/
if ( TCL_OK != Tk_GetPixelsFromObj(
interp,framePtr->core.tkwin,framePtr->frame.widthObj,&width)
|| TCL_OK != Tk_GetPixelsFromObj(
interp,framePtr->core.tkwin,framePtr->frame.heightObj,&height)
)
{
return TCL_ERROR;
}
if ((width > 0 || height > 0) && (mask & GEOMETRY_CHANGED)) {
Tk_GeometryRequest(framePtr->core.tkwin, width, height);
}
return TtkCoreConfigure(interp, recordPtr, mask);
}
static WidgetSpec FrameWidgetSpec =
{
"TFrame", /* className */
sizeof(Frame), /* recordSize */
FrameOptionSpecs, /* optionSpecs */
FrameCommands, /* subcommands */
TtkNullInitialize, /* initializeProc */
TtkNullCleanup, /* cleanupProc */
FrameConfigure, /* configureProc */
TtkNullPostConfigure, /* postConfigureProc */
TtkWidgetGetLayout, /* getLayoutProc */
FrameSize, /* sizeProc */
TtkWidgetDoLayout, /* layoutProc */
TtkWidgetDisplay /* displayProc */
};
/* ======================================================================
* +++ Labelframe widget:
*/
#define DEFAULT_LABELINSET 8
#define DEFAULT_BORDERWIDTH 2
int TtkGetLabelAnchorFromObj(
Tcl_Interp *interp, Tcl_Obj *objPtr, Ttk_PositionSpec *anchorPtr)
{
const char *string = Tcl_GetString(objPtr);
char c = *string++;
Ttk_PositionSpec flags = 0;
/* First character determines side:
*/
switch (c) {
case 'w' : flags = TTK_PACK_LEFT; break;
case 'e' : flags = TTK_PACK_RIGHT; break;
case 'n' : flags = TTK_PACK_TOP; break;
case 's' : flags = TTK_PACK_BOTTOM; break;
default : goto error;
}
/* Remaining characters are as per -sticky:
*/
while ((c = *string++) != '\0') {
switch (c) {
case 'w' : flags |= TTK_STICK_W; break;
case 'e' : flags |= TTK_STICK_E; break;
case 'n' : flags |= TTK_STICK_N; break;
case 's' : flags |= TTK_STICK_S; break;
default : goto error;
}
}
*anchorPtr = flags;
return TCL_OK;
error:
if (interp) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp,
"Bad label anchor specification ", Tcl_GetString(objPtr),
NULL);
}
return TCL_ERROR;
}
/* LabelAnchorSide --
* Returns the side corresponding to a LabelAnchor value.
*/
static Ttk_Side LabelAnchorSide(Ttk_PositionSpec flags)
{
if (flags & TTK_PACK_LEFT) return TTK_SIDE_LEFT;
else if (flags & TTK_PACK_RIGHT) return TTK_SIDE_RIGHT;
else if (flags & TTK_PACK_TOP) return TTK_SIDE_TOP;
else if (flags & TTK_PACK_BOTTOM) return TTK_SIDE_BOTTOM;
/*NOTREACHED*/
return TTK_SIDE_TOP;
}
/*
* Labelframe widget record:
*/
typedef struct {
Tcl_Obj *labelAnchorObj;
Tcl_Obj *textObj;
Tcl_Obj *underlineObj;
Tk_Window labelWidget;
Ttk_Manager *mgr;
Ttk_Box labelParcel; /* Set in layoutProc */
} LabelframePart;
typedef struct
{
WidgetCore core;
FramePart frame;
LabelframePart label;
} Labelframe;
#define LABELWIDGET_CHANGED 0x100
static Tk_OptionSpec LabelframeOptionSpecs[] =
{
{TK_OPTION_STRING, "-labelanchor", "labelAnchor", "LabelAnchor",
"nw", Tk_Offset(Labelframe, label.labelAnchorObj),-1,
0,0,GEOMETRY_CHANGED},
{TK_OPTION_STRING, "-text", "text", "Text", "",
Tk_Offset(Labelframe,label.textObj), -1,
0,0,GEOMETRY_CHANGED },
{TK_OPTION_INT, "-underline", "underline", "Underline",
"-1", Tk_Offset(Labelframe,label.underlineObj), -1,
0,0,0 },
{TK_OPTION_WINDOW, "-labelwidget", "labelWidget", "LabelWidget", NULL,
-1, Tk_Offset(Labelframe,label.labelWidget),
TK_OPTION_NULL_OK,0,LABELWIDGET_CHANGED|GEOMETRY_CHANGED },
WIDGET_INHERIT_OPTIONS(FrameOptionSpecs)
};
/*
* Labelframe style parameters:
*/
typedef struct
{
int borderWidth; /* border width */
Ttk_Padding padding; /* internal padding */
Ttk_PositionSpec labelAnchor; /* corner/side to place label */
Ttk_Padding labelMargins; /* extra space around label */
int labelOutside; /* true=>place label outside border */
} LabelframeStyle;
static void LabelframeStyleOptions(Labelframe *lf, LabelframeStyle *style)
{
Ttk_Layout layout = lf->core.layout;
Tcl_Obj *objPtr;
style->borderWidth = DEFAULT_BORDERWIDTH;
style->padding = Ttk_UniformPadding(0);
style->labelAnchor = TTK_PACK_TOP | TTK_STICK_W;
style->labelMargins =
Ttk_MakePadding(DEFAULT_LABELINSET,0,DEFAULT_LABELINSET,0);
style->labelOutside = 0;
if ((objPtr = Ttk_QueryOption(layout, "-borderwidth", 0)) != NULL) {
Tk_GetPixelsFromObj(NULL, lf->core.tkwin, objPtr, &style->borderWidth);
}
if ((objPtr = Ttk_QueryOption(layout, "-padding", 0)) != NULL) {
Ttk_GetPaddingFromObj(NULL, lf->core.tkwin, objPtr, &style->padding);
}
if ((objPtr = Ttk_QueryOption(layout,"-labelanchor", 0)) != NULL) {
TtkGetLabelAnchorFromObj(NULL, objPtr, &style->labelAnchor);
}
if ((objPtr = Ttk_QueryOption(layout,"-labelmargins", 0)) != NULL) {
Ttk_GetBorderFromObj(NULL, objPtr, &style->labelMargins);
}
if ((objPtr = Ttk_QueryOption(layout,"-labeloutside", 0)) != NULL) {
Tcl_GetBooleanFromObj(NULL, objPtr, &style->labelOutside);
}
return;
}
/* LabelframeLabelSize --
* Extract the requested width and height of the labelframe's label:
* taken from the label widget if specified, otherwise the text label.
*/
static void
LabelframeLabelSize(Labelframe *lframePtr, int *widthPtr, int *heightPtr)
{
WidgetCore *corePtr = &lframePtr->core;
Tk_Window labelWidget = lframePtr->label.labelWidget;
Ttk_LayoutNode *textNode = Ttk_LayoutFindNode(corePtr->layout, "text");
if (labelWidget) {
*widthPtr = Tk_ReqWidth(labelWidget);
*heightPtr = Tk_ReqHeight(labelWidget);
} else if (textNode) {
Ttk_LayoutNodeReqSize(corePtr->layout, textNode, widthPtr, heightPtr);
} else {
*widthPtr = *heightPtr = 0;
}
}
/*
* LabelframeSize --
* Like the frame, this doesn't request a size of its own
* but it does have internal padding and a minimum size.
*/
static int LabelframeSize(void *recordPtr, int *widthPtr, int *heightPtr)
{
Labelframe *lframePtr = recordPtr;
WidgetCore *corePtr = &lframePtr->core;
Ttk_Padding margins;
LabelframeStyle style;
int labelWidth, labelHeight;
LabelframeStyleOptions(lframePtr, &style);
/* Compute base margins (See also: FrameMargins)
*/
margins = Ttk_AddPadding(
style.padding, Ttk_UniformPadding((short)style.borderWidth));
/* Adjust margins based on label size and position:
*/
LabelframeLabelSize(lframePtr, &labelWidth, &labelHeight);
labelWidth += Ttk_PaddingWidth(style.labelMargins);
labelHeight += Ttk_PaddingHeight(style.labelMargins);
switch (LabelAnchorSide(style.labelAnchor)) {
case TTK_SIDE_LEFT: margins.left += labelWidth; break;
case TTK_SIDE_RIGHT: margins.right += labelWidth; break;
case TTK_SIDE_TOP: margins.top += labelHeight; break;
case TTK_SIDE_BOTTOM: margins.bottom += labelHeight; break;
}
Ttk_SetMargins(corePtr->tkwin,margins);
/* Request minimum size based on border width and label size:
*/
Tk_SetMinimumRequestSize(corePtr->tkwin,
labelWidth + 2*style.borderWidth,
labelHeight + 2*style.borderWidth);
return 0;
}
/*
* LabelframeDoLayout --
* Labelframe layout hook.
*
* Side effects: Computes labelParcel.
*/
static void LabelframeDoLayout(void *recordPtr)
{
Labelframe *lframePtr = recordPtr;
WidgetCore *corePtr = &lframePtr->core;
Ttk_Box borderParcel = Ttk_WinBox(corePtr->tkwin);
Ttk_LayoutNode
*textNode = Ttk_LayoutFindNode(corePtr->layout, "text"),
*borderNode = Ttk_LayoutFindNode(corePtr->layout, "border");
int lw, lh; /* Label width and height */
LabelframeStyle style;
Ttk_Box labelParcel;
/*
* Do base layout:
*/
Ttk_PlaceLayout(corePtr->layout,corePtr->state,borderParcel);
/*
* Compute label parcel:
*/
LabelframeStyleOptions(lframePtr, &style);
LabelframeLabelSize(lframePtr, &lw, &lh);
lw += Ttk_PaddingWidth(style.labelMargins);
lh += Ttk_PaddingHeight(style.labelMargins);
labelParcel = Ttk_PadBox(
Ttk_PositionBox(&borderParcel, lw, lh, style.labelAnchor),
style.labelMargins);
if (!style.labelOutside) {
/* Move border edge so it's over label:
*/
switch (LabelAnchorSide(style.labelAnchor)) {
case TTK_SIDE_LEFT: borderParcel.x -= lw / 2;
case TTK_SIDE_RIGHT: borderParcel.width += lw/2; break;
case TTK_SIDE_TOP: borderParcel.y -= lh / 2;
case TTK_SIDE_BOTTOM: borderParcel.height += lh / 2; break;
}
}
/*
* Place border and label:
*/
if (borderNode) {
Ttk_PlaceLayoutNode(corePtr->layout, borderNode, borderParcel);
}
if (textNode) {
Ttk_PlaceLayoutNode(corePtr->layout, textNode, labelParcel);
}
/* labelWidget placed in LabelframePlaceSlaves GM hook */
lframePtr->label.labelParcel = labelParcel;
}
/* LabelframePlaceSlaves --
* Sets the position and size of the labelwidget.
*/
static void LabelframePlaceSlaves(void *recordPtr)
{
Labelframe *lframe = recordPtr;
if (Ttk_NumberSlaves(lframe->label.mgr) == 1) {
Ttk_Box b;
LabelframeDoLayout(recordPtr);
b = lframe->label.labelParcel;
/* ASSERT: slave #0 is lframe->label.labelWidget */
Ttk_PlaceSlave(lframe->label.mgr, 0, b.x,b.y,b.width,b.height);
}
}
/* Labelframe geometry manager:
*/
/* LabelRemoved --
* Unset the -labelwidget option.
*
* <<NOTE-LABELREMOVED>>:
* This routine is also called when the widget voluntarily forgets
* the slave in LabelframeConfigure.
*/
static void LabelRemoved(Ttk_Manager *mgr, int slaveIndex)
{
Labelframe *lframe = Ttk_ManagerData(mgr);
lframe->label.labelWidget = 0;
}
static Ttk_ManagerSpec LabelframeManagerSpec =
{
{ "labelframe", Ttk_GeometryRequestProc, Ttk_LostSlaveProc },
LabelframeSize,
LabelframePlaceSlaves,
LabelRemoved
};
/* LabelframeInitialize --
* Initialization hook.
*/
static int LabelframeInitialize(Tcl_Interp *interp, void *recordPtr)
{
Labelframe *lframe = recordPtr;
lframe->label.mgr = Ttk_CreateManager(
&LabelframeManagerSpec, lframe, lframe->core.tkwin);
lframe->label.labelWidget = 0;
lframe->label.labelParcel = Ttk_MakeBox(-1,-1,-1,-1);
return TCL_OK;
}
/* LabelframeCleanup --
* Cleanup hook.
*/
static void LabelframeCleanup(void *recordPtr)
{
Labelframe *lframe = recordPtr;
Ttk_DeleteManager(lframe->label.mgr);
}
/* RaiseLabelWidget --
* Raise the -labelwidget to ensure that the labelframe doesn't
* obscure it (if it's not a direct child), or bring it to
* the top of the stacking order (if it is).
*/
static void RaiseLabelWidget(Labelframe *lframe)
{
Tk_Window parent = Tk_Parent(lframe->label.labelWidget);
Tk_Window sibling = NULL;
Tk_Window w = lframe->core.tkwin;
while (w && w != parent) {
sibling = w;
w = Tk_Parent(w);
}
Tk_RestackWindow(lframe->label.labelWidget, Above, sibling);
}
/* LabelframeConfigure --
* Configuration hook.
*/
static int LabelframeConfigure(Tcl_Interp *interp,void *recordPtr,int mask)
{
Labelframe *lframePtr = recordPtr;
Tk_Window labelWidget = lframePtr->label.labelWidget;
Ttk_PositionSpec unused;
/* Validate options:
*/
if (mask & LABELWIDGET_CHANGED && labelWidget != NULL) {
if (!Ttk_Maintainable(interp, labelWidget, lframePtr->core.tkwin)) {
return TCL_ERROR;
}
}
if (TtkGetLabelAnchorFromObj(
interp, lframePtr->label.labelAnchorObj, &unused) != TCL_OK)
{
return TCL_ERROR;
}
/* Base class configuration:
*/
if (FrameConfigure(interp, recordPtr, mask) != TCL_OK) {
return TCL_ERROR;
}
/* Update -labelwidget changes, if any:
*/
if (mask & LABELWIDGET_CHANGED) {
if (Ttk_NumberSlaves(lframePtr->label.mgr) == 1) {
Ttk_ForgetSlave(lframePtr->label.mgr, 0);
/* Restore labelWidget field (see <<NOTE-LABELREMOVED>>)
*/
lframePtr->label.labelWidget = labelWidget;
}
if (labelWidget) {
Ttk_InsertSlave(lframePtr->label.mgr, 0, labelWidget, NULL);
RaiseLabelWidget(lframePtr);
}
}
if (mask & GEOMETRY_CHANGED) {
Ttk_ManagerSizeChanged(lframePtr->label.mgr);
Ttk_ManagerLayoutChanged(lframePtr->label.mgr);
}
return TCL_OK;
}
static WidgetSpec LabelframeWidgetSpec =
{
"TLabelframe", /* className */
sizeof(Labelframe), /* recordSize */
LabelframeOptionSpecs, /* optionSpecs */
FrameCommands, /* subcommands */
LabelframeInitialize, /* initializeProc */
LabelframeCleanup, /* cleanupProc */
LabelframeConfigure, /* configureProc */
TtkNullPostConfigure, /* postConfigureProc */
TtkWidgetGetLayout, /* getLayoutProc */
LabelframeSize, /* sizeProc */
LabelframeDoLayout, /* layoutProc */
TtkWidgetDisplay /* displayProc */
};
/* ======================================================================
* +++ Initialization:
*/
MODULE_SCOPE
void TtkFrame_Init(Tcl_Interp *interp)
{
RegisterWidget(interp, "ttk::frame", &FrameWidgetSpec);
RegisterWidget(interp, "ttk::labelframe", &LabelframeWidgetSpec);
}
|