wmaker: reset "ignore workspace change" flag in all cases
[wmaker-crm.git] / src / actions.c
blob5ceb9d5777f204ad19bff333388670cdae603f69
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"
53 /****** Global Variables ******/
55 int ignore_wks_change = 0;
56 extern Time LastTimestamp;
57 extern Time LastFocusChange;
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;
413 wwin->maximus_x = new_x;
414 wwin->maximus_y = new_y;
415 wwin->flags.old_maximized |= MAX_MAXIMUS;
416 } else {
417 /* set default values if no option set then */
418 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
419 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
420 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
422 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
423 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
424 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
427 /* left|right position */
428 if (directions & MAX_LEFTHALF) {
429 new_width = half_scr_width - adj_size;
430 new_x = usableArea.x1;
431 } else if (directions & MAX_RIGHTHALF) {
432 new_width = half_scr_width - adj_size;
433 new_x = usableArea.x1 + half_scr_width;
435 /* top|bottom position */
436 if (directions & MAX_TOPHALF) {
437 new_height = half_scr_height - adj_size;
438 new_y = usableArea.y1;
439 } else if (directions & MAX_BOTTOMHALF) {
440 new_height = half_scr_height - adj_size;
441 new_y = usableArea.y1 + half_scr_height;
444 /* vertical|horizontal position */
445 if (directions & MAX_HORIZONTAL) {
446 new_width = usableArea.x2 - usableArea.x1 - adj_size;
447 new_x = usableArea.x1;
449 if (directions & MAX_VERTICAL) {
450 new_height = usableArea.y2 - usableArea.y1 - adj_size;
451 new_y = usableArea.y1;
452 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
453 new_y -= wwin->frame->top_width;
457 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
458 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
460 /* set maximization state */
461 wwin->flags.maximized = directions;
462 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
463 wwin->flags.maximized = MAX_MAXIMUS;
465 wWindowConstrainSize(wwin, &new_width, &new_height);
467 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
468 usableArea.y2 - usableArea.y1, &new_width, &new_height);
470 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
471 wWindowSynthConfigureNotify(wwin);
473 WMPostNotificationName(WMNChangedState, wwin, "maximize");
476 /* generic (un)maximizer */
477 void handleMaximize(WWindow *wwin, int directions)
479 int current = wwin->flags.maximized;
480 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
481 int effective = requested ^ current;
482 int flags = directions & ~requested;
484 if (!effective) {
485 /* allow wMaximizeWindow to restore the Maximusized size */
486 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
487 !(requested & MAX_MAXIMUS))
488 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
489 else
490 wUnmaximizeWindow(wwin);
491 /* these alone mean vertical|horizontal toggle */
492 } else if ((effective == MAX_LEFTHALF) ||
493 (effective == MAX_RIGHTHALF) ||
494 (effective == MAX_TOPHALF) ||
495 (effective == MAX_BOTTOMHALF))
496 wUnmaximizeWindow(wwin);
497 else {
498 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
499 (requested == MAX_MAXIMUS))
500 effective = requested;
501 else {
502 if (requested & MAX_LEFTHALF) {
503 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
504 effective |= MAX_VERTICAL;
505 else
506 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
507 effective |= MAX_LEFTHALF;
508 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
509 } else if (requested & MAX_RIGHTHALF) {
510 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
511 effective |= MAX_VERTICAL;
512 else
513 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
514 effective |= MAX_RIGHTHALF;
515 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
517 if (requested & MAX_TOPHALF) {
518 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
519 effective |= MAX_HORIZONTAL;
520 else
521 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
522 effective |= MAX_TOPHALF;
523 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
524 } else if (requested & MAX_BOTTOMHALF) {
525 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
526 effective |= MAX_HORIZONTAL;
527 else
528 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
529 effective |= MAX_BOTTOMHALF;
530 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
532 if (requested & MAX_HORIZONTAL)
533 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
534 if (requested & MAX_VERTICAL)
535 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
536 effective &= ~MAX_MAXIMUS;
538 wMaximizeWindow(wwin, effective | flags);
542 /* the window boundary coordinates */
543 typedef struct {
544 int left;
545 int right;
546 int bottom;
547 int top;
548 int width;
549 int height;
550 } win_coords;
552 static void set_window_coords(WWindow *wwin, win_coords *obs)
554 obs->left = wwin->frame_x;
555 obs->top = wwin->frame_y;
556 obs->width = wwin->frame->core->width;
557 obs->height = wwin->frame->core->height;
558 obs->bottom = obs->top + obs->height;
559 obs->right = obs->left + obs->width;
563 * Maximus: tiled maximization (maximize without overlapping other windows)
565 * The original window 'orig' will be maximized to new coordinates 'new'.
566 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
568 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
569 unsigned int *new_width, unsigned int *new_height)
571 WWindow *tmp;
572 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
573 short int adjust_height;
574 int x_intsect, y_intsect;
575 /* the obstructing, original and new windows */
576 win_coords obs, orig, new;
578 /* set the original coordinate positions of the window to be Maximumized */
579 if (wwin->flags.maximized) {
580 /* window is already maximized; consider original geometry */
581 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
582 orig.bottom = orig.top + orig.height;
583 orig.right = orig.left + orig.width;
585 else
586 set_window_coords(wwin, &orig);
588 /* Try to fully maximize first, then readjust later */
589 new.left = usableArea.x1;
590 new.right = usableArea.x2;
591 new.top = usableArea.y1;
592 new.bottom = usableArea.y2;
594 if (HAS_TITLEBAR(wwin))
595 tbar_height_0 = TITLEBAR_HEIGHT;
596 if (HAS_RESIZEBAR(wwin))
597 rbar_height_0 = RESIZEBAR_HEIGHT;
598 if (HAS_BORDER(wwin))
599 bd_width_0 = wwin->screen_ptr->frame_border_width;
601 /* the length to be subtracted if the window has titlebar, etc */
602 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
604 tmp = wwin;
605 /* The focused window is always the last in the list */
606 while (tmp->prev) {
607 /* ignore windows in other workspaces etc */
608 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
609 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
610 tmp = tmp->prev;
611 continue;
613 tmp = tmp->prev;
615 /* Set the coordinates of obstructing window */
616 set_window_coords(tmp, &obs);
618 /* Try to maximize in the y direction first */
619 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
620 if (x_intsect != 0) {
621 /* TODO: Consider the case when coords are equal */
622 if (obs.bottom < orig.top && obs.bottom > new.top) {
623 /* w_0 is below the bottom of w_j */
624 new.top = obs.bottom + 1;
626 if (orig.bottom < obs.top && obs.top < new.bottom) {
627 /* The bottom of w_0 is above the top of w_j */
628 new.bottom = obs.top - 1;
633 tmp = wwin;
634 while (tmp->prev) {
635 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
636 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
637 tmp = tmp->prev;
638 continue;
640 tmp = tmp->prev;
642 set_window_coords(tmp, &obs);
645 * Use the new.top and new.height instead of original values
646 * as they may have different intersections with the obstructing windows
648 new.height = new.bottom - new.top - adjust_height;
649 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
650 if (y_intsect != 0) {
651 if (obs.right < orig.left && obs.right > new.left) {
652 /* w_0 is completely to the right of w_j */
653 new.left = obs.right + 1;
655 if (orig.right < obs.left && obs.left < new.right) {
656 /* w_0 is completely to the left of w_j */
657 new.right = obs.left - 1;
662 *new_x = new.left;
663 *new_y = new.top;
664 /* xcalc needs -7 here, but other apps don't */
665 *new_height = new.bottom - new.top - adjust_height - 1;;
666 *new_width = new.right - new.left;
669 void wUnmaximizeWindow(WWindow *wwin)
671 int x, y, w, h;
673 if (!wwin->flags.maximized)
674 return;
676 if (wwin->flags.shaded) {
677 wwin->flags.skip_next_animation = 1;
678 wUnshadeWindow(wwin);
681 /* Use old coordinates if they are set, current values otherwise */
682 remember_geometry(wwin, &x, &y, &w, &h);
684 /* unMaximusize relative to original position */
685 if (wwin->flags.maximized & MAX_MAXIMUS) {
686 x += wwin->frame_x - wwin->maximus_x;
687 y += wwin->frame_y - wwin->maximus_y;
690 wwin->flags.maximized = 0;
691 wwin->flags.old_maximized = 0;
692 wWindowConfigure(wwin, x, y, w, h);
693 wWindowSynthConfigureNotify(wwin);
695 WMPostNotificationName(WMNChangedState, wwin, "maximize");
698 void wFullscreenWindow(WWindow *wwin)
700 int head;
701 WMRect rect;
703 if (wwin->flags.fullscreen)
704 return;
706 wwin->flags.fullscreen = True;
708 wWindowConfigureBorders(wwin);
710 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
712 wwin->bfs_geometry.x = wwin->frame_x;
713 wwin->bfs_geometry.y = wwin->frame_y;
714 wwin->bfs_geometry.width = wwin->frame->core->width;
715 wwin->bfs_geometry.height = wwin->frame->core->height;
717 head = wGetHeadForWindow(wwin);
718 rect = wGetRectForHead(wwin->screen_ptr, head);
719 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
721 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
724 void wUnfullscreenWindow(WWindow *wwin)
726 if (!wwin->flags.fullscreen)
727 return;
729 wwin->flags.fullscreen = False;
731 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
732 if (WFLAGP(wwin, sunken)) {
733 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
734 } else if (WFLAGP(wwin, floating)) {
735 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
736 } else {
737 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
741 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
742 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
744 wWindowConfigureBorders(wwin);
746 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
747 wFrameWindowPaint(wwin->frame);
750 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
753 #ifdef ANIMATIONS
754 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
756 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
757 float cx, cy, cw, ch;
758 float xstep, ystep, wstep, hstep;
759 XPoint points[5];
760 float dx, dch, midy;
761 float angle, final_angle, delta;
763 xstep = (float)(fx - x) / steps;
764 ystep = (float)(fy - y) / steps;
765 wstep = (float)(fw - w) / steps;
766 hstep = (float)(fh - h) / steps;
768 cx = (float)x;
769 cy = (float)y;
770 cw = (float)w;
771 ch = (float)h;
773 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
774 delta = (float)(final_angle / FRAMES);
775 for (angle = 0;; angle += delta) {
776 if (angle > final_angle)
777 angle = final_angle;
779 dx = (cw / 10) - ((cw / 5) * sin(angle));
780 dch = (ch / 2) * cos(angle);
781 midy = cy + (ch / 2);
783 points[0].x = cx + dx;
784 points[0].y = midy - dch;
785 points[1].x = cx + cw - dx;
786 points[1].y = points[0].y;
787 points[2].x = cx + cw + dx;
788 points[2].y = midy + dch;
789 points[3].x = cx - dx;
790 points[3].y = points[2].y;
791 points[4].x = points[0].x;
792 points[4].y = points[0].y;
794 XGrabServer(dpy);
795 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
796 XFlush(dpy);
797 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
799 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
800 XUngrabServer(dpy);
801 cx += xstep;
802 cy += ystep;
803 cw += wstep;
804 ch += hstep;
805 if (angle >= final_angle)
806 break;
809 XFlush(dpy);
812 #undef FRAMES
814 static void
815 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
817 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
818 float cx, cy, cw, ch;
819 float xstep, ystep, wstep, hstep;
820 XPoint points[5];
821 float angle, final_angle, a, d, delta;
823 x += w / 2;
824 y += h / 2;
825 fx += fw / 2;
826 fy += fh / 2;
828 xstep = (float)(fx - x) / steps;
829 ystep = (float)(fy - y) / steps;
830 wstep = (float)(fw - w) / steps;
831 hstep = (float)(fh - h) / steps;
833 cx = (float)x;
834 cy = (float)y;
835 cw = (float)w;
836 ch = (float)h;
838 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
839 delta = (float)(final_angle / FRAMES);
840 for (angle = 0;; angle += delta) {
841 if (angle > final_angle)
842 angle = final_angle;
844 a = atan(ch / cw);
845 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
847 points[0].x = cx + cos(angle - a) * d;
848 points[0].y = cy + sin(angle - a) * d;
849 points[1].x = cx + cos(angle + a) * d;
850 points[1].y = cy + sin(angle + a) * d;
851 points[2].x = cx + cos(angle - a + WM_PI) * d;
852 points[2].y = cy + sin(angle - a + WM_PI) * d;
853 points[3].x = cx + cos(angle + a + WM_PI) * d;
854 points[3].y = cy + sin(angle + a + WM_PI) * d;
855 points[4].x = cx + cos(angle - a) * d;
856 points[4].y = cy + sin(angle - a) * d;
857 XGrabServer(dpy);
858 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
859 XFlush(dpy);
860 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
862 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
863 XUngrabServer(dpy);
864 cx += xstep;
865 cy += ystep;
866 cw += wstep;
867 ch += hstep;
868 if (angle >= final_angle)
869 break;
872 XFlush(dpy);
875 #undef FRAMES
877 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
879 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
880 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
881 float xstep, ystep, wstep, hstep;
882 int i, j;
884 xstep = (float)(fx - x) / steps;
885 ystep = (float)(fy - y) / steps;
886 wstep = (float)(fw - w) / steps;
887 hstep = (float)(fh - h) / steps;
889 for (j = 0; j < FRAMES; j++) {
890 cx[j] = (float)x;
891 cy[j] = (float)y;
892 cw[j] = (float)w;
893 ch[j] = (float)h;
895 XGrabServer(dpy);
896 for (i = 0; i < steps; i++) {
897 for (j = 0; j < FRAMES; j++) {
898 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
899 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
901 XFlush(dpy);
902 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
904 for (j = 0; j < FRAMES; j++) {
905 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
906 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
907 if (j < FRAMES - 1) {
908 cx[j] = cx[j + 1];
909 cy[j] = cy[j + 1];
910 cw[j] = cw[j + 1];
911 ch[j] = ch[j + 1];
912 } else {
913 cx[j] += xstep;
914 cy[j] += ystep;
915 cw[j] += wstep;
916 ch[j] += hstep;
921 for (j = 0; j < FRAMES; j++) {
922 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
924 XFlush(dpy);
925 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
927 for (j = 0; j < FRAMES; j++) {
928 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
931 XUngrabServer(dpy);
934 #undef FRAMES
936 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
938 int style = wPreferences.iconification_style; /* Catch the value */
939 int steps;
941 if (style == WIS_NONE)
942 return;
944 if (style == WIS_RANDOM) {
945 style = rand() % 3;
948 switch (style) {
949 case WIS_TWIST:
950 steps = MINIATURIZE_ANIMATION_STEPS_T;
951 if (steps > 0)
952 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
953 break;
954 case WIS_FLIP:
955 steps = MINIATURIZE_ANIMATION_STEPS_F;
956 if (steps > 0)
957 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
958 break;
959 case WIS_ZOOM:
960 default:
961 steps = MINIATURIZE_ANIMATION_STEPS_Z;
962 if (steps > 0)
963 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
964 break;
967 #endif /* ANIMATIONS */
969 static void flushExpose(void)
971 XEvent tmpev;
973 while (XCheckTypedEvent(dpy, Expose, &tmpev))
974 WMHandleEvent(&tmpev);
975 XSync(dpy, 0);
978 static void unmapTransientsFor(WWindow *wwin)
980 WWindow *tmp;
982 tmp = wwin->screen_ptr->focused_window;
983 while (tmp) {
984 /* unmap the transients for this transient */
985 if (tmp != wwin && tmp->transient_for == wwin->client_win
986 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
987 unmapTransientsFor(tmp);
988 tmp->flags.miniaturized = 1;
989 if (!tmp->flags.shaded) {
990 wWindowUnmap(tmp);
991 } else {
992 XUnmapWindow(dpy, tmp->frame->core->window);
995 if (!tmp->flags.shaded)
997 wClientSetState(tmp, IconicState, None);
999 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1001 tmp = tmp->prev;
1005 static void mapTransientsFor(WWindow *wwin)
1007 WWindow *tmp;
1009 tmp = wwin->screen_ptr->focused_window;
1010 while (tmp) {
1011 /* recursively map the transients for this transient */
1012 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1013 && tmp->icon == NULL) {
1014 mapTransientsFor(tmp);
1015 tmp->flags.miniaturized = 0;
1016 if (!tmp->flags.shaded) {
1017 wWindowMap(tmp);
1018 } else {
1019 XMapWindow(dpy, tmp->frame->core->window);
1021 tmp->flags.semi_focused = 0;
1023 if (!tmp->flags.shaded)
1025 wClientSetState(tmp, NormalState, None);
1027 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1029 tmp = tmp->prev;
1033 static WWindow *recursiveTransientFor(WWindow * wwin)
1035 int i;
1037 if (!wwin)
1038 return None;
1040 /* hackish way to detect transient_for cycle */
1041 i = wwin->screen_ptr->window_count + 1;
1043 while (wwin && wwin->transient_for != None && i > 0) {
1044 wwin = wWindowFor(wwin->transient_for);
1045 i--;
1047 if (i == 0 && wwin) {
1048 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1049 return NULL;
1052 return wwin;
1055 void wIconifyWindow(WWindow * wwin)
1057 XWindowAttributes attribs;
1058 int present;
1060 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1061 return; /* the window doesn't exist anymore */
1063 if (wwin->flags.miniaturized)
1064 return; /* already miniaturized */
1066 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1067 WWindow *owner = wWindowFor(wwin->transient_for);
1069 if (owner && owner->flags.miniaturized)
1070 return;
1073 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1075 /* if the window is in another workspace, simplify process */
1076 if (present) {
1077 /* icon creation may take a while */
1078 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1079 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1080 GrabModeAsync, None, None, CurrentTime);
1083 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1084 if (!wwin->flags.icon_moved)
1085 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1087 wwin->icon = icon_create_for_wwindow(wwin);
1088 wwin->icon->mapped = 1;
1091 wwin->flags.miniaturized = 1;
1092 wwin->flags.mapped = 0;
1094 /* unmap transients */
1095 unmapTransientsFor(wwin);
1097 if (present) {
1098 XUngrabPointer(dpy, CurrentTime);
1099 wWindowUnmap(wwin);
1100 /* let all Expose events arrive so that we can repaint
1101 * something before the animation starts (and the server is grabbed) */
1102 XSync(dpy, 0);
1104 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1105 wClientSetState(wwin, IconicState, None);
1106 else
1107 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1109 flushExpose();
1110 #ifdef ANIMATIONS
1111 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1112 && !wPreferences.no_animations) {
1113 int ix, iy, iw, ih;
1115 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1116 ix = wwin->icon_x;
1117 iy = wwin->icon_y;
1118 iw = wwin->icon->core->width;
1119 ih = wwin->icon->core->height;
1120 } else {
1121 if (wwin->flags.net_handle_icon) {
1122 ix = wwin->icon_x;
1123 iy = wwin->icon_y;
1124 iw = wwin->icon_w;
1125 ih = wwin->icon_h;
1126 } else {
1127 ix = 0;
1128 iy = 0;
1129 iw = wwin->screen_ptr->scr_width;
1130 ih = wwin->screen_ptr->scr_height;
1133 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1134 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1136 #endif
1139 wwin->flags.skip_next_animation = 0;
1141 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1142 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1143 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1144 XMapWindow(dpy, wwin->icon->core->window);
1146 AddToStackList(wwin->icon->core);
1147 wLowerFrame(wwin->icon->core);
1150 if (present) {
1151 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1154 * It doesn't seem to be working and causes button event hangup
1155 * when deiconifying a transient window.
1156 setupIconGrabs(wwin->icon);
1158 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1159 && wPreferences.focus_mode == WKF_CLICK) {
1160 WWindow *tmp;
1162 tmp = wwin->prev;
1163 while (tmp) {
1164 if (!WFLAGP(tmp, no_focusable)
1165 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1166 && (wwin->frame->workspace == tmp->frame->workspace))
1167 break;
1168 tmp = tmp->prev;
1170 wSetFocusTo(wwin->screen_ptr, tmp);
1171 } else if (wPreferences.focus_mode != WKF_CLICK) {
1172 wSetFocusTo(wwin->screen_ptr, NULL);
1174 #ifdef ANIMATIONS
1175 if (!wwin->screen_ptr->flags.startup) {
1176 /* Catch up with events not processed while animation was running */
1177 Window clientwin = wwin->client_win;
1179 ProcessPendingEvents();
1181 /* the window can disappear while ProcessPendingEvents() runs */
1182 if (!wWindowFor(clientwin)) {
1183 return;
1186 #endif
1189 /* maybe we want to do this regardless of net_handle_icon
1190 * it seems to me we might break behaviour this way.
1192 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1193 && !wwin->flags.net_handle_icon)
1194 wIconSelect(wwin->icon);
1196 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1198 if (wPreferences.auto_arrange_icons)
1199 wArrangeIcons(wwin->screen_ptr, True);
1202 void wDeiconifyWindow(WWindow *wwin)
1204 /* Let's avoid changing workspace while deiconifying */
1205 ignore_wks_change = 1;
1207 /* we're hiding for show_desktop */
1208 int netwm_hidden = wwin->flags.net_show_desktop &&
1209 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1211 if (!netwm_hidden)
1212 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1214 if (!wwin->flags.miniaturized) {
1215 ignore_wks_change = 0;
1216 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 ignore_wks_change = 0;
1227 return;
1231 wwin->flags.miniaturized = 0;
1233 if (!netwm_hidden && !wwin->flags.shaded)
1234 wwin->flags.mapped = 1;
1236 if (!netwm_hidden || wPreferences.sticky_icons) {
1237 /* maybe we want to do this regardless of net_handle_icon
1238 * it seems to me we might break behaviour this way.
1240 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1241 && wwin->icon != NULL) {
1242 if (wwin->icon->selected)
1243 wIconSelect(wwin->icon);
1245 XUnmapWindow(dpy, wwin->icon->core->window);
1249 /* if the window is in another workspace, do it silently */
1250 if (!netwm_hidden) {
1251 #ifdef ANIMATIONS
1252 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1253 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1254 int ix, iy, iw, ih;
1256 if (!wPreferences.disable_miniwindows
1257 && !wwin->flags.net_handle_icon) {
1258 ix = wwin->icon_x;
1259 iy = wwin->icon_y;
1260 iw = wwin->icon->core->width;
1261 ih = wwin->icon->core->height;
1262 } else {
1263 if (wwin->flags.net_handle_icon) {
1264 ix = wwin->icon_x;
1265 iy = wwin->icon_y;
1266 iw = wwin->icon_w;
1267 ih = wwin->icon_h;
1268 } else {
1269 ix = 0;
1270 iy = 0;
1271 iw = wwin->screen_ptr->scr_width;
1272 ih = wwin->screen_ptr->scr_height;
1275 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1276 wwin->frame_x, wwin->frame_y,
1277 wwin->frame->core->width, wwin->frame->core->height);
1279 #endif /* ANIMATIONS */
1280 wwin->flags.skip_next_animation = 0;
1281 XGrabServer(dpy);
1282 if (!wwin->flags.shaded)
1283 XMapWindow(dpy, wwin->client_win);
1285 XMapWindow(dpy, wwin->frame->core->window);
1286 wRaiseFrame(wwin->frame->core);
1287 if (!wwin->flags.shaded)
1288 wClientSetState(wwin, NormalState, None);
1290 mapTransientsFor(wwin);
1293 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1294 && !wwin->flags.net_handle_icon) {
1295 RemoveFromStackList(wwin->icon->core);
1296 /* removeIconGrabs(wwin->icon); */
1297 wIconDestroy(wwin->icon);
1298 wwin->icon = NULL;
1301 if (!netwm_hidden) {
1302 XUngrabServer(dpy);
1304 wSetFocusTo(wwin->screen_ptr, wwin);
1306 #ifdef ANIMATIONS
1307 if (!wwin->screen_ptr->flags.startup) {
1308 /* Catch up with events not processed while animation was running */
1309 Window clientwin = wwin->client_win;
1311 ProcessPendingEvents();
1313 /* the window can disappear while ProcessPendingEvents() runs */
1314 if (!wWindowFor(clientwin)) {
1315 ignore_wks_change = 0;
1316 return;
1319 #endif
1322 if (wPreferences.auto_arrange_icons)
1323 wArrangeIcons(wwin->screen_ptr, True);
1325 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1327 /* In case we were shaded and iconified, also unshade */
1328 if (!netwm_hidden)
1329 wUnshadeWindow(wwin);
1331 ignore_wks_change = 0;
1334 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1336 if (wwin->flags.miniaturized) {
1337 if (wwin->icon) {
1338 XUnmapWindow(dpy, wwin->icon->core->window);
1339 wwin->icon->mapped = 0;
1341 wwin->flags.hidden = 1;
1343 WMPostNotificationName(WMNChangedState, wwin, "hide");
1344 return;
1347 if (wwin->flags.inspector_open) {
1348 wHideInspectorForWindow(wwin);
1351 wwin->flags.hidden = 1;
1352 wWindowUnmap(wwin);
1354 wClientSetState(wwin, IconicState, icon->icon_win);
1355 flushExpose();
1357 #ifdef ANIMATIONS
1358 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1359 !wwin->flags.skip_next_animation && animate) {
1360 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1361 wwin->frame->core->width, wwin->frame->core->height,
1362 icon_x, icon_y, icon->core->width, icon->core->height);
1364 #endif
1365 wwin->flags.skip_next_animation = 0;
1367 WMPostNotificationName(WMNChangedState, wwin, "hide");
1370 void wHideAll(WScreen *scr)
1372 WWindow *wwin;
1373 WWindow **windows;
1374 WMenu *menu;
1375 unsigned int wcount = 0;
1376 int i;
1378 if (!scr)
1379 return;
1381 menu = scr->switch_menu;
1383 windows = wmalloc(sizeof(WWindow *));
1385 if (menu != NULL) {
1386 for (i = 0; i < menu->entry_no; i++) {
1387 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1388 wcount++;
1389 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1391 } else {
1392 wwin = scr->focused_window;
1394 while (wwin) {
1395 windows[wcount] = wwin;
1396 wcount++;
1397 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1398 wwin = wwin->prev;
1403 for (i = 0; i < wcount; i++) {
1404 wwin = windows[i];
1405 if (wwin->frame->workspace == scr->current_workspace
1406 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1407 && !wwin->flags.internal_window
1408 && !WFLAGP(wwin, no_miniaturizable)
1410 wwin->flags.skip_next_animation = 1;
1411 wIconifyWindow(wwin);
1415 wfree(windows);
1418 void wHideOtherApplications(WWindow *awin)
1420 WWindow *wwin;
1421 WApplication *tapp;
1423 if (!awin)
1424 return;
1425 wwin = awin->screen_ptr->focused_window;
1427 while (wwin) {
1428 if (wwin != awin
1429 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1430 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1431 && !wwin->flags.internal_window
1432 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1434 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1435 if (!WFLAGP(wwin, no_miniaturizable)) {
1436 wwin->flags.skip_next_animation = 1;
1437 wIconifyWindow(wwin);
1439 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1440 tapp = wApplicationOf(wwin->main_window);
1441 if (tapp) {
1442 tapp->flags.skip_next_animation = 1;
1443 wHideApplication(tapp);
1444 } else {
1445 if (!WFLAGP(wwin, no_miniaturizable)) {
1446 wwin->flags.skip_next_animation = 1;
1447 wIconifyWindow(wwin);
1452 wwin = wwin->prev;
1455 wSetFocusTo(awin->screen_ptr, awin);
1459 void wHideApplication(WApplication *wapp)
1461 WScreen *scr;
1462 WWindow *wlist;
1463 int hadfocus;
1464 int animate;
1466 if (!wapp) {
1467 wwarning("trying to hide a non grouped window");
1468 return;
1470 if (!wapp->main_window_desc) {
1471 wwarning("group leader not found for window group");
1472 return;
1474 scr = wapp->main_window_desc->screen_ptr;
1475 hadfocus = 0;
1476 wlist = scr->focused_window;
1477 if (!wlist)
1478 return;
1480 if (wlist->main_window == wapp->main_window)
1481 wapp->last_focused = wlist;
1482 else
1483 wapp->last_focused = NULL;
1485 animate = !wapp->flags.skip_next_animation;
1487 while (wlist) {
1488 if (wlist->main_window == wapp->main_window) {
1489 if (wlist->flags.focused) {
1490 hadfocus = 1;
1492 if (wapp->app_icon) {
1493 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1494 wapp->app_icon->y_pos, wlist, animate);
1495 animate = False;
1498 wlist = wlist->prev;
1501 wapp->flags.skip_next_animation = 0;
1503 if (hadfocus) {
1504 if (wPreferences.focus_mode == WKF_CLICK) {
1505 wlist = scr->focused_window;
1506 while (wlist) {
1507 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1508 && (wlist->flags.mapped || wlist->flags.shaded))
1509 break;
1510 wlist = wlist->prev;
1512 wSetFocusTo(scr, wlist);
1513 } else {
1514 wSetFocusTo(scr, NULL);
1518 wapp->flags.hidden = 1;
1520 if (wPreferences.auto_arrange_icons)
1521 wArrangeIcons(scr, True);
1523 #ifdef HIDDENDOT
1524 if (wapp->app_icon)
1525 wAppIconPaint(wapp->app_icon);
1526 #endif
1529 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1531 if (bringToCurrentWS)
1532 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1534 wwin->flags.hidden = 0;
1536 #ifdef ANIMATIONS
1537 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1538 animateResize(wwin->screen_ptr, icon_x, icon_y,
1539 icon->core->width, icon->core->height,
1540 wwin->frame_x, wwin->frame_y,
1541 wwin->frame->core->width, wwin->frame->core->height);
1543 #endif
1544 wwin->flags.skip_next_animation = 0;
1545 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1546 XMapWindow(dpy, wwin->client_win);
1547 XMapWindow(dpy, wwin->frame->core->window);
1548 wClientSetState(wwin, NormalState, None);
1549 wwin->flags.mapped = 1;
1550 wRaiseFrame(wwin->frame->core);
1552 if (wwin->flags.inspector_open) {
1553 wUnhideInspectorForWindow(wwin);
1556 WMPostNotificationName(WMNChangedState, wwin, "hide");
1559 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1561 WScreen *scr;
1562 WWindow *wlist, *next;
1563 WWindow *focused = NULL;
1564 int animate;
1566 if (!wapp)
1567 return;
1569 scr = wapp->main_window_desc->screen_ptr;
1570 wlist = scr->focused_window;
1571 if (!wlist)
1572 return;
1574 /* goto beginning of list */
1575 while (wlist->prev)
1576 wlist = wlist->prev;
1578 animate = !wapp->flags.skip_next_animation;
1580 while (wlist) {
1581 next = wlist->next;
1583 if (wlist->main_window == wapp->main_window) {
1584 if (wlist->flags.focused)
1585 focused = wlist;
1586 else if (!focused || !focused->flags.focused)
1587 focused = wlist;
1589 if (wlist->flags.miniaturized) {
1590 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1591 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1592 if (!wlist->icon->mapped) {
1593 int x, y;
1595 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1596 if (wlist->icon_x != x || wlist->icon_y != y) {
1597 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1599 wlist->icon_x = x;
1600 wlist->icon_y = y;
1601 XMapWindow(dpy, wlist->icon->core->window);
1602 wlist->icon->mapped = 1;
1604 wRaiseFrame(wlist->icon->core);
1606 if (bringToCurrentWS)
1607 wWindowChangeWorkspace(wlist, scr->current_workspace);
1608 wlist->flags.hidden = 0;
1609 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1610 wDeiconifyWindow(wlist);
1612 WMPostNotificationName(WMNChangedState, wlist, "hide");
1613 } else if (wlist->flags.shaded) {
1614 if (bringToCurrentWS)
1615 wWindowChangeWorkspace(wlist, scr->current_workspace);
1616 wlist->flags.hidden = 0;
1617 wRaiseFrame(wlist->frame->core);
1618 if (wlist->frame->workspace == scr->current_workspace) {
1619 XMapWindow(dpy, wlist->frame->core->window);
1620 if (miniwindows) {
1621 wUnshadeWindow(wlist);
1624 WMPostNotificationName(WMNChangedState, wlist, "hide");
1625 } else if (wlist->flags.hidden) {
1626 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1627 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1628 animate = False;
1629 } else {
1630 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1631 wWindowChangeWorkspace(wlist, scr->current_workspace);
1633 wRaiseFrame(wlist->frame->core);
1636 wlist = next;
1639 wapp->flags.skip_next_animation = 0;
1640 wapp->flags.hidden = 0;
1642 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1643 wRaiseFrame(wapp->last_focused->frame->core);
1644 wSetFocusTo(scr, wapp->last_focused);
1645 } else if (focused) {
1646 wSetFocusTo(scr, focused);
1648 wapp->last_focused = NULL;
1649 if (wPreferences.auto_arrange_icons)
1650 wArrangeIcons(scr, True);
1652 #ifdef HIDDENDOT
1653 wAppIconPaint(wapp->app_icon);
1654 #endif
1657 void wShowAllWindows(WScreen *scr)
1659 WWindow *wwin, *old_foc;
1660 WApplication *wapp;
1662 old_foc = wwin = scr->focused_window;
1663 while (wwin) {
1664 if (!wwin->flags.internal_window &&
1665 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1666 if (wwin->flags.miniaturized) {
1667 wwin->flags.skip_next_animation = 1;
1668 wDeiconifyWindow(wwin);
1669 } else if (wwin->flags.hidden) {
1670 wapp = wApplicationOf(wwin->main_window);
1671 if (wapp) {
1672 wUnhideApplication(wapp, False, False);
1673 } else {
1674 wwin->flags.skip_next_animation = 1;
1675 wDeiconifyWindow(wwin);
1679 wwin = wwin->prev;
1681 wSetFocusTo(scr, old_foc);
1682 /*wRaiseFrame(old_foc->frame->core); */
1685 void wRefreshDesktop(WScreen *scr)
1687 Window win;
1688 XSetWindowAttributes attr;
1690 attr.backing_store = NotUseful;
1691 attr.save_under = False;
1692 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1693 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1694 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1695 XMapRaised(dpy, win);
1696 XDestroyWindow(dpy, win);
1697 XFlush(dpy);
1700 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1702 WWindow *wwin;
1703 WAppIcon *aicon;
1705 int head;
1706 const int heads = wXineramaHeads(scr);
1708 struct HeadVars {
1709 int pf; /* primary axis */
1710 int sf; /* secondary axis */
1711 int fullW;
1712 int fullH;
1713 int pi, si;
1714 int sx1, sx2, sy1, sy2; /* screen boundary */
1715 int sw, sh;
1716 int xo, yo;
1717 int xs, ys;
1718 } *vars;
1720 int isize = wPreferences.icon_size;
1722 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1724 for (head = 0; head < heads; ++head) {
1725 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1726 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1727 vars[head].pi = vars[head].si = 0;
1728 vars[head].sx1 = rect.pos.x;
1729 vars[head].sy1 = rect.pos.y;
1730 vars[head].sw = rect.size.width;
1731 vars[head].sh = rect.size.height;
1732 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1733 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1734 vars[head].sw = isize * (vars[head].sw / isize);
1735 vars[head].sh = isize * (vars[head].sh / isize);
1736 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1737 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1739 /* icon yard boundaries */
1740 if (wPreferences.icon_yard & IY_VERT) {
1741 vars[head].pf = vars[head].fullH;
1742 vars[head].sf = vars[head].fullW;
1743 } else {
1744 vars[head].pf = vars[head].fullW;
1745 vars[head].sf = vars[head].fullH;
1747 if (wPreferences.icon_yard & IY_RIGHT) {
1748 vars[head].xo = vars[head].sx2 - isize;
1749 vars[head].xs = -1;
1750 } else {
1751 vars[head].xo = vars[head].sx1;
1752 vars[head].xs = 1;
1754 if (wPreferences.icon_yard & IY_TOP) {
1755 vars[head].yo = vars[head].sy1;
1756 vars[head].ys = 1;
1757 } else {
1758 vars[head].yo = vars[head].sy2 - isize;
1759 vars[head].ys = -1;
1763 #define X ((wPreferences.icon_yard & IY_VERT) \
1764 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1765 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1767 #define Y ((wPreferences.icon_yard & IY_VERT) \
1768 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1769 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1771 /* arrange application icons */
1772 aicon = scr->app_icon_list;
1773 /* reverse them to avoid unnecessarily sliding of icons */
1774 while (aicon && aicon->next)
1775 aicon = aicon->next;
1777 while (aicon) {
1778 if (!aicon->docked) {
1779 /* CHECK: can icon be NULL here ? */
1780 /* The intention here is to place the AppIcon on the head that
1781 * contains most of the applications _main_ window. */
1782 head = wGetHeadForWindow(aicon->icon->owner);
1784 if (aicon->x_pos != X || aicon->y_pos != Y) {
1785 #ifdef ANIMATIONS
1786 if (!wPreferences.no_animations)
1787 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1788 #endif /* ANIMATIONS */
1790 wAppIconMove(aicon, X, Y);
1791 vars[head].pi++;
1792 if (vars[head].pi >= vars[head].pf) {
1793 vars[head].pi = 0;
1794 vars[head].si++;
1797 aicon = aicon->prev;
1800 /* arrange miniwindows */
1801 wwin = scr->focused_window;
1802 /* reverse them to avoid unnecessarily shuffling */
1803 while (wwin && wwin->prev)
1804 wwin = wwin->prev;
1806 while (wwin) {
1807 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1808 (wwin->frame->workspace == scr->current_workspace ||
1809 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1811 head = wGetHeadForWindow(wwin);
1813 if (arrangeAll || !wwin->flags.icon_moved) {
1814 if (wwin->icon_x != X || wwin->icon_y != Y)
1815 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1817 wwin->icon_x = X;
1818 wwin->icon_y = Y;
1820 vars[head].pi++;
1821 if (vars[head].pi >= vars[head].pf) {
1822 vars[head].pi = 0;
1823 vars[head].si++;
1827 if (arrangeAll) {
1828 wwin->flags.icon_moved = 0;
1830 /* we reversed the order, so we use next */
1831 wwin = wwin->next;
1834 wfree(vars);
1837 void wSelectWindow(WWindow *wwin, Bool flag)
1839 WScreen *scr = wwin->screen_ptr;
1841 if (flag) {
1842 wwin->flags.selected = 1;
1843 if (wwin->frame->selected_border_pixel)
1844 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1845 else
1846 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1848 if (!HAS_BORDER(wwin)) {
1849 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1852 if (!scr->selected_windows)
1853 scr->selected_windows = WMCreateArray(4);
1854 WMAddToArray(scr->selected_windows, wwin);
1855 } else {
1856 wwin->flags.selected = 0;
1857 if (wwin->frame->border_pixel)
1858 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1859 else
1860 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1862 if (!HAS_BORDER(wwin)) {
1863 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1866 if (scr->selected_windows) {
1867 WMRemoveFromArray(scr->selected_windows, wwin);
1872 void wMakeWindowVisible(WWindow *wwin)
1874 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1875 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1877 if (wwin->flags.shaded) {
1878 wUnshadeWindow(wwin);
1880 if (wwin->flags.hidden) {
1881 WApplication *app;
1883 app = wApplicationOf(wwin->main_window);
1884 if (app) {
1885 /* trick to get focus to this window */
1886 app->last_focused = wwin;
1887 wUnhideApplication(app, False, False);
1890 if (wwin->flags.miniaturized) {
1891 wDeiconifyWindow(wwin);
1892 } else {
1893 if (!WFLAGP(wwin, no_focusable))
1894 wSetFocusTo(wwin->screen_ptr, wwin);
1895 wRaiseFrame(wwin->frame->core);
1900 * Do the animation while shading (called with what = SHADE)
1901 * or unshading (what = UNSHADE).
1903 #ifdef ANIMATIONS
1904 static void shade_animate(WWindow *wwin, Bool what)
1906 int y, s, w, h;
1907 time_t time0 = time(NULL);
1909 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1910 return;
1912 switch(what) {
1913 case SHADE:
1914 if (!wwin->screen_ptr->flags.startup) {
1915 /* do the shading animation */
1916 h = wwin->frame->core->height;
1917 s = h / SHADE_STEPS;
1918 if (s < 1)
1919 s = 1;
1920 w = wwin->frame->core->width;
1921 y = wwin->frame->top_width;
1922 while (h > wwin->frame->top_width + 1) {
1923 XMoveWindow(dpy, wwin->client_win, 0, y);
1924 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1925 XFlush(dpy);
1927 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1928 break;
1930 if (SHADE_DELAY > 0) {
1931 wusleep(SHADE_DELAY * 1000L);
1932 } else {
1933 wusleep(10);
1935 h -= s;
1936 y -= s;
1938 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1940 break;
1942 case UNSHADE:
1943 h = wwin->frame->top_width + wwin->frame->bottom_width;
1944 y = wwin->frame->top_width - wwin->client.height;
1945 s = abs(y) / SHADE_STEPS;
1946 if (s < 1)
1947 s = 1;
1948 w = wwin->frame->core->width;
1949 XMoveWindow(dpy, wwin->client_win, 0, y);
1950 if (s > 0) {
1951 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1952 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1953 XMoveWindow(dpy, wwin->client_win, 0, y);
1954 XFlush(dpy);
1955 if (SHADE_DELAY > 0) {
1956 wusleep(SHADE_DELAY * 2000L / 3);
1957 } else {
1958 wusleep(10);
1960 h += s;
1961 y += s;
1963 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1964 break;
1967 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1968 break;
1971 #endif