simplified logic of wMaximizeWindow function
[wmaker-crm.git] / src / actions.c
bloba7cc3cad73b3260c9511b70b70f5b8440078ad6a
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;
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_MAXIMUS);
391 if (WFLAGP(wwin, full_maximize)) {
392 usableArea = totalArea;
394 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
396 if (wwin->flags.shaded) {
397 wwin->flags.skip_next_animation = 1;
398 wUnshadeWindow(wwin);
401 if (directions & MAX_MAXIMUS) {
402 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
403 new_width = maximus_width - adj_size;
404 new_height = maximus_height - adj_size;
405 new_x = maximus_x;
406 new_y = maximus_y;
407 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
408 new_height += wwin->frame->bottom_width - 1;
409 new_y -= wwin->frame->top_width;
411 /* HACK: this will be subtracted again below */
412 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
414 wwin->maximus_x = new_x;
415 wwin->maximus_y = new_y;
416 wwin->flags.old_maximized |= MAX_MAXIMUS;
417 } else {
418 /* set default values if no option set then */
419 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
420 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
421 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
423 if (!(directions & (MAX_VERTICAL | MAX_MAXIMUS))) {
424 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
425 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
428 /* left|right position */
429 if (directions & MAX_LEFTHALF) {
430 new_width = half_scr_width - adj_size;
431 new_x = usableArea.x1;
432 } else if (directions & MAX_RIGHTHALF) {
433 new_width = half_scr_width - adj_size;
434 new_x = usableArea.x1 + half_scr_width;
437 /* vertical|horizontal position */
438 if (directions & MAX_HORIZONTAL) {
439 new_width = usableArea.x2 - usableArea.x1 - adj_size;
440 new_x = usableArea.x1;
442 if (directions & MAX_VERTICAL) {
443 new_height = usableArea.y2 - usableArea.y1 - adj_size;
444 new_y = usableArea.y1;
445 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
446 new_y -= wwin->frame->top_width;
450 if (!WFLAGP(wwin, full_maximize))
451 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
453 /* set maximization state */
454 wwin->flags.maximized = directions;
455 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
456 wwin->flags.maximized = MAX_MAXIMUS;
458 wWindowConstrainSize(wwin, &new_width, &new_height);
460 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
461 usableArea.y2 - usableArea.y1, &new_width, &new_height);
463 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
464 wWindowSynthConfigureNotify(wwin);
466 WMPostNotificationName(WMNChangedState, wwin, "maximize");
469 /* generic (un)maximizer */
470 void handleMaximize(WWindow *wwin, int directions)
472 int current = wwin->flags.maximized;
473 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
474 int effective = requested ^ current;
475 int flags = directions & ~requested;
477 if (!effective) {
478 /* allow wMaximizeWindow to restore the Maximusized size */
479 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
480 !(requested & MAX_MAXIMUS))
481 wMaximizeWindow(wwin, flags);
482 else
483 wUnmaximizeWindow(wwin);
484 /* these alone mean vertical toggle */
485 } else if ((effective == MAX_LEFTHALF) ||
486 (effective == MAX_RIGHTHALF))
487 wUnmaximizeWindow(wwin);
488 else {
489 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
490 (requested == MAX_MAXIMUS))
491 effective = requested;
492 else {
493 if (requested & MAX_LEFTHALF) {
494 effective |= MAX_VERTICAL;
495 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
496 } else if (requested & MAX_RIGHTHALF) {
497 effective |= MAX_VERTICAL;
498 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
500 effective &= ~MAX_MAXIMUS;
502 wMaximizeWindow(wwin, effective | flags);
506 /* the window boundary coordinates */
507 typedef struct {
508 int left;
509 int right;
510 int bottom;
511 int top;
512 int width;
513 int height;
514 } win_coords;
516 static void set_window_coords(WWindow *wwin, win_coords *obs)
518 obs->left = wwin->frame_x;
519 obs->top = wwin->frame_y;
520 obs->width = wwin->frame->core->width;
521 obs->height = wwin->frame->core->height;
522 obs->bottom = obs->top + obs->height;
523 obs->right = obs->left + obs->width;
527 * Maximus: tiled maximization (maximize without overlapping other windows)
529 * The original window 'orig' will be maximized to new coordinates 'new'.
530 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
532 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
533 unsigned int *new_width, unsigned int *new_height)
535 WWindow *tmp;
536 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
537 short int adjust_height;
538 int x_intsect, y_intsect;
539 /* the obstructing, original and new windows */
540 win_coords obs, orig, new;
542 /* set the original coordinate positions of the window to be Maximumized */
543 if (wwin->flags.maximized) {
544 /* window is already maximized; consider original geometry */
545 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
546 orig.bottom = orig.top + orig.height;
547 orig.right = orig.left + orig.width;
549 else
550 set_window_coords(wwin, &orig);
552 /* Try to fully maximize first, then readjust later */
553 new.left = usableArea.x1;
554 new.right = usableArea.x2;
555 new.top = usableArea.y1;
556 new.bottom = usableArea.y2;
558 if (HAS_TITLEBAR(wwin))
559 tbar_height_0 = TITLEBAR_HEIGHT;
560 if (HAS_RESIZEBAR(wwin))
561 rbar_height_0 = RESIZEBAR_HEIGHT;
562 if (HAS_BORDER(wwin))
563 bd_width_0 = wwin->screen_ptr->frame_border_width;
565 /* the length to be subtracted if the window has titlebar, etc */
566 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
568 tmp = wwin;
569 /* The focused window is always the last in the list */
570 while (tmp->prev) {
571 /* ignore windows in other workspaces etc */
572 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
573 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
574 tmp = tmp->prev;
575 continue;
577 tmp = tmp->prev;
579 /* Set the coordinates of obstructing window */
580 set_window_coords(tmp, &obs);
582 /* Try to maximize in the y direction first */
583 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
584 if (x_intsect != 0) {
585 /* TODO: Consider the case when coords are equal */
586 if (obs.bottom < orig.top && obs.bottom > new.top) {
587 /* w_0 is below the bottom of w_j */
588 new.top = obs.bottom + 1;
590 if (orig.bottom < obs.top && obs.top < new.bottom) {
591 /* The bottom of w_0 is above the top of w_j */
592 new.bottom = obs.top - 1;
597 tmp = wwin;
598 while (tmp->prev) {
599 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
600 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
601 tmp = tmp->prev;
602 continue;
604 tmp = tmp->prev;
606 set_window_coords(tmp, &obs);
609 * Use the new.top and new.height instead of original values
610 * as they may have different intersections with the obstructing windows
612 new.height = new.bottom - new.top - adjust_height;
613 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
614 if (y_intsect != 0) {
615 if (obs.right < orig.left && obs.right > new.left) {
616 /* w_0 is completely to the right of w_j */
617 new.left = obs.right + 1;
619 if (orig.right < obs.left && obs.left < new.right) {
620 /* w_0 is completely to the left of w_j */
621 new.right = obs.left - 1;
626 *new_x = new.left;
627 *new_y = new.top;
628 /* xcalc needs -7 here, but other apps don't */
629 *new_height = new.bottom - new.top - adjust_height - 1;;
630 *new_width = new.right - new.left;
633 void wUnmaximizeWindow(WWindow *wwin)
635 int x, y, w, h;
637 if (!wwin->flags.maximized)
638 return;
640 if (wwin->flags.shaded) {
641 wwin->flags.skip_next_animation = 1;
642 wUnshadeWindow(wwin);
645 /* Use old coordinates if they are set, current values otherwise */
646 remember_geometry(wwin, &x, &y, &w, &h);
648 /* unMaximusize relative to original position */
649 if (wwin->flags.maximized & MAX_MAXIMUS) {
650 x += wwin->frame_x - wwin->maximus_x;
651 y += wwin->frame_y - wwin->maximus_y;
654 wwin->flags.maximized = 0;
655 wwin->flags.old_maximized = 0;
656 wWindowConfigure(wwin, x, y, w, h);
657 wWindowSynthConfigureNotify(wwin);
659 WMPostNotificationName(WMNChangedState, wwin, "maximize");
662 void wFullscreenWindow(WWindow *wwin)
664 int head;
665 WMRect rect;
667 if (wwin->flags.fullscreen)
668 return;
670 wwin->flags.fullscreen = True;
672 wWindowConfigureBorders(wwin);
674 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
676 wwin->bfs_geometry.x = wwin->frame_x;
677 wwin->bfs_geometry.y = wwin->frame_y;
678 wwin->bfs_geometry.width = wwin->frame->core->width;
679 wwin->bfs_geometry.height = wwin->frame->core->height;
681 head = wGetHeadForWindow(wwin);
682 rect = wGetRectForHead(wwin->screen_ptr, head);
683 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
685 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
688 void wUnfullscreenWindow(WWindow *wwin)
690 if (!wwin->flags.fullscreen)
691 return;
693 wwin->flags.fullscreen = False;
695 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
696 if (WFLAGP(wwin, sunken)) {
697 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
698 } else if (WFLAGP(wwin, floating)) {
699 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
700 } else {
701 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
705 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
706 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
708 wWindowConfigureBorders(wwin);
710 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
711 wFrameWindowPaint(wwin->frame);
714 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
717 #ifdef ANIMATIONS
718 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
720 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
721 float cx, cy, cw, ch;
722 float xstep, ystep, wstep, hstep;
723 XPoint points[5];
724 float dx, dch, midy;
725 float angle, final_angle, delta;
727 xstep = (float)(fx - x) / steps;
728 ystep = (float)(fy - y) / steps;
729 wstep = (float)(fw - w) / steps;
730 hstep = (float)(fh - h) / steps;
732 cx = (float)x;
733 cy = (float)y;
734 cw = (float)w;
735 ch = (float)h;
737 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
738 delta = (float)(final_angle / FRAMES);
739 for (angle = 0;; angle += delta) {
740 if (angle > final_angle)
741 angle = final_angle;
743 dx = (cw / 10) - ((cw / 5) * sin(angle));
744 dch = (ch / 2) * cos(angle);
745 midy = cy + (ch / 2);
747 points[0].x = cx + dx;
748 points[0].y = midy - dch;
749 points[1].x = cx + cw - dx;
750 points[1].y = points[0].y;
751 points[2].x = cx + cw + dx;
752 points[2].y = midy + dch;
753 points[3].x = cx - dx;
754 points[3].y = points[2].y;
755 points[4].x = points[0].x;
756 points[4].y = points[0].y;
758 XGrabServer(dpy);
759 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
760 XFlush(dpy);
761 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
763 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
764 XUngrabServer(dpy);
765 cx += xstep;
766 cy += ystep;
767 cw += wstep;
768 ch += hstep;
769 if (angle >= final_angle)
770 break;
773 XFlush(dpy);
776 #undef FRAMES
778 static void
779 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
781 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
782 float cx, cy, cw, ch;
783 float xstep, ystep, wstep, hstep;
784 XPoint points[5];
785 float angle, final_angle, a, d, delta;
787 x += w / 2;
788 y += h / 2;
789 fx += fw / 2;
790 fy += fh / 2;
792 xstep = (float)(fx - x) / steps;
793 ystep = (float)(fy - y) / steps;
794 wstep = (float)(fw - w) / steps;
795 hstep = (float)(fh - h) / steps;
797 cx = (float)x;
798 cy = (float)y;
799 cw = (float)w;
800 ch = (float)h;
802 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
803 delta = (float)(final_angle / FRAMES);
804 for (angle = 0;; angle += delta) {
805 if (angle > final_angle)
806 angle = final_angle;
808 a = atan(ch / cw);
809 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
811 points[0].x = cx + cos(angle - a) * d;
812 points[0].y = cy + sin(angle - a) * d;
813 points[1].x = cx + cos(angle + a) * d;
814 points[1].y = cy + sin(angle + a) * d;
815 points[2].x = cx + cos(angle - a + WM_PI) * d;
816 points[2].y = cy + sin(angle - a + WM_PI) * d;
817 points[3].x = cx + cos(angle + a + WM_PI) * d;
818 points[3].y = cy + sin(angle + a + WM_PI) * d;
819 points[4].x = cx + cos(angle - a) * d;
820 points[4].y = cy + sin(angle - a) * d;
821 XGrabServer(dpy);
822 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
823 XFlush(dpy);
824 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
826 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
827 XUngrabServer(dpy);
828 cx += xstep;
829 cy += ystep;
830 cw += wstep;
831 ch += hstep;
832 if (angle >= final_angle)
833 break;
836 XFlush(dpy);
839 #undef FRAMES
841 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
843 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
844 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
845 float xstep, ystep, wstep, hstep;
846 int i, j;
848 xstep = (float)(fx - x) / steps;
849 ystep = (float)(fy - y) / steps;
850 wstep = (float)(fw - w) / steps;
851 hstep = (float)(fh - h) / steps;
853 for (j = 0; j < FRAMES; j++) {
854 cx[j] = (float)x;
855 cy[j] = (float)y;
856 cw[j] = (float)w;
857 ch[j] = (float)h;
859 XGrabServer(dpy);
860 for (i = 0; i < steps; i++) {
861 for (j = 0; j < FRAMES; j++) {
862 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
863 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
865 XFlush(dpy);
866 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
868 for (j = 0; j < FRAMES; j++) {
869 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
870 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
871 if (j < FRAMES - 1) {
872 cx[j] = cx[j + 1];
873 cy[j] = cy[j + 1];
874 cw[j] = cw[j + 1];
875 ch[j] = ch[j + 1];
876 } else {
877 cx[j] += xstep;
878 cy[j] += ystep;
879 cw[j] += wstep;
880 ch[j] += hstep;
885 for (j = 0; j < FRAMES; j++) {
886 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
888 XFlush(dpy);
889 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
891 for (j = 0; j < FRAMES; j++) {
892 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
895 XUngrabServer(dpy);
898 #undef FRAMES
900 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
902 int style = wPreferences.iconification_style; /* Catch the value */
903 int steps;
905 if (style == WIS_NONE)
906 return;
908 if (style == WIS_RANDOM) {
909 style = rand() % 3;
912 switch (style) {
913 case WIS_TWIST:
914 steps = MINIATURIZE_ANIMATION_STEPS_T;
915 if (steps > 0)
916 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
917 break;
918 case WIS_FLIP:
919 steps = MINIATURIZE_ANIMATION_STEPS_F;
920 if (steps > 0)
921 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
922 break;
923 case WIS_ZOOM:
924 default:
925 steps = MINIATURIZE_ANIMATION_STEPS_Z;
926 if (steps > 0)
927 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
928 break;
931 #endif /* ANIMATIONS */
933 static void flushExpose(void)
935 XEvent tmpev;
937 while (XCheckTypedEvent(dpy, Expose, &tmpev))
938 WMHandleEvent(&tmpev);
939 XSync(dpy, 0);
942 static void unmapTransientsFor(WWindow *wwin)
944 WWindow *tmp;
946 tmp = wwin->screen_ptr->focused_window;
947 while (tmp) {
948 /* unmap the transients for this transient */
949 if (tmp != wwin && tmp->transient_for == wwin->client_win
950 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
951 unmapTransientsFor(tmp);
952 tmp->flags.miniaturized = 1;
953 if (!tmp->flags.shaded) {
954 wWindowUnmap(tmp);
955 } else {
956 XUnmapWindow(dpy, tmp->frame->core->window);
959 if (!tmp->flags.shaded)
961 wClientSetState(tmp, IconicState, None);
963 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
965 tmp = tmp->prev;
969 static void mapTransientsFor(WWindow *wwin)
971 WWindow *tmp;
973 tmp = wwin->screen_ptr->focused_window;
974 while (tmp) {
975 /* recursively map the transients for this transient */
976 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
977 && tmp->icon == NULL) {
978 mapTransientsFor(tmp);
979 tmp->flags.miniaturized = 0;
980 if (!tmp->flags.shaded) {
981 wWindowMap(tmp);
982 } else {
983 XMapWindow(dpy, tmp->frame->core->window);
985 tmp->flags.semi_focused = 0;
987 if (!tmp->flags.shaded)
989 wClientSetState(tmp, NormalState, None);
991 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
993 tmp = tmp->prev;
997 static WWindow *recursiveTransientFor(WWindow * wwin)
999 int i;
1001 if (!wwin)
1002 return None;
1004 /* hackish way to detect transient_for cycle */
1005 i = wwin->screen_ptr->window_count + 1;
1007 while (wwin && wwin->transient_for != None && i > 0) {
1008 wwin = wWindowFor(wwin->transient_for);
1009 i--;
1011 if (i == 0 && wwin) {
1012 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1013 return NULL;
1016 return wwin;
1019 void wIconifyWindow(WWindow * wwin)
1021 XWindowAttributes attribs;
1022 int present;
1024 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1025 return; /* the window doesn't exist anymore */
1027 if (wwin->flags.miniaturized)
1028 return; /* already miniaturized */
1030 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1031 WWindow *owner = wWindowFor(wwin->transient_for);
1033 if (owner && owner->flags.miniaturized)
1034 return;
1037 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1039 /* if the window is in another workspace, simplify process */
1040 if (present) {
1041 /* icon creation may take a while */
1042 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1043 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1044 GrabModeAsync, None, None, CurrentTime);
1047 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1048 if (!wwin->flags.icon_moved)
1049 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1051 wwin->icon = icon_create_for_wwindow(wwin);
1052 wwin->icon->mapped = 1;
1055 wwin->flags.miniaturized = 1;
1056 wwin->flags.mapped = 0;
1058 /* unmap transients */
1059 unmapTransientsFor(wwin);
1061 if (present) {
1062 XUngrabPointer(dpy, CurrentTime);
1063 wWindowUnmap(wwin);
1064 /* let all Expose events arrive so that we can repaint
1065 * something before the animation starts (and the server is grabbed) */
1066 XSync(dpy, 0);
1068 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1069 wClientSetState(wwin, IconicState, None);
1070 else
1071 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1073 flushExpose();
1074 #ifdef ANIMATIONS
1075 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1076 && !wPreferences.no_animations) {
1077 int ix, iy, iw, ih;
1079 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1080 ix = wwin->icon_x;
1081 iy = wwin->icon_y;
1082 iw = wwin->icon->core->width;
1083 ih = wwin->icon->core->height;
1084 } else {
1085 if (wwin->flags.net_handle_icon) {
1086 ix = wwin->icon_x;
1087 iy = wwin->icon_y;
1088 iw = wwin->icon_w;
1089 ih = wwin->icon_h;
1090 } else {
1091 ix = 0;
1092 iy = 0;
1093 iw = wwin->screen_ptr->scr_width;
1094 ih = wwin->screen_ptr->scr_height;
1097 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1098 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1100 #endif
1103 wwin->flags.skip_next_animation = 0;
1105 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1106 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1107 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1108 XMapWindow(dpy, wwin->icon->core->window);
1110 AddToStackList(wwin->icon->core);
1111 wLowerFrame(wwin->icon->core);
1114 if (present) {
1115 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1118 * It doesn't seem to be working and causes button event hangup
1119 * when deiconifying a transient window.
1120 setupIconGrabs(wwin->icon);
1122 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1123 && wPreferences.focus_mode == WKF_CLICK) {
1124 WWindow *tmp;
1126 tmp = wwin->prev;
1127 while (tmp) {
1128 if (!WFLAGP(tmp, no_focusable)
1129 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1130 && (wwin->frame->workspace == tmp->frame->workspace))
1131 break;
1132 tmp = tmp->prev;
1134 wSetFocusTo(wwin->screen_ptr, tmp);
1135 } else if (wPreferences.focus_mode != WKF_CLICK) {
1136 wSetFocusTo(wwin->screen_ptr, NULL);
1138 #ifdef ANIMATIONS
1139 if (!wwin->screen_ptr->flags.startup) {
1140 /* Catch up with events not processed while animation was running */
1141 Window clientwin = wwin->client_win;
1143 ProcessPendingEvents();
1145 /* the window can disappear while ProcessPendingEvents() runs */
1146 if (!wWindowFor(clientwin)) {
1147 return;
1150 #endif
1153 /* maybe we want to do this regardless of net_handle_icon
1154 * it seems to me we might break behaviour this way.
1156 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1157 && !wwin->flags.net_handle_icon)
1158 wIconSelect(wwin->icon);
1160 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1162 if (wPreferences.auto_arrange_icons)
1163 wArrangeIcons(wwin->screen_ptr, True);
1166 void wDeiconifyWindow(WWindow *wwin)
1168 /* Let's avoid changing workspace while deiconifying */
1169 ignore_wks_change = 1;
1171 /* we're hiding for show_desktop */
1172 int netwm_hidden = wwin->flags.net_show_desktop &&
1173 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1175 if (!netwm_hidden)
1176 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1178 if (!wwin->flags.miniaturized)
1179 return;
1181 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1182 WWindow *owner = recursiveTransientFor(wwin);
1184 if (owner && owner->flags.miniaturized) {
1185 wDeiconifyWindow(owner);
1186 wSetFocusTo(wwin->screen_ptr, wwin);
1187 wRaiseFrame(wwin->frame->core);
1188 return;
1192 wwin->flags.miniaturized = 0;
1194 if (!netwm_hidden && !wwin->flags.shaded)
1195 wwin->flags.mapped = 1;
1197 if (!netwm_hidden || wPreferences.sticky_icons) {
1198 /* maybe we want to do this regardless of net_handle_icon
1199 * it seems to me we might break behaviour this way.
1201 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1202 && wwin->icon != NULL) {
1203 if (wwin->icon->selected)
1204 wIconSelect(wwin->icon);
1206 XUnmapWindow(dpy, wwin->icon->core->window);
1210 /* if the window is in another workspace, do it silently */
1211 if (!netwm_hidden) {
1212 #ifdef ANIMATIONS
1213 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1214 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1215 int ix, iy, iw, ih;
1217 if (!wPreferences.disable_miniwindows
1218 && !wwin->flags.net_handle_icon) {
1219 ix = wwin->icon_x;
1220 iy = wwin->icon_y;
1221 iw = wwin->icon->core->width;
1222 ih = wwin->icon->core->height;
1223 } else {
1224 if (wwin->flags.net_handle_icon) {
1225 ix = wwin->icon_x;
1226 iy = wwin->icon_y;
1227 iw = wwin->icon_w;
1228 ih = wwin->icon_h;
1229 } else {
1230 ix = 0;
1231 iy = 0;
1232 iw = wwin->screen_ptr->scr_width;
1233 ih = wwin->screen_ptr->scr_height;
1236 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1237 wwin->frame_x, wwin->frame_y,
1238 wwin->frame->core->width, wwin->frame->core->height);
1240 #endif /* ANIMATIONS */
1241 wwin->flags.skip_next_animation = 0;
1242 XGrabServer(dpy);
1243 if (!wwin->flags.shaded)
1244 XMapWindow(dpy, wwin->client_win);
1246 XMapWindow(dpy, wwin->frame->core->window);
1247 wRaiseFrame(wwin->frame->core);
1248 if (!wwin->flags.shaded)
1249 wClientSetState(wwin, NormalState, None);
1251 mapTransientsFor(wwin);
1254 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1255 && !wwin->flags.net_handle_icon) {
1256 RemoveFromStackList(wwin->icon->core);
1257 /* removeIconGrabs(wwin->icon); */
1258 wIconDestroy(wwin->icon);
1259 wwin->icon = NULL;
1262 if (!netwm_hidden) {
1263 XUngrabServer(dpy);
1265 wSetFocusTo(wwin->screen_ptr, wwin);
1267 #ifdef ANIMATIONS
1268 if (!wwin->screen_ptr->flags.startup) {
1269 /* Catch up with events not processed while animation was running */
1270 Window clientwin = wwin->client_win;
1272 ProcessPendingEvents();
1274 /* the window can disappear while ProcessPendingEvents() runs */
1275 if (!wWindowFor(clientwin))
1276 return;
1278 #endif
1281 if (wPreferences.auto_arrange_icons)
1282 wArrangeIcons(wwin->screen_ptr, True);
1284 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1286 /* In case we were shaded and iconified, also unshade */
1287 if (!netwm_hidden)
1288 wUnshadeWindow(wwin);
1290 ignore_wks_change = 0;
1293 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1295 if (wwin->flags.miniaturized) {
1296 if (wwin->icon) {
1297 XUnmapWindow(dpy, wwin->icon->core->window);
1298 wwin->icon->mapped = 0;
1300 wwin->flags.hidden = 1;
1302 WMPostNotificationName(WMNChangedState, wwin, "hide");
1303 return;
1306 if (wwin->flags.inspector_open) {
1307 wHideInspectorForWindow(wwin);
1310 wwin->flags.hidden = 1;
1311 wWindowUnmap(wwin);
1313 wClientSetState(wwin, IconicState, icon->icon_win);
1314 flushExpose();
1316 #ifdef ANIMATIONS
1317 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1318 !wwin->flags.skip_next_animation && animate) {
1319 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1320 wwin->frame->core->width, wwin->frame->core->height,
1321 icon_x, icon_y, icon->core->width, icon->core->height);
1323 #endif
1324 wwin->flags.skip_next_animation = 0;
1326 WMPostNotificationName(WMNChangedState, wwin, "hide");
1329 void wHideAll(WScreen *scr)
1331 WWindow *wwin;
1332 WWindow **windows;
1333 WMenu *menu;
1334 unsigned int wcount = 0;
1335 int i;
1337 if (!scr)
1338 return;
1340 menu = scr->switch_menu;
1342 windows = wmalloc(sizeof(WWindow *));
1344 if (menu != NULL) {
1345 for (i = 0; i < menu->entry_no; i++) {
1346 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1347 wcount++;
1348 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1350 } else {
1351 wwin = scr->focused_window;
1353 while (wwin) {
1354 windows[wcount] = wwin;
1355 wcount++;
1356 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1357 wwin = wwin->prev;
1362 for (i = 0; i < wcount; i++) {
1363 wwin = windows[i];
1364 if (wwin->frame->workspace == scr->current_workspace
1365 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1366 && !wwin->flags.internal_window
1367 && !WFLAGP(wwin, no_miniaturizable)
1369 wwin->flags.skip_next_animation = 1;
1370 wIconifyWindow(wwin);
1374 wfree(windows);
1377 void wHideOtherApplications(WWindow *awin)
1379 WWindow *wwin;
1380 WApplication *tapp;
1382 if (!awin)
1383 return;
1384 wwin = awin->screen_ptr->focused_window;
1386 while (wwin) {
1387 if (wwin != awin
1388 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1389 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1390 && !wwin->flags.internal_window
1391 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1393 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1394 if (!WFLAGP(wwin, no_miniaturizable)) {
1395 wwin->flags.skip_next_animation = 1;
1396 wIconifyWindow(wwin);
1398 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1399 tapp = wApplicationOf(wwin->main_window);
1400 if (tapp) {
1401 tapp->flags.skip_next_animation = 1;
1402 wHideApplication(tapp);
1403 } else {
1404 if (!WFLAGP(wwin, no_miniaturizable)) {
1405 wwin->flags.skip_next_animation = 1;
1406 wIconifyWindow(wwin);
1411 wwin = wwin->prev;
1414 wSetFocusTo(awin->screen_ptr, awin);
1418 void wHideApplication(WApplication *wapp)
1420 WScreen *scr;
1421 WWindow *wlist;
1422 int hadfocus;
1423 int animate;
1425 if (!wapp) {
1426 wwarning("trying to hide a non grouped window");
1427 return;
1429 if (!wapp->main_window_desc) {
1430 wwarning("group leader not found for window group");
1431 return;
1433 scr = wapp->main_window_desc->screen_ptr;
1434 hadfocus = 0;
1435 wlist = scr->focused_window;
1436 if (!wlist)
1437 return;
1439 if (wlist->main_window == wapp->main_window)
1440 wapp->last_focused = wlist;
1441 else
1442 wapp->last_focused = NULL;
1444 animate = !wapp->flags.skip_next_animation;
1446 while (wlist) {
1447 if (wlist->main_window == wapp->main_window) {
1448 if (wlist->flags.focused) {
1449 hadfocus = 1;
1451 if (wapp->app_icon) {
1452 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1453 wapp->app_icon->y_pos, wlist, animate);
1454 animate = False;
1457 wlist = wlist->prev;
1460 wapp->flags.skip_next_animation = 0;
1462 if (hadfocus) {
1463 if (wPreferences.focus_mode == WKF_CLICK) {
1464 wlist = scr->focused_window;
1465 while (wlist) {
1466 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1467 && (wlist->flags.mapped || wlist->flags.shaded))
1468 break;
1469 wlist = wlist->prev;
1471 wSetFocusTo(scr, wlist);
1472 } else {
1473 wSetFocusTo(scr, NULL);
1477 wapp->flags.hidden = 1;
1479 if (wPreferences.auto_arrange_icons)
1480 wArrangeIcons(scr, True);
1482 #ifdef HIDDENDOT
1483 if (wapp->app_icon)
1484 wAppIconPaint(wapp->app_icon);
1485 #endif
1488 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1490 if (bringToCurrentWS)
1491 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1493 wwin->flags.hidden = 0;
1495 #ifdef ANIMATIONS
1496 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1497 animateResize(wwin->screen_ptr, icon_x, icon_y,
1498 icon->core->width, icon->core->height,
1499 wwin->frame_x, wwin->frame_y,
1500 wwin->frame->core->width, wwin->frame->core->height);
1502 #endif
1503 wwin->flags.skip_next_animation = 0;
1504 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1505 XMapWindow(dpy, wwin->client_win);
1506 XMapWindow(dpy, wwin->frame->core->window);
1507 wClientSetState(wwin, NormalState, None);
1508 wwin->flags.mapped = 1;
1509 wRaiseFrame(wwin->frame->core);
1511 if (wwin->flags.inspector_open) {
1512 wUnhideInspectorForWindow(wwin);
1515 WMPostNotificationName(WMNChangedState, wwin, "hide");
1518 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1520 WScreen *scr;
1521 WWindow *wlist, *next;
1522 WWindow *focused = NULL;
1523 int animate;
1525 if (!wapp)
1526 return;
1528 scr = wapp->main_window_desc->screen_ptr;
1529 wlist = scr->focused_window;
1530 if (!wlist)
1531 return;
1533 /* goto beginning of list */
1534 while (wlist->prev)
1535 wlist = wlist->prev;
1537 animate = !wapp->flags.skip_next_animation;
1539 while (wlist) {
1540 next = wlist->next;
1542 if (wlist->main_window == wapp->main_window) {
1543 if (wlist->flags.focused)
1544 focused = wlist;
1545 else if (!focused || !focused->flags.focused)
1546 focused = wlist;
1548 if (wlist->flags.miniaturized) {
1549 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1550 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1551 if (!wlist->icon->mapped) {
1552 int x, y;
1554 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1555 if (wlist->icon_x != x || wlist->icon_y != y) {
1556 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1558 wlist->icon_x = x;
1559 wlist->icon_y = y;
1560 XMapWindow(dpy, wlist->icon->core->window);
1561 wlist->icon->mapped = 1;
1563 wRaiseFrame(wlist->icon->core);
1565 if (bringToCurrentWS)
1566 wWindowChangeWorkspace(wlist, scr->current_workspace);
1567 wlist->flags.hidden = 0;
1568 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1569 wDeiconifyWindow(wlist);
1571 WMPostNotificationName(WMNChangedState, wlist, "hide");
1572 } else if (wlist->flags.shaded) {
1573 if (bringToCurrentWS)
1574 wWindowChangeWorkspace(wlist, scr->current_workspace);
1575 wlist->flags.hidden = 0;
1576 wRaiseFrame(wlist->frame->core);
1577 if (wlist->frame->workspace == scr->current_workspace) {
1578 XMapWindow(dpy, wlist->frame->core->window);
1579 if (miniwindows) {
1580 wUnshadeWindow(wlist);
1583 WMPostNotificationName(WMNChangedState, wlist, "hide");
1584 } else if (wlist->flags.hidden) {
1585 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1586 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1587 animate = False;
1588 } else {
1589 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1590 wWindowChangeWorkspace(wlist, scr->current_workspace);
1592 wRaiseFrame(wlist->frame->core);
1595 wlist = next;
1598 wapp->flags.skip_next_animation = 0;
1599 wapp->flags.hidden = 0;
1601 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1602 wRaiseFrame(wapp->last_focused->frame->core);
1603 wSetFocusTo(scr, wapp->last_focused);
1604 } else if (focused) {
1605 wSetFocusTo(scr, focused);
1607 wapp->last_focused = NULL;
1608 if (wPreferences.auto_arrange_icons)
1609 wArrangeIcons(scr, True);
1611 #ifdef HIDDENDOT
1612 wAppIconPaint(wapp->app_icon);
1613 #endif
1616 void wShowAllWindows(WScreen *scr)
1618 WWindow *wwin, *old_foc;
1619 WApplication *wapp;
1621 old_foc = wwin = scr->focused_window;
1622 while (wwin) {
1623 if (!wwin->flags.internal_window &&
1624 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1625 if (wwin->flags.miniaturized) {
1626 wwin->flags.skip_next_animation = 1;
1627 wDeiconifyWindow(wwin);
1628 } else if (wwin->flags.hidden) {
1629 wapp = wApplicationOf(wwin->main_window);
1630 if (wapp) {
1631 wUnhideApplication(wapp, False, False);
1632 } else {
1633 wwin->flags.skip_next_animation = 1;
1634 wDeiconifyWindow(wwin);
1638 wwin = wwin->prev;
1640 wSetFocusTo(scr, old_foc);
1641 /*wRaiseFrame(old_foc->frame->core); */
1644 void wRefreshDesktop(WScreen *scr)
1646 Window win;
1647 XSetWindowAttributes attr;
1649 attr.backing_store = NotUseful;
1650 attr.save_under = False;
1651 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1652 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1653 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1654 XMapRaised(dpy, win);
1655 XDestroyWindow(dpy, win);
1656 XFlush(dpy);
1659 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1661 WWindow *wwin;
1662 WAppIcon *aicon;
1664 int head;
1665 const int heads = wXineramaHeads(scr);
1667 struct HeadVars {
1668 int pf; /* primary axis */
1669 int sf; /* secondary axis */
1670 int fullW;
1671 int fullH;
1672 int pi, si;
1673 int sx1, sx2, sy1, sy2; /* screen boundary */
1674 int sw, sh;
1675 int xo, yo;
1676 int xs, ys;
1677 } *vars;
1679 int isize = wPreferences.icon_size;
1681 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1683 for (head = 0; head < heads; ++head) {
1684 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1685 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1686 vars[head].pi = vars[head].si = 0;
1687 vars[head].sx1 = rect.pos.x;
1688 vars[head].sy1 = rect.pos.y;
1689 vars[head].sw = rect.size.width;
1690 vars[head].sh = rect.size.height;
1691 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1692 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1693 vars[head].sw = isize * (vars[head].sw / isize);
1694 vars[head].sh = isize * (vars[head].sh / isize);
1695 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1696 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1698 /* icon yard boundaries */
1699 if (wPreferences.icon_yard & IY_VERT) {
1700 vars[head].pf = vars[head].fullH;
1701 vars[head].sf = vars[head].fullW;
1702 } else {
1703 vars[head].pf = vars[head].fullW;
1704 vars[head].sf = vars[head].fullH;
1706 if (wPreferences.icon_yard & IY_RIGHT) {
1707 vars[head].xo = vars[head].sx2 - isize;
1708 vars[head].xs = -1;
1709 } else {
1710 vars[head].xo = vars[head].sx1;
1711 vars[head].xs = 1;
1713 if (wPreferences.icon_yard & IY_TOP) {
1714 vars[head].yo = vars[head].sy1;
1715 vars[head].ys = 1;
1716 } else {
1717 vars[head].yo = vars[head].sy2 - isize;
1718 vars[head].ys = -1;
1722 #define X ((wPreferences.icon_yard & IY_VERT) \
1723 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1724 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1726 #define Y ((wPreferences.icon_yard & IY_VERT) \
1727 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1728 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1730 /* arrange application icons */
1731 aicon = scr->app_icon_list;
1732 /* reverse them to avoid unnecessarily sliding of icons */
1733 while (aicon && aicon->next)
1734 aicon = aicon->next;
1736 while (aicon) {
1737 if (!aicon->docked) {
1738 /* CHECK: can icon be NULL here ? */
1739 /* The intention here is to place the AppIcon on the head that
1740 * contains most of the applications _main_ window. */
1741 head = wGetHeadForWindow(aicon->icon->owner);
1743 if (aicon->x_pos != X || aicon->y_pos != Y) {
1744 #ifdef ANIMATIONS
1745 if (!wPreferences.no_animations)
1746 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1747 #endif /* ANIMATIONS */
1749 wAppIconMove(aicon, X, Y);
1750 vars[head].pi++;
1751 if (vars[head].pi >= vars[head].pf) {
1752 vars[head].pi = 0;
1753 vars[head].si++;
1756 aicon = aicon->prev;
1759 /* arrange miniwindows */
1760 wwin = scr->focused_window;
1761 /* reverse them to avoid unnecessarily shuffling */
1762 while (wwin && wwin->prev)
1763 wwin = wwin->prev;
1765 while (wwin) {
1766 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1767 (wwin->frame->workspace == scr->current_workspace ||
1768 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1770 head = wGetHeadForWindow(wwin);
1772 if (arrangeAll || !wwin->flags.icon_moved) {
1773 if (wwin->icon_x != X || wwin->icon_y != Y)
1774 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1776 wwin->icon_x = X;
1777 wwin->icon_y = Y;
1779 vars[head].pi++;
1780 if (vars[head].pi >= vars[head].pf) {
1781 vars[head].pi = 0;
1782 vars[head].si++;
1786 if (arrangeAll) {
1787 wwin->flags.icon_moved = 0;
1789 /* we reversed the order, so we use next */
1790 wwin = wwin->next;
1793 wfree(vars);
1796 void wSelectWindow(WWindow *wwin, Bool flag)
1798 WScreen *scr = wwin->screen_ptr;
1800 if (flag) {
1801 wwin->flags.selected = 1;
1802 if (wwin->frame->selected_border_pixel)
1803 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1804 else
1805 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1807 if (!HAS_BORDER(wwin)) {
1808 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1811 if (!scr->selected_windows)
1812 scr->selected_windows = WMCreateArray(4);
1813 WMAddToArray(scr->selected_windows, wwin);
1814 } else {
1815 wwin->flags.selected = 0;
1816 if (wwin->frame->border_pixel)
1817 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1818 else
1819 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1821 if (!HAS_BORDER(wwin)) {
1822 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1825 if (scr->selected_windows) {
1826 WMRemoveFromArray(scr->selected_windows, wwin);
1831 void wMakeWindowVisible(WWindow *wwin)
1833 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1834 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1836 if (wwin->flags.shaded) {
1837 wUnshadeWindow(wwin);
1839 if (wwin->flags.hidden) {
1840 WApplication *app;
1842 app = wApplicationOf(wwin->main_window);
1843 if (app) {
1844 /* trick to get focus to this window */
1845 app->last_focused = wwin;
1846 wUnhideApplication(app, False, False);
1849 if (wwin->flags.miniaturized) {
1850 wDeiconifyWindow(wwin);
1851 } else {
1852 if (!WFLAGP(wwin, no_focusable))
1853 wSetFocusTo(wwin->screen_ptr, wwin);
1854 wRaiseFrame(wwin->frame->core);
1859 * Do the animation while shading (called with what = SHADE)
1860 * or unshading (what = UNSHADE).
1862 #ifdef ANIMATIONS
1863 static void shade_animate(WWindow *wwin, Bool what)
1865 int y, s, w, h;
1866 time_t time0 = time(NULL);
1868 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1869 return;
1871 switch(what) {
1872 case SHADE:
1873 if (!wwin->screen_ptr->flags.startup) {
1874 /* do the shading animation */
1875 h = wwin->frame->core->height;
1876 s = h / SHADE_STEPS;
1877 if (s < 1)
1878 s = 1;
1879 w = wwin->frame->core->width;
1880 y = wwin->frame->top_width;
1881 while (h > wwin->frame->top_width + 1) {
1882 XMoveWindow(dpy, wwin->client_win, 0, y);
1883 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1884 XFlush(dpy);
1886 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1887 break;
1889 if (SHADE_DELAY > 0) {
1890 wusleep(SHADE_DELAY * 1000L);
1891 } else {
1892 wusleep(10);
1894 h -= s;
1895 y -= s;
1897 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1899 break;
1901 case UNSHADE:
1902 h = wwin->frame->top_width + wwin->frame->bottom_width;
1903 y = wwin->frame->top_width - wwin->client.height;
1904 s = abs(y) / SHADE_STEPS;
1905 if (s < 1)
1906 s = 1;
1907 w = wwin->frame->core->width;
1908 XMoveWindow(dpy, wwin->client_win, 0, y);
1909 if (s > 0) {
1910 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1911 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1912 XMoveWindow(dpy, wwin->client_win, 0, y);
1913 XFlush(dpy);
1914 if (SHADE_DELAY > 0) {
1915 wusleep(SHADE_DELAY * 2000L / 3);
1916 } else {
1917 wusleep(10);
1919 h += s;
1920 y += s;
1922 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1923 break;
1926 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1927 break;
1930 #endif