File: tkMacMenubutton.c

package info (click to toggle)
saods9 3.0.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 86,868 kB
  • ctags: 82,021
  • sloc: ansic: 663,637; tcl: 111,403; cpp: 52,727; sh: 27,784; makefile: 6,177; asm: 3,355; lex: 2,890; ada: 1,637; pascal: 1,621; xml: 1,221; yacc: 883; f90: 84; perl: 82; python: 33; fortran: 17; ruby: 13; sed: 12; php: 11
file content (495 lines) | stat: -rw-r--r-- 15,174 bytes parent folder | download
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
/* 
 * tkMacMenubutton.c --
 *
 *	This file implements the Macintosh specific portion of the
 *	menubutton widget.
 *
 * Copyright (c) 1996 by Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tkMacMenubutton.c,v 1.1.1.1 2004/04/02 22:35:04 joye Exp $
 */

#include "tkMenubutton.h"
#include "tkMacInt.h"
#include <Controls.h>

#define kShadowOffset	(3)	/* amount to offset shadow from frame */
#define kTriangleWidth	(11)	/* width of the triangle */
#define kTriangleHeight	(6)	/* height of the triangle */
#define kTriangleMargin	(5)	/* margin around triangle */

/*
 * Declaration of Unix specific button structure.
 */

typedef struct MacMenuButton {
    TkMenuButton info;		/* Generic button info. */
} MacMenuButton;

