4b336c941e2f4020c7f1a6cb4cb146c914f41e81
[wmaker-crm.git] / src / actions.c
blob4b336c941e2f4020c7f1a6cb4cb146c914f41e81
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"
51 #include "event.h"
54 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
55 unsigned int *new_width, unsigned int *new_height);
56 static void save_old_geometry(WWindow *wwin, int directions);
58 /******* Local Variables *******/
59 static struct {
60 int steps;
61 int delay;
62 } shadePars[5] = {
64 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
65 SHADE_STEPS_F, SHADE_DELAY_F}, {
66 SHADE_STEPS_M, SHADE_DELAY_M}, {
67 SHADE_STEPS_S, SHADE_DELAY_S}, {
68 SHADE_STEPS_US, SHADE_DELAY_US}};
70 #define UNSHADE 0
71 #define SHADE 1
72 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
73 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
75 static int compareTimes(Time t1, Time t2)
77 Time diff;
78 if (t1 == t2)
79 return 0;
80 diff = t1 - t2;
81 return (diff < 60000) ? 1 : -1;
84 #ifdef ANIMATIONS
85 static void shade_animate(WWindow *wwin, Bool what);
86 #else
87 static inline void shade_animate(WWindow *wwin, Bool what) {
89 * This function is empty on purpose, so tell the compiler
90 * to not warn about parameters being not used
92 (void) wwin;
93 (void) what;
95 #endif
98 *----------------------------------------------------------------------
99 * wSetFocusTo--
100 * Changes the window focus to the one passed as argument.
101 * If the window to focus is not already focused, it will be brought
102 * to the head of the list of windows. Previously focused window is
103 * unfocused.
105 * Side effects:
106 * Window list may be reordered and the window focus is changed.
108 *----------------------------------------------------------------------
110 void wSetFocusTo(WScreen *scr, WWindow *wwin)
112 static WScreen *old_scr = NULL;
114 WWindow *old_focused;
115 WWindow *focused = scr->focused_window;
116 Time timestamp = w_global.timestamp.last_event;
117 WApplication *oapp = NULL, *napp = NULL;
118 int wasfocused;
120 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
121 return;
123 if (!old_scr)
124 old_scr = scr;
126 old_focused = old_scr->focused_window;
128 w_global.timestamp.focus_change = timestamp;
130 if (old_focused)
131 oapp = wApplicationOf(old_focused->main_window);
133 if (wwin == NULL) {
134 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
135 if (old_focused)
136 wWindowUnfocus(old_focused);
138 if (oapp) {
139 wAppMenuUnmap(oapp->menu);
140 if (wPreferences.highlight_active_app)
141 wApplicationDeactivate(oapp);
144 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
145 return;
148 if (old_scr != scr && old_focused)
149 wWindowUnfocus(old_focused);
151 wasfocused = wwin->flags.focused;
152 napp = wApplicationOf(wwin->main_window);
154 /* remember last workspace where the app has been */
155 if (napp)
156 napp->last_workspace = wwin->frame->workspace;
158 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
159 /* install colormap if colormap mode is lock mode */
160 if (wPreferences.colormap_mode == WCM_CLICK)
161 wColormapInstallForWindow(scr, wwin);
163 /* set input focus */
164 switch (wwin->focus_mode) {
165 case WFM_NO_INPUT:
166 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
167 break;
168 case WFM_PASSIVE:
169 case WFM_LOCALLY_ACTIVE:
170 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
171 break;
172 case WFM_GLOBALLY_ACTIVE:
173 break;
176 XFlush(dpy);
177 if (wwin->protocols.TAKE_FOCUS)
178 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
180 XSync(dpy, False);
181 } else {
182 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
185 if (WFLAGP(wwin, no_focusable))
186 return;
188 /* if this is not the focused window focus it */
189 if (focused != wwin) {
190 /* change the focus window list order */
191 if (wwin->prev)
192 wwin->prev->next = wwin->next;
194 if (wwin->next)
195 wwin->next->prev = wwin->prev;
197 wwin->prev = focused;
198 focused->next = wwin;
199 wwin->next = NULL;
200 scr->focused_window = wwin;
202 if (oapp && oapp != napp) {
203 wAppMenuUnmap(oapp->menu);
204 if (wPreferences.highlight_active_app)
205 wApplicationDeactivate(oapp);
209 wWindowFocus(wwin, focused);
211 if (napp && !wasfocused) {
212 #ifdef USER_MENU
213 wUserMenuRefreshInstances(napp->menu, wwin);
214 #endif /* USER_MENU */
216 if (wwin->flags.mapped)
217 wAppMenuMap(napp->menu, wwin);
219 if (napp && wPreferences.highlight_active_app)
220 wApplicationActivate(napp);
222 XFlush(dpy);
223 old_scr = scr;
226 void wShadeWindow(WWindow *wwin)
229 if (wwin->flags.shaded)
230 return;
232 XLowerWindow(dpy, wwin->client_win);
233 shade_animate(wwin, SHADE);
235 wwin->flags.skip_next_animation = 0;
236 wwin->flags.shaded = 1;
237 wwin->flags.mapped = 0;
238 /* prevent window withdrawal when getting UnmapNotify */
239 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
240 XUnmapWindow(dpy, wwin->client_win);
241 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
243 /* for the client it's just like iconification */
244 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
246 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
247 wWindowSynthConfigureNotify(wwin);
250 wClientSetState(wwin, IconicState, None);
253 WMPostNotificationName(WMNChangedState, wwin, "shade");
255 #ifdef ANIMATIONS
256 if (!wwin->screen_ptr->flags.startup) {
257 /* Catch up with events not processed while animation was running */
258 ProcessPendingEvents();
260 #endif
263 void wUnshadeWindow(WWindow *wwin)
266 if (!wwin->flags.shaded)
267 return;
269 wwin->flags.shaded = 0;
270 wwin->flags.mapped = 1;
271 XMapWindow(dpy, wwin->client_win);
273 shade_animate(wwin, UNSHADE);
275 wwin->flags.skip_next_animation = 0;
276 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
277 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
279 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
280 wWindowSynthConfigureNotify(wwin);
282 /* if the window is focused, set the focus again as it was disabled during
283 * shading */
284 if (wwin->flags.focused)
285 wSetFocusTo(wwin->screen_ptr, wwin);
287 WMPostNotificationName(WMNChangedState, wwin, "shade");
290 /* Set the old coordinates using the current values */
291 static void save_old_geometry(WWindow *wwin, int directions)
293 /* never been saved? */
294 if (! wwin->old_geometry.width)
295 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
296 if (! wwin->old_geometry.height)
297 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
299 if (directions & SAVE_GEOMETRY_X)
300 wwin->old_geometry.x = wwin->frame_x;
301 if (directions & SAVE_GEOMETRY_Y)
302 wwin->old_geometry.y = wwin->frame_y;
303 if (directions & SAVE_GEOMETRY_WIDTH)
304 wwin->old_geometry.width = wwin->client.width;
305 if (directions & SAVE_GEOMETRY_HEIGHT)
306 wwin->old_geometry.height = wwin->client.height;
309 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
311 WMRect old_geom_rect;
312 int old_head;
313 Bool same_head;
315 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
316 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
317 same_head = (wGetHeadForWindow(wwin) == old_head);
318 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
319 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
320 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
321 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
324 /* Remember geometry for unmaximizing */
325 void update_saved_geometry(WWindow *wwin)
327 /* NOT if we aren't already maximized
328 * we'll save geometry when maximizing */
329 if (!wwin->flags.maximized)
330 return;
332 /* NOT if we are fully maximized */
333 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
334 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
335 (wwin->flags.maximized & MAX_VERTICAL)))
336 return;
338 /* save the co-ordinate in the axis in which we AREN'T maximized */
339 if (wwin->flags.maximized & MAX_HORIZONTAL)
340 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
341 if (wwin->flags.maximized & MAX_VERTICAL)
342 save_old_geometry(wwin, SAVE_GEOMETRY_X);
345 void wMaximizeWindow(WWindow *wwin, int directions)
347 unsigned int new_width, new_height, half_scr_width, half_scr_height;
348 int new_x = 0;
349 int new_y = 0;
350 int maximus_x = 0;
351 int maximus_y = 0;
352 unsigned int maximus_width = 0;
353 unsigned int maximus_height = 0;
354 WArea usableArea, totalArea;
355 Bool has_border = 1;
356 int adj_size;
357 WScreen *scr = wwin->screen_ptr;
359 if (!IS_RESIZABLE(wwin))
360 return;
362 if (!HAS_BORDER(wwin))
363 has_border = 0;
365 /* the size to adjust the geometry */
366 adj_size = scr->frame_border_width * 2 * has_border;
368 /* save old coordinates before we change the current values */
369 if (!wwin->flags.maximized)
370 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
372 totalArea.x2 = scr->scr_width;
373 totalArea.y2 = scr->scr_height;
374 totalArea.x1 = 0;
375 totalArea.y1 = 0;
376 usableArea = totalArea;
378 if (!(directions & MAX_IGNORE_XINERAMA)) {
379 WScreen *scr = wwin->screen_ptr;
380 int head;
382 if (directions & MAX_KEYBOARD)
383 head = wGetHeadForWindow(wwin);
384 else
385 head = wGetHeadForPointerLocation(scr);
387 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
390 /* check if user wants dock covered */
391 if (scr->dock && (!scr->dock->lowered || wPreferences.no_window_over_dock)) {
392 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
394 if (scr->dock->on_right_side)
395 usableArea.x2 -= offset;
396 else
397 usableArea.x1 += offset;
400 /* check if icons are on the same side as dock, and adjust if not done already */
401 if (scr->dock && wPreferences.no_window_over_icons && !wPreferences.no_window_over_dock && (wPreferences.icon_yard & IY_VERT)) {
402 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
404 if (scr->dock->on_right_side && (wPreferences.icon_yard & IY_RIGHT))
405 usableArea.x2 -= offset;
406 /* can't use IY_LEFT in if, it's 0 ... */
407 if (!scr->dock->on_right_side && !(wPreferences.icon_yard & IY_RIGHT))
408 usableArea.x1 += offset;
411 /* Only save directions, not kbd or xinerama hints */
412 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
414 if (WFLAGP(wwin, full_maximize)) {
415 usableArea = totalArea;
417 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
418 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
420 if (wwin->flags.shaded) {
421 wwin->flags.skip_next_animation = 1;
422 wUnshadeWindow(wwin);
425 if (directions & MAX_MAXIMUS) {
426 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
427 new_width = maximus_width - adj_size;
428 new_height = maximus_height - adj_size;
429 new_x = maximus_x;
430 new_y = maximus_y;
431 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
432 new_height += wwin->frame->bottom_width - 1;
433 new_y -= wwin->frame->top_width;
436 wwin->maximus_x = new_x;
437 wwin->maximus_y = new_y;
438 wwin->flags.old_maximized |= MAX_MAXIMUS;
439 } else {
440 /* set default values if no option set then */
441 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
442 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
443 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
445 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
446 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
447 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
450 /* left|right position */
451 if (directions & MAX_LEFTHALF) {
452 new_width = half_scr_width - adj_size;
453 new_x = usableArea.x1;
454 } else if (directions & MAX_RIGHTHALF) {
455 new_width = half_scr_width - adj_size;
456 new_x = usableArea.x1 + half_scr_width;
458 /* top|bottom position */
459 if (directions & MAX_TOPHALF) {
460 new_height = half_scr_height - adj_size;
461 new_y = usableArea.y1;
462 } else if (directions & MAX_BOTTOMHALF) {
463 new_height = half_scr_height - adj_size;
464 new_y = usableArea.y1 + half_scr_height;
467 /* vertical|horizontal position */
468 if (directions & MAX_HORIZONTAL) {
469 new_width = usableArea.x2 - usableArea.x1 - adj_size;
470 new_x = usableArea.x1;
472 if (directions & MAX_VERTICAL) {
473 new_height = usableArea.y2 - usableArea.y1 - adj_size;
474 new_y = usableArea.y1;
475 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
476 new_y -= wwin->frame->top_width;
480 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
481 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
483 /* set maximization state */
484 wwin->flags.maximized = directions;
485 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
486 wwin->flags.maximized = MAX_MAXIMUS;
488 wWindowConstrainSize(wwin, &new_width, &new_height);
490 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
491 usableArea.y2 - usableArea.y1, &new_width, &new_height);
493 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
494 wWindowSynthConfigureNotify(wwin);
496 WMPostNotificationName(WMNChangedState, wwin, "maximize");
499 /* generic (un)maximizer */
500 void handleMaximize(WWindow *wwin, int directions)
502 int current = wwin->flags.maximized;
503 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
504 int effective = requested ^ current;
505 int flags = directions & ~requested;
507 if (!effective) {
508 /* allow wMaximizeWindow to restore the Maximusized size */
509 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
510 !(requested & MAX_MAXIMUS))
511 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
512 else
513 wUnmaximizeWindow(wwin);
514 /* these alone mean vertical|horizontal toggle */
515 } else if ((effective == MAX_LEFTHALF) ||
516 (effective == MAX_RIGHTHALF) ||
517 (effective == MAX_TOPHALF) ||
518 (effective == MAX_BOTTOMHALF))
519 wUnmaximizeWindow(wwin);
520 else {
521 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
522 (requested == MAX_MAXIMUS))
523 effective = requested;
524 else {
525 if (requested & MAX_LEFTHALF) {
526 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
527 effective |= MAX_VERTICAL;
528 else
529 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
530 effective |= MAX_LEFTHALF;
531 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
532 } else if (requested & MAX_RIGHTHALF) {
533 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
534 effective |= MAX_VERTICAL;
535 else
536 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
537 effective |= MAX_RIGHTHALF;
538 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
540 if (requested & MAX_TOPHALF) {
541 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
542 effective |= MAX_HORIZONTAL;
543 else
544 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
545 effective |= MAX_TOPHALF;
546 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
547 } else if (requested & MAX_BOTTOMHALF) {
548 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
549 effective |= MAX_HORIZONTAL;
550 else
551 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
552 effective |= MAX_BOTTOMHALF;
553 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
555 if (requested & MAX_HORIZONTAL)
556 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
557 if (requested & MAX_VERTICAL)
558 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
559 effective &= ~MAX_MAXIMUS;
561 wMaximizeWindow(wwin, effective | flags);
565 /* the window boundary coordinates */
566 typedef struct {
567 int left;
568 int right;
569 int bottom;
570 int top;
571 int width;
572 int height;
573 } win_coords;
575 static void set_window_coords(WWindow *wwin, win_coords *obs)
577 obs->left = wwin->frame_x;
578 obs->top = wwin->frame_y;
579 obs->width = wwin->frame->core->width;
580 obs->height = wwin->frame->core->height;
581 obs->bottom = obs->top + obs->height;
582 obs->right = obs->left + obs->width;
586 * Maximus: tiled maximization (maximize without overlapping other windows)
588 * The original window 'orig' will be maximized to new coordinates 'new'.
589 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
591 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
592 unsigned int *new_width, unsigned int *new_height)
594 WWindow *tmp;
595 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
596 short int adjust_height;
597 int x_intsect, y_intsect;
598 /* the obstructing, original and new windows */
599 win_coords obs, orig, new;
601 /* set the original coordinate positions of the window to be Maximumized */
602 if (wwin->flags.maximized) {
603 /* window is already maximized; consider original geometry */
604 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
605 orig.bottom = orig.top + orig.height;
606 orig.right = orig.left + orig.width;
608 else
609 set_window_coords(wwin, &orig);
611 /* Try to fully maximize first, then readjust later */
612 new.left = usableArea.x1;
613 new.right = usableArea.x2;
614 new.top = usableArea.y1;
615 new.bottom = usableArea.y2;
617 if (HAS_TITLEBAR(wwin))
618 tbar_height_0 = TITLEBAR_HEIGHT;
619 if (HAS_RESIZEBAR(wwin))
620 rbar_height_0 = RESIZEBAR_HEIGHT;
621 if (HAS_BORDER(wwin))
622 bd_width_0 = wwin->screen_ptr->frame_border_width;
624 /* the length to be subtracted if the window has titlebar, etc */
625 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
627 tmp = wwin;
628 /* The focused window is always the last in the list */
629 while (tmp->prev) {
630 /* ignore windows in other workspaces etc */
631 if (tmp->prev->frame->workspace != w_global.workspace.current ||
632 tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
633 tmp = tmp->prev;
634 continue;
636 tmp = tmp->prev;
638 /* Set the coordinates of obstructing window */
639 set_window_coords(tmp, &obs);
641 /* Try to maximize in the y direction first */
642 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
643 if (x_intsect != 0) {
644 /* TODO: Consider the case when coords are equal */
645 if (obs.bottom < orig.top && obs.bottom > new.top) {
646 /* w_0 is below the bottom of w_j */
647 new.top = obs.bottom + 1;
649 if (orig.bottom < obs.top && obs.top < new.bottom) {
650 /* The bottom of w_0 is above the top of w_j */
651 new.bottom = obs.top - 1;
656 tmp = wwin;
657 while (tmp->prev) {
658 if (tmp->prev->frame->workspace != w_global.workspace.current ||
659 tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
660 tmp = tmp->prev;
661 continue;
663 tmp = tmp->prev;
665 set_window_coords(tmp, &obs);
668 * Use the new.top and new.height instead of original values
669 * as they may have different intersections with the obstructing windows
671 new.height = new.bottom - new.top - adjust_height;
672 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
673 if (y_intsect != 0) {
674 if (obs.right < orig.left && obs.right > new.left) {
675 /* w_0 is completely to the right of w_j */
676 new.left = obs.right + 1;
678 if (orig.right < obs.left && obs.left < new.right) {
679 /* w_0 is completely to the left of w_j */
680 new.right = obs.left - 1;
685 *new_x = new.left;
686 *new_y = new.top;
687 /* xcalc needs -7 here, but other apps don't */
688 *new_height = new.bottom - new.top - adjust_height - 1;;
689 *new_width = new.right - new.left;
692 void wUnmaximizeWindow(WWindow *wwin)
694 int x, y, w, h;
696 if (!wwin->flags.maximized)
697 return;
699 if (wwin->flags.shaded) {
700 wwin->flags.skip_next_animation = 1;
701 wUnshadeWindow(wwin);
704 /* Use old coordinates if they are set, current values otherwise */
705 remember_geometry(wwin, &x, &y, &w, &h);
707 /* unMaximusize relative to original position */
708 if (wwin->flags.maximized & MAX_MAXIMUS) {
709 x += wwin->frame_x - wwin->maximus_x;
710 y += wwin->frame_y - wwin->maximus_y;
713 wwin->flags.maximized = 0;
714 wwin->flags.old_maximized = 0;
715 wWindowConfigure(wwin, x, y, w, h);
716 wWindowSynthConfigureNotify(wwin);
718 WMPostNotificationName(WMNChangedState, wwin, "maximize");
721 void wFullscreenWindow(WWindow *wwin)
723 int head;
724 WMRect rect;
726 if (wwin->flags.fullscreen)
727 return;
729 wwin->flags.fullscreen = True;
731 wWindowConfigureBorders(wwin);
733 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
734 wRaiseFrame(wwin->frame->core);
736 wwin->bfs_geometry.x = wwin->frame_x;
737 wwin->bfs_geometry.y = wwin->frame_y;
738 wwin->bfs_geometry.width = wwin->frame->core->width;
739 wwin->bfs_geometry.height = wwin->frame->core->height;
741 head = wGetHeadForWindow(wwin);
742 rect = wGetRectForHead(wwin->screen_ptr, head);
743 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
745 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
746 wSetFocusTo(wwin->screen_ptr, wwin);
748 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
751 void wUnfullscreenWindow(WWindow *wwin)
753 if (!wwin->flags.fullscreen)
754 return;
756 wwin->flags.fullscreen = False;
758 if (WFLAGP(wwin, sunken)) {
759 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
760 } else if (WFLAGP(wwin, floating)) {
761 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
764 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
765 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
767 wWindowConfigureBorders(wwin);
769 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
770 wFrameWindowPaint(wwin->frame);
773 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
775 if (wwin->screen_ptr->bfs_focused_window) {
776 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
777 wwin->screen_ptr->bfs_focused_window = NULL;
781 #ifdef ANIMATIONS
782 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
784 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
785 float cx, cy, cw, ch;
786 float xstep, ystep, wstep, hstep;
787 XPoint points[5];
788 float dx, dch, midy;
789 float angle, final_angle, delta;
791 xstep = (float)(fx - x) / steps;
792 ystep = (float)(fy - y) / steps;
793 wstep = (float)(fw - w) / steps;
794 hstep = (float)(fh - h) / steps;
796 cx = (float)x;
797 cy = (float)y;
798 cw = (float)w;
799 ch = (float)h;
801 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
802 delta = (float)(final_angle / FRAMES);
803 for (angle = 0;; angle += delta) {
804 if (angle > final_angle)
805 angle = final_angle;
807 dx = (cw / 10) - ((cw / 5) * sin(angle));
808 dch = (ch / 2) * cos(angle);
809 midy = cy + (ch / 2);
811 points[0].x = cx + dx;
812 points[0].y = midy - dch;
813 points[1].x = cx + cw - dx;
814 points[1].y = points[0].y;
815 points[2].x = cx + cw + dx;
816 points[2].y = midy + dch;
817 points[3].x = cx - dx;
818 points[3].y = points[2].y;
819 points[4].x = points[0].x;
820 points[4].y = points[0].y;
822 XGrabServer(dpy);
823 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
824 XFlush(dpy);
825 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
827 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
828 XUngrabServer(dpy);
829 cx += xstep;
830 cy += ystep;
831 cw += wstep;
832 ch += hstep;
833 if (angle >= final_angle)
834 break;
837 XFlush(dpy);
840 #undef FRAMES
842 static void
843 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
845 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
846 float cx, cy, cw, ch;
847 float xstep, ystep, wstep, hstep;
848 XPoint points[5];
849 float angle, final_angle, a, d, delta;
851 x += w / 2;
852 y += h / 2;
853 fx += fw / 2;
854 fy += fh / 2;
856 xstep = (float)(fx - x) / steps;
857 ystep = (float)(fy - y) / steps;
858 wstep = (float)(fw - w) / steps;
859 hstep = (float)(fh - h) / steps;
861 cx = (float)x;
862 cy = (float)y;
863 cw = (float)w;
864 ch = (float)h;
866 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
867 delta = (float)(final_angle / FRAMES);
868 for (angle = 0;; angle += delta) {
869 if (angle > final_angle)
870 angle = final_angle;
872 a = atan(ch / cw);
873 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
875 points[0].x = cx + cos(angle - a) * d;
876 points[0].y = cy + sin(angle - a) * d;
877 points[1].x = cx + cos(angle + a) * d;
878 points[1].y = cy + sin(angle + a) * d;
879 points[2].x = cx + cos(angle - a + WM_PI) * d;
880 points[2].y = cy + sin(angle - a + WM_PI) * d;
881 points[3].x = cx + cos(angle + a + WM_PI) * d;
882 points[3].y = cy + sin(angle + a + WM_PI) * d;
883 points[4].x = cx + cos(angle - a) * d;
884 points[4].y = cy + sin(angle - a) * d;
885 XGrabServer(dpy);
886 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
887 XFlush(dpy);
888 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
890 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
891 XUngrabServer(dpy);
892 cx += xstep;
893 cy += ystep;
894 cw += wstep;
895 ch += hstep;
896 if (angle >= final_angle)
897 break;
900 XFlush(dpy);
903 #undef FRAMES
905 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
907 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
908 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
909 float xstep, ystep, wstep, hstep;
910 int i, j;
912 xstep = (float)(fx - x) / steps;
913 ystep = (float)(fy - y) / steps;
914 wstep = (float)(fw - w) / steps;
915 hstep = (float)(fh - h) / steps;
917 for (j = 0; j < FRAMES; j++) {
918 cx[j] = (float)x;
919 cy[j] = (float)y;
920 cw[j] = (float)w;
921 ch[j] = (float)h;
923 XGrabServer(dpy);
924 for (i = 0; i < steps; i++) {
925 for (j = 0; j < FRAMES; j++) {
926 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
927 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
929 XFlush(dpy);
930 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
932 for (j = 0; j < FRAMES; j++) {
933 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
934 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
935 if (j < FRAMES - 1) {
936 cx[j] = cx[j + 1];
937 cy[j] = cy[j + 1];
938 cw[j] = cw[j + 1];
939 ch[j] = ch[j + 1];
940 } else {
941 cx[j] += xstep;
942 cy[j] += ystep;
943 cw[j] += wstep;
944 ch[j] += hstep;
949 for (j = 0; j < FRAMES; j++) {
950 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
952 XFlush(dpy);
953 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
955 for (j = 0; j < FRAMES; j++) {
956 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
959 XUngrabServer(dpy);
962 #undef FRAMES
964 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
966 int style = wPreferences.iconification_style; /* Catch the value */
967 int steps;
969 if (style == WIS_NONE)
970 return;
972 if (style == WIS_RANDOM) {
973 style = rand() % 3;
976 switch (style) {
977 case WIS_TWIST:
978 steps = MINIATURIZE_ANIMATION_STEPS_T;
979 if (steps > 0)
980 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
981 break;
982 case WIS_FLIP:
983 steps = MINIATURIZE_ANIMATION_STEPS_F;
984 if (steps > 0)
985 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
986 break;
987 case WIS_ZOOM:
988 default:
989 steps = MINIATURIZE_ANIMATION_STEPS_Z;
990 if (steps > 0)
991 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
992 break;
995 #endif /* ANIMATIONS */
997 static void flushExpose(void)
999 XEvent tmpev;
1001 while (XCheckTypedEvent(dpy, Expose, &tmpev))
1002 WMHandleEvent(&tmpev);
1003 XSync(dpy, 0);
1006 static void unmapTransientsFor(WWindow *wwin)
1008 WWindow *tmp;
1010 tmp = wwin->screen_ptr->focused_window;
1011 while (tmp) {
1012 /* unmap the transients for this transient */
1013 if (tmp != wwin && tmp->transient_for == wwin->client_win
1014 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1015 unmapTransientsFor(tmp);
1016 tmp->flags.miniaturized = 1;
1017 if (!tmp->flags.shaded) {
1018 wWindowUnmap(tmp);
1019 } else {
1020 XUnmapWindow(dpy, tmp->frame->core->window);
1023 if (!tmp->flags.shaded)
1025 wClientSetState(tmp, IconicState, None);
1027 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1029 tmp = tmp->prev;
1033 static void mapTransientsFor(WWindow *wwin)
1035 WWindow *tmp;
1037 tmp = wwin->screen_ptr->focused_window;
1038 while (tmp) {
1039 /* recursively map the transients for this transient */
1040 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1041 && tmp->icon == NULL) {
1042 mapTransientsFor(tmp);
1043 tmp->flags.miniaturized = 0;
1044 if (!tmp->flags.shaded) {
1045 wWindowMap(tmp);
1046 } else {
1047 XMapWindow(dpy, tmp->frame->core->window);
1049 tmp->flags.semi_focused = 0;
1051 if (!tmp->flags.shaded)
1053 wClientSetState(tmp, NormalState, None);
1055 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1057 tmp = tmp->prev;
1061 static WWindow *recursiveTransientFor(WWindow * wwin)
1063 int i;
1065 if (!wwin)
1066 return None;
1068 /* hackish way to detect transient_for cycle */
1069 i = wwin->screen_ptr->window_count + 1;
1071 while (wwin && wwin->transient_for != None && i > 0) {
1072 wwin = wWindowFor(wwin->transient_for);
1073 i--;
1075 if (i == 0 && wwin) {
1076 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1077 return NULL;
1080 return wwin;
1083 void wIconifyWindow(WWindow * wwin)
1085 XWindowAttributes attribs;
1086 int present;
1088 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1089 return; /* the window doesn't exist anymore */
1091 if (wwin->flags.miniaturized)
1092 return; /* already miniaturized */
1094 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1095 WWindow *owner = wWindowFor(wwin->transient_for);
1097 if (owner && owner->flags.miniaturized)
1098 return;
1101 present = wwin->frame->workspace == w_global.workspace.current;
1103 /* if the window is in another workspace, simplify process */
1104 if (present) {
1105 /* icon creation may take a while */
1106 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1107 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1108 GrabModeAsync, None, None, CurrentTime);
1111 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1112 if (!wwin->flags.icon_moved)
1113 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1115 wwin->icon = icon_create_for_wwindow(wwin);
1116 wwin->icon->mapped = 1;
1119 wwin->flags.miniaturized = 1;
1120 wwin->flags.mapped = 0;
1122 /* unmap transients */
1123 unmapTransientsFor(wwin);
1125 if (present) {
1126 XUngrabPointer(dpy, CurrentTime);
1127 wWindowUnmap(wwin);
1128 /* let all Expose events arrive so that we can repaint
1129 * something before the animation starts (and the server is grabbed) */
1130 XSync(dpy, 0);
1132 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1133 wClientSetState(wwin, IconicState, None);
1134 else
1135 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1137 flushExpose();
1138 #ifdef ANIMATIONS
1139 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1140 && !wPreferences.no_animations) {
1141 int ix, iy, iw, ih;
1143 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1144 ix = wwin->icon_x;
1145 iy = wwin->icon_y;
1146 iw = wwin->icon->core->width;
1147 ih = wwin->icon->core->height;
1148 } else {
1149 if (wwin->flags.net_handle_icon) {
1150 ix = wwin->icon_x;
1151 iy = wwin->icon_y;
1152 iw = wwin->icon_w;
1153 ih = wwin->icon_h;
1154 } else {
1155 ix = 0;
1156 iy = 0;
1157 iw = wwin->screen_ptr->scr_width;
1158 ih = wwin->screen_ptr->scr_height;
1161 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1162 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1164 #endif
1167 wwin->flags.skip_next_animation = 0;
1169 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1170 if (w_global.workspace.current == wwin->frame->workspace ||
1171 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1172 XMapWindow(dpy, wwin->icon->core->window);
1174 AddToStackList(wwin->icon->core);
1175 wLowerFrame(wwin->icon->core);
1178 if (present) {
1179 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1182 * It doesn't seem to be working and causes button event hangup
1183 * when deiconifying a transient window.
1184 setupIconGrabs(wwin->icon);
1186 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1187 && wPreferences.focus_mode == WKF_CLICK) {
1188 WWindow *tmp;
1190 tmp = wwin->prev;
1191 while (tmp) {
1192 if (!WFLAGP(tmp, no_focusable)
1193 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1194 && (wwin->frame->workspace == tmp->frame->workspace))
1195 break;
1196 tmp = tmp->prev;
1198 wSetFocusTo(wwin->screen_ptr, tmp);
1199 } else if (wPreferences.focus_mode != WKF_CLICK) {
1200 wSetFocusTo(wwin->screen_ptr, NULL);
1202 #ifdef ANIMATIONS
1203 if (!wwin->screen_ptr->flags.startup) {
1204 /* Catch up with events not processed while animation was running */
1205 Window clientwin = wwin->client_win;
1207 ProcessPendingEvents();
1209 /* the window can disappear while ProcessPendingEvents() runs */
1210 if (!wWindowFor(clientwin)) {
1211 return;
1214 #endif
1217 /* maybe we want to do this regardless of net_handle_icon
1218 * it seems to me we might break behaviour this way.
1220 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1221 && !wwin->flags.net_handle_icon)
1222 wIconSelect(wwin->icon);
1224 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1226 if (wPreferences.auto_arrange_icons)
1227 wArrangeIcons(wwin->screen_ptr, True);
1230 void wDeiconifyWindow(WWindow *wwin)
1232 /* Let's avoid changing workspace while deiconifying */
1233 w_global.workspace.ignore_change = True;
1235 /* we're hiding for show_desktop */
1236 int netwm_hidden = wwin->flags.net_show_desktop &&
1237 wwin->frame->workspace != w_global.workspace.current;
1239 if (!netwm_hidden)
1240 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1242 if (!wwin->flags.miniaturized) {
1243 w_global.workspace.ignore_change = False;
1244 return;
1247 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1248 WWindow *owner = recursiveTransientFor(wwin);
1250 if (owner && owner->flags.miniaturized) {
1251 wDeiconifyWindow(owner);
1252 wSetFocusTo(wwin->screen_ptr, wwin);
1253 wRaiseFrame(wwin->frame->core);
1254 w_global.workspace.ignore_change = False;
1255 return;
1259 wwin->flags.miniaturized = 0;
1261 if (!netwm_hidden && !wwin->flags.shaded)
1262 wwin->flags.mapped = 1;
1264 if (!netwm_hidden || wPreferences.sticky_icons) {
1265 /* maybe we want to do this regardless of net_handle_icon
1266 * it seems to me we might break behaviour this way.
1268 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1269 && wwin->icon != NULL) {
1270 if (wwin->icon->selected)
1271 wIconSelect(wwin->icon);
1273 XUnmapWindow(dpy, wwin->icon->core->window);
1277 /* if the window is in another workspace, do it silently */
1278 if (!netwm_hidden) {
1279 #ifdef ANIMATIONS
1280 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1281 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1282 int ix, iy, iw, ih;
1284 if (!wPreferences.disable_miniwindows
1285 && !wwin->flags.net_handle_icon) {
1286 ix = wwin->icon_x;
1287 iy = wwin->icon_y;
1288 iw = wwin->icon->core->width;
1289 ih = wwin->icon->core->height;
1290 } else {
1291 if (wwin->flags.net_handle_icon) {
1292 ix = wwin->icon_x;
1293 iy = wwin->icon_y;
1294 iw = wwin->icon_w;
1295 ih = wwin->icon_h;
1296 } else {
1297 ix = 0;
1298 iy = 0;
1299 iw = wwin->screen_ptr->scr_width;
1300 ih = wwin->screen_ptr->scr_height;
1303 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1304 wwin->frame_x, wwin->frame_y,
1305 wwin->frame->core->width, wwin->frame->core->height);
1307 #endif /* ANIMATIONS */
1308 wwin->flags.skip_next_animation = 0;
1309 XGrabServer(dpy);
1310 if (!wwin->flags.shaded)
1311 XMapWindow(dpy, wwin->client_win);
1313 XMapWindow(dpy, wwin->frame->core->window);
1314 wRaiseFrame(wwin->frame->core);
1315 if (!wwin->flags.shaded)
1316 wClientSetState(wwin, NormalState, None);
1318 mapTransientsFor(wwin);
1321 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1322 && !wwin->flags.net_handle_icon) {
1323 RemoveFromStackList(wwin->icon->core);
1324 /* removeIconGrabs(wwin->icon); */
1325 wIconDestroy(wwin->icon);
1326 wwin->icon = NULL;
1329 if (!netwm_hidden) {
1330 XUngrabServer(dpy);
1332 wSetFocusTo(wwin->screen_ptr, wwin);
1334 #ifdef ANIMATIONS
1335 if (!wwin->screen_ptr->flags.startup) {
1336 /* Catch up with events not processed while animation was running */
1337 Window clientwin = wwin->client_win;
1339 ProcessPendingEvents();
1341 /* the window can disappear while ProcessPendingEvents() runs */
1342 if (!wWindowFor(clientwin)) {
1343 w_global.workspace.ignore_change = False;
1344 return;
1347 #endif
1350 if (wPreferences.auto_arrange_icons)
1351 wArrangeIcons(wwin->screen_ptr, True);
1353 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1355 /* In case we were shaded and iconified, also unshade */
1356 if (!netwm_hidden)
1357 wUnshadeWindow(wwin);
1359 w_global.workspace.ignore_change = False;
1362 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1364 if (wwin->flags.miniaturized) {
1365 if (wwin->icon) {
1366 XUnmapWindow(dpy, wwin->icon->core->window);
1367 wwin->icon->mapped = 0;
1369 wwin->flags.hidden = 1;
1371 WMPostNotificationName(WMNChangedState, wwin, "hide");
1372 return;
1375 if (wwin->flags.inspector_open) {
1376 wHideInspectorForWindow(wwin);
1379 wwin->flags.hidden = 1;
1380 wWindowUnmap(wwin);
1382 wClientSetState(wwin, IconicState, icon->icon_win);
1383 flushExpose();
1385 #ifdef ANIMATIONS
1386 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1387 !wwin->flags.skip_next_animation && animate) {
1388 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1389 wwin->frame->core->width, wwin->frame->core->height,
1390 icon_x, icon_y, icon->core->width, icon->core->height);
1392 #endif
1393 wwin->flags.skip_next_animation = 0;
1395 WMPostNotificationName(WMNChangedState, wwin, "hide");
1398 void wHideAll(WScreen *scr)
1400 WWindow *wwin;
1401 WWindow **windows;
1402 WMenu *menu;
1403 unsigned int wcount = 0;
1404 int i;
1406 if (!scr)
1407 return;
1409 menu = scr->switch_menu;
1411 windows = wmalloc(sizeof(WWindow *));
1413 if (menu != NULL) {
1414 for (i = 0; i < menu->entry_no; i++) {
1415 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1416 wcount++;
1417 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1419 } else {
1420 wwin = scr->focused_window;
1422 while (wwin) {
1423 windows[wcount] = wwin;
1424 wcount++;
1425 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1426 wwin = wwin->prev;
1431 for (i = 0; i < wcount; i++) {
1432 wwin = windows[i];
1433 if (wwin->frame->workspace == w_global.workspace.current
1434 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1435 && !wwin->flags.internal_window
1436 && !WFLAGP(wwin, no_miniaturizable)
1438 wwin->flags.skip_next_animation = 1;
1439 wIconifyWindow(wwin);
1443 wfree(windows);
1446 void wHideOtherApplications(WWindow *awin)
1448 WWindow *wwin;
1449 WApplication *tapp;
1451 if (!awin)
1452 return;
1453 wwin = awin->screen_ptr->focused_window;
1455 while (wwin) {
1456 if (wwin != awin
1457 && wwin->frame->workspace == w_global.workspace.current
1458 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1459 && !wwin->flags.internal_window
1460 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1462 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1463 if (!WFLAGP(wwin, no_miniaturizable)) {
1464 wwin->flags.skip_next_animation = 1;
1465 wIconifyWindow(wwin);
1467 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1468 tapp = wApplicationOf(wwin->main_window);
1469 if (tapp) {
1470 tapp->flags.skip_next_animation = 1;
1471 wHideApplication(tapp);
1472 } else {
1473 if (!WFLAGP(wwin, no_miniaturizable)) {
1474 wwin->flags.skip_next_animation = 1;
1475 wIconifyWindow(wwin);
1480 wwin = wwin->prev;
1483 wSetFocusTo(awin->screen_ptr, awin);
1487 void wHideApplication(WApplication *wapp)
1489 WScreen *scr;
1490 WWindow *wlist;
1491 int hadfocus;
1492 int animate;
1494 if (!wapp) {
1495 wwarning("trying to hide a non grouped window");
1496 return;
1498 if (!wapp->main_window_desc) {
1499 wwarning("group leader not found for window group");
1500 return;
1502 scr = wapp->main_window_desc->screen_ptr;
1503 hadfocus = 0;
1504 wlist = scr->focused_window;
1505 if (!wlist)
1506 return;
1508 if (wlist->main_window == wapp->main_window)
1509 wapp->last_focused = wlist;
1510 else
1511 wapp->last_focused = NULL;
1513 animate = !wapp->flags.skip_next_animation;
1515 while (wlist) {
1516 if (wlist->main_window == wapp->main_window) {
1517 if (wlist->flags.focused) {
1518 hadfocus = 1;
1520 if (wapp->app_icon) {
1521 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1522 wapp->app_icon->y_pos, wlist, animate);
1523 animate = False;
1526 wlist = wlist->prev;
1529 wapp->flags.skip_next_animation = 0;
1531 if (hadfocus) {
1532 if (wPreferences.focus_mode == WKF_CLICK) {
1533 wlist = scr->focused_window;
1534 while (wlist) {
1535 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1536 && (wlist->flags.mapped || wlist->flags.shaded))
1537 break;
1538 wlist = wlist->prev;
1540 wSetFocusTo(scr, wlist);
1541 } else {
1542 wSetFocusTo(scr, NULL);
1546 wapp->flags.hidden = 1;
1548 if (wPreferences.auto_arrange_icons)
1549 wArrangeIcons(scr, True);
1551 #ifdef HIDDENDOT
1552 if (wapp->app_icon)
1553 wAppIconPaint(wapp->app_icon);
1554 #endif
1557 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1559 if (bringToCurrentWS)
1560 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1562 wwin->flags.hidden = 0;
1564 #ifdef ANIMATIONS
1565 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1566 animateResize(wwin->screen_ptr, icon_x, icon_y,
1567 icon->core->width, icon->core->height,
1568 wwin->frame_x, wwin->frame_y,
1569 wwin->frame->core->width, wwin->frame->core->height);
1571 #endif
1572 wwin->flags.skip_next_animation = 0;
1573 if (w_global.workspace.current == wwin->frame->workspace) {
1574 XMapWindow(dpy, wwin->client_win);
1575 XMapWindow(dpy, wwin->frame->core->window);
1576 wClientSetState(wwin, NormalState, None);
1577 wwin->flags.mapped = 1;
1578 wRaiseFrame(wwin->frame->core);
1580 if (wwin->flags.inspector_open) {
1581 wUnhideInspectorForWindow(wwin);
1584 WMPostNotificationName(WMNChangedState, wwin, "hide");
1587 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1589 WScreen *scr;
1590 WWindow *wlist, *next;
1591 WWindow *focused = NULL;
1592 int animate;
1594 if (!wapp)
1595 return;
1597 scr = wapp->main_window_desc->screen_ptr;
1598 wlist = scr->focused_window;
1599 if (!wlist)
1600 return;
1602 /* goto beginning of list */
1603 while (wlist->prev)
1604 wlist = wlist->prev;
1606 animate = !wapp->flags.skip_next_animation;
1608 while (wlist) {
1609 next = wlist->next;
1611 if (wlist->main_window == wapp->main_window) {
1612 if (wlist->flags.focused)
1613 focused = wlist;
1614 else if (!focused || !focused->flags.focused)
1615 focused = wlist;
1617 if (wlist->flags.miniaturized) {
1618 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1619 wlist->frame->workspace == w_global.workspace.current) && wlist->icon) {
1620 if (!wlist->icon->mapped) {
1621 int x, y;
1623 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1624 if (wlist->icon_x != x || wlist->icon_y != y) {
1625 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1627 wlist->icon_x = x;
1628 wlist->icon_y = y;
1629 XMapWindow(dpy, wlist->icon->core->window);
1630 wlist->icon->mapped = 1;
1632 wRaiseFrame(wlist->icon->core);
1634 if (bringToCurrentWS)
1635 wWindowChangeWorkspace(wlist, w_global.workspace.current);
1636 wlist->flags.hidden = 0;
1637 if (miniwindows && wlist->frame->workspace == w_global.workspace.current) {
1638 wDeiconifyWindow(wlist);
1640 WMPostNotificationName(WMNChangedState, wlist, "hide");
1641 } else if (wlist->flags.shaded) {
1642 if (bringToCurrentWS)
1643 wWindowChangeWorkspace(wlist, w_global.workspace.current);
1644 wlist->flags.hidden = 0;
1645 wRaiseFrame(wlist->frame->core);
1646 if (wlist->frame->workspace == w_global.workspace.current) {
1647 XMapWindow(dpy, wlist->frame->core->window);
1648 if (miniwindows) {
1649 wUnshadeWindow(wlist);
1652 WMPostNotificationName(WMNChangedState, wlist, "hide");
1653 } else if (wlist->flags.hidden) {
1654 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1655 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1656 animate = False;
1657 } else {
1658 if (bringToCurrentWS && wlist->frame->workspace != w_global.workspace.current)
1659 wWindowChangeWorkspace(wlist, w_global.workspace.current);
1661 wRaiseFrame(wlist->frame->core);
1664 wlist = next;
1667 wapp->flags.skip_next_animation = 0;
1668 wapp->flags.hidden = 0;
1670 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1671 wRaiseFrame(wapp->last_focused->frame->core);
1672 wSetFocusTo(scr, wapp->last_focused);
1673 } else if (focused) {
1674 wSetFocusTo(scr, focused);
1676 wapp->last_focused = NULL;
1677 if (wPreferences.auto_arrange_icons)
1678 wArrangeIcons(scr, True);
1680 #ifdef HIDDENDOT
1681 wAppIconPaint(wapp->app_icon);
1682 #endif
1685 void wShowAllWindows(WScreen *scr)
1687 WWindow *wwin, *old_foc;
1688 WApplication *wapp;
1690 old_foc = wwin = scr->focused_window;
1691 while (wwin) {
1692 if (!wwin->flags.internal_window &&
1693 (w_global.workspace.current == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1694 if (wwin->flags.miniaturized) {
1695 wwin->flags.skip_next_animation = 1;
1696 wDeiconifyWindow(wwin);
1697 } else if (wwin->flags.hidden) {
1698 wapp = wApplicationOf(wwin->main_window);
1699 if (wapp) {
1700 wUnhideApplication(wapp, False, False);
1701 } else {
1702 wwin->flags.skip_next_animation = 1;
1703 wDeiconifyWindow(wwin);
1707 wwin = wwin->prev;
1709 wSetFocusTo(scr, old_foc);
1710 /*wRaiseFrame(old_foc->frame->core); */
1713 void wRefreshDesktop(WScreen *scr)
1715 Window win;
1716 XSetWindowAttributes attr;
1718 attr.backing_store = NotUseful;
1719 attr.save_under = False;
1720 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1721 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1722 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1723 XMapRaised(dpy, win);
1724 XDestroyWindow(dpy, win);
1725 XFlush(dpy);
1728 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1730 WWindow *wwin;
1731 WAppIcon *aicon;
1733 int head;
1734 const int heads = wXineramaHeads(scr);
1736 struct HeadVars {
1737 int pf; /* primary axis */
1738 int sf; /* secondary axis */
1739 int fullW;
1740 int fullH;
1741 int pi, si;
1742 int sx1, sx2, sy1, sy2; /* screen boundary */
1743 int sw, sh;
1744 int xo, yo;
1745 int xs, ys;
1746 } *vars;
1748 int isize = wPreferences.icon_size;
1750 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1752 for (head = 0; head < heads; ++head) {
1753 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1754 WMRect rect;
1756 if (scr->dock) {
1757 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1759 if (scr->dock->on_right_side)
1760 area.x2 -= offset;
1761 else
1762 area.x1 += offset;
1765 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1767 vars[head].pi = vars[head].si = 0;
1768 vars[head].sx1 = rect.pos.x;
1769 vars[head].sy1 = rect.pos.y;
1770 vars[head].sw = rect.size.width;
1771 vars[head].sh = rect.size.height;
1772 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1773 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1774 vars[head].sw = isize * (vars[head].sw / isize);
1775 vars[head].sh = isize * (vars[head].sh / isize);
1776 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1777 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1779 /* icon yard boundaries */
1780 if (wPreferences.icon_yard & IY_VERT) {
1781 vars[head].pf = vars[head].fullH;
1782 vars[head].sf = vars[head].fullW;
1783 } else {
1784 vars[head].pf = vars[head].fullW;
1785 vars[head].sf = vars[head].fullH;
1787 if (wPreferences.icon_yard & IY_RIGHT) {
1788 vars[head].xo = vars[head].sx2 - isize;
1789 vars[head].xs = -1;
1790 } else {
1791 vars[head].xo = vars[head].sx1;
1792 vars[head].xs = 1;
1794 if (wPreferences.icon_yard & IY_TOP) {
1795 vars[head].yo = vars[head].sy1;
1796 vars[head].ys = 1;
1797 } else {
1798 vars[head].yo = vars[head].sy2 - isize;
1799 vars[head].ys = -1;
1803 #define X ((wPreferences.icon_yard & IY_VERT) \
1804 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1805 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1807 #define Y ((wPreferences.icon_yard & IY_VERT) \
1808 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1809 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1811 /* arrange application icons */
1812 aicon = w_global.app_icon_list;
1813 /* reverse them to avoid unnecessarily sliding of icons */
1814 while (aicon && aicon->next)
1815 aicon = aicon->next;
1817 while (aicon) {
1818 if (!aicon->docked) {
1819 /* CHECK: can icon be NULL here ? */
1820 /* The intention here is to place the AppIcon on the head that
1821 * contains most of the applications _main_ window. */
1822 head = wGetHeadForWindow(aicon->icon->owner);
1824 if (aicon->x_pos != X || aicon->y_pos != Y) {
1825 #ifdef ANIMATIONS
1826 if (!wPreferences.no_animations)
1827 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1828 #endif /* ANIMATIONS */
1830 wAppIconMove(aicon, X, Y);
1831 vars[head].pi++;
1832 if (vars[head].pi >= vars[head].pf) {
1833 vars[head].pi = 0;
1834 vars[head].si++;
1837 aicon = aicon->prev;
1840 /* arrange miniwindows */
1841 wwin = scr->focused_window;
1842 /* reverse them to avoid unnecessarily shuffling */
1843 while (wwin && wwin->prev)
1844 wwin = wwin->prev;
1846 while (wwin) {
1847 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1848 (wwin->frame->workspace == w_global.workspace.current ||
1849 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1851 head = wGetHeadForWindow(wwin);
1853 if (arrangeAll || !wwin->flags.icon_moved) {
1854 if (wwin->icon_x != X || wwin->icon_y != Y)
1855 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1857 wwin->icon_x = X;
1858 wwin->icon_y = Y;
1860 vars[head].pi++;
1861 if (vars[head].pi >= vars[head].pf) {
1862 vars[head].pi = 0;
1863 vars[head].si++;
1867 if (arrangeAll) {
1868 wwin->flags.icon_moved = 0;
1870 /* we reversed the order, so we use next */
1871 wwin = wwin->next;
1874 wfree(vars);
1877 void wSelectWindow(WWindow *wwin, Bool flag)
1879 WScreen *scr = wwin->screen_ptr;
1881 if (flag) {
1882 wwin->flags.selected = 1;
1883 if (wwin->frame->selected_border_pixel)
1884 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1885 else
1886 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1888 if (!HAS_BORDER(wwin)) {
1889 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1892 if (!scr->selected_windows)
1893 scr->selected_windows = WMCreateArray(4);
1894 WMAddToArray(scr->selected_windows, wwin);
1895 } else {
1896 wwin->flags.selected = 0;
1897 if (wwin->frame->border_pixel)
1898 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1899 else
1900 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1902 if (!HAS_BORDER(wwin)) {
1903 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1906 if (scr->selected_windows) {
1907 WMRemoveFromArray(scr->selected_windows, wwin);
1912 void wMakeWindowVisible(WWindow *wwin)
1914 if (wwin->frame->workspace != w_global.workspace.current)
1915 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1917 if (wwin->flags.shaded)
1918 wUnshadeWindow(wwin);
1920 if (wwin->flags.hidden) {
1921 WApplication *app;
1923 app = wApplicationOf(wwin->main_window);
1924 if (app) {
1925 /* trick to get focus to this window */
1926 app->last_focused = wwin;
1927 wUnhideApplication(app, False, False);
1930 if (wwin->flags.miniaturized) {
1931 wDeiconifyWindow(wwin);
1932 } else {
1933 if (!WFLAGP(wwin, no_focusable))
1934 wSetFocusTo(wwin->screen_ptr, wwin);
1935 wRaiseFrame(wwin->frame->core);
1940 * Do the animation while shading (called with what = SHADE)
1941 * or unshading (what = UNSHADE).
1943 #ifdef ANIMATIONS
1944 static void shade_animate(WWindow *wwin, Bool what)
1946 int y, s, w, h;
1947 time_t time0 = time(NULL);
1949 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1950 return;
1952 switch(what) {
1953 case SHADE:
1954 if (!wwin->screen_ptr->flags.startup) {
1955 /* do the shading animation */
1956 h = wwin->frame->core->height;
1957 s = h / SHADE_STEPS;
1958 if (s < 1)
1959 s = 1;
1960 w = wwin->frame->core->width;
1961 y = wwin->frame->top_width;
1962 while (h > wwin->frame->top_width + 1) {
1963 XMoveWindow(dpy, wwin->client_win, 0, y);
1964 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1965 XFlush(dpy);
1967 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1968 break;
1970 if (SHADE_DELAY > 0) {
1971 wusleep(SHADE_DELAY * 1000L);
1972 } else {
1973 wusleep(10);
1975 h -= s;
1976 y -= s;
1978 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1980 break;
1982 case UNSHADE:
1983 h = wwin->frame->top_width + wwin->frame->bottom_width;
1984 y = wwin->frame->top_width - wwin->client.height;
1985 s = abs(y) / SHADE_STEPS;
1986 if (s < 1)
1987 s = 1;
1988 w = wwin->frame->core->width;
1989 XMoveWindow(dpy, wwin->client_win, 0, y);
1990 if (s > 0) {
1991 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1992 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1993 XMoveWindow(dpy, wwin->client_win, 0, y);
1994 XFlush(dpy);
1995 if (SHADE_DELAY > 0) {
1996 wusleep(SHADE_DELAY * 2000L / 3);
1997 } else {
1998 wusleep(10);
2000 h += s;
2001 y += s;
2003 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
2004 break;
2007 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2008 break;
2011 #endif