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
|
/*
* tkMacOSXMenubutton.c --
*
* This file implements the Macintosh specific portion of the
* menubutton widget.
*
* Copyright (c) 1996 by Sun Microsystems, Inc.
* Copyright 2001-2009, Apple Inc.
* Copyright (c) 2006-2009 Daniel A. Steffen <das@users.sourceforge.net>
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tkMacOSXMenubutton.c,v 1.22 2009/07/06 20:29:21 dkf Exp $
*/
#include "tkMacOSXPrivate.h"
#include "tkMenubutton.h"
#include "tkMacOSXFont.h"
#include "tkMacOSXDebug.h"
/*
#ifdef TK_MAC_DEBUG
#define TK_MAC_DEBUG_MENUBUTTON
#endif
*/
typedef struct MacMenuButton {
TkMenuButton info;
NSPopUpButton *button;
#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS
int fix;
#endif
} MacMenuButton;
#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS
/*
* Use the following heuristic conversion constants to make NSButton-based
* widget metrics match up with the old Carbon control buttons (for the
* default Lucida Grande 13 font).
* TODO: provide a scriptable way to turn this off and use the raw NSButton
* metrics (will also need dynamic adjustment of the default padding,
* c.f. tkMacOSXDefault.h).
*/
typedef struct {
int trimW, trimH, inset, shrinkW, offsetX, offsetY;
} BoundsFix;
#define fixForStyle(style) ( \
style == NSRoundedBezelStyle ? 1 : \
style == NSRegularSquareBezelStyle ? 2 : \
style == NSShadowlessSquareBezelStyle ? 3 : \
INT_MIN)
static const BoundsFix boundsFixes[] = {
[fixForStyle(NSRoundedBezelStyle)] = { 14, 10, -2, -1},
[fixForStyle(NSRegularSquareBezelStyle)] = { 6, 13, -2, 1, 1},
[fixForStyle(NSShadowlessSquareBezelStyle)] = { 15, 0, 2 },
};
#endif
/*
* Forward declarations for procedures defined later in this file:
*/
static void MenuButtonEventProc(ClientData clientData, XEvent *eventPtr);
/*
* The structure below defines menubutton class behavior by means of functions
* that can be invoked from generic window code.
*/
Tk_ClassProcs tkpMenubuttonClass = {
sizeof(Tk_ClassProcs), /* size */
TkMenuButtonWorldChanged, /* worldChangedProc */
};
/*
*----------------------------------------------------------------------
*
* TkpCreateMenuButton --
*
* Allocate a new TkMenuButton structure.
*
* Results:
* Returns a newly allocated TkMenuButton structure.
*
* Side effects:
* Registers an event handler for the widget.
*
*----------------------------------------------------------------------
*/
TkMenuButton *
TkpCreateMenuButton(
Tk_Window tkwin)
{
MacMenuButton *macButtonPtr =
(MacMenuButton *) ckalloc(sizeof(MacMenuButton));
macButtonPtr->button = nil;
Tk_CreateEventHandler(tkwin, ActivateMask,
MenuButtonEventProc, (ClientData) macButtonPtr);
return (TkMenuButton *) macButtonPtr;
}
/*
*----------------------------------------------------------------------
*
* TkpDestroyMenuButton --
*
* Free data structures associated with the menubutton control.
*
* Results:
* None.
*
* Side effects:
* Restores the default control state.
*
*----------------------------------------------------------------------
*/
void
TkpDestroyMenuButton(
TkMenuButton *mbPtr)
{
MacMenuButton *macButtonPtr = (MacMenuButton *) mbPtr;
TkMacOSXMakeCollectableAndRelease(macButtonPtr->button);
}
/*
*----------------------------------------------------------------------
*
* TkpDisplayMenuButton --
*
* This function is invoked to display a menubutton widget.
*
* Results:
* None.
*
* Side effects:
* Commands are output to X to display the menubutton in its current
* mode.
*
*----------------------------------------------------------------------
*/
void
TkpDisplayMenuButton(
ClientData clientData) /* Information about widget. */
{
TkMenuButton *mbPtr = (TkMenuButton *) clientData;
MacMenuButton *macButtonPtr = (MacMenuButton *) mbPtr;
NSPopUpButton *button = macButtonPtr->button;
Tk_Window tkwin = mbPtr->tkwin;
TkWindow *winPtr = (TkWindow *) tkwin;
MacDrawable *macWin = (MacDrawable *) winPtr->window;
TkMacOSXDrawingContext dc;
NSView *view = TkMacOSXDrawableView(macWin);
CGFloat viewHeight = [view bounds].size.height;
CGAffineTransform t = { .a = 1, .b = 0, .c = 0, .d = -1, .tx = 0,
.ty = viewHeight};
NSRect frame;
int enabled;
mbPtr->flags &= ~REDRAW_PENDING;
if (!tkwin || !Tk_IsMapped(tkwin) || !view ||
!TkMacOSXSetupDrawingContext((Drawable) macWin, NULL, 1, &dc)) {
return;
}
CGContextConcatCTM(dc.context, t);
Tk_Fill3DRectangle(tkwin, (Pixmap) macWin, mbPtr->normalBorder, 0, 0,
Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
if ([button superview] != view) {
[view addSubview:button];
}
enabled = !(mbPtr->state == STATE_DISABLED);
[button setEnabled:enabled];
if (enabled) {
[[button cell] setHighlighted:(mbPtr->state == STATE_ACTIVE)];
}
frame = NSMakeRect(macWin->xOff, macWin->yOff, Tk_Width(tkwin),
Tk_Height(tkwin));
frame = NSInsetRect(frame, mbPtr->inset, mbPtr->inset);
#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS
if (tkMacOSXUseCompatibilityMetrics) {
BoundsFix boundsFix = boundsFixes[macButtonPtr->fix];
frame = NSOffsetRect(frame, boundsFix.offsetX, boundsFix.offsetY);
frame.size.width -= boundsFix.shrinkW;
frame = NSInsetRect(frame, boundsFix.inset, boundsFix.inset);
}
#endif
frame.origin.y = viewHeight - (frame.origin.y + frame.size.height);
if (!NSEqualRects(frame, [button frame])) {
[button setFrame:frame];
}
[button displayRectIgnoringOpacity:[button bounds]];
TkMacOSXRestoreDrawingContext(&dc);
#ifdef TK_MAC_DEBUG_MENUBUTTON
TKLog(@"menubutton %s frame %@ width %d height %d",
((TkWindow *)mbPtr->tkwin)->pathName, NSStringFromRect(frame),
Tk_Width(tkwin), Tk_Height(tkwin));
#endif
}
/*
*----------------------------------------------------------------------
*
* TkpComputeMenuButtonGeometry --
*
* After changes in a menu button's text or bitmap, this function
* recomputes the menu button's geometry and passes this information
* along to the geometry manager for the window.
*
* Results:
* None.
*
* Side effects:
* The menu button's window may change size.
*
*----------------------------------------------------------------------
*/
void
TkpComputeMenuButtonGeometry(
TkMenuButton *mbPtr) /* Widget record for menu button. */
{
MacMenuButton *macButtonPtr = (MacMenuButton *) mbPtr;
NSPopUpButton *button = macButtonPtr->button;
NSPopUpButtonCell *cell;
NSMenuItem *menuItem;
NSBezelStyle style = NSRoundedBezelStyle;
NSFont *font;
NSRect bounds = NSZeroRect, titleRect = NSZeroRect;
int haveImage = (mbPtr->image || mbPtr->bitmap != None), haveText = 0;
int haveCompound = (mbPtr->compound != COMPOUND_NONE);
int width, height;
if (!button) {
button = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:YES];
macButtonPtr->button = TkMacOSXMakeUncollectable(button);
cell = [button cell];
[cell setUsesItemFromMenu:NO];
menuItem = [[[NSMenuItem alloc] initWithTitle:@""
action:NULL keyEquivalent:@""] autorelease];
[cell setMenuItem:menuItem];
} else {
cell = [button cell];
menuItem = [cell menuItem];
}
if (haveImage) {
style = NSShadowlessSquareBezelStyle;
} else if (!mbPtr->indicatorOn) {
style = NSRegularSquareBezelStyle;
}
[button setBezelStyle:style];
[cell setArrowPosition:(mbPtr->indicatorOn ? NSPopUpArrowAtBottom :
NSPopUpNoArrow)];
#if 0
NSControlSize controlSize = NSRegularControlSize;
if (mbPtr->borderWidth <= 2) {
controlSize = NSMiniControlSize;
} else if (mbPtr->borderWidth == 3) {
controlSize = NSSmallControlSize;
}
[cell setControlSize:controlSize];
#endif
if (mbPtr->text && *(mbPtr->text) && (!haveImage || haveCompound)) {
NSString *title = [[NSString alloc] initWithUTF8String:mbPtr->text];
[button setTitle:title];
[title release];
haveText = 1;
}
haveCompound = (haveCompound && haveImage && haveText);
if (haveText) {
NSTextAlignment alignment = NSNaturalTextAlignment;
switch (mbPtr->justify) {
case TK_JUSTIFY_LEFT:
alignment = NSLeftTextAlignment;
break;
case TK_JUSTIFY_RIGHT:
alignment = NSRightTextAlignment;
break;
case TK_JUSTIFY_CENTER:
alignment = NSCenterTextAlignment;
break;
}
[button setAlignment:alignment];
} else {
[button setTitle:@""];
}
font = TkMacOSXNSFontForFont(mbPtr->tkfont);
if (font) {
[button setFont:font];
}
if (haveImage) {
int width, height;
NSImage *image;
NSCellImagePosition pos = NSImageOnly;
if (mbPtr->image) {
Tk_SizeOfImage(mbPtr->image, &width, &height);
image = TkMacOSXGetNSImageWithTkImage(mbPtr->display,
mbPtr->image, width, height);
} else {
Tk_SizeOfBitmap(mbPtr->display, mbPtr->bitmap, &width, &height);
image = TkMacOSXGetNSImageWithBitmap(mbPtr->display,
mbPtr->bitmap, mbPtr->normalTextGC, width, height);
}
if (haveCompound) {
switch ((enum compound) mbPtr->compound) {
case COMPOUND_TOP:
pos = NSImageAbove;
break;
case COMPOUND_BOTTOM:
pos = NSImageBelow;
break;
case COMPOUND_LEFT:
pos = NSImageLeft;
break;
case COMPOUND_RIGHT:
pos = NSImageRight;
break;
case COMPOUND_CENTER:
pos = NSImageOverlaps;
break;
case COMPOUND_NONE:
pos = NSImageOnly;
break;
}
}
[button setImagePosition:pos];
[menuItem setImage:image];
bounds.size = cell ? [cell cellSize] : NSZeroSize;
if (bounds.size.height < height + 8) { /* workaround AppKit sizing bug */
bounds.size.height = height + 8;
}
#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS
if (!mbPtr->indicatorOn && tkMacOSXUseCompatibilityMetrics) {
bounds.size.width -= 16;
}
#endif
} else {
bounds.size = cell ? [cell cellSize] : NSZeroSize;
}
if (haveText) {
titleRect = cell ? [cell titleRectForBounds:bounds] : NSZeroRect;
if (mbPtr->wrapLength > 0 &&
titleRect.size.width > mbPtr->wrapLength) {
if (style == NSRoundedBezelStyle) {
[button setBezelStyle:(style = NSRegularSquareBezelStyle)];
bounds.size = cell ? [cell cellSize] : NSZeroSize;
titleRect = cell ? [cell titleRectForBounds:bounds] : NSZeroRect;
}
bounds.size.width -= titleRect.size.width - mbPtr->wrapLength;
bounds.size.height = 40000.0;
[cell setWraps:YES];
bounds.size = cell ? [cell cellSizeForBounds:bounds] : NSZeroSize;
#ifdef TK_MAC_DEBUG_MENUBUTTON
titleRect = cell ? [cell titleRectForBounds:bounds] : NSZeroRect;
#endif
#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS
if (tkMacOSXUseCompatibilityMetrics) {
bounds.size.height += 3;
}
#endif
}
}
width = lround(bounds.size.width);
height = lround(bounds.size.height);
#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS
if (tkMacOSXUseCompatibilityMetrics) {
macButtonPtr->fix = fixForStyle(style);
width -= boundsFixes[macButtonPtr->fix].trimW;
height -= boundsFixes[macButtonPtr->fix].trimH;
}
#endif
if (haveImage || haveCompound) {
if (mbPtr->width > 0) {
width = mbPtr->width;
}
if (mbPtr->height > 0) {
height = mbPtr->height;
}
} else {
if (mbPtr->width > 0) {
int avgWidth = Tk_TextWidth(mbPtr->tkfont, "0", 1);
width = mbPtr->width * avgWidth;
}
if (mbPtr->height > 0) {
Tk_FontMetrics fm;
Tk_GetFontMetrics(mbPtr->tkfont, &fm);
height = mbPtr->height * fm.linespace;
}
}
if (!haveImage || haveCompound) {
width += 2*mbPtr->padX;
height += 2*mbPtr->padY;
}
if (mbPtr->highlightWidth < 0) {
mbPtr->highlightWidth = 0;
}
if (haveImage) {
mbPtr->inset = mbPtr->highlightWidth;
width += 2*mbPtr->borderWidth;
height += 2*mbPtr->borderWidth;
} else {
mbPtr->inset = mbPtr->highlightWidth + mbPtr->borderWidth;
}
Tk_GeometryRequest(mbPtr->tkwin, width + 2 * mbPtr->inset,
height + 2 * mbPtr->inset);
Tk_SetInternalBorder(mbPtr->tkwin, mbPtr->inset);
#ifdef TK_MAC_DEBUG_MENUBUTTON
TKLog(@"menubutton %s bounds %@ titleRect %@ width %d height %d inset %d borderWidth %d",
((TkWindow *)mbPtr->tkwin)->pathName, NSStringFromRect(bounds),
NSStringFromRect(titleRect), width, height, mbPtr->inset,
mbPtr->borderWidth);
#endif
}
/*
*--------------------------------------------------------------
*
* MenuButtonEventProc --
*
* This procedure is invoked by the Tk dispatcher for various
* events on buttons.
*
* Results:
* None.
*
* Side effects:
* When activation state changes, it is redisplayed.
*
*--------------------------------------------------------------
*/
static void
MenuButtonEventProc(
ClientData clientData, /* Information about window. */
XEvent *eventPtr) /* Information about event. */
{
TkMenuButton *mbPtr = (TkMenuButton *) clientData;
if (!mbPtr->tkwin || !Tk_IsMapped(mbPtr->tkwin)) {
return;
}
switch (eventPtr->type) {
case ActivateNotify:
case DeactivateNotify:
if (!(mbPtr->flags & REDRAW_PENDING)) {
Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
mbPtr->flags |= REDRAW_PENDING;
}
break;
}
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|