Debian Codegraph @

pTk/mTk/win/tkWinWm.c

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