Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / actions.c
blob8a21e37842efa611b38290d420c40009e8445b49
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
33 #include "WindowMaker.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "colormap.h"
39 #include "application.h"
40 #include "actions.h"
41 #include "stacking.h"
42 #include "appicon.h"
43 #include "dock.h"
44 #include "appmenu.h"
45 #include "winspector.h"
46 #include "workspace.h"
47 #include "xinerama.h"
48 #include "usermenu.h"
49 #include "placement.h"
50 #include "misc.h"
52 /****** Global Variables ******/
54 int ignore_wks_change = 0;
55 extern Time LastTimestamp;
56 extern Time LastFocusChange;
57 extern WPreferences wPreferences;
58 extern Atom _XA_WM_TAKE_FOCUS;
60 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
61 unsigned int *new_width, unsigned int *new_height);
62 static void save_old_geometry(WWindow *wwin, int directions);
64 /******* Local Variables *******/
65 static struct {
66 int steps;
67 int delay;
68 } shadePars[5] = {
70 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
71 SHADE_STEPS_F, SHADE_DELAY_F}, {
72 SHADE_STEPS_M, SHADE_DELAY_M}, {
73 SHADE_STEPS_S, SHADE_DELAY_S}, {
74 SHADE_STEPS_US, SHADE_DELAY_US}};
76 #define UNSHADE 0
77 #define SHADE 1
78 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
79 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
81 static int compareTimes(Time t1, Time t2)
83 Time diff;
84 if (t1 == t2)
85 return 0;
86 diff = t1 - t2;
87 return (diff < 60000) ? 1 : -1;
90 #ifdef ANIMATIONS
91 static void shade_animate(WWindow *wwin, Bool what);
92 #else
93 static void shade_animate(WWindow *wwin, Bool what) { }
94 #endif
97 *----------------------------------------------------------------------
98 * wSetFocusTo--
99 * Changes the window focus to the one passed as argument.
100 * If the window to focus is not already focused, it will be brought
101 * to the head of the list of windows. Previously focused window is
102 * unfocused.
104 * Side effects:
105 * Window list may be reordered and the window focus is changed.
107 *----------------------------------------------------------------------
109 void wSetFocusTo(WScreen *scr, WWindow *wwin)
111 static WScreen *old_scr = NULL;
113 WWindow *old_focused;
114 WWindow *focused = scr->focused_window;
115 Time timestamp = LastTimestamp;
116 WApplication *oapp = NULL, *napp = NULL;
117 int wasfocused;
119 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
120 return;
122 if (!old_scr)
123 old_scr = scr;
125 old_focused = old_scr->focused_window;
127 LastFocusChange = timestamp;
129 if (old_focused)
130 oapp = wApplicationOf(old_focused->main_window);
132 if (wwin == NULL) {
133 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
134 if (old_focused)
135 wWindowUnfocus(old_focused);
137 if (oapp) {
138 wAppMenuUnmap(oapp->menu);
139 if (wPreferences.highlight_active_app)
140 wApplicationDeactivate(oapp);
143 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
144 return;
147 if (old_scr != scr && old_focused)
148 wWindowUnfocus(old_focused);
150 wasfocused = wwin->flags.focused;
151 napp = wApplicationOf(wwin->main_window);
153 /* remember last workspace where the app has been */
154 if (napp)
155 napp->last_workspace = wwin->frame->workspace;
157 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
158 /* install colormap if colormap mode is lock mode */
159 if (wPreferences.colormap_mode == WCM_CLICK)
160 wColormapInstallForWindow(scr, wwin);
162 /* set input focus */
163 switch (wwin->focus_mode) {
164 case WFM_NO_INPUT:
165 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
166 break;
167 case WFM_PASSIVE:
168 case WFM_LOCALLY_ACTIVE:
169 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
170 break;
171 case WFM_GLOBALLY_ACTIVE:
172 break;
175 XFlush(dpy);
176 if (wwin->protocols.TAKE_FOCUS)
177 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
179 XSync(dpy, False);
180 } else {
181 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
184 if (WFLAGP(wwin, no_focusable))
185 return;
187 /* if this is not the focused window focus it */
188 if (focused != wwin) {
189 /* change the focus window list order */
190 if (wwin->prev)
191 wwin->prev->next = wwin->next;
193 if (wwin->next)
194 wwin->next->prev = wwin->prev;
196 wwin->prev = focused;
197 focused->next = wwin;
198 wwin->next = NULL;
199 scr->focused_window = wwin;
201 if (oapp && oapp != napp) {
202 wAppMenuUnmap(oapp->menu);
203 if (wPreferences.highlight_active_app)
204 wApplicationDeactivate(oapp);
208 wWindowFocus(wwin, focused);
210 if (napp && !wasfocused) {
211 #ifdef USER_MENU
212 wUserMenuRefreshInstances(napp->menu, wwin);
213 #endif /* USER_MENU */
215 if (wwin->flags.mapped)
216 wAppMenuMap(napp->menu, wwin);
218 if (napp && wPreferences.highlight_active_app)
219 wApplicationActivate(napp);
221 XFlush(dpy);
222 old_scr = scr;
225 void wShadeWindow(WWindow *wwin)
228 if (wwin->flags.shaded)
229 return;
231 XLowerWindow(dpy, wwin->client_win);
232 shade_animate(wwin, SHADE);
234 wwin->flags.skip_next_animation = 0;
235 wwin->flags.shaded = 1;
236 wwin->flags.mapped = 0;
237 /* prevent window withdrawal when getting UnmapNotify */
238 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
239 XUnmapWindow(dpy, wwin->client_win);
240 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
242 /* for the client it's just like iconification */
243 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
245 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
246 wWindowSynthConfigureNotify(wwin);
249 wClientSetState(wwin, IconicState, None);
252 WMPostNotificationName(WMNChangedState, wwin, "shade");
254 #ifdef ANIMATIONS
255 if (!wwin->screen_ptr->flags.startup) {
256 /* Catch up with events not processed while animation was running */
257 ProcessPendingEvents();
259 #endif
262 void wUnshadeWindow(WWindow *wwin)
265 if (!wwin->flags.shaded)
266 return;
268 wwin->flags.shaded = 0;
269 wwin->flags.mapped = 1;
270 XMapWindow(dpy, wwin->client_win);
272 shade_animate(wwin, UNSHADE);
274 wwin->flags.skip_next_animation = 0;
275 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
276 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
278 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
279 wWindowSynthConfigureNotify(wwin);
281 /* if the window is focused, set the focus again as it was disabled during
282 * shading */
283 if (wwin->flags.focused)
284 wSetFocusTo(wwin->screen_ptr, wwin);
286 WMPostNotificationName(WMNChangedState, wwin, "shade");
289 /* Set the old coordinates using the current values */
290 static void save_old_geometry(WWindow *wwin, int directions)
292 /* never been saved? */
293 if (! wwin->old_geometry.width)
294 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
295 if (! wwin->old_geometry.height)
296 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
298 if (directions & SAVE_GEOMETRY_X)
299 wwin->old_geometry.x = wwin->frame_x;
300 if (directions & SAVE_GEOMETRY_Y)
301 wwin->old_geometry.y = wwin->frame_y;
302 if (directions & SAVE_GEOMETRY_WIDTH)
303 wwin->old_geometry.width = wwin->client.width;
304 if (directions & SAVE_GEOMETRY_HEIGHT)
305 wwin->old_geometry.height = wwin->client.height;
308 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
310 WMRect old_geom_rect;
311 int old_head;
312 Bool same_head;
314 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
315 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
316 same_head = (wGetHeadForWindow(wwin) == old_head);
317 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
318 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
319 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
320 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
323 /* Remember geometry for unmaximizing */
324 void update_saved_geometry(WWindow *wwin)
326 /* NOT if we aren't already maximized
327 * we'll save geometry when maximizing */
328 if (!wwin->flags.maximized)
329 return;
331 /* NOT if we are fully maximized */
332 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
333 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
334 (wwin->flags.maximized & MAX_VERTICAL)))
335 return;
337 /* save the co-ordinate in the axis in which we AREN'T maximized */
338 if (wwin->flags.maximized & MAX_HORIZONTAL)
339 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
340 if (wwin->flags.maximized & MAX_VERTICAL)
341 save_old_geometry(wwin, SAVE_GEOMETRY_X);
344 void wMaximizeWindow(WWindow *wwin, int directions)
346 unsigned int new_width, new_height, half_scr_width, half_scr_height;
347 int new_x = 0;
348 int new_y = 0;
349 int maximus_x = 0;
350 int maximus_y = 0;
351 unsigned int maximus_width = 0;
352 unsigned int maximus_height = 0;
353 WArea usableArea, totalArea;
354 Bool has_border = 1;
355 int adj_size;
357 if (!IS_RESIZABLE(wwin))
358 return;
360 if (!HAS_BORDER(wwin))
361 has_border = 0;
363 /* the size to adjust the geometry */
364 adj_size = wwin->screen_ptr->frame_border_width * 2 * has_border;
366 /* save old coordinates before we change the current values */
367 if (!wwin->flags.maximized)
368 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
370 totalArea.x2 = wwin->screen_ptr->scr_width;
371 totalArea.y2 = wwin->screen_ptr->scr_height;
372 totalArea.x1 = 0;
373 totalArea.y1 = 0;
374 usableArea = totalArea;
376 if (!(directions & MAX_IGNORE_XINERAMA)) {
377 WScreen *scr = wwin->screen_ptr;
378 int head;
380 if (directions & MAX_KEYBOARD)
381 head = wGetHeadForWindow(wwin);
382 else
383 head = wGetHeadForPointerLocation(scr);
385 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
388 /* Only save directions, not kbd or xinerama hints */
389 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
391 if (WFLAGP(wwin, full_maximize)) {
392 usableArea = totalArea;
394 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
395 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
397 if (wwin->flags.shaded) {
398 wwin->flags.skip_next_animation = 1;
399 wUnshadeWindow(wwin);
402 if (directions & MAX_MAXIMUS) {
403 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
404 new_width = maximus_width - adj_size;
405 new_height = maximus_height - adj_size;
406 new_x = maximus_x;
407 new_y = maximus_y;
408 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
409 new_height += wwin->frame->bottom_width - 1;
410 new_y -= wwin->frame->top_width;
412 /* HACK: this will be subtracted again below */
413 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
415 wwin->maximus_x = new_x;
416 wwin->maximus_y = new_y;
417 wwin->flags.old_maximized |= MAX_MAXIMUS;
418 } else {
419 /* set default values if no option set then */
420 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
421 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
422 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
424 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
425 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
426 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
429 /* left|right position */
430 if (directions & MAX_LEFTHALF) {
431 new_width = half_scr_width - adj_size;
432 new_x = usableArea.x1;
433 } else if (directions & MAX_RIGHTHALF) {
434 new_width = half_scr_width - adj_size;
435 new_x = usableArea.x1 + half_scr_width;
437 /* top|bottom position */
438 if (directions & MAX_TOPHALF) {
439 new_height = half_scr_height - adj_size;
440 new_y = usableArea.y1;
441 } else if (directions & MAX_BOTTOMHALF) {
442 new_height = half_scr_height - adj_size;
443 new_y = usableArea.y1 + half_scr_height;
446 /* vertical|horizontal position */
447 if (directions & MAX_HORIZONTAL) {
448 new_width = usableArea.x2 - usableArea.x1 - adj_size;
449 new_x = usableArea.x1;
451 if (directions & MAX_VERTICAL) {
452 new_height = usableArea.y2 - usableArea.y1 - adj_size;
453 new_y = usableArea.y1;
454 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
455 new_y -= wwin->frame->top_width;
459 if (!WFLAGP(wwin, full_maximize))
460 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
462 /* set maximization state */
463 wwin->flags.maximized = directions;
464 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
465 wwin->flags.maximized = MAX_MAXIMUS;
467 wWindowConstrainSize(wwin, &new_width, &new_height);
469 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
470 usableArea.y2 - usableArea.y1, &new_width, &new_height);
472 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
473 wWindowSynthConfigureNotify(wwin);
475 WMPostNotificationName(WMNChangedState, wwin, "maximize");
478 /* generic (un)maximizer */
479 void handleMaximize(WWindow *wwin, int directions)
481 int current = wwin->flags.maximized;
482 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
483 int effective = requested ^ current;
484 int flags = directions & ~requested;
486 if (!effective) {
487 /* allow wMaximizeWindow to restore the Maximusized size */
488 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
489 !(requested & MAX_MAXIMUS))
490 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
491 else
492 wUnmaximizeWindow(wwin);
493 /* these alone mean vertical|horizontal toggle */
494 } else if ((effective == MAX_LEFTHALF) ||
495 (effective == MAX_RIGHTHALF) ||
496 (effective == MAX_TOPHALF) ||
497 (effective == MAX_BOTTOMHALF))
498 wUnmaximizeWindow(wwin);
499 else {
500 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
501 (requested == MAX_MAXIMUS))
502 effective = requested;
503 else {
504 if (requested & MAX_LEFTHALF) {
505 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
506 effective |= MAX_VERTICAL;
507 else
508 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
509 effective |= MAX_LEFTHALF;
510 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
511 } else if (requested & MAX_RIGHTHALF) {
512 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
513 effective |= MAX_VERTICAL;
514 else
515 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
516 effective |= MAX_RIGHTHALF;
517 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
519 if (requested & MAX_TOPHALF) {
520 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
521 effective |= MAX_HORIZONTAL;
522 else
523 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
524 effective |= MAX_TOPHALF;
525 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
526 } else if (requested & MAX_BOTTOMHALF) {
527 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
528 effective |= MAX_HORIZONTAL;
529 else
530 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
531 effective |= MAX_BOTTOMHALF;
532 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
534 if (requested & MAX_HORIZONTAL)
535 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
536 if (requested & MAX_VERTICAL)
537 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
538 effective &= ~MAX_MAXIMUS;
540 wMaximizeWindow(wwin, effective | flags);
544 /* the window boundary coordinates */
545 typedef struct {
546 int left;
547 int right;
548 int bottom;
549 int top;
550 int width;
551 int height;
552 } win_coords;
554 static void set_window_coords(WWindow *wwin, win_coords *obs)
556 obs->left = wwin->frame_x;
557 obs->top = wwin->frame_y;
558 obs->width = wwin->frame->core->width;
559 obs->height = wwin->frame->core->height;
560 obs->bottom = obs->top + obs->height;
561 obs->right = obs->left + obs->width;
565 * Maximus: tiled maximization (maximize without overlapping other windows)
567 * The original window 'orig' will be maximized to new coordinates 'new'.
568 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
570 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
571 unsigned int *new_width, unsigned int *new_height)
573 WWindow *tmp;
574 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
575 short int adjust_height;
576 int x_intsect, y_intsect;
577 /* the obstructing, original and new windows */
578 win_coords obs, orig, new;
580 /* set the original coordinate positions of the window to be Maximumized */
581 if (wwin->flags.maximized) {
582 /* window is already maximized; consider original geometry */
583 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
584 orig.bottom = orig.top + orig.height;
585 orig.right = orig.left + orig.width;
587 else
588 set_window_coords(wwin, &orig);
590 /* Try to fully maximize first, then readjust later */
591 new.left = usableArea.x1;
592 new.right = usableArea.x2;
593 new.top = usableArea.y1;
594 new.bottom = usableArea.y2;
596 if (HAS_TITLEBAR(wwin))
597 tbar_height_0 = TITLEBAR_HEIGHT;
598 if (HAS_RESIZEBAR(wwin))
599 rbar_height_0 = RESIZEBAR_HEIGHT;
600 if (HAS_BORDER(wwin))
601 bd_width_0 = wwin->screen_ptr->frame_border_width;
603 /* the length to be subtracted if the window has titlebar, etc */
604 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
606 tmp = wwin;
607 /* The focused window is always the last in the list */
608 while (tmp->prev) {
609 /* ignore windows in other workspaces etc */
610 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
611 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
612 tmp = tmp->prev;
613 continue;
615 tmp = tmp->prev;
617 /* Set the coordinates of obstructing window */
618 set_window_coords(tmp, &obs);
620 /* Try to maximize in the y direction first */
621 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
622 if (x_intsect != 0) {
623 /* TODO: Consider the case when coords are equal */
624 if (obs.bottom < orig.top && obs.bottom > new.top) {
625 /* w_0 is below the bottom of w_j */
626 new.top = obs.bottom + 1;
628 if (orig.bottom < obs.top && obs.top < new.bottom) {
629 /* The bottom of w_0 is above the top of w_j */
630 new.bottom = obs.top - 1;
635 tmp = wwin;
636 while (tmp->prev) {
637 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
638 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
639 tmp = tmp->prev;
640 continue;
642 tmp = tmp->prev;
644 set_window_coords(tmp, &obs);
647 * Use the new.top and new.height instead of original values
648 * as they may have different intersections with the obstructing windows
650 new.height = new.bottom - new.top - adjust_height;
651 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
652 if (y_intsect != 0) {
653 if (obs.right < orig.left && obs.right > new.left) {
654 /* w_0 is completely to the right of w_j */
655 new.left = obs.right + 1;
657 if (orig.right < obs.left && obs.left < new.right) {
658 /* w_0 is completely to the left of w_j */
659 new.right = obs.left - 1;
664 *new_x = new.left;
665 *new_y = new.top;
666 /* xcalc needs -7 here, but other apps don't */
667 *new_height = new.bottom - new.top - adjust_height - 1;;
668 *new_width = new.right - new.left;
671 void wUnmaximizeWindow(WWindow *wwin)
673 int x, y, w, h;
675 if (!wwin->flags.maximized)
676 return;
678 if (wwin->flags.shaded) {
679 wwin->flags.skip_next_animation = 1;
680 wUnshadeWindow(wwin);
683 /* Use old coordinates if they are set, current values otherwise */
684 remember_geometry(wwin, &x, &y, &w, &h);
686 /* unMaximusize relative to original position */
687 if (wwin->flags.maximized & MAX_MAXIMUS) {
688 x += wwin->frame_x - wwin->maximus_x;
689 y += wwin->frame_y - wwin->maximus_y;
692 wwin->flags.maximized = 0;
693 wwin->flags.old_maximized = 0;
694 wWindowConfigure(wwin, x, y, w, h);
695 wWindowSynthConfigureNotify(wwin);
697 WMPostNotificationName(WMNChangedState, wwin, "maximize");
700 void wFullscreenWindow(WWindow *wwin)
702 int head;
703 WMRect rect;
705 if (wwin->flags.fullscreen)
706 return;
708 wwin->flags.fullscreen = True;
710 wWindowConfigureBorders(wwin);
712 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
714 wwin->bfs_geometry.x = wwin->frame_x;
715 wwin->bfs_geometry.y = wwin->frame_y;
716 wwin->bfs_geometry.width = wwin->frame->core->width;
717 wwin->bfs_geometry.height = wwin->frame->core->height;
719 head = wGetHeadForWindow(wwin);
720 rect = wGetRectForHead(wwin->screen_ptr, head);
721 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
723 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
726 void wUnfullscreenWindow(WWindow *wwin)
728 if (!wwin->flags.fullscreen)
729 return;
731 wwin->flags.fullscreen = False;
733 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
734 if (WFLAGP(wwin, sunken)) {
735 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
736 } else if (WFLAGP(wwin, floating)) {
737 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
738 } else {
739 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
743 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
744 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
746 wWindowConfigureBorders(wwin);
748 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
749 wFrameWindowPaint(wwin->frame);
752 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
755 #ifdef ANIMATIONS
756 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
758 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
759 float cx, cy, cw, ch;
760 float xstep, ystep, wstep, hstep;
761 XPoint points[5];
762 float dx, dch, midy;
763 float angle, final_angle, delta;
765 xstep = (float)(fx - x) / steps;
766 ystep = (float)(fy - y) / steps;
767 wstep = (float)(fw - w) / steps;
768 hstep = (float)(fh - h) / steps;
770 cx = (float)x;
771 cy = (float)y;
772 cw = (float)w;
773 ch = (float)h;
775 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
776 delta = (float)(final_angle / FRAMES);
777 for (angle = 0;; angle += delta) {
778 if (angle > final_angle)
779 angle = final_angle;
781 dx = (cw / 10) - ((cw / 5) * sin(angle));
782 dch = (ch / 2) * cos(angle);
783 midy = cy + (ch / 2);
785 points[0].x = cx + dx;
786 points[0].y = midy - dch;
787 points[1].x = cx + cw - dx;
788 points[1].y = points[0].y;
789 points[2].x = cx + cw + dx;
790 points[2].y = midy + dch;
791 points[3].x = cx - dx;
792 points[3].y = points[2].y;
793 points[4].x = points[0].x;
794 points[4].y = points[0].y;
796 XGrabServer(dpy);
797 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
798 XFlush(dpy);
799 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
801 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
802 XUngrabServer(dpy);
803 cx += xstep;
804 cy += ystep;
805 cw += wstep;
806 ch += hstep;
807 if (angle >= final_angle)
808 break;
811 XFlush(dpy);
814 #undef FRAMES
816 static void
817 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
819 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
820 float cx, cy, cw, ch;
821 float xstep, ystep, wstep, hstep;
822 XPoint points[5];
823 float angle, final_angle, a, d, delta;
825 x += w / 2;
826 y += h / 2;
827 fx += fw / 2;
828 fy += fh / 2;
830 xstep = (float)(fx - x) / steps;
831 ystep = (float)(fy - y) / steps;
832 wstep = (float)(fw - w) / steps;
833 hstep = (float)(fh - h) / steps;
835 cx = (float)x;
836 cy = (float)y;
837 cw = (float)w;
838 ch = (float)h;
840 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
841 delta = (float)(final_angle / FRAMES);
842 for (angle = 0;; angle += delta) {
843 if (angle > final_angle)
844 angle = final_angle;
846 a = atan(ch / cw);
847 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
849 points[0].x = cx + cos(angle - a) * d;
850 points[0].y = cy + sin(angle - a) * d;
851 points[1].x = cx + cos(angle + a) * d;
852 points[1].y = cy + sin(angle + a) * d;
853 points[2].x = cx + cos(angle - a + WM_PI) * d;
854 points[2].y = cy + sin(angle - a + WM_PI) * d;
855 points[3].x = cx + cos(angle + a + WM_PI) * d;
856 points[3].y = cy + sin(angle + a + WM_PI) * d;
857 points[4].x = cx + cos(angle - a) * d;
858 points[4].y = cy + sin(angle - a) * d;
859 XGrabServer(dpy);
860 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
861 XFlush(dpy);
862 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
864 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
865 XUngrabServer(dpy);
866 cx += xstep;
867 cy += ystep;
868 cw += wstep;
869 ch += hstep;
870 if (angle >= final_angle)
871 break;
874 XFlush(dpy);
877 #undef FRAMES
879 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
881 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
882 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
883 float xstep, ystep, wstep, hstep;
884 int i, j;
886 xstep = (float)(fx - x) / steps;
887 ystep = (float)(fy - y) / steps;
888 wstep = (float)(fw - w) / steps;
889 hstep = (float)(fh - h) / steps;
891 for (j = 0; j < FRAMES; j++) {
892 cx[j] = (float)x;
893 cy[j] = (float)y;
894 cw[j] = (float)w;
895 ch[j] = (float)h;
897 XGrabServer(dpy);
898 for (i = 0; i < steps; i++) {
899 for (j = 0; j < FRAMES; j++) {
900 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
901 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
903 XFlush(dpy);
904 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
906 for (j = 0; j < FRAMES; j++) {
907 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
908 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
909 if (j < FRAMES - 1) {
910 cx[j] = cx[j + 1];
911 cy[j] = cy[j + 1];
912 cw[j] = cw[j + 1];
913 ch[j] = ch[j + 1];
914 } else {
915 cx[j] += xstep;
916 cy[j] += ystep;
917 cw[j] += wstep;
918 ch[j] += hstep;
923 for (j = 0; j < FRAMES; j++) {
924 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
926 XFlush(dpy);
927 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
929 for (j = 0; j < FRAMES; j++) {
930 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
933 XUngrabServer(dpy);
936 #undef FRAMES
938 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
940 int style = wPreferences.iconification_style; /* Catch the value */
941 int steps;
943 if (style == WIS_NONE)
944 return;
946 if (style == WIS_RANDOM) {
947 style = rand() % 3;
950 switch (style) {
951 case WIS_TWIST:
952 steps = MINIATURIZE_ANIMATION_STEPS_T;
953 if (steps > 0)
954 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
955 break;
956 case WIS_FLIP:
957 steps = MINIATURIZE_ANIMATION_STEPS_F;
958 if (steps > 0)
959 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
960 break;
961 case WIS_ZOOM:
962 default:
963 steps = MINIATURIZE_ANIMATION_STEPS_Z;
964 if (steps > 0)
965 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
966 break;
969 #endif /* ANIMATIONS */
971 static void flushExpose(void)
973 XEvent tmpev;
975 while (XCheckTypedEvent(dpy, Expose, &tmpev))
976 WMHandleEvent(&tmpev);
977 XSync(dpy, 0);
980 static void unmapTransientsFor(WWindow *wwin)
982 WWindow *tmp;
984 tmp = wwin->screen_ptr->focused_window;
985 while (tmp) {
986 /* unmap the transients for this transient */
987 if (tmp != wwin && tmp->transient_for == wwin->client_win
988 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
989 unmapTransientsFor(tmp);
990 tmp->flags.miniaturized = 1;
991 if (!tmp->flags.shaded) {
992 wWindowUnmap(tmp);
993 } else {
994 XUnmapWindow(dpy, tmp->frame->core->window);
997 if (!tmp->flags.shaded)
999 wClientSetState(tmp, IconicState, None);
1001 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1003 tmp = tmp->prev;
1007 static void mapTransientsFor(WWindow *wwin)
1009 WWindow *tmp;
1011 tmp = wwin->screen_ptr->focused_window;
1012 while (tmp) {
1013 /* recursively map the transients for this transient */
1014 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1015 && tmp->icon == NULL) {
1016 mapTransientsFor(tmp);
1017 tmp->flags.miniaturized = 0;
1018 if (!tmp->flags.shaded) {
1019 wWindowMap(tmp);
1020 } else {
1021 XMapWindow(dpy, tmp->frame->core->window);
1023 tmp->flags.semi_focused = 0;
1025 if (!tmp->flags.shaded)
1027 wClientSetState(tmp, NormalState, None);
1029 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1031 tmp = tmp->prev;
1035 static WWindow *recursiveTransientFor(WWindow * wwin)
1037 int i;
1039 if (!wwin)
1040 return None;
1042 /* hackish way to detect transient_for cycle */
1043 i = wwin->screen_ptr->window_count + 1;
1045 while (wwin && wwin->transient_for != None && i > 0) {
1046 wwin = wWindowFor(wwin->transient_for);
1047 i--;
1049 if (i == 0 && wwin) {
1050 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1051 return NULL;
1054 return wwin;
1057 void wIconifyWindow(WWindow * wwin)
1059 XWindowAttributes attribs;
1060 int present;
1062 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1063 return; /* the window doesn't exist anymore */
1065 if (wwin->flags.miniaturized)
1066 return; /* already miniaturized */
1068 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1069 WWindow *owner = wWindowFor(wwin->transient_for);
1071 if (owner && owner->flags.miniaturized)
1072 return;
1075 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1077 /* if the window is in another workspace, simplify process */
1078 if (present) {
1079 /* icon creation may take a while */
1080 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1081 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1082 GrabModeAsync, None, None, CurrentTime);
1085 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1086 if (!wwin->flags.icon_moved)
1087 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1089 wwin->icon = icon_create_for_wwindow(wwin);
1090 wwin->icon->mapped = 1;
1093 wwin->flags.miniaturized = 1;
1094 wwin->flags.mapped = 0;
1096 /* unmap transients */
1097 unmapTransientsFor(wwin);
1099 if (present) {
1100 XUngrabPointer(dpy, CurrentTime);
1101 wWindowUnmap(wwin);
1102 /* let all Expose events arrive so that we can repaint
1103 * something before the animation starts (and the server is grabbed) */
1104 XSync(dpy, 0);
1106 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1107 wClientSetState(wwin, IconicState, None);
1108 else
1109 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1111 flushExpose();
1112 #ifdef ANIMATIONS
1113 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1114 && !wPreferences.no_animations) {
1115 int ix, iy, iw, ih;
1117 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1118 ix = wwin->icon_x;
1119 iy = wwin->icon_y;
1120 iw = wwin->icon->core->width;
1121 ih = wwin->icon->core->height;
1122 } else {
1123 if (wwin->flags.net_handle_icon) {
1124 ix = wwin->icon_x;
1125 iy = wwin->icon_y;
1126 iw = wwin->icon_w;
1127 ih = wwin->icon_h;
1128 } else {
1129 ix = 0;
1130 iy = 0;
1131 iw = wwin->screen_ptr->scr_width;
1132 ih = wwin->screen_ptr->scr_height;
1135 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1136 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1138 #endif
1141 wwin->flags.skip_next_animation = 0;
1143 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1144 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1145 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1146 XMapWindow(dpy, wwin->icon->core->window);
1148 AddToStackList(wwin->icon->core);
1149 wLowerFrame(wwin->icon->core);
1152 if (present) {
1153 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1156 * It doesn't seem to be working and causes button event hangup
1157 * when deiconifying a transient window.
1158 setupIconGrabs(wwin->icon);
1160 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1161 && wPreferences.focus_mode == WKF_CLICK) {
1162 WWindow *tmp;
1164 tmp = wwin->prev;
1165 while (tmp) {
1166 if (!WFLAGP(tmp, no_focusable)
1167 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1168 && (wwin->frame->workspace == tmp->frame->workspace))
1169 break;
1170 tmp = tmp->prev;
1172 wSetFocusTo(wwin->screen_ptr, tmp);
1173 } else if (wPreferences.focus_mode != WKF_CLICK) {
1174 wSetFocusTo(wwin->screen_ptr, NULL);
1176 #ifdef ANIMATIONS
1177 if (!wwin->screen_ptr->flags.startup) {
1178 /* Catch up with events not processed while animation was running */
1179 Window clientwin = wwin->client_win;
1181 ProcessPendingEvents();
1183 /* the window can disappear while ProcessPendingEvents() runs */
1184 if (!wWindowFor(clientwin)) {
1185 return;
1188 #endif
1191 /* maybe we want to do this regardless of net_handle_icon
1192 * it seems to me we might break behaviour this way.
1194 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1195 && !wwin->flags.net_handle_icon)
1196 wIconSelect(wwin->icon);
1198 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1200 if (wPreferences.auto_arrange_icons)
1201 wArrangeIcons(wwin->screen_ptr, True);
1204 void wDeiconifyWindow(WWindow *wwin)
1206 /* Let's avoid changing workspace while deiconifying */
1207 ignore_wks_change = 1;
1209 /* we're hiding for show_desktop */
1210 int netwm_hidden = wwin->flags.net_show_desktop &&
1211 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1213 if (!netwm_hidden)
1214 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1216 if (!wwin->flags.miniaturized)
1217 return;
1219 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1220 WWindow *owner = recursiveTransientFor(wwin);
1222 if (owner && owner->flags.miniaturized) {
1223 wDeiconifyWindow(owner);
1224 wSetFocusTo(wwin->screen_ptr, wwin);
1225 wRaiseFrame(wwin->frame->core);
1226 return;
1230 wwin->flags.miniaturized = 0;
1232 if (!netwm_hidden && !wwin->flags.shaded)
1233 wwin->flags.mapped = 1;
1235 if (!netwm_hidden || wPreferences.sticky_icons) {
1236 /* maybe we want to do this regardless of net_handle_icon
1237 * it seems to me we might break behaviour this way.
1239 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1240 && wwin->icon != NULL) {
1241 if (wwin->icon->selected)
1242 wIconSelect(wwin->icon);
1244 XUnmapWindow(dpy, wwin->icon->core->window);
1248 /* if the window is in another workspace, do it silently */
1249 if (!netwm_hidden) {
1250 #ifdef ANIMATIONS
1251 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1252 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1253 int ix, iy, iw, ih;
1255 if (!wPreferences.disable_miniwindows
1256 && !wwin->flags.net_handle_icon) {
1257 ix = wwin->icon_x;
1258 iy = wwin->icon_y;
1259 iw = wwin->icon->core->width;
1260 ih = wwin->icon->core->height;
1261 } else {
1262 if (wwin->flags.net_handle_icon) {
1263 ix = wwin->icon_x;
1264 iy = wwin->icon_y;
1265 iw = wwin->icon_w;
1266 ih = wwin->icon_h;
1267 } else {
1268 ix = 0;
1269 iy = 0;
1270 iw = wwin->screen_ptr->scr_width;
1271 ih = wwin->screen_ptr->scr_height;
1274 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1275 wwin->frame_x, wwin->frame_y,
1276 wwin->frame->core->width, wwin->frame->core->height);
1278 #endif /* ANIMATIONS */
1279 wwin->flags.skip_next_animation = 0;
1280 XGrabServer(dpy);
1281 if (!wwin->flags.shaded)
1282 XMapWindow(dpy, wwin->client_win);
1284 XMapWindow(dpy, wwin->frame->core->window);
1285 wRaiseFrame(wwin->frame->core);
1286 if (!wwin->flags.shaded)
1287 wClientSetState(wwin, NormalState, None);
1289 mapTransientsFor(wwin);
1292 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1293 && !wwin->flags.net_handle_icon) {
1294 RemoveFromStackList(wwin->icon->core);
1295 /* removeIconGrabs(wwin->icon); */
1296 wIconDestroy(wwin->icon);
1297 wwin->icon = NULL;
1300 if (!netwm_hidden) {
1301 XUngrabServer(dpy);
1303 wSetFocusTo(wwin->screen_ptr, wwin);
1305 #ifdef ANIMATIONS
1306 if (!wwin->screen_ptr->flags.startup) {
1307 /* Catch up with events not processed while animation was running */
1308 Window clientwin = wwin->client_win;
1310 ProcessPendingEvents();
1312 /* the window can disappear while ProcessPendingEvents() runs */
1313 if (!wWindowFor(clientwin))
1314 return;
1316 #endif
1319 if (wPreferences.auto_arrange_icons)
1320 wArrangeIcons(wwin->screen_ptr, True);
1322 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1324 /* In case we were shaded and iconified, also unshade */
1325 if (!netwm_hidden)
1326 wUnshadeWindow(wwin);
1328 ignore_wks_change = 0;
1331 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1333 if (wwin->flags.miniaturized) {
1334 if (wwin->icon) {
1335 XUnmapWindow(dpy, wwin->icon->core->window);
1336 wwin->icon->mapped = 0;
1338 wwin->flags.hidden = 1;
1340 WMPostNotificationName(WMNChangedState, wwin, "hide");
1341 return;
1344 if (wwin->flags.inspector_open) {
1345 wHideInspectorForWindow(wwin);
1348 wwin->flags.hidden = 1;
1349 wWindowUnmap(wwin);
1351 wClientSetState(wwin, IconicState, icon->icon_win);
1352 flushExpose();
1354 #ifdef ANIMATIONS
1355 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1356 !wwin->flags.skip_next_animation && animate) {
1357 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1358 wwin->frame->core->width, wwin->frame->core->height,
1359 icon_x, icon_y, icon->core->width, icon->core->height);
1361 #endif
1362 wwin->flags.skip_next_animation = 0;
1364 WMPostNotificationName(WMNChangedState, wwin, "hide");
1367 void wHideAll(WScreen *scr)
1369 WWindow *wwin;
1370 WWindow **windows;
1371 WMenu *menu;
1372 unsigned int wcount = 0;
1373 int i;
1375 if (!scr)
1376 return;
1378 menu = scr->switch_menu;
1380 windows = wmalloc(sizeof(WWindow *));
1382 if (menu != NULL) {
1383 for (i = 0; i < menu->entry_no; i++) {
1384 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1385 wcount++;
1386 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1388 } else {
1389 wwin = scr->focused_window;
1391 while (wwin) {
1392 windows[wcount] = wwin;
1393 wcount++;
1394 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1395 wwin = wwin->prev;
1400 for (i = 0; i < wcount; i++) {
1401 wwin = windows[i];
1402 if (wwin->frame->workspace == scr->current_workspace
1403 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1404 && !wwin->flags.internal_window
1405 && !WFLAGP(wwin, no_miniaturizable)
1407 wwin->flags.skip_next_animation = 1;
1408 wIconifyWindow(wwin);
1412 wfree(windows);
1415 void wHideOtherApplications(WWindow *awin)
1417 WWindow *wwin;
1418 WApplication *tapp;
1420 if (!awin)
1421 return;
1422 wwin = awin->screen_ptr->focused_window;
1424 while (wwin) {
1425 if (wwin != awin
1426 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1427 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1428 && !wwin->flags.internal_window
1429 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1431 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1432 if (!WFLAGP(wwin, no_miniaturizable)) {
1433 wwin->flags.skip_next_animation = 1;
1434 wIconifyWindow(wwin);
1436 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1437 tapp = wApplicationOf(wwin->main_window);
1438 if (tapp) {
1439 tapp->flags.skip_next_animation = 1;
1440 wHideApplication(tapp);
1441 } else {
1442 if (!WFLAGP(wwin, no_miniaturizable)) {
1443 wwin->flags.skip_next_animation = 1;
1444 wIconifyWindow(wwin);
1449 wwin = wwin->prev;
1452 wSetFocusTo(awin->screen_ptr, awin);
1456 void wHideApplication(WApplication *wapp)
1458 WScreen *scr;
1459 WWindow *wlist;
1460 int hadfocus;
1461 int animate;
1463 if (!wapp) {
1464 wwarning("trying to hide a non grouped window");
1465 return;
1467 if (!wapp->main_window_desc) {
1468 wwarning("group leader not found for window group");
1469 return;
1471 scr = wapp->main_window_desc->screen_ptr;
1472 hadfocus = 0;
1473 wlist = scr->focused_window;
1474 if (!wlist)
1475 return;
1477 if (wlist->main_window == wapp->main_window)
1478 wapp->last_focused = wlist;
1479 else
1480 wapp->last_focused = NULL;
1482 animate = !wapp->flags.skip_next_animation;
1484 while (wlist) {
1485 if (wlist->main_window == wapp->main_window) {
1486 if (wlist->flags.focused) {
1487 hadfocus = 1;
1489 if (wapp->app_icon) {
1490 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1491 wapp->app_icon->y_pos, wlist, animate);
1492 animate = False;
1495 wlist = wlist->prev;
1498 wapp->flags.skip_next_animation = 0;
1500 if (hadfocus) {
1501 if (wPreferences.focus_mode == WKF_CLICK) {
1502 wlist = scr->focused_window;
1503 while (wlist) {
1504 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1505 && (wlist->flags.mapped || wlist->flags.shaded))
1506 break;
1507 wlist = wlist->prev;
1509 wSetFocusTo(scr, wlist);
1510 } else {
1511 wSetFocusTo(scr, NULL);
1515 wapp->flags.hidden = 1;
1517 if (wPreferences.auto_arrange_icons)
1518 wArrangeIcons(scr, True);
1520 #ifdef HIDDENDOT
1521 if (wapp->app_icon)
1522 wAppIconPaint(wapp->app_icon);
1523 #endif
1526 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1528 if (bringToCurrentWS)
1529 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1531 wwin->flags.hidden = 0;
1533 #ifdef ANIMATIONS
1534 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1535 animateResize(wwin->screen_ptr, icon_x, icon_y,
1536 icon->core->width, icon->core->height,
1537 wwin->frame_x, wwin->frame_y,
1538 wwin->frame->core->width, wwin->frame->core->height);
1540 #endif
1541 wwin->flags.skip_next_animation = 0;
1542 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1543 XMapWindow(dpy, wwin->client_win);
1544 XMapWindow(dpy, wwin->frame->core->window);
1545 wClientSetState(wwin, NormalState, None);
1546 wwin->flags.mapped = 1;
1547 wRaiseFrame(wwin->frame->core);
1549 if (wwin->flags.inspector_open) {
1550 wUnhideInspectorForWindow(wwin);
1553 WMPostNotificationName(WMNChangedState, wwin, "hide");
1556 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1558 WScreen *scr;
1559 WWindow *wlist, *next;
1560 WWindow *focused = NULL;
1561 int animate;
1563 if (!wapp)
1564 return;
1566 scr = wapp->main_window_desc->screen_ptr;
1567 wlist = scr->focused_window;
1568 if (!wlist)
1569 return;
1571 /* goto beginning of list */
1572 while (wlist->prev)
1573 wlist = wlist->prev;
1575 animate = !wapp->flags.skip_next_animation;
1577 while (wlist) {
1578 next = wlist->next;
1580 if (wlist->main_window == wapp->main_window) {
1581 if (wlist->flags.focused)
1582 focused = wlist;
1583 else if (!focused || !focused->flags.focused)
1584 focused = wlist;
1586 if (wlist->flags.miniaturized) {
1587 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1588 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1589 if (!wlist->icon->mapped) {
1590 int x, y;
1592 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1593 if (wlist->icon_x != x || wlist->icon_y != y) {
1594 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1596 wlist->icon_x = x;
1597 wlist->icon_y = y;
1598 XMapWindow(dpy, wlist->icon->core->window);
1599 wlist->icon->mapped = 1;
1601 wRaiseFrame(wlist->icon->core);
1603 if (bringToCurrentWS)
1604 wWindowChangeWorkspace(wlist, scr->current_workspace);
1605 wlist->flags.hidden = 0;
1606 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1607 wDeiconifyWindow(wlist);
1609 WMPostNotificationName(WMNChangedState, wlist, "hide");
1610 } else if (wlist->flags.shaded) {
1611 if (bringToCurrentWS)
1612 wWindowChangeWorkspace(wlist, scr->current_workspace);
1613 wlist->flags.hidden = 0;
1614 wRaiseFrame(wlist->frame->core);
1615 if (wlist->frame->workspace == scr->current_workspace) {
1616 XMapWindow(dpy, wlist->frame->core->window);
1617 if (miniwindows) {
1618 wUnshadeWindow(wlist);
1621 WMPostNotificationName(WMNChangedState, wlist, "hide");
1622 } else if (wlist->flags.hidden) {
1623 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1624 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1625 animate = False;
1626 } else {
1627 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1628 wWindowChangeWorkspace(wlist, scr->current_workspace);
1630 wRaiseFrame(wlist->frame->core);
1633 wlist = next;
1636 wapp->flags.skip_next_animation = 0;
1637 wapp->flags.hidden = 0;
1639 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1640 wRaiseFrame(wapp->last_focused->frame->core);
1641 wSetFocusTo(scr, wapp->last_focused);
1642 } else if (focused) {
1643 wSetFocusTo(scr, focused);
1645 wapp->last_focused = NULL;
1646 if (wPreferences.auto_arrange_icons)
1647 wArrangeIcons(scr, True);
1649 #ifdef HIDDENDOT
1650 wAppIconPaint(wapp->app_icon);
1651 #endif
1654 void wShowAllWindows(WScreen *scr)
1656 WWindow *wwin, *old_foc;
1657 WApplication *wapp;
1659 old_foc = wwin = scr->focused_window;
1660 while (wwin) {
1661 if (!wwin->flags.internal_window &&
1662 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1663 if (wwin->flags.miniaturized) {
1664 wwin->flags.skip_next_animation = 1;
1665 wDeiconifyWindow(wwin);
1666 } else if (wwin->flags.hidden) {
1667 wapp = wApplicationOf(wwin->main_window);
1668 if (wapp) {
1669 wUnhideApplication(wapp, False, False);
1670 } else {
1671 wwin->flags.skip_next_animation = 1;
1672 wDeiconifyWindow(wwin);
1676 wwin = wwin->prev;
1678 wSetFocusTo(scr, old_foc);
1679 /*wRaiseFrame(old_foc->frame->core); */
1682 void wRefreshDesktop(WScreen *scr)
1684 Window win;
1685 XSetWindowAttributes attr;
1687 attr.backing_store = NotUseful;
1688 attr.save_under = False;
1689 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1690 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1691 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1692 XMapRaised(dpy, win);
1693 XDestroyWindow(dpy, win);
1694 XFlush(dpy);
1697 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1699 WWindow *wwin;
1700 WAppIcon *aicon;
1702 int head;
1703 const int heads = wXineramaHeads(scr);
1705 struct HeadVars {
1706 int pf; /* primary axis */
1707 int sf; /* secondary axis */
1708 int fullW;
1709 int fullH;
1710 int pi, si;
1711 int sx1, sx2, sy1, sy2; /* screen boundary */
1712 int sw, sh;
1713 int xo, yo;
1714 int xs, ys;
1715 } *vars;
1717 int isize = wPreferences.icon_size;
1719 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1721 for (head = 0; head < heads; ++head) {
1722 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1723 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1724 vars[head].pi = vars[head].si = 0;
1725 vars[head].sx1 = rect.pos.x;
1726 vars[head].sy1 = rect.pos.y;
1727 vars[head].sw = rect.size.width;
1728 vars[head].sh = rect.size.height;
1729 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1730 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1731 vars[head].sw = isize * (vars[head].sw / isize);
1732 vars[head].sh = isize * (vars[head].sh / isize);
1733 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1734 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1736 /* icon yard boundaries */
1737 if (wPreferences.icon_yard & IY_VERT) {
1738 vars[head].pf = vars[head].fullH;
1739 vars[head].sf = vars[head].fullW;
1740 } else {
1741 vars[head].pf = vars[head].fullW;
1742 vars[head].sf = vars[head].fullH;
1744 if (wPreferences.icon_yard & IY_RIGHT) {
1745 vars[head].xo = vars[head].sx2 - isize;
1746 vars[head].xs = -1;
1747 } else {
1748 vars[head].xo = vars[head].sx1;
1749 vars[head].xs = 1;
1751 if (wPreferences.icon_yard & IY_TOP) {
1752 vars[head].yo = vars[head].sy1;
1753 vars[head].ys = 1;
1754 } else {
1755 vars[head].yo = vars[head].sy2 - isize;
1756 vars[head].ys = -1;
1760 #define X ((wPreferences.icon_yard & IY_VERT) \
1761 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1762 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1764 #define Y ((wPreferences.icon_yard & IY_VERT) \
1765 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1766 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1768 /* arrange application icons */
1769 aicon = scr->app_icon_list;
1770 /* reverse them to avoid unnecessarily sliding of icons */
1771 while (aicon && aicon->next)
1772 aicon = aicon->next;
1774 while (aicon) {
1775 if (!aicon->docked) {
1776 /* CHECK: can icon be NULL here ? */
1777 /* The intention here is to place the AppIcon on the head that
1778 * contains most of the applications _main_ window. */
1779 head = wGetHeadForWindow(aicon->icon->owner);
1781 if (aicon->x_pos != X || aicon->y_pos != Y) {
1782 #ifdef ANIMATIONS
1783 if (!wPreferences.no_animations)
1784 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1785 #endif /* ANIMATIONS */
1787 wAppIconMove(aicon, X, Y);
1788 vars[head].pi++;
1789 if (vars[head].pi >= vars[head].pf) {
1790 vars[head].pi = 0;
1791 vars[head].si++;
1794 aicon = aicon->prev;
1797 /* arrange miniwindows */
1798 wwin = scr->focused_window;
1799 /* reverse them to avoid unnecessarily shuffling */
1800 while (wwin && wwin->prev)
1801 wwin = wwin->prev;
1803 while (wwin) {
1804 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1805 (wwin->frame->workspace == scr->current_workspace ||
1806 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1808 head = wGetHeadForWindow(wwin);
1810 if (arrangeAll || !wwin->flags.icon_moved) {
1811 if (wwin->icon_x != X || wwin->icon_y != Y)
1812 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1814 wwin->icon_x = X;
1815 wwin->icon_y = Y;
1817 vars[head].pi++;
1818 if (vars[head].pi >= vars[head].pf) {
1819 vars[head].pi = 0;
1820 vars[head].si++;
1824 if (arrangeAll) {
1825 wwin->flags.icon_moved = 0;
1827 /* we reversed the order, so we use next */
1828 wwin = wwin->next;
1831 wfree(vars);
1834 void wSelectWindow(WWindow *wwin, Bool flag)
1836 WScreen *scr = wwin->screen_ptr;
1838 if (flag) {
1839 wwin->flags.selected = 1;
1840 if (wwin->frame->selected_border_pixel)
1841 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1842 else
1843 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1845 if (!HAS_BORDER(wwin)) {
1846 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1849 if (!scr->selected_windows)
1850 scr->selected_windows = WMCreateArray(4);
1851 WMAddToArray(scr->selected_windows, wwin);
1852 } else {
1853 wwin->flags.selected = 0;
1854 if (wwin->frame->border_pixel)
1855 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1856 else
1857 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1859 if (!HAS_BORDER(wwin)) {
1860 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1863 if (scr->selected_windows) {
1864 WMRemoveFromArray(scr->selected_windows, wwin);
1869 void wMakeWindowVisible(WWindow *wwin)
1871 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1872 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1874 if (wwin->flags.shaded) {
1875 wUnshadeWindow(wwin);
1877 if (wwin->flags.hidden) {
1878 WApplication *app;
1880 app = wApplicationOf(wwin->main_window);
1881 if (app) {
1882 /* trick to get focus to this window */
1883 app->last_focused = wwin;
1884 wUnhideApplication(app, False, False);
1887 if (wwin->flags.miniaturized) {
1888 wDeiconifyWindow(wwin);
1889 } else {
1890 if (!WFLAGP(wwin, no_focusable))
1891 wSetFocusTo(wwin->screen_ptr, wwin);
1892 wRaiseFrame(wwin->frame->core);
1897 * Do the animation while shading (called with what = SHADE)
1898 * or unshading (what = UNSHADE).
1900 #ifdef ANIMATIONS
1901 static void shade_animate(WWindow *wwin, Bool what)
1903 int y, s, w, h;
1904 time_t time0 = time(NULL);
1906 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1907 return;
1909 switch(what) {
1910 case SHADE:
1911 if (!wwin->screen_ptr->flags.startup) {
1912 /* do the shading animation */
1913 h = wwin->frame->core->height;
1914 s = h / SHADE_STEPS;
1915 if (s < 1)
1916 s = 1;
1917 w = wwin->frame->core->width;
1918 y = wwin->frame->top_width;
1919 while (h > wwin->frame->top_width + 1) {
1920 XMoveWindow(dpy, wwin->client_win, 0, y);
1921 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1922 XFlush(dpy);
1924 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1925 break;
1927 if (SHADE_DELAY > 0) {
1928 wusleep(SHADE_DELAY * 1000L);
1929 } else {
1930 wusleep(10);
1932 h -= s;
1933 y -= s;
1935 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1937 break;
1939 case UNSHADE:
1940 h = wwin->frame->top_width + wwin->frame->bottom_width;
1941 y = wwin->frame->top_width - wwin->client.height;
1942 s = abs(y) / SHADE_STEPS;
1943 if (s < 1)
1944 s = 1;
1945 w = wwin->frame->core->width;
1946 XMoveWindow(dpy, wwin->client_win, 0, y);
1947 if (s > 0) {
1948 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1949 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1950 XMoveWindow(dpy, wwin->client_win, 0, y);
1951 XFlush(dpy);
1952 if (SHADE_DELAY > 0) {
1953 wusleep(SHADE_DELAY * 2000L / 3);
1954 } else {
1955 wusleep(10);
1957 h += s;
1958 y += s;
1960 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1961 break;
1964 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1965 break;
1968 #endif