Debian Codegraph @

pTk/mTk/generic/tkUnixMenubu.c

raw symbols SCIP
1 /*
2 * tkUnixMenubu.c --
3 *
4 * This file implements the Unix specific portion of the
5 * menubutton widget.
6 *
7 * Copyright (c) 1996-1997 by Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkUnixMenubu.c,v 1.6.4.1 2003/11/17 23:29:36 hobbs Exp $
13 */
14
15 #include "default.h"
16 #include "tkInt.h"
17 #include "tkMenubutton.h"
18
19 /*
20 * The structure below defines menubutton class behavior by means of
21 * procedures that can be invoked from generic window code.
22 */
23
24 Tk_ClassProcs tkpMenubuttonClass = {
25 sizeof(Tk_ClassProcs), /* size */
26 TkMenuButtonWorldChanged, /* worldChangedProc */
27 };
28
29 /*
30 *----------------------------------------------------------------------
31 *
32 * TkpCreateMenuButton --
33 *
34 * Allocate a new TkMenuButton structure.
35 *
36 * Results:
37 * Returns a newly allocated TkMenuButton structure.
38 *
39 * Side effects:
40 * Registers an event handler for the widget.
41 *
42 *----------------------------------------------------------------------
43 */
44
45 TkMenuButton *
46 TkpCreateMenuButton(tkwin)
47 Tk_Window tkwin;
48 {
49 return (TkMenuButton *)ckalloc(sizeof(TkMenuButton));
50 }
51
52 /*
53 *----------------------------------------------------------------------
54 *
55 * TkpDisplayMenuButton --
56 *
57 * This procedure is invoked to display a menubutton widget.
58 *
59 * Results:
60 * None.
61 *
62 * Side effects:
63 * Commands are output to X to display the menubutton in its
64 * current mode.
65 *
66 *----------------------------------------------------------------------
67 */
68
69 void
70 TkpDisplayMenuButton(clientData)
71 ClientData clientData; /* Information about widget. */
72 {
73 register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
74 GC gc;
75 Tk_3DBorder border;
76 Pixmap pixmap;
77 int x = 0; /* Initialization needed only to stop
78 * compiler warning. */
79 int y = 0;
80 register Tk_Window tkwin = mbPtr->tkwin;
81 int width, height, fullWidth, fullHeight;
82 int textXOffset, textYOffset;
83 int imageWidth, imageHeight;
84 int imageXOffset, imageYOffset; /* image information that will be used to
85 * restrict disabled pixmap as well */
86 int haveImage = 0, haveText = 0;
87
88 mbPtr->flags &= ~REDRAW_PENDING;
89 if ((mbPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
90 return;
91 }
92
93 if ((mbPtr->state == STATE_DISABLED) && (mbPtr->disabledFg != NULL)) {
94 gc = mbPtr->disabledGC;
95 border = mbPtr->normalBorder;
96 } else if ((mbPtr->state == STATE_ACTIVE)
97 && !Tk_StrictMotif(mbPtr->tkwin)) {
98 gc = mbPtr->activeTextGC;
99 border = mbPtr->activeBorder;
100 } else {
101 gc = mbPtr->normalTextGC;
102 border = mbPtr->normalBorder;
103 }
104
105 if (mbPtr->image != None) {
106 Tk_SizeOfImage(mbPtr->image, &width, &height);
107 haveImage = 1;
108 } else if (mbPtr->bitmap != None) {
109 Tk_SizeOfBitmap(mbPtr->display, mbPtr->bitmap, &width, &height);
110 haveImage = 1;
111 }
112 imageWidth = width;
113 imageHeight = height;
114
115 haveText = (mbPtr->textWidth != 0 && mbPtr->textHeight != 0);
116
117 /*
118 * In order to avoid screen flashes, this procedure redraws
119 * the menu button in a pixmap, then copies the pixmap to the
120 * screen in a single operation. This means that there's no
121 * point in time where the on-sreen image has been cleared.
122 */
123
124 pixmap = Tk_GetPixmap(mbPtr->display, Tk_WindowId(tkwin),
125 Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
126 Tk_Fill3DRectangle(tkwin, pixmap, border, 0, 0, Tk_Width(tkwin),
127 Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
128
129 imageXOffset = 0;
130 imageYOffset = 0;
131 textXOffset = 0;
132 textYOffset = 0;
133 fullWidth = 0;
134 fullHeight = 0;
135
136 if (mbPtr->compound != COMPOUND_NONE && haveImage && haveText) {
137 switch ((enum compound) mbPtr->compound) {
138 case COMPOUND_TOP:
139 case COMPOUND_BOTTOM: {
140 /* Image is above or below text */
141 if (mbPtr->compound == COMPOUND_TOP) {
142 textYOffset = height + mbPtr->padY;
143 } else {
144 imageYOffset = mbPtr->textHeight + mbPtr->padY;
145 }
146 fullHeight = height + mbPtr->textHeight + mbPtr->padY;
147 fullWidth = (width > mbPtr->textWidth ? width :
148 mbPtr->textWidth);
149 textXOffset = (fullWidth - mbPtr->textWidth)/2;
150 imageXOffset = (fullWidth - width)/2;
151 break;
152 }
153 case COMPOUND_LEFT:
154 case COMPOUND_RIGHT: {
155 /* Image is left or right of text */
156 if (mbPtr->compound == COMPOUND_LEFT) {
157 textXOffset = width + mbPtr->padX;
158 } else {
159 imageXOffset = mbPtr->textWidth + mbPtr->padX;
160 }
161 fullWidth = mbPtr->textWidth + mbPtr->padX + width;
162 fullHeight = (height > mbPtr->textHeight ? height :
163 mbPtr->textHeight);
164 textYOffset = (fullHeight - mbPtr->textHeight)/2;
165 imageYOffset = (fullHeight - height)/2;
166 break;
167 }
168 case COMPOUND_CENTER: {
169 /* Image and text are superimposed */
170 fullWidth = (width > mbPtr->textWidth ? width :
171 mbPtr->textWidth);
172 fullHeight = (height > mbPtr->textHeight ? height :
173 mbPtr->textHeight);
174 textXOffset = (fullWidth - mbPtr->textWidth)/2;
175 imageXOffset = (fullWidth - width)/2;
176 textYOffset = (fullHeight - mbPtr->textHeight)/2;
177 imageYOffset = (fullHeight - height)/2;
178 break;
179 }
180 case COMPOUND_NONE: {break;}
181 }
182
183 TkComputeAnchor(mbPtr->anchor, tkwin, 0, 0,
184 mbPtr->indicatorWidth + fullWidth, fullHeight, &x, &y);
185
186 imageXOffset += x;
187 imageYOffset += y;
188 if (mbPtr->image != NULL) {
189 Tk_RedrawImage(mbPtr->image, 0, 0, width, height, pixmap,
190 imageXOffset, imageYOffset);
191 } else if (mbPtr->bitmap != None) {
192 XSetClipOrigin(mbPtr->display, gc, imageXOffset, imageYOffset);
193 XCopyPlane(mbPtr->display, mbPtr->bitmap, pixmap,
194 gc, 0, 0, (unsigned) width, (unsigned) height,
195 imageXOffset, imageYOffset, 1);
196 XSetClipOrigin(mbPtr->display, gc, 0, 0);
197 }
198
199 Tk_DrawTextLayout(mbPtr->display, pixmap, gc, mbPtr->textLayout,
200 x + textXOffset, y + textYOffset, 0, -1);
201 Tk_UnderlineTextLayout(mbPtr->display, pixmap, gc, mbPtr->textLayout,
202 x + textXOffset, y + textYOffset, mbPtr->underline);
203 } else if (haveImage) {
204 TkComputeAnchor(mbPtr->anchor, tkwin, 0, 0,
205 width + mbPtr->indicatorWidth, height, &x, &y);
206 imageXOffset += x;
207 imageYOffset += y;
208 if (mbPtr->image != NULL) {
209 Tk_RedrawImage(mbPtr->image, 0, 0, width, height, pixmap,
210 imageXOffset, imageYOffset);
211 } else if (mbPtr->bitmap != None) {
212 XSetClipOrigin(mbPtr->display, gc, x, y);
213 XCopyPlane(mbPtr->display, mbPtr->bitmap, pixmap,
214 gc, 0, 0, (unsigned) width, (unsigned) height,
215 x, y, 1);
216 XSetClipOrigin(mbPtr->display, gc, 0, 0);
217 }
218 } else {
219 TkComputeAnchor(mbPtr->anchor, tkwin, mbPtr->padX, mbPtr->padY,
220 mbPtr->textWidth + mbPtr->indicatorWidth,
221 mbPtr->textHeight, &x, &y);
222 Tk_DrawTextLayout(mbPtr->display, pixmap, gc, mbPtr->textLayout,
223 x + textXOffset, y + textYOffset, 0, -1);
224 Tk_UnderlineTextLayout(mbPtr->display, pixmap, gc,
225 mbPtr->textLayout, x + textXOffset, y + textYOffset,
226 mbPtr->underline);
227 }
228
229 /*
230 * If the menu button is disabled with a stipple rather than a special
231 * foreground color, generate the stippled effect.
232 */
233
234 if ((mbPtr->state == STATE_DISABLED)
235 && ((mbPtr->disabledFg == NULL) || (mbPtr->image != NULL))) {
236 /*
237 * Stipple the whole button if no disabledFg was specified,
238 * otherwise restrict stippling only to displayed image
239 */
240 if (mbPtr->disabledFg == NULL) {
241 XFillRectangle(mbPtr->display, pixmap, mbPtr->stippleGC,
242 mbPtr->inset, mbPtr->inset,
243 (unsigned) (Tk_Width(tkwin) - 2*mbPtr->inset),
244 (unsigned) (Tk_Height(tkwin) - 2*mbPtr->inset));
245 } else {
246 XFillRectangle(mbPtr->display, pixmap, mbPtr->stippleGC,
247 imageXOffset, imageYOffset,
248 (unsigned) imageWidth, (unsigned) imageHeight);
249 }
250 }
251
252 /*
253 * Draw the cascade indicator for the menu button on the
254 * right side of the window, if desired.
255 */
256
257 if (mbPtr->indicatorOn) {
258 int borderWidth;
259
260 borderWidth = (mbPtr->indicatorHeight+1)/3;
261 if (borderWidth < 1) {
262 borderWidth = 1;
263 }
264 /*y += mbPtr->textHeight / 2;*/
265 Tk_Fill3DRectangle(tkwin, pixmap, border,
266 Tk_Width(tkwin) - mbPtr->inset - mbPtr->indicatorWidth
267 + mbPtr->indicatorHeight,
268 ((int) (Tk_Height(tkwin) - mbPtr->indicatorHeight))/2,
269 mbPtr->indicatorWidth - 2*mbPtr->indicatorHeight,
270 mbPtr->indicatorHeight, borderWidth, TK_RELIEF_RAISED);
271 }
272
273 /*
274 * Draw the border and traversal highlight last. This way, if the
275 * menu button's contents overflow onto the border they'll be covered
276 * up by the border.
277 */
278
279 if (mbPtr->relief != TK_RELIEF_FLAT) {
280 Tk_Draw3DRectangle(tkwin, pixmap, border,
281 mbPtr->highlightWidth, mbPtr->highlightWidth,
282 Tk_Width(tkwin) - 2*mbPtr->highlightWidth,
283 Tk_Height(tkwin) - 2*mbPtr->highlightWidth,
284 mbPtr->borderWidth, mbPtr->relief);
285 }
286 if (mbPtr->highlightWidth != 0) {
287 GC gc;
288
289 if (mbPtr->flags & GOT_FOCUS) {
290 gc = Tk_GCForColor(mbPtr->highlightColorPtr, pixmap);
291 } else {
292 gc = Tk_GCForColor(mbPtr->highlightBgColorPtr, pixmap);
293 }
294 Tk_DrawFocusHighlight(tkwin, gc, mbPtr->highlightWidth, pixmap);
295 }
296
297 /*
298 * Copy the information from the off-screen pixmap onto the screen,
299 * then delete the pixmap.
300 */
301
302 XCopyArea(mbPtr->display, pixmap, Tk_WindowId(tkwin),
303 mbPtr->normalTextGC, 0, 0, (unsigned) Tk_Width(tkwin),
304 (unsigned) Tk_Height(tkwin), 0, 0);
305 Tk_FreePixmap(mbPtr->display, pixmap);
306 }
307
308 /*
309 *----------------------------------------------------------------------
310 *
311 * TkpDestroyMenuButton --
312 *
313 * Free data structures associated with the menubutton control.
314 *
315 * Results:
316 * None.
317 *
318 * Side effects:
319 * Restores the default control state.
320 *
321 *----------------------------------------------------------------------
322 */
323
324 void
325 TkpDestroyMenuButton(mbPtr)
326 TkMenuButton *mbPtr;
327 {
328 }
329
330 /*
331 *----------------------------------------------------------------------
332 *
333 * TkpComputeMenuButtonGeometry --
334 *
335 * After changes in a menu button's text or bitmap, this procedure
336 * recomputes the menu button's geometry and passes this information
337 * along to the geometry manager for the window.
338 *
339 * Results:
340 * None.
341 *
342 * Side effects:
343 * The menu button's window may change size.
344 *
345 *----------------------------------------------------------------------
346 */
347
348 void
349 TkpComputeMenuButtonGeometry(mbPtr)
350 TkMenuButton *mbPtr; /* Widget record for menu button. */
351 {
352 int width, height, mm, pixels;
353 int avgWidth, txtWidth, txtHeight;
354 int haveImage = 0, haveText = 0;
355 Tk_FontMetrics fm;
356
357 mbPtr->inset = mbPtr->highlightWidth + mbPtr->borderWidth;
358
359 width = 0;
360 height = 0;
361 txtWidth = 0;
362 txtHeight = 0;
363 avgWidth = 0;
364
365 if (mbPtr->image != None) {
366 Tk_SizeOfImage(mbPtr->image, &width, &height);
367 haveImage = 1;
368 } else if (mbPtr->bitmap != None) {
369 Tk_SizeOfBitmap(mbPtr->display, mbPtr->bitmap, &width, &height);
370 haveImage = 1;
371 }
372
373 if (haveImage == 0 || mbPtr->compound != COMPOUND_NONE) {
374 Tk_FreeTextLayout(mbPtr->textLayout);
375
376 mbPtr->textLayout = Tk_ComputeTextLayout(mbPtr->tkfont, mbPtr->text,
377 -1, mbPtr->wrapLength, mbPtr->justify, 0, &mbPtr->textWidth,
378 &mbPtr->textHeight);
379 txtWidth = mbPtr->textWidth;
380 txtHeight = mbPtr->textHeight;
381 avgWidth = Tk_TextWidth(mbPtr->tkfont, "0", 1);
382 Tk_GetFontMetrics(mbPtr->tkfont, &fm);
383 haveText = (txtWidth != 0 && txtHeight != 0);
384 }
385
386 /*
387 * If the menubutton is compound (ie, it shows both an image and text),
388 * the new geometry is a combination of the image and text geometry.
389 * We only honor the compound bit if the menubutton has both text and
390 * an image, because otherwise it is not really a compound menubutton.
391 */
392
393 if (mbPtr->compound != COMPOUND_NONE && haveImage && haveText) {
394 switch ((enum compound) mbPtr->compound) {
395 case COMPOUND_TOP:
396 case COMPOUND_BOTTOM: {
397 /* Image is above or below text */
398 height += txtHeight + mbPtr->padY;
399 width = (width > txtWidth ? width : txtWidth);
400 break;
401 }
402 case COMPOUND_LEFT:
403 case COMPOUND_RIGHT: {
404 /* Image is left or right of text */
405 width += txtWidth + mbPtr->padX;
406 height = (height > txtHeight ? height : txtHeight);
407 break;
408 }
409 case COMPOUND_CENTER: {
410 /* Image and text are superimposed */
411 width = (width > txtWidth ? width : txtWidth);
412 height = (height > txtHeight ? height : txtHeight);
413 break;
414 }
415 case COMPOUND_NONE: {break;}
416 }
417 if (mbPtr->width > 0) {
418 width = mbPtr->width;
419 }
420 if (mbPtr->height > 0) {
421 height = mbPtr->height;
422 }
423 width += 2*mbPtr->padX;
424 height += 2*mbPtr->padY;
425 } else {
426 if (haveImage) {
427 if (mbPtr->width > 0) {
428 width = mbPtr->width;
429 }
430 if (mbPtr->height > 0) {
431 height = mbPtr->height;
432 }
433 } else {
434 width = txtWidth;
435 height = txtHeight;
436 if (mbPtr->width > 0) {
437 width = mbPtr->width * avgWidth;
438 }
439 if (mbPtr->height > 0) {
440 height = mbPtr->height * fm.linespace;
441 }
442 }
443 }
444
445 if (! haveImage) {
446 width += 2*mbPtr->padX;
447 height += 2*mbPtr->padY;
448 }
449
450 if (mbPtr->indicatorOn) {
451 mm = WidthMMOfScreen(Tk_Screen(mbPtr->tkwin));
452 pixels = WidthOfScreen(Tk_Screen(mbPtr->tkwin));
453 mbPtr->indicatorHeight= (INDICATOR_HEIGHT * pixels)/(10*mm);
454 mbPtr->indicatorWidth = (INDICATOR_WIDTH * pixels)/(10*mm)
455 + 2*mbPtr->indicatorHeight;
456 width += mbPtr->indicatorWidth;
457 } else {
458 mbPtr->indicatorHeight = 0;
459 mbPtr->indicatorWidth = 0;
460 }
461
462 Tk_GeometryRequest(mbPtr->tkwin, (int) (width + 2*mbPtr->inset),
463 (int) (height + 2*mbPtr->inset));
464 Tk_SetInternalBorder(mbPtr->tkwin, mbPtr->inset);
465 }