/*
 * The structure below defines menubutton class behavior by means of
 * procedures 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 *butPtr = (MacMenuButton *)ckalloc(sizeof(MacMenuButton));

    return (TkMenuButton *) butPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * TkpDisplayMenuButton --
 *
 *	This procedure 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;
    GC gc;
    Tk_3DBorder border;
    int x = 0;			/* Initialization needed only to stop
				 * compiler warning. */
    int y;
    Tk_Window tkwin = mbPtr->tkwin;
    int width, height, fullWidth, fullHeight;
    int imageWidth, imageHeight;
    int imageXOffset, imageYOffset, textXOffset, textYOffset;
    int haveImage = 0, haveText = 0;
    MacMenuButton * macMBPtr = (MacMenuButton *) mbPtr;
    GWorldPtr destPort;
    CGrafPtr saveWorld;
    GDHandle saveDevice;
    MacDrawable *macDraw;

    mbPtr->flags &= ~REDRAW_PENDING;
    if ((mbPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
	return;
    }

    GetGWorld(&saveWorld, &saveDevice);
    destPort = TkMacGetDrawablePort(Tk_WindowId(tkwin));
    SetGWorld(destPort, NULL);
    macDraw = (MacDrawable *) Tk_WindowId(tkwin);

    if ((mbPtr->state == STATE_DISABLED) && (mbPtr->disabledFg != NULL)) {
	gc = mbPtr->disabledGC;
    } else if ((mbPtr->state == STATE_ACTIVE)
	    && !Tk_StrictMotif(mbPtr->tkwin)) {
	gc = mbPtr->activeTextGC;
    } else {
	gc = mbPtr->normalTextGC;
    }
    border = mbPtr->normalBorder;

    if (mbPtr->image != None) {
        Tk_SizeOfImage(mbPtr->image, &width, &height);
        haveImage = 1;
    } else if (mbPtr->bitmap != None) {
        Tk_SizeOfBitmap(mbPtr->display, mbPtr->bitmap, &width, &height);
        haveImage = 1;
    }
    imageWidth  = width;
    imageHeight = height;

    haveText = (mbPtr->textWidth != 0 && mbPtr->textHeight != 0);

    /*
     * In order to avoid screen flashes, this procedure redraws
     * the menu button in a pixmap, then copies the pixmap to the
     * screen in a single operation.  This means that there's no
     * point in time where the on-sreen image has been cleared.
     */

    Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin), border, 0, 0,
	    Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);

    imageXOffset = 0;
    imageYOffset = 0;
    textXOffset = 0;
    textYOffset = 0;
    fullWidth = 0;
    fullHeight = 0;

    if (mbPtr->compound != COMPOUND_NONE && haveImage && haveText) {
        switch ((enum compound) mbPtr->compound) {
            case COMPOUND_TOP:
            case COMPOUND_BOTTOM: {
                /* Image is above or below text */
                if (mbPtr->compound == COMPOUND_TOP) {
                    textYOffset = height + mbPtr->padY;
                } else {
                    imageYOffset = mbPtr->textHeight + mbPtr->padY;
                }
                fullHeight = height + mbPtr->textHeight + mbPtr->padY;
                fullWidth = (width > mbPtr->textWidth ? width :
                        mbPtr->textWidth);
                textXOffset = (fullWidth - mbPtr->textWidth)/2;
                imageXOffset = (fullWidth - width)/2;
                break;
            }
            case COMPOUND_LEFT:
            case COMPOUND_RIGHT: {
                /* Image is left or right of text */
                if (mbPtr->compound == COMPOUND_LEFT) {
                    textXOffset = width + mbPtr->padX;
                } else {
                    imageXOffset = mbPtr->textWidth + mbPtr->padX;
                }
                fullWidth = mbPtr->textWidth + mbPtr->padX + width;
                fullHeight = (height > mbPtr->textHeight ? height :
                        mbPtr->textHeight);
                textYOffset = (fullHeight - mbPtr->textHeight)/2;
                imageYOffset = (fullHeight - height)/2;
                break;
            }
            case COMPOUND_CENTER: {
                /* Image and text are superimposed */
                fullWidth = (width > mbPtr->textWidth ? width :
                        mbPtr->textWidth);
                fullHeight = (height > mbPtr->textHeight ? height :
                        mbPtr->textHeight);
                textXOffset = (fullWidth - mbPtr->textWidth)/2;
                imageXOffset = (fullWidth - width)/2;
                textYOffset = (fullHeight - mbPtr->textHeight)/2;
                imageYOffset = (fullHeight - height)/2;
                break;
            }
            case COMPOUND_NONE: {break;}
        }


        TkComputeAnchor(mbPtr->anchor, tkwin, 0, 0,
                mbPtr->indicatorWidth + fullWidth, fullHeight,
                &x, &y);

	imageXOffset += x;
	imageYOffset += y;
        if (mbPtr->image != NULL) {
            Tk_RedrawImage(mbPtr->image, 0, 0, width, height, Tk_WindowId(tkwin),
                    imageXOffset, imageYOffset);
        } else if (mbPtr->bitmap != None) {
            XCopyPlane(mbPtr->display, mbPtr->bitmap, Tk_WindowId(tkwin),
                    gc, 0, 0, (unsigned) width, (unsigned) height,
                    imageXOffset, imageYOffset, 1);
        }

	Tk_DrawTextLayout(mbPtr->display, Tk_WindowId(tkwin), gc,
		mbPtr->textLayout, x + textXOffset, y + textYOffset, 0, -1);
	Tk_UnderlineTextLayout(mbPtr->display, Tk_WindowId(tkwin), gc,
		mbPtr->textLayout, x + textXOffset, y + textYOffset,
		mbPtr->underline);
    } else if (haveImage) {
	TkComputeAnchor(mbPtr->anchor, tkwin, 0, 0,
		width + mbPtr->indicatorWidth, height, &x, &y);
	imageXOffset += x;
	imageYOffset += y;
	if (mbPtr->image != NULL) {
	    Tk_RedrawImage(mbPtr->image, 0, 0, width, height, Tk_WindowId(tkwin),
		    imageXOffset, imageYOffset);
	} else if (mbPtr->bitmap != None) {
	    XCopyPlane(mbPtr->display, mbPtr->bitmap, Tk_WindowId(tkwin),
		    gc, 0, 0, (unsigned) width, (unsigned) height, 
		    x, y, 1);
	}
    } else {
	TkComputeAnchor(mbPtr->anchor, tkwin, mbPtr->padX, mbPtr->padY,
		mbPtr->textWidth + mbPtr->indicatorWidth,
		mbPtr->textHeight, &x, &y);
	Tk_DrawTextLayout(mbPtr->display, Tk_WindowId(tkwin), gc,
		mbPtr->textLayout, x + textXOffset, y + textYOffset, 0, -1);
	Tk_UnderlineTextLayout(mbPtr->display, Tk_WindowId(tkwin), gc,
		mbPtr->textLayout, x + textXOffset, y + textYOffset,
		mbPtr->underline);
    }

