Change to the linux kernel coding style
[wmaker-crm.git] / src / window.c
Commit [+]AuthorDateLineData
e7495baf dan1999-02-17 11:06:40 +00001/* window.c - client window managing stuffs
9aca0d5f dan2004-10-12 01:34:32 +00002 *
9af1c6c4 dan1998-10-21 14:43:47 +00003 * Window Maker window manager
9aca0d5f dan2004-10-12 01:34:32 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
9aca0d5f dan2004-10-12 01:34:32 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
9aca0d5f dan2004-10-12 01:34:32 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
23#include "wconfig.h"
24
25#include <X11/Xlib.h>
26#include <X11/Xutil.h>
27#ifdef SHAPE
28#include <X11/extensions/shape.h>
29#endif
9aca0d5f dan2004-10-12 01:34:32 +000030#ifdef KEEP_XKB_LOCK_STATUS
31#include <X11/XKBlib.h>
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020032#endif /* KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +000033#include <stdlib.h>
34#include <stdio.h>
35#include <string.h>
06f59b99 Carlos R. Mafra2008-11-06 01:37:44 +010036#include <stdint.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000037
75f79016 dan2001-10-09 03:35:45 +000038/* For getting mouse wheel mappings from WINGs */
39#include <WINGs/WINGsP.h>
59eb9475 dan1998-11-03 12:53:26 +000040
9d2e6ef9 scottc1998-09-29 22:36:29 +000041#include "WindowMaker.h"
42#include "GNUstep.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000043#include "wcore.h"
44#include "framewin.h"
45#include "texture.h"
46#include "window.h"
47#include "winspector.h"
48#include "icon.h"
49#include "properties.h"
50#include "actions.h"
51#include "client.h"
52#include "funcs.h"
53#include "keybind.h"
54#include "stacking.h"
55#include "defaults.h"
56#include "workspace.h"
a10214a5 kojima2002-11-28 22:04:07 +000057#include "xinerama.h"
3c046182 kojima1999-09-16 02:58:56 +000058
0261c326 dan1999-01-06 15:22:33 +000059#ifdef MWM_HINTS
60# include "motif.h"
61#endif
9aca0d5f dan2004-10-12 01:34:32 +000062#ifdef NETWM_HINTS
63# include "wmspec.h"
64#endif
0261c326 dan1999-01-06 15:22:33 +000065
9d2e6ef9 scottc1998-09-29 22:36:29 +000066/****** Global Variables ******/
67
68extern WShortKey wKeyBindings[WKBD_LAST];
69
9af1c6c4 dan1998-10-21 14:43:47 +000070#ifdef SHAPE
71extern Bool wShapeSupported;
72#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000073
74/* contexts */
75extern XContext wWinContext;
76
77/* cursors */
78extern Cursor wCursor[WCUR_LAST];
79
80/* protocol atoms */
81extern Atom _XA_WM_DELETE_WINDOW;
9af1c6c4 dan1998-10-21 14:43:47 +000082extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
9d2e6ef9 scottc1998-09-29 22:36:29 +000083
84extern Atom _XA_WINDOWMAKER_STATE;
85
86extern WPreferences wPreferences;
87
88#define MOD_MASK wPreferences.modifier_mask
89
90extern Time LastTimestamp;
91
92/* superfluous... */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020093extern void DoWindowBirth(WWindow *);
5ef342d9 kojima2001-09-06 21:42:28 +000094
9d2e6ef9 scottc1998-09-29 22:36:29 +000095/***** Local Stuff *****/
96
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097static WWindowState *windowState = NULL;
9af1c6c4 dan1998-10-21 14:43:47 +000098
9d2e6ef9 scottc1998-09-29 22:36:29 +000099/* local functions */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200100static FocusMode getFocusMode(WWindow * wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000101
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200102static int getSavedState(Window window, WSavedState ** state);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000103
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200104static void setupGNUstepHints(WWindow * wwin, GNUstepWMAttributes * gs_hints);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000105
9d2e6ef9 scottc1998-09-29 22:36:29 +0000106/* event handlers */
107
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108/* frame window (during window grabs) */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200109static void frameMouseDown(WObjDescriptor * desc, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000110
111/* close button */
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200112static void windowCloseClick(WCoreWindow * sender, void *data, XEvent * event);
113static void windowCloseDblClick(WCoreWindow * sender, void *data, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000114
115/* iconify button */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200116static void windowIconifyClick(WCoreWindow * sender, void *data, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000117
c28f3b2c id1999-05-20 23:30:53 +0000118#ifdef XKB_BUTTON_HINT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200119static void windowLanguageClick(WCoreWindow * sender, void *data, XEvent * event);
c28f3b2c id1999-05-20 23:30:53 +0000120#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000121
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200122static void titlebarMouseDown(WCoreWindow * sender, void *data, XEvent * event);
123static void titlebarDblClick(WCoreWindow * sender, void *data, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000124
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200125static void resizebarMouseDown(WCoreWindow * sender, void *data, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000126
9df0e167 kojima1999-04-03 03:51:17 +0000127/****** Notification Observers ******/
9af1c6c4 dan1998-10-21 14:43:47 +0000128
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200129static void appearanceObserver(void *self, WMNotification * notif)
9df0e167 kojima1999-04-03 03:51:17 +0000130{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200131 WWindow *wwin = (WWindow *) self;
132 int flags = (int)(uintptr_t) WMGetNotificationClientData(notif);
133
134 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
135 return;
136
137 if (flags & WFontSettings) {
138 wWindowConfigureBorders(wwin);
139 if (wwin->flags.shaded) {
140 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
141
142 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
143 wWindowSynthConfigureNotify(wwin);
144 }
145 }
146 if (flags & WTextureSettings) {
147 wwin->frame->flags.need_texture_remake = 1;
148 }
149 if (flags & (WTextureSettings | WColorSettings)) {
150 if (wwin->frame->titlebar)
151 XClearWindow(dpy, wwin->frame->titlebar->window);
152
153 wFrameWindowPaint(wwin->frame);
154 }
9df0e167 kojima1999-04-03 03:51:17 +0000155}
156
157/************************************/
9af1c6c4 dan1998-10-21 14:43:47 +0000158
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200159WWindow *wWindowFor(Window window)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000160{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161 WObjDescriptor *desc;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000162
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200163 if (window == None)
164 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000165
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200166 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
167 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000168
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200169 if (desc->parent_type == WCLASS_WINDOW)
170 return desc->parent;
171 else if (desc->parent_type == WCLASS_FRAME) {
172 WFrameWindow *frame = (WFrameWindow *) desc->parent;
173 if (frame->flags.is_client_window_frame)
174 return frame->child;
175 }
9df0e167 kojima1999-04-03 03:51:17 +0000176
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200177 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000178}
179
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200180WWindow *wWindowCreate()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000181{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200182 WWindow *wwin;
9aca0d5f dan2004-10-12 01:34:32 +0000183
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200184 wwin = wmalloc(sizeof(WWindow));
185 wretain(wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000186
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200187 memset(wwin, 0, sizeof(WWindow));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000188
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200189 wwin->client_descriptor.handle_mousedown = frameMouseDown;
190 wwin->client_descriptor.parent = wwin;
191 wwin->client_descriptor.self = wwin;
192 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
9df0e167 kojima1999-04-03 03:51:17 +0000193
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200194 return wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000195}
196
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200197void wWindowDestroy(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000198{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199 int i;
59eb9475 dan1998-11-03 12:53:26 +0000200
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200201 if (wwin->screen_ptr->cmap_window == wwin) {
202 wwin->screen_ptr->cmap_window = NULL;
203 }
9aca0d5f dan2004-10-12 01:34:32 +0000204
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200205 WMRemoveNotificationObserver(wwin);
9aca0d5f dan2004-10-12 01:34:32 +0000206
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200207 wwin->flags.destroyed = 1;
59eb9475 dan1998-11-03 12:53:26 +0000208
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200209 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
210 if (!wwin->screen_ptr->shortcutWindows[i])
211 continue;
3c046182 kojima1999-09-16 02:58:56 +0000212
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200213 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
5c881364 kojima1999-09-18 21:17:03 +0000214
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200215 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
216 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
217 wwin->screen_ptr->shortcutWindows[i] = NULL;
218 }
219 }
59eb9475 dan1998-11-03 12:53:26 +0000220
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200221 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
222 wwin->fake_group->retainCount--;
223 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
224 XDestroyWindow(dpy, wwin->fake_group->leader);
225 wwin->fake_group->leader = None;
226 wwin->fake_group->origLeader = None;
227 XFlush(dpy);
228 }
229 }
672c42cc dan2001-12-17 04:02:33 +0000230
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200231 if (wwin->normal_hints)
232 XFree(wwin->normal_hints);
672c42cc dan2001-12-17 04:02:33 +0000233
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200234 if (wwin->wm_hints)
235 XFree(wwin->wm_hints);
672c42cc dan2001-12-17 04:02:33 +0000236
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200237 if (wwin->wm_instance)
238 XFree(wwin->wm_instance);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000239
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200240 if (wwin->wm_class)
241 XFree(wwin->wm_class);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000242
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200243 if (wwin->wm_gnustep_attr)
244 wfree(wwin->wm_gnustep_attr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000245
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200246 if (wwin->cmap_windows)
247 XFree(wwin->cmap_windows);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000248
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200249 XDeleteContext(dpy, wwin->client_win, wWinContext);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000250
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200251 if (wwin->frame)
252 wFrameWindowDestroy(wwin->frame);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000253
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200254 if (wwin->icon) {
255 RemoveFromStackList(wwin->icon->core);
256 wIconDestroy(wwin->icon);
257 if (wPreferences.auto_arrange_icons)
258 wArrangeIcons(wwin->screen_ptr, True);
259 }
9aca0d5f dan2004-10-12 01:34:32 +0000260#ifdef NETWM_HINTS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200261 if (wwin->net_icon_image)
262 RReleaseImage(wwin->net_icon_image);
9aca0d5f dan2004-10-12 01:34:32 +0000263#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000264
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200265 wrelease(wwin);
9aca0d5f dan2004-10-12 01:34:32 +0000266}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000267
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200268static void setupGNUstepHints(WWindow * wwin, GNUstepWMAttributes * gs_hints)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000269{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200270 if (gs_hints->flags & GSWindowStyleAttr) {
271 if (gs_hints->window_style == WMBorderlessWindowMask) {
272 wwin->client_flags.no_border = 1;
273 wwin->client_flags.no_titlebar = 1;
274 wwin->client_flags.no_closable = 1;
275 wwin->client_flags.no_miniaturizable = 1;
276 wwin->client_flags.no_resizable = 1;
277 wwin->client_flags.no_close_button = 1;
278 wwin->client_flags.no_miniaturize_button = 1;
279 wwin->client_flags.no_resizebar = 1;
280 } else {
281 wwin->client_flags.no_close_button =
282 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
283
284 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
285
286 wwin->client_flags.no_miniaturize_button =
287 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
288
289 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
290
291 wwin->client_flags.no_resizebar =
292 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
293
294 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
295
296 /* these attributes supposedly imply in the existence
297 * of a titlebar */
298 if (gs_hints->window_style & (WMResizableWindowMask |
299 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
300 wwin->client_flags.no_titlebar = 0;
301 } else {
302 wwin->client_flags.no_titlebar =
303 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
304 }
305
306 }
307 } else {
308 /* setup the defaults */
309 wwin->client_flags.no_border = 0;
310 wwin->client_flags.no_titlebar = 0;
311 wwin->client_flags.no_closable = 0;
312 wwin->client_flags.no_miniaturizable = 0;
313 wwin->client_flags.no_resizable = 0;
314 wwin->client_flags.no_close_button = 0;
315 wwin->client_flags.no_miniaturize_button = 0;
316 wwin->client_flags.no_resizebar = 0;
317 }
318 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
319 wwin->client_flags.no_appicon = 1;
320 }
d08691d5 dan2004-10-22 22:27:11 +0000321
9d2e6ef9 scottc1998-09-29 22:36:29 +0000322}
323
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200324void wWindowCheckAttributeSanity(WWindow * wwin, WWindowAttributes * wflags, WWindowAttributes * mask)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000325{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200326 if (wflags->no_appicon && mask->no_appicon)
327 wflags->emulate_appicon = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000328
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200329 if (wwin->main_window != None) {
330 WApplication *wapp = wApplicationOf(wwin->main_window);
331 if (wapp && !wapp->flags.emulated)
332 wflags->emulate_appicon = 0;
333 }
9aca0d5f dan2004-10-12 01:34:32 +0000334
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200335 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
336 wflags->emulate_appicon = 0;
9aca0d5f dan2004-10-12 01:34:32 +0000337
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200338 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
339 wflags->sunken = 0;
0261c326 dan1999-01-06 15:22:33 +0000340}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000341
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200342void wWindowSetupInitialAttributes(WWindow * wwin, int *level, int *workspace)
0261c326 dan1999-01-06 15:22:33 +0000343{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200344 WScreen *scr = wwin->screen_ptr;
345
346 /* sets global default stuff */
347 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
348 /*
349 * Decoration setting is done in this precedence (lower to higher)
350 * - use global default in the resource database
351 * - guess some settings
352 * - use GNUstep/external window attributes
353 * - set hints specified for the app in the resource DB
354 *
355 */
356 WSETUFLAG(wwin, broken_close, 0);
357
358 if (wwin->protocols.DELETE_WINDOW)
359 WSETUFLAG(wwin, kill_close, 0);
360 else
361 WSETUFLAG(wwin, kill_close, 1);
362
363 /* transients can't be iconified or maximized */
364 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
365 WSETUFLAG(wwin, no_miniaturizable, 1);
366 WSETUFLAG(wwin, no_miniaturize_button, 1);
367 }
368
369 /* if the window can't be resized, remove the resizebar */
370 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
371 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
372 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
373 WSETUFLAG(wwin, no_resizable, 1);
374 WSETUFLAG(wwin, no_resizebar, 1);
375 }
376
377 /* set GNUstep window attributes */
378 if (wwin->wm_gnustep_attr) {
379 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
380
381 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
382
383 *level = wwin->wm_gnustep_attr->window_level;
384 /*
385 * INT_MIN is the only illegal window level.
386 */
387 if (*level == INT_MIN)
388 *level = INT_MIN + 1;
389 } else {
390 /* setup defaults */
391 *level = WMNormalLevel;
392 }
393 } else {
394 int tmp_workspace = -1;
395 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
396 Bool check;
397
398 check = False;
9aca0d5f dan2004-10-12 01:34:32 +0000399
0261c326 dan1999-01-06 15:22:33 +0000400#ifdef MWM_HINTS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200401 wMWMCheckClientHints(wwin);
402#endif /* MWM_HINTS */
0261c326 dan1999-01-06 15:22:33 +0000403
9aca0d5f dan2004-10-12 01:34:32 +0000404#ifdef NETWM_HINTS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200405 if (!check)
406 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
9aca0d5f dan2004-10-12 01:34:32 +0000407#endif
408
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200409 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
410 * have INT_MIN that means that no window level was requested. -Dan
411 */
412 if (tmp_level == INT_MIN) {
413 if (WFLAGP(wwin, floating))
414 *level = WMFloatingLevel;
415 else if (WFLAGP(wwin, sunken))
416 *level = WMSunkenLevel;
417 else
418 *level = WMNormalLevel;
419 } else {
420 *level = tmp_level;
421 }
422
423 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
424 WWindow *transientOwner = wWindowFor(wwin->transient_for);
425 if (transientOwner) {
426 int ownerLevel = transientOwner->frame->core->stacking->window_level;
427 if (ownerLevel > *level)
428 *level = ownerLevel;
429 }
430 }
431
432 if (tmp_workspace >= 0) {
433 *workspace = tmp_workspace % scr->workspace_count;
434 }
435 }
436
437 /*
438 * Set attributes specified only for that window/class.
439 * This might do duplicate work with the 1st wDefaultFillAttributes().
440 */
441 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
442 &wwin->user_flags, &wwin->defined_user_flags, False);
443 /*
444 * Sanity checks for attributes that depend on other attributes
445 */
446 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
447 wwin->user_flags.emulate_appicon = 0;
448
449 if (wwin->main_window != None) {
450 WApplication *wapp = wApplicationOf(wwin->main_window);
451 if (wapp && !wapp->flags.emulated)
452 wwin->user_flags.emulate_appicon = 0;
453 }
454
455 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
456 wwin->user_flags.emulate_appicon = 0;
457
458 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
459 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
460 wwin->user_flags.sunken = 0;
461
462 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
463
464 /* windows that have takefocus=False shouldn't take focus at all */
465 if (wwin->focus_mode == WFM_NO_INPUT) {
466 wwin->client_flags.no_focusable = 1;
467 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000468}
469
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200470Bool wWindowCanReceiveFocus(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000471{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200472 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
473 return False;
474 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
475 return False;
476 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
477 return False;
478
479 return True;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000480}
481
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200482Bool wWindowObscuresWindow(WWindow * wwin, WWindow * obscured)
0261c326 dan1999-01-06 15:22:33 +0000483{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200484 int w1, h1, w2, h2;
416e3a82 dan1999-01-25 19:06:50 +0000485
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200486 w1 = wwin->frame->core->width;
487 h1 = wwin->frame->core->height;
488 w2 = obscured->frame->core->width;
489 h2 = obscured->frame->core->height;
416e3a82 dan1999-01-25 19:06:50 +0000490
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200491 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
492 && wwin->frame->workspace != obscured->frame->workspace)
493 return False;
0261c326 dan1999-01-06 15:22:33 +0000494
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200495 if (wwin->frame_x + w1 < obscured->frame_x
496 || wwin->frame_y + h1 < obscured->frame_y
497 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2) {
498 return False;
499 }
0261c326 dan1999-01-06 15:22:33 +0000500
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200501 return True;
0261c326 dan1999-01-06 15:22:33 +0000502}
416e3a82 dan1999-01-25 19:06:50 +0000503
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200504static void fixLeaderProperties(WWindow * wwin)
9aca0d5f dan2004-10-12 01:34:32 +0000505{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200506 XClassHint *classHint;
507 XWMHints *hints, *clientHints;
508 Window leaders[2], window;
509 char **argv, *command;
510 int argc, i, pid;
511 Bool haveCommand;
512
513 classHint = XAllocClassHint();
514 clientHints = XGetWMHints(dpy, wwin->client_win);
515 pid = wNETWMGetPidForWindow(wwin->client_win);
516 if (pid > 0) {
517 haveCommand = GetCommandForPid(pid, &argv, &argc);
518 } else {
519 haveCommand = False;
520 }
521
522 leaders[0] = wwin->client_leader;
523 leaders[1] = wwin->group_id;
524
525 if (haveCommand) {
526 command = GetCommandForWindow(wwin->client_win);
527 if (command) {
528 /* command already set. nothing to do. */
529 wfree(command);
530 } else {
531 XSetCommand(dpy, wwin->client_win, argv, argc);
532 }
533 }
534
535 for (i = 0; i < 2; i++) {
536 window = leaders[i];
537 if (window) {
538 if (XGetClassHint(dpy, window, classHint) == 0) {
539 classHint->res_name = wwin->wm_instance;
540 classHint->res_class = wwin->wm_class;
541 XSetClassHint(dpy, window, classHint);
542 }
543 hints = XGetWMHints(dpy, window);
544 if (hints) {
545 XFree(hints);
546 } else if (clientHints) {
547 /* set window group leader to self */
548 clientHints->window_group = window;
549 clientHints->flags |= WindowGroupHint;
550 XSetWMHints(dpy, window, clientHints);
551 }
552
553 if (haveCommand) {
554 command = GetCommandForWindow(window);
555 if (command) {
556 /* command already set. nothing to do. */
557 wfree(command);
558 } else {
559 XSetCommand(dpy, window, argv, argc);
560 }
561 }
562 }
563 }
564
565 XFree(classHint);
566 if (clientHints) {
567 XFree(clientHints);
568 }
569 if (haveCommand) {
570 wfree(argv);
571 }
9aca0d5f dan2004-10-12 01:34:32 +0000572}
573
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200574static Window createFakeWindowGroupLeader(WScreen * scr, Window win, char *instance, char *class)
672c42cc dan2001-12-17 04:02:33 +0000575{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200576 XClassHint *classHint;
577 XWMHints *hints;
578 Window leader;
579 int argc;
580 char **argv;
581
582 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
583 /* set class hint */
584 classHint = XAllocClassHint();
585 classHint->res_name = instance;
586 classHint->res_class = class;
587 XSetClassHint(dpy, leader, classHint);
588 XFree(classHint);
589
590 /* inherit these from the original leader if available */
591 hints = XGetWMHints(dpy, win);
592 if (!hints) {
593 hints = XAllocWMHints();
594 hints->flags = 0;
595 }
596 /* set window group leader to self */
597 hints->window_group = leader;
598 hints->flags |= WindowGroupHint;
599 XSetWMHints(dpy, leader, hints);
600 XFree(hints);
601
602 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
603 XSetCommand(dpy, leader, argv, argc);
604 XFreeStringList(argv);
605 }
606
607 return leader;
672c42cc dan2001-12-17 04:02:33 +0000608}
609
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200610static int matchIdentifier(void *item, void *cdata)
672c42cc dan2001-12-17 04:02:33 +0000611{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200612 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
672c42cc dan2001-12-17 04:02:33 +0000613}
614
9d2e6ef9 scottc1998-09-29 22:36:29 +0000615/*
616 *----------------------------------------------------------------
617 * wManageWindow--
618 * reparents the window and allocates a descriptor for it.
619 * Window manager hints and other hints are fetched to configure
620 * the window decoration attributes and others. User preferences
9aca0d5f dan2004-10-12 01:34:32 +0000621 * for the window are used if available, to configure window
9d2e6ef9 scottc1998-09-29 22:36:29 +0000622 * decorations and some behaviour.
088c0ac2 dan1999-03-09 14:58:01 +0000623 * If in startup, windows that are override redirect,
624 * unmapped and never were managed and are Withdrawn are not
625 * managed.
9aca0d5f dan2004-10-12 01:34:32 +0000626 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000627 * Returns:
628 * the new window descriptor
9aca0d5f dan2004-10-12 01:34:32 +0000629 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000630 * Side effects:
631 * The window is reparented and appropriate notification
632 * is done to the client. Input mask for the window is setup.
9aca0d5f dan2004-10-12 01:34:32 +0000633 * The window descriptor is also associated with various window
9d2e6ef9 scottc1998-09-29 22:36:29 +0000634 * contexts and inserted in the head of the window list.
9aca0d5f dan2004-10-12 01:34:32 +0000635 * Event handler contexts are associated for some objects
9d2e6ef9 scottc1998-09-29 22:36:29 +0000636 * (buttons, titlebar and resizebar)
9aca0d5f dan2004-10-12 01:34:32 +0000637 *
638 *----------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000639 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200640WWindow *wManageWindow(WScreen * scr, Window window)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000641{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200642 WWindow *wwin;
643 int x, y;
644 unsigned width, height;
645 XWindowAttributes wattribs;
646 XSetWindowAttributes attribs;
647 WWindowState *win_state;
648 WWindow *transientOwner = NULL;
649 int window_level;
650 int wm_state;
651 int foo;
652 int workspace = -1;
653 char *title;
654 Bool withdraw = False;
655 Bool raise = False;
656
657 /* mutex. */
658 /* XGrabServer(dpy); */
659 XSync(dpy, False);
660 /* make sure the window is still there */
661 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
662 XUngrabServer(dpy);
663 return NULL;
664 }
665
666 /* if it's an override-redirect, ignore it */
667 if (wattribs.override_redirect) {
668 XUngrabServer(dpy);
669 return NULL;
670 }
671
672 wm_state = PropGetWindowState(window);
673
674 /* if it's startup and the window is unmapped, don't manage it */
675 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
676 XUngrabServer(dpy);
677 return NULL;
678 }
679
680 wwin = wWindowCreate();
681
682 title = wNETWMGetWindowName(window);
683 if (title)
684 wwin->flags.net_has_title = 1;
685 if (!title && !wFetchName(dpy, window, &title))
686 title = NULL;
687
688 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000689
690#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200691 printf("managing window %x\n", (unsigned)window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000692#endif
693
694#ifdef SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200695 if (wShapeSupported) {
696 int junk;
697 unsigned int ujunk;
698 int b_shaped;
699
700 XShapeSelectInput(dpy, window, ShapeNotifyMask);
701 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
702 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
703 wwin->flags.shaped = b_shaped;
704 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000705#endif
9aca0d5f dan2004-10-12 01:34:32 +0000706
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200707 /*
708 *--------------------------------------------------
709 *
710 * Get hints and other information in properties
711 *
712 *--------------------------------------------------
713 */
714 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
715
716 /* setup descriptor */
717 wwin->client_win = window;
718 wwin->screen_ptr = scr;
719
720 wwin->old_border_width = wattribs.border_width;
721
722 wwin->event_mask = CLIENT_EVENTS;
723 attribs.event_mask = CLIENT_EVENTS;
724 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
725 attribs.save_under = False;
726 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
727 XSetWindowBorderWidth(dpy, window, 0);
728
729 /* get hints from GNUstep app */
730 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
731 wwin->flags.is_gnustep = 1;
732 }
733 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
734 wwin->wm_gnustep_attr = NULL;
735 }
736
737 wwin->client_leader = PropGetClientLeader(window);
738 if (wwin->client_leader != None)
739 wwin->main_window = wwin->client_leader;
740
741 wwin->wm_hints = XGetWMHints(dpy, window);
742
743 if (wwin->wm_hints) {
744 if (wwin->wm_hints->flags & StateHint) {
745
746 if (wwin->wm_hints->initial_state == IconicState) {
747
748 wwin->flags.miniaturized = 1;
749
750 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
751
752 withdraw = True;
753 }
754 }
755
756 if (wwin->wm_hints->flags & WindowGroupHint) {
757 wwin->group_id = wwin->wm_hints->window_group;
758 /* window_group has priority over CLIENT_LEADER */
759 wwin->main_window = wwin->group_id;
760 } else {
761 wwin->group_id = None;
762 }
763
764 if (wwin->wm_hints->flags & UrgencyHint)
765 wwin->flags.urgent = 1;
766 } else {
767 wwin->group_id = None;
768 }
769
770 PropGetProtocols(window, &wwin->protocols);
771
772 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
773 wwin->transient_for = None;
774 } else {
775 if (wwin->transient_for == None || wwin->transient_for == window) {
776 wwin->transient_for = scr->root_win;
777 } else {
778 transientOwner = wWindowFor(wwin->transient_for);
779 if (transientOwner && transientOwner->main_window != None) {
780 wwin->main_window = transientOwner->main_window;
781 } /*else {
782 wwin->main_window = None;
783 } */
784 }
785 }
786
787 /* guess the focus mode */
788 wwin->focus_mode = getFocusMode(wwin);
789
790 /* get geometry stuff */
791 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
792
793 /* printf("wManageWindow: %d %d %d %d\n", x, y, width, height); */
794
795 /* get colormap windows */
796 GetColormapWindows(wwin);
797
798 /*
799 *--------------------------------------------------
800 *
801 * Setup the decoration/window attributes and
802 * geometry
803 *
804 *--------------------------------------------------
805 */
806
807 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
808
809 /* Make broken apps behave as a nice app. */
810 if (WFLAGP(wwin, emulate_appicon)) {
811 wwin->main_window = wwin->client_win;
812 }
813
814 fixLeaderProperties(wwin);
815
816 wwin->orig_main_window = wwin->main_window;
817
818 if (wwin->flags.is_gnustep) {
819 WSETUFLAG(wwin, shared_appicon, 0);
820 }
821
822 if (wwin->main_window) {
823 extern Atom _XA_WINDOWMAKER_MENU;
824 XTextProperty text_prop;
825
826 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
827 WSETUFLAG(wwin, shared_appicon, 0);
828 }
829 }
830
831 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
832 char *buffer, *instance, *class;
833 WFakeGroupLeader *fPtr;
834 int index;
672c42cc dan2001-12-17 04:02:33 +0000835
c8b3933c dan2001-12-18 05:18:35 +0000836#define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
837
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200838 /* // only enter here if PropGetWMClass() succeds */
839 PropGetWMClass(wwin->main_window, &class, &instance);
840 buffer = StrConcatDot(instance, class);
841
842 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
843 if (index != WANotFound) {
844 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
845 if (fPtr->retainCount == 0) {
846 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
847 instance, class);
848 }
849 fPtr->retainCount++;
c8b3933c dan2001-12-18 05:18:35 +0000850#undef method2
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200851 if (fPtr->origLeader == None) {
c8b3933c dan2001-12-18 05:18:35 +0000852#ifdef method2
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200853 if (ADEQUATE(wwin->group_id)) {
854 fPtr->retainCount++;
855 fPtr->origLeader = wwin->group_id;
856 } else if (ADEQUATE(wwin->client_leader)) {
857 fPtr->retainCount++;
858 fPtr->origLeader = wwin->client_leader;
859 } else if (ADEQUATE(wwin->main_window)) {
860 fPtr->retainCount++;
861 fPtr->origLeader = wwin->main_window;
862 }
c8b3933c dan2001-12-18 05:18:35 +0000863#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200864 if (ADEQUATE(wwin->main_window)) {
865 fPtr->retainCount++;
866 fPtr->origLeader = wwin->main_window;
867 }
c8b3933c dan2001-12-18 05:18:35 +0000868#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200869 }
870 wwin->fake_group = fPtr;
871 /*wwin->group_id = fPtr->leader; */
872 wwin->main_window = fPtr->leader;
873 wfree(buffer);
874 } else {
875 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
876
877 fPtr->identifier = buffer;
878 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
879 fPtr->origLeader = None;
880 fPtr->retainCount = 1;
881
882 WMAddToArray(scr->fakeGroupLeaders, fPtr);
672c42cc dan2001-12-17 04:02:33 +0000883
c8b3933c dan2001-12-18 05:18:35 +0000884#ifdef method2
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200885 if (ADEQUATE(wwin->group_id)) {
886 fPtr->retainCount++;
887 fPtr->origLeader = wwin->group_id;
888 } else if (ADEQUATE(wwin->client_leader)) {
889 fPtr->retainCount++;
890 fPtr->origLeader = wwin->client_leader;
891 } else if (ADEQUATE(wwin->main_window)) {
892 fPtr->retainCount++;
893 fPtr->origLeader = wwin->main_window;
894 }
c8b3933c dan2001-12-18 05:18:35 +0000895#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200896 if (ADEQUATE(wwin->main_window)) {
897 fPtr->retainCount++;
898 fPtr->origLeader = wwin->main_window;
899 }
c8b3933c dan2001-12-18 05:18:35 +0000900#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200901 wwin->fake_group = fPtr;
902 /*wwin->group_id = fPtr->leader; */
903 wwin->main_window = fPtr->leader;
904 }
905 if (instance)
906 XFree(instance);
907 if (class)
908 XFree(class);
672c42cc dan2001-12-17 04:02:33 +0000909
c8b3933c dan2001-12-18 05:18:35 +0000910#undef method2
911#undef ADEQUATE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200912 }
913
914 /*
915 *------------------------------------------------------------
916 *
917 * Setup the initial state of the window
918 *
919 *------------------------------------------------------------
920 */
921
922 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
923 wwin->flags.miniaturized = 1;
924 }
925
926 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin)) {
927 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
928 }
9aca0d5f dan2004-10-12 01:34:32 +0000929#ifdef NETWM_HINTS
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200930 wNETWMCheckInitialClientState(wwin);
9aca0d5f dan2004-10-12 01:34:32 +0000931#endif
0261c326 dan1999-01-06 15:22:33 +0000932
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200933 /* apply previous state if it exists and we're in startup */
934 if (scr->flags.startup && wm_state >= 0) {
935
936 if (wm_state == IconicState) {
937
938 wwin->flags.miniaturized = 1;
939
940 } else if (wm_state == WithdrawnState) {
941
942 withdraw = True;
943 }
944 }
945
946 /* if there is a saved state (from file), restore it */
947 win_state = NULL;
948 if (wwin->main_window != None /* && wwin->main_window!=window */ ) {
949 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
950 } else {
951 win_state = (WWindowState *) wWindowGetSavedState(window);
952 }
953 if (win_state && !withdraw) {
954
955 if (win_state->state->hidden > 0)
956 wwin->flags.hidden = win_state->state->hidden;
957
958 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
959 wwin->flags.shaded = win_state->state->shaded;
960
961 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
962 wwin->flags.miniaturized = win_state->state->miniaturized;
963 }
964
965 if (!IS_OMNIPRESENT(wwin)) {
966 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
967 wwin->wm_class);
968 if (w < 0 || w >= scr->workspace_count) {
969 workspace = win_state->state->workspace;
970 if (workspace >= scr->workspace_count)
971 workspace = scr->current_workspace;
972 } else {
973 workspace = w;
974 }
975 } else {
976 workspace = scr->current_workspace;
977 }
978 }
979
980 /* if we're restarting, restore saved state (from hints).
981 * This will overwrite previous */
982 {
983 WSavedState *wstate;
984
985 if (getSavedState(window, &wstate)) {
986 wwin->flags.shaded = wstate->shaded;
987 wwin->flags.hidden = wstate->hidden;
988 wwin->flags.miniaturized = wstate->miniaturized;
989 wwin->flags.maximized = wstate->maximized;
990 if (wwin->flags.maximized) {
991 wwin->old_geometry.x = wstate->x;
992 wwin->old_geometry.y = wstate->y;
993 wwin->old_geometry.width = wstate->w;
994 wwin->old_geometry.height = wstate->h;
995 }
996
997 workspace = wstate->workspace;
998 } else {
999 wstate = NULL;
1000 }
1001
1002 /* restore window shortcut */
1003 if (wstate != NULL || win_state != NULL) {
1004 unsigned mask = 0;
1005
1006 if (win_state != NULL)
1007 mask = win_state->state->window_shortcuts;
1008
1009 if (wstate != NULL && mask == 0)
1010 mask = wstate->window_shortcuts;
1011
1012 if (mask > 0) {
1013 int i;
1014
1015 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1016 if (mask & (1 << i)) {
1017 if (!scr->shortcutWindows[i])
1018 scr->shortcutWindows[i] = WMCreateArray(4);
1019
1020 WMAddToArray(scr->shortcutWindows[i], wwin);
1021 }
1022 }
1023 }
1024 }
1025 if (wstate != NULL) {
1026 wfree(wstate);
1027 }
1028 }
1029
1030 /* don't let transients start miniaturized if their owners are not */
1031 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
1032 wwin->flags.miniaturized = 0;
1033 if (wwin->wm_hints)
1034 wwin->wm_hints->initial_state = NormalState;
1035 }
1036
1037 /* set workspace on which the window starts */
1038 if (workspace >= 0) {
1039 if (workspace > scr->workspace_count - 1) {
1040 workspace = workspace % scr->workspace_count;
1041 }
1042 } else {
1043 int w;
1044
1045 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1046
1047 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1048
1049 workspace = w;
1050
1051 } else {
1052 if (wPreferences.open_transients_with_parent && transientOwner) {
1053
1054 workspace = transientOwner->frame->workspace;
1055
1056 } else {
1057
1058 workspace = scr->current_workspace;
1059 }
1060 }
1061 }
1062
1063 /* setup window geometry */
1064 if (win_state && win_state->state->w > 0) {
1065 width = win_state->state->w;
1066 height = win_state->state->h;
1067 }
1068 wWindowConstrainSize(wwin, &width, &height);
1069
1070 /* do not ask for window placement if the window is
1071 * transient, during startup, if the initial workspace is another one
1072 * or if the window wants to start iconic.
1073 * If geometry was saved, restore it. */
1074 {
1075 Bool dontBring = False;
1076
1077 if (win_state && win_state->state->w > 0) {
1078 x = win_state->state->x;
1079 y = win_state->state->y;
1080 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1081 && !scr->flags.startup
1082 && workspace == scr->current_workspace
1083 && !wwin->flags.miniaturized
1084 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1085
1086 if (transientOwner && transientOwner->flags.mapped) {
1087 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1088 WMRect rect;
1089 int head;
1090
1091 x = transientOwner->frame_x +
1092 abs((transientOwner->frame->core->width - width) / 2) + offs;
1093 y = transientOwner->frame_y +
1094 abs((transientOwner->frame->core->height - height) / 3) + offs;
1095
1096 /*
1097 * limit transient windows to be inside their parent's head
1098 */
1099 rect.pos.x = transientOwner->frame_x;
1100 rect.pos.y = transientOwner->frame_y;
1101 rect.size.width = transientOwner->frame->core->width;
1102 rect.size.height = transientOwner->frame->core->height;
1103
1104 head = wGetHeadForRect(scr, rect);
1105 rect = wGetRectForHead(scr, head);
1106
1107 if (x < rect.pos.x)
1108 x = rect.pos.x;
1109 else if (x + width > rect.pos.x + rect.size.width)
1110 x = rect.pos.x + rect.size.width - width;
1111
1112 if (y < rect.pos.y)
1113 y = rect.pos.y;
1114 else if (y + height > rect.pos.y + rect.size.height)
1115 y = rect.pos.y + rect.size.height - height;
1116
1117 } else {
1118 PlaceWindow(wwin, &x, &y, width, height);
1119 }
1120 if (wPreferences.window_placement == WPM_MANUAL) {
1121 dontBring = True;
1122 }
1123 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1124 int head, flags;
1125 WMRect rect;
1126 int reposition = 0;
1127
1128 /*
1129 * Make spash screens come out in the center of a head
1130 * trouble is that most splashies never get here
1131 * they are managed trough atoms but god knows where.
1132 * Dan, do you know ? -peter
1133 *
1134 * Most of them are not managed, they have set
1135 * OverrideRedirect, which means we can't do anything about
1136 * them. -alfredo
1137 */
a10214a5 kojima2002-11-28 22:04:07 +00001138#if 0
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001139 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width) / 2);
1140 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height) / 2);
a10214a5 kojima2002-11-28 22:04:07 +00001141
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001142 if ((unsigned)(x + (width - scr->scr_width) / 2 + 10) < 20 &&
1143 (unsigned)(y + (height - scr->scr_height) / 2 + 10) < 20) {
a10214a5 kojima2002-11-28 22:04:07 +00001144
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001145 reposition = 1;
a10214a5 kojima2002-11-28 22:04:07 +00001146
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001147 } else
a10214a5 kojima2002-11-28 22:04:07 +00001148#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001149 {
1150 /*
1151 * xinerama checks for: across head and dead space
1152 */
1153 rect.pos.x = x;
1154 rect.pos.y = y;
1155 rect.size.width = width;
1156 rect.size.height = height;
6830b057 dan2004-10-12 21:28:27 +00001157
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001158 head = wGetRectPlacementInfo(scr, rect, &flags);
6830b057 dan2004-10-12 21:28:27 +00001159
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001160 if (flags & XFLAG_DEAD)
1161 reposition = 1;
6830b057 dan2004-10-12 21:28:27 +00001162
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001163 if (flags & XFLAG_MULTIPLE)
1164 reposition = 2;
1165 }
9aca0d5f dan2004-10-12 01:34:32 +00001166
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001167 switch (reposition) {
1168 case 1:
1169 head = wGetHeadForPointerLocation(scr);
1170 rect = wGetRectForHead(scr, head);
9aca0d5f dan2004-10-12 01:34:32 +00001171
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001172 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1173 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1174 break;
9aca0d5f dan2004-10-12 01:34:32 +00001175
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001176 case 2:
1177 rect = wGetRectForHead(scr, head);
9aca0d5f dan2004-10-12 01:34:32 +00001178
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001179 if (x < rect.pos.x)
1180 x = rect.pos.x;
1181 else if (x + width > rect.pos.x + rect.size.width)
1182 x = rect.pos.x + rect.size.width - width;
9aca0d5f dan2004-10-12 01:34:32 +00001183
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001184 if (y < rect.pos.y)
1185 y = rect.pos.y;
1186 else if (y + height > rect.pos.y + rect.size.height)
1187 y = rect.pos.y + rect.size.height - height;
9aca0d5f dan2004-10-12 01:34:32 +00001188
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001189 break;
9aca0d5f dan2004-10-12 01:34:32 +00001190
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001191 default:
1192 break;
1193 }
1194 }
345d980b dan1998-11-27 12:26:46 +00001195
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001196 if (WFLAGP(wwin, dont_move_off) && dontBring)
1197 wScreenBringInside(scr, &x, &y, width, height);
1198 }
088c0ac2 dan1999-03-09 14:58:01 +00001199
9aca0d5f dan2004-10-12 01:34:32 +00001200#ifdef NETWM_HINTS
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001201 wNETWMPositionSplash(wwin, &x, &y, width, height);
9aca0d5f dan2004-10-12 01:34:32 +00001202#endif
1203
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001204 if (wwin->flags.urgent) {
1205 if (!IS_OMNIPRESENT(wwin))
1206 wwin->flags.omnipresent ^= 1;
1207 }
1208
1209 /*
1210 *--------------------------------------------------
1211 *
1212 * Create frame, borders and do reparenting
1213 *
1214 *--------------------------------------------------
1215 */
1216 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
c28f3b2c id1999-05-20 23:30:53 +00001217#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001218 if (wPreferences.modelock)
1219 foo |= WFF_LANGUAGE_BUTTON;
c28f3b2c id1999-05-20 23:30:53 +00001220#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001221 if (HAS_TITLEBAR(wwin))
1222 foo |= WFF_TITLEBAR;
1223 if (HAS_RESIZEBAR(wwin))
1224 foo |= WFF_RESIZEBAR;
1225 if (HAS_BORDER(wwin))
1226 foo |= WFF_BORDER;
1227
1228 wwin->frame = wFrameWindowCreate(scr, window_level,
1229 x, y, width, height,
1230 &wPreferences.window_title_clearance, foo,
1231 scr->window_title_texture,
1232 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1233
1234 wwin->frame->flags.is_client_window_frame = 1;
1235 wwin->frame->flags.justification = wPreferences.title_justification;
1236
1237 /* setup button images */
1238 wWindowUpdateButtonImages(wwin);
1239
1240 /* hide unused buttons */
1241 foo = 0;
1242 if (WFLAGP(wwin, no_close_button))
1243 foo |= WFF_RIGHT_BUTTON;
1244 if (WFLAGP(wwin, no_miniaturize_button))
1245 foo |= WFF_LEFT_BUTTON;
daaccf6f id1999-05-23 23:48:08 +00001246#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001247 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1248 foo |= WFF_LANGUAGE_BUTTON;
daaccf6f id1999-05-23 23:48:08 +00001249#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001250 if (foo != 0)
1251 wFrameWindowHideButton(wwin->frame, foo);
416e3a82 dan1999-01-25 19:06:50 +00001252
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001253 wwin->frame->child = wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001254
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001255 wwin->frame->workspace = workspace;
0261c326 dan1999-01-06 15:22:33 +00001256
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001257 wwin->frame->on_click_left = windowIconifyClick;
c28f3b2c id1999-05-20 23:30:53 +00001258#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001259 if (wPreferences.modelock)
1260 wwin->frame->on_click_language = windowLanguageClick;
c28f3b2c id1999-05-20 23:30:53 +00001261#endif
0261c326 dan1999-01-06 15:22:33 +00001262
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001263 wwin->frame->on_click_right = windowCloseClick;
1264 wwin->frame->on_dblclick_right = windowCloseDblClick;
1265
1266 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1267 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1268
1269 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1270
1271 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1272
1273 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1274
1275 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1276
1277 {
1278 int gx, gy;
1279
1280 wClientGetGravityOffsets(wwin, &gx, &gy);
1281
1282 /* if gravity is to the south, account for the border sizes */
1283 if (gy > 0)
1284 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1285 }
1286
1287 /*
1288 * wWindowConfigure() will init the client window's size
1289 * (wwin->client.{width,height}) and all other geometry
1290 * related variables (frame_x,frame_y)
1291 */
1292 wWindowConfigure(wwin, x, y, width, height);
1293
1294 /* to make sure the window receives it's new position after reparenting */
1295 wWindowSynthConfigureNotify(wwin);
1296
1297 /*
1298 *--------------------------------------------------
1299 *
1300 * Setup descriptors and save window to internal
1301 * lists
1302 *
1303 *--------------------------------------------------
1304 */
1305
1306 if (wwin->main_window != None) {
1307 WApplication *app;
1308 WWindow *leader;
1309
1310 /* Leader windows do not necessary set themselves as leaders.
1311 * If this is the case, point the leader of this window to
1312 * itself */
1313 leader = wWindowFor(wwin->main_window);
1314 if (leader && leader->main_window == None) {
1315 leader->main_window = leader->client_win;
1316 }
1317 app = wApplicationCreate(wwin);
1318 if (app) {
1319 app->last_workspace = workspace;
1320
1321 /*
1322 * Do application specific stuff, like setting application
1323 * wide attributes.
1324 */
1325
1326 if (wwin->flags.hidden) {
1327 /* if the window was set to hidden because it was hidden
1328 * in a previous incarnation and that state was restored */
1329 app->flags.hidden = 1;
1330 } else if (app->flags.hidden) {
1331 if (WFLAGP(app->main_window_desc, start_hidden)) {
1332 wwin->flags.hidden = 1;
1333 } else {
1334 wUnhideApplication(app, False, False);
1335 raise = True;
1336 }
1337 }
1338 }
1339 }
1340
1341 /* setup the frame descriptor */
1342 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1343 wwin->frame->core->descriptor.parent = wwin;
1344 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1345
1346 /* don't let windows go away if we die */
1347 XAddToSaveSet(dpy, window);
1348
1349 XLowerWindow(dpy, window);
1350
1351 /* if window is in this workspace and should be mapped, then map it */
1352 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1353 && !wwin->flags.hidden && !withdraw) {
1354
1355 /* The following "if" is to avoid crashing of clients that expect
1356 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1357 * after the return from this function.
1358 */
1359 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1360 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1361 } else {
1362 wClientSetState(wwin, NormalState, None);
1363 }
0261c326 dan1999-01-06 15:22:33 +00001364
9acdde00 kojima1999-03-20 02:42:48 +00001365#if 0
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001366 /* if not auto focus, then map the window under the currently
1367 * focused window */
416e3a82 dan1999-01-25 19:06:50 +00001368#define _WIDTH(w) (w)->frame->core->width
1369#define _HEIGHT(w) (w)->frame->core->height
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001370 if (!wPreferences.auto_focus && scr->focused_window
1371 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1372 && (_WIDTH(wwin) >
1373 (_WIDTH(scr->focused_window) * 5) / 3
1374 || _HEIGHT(wwin) >
1375 (_HEIGHT(scr->focused_window) * 5) / 3)
1376 && WINDOW_LEVEL(scr->focused_window) ==
1377 WINDOW_LEVEL(wwin))
1378 || wwin->flags.maximized)) {
1379 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1380 }
416e3a82 dan1999-01-25 19:06:50 +00001381#undef _WIDTH
1382#undef _HEIGHT
1383
9acdde00 kojima1999-03-20 02:42:48 +00001384#endif
1385
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001386 if (wPreferences.superfluous && !wPreferences.no_animations
1387 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1388 /*
1389 * The brain damaged idiotic non-click to focus modes will
1390 * have trouble with this because:
1391 *
1392 * 1. window is created and mapped by the client
1393 * 2. window is mapped by wmaker in small size
1394 * 3. window is animated to grow to normal size
1395 * 4. this function returns to normal event loop
1396 * 5. eventually, the EnterNotify event that would trigger
1397 * the window focusing (if the mouse is over that window)
1398 * will be processed by wmaker.
1399 * But since this event will be rather delayed
1400 * (step 3 has a large delay) the time when the event ocurred
1401 * and when it is processed, the client that owns that window
1402 * will reject the XSetInputFocus() for it.
1403 */
1404 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1405 DoWindowBirth(wwin);
1406 }
1407
1408 wWindowMap(wwin);
1409 }
1410
1411 /* setup stacking descriptor */
1412 if (transientOwner) {
1413 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1414 } else {
1415 wwin->frame->core->stacking->child_of = NULL;
1416 }
1417
1418 if (!scr->focused_window) {
1419 /* first window on the list */
1420 wwin->next = NULL;
1421 wwin->prev = NULL;
1422 scr->focused_window = wwin;
1423 } else {
1424 WWindow *tmp;
1425
1426 /* add window at beginning of focus window list */
1427 tmp = scr->focused_window;
1428 while (tmp->prev)
1429 tmp = tmp->prev;
1430 tmp->prev = wwin;
1431 wwin->next = tmp;
1432 wwin->prev = NULL;
1433 }
1434
1435 /* raise is set to true if we un-hid the app when this window was born.
1436 * we raise, else old windows of this app will be above this new one. */
1437 if (raise) {
1438 wRaiseFrame(wwin->frame->core);
1439 }
1440
1441 /* Update name must come after WApplication stuff is done */
1442 wWindowUpdateName(wwin, title);
1443 if (title)
1444 XFree(title);
1445
1446 XUngrabServer(dpy);
1447
1448 /*
1449 *--------------------------------------------------
1450 *
1451 * Final preparations before window is ready to go
1452 *
1453 *--------------------------------------------------
1454 */
1455
1456 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1457
1458 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1459 if (((transientOwner && transientOwner->flags.focused)
1460 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1461 wSetFocusTo(scr, wwin);
1462 }
1463 wWindowResetMouseGrabs(wwin);
1464
1465 if (!WFLAGP(wwin, no_bind_keys)) {
1466 wWindowSetKeyGrabs(wwin);
1467 }
1468
1469 WMPostNotificationName(WMNManaged, wwin, NULL);
1470
1471 wColormapInstallForWindow(scr, scr->cmap_window);
1472
1473 /*
1474 *------------------------------------------------------------
1475 * Setup Notification Observers
1476 *------------------------------------------------------------
1477 */
1478 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1479
1480 /*
1481 *--------------------------------------------------
1482 *
1483 * Cleanup temporary stuff
1484 *
1485 *--------------------------------------------------
1486 */
1487
1488 if (win_state)
1489 wWindowDeleteSavedState(win_state);
1490
1491 /* If the window must be withdrawed, then do it now.
1492 * Must do some optimization, 'though */
1493 if (withdraw) {
1494 wwin->flags.mapped = 0;
1495 wClientSetState(wwin, WithdrawnState, None);
1496 wUnmanageWindow(wwin, True, False);
1497 wwin = NULL;
1498 }
1499
1500 return wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001501}
1502
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001503WWindow *wManageInternalWindow(WScreen * scr, Window window, Window owner,
1504 char *title, int x, int y, int width, int height)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001505{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001506 WWindow *wwin;
1507 int foo;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001508
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001509 wwin = wWindowCreate();
9df0e167 kojima1999-04-03 03:51:17 +00001510
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001511 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
0261c326 dan1999-01-06 15:22:33 +00001512
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001513 wwin->flags.internal_window = 1;
0261c326 dan1999-01-06 15:22:33 +00001514
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001515 WSETUFLAG(wwin, omnipresent, 1);
1516 WSETUFLAG(wwin, no_shadeable, 1);
1517 WSETUFLAG(wwin, no_resizable, 1);
1518 WSETUFLAG(wwin, no_miniaturizable, 1);
9aca0d5f dan2004-10-12 01:34:32 +00001519
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001520 wwin->focus_mode = WFM_PASSIVE;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001521
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001522 wwin->client_win = window;
1523 wwin->screen_ptr = scr;
9aca0d5f dan2004-10-12 01:34:32 +00001524
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001525 wwin->transient_for = owner;
9aca0d5f dan2004-10-12 01:34:32 +00001526
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001527 wwin->client.x = x;
1528 wwin->client.y = y;
1529 wwin->client.width = width;
1530 wwin->client.height = height;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001531
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001532 wwin->frame_x = wwin->client.x;
1533 wwin->frame_y = wwin->client.y;
9aca0d5f dan2004-10-12 01:34:32 +00001534
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001535 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1536 foo |= WFF_TITLEBAR;
c28f3b2c id1999-05-20 23:30:53 +00001537#ifdef XKB_BUTTON_HINT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001538 foo |= WFF_LANGUAGE_BUTTON;
c28f3b2c id1999-05-20 23:30:53 +00001539#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +00001540
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001541 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1542 wwin->frame_x, wwin->frame_y,
1543 width, height,
1544 &wPreferences.window_title_clearance, foo,
1545 scr->window_title_texture,
1546 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
9aca0d5f dan2004-10-12 01:34:32 +00001547
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001548 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001549
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001550 wwin->frame->flags.is_client_window_frame = 1;
1551 wwin->frame->flags.justification = wPreferences.title_justification;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001552
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001553 wFrameWindowChangeTitle(wwin->frame, title);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001554
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001555 /* setup button images */
1556 wWindowUpdateButtonImages(wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001557
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001558 /* hide buttons */
1559 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
9aca0d5f dan2004-10-12 01:34:32 +00001560
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001561 wwin->frame->child = wwin;
9aca0d5f dan2004-10-12 01:34:32 +00001562
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001563 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
9aca0d5f dan2004-10-12 01:34:32 +00001564
b3112e2b id1999-05-22 21:30:35 +00001565#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001566 if (wPreferences.modelock)
1567 wwin->frame->on_click_language = windowLanguageClick;
c28f3b2c id1999-05-20 23:30:53 +00001568#endif
9aca0d5f dan2004-10-12 01:34:32 +00001569
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001570 wwin->frame->on_click_right = windowCloseClick;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001571
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001572 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1573 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001574
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001575 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001576
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001577 wwin->client.y += wwin->frame->top_width;
1578 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001579
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001580 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001581
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001582 /* setup the frame descriptor */
1583 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1584 wwin->frame->core->descriptor.parent = wwin;
1585 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
9aca0d5f dan2004-10-12 01:34:32 +00001586
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001587 XLowerWindow(dpy, window);
1588 XMapSubwindows(dpy, wwin->frame->core->window);
9aca0d5f dan2004-10-12 01:34:32 +00001589
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001590 /* setup stacking descriptor */
1591 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1592 WWindow *tmp;
1593 tmp = wWindowFor(wwin->transient_for);
1594 if (tmp)
1595 wwin->frame->core->stacking->child_of = tmp->frame->core;
1596 } else {
1597 wwin->frame->core->stacking->child_of = NULL;
1598 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001599
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001600 if (!scr->focused_window) {
1601 /* first window on the list */
1602 wwin->next = NULL;
1603 wwin->prev = NULL;
1604 scr->focused_window = wwin;
1605 } else {
1606 WWindow *tmp;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001607
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001608 /* add window at beginning of focus window list */
1609 tmp = scr->focused_window;
1610 while (tmp->prev)
1611 tmp = tmp->prev;
1612 tmp->prev = wwin;
1613 wwin->next = tmp;
1614 wwin->prev = NULL;
1615 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001616
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001617 if (wwin->flags.is_gnustep == 0)
1618 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001619
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001620 /* if (wPreferences.auto_focus) */
1621 wSetFocusTo(scr, wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001622
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001623 wWindowResetMouseGrabs(wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001624
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001625 wWindowSetKeyGrabs(wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001626
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001627 return wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001628}
1629
9d2e6ef9 scottc1998-09-29 22:36:29 +00001630/*
9aca0d5f dan2004-10-12 01:34:32 +00001631 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +00001632 * wUnmanageWindow--
1633 * Removes the frame window from a window and destroys all data
1634 * related to it. The window will be reparented back to the root window
1635 * if restore is True.
9aca0d5f dan2004-10-12 01:34:32 +00001636 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00001637 * Side effects:
1638 * Everything related to the window is destroyed and the window
1639 * is removed from the window lists. Focus is set to the previous on the
1640 * window list.
9aca0d5f dan2004-10-12 01:34:32 +00001641 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +00001642 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001643void wUnmanageWindow(WWindow * wwin, Bool restore, Bool destroyed)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001644{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001645 WCoreWindow *frame = wwin->frame->core;
1646 WWindow *owner = NULL;
1647 WWindow *newFocusedWindow = NULL;
1648 int wasFocused;
1649 WScreen *scr = wwin->screen_ptr;
1650
1651 /* First close attribute editor window if open */
1652 if (wwin->flags.inspector_open) {
1653 wCloseInspectorForWindow(wwin);
1654 }
1655
1656 /* Close window menu if it's open for this window */
1657 if (wwin->flags.menu_open_for_me) {
1658 CloseWindowMenu(scr);
1659 }
1660
1661 if (!destroyed) {
1662 if (!wwin->flags.internal_window)
1663 XRemoveFromSaveSet(dpy, wwin->client_win);
1664
1665 XSelectInput(dpy, wwin->client_win, NoEventMask);
1666
1667 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1668 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1669 }
1670
1671 XUnmapWindow(dpy, frame->window);
1672
1673 XUnmapWindow(dpy, wwin->client_win);
1674
1675 /* deselect window */
1676 wSelectWindow(wwin, False);
1677
1678 /* remove all pending events on window */
1679 /* I think this only matters for autoraise */
1680 if (wPreferences.raise_delay)
1681 WMDeleteTimerWithClientData(wwin->frame->core);
1682
1683 XFlush(dpy);
1684
1685 /* reparent the window back to the root */
1686 if (restore)
1687 wClientRestore(wwin);
1688
1689 if (wwin->transient_for != scr->root_win) {
1690 owner = wWindowFor(wwin->transient_for);
1691 if (owner) {
1692 if (!owner->flags.semi_focused) {
1693 owner = NULL;
1694 } else {
1695 owner->flags.semi_focused = 0;
1696 }
1697 }
1698 }
1699
1700 wasFocused = wwin->flags.focused;
1701
1702 /* remove from window focus list */
1703 if (!wwin->prev && !wwin->next) {
1704 /* was the only window */
1705 scr->focused_window = NULL;
1706 newFocusedWindow = NULL;
1707 } else {
1708 WWindow *tmp;
1709
1710 if (wwin->prev)
1711 wwin->prev->next = wwin->next;
1712 if (wwin->next)
1713 wwin->next->prev = wwin->prev;
1714 else {
1715 scr->focused_window = wwin->prev;
1716 scr->focused_window->next = NULL;
1717 }
1718
1719 if (wPreferences.focus_mode == WKF_CLICK) {
1720
1721 /* if in click to focus mode and the window
1722 * was a transient, focus the owner window
1723 */
1724 tmp = NULL;
1725 if (wPreferences.focus_mode == WKF_CLICK) {
1726 tmp = wWindowFor(wwin->transient_for);
1727 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1728 tmp = NULL;
1729 }
1730 }
1731 /* otherwise, focus the next one in the focus list */
1732 if (!tmp) {
1733 tmp = scr->focused_window;
1734 while (tmp) { /* look for one in the window list first */
1735 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1736 && (tmp->flags.mapped || tmp->flags.shaded))
1737 break;
1738 tmp = tmp->prev;
1739 }
1740 if (!tmp) { /* if unsuccessful, choose any focusable window */
1741 tmp = scr->focused_window;
1742 while (tmp) {
1743 if (!WFLAGP(tmp, no_focusable)
1744 && (tmp->flags.mapped || tmp->flags.shaded))
1745 break;
1746 tmp = tmp->prev;
1747 }
1748 }
1749 }
1750
1751 newFocusedWindow = tmp;
1752
1753 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1754 unsigned int mask;
1755 int foo;
1756 Window bar, win;
1757
1758 /* This is to let the root window get the keyboard input
1759 * if Sloppy focus mode and no other window get focus.
1760 * This way keybindings will not freeze.
1761 */
1762 tmp = NULL;
1763 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1764 tmp = wWindowFor(win);
1765 if (tmp == wwin)
1766 tmp = NULL;
1767 newFocusedWindow = tmp;
1768 } else {
1769 newFocusedWindow = NULL;
1770 }
1771 }
1772
1773 if (!wwin->flags.internal_window) {
1774 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1775 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001776#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001777 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win, (unsigned)frame->window);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001778#endif
416e3a82 dan1999-01-25 19:06:50 +00001779
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001780 if (wasFocused) {
1781 if (newFocusedWindow != owner && owner) {
1782 if (wwin->flags.is_gnustep == 0)
1783 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1784 }
1785 wSetFocusTo(scr, newFocusedWindow);
1786 }
1787 wWindowDestroy(wwin);
1788 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001789}
1790
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001791void wWindowMap(WWindow * wwin)
e7495baf dan1999-02-17 11:06:40 +00001792{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001793 XMapWindow(dpy, wwin->frame->core->window);
1794 if (!wwin->flags.shaded) {
1795 /* window will be remapped when getting MapNotify */
1796 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1797 XMapWindow(dpy, wwin->client_win);
1798 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1799
1800 wwin->flags.mapped = 1;
1801 }
e7495baf dan1999-02-17 11:06:40 +00001802}
1803
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001804void wWindowUnmap(WWindow * wwin)
e7495baf dan1999-02-17 11:06:40 +00001805{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001806 wwin->flags.mapped = 0;
e7495baf dan1999-02-17 11:06:40 +00001807
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001808 /* prevent window withdrawal when getting UnmapNotify */
1809 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1810 XUnmapWindow(dpy, wwin->client_win);
1811 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
e7495baf dan1999-02-17 11:06:40 +00001812
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001813 XUnmapWindow(dpy, wwin->frame->core->window);
e7495baf dan1999-02-17 11:06:40 +00001814}
1815
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001816void wWindowFocus(WWindow * wwin, WWindow * owin)
59eb9475 dan1998-11-03 12:53:26 +00001817{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001818 WWindow *nowner;
1819 WWindow *oowner;
e7495baf dan1999-02-17 11:06:40 +00001820
59eb9475 dan1998-11-03 12:53:26 +00001821#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001822 if (wPreferences.modelock) {
1823 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1824 }
1825#endif /* KEEP_XKB_LOCK_STATUS */
59eb9475 dan1998-11-03 12:53:26 +00001826
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001827 wwin->flags.semi_focused = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001828
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001829 if (wwin->flags.is_gnustep == 0)
1830 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001831
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001832 wwin->flags.focused = 1;
e7495baf dan1999-02-17 11:06:40 +00001833
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001834 wWindowResetMouseGrabs(wwin);
7f018497 kojima1999-04-24 20:08:20 +00001835
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001836 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
0261c326 dan1999-01-06 15:22:33 +00001837
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001838 if (owin == wwin || !owin)
1839 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001840
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001841 nowner = wWindowFor(wwin->transient_for);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001842
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001843 /* new window is a transient for the old window */
1844 if (nowner == owin) {
1845 owin->flags.semi_focused = 1;
1846 wWindowUnfocus(nowner);
1847 return;
1848 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001849
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001850 oowner = wWindowFor(owin->transient_for);
9eb4f894 kojima1999-04-17 23:11:34 +00001851
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001852 /* new window is owner of old window */
1853 if (wwin == oowner) {
1854 wWindowUnfocus(owin);
1855 return;
1856 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001857
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001858 if (!nowner) {
1859 wWindowUnfocus(owin);
1860 return;
1861 }
416e3a82 dan1999-01-25 19:06:50 +00001862
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001863 /* new window has same owner of old window */
1864 if (oowner == nowner) {
1865 /* prevent unfocusing of owner */
1866 oowner->flags.semi_focused = 0;
1867 wWindowUnfocus(owin);
1868 oowner->flags.semi_focused = 1;
416e3a82 dan1999-01-25 19:06:50 +00001869
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001870 return;
1871 }
416e3a82 dan1999-01-25 19:06:50 +00001872
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001873 /* nowner != NULL && oowner != nowner */
1874 nowner->flags.semi_focused = 1;
1875 wWindowUnfocus(nowner);
1876 wWindowUnfocus(owin);
416e3a82 dan1999-01-25 19:06:50 +00001877}
1878
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001879void wWindowUnfocus(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001880{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001881 CloseWindowMenu(wwin->screen_ptr);
1882
1883 if (wwin->flags.is_gnustep == 0)
1884 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1885
1886 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1887 WWindow *owner;
1888 owner = wWindowFor(wwin->transient_for);
1889 if (owner && owner->flags.semi_focused) {
1890 owner->flags.semi_focused = 0;
1891 if (owner->flags.mapped || owner->flags.shaded) {
1892 wWindowUnfocus(owner);
1893 wFrameWindowPaint(owner->frame);
1894 }
1895 }
1896 }
1897 wwin->flags.focused = 0;
1898
1899 wWindowResetMouseGrabs(wwin);
1900
1901 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001902}
1903
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001904void wWindowUpdateName(WWindow * wwin, char *newTitle)
7a491db6 kojima2001-02-11 02:18:26 +00001905{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001906 char *title;
9aca0d5f dan2004-10-12 01:34:32 +00001907
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001908 if (!wwin->frame)
1909 return;
9aca0d5f dan2004-10-12 01:34:32 +00001910
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001911 wwin->flags.wm_name_changed = 1;
9aca0d5f dan2004-10-12 01:34:32 +00001912
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001913 if (!newTitle) {
1914 /* the hint was removed */
1915 title = DEF_WINDOW_TITLE;
1916 } else {
1917 title = newTitle;
1918 }
5ef342d9 kojima2001-09-06 21:42:28 +00001919
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001920 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1921 WMPostNotificationName(WMNChangedName, wwin, NULL);
1922 }
7a491db6 kojima2001-02-11 02:18:26 +00001923}
1924
9d2e6ef9 scottc1998-09-29 22:36:29 +00001925/*
1926 *----------------------------------------------------------------------
9aca0d5f dan2004-10-12 01:34:32 +00001927 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00001928 * wWindowConstrainSize--
1929 * Constrains size for the client window, taking the maximal size,
1930 * window resize increments and other size hints into account.
9aca0d5f dan2004-10-12 01:34:32 +00001931 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00001932 * Returns:
9aca0d5f dan2004-10-12 01:34:32 +00001933 * The closest size to what was given that the client window can
9d2e6ef9 scottc1998-09-29 22:36:29 +00001934 * have.
9aca0d5f dan2004-10-12 01:34:32 +00001935 *
1936 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +00001937 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001938void wWindowConstrainSize(WWindow * wwin, unsigned int *nwidth, unsigned int *nheight)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001939{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001940 int width = (int)*nwidth;
1941 int height = (int)*nheight;
1942 int winc = 1;
1943 int hinc = 1;
1944 int minW = 1, minH = 1;
1945 int maxW = wwin->screen_ptr->scr_width * 2;
1946 int maxH = wwin->screen_ptr->scr_height * 2;
1947 int minAX = -1, minAY = -1;
1948 int maxAX = -1, maxAY = -1;
1949 int baseW = 0;
1950 int baseH = 0;
1951
1952 if (wwin->normal_hints) {
1953 winc = wwin->normal_hints->width_inc;
1954 hinc = wwin->normal_hints->height_inc;
1955 minW = wwin->normal_hints->min_width;
1956 minH = wwin->normal_hints->min_height;
1957 maxW = wwin->normal_hints->max_width;
1958 maxH = wwin->normal_hints->max_height;
1959 if (wwin->normal_hints->flags & PAspect) {
1960 minAX = wwin->normal_hints->min_aspect.x;
1961 minAY = wwin->normal_hints->min_aspect.y;
1962 maxAX = wwin->normal_hints->max_aspect.x;
1963 maxAY = wwin->normal_hints->max_aspect.y;
1964 }
1965
1966 baseW = wwin->normal_hints->base_width;
1967 baseH = wwin->normal_hints->base_height;
1968 }
1969
1970 if (width < minW)
1971 width = minW;
1972 if (height < minH)
1973 height = minH;
1974
1975 if (width > maxW)
1976 width = maxW;
1977 if (height > maxH)
1978 height = maxH;
1979
1980 /* aspect ratio code borrowed from olwm */
1981 if (minAX > 0) {
1982 /* adjust max aspect ratio */
1983 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1984 if (maxAX > maxAY) {
1985 height = (width * maxAY) / maxAX;
1986 if (height > maxH) {
1987 height = maxH;
1988 width = (height * maxAX) / maxAY;
1989 }
1990 } else {
1991 width = (height * maxAX) / maxAY;
1992 if (width > maxW) {
1993 width = maxW;
1994 height = (width * maxAY) / maxAX;
1995 }
1996 }
1997 }
1998
1999 /* adjust min aspect ratio */
2000 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
2001 if (minAX > minAY) {
2002 height = (width * minAY) / minAX;
2003 if (height < minH) {
2004 height = minH;
2005 width = (height * minAX) / minAY;
2006 }
2007 } else {
2008 width = (height * minAX) / minAY;
2009 if (width < minW) {
2010 width = minW;
2011 height = (width * minAY) / minAX;
2012 }
2013 }
2014 }
2015 }
2016
2017 if (baseW != 0) {
2018 width = (((width - baseW) / winc) * winc) + baseW;
2019 } else {
2020 width = (((width - minW) / winc) * winc) + minW;
2021 }
2022
2023 if (baseH != 0) {
2024 height = (((height - baseH) / hinc) * hinc) + baseH;
2025 } else {
2026 height = (((height - minH) / hinc) * hinc) + minH;
2027 }
2028
2029 /* broken stupid apps may cause preposterous values for these.. */
2030 if (width > 0)
2031 *nwidth = width;
2032 if (height > 0)
2033 *nheight = height;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002034}
2035
9d2e6ef9 scottc1998-09-29 22:36:29 +00002036void
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002037wWindowCropSize(WWindow * wwin, unsigned int maxW, unsigned int maxH, unsigned int *width, unsigned int *height)
7e1f0e1e kojima2001-06-06 18:20:23 +00002038{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002039 int baseW = 0, baseH = 0;
2040 int winc = 1, hinc = 1;
7e1f0e1e kojima2001-06-06 18:20:23 +00002041
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002042 if (wwin->normal_hints) {
2043 baseW = wwin->normal_hints->base_width;
2044 baseH = wwin->normal_hints->base_height;
9aca0d5f dan2004-10-12 01:34:32 +00002045
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002046 winc = wwin->normal_hints->width_inc;
2047 hinc = wwin->normal_hints->height_inc;
2048 }
7e1f0e1e kojima2001-06-06 18:20:23 +00002049
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002050 if (*width > maxW)
2051 *width = maxW - (maxW - baseW) % winc;
7e1f0e1e kojima2001-06-06 18:20:23 +00002052
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002053 if (*height > maxH)
2054 *height = maxH - (maxH - baseH) % hinc;
7e1f0e1e kojima2001-06-06 18:20:23 +00002055}
2056
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002057void wWindowChangeWorkspace(WWindow * wwin, int workspace)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002058{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002059 WScreen *scr = wwin->screen_ptr;
2060 WApplication *wapp;
2061 int unmap = 0;
2062
2063 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
2064 return;
2065
2066 if (workspace != scr->current_workspace) {
2067 /* Sent to other workspace. Unmap window */
2068 if ((wwin->flags.mapped
2069 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2070 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2071
2072 wapp = wApplicationOf(wwin->main_window);
2073 if (wapp) {
2074 wapp->last_workspace = workspace;
2075 }
2076 if (wwin->flags.miniaturized) {
2077 if (wwin->icon) {
2078 XUnmapWindow(dpy, wwin->icon->core->window);
2079 wwin->icon->mapped = 0;
2080 }
2081 } else {
2082 unmap = 1;
2083 wSetFocusTo(scr, NULL);
2084 }
2085 }
2086 } else {
2087 /* brought to current workspace. Map window */
2088 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2089 if (wwin->icon) {
2090 XMapWindow(dpy, wwin->icon->core->window);
2091 wwin->icon->mapped = 1;
2092 }
2093 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2094 wWindowMap(wwin);
2095 }
2096 }
2097 if (!IS_OMNIPRESENT(wwin)) {
2098 int oldWorkspace = wwin->frame->workspace;
2099
2100 wwin->frame->workspace = workspace;
2101
2102 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
2103 }
2104
2105 if (unmap) {
2106 wWindowUnmap(wwin);
2107 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002108}
2109
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002110void wWindowSynthConfigureNotify(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002111{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002112 XEvent sevent;
2113
2114 sevent.type = ConfigureNotify;
2115 sevent.xconfigure.display = dpy;
2116 sevent.xconfigure.event = wwin->client_win;
2117 sevent.xconfigure.window = wwin->client_win;
2118
2119 sevent.xconfigure.x = wwin->client.x;
2120 sevent.xconfigure.y = wwin->client.y;
2121 sevent.xconfigure.width = wwin->client.width;
2122 sevent.xconfigure.height = wwin->client.height;
2123
2124 sevent.xconfigure.border_width = wwin->old_border_width;
2125 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2126 sevent.xconfigure.above = wwin->frame->titlebar->window;
2127 else
2128 sevent.xconfigure.above = None;
2129
2130 sevent.xconfigure.override_redirect = False;
2131 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2132 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002133}
2134
9d2e6ef9 scottc1998-09-29 22:36:29 +00002135/*
2136 *----------------------------------------------------------------------
2137 * wWindowConfigure--
9aca0d5f dan2004-10-12 01:34:32 +00002138 * Configures the frame, decorations and client window to the
9d2e6ef9 scottc1998-09-29 22:36:29 +00002139 * specified geometry. The geometry is not checked for validity,
9aca0d5f dan2004-10-12 01:34:32 +00002140 * wWindowConstrainSize() must be used for that.
9d2e6ef9 scottc1998-09-29 22:36:29 +00002141 * The size parameters are for the client window, but the position is
2142 * for the frame.
2143 * The client window receives a ConfigureNotify event, according
2144 * to what ICCCM says.
9aca0d5f dan2004-10-12 01:34:32 +00002145 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00002146 * Returns:
2147 * None
9aca0d5f dan2004-10-12 01:34:32 +00002148 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00002149 * Side effects:
2150 * Window size and position are changed and client window receives
2151 * a ConfigureNotify event.
9aca0d5f dan2004-10-12 01:34:32 +00002152 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +00002153 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002154void wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002155WWindow *wwin;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002156int req_x, req_y; /* new position of the frame */
2157int req_width, req_height; /* new size of the client */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002158{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002159 int synth_notify = False;
2160 int resize;
2161
2162 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2163 /*
2164 * if the window is being moved but not resized then
2165 * send a synthetic ConfigureNotify
2166 */
2167 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2168 synth_notify = True;
2169 }
2170
2171 if (WFLAGP(wwin, dont_move_off))
2172 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2173 if (resize) {
2174 if (req_width < MIN_WINDOW_SIZE)
2175 req_width = MIN_WINDOW_SIZE;
2176 if (req_height < MIN_WINDOW_SIZE)
2177 req_height = MIN_WINDOW_SIZE;
2178
2179 /* If growing, resize inner part before frame,
2180 * if shrinking, resize frame before.
2181 * This will prevent the frame (that can have a different color)
2182 * to be exposed, causing flicker */
2183 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2184 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2185
2186 if (wwin->flags.shaded) {
2187 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2188 wwin->old_geometry.height = req_height;
2189 } else {
2190 int h;
2191
2192 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2193
2194 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2195 }
2196
2197 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2198 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2199
2200 wwin->client.x = req_x;
2201 wwin->client.y = req_y + wwin->frame->top_width;
2202 wwin->client.width = req_width;
2203 wwin->client.height = req_height;
2204 } else {
2205 wwin->client.x = req_x;
2206 wwin->client.y = req_y + wwin->frame->top_width;
2207
2208 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2209 }
2210 wwin->frame_x = req_x;
2211 wwin->frame_y = req_y;
2212 if (HAS_BORDER(wwin)) {
2213 wwin->client.x += FRAME_BORDER_WIDTH;
2214 wwin->client.y += FRAME_BORDER_WIDTH;
2215 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002216#ifdef SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002217 if (wShapeSupported && wwin->flags.shaped && resize) {
2218 wWindowSetShape(wwin);
2219 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002220#endif
9af1c6c4 dan1998-10-21 14:43:47 +00002221
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002222 if (synth_notify)
2223 wWindowSynthConfigureNotify(wwin);
2224 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002225}
2226
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002227void wWindowMove(wwin, req_x, req_y)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002228WWindow *wwin;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002229int req_x, req_y; /* new position of the frame */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002230{
2231#ifdef CONFIGURE_WINDOW_WHILE_MOVING
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002232 int synth_notify = False;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002233
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002234 /* Send a synthetic ConfigureNotify event for every window movement. */
2235 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2236 synth_notify = True;
2237 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002238#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002239 /* A single synthetic ConfigureNotify event is sent at the end of
2240 * a completed (opaque) movement in moveres.c */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002241#endif
2242
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002243 if (WFLAGP(wwin, dont_move_off))
2244 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2245 wwin->frame->core->width, wwin->frame->core->height);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002246
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002247 wwin->client.x = req_x;
2248 wwin->client.y = req_y + wwin->frame->top_width;
2249 if (HAS_BORDER(wwin)) {
2250 wwin->client.x += FRAME_BORDER_WIDTH;
2251 wwin->client.y += FRAME_BORDER_WIDTH;
2252 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002253
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002254 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002255
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002256 wwin->frame_x = req_x;
2257 wwin->frame_y = req_y;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002258
2259#ifdef CONFIGURE_WINDOW_WHILE_MOVING
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002260 if (synth_notify)
2261 wWindowSynthConfigureNotify(wwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002262#endif
2263}
2264
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002265void wWindowUpdateButtonImages(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002266{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002267 WScreen *scr = wwin->screen_ptr;
2268 Pixmap pixmap, mask;
2269 WFrameWindow *fwin = wwin->frame;
2270
2271 if (!HAS_TITLEBAR(wwin))
2272 return;
2273
2274 /* miniaturize button */
2275
2276 if (!WFLAGP(wwin, no_miniaturize_button)) {
2277 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2278 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2279
2280 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2281 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2282 } else {
2283 mask = None;
2284 }
2285
2286 if (fwin->lbutton_image
2287 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2288 wPixmapDestroy(fwin->lbutton_image);
2289 fwin->lbutton_image = NULL;
2290 }
2291
2292 if (!fwin->lbutton_image) {
2293 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2294 fwin->lbutton_image->client_owned = 1;
2295 fwin->lbutton_image->client_owned_mask = 1;
2296 }
2297 } else {
2298 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2299 wPixmapDestroy(fwin->lbutton_image);
2300 }
2301 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2302 }
2303 }
c28f3b2c id1999-05-20 23:30:53 +00002304#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002305 if (!WFLAGP(wwin, no_language_button)) {
2306 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2307 wPixmapDestroy(fwin->languagebutton_image);
2308 }
2309 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2310 }
c28f3b2c id1999-05-20 23:30:53 +00002311#endif
3c046182 kojima1999-09-16 02:58:56 +00002312
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002313 /* close button */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002314
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002315 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
3c046182 kojima1999-09-16 02:58:56 +00002316#define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2317
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002318 if (!WFLAGP(wwin, no_close_button)) {
2319 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2320 pixmap = wwin->wm_gnustep_attr->close_pixmap;
9aca0d5f dan2004-10-12 01:34:32 +00002321
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002322 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2323 mask = wwin->wm_gnustep_attr->close_mask;
2324 else
2325 mask = None;
3c046182 kojima1999-09-16 02:58:56 +00002326
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002327 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2328 || fwin->rbutton_image->mask != mask)) {
2329 wPixmapDestroy(fwin->rbutton_image);
2330 fwin->rbutton_image = NULL;
2331 }
9aca0d5f dan2004-10-12 01:34:32 +00002332
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002333 if (!fwin->rbutton_image) {
2334 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2335 fwin->rbutton_image->client_owned = 1;
2336 fwin->rbutton_image->client_owned_mask = 1;
2337 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002338
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002339 } else if (WFLAGP(wwin, kill_close)) {
3c046182 kojima1999-09-16 02:58:56 +00002340
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002341 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2342 wPixmapDestroy(fwin->rbutton_image);
3c046182 kojima1999-09-16 02:58:56 +00002343
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002344 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
9aca0d5f dan2004-10-12 01:34:32 +00002345
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002346 } else if (MGFLAGP(wwin, broken_close)) {
3c046182 kojima1999-09-16 02:58:56 +00002347
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002348 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2349 wPixmapDestroy(fwin->rbutton_image);
3c046182 kojima1999-09-16 02:58:56 +00002350
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002351 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
3c046182 kojima1999-09-16 02:58:56 +00002352
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002353 } else {
3c046182 kojima1999-09-16 02:58:56 +00002354
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002355 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2356 wPixmapDestroy(fwin->rbutton_image);
3c046182 kojima1999-09-16 02:58:56 +00002357
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002358 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2359 }
2360 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002361
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002362 /* force buttons to be redrawn */
2363 fwin->flags.need_texture_change = 1;
2364 wFrameWindowPaint(fwin);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002365}
2366
9d2e6ef9 scottc1998-09-29 22:36:29 +00002367/*
2368 *---------------------------------------------------------------------------
2369 * wWindowConfigureBorders--
0261c326 dan1999-01-06 15:22:33 +00002370 * Update window border configuration according to attribute flags.
9aca0d5f dan2004-10-12 01:34:32 +00002371 *
2372 *---------------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +00002373 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002374void wWindowConfigureBorders(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002375{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002376 if (wwin->frame) {
2377 int flags;
2378 int newy, oldh;
e7495baf dan1999-02-17 11:06:40 +00002379
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002380 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
9aca0d5f dan2004-10-12 01:34:32 +00002381
a43f369e id1999-05-29 15:45:19 +00002382#ifdef XKB_BUTTON_HINT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002383 flags |= WFF_LANGUAGE_BUTTON;
a43f369e id1999-05-29 15:45:19 +00002384#endif
9aca0d5f dan2004-10-12 01:34:32 +00002385
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002386 if (HAS_TITLEBAR(wwin))
2387 flags |= WFF_TITLEBAR;
2388 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2389 flags |= WFF_RESIZEBAR;
2390 if (HAS_BORDER(wwin))
2391 flags |= WFF_BORDER;
2392 if (wwin->flags.shaded)
2393 flags |= WFF_IS_SHADED;
2394
2395 oldh = wwin->frame->top_width;
2396 wFrameWindowUpdateBorders(wwin->frame, flags);
2397 if (oldh != wwin->frame->top_width) {
2398 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2399
2400 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2401 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2402 }
2403
2404 flags = 0;
2405 if (!WFLAGP(wwin, no_miniaturize_button)
2406 && wwin->frame->flags.hide_left_button)
2407 flags |= WFF_LEFT_BUTTON;
9af1c6c4 dan1998-10-21 14:43:47 +00002408
a43f369e id1999-05-29 15:45:19 +00002409#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002410 if (!WFLAGP(wwin, no_language_button)
2411 && wwin->frame->flags.hide_language_button) {
2412 flags |= WFF_LANGUAGE_BUTTON;
2413 }
a43f369e id1999-05-29 15:45:19 +00002414#endif
2415
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002416 if (!WFLAGP(wwin, no_close_button)
2417 && wwin->frame->flags.hide_right_button)
2418 flags |= WFF_RIGHT_BUTTON;
9af1c6c4 dan1998-10-21 14:43:47 +00002419
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002420 if (flags != 0) {
2421 wWindowUpdateButtonImages(wwin);
2422 wFrameWindowShowButton(wwin->frame, flags);
2423 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002424
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002425 flags = 0;
2426 if (WFLAGP(wwin, no_miniaturize_button)
2427 && !wwin->frame->flags.hide_left_button)
2428 flags |= WFF_LEFT_BUTTON;
9af1c6c4 dan1998-10-21 14:43:47 +00002429
a43f369e id1999-05-29 15:45:19 +00002430#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002431 if (WFLAGP(wwin, no_language_button)
2432 && !wwin->frame->flags.hide_language_button)
2433 flags |= WFF_LANGUAGE_BUTTON;
a43f369e id1999-05-29 15:45:19 +00002434#endif
2435
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002436 if (WFLAGP(wwin, no_close_button)
2437 && !wwin->frame->flags.hide_right_button)
2438 flags |= WFF_RIGHT_BUTTON;
9af1c6c4 dan1998-10-21 14:43:47 +00002439
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002440 if (flags != 0)
2441 wFrameWindowHideButton(wwin->frame, flags);
9af1c6c4 dan1998-10-21 14:43:47 +00002442
2443#ifdef SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002444 if (wShapeSupported && wwin->flags.shaped) {
2445 wWindowSetShape(wwin);
2446 }
9af1c6c4 dan1998-10-21 14:43:47 +00002447#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002448 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002449}
2450
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002451void wWindowSaveState(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002452{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002453 CARD32 data[10];
2454 int i;
2455
2456 memset(data, 0, sizeof(CARD32) * 10);
2457 data[0] = wwin->frame->workspace;
2458 data[1] = wwin->flags.miniaturized;
2459 data[2] = wwin->flags.shaded;
2460 data[3] = wwin->flags.hidden;
2461 data[4] = wwin->flags.maximized;
2462 if (wwin->flags.maximized == 0) {
2463 data[5] = wwin->frame_x;
2464 data[6] = wwin->frame_y;
2465 data[7] = wwin->frame->core->width;
2466 data[8] = wwin->frame->core->height;
2467 } else {
2468 data[5] = wwin->old_geometry.x;
2469 data[6] = wwin->old_geometry.y;
2470 data[7] = wwin->old_geometry.width;
2471 data[8] = wwin->old_geometry.height;
2472 }
2473
2474 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2475 if (wwin->screen_ptr->shortcutWindows[i] &&
2476 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2477 data[9] |= 1 << i;
2478 }
2479 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2480 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002481}
2482
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002483static int getSavedState(Window window, WSavedState ** state)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002484{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002485 Atom type_ret;
2486 int fmt_ret;
2487 unsigned long nitems_ret;
2488 unsigned long bytes_after_ret;
2489 CARD32 *data;
2490
2491 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2492 True, _XA_WINDOWMAKER_STATE,
2493 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2494 (unsigned char **)&data) != Success || !data)
2495 return 0;
2496
2497 *state = wmalloc(sizeof(WSavedState));
2498
2499 (*state)->workspace = data[0];
2500 (*state)->miniaturized = data[1];
2501 (*state)->shaded = data[2];
2502 (*state)->hidden = data[3];
2503 (*state)->maximized = data[4];
2504 (*state)->x = data[5];
2505 (*state)->y = data[6];
2506 (*state)->w = data[7];
2507 (*state)->h = data[8];
2508 (*state)->window_shortcuts = data[9];
2509
2510 XFree(data);
2511
2512 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2513 return 1;
2514 else
2515 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002516}
2517
9d2e6ef9 scottc1998-09-29 22:36:29 +00002518#ifdef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002519void wWindowClearShape(WWindow * wwin)
9af1c6c4 dan1998-10-21 14:43:47 +00002520{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002521 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2522 0, wwin->frame->top_width, None, ShapeSet);
2523 XFlush(dpy);
9af1c6c4 dan1998-10-21 14:43:47 +00002524}
2525
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002526void wWindowSetShape(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002527{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002528 XRectangle rect[2];
2529 int count;
9af1c6c4 dan1998-10-21 14:43:47 +00002530#ifdef OPTIMIZE_SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002531 XRectangle *rects;
2532 XRectangle *urec;
2533 int ordering;
2534
2535 /* only shape is the client's */
2536 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2537 goto alt_code;
2538 }
2539
2540 /* Get array of rectangles describing the shape mask */
2541 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2542 if (!rects) {
2543 goto alt_code;
2544 }
2545
2546 urec = malloc(sizeof(XRectangle) * (count + 2));
2547 if (!urec) {
2548 XFree(rects);
2549 goto alt_code;
2550 }
2551
2552 /* insert our decoration rectangles in the rect list */
2553 memcpy(urec, rects, sizeof(XRectangle) * count);
2554 XFree(rects);
2555
2556 if (HAS_TITLEBAR(wwin)) {
2557 urec[count].x = -1;
2558 urec[count].y = -1 - wwin->frame->top_width;
2559 urec[count].width = wwin->frame->core->width + 2;
2560 urec[count].height = wwin->frame->top_width + 1;
2561 count++;
2562 }
2563 if (HAS_RESIZEBAR(wwin)) {
2564 urec[count].x = -1;
2565 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2566 urec[count].width = wwin->frame->core->width + 2;
2567 urec[count].height = wwin->frame->bottom_width + 1;
2568 count++;
2569 }
2570
2571 /* shape our frame window */
2572 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2573 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2574 XFlush(dpy);
2575 wfree(urec);
2576 return;
2577
2578 alt_code:
2579#endif /* OPTIMIZE_SHAPE */
2580 count = 0;
2581 if (HAS_TITLEBAR(wwin)) {
2582 rect[count].x = -1;
2583 rect[count].y = -1;
2584 rect[count].width = wwin->frame->core->width + 2;
2585 rect[count].height = wwin->frame->top_width + 1;
2586 count++;
2587 }
2588 if (HAS_RESIZEBAR(wwin)) {
2589 rect[count].x = -1;
2590 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2591 rect[count].width = wwin->frame->core->width + 2;
2592 rect[count].height = wwin->frame->bottom_width + 1;
2593 count++;
2594 }
2595 if (count > 0) {
2596 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2597 0, 0, rect, count, ShapeSet, Unsorted);
2598 }
2599 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2600 0, wwin->frame->top_width, wwin->client_win,
2601 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2602 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002603}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002604#endif /* SHAPE */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002605
2606/* ====================================================================== */
2607
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002608static FocusMode getFocusMode(WWindow * wwin)
9af1c6c4 dan1998-10-21 14:43:47 +00002609{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002610 FocusMode mode;
2611
2612 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2613 if (wwin->wm_hints->input == True) {
2614 if (wwin->protocols.TAKE_FOCUS)
2615 mode = WFM_LOCALLY_ACTIVE;
2616 else
2617 mode = WFM_PASSIVE;
2618 } else {
2619 if (wwin->protocols.TAKE_FOCUS)
2620 mode = WFM_GLOBALLY_ACTIVE;
2621 else
2622 mode = WFM_NO_INPUT;
2623 }
2624 } else {
2625 mode = WFM_PASSIVE;
2626 }
2627 return mode;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002628}
2629
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002630void wWindowSetKeyGrabs(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002631{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002632 int i;
2633 WShortKey *key;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002634
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002635 for (i = 0; i < WKBD_LAST; i++) {
2636 key = &wKeyBindings[i];
9af1c6c4 dan1998-10-21 14:43:47 +00002637
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002638 if (key->keycode == 0)
2639 continue;
2640 if (key->modifier != AnyModifier) {
2641 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2642 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002643#ifdef NUMLOCK_HACK
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002644 /* Also grab all modifier combinations possible that include,
2645 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2646 * work even if the NumLock/ScrollLock key is on.
2647 */
2648 wHackedGrabKey(key->keycode, key->modifier,
2649 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002650#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002651 }
2652 XGrabKey(dpy, key->keycode, key->modifier,
2653 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2654 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002655
0261c326 dan1999-01-06 15:22:33 +00002656#ifndef LITE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002657 wRootMenuBindShortcuts(wwin->frame->core->window);
0261c326 dan1999-01-06 15:22:33 +00002658#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +00002659}
2660
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002661void wWindowResetMouseGrabs(WWindow * wwin)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002662{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002663 /* Mouse grabs can't be done on the client window because of
2664 * ICCCM and because clients that try to do the same will crash.
2665 *
2666 * But there is a problem wich makes tbar buttons of unfocused
2667 * windows not usable as the click goes to the frame window instead
2668 * of the button itself. Must figure a way to fix that.
2669 */
2670
2671 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2672
2673 if (!WFLAGP(wwin, no_bind_mouse)) {
2674 /* grabs for Meta+drag */
2675 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2676 True, ButtonPressMask | ButtonReleaseMask,
2677 GrabModeSync, GrabModeAsync, None, None);
2678 }
2679
2680 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2681 && !wwin->flags.is_gnustep) {
2682 /* the passive grabs to focus the window */
2683 /* if (wPreferences.focus_mode == WKF_CLICK) */
2684 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2685 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2686 }
2687 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002688}
2689
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002690void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002691{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002692 if (attr->flags & GSExtraFlagsAttr) {
2693 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2694 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2695 wWindowUpdateButtonImages(wwin);
2696 }
2697 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002698}
2699
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002700WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
9af1c6c4 dan1998-10-21 14:43:47 +00002701{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002702 WWindowState *wstate;
9af1c6c4 dan1998-10-21 14:43:47 +00002703
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002704 wstate = malloc(sizeof(WWindowState));
2705 if (!wstate)
2706 return 0;
9af1c6c4 dan1998-10-21 14:43:47 +00002707
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002708 memset(wstate, 0, sizeof(WWindowState));
2709 wstate->pid = pid;
2710 if (instance)
2711 wstate->instance = wstrdup(instance);
2712 if (class)
2713 wstate->class = wstrdup(class);
2714 if (command)
2715 wstate->command = wstrdup(command);
2716 wstate->state = state;
9af1c6c4 dan1998-10-21 14:43:47 +00002717
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002718 wstate->next = windowState;
2719 windowState = wstate;
9af1c6c4 dan1998-10-21 14:43:47 +00002720
2721#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002722 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
9af1c6c4 dan1998-10-21 14:43:47 +00002723#endif
2724
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002725 return wstate;
9af1c6c4 dan1998-10-21 14:43:47 +00002726}
2727
9af1c6c4 dan1998-10-21 14:43:47 +00002728#define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2729
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002730WMagicNumber wWindowGetSavedState(Window win)
9af1c6c4 dan1998-10-21 14:43:47 +00002731{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002732 char *instance, *class, *command = NULL;
2733 WWindowState *wstate = windowState;
2734
2735 if (!wstate)
2736 return NULL;
2737
2738 command = GetCommandForWindow(win);
2739 if (!command)
2740 return NULL;
2741
2742 if (PropGetWMClass(win, &class, &instance)) {
2743 while (wstate) {
2744 if (SAME(instance, wstate->instance) &&
2745 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2746 break;
2747 }
2748 wstate = wstate->next;
2749 }
2750 } else {
2751 wstate = NULL;
2752 }
9af1c6c4 dan1998-10-21 14:43:47 +00002753
2754#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002755 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
9af1c6c4 dan1998-10-21 14:43:47 +00002756#endif
2757
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002758 if (command)
2759 wfree(command);
2760 if (instance)
2761 XFree(instance);
2762 if (class)
2763 XFree(class);
9af1c6c4 dan1998-10-21 14:43:47 +00002764
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002765 return wstate;
9af1c6c4 dan1998-10-21 14:43:47 +00002766}
2767
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002768void wWindowDeleteSavedState(WMagicNumber id)
9af1c6c4 dan1998-10-21 14:43:47 +00002769{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002770 WWindowState *tmp, *wstate = (WWindowState *) id;
9af1c6c4 dan1998-10-21 14:43:47 +00002771
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002772 if (!wstate || !windowState)
2773 return;
9af1c6c4 dan1998-10-21 14:43:47 +00002774
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002775 tmp = windowState;
2776 if (tmp == wstate) {
2777 windowState = wstate->next;
9af1c6c4 dan1998-10-21 14:43:47 +00002778#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002779 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2780 wstate, wstate->instance, wstate->class, wstate->command);
9af1c6c4 dan1998-10-21 14:43:47 +00002781#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002782 if (wstate->instance)
2783 wfree(wstate->instance);
2784 if (wstate->class)
2785 wfree(wstate->class);
2786 if (wstate->command)
2787 wfree(wstate->command);
2788 wfree(wstate->state);
2789 wfree(wstate);
2790 } else {
2791 while (tmp->next) {
2792 if (tmp->next == wstate) {
2793 tmp->next = wstate->next;
9af1c6c4 dan1998-10-21 14:43:47 +00002794#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002795 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2796 wstate, wstate->instance, wstate->class, wstate->command);
9af1c6c4 dan1998-10-21 14:43:47 +00002797#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002798 if (wstate->instance)
2799 wfree(wstate->instance);
2800 if (wstate->class)
2801 wfree(wstate->class);
2802 if (wstate->command)
2803 wfree(wstate->command);
2804 wfree(wstate->state);
2805 wfree(wstate);
2806 break;
2807 }
2808 tmp = tmp->next;
2809 }
2810 }
9af1c6c4 dan1998-10-21 14:43:47 +00002811}
2812
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002813void wWindowDeleteSavedStatesForPID(pid_t pid)
9af1c6c4 dan1998-10-21 14:43:47 +00002814{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002815 WWindowState *tmp, *wstate;
9af1c6c4 dan1998-10-21 14:43:47 +00002816
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002817 if (!windowState)
2818 return;
9af1c6c4 dan1998-10-21 14:43:47 +00002819
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002820 tmp = windowState;
2821 if (tmp->pid == pid) {
2822 wstate = windowState;
2823 windowState = tmp->next;
9af1c6c4 dan1998-10-21 14:43:47 +00002824#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002825 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2826 wstate, wstate->instance, wstate->class, wstate->command);
9af1c6c4 dan1998-10-21 14:43:47 +00002827#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002828 if (wstate->instance)
2829 wfree(wstate->instance);
2830 if (wstate->class)
2831 wfree(wstate->class);
2832 if (wstate->command)
2833 wfree(wstate->command);
2834 wfree(wstate->state);
2835 wfree(wstate);
2836 } else {
2837 while (tmp->next) {
2838 if (tmp->next->pid == pid) {
2839 wstate = tmp->next;
2840 tmp->next = wstate->next;
9af1c6c4 dan1998-10-21 14:43:47 +00002841#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002842 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2843 wstate, wstate->instance, wstate->class, wstate->command);
9af1c6c4 dan1998-10-21 14:43:47 +00002844#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002845 if (wstate->instance)
2846 wfree(wstate->instance);
2847 if (wstate->class)
2848 wfree(wstate->class);
2849 if (wstate->command)
2850 wfree(wstate->command);
2851 wfree(wstate->state);
2852 wfree(wstate);
2853 break;
2854 }
2855 tmp = tmp->next;
2856 }
2857 }
9af1c6c4 dan1998-10-21 14:43:47 +00002858}
9d2e6ef9 scottc1998-09-29 22:36:29 +00002859
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002860void wWindowSetOmnipresent(WWindow * wwin, Bool flag)
5ef342d9 kojima2001-09-06 21:42:28 +00002861{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002862 if (wwin->flags.omnipresent == flag)
2863 return;
27a396d7 dan2003-06-06 04:11:00 +00002864
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002865 wwin->flags.omnipresent = flag;
2866 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
5ef342d9 kojima2001-09-06 21:42:28 +00002867}
2868
9d2e6ef9 scottc1998-09-29 22:36:29 +00002869/* ====================================================================== */
2870
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002871static void resizebarMouseDown(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002872{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002873 WWindow *wwin = data;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002874
2875#ifndef NUMLOCK_HACK
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002876 if ((event->xbutton.state & ValidModMask)
2877 != (event->xbutton.state & ~LockMask)) {
2878 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2879 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2880 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002881#endif
9aca0d5f dan2004-10-12 01:34:32 +00002882
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002883 event->xbutton.state &= ValidModMask;
9aca0d5f dan2004-10-12 01:34:32 +00002884
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002885 CloseWindowMenu(wwin->screen_ptr);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002886
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002887 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2888 && !WFLAGP(wwin, no_focusable)) {
2889 wSetFocusTo(wwin->screen_ptr, wwin);
2890 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002891
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002892 if (event->xbutton.button == Button1)
2893 wRaiseFrame(wwin->frame->core);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002894
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002895 if (event->xbutton.window != wwin->frame->resizebar->window) {
2896 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2897 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2898 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
9d2e6ef9 scottc1998-09-29 22:36:29 +00002899#ifdef DEBUG0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002900 wwarning("pointer grab failed for window move");
9d2e6ef9 scottc1998-09-29 22:36:29 +00002901#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002902 return;
2903 }
2904 }
2905
2906 if (event->xbutton.state & MOD_MASK) {
2907 /* move the window */
2908 wMouseMoveWindow(wwin, event);
2909 XUngrabPointer(dpy, CurrentTime);
2910 } else {
2911 wMouseResizeWindow(wwin, event);
2912 XUngrabPointer(dpy, CurrentTime);
2913 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002914}
2915
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002916static void titlebarDblClick(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002917{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002918 WWindow *wwin = data;
2919
2920 event->xbutton.state &= ValidModMask;
2921
2922 if (event->xbutton.button == Button1) {
2923 if (event->xbutton.state == 0) {
2924 if (!WFLAGP(wwin, no_shadeable)) {
2925 /* shade window */
2926 if (wwin->flags.shaded)
2927 wUnshadeWindow(wwin);
2928 else
2929 wShadeWindow(wwin);
2930 }
2931 } else {
2932 int dir = 0;
2933
2934 if (event->xbutton.state & ControlMask)
2935 dir |= MAX_VERTICAL;
2936
2937 if (event->xbutton.state & ShiftMask) {
2938 dir |= MAX_HORIZONTAL;
2939 if (!(event->xbutton.state & ControlMask))
2940 wSelectWindow(wwin, !wwin->flags.selected);
2941 }
2942
2943 /* maximize window */
2944 if (dir != 0 && IS_RESIZABLE(wwin)) {
2945 int ndir = dir ^ wwin->flags.maximized;
2946
2947 if (ndir != 0) {
2948 wMaximizeWindow(wwin, ndir);
2949 } else {
2950 wUnmaximizeWindow(wwin);
2951 }
2952 }
2953 }
2954 } else if (event->xbutton.button == Button3) {
2955 if (event->xbutton.state & MOD_MASK) {
2956 wHideOtherApplications(wwin);
2957 }
2958 } else if (event->xbutton.button == Button2) {
2959 wSelectWindow(wwin, !wwin->flags.selected);
2960 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2961 wShadeWindow(wwin);
2962 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2963 wUnshadeWindow(wwin);
2964 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002965}
2966
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002967static void frameMouseDown(WObjDescriptor * desc, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002968{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002969 WWindow *wwin = desc->parent;
2970
2971 event->xbutton.state &= ValidModMask;
2972
2973 CloseWindowMenu(wwin->screen_ptr);
2974
2975 if ( /*wPreferences.focus_mode==WKF_CLICK
2976 && */ !(event->xbutton.state & ControlMask)
2977 && !WFLAGP(wwin, no_focusable)) {
2978 wSetFocusTo(wwin->screen_ptr, wwin);
2979 }
2980 if (event->xbutton.button == Button1) {
2981 wRaiseFrame(wwin->frame->core);
2982 }
2983
2984 if (event->xbutton.state & MOD_MASK) {
2985 /* move the window */
2986 if (XGrabPointer(dpy, wwin->client_win, False,
2987 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2988 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
9d2e6ef9 scottc1998-09-29 22:36:29 +00002989#ifdef DEBUG0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002990 wwarning("pointer grab failed for window move");
9d2e6ef9 scottc1998-09-29 22:36:29 +00002991#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002992 return;
2993 }
2994 if (event->xbutton.button == Button3)
2995 wMouseResizeWindow(wwin, event);
2996 else if (event->xbutton.button == Button1 || event->xbutton.button == Button2)
2997 wMouseMoveWindow(wwin, event);
2998 XUngrabPointer(dpy, CurrentTime);
2999 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00003000}
3001
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003002static void titlebarMouseDown(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00003003{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003004 WWindow *wwin = (WWindow *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +00003005
3006#ifndef NUMLOCK_HACK
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003007 if ((event->xbutton.state & ValidModMask)
3008 != (event->xbutton.state & ~LockMask)) {
3009 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
3010 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3011 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00003012#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003013 event->xbutton.state &= ValidModMask;
3014
3015 CloseWindowMenu(wwin->screen_ptr);
3016
3017 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
3018 && !WFLAGP(wwin, no_focusable)) {
3019 wSetFocusTo(wwin->screen_ptr, wwin);
3020 }
3021
3022 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
3023
3024 if (event->xbutton.button == Button1) {
3025 if (event->xbutton.state & MOD_MASK) {
3026 wLowerFrame(wwin->frame->core);
3027 } else {
3028 wRaiseFrame(wwin->frame->core);
3029 }
3030 }
3031 if ((event->xbutton.state & ShiftMask)
3032 && !(event->xbutton.state & ControlMask)) {
3033 wSelectWindow(wwin, !wwin->flags.selected);
3034 return;
3035 }
3036 if (event->xbutton.window != wwin->frame->titlebar->window
3037 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3038 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3039 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
9007a657 dan1998-11-23 11:32:19 +00003040#ifdef DEBUG0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003041 wwarning("pointer grab failed for window move");
9007a657 dan1998-11-23 11:32:19 +00003042#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003043 return;
3044 }
3045
3046 /* move the window */
3047 wMouseMoveWindow(wwin, event);
3048
3049 XUngrabPointer(dpy, CurrentTime);
3050 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
3051 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
3052 WObjDescriptor *desc;
3053
3054 if (event->xbutton.window != wwin->frame->titlebar->window
3055 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3056 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3057 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
9007a657 dan1998-11-23 11:32:19 +00003058#ifdef DEBUG0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003059 wwarning("pointer grab failed for window move");
9007a657 dan1998-11-23 11:32:19 +00003060#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003061 return;
3062 }
9007a657 dan1998-11-23 11:32:19 +00003063
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003064 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
9d2e6ef9 scottc1998-09-29 22:36:29 +00003065
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003066 /* allow drag select */
3067 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3068 event->xany.send_event = True;
3069 (*desc->handle_mousedown) (desc, event);
9aca0d5f dan2004-10-12 01:34:32 +00003070
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003071 XUngrabPointer(dpy, CurrentTime);
3072 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00003073}
3074
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003075static void windowCloseClick(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00003076{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003077 WWindow *wwin = data;
9d2e6ef9 scottc1998-09-29 22:36:29 +00003078
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003079 event->xbutton.state &= ValidModMask;
9d2e6ef9 scottc1998-09-29 22:36:29 +00003080
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003081 CloseWindowMenu(wwin->screen_ptr);
9d2e6ef9 scottc1998-09-29 22:36:29 +00003082
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003083 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3084 return;
416e3a82 dan1999-01-25 19:06:50 +00003085
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003086 /* if control-click, kill the client */
3087 if (event->xbutton.state & ControlMask) {
3088 wClientKill(wwin);
3089 } else {
3090 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3091 /* send delete message */
3092 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3093 }
3094 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00003095}
3096
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003097static void windowCloseDblClick(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00003098{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003099 WWindow *wwin = data;
9d2e6ef9 scottc1998-09-29 22:36:29 +00003100
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003101 CloseWindowMenu(wwin->screen_ptr);
9d2e6ef9 scottc1998-09-29 22:36:29 +00003102
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003103 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3104 return;
416e3a82 dan1999-01-25 19:06:50 +00003105
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003106 /* send delete message */
3107 if (wwin->protocols.DELETE_WINDOW) {
3108 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3109 } else {
3110 wClientKill(wwin);
3111 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00003112}
3113
c28f3b2c id1999-05-20 23:30:53 +00003114#ifdef XKB_BUTTON_HINT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003115static void windowLanguageClick(WCoreWindow * sender, void *data, XEvent * event)
c28f3b2c id1999-05-20 23:30:53 +00003116{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003117 WWindow *wwin = data;
3118 WFrameWindow *fwin = wwin->frame;
3119 WScreen *scr = fwin->screen_ptr;
3120 XkbStateRec staterec;
3121 int tl;
3122
3123 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3124 return;
3125 tl = wwin->frame->languagemode;
3126 wwin->frame->languagemode = wwin->frame->last_languagemode;
3127 wwin->frame->last_languagemode = tl;
3128 wSetFocusTo(scr, wwin);
3129 wwin->frame->languagebutton_image =
3130 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3131 wFrameWindowUpdateLanguageButton(wwin->frame);
3132 if (event->xbutton.button == Button3)
3133 return;
3134 wRaiseFrame(fwin->core);
c28f3b2c id1999-05-20 23:30:53 +00003135}
3136#endif
3137
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003138static void windowIconifyClick(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00003139{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003140 WWindow *wwin = data;
3141
3142 event->xbutton.state &= ValidModMask;
3143
3144 CloseWindowMenu(wwin->screen_ptr);
6830b057 dan2004-10-12 21:28:27 +00003145
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003146 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3147 return;
3148
3149 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3150 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3151 } else {
3152 WApplication *wapp;
3153 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3154
3155 wapp = wApplicationOf(wwin->main_window);
3156 if (wapp && !WFLAGP(wwin, no_appicon))
3157 wHideApplication(wapp);
3158 } else if (event->xbutton.state == 0) {
3159 wIconifyWindow(wwin);
3160 }
3161 }
3162}