Debian Codegraph @

pTk/mTk/generic/tkMessage.c

raw symbols SCIP
1 /*
2 * tkMessage.c --
3 *
4 * This module implements a message widgets for the Tk
5 * toolkit. A message widget displays a multi-line string
6 * in a window according to a particular aspect ratio.
7 *
8 * Copyright (c) 1990-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 * Copyright (c) 1998-2000 by Ajuba Solutions.
11 *
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 *
15 * RCS: @(#) $Id: tkMessage.c,v 1.14 2002/08/05 04:30:40 dgp Exp $
16 */
17
18 #include "tkPort.h"
19 #include "default.h"
20 #include "tkInt.h"
21 #include "tkVMacro.h"
22
23 /*
24 * A data structure of the following type is kept for each message
25 * widget managed by this file:
26 */
27
28 typedef struct {
29 Tk_Window tkwin; /* Window that embodies the message. NULL
30 * means that the window has been destroyed
31 * but the data structures haven't yet been
32 * cleaned up.*/
33 Tk_OptionTable optionTable; /* Table that defines options available for
34 * this widget. */
35 Display *display; /* Display containing widget. Used, among
36 * other things, so that resources can be
37 * freed even after tkwin has gone away. */
38 Tcl_Interp *interp; /* Interpreter associated with message. */
39 Tcl_Command widgetCmd; /* Token for message's widget command. */
40
41 /*
42 * Information used when displaying widget:
43 */
44
45 char *string; /* String displayed in message. */
46 int numChars; /* Number of characters in string, not
47 * including terminating NULL character. */
48 Var textVarName; /* Name of variable (malloc'ed) or NULL.
49 * If non-NULL, message displays the contents
50 * of this variable. */
51 Tk_3DBorder border; /* Structure used to draw 3-D border and
52 * background. NULL means a border hasn't
53 * been created yet. */
54 int borderWidth; /* Width of border. */
55 int relief; /* 3-D effect: TK_RELIEF_RAISED, etc. */
56 int highlightWidth; /* Width in pixels of highlight to draw
57 * around widget when it has the focus.
58 * <= 0 means don't draw a highlight. */
59 XColor *highlightBgColorPtr;
60 /* Color for drawing traversal highlight
61 * area when highlight is off. */
62 XColor *highlightColorPtr; /* Color for drawing traversal highlight. */
63 Tk_Font tkfont; /* Information about text font, or NULL. */
64 XColor *fgColorPtr; /* Foreground color in normal mode. */
65 Tcl_Obj *padXPtr, *padYPtr; /* Tcl_Obj rep's of padX, padY values. */
66 int padX, padY; /* User-requested extra space around text. */
67 int width; /* User-requested width, in pixels. 0 means
68 * compute width using aspect ratio below. */
69 int aspect; /* Desired aspect ratio for window
70 * (100*width/height). */
71 int msgWidth; /* Width in pixels needed to display
72 * message. */
73 int msgHeight; /* Height in pixels needed to display
74 * message. */
75 Tk_Anchor anchor; /* Where to position text within window region
76 * if window is larger or smaller than
77 * needed. */
78 Tk_Justify justify; /* Justification for text. */
79
80 GC textGC; /* GC for drawing text in normal mode. */
81 Tk_TextLayout textLayout; /* Saved layout information. */
82
83 /*
84 * Miscellaneous information:
85 */
86
87 Tk_Cursor cursor; /* Current cursor for window, or None. */
88 char *takeFocus; /* Value of -takefocus option; not used in
89 * the C code, but used by keyboard traversal
90 * scripts. Malloc'ed, but may be NULL. */
91 int flags; /* Various flags; see below for
92 * definitions. */
93 } Message;
94
95 /*
96 * Flag bits for messages:
97 *
98 * REDRAW_PENDING: Non-zero means a DoWhenIdle handler
99 * has already been queued to redraw
100 * this window.
101 * GOT_FOCUS: Non-zero means this button currently
102 * has the input focus.
103 * MESSAGE_DELETED: The message has been effectively deleted.
104 */
105
106 #define REDRAW_PENDING 1
107 #define GOT_FOCUS 4
108 #define MESSAGE_DELETED 8
109
110 /*
111 * Custom option for handling "-tile"
112 */
113
114 static Tk_CustomOption tileOption = {
115 Tk_TileParseProc,
116 Tk_TilePrintProc,
117 (ClientData) NULL
118 };
119
120 /*
121 * Information used for argv parsing.
122 */
123
124 static Tk_OptionSpec optionSpecs[] = {
125 {TK_OPTION_ANCHOR, "-anchor", "anchor", "Anchor", DEF_MESSAGE_ANCHOR,
126 -1, Tk_Offset(Message, anchor), 0, 0, 0},
127 {TK_OPTION_INT, "-aspect", "aspect", "Aspect", DEF_MESSAGE_ASPECT,
128 -1, Tk_Offset(Message, aspect), 0, 0, 0},
129 {TK_OPTION_BORDER, "-background", "background", "Background",
130 DEF_MESSAGE_BG_COLOR, -1, Tk_Offset(Message, border), 0,
131 (ClientData) DEF_MESSAGE_BG_MONO, 0},
132 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL, (char *) NULL,
133 0, -1, 0, (ClientData) "-borderwidth", 0},
134 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL, (char *) NULL,
135 0, -1, 0, (ClientData) "-background", 0},
136 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
137 DEF_MESSAGE_BORDER_WIDTH, -1,
138 Tk_Offset(Message, borderWidth), 0, 0, 0},
139 {TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
140 DEF_MESSAGE_CURSOR, -1, Tk_Offset(Message, cursor),
141 TK_OPTION_NULL_OK, 0, 0},
142 {TK_OPTION_SYNONYM, "-fg", (char *) NULL, (char *) NULL, (char *) NULL,
143 0, -1, 0, (ClientData) "-foreground", 0},
144 {TK_OPTION_FONT, "-font", "font", "Font",
145 DEF_MESSAGE_FONT, -1, Tk_Offset(Message, tkfont), 0, 0, 0},
146 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
147 DEF_MESSAGE_FG, -1, Tk_Offset(Message, fgColorPtr), 0, 0, 0},
148 {TK_OPTION_COLOR, "-highlightbackground", "highlightBackground",
149 "HighlightBackground", DEF_MESSAGE_HIGHLIGHT_BG, -1,
150 Tk_Offset(Message, highlightBgColorPtr), 0, 0},
151 {TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
152 DEF_MESSAGE_HIGHLIGHT, -1, Tk_Offset(Message, highlightColorPtr),
153 0, 0, 0},
154 {TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
155 "HighlightThickness", DEF_MESSAGE_HIGHLIGHT_WIDTH, -1,
156 Tk_Offset(Message, highlightWidth), 0, 0, 0},
157 {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
158 DEF_MESSAGE_JUSTIFY, -1, Tk_Offset(Message, justify), 0, 0, 0},
159 {TK_OPTION_PIXELS, "-padx", "padX", "Pad",
160 DEF_MESSAGE_PADX, Tk_Offset(Message, padXPtr),
161 Tk_Offset(Message, padX), 0, 0, 0},
162 {TK_OPTION_PIXELS, "-pady", "padY", "Pad",
163 DEF_MESSAGE_PADY, Tk_Offset(Message, padYPtr),
164 Tk_Offset(Message, padY), 0, 0, 0},
165 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
166 DEF_MESSAGE_RELIEF, -1, Tk_Offset(Message, relief), 0, 0, 0},
167 {TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
168 DEF_MESSAGE_TAKE_FOCUS, -1, Tk_Offset(Message, takeFocus),
169 TK_OPTION_NULL_OK, 0, 0},
170 {TK_OPTION_STRING, "-text", "text", "Text",
171 DEF_MESSAGE_TEXT, -1, Tk_Offset(Message, string), 0, 0, 0},
172 {TK_OPTION_OBJ, "-textvariable", "textVariable", "Variable",
173 DEF_MESSAGE_TEXT_VARIABLE, -1, Tk_Offset(Message, textVarName),
174 TK_OPTION_NULL_OK, 0, 0},
175 {TK_OPTION_PIXELS, "-width", "width", "Width",
176 DEF_MESSAGE_WIDTH, -1, Tk_Offset(Message, width), 0, 0 ,0},
177 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
178 (char *) NULL, 0, 0, 0, 0}
179 };
180
181 /*
182 * Forward declarations for procedures defined later in this file:
183 */
184
185 static void MessageCmdDeletedProc _ANSI_ARGS_((
186 ClientData clientData));
187 static void MessageEventProc _ANSI_ARGS_((ClientData clientData,
188 XEvent *eventPtr));
189 static char * MessageTextVarProc _ANSI_ARGS_((ClientData clientData,
190 Tcl_Interp *interp, Var name1,
191 CONST char *name2, int flags));
192 static int MessageWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
193 Tcl_Interp *interp, int objc,
194 Tcl_Obj *CONST objv[]));
195 static void MessageWorldChanged _ANSI_ARGS_((
196 ClientData instanceData));
197 static void ComputeMessageGeometry _ANSI_ARGS_((Message *msgPtr));
198 static int ConfigureMessage _ANSI_ARGS_((Tcl_Interp *interp,
199 Message *msgPtr, int objc, Tcl_Obj *CONST objv[],
200 int flags));
201 static void DestroyMessage _ANSI_ARGS_((char *memPtr));
202 static void DisplayMessage _ANSI_ARGS_((ClientData clientData));
203
204 /*
205 * The structure below defines message class behavior by means of procedures
206 * that can be invoked from generic window code.
207 */
208
209 static Tk_ClassProcs messageClass = {
210 sizeof(Tk_ClassProcs), /* size */
211 MessageWorldChanged, /* worldChangedProc */
212 };
213
214
215 /*
216 *--------------------------------------------------------------
217 *
218 * Tk_MessageObjCmd --
219 *
220 * This procedure is invoked to process the "message" Tcl
221 * command. See the user documentation for details on what
222 * it does.
223 *
224 * Results:
225 * A standard Tcl result.
226 *
227 * Side effects:
228 * See the user documentation.
229 *
230 *--------------------------------------------------------------
231 */
232
233 int
234 Tk_MessageObjCmd(clientData, interp, objc, objv)
235 ClientData clientData; /* NULL. */
236 Tcl_Interp *interp; /* Current interpreter. */
237 int objc; /* Number of arguments. */
238 Tcl_Obj *CONST objv[]; /* Argument strings. */
239 {
240 register Message *msgPtr;
241 Tk_OptionTable optionTable;
242 Tk_Window tkwin;
243
244 if (objc < 2) {
245 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
246 return TCL_ERROR;
247 }
248
249 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
250 Tcl_GetString(objv[1]), (char *) NULL);
251 if (tkwin == NULL) {
252 return TCL_ERROR;
253 }
254
255 /*
256 * Create the option table for this widget class. If it has already
257 * been created, the cached pointer will be returned.
258 */
259
260 optionTable = Tk_CreateOptionTable(interp, optionSpecs);
261
262 msgPtr = (Message *) ckalloc(sizeof(Message));
263 memset(msgPtr, 0, (size_t) sizeof(Message));
264
265 /*
266 * Set values for those fields that don't take a 0 or NULL value.
267 */
268 msgPtr->tkwin = tkwin;
269 msgPtr->display = Tk_Display(tkwin);
270 msgPtr->interp = interp;
271 msgPtr->widgetCmd = Tcl_CreateObjCommand(interp,
272 Tk_PathName(msgPtr->tkwin), MessageWidgetObjCmd,
273 (ClientData) msgPtr, MessageCmdDeletedProc);
274 msgPtr->optionTable = optionTable;
275 msgPtr->relief = TK_RELIEF_FLAT;
276 msgPtr->textGC = None;
277 msgPtr->anchor = TK_ANCHOR_CENTER;
278 msgPtr->aspect = 150;
279 msgPtr->justify = TK_JUSTIFY_LEFT;
280 msgPtr->cursor = None;
281
282 Tk_SetClass(msgPtr->tkwin, "Message");
283 Tk_SetClassProcs(msgPtr->tkwin, &messageClass, (ClientData) msgPtr);
284 Tk_CreateEventHandler(msgPtr->tkwin,
285 ExposureMask|StructureNotifyMask|FocusChangeMask,
286 MessageEventProc, (ClientData) msgPtr);
287 if (Tk_InitOptions(interp, (char *)msgPtr, optionTable, tkwin) != TCL_OK) {
288 Tk_DestroyWindow(msgPtr->tkwin);
289 return TCL_ERROR;
290 }
291
292 if (ConfigureMessage(interp, msgPtr, objc-2, objv+2, 0) != TCL_OK) {
293 Tk_DestroyWindow(msgPtr->tkwin);
294 return TCL_ERROR;
295 }
296
297 Tcl_SetResult(interp, Tk_PathName(msgPtr->tkwin), TCL_STATIC);
298 return TCL_OK;
299 }
300
301 /*
302 *--------------------------------------------------------------
303 *
304 * MessageWidgetObjCmd --
305 *
306 * This procedure is invoked to process the Tcl command
307 * that corresponds to a widget managed by this module.
308 * See the user documentation for details on what it does.
309 *
310 * Results:
311 * A standard Tcl result.
312 *
313 * Side effects:
314 * See the user documentation.
315 *
316 *--------------------------------------------------------------
317 */
318
319 static int
320 MessageWidgetObjCmd(clientData, interp, objc, objv)
321 ClientData clientData; /* Information about message widget. */
322 Tcl_Interp *interp; /* Current interpreter. */
323 int objc; /* Number of arguments. */
324 Tcl_Obj *CONST objv[]; /* Argument strings. */
325 {
326 register Message *msgPtr = (Message *) clientData;
327 static CONST char *optionStrings[] = { "cget", "configure", (char *) NULL };
328 enum options { MESSAGE_CGET, MESSAGE_CONFIGURE };
329 int index;
330 int result = TCL_OK;
331 Tcl_Obj *objPtr;
332
333 if (objc < 2) {
334 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
335 return TCL_ERROR;
336 }
337
338 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
339 &index) != TCL_OK) {
340 return TCL_ERROR;
341 }
342
343 Tcl_Preserve((ClientData) msgPtr);
344
345 switch ((enum options) index) {
346 case MESSAGE_CGET: {
347 if (objc != 3) {
348 Tcl_WrongNumArgs(interp, 2, objv, "option");
349 return TCL_ERROR;
350 }
351 objPtr = Tk_GetOptionValue(interp, (char *) msgPtr,
352 msgPtr->optionTable, objv[2], msgPtr->tkwin);
353 if (objPtr == NULL) {
354 result = TCL_ERROR;
355 } else {
356 Tcl_SetObjResult(interp, objPtr);
357 result = TCL_OK;
358 }
359 break;
360 }
361 case MESSAGE_CONFIGURE: {
362 if (objc <= 3) {
363 objPtr = Tk_GetOptionInfo(interp, (char *) msgPtr,
364 msgPtr->optionTable,
365 (objc == 3) ? objv[2] : (Tcl_Obj *) NULL,
366 msgPtr->tkwin);
367 if (objPtr == NULL) {
368 result = TCL_ERROR;
369 } else {
370 Tcl_SetObjResult(interp, objPtr);
371 result = TCL_OK;
372 }
373 } else {
374 result = ConfigureMessage(interp, msgPtr, objc-2, objv+2, 0);
375 }
376 break;
377 }
378 }
379
380 Tcl_Release((ClientData) msgPtr);
381 return result;
382 }
383
384 /*
385 *----------------------------------------------------------------------
386 *
387 * DestroyMessage --
388 *
389 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
390 * to clean up the internal structure of a message at a safe time
391 * (when no-one is using it anymore).
392 *
393 * Results:
394 * None.
395 *
396 * Side effects:
397 * Everything associated with the message is freed up.
398 *
399 *----------------------------------------------------------------------
400 */
401
402 static void
403 DestroyMessage(memPtr)
404 char *memPtr; /* Info about message widget. */
405 {
406 register Message *msgPtr = (Message *) memPtr;
407
408 msgPtr->flags |= MESSAGE_DELETED;
409
410 Tcl_DeleteCommandFromToken(msgPtr->interp, msgPtr->widgetCmd);
411 if (msgPtr->flags & REDRAW_PENDING) {
412 Tcl_CancelIdleCall(DisplayMessage, (ClientData) msgPtr);
413 }
414
415 /*
416 * Free up all the stuff that requires special handling, then
417 * let Tk_FreeConfigOptions handle all the standard option-related
418 * stuff.
419 */
420
421 if (msgPtr->textGC != None) {
422 Tk_FreeGC(msgPtr->display, msgPtr->textGC);
423 }
424 if (msgPtr->textLayout != NULL) {
425 Tk_FreeTextLayout(msgPtr->textLayout);
426 }
427 if (msgPtr->textVarName != NULL) {
428 Lang_UntraceVar(msgPtr->interp, msgPtr->textVarName,
429 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
430 MessageTextVarProc, (ClientData) msgPtr);
431 }
432 Tk_FreeConfigOptions((char *) msgPtr, msgPtr->optionTable, msgPtr->tkwin);
433 msgPtr->tkwin = NULL;
434 ckfree((char *) msgPtr);
435 }
436
437 /*
438 *----------------------------------------------------------------------
439 *
440 * ConfigureMessage --
441 *
442 * This procedure is called to process an argv/argc list, plus
443 * the Tk option database, in order to configure (or
444 * reconfigure) a message widget.
445 *
446 * Results:
447 * The return value is a standard Tcl result. If TCL_ERROR is
448 * returned, then the interp's result contains an error message.
449 *
450 * Side effects:
451 * Configuration information, such as text string, colors, font,
452 * etc. get set for msgPtr; old resources get freed, if there
453 * were any.
454 *
455 *----------------------------------------------------------------------
456 */
457
458 static int
459 ConfigureMessage(interp, msgPtr, objc, objv, flags)
460 Tcl_Interp *interp; /* Used for error reporting. */
461 register Message *msgPtr; /* Information about widget; may or may
462 * not already have values for some fields. */
463 int objc; /* Number of valid entries in argv. */
464 Tcl_Obj *CONST objv[]; /* Arguments. */
465 int flags; /* Flags to pass to Tk_ConfigureWidget. */
466 {
467 Tk_SavedOptions savedOptions;
468
469 /*
470 * Eliminate any existing trace on a variable monitored by the message.
471 */
472
473 if (msgPtr->textVarName != NULL) {
474 Lang_UntraceVar(interp, msgPtr->textVarName,
475 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
476 MessageTextVarProc, (ClientData) msgPtr);
477 }
478
479 if (Tk_SetOptions(interp, (char *) msgPtr, msgPtr->optionTable, objc, objv,
480 msgPtr->tkwin, &savedOptions, (int *)NULL) != TCL_OK) {
481 Tk_RestoreSavedOptions(&savedOptions);
482 return TCL_ERROR;
483 }
484
485
486 /*
487 * If the message is to display the value of a variable, then set up
488 * a trace on the variable's value, create the variable if it doesn't
489 * exist, and fetch its current value.
490 */
491
492 if (msgPtr->textVarName != NULL) {
493 CONST char *value;
494
495 value = Tcl_GetString(Tcl_ObjGetVar2(interp, msgPtr->textVarName, NULL, TCL_GLOBAL_ONLY));
496 if (value == NULL) {
497 Tcl_Obj *temp = Tcl_NewStringObj(msgPtr->string,-1);
498 Tcl_ObjSetVar2(interp, msgPtr->textVarName, NULL, temp,
499 TCL_GLOBAL_ONLY);
500 Tcl_DecrRefCount(temp);
501 } else {
502 if (msgPtr->string != NULL) {
503 ckfree(msgPtr->string);
504 }
505 msgPtr->string = strcpy(ckalloc(strlen(value) + 1), value);
506 }
507 Lang_TraceVar(interp, msgPtr->textVarName,
508 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
509 MessageTextVarProc, (ClientData) msgPtr);
510 }
511
512 /*
513 * A few other options need special processing, such as setting
514 * the background from a 3-D border or handling special defaults
515 * that couldn't be specified to Tk_ConfigureWidget.
516 */
517
518 msgPtr->numChars = Tcl_NumUtfChars(msgPtr->string, -1);
519
520 if (msgPtr->highlightWidth < 0) {
521 msgPtr->highlightWidth = 0;
522 }
523
524 Tk_FreeSavedOptions(&savedOptions);
525 MessageWorldChanged((ClientData) msgPtr);
526 return TCL_OK;
527 }
528
529 /*
530 *---------------------------------------------------------------------------
531 *
532 * MessageWorldChanged --
533 *
534 * This procedure is called when the world has changed in some
535 * way and the widget needs to recompute all its graphics contexts
536 * and determine its new geometry.
537 *
538 * Results:
539 * None.
540 *
541 * Side effects:
542 * Message will be relayed out and redisplayed.
543 *
544 *---------------------------------------------------------------------------
545 */
546
547 static void
548 MessageWorldChanged(instanceData)
549 ClientData instanceData; /* Information about widget. */
550 {
551 XGCValues gcValues;
552 GC gc = None;
553 Tk_FontMetrics fm;
554 Message *msgPtr;
555
556 msgPtr = (Message *) instanceData;
557
558 if (msgPtr->border != NULL) {
559 Tk_SetBackgroundFromBorder(msgPtr->tkwin, msgPtr->border);
560 }
561
562 gcValues.font = Tk_FontId(msgPtr->tkfont);
563 gcValues.foreground = msgPtr->fgColorPtr->pixel;
564 gc = Tk_GetGC(msgPtr->tkwin, GCForeground | GCFont, &gcValues);
565 if (msgPtr->textGC != None) {
566 Tk_FreeGC(msgPtr->display, msgPtr->textGC);
567 }
568 msgPtr->textGC = gc;
569
570 Tk_GetFontMetrics(msgPtr->tkfont, &fm);
571 if (msgPtr->padX < 0) {
572 msgPtr->padX = fm.ascent / 2;
573 }
574 if (msgPtr->padY == -1) {
575 msgPtr->padY = fm.ascent / 4;
576 }
577
578 /*
579 * Recompute the desired geometry for the window, and arrange for
580 * the window to be redisplayed.
581 */
582
583 ComputeMessageGeometry(msgPtr);
584 if ((msgPtr->tkwin != NULL) && Tk_IsMapped(msgPtr->tkwin)
585 && !(msgPtr->flags & REDRAW_PENDING)) {
586 Tcl_DoWhenIdle(DisplayMessage, (ClientData) msgPtr);
587 msgPtr->flags |= REDRAW_PENDING;
588 }
589 }
590
591 /*
592 *--------------------------------------------------------------
593 *
594 * ComputeMessageGeometry --
595 *
596 * Compute the desired geometry for a message window,
597 * taking into account the desired aspect ratio for the
598 * window.
599 *
600 * Results:
601 * None.
602 *
603 * Side effects:
604 * Tk_GeometryRequest is called to inform the geometry
605 * manager of the desired geometry for this window.
606 *
607 *--------------------------------------------------------------
608 */
609
610 static void
611 ComputeMessageGeometry(msgPtr)
612 register Message *msgPtr; /* Information about window. */
613 {
614 int width, inc, height;
615 int thisWidth, thisHeight, maxWidth;
616 int aspect, lowerBound, upperBound, inset;
617
618 Tk_FreeTextLayout(msgPtr->textLayout);
619
620 inset = msgPtr->borderWidth + msgPtr->highlightWidth;
621
622 /*
623 * Compute acceptable bounds for the final aspect ratio.
624 */
625
626 aspect = msgPtr->aspect/10;
627 if (aspect < 5) {
628 aspect = 5;
629 }
630 lowerBound = msgPtr->aspect - aspect;
631 upperBound = msgPtr->aspect + aspect;
632
633 /*
634 * Do the computation in multiple passes: start off with
635 * a very wide window, and compute its height. Then change
636 * the width and try again. Reduce the size of the change
637 * and iterate until dimensions are found that approximate
638 * the desired aspect ratio. Or, if the user gave an explicit
639 * width then just use that.
640 */
641
642 if (msgPtr->width > 0) {
643 width = msgPtr->width;
644 inc = 0;
645 } else {
646 width = WidthOfScreen(Tk_Screen(msgPtr->tkwin))/2;
647 inc = width/2;
648 }
649
650 for ( ; ; inc /= 2) {
651 msgPtr->textLayout = Tk_ComputeTextLayout(msgPtr->tkfont,
652 msgPtr->string, msgPtr->numChars, width, msgPtr->justify,
653 0, &thisWidth, &thisHeight);
654 maxWidth = thisWidth + 2 * (inset + msgPtr->padX);
655 height = thisHeight + 2 * (inset + msgPtr->padY);
656
657 if (inc <= 2) {
658 break;
659 }
660 aspect = (100 * maxWidth) / height;
661
662 if (aspect < lowerBound) {
663 width += inc;
664 } else if (aspect > upperBound) {
665 width -= inc;
666 } else {
667 break;
668 }
669 Tk_FreeTextLayout(msgPtr->textLayout);
670 }
671 msgPtr->msgWidth = thisWidth;
672 msgPtr->msgHeight = thisHeight;
673 Tk_GeometryRequest(msgPtr->tkwin, maxWidth, height);
674 Tk_SetInternalBorder(msgPtr->tkwin, inset);
675 }
676
677 /*
678 *--------------------------------------------------------------
679 *
680 * DisplayMessage --
681 *
682 * This procedure redraws the contents of a message window.
683 *
684 * Results:
685 * None.
686 *
687 * Side effects:
688 * Information appears on the screen.
689 *
690 *--------------------------------------------------------------
691 */
692
693 static void
694 DisplayMessage(clientData)
695 ClientData clientData; /* Information about window. */
696 {
697 register Message *msgPtr = (Message *) clientData;
698 register Tk_Window tkwin = msgPtr->tkwin;
699 int x, y;
700 int borderWidth = msgPtr->highlightWidth;
701
702 msgPtr->flags &= ~REDRAW_PENDING;
703 if ((msgPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
704 return;
705 }
706 if (msgPtr->border != NULL) {
707 borderWidth += msgPtr->borderWidth;
708 }
709 if (msgPtr->relief == TK_RELIEF_FLAT) {
710 borderWidth = msgPtr->highlightWidth;
711 }
712 Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin), msgPtr->border,
713 borderWidth, borderWidth,
714 Tk_Width(tkwin) - 2 * borderWidth,
715 Tk_Height(tkwin) - 2 * borderWidth,
716 0, TK_RELIEF_FLAT);
717
718 /*
719 * Compute starting y-location for message based on message size
720 * and anchor option.
721 */
722
723 TkComputeAnchor(msgPtr->anchor, tkwin, msgPtr->padX, msgPtr->padY,
724 msgPtr->msgWidth, msgPtr->msgHeight, &x, &y);
725 Tk_DrawTextLayout(Tk_Display(tkwin), Tk_WindowId(tkwin), msgPtr->textGC,
726 msgPtr->textLayout, x, y, 0, -1);
727
728 if (borderWidth > msgPtr->highlightWidth) {
729 Tk_Draw3DRectangle(tkwin, Tk_WindowId(tkwin), msgPtr->border,
730 msgPtr->highlightWidth, msgPtr->highlightWidth,
731 Tk_Width(tkwin) - 2*msgPtr->highlightWidth,
732 Tk_Height(tkwin) - 2*msgPtr->highlightWidth,
733 msgPtr->borderWidth, msgPtr->relief);
734 }
735 if (msgPtr->highlightWidth != 0) {
736 GC fgGC, bgGC;
737
738 bgGC = Tk_GCForColor(msgPtr->highlightBgColorPtr, Tk_WindowId(tkwin));
739 if (msgPtr->flags & GOT_FOCUS) {
740 fgGC = Tk_GCForColor(msgPtr->highlightColorPtr, Tk_WindowId(tkwin));
741 TkpDrawHighlightBorder(tkwin, fgGC, bgGC, msgPtr->highlightWidth,
742 Tk_WindowId(tkwin));
743 } else {
744 TkpDrawHighlightBorder(tkwin, bgGC, bgGC, msgPtr->highlightWidth,
745 Tk_WindowId(tkwin));
746 }
747 }
748 }
749
750 /*
751 *--------------------------------------------------------------
752 *
753 * MessageEventProc --
754 *
755 * This procedure is invoked by the Tk dispatcher for various
756 * events on messages.
757 *
758 * Results:
759 * None.
760 *
761 * Side effects:
762 * When the window gets deleted, internal structures get
763 * cleaned up. When it gets exposed, it is redisplayed.
764 *
765 *--------------------------------------------------------------
766 */
767
768 static void
769 MessageEventProc(clientData, eventPtr)
770 ClientData clientData; /* Information about window. */
771 XEvent *eventPtr; /* Information about event. */
772 {
773 Message *msgPtr = (Message *) clientData;
774
775 if (((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0))
776 || (eventPtr->type == ConfigureNotify)) {
777 goto redraw;
778 } else if (eventPtr->type == DestroyNotify) {
779 DestroyMessage((char *) clientData);
780 } else if (eventPtr->type == FocusIn) {
781 if (eventPtr->xfocus.detail != NotifyInferior) {
782 msgPtr->flags |= GOT_FOCUS;
783 if (msgPtr->highlightWidth > 0) {
784 goto redraw;
785 }
786 }
787 } else if (eventPtr->type == FocusOut) {
788 if (eventPtr->xfocus.detail != NotifyInferior) {
789 msgPtr->flags &= ~GOT_FOCUS;
790 if (msgPtr->highlightWidth > 0) {
791 goto redraw;
792 }
793 }
794 }
795 return;
796
797 redraw:
798 if ((msgPtr->tkwin != NULL) && !(msgPtr->flags & REDRAW_PENDING)) {
799 Tcl_DoWhenIdle(DisplayMessage, (ClientData) msgPtr);
800 msgPtr->flags |= REDRAW_PENDING;
801 }
802 }
803
804 /*
805 *----------------------------------------------------------------------
806 *
807 * MessageCmdDeletedProc --
808 *
809 * This procedure is invoked when a widget command is deleted. If
810 * the widget isn't already in the process of being destroyed,
811 * this command destroys it.
812 *
813 * Results:
814 * None.
815 *
816 * Side effects:
817 * The widget is destroyed.
818 *
819 *----------------------------------------------------------------------
820 */
821
822 static void
823 MessageCmdDeletedProc(clientData)
824 ClientData clientData; /* Pointer to widget record for widget. */
825 {
826 Message *msgPtr = (Message *) clientData;
827
828 /*
829 * This procedure could be invoked either because the window was
830 * destroyed and the command was then deleted (in which case tkwin
831 * is NULL) or because the command was deleted, and then this procedure
832 * destroys the widget.
833 */
834
835 if (!(msgPtr->flags & MESSAGE_DELETED)) {
836 Tk_DestroyWindow(msgPtr->tkwin);
837 }
838 }
839
840 /*
841 *--------------------------------------------------------------
842 *
843 * MessageTextVarProc --
844 *
845 * This procedure is invoked when someone changes the variable
846 * whose contents are to be displayed in a message.
847 *
848 * Results:
849 * NULL is always returned.
850 *
851 * Side effects:
852 * The text displayed in the message will change to match the
853 * variable.
854 *
855 *--------------------------------------------------------------
856 */
857
858 /* ARGSUSED */
859 static char *
860 MessageTextVarProc(clientData, interp, name1, name2, flags)
861 ClientData clientData; /* Information about message. */
862 Tcl_Interp *interp; /* Interpreter containing variable. */
863 Var name1; /* Name of variable. */
864 CONST char *name2; /* Second part of variable name. */
865 int flags; /* Information about what happened. */
866 {
867 register Message *msgPtr = (Message *) clientData;
868 CONST char *value;
869
870 /*
871 * If the variable is unset, then immediately recreate it unless
872 * the whole interpreter is going away.
873 */
874
875 if (flags & TCL_TRACE_UNSETS) {
876 if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
877 Tcl_Obj *temp = Tcl_NewStringObj(msgPtr->string,-1);
878 Tcl_ObjSetVar2(interp, msgPtr->textVarName, NULL, temp,
879 TCL_GLOBAL_ONLY);
880 Tcl_DecrRefCount(temp);
881 Lang_TraceVar(interp, msgPtr->textVarName,
882 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
883 MessageTextVarProc, clientData);
884 }
885 return (char *) NULL;
886 }
887
888 value = Tcl_GetString(Tcl_ObjGetVar2(interp, msgPtr->textVarName, NULL, TCL_GLOBAL_ONLY));
889 if (value == NULL) {
890 value = "";
891 }
892 if (msgPtr->string != NULL) {
893 ckfree(msgPtr->string);
894 }
895 msgPtr->numChars = Tcl_NumUtfChars(value, -1);
896 msgPtr->string = (char *) ckalloc((unsigned) (strlen(value) + 1));
897 strcpy(msgPtr->string, value);
898 ComputeMessageGeometry(msgPtr);
899
900 if ((msgPtr->tkwin != NULL) && Tk_IsMapped(msgPtr->tkwin)
901 && !(msgPtr->flags & REDRAW_PENDING)) {
902 Tcl_DoWhenIdle(DisplayMessage, (ClientData) msgPtr);
903 msgPtr->flags |= REDRAW_PENDING;
904 }
905 return (char *) NULL;
906 }
907
908