added new maximize positions, top and bottom
[wmaker-crm.git] / src / actions.c
blob8a4a0c19387a0a9e9fdb9aff71ba8fec3b3a7dc1
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
33 #include "WindowMaker.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "colormap.h"
39 #include "application.h"
40 #include "actions.h"
41 #include "stacking.h"
42 #include "appicon.h"
43 #include "dock.h"
44 #include "appmenu.h"
45 #include "winspector.h"
46 #include "workspace.h"
47 #include "xinerama.h"
48 #include "usermenu.h"
49 #include "placement.h"
50 #include "misc.h"
52 /****** Global Variables ******/
54 int ignore_wks_change = 0;
55 extern Time LastTimestamp;
56 extern Time LastFocusChange;
57 extern WPreferences wPreferences;
58 extern Atom _XA_WM_TAKE_FOCUS;
60 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
61 unsigned int *new_width, unsigned int *new_height);
62 static void save_old_geometry(WWindow *wwin, int directions);
64 /******* Local Variables *******/
65 static struct {
66 int steps;
67 int delay;
68 } shadePars[5] = {
70 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
71 SHADE_STEPS_F, SHADE_DELAY_F}, {
72 SHADE_STEPS_M, SHADE_DELAY_M}, {
73 SHADE_STEPS_S, SHADE_DELAY_S}, {
74 SHADE_STEPS_US, SHADE_DELAY_US}};
76 #define UNSHADE 0
77 #define SHADE 1
78 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
79 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
81 static int compareTimes(Time t1, Time t2)
83 Time diff;
84 if (t1 == t2)
85 return 0;
86 diff = t1 - t2;
87 return (diff < 60000) ? 1 : -1;
90 #ifdef ANIMATIONS
91 static void shade_animate(WWindow *wwin, Bool what);
92 #else
93 static void shade_animate(WWindow *wwin, Bool what) { }
94 #endif
97 *----------------------------------------------------------------------
98 * wSetFocusTo--
99 * Changes the window focus to the one passed as argument.
100 * If the window to focus is not already focused, it will be brought
101 * to the head of the list of windows. Previously focused window is
102 * unfocused.
104 * Side effects:
105 * Window list may be reordered and the window focus is changed.
107 *----------------------------------------------------------------------
109 void wSetFocusTo(WScreen *scr, WWindow *wwin)
111 static WScreen *old_scr = NULL;
113 WWindow *old_focused;
114 WWindow *focused = scr->focused_window;
115 Time timestamp = LastTimestamp;
116 WApplication *oapp = NULL, *napp = NULL;
117 int wasfocused;
119 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
120 return;
122 if (!old_scr)
123 old_scr = scr;
125 old_focused = old_scr->focused_window;
127 LastFocusChange = timestamp;
129 if (old_focused)
130 oapp = wApplicationOf(old_focused->main_window);
132 if (wwin == NULL) {
133 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
134 if (old_focused)
135 wWindowUnfocus(old_focused);
137 if (oapp) {
138 wAppMenuUnmap(oapp->menu);
139 if (wPreferences.highlight_active_app)
140 wApplicationDeactivate(oapp);
143 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
144 return;
147 if (old_scr != scr && old_focused)
148 wWindowUnfocus(old_focused);
150 wasfocused = wwin->flags.focused;
151 napp = wApplicationOf(wwin->main_window);
153 /* remember last workspace where the app has been */
154 if (napp)
155 napp->last_workspace = wwin->frame->workspace;
157 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
158 /* install colormap if colormap mode is lock mode */
159 if (wPreferences.colormap_mode == WCM_CLICK)
160 wColormapInstallForWindow(scr, wwin);
162 /* set input focus */
163 switch (wwin->focus_mode) {
164 case WFM_NO_INPUT:
165 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
166 break;
167 case WFM_PASSIVE:
168 case WFM_LOCALLY_ACTIVE:
169 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
170 break;
171 case WFM_GLOBALLY_ACTIVE:
172 break;
175 XFlush(dpy);
176 if (wwin->protocols.TAKE_FOCUS)
177 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
179 XSync(dpy, False);
180 } else {
181 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
184 if (WFLAGP(wwin, no_focusable))
185 return;
187 /* if this is not the focused window focus it */
188 if (focused != wwin) {
189 /* change the focus window list order */
190 if (wwin->prev)
191 wwin->prev->next = wwin->next;
193 if (wwin->next)
194 wwin->next->prev = wwin->prev;
196 wwin->prev = focused;
197 focused->next = wwin;
198 wwin->next = NULL;
199 scr->focused_window = wwin;
201 if (oapp && oapp != napp) {
202 wAppMenuUnmap(oapp->menu);
203 if (wPreferences.highlight_active_app)
204 wApplicationDeactivate(oapp);
208 wWindowFocus(wwin, focused);
210 if (napp && !wasfocused) {
211 #ifdef USER_MENU
212 wUserMenuRefreshInstances(napp->menu, wwin);
213 #endif /* USER_MENU */
215 if (wwin->flags.mapped)
216 wAppMenuMap(napp->menu, wwin);
218 if (napp && wPreferences.highlight_active_app)
219 wApplicationActivate(napp);
221 XFlush(dpy);
222 old_scr = scr;
225 void wShadeWindow(WWindow *wwin)
228 if (wwin->flags.shaded)
229 return;
231 XLowerWindow(dpy, wwin->client_win);
232 shade_animate(wwin, SHADE);
234 wwin->flags.skip_next_animation = 0;
235 wwin->flags.shaded = 1;
236 wwin->flags.mapped = 0;
237 /* prevent window withdrawal when getting UnmapNotify */
238 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
239 XUnmapWindow(dpy, wwin->client_win);
240 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
242 /* for the client it's just like iconification */
243 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
245 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
246 wWindowSynthConfigureNotify(wwin);
249 wClientSetState(wwin, IconicState, None);
252 WMPostNotificationName(WMNChangedState, wwin, "shade");
254 #ifdef ANIMATIONS
255 if (!wwin->screen_ptr->flags.startup) {
256 /* Catch up with events not processed while animation was running */
257 ProcessPendingEvents();
259 #endif
262 void wUnshadeWindow(WWindow *wwin)
265 if (!wwin->flags.shaded)
266 return;
268 wwin->flags.shaded = 0;
269 wwin->flags.mapped = 1;
270 XMapWindow(dpy, wwin->client_win);
272 shade_animate(wwin, UNSHADE);
274 wwin->flags.skip_next_animation = 0;
275 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
276 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
278 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
279 wWindowSynthConfigureNotify(wwin);
281 /* if the window is focused, set the focus again as it was disabled during
282 * shading */
283 if (wwin->flags.focused)
284 wSetFocusTo(wwin->screen_ptr, wwin);
286 WMPostNotificationName(WMNChangedState, wwin, "shade");
289 /* Set the old coordinates using the current values */
290 static void save_old_geometry(WWindow *wwin, int directions)
292 /* never been saved? */
293 if (! wwin->old_geometry.width)
294 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
295 if (! wwin->old_geometry.height)
296 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
298 if (directions & SAVE_GEOMETRY_X)
299 wwin->old_geometry.x = wwin->frame_x;
300 if (directions & SAVE_GEOMETRY_Y)
301 wwin->old_geometry.y = wwin->frame_y;
302 if (directions & SAVE_GEOMETRY_WIDTH)
303 wwin->old_geometry.width = wwin->client.width;
304 if (directions & SAVE_GEOMETRY_HEIGHT)
305 wwin->old_geometry.height = wwin->client.height;
308 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
310 WMRect old_geom_rect;
311 int old_head;
312 Bool same_head;
314 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
315 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
316 same_head = (wGetHeadForWindow(wwin) == old_head);
317 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
318 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
319 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
320 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
323 /* Remember geometry for unmaximizing */
324 void update_saved_geometry(WWindow *wwin)
326 /* NOT if we aren't already maximized
327 * we'll save geometry when maximizing */
328 if (!wwin->flags.maximized)
329 return;
331 /* NOT if we are fully maximized */
332 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
333 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
334 (wwin->flags.maximized & MAX_VERTICAL)))
335 return;
337 /* save the co-ordinate in the axis in which we AREN'T maximized */
338 if (wwin->flags.maximized & MAX_HORIZONTAL)
339 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
340 if (wwin->flags.maximized & MAX_VERTICAL)
341 save_old_geometry(wwin, SAVE_GEOMETRY_X);
344 void wMaximizeWindow(WWindow *wwin, int directions)
346 unsigned int new_width, new_height, half_scr_width, half_scr_height;
347 int new_x = 0;
348 int new_y = 0;
349 int maximus_x = 0;
350 int maximus_y = 0;
351 unsigned int maximus_width = 0;
352 unsigned int maximus_height = 0;
353 WArea usableArea, totalArea;
354 Bool has_border = 1;
355 int adj_size;
357 if (!IS_RESIZABLE(wwin))
358 return;
360 if (!HAS_BORDER(wwin))
361 has_border = 0;
363 /* the size to adjust the geometry */
364 adj_size = wwin->screen_ptr->frame_border_width * 2 * has_border;
366 /* save old coordinates before we change the current values */
367 if (!wwin->flags.maximized)
368 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
370 totalArea.x2 = wwin->screen_ptr->scr_width;
371 totalArea.y2 = wwin->screen_ptr->scr_height;
372 totalArea.x1 = 0;
373 totalArea.y1 = 0;
374 usableArea = totalArea;
376 if (!(directions & MAX_IGNORE_XINERAMA)) {
377 WScreen *scr = wwin->screen_ptr;
378 int head;
380 if (directions & MAX_KEYBOARD)
381 head = wGetHeadForWindow(wwin);
382 else
383 head = wGetHeadForPointerLocation(scr);
385 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
388 /* Only save directions, not kbd or xinerama hints */
389 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
391 if (WFLAGP(wwin, full_maximize)) {
392 usableArea = totalArea;
394 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
395 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
397 if (wwin->flags.shaded) {
398 wwin->flags.skip_next_animation = 1;
399 wUnshadeWindow(wwin);
402 if (directions & MAX_MAXIMUS) {
403 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
404 new_width = maximus_width - adj_size;
405 new_height = maximus_height - adj_size;
406 new_x = maximus_x;
407 new_y = maximus_y;
408 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
409 new_height += wwin->frame->bottom_width - 1;
410 new_y -= wwin->frame->top_width;
412 /* HACK: this will be subtracted again below */
413 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
415 wwin->maximus_x = new_x;
416 wwin->maximus_y = new_y;
417 wwin->flags.old_maximized |= MAX_MAXIMUS;
418 } else {
419 /* set default values if no option set then */
420 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
421 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
422 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
424 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
425 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
426 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
429 /* left|right position */
430 if (directions & MAX_LEFTHALF) {
431 new_width = half_scr_width - adj_size;
432 new_x = usableArea.x1;
433 } else if (directions & MAX_RIGHTHALF) {
434 new_width = half_scr_width - adj_size;
435 new_x = usableArea.x1 + half_scr_width;
437 /* top|bottom position */
438 if (directions & MAX_TOPHALF) {
439 new_height = half_scr_height - adj_size;
440 new_y = usableArea.y1;
441 } else if (directions & MAX_BOTTOMHALF) {
442 new_height = half_scr_height - adj_size;
443 new_y = usableArea.y1 + half_scr_height;
446 /* vertical|horizontal position */
447 if (directions & MAX_HORIZONTAL) {
448 new_width = usableArea.x2 - usableArea.x1 - adj_size;
449 new_x = usableArea.x1;
451 if (directions & MAX_VERTICAL) {
452 new_height = usableArea.y2 - usableArea.y1 - adj_size;
453 new_y = usableArea.y1;
454 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
455 new_y -= wwin->frame->top_width;
459 if (!WFLAGP(wwin, full_maximize))
460 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
462 /* set maximization state */
463 wwin->flags.maximized = directions;
464 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
465 wwin->flags.maximized = MAX_MAXIMUS;
467 wWindowConstrainSize(wwin, &new_width, &new_height);
469 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
470 usableArea.y2 - usableArea.y1, &new_width, &new_height);
472 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
473 wWindowSynthConfigureNotify(wwin);
475 WMPostNotificationName(WMNChangedState, wwin, "maximize");
478 /* generic (un)maximizer */
479 void handleMaximize(WWindow *wwin, int directions)
481 int current = wwin->flags.maximized;
482 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
483 int effective = requested ^ current;
484 int flags = directions & ~requested;
486 if (!effective) {
487 /* allow wMaximizeWindow to restore the Maximusized size */
488 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
489 !(requested & MAX_MAXIMUS))
490 wMaximizeWindow(wwin, flags);
491 else
492 wUnmaximizeWindow(wwin);
493 /* these alone mean vertical|horizontal toggle */
494 } else if ((effective == MAX_LEFTHALF) ||
495 (effective == MAX_RIGHTHALF) ||
496 (effective == MAX_TOPHALF) ||
497 (effective == MAX_BOTTOMHALF))
498 wUnmaximizeWindow(wwin);
499 else {
500 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
501 (requested == MAX_MAXIMUS))
502 effective = requested;
503 else {
504 if (requested & MAX_LEFTHALF) {
505 effective |= MAX_VERTICAL;
506 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
507 } else if (requested & MAX_RIGHTHALF) {
508 effective |= MAX_VERTICAL;
509 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
511 if (requested & MAX_TOPHALF) {
512 effective |= MAX_HORIZONTAL;
513 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
514 } else if (requested & MAX_BOTTOMHALF) {
515 effective |= MAX_HORIZONTAL;
516 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
518 if (requested & MAX_HORIZONTAL)
519 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
520 if (requested & MAX_VERTICAL)
521 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
522 effective &= ~MAX_MAXIMUS;
524 wMaximizeWindow(wwin, effective | flags);
528 /* the window boundary coordinates */
529 typedef struct {
530 int left;
531 int right;
532 int bottom;
533 int top;
534 int width;
535 int height;
536 } win_coords;
538 static void set_window_coords(WWindow *wwin, win_coords *obs)
540 obs->left = wwin->frame_x;
541 obs->top = wwin->frame_y;
542 obs->width = wwin->frame->core->width;
543 obs->height = wwin->frame->core->height;
544 obs->bottom = obs->top + obs->height;
545 obs->right = obs->left + obs->width;
549 * Maximus: tiled maximization (maximize without overlapping other windows)
551 * The original window 'orig' will be maximized to new coordinates 'new'.
552 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
554 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
555 unsigned int *new_width, unsigned int *new_height)
557 WWindow *tmp;
558 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
559 short int adjust_height;
560 int x_intsect, y_intsect;
561 /* the obstructing, original and new windows */
562 win_coords obs, orig, new;
564 /* set the original coordinate positions of the window to be Maximumized */
565 if (wwin->flags.maximized) {
566 /* window is already maximized; consider original geometry */
567 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
568 orig.bottom = orig.top + orig.height;
569 orig.right = orig.left + orig.width;
571 else
572 set_window_coords(wwin, &orig);
574 /* Try to fully maximize first, then readjust later */
575 new.left = usableArea.x1;
576 new.right = usableArea.x2;
577 new.top = usableArea.y1;
578 new.bottom = usableArea.y2;
580 if (HAS_TITLEBAR(wwin))
581 tbar_height_0 = TITLEBAR_HEIGHT;
582 if (HAS_RESIZEBAR(wwin))
583 rbar_height_0 = RESIZEBAR_HEIGHT;
584 if (HAS_BORDER(wwin))
585 bd_width_0 = wwin->screen_ptr->frame_border_width;
587 /* the length to be subtracted if the window has titlebar, etc */
588 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
590 tmp = wwin;
591 /* The focused window is always the last in the list */
592 while (tmp->prev) {
593 /* ignore windows in other workspaces etc */
594 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
595 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
596 tmp = tmp->prev;
597 continue;
599 tmp = tmp->prev;
601 /* Set the coordinates of obstructing window */
602 set_window_coords(tmp, &obs);
604 /* Try to maximize in the y direction first */
605 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
606 if (x_intsect != 0) {
607 /* TODO: Consider the case when coords are equal */
608 if (obs.bottom < orig.top && obs.bottom > new.top) {
609 /* w_0 is below the bottom of w_j */
610 new.top = obs.bottom + 1;
612 if (orig.bottom < obs.top && obs.top < new.bottom) {
613 /* The bottom of w_0 is above the top of w_j */
614 new.bottom = obs.top - 1;
619 tmp = wwin;
620 while (tmp->prev) {
621 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
622 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
623 tmp = tmp->prev;
624 continue;
626 tmp = tmp->prev;
628 set_window_coords(tmp, &obs);
631 * Use the new.top and new.height instead of original values
632 * as they may have different intersections with the obstructing windows
634 new.height = new.bottom - new.top - adjust_height;
635 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
636 if (y_intsect != 0) {
637 if (obs.right < orig.left && obs.right > new.left) {
638 /* w_0 is completely to the right of w_j */
639 new.left = obs.right + 1;
641 if (orig.right < obs.left && obs.left < new.right) {
642 /* w_0 is completely to the left of w_j */
643 new.right = obs.left - 1;
648 *new_x = new.left;
649 *new_y = new.top;
650 /* xcalc needs -7 here, but other apps don't */
651 *new_height = new.bottom - new.top - adjust_height - 1;;
652 *new_width = new.right - new.left;
655 void wUnmaximizeWindow(WWindow *wwin)
657 int x, y, w, h;
659 if (!wwin->flags.maximized)
660 return;
662 if (wwin->flags.shaded) {
663 wwin->flags.skip_next_animation = 1;
664 wUnshadeWindow(wwin);
667 /* Use old coordinates if they are set, current values otherwise */
668 remember_geometry(wwin, &x, &y, &w, &h);
670 /* unMaximusize relative to original position */
671 if (wwin->flags.maximized & MAX_MAXIMUS) {
672 x += wwin->frame_x - wwin->maximus_x;
673 y += wwin->frame_y - wwin->maximus_y;
676 wwin->flags.maximized = 0;
677 wwin->flags.old_maximized = 0;
678 wWindowConfigure(wwin, x, y, w, h);
679 wWindowSynthConfigureNotify(wwin);
681 WMPostNotificationName(WMNChangedState, wwin, "maximize");
684 void wFullscreenWindow(WWindow *wwin)
686 int head;
687 WMRect rect;
689 if (wwin->flags.fullscreen)
690 return;
692 wwin->flags.fullscreen = True;
694 wWindowConfigureBorders(wwin);
696 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
698 wwin->bfs_geometry.x = wwin->frame_x;
699 wwin->bfs_geometry.y = wwin->frame_y;
700 wwin->bfs_geometry.width = wwin->frame->core->width;
701 wwin->bfs_geometry.height = wwin->frame->core->height;
703 head = wGetHeadForWindow(wwin);
704 rect = wGetRectForHead(wwin->screen_ptr, head);
705 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
707 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
710 void wUnfullscreenWindow(WWindow *wwin)
712 if (!wwin->flags.fullscreen)
713 return;
715 wwin->flags.fullscreen = False;
717 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
718 if (WFLAGP(wwin, sunken)) {
719 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
720 } else if (WFLAGP(wwin, floating)) {
721 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
722 } else {
723 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
727 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
728 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
730 wWindowConfigureBorders(wwin);
732 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
733 wFrameWindowPaint(wwin->frame);
736 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
739 #ifdef ANIMATIONS
740 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
742 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
743 float cx, cy, cw, ch;
744 float xstep, ystep, wstep, hstep;
745 XPoint points[5];
746 float dx, dch, midy;
747 float angle, final_angle, delta;
749 xstep = (float)(fx - x) / steps;
750 ystep = (float)(fy - y) / steps;
751 wstep = (float)(fw - w) / steps;
752 hstep = (float)(fh - h) / steps;
754 cx = (float)x;
755 cy = (float)y;
756 cw = (float)w;
757 ch = (float)h;
759 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
760 delta = (float)(final_angle / FRAMES);
761 for (angle = 0;; angle += delta) {
762 if (angle > final_angle)
763 angle = final_angle;
765 dx = (cw / 10) - ((cw / 5) * sin(angle));
766 dch = (ch / 2) * cos(angle);
767 midy = cy + (ch / 2);
769 points[0].x = cx + dx;
770 points[0].y = midy - dch;
771 points[1].x = cx + cw - dx;
772 points[1].y = points[0].y;
773 points[2].x = cx + cw + dx;
774 points[2].y = midy + dch;
775 points[3].x = cx - dx;
776 points[3].y = points[2].y;
777 points[4].x = points[0].x;
778 points[4].y = points[0].y;
780 XGrabServer(dpy);
781 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
782 XFlush(dpy);
783 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
785 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
786 XUngrabServer(dpy);
787 cx += xstep;
788 cy += ystep;
789 cw += wstep;
790 ch += hstep;
791 if (angle >= final_angle)
792 break;
795 XFlush(dpy);
798 #undef FRAMES
800 static void
801 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
803 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
804 float cx, cy, cw, ch;
805 float xstep, ystep, wstep, hstep;
806 XPoint points[5];
807 float angle, final_angle, a, d, delta;
809 x += w / 2;
810 y += h / 2;
811 fx += fw / 2;
812 fy += fh / 2;
814 xstep = (float)(fx - x) / steps;
815 ystep = (float)(fy - y) / steps;
816 wstep = (float)(fw - w) / steps;
817 hstep = (float)(fh - h) / steps;
819 cx = (float)x;
820 cy = (float)y;
821 cw = (float)w;
822 ch = (float)h;
824 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
825 delta = (float)(final_angle / FRAMES);
826 for (angle = 0;; angle += delta) {
827 if (angle > final_angle)
828 angle = final_angle;
830 a = atan(ch / cw);
831 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
833 points[0].x = cx + cos(angle - a) * d;
834 points[0].y = cy + sin(angle - a) * d;
835 points[1].x = cx + cos(angle + a) * d;
836 points[1].y = cy + sin(angle + a) * d;
837 points[2].x = cx + cos(angle - a + WM_PI) * d;
838 points[2].y = cy + sin(angle - a + WM_PI) * d;
839 points[3].x = cx + cos(angle + a + WM_PI) * d;
840 points[3].y = cy + sin(angle + a + WM_PI) * d;
841 points[4].x = cx + cos(angle - a) * d;
842 points[4].y = cy + sin(angle - a) * d;
843 XGrabServer(dpy);
844 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
845 XFlush(dpy);
846 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
848 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
849 XUngrabServer(dpy);
850 cx += xstep;
851 cy += ystep;
852 cw += wstep;
853 ch += hstep;
854 if (angle >= final_angle)
855 break;
858 XFlush(dpy);
861 #undef FRAMES
863 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
865 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
866 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
867 float xstep, ystep, wstep, hstep;
868 int i, j;
870 xstep = (float)(fx - x) / steps;
871 ystep = (float)(fy - y) / steps;
872 wstep = (float)(fw - w) / steps;
873 hstep = (float)(fh - h) / steps;
875 for (j = 0; j < FRAMES; j++) {
876 cx[j] = (float)x;
877 cy[j] = (float)y;
878 cw[j] = (float)w;
879 ch[j] = (float)h;
881 XGrabServer(dpy);
882 for (i = 0; i < steps; i++) {
883 for (j = 0; j < FRAMES; j++) {
884 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
885 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
887 XFlush(dpy);
888 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
890 for (j = 0; j < FRAMES; j++) {
891 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
892 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
893 if (j < FRAMES - 1) {
894 cx[j] = cx[j + 1];
895 cy[j] = cy[j + 1];
896 cw[j] = cw[j + 1];
897 ch[j] = ch[j + 1];
898 } else {
899 cx[j] += xstep;
900 cy[j] += ystep;
901 cw[j] += wstep;
902 ch[j] += hstep;
907 for (j = 0; j < FRAMES; j++) {
908 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
910 XFlush(dpy);
911 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
913 for (j = 0; j < FRAMES; j++) {
914 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
917 XUngrabServer(dpy);
920 #undef FRAMES
922 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
924 int style = wPreferences.iconification_style; /* Catch the value */
925 int steps;
927 if (style == WIS_NONE)
928 return;
930 if (style == WIS_RANDOM) {
931 style = rand() % 3;
934 switch (style) {
935 case WIS_TWIST:
936 steps = MINIATURIZE_ANIMATION_STEPS_T;
937 if (steps > 0)
938 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
939 break;
940 case WIS_FLIP:
941 steps = MINIATURIZE_ANIMATION_STEPS_F;
942 if (steps > 0)
943 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
944 break;
945 case WIS_ZOOM:
946 default:
947 steps = MINIATURIZE_ANIMATION_STEPS_Z;
948 if (steps > 0)
949 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
950 break;
953 #endif /* ANIMATIONS */
955 static void flushExpose(void)
957 XEvent tmpev;
959 while (XCheckTypedEvent(dpy, Expose, &tmpev))
960 WMHandleEvent(&tmpev);
961 XSync(dpy, 0);
964 static void unmapTransientsFor(WWindow *wwin)
966 WWindow *tmp;
968 tmp = wwin->screen_ptr->focused_window;
969 while (tmp) {
970 /* unmap the transients for this transient */
971 if (tmp != wwin && tmp->transient_for == wwin->client_win
972 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
973 unmapTransientsFor(tmp);
974 tmp->flags.miniaturized = 1;
975 if (!tmp->flags.shaded) {
976 wWindowUnmap(tmp);
977 } else {
978 XUnmapWindow(dpy, tmp->frame->core->window);
981 if (!tmp->flags.shaded)
983 wClientSetState(tmp, IconicState, None);
985 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
987 tmp = tmp->prev;
991 static void mapTransientsFor(WWindow *wwin)
993 WWindow *tmp;
995 tmp = wwin->screen_ptr->focused_window;
996 while (tmp) {
997 /* recursively map the transients for this transient */
998 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
999 && tmp->icon == NULL) {
1000 mapTransientsFor(tmp);
1001 tmp->flags.miniaturized = 0;
1002 if (!tmp->flags.shaded) {
1003 wWindowMap(tmp);
1004 } else {
1005 XMapWindow(dpy, tmp->frame->core->window);
1007 tmp->flags.semi_focused = 0;
1009 if (!tmp->flags.shaded)
1011 wClientSetState(tmp, NormalState, None);
1013 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1015 tmp = tmp->prev;
1019 static WWindow *recursiveTransientFor(WWindow * wwin)
1021 int i;
1023 if (!wwin)
1024 return None;
1026 /* hackish way to detect transient_for cycle */
1027 i = wwin->screen_ptr->window_count + 1;
1029 while (wwin && wwin->transient_for != None && i > 0) {
1030 wwin = wWindowFor(wwin->transient_for);
1031 i--;
1033 if (i == 0 && wwin) {
1034 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1035 return NULL;
1038 return wwin;
1041 void wIconifyWindow(WWindow * wwin)
1043 XWindowAttributes attribs;
1044 int present;
1046 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1047 return; /* the window doesn't exist anymore */
1049 if (wwin->flags.miniaturized)
1050 return; /* already miniaturized */
1052 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1053 WWindow *owner = wWindowFor(wwin->transient_for);
1055 if (owner && owner->flags.miniaturized)
1056 return;
1059 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1061 /* if the window is in another workspace, simplify process */
1062 if (present) {
1063 /* icon creation may take a while */
1064 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1065 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1066 GrabModeAsync, None, None, CurrentTime);
1069 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1070 if (!wwin->flags.icon_moved)
1071 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1073 wwin->icon = icon_create_for_wwindow(wwin);
1074 wwin->icon->mapped = 1;
1077 wwin->flags.miniaturized = 1;
1078 wwin->flags.mapped = 0;
1080 /* unmap transients */
1081 unmapTransientsFor(wwin);
1083 if (present) {
1084 XUngrabPointer(dpy, CurrentTime);
1085 wWindowUnmap(wwin);
1086 /* let all Expose events arrive so that we can repaint
1087 * something before the animation starts (and the server is grabbed) */
1088 XSync(dpy, 0);
1090 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1091 wClientSetState(wwin, IconicState, None);
1092 else
1093 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1095 flushExpose();
1096 #ifdef ANIMATIONS
1097 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1098 && !wPreferences.no_animations) {
1099 int ix, iy, iw, ih;
1101 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1102 ix = wwin->icon_x;
1103 iy = wwin->icon_y;
1104 iw = wwin->icon->core->width;
1105 ih = wwin->icon->core->height;
1106 } else {
1107 if (wwin->flags.net_handle_icon) {
1108 ix = wwin->icon_x;
1109 iy = wwin->icon_y;
1110 iw = wwin->icon_w;
1111 ih = wwin->icon_h;
1112 } else {
1113 ix = 0;
1114 iy = 0;
1115 iw = wwin->screen_ptr->scr_width;
1116 ih = wwin->screen_ptr->scr_height;
1119 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1120 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1122 #endif
1125 wwin->flags.skip_next_animation = 0;
1127 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1128 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1129 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1130 XMapWindow(dpy, wwin->icon->core->window);
1132 AddToStackList(wwin->icon->core);
1133 wLowerFrame(wwin->icon->core);
1136 if (present) {
1137 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1140 * It doesn't seem to be working and causes button event hangup
1141 * when deiconifying a transient window.
1142 setupIconGrabs(wwin->icon);
1144 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1145 && wPreferences.focus_mode == WKF_CLICK) {
1146 WWindow *tmp;
1148 tmp = wwin->prev;
1149 while (tmp) {
1150 if (!WFLAGP(tmp, no_focusable)
1151 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1152 && (wwin->frame->workspace == tmp->frame->workspace))
1153 break;
1154 tmp = tmp->prev;
1156 wSetFocusTo(wwin->screen_ptr, tmp);
1157 } else if (wPreferences.focus_mode != WKF_CLICK) {
1158 wSetFocusTo(wwin->screen_ptr, NULL);
1160 #ifdef ANIMATIONS
1161 if (!wwin->screen_ptr->flags.startup) {
1162 /* Catch up with events not processed while animation was running */
1163 Window clientwin = wwin->client_win;
1165 ProcessPendingEvents();
1167 /* the window can disappear while ProcessPendingEvents() runs */
1168 if (!wWindowFor(clientwin)) {
1169 return;
1172 #endif
1175 /* maybe we want to do this regardless of net_handle_icon
1176 * it seems to me we might break behaviour this way.
1178 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1179 && !wwin->flags.net_handle_icon)
1180 wIconSelect(wwin->icon);
1182 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1184 if (wPreferences.auto_arrange_icons)
1185 wArrangeIcons(wwin->screen_ptr, True);
1188 void wDeiconifyWindow(WWindow *wwin)
1190 /* Let's avoid changing workspace while deiconifying */
1191 ignore_wks_change = 1;
1193 /* we're hiding for show_desktop */
1194 int netwm_hidden = wwin->flags.net_show_desktop &&
1195 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1197 if (!netwm_hidden)
1198 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1200 if (!wwin->flags.miniaturized)
1201 return;
1203 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1204 WWindow *owner = recursiveTransientFor(wwin);
1206 if (owner && owner->flags.miniaturized) {
1207 wDeiconifyWindow(owner);
1208 wSetFocusTo(wwin->screen_ptr, wwin);
1209 wRaiseFrame(wwin->frame->core);
1210 return;
1214 wwin->flags.miniaturized = 0;
1216 if (!netwm_hidden && !wwin->flags.shaded)
1217 wwin->flags.mapped = 1;
1219 if (!netwm_hidden || wPreferences.sticky_icons) {
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 (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1224 && wwin->icon != NULL) {
1225 if (wwin->icon->selected)
1226 wIconSelect(wwin->icon);
1228 XUnmapWindow(dpy, wwin->icon->core->window);
1232 /* if the window is in another workspace, do it silently */
1233 if (!netwm_hidden) {
1234 #ifdef ANIMATIONS
1235 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1236 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1237 int ix, iy, iw, ih;
1239 if (!wPreferences.disable_miniwindows
1240 && !wwin->flags.net_handle_icon) {
1241 ix = wwin->icon_x;
1242 iy = wwin->icon_y;
1243 iw = wwin->icon->core->width;
1244 ih = wwin->icon->core->height;
1245 } else {
1246 if (wwin->flags.net_handle_icon) {
1247 ix = wwin->icon_x;
1248 iy = wwin->icon_y;
1249 iw = wwin->icon_w;
1250 ih = wwin->icon_h;
1251 } else {
1252 ix = 0;
1253 iy = 0;
1254 iw = wwin->screen_ptr->scr_width;
1255 ih = wwin->screen_ptr->scr_height;
1258 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1259 wwin->frame_x, wwin->frame_y,
1260 wwin->frame->core->width, wwin->frame->core->height);
1262 #endif /* ANIMATIONS */
1263 wwin->flags.skip_next_animation = 0;
1264 XGrabServer(dpy);
1265 if (!wwin->flags.shaded)
1266 XMapWindow(dpy, wwin->client_win);
1268 XMapWindow(dpy, wwin->frame->core->window);
1269 wRaiseFrame(wwin->frame->core);
1270 if (!wwin->flags.shaded)
1271 wClientSetState(wwin, NormalState, None);
1273 mapTransientsFor(wwin);
1276 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1277 && !wwin->flags.net_handle_icon) {
1278 RemoveFromStackList(wwin->icon->core);
1279 /* removeIconGrabs(wwin->icon); */
1280 wIconDestroy(wwin->icon);
1281 wwin->icon = NULL;
1284 if (!netwm_hidden) {
1285 XUngrabServer(dpy);
1287 wSetFocusTo(wwin->screen_ptr, wwin);
1289 #ifdef ANIMATIONS
1290 if (!wwin->screen_ptr->flags.startup) {
1291 /* Catch up with events not processed while animation was running */
1292 Window clientwin = wwin->client_win;
1294 ProcessPendingEvents();
1296 /* the window can disappear while ProcessPendingEvents() runs */
1297 if (!wWindowFor(clientwin))
1298 return;
1300 #endif
1303 if (wPreferences.auto_arrange_icons)
1304 wArrangeIcons(wwin->screen_ptr, True);
1306 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1308 /* In case we were shaded and iconified, also unshade */
1309 if (!netwm_hidden)
1310 wUnshadeWindow(wwin);
1312 ignore_wks_change = 0;
1315 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1317 if (wwin->flags.miniaturized) {
1318 if (wwin->icon) {
1319 XUnmapWindow(dpy, wwin->icon->core->window);
1320 wwin->icon->mapped = 0;
1322 wwin->flags.hidden = 1;
1324 WMPostNotificationName(WMNChangedState, wwin, "hide");
1325 return;
1328 if (wwin->flags.inspector_open) {
1329 wHideInspectorForWindow(wwin);
1332 wwin->flags.hidden = 1;
1333 wWindowUnmap(wwin);
1335 wClientSetState(wwin, IconicState, icon->icon_win);
1336 flushExpose();
1338 #ifdef ANIMATIONS
1339 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1340 !wwin->flags.skip_next_animation && animate) {
1341 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1342 wwin->frame->core->width, wwin->frame->core->height,
1343 icon_x, icon_y, icon->core->width, icon->core->height);
1345 #endif
1346 wwin->flags.skip_next_animation = 0;
1348 WMPostNotificationName(WMNChangedState, wwin, "hide");
1351 void wHideAll(WScreen *scr)
1353 WWindow *wwin;
1354 WWindow **windows;
1355 WMenu *menu;
1356 unsigned int wcount = 0;
1357 int i;
1359 if (!scr)
1360 return;
1362 menu = scr->switch_menu;
1364 windows = wmalloc(sizeof(WWindow *));
1366 if (menu != NULL) {
1367 for (i = 0; i < menu->entry_no; i++) {
1368 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1369 wcount++;
1370 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1372 } else {
1373 wwin = scr->focused_window;
1375 while (wwin) {
1376 windows[wcount] = wwin;
1377 wcount++;
1378 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1379 wwin = wwin->prev;
1384 for (i = 0; i < wcount; i++) {
1385 wwin = windows[i];
1386 if (wwin->frame->workspace == scr->current_workspace
1387 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1388 && !wwin->flags.internal_window
1389 && !WFLAGP(wwin, no_miniaturizable)
1391 wwin->flags.skip_next_animation = 1;
1392 wIconifyWindow(wwin);
1396 wfree(windows);
1399 void wHideOtherApplications(WWindow *awin)
1401 WWindow *wwin;
1402 WApplication *tapp;
1404 if (!awin)
1405 return;
1406 wwin = awin->screen_ptr->focused_window;
1408 while (wwin) {
1409 if (wwin != awin
1410 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1411 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1412 && !wwin->flags.internal_window
1413 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1415 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1416 if (!WFLAGP(wwin, no_miniaturizable)) {
1417 wwin->flags.skip_next_animation = 1;
1418 wIconifyWindow(wwin);
1420 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1421 tapp = wApplicationOf(wwin->main_window);
1422 if (tapp) {
1423 tapp->flags.skip_next_animation = 1;
1424 wHideApplication(tapp);
1425 } else {
1426 if (!WFLAGP(wwin, no_miniaturizable)) {
1427 wwin->flags.skip_next_animation = 1;
1428 wIconifyWindow(wwin);
1433 wwin = wwin->prev;
1436 wSetFocusTo(awin->screen_ptr, awin);
1440 void wHideApplication(WApplication *wapp)
1442 WScreen *scr;
1443 WWindow *wlist;
1444 int hadfocus;
1445 int animate;
1447 if (!wapp) {
1448 wwarning("trying to hide a non grouped window");
1449 return;
1451 if (!wapp->main_window_desc) {
1452 wwarning("group leader not found for window group");
1453 return;
1455 scr = wapp->main_window_desc->screen_ptr;
1456 hadfocus = 0;
1457 wlist = scr->focused_window;
1458 if (!wlist)
1459 return;
1461 if (wlist->main_window == wapp->main_window)
1462 wapp->last_focused = wlist;
1463 else
1464 wapp->last_focused = NULL;
1466 animate = !wapp->flags.skip_next_animation;
1468 while (wlist) {
1469 if (wlist->main_window == wapp->main_window) {
1470 if (wlist->flags.focused) {
1471 hadfocus = 1;
1473 if (wapp->app_icon) {
1474 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1475 wapp->app_icon->y_pos, wlist, animate);
1476 animate = False;
1479 wlist = wlist->prev;
1482 wapp->flags.skip_next_animation = 0;
1484 if (hadfocus) {
1485 if (wPreferences.focus_mode == WKF_CLICK) {
1486 wlist = scr->focused_window;
1487 while (wlist) {
1488 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1489 && (wlist->flags.mapped || wlist->flags.shaded))
1490 break;
1491 wlist = wlist->prev;
1493 wSetFocusTo(scr, wlist);
1494 } else {
1495 wSetFocusTo(scr, NULL);
1499 wapp->flags.hidden = 1;
1501 if (wPreferences.auto_arrange_icons)
1502 wArrangeIcons(scr, True);
1504 #ifdef HIDDENDOT
1505 if (wapp->app_icon)
1506 wAppIconPaint(wapp->app_icon);
1507 #endif
1510 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1512 if (bringToCurrentWS)
1513 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1515 wwin->flags.hidden = 0;
1517 #ifdef ANIMATIONS
1518 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1519 animateResize(wwin->screen_ptr, icon_x, icon_y,
1520 icon->core->width, icon->core->height,
1521 wwin->frame_x, wwin->frame_y,
1522 wwin->frame->core->width, wwin->frame->core->height);
1524 #endif
1525 wwin->flags.skip_next_animation = 0;
1526 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1527 XMapWindow(dpy, wwin->client_win);
1528 XMapWindow(dpy, wwin->frame->core->window);
1529 wClientSetState(wwin, NormalState, None);
1530 wwin->flags.mapped = 1;
1531 wRaiseFrame(wwin->frame->core);
1533 if (wwin->flags.inspector_open) {
1534 wUnhideInspectorForWindow(wwin);
1537 WMPostNotificationName(WMNChangedState, wwin, "hide");
1540 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1542 WScreen *scr;
1543 WWindow *wlist, *next;
1544 WWindow *focused = NULL;
1545 int animate;
1547 if (!wapp)
1548 return;
1550 scr = wapp->main_window_desc->screen_ptr;
1551 wlist = scr->focused_window;
1552 if (!wlist)
1553 return;
1555 /* goto beginning of list */
1556 while (wlist->prev)
1557 wlist = wlist->prev;
1559 animate = !wapp->flags.skip_next_animation;
1561 while (wlist) {
1562 next = wlist->next;
1564 if (wlist->main_window == wapp->main_window) {
1565 if (wlist->flags.focused)
1566 focused = wlist;
1567 else if (!focused || !focused->flags.focused)
1568 focused = wlist;
1570 if (wlist->flags.miniaturized) {
1571 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1572 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1573 if (!wlist->icon->mapped) {
1574 int x, y;
1576 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1577 if (wlist->icon_x != x || wlist->icon_y != y) {
1578 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1580 wlist->icon_x = x;
1581 wlist->icon_y = y;
1582 XMapWindow(dpy, wlist->icon->core->window);
1583 wlist->icon->mapped = 1;
1585 wRaiseFrame(wlist->icon->core);
1587 if (bringToCurrentWS)
1588 wWindowChangeWorkspace(wlist, scr->current_workspace);
1589 wlist->flags.hidden = 0;
1590 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1591 wDeiconifyWindow(wlist);
1593 WMPostNotificationName(WMNChangedState, wlist, "hide");
1594 } else if (wlist->flags.shaded) {
1595 if (bringToCurrentWS)
1596 wWindowChangeWorkspace(wlist, scr->current_workspace);
1597 wlist->flags.hidden = 0;
1598 wRaiseFrame(wlist->frame->core);
1599 if (wlist->frame->workspace == scr->current_workspace) {
1600 XMapWindow(dpy, wlist->frame->core->window);
1601 if (miniwindows) {
1602 wUnshadeWindow(wlist);
1605 WMPostNotificationName(WMNChangedState, wlist, "hide");
1606 } else if (wlist->flags.hidden) {
1607 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1608 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1609 animate = False;
1610 } else {
1611 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1612 wWindowChangeWorkspace(wlist, scr->current_workspace);
1614 wRaiseFrame(wlist->frame->core);
1617 wlist = next;
1620 wapp->flags.skip_next_animation = 0;
1621 wapp->flags.hidden = 0;
1623 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1624 wRaiseFrame(wapp->last_focused->frame->core);
1625 wSetFocusTo(scr, wapp->last_focused);
1626 } else if (focused) {
1627 wSetFocusTo(scr, focused);
1629 wapp->last_focused = NULL;
1630 if (wPreferences.auto_arrange_icons)
1631 wArrangeIcons(scr, True);
1633 #ifdef HIDDENDOT
1634 wAppIconPaint(wapp->app_icon);
1635 #endif
1638 void wShowAllWindows(WScreen *scr)
1640 WWindow *wwin, *old_foc;
1641 WApplication *wapp;
1643 old_foc = wwin = scr->focused_window;
1644 while (wwin) {
1645 if (!wwin->flags.internal_window &&
1646 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1647 if (wwin->flags.miniaturized) {
1648 wwin->flags.skip_next_animation = 1;
1649 wDeiconifyWindow(wwin);
1650 } else if (wwin->flags.hidden) {
1651 wapp = wApplicationOf(wwin->main_window);
1652 if (wapp) {
1653 wUnhideApplication(wapp, False, False);
1654 } else {
1655 wwin->flags.skip_next_animation = 1;
1656 wDeiconifyWindow(wwin);
1660 wwin = wwin->prev;
1662 wSetFocusTo(scr, old_foc);
1663 /*wRaiseFrame(old_foc->frame->core); */
1666 void wRefreshDesktop(WScreen *scr)
1668 Window win;
1669 XSetWindowAttributes attr;
1671 attr.backing_store = NotUseful;
1672 attr.save_under = False;
1673 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1674 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1675 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1676 XMapRaised(dpy, win);
1677 XDestroyWindow(dpy, win);
1678 XFlush(dpy);
1681 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1683 WWindow *wwin;
1684 WAppIcon *aicon;
1686 int head;
1687 const int heads = wXineramaHeads(scr);
1689 struct HeadVars {
1690 int pf; /* primary axis */
1691 int sf; /* secondary axis */
1692 int fullW;
1693 int fullH;
1694 int pi, si;
1695 int sx1, sx2, sy1, sy2; /* screen boundary */
1696 int sw, sh;
1697 int xo, yo;
1698 int xs, ys;
1699 } *vars;
1701 int isize = wPreferences.icon_size;
1703 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1705 for (head = 0; head < heads; ++head) {
1706 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1707 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1708 vars[head].pi = vars[head].si = 0;
1709 vars[head].sx1 = rect.pos.x;
1710 vars[head].sy1 = rect.pos.y;
1711 vars[head].sw = rect.size.width;
1712 vars[head].sh = rect.size.height;
1713 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1714 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1715 vars[head].sw = isize * (vars[head].sw / isize);
1716 vars[head].sh = isize * (vars[head].sh / isize);
1717 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1718 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1720 /* icon yard boundaries */
1721 if (wPreferences.icon_yard & IY_VERT) {
1722 vars[head].pf = vars[head].fullH;
1723 vars[head].sf = vars[head].fullW;
1724 } else {
1725 vars[head].pf = vars[head].fullW;
1726 vars[head].sf = vars[head].fullH;
1728 if (wPreferences.icon_yard & IY_RIGHT) {
1729 vars[head].xo = vars[head].sx2 - isize;
1730 vars[head].xs = -1;
1731 } else {
1732 vars[head].xo = vars[head].sx1;
1733 vars[head].xs = 1;
1735 if (wPreferences.icon_yard & IY_TOP) {
1736 vars[head].yo = vars[head].sy1;
1737 vars[head].ys = 1;
1738 } else {
1739 vars[head].yo = vars[head].sy2 - isize;
1740 vars[head].ys = -1;
1744 #define X ((wPreferences.icon_yard & IY_VERT) \
1745 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1746 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1748 #define Y ((wPreferences.icon_yard & IY_VERT) \
1749 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1750 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1752 /* arrange application icons */
1753 aicon = scr->app_icon_list;
1754 /* reverse them to avoid unnecessarily sliding of icons */
1755 while (aicon && aicon->next)
1756 aicon = aicon->next;
1758 while (aicon) {
1759 if (!aicon->docked) {
1760 /* CHECK: can icon be NULL here ? */
1761 /* The intention here is to place the AppIcon on the head that
1762 * contains most of the applications _main_ window. */
1763 head = wGetHeadForWindow(aicon->icon->owner);
1765 if (aicon->x_pos != X || aicon->y_pos != Y) {
1766 #ifdef ANIMATIONS
1767 if (!wPreferences.no_animations)
1768 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1769 #endif /* ANIMATIONS */
1771 wAppIconMove(aicon, X, Y);
1772 vars[head].pi++;
1773 if (vars[head].pi >= vars[head].pf) {
1774 vars[head].pi = 0;
1775 vars[head].si++;
1778 aicon = aicon->prev;
1781 /* arrange miniwindows */
1782 wwin = scr->focused_window;
1783 /* reverse them to avoid unnecessarily shuffling */
1784 while (wwin && wwin->prev)
1785 wwin = wwin->prev;
1787 while (wwin) {
1788 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1789 (wwin->frame->workspace == scr->current_workspace ||
1790 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1792 head = wGetHeadForWindow(wwin);
1794 if (arrangeAll || !wwin->flags.icon_moved) {
1795 if (wwin->icon_x != X || wwin->icon_y != Y)
1796 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1798 wwin->icon_x = X;
1799 wwin->icon_y = Y;
1801 vars[head].pi++;
1802 if (vars[head].pi >= vars[head].pf) {
1803 vars[head].pi = 0;
1804 vars[head].si++;
1808 if (arrangeAll) {
1809 wwin->flags.icon_moved = 0;
1811 /* we reversed the order, so we use next */
1812 wwin = wwin->next;
1815 wfree(vars);
1818 void wSelectWindow(WWindow *wwin, Bool flag)
1820 WScreen *scr = wwin->screen_ptr;
1822 if (flag) {
1823 wwin->flags.selected = 1;
1824 if (wwin->frame->selected_border_pixel)
1825 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1826 else
1827 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1829 if (!HAS_BORDER(wwin)) {
1830 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1833 if (!scr->selected_windows)
1834 scr->selected_windows = WMCreateArray(4);
1835 WMAddToArray(scr->selected_windows, wwin);
1836 } else {
1837 wwin->flags.selected = 0;
1838 if (wwin->frame->border_pixel)
1839 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1840 else
1841 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1843 if (!HAS_BORDER(wwin)) {
1844 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1847 if (scr->selected_windows) {
1848 WMRemoveFromArray(scr->selected_windows, wwin);
1853 void wMakeWindowVisible(WWindow *wwin)
1855 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1856 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1858 if (wwin->flags.shaded) {
1859 wUnshadeWindow(wwin);
1861 if (wwin->flags.hidden) {
1862 WApplication *app;
1864 app = wApplicationOf(wwin->main_window);
1865 if (app) {
1866 /* trick to get focus to this window */
1867 app->last_focused = wwin;
1868 wUnhideApplication(app, False, False);
1871 if (wwin->flags.miniaturized) {
1872 wDeiconifyWindow(wwin);
1873 } else {
1874 if (!WFLAGP(wwin, no_focusable))
1875 wSetFocusTo(wwin->screen_ptr, wwin);
1876 wRaiseFrame(wwin->frame->core);
1881 * Do the animation while shading (called with what = SHADE)
1882 * or unshading (what = UNSHADE).
1884 #ifdef ANIMATIONS
1885 static void shade_animate(WWindow *wwin, Bool what)
1887 int y, s, w, h;
1888 time_t time0 = time(NULL);
1890 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1891 return;
1893 switch(what) {
1894 case SHADE:
1895 if (!wwin->screen_ptr->flags.startup) {
1896 /* do the shading animation */
1897 h = wwin->frame->core->height;
1898 s = h / SHADE_STEPS;
1899 if (s < 1)
1900 s = 1;
1901 w = wwin->frame->core->width;
1902 y = wwin->frame->top_width;
1903 while (h > wwin->frame->top_width + 1) {
1904 XMoveWindow(dpy, wwin->client_win, 0, y);
1905 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1906 XFlush(dpy);
1908 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1909 break;
1911 if (SHADE_DELAY > 0) {
1912 wusleep(SHADE_DELAY * 1000L);
1913 } else {
1914 wusleep(10);
1916 h -= s;
1917 y -= s;
1919 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1921 break;
1923 case UNSHADE:
1924 h = wwin->frame->top_width + wwin->frame->bottom_width;
1925 y = wwin->frame->top_width - wwin->client.height;
1926 s = abs(y) / SHADE_STEPS;
1927 if (s < 1)
1928 s = 1;
1929 w = wwin->frame->core->width;
1930 XMoveWindow(dpy, wwin->client_win, 0, y);
1931 if (s > 0) {
1932 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1933 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1934 XMoveWindow(dpy, wwin->client_win, 0, y);
1935 XFlush(dpy);
1936 if (SHADE_DELAY > 0) {
1937 wusleep(SHADE_DELAY * 2000L / 3);
1938 } else {
1939 wusleep(10);
1941 h += s;
1942 y += s;
1944 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1945 break;
1948 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1949 break;
1952 #endif