wmaker: removed unused macro DBLCLICK_TIME
[wmaker-crm.git] / src / actions.c
blob03807fc723e8a309996365a94b14c8d2f4bd359f
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
7 * Copyright (c) 2014 Window Maker Team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
34 #include "WindowMaker.h"
35 #include "framewin.h"
36 #include "window.h"
37 #include "client.h"
38 #include "icon.h"
39 #include "colormap.h"
40 #include "application.h"
41 #include "actions.h"
42 #include "stacking.h"
43 #include "appicon.h"
44 #include "dock.h"
45 #include "appmenu.h"
46 #include "winspector.h"
47 #include "workspace.h"
48 #include "xinerama.h"
49 #include "usermenu.h"
50 #include "placement.h"
51 #include "misc.h"
52 #include "event.h"
55 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
56 unsigned int *new_width, unsigned int *new_height);
57 static void save_old_geometry(WWindow *wwin, int directions);
59 /******* Local Variables *******/
60 static struct {
61 int steps;
62 int delay;
63 } shadePars[5] = {
64 { SHADE_STEPS_UF, SHADE_DELAY_UF },
65 { SHADE_STEPS_F, SHADE_DELAY_F },
66 { SHADE_STEPS_M, SHADE_DELAY_M },
67 { SHADE_STEPS_S, SHADE_DELAY_S },
68 { SHADE_STEPS_US, SHADE_DELAY_US }
71 #define UNSHADE 0
72 #define SHADE 1
73 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
74 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
76 static int compareTimes(Time t1, Time t2)
78 Time diff;
79 if (t1 == t2)
80 return 0;
81 diff = t1 - t2;
82 return (diff < 60000) ? 1 : -1;
85 #ifdef ANIMATIONS
86 static void shade_animate(WWindow *wwin, Bool what);
87 #else
88 static inline void shade_animate(WWindow *wwin, Bool what)
91 * This function is empty on purpose, so tell the compiler
92 * to not warn about parameters being not used
94 (void) wwin;
95 (void) what;
97 #endif
100 *----------------------------------------------------------------------
101 * wSetFocusTo--
102 * Changes the window focus to the one passed as argument.
103 * If the window to focus is not already focused, it will be brought
104 * to the head of the list of windows. Previously focused window is
105 * unfocused.
107 * Side effects:
108 * Window list may be reordered and the window focus is changed.
110 *----------------------------------------------------------------------
112 void wSetFocusTo(WScreen *scr, WWindow *wwin)
114 static WScreen *old_scr = NULL;
116 WWindow *old_focused;
117 WWindow *focused = scr->focused_window;
118 Time timestamp = w_global.timestamp.last_event;
119 WApplication *oapp = NULL, *napp = NULL;
120 int wasfocused;
122 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
123 return;
125 if (!old_scr)
126 old_scr = scr;
128 old_focused = old_scr->focused_window;
130 w_global.timestamp.focus_change = timestamp;
132 if (old_focused)
133 oapp = wApplicationOf(old_focused->main_window);
135 if (wwin == NULL) {
136 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
137 if (old_focused)
138 wWindowUnfocus(old_focused);
140 if (oapp) {
141 wAppMenuUnmap(oapp->menu);
142 if (wPreferences.highlight_active_app)
143 wApplicationDeactivate(oapp);
146 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
147 return;
150 if (old_scr != scr && old_focused)
151 wWindowUnfocus(old_focused);
153 wasfocused = wwin->flags.focused;
154 napp = wApplicationOf(wwin->main_window);
156 /* remember last workspace where the app has been */
157 if (napp)
158 napp->last_workspace = wwin->frame->workspace;
160 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
161 /* install colormap if colormap mode is lock mode */
162 if (wPreferences.colormap_mode == WCM_CLICK)
163 wColormapInstallForWindow(scr, wwin);
165 /* set input focus */
166 switch (wwin->focus_mode) {
167 case WFM_NO_INPUT:
168 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
169 break;
170 case WFM_PASSIVE:
171 case WFM_LOCALLY_ACTIVE:
172 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
173 break;
174 case WFM_GLOBALLY_ACTIVE:
175 break;
178 XFlush(dpy);
179 if (wwin->protocols.TAKE_FOCUS)
180 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
182 XSync(dpy, False);
183 } else {
184 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
187 if (WFLAGP(wwin, no_focusable))
188 return;
190 /* if this is not the focused window focus it */
191 if (focused != wwin) {
192 /* change the focus window list order */
193 if (wwin->prev)
194 wwin->prev->next = wwin->next;
196 if (wwin->next)
197 wwin->next->prev = wwin->prev;
199 wwin->prev = focused;
200 focused->next = wwin;
201 wwin->next = NULL;
202 scr->focused_window = wwin;
204 if (oapp && oapp != napp) {
205 wAppMenuUnmap(oapp->menu);
206 if (wPreferences.highlight_active_app)
207 wApplicationDeactivate(oapp);
211 wWindowFocus(wwin, focused);
213 if (napp && !wasfocused) {
214 #ifdef USER_MENU
215 wUserMenuRefreshInstances(napp->menu, wwin);
216 #endif /* USER_MENU */
218 if (wwin->flags.mapped)
219 wAppMenuMap(napp->menu, wwin);
221 if (napp && wPreferences.highlight_active_app)
222 wApplicationActivate(napp);
224 XFlush(dpy);
225 old_scr = scr;
228 void wShadeWindow(WWindow *wwin)
231 if (wwin->flags.shaded)
232 return;
234 XLowerWindow(dpy, wwin->client_win);
235 shade_animate(wwin, SHADE);
237 wwin->flags.skip_next_animation = 0;
238 wwin->flags.shaded = 1;
239 wwin->flags.mapped = 0;
240 /* prevent window withdrawal when getting UnmapNotify */
241 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
242 XUnmapWindow(dpy, wwin->client_win);
243 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
245 /* for the client it's just like iconification */
246 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
248 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
249 wWindowSynthConfigureNotify(wwin);
252 wClientSetState(wwin, IconicState, None);
255 WMPostNotificationName(WMNChangedState, wwin, "shade");
257 #ifdef ANIMATIONS
258 if (!wwin->screen_ptr->flags.startup) {
259 /* Catch up with events not processed while animation was running */
260 ProcessPendingEvents();
262 #endif
265 void wUnshadeWindow(WWindow *wwin)
268 if (!wwin->flags.shaded)
269 return;
271 wwin->flags.shaded = 0;
272 wwin->flags.mapped = 1;
273 XMapWindow(dpy, wwin->client_win);
275 shade_animate(wwin, UNSHADE);
277 wwin->flags.skip_next_animation = 0;
278 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
279 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
281 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
282 wWindowSynthConfigureNotify(wwin);
284 /* if the window is focused, set the focus again as it was disabled during
285 * shading */
286 if (wwin->flags.focused)
287 wSetFocusTo(wwin->screen_ptr, wwin);
289 WMPostNotificationName(WMNChangedState, wwin, "shade");
292 /* Set the old coordinates using the current values */
293 static void save_old_geometry(WWindow *wwin, int directions)
295 /* never been saved? */
296 if (!wwin->old_geometry.width)
297 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
298 if (!wwin->old_geometry.height)
299 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
301 if (directions & SAVE_GEOMETRY_X)
302 wwin->old_geometry.x = wwin->frame_x;
303 if (directions & SAVE_GEOMETRY_Y)
304 wwin->old_geometry.y = wwin->frame_y;
305 if (directions & SAVE_GEOMETRY_WIDTH)
306 wwin->old_geometry.width = wwin->client.width;
307 if (directions & SAVE_GEOMETRY_HEIGHT)
308 wwin->old_geometry.height = wwin->client.height;
311 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
313 WMRect old_geom_rect;
314 int old_head;
315 Bool same_head;
317 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
318 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
319 same_head = (wGetHeadForWindow(wwin) == old_head);
320 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
321 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
322 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
323 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
326 /* Remember geometry for unmaximizing */
327 void update_saved_geometry(WWindow *wwin)
329 /* NOT if we aren't already maximized
330 * we'll save geometry when maximizing */
331 if (!wwin->flags.maximized)
332 return;
334 /* NOT if we are fully maximized */
335 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
336 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
337 (wwin->flags.maximized & MAX_VERTICAL)))
338 return;
340 /* save the co-ordinate in the axis in which we AREN'T maximized */
341 if (wwin->flags.maximized & MAX_HORIZONTAL)
342 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
343 if (wwin->flags.maximized & MAX_VERTICAL)
344 save_old_geometry(wwin, SAVE_GEOMETRY_X);
347 void wMaximizeWindow(WWindow *wwin, int directions)
349 unsigned int new_width, new_height, half_scr_width, half_scr_height;
350 int new_x = 0;
351 int new_y = 0;
352 int maximus_x = 0;
353 int maximus_y = 0;
354 unsigned int maximus_width = 0;
355 unsigned int maximus_height = 0;
356 WArea usableArea, totalArea;
357 Bool has_border = 1;
358 int adj_size;
359 WScreen *scr = wwin->screen_ptr;
361 if (!IS_RESIZABLE(wwin))
362 return;
364 if (!HAS_BORDER(wwin))
365 has_border = 0;
367 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
368 wwin->client_flags.no_movable = 1;
370 /* the size to adjust the geometry */
371 adj_size = scr->frame_border_width * 2 * has_border;
373 /* save old coordinates before we change the current values */
374 if (!wwin->flags.maximized)
375 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
377 totalArea.x2 = scr->scr_width;
378 totalArea.y2 = scr->scr_height;
379 totalArea.x1 = 0;
380 totalArea.y1 = 0;
381 usableArea = totalArea;
383 if (!(directions & MAX_IGNORE_XINERAMA)) {
384 WScreen *scr = wwin->screen_ptr;
385 int head;
387 if (directions & MAX_KEYBOARD)
388 head = wGetHeadForWindow(wwin);
389 else
390 head = wGetHeadForPointerLocation(scr);
392 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
396 /* Only save directions, not kbd or xinerama hints */
397 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
399 if (WFLAGP(wwin, full_maximize))
400 usableArea = totalArea;
401 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
402 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
404 if (wwin->flags.shaded) {
405 wwin->flags.skip_next_animation = 1;
406 wUnshadeWindow(wwin);
409 if (directions & MAX_MAXIMUS) {
410 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
411 new_width = maximus_width - adj_size;
412 new_height = maximus_height - adj_size;
413 new_x = maximus_x;
414 new_y = maximus_y;
415 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
416 new_height += wwin->frame->bottom_width - 1;
417 new_y -= wwin->frame->top_width;
420 wwin->maximus_x = new_x;
421 wwin->maximus_y = new_y;
422 wwin->flags.old_maximized |= MAX_MAXIMUS;
423 } else {
424 /* set default values if no option set then */
425 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
426 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
427 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
429 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
430 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
431 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
434 /* left|right position */
435 if (directions & MAX_LEFTHALF) {
436 new_width = half_scr_width - adj_size;
437 new_x = usableArea.x1;
438 } else if (directions & MAX_RIGHTHALF) {
439 new_width = half_scr_width - adj_size;
440 new_x = usableArea.x1 + half_scr_width;
442 /* top|bottom position */
443 if (directions & MAX_TOPHALF) {
444 new_height = half_scr_height - adj_size;
445 new_y = usableArea.y1;
446 } else if (directions & MAX_BOTTOMHALF) {
447 new_height = half_scr_height - adj_size;
448 new_y = usableArea.y1 + half_scr_height;
451 /* vertical|horizontal position */
452 if (directions & MAX_HORIZONTAL) {
453 new_width = usableArea.x2 - usableArea.x1 - adj_size;
454 new_x = usableArea.x1;
456 if (directions & MAX_VERTICAL) {
457 new_height = usableArea.y2 - usableArea.y1 - adj_size;
458 new_y = usableArea.y1;
459 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
460 new_y -= wwin->frame->top_width;
464 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
465 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
467 /* set maximization state */
468 wwin->flags.maximized = directions;
469 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
470 wwin->flags.maximized = MAX_MAXIMUS;
472 wWindowConstrainSize(wwin, &new_width, &new_height);
474 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
475 usableArea.y2 - usableArea.y1, &new_width, &new_height);
477 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
478 wWindowSynthConfigureNotify(wwin);
480 WMPostNotificationName(WMNChangedState, wwin, "maximize");
483 /* generic (un)maximizer */
484 void handleMaximize(WWindow *wwin, int directions)
486 int current = wwin->flags.maximized;
487 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
488 int effective = requested ^ current;
489 int flags = directions & ~requested;
491 if (!effective) {
492 /* allow wMaximizeWindow to restore the Maximusized size */
493 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
494 !(requested & MAX_MAXIMUS))
495 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
496 else
497 wUnmaximizeWindow(wwin);
498 /* these alone mean vertical|horizontal toggle */
499 } else if ((effective == MAX_LEFTHALF) ||
500 (effective == MAX_RIGHTHALF) ||
501 (effective == MAX_TOPHALF) ||
502 (effective == MAX_BOTTOMHALF))
503 wUnmaximizeWindow(wwin);
504 else {
505 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
506 (requested == MAX_MAXIMUS))
507 effective = requested;
508 else {
509 if (requested & MAX_LEFTHALF) {
510 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
511 effective |= MAX_VERTICAL;
512 else
513 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
514 effective |= MAX_LEFTHALF;
515 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
516 } else if (requested & MAX_RIGHTHALF) {
517 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
518 effective |= MAX_VERTICAL;
519 else
520 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
521 effective |= MAX_RIGHTHALF;
522 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
524 if (requested & MAX_TOPHALF) {
525 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
526 effective |= MAX_HORIZONTAL;
527 else
528 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
529 effective |= MAX_TOPHALF;
530 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
531 } else if (requested & MAX_BOTTOMHALF) {
532 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
533 effective |= MAX_HORIZONTAL;
534 else
535 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
536 effective |= MAX_BOTTOMHALF;
537 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
539 if (requested & MAX_HORIZONTAL)
540 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
541 if (requested & MAX_VERTICAL)
542 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
543 effective &= ~MAX_MAXIMUS;
545 wMaximizeWindow(wwin, effective | flags);
549 /* the window boundary coordinates */
550 typedef struct {
551 int left;
552 int right;
553 int bottom;
554 int top;
555 int width;
556 int height;
557 } win_coords;
559 static void set_window_coords(WWindow *wwin, win_coords *obs)
561 obs->left = wwin->frame_x;
562 obs->top = wwin->frame_y;
563 obs->width = wwin->frame->core->width;
564 obs->height = wwin->frame->core->height;
565 obs->bottom = obs->top + obs->height;
566 obs->right = obs->left + obs->width;
570 * Maximus: tiled maximization (maximize without overlapping other windows)
572 * The original window 'orig' will be maximized to new coordinates 'new'.
573 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
575 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
576 unsigned int *new_width, unsigned int *new_height)
578 WWindow *tmp;
579 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
580 short int adjust_height;
581 int x_intsect, y_intsect;
582 /* the obstructing, original and new windows */
583 win_coords obs, orig, new;
585 /* set the original coordinate positions of the window to be Maximumized */
586 if (wwin->flags.maximized) {
587 /* window is already maximized; consider original geometry */
588 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
589 orig.bottom = orig.top + orig.height;
590 orig.right = orig.left + orig.width;
591 } else
592 set_window_coords(wwin, &orig);
594 /* Try to fully maximize first, then readjust later */
595 new.left = usableArea.x1;
596 new.right = usableArea.x2;
597 new.top = usableArea.y1;
598 new.bottom = usableArea.y2;
600 if (HAS_TITLEBAR(wwin))
601 tbar_height_0 = TITLEBAR_HEIGHT;
602 if (HAS_RESIZEBAR(wwin))
603 rbar_height_0 = RESIZEBAR_HEIGHT;
604 if (HAS_BORDER(wwin))
605 bd_width_0 = wwin->screen_ptr->frame_border_width;
607 /* the length to be subtracted if the window has titlebar, etc */
608 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
610 tmp = wwin;
611 /* The focused window is always the last in the list */
612 while (tmp->prev) {
613 /* ignore windows in other workspaces etc */
614 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
615 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
616 tmp = tmp->prev;
617 continue;
619 tmp = tmp->prev;
621 /* Set the coordinates of obstructing window */
622 set_window_coords(tmp, &obs);
624 /* Try to maximize in the y direction first */
625 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
626 if (x_intsect != 0) {
627 /* TODO: Consider the case when coords are equal */
628 if (obs.bottom < orig.top && obs.bottom > new.top) {
629 /* w_0 is below the bottom of w_j */
630 new.top = obs.bottom + 1;
632 if (orig.bottom < obs.top && obs.top < new.bottom) {
633 /* The bottom of w_0 is above the top of w_j */
634 new.bottom = obs.top - 1;
639 tmp = wwin;
640 while (tmp->prev) {
641 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
642 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
643 tmp = tmp->prev;
644 continue;
646 tmp = tmp->prev;
648 set_window_coords(tmp, &obs);
651 * Use the new.top and new.height instead of original values
652 * as they may have different intersections with the obstructing windows
654 new.height = new.bottom - new.top - adjust_height;
655 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
656 if (y_intsect != 0) {
657 if (obs.right < orig.left && obs.right > new.left) {
658 /* w_0 is completely to the right of w_j */
659 new.left = obs.right + 1;
661 if (orig.right < obs.left && obs.left < new.right) {
662 /* w_0 is completely to the left of w_j */
663 new.right = obs.left - 1;
668 *new_x = new.left;
669 *new_y = new.top;
670 /* xcalc needs -7 here, but other apps don't */
671 *new_height = new.bottom - new.top - adjust_height - 1;
672 *new_width = new.right - new.left;
675 void wUnmaximizeWindow(WWindow *wwin)
677 int x, y, w, h;
679 if (!wwin->flags.maximized)
680 return;
682 if (wwin->flags.shaded) {
683 wwin->flags.skip_next_animation = 1;
684 wUnshadeWindow(wwin);
687 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
688 wwin->client_flags.no_movable = 0;
690 /* Use old coordinates if they are set, current values otherwise */
691 remember_geometry(wwin, &x, &y, &w, &h);
693 /* unMaximusize relative to original position */
694 if (wwin->flags.maximized & MAX_MAXIMUS) {
695 x += wwin->frame_x - wwin->maximus_x;
696 y += wwin->frame_y - wwin->maximus_y;
699 wwin->flags.maximized = 0;
700 wwin->flags.old_maximized = 0;
701 wWindowConfigure(wwin, x, y, w, h);
702 wWindowSynthConfigureNotify(wwin);
704 WMPostNotificationName(WMNChangedState, wwin, "maximize");
707 void wFullscreenWindow(WWindow *wwin)
709 int head;
710 WMRect rect;
712 if (wwin->flags.fullscreen)
713 return;
715 wwin->flags.fullscreen = True;
717 wWindowConfigureBorders(wwin);
719 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
720 wRaiseFrame(wwin->frame->core);
722 wwin->bfs_geometry.x = wwin->frame_x;
723 wwin->bfs_geometry.y = wwin->frame_y;
724 wwin->bfs_geometry.width = wwin->frame->core->width;
725 wwin->bfs_geometry.height = wwin->frame->core->height;
727 head = wGetHeadForWindow(wwin);
728 rect = wGetRectForHead(wwin->screen_ptr, head);
729 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
731 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
732 wSetFocusTo(wwin->screen_ptr, wwin);
734 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
737 void wUnfullscreenWindow(WWindow *wwin)
739 if (!wwin->flags.fullscreen)
740 return;
742 wwin->flags.fullscreen = False;
744 if (WFLAGP(wwin, sunken))
745 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
746 else if (WFLAGP(wwin, floating))
747 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
749 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
750 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
752 wWindowConfigureBorders(wwin);
754 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
755 wFrameWindowPaint(wwin->frame);
758 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
760 if (wwin->screen_ptr->bfs_focused_window) {
761 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
762 wwin->screen_ptr->bfs_focused_window = NULL;
766 #ifdef ANIMATIONS
767 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
769 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
770 float cx, cy, cw, ch;
771 float xstep, ystep, wstep, hstep;
772 XPoint points[5];
773 float dx, dch, midy;
774 float angle, final_angle, delta;
776 xstep = (float)(fx - x) / steps;
777 ystep = (float)(fy - y) / steps;
778 wstep = (float)(fw - w) / steps;
779 hstep = (float)(fh - h) / steps;
781 cx = (float)x;
782 cy = (float)y;
783 cw = (float)w;
784 ch = (float)h;
786 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
787 delta = (float)(final_angle / FRAMES);
788 for (angle = 0;; angle += delta) {
789 if (angle > final_angle)
790 angle = final_angle;
792 dx = (cw / 10) - ((cw / 5) * sin(angle));
793 dch = (ch / 2) * cos(angle);
794 midy = cy + (ch / 2);
796 points[0].x = cx + dx;
797 points[0].y = midy - dch;
798 points[1].x = cx + cw - dx;
799 points[1].y = points[0].y;
800 points[2].x = cx + cw + dx;
801 points[2].y = midy + dch;
802 points[3].x = cx - dx;
803 points[3].y = points[2].y;
804 points[4].x = points[0].x;
805 points[4].y = points[0].y;
807 XGrabServer(dpy);
808 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
809 XFlush(dpy);
810 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
812 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
813 XUngrabServer(dpy);
814 cx += xstep;
815 cy += ystep;
816 cw += wstep;
817 ch += hstep;
818 if (angle >= final_angle)
819 break;
822 XFlush(dpy);
825 #undef FRAMES
827 static void
828 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
830 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
831 float cx, cy, cw, ch;
832 float xstep, ystep, wstep, hstep;
833 XPoint points[5];
834 float angle, final_angle, a, d, delta;
836 x += w / 2;
837 y += h / 2;
838 fx += fw / 2;
839 fy += fh / 2;
841 xstep = (float)(fx - x) / steps;
842 ystep = (float)(fy - y) / steps;
843 wstep = (float)(fw - w) / steps;
844 hstep = (float)(fh - h) / steps;
846 cx = (float)x;
847 cy = (float)y;
848 cw = (float)w;
849 ch = (float)h;
851 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
852 delta = (float)(final_angle / FRAMES);
853 for (angle = 0;; angle += delta) {
854 if (angle > final_angle)
855 angle = final_angle;
857 a = atan(ch / cw);
858 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
860 points[0].x = cx + cos(angle - a) * d;
861 points[0].y = cy + sin(angle - a) * d;
862 points[1].x = cx + cos(angle + a) * d;
863 points[1].y = cy + sin(angle + a) * d;
864 points[2].x = cx + cos(angle - a + WM_PI) * d;
865 points[2].y = cy + sin(angle - a + WM_PI) * d;
866 points[3].x = cx + cos(angle + a + WM_PI) * d;
867 points[3].y = cy + sin(angle + a + WM_PI) * d;
868 points[4].x = cx + cos(angle - a) * d;
869 points[4].y = cy + sin(angle - a) * d;
870 XGrabServer(dpy);
871 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
872 XFlush(dpy);
873 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
875 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
876 XUngrabServer(dpy);
877 cx += xstep;
878 cy += ystep;
879 cw += wstep;
880 ch += hstep;
881 if (angle >= final_angle)
882 break;
885 XFlush(dpy);
888 #undef FRAMES
890 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
892 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
893 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
894 float xstep, ystep, wstep, hstep;
895 int i, j;
897 xstep = (float)(fx - x) / steps;
898 ystep = (float)(fy - y) / steps;
899 wstep = (float)(fw - w) / steps;
900 hstep = (float)(fh - h) / steps;
902 for (j = 0; j < FRAMES; j++) {
903 cx[j] = (float)x;
904 cy[j] = (float)y;
905 cw[j] = (float)w;
906 ch[j] = (float)h;
908 XGrabServer(dpy);
909 for (i = 0; i < steps; i++) {
910 for (j = 0; j < FRAMES; j++) {
911 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
912 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
914 XFlush(dpy);
915 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
917 for (j = 0; j < FRAMES; j++) {
918 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
919 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
920 if (j < FRAMES - 1) {
921 cx[j] = cx[j + 1];
922 cy[j] = cy[j + 1];
923 cw[j] = cw[j + 1];
924 ch[j] = ch[j + 1];
925 } else {
926 cx[j] += xstep;
927 cy[j] += ystep;
928 cw[j] += wstep;
929 ch[j] += hstep;
934 for (j = 0; j < FRAMES; j++)
935 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
936 XFlush(dpy);
937 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
939 for (j = 0; j < FRAMES; j++)
940 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
942 XUngrabServer(dpy);
945 #undef FRAMES
947 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
949 int style = wPreferences.iconification_style; /* Catch the value */
950 int steps;
952 if (style == WIS_NONE)
953 return;
955 if (style == WIS_RANDOM)
956 style = rand() % 3;
958 switch (style) {
959 case WIS_TWIST:
960 steps = MINIATURIZE_ANIMATION_STEPS_T;
961 if (steps > 0)
962 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
963 break;
964 case WIS_FLIP:
965 steps = MINIATURIZE_ANIMATION_STEPS_F;
966 if (steps > 0)
967 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
968 break;
969 case WIS_ZOOM:
970 default:
971 steps = MINIATURIZE_ANIMATION_STEPS_Z;
972 if (steps > 0)
973 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
974 break;
977 #endif /* ANIMATIONS */
979 static void flushExpose(void)
981 XEvent tmpev;
983 while (XCheckTypedEvent(dpy, Expose, &tmpev))
984 WMHandleEvent(&tmpev);
985 XSync(dpy, 0);
988 static void unmapTransientsFor(WWindow *wwin)
990 WWindow *tmp;
992 tmp = wwin->screen_ptr->focused_window;
993 while (tmp) {
994 /* unmap the transients for this transient */
995 if (tmp != wwin && tmp->transient_for == wwin->client_win
996 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
997 unmapTransientsFor(tmp);
998 tmp->flags.miniaturized = 1;
999 if (!tmp->flags.shaded)
1000 wWindowUnmap(tmp);
1001 else
1002 XUnmapWindow(dpy, tmp->frame->core->window);
1004 if (!tmp->flags.shaded)
1006 wClientSetState(tmp, IconicState, None);
1008 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1010 tmp = tmp->prev;
1014 static void mapTransientsFor(WWindow *wwin)
1016 WWindow *tmp;
1018 tmp = wwin->screen_ptr->focused_window;
1019 while (tmp) {
1020 /* recursively map the transients for this transient */
1021 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1022 && tmp->icon == NULL) {
1023 mapTransientsFor(tmp);
1024 tmp->flags.miniaturized = 0;
1025 if (!tmp->flags.shaded)
1026 wWindowMap(tmp);
1027 else
1028 XMapWindow(dpy, tmp->frame->core->window);
1029 tmp->flags.semi_focused = 0;
1031 if (!tmp->flags.shaded)
1033 wClientSetState(tmp, NormalState, None);
1035 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1037 tmp = tmp->prev;
1041 static WWindow *recursiveTransientFor(WWindow *wwin)
1043 int i;
1045 if (!wwin)
1046 return None;
1048 /* hackish way to detect transient_for cycle */
1049 i = wwin->screen_ptr->window_count + 1;
1051 while (wwin && wwin->transient_for != None && i > 0) {
1052 wwin = wWindowFor(wwin->transient_for);
1053 i--;
1055 if (i == 0 && wwin) {
1056 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1057 return NULL;
1060 return wwin;
1063 static int getAnimationGeometry(WWindow *wwin, int *ix, int *iy, int *iw, int *ih)
1065 if (wwin->screen_ptr->flags.startup || wPreferences.no_animations
1066 || wwin->flags.skip_next_animation || wwin->icon == NULL)
1067 return 0;
1069 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1070 *ix = wwin->icon_x;
1071 *iy = wwin->icon_y;
1072 *iw = wwin->icon->core->width;
1073 *ih = wwin->icon->core->height;
1074 } else {
1075 if (wwin->flags.net_handle_icon) {
1076 *ix = wwin->icon_x;
1077 *iy = wwin->icon_y;
1078 *iw = wwin->icon_w;
1079 *ih = wwin->icon_h;
1080 } else {
1081 *ix = 0;
1082 *iy = 0;
1083 *iw = wwin->screen_ptr->scr_width;
1084 *ih = wwin->screen_ptr->scr_height;
1087 return 1;
1090 void wIconifyWindow(WWindow *wwin)
1092 XWindowAttributes attribs;
1093 int present;
1095 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1096 return; /* the window doesn't exist anymore */
1098 if (wwin->flags.miniaturized)
1099 return; /* already miniaturized */
1101 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1102 WWindow *owner = wWindowFor(wwin->transient_for);
1104 if (owner && owner->flags.miniaturized)
1105 return;
1108 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1110 /* if the window is in another workspace, simplify process */
1111 if (present) {
1112 /* icon creation may take a while */
1113 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1114 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1115 GrabModeAsync, None, None, CurrentTime);
1118 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1119 if (!wwin->flags.icon_moved)
1120 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1122 wwin->icon = icon_create_for_wwindow(wwin);
1123 wwin->icon->mapped = 1;
1125 /* extract the window screenshot everytime, as the option can be enable anytime */
1126 if (wwin->client_win && wwin->flags.mapped) {
1127 RImage *apercu;
1128 XImage *pimg;
1129 unsigned int w, h;
1130 int x, y;
1131 Window baz;
1133 XRaiseWindow(dpy, wwin->frame->core->window);
1134 XTranslateCoordinates(dpy, wwin->client_win, wwin->screen_ptr->root_win, 0, 0, &x, &y, &baz);
1136 w = attribs.width;
1137 h = attribs.height;
1139 if (x - attribs.x + attribs.width > wwin->screen_ptr->scr_width)
1140 w = wwin->screen_ptr->scr_width - x + attribs.x;
1142 if (y - attribs.y + attribs.height > wwin->screen_ptr->scr_height)
1143 h = wwin->screen_ptr->scr_height - y + attribs.y;
1145 pimg = XGetImage(dpy, wwin->client_win, 0, 0, w, h, AllPlanes, ZPixmap);
1146 if (pimg) {
1147 apercu = RCreateImageFromXImage(wwin->screen_ptr->rcontext, pimg, NULL);
1148 XDestroyImage(pimg);
1150 if (apercu) {
1151 set_icon_apercu(wwin->icon, apercu);
1152 RReleaseImage(apercu);
1153 } else {
1154 wwarning("window apercu creation failed");
1160 wwin->flags.miniaturized = 1;
1161 wwin->flags.mapped = 0;
1163 /* unmap transients */
1164 unmapTransientsFor(wwin);
1166 if (present) {
1167 #ifdef ANIMATIONS
1168 int ix, iy, iw, ih;
1169 #endif
1170 XUngrabPointer(dpy, CurrentTime);
1171 wWindowUnmap(wwin);
1172 /* let all Expose events arrive so that we can repaint
1173 * something before the animation starts (and the server is grabbed) */
1174 XSync(dpy, 0);
1176 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1177 wClientSetState(wwin, IconicState, None);
1178 else
1179 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1181 flushExpose();
1182 #ifdef ANIMATIONS
1183 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1184 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1185 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1186 #endif
1189 wwin->flags.skip_next_animation = 0;
1191 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1192 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1193 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1194 XMapWindow(dpy, wwin->icon->core->window);
1196 AddToStackList(wwin->icon->core);
1197 wLowerFrame(wwin->icon->core);
1200 if (present) {
1201 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1204 * It doesn't seem to be working and causes button event hangup
1205 * when deiconifying a transient window.
1206 setupIconGrabs(wwin->icon);
1208 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1209 && wPreferences.focus_mode == WKF_CLICK) {
1210 WWindow *tmp;
1212 tmp = wwin->prev;
1213 while (tmp) {
1214 if (!WFLAGP(tmp, no_focusable)
1215 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1216 && (wwin->frame->workspace == tmp->frame->workspace))
1217 break;
1218 tmp = tmp->prev;
1220 wSetFocusTo(wwin->screen_ptr, tmp);
1221 } else if (wPreferences.focus_mode != WKF_CLICK) {
1222 wSetFocusTo(wwin->screen_ptr, NULL);
1224 #ifdef ANIMATIONS
1225 if (!wwin->screen_ptr->flags.startup) {
1226 /* Catch up with events not processed while animation was running */
1227 Window clientwin = wwin->client_win;
1229 ProcessPendingEvents();
1231 /* the window can disappear while ProcessPendingEvents() runs */
1232 if (!wWindowFor(clientwin))
1233 return;
1235 #endif
1238 /* maybe we want to do this regardless of net_handle_icon
1239 * it seems to me we might break behaviour this way.
1241 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1242 && !wwin->flags.net_handle_icon)
1243 wIconSelect(wwin->icon);
1245 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1247 if (wPreferences.auto_arrange_icons)
1248 wArrangeIcons(wwin->screen_ptr, True);
1251 void wDeiconifyWindow(WWindow *wwin)
1253 /* Let's avoid changing workspace while deiconifying */
1254 w_global.ignore_workspace_change = True;
1256 /* we're hiding for show_desktop */
1257 int netwm_hidden = wwin->flags.net_show_desktop &&
1258 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1260 if (!netwm_hidden)
1261 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1263 if (!wwin->flags.miniaturized) {
1264 w_global.ignore_workspace_change = False;
1265 return;
1268 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1269 WWindow *owner = recursiveTransientFor(wwin);
1271 if (owner && owner->flags.miniaturized) {
1272 wDeiconifyWindow(owner);
1273 wSetFocusTo(wwin->screen_ptr, wwin);
1274 wRaiseFrame(wwin->frame->core);
1275 w_global.ignore_workspace_change = False;
1276 return;
1280 wwin->flags.miniaturized = 0;
1282 if (!netwm_hidden && !wwin->flags.shaded)
1283 wwin->flags.mapped = 1;
1285 if (!netwm_hidden || wPreferences.sticky_icons) {
1286 /* maybe we want to do this regardless of net_handle_icon
1287 * it seems to me we might break behaviour this way.
1289 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1290 && wwin->icon != NULL) {
1291 if (wwin->icon->selected)
1292 wIconSelect(wwin->icon);
1294 XUnmapWindow(dpy, wwin->icon->core->window);
1298 /* if the window is in another workspace, do it silently */
1299 if (!netwm_hidden) {
1300 #ifdef ANIMATIONS
1301 int ix, iy, iw, ih;
1302 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
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);
1306 #endif
1307 wwin->flags.skip_next_animation = 0;
1308 XGrabServer(dpy);
1309 if (!wwin->flags.shaded)
1310 XMapWindow(dpy, wwin->client_win);
1312 XMapWindow(dpy, wwin->frame->core->window);
1313 wRaiseFrame(wwin->frame->core);
1314 if (!wwin->flags.shaded)
1315 wClientSetState(wwin, NormalState, None);
1317 mapTransientsFor(wwin);
1320 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1321 && !wwin->flags.net_handle_icon) {
1322 RemoveFromStackList(wwin->icon->core);
1323 wSetFocusTo(wwin->screen_ptr, wwin);
1324 wIconDestroy(wwin->icon);
1325 wwin->icon = NULL;
1328 if (!netwm_hidden) {
1329 XUngrabServer(dpy);
1331 wSetFocusTo(wwin->screen_ptr, wwin);
1333 #ifdef ANIMATIONS
1334 if (!wwin->screen_ptr->flags.startup) {
1335 /* Catch up with events not processed while animation was running */
1336 Window clientwin = wwin->client_win;
1338 ProcessPendingEvents();
1340 /* the window can disappear while ProcessPendingEvents() runs */
1341 if (!wWindowFor(clientwin)) {
1342 w_global.ignore_workspace_change = False;
1343 return;
1346 #endif
1349 if (wPreferences.auto_arrange_icons)
1350 wArrangeIcons(wwin->screen_ptr, True);
1352 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1354 /* In case we were shaded and iconified, also unshade */
1355 if (!netwm_hidden)
1356 wUnshadeWindow(wwin);
1358 w_global.ignore_workspace_change = False;
1361 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1363 if (wwin->flags.miniaturized) {
1364 if (wwin->icon) {
1365 XUnmapWindow(dpy, wwin->icon->core->window);
1366 wwin->icon->mapped = 0;
1368 wwin->flags.hidden = 1;
1370 WMPostNotificationName(WMNChangedState, wwin, "hide");
1371 return;
1374 if (wwin->flags.inspector_open)
1375 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;
1517 if (wapp->app_icon) {
1518 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1519 wapp->app_icon->y_pos, wlist, animate);
1520 animate = False;
1523 wlist = wlist->prev;
1526 wapp->flags.skip_next_animation = 0;
1528 if (hadfocus) {
1529 if (wPreferences.focus_mode == WKF_CLICK) {
1530 wlist = scr->focused_window;
1531 while (wlist) {
1532 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1533 && (wlist->flags.mapped || wlist->flags.shaded))
1534 break;
1535 wlist = wlist->prev;
1537 wSetFocusTo(scr, wlist);
1538 } else {
1539 wSetFocusTo(scr, NULL);
1543 wapp->flags.hidden = 1;
1545 if (wPreferences.auto_arrange_icons)
1546 wArrangeIcons(scr, True);
1548 #ifdef HIDDENDOT
1549 if (wapp->app_icon)
1550 wAppIconPaint(wapp->app_icon);
1551 #endif
1554 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1556 if (bringToCurrentWS)
1557 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1559 wwin->flags.hidden = 0;
1561 #ifdef ANIMATIONS
1562 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1563 animateResize(wwin->screen_ptr, icon_x, icon_y,
1564 icon->core->width, icon->core->height,
1565 wwin->frame_x, wwin->frame_y,
1566 wwin->frame->core->width, wwin->frame->core->height);
1568 #endif
1569 wwin->flags.skip_next_animation = 0;
1570 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1571 XMapWindow(dpy, wwin->client_win);
1572 XMapWindow(dpy, wwin->frame->core->window);
1573 wClientSetState(wwin, NormalState, None);
1574 wwin->flags.mapped = 1;
1575 wRaiseFrame(wwin->frame->core);
1577 if (wwin->flags.inspector_open)
1578 wUnhideInspectorForWindow(wwin);
1580 WMPostNotificationName(WMNChangedState, wwin, "hide");
1583 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1585 WScreen *scr;
1586 WWindow *wlist, *next;
1587 WWindow *focused = NULL;
1588 int animate;
1590 if (!wapp)
1591 return;
1593 scr = wapp->main_window_desc->screen_ptr;
1594 wlist = scr->focused_window;
1595 if (!wlist)
1596 return;
1598 /* goto beginning of list */
1599 while (wlist->prev)
1600 wlist = wlist->prev;
1602 animate = !wapp->flags.skip_next_animation;
1604 while (wlist) {
1605 next = wlist->next;
1607 if (wlist->main_window == wapp->main_window) {
1608 if (wlist->flags.focused)
1609 focused = wlist;
1610 else if (!focused || !focused->flags.focused)
1611 focused = wlist;
1613 if (wlist->flags.miniaturized) {
1614 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1615 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1616 if (!wlist->icon->mapped) {
1617 int x, y;
1619 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1620 if (wlist->icon_x != x || wlist->icon_y != y)
1621 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1622 wlist->icon_x = x;
1623 wlist->icon_y = y;
1624 XMapWindow(dpy, wlist->icon->core->window);
1625 wlist->icon->mapped = 1;
1627 wRaiseFrame(wlist->icon->core);
1629 if (bringToCurrentWS)
1630 wWindowChangeWorkspace(wlist, scr->current_workspace);
1631 wlist->flags.hidden = 0;
1632 if (miniwindows && wlist->frame->workspace == scr->current_workspace)
1633 wDeiconifyWindow(wlist);
1634 WMPostNotificationName(WMNChangedState, wlist, "hide");
1635 } else if (wlist->flags.shaded) {
1636 if (bringToCurrentWS)
1637 wWindowChangeWorkspace(wlist, scr->current_workspace);
1638 wlist->flags.hidden = 0;
1639 wRaiseFrame(wlist->frame->core);
1640 if (wlist->frame->workspace == scr->current_workspace) {
1641 XMapWindow(dpy, wlist->frame->core->window);
1642 if (miniwindows)
1643 wUnshadeWindow(wlist);
1645 WMPostNotificationName(WMNChangedState, wlist, "hide");
1646 } else if (wlist->flags.hidden) {
1647 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1648 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1649 animate = False;
1650 } else {
1651 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace)
1652 wWindowChangeWorkspace(wlist, scr->current_workspace);
1654 wRaiseFrame(wlist->frame->core);
1657 wlist = next;
1660 wapp->flags.skip_next_animation = 0;
1661 wapp->flags.hidden = 0;
1663 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1664 wRaiseFrame(wapp->last_focused->frame->core);
1665 wSetFocusTo(scr, wapp->last_focused);
1666 } else if (focused) {
1667 wSetFocusTo(scr, focused);
1669 wapp->last_focused = NULL;
1670 if (wPreferences.auto_arrange_icons)
1671 wArrangeIcons(scr, True);
1673 #ifdef HIDDENDOT
1674 wAppIconPaint(wapp->app_icon);
1675 #endif
1678 void wShowAllWindows(WScreen *scr)
1680 WWindow *wwin, *old_foc;
1681 WApplication *wapp;
1683 old_foc = wwin = scr->focused_window;
1684 while (wwin) {
1685 if (!wwin->flags.internal_window &&
1686 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1687 if (wwin->flags.miniaturized) {
1688 wwin->flags.skip_next_animation = 1;
1689 wDeiconifyWindow(wwin);
1690 } else if (wwin->flags.hidden) {
1691 wapp = wApplicationOf(wwin->main_window);
1692 if (wapp) {
1693 wUnhideApplication(wapp, False, False);
1694 } else {
1695 wwin->flags.skip_next_animation = 1;
1696 wDeiconifyWindow(wwin);
1700 wwin = wwin->prev;
1702 wSetFocusTo(scr, old_foc);
1703 /*wRaiseFrame(old_foc->frame->core); */
1706 void wRefreshDesktop(WScreen *scr)
1708 Window win;
1709 XSetWindowAttributes attr;
1711 attr.backing_store = NotUseful;
1712 attr.save_under = False;
1713 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1714 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1715 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1716 XMapRaised(dpy, win);
1717 XDestroyWindow(dpy, win);
1718 XFlush(dpy);
1721 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1723 WWindow *wwin;
1724 WAppIcon *aicon;
1726 int head;
1727 const int heads = wXineramaHeads(scr);
1729 struct HeadVars {
1730 int pf; /* primary axis */
1731 int sf; /* secondary axis */
1732 int fullW;
1733 int fullH;
1734 int pi, si;
1735 int sx1, sx2, sy1, sy2; /* screen boundary */
1736 int sw, sh;
1737 int xo, yo;
1738 int xs, ys;
1739 } *vars;
1741 int isize = wPreferences.icon_size;
1743 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1745 for (head = 0; head < heads; ++head) {
1746 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1747 WMRect rect;
1749 if (scr->dock) {
1750 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1752 if (scr->dock->on_right_side)
1753 area.x2 -= offset;
1754 else
1755 area.x1 += offset;
1758 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1760 vars[head].pi = vars[head].si = 0;
1761 vars[head].sx1 = rect.pos.x;
1762 vars[head].sy1 = rect.pos.y;
1763 vars[head].sw = rect.size.width;
1764 vars[head].sh = rect.size.height;
1765 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1766 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1767 vars[head].sw = isize * (vars[head].sw / isize);
1768 vars[head].sh = isize * (vars[head].sh / isize);
1769 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1770 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1772 /* icon yard boundaries */
1773 if (wPreferences.icon_yard & IY_VERT) {
1774 vars[head].pf = vars[head].fullH;
1775 vars[head].sf = vars[head].fullW;
1776 } else {
1777 vars[head].pf = vars[head].fullW;
1778 vars[head].sf = vars[head].fullH;
1780 if (wPreferences.icon_yard & IY_RIGHT) {
1781 vars[head].xo = vars[head].sx2 - isize;
1782 vars[head].xs = -1;
1783 } else {
1784 vars[head].xo = vars[head].sx1;
1785 vars[head].xs = 1;
1787 if (wPreferences.icon_yard & IY_TOP) {
1788 vars[head].yo = vars[head].sy1;
1789 vars[head].ys = 1;
1790 } else {
1791 vars[head].yo = vars[head].sy2 - isize;
1792 vars[head].ys = -1;
1796 #define X ((wPreferences.icon_yard & IY_VERT) \
1797 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1798 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1800 #define Y ((wPreferences.icon_yard & IY_VERT) \
1801 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1802 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1804 /* arrange application icons */
1805 aicon = scr->app_icon_list;
1806 /* reverse them to avoid unnecessarily sliding of icons */
1807 while (aicon && aicon->next)
1808 aicon = aicon->next;
1810 while (aicon) {
1811 if (!aicon->docked) {
1812 /* CHECK: can icon be NULL here ? */
1813 /* The intention here is to place the AppIcon on the head that
1814 * contains most of the applications _main_ window. */
1815 head = wGetHeadForWindow(aicon->icon->owner);
1817 if (aicon->x_pos != X || aicon->y_pos != Y) {
1818 #ifdef ANIMATIONS
1819 if (!wPreferences.no_animations)
1820 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1821 #endif /* ANIMATIONS */
1823 wAppIconMove(aicon, X, Y);
1824 vars[head].pi++;
1825 if (vars[head].pi >= vars[head].pf) {
1826 vars[head].pi = 0;
1827 vars[head].si++;
1830 aicon = aicon->prev;
1833 /* arrange miniwindows */
1834 wwin = scr->focused_window;
1835 /* reverse them to avoid unnecessarily shuffling */
1836 while (wwin && wwin->prev)
1837 wwin = wwin->prev;
1839 while (wwin) {
1840 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1841 (wwin->frame->workspace == scr->current_workspace ||
1842 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1844 head = wGetHeadForWindow(wwin);
1846 if (arrangeAll || !wwin->flags.icon_moved) {
1847 if (wwin->icon_x != X || wwin->icon_y != Y)
1848 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1850 wwin->icon_x = X;
1851 wwin->icon_y = Y;
1853 vars[head].pi++;
1854 if (vars[head].pi >= vars[head].pf) {
1855 vars[head].pi = 0;
1856 vars[head].si++;
1860 if (arrangeAll)
1861 wwin->flags.icon_moved = 0;
1862 /* we reversed the order, so we use next */
1863 wwin = wwin->next;
1866 wfree(vars);
1869 void wSelectWindow(WWindow *wwin, Bool flag)
1871 WScreen *scr = wwin->screen_ptr;
1873 if (flag) {
1874 wwin->flags.selected = 1;
1875 if (wwin->frame->selected_border_pixel)
1876 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1877 else
1878 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1880 if (!HAS_BORDER(wwin))
1881 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1883 if (!scr->selected_windows)
1884 scr->selected_windows = WMCreateArray(4);
1885 WMAddToArray(scr->selected_windows, wwin);
1886 } else {
1887 wwin->flags.selected = 0;
1888 if (wwin->flags.focused) {
1889 if (wwin->frame->focused_border_pixel)
1890 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->focused_border_pixel);
1891 else
1892 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_focused_border_pixel);
1893 } else {
1894 if (wwin->frame->border_pixel)
1895 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1896 else
1897 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1900 if (!HAS_BORDER(wwin))
1901 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1903 if (scr->selected_windows)
1904 WMRemoveFromArray(scr->selected_windows, wwin);
1908 void wMakeWindowVisible(WWindow *wwin)
1910 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1911 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1913 if (wwin->flags.shaded)
1914 wUnshadeWindow(wwin);
1916 if (wwin->flags.hidden) {
1917 WApplication *app;
1919 app = wApplicationOf(wwin->main_window);
1920 if (app) {
1921 /* trick to get focus to this window */
1922 app->last_focused = wwin;
1923 wUnhideApplication(app, False, False);
1926 if (wwin->flags.miniaturized) {
1927 wDeiconifyWindow(wwin);
1928 } else {
1929 if (!WFLAGP(wwin, no_focusable))
1930 wSetFocusTo(wwin->screen_ptr, wwin);
1931 wRaiseFrame(wwin->frame->core);
1936 * Do the animation while shading (called with what = SHADE)
1937 * or unshading (what = UNSHADE).
1939 #ifdef ANIMATIONS
1940 static void shade_animate(WWindow *wwin, Bool what)
1942 int y, s, w, h;
1943 time_t time0 = time(NULL);
1945 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1946 return;
1948 switch (what) {
1949 case SHADE:
1950 if (!wwin->screen_ptr->flags.startup) {
1951 /* do the shading animation */
1952 h = wwin->frame->core->height;
1953 s = h / SHADE_STEPS;
1954 if (s < 1)
1955 s = 1;
1956 w = wwin->frame->core->width;
1957 y = wwin->frame->top_width;
1958 while (h > wwin->frame->top_width + 1) {
1959 XMoveWindow(dpy, wwin->client_win, 0, y);
1960 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1961 XFlush(dpy);
1963 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1964 break;
1966 if (SHADE_DELAY > 0)
1967 wusleep(SHADE_DELAY * 1000L);
1968 else
1969 wusleep(10);
1970 h -= s;
1971 y -= s;
1973 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1975 break;
1977 case UNSHADE:
1978 h = wwin->frame->top_width + wwin->frame->bottom_width;
1979 y = wwin->frame->top_width - wwin->client.height;
1980 s = abs(y) / SHADE_STEPS;
1981 if (s < 1)
1982 s = 1;
1983 w = wwin->frame->core->width;
1984 XMoveWindow(dpy, wwin->client_win, 0, y);
1985 if (s > 0) {
1986 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1987 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1988 XMoveWindow(dpy, wwin->client_win, 0, y);
1989 XFlush(dpy);
1990 if (SHADE_DELAY > 0)
1991 wusleep(SHADE_DELAY * 2000L / 3);
1992 else
1993 wusleep(10);
1994 h += s;
1995 y += s;
1997 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1998 break;
2001 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2002 break;
2005 #endif