#if 0		/* this is the original code */
    /*
     * Display image or bitmap or text for button.
     */

    if (mbPtr->image != None) {
	Tk_SizeOfImage(mbPtr->image, &width, &height);

	imageOrBitmap:
	TkComputeAnchor(mbPtr->anchor, tkwin, 0, 0, 
		width + mbPtr->indicatorWidth, height, &x, &y);
	if (mbPtr->image != NULL) {
	    Tk_RedrawImage(mbPtr->image, 0, 0, width, height,
		    Tk_WindowId(tkwin), x, y);
	} else {
	    XCopyPlane(mbPtr->display, mbPtr->bitmap, Tk_WindowId(tkwin),
		    gc, 0, 0, (unsigned) width, (unsigned) height, x, y, 1);
	}
    } else if (mbPtr->bitmap != None) {
	Tk_SizeOfBitmap(mbPtr->display, mbPtr->bitmap, &width, &height);
	goto imageOrBitmap;
    } else {
	TkComputeAnchor(mbPtr->anchor, tkwin, mbPtr->padX, mbPtr->padY,
		mbPtr->textWidth + mbPtr->indicatorWidth, mbPtr->textHeight,
		&x, &y);
	Tk_DrawTextLayout(mbPtr->display, Tk_WindowId(tkwin), gc,
		mbPtr->textLayout, x, y, 0, -1);
    }
#endif

    /*
     * If the menu button is disabled with a stipple rather than a special
     * foreground color, generate the stippled effect.
     */

    if ((mbPtr->state == STATE_DISABLED)
	    && ((mbPtr->disabledFg != NULL) || (mbPtr->image != NULL))) {
	/*
	 * Stipple the whole button if no disabledFg was specified,
	 * otherwise restrict stippling only to displayed image
	 */
	if (mbPtr->disabledFg == NULL) {
	    XFillRectangle(mbPtr->display, Tk_WindowId(tkwin),
		    mbPtr->stippleGC, mbPtr->inset, mbPtr->inset,
		    (unsigned) (Tk_Width(tkwin) - 2*mbPtr->inset),
		    (unsigned) (Tk_Height(tkwin) - 2*mbPtr->inset));
	} else {
	    XFillRectangle(mbPtr->display, Tk_WindowId(tkwin),
		    mbPtr->stippleGC, imageXOffset, imageYOffset,
		    (unsigned) imageWidth, (unsigned) imageHeight);
	}
    }

    /*
     * Draw the cascade indicator for the menu button on the
     * right side of the window, if desired.
     */

    if (mbPtr->indicatorOn) {
	int w, h, i;
	Rect r;

	r.left = macDraw->xOff + Tk_Width(tkwin) - mbPtr->inset
	    - mbPtr->indicatorWidth;
	r.top = macDraw->yOff + Tk_Height(tkwin)/2
	    - mbPtr->indicatorHeight/2;
	r.right = macDraw->xOff + Tk_Width(tkwin) - mbPtr->inset
	    - kTriangleMargin;
	r.bottom = macDraw->yOff + Tk_Height(tkwin)/2
	    + mbPtr->indicatorHeight/2;

	h = mbPtr->indicatorHeight;
	w = mbPtr->indicatorWidth - 1 - kTriangleMargin;
	for (i = 0; i < h; i++) {
	    MoveTo(r.left + i, r.top + i);
	    LineTo(r.left + i + w, r.top + i);
	    w -= 2;
	}
    }

    /*
     * Draw the border and traversal highlight last.  This way, if the
     * menu button's contents overflow onto the border they'll be covered
     * up by the border.
     */

    TkMacSetUpClippingRgn(Tk_WindowId(tkwin));
    if (mbPtr->borderWidth > 0) {
	Rect r;
	
	r.left = macDraw->xOff + mbPtr->highlightWidth + mbPtr->borderWidth;
	r.top = macDraw->yOff + mbPtr->highlightWidth + mbPtr->borderWidth;
	r.right = macDraw->xOff + Tk_Width(tkwin) - mbPtr->highlightWidth
	    - mbPtr->borderWidth;
	r.bottom = macDraw->yOff + Tk_Height(tkwin) - mbPtr->highlightWidth
	    - mbPtr->borderWidth;
	FrameRect(&r);

	PenSize(mbPtr->borderWidth - 1, mbPtr->borderWidth - 1);
	MoveTo(r.right, r.top + kShadowOffset);
	LineTo(r.right, r.bottom);
	LineTo(r.left + kShadowOffset, r.bottom);
    }
    
    if (mbPtr->highlightWidth != 0) {
	GC fgGC, bgGC;

	bgGC = Tk_GCForColor(mbPtr->highlightBgColorPtr, Tk_WindowId(tkwin));
	if (mbPtr->flags & GOT_FOCUS) {
	    fgGC = Tk_GCForColor(mbPtr->highlightColorPtr, Tk_WindowId(tkwin));
	    TkpDrawHighlightBorder(tkwin, fgGC, bgGC, mbPtr->highlightWidth,
		    Tk_WindowId(tkwin));
	} else {
	    TkpDrawHighlightBorder(tkwin, bgGC, bgGC, mbPtr->highlightWidth,
		    Tk_WindowId(tkwin));
	}
    }

    SetGWorld(saveWorld, saveDevice);
}

