Debian Codegraph @

pTk/mTk/unix/tkUnixWm.c

raw symbols SCIP
1 /*
2 * tkUnixWm.c --
3 *
4 * This module takes care of the interactions between a Tk-based
5 * application and the window manager. Among other things, it implements
6 * the "wm" command and passes geometry information to the window
7 * manager.
8 *
9 * Copyright (c) 1991-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 *
12 * See the file "license.terms" for information on usage and redistribution of
13 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 *
15 * RCS: @(#) $Id: tkUnixWm.c,v 1.51 2006/05/29 22:21:40 hobbs Exp $
16 */
17
18 #include "tkPort.h"
19 #include "tkInt.h"
20 #include "tkUnixInt.h"
21
22 /*
23 * A data structure of the following type holds information for each window
24 * manager protocol (such as WM_DELETE_WINDOW) for which a handler (i.e. a Tcl
25 * command) has been defined for a particular top-level window.
26 */
27
28 typedef struct ProtocolHandler {
29 Atom protocol; /* Identifies the protocol. */
30 struct ProtocolHandler *nextPtr;
31 /* Next in list of protocol handlers for the
32 * same top-level window, or NULL for end of
33 * list. */
34 Tcl_Interp *interp; /* Interpreter in which to invoke command. */
35 #if 0
36 char command[4]; /* Tcl command to invoke when a client message
37 * for this protocol arrives. The actual size
38 * of the structure varies to accommodate the
39 * needs of the actual command. THIS MUST BE
40 * THE LAST FIELD OF THE STRUCTURE. */
41 #else
42 LangCallback *command;
43 #endif
44 } ProtocolHandler;
45
46 static void ProtocolFree _ANSI_ARGS_((char *clientData));
47
48 static void
49 ProtocolFree(clientData)
50 char *clientData;
51 {ProtocolHandler *p = (ProtocolHandler *) clientData;
52 LangFreeCallback(p->command);
53 ckfree((char *) p);
54 }
55
56 /*
57 * Data for [wm attributes] command:
58 */
59 typedef struct {
60 double alpha; /* Transparency; 0.0=transparent, 1.0=opaque */
61 int topmost; /* Flag: true=>stay-on-top */
62 int zoomed; /* Flag: true=>maximized */
63 int fullscreen; /* Flag: true=>fullscreen */
64 } WmAttributes;
65
66 typedef enum {
67 WMATT_ALPHA, WMATT_TOPMOST, WMATT_ZOOMED, WMATT_FULLSCREEN,
68 _WMATT_LAST_ATTRIBUTE
69 } WmAttribute;
70
71 static const char *WmAttributeNames[] = {
72 "-alpha", "-topmost", "-zoomed", "-fullscreen",
73 NULL
74 };
75
76 /*
77 * A data structure of the following type holds window-manager-related
78 * information for each top-level window in an application.
79 */
80
81 typedef struct TkWmInfo {
82 TkWindow *winPtr; /* Pointer to main Tk information for this
83 * window. */
84 Window reparent; /* If the window has been reparented, this
85 * gives the ID of the ancestor of the window
86 * that is a child of the root window (may not
87 * be window's immediate parent). If the
88 * window isn't reparented, this has the value
89 * None. */
90 char *title; /* Title to display in window caption. If
91 * NULL, use name of widget. Malloced. */
92 char *iconName; /* Name to display in icon. Malloced. */
93 XWMHints hints; /* Various pieces of information for window
94 * manager. */
95 char *leaderName; /* Path name of leader of window group
96 * (corresponds to hints.window_group).
97 * Malloc-ed. Note: this field doesn't get
98 * updated if leader is destroyed. */
99 TkWindow *masterPtr; /* Master window for TRANSIENT_FOR property,
100 * or NULL. */
101 Tk_Window icon; /* Window to use as icon for this window, or
102 * NULL. */
103 Tk_Image iconImage; /* Image used to generate Icon - or NULL */
104 Tk_Window iconFor; /* Window for which this window is icon, or
105 * NULL if this isn't an icon for anyone. */
106 int withdrawn; /* Non-zero means window has been withdrawn. */
107
108 /*
109 * In order to support menubars transparently under X, each toplevel
110 * window is encased in an additional window, called the wrapper, that
111 * holds the toplevel and the menubar, if any. The information below is
112 * used to keep track of the wrapper and the menubar.
113 */
114
115 TkWindow *wrapperPtr; /* Pointer to information about the wrapper.
116 * This is the "real" toplevel window as seen
117 * by the window manager. Although this is an
118 * official Tk window, it doesn't appear in
119 * the application's window hierarchy. NULL
120 * means that the wrapper hasn't been created
121 * yet. */
122 Tk_Window menubar; /* Pointer to information about the menubar,
123 * or NULL if there is no menubar for this
124 * toplevel. */
125 int menuHeight; /* Amount of vertical space needed for
126 * menubar, measured in pixels. If menubar is
127 * non-NULL, this is >= 1 (X servers don't
128 * like dimensions of 0). */
129
130 /*
131 * Information used to construct an XSizeHints structure for the window
132 * manager:
133 */
134
135 int sizeHintsFlags; /* Flags word for XSizeHints structure. If the
136 * PBaseSize flag is set then the window is
137 * gridded; otherwise it isn't gridded. */
138 int minWidth, minHeight; /* Minimum dimensions of window, in pixels or
139 * grid units. */
140 int maxWidth, maxHeight; /* Maximum dimensions of window, in pixels or
141 * grid units. 0 to default.*/
142 Tk_Window gridWin; /* Identifies the window that controls
143 * gridding for this top-level, or NULL if the
144 * top-level isn't currently gridded. */
145 int widthInc, heightInc; /* Increments for size changes (# pixels per
146 * step). */
147 struct {
148 int x; /* numerator */
149 int y; /* denominator */
150 } minAspect, maxAspect; /* Min/max aspect ratios for window. */
151 int reqGridWidth, reqGridHeight;
152 /* The dimensions of the window (in grid
153 * units) requested through the geometry
154 * manager. */
155 int gravity; /* Desired window gravity. */
156
157 /*
158 * Information used to manage the size and location of a window.
159 */
160
161 int width, height; /* Desired dimensions of window, specified in
162 * pixels or grid units. These values are set
163 * by the "wm geometry" command and by
164 * ConfigureNotify events (for when wm resizes
165 * window). -1 means user hasn't requested
166 * dimensions. */
167 int x, y; /* Desired X and Y coordinates for window.
168 * These values are set by "wm geometry", plus
169 * by ConfigureNotify events (when wm moves
170 * window). These numbers are different than
171 * the numbers stored in winPtr->changes
172 * because (a) they could be measured from the
173 * right or bottom edge of the screen (see
174 * WM_NEGATIVE_X and WM_NEGATIVE_Y flags) and
175 * (b) if the window has been reparented then
176 * they refer to the parent rather than the
177 * window itself. */
178 int parentWidth, parentHeight;
179 /* Width and height of reparent, in pixels
180 * *including border*. If window hasn't been
181 * reparented then these will be the outer
182 * dimensions of the window, including
183 * border. */
184 int xInParent, yInParent; /* Offset of wrapperPtr within reparent,
185 * measured in pixels from upper-left outer
186 * corner of reparent's border to upper-left
187 * outer corner of wrapperPtr's border. If not
188 * reparented then these are zero. */
189 int configWidth, configHeight;
190 /* Dimensions passed to last request that we
191 * issued to change geometry of the wrapper.
192 * Used to eliminate redundant resize
193 * operations. */
194
195 /*
196 * Information about the virtual root window for this top-level, if there
197 * is one.
198 */
199
200 Window vRoot; /* Virtual root window for this top-level, or
201 * None if there is no virtual root window
202 * (i.e. just use the screen's root). */
203 int vRootX, vRootY; /* Position of the virtual root inside the
204 * root window. If the WM_VROOT_OFFSET_STALE
205 * flag is set then this information may be
206 * incorrect and needs to be refreshed from
207 * the X server. If vRoot is None then these
208 * values are both 0. */
209 int vRootWidth, vRootHeight;/* Dimensions of the virtual root window. If
210 * vRoot is None, gives the dimensions of the
211 * containing screen. This information is
212 * never stale, even though vRootX and vRootY
213 * can be. */
214
215 /*
216 * Miscellaneous information.
217 */
218
219 WmAttributes attributes; /* Current state of [wm attributes] */
220 WmAttributes reqState; /* Requested state of [wm attributes] */
221 ProtocolHandler *protPtr; /* First in list of protocol handlers for this
222 * window (NULL means none). */
223 #ifndef _LANG
224 int cmdArgc; /* Number of elements in cmdArgv below. */
225 CONST char **cmdArgv; /* Array of strings to store in the WM_COMMAND
226 * property. NULL means nothing available. */
227 #else
228 Tcl_Obj *cmdArg; /* Object holding list - to keep strings live */
229 #endif
230 char *clientMachine; /* String to store in WM_CLIENT_MACHINE
231 * property, or NULL. */
232 int flags; /* Miscellaneous flags, defined below. */
233 int numTransients; /* number of transients on this window */
234 int iconDataSize; /* size of iconphoto image data */
235 unsigned char *iconDataPtr; /* iconphoto image data, if set */
236 struct TkWmInfo *nextPtr; /* Next in list of all top-level windows. */
237 } WmInfo;
238
239 /*
240 * Flag values for WmInfo structures:
241 *
242 * WM_NEVER_MAPPED - non-zero means window has never been mapped;
243 * need to update all info when window is first
244 * mapped.
245 * WM_UPDATE_PENDING - non-zero means a call to UpdateGeometryInfo
246 * has already been scheduled for this window;
247 * no need to schedule another one.
248 * WM_NEGATIVE_X - non-zero means x-coordinate is measured in
249 * pixels from right edge of screen, rather than
250 * from left edge.
251 * WM_NEGATIVE_Y - non-zero means y-coordinate is measured in
252 * pixels up from bottom of screen, rather than
253 * down from top.
254 * WM_UPDATE_SIZE_HINTS - non-zero means that new size hints need to be
255 * propagated to window manager.
256 * WM_SYNC_PENDING - set to non-zero while waiting for the window
257 * manager to respond to some state change.
258 * WM_VROOT_OFFSET_STALE - non-zero means that (x,y) offset information
259 * about the virtual root window is stale and
260 * needs to be fetched fresh from the X server.
261 * WM_ABOUT_TO_MAP - non-zero means that the window is about to be
262 * mapped by TkWmMapWindow. This is used by
263 * UpdateGeometryInfo to modify its behavior.
264 * WM_MOVE_PENDING - non-zero means the application has requested a
265 * new position for the window, but it hasn't
266 * been reflected through the window manager yet.
267 * WM_COLORMAPS_EXPLICIT - non-zero means the colormap windows were set
268 * explicitly via "wm colormapwindows".
269 * WM_ADDED_TOPLEVEL_COLORMAP - non-zero means that when "wm colormapwindows"
270 * was called the top-level itself wasn't
271 * specified, so we added it implicitly at the
272 * end of the list.
273 * WM_WIDTH_NOT_RESIZABLE - non-zero means that we're not supposed to
274 * allow the user to change the width of the
275 * window (controlled by "wm resizable" command).
276 * WM_HEIGHT_NOT_RESIZABLE - non-zero means that we're not supposed to
277 * allow the user to change the height of the
278 * window (controlled by "wm resizable" command).
279 * WM_WITHDRAWN - non-zero means that this window has explicitly
280 * been withdrawn. If it's a transient, it should
281 * not mirror state changes in the master.
282 */
283
284 #define WM_NEVER_MAPPED 1
285 #define WM_UPDATE_PENDING 2
286 #define WM_NEGATIVE_X 4
287 #define WM_NEGATIVE_Y 8
288 #define WM_UPDATE_SIZE_HINTS 0x10
289 #define WM_SYNC_PENDING 0x20
290 #define WM_VROOT_OFFSET_STALE 0x40
291 #define WM_ABOUT_TO_MAP 0x100
292 #define WM_MOVE_PENDING 0x200
293 #define WM_COLORMAPS_EXPLICIT 0x400
294 #define WM_ADDED_TOPLEVEL_COLORMAP 0x800
295 #define WM_WIDTH_NOT_RESIZABLE 0x1000
296 #define WM_HEIGHT_NOT_RESIZABLE 0x2000
297 #define WM_WITHDRAWN 0x4000
298
299 /*
300 * This module keeps a list of all top-level windows, primarily to simplify
301 * the job of Tk_CoordsToWindow. The list is called firstWmPtr and is stored
302 * in the TkDisplay structure.
303 */
304
305 /*
306 * The following structures are the official type records for geometry
307 * management of top-level and menubar windows.
308 */
309
310 static void TopLevelReqProc(ClientData dummy, Tk_Window tkwin);
311 static void TopLevelLostSlaveProc _ANSI_ARGS_((ClientData clientData,
312 Tk_Window tkwin));
313
314
315 static Tk_GeomMgr wmMgrType = {
316 "wm", /* name */
317 TopLevelReqProc, /* requestProc */
318 TopLevelLostSlaveProc, /* lostSlaveProc */
319 };
320 /*
321 * Forward declarations for procedures defined in this file:
322 */
323
324 static void TkWmFreeCmd _ANSI_ARGS_((WmInfo *wmPtr));
325
326 static void ImageChangedProc _ANSI_ARGS_((ClientData clientData,
327 int x, int y, int width, int height, int imageWidth,
328 int imageHeight));
329
330 static void MenubarReqProc _ANSI_ARGS_((ClientData clientData,
331 Tk_Window tkwin));
332
333 static Tk_GeomMgr menubarMgrType = {
334 "menubar", /* name */
335 MenubarReqProc, /* requestProc */
336 NULL, /* lostSlaveProc */
337 };
338
339 /*
340 * Structures of the following type are used for communication between
341 * WaitForEvent, WaitRestrictProc, and WaitTimeoutProc.
342 */
343
344 typedef struct WaitRestrictInfo {
345 Display *display; /* Window belongs to this display. */
346 WmInfo *wmInfoPtr;
347 int type; /* We only care about this type of event. */
348 XEvent *eventPtr; /* Where to store the event when it's found. */
349 int foundEvent; /* Non-zero means that an event of the desired
350 * type has been found. */
351 } WaitRestrictInfo;
352
353 /*
354 * Forward declarations for functions defined in this file:
355 */
356
357 static int ComputeReparentGeometry _ANSI_ARGS_((WmInfo *wmPtr));
358 static void ConfigureEvent _ANSI_ARGS_((WmInfo *wmPtr,
359 XConfigureEvent *eventPtr));
360 static void CreateWrapper _ANSI_ARGS_((WmInfo *wmPtr));
361 static void GetMaxSize _ANSI_ARGS_((WmInfo *wmPtr,
362 int *maxWidthPtr, int *maxHeightPtr));
363 static void MenubarDestroyProc _ANSI_ARGS_((ClientData clientData,
364 XEvent *eventPtr));
365 static int ParseGeometry _ANSI_ARGS_((Tcl_Interp *interp,
366 char *string, TkWindow *winPtr));
367 static void ReparentEvent _ANSI_ARGS_((WmInfo *wmPtr,
368 XReparentEvent *eventPtr));
369 static void TkWmStackorderToplevelWrapperMap _ANSI_ARGS_((
370 TkWindow *winPtr,
371 Display *display,
372 Tcl_HashTable *reparentTable));
373 static void TopLevelEventProc _ANSI_ARGS_((ClientData clientData,
374 XEvent *eventPtr));
375 static void TopLevelReqProc _ANSI_ARGS_((ClientData dummy,
376 Tk_Window tkwin));
377 static void UpdateCommand _ANSI_ARGS_((TkWindow *winPtr));
378 static void UpdateGeometryInfo _ANSI_ARGS_((
379 ClientData clientData));
380 static void UpdateHints _ANSI_ARGS_((TkWindow *winPtr));
381 static void UpdateSizeHints _ANSI_ARGS_((TkWindow *winPtr,
382 int newWidth,
383 int newHeight));
384 static void UpdateTitle(TkWindow *winPtr);
385 static void UpdatePhotoIcon(TkWindow *winPtr);
386 static void UpdateVRootGeometry _ANSI_ARGS_((WmInfo *wmPtr));
387 static void UpdateWmProtocols _ANSI_ARGS_((WmInfo *wmPtr));
388 static void SetNetWmState(TkWindow*, const char *atomName, int on);
389 static void CheckNetWmState(WmInfo *, Atom *atoms, int numAtoms);
390 static void UpdateNetWmState(WmInfo *);
391 static void WaitForConfigureNotify _ANSI_ARGS_((TkWindow *winPtr,
392 unsigned long serial));
393 static int WaitForEvent _ANSI_ARGS_((Display *display,
394 WmInfo *wmInfoPtr, int type, XEvent *eventPtr));
395 static void WaitForMapNotify _ANSI_ARGS_((TkWindow *winPtr,
396 int mapped));
397
398 static void UnmanageGeometry _ANSI_ARGS_((Tk_Window tkwin));
399
400 static int WmProtocolCmd(Tk_Window tkwin, TkWindow *winPtr,
401 Tcl_Interp *interp, int objc,
402 Tcl_Obj *CONST objv[]);
403 static int WmResizableCmd(Tk_Window tkwin, TkWindow *winPtr,
404 Tcl_Interp *interp, int objc,
405 Tcl_Obj *CONST objv[]);
406 static int WmSizefromCmd(Tk_Window tkwin, TkWindow *winPtr,
407 Tcl_Interp *interp, int objc,
408 Tcl_Obj *CONST objv[]);
409 static int WmStackorderCmd(Tk_Window tkwin, TkWindow *winPtr,
410 Tcl_Interp *interp, int objc,
411 Tcl_Obj *CONST objv[]);
412 static int WmStateCmd(Tk_Window tkwin, TkWindow *winPtr,
413 Tcl_Interp *interp, int objc,
414 Tcl_Obj *CONST objv[]);
415 static int WmTitleCmd(Tk_Window tkwin, TkWindow *winPtr,
416 Tcl_Interp *interp, int objc,
417 Tcl_Obj *CONST objv[]);
418 static int WmTransientCmd(Tk_Window tkwin, TkWindow *winPtr,
419 Tcl_Interp *interp, int objc,
420 Tcl_Obj *CONST objv[]);
421 static int WmWithdrawCmd(Tk_Window tkwin, TkWindow *winPtr,
422 Tcl_Interp *interp, int objc,
423 Tcl_Obj *CONST objv[]);
424 static void WmUpdateGeom(WmInfo *wmPtr, TkWindow *winPtr);
425 static Tk_RestrictAction
426 WaitRestrictProc _ANSI_ARGS_((ClientData clientData,
427 XEvent *eventPtr));
428 static void WrapperEventProc _ANSI_ARGS_((ClientData clientData,
429 XEvent *eventPtr));
430 static void WmWaitMapProc _ANSI_ARGS_((
431 ClientData clientData, XEvent *eventPtr));
432
433 static int WmAspectCmd _ANSI_ARGS_((Tk_Window tkwin,
434 TkWindow *winPtr, Tcl_Interp *interp, int objc,
435 Tcl_Obj *CONST objv[]));
436 static int WmAttributesCmd _ANSI_ARGS_((Tk_Window tkwin,
437 TkWindow *winPtr, Tcl_Interp *interp, int objc,
438 Tcl_Obj *CONST objv[]));
439 static int WmCaptureCmd _ANSI_ARGS_((Tk_Window tkwin,
440 TkWindow *winPtr, Tcl_Interp *interp, int objc,
441 Tcl_Obj *CONST objv[]));
442 static int WmClientCmd _ANSI_ARGS_((Tk_Window tkwin,
443 TkWindow *winPtr, Tcl_Interp *interp, int objc,
444 Tcl_Obj *CONST objv[]));
445 static int WmColormapwindowsCmd _ANSI_ARGS_((Tk_Window tkwin,
446 TkWindow *winPtr, Tcl_Interp *interp, int objc,
447 Tcl_Obj *CONST objv[]));
448 static int WmCommandCmd _ANSI_ARGS_((Tk_Window tkwin,
449 TkWindow *winPtr, Tcl_Interp *interp, int objc,
450 Tcl_Obj *CONST objv[]));
451 static int WmDeiconifyCmd _ANSI_ARGS_((Tk_Window tkwin,
452 TkWindow *winPtr, Tcl_Interp *interp, int objc,
453 Tcl_Obj *CONST objv[]));
454 static int WmFocusmodelCmd _ANSI_ARGS_((Tk_Window tkwin,
455 TkWindow *winPtr, Tcl_Interp *interp, int objc,
456 Tcl_Obj *CONST objv[]));
457 static int WmFrameCmd _ANSI_ARGS_((Tk_Window tkwin,
458 TkWindow *winPtr, Tcl_Interp *interp, int objc,
459 Tcl_Obj *CONST objv[]));
460 static int WmGeometryCmd _ANSI_ARGS_((Tk_Window tkwin,
461 TkWindow *winPtr, Tcl_Interp *interp, int objc,
462 Tcl_Obj *CONST objv[]));
463 static int WmGridCmd _ANSI_ARGS_((Tk_Window tkwin,
464 TkWindow *winPtr, Tcl_Interp *interp, int objc,
465 Tcl_Obj *CONST objv[]));
466 static int WmGroupCmd _ANSI_ARGS_((Tk_Window tkwin,
467 TkWindow *winPtr, Tcl_Interp *interp, int objc,
468 Tcl_Obj *CONST objv[]));
469 static int WmIconbitmapCmd _ANSI_ARGS_((Tk_Window tkwin,
470 TkWindow *winPtr, Tcl_Interp *interp, int objc,
471 Tcl_Obj *CONST objv[]));
472 static int WmIconifyCmd _ANSI_ARGS_((Tk_Window tkwin,
473 TkWindow *winPtr, Tcl_Interp *interp, int objc,
474 Tcl_Obj *CONST objv[]));
475 static int WmIconimageCmd _ANSI_ARGS_((Tk_Window tkwin,
476 TkWindow *winPtr, Tcl_Interp *interp, int objc,
477 Tcl_Obj *CONST objv[]));
478 static int WmIconmaskCmd _ANSI_ARGS_((Tk_Window tkwin,
479 TkWindow *winPtr, Tcl_Interp *interp, int objc,
480 Tcl_Obj *CONST objv[]));
481 static int WmIconnameCmd _ANSI_ARGS_((Tk_Window tkwin,
482 TkWindow *winPtr, Tcl_Interp *interp, int objc,
483 Tcl_Obj *CONST objv[]));
484 static int WmIconphotoCmd(Tk_Window tkwin, TkWindow *winPtr,
485 Tcl_Interp *interp, int objc,
486 Tcl_Obj *CONST objv[]);
487 static int WmIconpositionCmd _ANSI_ARGS_((Tk_Window tkwin,
488 TkWindow *winPtr, Tcl_Interp *interp, int objc,
489 Tcl_Obj *CONST objv[]));
490 static int WmIconwindowCmd _ANSI_ARGS_((Tk_Window tkwin,
491 TkWindow *winPtr, Tcl_Interp *interp, int objc,
492 Tcl_Obj *CONST objv[]));
493 static int WmMaxsizeCmd _ANSI_ARGS_((Tk_Window tkwin,
494 TkWindow *winPtr, Tcl_Interp *interp, int objc,
495 Tcl_Obj *CONST objv[]));
496 static int WmMinsizeCmd _ANSI_ARGS_((Tk_Window tkwin,
497 TkWindow *winPtr, Tcl_Interp *interp, int objc,
498 Tcl_Obj *CONST objv[]));
499 static int WmOverrideredirectCmd _ANSI_ARGS_((Tk_Window tkwin,
500 TkWindow *winPtr, Tcl_Interp *interp, int objc,
501 Tcl_Obj *CONST objv[]));
502 static int WmPositionfromCmd _ANSI_ARGS_((Tk_Window tkwin,
503 TkWindow *winPtr, Tcl_Interp *interp, int objc,
504 Tcl_Obj *CONST objv[]));
505 static int WmProtocolCmd _ANSI_ARGS_((Tk_Window tkwin,
506 TkWindow *winPtr, Tcl_Interp *interp, int objc,
507 Tcl_Obj *CONST objv[]));
508 static int WmReleaseCmd _ANSI_ARGS_((Tk_Window tkwin,
509 TkWindow *winPtr, Tcl_Interp *interp, int objc,
510 Tcl_Obj *CONST objv[]));
511 static int WmResizableCmd _ANSI_ARGS_((Tk_Window tkwin,
512 TkWindow *winPtr, Tcl_Interp *interp, int objc,
513 Tcl_Obj *CONST objv[]));
514 static int WmSizefromCmd _ANSI_ARGS_((Tk_Window tkwin,
515 TkWindow *winPtr, Tcl_Interp *interp, int objc,
516 Tcl_Obj *CONST objv[]));
517 static int WmStackorderCmd _ANSI_ARGS_((Tk_Window tkwin,
518 TkWindow *winPtr, Tcl_Interp *interp, int objc,
519 Tcl_Obj *CONST objv[]));
520 static int WmStateCmd _ANSI_ARGS_((Tk_Window tkwin,
521 TkWindow *winPtr, Tcl_Interp *interp, int objc,
522 Tcl_Obj *CONST objv[]));
523 static int WmTitleCmd _ANSI_ARGS_((Tk_Window tkwin,
524 TkWindow *winPtr, Tcl_Interp *interp, int objc,
525 Tcl_Obj *CONST objv[]));
526 static int WmTransientCmd _ANSI_ARGS_((Tk_Window tkwin,
527 TkWindow *winPtr, Tcl_Interp *interp, int objc,
528 Tcl_Obj *CONST objv[]));
529 static int WmWithdrawCmd _ANSI_ARGS_((Tk_Window tkwin,
530 TkWindow *winPtr, Tcl_Interp *interp, int objc,
531 Tcl_Obj *CONST objv[]));
532 static int WmWrapperCmd _ANSI_ARGS_((Tk_Window tkwin,
533 TkWindow *winPtr, Tcl_Interp *interp, int objc,
534 Tcl_Obj *CONST objv[]));
535 static void WmUpdateGeom _ANSI_ARGS_((WmInfo *wmPtr,
536 TkWindow *winPtr));
537
538 /*
539 *--------------------------------------------------------------
540 *
541 * TkWmCleanup --
542 *
543 * This function is invoked to cleanup remaining wm resources associated
544 * with a display.
545 *
546 * Results:
547 * None.
548 *
549 * Side effects:
550 * All WmInfo structure resources are freed and invalidated.
551 *
552 *--------------------------------------------------------------
553 */
554
555 static void
556 TkWmFreeCmd(wmPtr)
557 WmInfo *wmPtr;
558 {
559 if (wmPtr->cmdArg) {
560 Tcl_DecrRefCount(wmPtr->cmdArg);
561 wmPtr->cmdArg = NULL;
562 }
563 }
564
565 void TkWmCleanup(
566 TkDisplay *dispPtr)
567 {
568 WmInfo *wmPtr, *nextPtr;
569
570 for (wmPtr = dispPtr->firstWmPtr; wmPtr != NULL; wmPtr = nextPtr) {
571 /*
572 * We can't assume we have access to winPtr's anymore, so some cleanup
573 * requiring winPtr data is avoided.
574 */
575
576 nextPtr = wmPtr->nextPtr;
577 if (wmPtr->title != NULL) {
578 ckfree(wmPtr->title);
579 }
580 if (wmPtr->iconName != NULL) {
581 ckfree(wmPtr->iconName);
582 }
583 if (wmPtr->iconDataPtr != NULL) {
584 ckfree((char *)wmPtr->iconDataPtr);
585 }
586 if (wmPtr->leaderName != NULL) {
587 ckfree(wmPtr->leaderName);
588 }
589 if (wmPtr->menubar != NULL) {
590 Tk_DestroyWindow(wmPtr->menubar);
591 }
592 if (wmPtr->wrapperPtr != NULL) {
593 Tk_DestroyWindow((Tk_Window) wmPtr->wrapperPtr);
594 }
595 while (wmPtr->protPtr != NULL) {
596 ProtocolHandler *protPtr;
597
598 protPtr = wmPtr->protPtr;
599 wmPtr->protPtr = protPtr->nextPtr;
600 Tcl_EventuallyFree((ClientData) protPtr, ProtocolFree);
601 }
602 #ifdef _LANG
603 TkWmFreeCmd(wmPtr);
604 #else
605 if (wmPtr->cmdArgv != NULL) {
606 ckfree((char *) wmPtr->cmdArgv);
607 }
608 #endif
609 if (wmPtr->clientMachine != NULL) {
610 ckfree((char *) wmPtr->clientMachine);
611 }
612 ckfree((char *) wmPtr);
613 }
614 if (dispPtr->iconDataPtr != NULL) {
615 ckfree((char *)dispPtr->iconDataPtr);
616 dispPtr->iconDataPtr = NULL;
617 }
618 }
619
620 /*
621 *--------------------------------------------------------------
622 *
623 * TkWmNewWindow --
624 *
625 * This function is invoked whenever a new top-level window is created.
626 * Its job is to initialize the WmInfo structure for the window.
627 *
628 * Results:
629 * None.
630 *
631 * Side effects:
632 * A WmInfo structure gets allocated and initialized.
633 *
634 *--------------------------------------------------------------
635 */
636
637 void
638 TkWmNewWindow(
639 TkWindow *winPtr) /* Newly-created top-level window. */
640 {
641 register WmInfo *wmPtr;
642 TkDisplay *dispPtr = winPtr->dispPtr;
643
644 wmPtr = (WmInfo *) ckalloc(sizeof(WmInfo));
645 memset(wmPtr, 0, sizeof(WmInfo));
646 wmPtr->winPtr = winPtr;
647 wmPtr->reparent = None;
648 wmPtr->masterPtr = NULL;
649 wmPtr->numTransients = 0;
650 wmPtr->hints.flags = InputHint | StateHint;
651 wmPtr->hints.input = True;
652 wmPtr->hints.initial_state = NormalState;
653 wmPtr->hints.icon_pixmap = None;
654 wmPtr->hints.icon_window = None;
655 wmPtr->hints.icon_x = wmPtr->hints.icon_y = 0;
656 wmPtr->hints.icon_mask = None;
657 wmPtr->hints.window_group = None;
658
659 /*
660 * Initialize attributes.
661 */
662 wmPtr->attributes.alpha = 1.0;
663 wmPtr->attributes.topmost = 0;
664 wmPtr->attributes.zoomed = 0;
665 wmPtr->attributes.fullscreen = 0;
666 wmPtr->reqState = wmPtr->attributes;
667
668 /*
669 * Default the maximum dimensions to the size of the display, minus a
670 * guess about how space is needed for window manager decorations.
671 */
672
673 wmPtr->gridWin = NULL;
674 wmPtr->minWidth = wmPtr->minHeight = 1;
675 wmPtr->maxWidth = wmPtr->maxHeight = 0;
676 wmPtr->widthInc = wmPtr->heightInc = 1;
677 wmPtr->minAspect.x = wmPtr->minAspect.y = 1;
678 wmPtr->maxAspect.x = wmPtr->maxAspect.y = 1;
679 wmPtr->reqGridWidth = wmPtr->reqGridHeight = -1;
680 wmPtr->gravity = NorthWestGravity;
681 wmPtr->width = -1;
682 wmPtr->height = -1;
683 wmPtr->x = winPtr->changes.x;
684 wmPtr->y = winPtr->changes.y;
685 wmPtr->parentWidth = winPtr->changes.width
686 + 2*winPtr->changes.border_width;
687 wmPtr->parentHeight = winPtr->changes.height
688 + 2*winPtr->changes.border_width;
689 wmPtr->configWidth = -1;
690 wmPtr->configHeight = -1;
691 wmPtr->vRoot = None;
692 wmPtr->flags = WM_NEVER_MAPPED;
693 wmPtr->nextPtr = (WmInfo *) dispPtr->firstWmPtr;
694 dispPtr->firstWmPtr = wmPtr;
695 winPtr->wmInfoPtr = wmPtr;
696
697 UpdateVRootGeometry(wmPtr);
698
699 /*
700 * Arrange for geometry requests to be reflected from the window to the
701 * window manager.
702 */
703
704 Tk_ManageGeometry((Tk_Window) winPtr, &wmMgrType, (ClientData) 0);
705 }
706
707 /*
708 *--------------------------------------------------------------
709 *
710 * TkWmMapWindow --
711 *
712 * This function is invoked to map a top-level window. This module gets a
713 * chance to update all window-manager-related information in properties
714 * before the window manager sees the map event and checks the
715 * properties. It also gets to decide whether or not to even map the
716 * window after all.
717 *
718 * Results:
719 * None.
720 *
721 * Side effects:
722 * Properties of winPtr may get updated to provide up-to-date information
723 * to the window manager. The window may also get mapped, but it may not
724 * be if this function decides that isn't appropriate (e.g. because the
725 * window is withdrawn).
726 *
727 *--------------------------------------------------------------
728 */
729
730 void
731 TkWmMapWindow(
732 TkWindow *winPtr) /* Top-level window that's about to be
733 * mapped. */
734 {
735 register WmInfo *wmPtr = winPtr->wmInfoPtr;
736 XTextProperty textProp;
737
738 if (wmPtr->flags & WM_NEVER_MAPPED) {
739 Tcl_DString ds;
740
741 wmPtr->flags &= ~WM_NEVER_MAPPED;
742
743 /*
744 * This is the first time this window has ever been mapped. First
745 * create the wrapper window that provides space for a menubar.
746 */
747
748 if (wmPtr->wrapperPtr == NULL) {
749 CreateWrapper(wmPtr);
750 }
751
752 /*
753 * Store all the window-manager-related information for the window.
754 */
755
756 #if 0
757 REMOVED >>>> ORIGINAL tkUnixWm.c#1
758 if (XStringListToTextProperty(&(Tcl_DStringValue(&ds)), 1,
759 &textProp) != 0) {
760 XSetWMName(winPtr->display, wmPtr->wrapperPtr->window, &textProp);
761 XFree((char *) textProp.value);
762 }
763 Tcl_DStringFree(&ds);
764
765 REMOVED ==== THEIRS tkUnixWm.c#3
766 REMOVED ==== YOURS tkUnixWm.c
767 if (XStringListToTextProperty((char **)&string, 1,
768 &textProp) != 0) {
769 XSetWMName(winPtr->display, wmPtr->wrapperPtr->window, &textProp);
770 XFree((char *) textProp.value);
771 }
772 Tcl_DStringFree(&ds);
773
774 REMOVED <<<<
775 #endif
776 TkWmSetClass(winPtr);
777 UpdateTitle(winPtr);
778 UpdatePhotoIcon(winPtr);
779
780 if (wmPtr->masterPtr != NULL) {
781 /*
782 * Don't map a transient if the master is not mapped.
783 */
784
785 if (!Tk_IsMapped(wmPtr->masterPtr)) {
786 wmPtr->withdrawn = 1;
787 wmPtr->hints.initial_state = WithdrawnState;
788 } else {
789 XSetTransientForHint(winPtr->display,
790 wmPtr->wrapperPtr->window,
791 wmPtr->masterPtr->wmInfoPtr->wrapperPtr->window);
792 }
793 }
794
795 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
796 UpdateHints(winPtr);
797 UpdateWmProtocols(wmPtr);
798 if (wmPtr->cmdArg != NULL) {
799 UpdateCommand(winPtr);
800 }
801 if (wmPtr->clientMachine != NULL) {
802 char *temp;
803 Tcl_UtfToExternalDString(NULL, wmPtr->clientMachine, -1, &ds);
804 temp = Tcl_DStringValue(&ds);
805 if (XStringListToTextProperty(&temp, 1,
806 &textProp) != 0) {
807 XSetWMClientMachine(winPtr->display, wmPtr->wrapperPtr->window,
808 &textProp);
809 XFree((char *) textProp.value);
810 }
811 Tcl_DStringFree(&ds);
812 }
813 }
814 if (wmPtr->hints.initial_state == WithdrawnState) {
815 return;
816 }
817 if (wmPtr->iconFor != NULL) {
818 /*
819 * This window is an icon for somebody else. Make sure that the
820 * geometry is up-to-date, then return without mapping the window.
821 */
822
823 if (wmPtr->flags & WM_UPDATE_PENDING) {
824 Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
825 }
826 UpdateGeometryInfo((ClientData) winPtr);
827 return;
828 }
829 wmPtr->flags |= WM_ABOUT_TO_MAP;
830 if (wmPtr->flags & WM_UPDATE_PENDING) {
831 Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
832 }
833 UpdateGeometryInfo((ClientData) winPtr);
834 wmPtr->flags &= ~WM_ABOUT_TO_MAP;
835
836 /*
837 * Update _NET_WM_STATE hints:
838 */
839 UpdateNetWmState(wmPtr);
840
841 /*
842 * Map the window, then wait to be sure that the window manager has
843 * processed the map operation.
844 */
845
846 XMapWindow(winPtr->display, wmPtr->wrapperPtr->window);
847 if (wmPtr->hints.initial_state == NormalState) {
848 WaitForMapNotify(winPtr, 1);
849 }
850 }
851
852 /*
853 *--------------------------------------------------------------
854 *
855 * TkWmUnmapWindow --
856 *
857 * This function is invoked to unmap a top-level window. The only thing
858 * it does special is to wait for the window actually to be unmapped.
859 *
860 * Results:
861 * None.
862 *
863 * Side effects:
864 * Unmaps the window.
865 *
866 *--------------------------------------------------------------
867 */
868
869 void
870 TkWmUnmapWindow(
871 TkWindow *winPtr) /* Top-level window that's about to be
872 * mapped. */
873 {
874 /*
875 * It seems to be important to wait after unmapping a top-level window
876 * until the window really gets unmapped. I don't completely understand
877 * all the interactions with the window manager, but if we go on without
878 * waiting, and if the window is then mapped again quickly, events seem to
879 * get lost so that we think the window isn't mapped when in fact it is
880 * mapped. I suspect that this has something to do with the window manager
881 * filtering Map events (and possily not filtering Unmap events?).
882 */
883
884 XUnmapWindow(winPtr->display, winPtr->wmInfoPtr->wrapperPtr->window);
885 WaitForMapNotify(winPtr, 0);
886 }
887
888
889 /*
890 *--------------------------------------------------------------
891 *
892 * TkWmDeadWindow --
893 *
894 * This function is invoked when a top-level window is about to be
895 * deleted. It cleans up the wm-related data structures for the window.
896 *
897 * Results:
898 * None.
899 *
900 * Side effects:
901 * The WmInfo structure for winPtr gets freed up.
902 *
903 *--------------------------------------------------------------
904 */
905
906
907 void
908 TkWmDeadWindow(
909 TkWindow *winPtr) /* Top-level window that's being deleted. */
910 {
911 register WmInfo *wmPtr = winPtr->wmInfoPtr;
912 WmInfo *wmPtr2;
913
914 if (wmPtr == NULL) {
915 return;
916 }
917 if ((WmInfo *) winPtr->dispPtr->firstWmPtr == wmPtr) {
918 winPtr->dispPtr->firstWmPtr = wmPtr->nextPtr;
919 } else {
920 register WmInfo *prevPtr;
921
922 for (prevPtr = (WmInfo *) winPtr->dispPtr->firstWmPtr; ;
923 prevPtr = prevPtr->nextPtr) {
924 if (prevPtr == NULL) {
925 Tcl_Panic("couldn't unlink window in TkWmDeadWindow");
926 }
927 if (prevPtr->nextPtr == wmPtr) {
928 prevPtr->nextPtr = wmPtr->nextPtr;
929 break;
930 }
931 }
932 }
933 if (wmPtr->title != NULL) {
934 ckfree(wmPtr->title);
935 }
936 if (wmPtr->iconName != NULL) {
937 ckfree(wmPtr->iconName);
938 }
939 if (wmPtr->iconDataPtr != NULL) {
940 ckfree((char *)wmPtr->iconDataPtr);
941 }
942
943 if (wmPtr->hints.icon_pixmap) {
944 if (wmPtr->iconImage) {
945 Tk_FreePixmap(winPtr->display, wmPtr->hints.icon_pixmap);
946 } else {
947 Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
948 }
949 }
950
951 if (wmPtr->iconImage) {
952 Tk_FreeImage(wmPtr->iconImage);
953 }
954
955 if (wmPtr->hints.flags & IconMaskHint) {
956 Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_mask);
957 }
958 if (wmPtr->leaderName != NULL) {
959 ckfree(wmPtr->leaderName);
960 }
961 if (wmPtr->icon != NULL) {
962 wmPtr2 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
963 wmPtr2->iconFor = NULL;
964 wmPtr2->withdrawn = 1;
965 }
966 if (wmPtr->iconFor != NULL) {
967 wmPtr2 = ((TkWindow *) wmPtr->iconFor)->wmInfoPtr;
968 wmPtr2->icon = NULL;
969 wmPtr2->hints.flags &= ~IconWindowHint;
970 UpdateHints((TkWindow *) wmPtr->iconFor);
971 }
972 if (wmPtr->menubar != NULL) {
973 Tk_DestroyWindow(wmPtr->menubar);
974 }
975 if (wmPtr->wrapperPtr != NULL) {
976 /*
977 * The rest of Tk doesn't know that we reparent the toplevel inside
978 * the wrapper, so reparent it back out again before deleting the
979 * wrapper; otherwise the toplevel will get deleted twice (once
980 * implicitly by the deletion of the wrapper).
981 */
982
983 XUnmapWindow(winPtr->display, winPtr->window);
984 XReparentWindow(winPtr->display, winPtr->window,
985 XRootWindow(winPtr->display, winPtr->screenNum), 0, 0);
986 Tk_DestroyWindow((Tk_Window) wmPtr->wrapperPtr);
987 }
988 while (wmPtr->protPtr != NULL) {
989 ProtocolHandler *protPtr;
990
991 protPtr = wmPtr->protPtr;
992 wmPtr->protPtr = protPtr->nextPtr;
993 Tcl_EventuallyFree((ClientData) protPtr, ProtocolFree);
994 }
995 #ifdef _LANG
996 TkWmFreeCmd(wmPtr);
997 #else
998 if (wmPtr->cmdArgv != NULL) {
999 ckfree((char *) wmPtr->cmdArgv);
1000 }
1001 #endif
1002 if (wmPtr->clientMachine != NULL) {
1003 ckfree((char *) wmPtr->clientMachine);
1004 }
1005 if (wmPtr->flags & WM_UPDATE_PENDING) {
1006 Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
1007 }
1008
1009 /*
1010 * Reset all transient windows whose master is the dead window.
1011 */
1012
1013 for (wmPtr2 = winPtr->dispPtr->firstWmPtr; wmPtr2 != NULL;
1014 wmPtr2 = wmPtr2->nextPtr) {
1015 if (wmPtr2->masterPtr == winPtr) {
1016 wmPtr->numTransients--;
1017 Tk_DeleteEventHandler((Tk_Window) wmPtr2->masterPtr,
1018 StructureNotifyMask,
1019 WmWaitMapProc, (ClientData) wmPtr2->winPtr);
1020 wmPtr2->masterPtr = NULL;
1021 if (!(wmPtr2->flags & WM_NEVER_MAPPED)) {
1022 XDeleteProperty(winPtr->display, wmPtr2->wrapperPtr->window,
1023 Tk_InternAtom((Tk_Window) winPtr, "WM_TRANSIENT_FOR"));
1024
1025 /*
1026 * FIXME: Need a call like Win32's UpdateWrapper() so we can
1027 * recreate the wrapper and get rid of the transient window
1028 * decorations.
1029 */
1030 }
1031 }
1032 }
1033 if (wmPtr->numTransients != 0) {
1034 Tcl_Panic("numTransients should be 0");
1035 }
1036
1037 if (wmPtr->masterPtr != NULL) {
1038 wmPtr2 = wmPtr->masterPtr->wmInfoPtr;
1039
1040 /*
1041 * If we had a master, tell them that we aren't tied to them anymore
1042 */
1043
1044 if (wmPtr2 != NULL) {
1045 wmPtr2->numTransients--;
1046 }
1047 Tk_DeleteEventHandler((Tk_Window) wmPtr->masterPtr,
1048 StructureNotifyMask, WmWaitMapProc, (ClientData) winPtr);
1049 wmPtr->masterPtr = NULL;
1050 }
1051 ckfree((char *) wmPtr);
1052 winPtr->wmInfoPtr = NULL;
1053 }
1054
1055 /*
1056 *--------------------------------------------------------------
1057 *
1058 * TkWmSetClass --
1059 *
1060 * This function is invoked whenever a top-level window's class is
1061 * changed. If the window has been mapped then this function updates the
1062 * window manager property for the class. If the window hasn't been
1063 * mapped, the update is deferred until just before the first mapping.
1064 *
1065 * Results:
1066 * None.
1067 *
1068 * Side effects:
1069 * A window property may get updated.
1070 *
1071 *--------------------------------------------------------------
1072 */
1073
1074 void
1075 TkWmSetClass(
1076 TkWindow *winPtr) /* Newly-created top-level window. */
1077 {
1078 if (winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
1079 return;
1080 }
1081
1082 if (winPtr->classUid != NULL) {
1083 XClassHint *classPtr;
1084 Tcl_DString name, class;
1085
1086 Tcl_UtfToExternalDString(NULL, winPtr->nameUid, -1, &name);
1087 Tcl_UtfToExternalDString(NULL, winPtr->classUid, -1, &class);
1088 classPtr = XAllocClassHint();
1089 classPtr->res_name = Tcl_DStringValue(&name);
1090 classPtr->res_class = Tcl_DStringValue(&class);
1091 XSetClassHint(winPtr->display, winPtr->wmInfoPtr->wrapperPtr->window,
1092 classPtr);
1093 XFree((char *) classPtr);
1094 Tcl_DStringFree(&name);
1095 Tcl_DStringFree(&class);
1096 }
1097 }
1098
1099 /*
1100 *----------------------------------------------------------------------
1101 *
1102 * TopLevelLostSlaveProc --
1103 *
1104 * This procedure is invoked when a toplevel window becomes
1105 * managed by another geometry manager.
1106 *
1107 * Results:
1108 * None.
1109 *
1110 * Side effects:
1111 * Should be equivalent to Tix's "wm capture" ??
1112 *
1113 *----------------------------------------------------------------------
1114 */
1115
1116 static
1117 void
1118 TopLevelLostSlaveProc(clientData, tkwin)
1119 ClientData clientData;
1120 Tk_Window tkwin;
1121 {
1122 /* Don't do anything yet */
1123 }
1124
1125 /*
1126 *----------------------------------------------------------------------
1127 *
1128 * Tk_WmObjCmd --
1129 * Tk_WmCmd --
1130 *
1131 * This function is invoked to process the "wm" Tcl command.
1132 *
1133 *----------------------------------------------------------------------
1134 */
1135 /* ARGSUSED */
1136 int
1137 Tk_WmObjCmd(
1138 ClientData clientData, /* Main window associated with interpreter. */
1139 Tcl_Interp *interp, /* Current interpreter. */
1140 int objc, /* Number of arguments. */
1141 Tcl_Obj *CONST objv[]) /* Argument objects. */
1142 {
1143 Tk_Window tkwin = (Tk_Window) clientData;
1144 static CONST char *optionStrings[] = {
1145 "aspect", "attributes", "capture", "client", "colormapwindows",
1146 "command", "deiconify", "focusmodel", "frame",
1147 "geometry", "grid", "group", "iconbitmap",
1148 "iconify", "iconimage", "iconmask", "iconname",
1149 "iconphoto", "iconposition",
1150 "iconwindow", "maxsize", "minsize", "overrideredirect",
1151 "positionfrom", "protocol", "release", "resizable", "sizefrom",
1152 "stackorder", "state", "title", "transient",
1153 "withdraw", "wrapper", NULL };
1154 enum options {
1155 WMOPT_ASPECT, WMOPT_ATTRIBUTES, WMOPT_CAPTURE, WMOPT_CLIENT, WMOPT_COLORMAPWINDOWS,
1156 WMOPT_COMMAND, WMOPT_DEICONIFY, WMOPT_FOCUSMODEL, WMOPT_FRAME,
1157 WMOPT_GEOMETRY, WMOPT_GRID, WMOPT_GROUP, WMOPT_ICONBITMAP,
1158 WMOPT_ICONIFY, WMOPT_ICONIMAGE, WMOPT_ICONMASK, WMOPT_ICONNAME,
1159 WMOPT_ICONPHOTO, WMOPT_ICONPOSITION,
1160 WMOPT_ICONWINDOW, WMOPT_MAXSIZE, WMOPT_MINSIZE, WMOPT_OVERRIDEREDIRECT,
1161 WMOPT_POSITIONFROM, WMOPT_PROTOCOL, WMOPT_RELEASE, WMOPT_RESIZABLE, WMOPT_SIZEFROM,
1162 WMOPT_STACKORDER, WMOPT_STATE, WMOPT_TITLE, WMOPT_TRANSIENT,
1163 WMOPT_WITHDRAW, WMOPT_WRAPPER };
1164 int index;
1165 int length;
1166 char *argv1;
1167 TkWindow *winPtr;
1168 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
1169
1170 if (objc < 2) {
1171 wrongNumArgs:
1172 Tcl_WrongNumArgs(interp, 1, objv, "option window ?arg ...?");
1173 return TCL_ERROR;
1174 }
1175
1176 argv1 = Tcl_GetStringFromObj(objv[1], &length);
1177 if ((argv1[0] == 't') && (strncmp(argv1, "tracing", (size_t) length) == 0)
1178 && (length >= 3)) {
1179 int wmTracing;
1180 /* Perl/Tk: increased parameter position by one because of implicite window parameter */
1181 if ((objc != 3) && (objc != 4)) {
1182 Tcl_WrongNumArgs(interp, 2, objv, "?boolean?");
1183 return TCL_ERROR;
1184 }
1185 if (objc == 3) {
1186 Tcl_SetResult(interp,
1187 ((dispPtr->flags & TK_DISPLAY_WM_TRACING) ? "1" : ""),
1188 TCL_STATIC);
1189 return TCL_OK;
1190 }
1191 if (Tcl_GetBooleanFromObj(interp, objv[3], &wmTracing) != TCL_OK) {
1192 return TCL_ERROR;
1193 }
1194 if (wmTracing) {
1195 dispPtr->flags |= TK_DISPLAY_WM_TRACING;
1196 } else {
1197 dispPtr->flags &= ~TK_DISPLAY_WM_TRACING;
1198 }
1199 return TCL_OK;
1200 }
1201
1202 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
1203 &index) != TCL_OK) {
1204 return TCL_ERROR;
1205 }
1206
1207 if (objc < 3) {
1208 goto wrongNumArgs;
1209 }
1210
1211 if (TkGetWindowFromObj(interp, tkwin, objv[2], (Tk_Window *) &winPtr)
1212 != TCL_OK) {
1213 return TCL_ERROR;
1214 }
1215
1216 if (Tk_IsTopLevel(winPtr)) {
1217 if ((enum options) index == WMOPT_RELEASE) {
1218 Tcl_AppendResult(interp, "window \"", winPtr->pathName,
1219 "\" is already top-level window", (char *) NULL);
1220 return TCL_ERROR;
1221 }
1222 }
1223 else if ((enum options) index != WMOPT_RELEASE) {
1224 Tcl_AppendResult(interp, "window \"", winPtr->pathName,
1225 "\" isn't a top-level window", NULL);
1226 return TCL_ERROR;
1227 }
1228
1229 switch ((enum options) index) {
1230 case WMOPT_ASPECT:
1231 return WmAspectCmd(tkwin, winPtr, interp, objc, objv);
1232 case WMOPT_ATTRIBUTES:
1233 return WmAttributesCmd(tkwin, winPtr, interp, objc, objv);
1234 case WMOPT_CLIENT:
1235 return WmClientCmd(tkwin, winPtr, interp, objc, objv);
1236 case WMOPT_CAPTURE:
1237 return WmCaptureCmd(tkwin, winPtr, interp, objc, objv);
1238 case WMOPT_COLORMAPWINDOWS:
1239 return WmColormapwindowsCmd(tkwin, winPtr, interp, objc, objv);
1240 case WMOPT_COMMAND:
1241 return WmCommandCmd(tkwin, winPtr, interp, objc, objv);
1242 case WMOPT_DEICONIFY:
1243 return WmDeiconifyCmd(tkwin, winPtr, interp, objc, objv);
1244 case WMOPT_FOCUSMODEL:
1245 return WmFocusmodelCmd(tkwin, winPtr, interp, objc, objv);
1246 case WMOPT_FRAME:
1247 return WmFrameCmd(tkwin, winPtr, interp, objc, objv);
1248 case WMOPT_GEOMETRY:
1249 return WmGeometryCmd(tkwin, winPtr, interp, objc, objv);
1250 case WMOPT_GRID:
1251 return WmGridCmd(tkwin, winPtr, interp, objc, objv);
1252 case WMOPT_GROUP:
1253 return WmGroupCmd(tkwin, winPtr, interp, objc, objv);
1254 case WMOPT_ICONBITMAP:
1255 return WmIconbitmapCmd(tkwin, winPtr, interp, objc, objv);
1256 case WMOPT_ICONIFY:
1257 return WmIconifyCmd(tkwin, winPtr, interp, objc, objv);
1258 case WMOPT_ICONIMAGE:
1259 return WmIconimageCmd(tkwin, winPtr, interp, objc, objv);
1260 case WMOPT_ICONMASK:
1261 return WmIconmaskCmd(tkwin, winPtr, interp, objc, objv);
1262 case WMOPT_ICONNAME:
1263 return WmIconnameCmd(tkwin, winPtr, interp, objc, objv);
1264 case WMOPT_ICONPHOTO:
1265 return WmIconphotoCmd(tkwin, winPtr, interp, objc, objv);
1266 case WMOPT_ICONPOSITION:
1267 return WmIconpositionCmd(tkwin, winPtr, interp, objc, objv);
1268 case WMOPT_ICONWINDOW:
1269 return WmIconwindowCmd(tkwin, winPtr, interp, objc, objv);
1270 case WMOPT_MAXSIZE:
1271 return WmMaxsizeCmd(tkwin, winPtr, interp, objc, objv);
1272 case WMOPT_MINSIZE:
1273 return WmMinsizeCmd(tkwin, winPtr, interp, objc, objv);
1274 case WMOPT_OVERRIDEREDIRECT:
1275 return WmOverrideredirectCmd(tkwin, winPtr, interp, objc, objv);
1276 case WMOPT_POSITIONFROM:
1277 return WmPositionfromCmd(tkwin, winPtr, interp, objc, objv);
1278 case WMOPT_PROTOCOL:
1279 return WmProtocolCmd(tkwin, winPtr, interp, objc, objv);
1280 case WMOPT_RESIZABLE:
1281 return WmResizableCmd(tkwin, winPtr, interp, objc, objv);
1282 case WMOPT_RELEASE:
1283 return WmReleaseCmd(tkwin, winPtr, interp, objc, objv);
1284 case WMOPT_SIZEFROM:
1285 return WmSizefromCmd(tkwin, winPtr, interp, objc, objv);
1286 case WMOPT_STACKORDER:
1287 return WmStackorderCmd(tkwin, winPtr, interp, objc, objv);
1288 case WMOPT_STATE:
1289 return WmStateCmd(tkwin, winPtr, interp, objc, objv);
1290 case WMOPT_TITLE:
1291 return WmTitleCmd(tkwin, winPtr, interp, objc, objv);
1292 case WMOPT_TRANSIENT:
1293 return WmTransientCmd(tkwin, winPtr, interp, objc, objv);
1294 case WMOPT_WITHDRAW:
1295 return WmWithdrawCmd(tkwin, winPtr, interp, objc, objv);
1296 case WMOPT_WRAPPER:
1297 return WmWrapperCmd(tkwin, winPtr, interp, objc, objv);
1298 }
1299
1300 /* This should not happen */
1301 return TCL_ERROR;
1302 }
1303
1304 /*
1305 *----------------------------------------------------------------------
1306 *
1307 * WmAspectCmd --
1308 *
1309 * This function is invoked to process the "wm aspect" Tcl command. See
1310 * the user documentation for details on what it does.
1311 *
1312 * Results:
1313 * A standard Tcl result.
1314 *
1315 * Side effects:
1316 * See the user documentation.
1317 *
1318 *----------------------------------------------------------------------
1319 */
1320
1321 static int
1322 WmAspectCmd(
1323 Tk_Window tkwin, /* Main window of the application. */
1324 TkWindow *winPtr, /* Toplevel to work with */
1325 Tcl_Interp *interp, /* Current interpreter. */
1326 int objc, /* Number of arguments. */
1327 Tcl_Obj *CONST objv[]) /* Argument objects. */
1328 {
1329 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1330 int numer1, denom1, numer2, denom2;
1331
1332 if ((objc != 3) && (objc != 7)) {
1333 Tcl_WrongNumArgs(interp, 2, objv,
1334 "window ?minNumer minDenom maxNumer maxDenom?");
1335 return TCL_ERROR;
1336 }
1337 if (objc == 3) {
1338 if (wmPtr->sizeHintsFlags & PAspect) {
1339 Tcl_IntResults(interp, 4, 0, wmPtr->minAspect.x,
1340 wmPtr->minAspect.y, wmPtr->maxAspect.x,
1341 wmPtr->maxAspect.y);
1342 }
1343 return TCL_OK;
1344 }
1345 if (*Tcl_GetString(objv[3]) == '\0') {
1346 wmPtr->sizeHintsFlags &= ~PAspect;
1347 } else {
1348 if ((Tcl_GetIntFromObj(interp, objv[3], &numer1) != TCL_OK)
1349 || (Tcl_GetIntFromObj(interp, objv[4], &denom1) != TCL_OK)
1350 || (Tcl_GetIntFromObj(interp, objv[5], &numer2) != TCL_OK)
1351 || (Tcl_GetIntFromObj(interp, objv[6], &denom2) != TCL_OK)) {
1352 return TCL_ERROR;
1353 }
1354 if ((numer1 <= 0) || (denom1 <= 0) || (numer2 <= 0) ||
1355 (denom2 <= 0)) {
1356 Tcl_SetResult(interp, "aspect number can't be <= 0",
1357 TCL_STATIC);
1358 return TCL_ERROR;
1359 }
1360 wmPtr->minAspect.x = numer1;
1361 wmPtr->minAspect.y = denom1;
1362 wmPtr->maxAspect.x = numer2;
1363 wmPtr->maxAspect.y = denom2;
1364 wmPtr->sizeHintsFlags |= PAspect;
1365 }
1366 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
1367 WmUpdateGeom(wmPtr, winPtr);
1368 return TCL_OK;
1369 }
1370
1371 /*
1372 *----------------------------------------------------------------------
1373 *
1374 * WmSetAttribute --
1375 *
1376 * Helper routine for WmAttributesCmd. Sets the value
1377 * of the specified attribute.
1378 *
1379 * Returns:
1380 *
1381 * TCL_OK if successful, TCL_ERROR otherwise. In case of an
1382 * error, leaves a message in the interpreter's result.
1383 *
1384 *----------------------------------------------------------------------
1385 */
1386 static int WmSetAttribute(
1387 TkWindow *winPtr, /* Toplevel to work with */
1388 Tcl_Interp *interp, /* Current interpreter */
1389 WmAttribute attribute, /* Code of attribute to set */
1390 Tcl_Obj *value) /* New value */
1391 {
1392 WmInfo *wmPtr = winPtr->wmInfoPtr;
1393 switch (attribute) {
1394 case WMATT_ALPHA:
1395 {
1396 unsigned long opacity; /* 0=transparent, 0xFFFFFFFF=opaque */
1397
1398 if (TCL_OK != Tcl_GetDoubleFromObj(
1399 interp, value, &wmPtr->reqState.alpha)) {
1400 return TCL_ERROR;
1401 }
1402 if (wmPtr->reqState.alpha < 0.0) {
1403 wmPtr->reqState.alpha = 0.0;
1404 }
1405 if (wmPtr->reqState.alpha > 1.0) {
1406 wmPtr->reqState.alpha = 1.0;
1407 }
1408
1409 if (!wmPtr->wrapperPtr) {
1410 break;
1411 }
1412
1413 opacity = 0xFFFFFFFFul * wmPtr->reqState.alpha;
1414 XChangeProperty(winPtr->display, wmPtr->wrapperPtr->window,
1415 Tk_InternAtom((Tk_Window)winPtr, "_NET_WM_WINDOW_OPACITY"),
1416 XA_CARDINAL, 32, PropModeReplace,
1417 (unsigned char *)&opacity, 1L);
1418 wmPtr->attributes.alpha = wmPtr->reqState.alpha;
1419
1420 break;
1421 }
1422 case WMATT_TOPMOST:
1423 if (TCL_OK != Tcl_GetBooleanFromObj(
1424 interp, value, &wmPtr->reqState.topmost)) {
1425 return TCL_ERROR;
1426 }
1427 SetNetWmState(winPtr,
1428 "_NET_WM_STATE_ABOVE", wmPtr->reqState.topmost);
1429 break;
1430 case WMATT_ZOOMED:
1431 if (TCL_OK != Tcl_GetBooleanFromObj(
1432 interp, value, &wmPtr->reqState.zoomed)) {
1433 return TCL_ERROR;
1434 }
1435 SetNetWmState(winPtr,
1436 "_NET_WM_STATE_MAXIMIZED_VERT", wmPtr->reqState.zoomed);
1437 SetNetWmState(winPtr,
1438 "_NET_WM_STATE_MAXIMIZED_HORZ", wmPtr->reqState.zoomed);
1439 break;
1440 case WMATT_FULLSCREEN:
1441 if (TCL_OK != Tcl_GetBooleanFromObj(
1442 interp, value, &wmPtr->reqState.fullscreen)) {
1443 return TCL_ERROR;
1444 }
1445 SetNetWmState(winPtr,
1446 "_NET_WM_STATE_FULLSCREEN", wmPtr->reqState.fullscreen);
1447 break;
1448 case _WMATT_LAST_ATTRIBUTE: /* NOTREACHED */
1449 return TCL_ERROR;
1450 }
1451 return TCL_OK;
1452 }
1453
1454 /*
1455 *----------------------------------------------------------------------
1456 *
1457 * WmGetAttribute --
1458 *
1459 * Helper routine for WmAttributesCmd. Returns the current value
1460 * of the specified attribute.
1461 *
1462 * See also: CheckNetWmState().
1463 *
1464 *----------------------------------------------------------------------
1465 */
1466 static Tcl_Obj *WmGetAttribute(
1467 TkWindow *winPtr, /* Toplevel to work with */
1468 WmAttribute attribute) /* Code of attribute to get */
1469 {
1470 WmInfo *wmPtr = winPtr->wmInfoPtr;
1471 switch (attribute) {
1472 case WMATT_ALPHA:
1473 return Tcl_NewDoubleObj(wmPtr->attributes.alpha);
1474 case WMATT_TOPMOST:
1475 return Tcl_NewBooleanObj(wmPtr->attributes.topmost);
1476 case WMATT_ZOOMED:
1477 return Tcl_NewBooleanObj(wmPtr->attributes.zoomed);
1478 case WMATT_FULLSCREEN:
1479 return Tcl_NewBooleanObj(wmPtr->attributes.fullscreen);
1480 case _WMATT_LAST_ATTRIBUTE: /*NOTREACHED*/
1481 break;
1482 }
1483 /*NOTREACHED*/
1484 return NULL;
1485 }
1486
1487 /*
1488 *----------------------------------------------------------------------
1489 *
1490 * WmAttributesCmd --
1491 *
1492 * This function is invoked to process the "wm attributes" Tcl command.
1493 *
1494 * Syntax:
1495 *
1496 * wm attributes $win ?-attribute ?value attribute value...??
1497 *
1498 * Notes:
1499 *
1500 * Attributes of mapped windows are set by sending a _NET_WM_STATE
1501 * ClientMessage to the root window (see SetNetWmState).
1502 * For withdrawn windows, we keep track of the requested attribute
1503 * state, and set the _NET_WM_STATE property ourselves immediately
1504 * prior to mapping the window.
1505 *
1506 * See also: TIP#231, EWMH.
1507 *
1508 *----------------------------------------------------------------------
1509 */
1510
1511 static int
1512 WmAttributesCmd(
1513 Tk_Window tkwin, /* Main window of the application. */
1514 TkWindow *winPtr, /* Toplevel to work with */
1515 Tcl_Interp *interp, /* Current interpreter. */
1516 int objc, /* Number of arguments. */
1517 Tcl_Obj *CONST objv[]) /* Argument objects. */
1518 {
1519 int attribute = 0;
1520 if (objc == 3) { /* wm attributes $win */
1521 Tcl_Obj *result = Tcl_NewListObj(0,0);
1522 for (attribute = 0; attribute < _WMATT_LAST_ATTRIBUTE; ++attribute) {
1523 Tcl_ListObjAppendElement(interp, result,
1524 Tcl_NewStringObj(WmAttributeNames[attribute], -1));
1525 Tcl_ListObjAppendElement(interp, result,
1526 WmGetAttribute(winPtr, attribute));
1527 }
1528 Tcl_SetObjResult(interp, result);
1529 return TCL_OK;
1530 } else if (objc == 4) { /* wm attributes $win -attribute */
1531 if (Tcl_GetIndexFromObj(interp, objv[3], WmAttributeNames,
1532 "attribute", 0, &attribute) != TCL_OK) {
1533 return TCL_ERROR;
1534 }
1535 Tcl_SetObjResult(interp,
1536 WmGetAttribute(winPtr, attribute));
1537 return TCL_OK;
1538 } else if ((objc - 3) % 2 == 0) { /* wm attributes $win -att value... */
1539 int i;
1540 for (i = 3; i < objc; i += 2) {
1541 if (Tcl_GetIndexFromObj(interp, objv[i], WmAttributeNames,
1542 "attribute", 0, &attribute) != TCL_OK) {
1543 return TCL_ERROR;
1544 }
1545 if (WmSetAttribute(winPtr,interp,attribute,objv[i+1]) != TCL_OK) {
1546 return TCL_ERROR;
1547 }
1548 }
1549 return TCL_OK;
1550 } else {
1551 Tcl_WrongNumArgs(interp, 2, objv, "window ?-attribute ?value ...??");
1552 return TCL_ERROR;
1553 }
1554 return TCL_OK;
1555 }
1556
1557 /*
1558 *----------------------------------------------------------------------
1559 *
1560 * WmClientCmd --
1561 *
1562 * This function is invoked to process the "wm client" Tcl command. See
1563 * the user documentation for details on what it does.
1564 *
1565 * Results:
1566 * A standard Tcl result.
1567 *
1568 * Side effects:
1569 * See the user documentation.
1570 *
1571 *----------------------------------------------------------------------
1572 */
1573
1574 static int
1575 WmClientCmd(
1576 Tk_Window tkwin, /* Main window of the application. */
1577 TkWindow *winPtr, /* Toplevel to work with */
1578 Tcl_Interp *interp, /* Current interpreter. */
1579 int objc, /* Number of arguments. */
1580 Tcl_Obj *CONST objv[]) /* Argument objects. */
1581 {
1582 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1583 char *argv3;
1584 int length;
1585
1586 if ((objc != 3) && (objc != 4)) {
1587 Tcl_WrongNumArgs(interp, 2, objv, "window ?name?");
1588 return TCL_ERROR;
1589 }
1590 if (objc == 3) {
1591 if (wmPtr->clientMachine != NULL) {
1592 Tcl_SetResult(interp, wmPtr->clientMachine, TCL_STATIC);
1593 }
1594 return TCL_OK;
1595 }
1596 argv3 = Tcl_GetStringFromObj(objv[3], &length);
1597 if (argv3[0] == 0) {
1598 if (wmPtr->clientMachine != NULL) {
1599 ckfree((char *) wmPtr->clientMachine);
1600 wmPtr->clientMachine = NULL;
1601 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
1602 XDeleteProperty(winPtr->display, wmPtr->wrapperPtr->window,
1603 Tk_InternAtom((Tk_Window)winPtr, "WM_CLIENT_MACHINE"));
1604 }
1605 }
1606 return TCL_OK;
1607 }
1608 if (wmPtr->clientMachine != NULL) {
1609 ckfree((char *) wmPtr->clientMachine);
1610 }
1611 wmPtr->clientMachine = (char *)
1612 ckalloc((unsigned) (length + 1));
1613 strcpy(wmPtr->clientMachine, argv3);
1614 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
1615 XTextProperty textProp;
1616 Tcl_DString ds;
1617 char *string;
1618
1619 Tcl_UtfToExternalDString(NULL, wmPtr->clientMachine, -1, &ds);
1620 string = Tcl_DStringValue(&ds);
1621 if (XStringListToTextProperty(&string, 1,
1622 &textProp) != 0) {
1623 XSetWMClientMachine(winPtr->display, wmPtr->wrapperPtr->window,
1624 &textProp);
1625 XFree((char *) textProp.value);
1626 }
1627 Tcl_DStringFree(&ds);
1628 }
1629 return TCL_OK;
1630 }
1631
1632 /*
1633 *----------------------------------------------------------------------
1634 *
1635 * WmColormapwindowsCmd --
1636 *
1637 * This function is invoked to process the "wm colormapwindows" Tcl
1638 * command. See the user documentation for details on what it does.
1639 *
1640 * Results:
1641 * A standard Tcl result.
1642 *
1643 * Side effects:
1644 * See the user documentation.
1645 *
1646 *----------------------------------------------------------------------
1647 */
1648
1649 static int
1650 WmColormapwindowsCmd(
1651 Tk_Window tkwin, /* Main window of the application. */
1652 TkWindow *winPtr, /* Toplevel to work with */
1653 Tcl_Interp *interp, /* Current interpreter. */
1654 int objc, /* Number of arguments. */
1655 Tcl_Obj *CONST objv[]) /* Argument objects. */
1656 {
1657 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1658 Window *cmapList;
1659 TkWindow *winPtr2;
1660 int count, i, windowObjc, gotToplevel;
1661 Tcl_Obj **windowObjv;
1662 char buffer[20];
1663
1664 if ((objc != 3) && (objc != 4)) {
1665 Tcl_WrongNumArgs(interp, 2, objv, "window ?windowList?");
1666 return TCL_ERROR;
1667 }
1668 Tk_MakeWindowExist((Tk_Window) winPtr);
1669 if (wmPtr->wrapperPtr == NULL) {
1670 CreateWrapper(wmPtr);
1671 }
1672 if (objc == 3) {
1673 if (XGetWMColormapWindows(winPtr->display,
1674 wmPtr->wrapperPtr->window, &cmapList, &count) == 0) {
1675 return TCL_OK;
1676 }
1677 for (i = 0; i < count; i++) {
1678 if ((i == (count-1))
1679 && (wmPtr->flags & WM_ADDED_TOPLEVEL_COLORMAP)) {
1680 break;
1681 }
1682 winPtr2 = (TkWindow *) Tk_IdToWindow(winPtr->display,
1683 cmapList[i]);
1684 if (winPtr2 == NULL) {
1685 sprintf(buffer, "0x%lx", cmapList[i]);
1686 Tcl_AppendElement(interp, buffer);
1687 } else {
1688 Tcl_AppendElement(interp, winPtr2->pathName);
1689 }
1690 }
1691 XFree((char *) cmapList);
1692 return TCL_OK;
1693 }
1694 if (Tcl_ListObjGetElements(interp, objv[3], &windowObjc, &windowObjv)
1695 != TCL_OK) {
1696 return TCL_ERROR;
1697 }
1698 cmapList = (Window *) ckalloc((unsigned)
1699 ((windowObjc+1)*sizeof(Window)));
1700 gotToplevel = 0;
1701 for (i = 0; i < windowObjc; i++) {
1702 if (TkGetWindowFromObj(interp, tkwin, windowObjv[i],
1703 (Tk_Window *) &winPtr2) != TCL_OK) {
1704 ckfree((char *) cmapList);
1705 return TCL_ERROR;
1706 }
1707 if (winPtr2 == winPtr) {
1708 gotToplevel = 1;
1709 }
1710 if (winPtr2->window == None) {
1711 Tk_MakeWindowExist((Tk_Window) winPtr2);
1712 }
1713 cmapList[i] = winPtr2->window;
1714 }
1715 if (!gotToplevel) {
1716 wmPtr->flags |= WM_ADDED_TOPLEVEL_COLORMAP;
1717 cmapList[windowObjc] = wmPtr->wrapperPtr->window;
1718 windowObjc++;
1719 } else {
1720 wmPtr->flags &= ~WM_ADDED_TOPLEVEL_COLORMAP;
1721 }
1722 wmPtr->flags |= WM_COLORMAPS_EXPLICIT;
1723 XSetWMColormapWindows(winPtr->display, wmPtr->wrapperPtr->window,
1724 cmapList, windowObjc);
1725 ckfree((char *) cmapList);
1726 return TCL_OK;
1727 }
1728
1729 /*
1730 *----------------------------------------------------------------------
1731 *
1732 * WmCommandCmd --
1733 *
1734 * This function is invoked to process the "wm command" Tcl command. See
1735 * the user documentation for details on what it does.
1736 *
1737 * Results:
1738 * A standard Tcl result.
1739 *
1740 * Side effects:
1741 * See the user documentation.
1742 *
1743 *----------------------------------------------------------------------
1744 */
1745
1746 static int
1747 WmCommandCmd(
1748 Tk_Window tkwin, /* Main window of the application. */
1749 TkWindow *winPtr, /* Toplevel to work with */
1750 Tcl_Interp *interp, /* Current interpreter. */
1751 int objc, /* Number of arguments. */
1752 Tcl_Obj *CONST objv[]) /* Argument objects. */
1753 {
1754 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1755 char *argv3;
1756 int i;
1757 int cmdArgc;
1758 Tcl_Obj **cmdArgv;
1759
1760 if ((objc != 3) && (objc != 4)) {
1761 Tcl_WrongNumArgs(interp, 2, objv, "window ?value?");
1762 return TCL_ERROR;
1763 }
1764 if (objc == 3) {
1765 #ifdef _LANG
1766 Tcl_IncrRefCount(wmPtr->cmdArg);
1767 Tcl_SetObjResult(interp,wmPtr->cmdArg);
1768 #else
1769 Tcl_ResetResult(interp);
1770 if (wmPtr->cmdArgv != NULL) {
1771 for (i=0; i < wmPtr->cmdArgc; i++) {
1772 Tcl_ListObjAppendElement(interp,
1773 Tcl_GetObjResult(interp),
1774 Tcl_NewStringObj(wmPtr->cmdArgv[i],-1));
1775 }
1776 }
1777 #endif
1778 return TCL_OK;
1779 }
1780
1781 if (LangNull(objv[3])) {
1782 #ifdef _LANG
1783 if (wmPtr->cmdArg != NULL) {
1784 TkWmFreeCmd(wmPtr);
1785 #else
1786 if (wmPtr->cmdArgv != NULL) {
1787 ckfree((char *) wmPtr->cmdArgv);
1788 wmPtr->cmdArgv = NULL;
1789 #endif
1790 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
1791 XDeleteProperty(winPtr->display, wmPtr->wrapperPtr->window,
1792 Tk_InternAtom((Tk_Window) winPtr, "WM_COMMAND"));
1793 }
1794 }
1795 return TCL_OK;
1796 }
1797 if (Tcl_ListObjGetElements(interp, objv[3], &cmdArgc, &cmdArgv) != TCL_OK) {
1798 return TCL_ERROR;
1799 }
1800 #ifdef _LANG
1801 Tcl_IncrRefCount(objv[3]);
1802 /* Free the old one */
1803 TkWmFreeCmd(wmPtr);
1804 /* Save reference to new one to keep strings live */
1805 wmPtr->cmdArg = objv[3];
1806 #else
1807 if (wmPtr->cmdArgv != NULL) {
1808 ckfree((char *) wmPtr->cmdArgv);
1809 }
1810 wmPtr->cmdArgc = cmdArgc;
1811 wmPtr->cmdArgv = (CONST char **) ckalloc(cmdArgc*sizeof(char *));
1812 for (i=0; i < cmdArgc; i++)
1813 {
1814 wmPtr->cmdArgv[i] = Tcl_GetString(cmdArgv[i]);
1815 }
1816 #endif
1817 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
1818 UpdateCommand(winPtr);
1819 }
1820 return TCL_OK;
1821 }
1822
1823 /*
1824 *----------------------------------------------------------------------
1825 *
1826 * WmDeiconifyCmd --
1827 *
1828 * This function is invoked to process the "wm deiconify" Tcl command.
1829 * See the user documentation for details on what it does.
1830 *
1831 * Results:
1832 * A standard Tcl result.
1833 *
1834 * Side effects:
1835 * See the user documentation.
1836 *
1837 *----------------------------------------------------------------------
1838 */
1839
1840 static int
1841 WmDeiconifyCmd(
1842 Tk_Window tkwin, /* Main window of the application. */
1843 TkWindow *winPtr, /* Toplevel to work with */
1844 Tcl_Interp *interp, /* Current interpreter. */
1845 int objc, /* Number of arguments. */
1846 Tcl_Obj *CONST objv[]) /* Argument objects. */
1847 {
1848 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1849
1850 if (objc != 3) {
1851 Tcl_WrongNumArgs(interp, 2, objv, "window");
1852 return TCL_ERROR;
1853 }
1854 if (wmPtr->iconFor != NULL) {
1855 Tcl_AppendResult(interp, "can't deiconify ", Tcl_GetString(objv[2]),
1856 ": it is an icon for ", Tk_PathName(wmPtr->iconFor), NULL);
1857 return TCL_ERROR;
1858 }
1859 if (winPtr->flags & TK_EMBEDDED) {
1860 Tcl_AppendResult(interp, "can't deiconify ", winPtr->pathName,
1861 ": it is an embedded window", NULL);
1862 return TCL_ERROR;
1863 }
1864 wmPtr->flags &= ~WM_WITHDRAWN;
1865 TkpWmSetState(winPtr, NormalState);
1866 return TCL_OK;
1867 }
1868
1869 /*
1870 *----------------------------------------------------------------------
1871 *
1872 * WmFocusmodelCmd --
1873 *
1874 * This function is invoked to process the "wm focusmodel" Tcl command.
1875 * See the user documentation for details on what it does.
1876 *
1877 * Results:
1878 * A standard Tcl result.
1879 *
1880 * Side effects:
1881 * See the user documentation.
1882 *
1883 *----------------------------------------------------------------------
1884 */
1885
1886 static int
1887 WmFocusmodelCmd(
1888 Tk_Window tkwin, /* Main window of the application. */
1889 TkWindow *winPtr, /* Toplevel to work with */
1890 Tcl_Interp *interp, /* Current interpreter. */
1891 int objc, /* Number of arguments. */
1892 Tcl_Obj *CONST objv[]) /* Argument objects. */
1893 {
1894 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1895 static CONST char *optionStrings[] = {
1896 "active", "passive", NULL };
1897 enum options {
1898 OPT_ACTIVE, OPT_PASSIVE };
1899 int index;
1900
1901 if ((objc != 3) && (objc != 4)) {
1902 Tcl_WrongNumArgs(interp, 2, objv, "window ?active|passive?");
1903 return TCL_ERROR;
1904 }
1905 if (objc == 3) {
1906 Tcl_SetResult(interp, (wmPtr->hints.input ? "passive" : "active"),
1907 TCL_STATIC);
1908 return TCL_OK;
1909 }
1910
1911 if (Tcl_GetIndexFromObj(interp, objv[3], optionStrings, "argument", 0,
1912 &index) != TCL_OK) {
1913 return TCL_ERROR;
1914 }
1915 if (index == OPT_ACTIVE) {
1916 wmPtr->hints.input = False;
1917 } else { /* OPT_PASSIVE */
1918 wmPtr->hints.input = True;
1919 }
1920 UpdateHints(winPtr);
1921 return TCL_OK;
1922 }
1923
1924 /*
1925 *----------------------------------------------------------------------
1926 *
1927 * WmFrameCmd --
1928 *
1929 * This function is invoked to process the "wm frame" Tcl command. See
1930 * the user documentation for details on what it does.
1931 *
1932 * Results:
1933 * A standard Tcl result.
1934 *
1935 * Side effects:
1936 * See the user documentation.
1937 *
1938 *----------------------------------------------------------------------
1939 */
1940
1941 static int
1942 WmFrameCmd(
1943 Tk_Window tkwin, /* Main window of the application. */
1944 TkWindow *winPtr, /* Toplevel to work with */
1945 Tcl_Interp *interp, /* Current interpreter. */
1946 int objc, /* Number of arguments. */
1947 Tcl_Obj *CONST objv[]) /* Argument objects. */
1948 {
1949 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1950 Window window;
1951 char buf[TCL_INTEGER_SPACE];
1952
1953 if (objc != 3) {
1954 Tcl_WrongNumArgs(interp, 2, objv, "window");
1955 return TCL_ERROR;
1956 }
1957 window = wmPtr->reparent;
1958 if (window == None) {
1959 window = Tk_WindowId((Tk_Window) winPtr);
1960 }
1961 sprintf(buf, "0x%x", (unsigned int) window);
1962 Tcl_SetResult(interp, buf, TCL_VOLATILE);
1963 return TCL_OK;
1964 }
1965
1966 /*
1967 *----------------------------------------------------------------------
1968 *
1969 * WmGeometryCmd --
1970 *
1971 * This function is invoked to process the "wm geometry" Tcl command.
1972 * See the user documentation for details on what it does.
1973 *
1974 * Results:
1975 * A standard Tcl result.
1976 *
1977 * Side effects:
1978 * See the user documentation.
1979 *
1980 *----------------------------------------------------------------------
1981 */
1982
1983 static int
1984 WmGeometryCmd(
1985 Tk_Window tkwin, /* Main window of the application. */
1986 TkWindow *winPtr, /* Toplevel to work with */
1987 Tcl_Interp *interp, /* Current interpreter. */
1988 int objc, /* Number of arguments. */
1989 Tcl_Obj *CONST objv[]) /* Argument objects. */
1990 {
1991 register WmInfo *wmPtr = winPtr->wmInfoPtr;
1992 char xSign, ySign;
1993 int width, height;
1994 char *argv3;
1995
1996 if ((objc != 3) && (objc != 4)) {
1997 Tcl_WrongNumArgs(interp, 2, objv, "window ?newGeometry?");
1998 return TCL_ERROR;
1999 }
2000 if (objc == 3) {
2001 char buf[16 + TCL_INTEGER_SPACE * 4];
2002
2003 xSign = (wmPtr->flags & WM_NEGATIVE_X) ? '-' : '+';
2004 ySign = (wmPtr->flags & WM_NEGATIVE_Y) ? '-' : '+';
2005 if (wmPtr->gridWin != NULL) {
2006 width = wmPtr->reqGridWidth + (winPtr->changes.width
2007 - winPtr->reqWidth)/wmPtr->widthInc;
2008 height = wmPtr->reqGridHeight + (winPtr->changes.height
2009 - winPtr->reqHeight)/wmPtr->heightInc;
2010 } else {
2011 width = winPtr->changes.width;
2012 height = winPtr->changes.height;
2013 }
2014 sprintf(buf, "%dx%d%c%d%c%d", width, height, xSign, wmPtr->x,
2015 ySign, wmPtr->y);
2016 Tcl_SetResult(interp, buf, TCL_VOLATILE);
2017 return TCL_OK;
2018 }
2019 argv3 = Tcl_GetString(objv[3]);
2020 if (*argv3 == '\0') {
2021 wmPtr->width = -1;
2022 wmPtr->height = -1;
2023 WmUpdateGeom(wmPtr, winPtr);
2024 return TCL_OK;
2025 }
2026 return ParseGeometry(interp, argv3, winPtr);
2027 }
2028
2029 /*
2030 *----------------------------------------------------------------------
2031 *
2032 * WmGridCmd --
2033 *
2034 * This function is invoked to process the "wm grid" Tcl command. See the
2035 * user documentation for details on what it does.
2036 *
2037 * Results:
2038 * A standard Tcl result.
2039 *
2040 * Side effects:
2041 * See the user documentation.
2042 *
2043 *----------------------------------------------------------------------
2044 */
2045
2046 static int
2047 WmGridCmd(
2048 Tk_Window tkwin, /* Main window of the application. */
2049 TkWindow *winPtr, /* Toplevel to work with */
2050 Tcl_Interp *interp, /* Current interpreter. */
2051 int objc, /* Number of arguments. */
2052 Tcl_Obj *CONST objv[]) /* Argument objects. */
2053 {
2054 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2055 int reqWidth, reqHeight, widthInc, heightInc;
2056
2057 if ((objc != 3) && (objc != 7)) {
2058 Tcl_WrongNumArgs(interp, 2, objv,
2059 "window ?baseWidth baseHeight widthInc heightInc?");
2060 return TCL_ERROR;
2061 }
2062 if (objc == 3) {
2063 if (wmPtr->sizeHintsFlags & PBaseSize) {
2064 Tcl_IntResults(interp, 4, 0, wmPtr->reqGridWidth,
2065 wmPtr->reqGridHeight, wmPtr->widthInc,
2066 wmPtr->heightInc);
2067 }
2068 return TCL_OK;
2069 }
2070 if (*Tcl_GetString(objv[3]) == '\0') {
2071 /*
2072 * Turn off gridding and reset the width and height to make sense as
2073 * ungridded numbers.
2074 */
2075
2076 wmPtr->sizeHintsFlags &= ~(PBaseSize|PResizeInc);
2077 if (wmPtr->width != -1) {
2078 wmPtr->width = winPtr->reqWidth + (wmPtr->width
2079 - wmPtr->reqGridWidth)*wmPtr->widthInc;
2080 wmPtr->height = winPtr->reqHeight + (wmPtr->height
2081 - wmPtr->reqGridHeight)*wmPtr->heightInc;
2082 }
2083 wmPtr->widthInc = 1;
2084 wmPtr->heightInc = 1;
2085 } else {
2086 if ((Tcl_GetIntFromObj(interp, objv[3], &reqWidth) != TCL_OK)
2087 || (Tcl_GetIntFromObj(interp, objv[4], &reqHeight) != TCL_OK)
2088 || (Tcl_GetIntFromObj(interp, objv[5], &widthInc) != TCL_OK)
2089 || (Tcl_GetIntFromObj(interp, objv[6], &heightInc) !=TCL_OK)) {
2090 return TCL_ERROR;
2091 }
2092 if (reqWidth < 0) {
2093 Tcl_SetResult(interp, "baseWidth can't be < 0", TCL_STATIC);
2094 return TCL_ERROR;
2095 }
2096 if (reqHeight < 0) {
2097 Tcl_SetResult(interp, "baseHeight can't be < 0", TCL_STATIC);
2098 return TCL_ERROR;
2099 }
2100 if (widthInc <= 0) {
2101 Tcl_SetResult(interp, "widthInc can't be <= 0", TCL_STATIC);
2102 return TCL_ERROR;
2103 }
2104 if (heightInc <= 0) {
2105 Tcl_SetResult(interp, "heightInc can't be <= 0", TCL_STATIC);
2106 return TCL_ERROR;
2107 }
2108 Tk_SetGrid((Tk_Window) winPtr, reqWidth, reqHeight, widthInc,
2109 heightInc);
2110 }
2111 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
2112 WmUpdateGeom(wmPtr, winPtr);
2113 return TCL_OK;
2114 }
2115
2116 /*
2117 *----------------------------------------------------------------------
2118 *
2119 * WmGroupCmd --
2120 *
2121 * This function is invoked to process the "wm group" Tcl command. See
2122 * the user documentation for details on what it does.
2123 *
2124 * Results:
2125 * A standard Tcl result.
2126 *
2127 * Side effects:
2128 * See the user documentation.
2129 *
2130 *----------------------------------------------------------------------
2131 */
2132
2133 static int
2134 WmGroupCmd(
2135 Tk_Window tkwin, /* Main window of the application. */
2136 TkWindow *winPtr, /* Toplevel to work with */
2137 Tcl_Interp *interp, /* Current interpreter. */
2138 int objc, /* Number of arguments. */
2139 Tcl_Obj *CONST objv[]) /* Argument objects. */
2140 {
2141 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2142 Tk_Window tkwin2;
2143 WmInfo *wmPtr2;
2144 char *argv3;
2145 int length;
2146
2147 if ((objc != 3) && (objc != 4)) {
2148 Tcl_WrongNumArgs(interp, 2, objv, "window ?pathName?");
2149 return TCL_ERROR;
2150 }
2151 if (objc == 3) {
2152 if (wmPtr->hints.flags & WindowGroupHint) {
2153 Tcl_SetResult(interp, wmPtr->leaderName, TCL_STATIC);
2154 }
2155 return TCL_OK;
2156 }
2157 argv3 = Tcl_GetStringFromObj(objv[3], &length);
2158 if (*argv3 == '\0') {
2159 wmPtr->hints.flags &= ~WindowGroupHint;
2160 if (wmPtr->leaderName != NULL) {
2161 ckfree(wmPtr->leaderName);
2162 }
2163 wmPtr->leaderName = NULL;
2164 } else {
2165 if (TkGetWindowFromObj(interp, tkwin, objv[3], &tkwin2) != TCL_OK) {
2166 return TCL_ERROR;
2167 }
2168 while (!Tk_TopWinHierarchy(tkwin2)) {
2169 /*
2170 * Ensure that the group leader is actually a Tk toplevel.
2171 */
2172
2173 tkwin2 = Tk_Parent(tkwin2);
2174 }
2175 Tk_MakeWindowExist(tkwin2);
2176 wmPtr2 = ((TkWindow *) tkwin2)->wmInfoPtr;
2177 if (wmPtr2->wrapperPtr == NULL) {
2178 CreateWrapper(wmPtr2);
2179 }
2180 if (wmPtr->leaderName != NULL) {
2181 ckfree(wmPtr->leaderName);
2182 }
2183 wmPtr->hints.window_group = Tk_WindowId(wmPtr2->wrapperPtr);
2184 wmPtr->hints.flags |= WindowGroupHint;
2185 wmPtr->leaderName = ckalloc((unsigned) (length + 1));
2186 strcpy(wmPtr->leaderName, argv3);
2187 }
2188 UpdateHints(winPtr);
2189 return TCL_OK;
2190 }
2191
2192 /*
2193 *----------------------------------------------------------------------
2194 *
2195 * WmIconbitmapCmd --
2196 *
2197 * This function is invoked to process the "wm iconbitmap" Tcl command.
2198 * See the user documentation for details on what it does.
2199 *
2200 * Results:
2201 * A standard Tcl result.
2202 *
2203 * Side effects:
2204 * See the user documentation.
2205 *
2206 *----------------------------------------------------------------------
2207 */
2208
2209 static int
2210 WmIconbitmapCmd(
2211 Tk_Window tkwin, /* Main window of the application. */
2212 TkWindow *winPtr, /* Toplevel to work with */
2213 Tcl_Interp *interp, /* Current interpreter. */
2214 int objc, /* Number of arguments. */
2215 Tcl_Obj *CONST objv[]) /* Argument objects. */
2216 {
2217 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2218 Pixmap pixmap;
2219 char *argv3;
2220
2221 if ((objc < 3) || (objc > 4)) {
2222 Tcl_WrongNumArgs(interp, 2, objv, "window ?bitmap?");
2223 return TCL_ERROR;
2224 }
2225 if (objc == 3) {
2226 if (wmPtr->hints.flags & IconPixmapHint) {
2227 Tcl_SetResult(interp, (char *)
2228 Tk_NameOfBitmap(winPtr->display, wmPtr->hints.icon_pixmap),
2229 TCL_STATIC);
2230 }
2231 return TCL_OK;
2232 }
2233 argv3 = Tcl_GetString(objv[3]);
2234 if (*argv3 == '\0') {
2235 if (wmPtr->hints.icon_pixmap != None) {
2236 Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
2237 wmPtr->hints.icon_pixmap = None;
2238 }
2239 wmPtr->hints.flags &= ~IconPixmapHint;
2240 } else {
2241 pixmap = Tk_GetBitmap(interp, (Tk_Window) winPtr, argv3);
2242 if (pixmap == None) {
2243 return TCL_ERROR;
2244 }
2245 wmPtr->hints.icon_pixmap = pixmap;
2246 wmPtr->hints.flags |= IconPixmapHint;
2247 }
2248 UpdateHints(winPtr);
2249 return TCL_OK;
2250 }
2251
2252 /*
2253 *----------------------------------------------------------------------
2254 *
2255 * WmIconifyCmd --
2256 *
2257 * This function is invoked to process the "wm iconify" Tcl command. See
2258 * the user documentation for details on what it does.
2259 *
2260 * Results:
2261 * A standard Tcl result.
2262 *
2263 * Side effects:
2264 * See the user documentation.
2265 *
2266 *----------------------------------------------------------------------
2267 */
2268
2269 static int
2270 WmIconifyCmd(
2271 Tk_Window tkwin, /* Main window of the application. */
2272 TkWindow *winPtr, /* Toplevel to work with */
2273 Tcl_Interp *interp, /* Current interpreter. */
2274 int objc, /* Number of arguments. */
2275 Tcl_Obj *CONST objv[]) /* Argument objects. */
2276 {
2277 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2278 if (objc != 3) {
2279 Tcl_WrongNumArgs(interp, 2, objv, "window");
2280 return TCL_ERROR;
2281 }
2282 if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
2283 Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
2284 "\": override-redirect flag is set", NULL);
2285 return TCL_ERROR;
2286 }
2287 if (wmPtr->masterPtr != NULL) {
2288 Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
2289 "\": it is a transient", NULL);
2290 return TCL_ERROR;
2291 }
2292 if (wmPtr->iconFor != NULL) {
2293 Tcl_AppendResult(interp, "can't iconify ", winPtr->pathName,
2294 ": it is an icon for ", Tk_PathName(wmPtr->iconFor), NULL);
2295 return TCL_ERROR;
2296 }
2297 if (winPtr->flags & TK_EMBEDDED) {
2298 Tcl_AppendResult(interp, "can't iconify ", winPtr->pathName,
2299 ": it is an embedded window", NULL);
2300 return TCL_ERROR;
2301 }
2302 if (TkpWmSetState(winPtr, IconicState) == 0) {
2303 Tcl_SetResult(interp,
2304 "couldn't send iconify message to window manager",
2305 TCL_STATIC);
2306 return TCL_ERROR;
2307 }
2308 return TCL_OK;
2309 }
2310
2311 /*
2312 *----------------------------------------------------------------------
2313 *
2314 * WmIconmaskCmd --
2315 *
2316 * This function is invoked to process the "wm iconmask" Tcl command.
2317 * See the user documentation for details on what it does.
2318 *
2319 * Results:
2320 * A standard Tcl result.
2321 *
2322 * Side effects:
2323 * See the user documentation.
2324 *
2325 *----------------------------------------------------------------------
2326 */
2327
2328 static int
2329 WmIconmaskCmd(
2330 Tk_Window tkwin, /* Main window of the application. */
2331 TkWindow *winPtr, /* Toplevel to work with */
2332 Tcl_Interp *interp, /* Current interpreter. */
2333 int objc, /* Number of arguments. */
2334 Tcl_Obj *CONST objv[]) /* Argument objects. */
2335 {
2336 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2337 Pixmap pixmap;
2338 char *argv3;
2339
2340 if ((objc != 3) && (objc != 4)) {
2341 Tcl_WrongNumArgs(interp, 2, objv, "window ?bitmap?");
2342 return TCL_ERROR;
2343 }
2344 if (objc == 3) {
2345 if (wmPtr->hints.flags & IconMaskHint) {
2346 Tcl_SetResult(interp, (char *)
2347 Tk_NameOfBitmap(winPtr->display, wmPtr->hints.icon_mask),
2348 TCL_STATIC);
2349 }
2350 return TCL_OK;
2351 }
2352 argv3 = Tcl_GetString(objv[3]);
2353 if (*argv3 == '\0') {
2354 if (wmPtr->hints.icon_mask != None) {
2355 Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_mask);
2356 }
2357 wmPtr->hints.flags &= ~IconMaskHint;
2358 } else {
2359 pixmap = Tk_GetBitmap(interp, tkwin, argv3);
2360 if (pixmap == None) {
2361 return TCL_ERROR;
2362 }
2363 wmPtr->hints.icon_mask = pixmap;
2364 wmPtr->hints.flags |= IconMaskHint;
2365 }
2366 UpdateHints(winPtr);
2367 return TCL_OK;
2368 }
2369
2370 /*
2371 *----------------------------------------------------------------------
2372 *
2373 * WmIconnameCmd --
2374 *
2375 * This function is invoked to process the "wm iconname" Tcl command.
2376 * See the user documentation for details on what it does.
2377 *
2378 * Results:
2379 * A standard Tcl result.
2380 *
2381 * Side effects:
2382 * See the user documentation.
2383 *
2384 *----------------------------------------------------------------------
2385 */
2386
2387 static int
2388 WmIconnameCmd(
2389 Tk_Window tkwin, /* Main window of the application. */
2390 TkWindow *winPtr, /* Toplevel to work with */
2391 Tcl_Interp *interp, /* Current interpreter. */
2392 int objc, /* Number of arguments. */
2393 Tcl_Obj *CONST objv[]) /* Argument objects. */
2394 {
2395 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2396 char *argv3;
2397 int length;
2398
2399 if (objc > 4) {
2400 Tcl_WrongNumArgs(interp, 2, objv, "window ?newName?");
2401 return TCL_ERROR;
2402 }
2403 if (objc == 3) {
2404 Tcl_SetResult(interp,
2405 ((wmPtr->iconName != NULL) ? wmPtr->iconName : ""),
2406 TCL_STATIC);
2407 return TCL_OK;
2408 } else {
2409 if (wmPtr->iconName != NULL) {
2410 ckfree((char *) wmPtr->iconName);
2411 }
2412 argv3 = Tcl_GetStringFromObj(objv[3], &length);
2413 wmPtr->iconName = ckalloc((unsigned) (length + 1));
2414 strcpy(wmPtr->iconName, argv3);
2415 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
2416 UpdateTitle(winPtr);
2417 }
2418 }
2419 return TCL_OK;
2420 }
2421
2422 /*
2423 *----------------------------------------------------------------------
2424 *
2425 * WmIconphotoCmd --
2426 *
2427 * This function is invoked to process the "wm iconphoto" Tcl command.
2428 * See the user documentation for details on what it does.
2429 *
2430 * Results:
2431 * A standard Tcl result.
2432 *
2433 * Side effects:
2434 * See the user documentation.
2435 *
2436 *----------------------------------------------------------------------
2437 */
2438
2439 static int
2440 WmIconphotoCmd(
2441 Tk_Window tkwin, /* Main window of the application. */
2442 TkWindow *winPtr, /* Toplevel to work with */
2443 Tcl_Interp *interp, /* Current interpreter. */
2444 int objc, /* Number of arguments. */
2445 Tcl_Obj *CONST objv[]) /* Argument objects. */
2446 {
2447 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2448 Tk_PhotoHandle photo;
2449 Tk_PhotoImageBlock block;
2450 int i, size = 0, width, height, index = 0, x, y, isDefault = 0;
2451 long R, G, B, A;
2452 long *iconPropertyData;
2453 unsigned char *pixelByte;
2454
2455 if (objc < 4) {
2456 Tcl_WrongNumArgs(interp, 2, objv,
2457 "window ?-default? image1 ?image2 ...?");
2458 return TCL_ERROR;
2459 }
2460 if (strcmp(Tcl_GetString(objv[3]), "-default") == 0) {
2461 isDefault = 1;
2462 if (objc == 4) {
2463 Tcl_WrongNumArgs(interp, 2, objv,
2464 "window ?-default? image1 ?image2 ...?");
2465 return TCL_ERROR;
2466 }
2467 }
2468
2469 /*
2470 * Iterate over all images to retrieve their sizes, in order to allocate a
2471 * buffer large enough to hold all images.
2472 */
2473
2474 for (i = 3 + isDefault; i < objc; i++) {
2475 photo = Tk_FindPhoto(interp, Tcl_GetString(objv[i]));
2476 if (photo == NULL) {
2477 Tcl_AppendResult(interp, "can't use \"", Tcl_GetString(objv[i]),
2478 "\" as iconphoto: not a photo image", NULL);
2479 return TCL_ERROR;
2480 }
2481 Tk_PhotoGetSize(photo, &width, &height);
2482
2483 /*
2484 * We need to cardinals for width & height and one cardinal for each
2485 * image pixel.
2486 */
2487
2488 size += 2 + width * height;
2489 }
2490
2491 /*
2492 * We have calculated the size of the data. Try to allocate the needed
2493 * memory space.
2494 */
2495
2496 #ifdef NOTYET_XXX
2497 iconPropertyData = (long *) Tcl_AttemptAlloc(sizeof(long)*size);
2498 #else
2499 iconPropertyData = (long *) ckalloc(sizeof(long)*size);
2500 #endif
2501 if (iconPropertyData == NULL) {
2502 return TCL_ERROR;
2503 }
2504 memset(iconPropertyData, 0, sizeof(long)*size);
2505
2506 for (i = 3 + isDefault; i < objc; i++) {
2507 photo = Tk_FindPhoto(interp, Tcl_GetString(objv[i]));
2508 if (photo == NULL) {
2509 Tcl_Free((char *) iconPropertyData);
2510 return TCL_ERROR;
2511 }
2512 Tk_PhotoGetSize(photo, &width, &height);
2513 Tk_PhotoGetImage(photo, &block);
2514
2515 /*
2516 * Each image data will be placed as an array of 32bit packed
2517 * CARDINAL, in a window property named "_NET_WM_ICON": _NET_WM_ICON
2518 *
2519 * _NET_WM_ICON CARDINAL[][2+n]/32
2520 *
2521 * This is an array of possible icons for the client. This spec. does
2522 * not stipulate what size these icons should be, but individual
2523 * desktop environments or toolkits may do so. The Window Manager MAY
2524 * scale any of these icons to an appropriate size.
2525 *
2526 * This is an array of 32bit packed CARDINAL ARGB with high byte being
2527 * A, low byte being B. The first two cardinals are width, height.
2528 * Data is in rows, left to right and top to bottom.
2529 */
2530
2531 /*
2532 * Encode the image data in the iconPropertyData array.
2533 */
2534
2535 iconPropertyData[index++] = width;
2536 iconPropertyData[index++] = height;
2537 for (y = 0; y < height; y++) {
2538 for (x = 0; x < width; x++) {
2539 R = *(block.pixelPtr + x*block.pixelSize +
2540 y*block.pitch + block.offset[0]);
2541 G = *(block.pixelPtr + x*block.pixelSize +
2542 y*block.pitch + block.offset[1]);
2543 B = *(block.pixelPtr + x*block.pixelSize +
2544 y*block.pitch + block.offset[2]);
2545 A = *(block.pixelPtr + x*block.pixelSize +
2546 y*block.pitch + block.offset[3]);
2547 pixelByte = (unsigned char *) &iconPropertyData[index];
2548 pixelByte[3] = A;
2549 pixelByte[2] = R;
2550 pixelByte[1] = G;
2551 pixelByte[0] = B;
2552 index++;
2553 }
2554 }
2555 }
2556 if (wmPtr->iconDataPtr != NULL) {
2557 ckfree((char *)wmPtr->iconDataPtr);
2558 wmPtr->iconDataPtr = NULL;
2559 }
2560 if (isDefault) {
2561 if (winPtr->dispPtr->iconDataPtr != NULL) {
2562 ckfree((char *) winPtr->dispPtr->iconDataPtr);
2563 }
2564 winPtr->dispPtr->iconDataPtr = (unsigned char *) iconPropertyData;
2565 winPtr->dispPtr->iconDataSize = size;
2566 } else {
2567 wmPtr->iconDataPtr = (unsigned char *) iconPropertyData;
2568 wmPtr->iconDataSize = size;
2569 }
2570 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
2571 UpdatePhotoIcon(winPtr);
2572 }
2573 return TCL_OK;
2574 }
2575
2576 /*
2577 *----------------------------------------------------------------------
2578 *
2579 * WmIconpositionCmd --
2580 *
2581 * This function is invoked to process the "wm iconposition" Tcl command.
2582 * See the user documentation for details on what it does.
2583 *
2584 * Results:
2585 * A standard Tcl result.
2586 *
2587 * Side effects:
2588 * See the user documentation.
2589 *
2590 *----------------------------------------------------------------------
2591 */
2592
2593 static int
2594 WmIconpositionCmd(
2595 Tk_Window tkwin, /* Main window of the application. */
2596 TkWindow *winPtr, /* Toplevel to work with */
2597 Tcl_Interp *interp, /* Current interpreter. */
2598 int objc, /* Number of arguments. */
2599 Tcl_Obj *CONST objv[]) /* Argument objects. */
2600 {
2601 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2602 int x, y;
2603
2604 if ((objc != 3) && (objc != 5)) {
2605 Tcl_WrongNumArgs(interp, 2, objv, "window ?x y?");
2606 return TCL_ERROR;
2607 }
2608 if (objc == 3) {
2609 if (wmPtr->hints.flags & IconPositionHint) {
2610 Tcl_IntResults(interp, 2, 0, wmPtr->hints.icon_x,
2611 wmPtr->hints.icon_y);
2612 }
2613 return TCL_OK;
2614 }
2615 if (*Tcl_GetString(objv[3]) == '\0') {
2616 wmPtr->hints.flags &= ~IconPositionHint;
2617 } else {
2618 if ((Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK)
2619 || (Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK)){
2620 return TCL_ERROR;
2621 }
2622 wmPtr->hints.icon_x = x;
2623 wmPtr->hints.icon_y = y;
2624 wmPtr->hints.flags |= IconPositionHint;
2625 }
2626 UpdateHints(winPtr);
2627 return TCL_OK;
2628 }
2629
2630 /*
2631 *----------------------------------------------------------------------
2632 *
2633 * WmIconwindowCmd --
2634 *
2635 * This function is invoked to process the "wm iconwindow" Tcl command.
2636 * See the user documentation for details on what it does.
2637 *
2638 * Results:
2639 * A standard Tcl result.
2640 *
2641 * Side effects:
2642 * See the user documentation.
2643 *
2644 *----------------------------------------------------------------------
2645 */
2646
2647 static int
2648 WmIconwindowCmd(
2649 Tk_Window tkwin, /* Main window of the application. */
2650 TkWindow *winPtr, /* Toplevel to work with */
2651 Tcl_Interp *interp, /* Current interpreter. */
2652 int objc, /* Number of arguments. */
2653 Tcl_Obj *CONST objv[]) /* Argument objects. */
2654 {
2655 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2656 Tk_Window tkwin2;
2657 WmInfo *wmPtr2;
2658 XSetWindowAttributes atts;
2659
2660 if ((objc != 3) && (objc != 4)) {
2661 Tcl_WrongNumArgs(interp, 2, objv, "window ?pathName?");
2662 return TCL_ERROR;
2663 }
2664 if (objc == 3) {
2665 if (wmPtr->icon != NULL) {
2666 Tcl_SetResult(interp, Tk_PathName(wmPtr->icon), TCL_STATIC);
2667 }
2668 return TCL_OK;
2669 }
2670 if (*Tcl_GetString(objv[3]) == '\0') {
2671 wmPtr->hints.flags &= ~IconWindowHint;
2672 if (wmPtr->icon != NULL) {
2673 /*
2674 * Remove the icon window relationship. In principle we should
2675 * also re-enable button events for the window, but this doesn't
2676 * work in general because the window manager is probably
2677 * selecting on them (we'll get an error if we try to re-enable
2678 * the events). So, just leave the icon window event-challenged;
2679 * the user will have to recreate it if they want button events.
2680 */
2681
2682 wmPtr2 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
2683 wmPtr2->iconFor = NULL;
2684 wmPtr2->withdrawn = 1;
2685 wmPtr2->hints.initial_state = WithdrawnState;
2686 }
2687 wmPtr->icon = NULL;
2688 } else {
2689 if (TkGetWindowFromObj(interp, tkwin, objv[3], &tkwin2) != TCL_OK) {
2690 return TCL_ERROR;
2691 }
2692 if (!Tk_IsTopLevel(tkwin2)) {
2693 Tcl_AppendResult(interp, "can't use ", Tcl_GetString(objv[3]),
2694 " as icon window: not at top level", NULL);
2695 return TCL_ERROR;
2696 }
2697 wmPtr2 = ((TkWindow *) tkwin2)->wmInfoPtr;
2698 if (wmPtr2->iconFor != NULL) {
2699 Tcl_AppendResult(interp, Tcl_GetString(objv[3]),
2700 " is already an icon for ", Tk_PathName(wmPtr2->iconFor),
2701 NULL);
2702 return TCL_ERROR;
2703 }
2704 if (wmPtr->icon != NULL) {
2705 WmInfo *wmPtr3 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
2706 wmPtr3->iconFor = NULL;
2707 wmPtr3->withdrawn = 1;
2708 wmPtr3->hints.initial_state = WithdrawnState;
2709 }
2710
2711 /*
2712 * Disable button events in the icon window: some window managers
2713 * (like olvwm) want to get the events themselves, but X only allows
2714 * one application at a time to receive button events for a window.
2715 */
2716
2717 atts.event_mask = Tk_Attributes(tkwin2)->event_mask
2718 & ~ButtonPressMask;
2719 Tk_ChangeWindowAttributes(tkwin2, CWEventMask, &atts);
2720 Tk_MakeWindowExist(tkwin2);
2721 if (wmPtr2->wrapperPtr == NULL) {
2722 CreateWrapper(wmPtr2);
2723 }
2724 wmPtr->hints.icon_window = Tk_WindowId(wmPtr2->wrapperPtr);
2725 wmPtr->hints.flags |= IconWindowHint;
2726 wmPtr->icon = tkwin2;
2727 wmPtr2->iconFor = (Tk_Window) winPtr;
2728 if (!wmPtr2->withdrawn && !(wmPtr2->flags & WM_NEVER_MAPPED)) {
2729 wmPtr2->withdrawn = 0;
2730 if (XWithdrawWindow(Tk_Display(tkwin2),
2731 Tk_WindowId(wmPtr2->wrapperPtr),
2732 Tk_ScreenNumber(tkwin2)) == 0) {
2733 Tcl_SetResult(interp,
2734 "couldn't send withdraw message to window manager",
2735 TCL_STATIC);
2736 return TCL_ERROR;
2737 }
2738 WaitForMapNotify((TkWindow *) tkwin2, 0);
2739 }
2740 }
2741 UpdateHints(winPtr);
2742 return TCL_OK;
2743 }
2744
2745 /*
2746 *----------------------------------------------------------------------
2747 *
2748 * WmMaxsizeCmd --
2749 *
2750 * This function is invoked to process the "wm maxsize" Tcl command. See
2751 * the user documentation for details on what it does.
2752 *
2753 * Results:
2754 * A standard Tcl result.
2755 *
2756 * Side effects:
2757 * See the user documentation.
2758 *
2759 *----------------------------------------------------------------------
2760 */
2761
2762 static int
2763 WmMaxsizeCmd(
2764 Tk_Window tkwin, /* Main window of the application. */
2765 TkWindow *winPtr, /* Toplevel to work with */
2766 Tcl_Interp *interp, /* Current interpreter. */
2767 int objc, /* Number of arguments. */
2768 Tcl_Obj *CONST objv[]) /* Argument objects. */
2769 {
2770 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2771 int width, height;
2772
2773 if ((objc != 3) && (objc != 5)) {
2774 Tcl_WrongNumArgs(interp, 2, objv, "window ?width height?");
2775 return TCL_ERROR;
2776 }
2777 if (objc == 3) {
2778
2779 GetMaxSize(wmPtr, &width, &height);
2780 Tcl_IntResults(interp, 2, 0, width, height);
2781 return TCL_OK;
2782 }
2783 if ((Tcl_GetIntFromObj(interp, objv[3], &width) != TCL_OK)
2784 || (Tcl_GetIntFromObj(interp, objv[4], &height) != TCL_OK)) {
2785 return TCL_ERROR;
2786 }
2787 wmPtr->maxWidth = width;
2788 wmPtr->maxHeight = height;
2789 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
2790
2791 if (width <= 0 && height <= 0) {
2792 wmPtr->sizeHintsFlags &= ~PMaxSize;
2793 } else {
2794 wmPtr->sizeHintsFlags |= PMaxSize;
2795 }
2796
2797 WmUpdateGeom(wmPtr, winPtr);
2798 return TCL_OK;
2799 }
2800
2801 /*
2802 *----------------------------------------------------------------------
2803 *
2804 * WmMinsizeCmd --
2805 *
2806 * This function is invoked to process the "wm minsize" Tcl command. See
2807 * the user documentation for details on what it does.
2808 *
2809 * Results:
2810 * A standard Tcl result.
2811 *
2812 * Side effects:
2813 * See the user documentation.
2814 *
2815 *----------------------------------------------------------------------
2816 */
2817
2818 static int
2819 WmMinsizeCmd(
2820 Tk_Window tkwin, /* Main window of the application. */
2821 TkWindow *winPtr, /* Toplevel to work with */
2822 Tcl_Interp *interp, /* Current interpreter. */
2823 int objc, /* Number of arguments. */
2824 Tcl_Obj *CONST objv[]) /* Argument objects. */
2825 {
2826 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2827 int width, height;
2828
2829 if ((objc != 3) && (objc != 5)) {
2830 Tcl_WrongNumArgs(interp, 2, objv, "window ?width height?");
2831 return TCL_ERROR;
2832 }
2833 if (objc == 3) {
2834 Tcl_IntResults(interp, 2, 0, wmPtr->minWidth, wmPtr->minHeight);
2835 return TCL_OK;
2836 }
2837 if ((Tcl_GetIntFromObj(interp, objv[3], &width) != TCL_OK)
2838 || (Tcl_GetIntFromObj(interp, objv[4], &height) != TCL_OK)) {
2839 return TCL_ERROR;
2840 }
2841 wmPtr->minWidth = width;
2842 wmPtr->minHeight = height;
2843 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
2844 WmUpdateGeom(wmPtr, winPtr);
2845 return TCL_OK;
2846 }
2847
2848 /*
2849 *----------------------------------------------------------------------
2850 *
2851 * WmOverrideredirectCmd --
2852 *
2853 * This function is invoked to process the "wm overrideredirect" Tcl
2854 * command. See the user documentation for details on what it does.
2855 *
2856 * Results:
2857 * A standard Tcl result.
2858 *
2859 * Side effects:
2860 * See the user documentation.
2861 *
2862 *----------------------------------------------------------------------
2863 */
2864
2865 static int
2866 WmOverrideredirectCmd(
2867 Tk_Window tkwin, /* Main window of the application. */
2868 TkWindow *winPtr, /* Toplevel to work with */
2869 Tcl_Interp *interp, /* Current interpreter. */
2870 int objc, /* Number of arguments. */
2871 Tcl_Obj *CONST objv[]) /* Argument objects. */
2872 {
2873 int boolean, curValue;
2874 XSetWindowAttributes atts;
2875
2876 if ((objc != 3) && (objc != 4)) {
2877 Tcl_WrongNumArgs(interp, 2, objv, "window ?boolean?");
2878 return TCL_ERROR;
2879 }
2880 curValue = Tk_Attributes((Tk_Window) winPtr)->override_redirect;
2881 if (objc == 3) {
2882 Tcl_SetBooleanObj(Tcl_GetObjResult(interp), curValue);
2883 return TCL_OK;
2884 }
2885 if (Tcl_GetBooleanFromObj(interp, objv[3], &boolean) != TCL_OK) {
2886 return TCL_ERROR;
2887 }
2888 if (curValue != boolean) {
2889 /*
2890 * Only do this if we are really changing value, because it causes
2891 * some funky stuff to occur
2892 */
2893
2894 atts.override_redirect = (boolean) ? True : False;
2895 atts.save_under = (boolean) ? True : False;
2896
2897 Tk_ChangeWindowAttributes((Tk_Window) winPtr, CWOverrideRedirect|CWSaveUnder,
2898 &atts);
2899 if (winPtr->wmInfoPtr->wrapperPtr != NULL) {
2900 Tk_ChangeWindowAttributes(
2901 (Tk_Window) winPtr->wmInfoPtr->wrapperPtr,
2902 CWOverrideRedirect, &atts);
2903 }
2904 }
2905 return TCL_OK;
2906 }
2907
2908 /*
2909 *----------------------------------------------------------------------
2910 *
2911 * WmPositionfromCmd --
2912 *
2913 * This function is invoked to process the "wm positionfrom" Tcl command.
2914 * See the user documentation for details on what it does.
2915 *
2916 * Results:
2917 * A standard Tcl result.
2918 *
2919 * Side effects:
2920 * See the user documentation.
2921 *
2922 *----------------------------------------------------------------------
2923 */
2924
2925 static int
2926 WmPositionfromCmd(
2927 Tk_Window tkwin, /* Main window of the application. */
2928 TkWindow *winPtr, /* Toplevel to work with */
2929 Tcl_Interp *interp, /* Current interpreter. */
2930 int objc, /* Number of arguments. */
2931 Tcl_Obj *CONST objv[]) /* Argument objects. */
2932 {
2933 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2934 static CONST char *optionStrings[] = {
2935 "program", "user", NULL };
2936 enum options {
2937 OPT_PROGRAM, OPT_USER };
2938 int index;
2939
2940 if ((objc != 3) && (objc != 4)) {
2941 Tcl_WrongNumArgs(interp, 2, objv, "window ?user/program?");
2942 return TCL_ERROR;
2943 }
2944 if (objc == 3) {
2945 if (wmPtr->sizeHintsFlags & USPosition) {
2946 Tcl_SetResult(interp, "user", TCL_STATIC);
2947 } else if (wmPtr->sizeHintsFlags & PPosition) {
2948 Tcl_SetResult(interp, "program", TCL_STATIC);
2949 }
2950 return TCL_OK;
2951 }
2952 if (*Tcl_GetString(objv[3]) == '\0') {
2953 wmPtr->sizeHintsFlags &= ~(USPosition|PPosition);
2954 } else {
2955 if (Tcl_GetIndexFromObj(interp, objv[3], optionStrings, "argument", 0,
2956 &index) != TCL_OK) {
2957 return TCL_ERROR;
2958 }
2959 if (index == OPT_USER) {
2960 wmPtr->sizeHintsFlags &= ~PPosition;
2961 wmPtr->sizeHintsFlags |= USPosition;
2962 } else {
2963 wmPtr->sizeHintsFlags &= ~USPosition;
2964 wmPtr->sizeHintsFlags |= PPosition;
2965 }
2966 }
2967 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
2968 WmUpdateGeom(wmPtr, winPtr);
2969 return TCL_OK;
2970 }
2971
2972 /*
2973 *----------------------------------------------------------------------
2974 *
2975 * WmProtocolCmd --
2976 *
2977 * This function is invoked to process the "wm protocol" Tcl command. See
2978 * the user documentation for details on what it does.
2979 *
2980 * Results:
2981 * A standard Tcl result.
2982 *
2983 * Side effects:
2984 * See the user documentation.
2985 *
2986 *----------------------------------------------------------------------
2987 */
2988
2989 static int
2990 WmProtocolCmd(
2991 Tk_Window tkwin, /* Main window of the application. */
2992 TkWindow *winPtr, /* Toplevel to work with */
2993 Tcl_Interp *interp, /* Current interpreter. */
2994 int objc, /* Number of arguments. */
2995 Tcl_Obj *CONST objv[]) /* Argument objects. */
2996 {
2997 register WmInfo *wmPtr = winPtr->wmInfoPtr;
2998 register ProtocolHandler *protPtr, *prevPtr;
2999 Atom protocol;
3000 char *cmd;
3001 int cmdLength;
3002
3003 if ((objc < 3) || (objc > 5)) {
3004 Tcl_WrongNumArgs(interp, 2, objv, "window ?name? ?command?");
3005 return TCL_ERROR;
3006 }
3007 if (objc == 3) {
3008 /*
3009 * Return a list of all defined protocols for the window.
3010 */
3011
3012 for (protPtr = wmPtr->protPtr; protPtr != NULL;
3013 protPtr = protPtr->nextPtr) {
3014 Tcl_AppendElement(interp,
3015 Tk_GetAtomName((Tk_Window) winPtr, protPtr->protocol));
3016 }
3017 return TCL_OK;
3018 }
3019 protocol = Tk_InternAtom((Tk_Window) winPtr, Tcl_GetString(objv[3]));
3020 if (objc == 4) {
3021 /*
3022 * Return the command to handle a given protocol.
3023 */
3024
3025 for (protPtr = wmPtr->protPtr; protPtr != NULL;
3026 protPtr = protPtr->nextPtr) {
3027 if (protPtr->protocol == protocol) {
3028 Tcl_SetObjResult(interp,LangCallbackObj(protPtr->command));
3029 return TCL_OK;
3030 }
3031 }
3032 return TCL_OK;
3033 }
3034
3035 /*
3036 * Delete any current protocol handler, then create a new one with the
3037 * specified command, unless the command is empty.
3038 */
3039
3040 for (protPtr = wmPtr->protPtr, prevPtr = NULL; protPtr != NULL;
3041 prevPtr = protPtr, protPtr = protPtr->nextPtr) {
3042 if (protPtr->protocol == protocol) {
3043 if (prevPtr == NULL) {
3044 wmPtr->protPtr = protPtr->nextPtr;
3045 } else {
3046 prevPtr->nextPtr = protPtr->nextPtr;
3047 }
3048 Tcl_EventuallyFree((ClientData) protPtr, ProtocolFree);
3049 break;
3050 }
3051 }
3052 cmd = Tcl_GetStringFromObj(objv[4], &cmdLength);
3053 if (cmdLength > 0) {
3054 #ifndef _LANG
3055 protPtr = (ProtocolHandler *) ckalloc(HANDLER_SIZE(cmdLength));
3056 #else
3057 protPtr = (ProtocolHandler *) ckalloc(sizeof(ProtocolHandler));
3058 #endif
3059 protPtr->protocol = protocol;
3060 protPtr->nextPtr = wmPtr->protPtr;
3061 wmPtr->protPtr = protPtr;
3062 protPtr->interp = interp;
3063 #ifndef _LANG
3064 strcpy(protPtr->command, cmd);
3065 #else
3066 protPtr->command = LangMakeCallback(objv[4]);
3067 #endif
3068 }
3069 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
3070 UpdateWmProtocols(wmPtr);
3071 }
3072 return TCL_OK;
3073 }
3074
3075 /*
3076 *----------------------------------------------------------------------
3077 *
3078 * WmResizableCmd --
3079 *
3080 * This function is invoked to process the "wm resizable" Tcl command.
3081 * See the user documentation for details on what it does.
3082 *
3083 * Results:
3084 * A standard Tcl result.
3085 *
3086 * Side effects:
3087 * See the user documentation.
3088 *
3089 *----------------------------------------------------------------------
3090 */
3091
3092 static int
3093 WmResizableCmd(
3094 Tk_Window tkwin, /* Main window of the application. */
3095 TkWindow *winPtr, /* Toplevel to work with */
3096 Tcl_Interp *interp, /* Current interpreter. */
3097 int objc, /* Number of arguments. */
3098 Tcl_Obj *CONST objv[]) /* Argument objects. */
3099 {
3100 register WmInfo *wmPtr = winPtr->wmInfoPtr;
3101 int width, height;
3102
3103 if ((objc != 3) && (objc != 5)) {
3104 Tcl_WrongNumArgs(interp, 2, objv, "window ?width height?");
3105 return TCL_ERROR;
3106 }
3107 if (objc == 3) {
3108 Tcl_IntResults(interp, 2, 0,
3109 (wmPtr->flags & WM_WIDTH_NOT_RESIZABLE) ? 0 : 1,
3110 (wmPtr->flags & WM_HEIGHT_NOT_RESIZABLE) ? 0 : 1);
3111 return TCL_OK;
3112 }
3113 if ((Tcl_GetBooleanFromObj(interp, objv[3], &width) != TCL_OK)
3114 || (Tcl_GetBooleanFromObj(interp, objv[4], &height) != TCL_OK)) {
3115 return TCL_ERROR;
3116 }
3117 if (width) {
3118 wmPtr->flags &= ~WM_WIDTH_NOT_RESIZABLE;
3119 } else {
3120 wmPtr->flags |= WM_WIDTH_NOT_RESIZABLE;
3121 }
3122 if (height) {
3123 wmPtr->flags &= ~WM_HEIGHT_NOT_RESIZABLE;
3124 } else {
3125 wmPtr->flags |= WM_HEIGHT_NOT_RESIZABLE;
3126 }
3127 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
3128 WmUpdateGeom(wmPtr, winPtr);
3129 return TCL_OK;
3130 }
3131
3132 /*
3133 *----------------------------------------------------------------------
3134 *
3135 * WmSizefromCmd --
3136 *
3137 * This function is invoked to process the "wm sizefrom" Tcl command. See
3138 * the user documentation for details on what it does.
3139 *
3140 * Results:
3141 * A standard Tcl result.
3142 *
3143 * Side effects:
3144 * See the user documentation.
3145 *
3146 *----------------------------------------------------------------------
3147 */
3148
3149 static int
3150 WmSizefromCmd(
3151 Tk_Window tkwin, /* Main window of the application. */
3152 TkWindow *winPtr, /* Toplevel to work with */
3153 Tcl_Interp *interp, /* Current interpreter. */
3154 int objc, /* Number of arguments. */
3155 Tcl_Obj *CONST objv[]) /* Argument objects. */
3156 {
3157 register WmInfo *wmPtr = winPtr->wmInfoPtr;
3158 static CONST char *optionStrings[] = {
3159 "program", "user", NULL };
3160 enum options {
3161 OPT_PROGRAM, OPT_USER };
3162 int index;
3163
3164 if ((objc != 3) && (objc != 4)) {
3165 Tcl_WrongNumArgs(interp, 2, objv, "window ?user|program?");
3166 return TCL_ERROR;
3167 }
3168 if (objc == 3) {
3169 if (wmPtr->sizeHintsFlags & USSize) {
3170 Tcl_SetResult(interp, "user", TCL_STATIC);
3171 } else if (wmPtr->sizeHintsFlags & PSize) {
3172 Tcl_SetResult(interp, "program", TCL_STATIC);
3173 }
3174 return TCL_OK;
3175 }
3176
3177 if (*Tcl_GetString(objv[3]) == '\0') {
3178 wmPtr->sizeHintsFlags &= ~(USSize|PSize);
3179 } else {
3180 if (Tcl_GetIndexFromObj(interp, objv[3], optionStrings, "argument", 0,
3181 &index) != TCL_OK) {
3182 return TCL_ERROR;
3183 }
3184 if (index == OPT_USER) {
3185 wmPtr->sizeHintsFlags &= ~PSize;
3186 wmPtr->sizeHintsFlags |= USSize;
3187 } else { /* OPT_PROGRAM */
3188 wmPtr->sizeHintsFlags &= ~USSize;
3189 wmPtr->sizeHintsFlags |= PSize;
3190 }
3191 }
3192 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
3193 WmUpdateGeom(wmPtr, winPtr);
3194 return TCL_OK;
3195 }
3196
3197 /*
3198 *----------------------------------------------------------------------
3199 *
3200 * WmStackorderCmd --
3201 *
3202 * This function is invoked to process the "wm stackorder" Tcl command.
3203 * See the user documentation for details on what it does.
3204 *
3205 * Results:
3206 * A standard Tcl result.
3207 *
3208 * Side effects:
3209 * See the user documentation.
3210 *
3211 *----------------------------------------------------------------------
3212 */
3213
3214 static int
3215 WmStackorderCmd(
3216 Tk_Window tkwin, /* Main window of the application. */
3217 TkWindow *winPtr, /* Toplevel to work with */
3218 Tcl_Interp *interp, /* Current interpreter. */
3219 int objc, /* Number of arguments. */
3220 Tcl_Obj *CONST objv[]) /* Argument objects. */
3221 {
3222 TkWindow **windows, **window_ptr;
3223 static CONST char *optionStrings[] = {
3224 "isabove", "isbelow", NULL };
3225 enum options {
3226 OPT_ISABOVE, OPT_ISBELOW };
3227 int index;
3228
3229 if ((objc != 3) && (objc != 5)) {
3230 Tcl_WrongNumArgs(interp, 2, objv, "window ?isabove|isbelow window?");
3231 return TCL_ERROR;
3232 }
3233
3234 if (objc == 3) {
3235 windows = TkWmStackorderToplevel(winPtr);
3236 if (windows == NULL) {
3237 Tcl_Panic("TkWmStackorderToplevel failed");
3238 } else {
3239 for (window_ptr = windows; *window_ptr ; window_ptr++) {
3240 Tcl_AppendElement(interp, (*window_ptr)->pathName);
3241 }
3242 ckfree((char *) windows);
3243 return TCL_OK;
3244 }
3245 } else {
3246 TkWindow *winPtr2;
3247 int index1=-1, index2=-1, result;
3248
3249 if (TkGetWindowFromObj(interp, tkwin, objv[4], (Tk_Window *) &winPtr2)
3250 != TCL_OK) {
3251 return TCL_ERROR;
3252 }
3253
3254 if (!Tk_IsTopLevel(winPtr2)) {
3255 Tcl_AppendResult(interp, "window \"", winPtr2->pathName,
3256 "\" isn't a top-level window", NULL);
3257 return TCL_ERROR;
3258 }
3259
3260 if (!Tk_IsMapped(winPtr)) {
3261 Tcl_AppendResult(interp, "window \"", winPtr->pathName,
3262 "\" isn't mapped", NULL);
3263 return TCL_ERROR;
3264 }
3265
3266 if (!Tk_IsMapped(winPtr2)) {
3267 Tcl_AppendResult(interp, "window \"", winPtr2->pathName,
3268 "\" isn't mapped", NULL);
3269 return TCL_ERROR;
3270 }
3271
3272 /*
3273 * Lookup stacking order of all toplevels that are children of "." and
3274 * find the position of winPtr and winPtr2 in the stacking order.
3275 */
3276
3277 windows = TkWmStackorderToplevel(winPtr->mainPtr->winPtr);
3278
3279 if (windows == NULL) {
3280 Tcl_AppendResult(interp, "TkWmStackorderToplevel failed", NULL);
3281 return TCL_ERROR;
3282 } else {
3283 for (window_ptr = windows; *window_ptr ; window_ptr++) {
3284 if (*window_ptr == winPtr) {
3285 index1 = (window_ptr - windows);
3286 }
3287 if (*window_ptr == winPtr2) {
3288 index2 = (window_ptr - windows);
3289 }
3290 }
3291 if (index1 == -1) {
3292 Tcl_Panic("winPtr window not found");
3293 }
3294 if (index2 == -1) {
3295 Tcl_Panic("winPtr2 window not found");
3296 }
3297
3298 ckfree((char *) windows);
3299 }
3300
3301 if (Tcl_GetIndexFromObj(interp, objv[3], optionStrings, "argument", 0,
3302 &index) != TCL_OK) {
3303 return TCL_ERROR;
3304 }
3305 if (index == OPT_ISABOVE) {
3306 result = index1 > index2;
3307 } else { /* OPT_ISBELOW */
3308 result = index1 < index2;
3309 }
3310 Tcl_SetIntObj(Tcl_GetObjResult(interp), result);
3311 return TCL_OK;
3312 }
3313 return TCL_OK;
3314 }
3315
3316 /*
3317 *----------------------------------------------------------------------
3318 *
3319 * WmStateCmd --
3320 *
3321 * This function is invoked to process the "wm state" Tcl command. See
3322 * the user documentation for details on what it does.
3323 *
3324 * Results:
3325 * A standard Tcl result.
3326 *
3327 * Side effects:
3328 * See the user documentation.
3329 *
3330 *----------------------------------------------------------------------
3331 */
3332
3333 static int
3334 WmStateCmd(
3335 Tk_Window tkwin, /* Main window of the application. */
3336 TkWindow *winPtr, /* Toplevel to work with */
3337 Tcl_Interp *interp, /* Current interpreter. */
3338 int objc, /* Number of arguments. */
3339 Tcl_Obj *CONST objv[]) /* Argument objects. */
3340 {
3341 register WmInfo *wmPtr = winPtr->wmInfoPtr;
3342 static CONST char *optionStrings[] = {
3343 "normal", "iconic", "withdrawn", NULL };
3344 enum options {
3345 OPT_NORMAL, OPT_ICONIC, OPT_WITHDRAWN };
3346 int index;
3347
3348 if ((objc < 3) || (objc > 4)) {
3349 Tcl_WrongNumArgs(interp, 2, objv, "window ?state?");
3350 return TCL_ERROR;
3351 }
3352 if (objc == 4) {
3353 if (wmPtr->iconFor != NULL) {
3354 Tcl_AppendResult(interp, "can't change state of ",
3355 Tcl_GetString(objv[2]), ": it is an icon for ",
3356 Tk_PathName(wmPtr->iconFor), NULL);
3357 return TCL_ERROR;
3358 }
3359
3360 if (Tcl_GetIndexFromObj(interp, objv[3], optionStrings, "argument", 0,
3361 &index) != TCL_OK) {
3362 return TCL_ERROR;
3363 }
3364
3365 if (index == OPT_NORMAL) {
3366 wmPtr->flags &= ~WM_WITHDRAWN;
3367 (void) TkpWmSetState(winPtr, NormalState);
3368 } else if (index == OPT_ICONIC) {
3369 if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
3370 Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
3371 "\": override-redirect flag is set", NULL);
3372 return TCL_ERROR;
3373 }
3374 if (wmPtr->masterPtr != NULL) {
3375 Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
3376 "\": it is a transient", NULL);
3377 return TCL_ERROR;
3378 }
3379 if (TkpWmSetState(winPtr, IconicState) == 0) {
3380 Tcl_SetResult(interp,
3381 "couldn't send iconify message to window manager",
3382 TCL_STATIC);
3383 return TCL_ERROR;
3384 }
3385 } else { /* OPT_WITHDRAWN */
3386 wmPtr->flags |= WM_WITHDRAWN;
3387 if (TkpWmSetState(winPtr, WithdrawnState) == 0) {
3388 Tcl_SetResult(interp,
3389 "couldn't send withdraw message to window manager",
3390 TCL_STATIC);
3391 return TCL_ERROR;
3392 }
3393 }
3394 } else {
3395 if (wmPtr->iconFor != NULL) {
3396 Tcl_SetResult(interp, "icon", TCL_STATIC);
3397 } else if (wmPtr->withdrawn) {
3398 Tcl_SetResult(interp, "withdrawn", TCL_STATIC);
3399 } else if (Tk_IsMapped((Tk_Window) winPtr)
3400 || ((wmPtr->flags & WM_NEVER_MAPPED)
3401 && (wmPtr->hints.initial_state == NormalState))) {
3402 Tcl_SetResult(interp, "normal", TCL_STATIC);
3403 } else {
3404 Tcl_SetResult(interp, "iconic", TCL_STATIC);
3405 }
3406 }
3407 return TCL_OK;
3408 }
3409
3410 /*
3411 *----------------------------------------------------------------------
3412 *
3413 * WmTitleCmd --
3414 *
3415 * This function is invoked to process the "wm title" Tcl command. See
3416 * the user documentation for details on what it does.
3417 *
3418 * Results:
3419 * A standard Tcl result.
3420 *
3421 * Side effects:
3422 * See the user documentation.
3423 *
3424 *----------------------------------------------------------------------
3425 */
3426
3427 static int
3428 WmTitleCmd(
3429 Tk_Window tkwin, /* Main window of the application. */
3430 TkWindow *winPtr, /* Toplevel to work with */
3431 Tcl_Interp *interp, /* Current interpreter. */
3432 int objc, /* Number of arguments. */
3433 Tcl_Obj *CONST objv[]) /* Argument objects. */
3434 {
3435 register WmInfo *wmPtr = winPtr->wmInfoPtr;
3436 char *argv3;
3437 int length;
3438
3439 if (objc > 4) {
3440 Tcl_WrongNumArgs(interp, 2, objv, "window ?newTitle?");
3441 return TCL_ERROR;
3442 }
3443 if (objc == 3) {
3444 Tcl_SetResult(interp, (char *)
3445 ((wmPtr->title != NULL) ? wmPtr->title : winPtr->nameUid),
3446 TCL_STATIC);
3447 return TCL_OK;
3448 } else {
3449 if (wmPtr->title != NULL) {
3450 ckfree((char *) wmPtr->title);
3451 }
3452 argv3 = Tcl_GetStringFromObj(objv[3], &length);
3453 wmPtr->title = ckalloc((unsigned) (length + 1));
3454 strcpy(wmPtr->title, argv3);
3455
3456 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
3457 UpdateTitle(winPtr);
3458 }
3459 }
3460 return TCL_OK;
3461 }
3462
3463 /*
3464 *----------------------------------------------------------------------
3465 *
3466 * WmTransientCmd --
3467 *
3468 * This function is invoked to process the "wm transient" Tcl command.
3469 * See the user documentation for details on what it does.
3470 *
3471 * Results:
3472 * A standard Tcl result.
3473 *
3474 * Side effects:
3475 * See the user documentation.
3476 *
3477 *----------------------------------------------------------------------
3478 */
3479
3480 static int
3481 WmTransientCmd(
3482 Tk_Window tkwin, /* Main window of the application. */
3483 TkWindow *winPtr, /* Toplevel to work with */
3484 Tcl_Interp *interp, /* Current interpreter. */
3485 int objc, /* Number of arguments. */
3486 Tcl_Obj *CONST objv[]) /* Argument objects. */
3487 {
3488 register WmInfo *wmPtr = winPtr->wmInfoPtr;
3489 TkWindow *masterPtr = wmPtr->masterPtr;
3490 WmInfo *wmPtr2;
3491
3492 if ((objc != 3) && (objc != 4)) {
3493 Tcl_WrongNumArgs(interp, 2, objv, "window ?master?");
3494 return TCL_ERROR;
3495 }
3496 if (objc == 3) {
3497 if (masterPtr != NULL) {
3498 Tk_Window master = (Tk_Window) masterPtr;
3499 Tcl_SetResult(interp, Tk_PathName(master), TCL_STATIC);
3500 }
3501 return TCL_OK;
3502 }
3503 if (Tcl_GetString(objv[3])[0] == '\0') {
3504 if (masterPtr != NULL) {
3505 /*
3506 * If we had a master, tell them that we aren't tied to them
3507 * anymore
3508 */
3509
3510 masterPtr->wmInfoPtr->numTransients--;
3511 Tk_DeleteEventHandler((Tk_Window) masterPtr, StructureNotifyMask,
3512 WmWaitMapProc, (ClientData) winPtr);
3513
3514 /*
3515 * FIXME: Need a call like Win32's UpdateWrapper() so we can
3516 * recreate the wrapper and get rid of the transient window
3517 * decorations.
3518 */
3519 }
3520
3521 wmPtr->masterPtr = NULL;
3522 } else {
3523 if (TkGetWindowFromObj(interp, tkwin, objv[3],
3524 (Tk_Window *) &masterPtr) != TCL_OK) {
3525 return TCL_ERROR;
3526 }
3527 while (!Tk_TopWinHierarchy(masterPtr)) {
3528 /*
3529 * Ensure that the master window is actually a Tk toplevel.
3530 */
3531
3532 masterPtr = masterPtr->parentPtr;
3533 }
3534 Tk_MakeWindowExist((Tk_Window) masterPtr);
3535
3536 if (wmPtr->iconFor != NULL) {
3537 Tcl_AppendResult(interp, "can't make \"", Tcl_GetString(objv[2]),
3538 "\" a transient: it is an icon for ",
3539 Tk_PathName(wmPtr->iconFor), NULL);
3540 return TCL_ERROR;
3541 }
3542
3543 wmPtr2 = masterPtr->wmInfoPtr;
3544 if (wmPtr2->wrapperPtr == NULL) {
3545 CreateWrapper(wmPtr2);
3546 }
3547
3548 if (wmPtr2->iconFor != NULL) {
3549 Tcl_AppendResult(interp, "can't make \"", Tcl_GetString(objv[3]),
3550 "\" a master: it is an icon for ",
3551 Tk_PathName(wmPtr2->iconFor), NULL);
3552 return TCL_ERROR;
3553 }
3554
3555 if (masterPtr == winPtr) {
3556 Tcl_AppendResult(interp, "can't make \"", Tk_PathName(winPtr),
3557 "\" its own master", NULL);
3558 return TCL_ERROR;
3559 } else if (masterPtr != wmPtr->masterPtr) {
3560 /*
3561 * Remove old master map/unmap binding before setting the new
3562 * master. The event handler will ensure that transient states
3563 * reflect the state of the master.
3564 */
3565
3566 if (wmPtr->masterPtr != NULL) {
3567 wmPtr->masterPtr->wmInfoPtr->numTransients--;
3568 Tk_DeleteEventHandler((Tk_Window) wmPtr->masterPtr,
3569 StructureNotifyMask,
3570 WmWaitMapProc, (ClientData) winPtr);
3571 }
3572
3573 masterPtr->wmInfoPtr->numTransients++;
3574 Tk_CreateEventHandler((Tk_Window) masterPtr,
3575 StructureNotifyMask, WmWaitMapProc, (ClientData) winPtr);
3576
3577 wmPtr->masterPtr = masterPtr;
3578 }
3579 }
3580 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
3581 if (wmPtr->masterPtr != NULL && !Tk_IsMapped(wmPtr->masterPtr)) {
3582 if (TkpWmSetState(winPtr, WithdrawnState) == 0) {
3583 Tcl_SetResult(interp,
3584 "couldn't send withdraw message to window manager",
3585 TCL_STATIC);
3586 return TCL_ERROR;
3587 }
3588 } else {
3589 if (wmPtr->masterPtr != NULL) {
3590 XSetTransientForHint(winPtr->display,
3591 wmPtr->wrapperPtr->window,
3592 wmPtr->masterPtr->wmInfoPtr->wrapperPtr->window);
3593 } else {
3594 XDeleteProperty(winPtr->display, wmPtr->wrapperPtr->window,
3595 Tk_InternAtom((Tk_Window)winPtr, "WM_TRANSIENT_FOR"));
3596 }
3597 }
3598 }
3599 return TCL_OK;
3600 }
3601
3602 /*
3603 *----------------------------------------------------------------------
3604 *
3605 * WmWithdrawCmd --
3606 *
3607 * This function is invoked to process the "wm withdraw" Tcl command. See
3608 * the user documentation for details on what it does.
3609 *
3610 * Results:
3611 * A standard Tcl result.
3612 *
3613 * Side effects:
3614 * See the user documentation.
3615 *
3616 *----------------------------------------------------------------------
3617 */
3618
3619 static int
3620 WmWithdrawCmd(
3621 Tk_Window tkwin, /* Main window of the application. */
3622 TkWindow *winPtr, /* Toplevel to work with */
3623 Tcl_Interp *interp, /* Current interpreter. */
3624 int objc, /* Number of arguments. */
3625 Tcl_Obj *CONST objv[]) /* Argument objects. */
3626 {
3627 register WmInfo *wmPtr = winPtr->wmInfoPtr;
3628
3629 if (objc != 3) {
3630 Tcl_WrongNumArgs(interp, 2, objv, "window");
3631 return TCL_ERROR;
3632 }
3633 if (wmPtr->iconFor != NULL) {
3634 Tcl_AppendResult(interp, "can't withdraw ", Tcl_GetString(objv[2]),
3635 ": it is an icon for ", Tk_PathName(wmPtr->iconFor), NULL);
3636 return TCL_ERROR;
3637 }
3638 wmPtr->flags |= WM_WITHDRAWN;
3639 if (TkpWmSetState(winPtr, WithdrawnState) == 0) {
3640 Tcl_SetResult(interp,
3641 "couldn't send withdraw message to window manager",
3642 TCL_STATIC);
3643 return TCL_ERROR;
3644 }
3645 return TCL_OK;
3646 }
3647
3648 /*
3649 * Invoked by those wm subcommands that affect geometry. Schedules a geometry
3650 * update.
3651 */
3652
3653 static void
3654 WmUpdateGeom(
3655 WmInfo *wmPtr,
3656 TkWindow *winPtr)
3657 {
3658 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
3659 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
3660 wmPtr->flags |= WM_UPDATE_PENDING;
3661 }
3662 }
3663
3664 /*
3665 * Invoked when a MapNotify or UnmapNotify event is delivered for a toplevel
3666 * that is the master of a transient toplevel.
3667 */
3668
3669 static void
3670 WmWaitMapProc(
3671 ClientData clientData, /* Pointer to window. */
3672 XEvent *eventPtr) /* Information about event. */
3673 {
3674 TkWindow *winPtr = (TkWindow *) clientData;
3675 TkWindow *masterPtr = winPtr->wmInfoPtr->masterPtr;
3676
3677 if (masterPtr == NULL) {
3678 return;
3679 }
3680
3681 if (eventPtr->type == MapNotify) {
3682 if (!(winPtr->wmInfoPtr->flags & WM_WITHDRAWN)) {
3683 (void) TkpWmSetState(winPtr, NormalState);
3684 }
3685 } else if (eventPtr->type == UnmapNotify) {
3686 (void) TkpWmSetState(winPtr, WithdrawnState);
3687 }
3688 }
3689
3690 /*
3691 *----------------------------------------------------------------------
3692 *
3693 * Tk_SetGrid --
3694 *
3695 * This function is invoked by a widget when it wishes to set a grid
3696 * coordinate system that controls the size of a top-level window. It
3697 * provides a C interface equivalent to the "wm grid" command and is
3698 * usually asscoiated with the -setgrid option.
3699 *
3700 * Results:
3701 * None.
3702 *
3703 * Side effects:
3704 * Grid-related information will be passed to the window manager, so that
3705 * the top-level window associated with tkwin will resize on even grid
3706 * units. If some other window already controls gridding for the
3707 * top-level window then this function call has no effect.
3708 *
3709 *----------------------------------------------------------------------
3710 */
3711
3712 void
3713 Tk_SetGrid(
3714 Tk_Window tkwin, /* Token for window. New window mgr info will
3715 * be posted for the top-level window
3716 * associated with this window. */
3717 int reqWidth, /* Width (in grid units) corresponding to the
3718 * requested geometry for tkwin. */
3719 int reqHeight, /* Height (in grid units) corresponding to the
3720 * requested geometry for tkwin. */
3721 int widthInc, int heightInc)/* Pixel increments corresponding to a change
3722 * of one grid unit. */
3723 {
3724 TkWindow *winPtr = (TkWindow *) tkwin;
3725 register WmInfo *wmPtr;
3726
3727 /*
3728 * Ensure widthInc and heightInc are greater than 0
3729 */
3730
3731 if (widthInc <= 0) {
3732 widthInc = 1;
3733 }
3734 if (heightInc <= 0) {
3735 heightInc = 1;
3736 }
3737
3738 /*
3739 * Find the top-level window for tkwin, plus the window manager
3740 * information.
3741 */
3742
3743 while (!(winPtr->flags & TK_TOP_HIERARCHY)) {
3744 winPtr = winPtr->parentPtr;
3745 if (winPtr == NULL) {
3746 /*
3747 * The window is being deleted... just skip this operation.
3748 */
3749
3750 return;
3751 }
3752 }
3753 wmPtr = winPtr->wmInfoPtr;
3754 if (wmPtr == NULL) {
3755 return;
3756 }
3757
3758 if ((wmPtr->gridWin != NULL) && (wmPtr->gridWin != tkwin)) {
3759 return;
3760 }
3761
3762 if ((wmPtr->reqGridWidth == reqWidth)
3763 && (wmPtr->reqGridHeight == reqHeight)
3764 && (wmPtr->widthInc == widthInc)
3765 && (wmPtr->heightInc == heightInc)
3766 && ((wmPtr->sizeHintsFlags & (PBaseSize|PResizeInc))
3767 == (PBaseSize|PResizeInc))) {
3768 return;
3769 }
3770
3771 /*
3772 * If gridding was previously off, then forget about any window size
3773 * requests made by the user or via "wm geometry": these are in pixel
3774 * units and there's no easy way to translate them to grid units since the
3775 * new requested size of the top-level window in pixels may not yet have
3776 * been registered yet (it may filter up the hierarchy in DoWhenIdle
3777 * handlers). However, if the window has never been mapped yet then just
3778 * leave the window size alone: assume that it is intended to be in grid
3779 * units but just happened to have been specified before this function was
3780 * called.
3781 */
3782
3783 if ((wmPtr->gridWin == NULL) && !(wmPtr->flags & WM_NEVER_MAPPED)) {
3784 wmPtr->width = -1;
3785 wmPtr->height = -1;
3786 }
3787
3788 /*
3789 * Set the new gridding information, and start the process of passing all
3790 * of this information to the window manager.
3791 */
3792
3793 wmPtr->gridWin = tkwin;
3794 wmPtr->reqGridWidth = reqWidth;
3795 wmPtr->reqGridHeight = reqHeight;
3796 wmPtr->widthInc = widthInc;
3797 wmPtr->heightInc = heightInc;
3798 wmPtr->sizeHintsFlags |= PBaseSize|PResizeInc;
3799 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
3800 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
3801 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
3802 wmPtr->flags |= WM_UPDATE_PENDING;
3803 }
3804 }
3805
3806 /*
3807 *----------------------------------------------------------------------
3808 *
3809 * Tk_UnsetGrid --
3810 *
3811 * This function cancels the effect of a previous call to Tk_SetGrid.
3812 *
3813 * Results:
3814 * None.
3815 *
3816 * Side effects:
3817 * If tkwin currently controls gridding for its top-level window,
3818 * gridding is cancelled for that top-level window; if some other window
3819 * controls gridding then this function has no effect.
3820 *
3821 *----------------------------------------------------------------------
3822 */
3823
3824 void
3825 Tk_UnsetGrid(
3826 Tk_Window tkwin) /* Token for window that is currently
3827 * controlling gridding. */
3828 {
3829 TkWindow *winPtr = (TkWindow *) tkwin;
3830 register WmInfo *wmPtr;
3831
3832 /*
3833 * Find the top-level window for tkwin, plus the window manager
3834 * information.
3835 */
3836
3837 while (!(winPtr->flags & TK_TOP_HIERARCHY)) {
3838 winPtr = winPtr->parentPtr;
3839 if (winPtr == NULL) {
3840 /*
3841 * The window is being deleted... just skip this operation.
3842 */
3843
3844 return;
3845 }
3846 }
3847 wmPtr = winPtr->wmInfoPtr;
3848 if (wmPtr == NULL) {
3849 return;
3850 }
3851
3852 if (tkwin != wmPtr->gridWin) {
3853 return;
3854 }
3855
3856 wmPtr->gridWin = NULL;
3857 wmPtr->sizeHintsFlags &= ~(PBaseSize|PResizeInc);
3858 if (wmPtr->width != -1) {
3859 wmPtr->width = winPtr->reqWidth + (wmPtr->width
3860 - wmPtr->reqGridWidth)*wmPtr->widthInc;
3861 wmPtr->height = winPtr->reqHeight + (wmPtr->height
3862 - wmPtr->reqGridHeight)*wmPtr->heightInc;
3863 }
3864 wmPtr->widthInc = 1;
3865 wmPtr->heightInc = 1;
3866
3867 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
3868 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
3869 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
3870 wmPtr->flags |= WM_UPDATE_PENDING;
3871 }
3872 }
3873
3874 /*
3875 *----------------------------------------------------------------------
3876 *
3877 * ConfigureEvent --
3878 *
3879 * This function is called to handle ConfigureNotify events on wrapper
3880 * windows.
3881 *
3882 * Results:
3883 * None.
3884 *
3885 * Side effects:
3886 * Information gets updated in the WmInfo structure for the window and
3887 * the toplevel itself gets repositioned within the wrapper.
3888 *
3889 *----------------------------------------------------------------------
3890 */
3891
3892 static void
3893 ConfigureEvent(
3894 WmInfo *wmPtr, /* Information about toplevel window. */
3895 XConfigureEvent *configEventPtr)
3896 /* Event that just occurred for
3897 * wmPtr->wrapperPtr. */
3898 {
3899 TkWindow *wrapperPtr = wmPtr->wrapperPtr;
3900 TkWindow *winPtr = wmPtr->winPtr;
3901 TkDisplay *dispPtr = wmPtr->winPtr->dispPtr;
3902 Tk_ErrorHandler handler;
3903
3904 /*
3905 * Update size information from the event. There are a couple of tricky
3906 * points here:
3907 *
3908 * 1. If the user changed the size externally then set wmPtr->width and
3909 * wmPtr->height just as if a "wm geometry" command had been invoked
3910 * with the same information.
3911 * 2. However, if the size is changing in response to a request coming
3912 * from us (WM_SYNC_PENDING is set), then don't set wmPtr->width or
3913 * wmPtr->height if they were previously -1 (otherwise the window will
3914 * stop tracking geometry manager requests).
3915 */
3916
3917 if (((wrapperPtr->changes.width != configEventPtr->width)
3918 || (wrapperPtr->changes.height != configEventPtr->height))
3919 && !(wmPtr->flags & WM_SYNC_PENDING)) {
3920 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
3921 printf("TopLevelEventProc: user changed %s size to %dx%d\n",
3922 winPtr->pathName, configEventPtr->width,
3923 configEventPtr->height);
3924 }
3925 if ((wmPtr->width == -1)
3926 && (configEventPtr->width == winPtr->reqWidth)) {
3927 /*
3928 * Don't set external width, since the user didn't change it from
3929 * what the widgets asked for.
3930 */
3931 } else {
3932 /*
3933 * Note: if this window is embedded then don't set the external
3934 * size, since it came from the containing application, not the
3935 * user. In this case we want to keep sending our size requests to
3936 * the containing application; if the user fixes the size of that
3937 * application then it will still percolate down to us in the
3938 * right way.
3939 */
3940
3941 if (!(winPtr->flags & TK_EMBEDDED)) {
3942 if (wmPtr->gridWin != NULL) {
3943 wmPtr->width = wmPtr->reqGridWidth
3944 + (configEventPtr->width
3945 - winPtr->reqWidth)/wmPtr->widthInc;
3946 if (wmPtr->width < 0) {
3947 wmPtr->width = 0;
3948 }
3949 } else {
3950 wmPtr->width = configEventPtr->width;
3951 }
3952 }
3953 }
3954 if ((wmPtr->height == -1)
3955 && (configEventPtr->height ==
3956 (winPtr->reqHeight + wmPtr->menuHeight))) {
3957 /*
3958 * Don't set external height, since the user didn't change it from
3959 * what the widgets asked for.
3960 */
3961 } else {
3962 /*
3963 * See note for wmPtr->width about not setting external size for
3964 * embedded windows.
3965 */
3966
3967 if (!(winPtr->flags & TK_EMBEDDED)) {
3968 if (wmPtr->gridWin != NULL) {
3969 wmPtr->height = wmPtr->reqGridHeight
3970 + (configEventPtr->height - wmPtr->menuHeight
3971 - winPtr->reqHeight)/wmPtr->heightInc;
3972 if (wmPtr->height < 0) {
3973 wmPtr->height = 0;
3974 }
3975 } else {
3976 wmPtr->height = configEventPtr->height - wmPtr->menuHeight;
3977 }
3978 }
3979 }
3980 wmPtr->configWidth = configEventPtr->width;
3981 wmPtr->configHeight = configEventPtr->height;
3982 }
3983
3984 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
3985 printf("ConfigureEvent: %s x = %d y = %d, width = %d, height = %d\n",
3986 winPtr->pathName, configEventPtr->x, configEventPtr->y,
3987 configEventPtr->width, configEventPtr->height);
3988 printf(" send_event = %d, serial = %ld (win %p, wrapper %p)\n",
3989 configEventPtr->send_event, configEventPtr->serial,
3990 winPtr, wrapperPtr);
3991 }
3992 wrapperPtr->changes.width = configEventPtr->width;
3993 wrapperPtr->changes.height = configEventPtr->height;
3994 wrapperPtr->changes.border_width = configEventPtr->border_width;
3995 wrapperPtr->changes.sibling = configEventPtr->above;
3996 wrapperPtr->changes.stack_mode = Above;
3997
3998 /*
3999 * Reparenting window managers make life difficult. If the window manager
4000 * reparents a top-level window then the x and y information that comes in
4001 * events for the window is wrong: it gives the location of the window
4002 * inside its decorative parent, rather than the location of the window in
4003 * root coordinates, which is what we want. Window managers are supposed
4004 * to send synthetic events with the correct information, but ICCCM
4005 * doesn't require them to do this under all conditions, and the
4006 * information provided doesn't include everything we need here. So, the
4007 * code below maintains a bunch of information about the parent window.
4008 * If the window hasn't been reparented, we pretend that there is a parent
4009 * shrink-wrapped around the window.
4010 */
4011
4012 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4013 printf(" %s parent == %p, above %p\n",
4014 winPtr->pathName, (void *) wmPtr->reparent,
4015 (void *) configEventPtr->above);
4016 }
4017
4018 if ((wmPtr->reparent == None) || !ComputeReparentGeometry(wmPtr)) {
4019 wmPtr->parentWidth = configEventPtr->width
4020 + 2*configEventPtr->border_width;
4021 wmPtr->parentHeight = configEventPtr->height
4022 + 2*configEventPtr->border_width;
4023 wrapperPtr->changes.x = wmPtr->x = configEventPtr->x;
4024 wrapperPtr->changes.y = wmPtr->y = configEventPtr->y;
4025 if (wmPtr->flags & WM_NEGATIVE_X) {
4026 wmPtr->x = wmPtr->vRootWidth - (wmPtr->x + wmPtr->parentWidth);
4027 }
4028 if (wmPtr->flags & WM_NEGATIVE_Y) {
4029 wmPtr->y = wmPtr->vRootHeight - (wmPtr->y + wmPtr->parentHeight);
4030 }
4031 }
4032
4033 /*
4034 * Make sure that the toplevel and menubar are properly positioned within
4035 * the wrapper. If the menuHeight happens to be zero, we'll get a BadValue
4036 * X error that we want to ignore [Bug: 3377]
4037 */
4038
4039 handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1, NULL, NULL);
4040 XMoveResizeWindow(winPtr->display, winPtr->window, 0,
4041 wmPtr->menuHeight, (unsigned) wrapperPtr->changes.width,
4042 (unsigned) (wrapperPtr->changes.height - wmPtr->menuHeight));
4043 Tk_DeleteErrorHandler(handler);
4044 if ((wmPtr->menubar != NULL)
4045 && ((Tk_Width(wmPtr->menubar) != wrapperPtr->changes.width)
4046 || (Tk_Height(wmPtr->menubar) != wmPtr->menuHeight))) {
4047 Tk_MoveResizeWindow(wmPtr->menubar, 0, 0, wrapperPtr->changes.width,
4048 wmPtr->menuHeight);
4049 }
4050
4051 /*
4052 * Update the coordinates in the toplevel (they should refer to the
4053 * position in root window coordinates, not the coordinates of the wrapper
4054 * window). Then synthesize a ConfigureNotify event to tell the
4055 * application about the change.
4056 */
4057
4058 winPtr->changes.x = wrapperPtr->changes.x;
4059 winPtr->changes.y = wrapperPtr->changes.y + wmPtr->menuHeight;
4060 winPtr->changes.width = wrapperPtr->changes.width;
4061 winPtr->changes.height = wrapperPtr->changes.height - wmPtr->menuHeight;
4062 TkDoConfigureNotify(winPtr);
4063 }
4064
4065 /*
4066 *----------------------------------------------------------------------
4067 *
4068 * ReparentEvent --
4069 *
4070 * This function is called to handle ReparentNotify events on wrapper
4071 * windows.
4072 *
4073 * Results:
4074 * None.
4075 *
4076 * Side effects:
4077 * Information gets updated in the WmInfo structure for the window.
4078 *
4079 *----------------------------------------------------------------------
4080 */
4081
4082 static void
4083 ReparentEvent(
4084 WmInfo *wmPtr, /* Information about toplevel window. */
4085 XReparentEvent *reparentEventPtr)
4086 /* Event that just occurred for
4087 * wmPtr->wrapperPtr. */
4088 {
4089 TkWindow *wrapperPtr = wmPtr->wrapperPtr;
4090 Window vRoot, ancestor, *children, dummy2, *virtualRootPtr;
4091 Atom actualType;
4092 int actualFormat;
4093 unsigned long numItems, bytesAfter;
4094 unsigned int dummy;
4095 Tk_ErrorHandler handler;
4096 TkDisplay *dispPtr = wmPtr->winPtr->dispPtr;
4097
4098 /*
4099 * Identify the root window for wrapperPtr. This is tricky because of
4100 * virtual root window managers like tvtwm. If the window has a property
4101 * named __SWM_ROOT or __WM_ROOT then this property gives the id for a
4102 * virtual root window that should be used instead of the root window of
4103 * the screen.
4104 */
4105
4106 vRoot = RootWindow(wrapperPtr->display, wrapperPtr->screenNum);
4107 wmPtr->vRoot = None;
4108 handler = Tk_CreateErrorHandler(wrapperPtr->display, -1,-1,-1, NULL,NULL);
4109 if (((XGetWindowProperty(wrapperPtr->display, wrapperPtr->window,
4110 Tk_InternAtom((Tk_Window) wrapperPtr, "__WM_ROOT"), 0, (long) 1,
4111 False, XA_WINDOW, &actualType, &actualFormat, &numItems,
4112 &bytesAfter, (unsigned char **) &virtualRootPtr) == Success)
4113 && (actualType == XA_WINDOW))
4114 || ((XGetWindowProperty(wrapperPtr->display, wrapperPtr->window,
4115 Tk_InternAtom((Tk_Window) wrapperPtr, "__SWM_ROOT"), 0, (long) 1,
4116 False, XA_WINDOW, &actualType, &actualFormat, &numItems,
4117 &bytesAfter, (unsigned char **) &virtualRootPtr) == Success)
4118 && (actualType == XA_WINDOW))) {
4119 if ((actualFormat == 32) && (numItems == 1)) {
4120 vRoot = wmPtr->vRoot = *virtualRootPtr;
4121 } else if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4122 printf("%s format %d numItems %ld\n",
4123 "ReparentEvent got bogus VROOT property:", actualFormat,
4124 numItems);
4125 }
4126 XFree((char *) virtualRootPtr);
4127 }
4128 Tk_DeleteErrorHandler(handler);
4129
4130 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4131 printf("ReparentEvent: %s (%p) reparented to 0x%x, vRoot = 0x%x\n",
4132 wmPtr->winPtr->pathName, wmPtr->winPtr,
4133 (unsigned int) reparentEventPtr->parent, (unsigned int) vRoot);
4134 }
4135
4136 /*
4137 * Fetch correct geometry information for the new virtual root.
4138 */
4139
4140 UpdateVRootGeometry(wmPtr);
4141
4142 /*
4143 * If the window's new parent is the root window, then mark it as no
4144 * longer reparented.
4145 */
4146
4147 if (reparentEventPtr->parent == vRoot) {
4148 noReparent:
4149 wmPtr->reparent = None;
4150 wmPtr->parentWidth = wrapperPtr->changes.width;
4151 wmPtr->parentHeight = wrapperPtr->changes.height;
4152 wmPtr->xInParent = wmPtr->yInParent = 0;
4153 wrapperPtr->changes.x = reparentEventPtr->x;
4154 wrapperPtr->changes.y = reparentEventPtr->y;
4155 wmPtr->winPtr->changes.x = reparentEventPtr->x;
4156 wmPtr->winPtr->changes.y = reparentEventPtr->y + wmPtr->menuHeight;
4157 return;
4158 }
4159
4160 /*
4161 * Search up the window hierarchy to find the ancestor of this window that
4162 * is just below the (virtual) root. This is tricky because it's possible
4163 * that things have changed since the event was generated so that the
4164 * ancestry indicated by the event no longer exists. If this happens then
4165 * an error will occur and we just discard the event (there will be a more
4166 * up-to-date ReparentNotify event coming later).
4167 */
4168
4169 handler = Tk_CreateErrorHandler(wrapperPtr->display, -1,-1,-1, NULL,NULL);
4170 wmPtr->reparent = reparentEventPtr->parent;
4171 while (1) {
4172 if (XQueryTree(wrapperPtr->display, wmPtr->reparent, &dummy2,
4173 &ancestor, &children, &dummy) == 0) {
4174 Tk_DeleteErrorHandler(handler);
4175 goto noReparent;
4176 }
4177 XFree((char *) children);
4178 if ((ancestor == vRoot) ||
4179 (ancestor == RootWindow(wrapperPtr->display,
4180 wrapperPtr->screenNum))) {
4181 break;
4182 }
4183 wmPtr->reparent = ancestor;
4184 }
4185 Tk_DeleteErrorHandler(handler);
4186
4187 if (!ComputeReparentGeometry(wmPtr)) {
4188 goto noReparent;
4189 }
4190 }
4191
4192 /*
4193 *----------------------------------------------------------------------
4194 *
4195 * ComputeReparentGeometry --
4196 *
4197 * This function is invoked to recompute geometry information related to
4198 * a reparented top-level window, such as the position and total size of
4199 * the parent and the position within it of the top-level window.
4200 *
4201 * Results:
4202 * The return value is 1 if everything completed successfully and 0 if an
4203 * error occurred while querying information about winPtr's parents. In
4204 * this case winPtr is marked as no longer being reparented.
4205 *
4206 * Side effects:
4207 * Geometry information in wmPtr, wmPtr->winPtr, and wmPtr->wrapperPtr
4208 * gets updated.
4209 *
4210 *----------------------------------------------------------------------
4211 */
4212
4213 static int
4214 ComputeReparentGeometry(
4215 WmInfo *wmPtr) /* Information about toplevel window whose
4216 * reparent info is to be recomputed. */
4217 {
4218 TkWindow *wrapperPtr = wmPtr->wrapperPtr;
4219 int width, height, bd;
4220 unsigned int dummy;
4221 int xOffset, yOffset, x, y;
4222 Window dummy2;
4223 Status status;
4224 Tk_ErrorHandler handler;
4225 TkDisplay *dispPtr = wmPtr->winPtr->dispPtr;
4226
4227 handler = Tk_CreateErrorHandler(wrapperPtr->display, -1,-1,-1, NULL,NULL);
4228 (void) XTranslateCoordinates(wrapperPtr->display, wrapperPtr->window,
4229 wmPtr->reparent, 0, 0, &xOffset, &yOffset, &dummy2);
4230 status = XGetGeometry(wrapperPtr->display, wmPtr->reparent,
4231 &dummy2, &x, &y, (unsigned int *) &width,
4232 (unsigned int *) &height, (unsigned int *) &bd, &dummy);
4233 Tk_DeleteErrorHandler(handler);
4234 if (status == 0) {
4235 /*
4236 * It appears that the reparented parent went away and no-one told us.
4237 * Reset the window to indicate that it's not reparented.
4238 */
4239
4240 wmPtr->reparent = None;
4241 wmPtr->xInParent = wmPtr->yInParent = 0;
4242 return 0;
4243 }
4244 wmPtr->xInParent = xOffset + bd;
4245 wmPtr->yInParent = yOffset + bd;
4246 wmPtr->parentWidth = width + 2*bd;
4247 wmPtr->parentHeight = height + 2*bd;
4248
4249 /*
4250 * Some tricky issues in updating wmPtr->x and wmPtr->y:
4251 *
4252 * 1. Don't update them if the event occurred because of something we did
4253 * (i.e. WM_SYNC_PENDING and WM_MOVE_PENDING are both set). This is
4254 * because window managers treat coords differently than Tk, and no two
4255 * window managers are alike. If the window manager moved the window
4256 * because we told it to, remember the coordinates we told it, not the
4257 * ones it actually moved it to. This allows us to move the window back to
4258 * the same coordinates later and get the same result. Without this check,
4259 * windows can "walk" across the screen under some conditions.
4260 *
4261 * 2. Don't update wmPtr->x and wmPtr->y unless wrapperPtr->changes.x or
4262 * wrapperPtr->changes.y has changed (otherwise a size change can spoof us
4263 * into thinking that the position changed too and defeat the intent of
4264 * (1) above.
4265 *
4266 * (As of 9/96 the above 2 comments appear to be stale. They're being left
4267 * in place as a reminder of what was once true (and perhaps should still
4268 * be true?)).
4269 *
4270 * 3. Ignore size changes coming from the window system if we're about to
4271 * change the size ourselves but haven't seen the event for it yet: our
4272 * size change is supposed to take priority.
4273 */
4274
4275 if (!(wmPtr->flags & WM_MOVE_PENDING)
4276 && ((wrapperPtr->changes.x != (x + wmPtr->xInParent))
4277 || (wrapperPtr->changes.y != (y + wmPtr->yInParent)))) {
4278 wmPtr->x = x;
4279 if (wmPtr->flags & WM_NEGATIVE_X) {
4280 wmPtr->x = wmPtr->vRootWidth - (wmPtr->x + wmPtr->parentWidth);
4281 }
4282 wmPtr->y = y;
4283 if (wmPtr->flags & WM_NEGATIVE_Y) {
4284 wmPtr->y = wmPtr->vRootHeight - (wmPtr->y + wmPtr->parentHeight);
4285 }
4286 }
4287
4288 wrapperPtr->changes.x = x + wmPtr->xInParent;
4289 wrapperPtr->changes.y = y + wmPtr->yInParent;
4290 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4291 printf("wrapperPtr %p coords %d,%d\n",
4292 wrapperPtr, wrapperPtr->changes.x, wrapperPtr->changes.y);
4293 printf(" wmPtr %p coords %d,%d, offsets %d %d\n",
4294 wmPtr, wmPtr->x, wmPtr->y, wmPtr->xInParent, wmPtr->yInParent);
4295 }
4296 return 1;
4297 }
4298
4299 /*
4300 *----------------------------------------------------------------------
4301 *
4302 * PropertyEvent --
4303 *
4304 * Handle PropertyNotify events on wrapper windows.
4305 * The following properties are of interest:
4306 *
4307 * _NET_WM_STATE:
4308 * Used to keep wmPtr->attributes up to date.
4309 *
4310 *----------------------------------------------------------------------
4311 */
4312
4313 static void
4314 PropertyEvent(
4315 WmInfo *wmPtr, /* Information about toplevel window. */
4316 XPropertyEvent *eventPtr) /* PropertyNotify event structure */
4317 {
4318 TkWindow *wrapperPtr = wmPtr->wrapperPtr;
4319 Atom _NET_WM_STATE =
4320 Tk_InternAtom((Tk_Window)wmPtr->winPtr, "_NET_WM_STATE");
4321
4322 if (eventPtr->atom == _NET_WM_STATE) {
4323 Atom actualType;
4324 int actualFormat;
4325 unsigned long numItems, bytesAfter;
4326 unsigned char *propertyValue = 0;
4327 long maxLength = 1024;
4328
4329 if (XGetWindowProperty(
4330 wrapperPtr->display, wrapperPtr->window, _NET_WM_STATE,
4331 0l, maxLength, False, XA_ATOM,
4332 &actualType, &actualFormat, &numItems, &bytesAfter,
4333 &propertyValue) == Success
4334 ) {
4335 CheckNetWmState(wmPtr, (Atom*)propertyValue, (int)numItems);
4336 XFree(propertyValue);
4337 }
4338 }
4339 }
4340
4341 /*
4342 *----------------------------------------------------------------------
4343 *
4344 * WrapperEventProc --
4345 *
4346 * This function is invoked by the event loop when a wrapper window is
4347 * restructured.
4348 *
4349 * Results:
4350 * None.
4351 *
4352 * Side effects:
4353 * Tk's internal data structures for the window get modified to reflect
4354 * the structural change.
4355 *
4356 *----------------------------------------------------------------------
4357 */
4358
4359 static const unsigned int WrapperEventMask =
4360 (StructureNotifyMask | PropertyChangeMask);
4361
4362 static void
4363 WrapperEventProc(
4364 ClientData clientData, /* Information about toplevel window. */
4365 XEvent *eventPtr) /* Event that just happened. */
4366 {
4367 WmInfo *wmPtr = (WmInfo *) clientData;
4368 XEvent mapEvent;
4369 TkDisplay *dispPtr = wmPtr->winPtr->dispPtr;
4370
4371 wmPtr->flags |= WM_VROOT_OFFSET_STALE;
4372 if (eventPtr->type == DestroyNotify) {
4373 Tk_ErrorHandler handler;
4374
4375 if (!(wmPtr->wrapperPtr->flags & TK_ALREADY_DEAD)) {
4376 /*
4377 * A top-level window was deleted externally (e.g., by the window
4378 * manager). This is probably not a good thing, but cleanup as
4379 * best we can. The error handler is needed because
4380 * Tk_DestroyWindow will try to destroy the window, but of course
4381 * it's already gone.
4382 */
4383
4384 handler = Tk_CreateErrorHandler(wmPtr->winPtr->display, -1, -1, -1,
4385 NULL, NULL);
4386 Tk_DestroyWindow((Tk_Window) wmPtr->winPtr);
4387 Tk_DeleteErrorHandler(handler);
4388 }
4389 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4390 printf("TopLevelEventProc: %s deleted\n", wmPtr->winPtr->pathName);
4391 }
4392 } else if (eventPtr->type == ConfigureNotify) {
4393 /*
4394 * Ignore the event if the window has never been mapped yet. Such an
4395 * event occurs only in weird cases like changing the internal border
4396 * width of a top-level window, which results in a synthetic Configure
4397 * event. These events are not relevant to us, and if we process them
4398 * confusion may result (e.g. we may conclude erroneously that the
4399 * user repositioned or resized the window).
4400 */
4401
4402 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
4403 ConfigureEvent(wmPtr, &eventPtr->xconfigure);
4404 }
4405 } else if (eventPtr->type == MapNotify) {
4406 wmPtr->wrapperPtr->flags |= TK_MAPPED;
4407 wmPtr->winPtr->flags |= TK_MAPPED;
4408 XMapWindow(wmPtr->winPtr->display, wmPtr->winPtr->window);
4409 goto doMapEvent;
4410 } else if (eventPtr->type == UnmapNotify) {
4411 wmPtr->wrapperPtr->flags &= ~TK_MAPPED;
4412 wmPtr->winPtr->flags &= ~TK_MAPPED;
4413 XUnmapWindow(wmPtr->winPtr->display, wmPtr->winPtr->window);
4414 goto doMapEvent;
4415 } else if (eventPtr->type == ReparentNotify) {
4416 ReparentEvent(wmPtr, &eventPtr->xreparent);
4417 } else if (eventPtr->type == PropertyNotify) {
4418 PropertyEvent(wmPtr, &eventPtr->xproperty);
4419 } else if (0 && eventPtr->type == Expose) {
4420 mapEvent = *eventPtr;
4421 mapEvent.xexpose.window = wmPtr->winPtr->window;
4422 Tk_HandleEvent(&mapEvent);
4423 }
4424 return;
4425
4426 doMapEvent:
4427 mapEvent = *eventPtr;
4428 mapEvent.xmap.event = wmPtr->winPtr->window;
4429 mapEvent.xmap.window = wmPtr->winPtr->window;
4430 Tk_HandleEvent(&mapEvent);
4431 }
4432
4433 /*
4434 *----------------------------------------------------------------------
4435 *
4436 * TopLevelReqProc --
4437 *
4438 * This function is invoked by the geometry manager whenever the
4439 * requested size for a top-level window is changed.
4440 *
4441 * Results:
4442 * None.
4443 *
4444 * Side effects:
4445 * Arrange for the window to be resized to satisfy the request (this
4446 * happens as a when-idle action).
4447 *
4448 *----------------------------------------------------------------------
4449 */
4450
4451 /* ARGSUSED */
4452 static void
4453 TopLevelReqProc(
4454 ClientData dummy, /* Not used. */
4455 Tk_Window tkwin) /* Information about window. */
4456 {
4457 TkWindow *winPtr = (TkWindow *) tkwin;
4458 WmInfo *wmPtr;
4459
4460 wmPtr = winPtr->wmInfoPtr;
4461
4462 if ((wmPtr->width >= 0) && (wmPtr->height >= 0)) {
4463 /*
4464 * Explicit dimensions have been set for this window, so we should
4465 * ignore the geometry request. It's actually important to ignore the
4466 * geometry request because, due to quirks in window managers,
4467 * invoking UpdateGeometryInfo may cause the window to move. For
4468 * example, if "wm geometry -10-20" was invoked, the window may be
4469 * positioned incorrectly the first time it appears (because we didn't
4470 * know the proper width of the window manager borders); if we invoke
4471 * UpdateGeometryInfo again, the window will be positioned correctly,
4472 * which may cause it to jump on the screen.
4473 */
4474
4475 return;
4476 }
4477
4478 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
4479 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
4480 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
4481 wmPtr->flags |= WM_UPDATE_PENDING;
4482 }
4483
4484 /*
4485 * If the window isn't being positioned by its upper left corner then we
4486 * have to move it as well.
4487 */
4488
4489 if (wmPtr->flags & (WM_NEGATIVE_X | WM_NEGATIVE_Y)) {
4490 wmPtr->flags |= WM_MOVE_PENDING;
4491 }
4492 }
4493
4494 /*
4495 *----------------------------------------------------------------------
4496 *
4497 * UpdateGeometryInfo --
4498 *
4499 * This function is invoked when a top-level window is first mapped, and
4500 * also as a when-idle function, to bring the geometry and/or position of
4501 * a top-level window back into line with what has been requested by the
4502 * user and/or widgets. This function doesn't return until the window
4503 * manager has responded to the geometry change.
4504 *
4505 * Results:
4506 * None.
4507 *
4508 * Side effects:
4509 * The size and location of both the toplevel window and its wrapper may
4510 * change, unless the WM prevents that from happening.
4511 *
4512 *----------------------------------------------------------------------
4513 */
4514
4515 static void
4516 UpdateGeometryInfo(
4517 ClientData clientData) /* Pointer to the window's record. */
4518 {
4519 register TkWindow *winPtr = (TkWindow *) clientData;
4520 register WmInfo *wmPtr = winPtr->wmInfoPtr;
4521 int x, y, width, height, min, max;
4522 unsigned long serial;
4523
4524 wmPtr->flags &= ~WM_UPDATE_PENDING;
4525
4526 /*
4527 * Compute the new size for the top-level window. See the user
4528 * documentation for details on this, but the size requested depends on
4529 * (a) the size requested internally by the window's widgets, (b) the size
4530 * requested by the user in a "wm geometry" command or via wm-based
4531 * interactive resizing (if any), (c) whether or not the window is
4532 * gridded, and (d) the current min or max size for the toplevel. Don't
4533 * permit sizes <= 0 because this upsets the X server.
4534 */
4535
4536 if (wmPtr->width == -1) {
4537 width = winPtr->reqWidth;
4538 } else if (wmPtr->gridWin != NULL) {
4539 width = winPtr->reqWidth
4540 + (wmPtr->width - wmPtr->reqGridWidth)*wmPtr->widthInc;
4541 } else {
4542 width = wmPtr->width;
4543 }
4544 if (width <= 0) {
4545 width = 1;
4546 }
4547
4548 /*
4549 * Account for window max/min width
4550 */
4551
4552 if (wmPtr->gridWin != NULL) {
4553 min = winPtr->reqWidth
4554 + (wmPtr->minWidth - wmPtr->reqGridWidth)*wmPtr->widthInc;
4555 if (wmPtr->maxWidth > 0) {
4556 max = winPtr->reqWidth
4557 + (wmPtr->maxWidth - wmPtr->reqGridWidth)*wmPtr->widthInc;
4558 } else {
4559 max = 0;
4560 }
4561 } else {
4562 min = wmPtr->minWidth;
4563 max = wmPtr->maxWidth;
4564 }
4565 if (width < min) {
4566 width = min;
4567 } else if ((max > 0) && (width > max)) {
4568 width = max;
4569 }
4570
4571 if (wmPtr->height == -1) {
4572 height = winPtr->reqHeight;
4573 } else if (wmPtr->gridWin != NULL) {
4574 height = winPtr->reqHeight
4575 + (wmPtr->height - wmPtr->reqGridHeight)*wmPtr->heightInc;
4576 } else {
4577 height = wmPtr->height;
4578 }
4579 if (height <= 0) {
4580 height = 1;
4581 }
4582
4583 /*
4584 * Account for window max/min height
4585 */
4586
4587 if (wmPtr->gridWin != NULL) {
4588 min = winPtr->reqHeight
4589 + (wmPtr->minHeight - wmPtr->reqGridHeight)*wmPtr->heightInc;
4590 if (wmPtr->maxHeight > 0) {
4591 max = winPtr->reqHeight
4592 + (wmPtr->maxHeight - wmPtr->reqGridHeight)*wmPtr->heightInc;
4593 } else {
4594 max = 0;
4595 }
4596 } else {
4597 min = wmPtr->minHeight;
4598 max = wmPtr->maxHeight;
4599 }
4600 if (height < min) {
4601 height = min;
4602 } else if ((max > 0) && (height > max)) {
4603 height = max;
4604 }
4605
4606 /*
4607 * Compute the new position for the upper-left pixel of the window's
4608 * decorative frame. This is tricky, because we need to include the border
4609 * widths supplied by a reparented parent in this calculation, but can't
4610 * use the parent's current overall size since that may change as a result
4611 * of this code.
4612 */
4613
4614 if (wmPtr->flags & WM_NEGATIVE_X) {
4615 x = wmPtr->vRootWidth - wmPtr->x
4616 - (width + (wmPtr->parentWidth - winPtr->changes.width));
4617 } else {
4618 x = wmPtr->x;
4619 }
4620 if (wmPtr->flags & WM_NEGATIVE_Y) {
4621 y = wmPtr->vRootHeight - wmPtr->y
4622 - (height + (wmPtr->parentHeight - winPtr->changes.height));
4623 } else {
4624 y = wmPtr->y;
4625 }
4626
4627 /*
4628 * If the window's size is going to change and the window is supposed to
4629 * not be resizable by the user, then we have to update the size hints.
4630 * There may also be a size-hint-update request pending from somewhere
4631 * else, too.
4632 */
4633
4634 if (((width != winPtr->changes.width)
4635 || (height != winPtr->changes.height))
4636 && (wmPtr->gridWin == NULL)
4637 && ((wmPtr->sizeHintsFlags & (PMinSize|PMaxSize)) == 0)) {
4638 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
4639 }
4640 if (wmPtr->flags & WM_UPDATE_SIZE_HINTS) {
4641 UpdateSizeHints(winPtr, width, height);
4642 }
4643
4644 /*
4645 * Reconfigure the wrapper if it isn't already configured correctly. A few
4646 * tricky points:
4647 *
4648 * 1. If the window is embeddedand the container is also in this process,
4649 * don't actually reconfigure the window; just pass the desired size on
4650 * to the container. Also, zero out any position information, since
4651 * embedded windows are not allowed to move.
4652 * 2. Sometimes the window manager will give us a different size than we
4653 * asked for (e.g. mwm has a minimum size for windows), so base the
4654 * size check on what we *asked for* last time, not what we got.
4655 * 3. Can't just reconfigure always, because we may not get a
4656 * ConfigureNotify event back if nothing changed, so
4657 * WaitForConfigureNotify will hang a long time.
4658 * 4. Don't move window unless a new position has been requested for it.
4659 * This is because of "features" in some window managers (e.g. twm, as
4660 * of 4/24/91) where they don't interpret coordinates according to
4661 * ICCCM. Moving a window to its current location may cause it to shift
4662 * position on the screen.
4663 */
4664
4665 if ((winPtr->flags & (TK_EMBEDDED|TK_BOTH_HALVES))
4666 == (TK_EMBEDDED|TK_BOTH_HALVES)) {
4667 TkWindow *childPtr = TkpGetOtherWindow(winPtr);
4668
4669 /*
4670 * This window is embedded and the container is also in this process,
4671 * so we don't need to do anything special about the geometry, except
4672 * to make sure that the desired size is known by the container. Also,
4673 * zero out any position information, since embedded windows are not
4674 * allowed to move.
4675 */
4676
4677 wmPtr->x = wmPtr->y = 0;
4678 wmPtr->flags &= ~(WM_NEGATIVE_X|WM_NEGATIVE_Y);
4679 height += wmPtr->menuHeight;
4680 if (childPtr != NULL) {
4681 Tk_GeometryRequest((Tk_Window) childPtr, width, height);
4682 }
4683 return;
4684 }
4685 serial = NextRequest(winPtr->display);
4686 height += wmPtr->menuHeight;
4687 if (wmPtr->flags & WM_MOVE_PENDING) {
4688 if ((x + wmPtr->xInParent == winPtr->changes.x) &&
4689 (y + wmPtr->yInParent + wmPtr->menuHeight == winPtr->changes.y)
4690 && (width == wmPtr->wrapperPtr->changes.width)
4691 && (height == wmPtr->wrapperPtr->changes.height)) {
4692 /*
4693 * The window already has the correct geometry, so don't bother to
4694 * configure it; the X server appears to ignore these requests, so
4695 * we won't get back a ConfigureNotify and the
4696 * WaitForConfigureNotify call below will hang for a while.
4697 */
4698
4699 wmPtr->flags &= ~WM_MOVE_PENDING;
4700 return;
4701 }
4702 wmPtr->configWidth = width;
4703 wmPtr->configHeight = height;
4704 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4705 printf("UpdateGeometryInfo moving to %d %d, resizing to %dx%d,\n",
4706 x, y, width, height);
4707 }
4708 XMoveResizeWindow(winPtr->display, wmPtr->wrapperPtr->window, x, y,
4709 (unsigned) width, (unsigned) height);
4710 } else if ((width != wmPtr->configWidth)
4711 || (height != wmPtr->configHeight)) {
4712 if ((width == wmPtr->wrapperPtr->changes.width)
4713 && (height == wmPtr->wrapperPtr->changes.height)) {
4714 /*
4715 * The window is already just the size we want, so don't bother to
4716 * configure it; the X server appears to ignore these requests, so
4717 * we won't get back a ConfigureNotify and the
4718 * WaitForConfigureNotify call below will hang for a while.
4719 */
4720
4721 return;
4722 }
4723 wmPtr->configWidth = width;
4724 wmPtr->configHeight = height;
4725 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
4726 printf("UpdateGeometryInfo resizing %p to %d x %d\n",
4727 (void *)wmPtr->wrapperPtr->window, width, height);
4728 }
4729 XResizeWindow(winPtr->display, wmPtr->wrapperPtr->window,
4730 (unsigned) width, (unsigned) height);
4731 } else if ((wmPtr->menubar != NULL)
4732 && ((Tk_Width(wmPtr->menubar) != wmPtr->wrapperPtr->changes.width)
4733 || (Tk_Height(wmPtr->menubar) != wmPtr->menuHeight))) {
4734 /*
4735 * It is possible that the window's overall size has not changed but
4736 * the menu size has.
4737 */
4738
4739 Tk_MoveResizeWindow(wmPtr->menubar, 0, 0,
4740 wmPtr->wrapperPtr->changes.width, wmPtr->menuHeight);
4741 XResizeWindow(winPtr->display, wmPtr->wrapperPtr->window,
4742 (unsigned) width, (unsigned) height);
4743 } else {
4744 return;
4745 }
4746
4747 /*
4748 * Wait for the configure operation to complete. Don't need to do this,
4749 * however, if the window is about to be mapped: it will be taken care of
4750 * elsewhere.
4751 */
4752
4753 if (!(wmPtr->flags & WM_ABOUT_TO_MAP)) {
4754 WaitForConfigureNotify(winPtr, serial);
4755 }
4756 }
4757
4758 /*
4759 *--------------------------------------------------------------
4760 *
4761 * UpdateSizeHints --
4762 *
4763 * This function is called to update the window manager's size hints
4764 * information from the information in a WmInfo structure.
4765 *
4766 * Results:
4767 * None.
4768 *
4769 * Side effects:
4770 * Properties get changed for winPtr.
4771 *
4772 *--------------------------------------------------------------
4773 */
4774
4775 static void
4776 UpdateSizeHints(
4777 TkWindow *winPtr,
4778 int newWidth,
4779 int newHeight)
4780 {
4781 register WmInfo *wmPtr = winPtr->wmInfoPtr;
4782 XSizeHints *hintsPtr;
4783 int maxWidth, maxHeight;
4784
4785 wmPtr->flags &= ~WM_UPDATE_SIZE_HINTS;
4786
4787 hintsPtr = XAllocSizeHints();
4788 if (hintsPtr == NULL) {
4789 return;
4790 }
4791
4792 /*
4793 * Compute the pixel-based sizes for the various fields in the size hints
4794 * structure, based on the grid-based sizes in our structure.
4795 */
4796
4797 GetMaxSize(wmPtr, &maxWidth, &maxHeight);
4798 if (wmPtr->gridWin != NULL) {
4799 hintsPtr->base_width = winPtr->reqWidth
4800 - (wmPtr->reqGridWidth * wmPtr->widthInc);
4801 if (hintsPtr->base_width < 0) {
4802 hintsPtr->base_width = 0;
4803 }
4804 hintsPtr->base_height = winPtr->reqHeight + wmPtr->menuHeight
4805 - (wmPtr->reqGridHeight * wmPtr->heightInc);
4806 if (hintsPtr->base_height < 0) {
4807 hintsPtr->base_height = 0;
4808 }
4809 hintsPtr->min_width = hintsPtr->base_width
4810 + (wmPtr->minWidth * wmPtr->widthInc);
4811 hintsPtr->min_height = hintsPtr->base_height
4812 + (wmPtr->minHeight * wmPtr->heightInc);
4813 hintsPtr->max_width = hintsPtr->base_width
4814 + (maxWidth * wmPtr->widthInc);
4815 hintsPtr->max_height = hintsPtr->base_height
4816 + (maxHeight * wmPtr->heightInc);
4817 } else {
4818 hintsPtr->min_width = wmPtr->minWidth;
4819 hintsPtr->min_height = wmPtr->minHeight;
4820 hintsPtr->max_width = maxWidth;
4821 hintsPtr->max_height = maxHeight;
4822 hintsPtr->base_width = 0;
4823 hintsPtr->base_height = 0;
4824 }
4825 hintsPtr->width_inc = wmPtr->widthInc;
4826 hintsPtr->height_inc = wmPtr->heightInc;
4827 hintsPtr->min_aspect.x = wmPtr->minAspect.x;
4828 hintsPtr->min_aspect.y = wmPtr->minAspect.y;
4829 hintsPtr->max_aspect.x = wmPtr->maxAspect.x;
4830 hintsPtr->max_aspect.y = wmPtr->maxAspect.y;
4831 hintsPtr->win_gravity = wmPtr->gravity;
4832 hintsPtr->flags = wmPtr->sizeHintsFlags | PMinSize;
4833
4834 /*
4835 * If the window isn't supposed to be resizable, then set the minimum and
4836 * maximum dimensions to be the same.
4837 */
4838
4839 if (wmPtr->flags & WM_WIDTH_NOT_RESIZABLE) {
4840 hintsPtr->max_width = hintsPtr->min_width = newWidth;
4841 hintsPtr->flags |= PMaxSize;
4842 }
4843 if (wmPtr->flags & WM_HEIGHT_NOT_RESIZABLE) {
4844 hintsPtr->max_height = hintsPtr->min_height =
4845 newHeight + wmPtr->menuHeight;
4846 hintsPtr->flags |= PMaxSize;
4847 }
4848
4849 XSetWMNormalHints(winPtr->display, wmPtr->wrapperPtr->window, hintsPtr);
4850
4851 XFree((char *) hintsPtr);
4852 }
4853 /*
4854 *--------------------------------------------------------------
4855 *
4856 * UpdateTitle --
4857 *
4858 * This function is called to update the window title and icon name. It
4859 * sets the ICCCM-defined properties WM_NAME and WM_ICON_NAME for older
4860 * window managers, and the freedesktop.org-defined _NET_WM_NAME and
4861 * _NET_WM_ICON_NAME properties for newer ones. The ICCCM properties are
4862 * stored in the system encoding, the newer properties are stored in
4863 * UTF-8.
4864 *
4865 * NOTE: the ICCCM specifies that WM_NAME and WM_ICON_NAME are stored in
4866 * ISO-Latin-1. Tk has historically used the default system encoding
4867 * (since 8.1). It's not clear whether this is correct or not.
4868 *
4869 * Side effects:
4870 * Properties get changed for winPtr.
4871 *
4872 *--------------------------------------------------------------
4873 */
4874
4875 static void
4876 UpdateTitle(
4877 TkWindow *winPtr)
4878 {
4879 WmInfo *wmPtr = winPtr->wmInfoPtr;
4880 Atom XA_UTF8_STRING = Tk_InternAtom((Tk_Window) winPtr, "UTF8_STRING");
4881 const char *string;
4882 Tcl_DString ds;
4883
4884 /*
4885 * Set window title:
4886 */
4887
4888 string = (wmPtr->title != NULL) ? wmPtr->title : winPtr->nameUid;
4889 Tcl_UtfToExternalDString(NULL, string, -1, &ds);
4890 XStoreName(winPtr->display, wmPtr->wrapperPtr->window,
4891 Tcl_DStringValue(&ds));
4892 Tcl_DStringFree(&ds);
4893
4894 XChangeProperty(winPtr->display, wmPtr->wrapperPtr->window,
4895 Tk_InternAtom((Tk_Window) winPtr, "_NET_WM_NAME"),
4896 XA_UTF8_STRING, 8, PropModeReplace,
4897 (const unsigned char*)string, (signed int)strlen(string));
4898
4899 /*
4900 * Set icon name:
4901 */
4902
4903 if (wmPtr->iconName != NULL) {
4904 Tcl_UtfToExternalDString(NULL, wmPtr->iconName, -1, &ds);
4905 XSetIconName(winPtr->display, wmPtr->wrapperPtr->window,
4906 Tcl_DStringValue(&ds));
4907 Tcl_DStringFree(&ds);
4908
4909 XChangeProperty(winPtr->display, wmPtr->wrapperPtr->window,
4910 Tk_InternAtom((Tk_Window) winPtr, "_NET_WM_ICON_NAME"),
4911 XA_UTF8_STRING, 8, PropModeReplace,
4912 (const unsigned char*)wmPtr->iconName,
4913 (signed int)strlen(wmPtr->iconName));
4914 }
4915 }
4916
4917 /*
4918 *--------------------------------------------------------------
4919 *
4920 * UpdatePhotoIcon --
4921 *
4922 * This function is called to update the window photo icon. It sets the
4923 * EWMH-defined properties _NET_WM_ICON.
4924 *
4925 * Side effects:
4926 * Properties get changed for winPtr.
4927 *
4928 *--------------------------------------------------------------
4929 */
4930
4931 static void
4932 UpdatePhotoIcon(
4933 TkWindow *winPtr)
4934 {
4935 WmInfo *wmPtr = winPtr->wmInfoPtr;
4936 unsigned char *data = wmPtr->iconDataPtr;
4937 int size = wmPtr->iconDataSize;
4938
4939 if (data == NULL) {
4940 data = winPtr->dispPtr->iconDataPtr;
4941 size = winPtr->dispPtr->iconDataSize;
4942 }
4943 if (data != NULL) {
4944 /*
4945 * Set icon:
4946 */
4947
4948 XChangeProperty(winPtr->display, wmPtr->wrapperPtr->window,
4949 Tk_InternAtom((Tk_Window) winPtr, "_NET_WM_ICON"),
4950 XA_CARDINAL, 32, PropModeReplace,
4951 (unsigned char *) data, size);
4952 }
4953 }
4954
4955 /*
4956 *----------------------------------------------------------------------
4957 *
4958 * SetNetWmState --
4959 *
4960 * Sets the specified state property by sending a _NET_WM_STATE
4961 * ClientMessage to the root window.
4962 *
4963 * Preconditions:
4964 *
4965 * Wrapper window must be created.
4966 *
4967 * See also:
4968 * UpdateNetWmState; EWMH spec, section _NET_WM_STATE.
4969 *
4970 *----------------------------------------------------------------------
4971 */
4972
4973 #define _NET_WM_STATE_REMOVE 0l
4974 #define _NET_WM_STATE_ADD 1l
4975 #define _NET_WM_STATE_TOGGLE 2l
4976
4977 static void SetNetWmState(TkWindow *winPtr, const char *atomName, int on)
4978 {
4979 Tk_Window tkwin = (Tk_Window)winPtr;
4980 Atom messageType = Tk_InternAtom(tkwin, "_NET_WM_STATE");
4981 Atom action = on ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
4982 Atom property = Tk_InternAtom(tkwin, atomName);
4983 XEvent e;
4984
4985 if (!winPtr->wmInfoPtr->wrapperPtr) {
4986 return;
4987 }
4988
4989 e.xany.type = ClientMessage;
4990 e.xany.window = winPtr->wmInfoPtr->wrapperPtr->window;
4991 e.xclient.message_type = messageType;
4992 e.xclient.format = 32;
4993 e.xclient.data.l[0] = action;
4994 e.xclient.data.l[1] = property;
4995 e.xclient.data.l[2] = e.xclient.data.l[3] = e.xclient.data.l[4] = 0l;
4996
4997 XSendEvent(winPtr->display,
4998 RootWindow(winPtr->display, winPtr->screenNum), 0,
4999 SubstructureNotifyMask|SubstructureRedirectMask, &e);
5000 }
5001
5002 /*
5003 * CheckNetWmState --
5004 *
5005 * Updates the window attributes whenever the _NET_WM_STATE
5006 * property changes.
5007 *
5008 * Notes:
5009 *
5010 * Tk uses a single -zoomed state, while the EWMH spec supports
5011 * separate vertical and horizontal maximization. We consider
5012 * the window to be "zoomed" if _NET_WM_STATE_MAXIMIZED_VERT
5013 * and _NET_WM_STATE_MAXIMIZED_HORZ are both set.
5014 */
5015 static void CheckNetWmState(WmInfo *wmPtr, Atom *atoms, int numAtoms)
5016 {
5017 Tk_Window tkwin = (Tk_Window)wmPtr->wrapperPtr;
5018 int i;
5019 Atom _NET_WM_STATE_ABOVE
5020 = Tk_InternAtom(tkwin, "_NET_WM_STATE_ABOVE"),
5021 _NET_WM_STATE_MAXIMIZED_VERT
5022 = Tk_InternAtom(tkwin, "_NET_WM_STATE_MAXIMIZED_VERT"),
5023 _NET_WM_STATE_MAXIMIZED_HORZ
5024 = Tk_InternAtom(tkwin, "_NET_WM_STATE_MAXIMIZED_HORZ"),
5025 _NET_WM_STATE_FULLSCREEN
5026 = Tk_InternAtom(tkwin, "_NET_WM_STATE_FULLSCREEN");
5027
5028 wmPtr->attributes.topmost = 0;
5029 wmPtr->attributes.zoomed = 0;
5030 wmPtr->attributes.fullscreen = 0;
5031 for (i = 0; i < numAtoms; ++i) {
5032 if (atoms[i] == _NET_WM_STATE_ABOVE) {
5033 wmPtr->attributes.topmost = 1;
5034 } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
5035 wmPtr->attributes.zoomed |= 1;
5036 } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
5037 wmPtr->attributes.zoomed |= 2;
5038 } else if (atoms[i] == _NET_WM_STATE_FULLSCREEN) {
5039 wmPtr->attributes.fullscreen = 1;
5040 }
5041 }
5042
5043 wmPtr->attributes.zoomed = (wmPtr->attributes.zoomed == 3);
5044
5045 return;
5046 }
5047
5048 /*
5049 * UpdateNetWmState --
5050 *
5051 * Sets the _NET_WM_STATE property to match the requested attribute state
5052 * just prior to mapping a withdrawn window.
5053 */
5054 #define NET_WM_STATE_MAX_ATOMS 4
5055 static void UpdateNetWmState(WmInfo *wmPtr)
5056 {
5057 Tk_Window tkwin = (Tk_Window)wmPtr->wrapperPtr;
5058 Atom atoms[NET_WM_STATE_MAX_ATOMS];
5059 long numAtoms = 0;
5060
5061 if (wmPtr->reqState.topmost) {
5062 atoms[numAtoms++] = Tk_InternAtom(tkwin,"_NET_WM_STATE_ABOVE");
5063 }
5064 if (wmPtr->reqState.zoomed) {
5065 atoms[numAtoms++] = Tk_InternAtom(tkwin,"_NET_WM_STATE_MAXIMIZED_VERT");
5066 atoms[numAtoms++] = Tk_InternAtom(tkwin,"_NET_WM_STATE_MAXIMIZED_HORZ");
5067 }
5068 if (wmPtr->reqState.fullscreen) {
5069 atoms[numAtoms++] = Tk_InternAtom(tkwin, "_NET_WM_STATE_FULLSCREEN");
5070 }
5071
5072 XChangeProperty(Tk_Display(tkwin), wmPtr->wrapperPtr->window,
5073 Tk_InternAtom(tkwin, "_NET_WM_STATE"), XA_ATOM, 32,
5074 PropModeReplace, (unsigned char *)atoms, numAtoms);
5075 }
5076
5077 /*
5078 *----------------------------------------------------------------------
5079 *
5080 * WaitForConfigureNotify --
5081 *
5082 * This function is invoked in order to synchronize with the window
5083 * manager. It waits for a ConfigureNotify event to arrive, signalling
5084 * that the window manager has seen an attempt on our part to move or
5085 * resize a top-level window.
5086 *
5087 * Results:
5088 * None.
5089 *
5090 * Side effects:
5091 * Delays the execution of the process until a ConfigureNotify event
5092 * arrives with serial number at least as great as serial. This is useful
5093 * for two reasons:
5094 *
5095 * 1. It's important to distinguish ConfigureNotify events that are
5096 * coming in response to a request we've made from those generated
5097 * spontaneously by the user. The reason for this is that if the user
5098 * resizes the window we take that as an order to ignore geometry
5099 * requests coming from inside the window hierarchy. If we
5100 * accidentally interpret a response to our request as a
5101 * user-initiated action, the window will stop responding to new
5102 * geometry requests. To make this distinction, (a) this function sets
5103 * a flag for TopLevelEventProc to indicate that we're waiting to sync
5104 * with the wm, and (b) all changes to the size of a top-level window
5105 * are followed by calls to this function.
5106 * 2. Races and confusion can come about if there are multiple operations
5107 * outstanding at a time (e.g. two different resizes of the top-level
5108 * window: it's hard to tell which of the ConfigureNotify events
5109 * coming back is for which request).
5110 * While waiting, some events covered by StructureNotifyMask are
5111 * processed (ConfigureNotify, MapNotify, and UnmapNotify) and all others
5112 * are deferred.
5113 *
5114 *----------------------------------------------------------------------
5115 */
5116
5117 static void
5118 WaitForConfigureNotify(
5119 TkWindow *winPtr, /* Top-level window for which we want to see a
5120 * ConfigureNotify. */
5121 unsigned long serial) /* Serial number of resize request. Want to be
5122 * sure wm has seen this. */
5123 {
5124 WmInfo *wmPtr = winPtr->wmInfoPtr;
5125 XEvent event;
5126 int diff, code;
5127 int gotConfig = 0;
5128 Window window = (wmPtr->reparent != None) ? wmPtr->reparent : wmPtr->wrapperPtr->window;
5129
5130 /*
5131 * One more tricky detail about this function. In some cases the window
5132 * manager will decide to ignore a configure request (e.g. because it
5133 * thinks the window is already in the right place). To avoid hanging in
5134 * this situation, only wait for a few seconds, then give up.
5135 */
5136
5137 /*
5138 * Note too that if window manager has reparented the window it may achieve
5139 * the result by configuring the parent so we need to watch the parent too.
5140 */
5141
5142 if (window != wmPtr->wrapperPtr->window) {
5143 Tk_ErrorHandler handler;
5144 /*
5145 * We're going to have to wait for events on a window that
5146 * Tk doesn't own, so we have to tell X specially that we
5147 * want to get events on that window. To make matters worse,
5148 * it's possible that the window doesn't exist anymore (e.g.
5149 * the toplevel could have been withdrawn) so ignore events
5150 * occurring during the request.
5151 */
5152
5153 handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
5154 (Tk_ErrorProc *) NULL, (ClientData) NULL);
5155 XSelectInput(winPtr->display, window, StructureNotifyMask);
5156 Tk_DeleteErrorHandler(handler);
5157 }
5158
5159 while (!gotConfig) {
5160 wmPtr->flags |= WM_SYNC_PENDING;
5161 code = WaitForEvent(winPtr->display, wmPtr, ConfigureNotify, &event);
5162 wmPtr->flags &= ~WM_SYNC_PENDING;
5163 if (code != TCL_OK) {
5164 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
5165 printf("WaitForConfigureNotify giving up on %s\n",
5166 winPtr->pathName);
5167 }
5168 break;
5169 }
5170 diff = event.xconfigure.serial - serial;
5171 if (diff >= 0) {
5172 gotConfig = 1;
5173 }
5174 }
5175 wmPtr->flags &= ~WM_MOVE_PENDING;
5176 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
5177 printf("WaitForConfigureNotify finished with %s, serial %ld\n",
5178 winPtr->pathName, serial);
5179 }
5180
5181 if (window != wmPtr->wrapperPtr->window) {
5182 Tk_ErrorHandler handler;
5183 /*
5184 * Ignore errors that occur when we are de-selecting events on
5185 * window, since it's possible that the window doesn't exist
5186 * anymore (see comment above previous call to XSelectInput).
5187 */
5188
5189 handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
5190 (Tk_ErrorProc *) NULL, (ClientData) NULL);
5191 XSelectInput(winPtr->display, window, (long) 0);
5192 Tk_DeleteErrorHandler(handler);
5193 }
5194 }
5195
5196 /*
5197 *----------------------------------------------------------------------
5198 *
5199 * WaitForEvent --
5200 *
5201 * This function is used by WaitForConfigureNotify and WaitForMapNotify
5202 * to wait for an event of a certain type to arrive.
5203 *
5204 * Results:
5205 * Under normal conditions, TCL_OK is returned and an event for display
5206 * and window that matches "mask" is stored in *eventPtr. This event has
5207 * already been processed by Tk before this function returns. If a long
5208 * time goes by with no event of the right type arriving, or if an error
5209 * occurs while waiting for the event to arrive, then TCL_ERROR is
5210 * returned.
5211 *
5212 * Side effects:
5213 * While waiting for the desired event to occur, Configurenotify,
5214 * MapNotify, and UnmapNotify events for window are processed, as are all
5215 * ReparentNotify events.
5216 *
5217 *----------------------------------------------------------------------
5218 */
5219
5220 static int
5221 WaitForEvent(
5222 Display *display, /* Display event is coming from. */
5223 WmInfo *wmInfoPtr, /* Window for which event is desired. */
5224 int type, /* Type of event that is wanted. */
5225 XEvent *eventPtr) /* Place to store event. */
5226 {
5227 WaitRestrictInfo info;
5228 Tk_RestrictProc *oldRestrictProc;
5229 ClientData oldRestrictData;
5230 Tcl_Time timeout;
5231
5232 /*
5233 * Set up an event filter to select just the events we want, and a timer
5234 * handler, then wait for events until we get the event we want or a
5235 * timeout happens.
5236 */
5237
5238 info.display = display;
5239 info.wmInfoPtr = wmInfoPtr;
5240 info.type = type;
5241 info.eventPtr = eventPtr;
5242 info.foundEvent = 0;
5243 oldRestrictProc = Tk_RestrictEvents(WaitRestrictProc, (ClientData) &info,
5244 &oldRestrictData);
5245
5246 Tcl_GetTime(&timeout);
5247 timeout.sec += 2;
5248
5249 while (!info.foundEvent) {
5250 if (!TkUnixDoOneXEvent(&timeout)) {
5251 break;
5252 }
5253 }
5254 (void) Tk_RestrictEvents(oldRestrictProc, oldRestrictData,
5255 &oldRestrictData);
5256 if (info.foundEvent) {
5257 return TCL_OK;
5258 }
5259 return TCL_ERROR;
5260 }
5261
5262 /*
5263 *----------------------------------------------------------------------
5264 *
5265 * WaitRestrictProc --
5266 *
5267 * This function is a Tk_RestrictProc that is used to filter events while
5268 * WaitForEvent is active.
5269 *
5270 * Results:
5271 * Returns TK_PROCESS_EVENT if the right event is found. Also returns
5272 * TK_PROCESS_EVENT if any ReparentNotify event is found or if the event
5273 * is a ConfigureNotify, MapNotify, or UnmapNotify for window. Otherwise
5274 * returns TK_DEFER_EVENT.
5275 *
5276 * Side effects:
5277 * An event may get stored in the area indicated by the caller of
5278 * WaitForEvent.
5279 *
5280 *----------------------------------------------------------------------
5281 */
5282
5283 static Tk_RestrictAction
5284 WaitRestrictProc(
5285 ClientData clientData, /* Pointer to WaitRestrictInfo structure. */
5286 XEvent *eventPtr) /* Event that is about to be handled. */
5287 {
5288 WaitRestrictInfo *infoPtr = (WaitRestrictInfo *) clientData;
5289
5290 if (eventPtr->type == SelectionNotify) {
5291 return TK_PROCESS_EVENT;
5292 }
5293 if (eventPtr->type == ReparentNotify) {
5294 return TK_PROCESS_EVENT;
5295 }
5296 if (((eventPtr->xany.window != infoPtr->wmInfoPtr->wrapperPtr->window)
5297 && (eventPtr->xany.window != infoPtr->wmInfoPtr->reparent))
5298 || (eventPtr->xany.display != infoPtr->display)) {
5299 return TK_DEFER_EVENT;
5300 }
5301 if (eventPtr->type == infoPtr->type) {
5302 *infoPtr->eventPtr = *eventPtr;
5303 infoPtr->foundEvent = 1;
5304 return TK_PROCESS_EVENT;
5305 }
5306 if (eventPtr->type == ConfigureNotify || eventPtr->type == MapNotify
5307 || eventPtr->type == UnmapNotify) {
5308 return TK_PROCESS_EVENT;
5309 }
5310 return TK_DEFER_EVENT;
5311 }
5312
5313 /*
5314 *----------------------------------------------------------------------
5315 *
5316 * WaitForMapNotify --
5317 *
5318 * This function is invoked in order to synchronize with the window
5319 * manager. It waits for the window's mapped state to reach the value
5320 * given by mapped.
5321 *
5322 * Results:
5323 * None.
5324 *
5325 * Side effects:
5326 * Delays the execution of the process until winPtr becomes mapped or
5327 * unmapped, depending on the "mapped" argument. This allows us to
5328 * synchronize with the window manager, and allows us to identify changes
5329 * in window size that come about when the window manager first starts
5330 * managing the window (as opposed to those requested interactively by
5331 * the user later). See the comments for WaitForConfigureNotify and
5332 * WM_SYNC_PENDING. While waiting, some events covered by
5333 * StructureNotifyMask are processed and all others are deferred.
5334 *
5335 *----------------------------------------------------------------------
5336 */
5337
5338 static void
5339 WaitForMapNotify(
5340 TkWindow *winPtr, /* Top-level window for which we want to see a
5341 * particular mapping state. */
5342 int mapped) /* If non-zero, wait for window to become
5343 * mapped, otherwise wait for it to become
5344 * unmapped. */
5345 {
5346 WmInfo *wmPtr = winPtr->wmInfoPtr;
5347 XEvent event;
5348 int code;
5349
5350 while (1) {
5351 if (mapped) {
5352 if (winPtr->flags & TK_MAPPED) {
5353 break;
5354 }
5355 } else if (!(winPtr->flags & TK_MAPPED)) {
5356 break;
5357 }
5358 wmPtr->flags |= WM_SYNC_PENDING;
5359 code = WaitForEvent(winPtr->display, wmPtr,
5360 mapped ? MapNotify : UnmapNotify, &event);
5361 wmPtr->flags &= ~WM_SYNC_PENDING;
5362 if (code != TCL_OK) {
5363 /*
5364 * There are some bizarre situations in which the window manager
5365 * can't respond or chooses not to (e.g. if we've got a grab set
5366 * it can't respond). If this happens then just quit.
5367 */
5368
5369 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
5370 printf("WaitForMapNotify giving up on %s\n", winPtr->pathName);
5371 }
5372 break;
5373 }
5374 }
5375 wmPtr->flags &= ~WM_MOVE_PENDING;
5376 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
5377 printf("WaitForMapNotify finished with %s (winPtr %p, wmPtr %p)\n",
5378 winPtr->pathName, winPtr, wmPtr);
5379 }
5380 }
5381
5382 /*
5383 *--------------------------------------------------------------
5384 *
5385 * UpdateHints --
5386 *
5387 * This function is called to update the window manager's hints
5388 * information from the information in a WmInfo structure.
5389 *
5390 * Results:
5391 * None.
5392 *
5393 * Side effects:
5394 * Properties get changed for winPtr.
5395 *
5396 *--------------------------------------------------------------
5397 */
5398
5399 static void
5400 UpdateHints(
5401 TkWindow *winPtr)
5402 {
5403 WmInfo *wmPtr = winPtr->wmInfoPtr;
5404
5405 if (wmPtr->flags & WM_NEVER_MAPPED) {
5406 return;
5407 }
5408 XSetWMHints(winPtr->display, wmPtr->wrapperPtr->window, &wmPtr->hints);
5409 }
5410
5411 /*
5412 *--------------------------------------------------------------
5413 *
5414 * ParseGeometry --
5415 *
5416 * This function parses a geometry string and updates information used to
5417 * control the geometry of a top-level window.
5418 *
5419 * Results:
5420 * A standard Tcl return value, plus an error message in the interp's
5421 * result if an error occurs.
5422 *
5423 * Side effects:
5424 * The size and/or location of winPtr may change.
5425 *
5426 *--------------------------------------------------------------
5427 */
5428
5429 static int
5430 ParseGeometry(
5431 Tcl_Interp *interp, /* Used for error reporting. */
5432 char *string, /* String containing new geometry. Has the
5433 * standard form "=wxh+x+y". */
5434 TkWindow *winPtr) /* Pointer to top-level window whose geometry
5435 * is to be changed. */
5436 {
5437 register WmInfo *wmPtr = winPtr->wmInfoPtr;
5438 int x, y, width, height, flags;
5439 char *end;
5440 register char *p = string;
5441
5442 /*
5443 * The leading "=" is optional.
5444 */
5445
5446 if (*p == '=') {
5447 p++;
5448 }
5449
5450 /*
5451 * Parse the width and height, if they are present. Don't actually update
5452 * any of the fields of wmPtr until we've successfully parsed the entire
5453 * geometry string.
5454 */
5455
5456 width = wmPtr->width;
5457 height = wmPtr->height;
5458 x = wmPtr->x;
5459 y = wmPtr->y;
5460 flags = wmPtr->flags;
5461 if (isdigit(UCHAR(*p))) {
5462 width = strtoul(p, &end, 10);
5463 p = end;
5464 if (*p != 'x') {
5465 goto error;
5466 }
5467 p++;
5468 if (!isdigit(UCHAR(*p))) {
5469 goto error;
5470 }
5471 height = strtoul(p, &end, 10);
5472 p = end;
5473 }
5474
5475 /*
5476 * Parse the X and Y coordinates, if they are present.
5477 */
5478
5479 if (*p != '\0') {
5480 flags &= ~(WM_NEGATIVE_X | WM_NEGATIVE_Y);
5481 if (*p == '-') {
5482 flags |= WM_NEGATIVE_X;
5483 } else if (*p != '+') {
5484 goto error;
5485 }
5486 p++;
5487 if (!isdigit(UCHAR(*p)) && (*p != '-')) {
5488 goto error;
5489 }
5490 x = strtol(p, &end, 10);
5491 p = end;
5492 if (*p == '-') {
5493 flags |= WM_NEGATIVE_Y;
5494 } else if (*p != '+') {
5495 goto error;
5496 }
5497 p++;
5498 if (!isdigit(UCHAR(*p)) && (*p != '-')) {
5499 goto error;
5500 }
5501 y = strtol(p, &end, 10);
5502 if (*end != '\0') {
5503 goto error;
5504 }
5505
5506 /*
5507 * Assume that the geometry information came from the user, unless an
5508 * explicit source has been specified. Otherwise most window managers
5509 * assume that the size hints were program-specified and they ignore
5510 * them.
5511 */
5512
5513 if ((wmPtr->sizeHintsFlags & (USPosition|PPosition)) == 0) {
5514 wmPtr->sizeHintsFlags |= USPosition;
5515 flags |= WM_UPDATE_SIZE_HINTS;
5516 }
5517 }
5518
5519 /*
5520 * Everything was parsed OK. Update the fields of *wmPtr and arrange for
5521 * the appropriate information to be percolated out to the window manager
5522 * at the next idle moment.
5523 */
5524
5525 wmPtr->width = width;
5526 wmPtr->height = height;
5527 wmPtr->x = x;
5528 wmPtr->y = y;
5529 flags |= WM_MOVE_PENDING;
5530 wmPtr->flags = flags;
5531
5532 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
5533 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
5534 wmPtr->flags |= WM_UPDATE_PENDING;
5535 }
5536 return TCL_OK;
5537
5538 error:
5539 Tcl_AppendResult(interp, "bad geometry specifier \"", string, "\"", NULL);
5540 return TCL_ERROR;
5541 }
5542
5543 /*
5544 *----------------------------------------------------------------------
5545 *
5546 * Tk_GetRootCoords --
5547 *
5548 * Given a token for a window, this function traces through the window's
5549 * lineage to find the (virtual) root-window coordinates corresponding to
5550 * point (0,0) in the window.
5551 *
5552 * Results:
5553 * The locations pointed to by xPtr and yPtr are filled in with the root
5554 * coordinates of the (0,0) point in tkwin. If a virtual root window is
5555 * in effect for the window, then the coordinates in the virtual root are
5556 * returned.
5557 *
5558 * Side effects:
5559 * None.
5560 *
5561 *----------------------------------------------------------------------
5562 */
5563
5564 void
5565 Tk_GetRootCoords(
5566 Tk_Window tkwin, /* Token for window. */
5567 int *xPtr, /* Where to store x-displacement of (0,0). */
5568 int *yPtr) /* Where to store y-displacement of (0,0). */
5569 {
5570 int x, y;
5571 register TkWindow *winPtr = (TkWindow *) tkwin;
5572
5573 /*
5574 * Search back through this window's parents all the way to a top-level
5575 * window, combining the offsets of each window within its parent.
5576 */
5577
5578 x = y = 0;
5579 while (1) {
5580 x += winPtr->changes.x + winPtr->changes.border_width;
5581 y += winPtr->changes.y + winPtr->changes.border_width;
5582 if ((winPtr->wmInfoPtr != NULL)
5583 && (winPtr->wmInfoPtr->menubar == (Tk_Window) winPtr)) {
5584 /*
5585 * This window is a special menubar; switch over to its associated
5586 * toplevel, compensate for their differences in y coordinates,
5587 * then continue with the toplevel (in case it's embedded).
5588 */
5589 y -= winPtr->wmInfoPtr->menuHeight;
5590 winPtr = winPtr->wmInfoPtr->winPtr;
5591 continue;
5592 }
5593 if (winPtr->flags & TK_TOP_LEVEL) {
5594 TkWindow *otherPtr;
5595 if (!(winPtr->flags & TK_EMBEDDED)) {
5596 break;
5597 }
5598 otherPtr = TkpGetOtherWindow(winPtr);
5599 if (otherPtr == NULL) {
5600 /*
5601 * The container window is not in the same application. Query
5602 * the X server.
5603 */
5604
5605 Window root, dummyChild;
5606 int rootX, rootY;
5607
5608 root = winPtr->wmInfoPtr->vRoot;
5609 if (root == None) {
5610 root = RootWindowOfScreen(Tk_Screen((Tk_Window)winPtr));
5611 }
5612 XTranslateCoordinates(winPtr->display, winPtr->window,
5613 root, 0, 0, &rootX, &rootY, &dummyChild);
5614 x += rootX;
5615 y += rootY;
5616 break;
5617 } else {
5618 /*
5619 * The container window is in the same application. Let's
5620 * query its coordinates.
5621 */
5622
5623 winPtr = otherPtr;
5624 continue;
5625 }
5626 }
5627 winPtr = winPtr->parentPtr;
5628 if (winPtr == NULL) {
5629 break;
5630 }
5631 }
5632 *xPtr = x;
5633 *yPtr = y;
5634 }
5635
5636 /*
5637 *----------------------------------------------------------------------
5638 *
5639 * Tk_CoordsToWindow --
5640 *
5641 * Given the (virtual) root coordinates of a point, this function returns
5642 * the token for the top-most window covering that point, if there exists
5643 * such a window in this application.
5644 *
5645 * Results:
5646 * The return result is either a token for the window corresponding to
5647 * rootX and rootY, or else NULL to indicate that there is no such
5648 * window.
5649 *
5650 * Side effects:
5651 * None.
5652 *
5653 *----------------------------------------------------------------------
5654 */
5655
5656 Tk_Window
5657 Tk_CoordsToWindow(
5658 int rootX, int rootY, /* Coordinates of point in root window. If a
5659 * virtual-root window manager is in use,
5660 * these coordinates refer to the virtual
5661 * root, not the real root. */
5662 Tk_Window tkwin) /* Token for any window in application; used
5663 * to identify the display. */
5664 {
5665 Window window, parent, child;
5666 int x, y, childX, childY, tmpx, tmpy, bd;
5667 WmInfo *wmPtr;
5668 TkWindow *winPtr, *childPtr, *nextPtr;
5669 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
5670 Tk_ErrorHandler handler = NULL;
5671
5672 /*
5673 * Step 1: scan the list of toplevel windows to see if there is a virtual
5674 * root for the screen we're interested in. If so, we have to translate
5675 * the coordinates from virtual root to root coordinates.
5676 */
5677
5678 parent = window = RootWindowOfScreen(Tk_Screen(tkwin));
5679 x = rootX;
5680 y = rootY;
5681 for (wmPtr = (WmInfo *) dispPtr->firstWmPtr; wmPtr != NULL;
5682 wmPtr = wmPtr->nextPtr) {
5683 if (Tk_Screen(wmPtr->winPtr) != Tk_Screen(tkwin)) {
5684 continue;
5685 }
5686 if (wmPtr->vRoot == None) {
5687 continue;
5688 }
5689 UpdateVRootGeometry(wmPtr);
5690 parent = wmPtr->vRoot;
5691 break;
5692 }
5693
5694 /*
5695 * Step 2: work down through the window hierarchy starting at the root.
5696 * For each window, find the child that contains the given point and then
5697 * see if this child is either a wrapper for one of our toplevel windows
5698 * or a window manager decoration window for one of our toplevels. This
5699 * approach handles several tricky cases:
5700 *
5701 * 1. There may be a virtual root window between the root and one of our
5702 * toplevels.
5703 * 2. If a toplevel is embedded, we may have to search through the
5704 * windows of the container application(s) before getting to the
5705 * toplevel.
5706 */
5707
5708 handler = Tk_CreateErrorHandler(Tk_Display(tkwin), -1, -1, -1, NULL, NULL);
5709 while (1) {
5710 if (XTranslateCoordinates(Tk_Display(tkwin), parent, window,
5711 x, y, &childX, &childY, &child) == False) {
5712 /*
5713 * We can end up here when the window is in the middle of being
5714 * deleted
5715 */
5716
5717 Tk_DeleteErrorHandler(handler);
5718 return NULL;
5719 }
5720 if (child == None) {
5721 Tk_DeleteErrorHandler(handler);
5722 return NULL;
5723 }
5724 for (wmPtr = (WmInfo *) dispPtr->firstWmPtr; wmPtr != NULL;
5725 wmPtr = wmPtr->nextPtr) {
5726 if (wmPtr->reparent == child) {
5727 goto gotToplevel;
5728 }
5729 if (wmPtr->wrapperPtr != NULL) {
5730 if (child == wmPtr->wrapperPtr->window) {
5731 goto gotToplevel;
5732 }
5733 } else if (child == wmPtr->winPtr->window) {
5734 goto gotToplevel;
5735 }
5736 }
5737 x = childX;
5738 y = childY;
5739 parent = window;
5740 window = child;
5741 }
5742
5743 gotToplevel:
5744 if (handler) {
5745 /*
5746 * Check value of handler, because we can reach this label from above
5747 * or below
5748 */
5749
5750 Tk_DeleteErrorHandler(handler);
5751 handler = NULL;
5752 }
5753 winPtr = wmPtr->winPtr;
5754 if (winPtr->mainPtr != ((TkWindow *) tkwin)->mainPtr) {
5755 return NULL;
5756 }
5757
5758 /*
5759 * Step 3: at this point winPtr and wmPtr refer to the toplevel that
5760 * contains the given coordinates, and childX and childY give the
5761 * translated coordinates in the *parent* of the toplevel. Now decide
5762 * whether the coordinates are in the menubar or the actual toplevel, and
5763 * translate the coordinates into the coordinate system of that window.
5764 */
5765
5766 x = childX - winPtr->changes.x;
5767 y = childY - winPtr->changes.y;
5768 if ((x < 0) || (x >= winPtr->changes.width)
5769 || (y >= winPtr->changes.height)) {
5770 return NULL;
5771 }
5772 if (y < 0) {
5773 winPtr = (TkWindow *) wmPtr->menubar;
5774 if (winPtr == NULL) {
5775 return NULL;
5776 }
5777 y += wmPtr->menuHeight;
5778 if (y < 0) {
5779 return NULL;
5780 }
5781 }
5782
5783 /*
5784 * Step 4: work down through the hierarchy underneath the current window.
5785 * At each level, scan through all the children to find the highest one in
5786 * the stacking order that contains the point. Then repeat the whole
5787 * process on that child.
5788 */
5789
5790 while (1) {
5791 nextPtr = NULL;
5792 for (childPtr = winPtr->childList; childPtr != NULL;
5793 childPtr = childPtr->nextPtr) {
5794 if (!Tk_IsMapped(childPtr)
5795 || (childPtr->flags & TK_TOP_HIERARCHY)) {
5796 continue;
5797 }
5798 if (childPtr->flags & TK_REPARENTED) {
5799 continue;
5800 }
5801 tmpx = x - childPtr->changes.x;
5802 tmpy = y - childPtr->changes.y;
5803 bd = childPtr->changes.border_width;
5804 if ((tmpx >= -bd) && (tmpy >= -bd)
5805 && (tmpx < (childPtr->changes.width + bd))
5806 && (tmpy < (childPtr->changes.height + bd))) {
5807 nextPtr = childPtr;
5808 }
5809 }
5810 if (nextPtr == NULL) {
5811 break;
5812 }
5813 winPtr = nextPtr;
5814 x -= winPtr->changes.x;
5815 y -= winPtr->changes.y;
5816 if ((winPtr->flags & TK_CONTAINER)
5817 && (winPtr->flags & TK_BOTH_HALVES)) {
5818 /*
5819 * The window containing the point is a container, and the
5820 * embedded application is in this same process. Switch over to
5821 * the toplevel for the embedded application and start processing
5822 * that toplevel from scratch.
5823 */
5824
5825 winPtr = TkpGetOtherWindow(winPtr);
5826 if (winPtr == NULL) {
5827 return NULL;
5828 }
5829 wmPtr = winPtr->wmInfoPtr;
5830 childX = x;
5831 childY = y;
5832 goto gotToplevel;
5833 }
5834 }
5835 return (Tk_Window) winPtr;
5836 }
5837
5838 /*
5839 *----------------------------------------------------------------------
5840 *
5841 * UpdateVRootGeometry --
5842 *
5843 * This function is called to update all the virtual root geometry
5844 * information in wmPtr.
5845 *
5846 * Results:
5847 * None.
5848 *
5849 * Side effects:
5850 * The vRootX, vRootY, vRootWidth, and vRootHeight fields in wmPtr are
5851 * filled with the most up-to-date information.
5852 *
5853 *----------------------------------------------------------------------
5854 */
5855
5856 static void
5857 UpdateVRootGeometry(
5858 WmInfo *wmPtr) /* Window manager information to be updated.
5859 * The wmPtr->vRoot field must be valid. */
5860 {
5861 TkWindow *winPtr = wmPtr->winPtr;
5862 int bd;
5863 unsigned int dummy;
5864 Window dummy2;
5865 Status status;
5866 Tk_ErrorHandler handler;
5867
5868 /*
5869 * If this isn't a virtual-root window manager, just return information
5870 * about the screen.
5871 */
5872
5873 wmPtr->flags &= ~WM_VROOT_OFFSET_STALE;
5874 if (wmPtr->vRoot == None) {
5875 noVRoot:
5876 wmPtr->vRootX = wmPtr->vRootY = 0;
5877 wmPtr->vRootWidth = DisplayWidth(winPtr->display, winPtr->screenNum);
5878 wmPtr->vRootHeight = DisplayHeight(winPtr->display, winPtr->screenNum);
5879 return;
5880 }
5881
5882 /*
5883 * Refresh the virtual root information if it's out of date.
5884 */
5885
5886 handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1, NULL, NULL);
5887 status = XGetGeometry(winPtr->display, wmPtr->vRoot,
5888 &dummy2, &wmPtr->vRootX, &wmPtr->vRootY,
5889 (unsigned int *) &wmPtr->vRootWidth,
5890 (unsigned int *) &wmPtr->vRootHeight, (unsigned int *) &bd,
5891 &dummy);
5892 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
5893 printf("UpdateVRootGeometry: x = %d, y = %d, width = %d, ",
5894 wmPtr->vRootX, wmPtr->vRootY, wmPtr->vRootWidth);
5895 printf("height = %d, status = %d\n", wmPtr->vRootHeight, status);
5896 }
5897 Tk_DeleteErrorHandler(handler);
5898 if (status == 0) {
5899 /*
5900 * The virtual root is gone! Pretend that it never existed.
5901 */
5902
5903 wmPtr->vRoot = None;
5904 goto noVRoot;
5905 }
5906 }
5907
5908 /*
5909 *----------------------------------------------------------------------
5910 *
5911 * Tk_GetVRootGeometry --
5912 *
5913 * This function returns information about the virtual root window
5914 * corresponding to a particular Tk window.
5915 *
5916 * Results:
5917 * The values at xPtr, yPtr, widthPtr, and heightPtr are set with the
5918 * offset and dimensions of the root window corresponding to tkwin. If
5919 * tkwin is being managed by a virtual root window manager these values
5920 * correspond to the virtual root window being used for tkwin; otherwise
5921 * the offsets will be 0 and the dimensions will be those of the screen.
5922 *
5923 * Side effects:
5924 * Vroot window information is refreshed if it is out of date.
5925 *
5926 *----------------------------------------------------------------------
5927 */
5928
5929 void
5930 Tk_GetVRootGeometry(
5931 Tk_Window tkwin, /* Window whose virtual root is to be
5932 * queried. */
5933 int *xPtr, int *yPtr, /* Store x and y offsets of virtual root
5934 * here. */
5935 int *widthPtr, int *heightPtr)
5936 /* Store dimensions of virtual root here. */
5937 {
5938 WmInfo *wmPtr;
5939 TkWindow *winPtr = (TkWindow *) tkwin;
5940
5941 /*
5942 * Find the top-level window for tkwin, and locate the window manager
5943 * information for that window.
5944 */
5945
5946 while (!(winPtr->flags & TK_TOP_HIERARCHY)
5947 && (winPtr->parentPtr != NULL)) {
5948 winPtr = winPtr->parentPtr;
5949 }
5950 wmPtr = winPtr->wmInfoPtr;
5951 if (wmPtr == NULL) {
5952 /* Punt. */
5953 *xPtr = 0;
5954 *yPtr = 0;
5955 *widthPtr = 0;
5956 *heightPtr = 0;
5957 }
5958
5959 /*
5960 * Make sure that the geometry information is up-to-date, then copy it out
5961 * to the caller.
5962 */
5963
5964 if (wmPtr->flags & WM_VROOT_OFFSET_STALE) {
5965 UpdateVRootGeometry(wmPtr);
5966 }
5967 *xPtr = wmPtr->vRootX;
5968 *yPtr = wmPtr->vRootY;
5969 *widthPtr = wmPtr->vRootWidth;
5970 *heightPtr = wmPtr->vRootHeight;
5971 }
5972
5973 /*
5974 *----------------------------------------------------------------------
5975 *
5976 * Tk_MoveToplevelWindow --
5977 *
5978 * This function is called instead of Tk_MoveWindow to adjust the x-y
5979 * location of a top-level window. It delays the actual move to a later
5980 * time and keeps window-manager information up-to-date with the move
5981 *
5982 * Results:
5983 * None.
5984 *
5985 * Side effects:
5986 * The window is eventually moved so that its upper-left corner
5987 * (actually, the upper-left corner of the window's decorative frame, if
5988 * there is one) is at (x,y).
5989 *
5990 *----------------------------------------------------------------------
5991 */
5992
5993 void
5994 Tk_MoveToplevelWindow(
5995 Tk_Window tkwin, /* Window to move. */
5996 int x, int y) /* New location for window (within parent). */
5997 {
5998 TkWindow *winPtr = (TkWindow *) tkwin;
5999 register WmInfo *wmPtr = winPtr->wmInfoPtr;
6000
6001 if (!(winPtr->flags & TK_TOP_LEVEL)) {
6002 Tcl_Panic("Tk_MoveToplevelWindow called with non-toplevel window");
6003 }
6004 wmPtr->x = x;
6005 wmPtr->y = y;
6006 wmPtr->flags |= WM_MOVE_PENDING;
6007 wmPtr->flags &= ~(WM_NEGATIVE_X|WM_NEGATIVE_Y);
6008 if ((wmPtr->sizeHintsFlags & (USPosition|PPosition)) == 0) {
6009 wmPtr->sizeHintsFlags |= USPosition;
6010 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
6011 }
6012
6013 /*
6014 * If the window has already been mapped, must bring its geometry
6015 * up-to-date immediately, otherwise an event might arrive from the server
6016 * that would overwrite wmPtr->x and wmPtr->y and lose the new position.
6017 */
6018
6019 if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
6020 if (wmPtr->flags & WM_UPDATE_PENDING) {
6021 Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
6022 }
6023 UpdateGeometryInfo((ClientData) winPtr);
6024 }
6025 }
6026
6027 /*
6028 *----------------------------------------------------------------------
6029 *
6030 * UpdateWmProtocols --
6031 *
6032 * This function transfers the most up-to-date information about window
6033 * manager protocols from the WmInfo structure to the actual property on
6034 * the top-level window.
6035 *
6036 * Results:
6037 * None.
6038 *
6039 * Side effects:
6040 * The WM_PROTOCOLS property gets changed for wmPtr's window.
6041 *
6042 *----------------------------------------------------------------------
6043 */
6044
6045 static void
6046 UpdateWmProtocols(
6047 register WmInfo *wmPtr) /* Information about top-level window. */
6048 {
6049 register ProtocolHandler *protPtr;
6050 Atom deleteWindowAtom;
6051 int count;
6052 Atom *arrayPtr, *atomPtr;
6053
6054 /*
6055 * There are only two tricky parts here. First, there could be any number
6056 * of atoms for the window, so count them and malloc an array to hold all
6057 * of their atoms. Second, we *always* want to respond to the
6058 * WM_DELETE_WINDOW protocol, even if no-one's officially asked.
6059 */
6060
6061 for (protPtr = wmPtr->protPtr, count = 1; protPtr != NULL;
6062 protPtr = protPtr->nextPtr, count++) {
6063 /* Empty loop body; we're just counting the handlers. */
6064 }
6065 arrayPtr = (Atom *) ckalloc((unsigned) (count * sizeof(Atom)));
6066 deleteWindowAtom = Tk_InternAtom((Tk_Window) wmPtr->winPtr,
6067 "WM_DELETE_WINDOW");
6068 arrayPtr[0] = deleteWindowAtom;
6069 for (protPtr = wmPtr->protPtr, atomPtr = &arrayPtr[1];
6070 protPtr != NULL; protPtr = protPtr->nextPtr) {
6071 if (protPtr->protocol != deleteWindowAtom) {
6072 *atomPtr = protPtr->protocol;
6073 atomPtr++;
6074 }
6075 }
6076 XChangeProperty(wmPtr->winPtr->display, wmPtr->wrapperPtr->window,
6077 Tk_InternAtom((Tk_Window) wmPtr->winPtr, "WM_PROTOCOLS"),
6078 XA_ATOM, 32, PropModeReplace, (unsigned char *) arrayPtr,
6079 atomPtr-arrayPtr);
6080 ckfree((char *) arrayPtr);
6081 }
6082
6083 /*
6084 *----------------------------------------------------------------------
6085 *
6086 * TkWmProtocolEventProc --
6087 *
6088 * This function is called by the Tk_HandleEvent whenever a ClientMessage
6089 * event arrives whose type is "WM_PROTOCOLS". This function handles the
6090 * message from the window manager in an appropriate fashion.
6091 *
6092 * Results:
6093 * None.
6094 *
6095 * Side effects:
6096 * Depends on what sort of handler, if any, was set up for the protocol.
6097 *
6098 *----------------------------------------------------------------------
6099 */
6100
6101 void
6102 TkWmProtocolEventProc(
6103 TkWindow *winPtr, /* Window to which the event was sent. */
6104 XEvent *eventPtr) /* X event. */
6105 {
6106 WmInfo *wmPtr;
6107 register ProtocolHandler *protPtr;
6108 Atom protocol;
6109 int result;
6110 CONST char *protocolName;
6111 Tcl_Interp *interp;
6112
6113 wmPtr = winPtr->wmInfoPtr;
6114 if (wmPtr == NULL) {
6115 return;
6116 }
6117 protocol = (Atom) eventPtr->xclient.data.l[0];
6118
6119 /*
6120 * Note: it's very important to retrieve the protocol name now, before
6121 * invoking the command, even though the name won't be used until after
6122 * the command returns. This is because the command could delete winPtr,
6123 * making it impossible for us to use it later in the call to
6124 * Tk_GetAtomName.
6125 */
6126
6127 protocolName = Tk_GetAtomName((Tk_Window) winPtr, protocol);
6128 for (protPtr = wmPtr->protPtr; protPtr != NULL;
6129 protPtr = protPtr->nextPtr) {
6130 if (protocol == protPtr->protocol) {
6131 Tcl_Preserve((ClientData) protPtr);
6132 interp = protPtr->interp;
6133 Tcl_Preserve((ClientData) interp);
6134 winPtr->dispPtr->lastEventTime = (Time) eventPtr->xclient.data.l[1];
6135 result = LangDoCallback(protPtr->interp, protPtr->command, 0, 0);
6136 if (result != TCL_OK) {
6137 Tcl_AddErrorInfo(interp, "\n (command for \"");
6138 Tcl_AddErrorInfo(interp, protocolName);
6139 Tcl_AddErrorInfo(interp,
6140 "\" window manager protocol)");
6141 Tcl_BackgroundError(interp);
6142 }
6143 Tcl_Release((ClientData) interp);
6144 Tcl_Release((ClientData) protPtr);
6145 return;
6146 }
6147 }
6148
6149 /*
6150 * No handler was present for this protocol. If this is a WM_DELETE_WINDOW
6151 * message then just destroy the window.
6152 */
6153
6154 if (protocol == Tk_InternAtom((Tk_Window) winPtr, "WM_DELETE_WINDOW")) {
6155 Tk_DestroyWindow((Tk_Window) wmPtr->winPtr);
6156 }
6157 }
6158
6159 /*
6160 *----------------------------------------------------------------------
6161 *
6162 * TkWmStackorderToplevelWrapperMap --
6163 *
6164 * This function will create a table that maps the reparent wrapper X id
6165 * for a toplevel to the TkWindow structure that is wraps. Tk keeps track
6166 * of a mapping from the window X id to the TkWindow structure but that
6167 * does us no good here since we only get the X id of the wrapper window.
6168 * Only those toplevel windows that are mapped have a position in the
6169 * stacking order.
6170 *
6171 * Results:
6172 * None.
6173 *
6174 * Side effects:
6175 * Adds entries to the passed hashtable.
6176 *
6177 *----------------------------------------------------------------------
6178 */
6179
6180 static void
6181 TkWmStackorderToplevelWrapperMap(
6182 TkWindow *winPtr, /* TkWindow to recurse on */
6183 Display *display, /* X display of parent window */
6184 Tcl_HashTable *table) /* Maps X id to TkWindow */
6185 {
6186 TkWindow *childPtr;
6187
6188 if (Tk_IsMapped(winPtr) && Tk_IsTopLevel(winPtr) &&
6189 !Tk_IsEmbedded(winPtr) && (winPtr->display == display)) {
6190 Window wrapper = (winPtr->wmInfoPtr->reparent != None)
6191 ? winPtr->wmInfoPtr->reparent
6192 : winPtr->wmInfoPtr->wrapperPtr->window;
6193 Tcl_HashEntry *hPtr;
6194 int newEntry;
6195
6196 hPtr = Tcl_CreateHashEntry(table, (char *) wrapper, &newEntry);
6197 Tcl_SetHashValue(hPtr, winPtr);
6198 }
6199
6200 for (childPtr = winPtr->childList; childPtr != NULL;
6201 childPtr = childPtr->nextPtr) {
6202 TkWmStackorderToplevelWrapperMap(childPtr, display, table);
6203 }
6204 }
6205
6206 /*
6207 *----------------------------------------------------------------------
6208 *
6209 * TkWmStackorderToplevel --
6210 *
6211 * This function returns the stack order of toplevel windows.
6212 *
6213 * Results:
6214 * An array of pointers to tk window objects in stacking order or else
6215 * NULL if there was an error.
6216 *
6217 * Side effects:
6218 * None.
6219 *
6220 *----------------------------------------------------------------------
6221 */
6222
6223 TkWindow **
6224 TkWmStackorderToplevel(
6225 TkWindow *parentPtr) /* Parent toplevel window. */
6226 {
6227 Window dummy1, dummy2, vRoot;
6228 Window *children;
6229 unsigned int numChildren, i;
6230 TkWindow *childWinPtr, **windows, **window_ptr;
6231 Tcl_HashTable table;
6232 Tcl_HashEntry *hPtr;
6233 Tcl_HashSearch search;
6234
6235 /*
6236 * Map X Window ids to a TkWindow of the wrapped toplevel.
6237 */
6238
6239 Tcl_InitHashTable(&table, TCL_ONE_WORD_KEYS);
6240 TkWmStackorderToplevelWrapperMap(parentPtr, parentPtr->display, &table);
6241
6242 window_ptr = windows = (TkWindow **)
6243 ckalloc((table.numEntries+1) * sizeof(TkWindow *));
6244
6245 /*
6246 * Special cases: If zero or one toplevels were mapped there is no need to
6247 * call XQueryTree.
6248 */
6249
6250 switch (table.numEntries) {
6251 case 0:
6252 windows[0] = NULL;
6253 goto done;
6254 case 1:
6255 hPtr = Tcl_FirstHashEntry(&table, &search);
6256 windows[0] = (TkWindow *) Tcl_GetHashValue(hPtr);
6257 windows[1] = NULL;
6258 goto done;
6259 }
6260
6261 vRoot = parentPtr->wmInfoPtr->vRoot;
6262 if (vRoot == None) {
6263 vRoot = RootWindowOfScreen(Tk_Screen((Tk_Window) parentPtr));
6264 }
6265
6266 if (XQueryTree(parentPtr->display, vRoot, &dummy1, &dummy2,
6267 &children, &numChildren) == 0) {
6268 ckfree((char *) windows);
6269 windows = NULL;
6270 } else {
6271 for (i = 0; i < numChildren; i++) {
6272 hPtr = Tcl_FindHashEntry(&table, (char *) children[i]);
6273 if (hPtr != NULL) {
6274 childWinPtr = (TkWindow *) Tcl_GetHashValue(hPtr);
6275 *window_ptr++ = childWinPtr;
6276 }
6277 }
6278 if ((window_ptr - windows) != table.numEntries) {
6279 Tcl_Panic("num matched toplevel windows does not equal num children");
6280 }
6281 *window_ptr = NULL;
6282 if (numChildren) {
6283 XFree((char *) children);
6284 }
6285 }
6286
6287 done:
6288 Tcl_DeleteHashTable(&table);
6289 return windows;
6290 }
6291
6292 /*
6293 *----------------------------------------------------------------------
6294 *
6295 * TkWmRestackToplevel --
6296 *
6297 * This function restacks a top-level window.
6298 *
6299 * Results:
6300 * None.
6301 *
6302 * Side effects:
6303 * WinPtr gets restacked as specified by aboveBelow and otherPtr.
6304 *
6305 *----------------------------------------------------------------------
6306 */
6307
6308 void
6309 TkWmRestackToplevel(
6310 TkWindow *winPtr, /* Window to restack. */
6311 int aboveBelow, /* Gives relative position for restacking;
6312 * must be Above or Below. */
6313 TkWindow *otherPtr) /* Window relative to which to restack; if
6314 * NULL, then winPtr gets restacked above or
6315 * below *all* siblings. */
6316 {
6317 XWindowChanges changes;
6318 unsigned int mask;
6319 TkWindow *wrapperPtr;
6320
6321 memset(&changes, 0, sizeof(XWindowChanges));
6322 changes.stack_mode = aboveBelow;
6323 mask = CWStackMode;
6324
6325 /*
6326 * Make sure that winPtr and its wrapper window have been created.
6327 */
6328
6329 if (winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
6330 TkWmMapWindow(winPtr);
6331 }
6332 wrapperPtr = winPtr->wmInfoPtr->wrapperPtr;
6333
6334 if (otherPtr != NULL) {
6335 /*
6336 * The window is to be restacked with respect to another toplevel.
6337 * Make sure it has been created as well.
6338 */
6339
6340 if (otherPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
6341 TkWmMapWindow(otherPtr);
6342 }
6343 changes.sibling = otherPtr->wmInfoPtr->wrapperPtr->window;
6344 mask |= CWSibling;
6345 }
6346
6347 /*
6348 * Reconfigure the window. Note that we use XReconfigureWMWindow instead
6349 * of XConfigureWindow, in order to handle the case where the window is to
6350 * be restacked with respect to another toplevel. See [ICCCM] 4.1.5
6351 * "Configuring the Window" and XReconfigureWMWindow(3) for details.
6352 */
6353
6354 XReconfigureWMWindow(winPtr->display, wrapperPtr->window,
6355 Tk_ScreenNumber((Tk_Window) winPtr), mask, &changes);
6356 }
6357
6358
6359 /*
6360 *----------------------------------------------------------------------
6361 *
6362 * TkWmAddToColormapWindows --
6363 *
6364 * This function is called to add a given window to the
6365 * WM_COLORMAP_WINDOWS property for its top-level, if it isn't already
6366 * there. It is invoked by the Tk code that creates a new colormap, in
6367 * order to make sure that colormap information is propagated to the
6368 * window manager by default.
6369 *
6370 * Results:
6371 * None.
6372 *
6373 * Side effects:
6374 * WinPtr's window gets added to the WM_COLORMAP_WINDOWS property of its
6375 * nearest top-level ancestor, unless the colormaps have been set
6376 * explicitly with the "wm colormapwindows" command.
6377 *
6378 *----------------------------------------------------------------------
6379 */
6380
6381 void
6382 TkWmAddToColormapWindows(
6383 TkWindow *winPtr) /* Window with a non-default colormap. Should
6384 * not be a top-level window. */
6385 {
6386 TkWindow *wrapperPtr;
6387 TkWindow *topPtr;
6388 Window *oldPtr, *newPtr;
6389 int count, i;
6390
6391 if (winPtr->window == None) {
6392 return;
6393 }
6394
6395 for (topPtr = winPtr->parentPtr; ; topPtr = topPtr->parentPtr) {
6396 if (topPtr == NULL) {
6397 /*
6398 * Window is being deleted. Skip the whole operation.
6399 */
6400
6401 return;
6402 }
6403 if (topPtr->flags & TK_TOP_HIERARCHY) {
6404 break;
6405 }
6406 }
6407 if (topPtr->wmInfoPtr == NULL) {
6408 return;
6409 }
6410
6411 if (topPtr->wmInfoPtr->flags & WM_COLORMAPS_EXPLICIT) {
6412 return;
6413 }
6414 if (topPtr->wmInfoPtr->wrapperPtr == NULL) {
6415 CreateWrapper(topPtr->wmInfoPtr);
6416 }
6417 wrapperPtr = topPtr->wmInfoPtr->wrapperPtr;
6418
6419 /*
6420 * Fetch the old value of the property.
6421 */
6422
6423 if (XGetWMColormapWindows(topPtr->display, wrapperPtr->window,
6424 &oldPtr, &count) == 0) {
6425 oldPtr = NULL;
6426 count = 0;
6427 }
6428
6429 /*
6430 * Make sure that the window isn't already in the list.
6431 */
6432
6433 for (i = 0; i < count; i++) {
6434 if (oldPtr[i] == winPtr->window) {
6435 return;
6436 }
6437 }
6438
6439 /*
6440 * Make a new bigger array and use it to reset the property. Automatically
6441 * add the toplevel itself as the last element of the list.
6442 */
6443
6444 newPtr = (Window *) ckalloc((unsigned) ((count+2)*sizeof(Window)));
6445 for (i = 0; i < count; i++) {
6446 newPtr[i] = oldPtr[i];
6447 }
6448 if (count == 0) {
6449 count++;
6450 }
6451 newPtr[count-1] = winPtr->window;
6452 newPtr[count] = topPtr->window;
6453 XSetWMColormapWindows(topPtr->display, wrapperPtr->window, newPtr,
6454 count+1);
6455 ckfree((char *) newPtr);
6456 if (oldPtr != NULL) {
6457 XFree((char *) oldPtr);
6458 }
6459 }
6460
6461 /*
6462 *----------------------------------------------------------------------
6463 *
6464 * TkWmRemoveFromColormapWindows --
6465 *
6466 * This function is called to remove a given window from the
6467 * WM_COLORMAP_WINDOWS property for its top-level. It is invoked when
6468 * windows are deleted.
6469 *
6470 * Results:
6471 * None.
6472 *
6473 * Side effects:
6474 * WinPtr's window gets removed from the WM_COLORMAP_WINDOWS property of
6475 * its nearest top-level ancestor, unless the top-level itself is being
6476 * deleted too.
6477 *
6478 *----------------------------------------------------------------------
6479 */
6480
6481 void
6482 TkWmRemoveFromColormapWindows(
6483 TkWindow *winPtr) /* Window that may be present in
6484 * WM_COLORMAP_WINDOWS property for its
6485 * top-level. Should not be a top-level
6486 * window. */
6487 {
6488 TkWindow *wrapperPtr;
6489 TkWindow *topPtr;
6490 Window *oldPtr;
6491 int count, i, j;
6492
6493 if (winPtr->window == None) {
6494 return;
6495 }
6496
6497 for (topPtr = winPtr->parentPtr; ; topPtr = topPtr->parentPtr) {
6498 if (topPtr == NULL) {
6499 /*
6500 * Ancestors have been deleted, so skip the whole operation.
6501 * Seems like this can't ever happen?
6502 */
6503
6504 return;
6505 }
6506 if (topPtr->flags & TK_TOP_HIERARCHY) {
6507 break;
6508 }
6509 }
6510 if (topPtr->flags & TK_ALREADY_DEAD) {
6511 /*
6512 * Top-level is being deleted, so there's no need to cleanup the
6513 * WM_COLORMAP_WINDOWS property.
6514 */
6515
6516 return;
6517 }
6518 if (topPtr->wmInfoPtr == NULL) {
6519 return;
6520 }
6521
6522 if (topPtr->wmInfoPtr->wrapperPtr == NULL) {
6523 CreateWrapper(topPtr->wmInfoPtr);
6524 }
6525 wrapperPtr = topPtr->wmInfoPtr->wrapperPtr;
6526 if (wrapperPtr == NULL) {
6527 return;
6528 }
6529
6530 /*
6531 * Fetch the old value of the property.
6532 */
6533
6534 if (XGetWMColormapWindows(topPtr->display, wrapperPtr->window,
6535 &oldPtr, &count) == 0) {
6536 return;
6537 }
6538
6539 /*
6540 * Find the window and slide the following ones down to cover it up.
6541 */
6542
6543 for (i = 0; i < count; i++) {
6544 if (oldPtr[i] == winPtr->window) {
6545 for (j = i ; j < count-1; j++) {
6546 oldPtr[j] = oldPtr[j+1];
6547 }
6548 XSetWMColormapWindows(topPtr->display, wrapperPtr->window,
6549 oldPtr, count-1);
6550 break;
6551 }
6552 }
6553 XFree((char *) oldPtr);
6554 }
6555
6556 /*
6557 *----------------------------------------------------------------------
6558 *
6559 * TkGetPointerCoords --
6560 *
6561 * Fetch the position of the mouse pointer.
6562 *
6563 * Results:
6564 * *xPtr and *yPtr are filled in with the (virtual) root coordinates of
6565 * the mouse pointer for tkwin's display. If the pointer isn't on tkwin's
6566 * screen, then -1 values are returned for both coordinates. The argument
6567 * tkwin must be a toplevel window.
6568 *
6569 * Side effects:
6570 * None.
6571 *
6572 *----------------------------------------------------------------------
6573 */
6574
6575 void
6576 TkGetPointerCoords(
6577 Tk_Window tkwin, /* Toplevel window that identifies screen on
6578 * which lookup is to be done. */
6579 int *xPtr, int *yPtr) /* Store pointer coordinates here. */
6580 {
6581 TkWindow *winPtr = (TkWindow *) tkwin;
6582 WmInfo *wmPtr;
6583 Window w, root, child;
6584 int rootX, rootY;
6585 unsigned int mask;
6586
6587 wmPtr = winPtr->wmInfoPtr;
6588
6589 w = wmPtr->vRoot;
6590 if (w == None) {
6591 w = RootWindow(winPtr->display, winPtr->screenNum);
6592 }
6593 if (XQueryPointer(winPtr->display, w, &root, &child, &rootX, &rootY,
6594 xPtr, yPtr, &mask) != True) {
6595 *xPtr = -1;
6596 *yPtr = -1;
6597 }
6598 }
6599
6600 /*
6601 *----------------------------------------------------------------------
6602 *
6603 * GetMaxSize --
6604 *
6605 * This function computes the current maxWidth and maxHeight values for a
6606 * window, taking into account the possibility that they may be
6607 * defaulted.
6608 *
6609 * Results:
6610 * The values at *maxWidthPtr and *maxHeightPtr are filled in with the
6611 * maximum allowable dimensions of wmPtr's window, in grid units. If no
6612 * maximum has been specified for the window, then this function computes
6613 * the largest sizes that will fit on the screen.
6614 *
6615 * Side effects:
6616 * None.
6617 *
6618 *----------------------------------------------------------------------
6619 */
6620
6621 static void
6622 GetMaxSize(
6623 WmInfo *wmPtr, /* Window manager information for the
6624 * window. */
6625 int *maxWidthPtr, /* Where to store the current maximum width of
6626 * the window. */
6627 int *maxHeightPtr) /* Where to store the current maximum height
6628 * of the window. */
6629 {
6630 int tmp;
6631
6632 if (wmPtr->maxWidth > 0) {
6633 *maxWidthPtr = wmPtr->maxWidth;
6634 } else {
6635 /*
6636 * Must compute a default width. Fill up the display, leaving a bit of
6637 * extra space for the window manager's borders.
6638 */
6639
6640 tmp = DisplayWidth(wmPtr->winPtr->display, wmPtr->winPtr->screenNum)
6641 - 15;
6642 if (wmPtr->gridWin != NULL) {
6643 /*
6644 * Gridding is turned on; convert from pixels to grid units.
6645 */
6646
6647 tmp = wmPtr->reqGridWidth
6648 + (tmp - wmPtr->winPtr->reqWidth)/wmPtr->widthInc;
6649 }
6650 *maxWidthPtr = tmp;
6651 }
6652 if (wmPtr->maxHeight > 0) {
6653 *maxHeightPtr = wmPtr->maxHeight;
6654 } else {
6655 tmp = DisplayHeight(wmPtr->winPtr->display, wmPtr->winPtr->screenNum)
6656 - 30;
6657 if (wmPtr->gridWin != NULL) {
6658 tmp = wmPtr->reqGridHeight
6659 + (tmp - wmPtr->winPtr->reqHeight)/wmPtr->heightInc;
6660 }
6661 *maxHeightPtr = tmp;
6662 }
6663 }
6664
6665 /*
6666 *----------------------------------------------------------------------
6667 *
6668 * TkpMakeMenuWindow --
6669 *
6670 * Configure the window to be either a pull-down (or pop-up) menu, or as
6671 * a toplevel (torn-off) menu or palette.
6672 *
6673 * Results:
6674 * None.
6675 *
6676 * Side effects:
6677 * Changes the style bit used to create a new Mac toplevel.
6678 *
6679 *----------------------------------------------------------------------
6680 */
6681
6682 void
6683 TkpMakeMenuWindow(
6684 Tk_Window tkwin, /* New window. */
6685 int transient) /* 1 means menu is only posted briefly as a
6686 * popup or pulldown or cascade. 0 means menu
6687 * is always visible, e.g. as a torn-off menu.
6688 * Determines whether save_under and
6689 * override_redirect should be set. */
6690 {
6691 WmInfo *wmPtr;
6692 XSetWindowAttributes atts;
6693 TkWindow *wrapperPtr;
6694
6695 if (!Tk_HasWrapper(tkwin)) {
6696 return;
6697 }
6698 wmPtr = ((TkWindow *) tkwin)->wmInfoPtr;
6699 if (wmPtr->wrapperPtr == NULL) {
6700 CreateWrapper(wmPtr);
6701 }
6702 wrapperPtr = wmPtr->wrapperPtr;
6703 if (transient) {
6704 atts.override_redirect = True;
6705 atts.save_under = True;
6706 } else {
6707 atts.override_redirect = False;
6708 atts.save_under = False;
6709 }
6710
6711 /*
6712 * The override-redirect and save-under bits must be set on the wrapper
6713 * window in order to have the desired effect. However, also set the
6714 * override-redirect bit on the window itself, so that the "wm
6715 * overrideredirect" command will see it.
6716 */
6717
6718 if ((atts.override_redirect!=Tk_Attributes(wrapperPtr)->override_redirect)
6719 || (atts.save_under != Tk_Attributes(wrapperPtr)->save_under)) {
6720 Tk_ChangeWindowAttributes((Tk_Window) wrapperPtr,
6721 CWOverrideRedirect|CWSaveUnder, &atts);
6722 }
6723 if ((atts.override_redirect != Tk_Attributes(tkwin)->override_redirect)
6724 || (atts.save_under != Tk_Attributes(tkwin)->save_under)) {
6725 Tk_ChangeWindowAttributes(tkwin,
6726 CWOverrideRedirect|CWSaveUnder, &atts);
6727 }
6728 }
6729
6730 /*
6731 *----------------------------------------------------------------------
6732 *
6733 * CreateWrapper --
6734 *
6735 * This function is invoked to create the wrapper window for a toplevel
6736 * window. It is called just before a toplevel is mapped for the first
6737 * time.
6738 *
6739 * Results:
6740 * None.
6741 *
6742 * Side effects:
6743 * The wrapper is created and the toplevel is reparented inside it.
6744 *
6745 *----------------------------------------------------------------------
6746 */
6747
6748 static void
6749 CreateWrapper(
6750 WmInfo *wmPtr) /* Window manager information for the
6751 * window. */
6752 {
6753 TkWindow *winPtr, *wrapperPtr;
6754 Window parent;
6755 Tcl_HashEntry *hPtr;
6756 int new;
6757
6758 winPtr = wmPtr->winPtr;
6759 if (winPtr->window == None) {
6760 Tk_MakeWindowExist((Tk_Window) winPtr);
6761 }
6762
6763 /*
6764 * The code below is copied from CreateTopLevelWindow, Tk_MakeWindowExist,
6765 * and TkpMakeWindow. The idea is to create an "official" Tk window (so
6766 * that we can get events on it), but to hide the window outside the
6767 * official Tk hierarchy so that it isn't visible to the application. See
6768 * the comments for the other functions if you have questions about this
6769 * code.
6770 */
6771
6772 wmPtr->wrapperPtr = wrapperPtr = TkAllocWindow(winPtr->dispPtr,
6773 Tk_ScreenNumber((Tk_Window) winPtr), winPtr);
6774 wrapperPtr->dirtyAtts |= CWBorderPixel;
6775
6776 /*
6777 * Tk doesn't normally select for StructureNotifyMask events because the
6778 * events are synthesized internally. However, for wrapper windows we need
6779 * to know when the window manager modifies the window configuration. We
6780 * also need to select on focus change events; these are the only windows
6781 * for which we care about focus changes.
6782 */
6783
6784 wrapperPtr->flags |= TK_WRAPPER;
6785 wrapperPtr->atts.event_mask |= StructureNotifyMask|FocusChangeMask;
6786 wrapperPtr->atts.override_redirect = winPtr->atts.override_redirect;
6787 if (winPtr->flags & TK_EMBEDDED) {
6788 parent = TkUnixContainerId(winPtr);
6789 } else {
6790 parent = XRootWindow(wrapperPtr->display, wrapperPtr->screenNum);
6791 }
6792 wrapperPtr->window = XCreateWindow(wrapperPtr->display,
6793 parent, wrapperPtr->changes.x, wrapperPtr->changes.y,
6794 (unsigned) wrapperPtr->changes.width,
6795 (unsigned) wrapperPtr->changes.height,
6796 (unsigned) wrapperPtr->changes.border_width, wrapperPtr->depth,
6797 InputOutput, wrapperPtr->visual,
6798 wrapperPtr->dirtyAtts|CWOverrideRedirect, &wrapperPtr->atts);
6799 hPtr = Tcl_CreateHashEntry(&wrapperPtr->dispPtr->winTable,
6800 (char *) wrapperPtr->window, &new);
6801 Tcl_SetHashValue(hPtr, wrapperPtr);
6802 wrapperPtr->mainPtr = winPtr->mainPtr;
6803 wrapperPtr->mainPtr->refCount++;
6804 wrapperPtr->dirtyAtts = 0;
6805 wrapperPtr->dirtyChanges = 0;
6806 wrapperPtr->wmInfoPtr = wmPtr;
6807
6808 /*
6809 * Reparent the toplevel window inside the wrapper.
6810 */
6811
6812 XReparentWindow(wrapperPtr->display, winPtr->window, wrapperPtr->window,
6813 0, 0);
6814
6815 /*
6816 * Tk must monitor structure events for wrapper windows in order to detect
6817 * changes made by window managers such as resizing, mapping, unmapping,
6818 * etc..
6819 */
6820
6821 Tk_CreateEventHandler((Tk_Window) wmPtr->wrapperPtr,
6822 WrapperEventMask, WrapperEventProc, (ClientData) wmPtr);
6823 }
6824
6825 /*
6826 *----------------------------------------------------------------------
6827 *
6828 * TkWmFocusToplevel --
6829 *
6830 * This is a utility function invoked by focus-management code. The focus
6831 * code responds to externally generated focus-related events on wrapper
6832 * windows but ignores those events for any other windows. This function
6833 * determines whether a given window is a wrapper window and, if so,
6834 * returns the toplevel window corresponding to the wrapper.
6835 *
6836 * Results:
6837 * If winPtr is a wrapper window, returns a pointer to the corresponding
6838 * toplevel window; otherwise returns NULL.
6839 *
6840 * Side effects:
6841 * None.
6842 *
6843 *----------------------------------------------------------------------
6844 */
6845
6846 TkWindow *
6847 TkWmFocusToplevel(
6848 TkWindow *winPtr) /* Window that received a focus-related
6849 * event. */
6850 {
6851 if (!(winPtr->flags & TK_WRAPPER)) {
6852 return NULL;
6853 }
6854 return winPtr->wmInfoPtr->winPtr;
6855 }
6856
6857 /*
6858 *----------------------------------------------------------------------
6859 *
6860 * TkUnixSetMenubar --
6861 *
6862 * This function is invoked by menu management code to specify the window
6863 * to use as a menubar for a given toplevel window.
6864 *
6865 * Results:
6866 * None.
6867 *
6868 * Side effects:
6869 * The window given by menubar will be mapped and positioned inside the
6870 * wrapper for tkwin and above tkwin. Menubar will automatically be
6871 * resized to maintain the height specified by TkUnixSetMenuHeight the
6872 * same width as tkwin. Any previous menubar specified for tkwin will be
6873 * unmapped and ignored from now on.
6874 *
6875 *----------------------------------------------------------------------
6876 */
6877
6878 void
6879 TkUnixSetMenubar(
6880 Tk_Window tkwin, /* Token for toplevel window. */
6881 Tk_Window menubar) /* Token for window that is to serve as
6882 * menubar for tkwin. Must not be a toplevel
6883 * window. If NULL, any existing menubar is
6884 * canceled and the menu height is reset to
6885 * 0. */
6886 {
6887 WmInfo *wmPtr = ((TkWindow *) tkwin)->wmInfoPtr;
6888 Tk_Window parent;
6889 TkWindow *menubarPtr = (TkWindow *) menubar;
6890
6891 if (wmPtr == NULL) {
6892 return;
6893 }
6894
6895 if (wmPtr->menubar != NULL) {
6896 /*
6897 * There's already a menubar for this toplevel. If it isn't the same
6898 * as the new menubar, unmap it so that it is out of the way, and
6899 * reparent it back to its original parent.
6900 */
6901
6902 if (wmPtr->menubar == menubar) {
6903 return;
6904 }
6905 ((TkWindow *) wmPtr->menubar)->wmInfoPtr = NULL;
6906 ((TkWindow *) wmPtr->menubar)->flags &= ~TK_REPARENTED;
6907 Tk_UnmapWindow(wmPtr->menubar);
6908 parent = Tk_Parent(wmPtr->menubar);
6909 if (parent != NULL) {
6910 Tk_MakeWindowExist(parent);
6911 XReparentWindow(Tk_Display(wmPtr->menubar),
6912 Tk_WindowId(wmPtr->menubar), Tk_WindowId(parent), 0, 0);
6913 }
6914 Tk_DeleteEventHandler(wmPtr->menubar, StructureNotifyMask,
6915 MenubarDestroyProc, (ClientData) wmPtr->menubar);
6916 Tk_ManageGeometry(wmPtr->menubar, NULL, NULL);
6917 }
6918
6919 wmPtr->menubar = menubar;
6920 if (menubar == NULL) {
6921 wmPtr->menuHeight = 0;
6922 } else {
6923 if ((menubarPtr->flags & TK_TOP_LEVEL)
6924 || (Tk_Screen(menubar) != Tk_Screen(tkwin))) {
6925 Tcl_Panic("TkUnixSetMenubar got bad menubar");
6926 }
6927 wmPtr->menuHeight = Tk_ReqHeight(menubar);
6928 if (wmPtr->menuHeight == 0) {
6929 wmPtr->menuHeight = 1;
6930 }
6931 Tk_MakeWindowExist(tkwin);
6932 Tk_MakeWindowExist(menubar);
6933 if (wmPtr->wrapperPtr == NULL) {
6934 CreateWrapper(wmPtr);
6935 }
6936 XReparentWindow(Tk_Display(menubar), Tk_WindowId(menubar),
6937 wmPtr->wrapperPtr->window, 0, 0);
6938 menubarPtr->wmInfoPtr = wmPtr;
6939 Tk_MoveResizeWindow(menubar, 0, 0, Tk_Width(tkwin), wmPtr->menuHeight);
6940 Tk_MapWindow(menubar);
6941 Tk_CreateEventHandler(menubar, StructureNotifyMask, MenubarDestroyProc,
6942 (ClientData) menubar);
6943 Tk_ManageGeometry(menubar, &menubarMgrType, (ClientData) wmPtr);
6944 menubarPtr->flags |= TK_REPARENTED;
6945 }
6946 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
6947 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
6948 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) tkwin);
6949 wmPtr->flags |= WM_UPDATE_PENDING;
6950 }
6951 }
6952
6953 /*
6954 *----------------------------------------------------------------------
6955 *
6956 * MenubarDestroyProc --
6957 *
6958 * This function is invoked by the event dispatcher whenever a menubar
6959 * window is destroyed (it's also invoked for a few other kinds of
6960 * events, but we ignore those).
6961 *
6962 * Results:
6963 * None.
6964 *
6965 * Side effects:
6966 * The association between the window and its toplevel is broken, so that
6967 * the window is no longer considered to be a menubar.
6968 *
6969 *----------------------------------------------------------------------
6970 */
6971
6972 static void
6973 MenubarDestroyProc(
6974 ClientData clientData, /* TkWindow pointer for menubar. */
6975 XEvent *eventPtr) /* Describes what just happened. */
6976 {
6977 WmInfo *wmPtr;
6978
6979 if (eventPtr->type != DestroyNotify) {
6980 return;
6981 }
6982 wmPtr = ((TkWindow *) clientData)->wmInfoPtr;
6983 wmPtr->menubar = NULL;
6984 wmPtr->menuHeight = 0;
6985 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
6986 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
6987 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) wmPtr->winPtr);
6988 wmPtr->flags |= WM_UPDATE_PENDING;
6989 }
6990 }
6991
6992 /*
6993 *----------------------------------------------------------------------
6994 *
6995 * MenubarReqProc --
6996 *
6997 * This function is invoked by the Tk geometry management code whenever a
6998 * menubar calls Tk_GeometryRequest to request a new size.
6999 *
7000 * Results:
7001 * None.
7002 *
7003 * Side effects:
7004 * None.
7005 *
7006 *----------------------------------------------------------------------
7007 */
7008
7009 static void
7010 MenubarReqProc(
7011 ClientData clientData, /* Pointer to the window manager information
7012 * for tkwin's toplevel. */
7013 Tk_Window tkwin) /* Handle for menubar window. */
7014 {
7015 WmInfo *wmPtr = (WmInfo *) clientData;
7016
7017 wmPtr->menuHeight = Tk_ReqHeight(tkwin);
7018 if (wmPtr->menuHeight <= 0) {
7019 wmPtr->menuHeight = 1;
7020 }
7021 wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
7022 if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
7023 Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) wmPtr->winPtr);
7024 wmPtr->flags |= WM_UPDATE_PENDING;
7025 }
7026 }
7027
7028 /*
7029 *----------------------------------------------------------------------
7030 *
7031 * TkpGetWrapperWindow --
7032 *
7033 * Given a toplevel window return the hidden wrapper window for the
7034 * toplevel window if available.
7035 *
7036 * Results:
7037 * The wrapper window. NULL is we were not passed a toplevel window or
7038 * the wrapper has yet to be created.
7039 *
7040 * Side effects:
7041 * None.
7042 *
7043 *----------------------------------------------------------------------
7044 */
7045
7046 TkWindow *
7047 TkpGetWrapperWindow(
7048 TkWindow *winPtr) /* A toplevel window pointer. */
7049 {
7050 register WmInfo *wmPtr = winPtr->wmInfoPtr;
7051
7052 if ((winPtr == NULL) || (wmPtr == NULL)) {
7053 return NULL;
7054 }
7055
7056 return wmPtr->wrapperPtr;
7057 }
7058
7059 /* Support Procedures for release and capture */
7060 /*
7061 *----------------------------------------------------------------------
7062 *
7063 * UnmanageGeometry --
7064 *
7065 * Since there is a bug in tkGeometry.c, we need this routine to
7066 * replace Tk_ManageGeometry(tkwin, NULL, NULL);
7067 *
7068 * Results:
7069 * None.
7070 *
7071 * Side effects:
7072 * The window given by the clientData argument is mapped.
7073 *
7074 *----------------------------------------------------------------------
7075 */
7076 static void UnmanageGeometry(tkwin)
7077 Tk_Window tkwin; /* Window whose geometry is to
7078 * be unmanaged.*/
7079 {
7080 register TkWindow *winPtr = (TkWindow *) tkwin;
7081
7082 if ((winPtr->geomMgrPtr != NULL) &&
7083 (winPtr->geomMgrPtr->lostSlaveProc != NULL)) {
7084 (*winPtr->geomMgrPtr->lostSlaveProc)(winPtr->geomData, tkwin);
7085 }
7086
7087 winPtr->geomMgrPtr = NULL;
7088 winPtr->geomData = NULL;
7089 }
7090
7091 /*
7092 *----------------------------------------------------------------------
7093 *
7094 * TopLevelEventProc --
7095 *
7096 * This procedure is invoked when a top-level (or other externally-
7097 * managed window) is restructured in any way.
7098 *
7099 * Results:
7100 * None.
7101 *
7102 * Side effects:
7103 * Tk's internal data structures for the window get modified to
7104 * reflect the structural change.
7105 *
7106 *----------------------------------------------------------------------
7107 */
7108
7109 static void
7110 TopLevelEventProc(clientData, eventPtr)
7111 ClientData clientData; /* Window for which event occurred. */
7112 XEvent *eventPtr; /* Event that just happened. */
7113 {
7114 register TkWindow *winPtr = (TkWindow *) clientData;
7115 WmInfo *wmPtr = winPtr->wmInfoPtr;
7116
7117 wmPtr->flags |= WM_VROOT_OFFSET_STALE;
7118 if (eventPtr->type == DestroyNotify) {
7119 Tk_ErrorHandler handler;
7120
7121 if (!(winPtr->flags & TK_ALREADY_DEAD)) {
7122 /*
7123 * A top-level window was deleted externally (e.g., by the window
7124 * manager). This is probably not a good thing, but cleanup as
7125 * best we can. The error handler is needed because
7126 * Tk_DestroyWindow will try to destroy the window, but of course
7127 * it's already gone.
7128 */
7129
7130 handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
7131 (Tk_ErrorProc *) NULL, (ClientData) NULL);
7132 Tk_DestroyWindow((Tk_Window) winPtr);
7133 Tk_DeleteErrorHandler(handler);
7134 }
7135 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
7136 printf("TopLevelEventProc: %s deleted\n", winPtr->pathName);
7137 }
7138 } else if (eventPtr->type == ConfigureNotify) {
7139 /*
7140 * Ignore the event if the window has never been mapped yet.
7141 * Such an event occurs only in weird cases like changing the
7142 * internal border width of a top-level window, which results
7143 * in a synthetic Configure event. These events are not relevant
7144 * to us, and if we process them confusion may result (e.g. we
7145 * may conclude erroneously that the user repositioned or resized
7146 * the window).
7147 */
7148
7149 if (!(winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED)) {
7150 ConfigureEvent(wmPtr, &eventPtr->xconfigure);
7151 }
7152 } else if (eventPtr->type == MapNotify) {
7153 winPtr->flags |= TK_MAPPED;
7154 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
7155 printf("TopLevelEventProc: %s mapped\n", winPtr->pathName);
7156 }
7157 } else if (eventPtr->type == UnmapNotify) {
7158 winPtr->flags &= ~TK_MAPPED;
7159 if (winPtr->dispPtr->flags & TK_DISPLAY_WM_TRACING) {
7160 printf("TopLevelEventProc: %s unmapped\n", winPtr->pathName);
7161 }
7162 } else if (eventPtr->type == ReparentNotify) {
7163 ReparentEvent(wmPtr, &eventPtr->xreparent);
7164 }
7165 }
7166
7167 void
7168 ImageChangedProc(clientData, x, y, width, height, imageWidth, imageHeight)
7169 ClientData clientData;
7170 int x;
7171 int y;
7172 int width;
7173 int height;
7174 int imageWidth;
7175 int imageHeight;
7176 {
7177 register TkWindow *winPtr = (TkWindow *) clientData;
7178 WmInfo *wmPtr = winPtr->wmInfoPtr;
7179 Pixmap old = wmPtr->hints.icon_pixmap;
7180 Pixmap pixmap = None;
7181 if (imageWidth && imageHeight) {
7182 pixmap = Tk_GetPixmap(winPtr->display,
7183 RootWindowOfScreen(Tk_Screen((Tk_Window)winPtr)),
7184 imageWidth, imageHeight,
7185 DefaultDepthOfScreen(Tk_Screen((Tk_Window)winPtr)));
7186 }
7187 if (pixmap != None) {
7188 Tk_RedrawImage(wmPtr->iconImage, 0, 0, imageWidth, imageHeight, pixmap, 0, 0);
7189 wmPtr->hints.flags |= IconPixmapHint;
7190 } else {
7191 wmPtr->hints.flags &= ~IconPixmapHint;
7192 }
7193 wmPtr->hints.icon_pixmap = pixmap;
7194 UpdateHints(winPtr);
7195 if (old != None) {
7196 Tk_FreePixmap(winPtr->display, old);
7197 }
7198 }
7199
7200 /*
7201 *----------------------------------------------------------------------
7202 *
7203 * UpdateCommand --
7204 *
7205 * Update the WM_COMMAND property, taking care to translate the command
7206 * strings into the external encoding.
7207 *
7208 * Results:
7209 * None.
7210 *
7211 * Side effects:
7212 * None.
7213 *
7214 *----------------------------------------------------------------------
7215 */
7216
7217 static void
7218 UpdateCommand(
7219 TkWindow *winPtr)
7220 {
7221 register WmInfo *wmPtr = winPtr->wmInfoPtr;
7222 Tcl_DString cmds, ds;
7223 int i, *offsets;
7224 char **cmdArgv;
7225 int cmdArgc;
7226
7227 /*
7228 * Translate the argv strings into the external encoding. To avoid
7229 * allocating lots of memory, the strings are appended to a buffer with
7230 * nulls between each string.
7231 *
7232 * This code is tricky because we need to pass and array of pointers to
7233 * XSetCommand. However, we can't compute the pointers as we go because
7234 * the DString buffer space could get reallocated. So, store offsets for
7235 * each element as we go, then compute pointers from the offsets once the
7236 * entire DString is done.
7237 */
7238 #ifdef _LANG
7239 Tcl_Obj **cmdObjv;
7240 if (Tcl_ListObjGetElements(NULL, wmPtr->cmdArg, &cmdArgc, &cmdObjv) != TCL_OK) {
7241 return;
7242 }
7243 #else
7244 cmdArgc = wmPtr->cmdArgc;
7245 #endif
7246 cmdArgv = (char **) ckalloc(sizeof(char *) * cmdArgc);
7247 offsets = (int *) ckalloc( sizeof(int) * cmdArgc);
7248 Tcl_DStringInit(&cmds);
7249 for (i = 0; i < cmdArgc; i++) {
7250 Tcl_UtfToExternalDString(NULL,
7251 #ifdef _LANG
7252 Tcl_GetString(cmdObjv[i]),
7253 #else
7254 wmPtr->cmdArgv[i],
7255 #endif
7256 -1, &ds);
7257 offsets[i] = Tcl_DStringLength(&cmds);
7258 Tcl_DStringAppend(&cmds, Tcl_DStringValue(&ds),
7259 Tcl_DStringLength(&ds)+1);
7260 Tcl_DStringFree(&ds);
7261 }
7262 cmdArgv[0] = Tcl_DStringValue(&cmds);
7263 for (i = 1; i < cmdArgc; i++) {
7264 cmdArgv[i] = cmdArgv[0] + offsets[i];
7265 }
7266
7267 XSetCommand(winPtr->display, wmPtr->wrapperPtr->window,
7268 cmdArgv, cmdArgc);
7269 Tcl_DStringFree(&cmds);
7270 ckfree((char *) cmdArgv);
7271 ckfree((char *) offsets);
7272 }
7273
7274 /*
7275 *----------------------------------------------------------------------
7276 *
7277 * TkpWmSetState --
7278 *
7279 * Sets the window manager state for the wrapper window of a given
7280 * toplevel window.
7281 *
7282 * Results:
7283 * 0 on error, 1 otherwise
7284 *
7285 * Side effects:
7286 * May minimize, restore, or withdraw a window.
7287 *
7288 *----------------------------------------------------------------------
7289 */
7290
7291 int
7292 TkpWmSetState(
7293 TkWindow *winPtr, /* Toplevel window to operate on. */
7294 int state) /* One of IconicState, NormalState, or
7295 * WithdrawnState. */
7296 {
7297 WmInfo *wmPtr = winPtr->wmInfoPtr;
7298
7299 if (state == WithdrawnState) {
7300 wmPtr->hints.initial_state = WithdrawnState;
7301 wmPtr->withdrawn = 1;
7302 if (wmPtr->flags & WM_NEVER_MAPPED) {
7303 return 1;
7304 }
7305 if (XWithdrawWindow(winPtr->display, wmPtr->wrapperPtr->window,
7306 winPtr->screenNum) == 0) {
7307 return 0;
7308 }
7309 WaitForMapNotify(winPtr, 0);
7310 } else if (state == NormalState) {
7311 wmPtr->hints.initial_state = NormalState;
7312 wmPtr->withdrawn = 0;
7313 if (wmPtr->flags & WM_NEVER_MAPPED) {
7314 return 1;
7315 }
7316 UpdateHints(winPtr);
7317 Tk_MapWindow((Tk_Window) winPtr);
7318 } else if (state == IconicState) {
7319 wmPtr->hints.initial_state = IconicState;
7320 if (wmPtr->flags & WM_NEVER_MAPPED) {
7321 return 1;
7322 }
7323 if (wmPtr->withdrawn) {
7324 UpdateHints(winPtr);
7325 Tk_MapWindow((Tk_Window) winPtr);
7326 wmPtr->withdrawn = 0;
7327 } else {
7328 if (XIconifyWindow(winPtr->display, wmPtr->wrapperPtr->window,
7329 winPtr->screenNum) == 0) {
7330 return 0;
7331 }
7332 WaitForMapNotify(winPtr, 0);
7333 }
7334 }
7335
7336 return 1;
7337 }
7338
7339 /*
7340 *----------------------------------------------------------------------
7341 *
7342 * WmIconimageCmd --
7343 *
7344 * This procedure is invoked to process the "wm iconimage" Tcl command.
7345 * See the user documentation for details on what it does.
7346 *
7347 * Results:
7348 * A standard Tcl result.
7349 *
7350 * Side effects:
7351 * See the user documentation.
7352 *
7353 *----------------------------------------------------------------------
7354 */
7355
7356 static int
7357 WmIconimageCmd(tkwin, winPtr, interp, objc, objv)
7358 Tk_Window tkwin; /* Main window of the application. */
7359 TkWindow *winPtr; /* Toplevel to work with */
7360 Tcl_Interp *interp; /* Current interpreter. */
7361 int objc; /* Number of arguments. */
7362 Tcl_Obj *CONST objv[]; /* Argument objects. */
7363 {
7364 register WmInfo *wmPtr = winPtr->wmInfoPtr;
7365 Pixmap pixmap;
7366 char *argv3;
7367
7368 if ((objc != 3) && (objc != 4)) {
7369 Tcl_WrongNumArgs(interp, 2, objv, "window ?image?");
7370 return TCL_ERROR;
7371 }
7372 if (objc == 3) {
7373 if (wmPtr->hints.flags & IconPixmapHint && wmPtr->iconImage) {
7374 Tcl_SetResult(interp, (char *) Tk_NameOfBitmap(winPtr->display, wmPtr->hints.icon_pixmap),TCL_STATIC);
7375 }
7376 return TCL_OK;
7377 }
7378 /* clear any existing pixmap hints and free associated resources */
7379 if (wmPtr->hints.icon_pixmap != None) {
7380 if (wmPtr->iconImage) {
7381 Tk_FreePixmap(winPtr->display, wmPtr->hints.icon_pixmap);
7382 } else {
7383 Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
7384 }
7385 wmPtr->hints.icon_pixmap = None;
7386 }
7387 if (wmPtr->iconImage) {
7388 Tk_FreeImage(wmPtr->iconImage);
7389 wmPtr->iconImage = NULL;
7390 }
7391 wmPtr->hints.flags &= ~IconPixmapHint;
7392 wmPtr->iconImage = Tk_GetImage(interp, tkwin, Tcl_GetString(objv[3]),
7393 ImageChangedProc, (ClientData) winPtr);
7394 if (wmPtr->iconImage != NULL) {
7395 int width = 0;
7396 int height = 0;
7397 Tk_SizeOfImage(wmPtr->iconImage, &width, &height);
7398 if (!width || !height) {
7399 LangDebug("Size of %s is %d,%d\n",Tcl_GetString(objv[3]),width,height);
7400 }
7401 ImageChangedProc((ClientData) winPtr, 0, 0, width, height, width, height);
7402 return TCL_OK;
7403 } else {
7404 UpdateHints(winPtr);
7405 return TCL_ERROR;
7406 }
7407 }
7408
7409 static int
7410 WmCaptureCmd(tkwin, winPtr, interp, objc, objv)
7411 Tk_Window tkwin; /* Main window of the application. */
7412 TkWindow *winPtr; /* Toplevel to work with */
7413 Tcl_Interp *interp; /* Current interpreter. */
7414 int objc; /* Number of arguments. */
7415 Tcl_Obj *CONST objv[]; /* Argument objects. */
7416 {
7417 register WmInfo *wmPtr = winPtr->wmInfoPtr;
7418
7419 if (winPtr->parentPtr == NULL) {
7420 Tcl_AppendResult(interp, "Cannot capture main window", NULL);
7421 return TCL_ERROR;
7422 }
7423
7424
7425 if ((winPtr->flags & TK_TOP_LEVEL) == 0) {
7426 /* Window is already captured */
7427 return TCL_OK;
7428 }
7429 /* Withdraw the window */
7430 wmPtr->hints.initial_state = WithdrawnState;
7431 wmPtr->withdrawn = 1;
7432 if (wmPtr->flags & WM_NEVER_MAPPED) {
7433 /* Now handle all idletasks so that the initial
7434 * idle map is certain to have happened
7435 */
7436 while (Tcl_DoOneEvent(TCL_IDLE_EVENTS)) {
7437 /* Empty loop body */
7438 }
7439 }
7440 else {
7441 if (XWithdrawWindow(winPtr->display, wmPtr->wrapperPtr->window,
7442 winPtr->screenNum) != 0) {
7443 WaitForMapNotify(winPtr, 0);
7444 }
7445 }
7446
7447 /* Dis-associate from wm - do this later ? */
7448 TkWmDeadWindow(winPtr);
7449
7450 if (winPtr->window == None) {
7451 /* cause this and parent window to exist */
7452 winPtr->atts.event_mask &= ~StructureNotifyMask;
7453 winPtr->flags &= ~TK_TOP_LEVEL;
7454
7455 UnmanageGeometry((Tk_Window) winPtr);
7456 Tk_DeleteEventHandler((Tk_Window) winPtr, StructureNotifyMask,
7457 TopLevelEventProc, (ClientData) winPtr);
7458 }
7459 else {
7460 XEvent event;
7461 unsigned long serial;
7462 XSetWindowAttributes atts;
7463 int i, done1 = 0, done2 = 0, count = 0;
7464 TkDisplay *dispPtr = winPtr->dispPtr;
7465
7466 /* wmDontReparent is set to 2 if it is determined that
7467 * the window manager does not do a reparent after
7468 * "wm capture" does the reparent. If that's the case, we don't
7469 * need to perform the hack
7470 */
7471 static int wmDontReparent = 0;
7472
7473 /* Hack begins here --
7474 *
7475 * To change a widget from a toplevel window to a non-toplevel
7476 * window, we reparent it (from the root window) to its
7477 * real (TK) parent. However, after we do that, some window
7478 * managers (mwm in particular), will reparent the widget, again,
7479 * to its decoration frames. In that case, we need to perform the
7480 * reparenting again.
7481 *
7482 * The following code keeps reparenting the widget to its
7483 * real parent until it detects the stupid move by the window
7484 * manager. After that, it reparents once more and the widget
7485 * will be finally reparented to its real parent.
7486 */
7487 while (done2 == 0) {
7488 XUnmapWindow(winPtr->display, winPtr->window);
7489 Tk_MakeWindowExist((Tk_Window) winPtr->parentPtr);
7490 XReparentWindow(winPtr->display, winPtr->window,
7491 winPtr->parentPtr->window, 0, 0);
7492 if (wmDontReparent >= 2) {
7493 goto done;
7494 }
7495
7496 do {
7497 /* FIXME - we are waiting for events on wmPTR
7498 here 'cos that is what WaitForEvent wants we wanted
7499 winPtr->window
7500 */
7501 if (WaitForEvent(winPtr->display, wmPtr,
7502 StructureNotifyMask, &event) != TCL_OK) {
7503 goto done;
7504 }
7505 Tk_HandleEvent(&event);
7506 } while (event.type != ReparentNotify);
7507
7508 if (event.xreparent.parent == winPtr->parentPtr->window) {
7509 if (done1 == 1) {
7510 done2 = 1;
7511 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
7512 printf("tixdebug: done reparenting.\n");
7513 }
7514 }
7515 else {
7516 ++count;
7517 }
7518 }
7519 else {
7520 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
7521 printf("tixdebug: wm reparenting, retry ...\n");
7522 }
7523 done1 = 1;
7524 }
7525 if (count > 15) {
7526 ++wmDontReparent;
7527 if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
7528 printf("tixdebug: window manager doesn't reparent.\n");
7529 }
7530 goto done;
7531 }
7532 }
7533 /* Hack ends here
7534 */
7535
7536 done:
7537 /* clear those attributes that non-toplevel windows don't
7538 * possess
7539 */
7540 winPtr->flags &= ~TK_TOP_LEVEL;
7541 atts.event_mask = winPtr->atts.event_mask;
7542 atts.event_mask &= ~StructureNotifyMask;
7543 Tk_ChangeWindowAttributes((Tk_Window) winPtr, CWEventMask, &atts);
7544
7545 UnmanageGeometry((Tk_Window) winPtr);
7546 }
7547 return TCL_OK;
7548 }
7549
7550 static int
7551 WmReleaseCmd(tkwin, winPtr, interp, objc, objv)
7552 Tk_Window tkwin; /* Main window of the application. */
7553 TkWindow *winPtr; /* Toplevel to work with */
7554 Tcl_Interp *interp; /* Current interpreter. */
7555 int objc; /* Number of arguments. */
7556 Tcl_Obj *CONST objv[]; /* Argument objects. */
7557 {
7558
7559 register WmInfo *wmPtr = winPtr->wmInfoPtr;
7560
7561 if (Tk_IsTopLevel(winPtr)) {
7562 Tcl_AppendResult(interp, "Already a toplevel window", NULL);
7563 return TCL_ERROR;
7564 }
7565
7566 /* detach the window from its gemoetry manager, if any */
7567 UnmanageGeometry((Tk_Window) winPtr);
7568
7569 if (winPtr->window == None) {
7570 /* Good, the window is not created yet, we still have time
7571 * to make it an legitimate toplevel window
7572 */
7573 winPtr->dirtyAtts |= CWBorderPixel;
7574 }
7575 else {
7576 Window parent;
7577
7578 if (winPtr->flags & TK_MAPPED) {
7579 Tk_UnmapWindow((Tk_Window) winPtr);
7580 }
7581 parent = XRootWindow(winPtr->display, winPtr->screenNum);
7582 XReparentWindow(winPtr->display, winPtr->window, parent, 0, 0);
7583
7584 /* Should flush the events here */
7585 }
7586
7587 winPtr->flags |= TK_TOP_LEVEL;
7588 TkWmNewWindow(winPtr);
7589
7590 wmPtr = winPtr->wmInfoPtr;
7591 wmPtr->hints.initial_state = WithdrawnState;
7592 wmPtr->withdrawn = 1;
7593
7594 /* Size was set - force a call to Geometry Manager */
7595 winPtr->reqWidth++;
7596 winPtr->reqHeight++;
7597 Tk_GeometryRequest((Tk_Window) winPtr, winPtr->reqWidth - 1,
7598 winPtr->reqHeight - 1);
7599
7600 return TCL_OK;
7601 }
7602
7603 static int
7604 WmWrapperCmd(tkwin, winPtr, interp, objc, objv)
7605 Tk_Window tkwin; /* Main window of the application. */
7606 TkWindow *winPtr; /* Toplevel to work with */
7607 Tcl_Interp *interp; /* Current interpreter. */
7608 int objc; /* Number of arguments. */
7609 Tcl_Obj *CONST objv[]; /* Argument objects. */
7610 {
7611 register WmInfo *wmPtr = winPtr->wmInfoPtr;
7612
7613 if (wmPtr->wrapperPtr == NULL) {
7614 CreateWrapper(wmPtr);
7615 }
7616 Tcl_IntResults(interp,2,0,wmPtr->wrapperPtr->window,wmPtr->menuHeight);
7617 return TCL_OK;
7618 }
7619
7620
7621
7622
7623