| [ Team LiB ] |
|
The wm CommandThe wm command has about 20 operations that interact with the window manager. The general form of the command is: wm operation win ?args? In all cases the win argument must be a toplevel. Otherwise, an error is raised. In many cases, the operation either sets or queries a value. If a new value is not specified, then the current settings are returned. For example, this command returns the current window geometry:
wm geometry .
=> 300x200+327+20
This command defines a new geometry: wm geometry . 400x200+0+0 There are lots of wm operations, and this reflects the complex protocol with UNIX window managers. The summary below lists the subset of operations that I find useful. The operations can be grouped into four main categories:
Toplevel Size, Placement, and DecorationEach window has a title that appears in the title bar that the window manager places above the window. In a wish script, the default title of the main window is the last component of the file name of the script. Use the wm title command to change the title of the window. The title can also appear in the icon for your window, unless you specify another name with wm iconname. wm title . "My Application" Use the wm geometry command to adjust the position or size of your main windows. A geometry specification has the general form WxH+X+Y, where W is the width, H is the height, and X and Y specify the location of the upper-left corner of the window. The location +0+0 is the upper-left corner of the display. You can specify a negative X or Y to position the bottom (right) side of the window relative to the bottom (right) side of the display. For example, +0-0 is the lower-left corner, and -100-100 is offset from the lower-right corner by 100 pixels in the X and Y direction. If you do not specify a geometry, then the current geometry is returned. Example 44-1 Gridded geometry for a canvascanvas .c -width 300 -height 150 pack .c -fill both -expand true wm geometry . => 300x200+678+477 wm grid . 30 15 10 10 wm geometry . => 30x20+678+477 Example 44-1 sets up gridded geometry for a canvas, which means that the geometry is in terms of some unit other than pixels. With the canvas, use the wm grid command to define the size of the grid. The text and listbox widgets set a grid based on the size of the characters they display. They have a setgrid attribute that turns on gridding, which is described on page 642. The wm resizable command controls whether a user can resize a window. The following command allows a resize in the X direction, but not in the Y direction: wm resizable . 1 0 You can constrain the minimum size, maximum size, and the aspect ratio of a toplevel. The aspect ratio is the width divided by the height. The constraint is applied when the user resizes the window interactively. The minsize, maxsize, and aspect operations apply these constraints. Some window managers insist on having the user position windows. The sizefrom and positionfrom operations let you pretend that the user specified the size and position in order to work around this restriction. Table 44-1 summarizes the wm commands that deal with size, decorations, placement:
The stackorder operation, introduced in Tk 8.4, returns information about the stacking order of the application's toplevel windows. Given the name of a toplevel, it returns a list of toplevel children windows in stacking order, from lowest to highest. Only those toplevels that are currently mapped to the screen are returned. The following command returns all mapped toplevels in their stacking order: wm stackorder . The stackorder operation can also be used to determine if one toplevel is positioned above or below a second toplevel. When two window arguments separated by either isabove or isbelow are passed, a boolean result indicates whether or not the first window is currently above or below the second window in the stacking order. For example: wm stackorder . isabove .dialog IconsUNIX window managers let you close a window and replace it with an icon. The window still exists in your application, and users can open the window later. You can open and close a window yourself with the deiconify and iconify operations, respectively. Use the withdraw operation to unmap the window without replacing it with an icon. The state operation returns the current state, which is one of normal, iconified, or withdrawn. If you withdraw a window, you can restore it to the normal state with deiconify. Windows and Macintosh do not implement icons for program windows. Instead, icons represent files and applications in the desktop environment. When you iconify under Windows, the window gets minimized and users can open it by clicking on the taskbar at the bottom of the screen. When you iconify under Macintosh, the window simply gets withdrawn from the screen. As of Tk 8.3, Windows applications have an additional state, zoomed, which is a full-screen (or "maximized") display mode. Future versions of Tk may support this state for other operating systems You can set the attributes of UNIX icons with the iconname, iconposition, iconbitmap, and iconmask operations. The icon's mask is used to get irregularly shaped icons. Chapter 41 describes how masks and bitmaps are defined. In the case of an icon, it is most likely that you have the definition in a file, so your command will look like this: wm iconbitmap . @myfilename Starting with Tk 8.3.3, on Windows systems, you can provide the path of a valid Windows icon file (usually .ico or .icr files) when setting the window's icon with the wm iconbitmap command. And if you use the optional -default option, introduced in Tk 8.4, the specified bitmap is used as the default icon for all windows. However, when setting the icon bitmap under windows, remember that the argument you provide must be a filename without a leading "@". For example: wm iconbitmap . -default [file join $lib myapp.ico] Table 44-2 summarizes the wm operations that have to do with icons:
Application Session StateThe window manager lets users delete windows with a close operation. When the main Tk window gets deleted, wish normally quits. If you have any special processing that must take place when the user deletes a window, you need to intercept the close action. Use the wm protocol operation to register a command that handles the WM_DELETE_WINDOW message from the window manager. This works on all platforms even though "delete" is a UNIX term and "close" is the Windows and Macintosh term: wm protocol . WM_DELETE_WINDOW Quit If you intercept close on the main Tk window (i.e., dot), you must eventually call exit to actually stop your application. However, you can also take the time to prompt the user about unsaved changes, or even let the user change their mind about quitting. Other window manager messages that you can intercept are WM_SAVE_YOURSELF and WM_TAKE_FOCUS. The first is called periodically by some UNIX session managers, which are described below. The latter is used in the active focus model. Tk (and this book) assumes a passive focus model where the window manager assigns focus to a top-level window. Some UNIX window managers support the notion of a session that lasts between runs of the window system. A session is implemented by saving state about the applications that are running, and using this information to restart the applications when the window system is restarted. An easy way to participate in the session protocol is to save the command used to start your application. The wm command operation does this. The wish shell saves this information, so it is just a matter of registering it with the window manager. argv0 is the command, and argv is the command-line arguments: wm command . [linsert $argv 0 $argv0] If your application is typically run on a different host than the one with the display (like in an Xterminal environment), then you also need to record what host to run the application on. Use the wm client operation for this. You might need to use hostname instead of uname on your system: wm client . [exec uname -n] Table 44-3 describes the session-related window manager operations.
Miscellaneous Window Manager OperationsThe UNIX window managers work by reparenting an application's window so that it is a child of the window that forms the border and decorative title bar. The wm frame operation returns the window ID of the new parent, or the ID of the window itself if it has not been reparented. The wm overrideredirect operation can set a bit that overrides the reparenting. This means that no title or border will be drawn around the window, and you cannot control the window through the window manager. The wm group operation defines groups of windows so that the window manager can open and close them together. One window, typically the main window, is chosen as the leader. The other members of the group are iconified when it is iconified. This is not implemented on Windows and Macintosh, and not all UNIX window managers implement this, either. The wm transient operation informs the window manager that this is a temporary window and there is no need to decorate it with the border and decorative title bar. This is used, for example, on pop-up menus. On Windows, a transient window is a toolbar window that does not appear in the task bar. On Macintosh, the tk::unsupported::MacWindowStyle command, which is described on page 489, lets you create different styles of top-level windows. The wm attributes command, added in Tk 8.4, allows you to set or query platform-specific attributes associated with a specific window. The following Windows attributes are supported: -disabled gets or sets whether the window is in a disabled state; -toolwindow gets or sets the style of the window to toolwindow (as defined in the MSDN); and -topmost gets or sets whether this window is displayed above all other windows. Table 44-4 lists the remaining window manager operations:
|
| [ Team LiB ] |
|