/*
 *----------------------------------------------------------------------
 *
 * TkpDestroyMenuButton --
 *
 *	Free data structures associated with the menubutton control.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Restores the default control state.
 *
 *----------------------------------------------------------------------
 */

void
TkpDestroyMenuButton(
    TkMenuButton *mbPtr)
{
}

/*
 *----------------------------------------------------------------------
 *
 * TkpComputeMenuButtonGeometry --
 *
 *	After changes in a menu button's text or bitmap, this procedure
 *	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(mbPtr)
    register TkMenuButton *mbPtr;		/* Widget record for menu button. */
{
    int width=0, height=0, textwidth=0, textheight=0, mm, pixels, noimage=0;

    mbPtr->inset = mbPtr->highlightWidth + mbPtr->borderWidth;
    if (mbPtr->image != None) {
	Tk_SizeOfImage(mbPtr->image, &width, &height);
	if (mbPtr->width > 0) {
	    width = mbPtr->width;
	}
	if (mbPtr->height > 0) {
	    height = mbPtr->height;
	}
    } else if (mbPtr->bitmap != None) {
	Tk_SizeOfBitmap(mbPtr->display, mbPtr->bitmap, &width, &height);
	if (mbPtr->width > 0) {
	    width = mbPtr->width;
	}
	if (mbPtr->height > 0) {
	    height = mbPtr->height;
	}
    } else {
    noimage=1;
    }
    
    if ( noimage || mbPtr->compound != COMPOUND_NONE ) {
	Tk_FreeTextLayout(mbPtr->textLayout);
	mbPtr->textLayout = Tk_ComputeTextLayout(mbPtr->tkfont, mbPtr->text,
		-1, mbPtr->wrapLength, mbPtr->justify, 0, &mbPtr->textWidth,
		&mbPtr->textHeight);
	textwidth = mbPtr->textWidth;
	textheight = mbPtr->textHeight;
	if (mbPtr->width > 0) {
	    textwidth = mbPtr->width * Tk_TextWidth(mbPtr->tkfont, "0", 1);
	}
	if (mbPtr->height > 0) {
	    Tk_FontMetrics fm;

	    Tk_GetFontMetrics(mbPtr->tkfont, &fm);
	    textheight = mbPtr->height * fm.linespace;
	}
	textwidth += 2*mbPtr->padX;
	textheight +=  2*mbPtr->padY;
    }
    
	switch ((enum compound) mbPtr->compound) {
	  case COMPOUND_TOP:
	  case COMPOUND_BOTTOM: {
	      height += textheight + mbPtr->padY;
	      width = (width > textwidth ? width : textwidth);
	      break;
	  }
	  case COMPOUND_LEFT:
	  case COMPOUND_RIGHT: {
	      height = (height > textheight ? height : textheight);
	      width += textwidth + mbPtr->padX;
	      break;
	  }
	  case COMPOUND_CENTER: {
	      height = (height > textheight ? height : textheight);
	      width = (width > textwidth ? width : textwidth);
	      break;
	  }
	  case COMPOUND_NONE: {
	     if (noimage) {
	     	height = textheight;
	     	width = textwidth;
	     }
	     break;
	}
    }

    if (mbPtr->indicatorOn) {
	mm = WidthMMOfScreen(Tk_Screen(mbPtr->tkwin));
	pixels = WidthOfScreen(Tk_Screen(mbPtr->tkwin));
	mbPtr->indicatorHeight= kTriangleHeight;
	mbPtr->indicatorWidth = kTriangleWidth + kTriangleMargin;
	width += mbPtr->indicatorWidth;
    } else {
	mbPtr->indicatorHeight = 0;
	mbPtr->indicatorWidth = 0;
    }

    Tk_GeometryRequest(mbPtr->tkwin, (int) (width + 2*mbPtr->inset),
	    (int) (height + 2*mbPtr->inset));
    Tk_SetInternalBorder(mbPtr->tkwin, mbPtr->inset);
}