19b4322714279ca34802fae9e0fd98c63d0e19ee
[wmaker-crm.git] / src / actions.c
blob19b4322714279ca34802fae9e0fd98c63d0e19ee
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
33 #include "WindowMaker.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "colormap.h"
39 #include "application.h"
40 #include "actions.h"
41 #include "stacking.h"
42 #include "appicon.h"
43 #include "dock.h"
44 #include "appmenu.h"
45 #include "winspector.h"
46 #include "workspace.h"
47 #include "xinerama.h"
48 #include "usermenu.h"
49 #include "placement.h"
50 #include "misc.h"
52 /****** Global Variables ******/
54 int ignore_wks_change = 0;
55 extern Time LastTimestamp;
56 extern Time LastFocusChange;
57 extern WPreferences wPreferences;
58 extern Atom _XA_WM_TAKE_FOCUS;
60 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
61 unsigned int *new_width, unsigned int *new_height);
62 static void save_old_geometry(WWindow *wwin, int directions);
64 /******* Local Variables *******/
65 static struct {
66 int steps;
67 int delay;
68 } shadePars[5] = {
70 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
71 SHADE_STEPS_F, SHADE_DELAY_F}, {
72 SHADE_STEPS_M, SHADE_DELAY_M}, {
73 SHADE_STEPS_S, SHADE_DELAY_S}, {
74 SHADE_STEPS_US, SHADE_DELAY_US}};
76 #define UNSHADE 0
77 #define SHADE 1
78 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
79 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
81 static int compareTimes(Time t1, Time t2)
83 Time diff;
84 if (t1 == t2)
85 return 0;
86 diff = t1 - t2;
87 return (diff < 60000) ? 1 : -1;
90 #ifdef ANIMATIONS
91 static void shade_animate(WWindow *wwin, Bool what);
92 #else
93 static void shade_animate(WWindow *wwin, Bool what) { }
94 #endif
97 *----------------------------------------------------------------------
98 * wSetFocusTo--
99 * Changes the window focus to the one passed as argument.
100 * If the window to focus is not already focused, it will be brought
101 * to the head of the list of windows. Previously focused window is
102 * unfocused.
104 * Side effects:
105 * Window list may be reordered and the window focus is changed.
107 *----------------------------------------------------------------------
109 void wSetFocusTo(WScreen *scr, WWindow *wwin)
111 static WScreen *old_scr = NULL;
113 WWindow *old_focused;
114 WWindow *focused = scr->focused_window;
115 Time timestamp = LastTimestamp;
116 WApplication *oapp = NULL, *napp = NULL;
117 int wasfocused;
119 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
120 return;
122 if (!old_scr)
123 old_scr = scr;
125 old_focused = old_scr->focused_window;
127 LastFocusChange = timestamp;
129 if (old_focused)
130 oapp = wApplicationOf(old_focused->main_window);
132 if (wwin == NULL) {
133 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
134 if (old_focused)
135 wWindowUnfocus(old_focused);
137 if (oapp) {
138 wAppMenuUnmap(oapp->menu);
139 if (wPreferences.highlight_active_app)
140 wApplicationDeactivate(oapp);
143 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
144 return;
147 if (old_scr != scr && old_focused)
148 wWindowUnfocus(old_focused);
150 wasfocused = wwin->flags.focused;
151 napp = wApplicationOf(wwin->main_window);
153 /* remember last workspace where the app has been */
154 if (napp)
155 napp->last_workspace = wwin->frame->workspace;
157 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
158 /* install colormap if colormap mode is lock mode */
159 if (wPreferences.colormap_mode == WCM_CLICK)
160 wColormapInstallForWindow(scr, wwin);
162 /* set input focus */
163 switch (wwin->focus_mode) {
164 case WFM_NO_INPUT:
165 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
166 break;
167 case WFM_PASSIVE:
168 case WFM_LOCALLY_ACTIVE:
169 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
170 break;
171 case WFM_GLOBALLY_ACTIVE:
172 break;
175 XFlush(dpy);
176 if (wwin->protocols.TAKE_FOCUS)
177 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
179 XSync(dpy, False);
180 } else {
181 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
184 if (WFLAGP(wwin, no_focusable))
185 return;
187 /* if this is not the focused window focus it */
188 if (focused != wwin) {
189 /* change the focus window list order */
190 if (wwin->prev)
191 wwin->prev->next = wwin->next;
193 if (wwin->next)
194 wwin->next->prev = wwin->prev;
196 wwin->prev = focused;
197 focused->next = wwin;
198 wwin->next = NULL;
199 scr->focused_window = wwin;
201 if (oapp && oapp != napp) {
202 wAppMenuUnmap(oapp->menu);
203 if (wPreferences.highlight_active_app)
204 wApplicationDeactivate(oapp);
208 wWindowFocus(wwin, focused);
210 if (napp && !wasfocused) {
211 #ifdef USER_MENU
212 wUserMenuRefreshInstances(napp->menu, wwin);
213 #endif /* USER_MENU */
215 if (wwin->flags.mapped)
216 wAppMenuMap(napp->menu, wwin);
218 if (napp && wPreferences.highlight_active_app)
219 wApplicationActivate(napp);
221 XFlush(dpy);
222 old_scr = scr;
225 void wShadeWindow(WWindow *wwin)
228 if (wwin->flags.shaded)
229 return;
231 XLowerWindow(dpy, wwin->client_win);
232 shade_animate(wwin, SHADE);
234 wwin->flags.skip_next_animation = 0;
235 wwin->flags.shaded = 1;
236 wwin->flags.mapped = 0;
237 /* prevent window withdrawal when getting UnmapNotify */
238 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
239 XUnmapWindow(dpy, wwin->client_win);
240 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
242 /* for the client it's just like iconification */
243 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
245 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
246 wWindowSynthConfigureNotify(wwin);
249 wClientSetState(wwin, IconicState, None);
252 WMPostNotificationName(WMNChangedState, wwin, "shade");
254 #ifdef ANIMATIONS
255 if (!wwin->screen_ptr->flags.startup) {
256 /* Catch up with events not processed while animation was running */
257 ProcessPendingEvents();
259 #endif
262 void wUnshadeWindow(WWindow *wwin)
265 if (!wwin->flags.shaded)
266 return;
268 wwin->flags.shaded = 0;
269 wwin->flags.mapped = 1;
270 XMapWindow(dpy, wwin->client_win);
272 shade_animate(wwin, UNSHADE);
274 wwin->flags.skip_next_animation = 0;
275 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
276 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
278 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
279 wWindowSynthConfigureNotify(wwin);
281 /* if the window is focused, set the focus again as it was disabled during
282 * shading */
283 if (wwin->flags.focused)
284 wSetFocusTo(wwin->screen_ptr, wwin);
286 WMPostNotificationName(WMNChangedState, wwin, "shade");
289 /* Set the old coordinates using the current values */
290 static void save_old_geometry(WWindow *wwin, int directions)
292 /* never been saved? */
293 if (! wwin->old_geometry.width)
294 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
295 if (! wwin->old_geometry.height)
296 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
298 if (directions & SAVE_GEOMETRY_X)
299 wwin->old_geometry.x = wwin->frame_x;
300 if (directions & SAVE_GEOMETRY_Y)
301 wwin->old_geometry.y = wwin->frame_y;
302 if (directions & SAVE_GEOMETRY_WIDTH)
303 wwin->old_geometry.width = wwin->client.width;
304 if (directions & SAVE_GEOMETRY_HEIGHT)
305 wwin->old_geometry.height = wwin->client.height;
308 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
310 WMRect old_geom_rect;
311 int old_head;
312 Bool same_head;
314 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
315 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
316 same_head = (wGetHeadForWindow(wwin) == old_head);
317 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
318 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
319 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
320 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
323 /* Remember geometry for unmaximizing */
324 void update_saved_geometry(WWindow *wwin)
326 /* NOT if we aren't already maximized
327 * we'll save geometry when maximizing */
328 if (!wwin->flags.maximized)
329 return;
331 /* NOT if we are fully maximized */
332 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
333 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
334 (wwin->flags.maximized & MAX_VERTICAL)))
335 return;
337 /* save the co-ordinate in the axis in which we AREN'T maximized */
338 if (wwin->flags.maximized & MAX_HORIZONTAL)
339 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
340 if (wwin->flags.maximized & MAX_VERTICAL)
341 save_old_geometry(wwin, SAVE_GEOMETRY_X);
344 void wMaximizeWindow(WWindow *wwin, int directions)
346 unsigned int new_width, new_height, half_scr_width, half_scr_height;
347 int new_x = 0;
348 int new_y = 0;
349 int maximus_x = 0;
350 int maximus_y = 0;
351 unsigned int maximus_width = 0;
352 unsigned int maximus_height = 0;
353 WArea usableArea, totalArea;
354 Bool has_border = 1;
355 int adj_size;
357 if (!IS_RESIZABLE(wwin))
358 return;
360 if (!HAS_BORDER(wwin))
361 has_border = 0;
363 /* the size to adjust the geometry */
364 adj_size = wwin->screen_ptr->frame_border_width * 2 * has_border;
366 /* save old coordinates before we change the current values */
367 if (!wwin->flags.maximized)
368 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
370 totalArea.x2 = wwin->screen_ptr->scr_width;
371 totalArea.y2 = wwin->screen_ptr->scr_height;
372 totalArea.x1 = 0;
373 totalArea.y1 = 0;
374 usableArea = totalArea;
376 if (!(directions & MAX_IGNORE_XINERAMA)) {
377 WScreen *scr = wwin->screen_ptr;
378 int head;
380 if (directions & MAX_KEYBOARD)
381 head = wGetHeadForWindow(wwin);
382 else
383 head = wGetHeadForPointerLocation(scr);
385 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
388 /* Only save directions, not kbd or xinerama hints */
389 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
391 if (WFLAGP(wwin, full_maximize)) {
392 usableArea = totalArea;
394 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
395 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
397 if (wwin->flags.shaded) {
398 wwin->flags.skip_next_animation = 1;
399 wUnshadeWindow(wwin);
402 if (directions & MAX_MAXIMUS) {
403 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
404 new_width = maximus_width - adj_size;
405 new_height = maximus_height - adj_size;
406 new_x = maximus_x;
407 new_y = maximus_y;
408 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
409 new_height += wwin->frame->bottom_width - 1;
410 new_y -= wwin->frame->top_width;
412 /* HACK: this will be subtracted again below */
413 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
415 wwin->maximus_x = new_x;
416 wwin->maximus_y = new_y;
417 wwin->flags.old_maximized |= MAX_MAXIMUS;
418 } else {
419 /* set default values if no option set then */
420 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
421 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
422 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
424 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
425 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
426 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
429 /* left|right position */
430 if (directions & MAX_LEFTHALF) {
431 new_width = half_scr_width - adj_size;
432 new_x = usableArea.x1;
433 } else if (directions & MAX_RIGHTHALF) {
434 new_width = half_scr_width - adj_size;
435 new_x = usableArea.x1 + half_scr_width;
437 /* top|bottom position */
438 if (directions & MAX_TOPHALF) {
439 new_height = half_scr_height - adj_size;
440 new_y = usableArea.y1;
441 } else if (directions & MAX_BOTTOMHALF) {
442 new_height = half_scr_height - adj_size;
443 new_y = usableArea.y1 + half_scr_height;
446 /* vertical|horizontal position */
447 if (directions & MAX_HORIZONTAL) {
448 new_width = usableArea.x2 - usableArea.x1 - adj_size;
449 new_x = usableArea.x1;
451 if (directions & MAX_VERTICAL) {
452 new_height = usableArea.y2 - usableArea.y1 - adj_size;
453 new_y = usableArea.y1;
454 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
455 new_y -= wwin->frame->top_width;
459 if (!WFLAGP(wwin, full_maximize))
460 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
462 /* set maximization state */
463 wwin->flags.maximized = directions;
464 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
465 wwin->flags.maximized = MAX_MAXIMUS;
467 wWindowConstrainSize(wwin, &new_width, &new_height);
469 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
470 usableArea.y2 - usableArea.y1, &new_width, &new_height);
472 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
473 wWindowSynthConfigureNotify(wwin);
475 WMPostNotificationName(WMNChangedState, wwin, "maximize");
478 /* generic (un)maximizer */
479 void handleMaximize(WWindow *wwin, int directions)
481 int current = wwin->flags.maximized;
482 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
483 int effective = requested ^ current;
484 int flags = directions & ~requested;
486 if (!effective) {
487 /* allow wMaximizeWindow to restore the Maximusized size */
488 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
489 !(requested & MAX_MAXIMUS))
490 wMaximizeWindow(wwin, flags);
491 else
492 wUnmaximizeWindow(wwin);
493 /* these alone mean vertical|horizontal toggle */
494 } else if ((effective == MAX_LEFTHALF) ||
495 (effective == MAX_RIGHTHALF) ||
496 (effective == MAX_TOPHALF) ||
497 (effective == MAX_BOTTOMHALF))
498 wUnmaximizeWindow(wwin);
499 else {
500 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
501 (requested == MAX_MAXIMUS))
502 effective = requested;
503 else {
504 if (requested & MAX_LEFTHALF) {
505 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
506 effective |= MAX_VERTICAL;
507 else
508 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
509 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
510 } else if (requested & MAX_RIGHTHALF) {
511 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
512 effective |= MAX_VERTICAL;
513 else
514 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
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_VERTICAL | MAX_BOTTOMHALF);
523 } else if (requested & MAX_BOTTOMHALF) {
524 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
525 effective |= MAX_HORIZONTAL;
526 else
527 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
528 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
530 if (requested & MAX_HORIZONTAL)
531 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
532 if (requested & MAX_VERTICAL)
533 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
534 effective &= ~MAX_MAXIMUS;
536 wMaximizeWindow(wwin, effective | flags);
540 /* the window boundary coordinates */
541 typedef struct {
542 int left;
543 int right;
544 int bottom;
545 int top;
546 int width;
547 int height;
548 } win_coords;
550 static void set_window_coords(WWindow *wwin, win_coords *obs)
552 obs->left = wwin->frame_x;
553 obs->top = wwin->frame_y;
554 obs->width = wwin->frame->core->width;
555 obs->height = wwin->frame->core->height;
556 obs->bottom = obs->top + obs->height;
557 obs->right = obs->left + obs->width;
561 * Maximus: tiled maximization (maximize without overlapping other windows)
563 * The original window 'orig' will be maximized to new coordinates 'new'.
564 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
566 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
567 unsigned int *new_width, unsigned int *new_height)
569 WWindow *tmp;
570 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
571 short int adjust_height;
572 int x_intsect, y_intsect;
573 /* the obstructing, original and new windows */
574 win_coords obs, orig, new;
576 /* set the original coordinate positions of the window to be Maximumized */
577 if (wwin->flags.maximized) {
578 /* window is already maximized; consider original geometry */
579 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
580 orig.bottom = orig.top + orig.height;
581 orig.right = orig.left + orig.width;
583 else
584 set_window_coords(wwin, &orig);
586 /* Try to fully maximize first, then readjust later */
587 new.left = usableArea.x1;
588 new.right = usableArea.x2;
589 new.top = usableArea.y1;
590 new.bottom = usableArea.y2;
592 if (HAS_TITLEBAR(wwin))
593 tbar_height_0 = TITLEBAR_HEIGHT;
594 if (HAS_RESIZEBAR(wwin))
595 rbar_height_0 = RESIZEBAR_HEIGHT;
596 if (HAS_BORDER(wwin))
597 bd_width_0 = wwin->screen_ptr->frame_border_width;
599 /* the length to be subtracted if the window has titlebar, etc */
600 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
602 tmp = wwin;
603 /* The focused window is always the last in the list */
604 while (tmp->prev) {
605 /* ignore windows in other workspaces etc */
606 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
607 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
608 tmp = tmp->prev;
609 continue;
611 tmp = tmp->prev;
613 /* Set the coordinates of obstructing window */
614 set_window_coords(tmp, &obs);
616 /* Try to maximize in the y direction first */
617 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
618 if (x_intsect != 0) {
619 /* TODO: Consider the case when coords are equal */
620 if (obs.bottom < orig.top && obs.bottom > new.top) {
621 /* w_0 is below the bottom of w_j */
622 new.top = obs.bottom + 1;
624 if (orig.bottom < obs.top && obs.top < new.bottom) {
625 /* The bottom of w_0 is above the top of w_j */
626 new.bottom = obs.top - 1;
631 tmp = wwin;
632 while (tmp->prev) {
633 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
634 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
635 tmp = tmp->prev;
636 continue;
638 tmp = tmp->prev;
640 set_window_coords(tmp, &obs);
643 * Use the new.top and new.height instead of original values
644 * as they may have different intersections with the obstructing windows
646 new.height = new.bottom - new.top - adjust_height;
647 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
648 if (y_intsect != 0) {
649 if (obs.right < orig.left && obs.right > new.left) {
650 /* w_0 is completely to the right of w_j */
651 new.left = obs.right + 1;
653 if (orig.right < obs.left && obs.left < new.right) {
654 /* w_0 is completely to the left of w_j */
655 new.right = obs.left - 1;
660 *new_x = new.left;
661 *new_y = new.top;
662 /* xcalc needs -7 here, but other apps don't */
663 *new_height = new.bottom - new.top - adjust_height - 1;;
664 *new_width = new.right - new.left;
667 void wUnmaximizeWindow(WWindow *wwin)
669 int x, y, w, h;
671 if (!wwin->flags.maximized)
672 return;
674 if (wwin->flags.shaded) {
675 wwin->flags.skip_next_animation = 1;
676 wUnshadeWindow(wwin);
679 /* Use old coordinates if they are set, current values otherwise */
680 remember_geometry(wwin, &x, &y, &w, &h);
682 /* unMaximusize relative to original position */
683 if (wwin->flags.maximized & MAX_MAXIMUS) {
684 x += wwin->frame_x - wwin->maximus_x;
685 y += wwin->frame_y - wwin->maximus_y;
688 wwin->flags.maximized = 0;
689 wwin->flags.old_maximized = 0;
690 wWindowConfigure(wwin, x, y, w, h);
691 wWindowSynthConfigureNotify(wwin);
693 WMPostNotificationName(WMNChangedState, wwin, "maximize");
696 void wFullscreenWindow(WWindow *wwin)
698 int head;
699 WMRect rect;
701 if (wwin->flags.fullscreen)
702 return;
704 wwin->flags.fullscreen = True;
706 wWindowConfigureBorders(wwin);
708 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
710 wwin->bfs_geometry.x = wwin->frame_x;
711 wwin->bfs_geometry.y = wwin->frame_y;
712 wwin->bfs_geometry.width = wwin->frame->core->width;
713 wwin->bfs_geometry.height = wwin->frame->core->height;
715 head = wGetHeadForWindow(wwin);
716 rect = wGetRectForHead(wwin->screen_ptr, head);
717 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
719 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
722 void wUnfullscreenWindow(WWindow *wwin)
724 if (!wwin->flags.fullscreen)
725 return;
727 wwin->flags.fullscreen = False;
729 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
730 if (WFLAGP(wwin, sunken)) {
731 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
732 } else if (WFLAGP(wwin, floating)) {
733 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
734 } else {
735 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
739 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
740 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
742 wWindowConfigureBorders(wwin);
744 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
745 wFrameWindowPaint(wwin->frame);
748 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
751 #ifdef ANIMATIONS
752 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
754 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
755 float cx, cy, cw, ch;
756 float xstep, ystep, wstep, hstep;
757 XPoint points[5];
758 float dx, dch, midy;
759 float angle, final_angle, delta;
761 xstep = (float)(fx - x) / steps;
762 ystep = (float)(fy - y) / steps;
763 wstep = (float)(fw - w) / steps;
764 hstep = (float)(fh - h) / steps;
766 cx = (float)x;
767 cy = (float)y;
768 cw = (float)w;
769 ch = (float)h;
771 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
772 delta = (float)(final_angle / FRAMES);
773 for (angle = 0;; angle += delta) {
774 if (angle > final_angle)
775 angle = final_angle;
777 dx = (cw / 10) - ((cw / 5) * sin(angle));
778 dch = (ch / 2) * cos(angle);
779 midy = cy + (ch / 2);
781 points[0].x = cx + dx;
782 points[0].y = midy - dch;
783 points[1].x = cx + cw - dx;
784 points[1].y = points[0].y;
785 points[2].x = cx + cw + dx;
786 points[2].y = midy + dch;
787 points[3].x = cx - dx;
788 points[3].y = points[2].y;
789 points[4].x = points[0].x;
790 points[4].y = points[0].y;
792 XGrabServer(dpy);
793 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
794 XFlush(dpy);
795 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
797 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
798 XUngrabServer(dpy);
799 cx += xstep;
800 cy += ystep;
801 cw += wstep;
802 ch += hstep;
803 if (angle >= final_angle)
804 break;
807 XFlush(dpy);
810 #undef FRAMES
812 static void
813 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
815 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
816 float cx, cy, cw, ch;
817 float xstep, ystep, wstep, hstep;
818 XPoint points[5];
819 float angle, final_angle, a, d, delta;
821 x += w / 2;
822 y += h / 2;
823 fx += fw / 2;
824 fy += fh / 2;
826 xstep = (float)(fx - x) / steps;
827 ystep = (float)(fy - y) / steps;
828 wstep = (float)(fw - w) / steps;
829 hstep = (float)(fh - h) / steps;
831 cx = (float)x;
832 cy = (float)y;
833 cw = (float)w;
834 ch = (float)h;
836 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
837 delta = (float)(final_angle / FRAMES);
838 for (angle = 0;; angle += delta) {
839 if (angle > final_angle)
840 angle = final_angle;
842 a = atan(ch / cw);
843 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
845 points[0].x = cx + cos(angle - a) * d;
846 points[0].y = cy + sin(angle - a) * d;
847 points[1].x = cx + cos(angle + a) * d;
848 points[1].y = cy + sin(angle + a) * d;
849 points[2].x = cx + cos(angle - a + WM_PI) * d;
850 points[2].y = cy + sin(angle - a + WM_PI) * d;
851 points[3].x = cx + cos(angle + a + WM_PI) * d;
852 points[3].y = cy + sin(angle + a + WM_PI) * d;
853 points[4].x = cx + cos(angle - a) * d;
854 points[4].y = cy + sin(angle - a) * d;
855 XGrabServer(dpy);
856 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
857 XFlush(dpy);
858 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
860 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
861 XUngrabServer(dpy);
862 cx += xstep;
863 cy += ystep;
864 cw += wstep;
865 ch += hstep;
866 if (angle >= final_angle)
867 break;
870 XFlush(dpy);
873 #undef FRAMES
875 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
877 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
878 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
879 float xstep, ystep, wstep, hstep;
880 int i, j;
882 xstep = (float)(fx - x) / steps;
883 ystep = (float)(fy - y) / steps;
884 wstep = (float)(fw - w) / steps;
885 hstep = (float)(fh - h) / steps;
887 for (j = 0; j < FRAMES; j++) {
888 cx[j] = (float)x;
889 cy[j] = (float)y;
890 cw[j] = (float)w;
891 ch[j] = (float)h;
893 XGrabServer(dpy);
894 for (i = 0; i < steps; i++) {
895 for (j = 0; j < FRAMES; j++) {
896 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
897 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
899 XFlush(dpy);
900 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
902 for (j = 0; j < FRAMES; j++) {
903 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
904 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
905 if (j < FRAMES - 1) {
906 cx[j] = cx[j + 1];
907 cy[j] = cy[j + 1];
908 cw[j] = cw[j + 1];
909 ch[j] = ch[j + 1];
910 } else {
911 cx[j] += xstep;
912 cy[j] += ystep;
913 cw[j] += wstep;
914 ch[j] += hstep;
919 for (j = 0; j < FRAMES; j++) {
920 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
922 XFlush(dpy);
923 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
925 for (j = 0; j < FRAMES; j++) {
926 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
929 XUngrabServer(dpy);
932 #undef FRAMES
934 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
936 int style = wPreferences.iconification_style; /* Catch the value */
937 int steps;
939 if (style == WIS_NONE)
940 return;
942 if (style == WIS_RANDOM) {
943 style = rand() % 3;
946 switch (style) {
947 case WIS_TWIST:
948 steps = MINIATURIZE_ANIMATION_STEPS_T;
949 if (steps > 0)
950 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
951 break;
952 case WIS_FLIP:
953 steps = MINIATURIZE_ANIMATION_STEPS_F;
954 if (steps > 0)
955 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
956 break;
957 case WIS_ZOOM:
958 default:
959 steps = MINIATURIZE_ANIMATION_STEPS_Z;
960 if (steps > 0)
961 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
962 break;
965 #endif /* ANIMATIONS */
967 static void flushExpose(void)
969 XEvent tmpev;
971 while (XCheckTypedEvent(dpy, Expose, &tmpev))
972 WMHandleEvent(&tmpev);
973 XSync(dpy, 0);
976 static void unmapTransientsFor(WWindow *wwin)
978 WWindow *tmp;
980 tmp = wwin->screen_ptr->focused_window;
981 while (tmp) {
982 /* unmap the transients for this transient */
983 if (tmp != wwin && tmp->transient_for == wwin->client_win
984 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
985 unmapTransientsFor(tmp);
986 tmp->flags.miniaturized = 1;
987 if (!tmp->flags.shaded) {
988 wWindowUnmap(tmp);
989 } else {
990 XUnmapWindow(dpy, tmp->frame->core->window);
993 if (!tmp->flags.shaded)
995 wClientSetState(tmp, IconicState, None);
997 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
999 tmp = tmp->prev;
1003 static void mapTransientsFor(WWindow *wwin)
1005 WWindow *tmp;
1007 tmp = wwin->screen_ptr->focused_window;
1008 while (tmp) {
1009 /* recursively map the transients for this transient */
1010 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1011 && tmp->icon == NULL) {
1012 mapTransientsFor(tmp);
1013 tmp->flags.miniaturized = 0;
1014 if (!tmp->flags.shaded) {
1015 wWindowMap(tmp);
1016 } else {
1017 XMapWindow(dpy, tmp->frame->core->window);
1019 tmp->flags.semi_focused = 0;
1021 if (!tmp->flags.shaded)
1023 wClientSetState(tmp, NormalState, None);
1025 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1027 tmp = tmp->prev;
1031 static WWindow *recursiveTransientFor(WWindow * wwin)
1033 int i;
1035 if (!wwin)
1036 return None;
1038 /* hackish way to detect transient_for cycle */
1039 i = wwin->screen_ptr->window_count + 1;
1041 while (wwin && wwin->transient_for != None && i > 0) {
1042 wwin = wWindowFor(wwin->transient_for);
1043 i--;
1045 if (i == 0 && wwin) {
1046 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1047 return NULL;
1050 return wwin;
1053 void wIconifyWindow(WWindow * wwin)
1055 XWindowAttributes attribs;
1056 int present;
1058 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1059 return; /* the window doesn't exist anymore */
1061 if (wwin->flags.miniaturized)
1062 return; /* already miniaturized */
1064 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1065 WWindow *owner = wWindowFor(wwin->transient_for);
1067 if (owner && owner->flags.miniaturized)
1068 return;
1071 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1073 /* if the window is in another workspace, simplify process */
1074 if (present) {
1075 /* icon creation may take a while */
1076 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1077 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1078 GrabModeAsync, None, None, CurrentTime);
1081 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1082 if (!wwin->flags.icon_moved)
1083 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1085 wwin->icon = icon_create_for_wwindow(wwin);
1086 wwin->icon->mapped = 1;
1089 wwin->flags.miniaturized = 1;
1090 wwin->flags.mapped = 0;
1092 /* unmap transients */
1093 unmapTransientsFor(wwin);
1095 if (present) {
1096 XUngrabPointer(dpy, CurrentTime);
1097 wWindowUnmap(wwin);
1098 /* let all Expose events arrive so that we can repaint
1099 * something before the animation starts (and the server is grabbed) */
1100 XSync(dpy, 0);
1102 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1103 wClientSetState(wwin, IconicState, None);
1104 else
1105 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1107 flushExpose();
1108 #ifdef ANIMATIONS
1109 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1110 && !wPreferences.no_animations) {
1111 int ix, iy, iw, ih;
1113 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1114 ix = wwin->icon_x;
1115 iy = wwin->icon_y;
1116 iw = wwin->icon->core->width;
1117 ih = wwin->icon->core->height;
1118 } else {
1119 if (wwin->flags.net_handle_icon) {
1120 ix = wwin->icon_x;
1121 iy = wwin->icon_y;
1122 iw = wwin->icon_w;
1123 ih = wwin->icon_h;
1124 } else {
1125 ix = 0;
1126 iy = 0;
1127 iw = wwin->screen_ptr->scr_width;
1128 ih = wwin->screen_ptr->scr_height;
1131 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1132 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1134 #endif
1137 wwin->flags.skip_next_animation = 0;
1139 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1140 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1141 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1142 XMapWindow(dpy, wwin->icon->core->window);
1144 AddToStackList(wwin->icon->core);
1145 wLowerFrame(wwin->icon->core);
1148 if (present) {
1149 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1152 * It doesn't seem to be working and causes button event hangup
1153 * when deiconifying a transient window.
1154 setupIconGrabs(wwin->icon);
1156 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1157 && wPreferences.focus_mode == WKF_CLICK) {
1158 WWindow *tmp;
1160 tmp = wwin->prev;
1161 while (tmp) {
1162 if (!WFLAGP(tmp, no_focusable)
1163 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1164 && (wwin->frame->workspace == tmp->frame->workspace))
1165 break;
1166 tmp = tmp->prev;
1168 wSetFocusTo(wwin->screen_ptr, tmp);
1169 } else if (wPreferences.focus_mode != WKF_CLICK) {
1170 wSetFocusTo(wwin->screen_ptr, NULL);
1172 #ifdef ANIMATIONS
1173 if (!wwin->screen_ptr->flags.startup) {
1174 /* Catch up with events not processed while animation was running */
1175 Window clientwin = wwin->client_win;
1177 ProcessPendingEvents();
1179 /* the window can disappear while ProcessPendingEvents() runs */
1180 if (!wWindowFor(clientwin)) {
1181 return;
1184 #endif
1187 /* maybe we want to do this regardless of net_handle_icon
1188 * it seems to me we might break behaviour this way.
1190 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1191 && !wwin->flags.net_handle_icon)
1192 wIconSelect(wwin->icon);
1194 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1196 if (wPreferences.auto_arrange_icons)
1197 wArrangeIcons(wwin->screen_ptr, True);
1200 void wDeiconifyWindow(WWindow *wwin)
1202 /* Let's avoid changing workspace while deiconifying */
1203 ignore_wks_change = 1;
1205 /* we're hiding for show_desktop */
1206 int netwm_hidden = wwin->flags.net_show_desktop &&
1207 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1209 if (!netwm_hidden)
1210 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1212 if (!wwin->flags.miniaturized)
1213 return;
1215 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1216 WWindow *owner = recursiveTransientFor(wwin);
1218 if (owner && owner->flags.miniaturized) {
1219 wDeiconifyWindow(owner);
1220 wSetFocusTo(wwin->screen_ptr, wwin);
1221 wRaiseFrame(wwin->frame->core);
1222 return;
1226 wwin->flags.miniaturized = 0;
1228 if (!netwm_hidden && !wwin->flags.shaded)
1229 wwin->flags.mapped = 1;
1231 if (!netwm_hidden || wPreferences.sticky_icons) {
1232 /* maybe we want to do this regardless of net_handle_icon
1233 * it seems to me we might break behaviour this way.
1235 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1236 && wwin->icon != NULL) {
1237 if (wwin->icon->selected)
1238 wIconSelect(wwin->icon);
1240 XUnmapWindow(dpy, wwin->icon->core->window);
1244 /* if the window is in another workspace, do it silently */
1245 if (!netwm_hidden) {
1246 #ifdef ANIMATIONS
1247 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1248 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1249 int ix, iy, iw, ih;
1251 if (!wPreferences.disable_miniwindows
1252 && !wwin->flags.net_handle_icon) {
1253 ix = wwin->icon_x;
1254 iy = wwin->icon_y;
1255 iw = wwin->icon->core->width;
1256 ih = wwin->icon->core->height;
1257 } else {
1258 if (wwin->flags.net_handle_icon) {
1259 ix = wwin->icon_x;
1260 iy = wwin->icon_y;
1261 iw = wwin->icon_w;
1262 ih = wwin->icon_h;
1263 } else {
1264 ix = 0;
1265 iy = 0;
1266 iw = wwin->screen_ptr->scr_width;
1267 ih = wwin->screen_ptr->scr_height;
1270 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1271 wwin->frame_x, wwin->frame_y,
1272 wwin->frame->core->width, wwin->frame->core->height);
1274 #endif /* ANIMATIONS */
1275 wwin->flags.skip_next_animation = 0;
1276 XGrabServer(dpy);
1277 if (!wwin->flags.shaded)
1278 XMapWindow(dpy, wwin->client_win);
1280 XMapWindow(dpy, wwin->frame->core->window);
1281 wRaiseFrame(wwin->frame->core);
1282 if (!wwin->flags.shaded)
1283 wClientSetState(wwin, NormalState, None);
1285 mapTransientsFor(wwin);
1288 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1289 && !wwin->flags.net_handle_icon) {
1290 RemoveFromStackList(wwin->icon->core);
1291 /* removeIconGrabs(wwin->icon); */
1292 wIconDestroy(wwin->icon);
1293 wwin->icon = NULL;
1296 if (!netwm_hidden) {
1297 XUngrabServer(dpy);
1299 wSetFocusTo(wwin->screen_ptr, wwin);
1301 #ifdef ANIMATIONS
1302 if (!wwin->screen_ptr->flags.startup) {
1303 /* Catch up with events not processed while animation was running */
1304 Window clientwin = wwin->client_win;
1306 ProcessPendingEvents();
1308 /* the window can disappear while ProcessPendingEvents() runs */
1309 if (!wWindowFor(clientwin))
1310 return;
1312 #endif
1315 if (wPreferences.auto_arrange_icons)
1316 wArrangeIcons(wwin->screen_ptr, True);
1318 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1320 /* In case we were shaded and iconified, also unshade */
1321 if (!netwm_hidden)
1322 wUnshadeWindow(wwin);
1324 ignore_wks_change = 0;
1327 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1329 if (wwin->flags.miniaturized) {
1330 if (wwin->icon) {
1331 XUnmapWindow(dpy, wwin->icon->core->window);
1332 wwin->icon->mapped = 0;
1334 wwin->flags.hidden = 1;
1336 WMPostNotificationName(WMNChangedState, wwin, "hide");
1337 return;
1340 if (wwin->flags.inspector_open) {
1341 wHideInspectorForWindow(wwin);
1344 wwin->flags.hidden = 1;
1345 wWindowUnmap(wwin);
1347 wClientSetState(wwin, IconicState, icon->icon_win);
1348 flushExpose();
1350 #ifdef ANIMATIONS
1351 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1352 !wwin->flags.skip_next_animation && animate) {
1353 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1354 wwin->frame->core->width, wwin->frame->core->height,
1355 icon_x, icon_y, icon->core->width, icon->core->height);
1357 #endif
1358 wwin->flags.skip_next_animation = 0;
1360 WMPostNotificationName(WMNChangedState, wwin, "hide");
1363 void wHideAll(WScreen *scr)
1365 WWindow *wwin;
1366 WWindow **windows;
1367 WMenu *menu;
1368 unsigned int wcount = 0;
1369 int i;
1371 if (!scr)
1372 return;
1374 menu = scr->switch_menu;
1376 windows = wmalloc(sizeof(WWindow *));
1378 if (menu != NULL) {
1379 for (i = 0; i < menu->entry_no; i++) {
1380 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1381 wcount++;
1382 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1384 } else {
1385 wwin = scr->focused_window;
1387 while (wwin) {
1388 windows[wcount] = wwin;
1389 wcount++;
1390 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1391 wwin = wwin->prev;
1396 for (i = 0; i < wcount; i++) {
1397 wwin = windows[i];
1398 if (wwin->frame->workspace == scr->current_workspace
1399 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1400 && !wwin->flags.internal_window
1401 && !WFLAGP(wwin, no_miniaturizable)
1403 wwin->flags.skip_next_animation = 1;
1404 wIconifyWindow(wwin);
1408 wfree(windows);
1411 void wHideOtherApplications(WWindow *awin)
1413 WWindow *wwin;
1414 WApplication *tapp;
1416 if (!awin)
1417 return;
1418 wwin = awin->screen_ptr->focused_window;
1420 while (wwin) {
1421 if (wwin != awin
1422 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1423 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1424 && !wwin->flags.internal_window
1425 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1427 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1428 if (!WFLAGP(wwin, no_miniaturizable)) {
1429 wwin->flags.skip_next_animation = 1;
1430 wIconifyWindow(wwin);
1432 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1433 tapp = wApplicationOf(wwin->main_window);
1434 if (tapp) {
1435 tapp->flags.skip_next_animation = 1;
1436 wHideApplication(tapp);
1437 } else {
1438 if (!WFLAGP(wwin, no_miniaturizable)) {
1439 wwin->flags.skip_next_animation = 1;
1440 wIconifyWindow(wwin);
1445 wwin = wwin->prev;
1448 wSetFocusTo(awin->screen_ptr, awin);
1452 void wHideApplication(WApplication *wapp)
1454 WScreen *scr;
1455 WWindow *wlist;
1456 int hadfocus;
1457 int animate;
1459 if (!wapp) {
1460 wwarning("trying to hide a non grouped window");
1461 return;
1463 if (!wapp->main_window_desc) {
1464 wwarning("group leader not found for window group");
1465 return;
1467 scr = wapp->main_window_desc->screen_ptr;
1468 hadfocus = 0;
1469 wlist = scr->focused_window;
1470 if (!wlist)
1471 return;
1473 if (wlist->main_window == wapp->main_window)
1474 wapp->last_focused = wlist;
1475 else
1476 wapp->last_focused = NULL;
1478 animate = !wapp->flags.skip_next_animation;
1480 while (wlist) {
1481 if (wlist->main_window == wapp->main_window) {
1482 if (wlist->flags.focused) {
1483 hadfocus = 1;
1485 if (wapp->app_icon) {
1486 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1487 wapp->app_icon->y_pos, wlist, animate);
1488 animate = False;
1491 wlist = wlist->prev;
1494 wapp->flags.skip_next_animation = 0;
1496 if (hadfocus) {
1497 if (wPreferences.focus_mode == WKF_CLICK) {
1498 wlist = scr->focused_window;
1499 while (wlist) {
1500 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1501 && (wlist->flags.mapped || wlist->flags.shaded))
1502 break;
1503 wlist = wlist->prev;
1505 wSetFocusTo(scr, wlist);
1506 } else {
1507 wSetFocusTo(scr, NULL);
1511 wapp->flags.hidden = 1;
1513 if (wPreferences.auto_arrange_icons)
1514 wArrangeIcons(scr, True);
1516 #ifdef HIDDENDOT
1517 if (wapp->app_icon)
1518 wAppIconPaint(wapp->app_icon);
1519 #endif
1522 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1524 if (bringToCurrentWS)
1525 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1527 wwin->flags.hidden = 0;
1529 #ifdef ANIMATIONS
1530 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1531 animateResize(wwin->screen_ptr, icon_x, icon_y,
1532 icon->core->width, icon->core->height,
1533 wwin->frame_x, wwin->frame_y,
1534 wwin->frame->core->width, wwin->frame->core->height);
1536 #endif
1537 wwin->flags.skip_next_animation = 0;
1538 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1539 XMapWindow(dpy, wwin->client_win);
1540 XMapWindow(dpy, wwin->frame->core->window);
1541 wClientSetState(wwin, NormalState, None);
1542 wwin->flags.mapped = 1;
1543 wRaiseFrame(wwin->frame->core);
1545 if (wwin->flags.inspector_open) {
1546 wUnhideInspectorForWindow(wwin);
1549 WMPostNotificationName(WMNChangedState, wwin, "hide");
1552 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1554 WScreen *scr;
1555 WWindow *wlist, *next;
1556 WWindow *focused = NULL;
1557 int animate;
1559 if (!wapp)
1560 return;
1562 scr = wapp->main_window_desc->screen_ptr;
1563 wlist = scr->focused_window;
1564 if (!wlist)
1565 return;
1567 /* goto beginning of list */
1568 while (wlist->prev)
1569 wlist = wlist->prev;
1571 animate = !wapp->flags.skip_next_animation;
1573 while (wlist) {
1574 next = wlist->next;
1576 if (wlist->main_window == wapp->main_window) {
1577 if (wlist->flags.focused)
1578 focused = wlist;
1579 else if (!focused || !focused->flags.focused)
1580 focused = wlist;
1582 if (wlist->flags.miniaturized) {
1583 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1584 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1585 if (!wlist->icon->mapped) {
1586 int x, y;
1588 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1589 if (wlist->icon_x != x || wlist->icon_y != y) {
1590 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1592 wlist->icon_x = x;
1593 wlist->icon_y = y;
1594 XMapWindow(dpy, wlist->icon->core->window);
1595 wlist->icon->mapped = 1;
1597 wRaiseFrame(wlist->icon->core);
1599 if (bringToCurrentWS)
1600 wWindowChangeWorkspace(wlist, scr->current_workspace);
1601 wlist->flags.hidden = 0;
1602 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1603 wDeiconifyWindow(wlist);
1605 WMPostNotificationName(WMNChangedState, wlist, "hide");
1606 } else if (wlist->flags.shaded) {
1607 if (bringToCurrentWS)
1608 wWindowChangeWorkspace(wlist, scr->current_workspace);
1609 wlist->flags.hidden = 0;
1610 wRaiseFrame(wlist->frame->core);
1611 if (wlist->frame->workspace == scr->current_workspace) {
1612 XMapWindow(dpy, wlist->frame->core->window);
1613 if (miniwindows) {
1614 wUnshadeWindow(wlist);
1617 WMPostNotificationName(WMNChangedState, wlist, "hide");
1618 } else if (wlist->flags.hidden) {
1619 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1620 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1621 animate = False;
1622 } else {
1623 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1624 wWindowChangeWorkspace(wlist, scr->current_workspace);
1626 wRaiseFrame(wlist->frame->core);
1629 wlist = next;
1632 wapp->flags.skip_next_animation = 0;
1633 wapp->flags.hidden = 0;
1635 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1636 wRaiseFrame(wapp->last_focused->frame->core);
1637 wSetFocusTo(scr, wapp->last_focused);
1638 } else if (focused) {
1639 wSetFocusTo(scr, focused);
1641 wapp->last_focused = NULL;
1642 if (wPreferences.auto_arrange_icons)
1643 wArrangeIcons(scr, True);
1645 #ifdef HIDDENDOT
1646 wAppIconPaint(wapp->app_icon);
1647 #endif
1650 void wShowAllWindows(WScreen *scr)
1652 WWindow *wwin, *old_foc;
1653 WApplication *wapp;
1655 old_foc = wwin = scr->focused_window;
1656 while (wwin) {
1657 if (!wwin->flags.internal_window &&
1658 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1659 if (wwin->flags.miniaturized) {
1660 wwin->flags.skip_next_animation = 1;
1661 wDeiconifyWindow(wwin);
1662 } else if (wwin->flags.hidden) {
1663 wapp = wApplicationOf(wwin->main_window);
1664 if (wapp) {
1665 wUnhideApplication(wapp, False, False);
1666 } else {
1667 wwin->flags.skip_next_animation = 1;
1668 wDeiconifyWindow(wwin);
1672 wwin = wwin->prev;
1674 wSetFocusTo(scr, old_foc);
1675 /*wRaiseFrame(old_foc->frame->core); */
1678 void wRefreshDesktop(WScreen *scr)
1680 Window win;
1681 XSetWindowAttributes attr;
1683 attr.backing_store = NotUseful;
1684 attr.save_under = False;
1685 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1686 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1687 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1688 XMapRaised(dpy, win);
1689 XDestroyWindow(dpy, win);
1690 XFlush(dpy);
1693 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1695 WWindow *wwin;
1696 WAppIcon *aicon;
1698 int head;
1699 const int heads = wXineramaHeads(scr);
1701 struct HeadVars {
1702 int pf; /* primary axis */
1703 int sf; /* secondary axis */
1704 int fullW;
1705 int fullH;
1706 int pi, si;
1707 int sx1, sx2, sy1, sy2; /* screen boundary */
1708 int sw, sh;
1709 int xo, yo;
1710 int xs, ys;
1711 } *vars;
1713 int isize = wPreferences.icon_size;
1715 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1717 for (head = 0; head < heads; ++head) {
1718 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1719 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1720 vars[head].pi = vars[head].si = 0;
1721 vars[head].sx1 = rect.pos.x;
1722 vars[head].sy1 = rect.pos.y;
1723 vars[head].sw = rect.size.width;
1724 vars[head].sh = rect.size.height;
1725 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1726 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1727 vars[head].sw = isize * (vars[head].sw / isize);
1728 vars[head].sh = isize * (vars[head].sh / isize);
1729 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1730 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1732 /* icon yard boundaries */
1733 if (wPreferences.icon_yard & IY_VERT) {
1734 vars[head].pf = vars[head].fullH;
1735 vars[head].sf = vars[head].fullW;
1736 } else {
1737 vars[head].pf = vars[head].fullW;
1738 vars[head].sf = vars[head].fullH;
1740 if (wPreferences.icon_yard & IY_RIGHT) {
1741 vars[head].xo = vars[head].sx2 - isize;
1742 vars[head].xs = -1;
1743 } else {
1744 vars[head].xo = vars[head].sx1;
1745 vars[head].xs = 1;
1747 if (wPreferences.icon_yard & IY_TOP) {
1748 vars[head].yo = vars[head].sy1;
1749 vars[head].ys = 1;
1750 } else {
1751 vars[head].yo = vars[head].sy2 - isize;
1752 vars[head].ys = -1;
1756 #define X ((wPreferences.icon_yard & IY_VERT) \
1757 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1758 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1760 #define Y ((wPreferences.icon_yard & IY_VERT) \
1761 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1762 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1764 /* arrange application icons */
1765 aicon = scr->app_icon_list;
1766 /* reverse them to avoid unnecessarily sliding of icons */
1767 while (aicon && aicon->next)
1768 aicon = aicon->next;
1770 while (aicon) {
1771 if (!aicon->docked) {
1772 /* CHECK: can icon be NULL here ? */
1773 /* The intention here is to place the AppIcon on the head that
1774 * contains most of the applications _main_ window. */
1775 head = wGetHeadForWindow(aicon->icon->owner);
1777 if (aicon->x_pos != X || aicon->y_pos != Y) {
1778 #ifdef ANIMATIONS
1779 if (!wPreferences.no_animations)
1780 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1781 #endif /* ANIMATIONS */
1783 wAppIconMove(aicon, X, Y);
1784 vars[head].pi++;
1785 if (vars[head].pi >= vars[head].pf) {
1786 vars[head].pi = 0;
1787 vars[head].si++;
1790 aicon = aicon->prev;
1793 /* arrange miniwindows */
1794 wwin = scr->focused_window;
1795 /* reverse them to avoid unnecessarily shuffling */
1796 while (wwin && wwin->prev)
1797 wwin = wwin->prev;
1799 while (wwin) {
1800 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1801 (wwin->frame->workspace == scr->current_workspace ||
1802 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1804 head = wGetHeadForWindow(wwin);
1806 if (arrangeAll || !wwin->flags.icon_moved) {
1807 if (wwin->icon_x != X || wwin->icon_y != Y)
1808 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1810 wwin->icon_x = X;
1811 wwin->icon_y = Y;
1813 vars[head].pi++;
1814 if (vars[head].pi >= vars[head].pf) {
1815 vars[head].pi = 0;
1816 vars[head].si++;
1820 if (arrangeAll) {
1821 wwin->flags.icon_moved = 0;
1823 /* we reversed the order, so we use next */
1824 wwin = wwin->next;
1827 wfree(vars);
1830 void wSelectWindow(WWindow *wwin, Bool flag)
1832 WScreen *scr = wwin->screen_ptr;
1834 if (flag) {
1835 wwin->flags.selected = 1;
1836 if (wwin->frame->selected_border_pixel)
1837 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1838 else
1839 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1841 if (!HAS_BORDER(wwin)) {
1842 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1845 if (!scr->selected_windows)
1846 scr->selected_windows = WMCreateArray(4);
1847 WMAddToArray(scr->selected_windows, wwin);
1848 } else {
1849 wwin->flags.selected = 0;
1850 if (wwin->frame->border_pixel)
1851 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1852 else
1853 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1855 if (!HAS_BORDER(wwin)) {
1856 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1859 if (scr->selected_windows) {
1860 WMRemoveFromArray(scr->selected_windows, wwin);
1865 void wMakeWindowVisible(WWindow *wwin)
1867 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1868 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1870 if (wwin->flags.shaded) {
1871 wUnshadeWindow(wwin);
1873 if (wwin->flags.hidden) {
1874 WApplication *app;
1876 app = wApplicationOf(wwin->main_window);
1877 if (app) {
1878 /* trick to get focus to this window */
1879 app->last_focused = wwin;
1880 wUnhideApplication(app, False, False);
1883 if (wwin->flags.miniaturized) {
1884 wDeiconifyWindow(wwin);
1885 } else {
1886 if (!WFLAGP(wwin, no_focusable))
1887 wSetFocusTo(wwin->screen_ptr, wwin);
1888 wRaiseFrame(wwin->frame->core);
1893 * Do the animation while shading (called with what = SHADE)
1894 * or unshading (what = UNSHADE).
1896 #ifdef ANIMATIONS
1897 static void shade_animate(WWindow *wwin, Bool what)
1899 int y, s, w, h;
1900 time_t time0 = time(NULL);
1902 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1903 return;
1905 switch(what) {
1906 case SHADE:
1907 if (!wwin->screen_ptr->flags.startup) {
1908 /* do the shading animation */
1909 h = wwin->frame->core->height;
1910 s = h / SHADE_STEPS;
1911 if (s < 1)
1912 s = 1;
1913 w = wwin->frame->core->width;
1914 y = wwin->frame->top_width;
1915 while (h > wwin->frame->top_width + 1) {
1916 XMoveWindow(dpy, wwin->client_win, 0, y);
1917 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1918 XFlush(dpy);
1920 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1921 break;
1923 if (SHADE_DELAY > 0) {
1924 wusleep(SHADE_DELAY * 1000L);
1925 } else {
1926 wusleep(10);
1928 h -= s;
1929 y -= s;
1931 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1933 break;
1935 case UNSHADE:
1936 h = wwin->frame->top_width + wwin->frame->bottom_width;
1937 y = wwin->frame->top_width - wwin->client.height;
1938 s = abs(y) / SHADE_STEPS;
1939 if (s < 1)
1940 s = 1;
1941 w = wwin->frame->core->width;
1942 XMoveWindow(dpy, wwin->client_win, 0, y);
1943 if (s > 0) {
1944 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1945 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1946 XMoveWindow(dpy, wwin->client_win, 0, y);
1947 XFlush(dpy);
1948 if (SHADE_DELAY > 0) {
1949 wusleep(SHADE_DELAY * 2000L / 3);
1950 } else {
1951 wusleep(10);
1953 h += s;
1954 y += s;
1956 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1957 break;
1960 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1961 break;
1964 #endif