Fix Maximize/Unmaximize in window menu
[wmaker-crm.git] / src / actions.c
bloba79de8a0ceb51801351940f31dba3f44f3d6d9a1
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"
50 /****** Global Variables ******/
52 int ignore_wks_change = 0;
53 extern Time LastTimestamp;
54 extern Time LastFocusChange;
55 extern WPreferences wPreferences;
56 extern Atom _XA_WM_TAKE_FOCUS;
57 extern void ProcessPendingEvents();
58 extern int calcIntersectionLength(int p1, int l1, int p2, int l2);
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 && same_head) ? wwin->old_geometry.x : wwin->frame_x;
318 *y = (wwin->old_geometry.y && 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 #define IS_MAX_HORIZONTALLY(directions) ((directions & MAX_HORIZONTAL) | (directions & MAX_LEFTHALF) | (directions & MAX_RIGHTHALF))
345 void wMaximizeWindow(WWindow *wwin, int directions)
347 int new_x, new_y;
348 unsigned int new_width, new_height, half_scr_width;
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 save_directions = 0;
356 int adj_size;
358 if (!IS_RESIZABLE(wwin))
359 return;
361 if (!HAS_BORDER(wwin))
362 has_border = 0;
364 /* the size to adjust the geometry */
365 adj_size = FRAME_BORDER_WIDTH * 2 * has_border;
367 /* save old coordinates before we change the current values
368 * always if the window is not currently maximized at all
369 * but never if the window has been Maximusized */
370 if (!wwin->flags.maximized)
371 save_directions |= SAVE_GEOMETRY_ALL;
372 else if (!(wwin->flags.old_maximized & MAX_MAXIMUS)) {
373 if ((directions & MAX_VERTICAL) &&
374 !(wwin->flags.maximized & MAX_VERTICAL))
375 save_directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
376 if (IS_MAX_HORIZONTALLY(directions) &&
377 !IS_MAX_HORIZONTALLY(wwin->flags.maximized))
378 save_directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
380 if ((directions & MAX_MAXIMUS) && !wwin->flags.maximized)
381 save_directions |= SAVE_GEOMETRY_ALL;
382 save_old_geometry(wwin, save_directions);
384 totalArea.x1 = 0;
385 totalArea.y1 = 0;
386 totalArea.x2 = wwin->screen_ptr->scr_width;
387 totalArea.y2 = wwin->screen_ptr->scr_height;
388 usableArea = totalArea;
390 if (!(directions & MAX_IGNORE_XINERAMA)) {
391 WScreen *scr = wwin->screen_ptr;
392 int head;
394 if (directions & MAX_KEYBOARD)
395 head = wGetHeadForWindow(wwin);
396 else
397 head = wGetHeadForPointerLocation(scr);
399 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
402 /* remember Maximus geometry if we'll need it later */
403 if ((wwin->flags.old_maximized & MAX_MAXIMUS) || (directions & MAX_MAXIMUS))
404 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
406 /* Only save directions, not kbd or xinerama hints */
407 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
409 if (WFLAGP(wwin, full_maximize)) {
410 usableArea = totalArea;
412 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
414 if (wwin->flags.shaded) {
415 wwin->flags.skip_next_animation = 1;
416 wUnshadeWindow(wwin);
419 if (directions & MAX_HORIZONTAL) {
420 new_width = usableArea.x2 - usableArea.x1 - adj_size;
421 new_x = usableArea.x1;
422 } else if (directions & MAX_LEFTHALF) {
423 new_width = half_scr_width - adj_size;
424 new_x = usableArea.x1;
425 wwin->flags.old_maximized |= MAX_LEFTHALF;
426 wwin->flags.old_maximized &= ~MAX_RIGHTHALF;
427 } else if (directions & MAX_RIGHTHALF) {
428 new_width = half_scr_width - adj_size;
429 new_x = usableArea.x1 + half_scr_width;
430 wwin->flags.old_maximized |= MAX_RIGHTHALF;
431 wwin->flags.old_maximized &= ~MAX_LEFTHALF;
432 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
433 new_x = maximus_x;
434 new_width = maximus_width - adj_size;
435 } else if (IS_MAX_HORIZONTALLY(wwin->flags.maximized)) {
436 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
437 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
438 } else {
439 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
440 new_x = wwin->frame_x;
441 new_width = wwin->frame->core->width;
444 if (directions & MAX_VERTICAL) {
445 new_height = usableArea.y2 - usableArea.y1 - adj_size;
446 new_y = usableArea.y1;
447 if (WFLAGP(wwin, full_maximize)) {
448 new_y -= wwin->frame->top_width;
449 new_height += wwin->frame->bottom_width - 1;
451 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
452 new_y = maximus_y;
453 new_height = maximus_height - adj_size;
454 /* HACK: this will be subtracted again below */
455 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
456 } else if (wwin->flags.maximized & MAX_VERTICAL) {
457 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
458 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
459 /* HACK: this will be subtracted again below */
460 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
461 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
462 } else {
463 new_y = wwin->frame_y;
464 new_height = wwin->frame->core->height;
467 if (!WFLAGP(wwin, full_maximize)) {
468 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
471 if (directions & MAX_MAXIMUS) {
472 new_x = maximus_x;
473 new_y = maximus_y;
474 new_width = maximus_width - adj_size;
475 new_height = maximus_height - adj_size;
476 if (WFLAGP(wwin, full_maximize) && new_y == 0) {
477 new_y -= wwin->frame->top_width;
478 new_height += wwin->frame->top_width - 1;
480 wwin->maximus_x = new_x;
481 wwin->maximus_y = new_y;
482 wwin->flags.old_maximized |= MAX_MAXIMUS;
485 wWindowConstrainSize(wwin, &new_width, &new_height);
487 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
488 usableArea.y2 - usableArea.y1, &new_width, &new_height);
490 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
491 wWindowSynthConfigureNotify(wwin);
493 WMPostNotificationName(WMNChangedState, wwin, "maximize");
495 /* set maximization state */
496 wwin->flags.maximized = directions;
497 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
498 wwin->flags.maximized = MAX_MAXIMUS;
501 /* generic (un)maximizer */
502 void handleMaximize(WWindow *wwin, int directions)
504 int current = wwin->flags.maximized;
505 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
506 int effective = requested ^ current;
507 int flags = directions & ~requested;
509 if (!effective) {
510 /* allow wMaximizeWindow to restore the Maximusized size */
511 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
512 !(requested & MAX_MAXIMUS))
513 wMaximizeWindow(wwin, flags);
514 else
515 wUnmaximizeWindow(wwin);
517 else {
518 /* MAX_MAXIMUS takes precedence */
519 effective &= ~MAX_MAXIMUS;
520 if (requested & MAX_MAXIMUS) {
521 /* window was previously Maximusized then maximized */
522 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !current) {
523 wUnmaximizeWindow(wwin);
524 return;
526 else
527 effective = MAX_MAXIMUS;
529 else if (requested == (MAX_HORIZONTAL | MAX_VERTICAL))
530 effective = requested;
531 else {
532 /* handle MAX_HORIZONTAL -> MAX_(LEFT|RIGHT)HALF */
533 if (IS_MAX_HORIZONTALLY(current)) {
534 if (IS_MAX_HORIZONTALLY(requested)) {
535 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF);
536 effective |= (requested & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF));
537 if (requested & MAX_HORIZONTAL) {
538 /* restore to half maximization */
539 if (wwin->flags.old_maximized & MAX_LEFTHALF)
540 effective |= MAX_LEFTHALF;
541 else if (wwin->flags.old_maximized & MAX_RIGHTHALF)
542 effective |= MAX_RIGHTHALF;
544 /* MAX_VERTICAL is implicit with MAX_(LEFT|RIGHT)HALF */
545 else
546 effective |= MAX_VERTICAL;
547 } else {
548 /* toggling MAX_VERTICAL */
549 if ((requested & MAX_VERTICAL) &&
550 (current & MAX_VERTICAL)) {
551 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF | MAX_VERTICAL);
555 /* handle MAX_VERTICAL -> MAX_(LEFT|RIGHT)HALF */
556 if (current & MAX_VERTICAL) {
557 if ((requested & MAX_LEFTHALF) ||
558 (requested & MAX_RIGHTHALF)) {
559 effective |= MAX_VERTICAL;
562 /* toggling MAX_HORIZONTAL */
563 if ((requested & MAX_HORIZONTAL) &&
564 (current & MAX_HORIZONTAL))
565 effective &= ~MAX_HORIZONTAL;
567 wMaximizeWindow(wwin, effective | flags);
569 return;
572 /* the window boundary coordinates */
573 typedef struct {
574 int left;
575 int right;
576 int bottom;
577 int top;
578 int width;
579 int height;
580 } win_coords;
582 static void set_window_coords(WWindow *wwin, win_coords *obs)
584 obs->left = wwin->frame_x;
585 obs->top = wwin->frame_y;
586 obs->width = wwin->frame->core->width;
587 obs->height = wwin->frame->core->height;
588 obs->bottom = obs->top + obs->height;
589 obs->right = obs->left + obs->width;
593 * Maximus: tiled maximization (maximize without overlapping other windows)
595 * The original window 'orig' will be maximized to new coordinates 'new'.
596 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
598 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
599 unsigned int *new_width, unsigned int *new_height)
601 WWindow *tmp;
602 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
603 short int adjust_height;
604 int x_intsect, y_intsect;
605 /* the obstructing, original and new windows */
606 win_coords obs, orig, new;
608 /* set the original coordinate positions of the window to be Maximumized */
609 if (wwin->flags.maximized) {
610 /* window is already maximized; consider original geometry */
611 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
612 orig.bottom = orig.top + orig.height;
613 orig.right = orig.left + orig.width;
615 else
616 set_window_coords(wwin, &orig);
618 /* Try to fully maximize first, then readjust later */
619 new.left = usableArea.x1;
620 new.right = usableArea.x2;
621 new.top = usableArea.y1;
622 new.bottom = usableArea.y2;
624 if (HAS_TITLEBAR(wwin))
625 tbar_height_0 = TITLEBAR_HEIGHT;
626 if (HAS_RESIZEBAR(wwin))
627 rbar_height_0 = RESIZEBAR_HEIGHT;
628 if (HAS_BORDER(wwin))
629 bd_width_0 = FRAME_BORDER_WIDTH;
631 /* the length to be subtracted if the window has titlebar, etc */
632 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
634 tmp = wwin;
635 /* The focused window is always the last in the list */
636 while (tmp->prev) {
637 /* ignore windows in other workspaces etc */
638 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
639 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
640 tmp = tmp->prev;
641 continue;
643 tmp = tmp->prev;
645 /* Set the coordinates of obstructing window */
646 set_window_coords(tmp, &obs);
648 /* Try to maximize in the y direction first */
649 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
650 if (x_intsect != 0) {
651 /* TODO: Consider the case when coords are equal */
652 if (obs.bottom < orig.top && obs.bottom > new.top) {
653 /* w_0 is below the bottom of w_j */
654 new.top = obs.bottom + 1;
656 if (orig.bottom < obs.top && obs.top < new.bottom) {
657 /* The bottom of w_0 is above the top of w_j */
658 new.bottom = obs.top - 1;
663 tmp = wwin;
664 while (tmp->prev) {
665 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
666 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
667 tmp = tmp->prev;
668 continue;
670 tmp = tmp->prev;
672 set_window_coords(tmp, &obs);
675 * Use the new.top and new.height instead of original values
676 * as they may have different intersections with the obstructing windows
678 new.height = new.bottom - new.top - adjust_height;
679 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
680 if (y_intsect != 0) {
681 if (obs.right < orig.left && obs.right > new.left) {
682 /* w_0 is completely to the right of w_j */
683 new.left = obs.right + 1;
685 if (orig.right < obs.left && obs.left < new.right) {
686 /* w_0 is completely to the left of w_j */
687 new.right = obs.left - 1;
692 *new_x = new.left;
693 *new_y = new.top;
694 /* xcalc needs -7 here, but other apps don't */
695 *new_height = new.bottom - new.top - adjust_height - 1;;
696 *new_width = new.right - new.left;
699 void wUnmaximizeWindow(WWindow *wwin)
701 int x, y, w, h;
703 if (!wwin->flags.maximized)
704 return;
706 if (wwin->flags.shaded) {
707 wwin->flags.skip_next_animation = 1;
708 wUnshadeWindow(wwin);
711 /* Use old coordinates if they are set, current values otherwise */
712 remember_geometry(wwin, &x, &y, &w, &h);
714 /* unMaximusize relative to original position */
715 if (wwin->flags.maximized & MAX_MAXIMUS) {
716 x += wwin->frame_x - wwin->maximus_x;
717 y += wwin->frame_y - wwin->maximus_y;
720 wwin->flags.maximized = 0;
721 wwin->flags.old_maximized = 0;
722 wWindowConfigure(wwin, x, y, w, h);
723 wWindowSynthConfigureNotify(wwin);
725 WMPostNotificationName(WMNChangedState, wwin, "maximize");
728 void wFullscreenWindow(WWindow *wwin)
730 int head;
731 WMRect rect;
733 if (wwin->flags.fullscreen)
734 return;
736 wwin->flags.fullscreen = True;
738 wWindowConfigureBorders(wwin);
740 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
742 wwin->bfs_geometry.x = wwin->frame_x;
743 wwin->bfs_geometry.y = wwin->frame_y;
744 wwin->bfs_geometry.width = wwin->frame->core->width;
745 wwin->bfs_geometry.height = wwin->frame->core->height;
747 head = wGetHeadForWindow(wwin);
748 rect = wGetRectForHead(wwin->screen_ptr, head);
749 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
751 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
754 void wUnfullscreenWindow(WWindow *wwin)
756 if (!wwin->flags.fullscreen)
757 return;
759 wwin->flags.fullscreen = False;
761 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
762 if (WFLAGP(wwin, sunken)) {
763 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
764 } else if (WFLAGP(wwin, floating)) {
765 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
766 } else {
767 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
771 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
772 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
774 wWindowConfigureBorders(wwin);
776 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
777 wFrameWindowPaint(wwin->frame);
780 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
783 #ifdef ANIMATIONS
784 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
786 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
787 float cx, cy, cw, ch;
788 float xstep, ystep, wstep, hstep;
789 XPoint points[5];
790 float dx, dch, midy;
791 float angle, final_angle, delta;
793 xstep = (float)(fx - x) / steps;
794 ystep = (float)(fy - y) / steps;
795 wstep = (float)(fw - w) / steps;
796 hstep = (float)(fh - h) / steps;
798 cx = (float)x;
799 cy = (float)y;
800 cw = (float)w;
801 ch = (float)h;
803 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
804 delta = (float)(final_angle / FRAMES);
805 for (angle = 0;; angle += delta) {
806 if (angle > final_angle)
807 angle = final_angle;
809 dx = (cw / 10) - ((cw / 5) * sin(angle));
810 dch = (ch / 2) * cos(angle);
811 midy = cy + (ch / 2);
813 points[0].x = cx + dx;
814 points[0].y = midy - dch;
815 points[1].x = cx + cw - dx;
816 points[1].y = points[0].y;
817 points[2].x = cx + cw + dx;
818 points[2].y = midy + dch;
819 points[3].x = cx - dx;
820 points[3].y = points[2].y;
821 points[4].x = points[0].x;
822 points[4].y = points[0].y;
824 XGrabServer(dpy);
825 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
826 XFlush(dpy);
827 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
829 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
830 XUngrabServer(dpy);
831 cx += xstep;
832 cy += ystep;
833 cw += wstep;
834 ch += hstep;
835 if (angle >= final_angle)
836 break;
839 XFlush(dpy);
842 #undef FRAMES
844 static void
845 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
847 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
848 float cx, cy, cw, ch;
849 float xstep, ystep, wstep, hstep;
850 XPoint points[5];
851 float angle, final_angle, a, d, delta;
853 x += w / 2;
854 y += h / 2;
855 fx += fw / 2;
856 fy += fh / 2;
858 xstep = (float)(fx - x) / steps;
859 ystep = (float)(fy - y) / steps;
860 wstep = (float)(fw - w) / steps;
861 hstep = (float)(fh - h) / steps;
863 cx = (float)x;
864 cy = (float)y;
865 cw = (float)w;
866 ch = (float)h;
868 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
869 delta = (float)(final_angle / FRAMES);
870 for (angle = 0;; angle += delta) {
871 if (angle > final_angle)
872 angle = final_angle;
874 a = atan(ch / cw);
875 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
877 points[0].x = cx + cos(angle - a) * d;
878 points[0].y = cy + sin(angle - a) * d;
879 points[1].x = cx + cos(angle + a) * d;
880 points[1].y = cy + sin(angle + a) * d;
881 points[2].x = cx + cos(angle - a + WM_PI) * d;
882 points[2].y = cy + sin(angle - a + WM_PI) * d;
883 points[3].x = cx + cos(angle + a + WM_PI) * d;
884 points[3].y = cy + sin(angle + a + WM_PI) * d;
885 points[4].x = cx + cos(angle - a) * d;
886 points[4].y = cy + sin(angle - a) * d;
887 XGrabServer(dpy);
888 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
889 XFlush(dpy);
890 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
892 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
893 XUngrabServer(dpy);
894 cx += xstep;
895 cy += ystep;
896 cw += wstep;
897 ch += hstep;
898 if (angle >= final_angle)
899 break;
902 XFlush(dpy);
905 #undef FRAMES
907 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
909 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
910 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
911 float xstep, ystep, wstep, hstep;
912 int i, j;
914 xstep = (float)(fx - x) / steps;
915 ystep = (float)(fy - y) / steps;
916 wstep = (float)(fw - w) / steps;
917 hstep = (float)(fh - h) / steps;
919 for (j = 0; j < FRAMES; j++) {
920 cx[j] = (float)x;
921 cy[j] = (float)y;
922 cw[j] = (float)w;
923 ch[j] = (float)h;
925 XGrabServer(dpy);
926 for (i = 0; i < steps; i++) {
927 for (j = 0; j < FRAMES; j++) {
928 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
929 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
931 XFlush(dpy);
932 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
934 for (j = 0; j < FRAMES; j++) {
935 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
936 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
937 if (j < FRAMES - 1) {
938 cx[j] = cx[j + 1];
939 cy[j] = cy[j + 1];
940 cw[j] = cw[j + 1];
941 ch[j] = ch[j + 1];
942 } else {
943 cx[j] += xstep;
944 cy[j] += ystep;
945 cw[j] += wstep;
946 ch[j] += hstep;
951 for (j = 0; j < FRAMES; j++) {
952 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
954 XFlush(dpy);
955 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
957 for (j = 0; j < FRAMES; j++) {
958 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
961 XUngrabServer(dpy);
964 #undef FRAMES
966 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
968 int style = wPreferences.iconification_style; /* Catch the value */
969 int steps;
971 if (style == WIS_NONE)
972 return;
974 if (style == WIS_RANDOM) {
975 style = rand() % 3;
978 switch (style) {
979 case WIS_TWIST:
980 steps = MINIATURIZE_ANIMATION_STEPS_T;
981 if (steps > 0)
982 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
983 break;
984 case WIS_FLIP:
985 steps = MINIATURIZE_ANIMATION_STEPS_F;
986 if (steps > 0)
987 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
988 break;
989 case WIS_ZOOM:
990 default:
991 steps = MINIATURIZE_ANIMATION_STEPS_Z;
992 if (steps > 0)
993 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
994 break;
997 #endif /* ANIMATIONS */
999 static void flushExpose(void)
1001 XEvent tmpev;
1003 while (XCheckTypedEvent(dpy, Expose, &tmpev))
1004 WMHandleEvent(&tmpev);
1005 XSync(dpy, 0);
1008 static void unmapTransientsFor(WWindow *wwin)
1010 WWindow *tmp;
1012 tmp = wwin->screen_ptr->focused_window;
1013 while (tmp) {
1014 /* unmap the transients for this transient */
1015 if (tmp != wwin && tmp->transient_for == wwin->client_win
1016 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1017 unmapTransientsFor(tmp);
1018 tmp->flags.miniaturized = 1;
1019 if (!tmp->flags.shaded) {
1020 wWindowUnmap(tmp);
1021 } else {
1022 XUnmapWindow(dpy, tmp->frame->core->window);
1025 if (!tmp->flags.shaded)
1027 wClientSetState(tmp, IconicState, None);
1029 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1031 tmp = tmp->prev;
1035 static void mapTransientsFor(WWindow *wwin)
1037 WWindow *tmp;
1039 tmp = wwin->screen_ptr->focused_window;
1040 while (tmp) {
1041 /* recursively map the transients for this transient */
1042 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1043 && tmp->icon == NULL) {
1044 mapTransientsFor(tmp);
1045 tmp->flags.miniaturized = 0;
1046 if (!tmp->flags.shaded) {
1047 wWindowMap(tmp);
1048 } else {
1049 XMapWindow(dpy, tmp->frame->core->window);
1051 tmp->flags.semi_focused = 0;
1053 if (!tmp->flags.shaded)
1055 wClientSetState(tmp, NormalState, None);
1057 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1059 tmp = tmp->prev;
1063 static WWindow *recursiveTransientFor(WWindow * wwin)
1065 int i;
1067 if (!wwin)
1068 return None;
1070 /* hackish way to detect transient_for cycle */
1071 i = wwin->screen_ptr->window_count + 1;
1073 while (wwin && wwin->transient_for != None && i > 0) {
1074 wwin = wWindowFor(wwin->transient_for);
1075 i--;
1077 if (i == 0 && wwin) {
1078 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1079 return NULL;
1082 return wwin;
1085 void wIconifyWindow(WWindow * wwin)
1087 XWindowAttributes attribs;
1088 int present;
1090 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1091 return; /* the window doesn't exist anymore */
1093 if (wwin->flags.miniaturized)
1094 return; /* already miniaturized */
1096 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1097 WWindow *owner = wWindowFor(wwin->transient_for);
1099 if (owner && owner->flags.miniaturized)
1100 return;
1103 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1105 /* if the window is in another workspace, simplify process */
1106 if (present) {
1107 /* icon creation may take a while */
1108 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1109 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1110 GrabModeAsync, None, None, CurrentTime);
1113 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1114 if (!wwin->flags.icon_moved)
1115 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1117 wwin->icon = icon_create_for_wwindow(wwin);
1118 wwin->icon->mapped = 1;
1121 wwin->flags.miniaturized = 1;
1122 wwin->flags.mapped = 0;
1124 /* unmap transients */
1125 unmapTransientsFor(wwin);
1127 if (present) {
1128 XUngrabPointer(dpy, CurrentTime);
1129 wWindowUnmap(wwin);
1130 /* let all Expose events arrive so that we can repaint
1131 * something before the animation starts (and the server is grabbed) */
1132 XSync(dpy, 0);
1134 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1135 wClientSetState(wwin, IconicState, None);
1136 else
1137 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1139 flushExpose();
1140 #ifdef ANIMATIONS
1141 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1142 && !wPreferences.no_animations) {
1143 int ix, iy, iw, ih;
1145 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1146 ix = wwin->icon_x;
1147 iy = wwin->icon_y;
1148 iw = wwin->icon->core->width;
1149 ih = wwin->icon->core->height;
1150 } else {
1151 if (wwin->flags.net_handle_icon) {
1152 ix = wwin->icon_x;
1153 iy = wwin->icon_y;
1154 iw = wwin->icon_w;
1155 ih = wwin->icon_h;
1156 } else {
1157 ix = 0;
1158 iy = 0;
1159 iw = wwin->screen_ptr->scr_width;
1160 ih = wwin->screen_ptr->scr_height;
1163 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1164 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1166 #endif
1169 wwin->flags.skip_next_animation = 0;
1171 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1172 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1173 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1174 XMapWindow(dpy, wwin->icon->core->window);
1176 AddToStackList(wwin->icon->core);
1177 wLowerFrame(wwin->icon->core);
1180 if (present) {
1181 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1184 * It doesn't seem to be working and causes button event hangup
1185 * when deiconifying a transient window.
1186 setupIconGrabs(wwin->icon);
1188 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1189 && wPreferences.focus_mode == WKF_CLICK) {
1190 WWindow *tmp;
1192 tmp = wwin->prev;
1193 while (tmp) {
1194 if (!WFLAGP(tmp, no_focusable)
1195 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1196 && (wwin->frame->workspace == tmp->frame->workspace))
1197 break;
1198 tmp = tmp->prev;
1200 wSetFocusTo(wwin->screen_ptr, tmp);
1201 } else if (wPreferences.focus_mode != WKF_CLICK) {
1202 wSetFocusTo(wwin->screen_ptr, NULL);
1204 #ifdef ANIMATIONS
1205 if (!wwin->screen_ptr->flags.startup) {
1206 /* Catch up with events not processed while animation was running */
1207 Window clientwin = wwin->client_win;
1209 ProcessPendingEvents();
1211 /* the window can disappear while ProcessPendingEvents() runs */
1212 if (!wWindowFor(clientwin)) {
1213 return;
1216 #endif
1219 /* maybe we want to do this regardless of net_handle_icon
1220 * it seems to me we might break behaviour this way.
1222 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1223 && !wwin->flags.net_handle_icon)
1224 wIconSelect(wwin->icon);
1226 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1228 if (wPreferences.auto_arrange_icons)
1229 wArrangeIcons(wwin->screen_ptr, True);
1232 void wDeiconifyWindow(WWindow *wwin)
1234 /* Let's avoid changing workspace while deiconifying */
1235 ignore_wks_change = 1;
1237 /* we're hiding for show_desktop */
1238 int netwm_hidden = wwin->flags.net_show_desktop &&
1239 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1241 if (!netwm_hidden)
1242 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1244 if (!wwin->flags.miniaturized)
1245 return;
1247 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1248 WWindow *owner = recursiveTransientFor(wwin);
1250 if (owner && owner->flags.miniaturized) {
1251 wDeiconifyWindow(owner);
1252 wSetFocusTo(wwin->screen_ptr, wwin);
1253 wRaiseFrame(wwin->frame->core);
1254 return;
1258 wwin->flags.miniaturized = 0;
1260 if (!netwm_hidden && !wwin->flags.shaded)
1261 wwin->flags.mapped = 1;
1263 if (!netwm_hidden || wPreferences.sticky_icons) {
1264 /* maybe we want to do this regardless of net_handle_icon
1265 * it seems to me we might break behaviour this way.
1267 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1268 && wwin->icon != NULL) {
1269 if (wwin->icon->selected)
1270 wIconSelect(wwin->icon);
1272 XUnmapWindow(dpy, wwin->icon->core->window);
1276 /* if the window is in another workspace, do it silently */
1277 if (!netwm_hidden) {
1278 #ifdef ANIMATIONS
1279 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1280 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1281 int ix, iy, iw, ih;
1283 if (!wPreferences.disable_miniwindows
1284 && !wwin->flags.net_handle_icon) {
1285 ix = wwin->icon_x;
1286 iy = wwin->icon_y;
1287 iw = wwin->icon->core->width;
1288 ih = wwin->icon->core->height;
1289 } else {
1290 if (wwin->flags.net_handle_icon) {
1291 ix = wwin->icon_x;
1292 iy = wwin->icon_y;
1293 iw = wwin->icon_w;
1294 ih = wwin->icon_h;
1295 } else {
1296 ix = 0;
1297 iy = 0;
1298 iw = wwin->screen_ptr->scr_width;
1299 ih = wwin->screen_ptr->scr_height;
1302 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1303 wwin->frame_x, wwin->frame_y,
1304 wwin->frame->core->width, wwin->frame->core->height);
1306 #endif /* ANIMATIONS */
1307 wwin->flags.skip_next_animation = 0;
1308 XGrabServer(dpy);
1309 if (!wwin->flags.shaded)
1310 XMapWindow(dpy, wwin->client_win);
1312 XMapWindow(dpy, wwin->frame->core->window);
1313 wRaiseFrame(wwin->frame->core);
1314 if (!wwin->flags.shaded)
1315 wClientSetState(wwin, NormalState, None);
1317 mapTransientsFor(wwin);
1320 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1321 && !wwin->flags.net_handle_icon) {
1322 RemoveFromStackList(wwin->icon->core);
1323 /* removeIconGrabs(wwin->icon); */
1324 wIconDestroy(wwin->icon);
1325 wwin->icon = NULL;
1328 if (!netwm_hidden) {
1329 XUngrabServer(dpy);
1331 wSetFocusTo(wwin->screen_ptr, wwin);
1333 #ifdef ANIMATIONS
1334 if (!wwin->screen_ptr->flags.startup) {
1335 /* Catch up with events not processed while animation was running */
1336 Window clientwin = wwin->client_win;
1338 ProcessPendingEvents();
1340 /* the window can disappear while ProcessPendingEvents() runs */
1341 if (!wWindowFor(clientwin))
1342 return;
1344 #endif
1347 if (wPreferences.auto_arrange_icons)
1348 wArrangeIcons(wwin->screen_ptr, True);
1350 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1352 /* In case we were shaded and iconified, also unshade */
1353 if (!netwm_hidden)
1354 wUnshadeWindow(wwin);
1356 ignore_wks_change = 0;
1359 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1361 if (wwin->flags.miniaturized) {
1362 if (wwin->icon) {
1363 XUnmapWindow(dpy, wwin->icon->core->window);
1364 wwin->icon->mapped = 0;
1366 wwin->flags.hidden = 1;
1368 WMPostNotificationName(WMNChangedState, wwin, "hide");
1369 return;
1372 if (wwin->flags.inspector_open) {
1373 wHideInspectorForWindow(wwin);
1376 wwin->flags.hidden = 1;
1377 wWindowUnmap(wwin);
1379 wClientSetState(wwin, IconicState, icon->icon_win);
1380 flushExpose();
1382 #ifdef ANIMATIONS
1383 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1384 !wwin->flags.skip_next_animation && animate) {
1385 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1386 wwin->frame->core->width, wwin->frame->core->height,
1387 icon_x, icon_y, icon->core->width, icon->core->height);
1389 #endif
1390 wwin->flags.skip_next_animation = 0;
1392 WMPostNotificationName(WMNChangedState, wwin, "hide");
1395 void wHideAll(WScreen *scr)
1397 WWindow *wwin;
1398 WWindow **windows;
1399 WMenu *menu;
1400 unsigned int wcount = 0;
1401 int i;
1403 if (!scr)
1404 return;
1406 menu = scr->switch_menu;
1408 windows = wmalloc(sizeof(WWindow *));
1410 if (menu != NULL) {
1411 for (i = 0; i < menu->entry_no; i++) {
1412 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1413 wcount++;
1414 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1416 } else {
1417 wwin = scr->focused_window;
1419 while (wwin) {
1420 windows[wcount] = wwin;
1421 wcount++;
1422 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1423 wwin = wwin->prev;
1428 for (i = 0; i < wcount; i++) {
1429 wwin = windows[i];
1430 if (wwin->frame->workspace == scr->current_workspace
1431 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1432 && !wwin->flags.internal_window
1433 && !WFLAGP(wwin, no_miniaturizable)
1435 wwin->flags.skip_next_animation = 1;
1436 wIconifyWindow(wwin);
1440 wfree(windows);
1443 void wHideOtherApplications(WWindow *awin)
1445 WWindow *wwin;
1446 WApplication *tapp;
1448 if (!awin)
1449 return;
1450 wwin = awin->screen_ptr->focused_window;
1452 while (wwin) {
1453 if (wwin != awin
1454 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1455 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1456 && !wwin->flags.internal_window
1457 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1459 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1460 if (!WFLAGP(wwin, no_miniaturizable)) {
1461 wwin->flags.skip_next_animation = 1;
1462 wIconifyWindow(wwin);
1464 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1465 tapp = wApplicationOf(wwin->main_window);
1466 if (tapp) {
1467 tapp->flags.skip_next_animation = 1;
1468 wHideApplication(tapp);
1469 } else {
1470 if (!WFLAGP(wwin, no_miniaturizable)) {
1471 wwin->flags.skip_next_animation = 1;
1472 wIconifyWindow(wwin);
1477 wwin = wwin->prev;
1480 wSetFocusTo(awin->screen_ptr, awin);
1484 void wHideApplication(WApplication *wapp)
1486 WScreen *scr;
1487 WWindow *wlist;
1488 int hadfocus;
1489 int animate;
1491 if (!wapp) {
1492 wwarning("trying to hide a non grouped window");
1493 return;
1495 if (!wapp->main_window_desc) {
1496 wwarning("group leader not found for window group");
1497 return;
1499 scr = wapp->main_window_desc->screen_ptr;
1500 hadfocus = 0;
1501 wlist = scr->focused_window;
1502 if (!wlist)
1503 return;
1505 if (wlist->main_window == wapp->main_window)
1506 wapp->last_focused = wlist;
1507 else
1508 wapp->last_focused = NULL;
1510 animate = !wapp->flags.skip_next_animation;
1512 while (wlist) {
1513 if (wlist->main_window == wapp->main_window) {
1514 if (wlist->flags.focused) {
1515 hadfocus = 1;
1517 if (wapp->app_icon) {
1518 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1519 wapp->app_icon->y_pos, wlist, animate);
1520 animate = False;
1523 wlist = wlist->prev;
1526 wapp->flags.skip_next_animation = 0;
1528 if (hadfocus) {
1529 if (wPreferences.focus_mode == WKF_CLICK) {
1530 wlist = scr->focused_window;
1531 while (wlist) {
1532 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1533 && (wlist->flags.mapped || wlist->flags.shaded))
1534 break;
1535 wlist = wlist->prev;
1537 wSetFocusTo(scr, wlist);
1538 } else {
1539 wSetFocusTo(scr, NULL);
1543 wapp->flags.hidden = 1;
1545 if (wPreferences.auto_arrange_icons)
1546 wArrangeIcons(scr, True);
1548 #ifdef HIDDENDOT
1549 if (wapp->app_icon)
1550 wAppIconPaint(wapp->app_icon);
1551 #endif
1554 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1556 if (bringToCurrentWS)
1557 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1559 wwin->flags.hidden = 0;
1561 #ifdef ANIMATIONS
1562 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1563 animateResize(wwin->screen_ptr, icon_x, icon_y,
1564 icon->core->width, icon->core->height,
1565 wwin->frame_x, wwin->frame_y,
1566 wwin->frame->core->width, wwin->frame->core->height);
1568 #endif
1569 wwin->flags.skip_next_animation = 0;
1570 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1571 XMapWindow(dpy, wwin->client_win);
1572 XMapWindow(dpy, wwin->frame->core->window);
1573 wClientSetState(wwin, NormalState, None);
1574 wwin->flags.mapped = 1;
1575 wRaiseFrame(wwin->frame->core);
1577 if (wwin->flags.inspector_open) {
1578 wUnhideInspectorForWindow(wwin);
1581 WMPostNotificationName(WMNChangedState, wwin, "hide");
1584 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1586 WScreen *scr;
1587 WWindow *wlist, *next;
1588 WWindow *focused = NULL;
1589 int animate;
1591 if (!wapp)
1592 return;
1594 scr = wapp->main_window_desc->screen_ptr;
1595 wlist = scr->focused_window;
1596 if (!wlist)
1597 return;
1599 /* goto beginning of list */
1600 while (wlist->prev)
1601 wlist = wlist->prev;
1603 animate = !wapp->flags.skip_next_animation;
1605 while (wlist) {
1606 next = wlist->next;
1608 if (wlist->main_window == wapp->main_window) {
1609 if (wlist->flags.focused)
1610 focused = wlist;
1611 else if (!focused || !focused->flags.focused)
1612 focused = wlist;
1614 if (wlist->flags.miniaturized) {
1615 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1616 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1617 if (!wlist->icon->mapped) {
1618 int x, y;
1620 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1621 if (wlist->icon_x != x || wlist->icon_y != y) {
1622 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1624 wlist->icon_x = x;
1625 wlist->icon_y = y;
1626 XMapWindow(dpy, wlist->icon->core->window);
1627 wlist->icon->mapped = 1;
1629 wRaiseFrame(wlist->icon->core);
1631 if (bringToCurrentWS)
1632 wWindowChangeWorkspace(wlist, scr->current_workspace);
1633 wlist->flags.hidden = 0;
1634 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1635 wDeiconifyWindow(wlist);
1637 WMPostNotificationName(WMNChangedState, wlist, "hide");
1638 } else if (wlist->flags.shaded) {
1639 if (bringToCurrentWS)
1640 wWindowChangeWorkspace(wlist, scr->current_workspace);
1641 wlist->flags.hidden = 0;
1642 wRaiseFrame(wlist->frame->core);
1643 if (wlist->frame->workspace == scr->current_workspace) {
1644 XMapWindow(dpy, wlist->frame->core->window);
1645 if (miniwindows) {
1646 wUnshadeWindow(wlist);
1649 WMPostNotificationName(WMNChangedState, wlist, "hide");
1650 } else if (wlist->flags.hidden) {
1651 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1652 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1653 animate = False;
1654 } else {
1655 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1656 wWindowChangeWorkspace(wlist, scr->current_workspace);
1658 wRaiseFrame(wlist->frame->core);
1661 wlist = next;
1664 wapp->flags.skip_next_animation = 0;
1665 wapp->flags.hidden = 0;
1667 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1668 wRaiseFrame(wapp->last_focused->frame->core);
1669 wSetFocusTo(scr, wapp->last_focused);
1670 } else if (focused) {
1671 wSetFocusTo(scr, focused);
1673 wapp->last_focused = NULL;
1674 if (wPreferences.auto_arrange_icons)
1675 wArrangeIcons(scr, True);
1677 #ifdef HIDDENDOT
1678 wAppIconPaint(wapp->app_icon);
1679 #endif
1682 void wShowAllWindows(WScreen *scr)
1684 WWindow *wwin, *old_foc;
1685 WApplication *wapp;
1687 old_foc = wwin = scr->focused_window;
1688 while (wwin) {
1689 if (!wwin->flags.internal_window &&
1690 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1691 if (wwin->flags.miniaturized) {
1692 wwin->flags.skip_next_animation = 1;
1693 wDeiconifyWindow(wwin);
1694 } else if (wwin->flags.hidden) {
1695 wapp = wApplicationOf(wwin->main_window);
1696 if (wapp) {
1697 wUnhideApplication(wapp, False, False);
1698 } else {
1699 wwin->flags.skip_next_animation = 1;
1700 wDeiconifyWindow(wwin);
1704 wwin = wwin->prev;
1706 wSetFocusTo(scr, old_foc);
1707 /*wRaiseFrame(old_foc->frame->core); */
1710 void wRefreshDesktop(WScreen *scr)
1712 Window win;
1713 XSetWindowAttributes attr;
1715 attr.backing_store = NotUseful;
1716 attr.save_under = False;
1717 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1718 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1719 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1720 XMapRaised(dpy, win);
1721 XDestroyWindow(dpy, win);
1722 XFlush(dpy);
1725 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1727 WWindow *wwin;
1728 WAppIcon *aicon;
1730 int head;
1731 const int heads = wXineramaHeads(scr);
1733 struct HeadVars {
1734 int pf; /* primary axis */
1735 int sf; /* secondary axis */
1736 int fullW;
1737 int fullH;
1738 int pi, si;
1739 int sx1, sx2, sy1, sy2; /* screen boundary */
1740 int sw, sh;
1741 int xo, yo;
1742 int xs, ys;
1743 } *vars;
1745 int isize = wPreferences.icon_size;
1747 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1749 for (head = 0; head < heads; ++head) {
1750 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1751 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1752 vars[head].pi = vars[head].si = 0;
1753 vars[head].sx1 = rect.pos.x;
1754 vars[head].sy1 = rect.pos.y;
1755 vars[head].sw = rect.size.width;
1756 vars[head].sh = rect.size.height;
1757 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1758 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1759 vars[head].sw = isize * (vars[head].sw / isize);
1760 vars[head].sh = isize * (vars[head].sh / isize);
1761 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1762 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1764 /* icon yard boundaries */
1765 if (wPreferences.icon_yard & IY_VERT) {
1766 vars[head].pf = vars[head].fullH;
1767 vars[head].sf = vars[head].fullW;
1768 } else {
1769 vars[head].pf = vars[head].fullW;
1770 vars[head].sf = vars[head].fullH;
1772 if (wPreferences.icon_yard & IY_RIGHT) {
1773 vars[head].xo = vars[head].sx2 - isize;
1774 vars[head].xs = -1;
1775 } else {
1776 vars[head].xo = vars[head].sx1;
1777 vars[head].xs = 1;
1779 if (wPreferences.icon_yard & IY_TOP) {
1780 vars[head].yo = vars[head].sy1;
1781 vars[head].ys = 1;
1782 } else {
1783 vars[head].yo = vars[head].sy2 - isize;
1784 vars[head].ys = -1;
1788 #define X ((wPreferences.icon_yard & IY_VERT) \
1789 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1790 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1792 #define Y ((wPreferences.icon_yard & IY_VERT) \
1793 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1794 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1796 /* arrange application icons */
1797 aicon = scr->app_icon_list;
1798 /* reverse them to avoid unnecessarily sliding of icons */
1799 while (aicon && aicon->next)
1800 aicon = aicon->next;
1802 while (aicon) {
1803 if (!aicon->docked) {
1804 /* CHECK: can icon be NULL here ? */
1805 /* The intention here is to place the AppIcon on the head that
1806 * contains most of the applications _main_ window. */
1807 head = wGetHeadForWindow(aicon->icon->owner);
1809 if (aicon->x_pos != X || aicon->y_pos != Y) {
1810 #ifdef ANIMATIONS
1811 if (!wPreferences.no_animations)
1812 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1813 #endif /* ANIMATIONS */
1815 wAppIconMove(aicon, X, Y);
1816 vars[head].pi++;
1817 if (vars[head].pi >= vars[head].pf) {
1818 vars[head].pi = 0;
1819 vars[head].si++;
1822 aicon = aicon->prev;
1825 /* arrange miniwindows */
1826 wwin = scr->focused_window;
1827 /* reverse them to avoid unnecessarily shuffling */
1828 while (wwin && wwin->prev)
1829 wwin = wwin->prev;
1831 while (wwin) {
1832 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1833 (wwin->frame->workspace == scr->current_workspace ||
1834 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1836 head = wGetHeadForWindow(wwin);
1838 if (arrangeAll || !wwin->flags.icon_moved) {
1839 if (wwin->icon_x != X || wwin->icon_y != Y)
1840 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1842 wwin->icon_x = X;
1843 wwin->icon_y = Y;
1845 vars[head].pi++;
1846 if (vars[head].pi >= vars[head].pf) {
1847 vars[head].pi = 0;
1848 vars[head].si++;
1852 if (arrangeAll) {
1853 wwin->flags.icon_moved = 0;
1855 /* we reversed the order, so we use next */
1856 wwin = wwin->next;
1859 wfree(vars);
1862 void wSelectWindow(WWindow *wwin, Bool flag)
1864 WScreen *scr = wwin->screen_ptr;
1866 if (flag) {
1867 wwin->flags.selected = 1;
1868 if (wwin->frame->selected_border_pixel)
1869 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1870 else
1871 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1873 if (!HAS_BORDER(wwin)) {
1874 XSetWindowBorderWidth(dpy, wwin->frame->core->window, FRAME_BORDER_WIDTH);
1877 if (!scr->selected_windows)
1878 scr->selected_windows = WMCreateArray(4);
1879 WMAddToArray(scr->selected_windows, wwin);
1880 } else {
1881 wwin->flags.selected = 0;
1882 if (wwin->frame->border_pixel)
1883 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1884 else
1885 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1887 if (!HAS_BORDER(wwin)) {
1888 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1891 if (scr->selected_windows) {
1892 WMRemoveFromArray(scr->selected_windows, wwin);
1897 void wMakeWindowVisible(WWindow *wwin)
1899 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1900 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1902 if (wwin->flags.shaded) {
1903 wUnshadeWindow(wwin);
1905 if (wwin->flags.hidden) {
1906 WApplication *app;
1908 app = wApplicationOf(wwin->main_window);
1909 if (app) {
1910 /* trick to get focus to this window */
1911 app->last_focused = wwin;
1912 wUnhideApplication(app, False, False);
1915 if (wwin->flags.miniaturized) {
1916 wDeiconifyWindow(wwin);
1917 } else {
1918 if (!WFLAGP(wwin, no_focusable))
1919 wSetFocusTo(wwin->screen_ptr, wwin);
1920 wRaiseFrame(wwin->frame->core);
1925 * Do the animation while shading (called with what = SHADE)
1926 * or unshading (what = UNSHADE).
1928 #ifdef ANIMATIONS
1929 static void shade_animate(WWindow *wwin, Bool what)
1931 int y, s, w, h;
1932 time_t time0 = time(NULL);
1934 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1935 return;
1937 switch(what) {
1938 case SHADE:
1939 if (!wwin->screen_ptr->flags.startup) {
1940 /* do the shading animation */
1941 h = wwin->frame->core->height;
1942 s = h / SHADE_STEPS;
1943 if (s < 1)
1944 s = 1;
1945 w = wwin->frame->core->width;
1946 y = wwin->frame->top_width;
1947 while (h > wwin->frame->top_width + 1) {
1948 XMoveWindow(dpy, wwin->client_win, 0, y);
1949 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1950 XFlush(dpy);
1952 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1953 break;
1955 if (SHADE_DELAY > 0) {
1956 wusleep(SHADE_DELAY * 1000L);
1957 } else {
1958 wusleep(10);
1960 h -= s;
1961 y -= s;
1963 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1965 break;
1967 case UNSHADE:
1968 h = wwin->frame->top_width + wwin->frame->bottom_width;
1969 y = wwin->frame->top_width - wwin->client.height;
1970 s = abs(y) / SHADE_STEPS;
1971 if (s < 1)
1972 s = 1;
1973 w = wwin->frame->core->width;
1974 XMoveWindow(dpy, wwin->client_win, 0, y);
1975 if (s > 0) {
1976 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1977 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1978 XMoveWindow(dpy, wwin->client_win, 0, y);
1979 XFlush(dpy);
1980 if (SHADE_DELAY > 0) {
1981 wusleep(SHADE_DELAY * 2000L / 3);
1982 } else {
1983 wusleep(10);
1985 h += s;
1986 y += s;
1988 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1989 break;
1992 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1993 break;
1996 #endif