icon_update_pixmap default moved to bottom
[wmaker-crm.git] / src / actions.c
blob3fb3fe62a1d0a4e5e5baa3adaadec25cbc14bd5b
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;
59 extern void ProcessPendingEvents();
61 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
62 unsigned int *new_width, unsigned int *new_height);
63 static void save_old_geometry(WWindow *wwin, int directions);
65 /******* Local Variables *******/
66 static struct {
67 int steps;
68 int delay;
69 } shadePars[5] = {
71 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
72 SHADE_STEPS_F, SHADE_DELAY_F}, {
73 SHADE_STEPS_M, SHADE_DELAY_M}, {
74 SHADE_STEPS_S, SHADE_DELAY_S}, {
75 SHADE_STEPS_US, SHADE_DELAY_US}};
77 #define UNSHADE 0
78 #define SHADE 1
79 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
80 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
82 static int compareTimes(Time t1, Time t2)
84 Time diff;
85 if (t1 == t2)
86 return 0;
87 diff = t1 - t2;
88 return (diff < 60000) ? 1 : -1;
91 #ifdef ANIMATIONS
92 static void shade_animate(WWindow *wwin, Bool what);
93 #else
94 static void shade_animate(WWindow *wwin, Bool what) { }
95 #endif
98 *----------------------------------------------------------------------
99 * wSetFocusTo--
100 * Changes the window focus to the one passed as argument.
101 * If the window to focus is not already focused, it will be brought
102 * to the head of the list of windows. Previously focused window is
103 * unfocused.
105 * Side effects:
106 * Window list may be reordered and the window focus is changed.
108 *----------------------------------------------------------------------
110 void wSetFocusTo(WScreen *scr, WWindow *wwin)
112 static WScreen *old_scr = NULL;
114 WWindow *old_focused;
115 WWindow *focused = scr->focused_window;
116 Time timestamp = LastTimestamp;
117 WApplication *oapp = NULL, *napp = NULL;
118 int wasfocused;
120 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
121 return;
123 if (!old_scr)
124 old_scr = scr;
126 old_focused = old_scr->focused_window;
128 LastFocusChange = timestamp;
130 if (old_focused)
131 oapp = wApplicationOf(old_focused->main_window);
133 if (wwin == NULL) {
134 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
135 if (old_focused)
136 wWindowUnfocus(old_focused);
138 if (oapp) {
139 wAppMenuUnmap(oapp->menu);
140 if (wPreferences.highlight_active_app)
141 wApplicationDeactivate(oapp);
144 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
145 return;
148 if (old_scr != scr && old_focused)
149 wWindowUnfocus(old_focused);
151 wasfocused = wwin->flags.focused;
152 napp = wApplicationOf(wwin->main_window);
154 /* remember last workspace where the app has been */
155 if (napp)
156 napp->last_workspace = wwin->frame->workspace;
158 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
159 /* install colormap if colormap mode is lock mode */
160 if (wPreferences.colormap_mode == WCM_CLICK)
161 wColormapInstallForWindow(scr, wwin);
163 /* set input focus */
164 switch (wwin->focus_mode) {
165 case WFM_NO_INPUT:
166 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
167 break;
168 case WFM_PASSIVE:
169 case WFM_LOCALLY_ACTIVE:
170 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
171 break;
172 case WFM_GLOBALLY_ACTIVE:
173 break;
176 XFlush(dpy);
177 if (wwin->protocols.TAKE_FOCUS)
178 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
180 XSync(dpy, False);
181 } else {
182 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
185 if (WFLAGP(wwin, no_focusable))
186 return;
188 /* if this is not the focused window focus it */
189 if (focused != wwin) {
190 /* change the focus window list order */
191 if (wwin->prev)
192 wwin->prev->next = wwin->next;
194 if (wwin->next)
195 wwin->next->prev = wwin->prev;
197 wwin->prev = focused;
198 focused->next = wwin;
199 wwin->next = NULL;
200 scr->focused_window = wwin;
202 if (oapp && oapp != napp) {
203 wAppMenuUnmap(oapp->menu);
204 if (wPreferences.highlight_active_app)
205 wApplicationDeactivate(oapp);
209 wWindowFocus(wwin, focused);
211 if (napp && !wasfocused) {
212 #ifdef USER_MENU
213 wUserMenuRefreshInstances(napp->menu, wwin);
214 #endif /* USER_MENU */
216 if (wwin->flags.mapped)
217 wAppMenuMap(napp->menu, wwin);
219 if (napp && wPreferences.highlight_active_app)
220 wApplicationActivate(napp);
222 XFlush(dpy);
223 old_scr = scr;
226 void wShadeWindow(WWindow *wwin)
229 if (wwin->flags.shaded)
230 return;
232 XLowerWindow(dpy, wwin->client_win);
233 shade_animate(wwin, SHADE);
235 wwin->flags.skip_next_animation = 0;
236 wwin->flags.shaded = 1;
237 wwin->flags.mapped = 0;
238 /* prevent window withdrawal when getting UnmapNotify */
239 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
240 XUnmapWindow(dpy, wwin->client_win);
241 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
243 /* for the client it's just like iconification */
244 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
246 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
247 wWindowSynthConfigureNotify(wwin);
250 wClientSetState(wwin, IconicState, None);
253 WMPostNotificationName(WMNChangedState, wwin, "shade");
255 #ifdef ANIMATIONS
256 if (!wwin->screen_ptr->flags.startup) {
257 /* Catch up with events not processed while animation was running */
258 ProcessPendingEvents();
260 #endif
263 void wUnshadeWindow(WWindow *wwin)
266 if (!wwin->flags.shaded)
267 return;
269 wwin->flags.shaded = 0;
270 wwin->flags.mapped = 1;
271 XMapWindow(dpy, wwin->client_win);
273 shade_animate(wwin, UNSHADE);
275 wwin->flags.skip_next_animation = 0;
276 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
277 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
279 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
280 wWindowSynthConfigureNotify(wwin);
282 /* if the window is focused, set the focus again as it was disabled during
283 * shading */
284 if (wwin->flags.focused)
285 wSetFocusTo(wwin->screen_ptr, wwin);
287 WMPostNotificationName(WMNChangedState, wwin, "shade");
290 /* Set the old coordinates using the current values */
291 static void save_old_geometry(WWindow *wwin, int directions)
293 /* never been saved? */
294 if (! wwin->old_geometry.width)
295 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
296 if (! wwin->old_geometry.height)
297 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
299 if (directions & SAVE_GEOMETRY_X)
300 wwin->old_geometry.x = wwin->frame_x;
301 if (directions & SAVE_GEOMETRY_Y)
302 wwin->old_geometry.y = wwin->frame_y;
303 if (directions & SAVE_GEOMETRY_WIDTH)
304 wwin->old_geometry.width = wwin->client.width;
305 if (directions & SAVE_GEOMETRY_HEIGHT)
306 wwin->old_geometry.height = wwin->client.height;
309 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
311 WMRect old_geom_rect;
312 int old_head;
313 Bool same_head;
315 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
316 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
317 same_head = (wGetHeadForWindow(wwin) == old_head);
318 *x = (wwin->old_geometry.x && same_head) ? wwin->old_geometry.x : wwin->frame_x;
319 *y = (wwin->old_geometry.y && same_head) ? wwin->old_geometry.y : wwin->frame_y;
320 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
321 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
324 /* Remember geometry for unmaximizing */
325 void update_saved_geometry(WWindow *wwin)
327 /* NOT if we aren't already maximized
328 * we'll save geometry when maximizing */
329 if (!wwin->flags.maximized)
330 return;
332 /* NOT if we are fully maximized */
333 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
334 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
335 (wwin->flags.maximized & MAX_VERTICAL)))
336 return;
338 /* save the co-ordinate in the axis in which we AREN'T maximized */
339 if (wwin->flags.maximized & MAX_HORIZONTAL)
340 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
341 if (wwin->flags.maximized & MAX_VERTICAL)
342 save_old_geometry(wwin, SAVE_GEOMETRY_X);
345 #define IS_MAX_HORIZONTALLY(directions) ((directions & MAX_HORIZONTAL) | (directions & MAX_LEFTHALF) | (directions & MAX_RIGHTHALF))
346 void wMaximizeWindow(WWindow *wwin, int directions)
348 int new_x, new_y;
349 unsigned int new_width, new_height, half_scr_width;
350 int maximus_x = 0;
351 int maximus_y = 0;
352 unsigned int maximus_width = 0;
353 unsigned int maximus_height = 0;
354 WArea usableArea, totalArea;
355 Bool has_border = 1;
356 int save_directions = 0;
357 int adj_size;
359 if (!IS_RESIZABLE(wwin))
360 return;
362 if (!HAS_BORDER(wwin))
363 has_border = 0;
365 /* the size to adjust the geometry */
366 adj_size = wwin->screen_ptr->frame_border_width * 2 * has_border;
368 /* save old coordinates before we change the current values
369 * always if the window is not currently maximized at all
370 * but never if the window has been Maximusized */
371 if (!wwin->flags.maximized)
372 save_directions |= SAVE_GEOMETRY_ALL;
373 else if (!(wwin->flags.old_maximized & MAX_MAXIMUS)) {
374 if ((directions & MAX_VERTICAL) &&
375 !(wwin->flags.maximized & MAX_VERTICAL))
376 save_directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
377 if (IS_MAX_HORIZONTALLY(directions) &&
378 !IS_MAX_HORIZONTALLY(wwin->flags.maximized))
379 save_directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
381 if ((directions & MAX_MAXIMUS) && !wwin->flags.maximized)
382 save_directions |= SAVE_GEOMETRY_ALL;
383 save_old_geometry(wwin, save_directions);
385 totalArea.x1 = 0;
386 totalArea.y1 = 0;
387 totalArea.x2 = wwin->screen_ptr->scr_width;
388 totalArea.y2 = wwin->screen_ptr->scr_height;
389 usableArea = totalArea;
391 if (!(directions & MAX_IGNORE_XINERAMA)) {
392 WScreen *scr = wwin->screen_ptr;
393 int head;
395 if (directions & MAX_KEYBOARD)
396 head = wGetHeadForWindow(wwin);
397 else
398 head = wGetHeadForPointerLocation(scr);
400 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
403 /* remember Maximus geometry if we'll need it later */
404 if ((wwin->flags.old_maximized & MAX_MAXIMUS) || (directions & MAX_MAXIMUS))
405 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
407 /* Only save directions, not kbd or xinerama hints */
408 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
410 if (WFLAGP(wwin, full_maximize)) {
411 usableArea = totalArea;
413 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
415 if (wwin->flags.shaded) {
416 wwin->flags.skip_next_animation = 1;
417 wUnshadeWindow(wwin);
420 if (directions & MAX_HORIZONTAL) {
421 new_width = usableArea.x2 - usableArea.x1 - adj_size;
422 new_x = usableArea.x1;
423 } else if (directions & MAX_LEFTHALF) {
424 new_width = half_scr_width - adj_size;
425 new_x = usableArea.x1;
426 wwin->flags.old_maximized |= MAX_LEFTHALF;
427 wwin->flags.old_maximized &= ~MAX_RIGHTHALF;
428 } else if (directions & MAX_RIGHTHALF) {
429 new_width = half_scr_width - adj_size;
430 new_x = usableArea.x1 + half_scr_width;
431 wwin->flags.old_maximized |= MAX_RIGHTHALF;
432 wwin->flags.old_maximized &= ~MAX_LEFTHALF;
433 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
434 new_x = maximus_x;
435 new_width = maximus_width - adj_size;
436 } else if (IS_MAX_HORIZONTALLY(wwin->flags.maximized)) {
437 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
438 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
439 } else {
440 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
441 new_x = wwin->frame_x;
442 new_width = wwin->frame->core->width;
445 if (directions & MAX_VERTICAL) {
446 new_height = usableArea.y2 - usableArea.y1 - adj_size;
447 new_y = usableArea.y1;
448 if (WFLAGP(wwin, full_maximize)) {
449 new_y -= wwin->frame->top_width;
450 new_height += wwin->frame->bottom_width - 1;
452 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
453 new_y = maximus_y;
454 new_height = maximus_height - adj_size;
455 /* HACK: this will be subtracted again below */
456 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
457 } else if (wwin->flags.maximized & MAX_VERTICAL) {
458 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
459 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
460 /* HACK: this will be subtracted again below */
461 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
462 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
463 } else {
464 new_y = wwin->frame_y;
465 new_height = wwin->frame->core->height;
468 if (!WFLAGP(wwin, full_maximize)) {
469 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
472 if (directions & MAX_MAXIMUS) {
473 new_x = maximus_x;
474 new_y = maximus_y;
475 new_width = maximus_width - adj_size;
476 new_height = maximus_height - adj_size;
477 if (WFLAGP(wwin, full_maximize) && new_y == 0) {
478 new_y -= wwin->frame->top_width;
479 new_height += wwin->frame->top_width - 1;
481 wwin->maximus_x = new_x;
482 wwin->maximus_y = new_y;
483 wwin->flags.old_maximized |= MAX_MAXIMUS;
486 wWindowConstrainSize(wwin, &new_width, &new_height);
488 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
489 usableArea.y2 - usableArea.y1, &new_width, &new_height);
491 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
492 wWindowSynthConfigureNotify(wwin);
494 WMPostNotificationName(WMNChangedState, wwin, "maximize");
496 /* set maximization state */
497 wwin->flags.maximized = directions;
498 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
499 wwin->flags.maximized = MAX_MAXIMUS;
502 /* generic (un)maximizer */
503 void handleMaximize(WWindow *wwin, int directions)
505 int current = wwin->flags.maximized;
506 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
507 int effective = requested ^ current;
508 int flags = directions & ~requested;
510 if (!effective) {
511 /* allow wMaximizeWindow to restore the Maximusized size */
512 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
513 !(requested & MAX_MAXIMUS))
514 wMaximizeWindow(wwin, flags);
515 else
516 wUnmaximizeWindow(wwin);
518 else {
519 /* MAX_MAXIMUS takes precedence */
520 effective &= ~MAX_MAXIMUS;
521 if (requested & MAX_MAXIMUS) {
522 /* window was previously Maximusized then maximized */
523 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !current) {
524 wUnmaximizeWindow(wwin);
525 return;
527 else
528 effective = MAX_MAXIMUS;
530 else if (requested == (MAX_HORIZONTAL | MAX_VERTICAL))
531 effective = requested;
532 else {
533 /* handle MAX_HORIZONTAL -> MAX_(LEFT|RIGHT)HALF */
534 if (IS_MAX_HORIZONTALLY(current)) {
535 if (IS_MAX_HORIZONTALLY(requested)) {
536 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF);
537 effective |= (requested & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF));
538 if (requested & MAX_HORIZONTAL) {
539 /* restore to half maximization */
540 if (wwin->flags.old_maximized & MAX_LEFTHALF)
541 effective |= MAX_LEFTHALF;
542 else if (wwin->flags.old_maximized & MAX_RIGHTHALF)
543 effective |= MAX_RIGHTHALF;
545 /* MAX_VERTICAL is implicit with MAX_(LEFT|RIGHT)HALF */
546 else
547 effective |= MAX_VERTICAL;
548 } else {
549 /* toggling MAX_VERTICAL */
550 if ((requested & MAX_VERTICAL) &&
551 (current & MAX_VERTICAL)) {
552 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF | MAX_VERTICAL);
556 /* handle MAX_VERTICAL -> MAX_(LEFT|RIGHT)HALF */
557 if (current & MAX_VERTICAL) {
558 if ((requested & MAX_LEFTHALF) ||
559 (requested & MAX_RIGHTHALF)) {
560 effective |= MAX_VERTICAL;
563 /* toggling MAX_HORIZONTAL */
564 if ((requested & MAX_HORIZONTAL) &&
565 (current & MAX_HORIZONTAL))
566 effective &= ~MAX_HORIZONTAL;
568 wMaximizeWindow(wwin, effective | flags);
570 return;
573 /* the window boundary coordinates */
574 typedef struct {
575 int left;
576 int right;
577 int bottom;
578 int top;
579 int width;
580 int height;
581 } win_coords;
583 static void set_window_coords(WWindow *wwin, win_coords *obs)
585 obs->left = wwin->frame_x;
586 obs->top = wwin->frame_y;
587 obs->width = wwin->frame->core->width;
588 obs->height = wwin->frame->core->height;
589 obs->bottom = obs->top + obs->height;
590 obs->right = obs->left + obs->width;
594 * Maximus: tiled maximization (maximize without overlapping other windows)
596 * The original window 'orig' will be maximized to new coordinates 'new'.
597 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
599 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
600 unsigned int *new_width, unsigned int *new_height)
602 WWindow *tmp;
603 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
604 short int adjust_height;
605 int x_intsect, y_intsect;
606 /* the obstructing, original and new windows */
607 win_coords obs, orig, new;
609 /* set the original coordinate positions of the window to be Maximumized */
610 if (wwin->flags.maximized) {
611 /* window is already maximized; consider original geometry */
612 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
613 orig.bottom = orig.top + orig.height;
614 orig.right = orig.left + orig.width;
616 else
617 set_window_coords(wwin, &orig);
619 /* Try to fully maximize first, then readjust later */
620 new.left = usableArea.x1;
621 new.right = usableArea.x2;
622 new.top = usableArea.y1;
623 new.bottom = usableArea.y2;
625 if (HAS_TITLEBAR(wwin))
626 tbar_height_0 = TITLEBAR_HEIGHT;
627 if (HAS_RESIZEBAR(wwin))
628 rbar_height_0 = RESIZEBAR_HEIGHT;
629 if (HAS_BORDER(wwin))
630 bd_width_0 = wwin->screen_ptr->frame_border_width;
632 /* the length to be subtracted if the window has titlebar, etc */
633 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
635 tmp = wwin;
636 /* The focused window is always the last in the list */
637 while (tmp->prev) {
638 /* ignore windows in other workspaces etc */
639 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
640 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
641 tmp = tmp->prev;
642 continue;
644 tmp = tmp->prev;
646 /* Set the coordinates of obstructing window */
647 set_window_coords(tmp, &obs);
649 /* Try to maximize in the y direction first */
650 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
651 if (x_intsect != 0) {
652 /* TODO: Consider the case when coords are equal */
653 if (obs.bottom < orig.top && obs.bottom > new.top) {
654 /* w_0 is below the bottom of w_j */
655 new.top = obs.bottom + 1;
657 if (orig.bottom < obs.top && obs.top < new.bottom) {
658 /* The bottom of w_0 is above the top of w_j */
659 new.bottom = obs.top - 1;
664 tmp = wwin;
665 while (tmp->prev) {
666 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
667 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
668 tmp = tmp->prev;
669 continue;
671 tmp = tmp->prev;
673 set_window_coords(tmp, &obs);
676 * Use the new.top and new.height instead of original values
677 * as they may have different intersections with the obstructing windows
679 new.height = new.bottom - new.top - adjust_height;
680 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
681 if (y_intsect != 0) {
682 if (obs.right < orig.left && obs.right > new.left) {
683 /* w_0 is completely to the right of w_j */
684 new.left = obs.right + 1;
686 if (orig.right < obs.left && obs.left < new.right) {
687 /* w_0 is completely to the left of w_j */
688 new.right = obs.left - 1;
693 *new_x = new.left;
694 *new_y = new.top;
695 /* xcalc needs -7 here, but other apps don't */
696 *new_height = new.bottom - new.top - adjust_height - 1;;
697 *new_width = new.right - new.left;
700 void wUnmaximizeWindow(WWindow *wwin)
702 int x, y, w, h;
704 if (!wwin->flags.maximized)
705 return;
707 if (wwin->flags.shaded) {
708 wwin->flags.skip_next_animation = 1;
709 wUnshadeWindow(wwin);
712 /* Use old coordinates if they are set, current values otherwise */
713 remember_geometry(wwin, &x, &y, &w, &h);
715 /* unMaximusize relative to original position */
716 if (wwin->flags.maximized & MAX_MAXIMUS) {
717 x += wwin->frame_x - wwin->maximus_x;
718 y += wwin->frame_y - wwin->maximus_y;
721 wwin->flags.maximized = 0;
722 wwin->flags.old_maximized = 0;
723 wWindowConfigure(wwin, x, y, w, h);
724 wWindowSynthConfigureNotify(wwin);
726 WMPostNotificationName(WMNChangedState, wwin, "maximize");
729 void wFullscreenWindow(WWindow *wwin)
731 int head;
732 WMRect rect;
734 if (wwin->flags.fullscreen)
735 return;
737 wwin->flags.fullscreen = True;
739 wWindowConfigureBorders(wwin);
741 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
743 wwin->bfs_geometry.x = wwin->frame_x;
744 wwin->bfs_geometry.y = wwin->frame_y;
745 wwin->bfs_geometry.width = wwin->frame->core->width;
746 wwin->bfs_geometry.height = wwin->frame->core->height;
748 head = wGetHeadForWindow(wwin);
749 rect = wGetRectForHead(wwin->screen_ptr, head);
750 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
752 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
755 void wUnfullscreenWindow(WWindow *wwin)
757 if (!wwin->flags.fullscreen)
758 return;
760 wwin->flags.fullscreen = False;
762 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
763 if (WFLAGP(wwin, sunken)) {
764 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
765 } else if (WFLAGP(wwin, floating)) {
766 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
767 } else {
768 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
772 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
773 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
775 wWindowConfigureBorders(wwin);
777 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
778 wFrameWindowPaint(wwin->frame);
781 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
784 #ifdef ANIMATIONS
785 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
787 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
788 float cx, cy, cw, ch;
789 float xstep, ystep, wstep, hstep;
790 XPoint points[5];
791 float dx, dch, midy;
792 float angle, final_angle, delta;
794 xstep = (float)(fx - x) / steps;
795 ystep = (float)(fy - y) / steps;
796 wstep = (float)(fw - w) / steps;
797 hstep = (float)(fh - h) / steps;
799 cx = (float)x;
800 cy = (float)y;
801 cw = (float)w;
802 ch = (float)h;
804 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
805 delta = (float)(final_angle / FRAMES);
806 for (angle = 0;; angle += delta) {
807 if (angle > final_angle)
808 angle = final_angle;
810 dx = (cw / 10) - ((cw / 5) * sin(angle));
811 dch = (ch / 2) * cos(angle);
812 midy = cy + (ch / 2);
814 points[0].x = cx + dx;
815 points[0].y = midy - dch;
816 points[1].x = cx + cw - dx;
817 points[1].y = points[0].y;
818 points[2].x = cx + cw + dx;
819 points[2].y = midy + dch;
820 points[3].x = cx - dx;
821 points[3].y = points[2].y;
822 points[4].x = points[0].x;
823 points[4].y = points[0].y;
825 XGrabServer(dpy);
826 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
827 XFlush(dpy);
828 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
830 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
831 XUngrabServer(dpy);
832 cx += xstep;
833 cy += ystep;
834 cw += wstep;
835 ch += hstep;
836 if (angle >= final_angle)
837 break;
840 XFlush(dpy);
843 #undef FRAMES
845 static void
846 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
848 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
849 float cx, cy, cw, ch;
850 float xstep, ystep, wstep, hstep;
851 XPoint points[5];
852 float angle, final_angle, a, d, delta;
854 x += w / 2;
855 y += h / 2;
856 fx += fw / 2;
857 fy += fh / 2;
859 xstep = (float)(fx - x) / steps;
860 ystep = (float)(fy - y) / steps;
861 wstep = (float)(fw - w) / steps;
862 hstep = (float)(fh - h) / steps;
864 cx = (float)x;
865 cy = (float)y;
866 cw = (float)w;
867 ch = (float)h;
869 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
870 delta = (float)(final_angle / FRAMES);
871 for (angle = 0;; angle += delta) {
872 if (angle > final_angle)
873 angle = final_angle;
875 a = atan(ch / cw);
876 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
878 points[0].x = cx + cos(angle - a) * d;
879 points[0].y = cy + sin(angle - a) * d;
880 points[1].x = cx + cos(angle + a) * d;
881 points[1].y = cy + sin(angle + a) * d;
882 points[2].x = cx + cos(angle - a + WM_PI) * d;
883 points[2].y = cy + sin(angle - a + WM_PI) * d;
884 points[3].x = cx + cos(angle + a + WM_PI) * d;
885 points[3].y = cy + sin(angle + a + WM_PI) * d;
886 points[4].x = cx + cos(angle - a) * d;
887 points[4].y = cy + sin(angle - a) * d;
888 XGrabServer(dpy);
889 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
890 XFlush(dpy);
891 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
893 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
894 XUngrabServer(dpy);
895 cx += xstep;
896 cy += ystep;
897 cw += wstep;
898 ch += hstep;
899 if (angle >= final_angle)
900 break;
903 XFlush(dpy);
906 #undef FRAMES
908 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
910 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
911 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
912 float xstep, ystep, wstep, hstep;
913 int i, j;
915 xstep = (float)(fx - x) / steps;
916 ystep = (float)(fy - y) / steps;
917 wstep = (float)(fw - w) / steps;
918 hstep = (float)(fh - h) / steps;
920 for (j = 0; j < FRAMES; j++) {
921 cx[j] = (float)x;
922 cy[j] = (float)y;
923 cw[j] = (float)w;
924 ch[j] = (float)h;
926 XGrabServer(dpy);
927 for (i = 0; i < steps; i++) {
928 for (j = 0; j < FRAMES; j++) {
929 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
930 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
932 XFlush(dpy);
933 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
935 for (j = 0; j < FRAMES; j++) {
936 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
937 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
938 if (j < FRAMES - 1) {
939 cx[j] = cx[j + 1];
940 cy[j] = cy[j + 1];
941 cw[j] = cw[j + 1];
942 ch[j] = ch[j + 1];
943 } else {
944 cx[j] += xstep;
945 cy[j] += ystep;
946 cw[j] += wstep;
947 ch[j] += hstep;
952 for (j = 0; j < FRAMES; j++) {
953 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
955 XFlush(dpy);
956 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
958 for (j = 0; j < FRAMES; j++) {
959 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
962 XUngrabServer(dpy);
965 #undef FRAMES
967 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
969 int style = wPreferences.iconification_style; /* Catch the value */
970 int steps;
972 if (style == WIS_NONE)
973 return;
975 if (style == WIS_RANDOM) {
976 style = rand() % 3;
979 switch (style) {
980 case WIS_TWIST:
981 steps = MINIATURIZE_ANIMATION_STEPS_T;
982 if (steps > 0)
983 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
984 break;
985 case WIS_FLIP:
986 steps = MINIATURIZE_ANIMATION_STEPS_F;
987 if (steps > 0)
988 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
989 break;
990 case WIS_ZOOM:
991 default:
992 steps = MINIATURIZE_ANIMATION_STEPS_Z;
993 if (steps > 0)
994 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
995 break;
998 #endif /* ANIMATIONS */
1000 static void flushExpose(void)
1002 XEvent tmpev;
1004 while (XCheckTypedEvent(dpy, Expose, &tmpev))
1005 WMHandleEvent(&tmpev);
1006 XSync(dpy, 0);
1009 static void unmapTransientsFor(WWindow *wwin)
1011 WWindow *tmp;
1013 tmp = wwin->screen_ptr->focused_window;
1014 while (tmp) {
1015 /* unmap the transients for this transient */
1016 if (tmp != wwin && tmp->transient_for == wwin->client_win
1017 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1018 unmapTransientsFor(tmp);
1019 tmp->flags.miniaturized = 1;
1020 if (!tmp->flags.shaded) {
1021 wWindowUnmap(tmp);
1022 } else {
1023 XUnmapWindow(dpy, tmp->frame->core->window);
1026 if (!tmp->flags.shaded)
1028 wClientSetState(tmp, IconicState, None);
1030 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1032 tmp = tmp->prev;
1036 static void mapTransientsFor(WWindow *wwin)
1038 WWindow *tmp;
1040 tmp = wwin->screen_ptr->focused_window;
1041 while (tmp) {
1042 /* recursively map the transients for this transient */
1043 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1044 && tmp->icon == NULL) {
1045 mapTransientsFor(tmp);
1046 tmp->flags.miniaturized = 0;
1047 if (!tmp->flags.shaded) {
1048 wWindowMap(tmp);
1049 } else {
1050 XMapWindow(dpy, tmp->frame->core->window);
1052 tmp->flags.semi_focused = 0;
1054 if (!tmp->flags.shaded)
1056 wClientSetState(tmp, NormalState, None);
1058 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1060 tmp = tmp->prev;
1064 static WWindow *recursiveTransientFor(WWindow * wwin)
1066 int i;
1068 if (!wwin)
1069 return None;
1071 /* hackish way to detect transient_for cycle */
1072 i = wwin->screen_ptr->window_count + 1;
1074 while (wwin && wwin->transient_for != None && i > 0) {
1075 wwin = wWindowFor(wwin->transient_for);
1076 i--;
1078 if (i == 0 && wwin) {
1079 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1080 return NULL;
1083 return wwin;
1086 void wIconifyWindow(WWindow * wwin)
1088 XWindowAttributes attribs;
1089 int present;
1091 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1092 return; /* the window doesn't exist anymore */
1094 if (wwin->flags.miniaturized)
1095 return; /* already miniaturized */
1097 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1098 WWindow *owner = wWindowFor(wwin->transient_for);
1100 if (owner && owner->flags.miniaturized)
1101 return;
1104 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1106 /* if the window is in another workspace, simplify process */
1107 if (present) {
1108 /* icon creation may take a while */
1109 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1110 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1111 GrabModeAsync, None, None, CurrentTime);
1114 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1115 if (!wwin->flags.icon_moved)
1116 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1118 wwin->icon = icon_create_for_wwindow(wwin);
1119 wwin->icon->mapped = 1;
1122 wwin->flags.miniaturized = 1;
1123 wwin->flags.mapped = 0;
1125 /* unmap transients */
1126 unmapTransientsFor(wwin);
1128 if (present) {
1129 XUngrabPointer(dpy, CurrentTime);
1130 wWindowUnmap(wwin);
1131 /* let all Expose events arrive so that we can repaint
1132 * something before the animation starts (and the server is grabbed) */
1133 XSync(dpy, 0);
1135 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1136 wClientSetState(wwin, IconicState, None);
1137 else
1138 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1140 flushExpose();
1141 #ifdef ANIMATIONS
1142 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1143 && !wPreferences.no_animations) {
1144 int ix, iy, iw, ih;
1146 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1147 ix = wwin->icon_x;
1148 iy = wwin->icon_y;
1149 iw = wwin->icon->core->width;
1150 ih = wwin->icon->core->height;
1151 } else {
1152 if (wwin->flags.net_handle_icon) {
1153 ix = wwin->icon_x;
1154 iy = wwin->icon_y;
1155 iw = wwin->icon_w;
1156 ih = wwin->icon_h;
1157 } else {
1158 ix = 0;
1159 iy = 0;
1160 iw = wwin->screen_ptr->scr_width;
1161 ih = wwin->screen_ptr->scr_height;
1164 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1165 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1167 #endif
1170 wwin->flags.skip_next_animation = 0;
1172 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1173 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1174 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1175 XMapWindow(dpy, wwin->icon->core->window);
1177 AddToStackList(wwin->icon->core);
1178 wLowerFrame(wwin->icon->core);
1181 if (present) {
1182 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1185 * It doesn't seem to be working and causes button event hangup
1186 * when deiconifying a transient window.
1187 setupIconGrabs(wwin->icon);
1189 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1190 && wPreferences.focus_mode == WKF_CLICK) {
1191 WWindow *tmp;
1193 tmp = wwin->prev;
1194 while (tmp) {
1195 if (!WFLAGP(tmp, no_focusable)
1196 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1197 && (wwin->frame->workspace == tmp->frame->workspace))
1198 break;
1199 tmp = tmp->prev;
1201 wSetFocusTo(wwin->screen_ptr, tmp);
1202 } else if (wPreferences.focus_mode != WKF_CLICK) {
1203 wSetFocusTo(wwin->screen_ptr, NULL);
1205 #ifdef ANIMATIONS
1206 if (!wwin->screen_ptr->flags.startup) {
1207 /* Catch up with events not processed while animation was running */
1208 Window clientwin = wwin->client_win;
1210 ProcessPendingEvents();
1212 /* the window can disappear while ProcessPendingEvents() runs */
1213 if (!wWindowFor(clientwin)) {
1214 return;
1217 #endif
1220 /* maybe we want to do this regardless of net_handle_icon
1221 * it seems to me we might break behaviour this way.
1223 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1224 && !wwin->flags.net_handle_icon)
1225 wIconSelect(wwin->icon);
1227 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1229 if (wPreferences.auto_arrange_icons)
1230 wArrangeIcons(wwin->screen_ptr, True);
1233 void wDeiconifyWindow(WWindow *wwin)
1235 /* Let's avoid changing workspace while deiconifying */
1236 ignore_wks_change = 1;
1238 /* we're hiding for show_desktop */
1239 int netwm_hidden = wwin->flags.net_show_desktop &&
1240 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1242 if (!netwm_hidden)
1243 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1245 if (!wwin->flags.miniaturized)
1246 return;
1248 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1249 WWindow *owner = recursiveTransientFor(wwin);
1251 if (owner && owner->flags.miniaturized) {
1252 wDeiconifyWindow(owner);
1253 wSetFocusTo(wwin->screen_ptr, wwin);
1254 wRaiseFrame(wwin->frame->core);
1255 return;
1259 wwin->flags.miniaturized = 0;
1261 if (!netwm_hidden && !wwin->flags.shaded)
1262 wwin->flags.mapped = 1;
1264 if (!netwm_hidden || wPreferences.sticky_icons) {
1265 /* maybe we want to do this regardless of net_handle_icon
1266 * it seems to me we might break behaviour this way.
1268 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1269 && wwin->icon != NULL) {
1270 if (wwin->icon->selected)
1271 wIconSelect(wwin->icon);
1273 XUnmapWindow(dpy, wwin->icon->core->window);
1277 /* if the window is in another workspace, do it silently */
1278 if (!netwm_hidden) {
1279 #ifdef ANIMATIONS
1280 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1281 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1282 int ix, iy, iw, ih;
1284 if (!wPreferences.disable_miniwindows
1285 && !wwin->flags.net_handle_icon) {
1286 ix = wwin->icon_x;
1287 iy = wwin->icon_y;
1288 iw = wwin->icon->core->width;
1289 ih = wwin->icon->core->height;
1290 } else {
1291 if (wwin->flags.net_handle_icon) {
1292 ix = wwin->icon_x;
1293 iy = wwin->icon_y;
1294 iw = wwin->icon_w;
1295 ih = wwin->icon_h;
1296 } else {
1297 ix = 0;
1298 iy = 0;
1299 iw = wwin->screen_ptr->scr_width;
1300 ih = wwin->screen_ptr->scr_height;
1303 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1304 wwin->frame_x, wwin->frame_y,
1305 wwin->frame->core->width, wwin->frame->core->height);
1307 #endif /* ANIMATIONS */
1308 wwin->flags.skip_next_animation = 0;
1309 XGrabServer(dpy);
1310 if (!wwin->flags.shaded)
1311 XMapWindow(dpy, wwin->client_win);
1313 XMapWindow(dpy, wwin->frame->core->window);
1314 wRaiseFrame(wwin->frame->core);
1315 if (!wwin->flags.shaded)
1316 wClientSetState(wwin, NormalState, None);
1318 mapTransientsFor(wwin);
1321 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1322 && !wwin->flags.net_handle_icon) {
1323 RemoveFromStackList(wwin->icon->core);
1324 /* removeIconGrabs(wwin->icon); */
1325 wIconDestroy(wwin->icon);
1326 wwin->icon = NULL;
1329 if (!netwm_hidden) {
1330 XUngrabServer(dpy);
1332 wSetFocusTo(wwin->screen_ptr, wwin);
1334 #ifdef ANIMATIONS
1335 if (!wwin->screen_ptr->flags.startup) {
1336 /* Catch up with events not processed while animation was running */
1337 Window clientwin = wwin->client_win;
1339 ProcessPendingEvents();
1341 /* the window can disappear while ProcessPendingEvents() runs */
1342 if (!wWindowFor(clientwin))
1343 return;
1345 #endif
1348 if (wPreferences.auto_arrange_icons)
1349 wArrangeIcons(wwin->screen_ptr, True);
1351 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1353 /* In case we were shaded and iconified, also unshade */
1354 if (!netwm_hidden)
1355 wUnshadeWindow(wwin);
1357 ignore_wks_change = 0;
1360 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1362 if (wwin->flags.miniaturized) {
1363 if (wwin->icon) {
1364 XUnmapWindow(dpy, wwin->icon->core->window);
1365 wwin->icon->mapped = 0;
1367 wwin->flags.hidden = 1;
1369 WMPostNotificationName(WMNChangedState, wwin, "hide");
1370 return;
1373 if (wwin->flags.inspector_open) {
1374 wHideInspectorForWindow(wwin);
1377 wwin->flags.hidden = 1;
1378 wWindowUnmap(wwin);
1380 wClientSetState(wwin, IconicState, icon->icon_win);
1381 flushExpose();
1383 #ifdef ANIMATIONS
1384 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1385 !wwin->flags.skip_next_animation && animate) {
1386 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1387 wwin->frame->core->width, wwin->frame->core->height,
1388 icon_x, icon_y, icon->core->width, icon->core->height);
1390 #endif
1391 wwin->flags.skip_next_animation = 0;
1393 WMPostNotificationName(WMNChangedState, wwin, "hide");
1396 void wHideAll(WScreen *scr)
1398 WWindow *wwin;
1399 WWindow **windows;
1400 WMenu *menu;
1401 unsigned int wcount = 0;
1402 int i;
1404 if (!scr)
1405 return;
1407 menu = scr->switch_menu;
1409 windows = wmalloc(sizeof(WWindow *));
1411 if (menu != NULL) {
1412 for (i = 0; i < menu->entry_no; i++) {
1413 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1414 wcount++;
1415 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1417 } else {
1418 wwin = scr->focused_window;
1420 while (wwin) {
1421 windows[wcount] = wwin;
1422 wcount++;
1423 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1424 wwin = wwin->prev;
1429 for (i = 0; i < wcount; i++) {
1430 wwin = windows[i];
1431 if (wwin->frame->workspace == scr->current_workspace
1432 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1433 && !wwin->flags.internal_window
1434 && !WFLAGP(wwin, no_miniaturizable)
1436 wwin->flags.skip_next_animation = 1;
1437 wIconifyWindow(wwin);
1441 wfree(windows);
1444 void wHideOtherApplications(WWindow *awin)
1446 WWindow *wwin;
1447 WApplication *tapp;
1449 if (!awin)
1450 return;
1451 wwin = awin->screen_ptr->focused_window;
1453 while (wwin) {
1454 if (wwin != awin
1455 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1456 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1457 && !wwin->flags.internal_window
1458 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1460 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1461 if (!WFLAGP(wwin, no_miniaturizable)) {
1462 wwin->flags.skip_next_animation = 1;
1463 wIconifyWindow(wwin);
1465 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1466 tapp = wApplicationOf(wwin->main_window);
1467 if (tapp) {
1468 tapp->flags.skip_next_animation = 1;
1469 wHideApplication(tapp);
1470 } else {
1471 if (!WFLAGP(wwin, no_miniaturizable)) {
1472 wwin->flags.skip_next_animation = 1;
1473 wIconifyWindow(wwin);
1478 wwin = wwin->prev;
1481 wSetFocusTo(awin->screen_ptr, awin);
1485 void wHideApplication(WApplication *wapp)
1487 WScreen *scr;
1488 WWindow *wlist;
1489 int hadfocus;
1490 int animate;
1492 if (!wapp) {
1493 wwarning("trying to hide a non grouped window");
1494 return;
1496 if (!wapp->main_window_desc) {
1497 wwarning("group leader not found for window group");
1498 return;
1500 scr = wapp->main_window_desc->screen_ptr;
1501 hadfocus = 0;
1502 wlist = scr->focused_window;
1503 if (!wlist)
1504 return;
1506 if (wlist->main_window == wapp->main_window)
1507 wapp->last_focused = wlist;
1508 else
1509 wapp->last_focused = NULL;
1511 animate = !wapp->flags.skip_next_animation;
1513 while (wlist) {
1514 if (wlist->main_window == wapp->main_window) {
1515 if (wlist->flags.focused) {
1516 hadfocus = 1;
1518 if (wapp->app_icon) {
1519 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1520 wapp->app_icon->y_pos, wlist, animate);
1521 animate = False;
1524 wlist = wlist->prev;
1527 wapp->flags.skip_next_animation = 0;
1529 if (hadfocus) {
1530 if (wPreferences.focus_mode == WKF_CLICK) {
1531 wlist = scr->focused_window;
1532 while (wlist) {
1533 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1534 && (wlist->flags.mapped || wlist->flags.shaded))
1535 break;
1536 wlist = wlist->prev;
1538 wSetFocusTo(scr, wlist);
1539 } else {
1540 wSetFocusTo(scr, NULL);
1544 wapp->flags.hidden = 1;
1546 if (wPreferences.auto_arrange_icons)
1547 wArrangeIcons(scr, True);
1549 #ifdef HIDDENDOT
1550 if (wapp->app_icon)
1551 wAppIconPaint(wapp->app_icon);
1552 #endif
1555 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1557 if (bringToCurrentWS)
1558 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1560 wwin->flags.hidden = 0;
1562 #ifdef ANIMATIONS
1563 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1564 animateResize(wwin->screen_ptr, icon_x, icon_y,
1565 icon->core->width, icon->core->height,
1566 wwin->frame_x, wwin->frame_y,
1567 wwin->frame->core->width, wwin->frame->core->height);
1569 #endif
1570 wwin->flags.skip_next_animation = 0;
1571 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1572 XMapWindow(dpy, wwin->client_win);
1573 XMapWindow(dpy, wwin->frame->core->window);
1574 wClientSetState(wwin, NormalState, None);
1575 wwin->flags.mapped = 1;
1576 wRaiseFrame(wwin->frame->core);
1578 if (wwin->flags.inspector_open) {
1579 wUnhideInspectorForWindow(wwin);
1582 WMPostNotificationName(WMNChangedState, wwin, "hide");
1585 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1587 WScreen *scr;
1588 WWindow *wlist, *next;
1589 WWindow *focused = NULL;
1590 int animate;
1592 if (!wapp)
1593 return;
1595 scr = wapp->main_window_desc->screen_ptr;
1596 wlist = scr->focused_window;
1597 if (!wlist)
1598 return;
1600 /* goto beginning of list */
1601 while (wlist->prev)
1602 wlist = wlist->prev;
1604 animate = !wapp->flags.skip_next_animation;
1606 while (wlist) {
1607 next = wlist->next;
1609 if (wlist->main_window == wapp->main_window) {
1610 if (wlist->flags.focused)
1611 focused = wlist;
1612 else if (!focused || !focused->flags.focused)
1613 focused = wlist;
1615 if (wlist->flags.miniaturized) {
1616 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1617 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1618 if (!wlist->icon->mapped) {
1619 int x, y;
1621 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1622 if (wlist->icon_x != x || wlist->icon_y != y) {
1623 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1625 wlist->icon_x = x;
1626 wlist->icon_y = y;
1627 XMapWindow(dpy, wlist->icon->core->window);
1628 wlist->icon->mapped = 1;
1630 wRaiseFrame(wlist->icon->core);
1632 if (bringToCurrentWS)
1633 wWindowChangeWorkspace(wlist, scr->current_workspace);
1634 wlist->flags.hidden = 0;
1635 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1636 wDeiconifyWindow(wlist);
1638 WMPostNotificationName(WMNChangedState, wlist, "hide");
1639 } else if (wlist->flags.shaded) {
1640 if (bringToCurrentWS)
1641 wWindowChangeWorkspace(wlist, scr->current_workspace);
1642 wlist->flags.hidden = 0;
1643 wRaiseFrame(wlist->frame->core);
1644 if (wlist->frame->workspace == scr->current_workspace) {
1645 XMapWindow(dpy, wlist->frame->core->window);
1646 if (miniwindows) {
1647 wUnshadeWindow(wlist);
1650 WMPostNotificationName(WMNChangedState, wlist, "hide");
1651 } else if (wlist->flags.hidden) {
1652 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1653 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1654 animate = False;
1655 } else {
1656 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1657 wWindowChangeWorkspace(wlist, scr->current_workspace);
1659 wRaiseFrame(wlist->frame->core);
1662 wlist = next;
1665 wapp->flags.skip_next_animation = 0;
1666 wapp->flags.hidden = 0;
1668 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1669 wRaiseFrame(wapp->last_focused->frame->core);
1670 wSetFocusTo(scr, wapp->last_focused);
1671 } else if (focused) {
1672 wSetFocusTo(scr, focused);
1674 wapp->last_focused = NULL;
1675 if (wPreferences.auto_arrange_icons)
1676 wArrangeIcons(scr, True);
1678 #ifdef HIDDENDOT
1679 wAppIconPaint(wapp->app_icon);
1680 #endif
1683 void wShowAllWindows(WScreen *scr)
1685 WWindow *wwin, *old_foc;
1686 WApplication *wapp;
1688 old_foc = wwin = scr->focused_window;
1689 while (wwin) {
1690 if (!wwin->flags.internal_window &&
1691 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1692 if (wwin->flags.miniaturized) {
1693 wwin->flags.skip_next_animation = 1;
1694 wDeiconifyWindow(wwin);
1695 } else if (wwin->flags.hidden) {
1696 wapp = wApplicationOf(wwin->main_window);
1697 if (wapp) {
1698 wUnhideApplication(wapp, False, False);
1699 } else {
1700 wwin->flags.skip_next_animation = 1;
1701 wDeiconifyWindow(wwin);
1705 wwin = wwin->prev;
1707 wSetFocusTo(scr, old_foc);
1708 /*wRaiseFrame(old_foc->frame->core); */
1711 void wRefreshDesktop(WScreen *scr)
1713 Window win;
1714 XSetWindowAttributes attr;
1716 attr.backing_store = NotUseful;
1717 attr.save_under = False;
1718 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1719 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1720 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1721 XMapRaised(dpy, win);
1722 XDestroyWindow(dpy, win);
1723 XFlush(dpy);
1726 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1728 WWindow *wwin;
1729 WAppIcon *aicon;
1731 int head;
1732 const int heads = wXineramaHeads(scr);
1734 struct HeadVars {
1735 int pf; /* primary axis */
1736 int sf; /* secondary axis */
1737 int fullW;
1738 int fullH;
1739 int pi, si;
1740 int sx1, sx2, sy1, sy2; /* screen boundary */
1741 int sw, sh;
1742 int xo, yo;
1743 int xs, ys;
1744 } *vars;
1746 int isize = wPreferences.icon_size;
1748 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1750 for (head = 0; head < heads; ++head) {
1751 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1752 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1753 vars[head].pi = vars[head].si = 0;
1754 vars[head].sx1 = rect.pos.x;
1755 vars[head].sy1 = rect.pos.y;
1756 vars[head].sw = rect.size.width;
1757 vars[head].sh = rect.size.height;
1758 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1759 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1760 vars[head].sw = isize * (vars[head].sw / isize);
1761 vars[head].sh = isize * (vars[head].sh / isize);
1762 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1763 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1765 /* icon yard boundaries */
1766 if (wPreferences.icon_yard & IY_VERT) {
1767 vars[head].pf = vars[head].fullH;
1768 vars[head].sf = vars[head].fullW;
1769 } else {
1770 vars[head].pf = vars[head].fullW;
1771 vars[head].sf = vars[head].fullH;
1773 if (wPreferences.icon_yard & IY_RIGHT) {
1774 vars[head].xo = vars[head].sx2 - isize;
1775 vars[head].xs = -1;
1776 } else {
1777 vars[head].xo = vars[head].sx1;
1778 vars[head].xs = 1;
1780 if (wPreferences.icon_yard & IY_TOP) {
1781 vars[head].yo = vars[head].sy1;
1782 vars[head].ys = 1;
1783 } else {
1784 vars[head].yo = vars[head].sy2 - isize;
1785 vars[head].ys = -1;
1789 #define X ((wPreferences.icon_yard & IY_VERT) \
1790 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1791 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1793 #define Y ((wPreferences.icon_yard & IY_VERT) \
1794 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1795 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1797 /* arrange application icons */
1798 aicon = scr->app_icon_list;
1799 /* reverse them to avoid unnecessarily sliding of icons */
1800 while (aicon && aicon->next)
1801 aicon = aicon->next;
1803 while (aicon) {
1804 if (!aicon->docked) {
1805 /* CHECK: can icon be NULL here ? */
1806 /* The intention here is to place the AppIcon on the head that
1807 * contains most of the applications _main_ window. */
1808 head = wGetHeadForWindow(aicon->icon->owner);
1810 if (aicon->x_pos != X || aicon->y_pos != Y) {
1811 #ifdef ANIMATIONS
1812 if (!wPreferences.no_animations)
1813 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1814 #endif /* ANIMATIONS */
1816 wAppIconMove(aicon, X, Y);
1817 vars[head].pi++;
1818 if (vars[head].pi >= vars[head].pf) {
1819 vars[head].pi = 0;
1820 vars[head].si++;
1823 aicon = aicon->prev;
1826 /* arrange miniwindows */
1827 wwin = scr->focused_window;
1828 /* reverse them to avoid unnecessarily shuffling */
1829 while (wwin && wwin->prev)
1830 wwin = wwin->prev;
1832 while (wwin) {
1833 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1834 (wwin->frame->workspace == scr->current_workspace ||
1835 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1837 head = wGetHeadForWindow(wwin);
1839 if (arrangeAll || !wwin->flags.icon_moved) {
1840 if (wwin->icon_x != X || wwin->icon_y != Y)
1841 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1843 wwin->icon_x = X;
1844 wwin->icon_y = Y;
1846 vars[head].pi++;
1847 if (vars[head].pi >= vars[head].pf) {
1848 vars[head].pi = 0;
1849 vars[head].si++;
1853 if (arrangeAll) {
1854 wwin->flags.icon_moved = 0;
1856 /* we reversed the order, so we use next */
1857 wwin = wwin->next;
1860 wfree(vars);
1863 void wSelectWindow(WWindow *wwin, Bool flag)
1865 WScreen *scr = wwin->screen_ptr;
1867 if (flag) {
1868 wwin->flags.selected = 1;
1869 if (wwin->frame->selected_border_pixel)
1870 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1871 else
1872 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1874 if (!HAS_BORDER(wwin)) {
1875 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1878 if (!scr->selected_windows)
1879 scr->selected_windows = WMCreateArray(4);
1880 WMAddToArray(scr->selected_windows, wwin);
1881 } else {
1882 wwin->flags.selected = 0;
1883 if (wwin->frame->border_pixel)
1884 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1885 else
1886 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1888 if (!HAS_BORDER(wwin)) {
1889 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1892 if (scr->selected_windows) {
1893 WMRemoveFromArray(scr->selected_windows, wwin);
1898 void wMakeWindowVisible(WWindow *wwin)
1900 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1901 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1903 if (wwin->flags.shaded) {
1904 wUnshadeWindow(wwin);
1906 if (wwin->flags.hidden) {
1907 WApplication *app;
1909 app = wApplicationOf(wwin->main_window);
1910 if (app) {
1911 /* trick to get focus to this window */
1912 app->last_focused = wwin;
1913 wUnhideApplication(app, False, False);
1916 if (wwin->flags.miniaturized) {
1917 wDeiconifyWindow(wwin);
1918 } else {
1919 if (!WFLAGP(wwin, no_focusable))
1920 wSetFocusTo(wwin->screen_ptr, wwin);
1921 wRaiseFrame(wwin->frame->core);
1926 * Do the animation while shading (called with what = SHADE)
1927 * or unshading (what = UNSHADE).
1929 #ifdef ANIMATIONS
1930 static void shade_animate(WWindow *wwin, Bool what)
1932 int y, s, w, h;
1933 time_t time0 = time(NULL);
1935 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1936 return;
1938 switch(what) {
1939 case SHADE:
1940 if (!wwin->screen_ptr->flags.startup) {
1941 /* do the shading animation */
1942 h = wwin->frame->core->height;
1943 s = h / SHADE_STEPS;
1944 if (s < 1)
1945 s = 1;
1946 w = wwin->frame->core->width;
1947 y = wwin->frame->top_width;
1948 while (h > wwin->frame->top_width + 1) {
1949 XMoveWindow(dpy, wwin->client_win, 0, y);
1950 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1951 XFlush(dpy);
1953 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1954 break;
1956 if (SHADE_DELAY > 0) {
1957 wusleep(SHADE_DELAY * 1000L);
1958 } else {
1959 wusleep(10);
1961 h -= s;
1962 y -= s;
1964 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1966 break;
1968 case UNSHADE:
1969 h = wwin->frame->top_width + wwin->frame->bottom_width;
1970 y = wwin->frame->top_width - wwin->client.height;
1971 s = abs(y) / SHADE_STEPS;
1972 if (s < 1)
1973 s = 1;
1974 w = wwin->frame->core->width;
1975 XMoveWindow(dpy, wwin->client_win, 0, y);
1976 if (s > 0) {
1977 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1978 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1979 XMoveWindow(dpy, wwin->client_win, 0, y);
1980 XFlush(dpy);
1981 if (SHADE_DELAY > 0) {
1982 wusleep(SHADE_DELAY * 2000L / 3);
1983 } else {
1984 wusleep(10);
1986 h += s;
1987 y += s;
1989 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1990 break;
1993 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1994 break;
1997 #endif