wAppIconPaint paint argument
[wmaker-crm.git] / src / actions.c
blobe168c566dfe1ec6981f68b2c63f3859139030cd3
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 wApplicationDeactivate(oapp);
142 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
143 return;
146 if (old_scr != scr && old_focused)
147 wWindowUnfocus(old_focused);
149 wasfocused = wwin->flags.focused;
150 napp = wApplicationOf(wwin->main_window);
152 /* remember last workspace where the app has been */
153 if (napp)
154 napp->last_workspace = wwin->frame->workspace;
156 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
157 /* install colormap if colormap mode is lock mode */
158 if (wPreferences.colormap_mode == WCM_CLICK)
159 wColormapInstallForWindow(scr, wwin);
161 /* set input focus */
162 switch (wwin->focus_mode) {
163 case WFM_NO_INPUT:
164 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
165 break;
166 case WFM_PASSIVE:
167 case WFM_LOCALLY_ACTIVE:
168 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
169 break;
170 case WFM_GLOBALLY_ACTIVE:
171 break;
174 XFlush(dpy);
175 if (wwin->protocols.TAKE_FOCUS)
176 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
178 XSync(dpy, False);
179 } else {
180 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
183 if (WFLAGP(wwin, no_focusable))
184 return;
186 /* if this is not the focused window focus it */
187 if (focused != wwin) {
188 /* change the focus window list order */
189 if (wwin->prev)
190 wwin->prev->next = wwin->next;
192 if (wwin->next)
193 wwin->next->prev = wwin->prev;
195 wwin->prev = focused;
196 focused->next = wwin;
197 wwin->next = NULL;
198 scr->focused_window = wwin;
200 if (oapp && oapp != napp) {
201 wAppMenuUnmap(oapp->menu);
202 wApplicationDeactivate(oapp);
206 wWindowFocus(wwin, focused);
208 if (napp && !wasfocused) {
209 #ifdef USER_MENU
210 wUserMenuRefreshInstances(napp->menu, wwin);
211 #endif /* USER_MENU */
213 if (wwin->flags.mapped)
214 wAppMenuMap(napp->menu, wwin);
216 if (napp)
217 wApplicationActivate(napp);
219 XFlush(dpy);
220 old_scr = scr;
223 void wShadeWindow(WWindow *wwin)
226 if (wwin->flags.shaded)
227 return;
229 XLowerWindow(dpy, wwin->client_win);
230 shade_animate(wwin, SHADE);
232 wwin->flags.skip_next_animation = 0;
233 wwin->flags.shaded = 1;
234 wwin->flags.mapped = 0;
235 /* prevent window withdrawal when getting UnmapNotify */
236 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
237 XUnmapWindow(dpy, wwin->client_win);
238 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
240 /* for the client it's just like iconification */
241 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
243 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
244 wWindowSynthConfigureNotify(wwin);
247 wClientSetState(wwin, IconicState, None);
250 WMPostNotificationName(WMNChangedState, wwin, "shade");
252 #ifdef ANIMATIONS
253 if (!wwin->screen_ptr->flags.startup) {
254 /* Catch up with events not processed while animation was running */
255 ProcessPendingEvents();
257 #endif
260 void wUnshadeWindow(WWindow *wwin)
263 if (!wwin->flags.shaded)
264 return;
266 wwin->flags.shaded = 0;
267 wwin->flags.mapped = 1;
268 XMapWindow(dpy, wwin->client_win);
270 shade_animate(wwin, UNSHADE);
272 wwin->flags.skip_next_animation = 0;
273 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
274 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
276 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
277 wWindowSynthConfigureNotify(wwin);
279 /* if the window is focused, set the focus again as it was disabled during
280 * shading */
281 if (wwin->flags.focused)
282 wSetFocusTo(wwin->screen_ptr, wwin);
284 WMPostNotificationName(WMNChangedState, wwin, "shade");
287 /* Set the old coordinates using the current values */
288 static void save_old_geometry(WWindow *wwin, int directions)
290 /* never been saved? */
291 if (! wwin->old_geometry.width)
292 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
293 if (! wwin->old_geometry.height)
294 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
296 if (directions & SAVE_GEOMETRY_X)
297 wwin->old_geometry.x = wwin->frame_x;
298 if (directions & SAVE_GEOMETRY_Y)
299 wwin->old_geometry.y = wwin->frame_y;
300 if (directions & SAVE_GEOMETRY_WIDTH)
301 wwin->old_geometry.width = wwin->client.width;
302 if (directions & SAVE_GEOMETRY_HEIGHT)
303 wwin->old_geometry.height = wwin->client.height;
306 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
308 WMRect old_geom_rect;
309 int old_head;
310 Bool same_head;
312 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
313 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
314 same_head = (wGetHeadForWindow(wwin) == old_head);
315 *x = (wwin->old_geometry.x && same_head) ? wwin->old_geometry.x : wwin->frame_x;
316 *y = (wwin->old_geometry.y && same_head) ? wwin->old_geometry.y : wwin->frame_y;
317 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
318 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
321 /* Remember geometry for unmaximizing */
322 void update_saved_geometry(WWindow *wwin)
324 /* NOT if we aren't already maximized
325 * we'll save geometry when maximizing */
326 if (!wwin->flags.maximized)
327 return;
329 /* NOT if we are fully maximized */
330 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
331 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
332 (wwin->flags.maximized & MAX_VERTICAL)))
333 return;
335 /* save the co-ordinate in the axis in which we AREN'T maximized */
336 if (wwin->flags.maximized & MAX_HORIZONTAL)
337 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
338 if (wwin->flags.maximized & MAX_VERTICAL)
339 save_old_geometry(wwin, SAVE_GEOMETRY_X);
342 #define IS_MAX_HORIZONTALLY(directions) ((directions & MAX_HORIZONTAL) | (directions & MAX_LEFTHALF) | (directions & MAX_RIGHTHALF))
343 void wMaximizeWindow(WWindow *wwin, int directions)
345 int new_x, new_y;
346 unsigned int new_width, new_height, half_scr_width;
347 int maximus_x = 0;
348 int maximus_y = 0;
349 unsigned int maximus_width = 0;
350 unsigned int maximus_height = 0;
351 WArea usableArea, totalArea;
352 Bool has_border = 1;
353 int save_directions = 0;
354 int adj_size;
356 if (!IS_RESIZABLE(wwin))
357 return;
359 if (!HAS_BORDER(wwin))
360 has_border = 0;
362 /* the size to adjust the geometry */
363 adj_size = FRAME_BORDER_WIDTH * 2 * has_border;
365 /* save old coordinates before we change the current values
366 * always if the window is not currently maximized at all
367 * but never if the window has been Maximusized */
368 if (!wwin->flags.maximized)
369 save_directions |= SAVE_GEOMETRY_ALL;
370 else if (!(wwin->flags.old_maximized & MAX_MAXIMUS)) {
371 if ((directions & MAX_VERTICAL) &&
372 !(wwin->flags.maximized & MAX_VERTICAL))
373 save_directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
374 if (IS_MAX_HORIZONTALLY(directions) &&
375 !IS_MAX_HORIZONTALLY(wwin->flags.maximized))
376 save_directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
378 if ((directions & MAX_MAXIMUS) && !wwin->flags.maximized)
379 save_directions |= SAVE_GEOMETRY_ALL;
380 save_old_geometry(wwin, save_directions);
382 totalArea.x1 = 0;
383 totalArea.y1 = 0;
384 totalArea.x2 = wwin->screen_ptr->scr_width;
385 totalArea.y2 = wwin->screen_ptr->scr_height;
386 usableArea = totalArea;
388 if (!(directions & MAX_IGNORE_XINERAMA)) {
389 WScreen *scr = wwin->screen_ptr;
390 int head;
392 if (directions & MAX_KEYBOARD)
393 head = wGetHeadForWindow(wwin);
394 else
395 head = wGetHeadForPointerLocation(scr);
397 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
400 /* remember Maximus geometry if we'll need it later */
401 if ((wwin->flags.old_maximized & MAX_MAXIMUS) || (directions & MAX_MAXIMUS))
402 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
404 /* Only save directions, not kbd or xinerama hints */
405 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
407 if (WFLAGP(wwin, full_maximize)) {
408 usableArea = totalArea;
410 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
412 if (wwin->flags.shaded) {
413 wwin->flags.skip_next_animation = 1;
414 wUnshadeWindow(wwin);
417 if (directions & MAX_HORIZONTAL) {
418 new_width = usableArea.x2 - usableArea.x1 - adj_size;
419 new_x = usableArea.x1;
420 } else if (directions & MAX_LEFTHALF) {
421 new_width = half_scr_width - adj_size;
422 new_x = usableArea.x1;
423 wwin->flags.old_maximized |= MAX_LEFTHALF;
424 wwin->flags.old_maximized &= ~MAX_RIGHTHALF;
425 } else if (directions & MAX_RIGHTHALF) {
426 new_width = half_scr_width - adj_size;
427 new_x = usableArea.x1 + half_scr_width;
428 wwin->flags.old_maximized |= MAX_RIGHTHALF;
429 wwin->flags.old_maximized &= ~MAX_LEFTHALF;
430 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
431 new_x = maximus_x;
432 new_width = maximus_width - adj_size;
433 } else if (IS_MAX_HORIZONTALLY(wwin->flags.maximized)) {
434 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
435 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
436 } else {
437 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
438 new_x = wwin->frame_x;
439 new_width = wwin->frame->core->width;
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)) {
446 new_y -= wwin->frame->top_width;
447 new_height += wwin->frame->bottom_width - 1;
449 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
450 new_y = maximus_y;
451 new_height = maximus_height - adj_size;
452 /* HACK: this will be subtracted again below */
453 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
454 } else if (wwin->flags.maximized & MAX_VERTICAL) {
455 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
456 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
457 /* HACK: this will be subtracted again below */
458 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
459 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
460 } else {
461 new_y = wwin->frame_y;
462 new_height = wwin->frame->core->height;
465 if (!WFLAGP(wwin, full_maximize)) {
466 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
469 if (directions & MAX_MAXIMUS) {
470 new_x = maximus_x;
471 new_y = maximus_y;
472 new_width = maximus_width - adj_size;
473 new_height = maximus_height - adj_size;
474 if (WFLAGP(wwin, full_maximize) && new_y == 0) {
475 new_y -= wwin->frame->top_width;
476 new_height += wwin->frame->top_width - 1;
478 wwin->maximus_x = new_x;
479 wwin->maximus_y = new_y;
480 wwin->flags.old_maximized |= MAX_MAXIMUS;
483 wWindowConstrainSize(wwin, &new_width, &new_height);
485 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
486 usableArea.y2 - usableArea.y1, &new_width, &new_height);
488 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
489 wWindowSynthConfigureNotify(wwin);
491 WMPostNotificationName(WMNChangedState, wwin, "maximize");
493 /* set maximization state */
494 wwin->flags.maximized = directions;
495 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
496 wwin->flags.maximized = MAX_MAXIMUS;
499 /* generic (un)maximizer */
500 void handleMaximize(WWindow *wwin, int directions)
502 int current = wwin->flags.maximized;
503 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
504 int effective = requested ^ current;
505 int flags = directions & ~requested;
507 if (!effective) {
508 /* allow wMaximizeWindow to restore the Maximusized size */
509 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
510 !(requested & MAX_MAXIMUS))
511 wMaximizeWindow(wwin, flags);
512 else
513 wUnmaximizeWindow(wwin);
515 else {
516 /* MAX_MAXIMUS takes precedence */
517 effective &= ~MAX_MAXIMUS;
518 if (requested & MAX_MAXIMUS) {
519 /* window was previously Maximusized then maximized */
520 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !current) {
521 wUnmaximizeWindow(wwin);
522 return;
524 else
525 effective = MAX_MAXIMUS;
527 else if (requested == (MAX_HORIZONTAL | MAX_VERTICAL))
528 effective = requested;
529 else {
530 /* handle MAX_HORIZONTAL -> MAX_(LEFT|RIGHT)HALF */
531 if (IS_MAX_HORIZONTALLY(current)) {
532 if (IS_MAX_HORIZONTALLY(requested)) {
533 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF);
534 effective |= (requested & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF));
535 if (requested & MAX_HORIZONTAL) {
536 /* restore to half maximization */
537 if (wwin->flags.old_maximized & MAX_LEFTHALF)
538 effective |= MAX_LEFTHALF;
539 else if (wwin->flags.old_maximized & MAX_RIGHTHALF)
540 effective |= MAX_RIGHTHALF;
542 /* MAX_VERTICAL is implicit with MAX_(LEFT|RIGHT)HALF */
543 else
544 effective |= MAX_VERTICAL;
545 } else {
546 /* toggling MAX_VERTICAL */
547 if ((requested & MAX_VERTICAL) &&
548 (current & MAX_VERTICAL)) {
549 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF | MAX_VERTICAL);
553 /* handle MAX_VERTICAL -> MAX_(LEFT|RIGHT)HALF */
554 if (current & MAX_VERTICAL) {
555 if ((requested & MAX_LEFTHALF) ||
556 (requested & MAX_RIGHTHALF)) {
557 effective |= MAX_VERTICAL;
560 /* toggling MAX_HORIZONTAL */
561 if ((requested & MAX_HORIZONTAL) &&
562 (current & MAX_HORIZONTAL))
563 effective &= ~MAX_HORIZONTAL;
565 wMaximizeWindow(wwin, effective | flags);
567 return;
570 /* the window boundary coordinates */
571 typedef struct {
572 int left;
573 int right;
574 int bottom;
575 int top;
576 int width;
577 int height;
578 } win_coords;
580 static void set_window_coords(WWindow *wwin, win_coords *obs)
582 obs->left = wwin->frame_x;
583 obs->top = wwin->frame_y;
584 obs->width = wwin->frame->core->width;
585 obs->height = wwin->frame->core->height;
586 obs->bottom = obs->top + obs->height;
587 obs->right = obs->left + obs->width;
591 * Maximus: tiled maximization (maximize without overlapping other windows)
593 * The original window 'orig' will be maximized to new coordinates 'new'.
594 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
596 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
597 unsigned int *new_width, unsigned int *new_height)
599 WWindow *tmp;
600 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
601 short int adjust_height;
602 int x_intsect, y_intsect;
603 /* the obstructing, original and new windows */
604 win_coords obs, orig, new;
606 /* set the original coordinate positions of the window to be Maximumized */
607 if (wwin->flags.maximized) {
608 /* window is already maximized; consider original geometry */
609 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
610 orig.bottom = orig.top + orig.height;
611 orig.right = orig.left + orig.width;
613 else
614 set_window_coords(wwin, &orig);
616 /* Try to fully maximize first, then readjust later */
617 new.left = usableArea.x1;
618 new.right = usableArea.x2;
619 new.top = usableArea.y1;
620 new.bottom = usableArea.y2;
622 if (HAS_TITLEBAR(wwin))
623 tbar_height_0 = TITLEBAR_HEIGHT;
624 if (HAS_RESIZEBAR(wwin))
625 rbar_height_0 = RESIZEBAR_HEIGHT;
626 if (HAS_BORDER(wwin))
627 bd_width_0 = FRAME_BORDER_WIDTH;
629 /* the length to be subtracted if the window has titlebar, etc */
630 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
632 tmp = wwin;
633 /* The focused window is always the last in the list */
634 while (tmp->prev) {
635 /* ignore windows in other workspaces etc */
636 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
637 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
638 tmp = tmp->prev;
639 continue;
641 tmp = tmp->prev;
643 /* Set the coordinates of obstructing window */
644 set_window_coords(tmp, &obs);
646 /* Try to maximize in the y direction first */
647 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
648 if (x_intsect != 0) {
649 /* TODO: Consider the case when coords are equal */
650 if (obs.bottom < orig.top && obs.bottom > new.top) {
651 /* w_0 is below the bottom of w_j */
652 new.top = obs.bottom + 1;
654 if (orig.bottom < obs.top && obs.top < new.bottom) {
655 /* The bottom of w_0 is above the top of w_j */
656 new.bottom = obs.top - 1;
661 tmp = wwin;
662 while (tmp->prev) {
663 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
664 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
665 tmp = tmp->prev;
666 continue;
668 tmp = tmp->prev;
670 set_window_coords(tmp, &obs);
673 * Use the new.top and new.height instead of original values
674 * as they may have different intersections with the obstructing windows
676 new.height = new.bottom - new.top - adjust_height;
677 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
678 if (y_intsect != 0) {
679 if (obs.right < orig.left && obs.right > new.left) {
680 /* w_0 is completely to the right of w_j */
681 new.left = obs.right + 1;
683 if (orig.right < obs.left && obs.left < new.right) {
684 /* w_0 is completely to the left of w_j */
685 new.right = obs.left - 1;
690 *new_x = new.left;
691 *new_y = new.top;
692 /* xcalc needs -7 here, but other apps don't */
693 *new_height = new.bottom - new.top - adjust_height - 1;;
694 *new_width = new.right - new.left;
697 void wUnmaximizeWindow(WWindow *wwin)
699 int x, y, w, h;
701 if (!wwin->flags.maximized)
702 return;
704 if (wwin->flags.shaded) {
705 wwin->flags.skip_next_animation = 1;
706 wUnshadeWindow(wwin);
709 /* Use old coordinates if they are set, current values otherwise */
710 remember_geometry(wwin, &x, &y, &w, &h);
712 /* unMaximusize relative to original position */
713 if (wwin->flags.maximized & MAX_MAXIMUS) {
714 x += wwin->frame_x - wwin->maximus_x;
715 y += wwin->frame_y - wwin->maximus_y;
718 wwin->flags.maximized = 0;
719 wwin->flags.old_maximized = 0;
720 wWindowConfigure(wwin, x, y, w, h);
721 wWindowSynthConfigureNotify(wwin);
723 WMPostNotificationName(WMNChangedState, wwin, "maximize");
726 void wFullscreenWindow(WWindow *wwin)
728 int head;
729 WMRect rect;
731 if (wwin->flags.fullscreen)
732 return;
734 wwin->flags.fullscreen = True;
736 wWindowConfigureBorders(wwin);
738 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
740 wwin->bfs_geometry.x = wwin->frame_x;
741 wwin->bfs_geometry.y = wwin->frame_y;
742 wwin->bfs_geometry.width = wwin->frame->core->width;
743 wwin->bfs_geometry.height = wwin->frame->core->height;
745 head = wGetHeadForWindow(wwin);
746 rect = wGetRectForHead(wwin->screen_ptr, head);
747 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
749 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
752 void wUnfullscreenWindow(WWindow *wwin)
754 if (!wwin->flags.fullscreen)
755 return;
757 wwin->flags.fullscreen = False;
759 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
760 if (WFLAGP(wwin, sunken)) {
761 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
762 } else if (WFLAGP(wwin, floating)) {
763 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
764 } else {
765 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
769 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
770 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
772 wWindowConfigureBorders(wwin);
774 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
775 wFrameWindowPaint(wwin->frame);
778 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
781 #ifdef ANIMATIONS
782 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
784 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
785 float cx, cy, cw, ch;
786 float xstep, ystep, wstep, hstep;
787 XPoint points[5];
788 float dx, dch, midy;
789 float angle, final_angle, delta;
791 xstep = (float)(fx - x) / steps;
792 ystep = (float)(fy - y) / steps;
793 wstep = (float)(fw - w) / steps;
794 hstep = (float)(fh - h) / steps;
796 cx = (float)x;
797 cy = (float)y;
798 cw = (float)w;
799 ch = (float)h;
801 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
802 delta = (float)(final_angle / FRAMES);
803 for (angle = 0;; angle += delta) {
804 if (angle > final_angle)
805 angle = final_angle;
807 dx = (cw / 10) - ((cw / 5) * sin(angle));
808 dch = (ch / 2) * cos(angle);
809 midy = cy + (ch / 2);
811 points[0].x = cx + dx;
812 points[0].y = midy - dch;
813 points[1].x = cx + cw - dx;
814 points[1].y = points[0].y;
815 points[2].x = cx + cw + dx;
816 points[2].y = midy + dch;
817 points[3].x = cx - dx;
818 points[3].y = points[2].y;
819 points[4].x = points[0].x;
820 points[4].y = points[0].y;
822 XGrabServer(dpy);
823 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
824 XFlush(dpy);
825 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
827 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
828 XUngrabServer(dpy);
829 cx += xstep;
830 cy += ystep;
831 cw += wstep;
832 ch += hstep;
833 if (angle >= final_angle)
834 break;
837 XFlush(dpy);
840 #undef FRAMES
842 static void
843 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
845 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
846 float cx, cy, cw, ch;
847 float xstep, ystep, wstep, hstep;
848 XPoint points[5];
849 float angle, final_angle, a, d, delta;
851 x += w / 2;
852 y += h / 2;
853 fx += fw / 2;
854 fy += fh / 2;
856 xstep = (float)(fx - x) / steps;
857 ystep = (float)(fy - y) / steps;
858 wstep = (float)(fw - w) / steps;
859 hstep = (float)(fh - h) / steps;
861 cx = (float)x;
862 cy = (float)y;
863 cw = (float)w;
864 ch = (float)h;
866 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
867 delta = (float)(final_angle / FRAMES);
868 for (angle = 0;; angle += delta) {
869 if (angle > final_angle)
870 angle = final_angle;
872 a = atan(ch / cw);
873 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
875 points[0].x = cx + cos(angle - a) * d;
876 points[0].y = cy + sin(angle - a) * d;
877 points[1].x = cx + cos(angle + a) * d;
878 points[1].y = cy + sin(angle + a) * d;
879 points[2].x = cx + cos(angle - a + WM_PI) * d;
880 points[2].y = cy + sin(angle - a + WM_PI) * d;
881 points[3].x = cx + cos(angle + a + WM_PI) * d;
882 points[3].y = cy + sin(angle + a + WM_PI) * d;
883 points[4].x = cx + cos(angle - a) * d;
884 points[4].y = cy + sin(angle - a) * d;
885 XGrabServer(dpy);
886 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
887 XFlush(dpy);
888 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
890 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
891 XUngrabServer(dpy);
892 cx += xstep;
893 cy += ystep;
894 cw += wstep;
895 ch += hstep;
896 if (angle >= final_angle)
897 break;
900 XFlush(dpy);
903 #undef FRAMES
905 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
907 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
908 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
909 float xstep, ystep, wstep, hstep;
910 int i, j;
912 xstep = (float)(fx - x) / steps;
913 ystep = (float)(fy - y) / steps;
914 wstep = (float)(fw - w) / steps;
915 hstep = (float)(fh - h) / steps;
917 for (j = 0; j < FRAMES; j++) {
918 cx[j] = (float)x;
919 cy[j] = (float)y;
920 cw[j] = (float)w;
921 ch[j] = (float)h;
923 XGrabServer(dpy);
924 for (i = 0; i < steps; i++) {
925 for (j = 0; j < FRAMES; j++) {
926 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
927 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
929 XFlush(dpy);
930 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
932 for (j = 0; j < FRAMES; j++) {
933 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
934 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
935 if (j < FRAMES - 1) {
936 cx[j] = cx[j + 1];
937 cy[j] = cy[j + 1];
938 cw[j] = cw[j + 1];
939 ch[j] = ch[j + 1];
940 } else {
941 cx[j] += xstep;
942 cy[j] += ystep;
943 cw[j] += wstep;
944 ch[j] += hstep;
949 for (j = 0; j < FRAMES; j++) {
950 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
952 XFlush(dpy);
953 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
955 for (j = 0; j < FRAMES; j++) {
956 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
959 XUngrabServer(dpy);
962 #undef FRAMES
964 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
966 int style = wPreferences.iconification_style; /* Catch the value */
967 int steps;
969 if (style == WIS_NONE)
970 return;
972 if (style == WIS_RANDOM) {
973 style = rand() % 3;
976 switch (style) {
977 case WIS_TWIST:
978 steps = MINIATURIZE_ANIMATION_STEPS_T;
979 if (steps > 0)
980 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
981 break;
982 case WIS_FLIP:
983 steps = MINIATURIZE_ANIMATION_STEPS_F;
984 if (steps > 0)
985 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
986 break;
987 case WIS_ZOOM:
988 default:
989 steps = MINIATURIZE_ANIMATION_STEPS_Z;
990 if (steps > 0)
991 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
992 break;
995 #endif /* ANIMATIONS */
997 static void flushExpose(void)
999 XEvent tmpev;
1001 while (XCheckTypedEvent(dpy, Expose, &tmpev))
1002 WMHandleEvent(&tmpev);
1003 XSync(dpy, 0);
1006 static void unmapTransientsFor(WWindow *wwin)
1008 WWindow *tmp;
1010 tmp = wwin->screen_ptr->focused_window;
1011 while (tmp) {
1012 /* unmap the transients for this transient */
1013 if (tmp != wwin && tmp->transient_for == wwin->client_win
1014 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1015 unmapTransientsFor(tmp);
1016 tmp->flags.miniaturized = 1;
1017 if (!tmp->flags.shaded) {
1018 wWindowUnmap(tmp);
1019 } else {
1020 XUnmapWindow(dpy, tmp->frame->core->window);
1023 if (!tmp->flags.shaded)
1025 wClientSetState(tmp, IconicState, None);
1027 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1029 tmp = tmp->prev;
1033 static void mapTransientsFor(WWindow *wwin)
1035 WWindow *tmp;
1037 tmp = wwin->screen_ptr->focused_window;
1038 while (tmp) {
1039 /* recursively map the transients for this transient */
1040 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1041 && tmp->icon == NULL) {
1042 mapTransientsFor(tmp);
1043 tmp->flags.miniaturized = 0;
1044 if (!tmp->flags.shaded) {
1045 wWindowMap(tmp);
1046 } else {
1047 XMapWindow(dpy, tmp->frame->core->window);
1049 tmp->flags.semi_focused = 0;
1051 if (!tmp->flags.shaded)
1053 wClientSetState(tmp, NormalState, None);
1055 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1057 tmp = tmp->prev;
1061 static WWindow *recursiveTransientFor(WWindow * wwin)
1063 int i;
1065 if (!wwin)
1066 return None;
1068 /* hackish way to detect transient_for cycle */
1069 i = wwin->screen_ptr->window_count + 1;
1071 while (wwin && wwin->transient_for != None && i > 0) {
1072 wwin = wWindowFor(wwin->transient_for);
1073 i--;
1075 if (i == 0 && wwin) {
1076 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1077 return NULL;
1080 return wwin;
1083 void wIconifyWindow(WWindow * wwin)
1085 XWindowAttributes attribs;
1086 int present;
1088 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1089 return; /* the window doesn't exist anymore */
1091 if (wwin->flags.miniaturized)
1092 return; /* already miniaturized */
1094 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1095 WWindow *owner = wWindowFor(wwin->transient_for);
1097 if (owner && owner->flags.miniaturized)
1098 return;
1101 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1103 /* if the window is in another workspace, simplify process */
1104 if (present) {
1105 /* icon creation may take a while */
1106 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1107 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1108 GrabModeAsync, None, None, CurrentTime);
1111 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1112 if (!wwin->flags.icon_moved)
1113 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1115 wwin->icon = icon_create_for_wwindow(wwin);
1116 wwin->icon->mapped = 1;
1119 wwin->flags.miniaturized = 1;
1120 wwin->flags.mapped = 0;
1122 /* unmap transients */
1123 unmapTransientsFor(wwin);
1125 if (present) {
1126 XUngrabPointer(dpy, CurrentTime);
1127 wWindowUnmap(wwin);
1128 /* let all Expose events arrive so that we can repaint
1129 * something before the animation starts (and the server is grabbed) */
1130 XSync(dpy, 0);
1132 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1133 wClientSetState(wwin, IconicState, None);
1134 else
1135 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1137 flushExpose();
1138 #ifdef ANIMATIONS
1139 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1140 && !wPreferences.no_animations) {
1141 int ix, iy, iw, ih;
1143 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1144 ix = wwin->icon_x;
1145 iy = wwin->icon_y;
1146 iw = wwin->icon->core->width;
1147 ih = wwin->icon->core->height;
1148 } else {
1149 if (wwin->flags.net_handle_icon) {
1150 ix = wwin->icon_x;
1151 iy = wwin->icon_y;
1152 iw = wwin->icon_w;
1153 ih = wwin->icon_h;
1154 } else {
1155 ix = 0;
1156 iy = 0;
1157 iw = wwin->screen_ptr->scr_width;
1158 ih = wwin->screen_ptr->scr_height;
1161 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1162 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1164 #endif
1167 wwin->flags.skip_next_animation = 0;
1169 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1170 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1171 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1172 XMapWindow(dpy, wwin->icon->core->window);
1174 AddToStackList(wwin->icon->core);
1175 wLowerFrame(wwin->icon->core);
1178 if (present) {
1179 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1182 * It doesn't seem to be working and causes button event hangup
1183 * when deiconifying a transient window.
1184 setupIconGrabs(wwin->icon);
1186 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1187 && wPreferences.focus_mode == WKF_CLICK) {
1188 WWindow *tmp;
1190 tmp = wwin->prev;
1191 while (tmp) {
1192 if (!WFLAGP(tmp, no_focusable)
1193 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1194 && (wwin->frame->workspace == tmp->frame->workspace))
1195 break;
1196 tmp = tmp->prev;
1198 wSetFocusTo(wwin->screen_ptr, tmp);
1199 } else if (wPreferences.focus_mode != WKF_CLICK) {
1200 wSetFocusTo(wwin->screen_ptr, NULL);
1202 #ifdef ANIMATIONS
1203 if (!wwin->screen_ptr->flags.startup) {
1204 /* Catch up with events not processed while animation was running */
1205 Window clientwin = wwin->client_win;
1207 ProcessPendingEvents();
1209 /* the window can disappear while ProcessPendingEvents() runs */
1210 if (!wWindowFor(clientwin)) {
1211 return;
1214 #endif
1217 /* maybe we want to do this regardless of net_handle_icon
1218 * it seems to me we might break behaviour this way.
1220 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1221 && !wwin->flags.net_handle_icon)
1222 wIconSelect(wwin->icon);
1224 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1226 if (wPreferences.auto_arrange_icons)
1227 wArrangeIcons(wwin->screen_ptr, True);
1230 void wDeiconifyWindow(WWindow *wwin)
1232 /* Let's avoid changing workspace while deiconifying */
1233 ignore_wks_change = 1;
1235 /* we're hiding for show_desktop */
1236 int netwm_hidden = wwin->flags.net_show_desktop &&
1237 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1239 if (!netwm_hidden)
1240 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1242 if (!wwin->flags.miniaturized)
1243 return;
1245 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1246 WWindow *owner = recursiveTransientFor(wwin);
1248 if (owner && owner->flags.miniaturized) {
1249 wDeiconifyWindow(owner);
1250 wSetFocusTo(wwin->screen_ptr, wwin);
1251 wRaiseFrame(wwin->frame->core);
1252 return;
1256 wwin->flags.miniaturized = 0;
1258 if (!netwm_hidden && !wwin->flags.shaded)
1259 wwin->flags.mapped = 1;
1261 if (!netwm_hidden || wPreferences.sticky_icons) {
1262 /* maybe we want to do this regardless of net_handle_icon
1263 * it seems to me we might break behaviour this way.
1265 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1266 && wwin->icon != NULL) {
1267 if (wwin->icon->selected)
1268 wIconSelect(wwin->icon);
1270 XUnmapWindow(dpy, wwin->icon->core->window);
1274 /* if the window is in another workspace, do it silently */
1275 if (!netwm_hidden) {
1276 #ifdef ANIMATIONS
1277 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1278 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1279 int ix, iy, iw, ih;
1281 if (!wPreferences.disable_miniwindows
1282 && !wwin->flags.net_handle_icon) {
1283 ix = wwin->icon_x;
1284 iy = wwin->icon_y;
1285 iw = wwin->icon->core->width;
1286 ih = wwin->icon->core->height;
1287 } else {
1288 if (wwin->flags.net_handle_icon) {
1289 ix = wwin->icon_x;
1290 iy = wwin->icon_y;
1291 iw = wwin->icon_w;
1292 ih = wwin->icon_h;
1293 } else {
1294 ix = 0;
1295 iy = 0;
1296 iw = wwin->screen_ptr->scr_width;
1297 ih = wwin->screen_ptr->scr_height;
1300 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1301 wwin->frame_x, wwin->frame_y,
1302 wwin->frame->core->width, wwin->frame->core->height);
1304 #endif /* ANIMATIONS */
1305 wwin->flags.skip_next_animation = 0;
1306 XGrabServer(dpy);
1307 if (!wwin->flags.shaded)
1308 XMapWindow(dpy, wwin->client_win);
1310 XMapWindow(dpy, wwin->frame->core->window);
1311 wRaiseFrame(wwin->frame->core);
1312 if (!wwin->flags.shaded)
1313 wClientSetState(wwin, NormalState, None);
1315 mapTransientsFor(wwin);
1318 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1319 && !wwin->flags.net_handle_icon) {
1320 RemoveFromStackList(wwin->icon->core);
1321 /* removeIconGrabs(wwin->icon); */
1322 wIconDestroy(wwin->icon);
1323 wwin->icon = NULL;
1326 if (!netwm_hidden) {
1327 XUngrabServer(dpy);
1329 wSetFocusTo(wwin->screen_ptr, wwin);
1331 #ifdef ANIMATIONS
1332 if (!wwin->screen_ptr->flags.startup) {
1333 /* Catch up with events not processed while animation was running */
1334 Window clientwin = wwin->client_win;
1336 ProcessPendingEvents();
1338 /* the window can disappear while ProcessPendingEvents() runs */
1339 if (!wWindowFor(clientwin))
1340 return;
1342 #endif
1345 if (wPreferences.auto_arrange_icons)
1346 wArrangeIcons(wwin->screen_ptr, True);
1348 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1350 /* In case we were shaded and iconified, also unshade */
1351 if (!netwm_hidden)
1352 wUnshadeWindow(wwin);
1354 ignore_wks_change = 0;
1357 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1359 if (wwin->flags.miniaturized) {
1360 if (wwin->icon) {
1361 XUnmapWindow(dpy, wwin->icon->core->window);
1362 wwin->icon->mapped = 0;
1364 wwin->flags.hidden = 1;
1366 WMPostNotificationName(WMNChangedState, wwin, "hide");
1367 return;
1370 if (wwin->flags.inspector_open) {
1371 wHideInspectorForWindow(wwin);
1374 wwin->flags.hidden = 1;
1375 wWindowUnmap(wwin);
1377 wClientSetState(wwin, IconicState, icon->icon_win);
1378 flushExpose();
1380 #ifdef ANIMATIONS
1381 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1382 !wwin->flags.skip_next_animation && animate) {
1383 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1384 wwin->frame->core->width, wwin->frame->core->height,
1385 icon_x, icon_y, icon->core->width, icon->core->height);
1387 #endif
1388 wwin->flags.skip_next_animation = 0;
1390 WMPostNotificationName(WMNChangedState, wwin, "hide");
1393 void wHideAll(WScreen *scr)
1395 WWindow *wwin;
1396 WWindow **windows;
1397 WMenu *menu;
1398 unsigned int wcount = 0;
1399 int i;
1401 if (!scr)
1402 return;
1404 menu = scr->switch_menu;
1406 windows = wmalloc(sizeof(WWindow *));
1408 if (menu != NULL) {
1409 for (i = 0; i < menu->entry_no; i++) {
1410 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1411 wcount++;
1412 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1414 } else {
1415 wwin = scr->focused_window;
1417 while (wwin) {
1418 windows[wcount] = wwin;
1419 wcount++;
1420 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1421 wwin = wwin->prev;
1426 for (i = 0; i < wcount; i++) {
1427 wwin = windows[i];
1428 if (wwin->frame->workspace == scr->current_workspace
1429 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1430 && !wwin->flags.internal_window
1431 && !WFLAGP(wwin, no_miniaturizable)
1433 wwin->flags.skip_next_animation = 1;
1434 wIconifyWindow(wwin);
1438 wfree(windows);
1441 void wHideOtherApplications(WWindow *awin)
1443 WWindow *wwin;
1444 WApplication *tapp;
1446 if (!awin)
1447 return;
1448 wwin = awin->screen_ptr->focused_window;
1450 while (wwin) {
1451 if (wwin != awin
1452 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1453 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1454 && !wwin->flags.internal_window
1455 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1457 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1458 if (!WFLAGP(wwin, no_miniaturizable)) {
1459 wwin->flags.skip_next_animation = 1;
1460 wIconifyWindow(wwin);
1462 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1463 tapp = wApplicationOf(wwin->main_window);
1464 if (tapp) {
1465 tapp->flags.skip_next_animation = 1;
1466 wHideApplication(tapp);
1467 } else {
1468 if (!WFLAGP(wwin, no_miniaturizable)) {
1469 wwin->flags.skip_next_animation = 1;
1470 wIconifyWindow(wwin);
1475 wwin = wwin->prev;
1478 wSetFocusTo(awin->screen_ptr, awin);
1482 void wHideApplication(WApplication *wapp)
1484 WScreen *scr;
1485 WWindow *wlist;
1486 int hadfocus;
1487 int animate;
1489 if (!wapp) {
1490 wwarning("trying to hide a non grouped window");
1491 return;
1493 if (!wapp->main_window_desc) {
1494 wwarning("group leader not found for window group");
1495 return;
1497 scr = wapp->main_window_desc->screen_ptr;
1498 hadfocus = 0;
1499 wlist = scr->focused_window;
1500 if (!wlist)
1501 return;
1503 if (wlist->main_window == wapp->main_window)
1504 wapp->last_focused = wlist;
1505 else
1506 wapp->last_focused = NULL;
1508 animate = !wapp->flags.skip_next_animation;
1510 while (wlist) {
1511 if (wlist->main_window == wapp->main_window) {
1512 if (wlist->flags.focused) {
1513 hadfocus = 1;
1515 if (wapp->app_icon) {
1516 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1517 wapp->app_icon->y_pos, wlist, animate);
1518 animate = False;
1521 wlist = wlist->prev;
1524 wapp->flags.skip_next_animation = 0;
1526 if (hadfocus) {
1527 if (wPreferences.focus_mode == WKF_CLICK) {
1528 wlist = scr->focused_window;
1529 while (wlist) {
1530 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1531 && (wlist->flags.mapped || wlist->flags.shaded))
1532 break;
1533 wlist = wlist->prev;
1535 wSetFocusTo(scr, wlist);
1536 } else {
1537 wSetFocusTo(scr, NULL);
1541 wapp->flags.hidden = 1;
1543 if (wPreferences.auto_arrange_icons)
1544 wArrangeIcons(scr, True);
1546 #ifdef HIDDENDOT
1547 if (wapp->app_icon)
1548 wAppIconPaint(wapp->app_icon, False);
1549 #endif
1552 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1554 if (bringToCurrentWS)
1555 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1557 wwin->flags.hidden = 0;
1559 #ifdef ANIMATIONS
1560 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1561 animateResize(wwin->screen_ptr, icon_x, icon_y,
1562 icon->core->width, icon->core->height,
1563 wwin->frame_x, wwin->frame_y,
1564 wwin->frame->core->width, wwin->frame->core->height);
1566 #endif
1567 wwin->flags.skip_next_animation = 0;
1568 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1569 XMapWindow(dpy, wwin->client_win);
1570 XMapWindow(dpy, wwin->frame->core->window);
1571 wClientSetState(wwin, NormalState, None);
1572 wwin->flags.mapped = 1;
1573 wRaiseFrame(wwin->frame->core);
1575 if (wwin->flags.inspector_open) {
1576 wUnhideInspectorForWindow(wwin);
1579 WMPostNotificationName(WMNChangedState, wwin, "hide");
1582 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1584 WScreen *scr;
1585 WWindow *wlist, *next;
1586 WWindow *focused = NULL;
1587 int animate;
1589 if (!wapp)
1590 return;
1592 scr = wapp->main_window_desc->screen_ptr;
1593 wlist = scr->focused_window;
1594 if (!wlist)
1595 return;
1597 /* goto beginning of list */
1598 while (wlist->prev)
1599 wlist = wlist->prev;
1601 animate = !wapp->flags.skip_next_animation;
1603 while (wlist) {
1604 next = wlist->next;
1606 if (wlist->main_window == wapp->main_window) {
1607 if (wlist->flags.focused)
1608 focused = wlist;
1609 else if (!focused || !focused->flags.focused)
1610 focused = wlist;
1612 if (wlist->flags.miniaturized) {
1613 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1614 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1615 if (!wlist->icon->mapped) {
1616 int x, y;
1618 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1619 if (wlist->icon_x != x || wlist->icon_y != y) {
1620 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1622 wlist->icon_x = x;
1623 wlist->icon_y = y;
1624 XMapWindow(dpy, wlist->icon->core->window);
1625 wlist->icon->mapped = 1;
1627 wRaiseFrame(wlist->icon->core);
1629 if (bringToCurrentWS)
1630 wWindowChangeWorkspace(wlist, scr->current_workspace);
1631 wlist->flags.hidden = 0;
1632 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1633 wDeiconifyWindow(wlist);
1635 WMPostNotificationName(WMNChangedState, wlist, "hide");
1636 } else if (wlist->flags.shaded) {
1637 if (bringToCurrentWS)
1638 wWindowChangeWorkspace(wlist, scr->current_workspace);
1639 wlist->flags.hidden = 0;
1640 wRaiseFrame(wlist->frame->core);
1641 if (wlist->frame->workspace == scr->current_workspace) {
1642 XMapWindow(dpy, wlist->frame->core->window);
1643 if (miniwindows) {
1644 wUnshadeWindow(wlist);
1647 WMPostNotificationName(WMNChangedState, wlist, "hide");
1648 } else if (wlist->flags.hidden) {
1649 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1650 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1651 animate = False;
1652 } else {
1653 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1654 wWindowChangeWorkspace(wlist, scr->current_workspace);
1656 wRaiseFrame(wlist->frame->core);
1659 wlist = next;
1662 wapp->flags.skip_next_animation = 0;
1663 wapp->flags.hidden = 0;
1665 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1666 wRaiseFrame(wapp->last_focused->frame->core);
1667 wSetFocusTo(scr, wapp->last_focused);
1668 } else if (focused) {
1669 wSetFocusTo(scr, focused);
1671 wapp->last_focused = NULL;
1672 if (wPreferences.auto_arrange_icons)
1673 wArrangeIcons(scr, True);
1675 #ifdef HIDDENDOT
1676 wAppIconPaint(wapp->app_icon, False);
1677 #endif
1680 void wShowAllWindows(WScreen *scr)
1682 WWindow *wwin, *old_foc;
1683 WApplication *wapp;
1685 old_foc = wwin = scr->focused_window;
1686 while (wwin) {
1687 if (!wwin->flags.internal_window &&
1688 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1689 if (wwin->flags.miniaturized) {
1690 wwin->flags.skip_next_animation = 1;
1691 wDeiconifyWindow(wwin);
1692 } else if (wwin->flags.hidden) {
1693 wapp = wApplicationOf(wwin->main_window);
1694 if (wapp) {
1695 wUnhideApplication(wapp, False, False);
1696 } else {
1697 wwin->flags.skip_next_animation = 1;
1698 wDeiconifyWindow(wwin);
1702 wwin = wwin->prev;
1704 wSetFocusTo(scr, old_foc);
1705 /*wRaiseFrame(old_foc->frame->core); */
1708 void wRefreshDesktop(WScreen *scr)
1710 Window win;
1711 XSetWindowAttributes attr;
1713 attr.backing_store = NotUseful;
1714 attr.save_under = False;
1715 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1716 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1717 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1718 XMapRaised(dpy, win);
1719 XDestroyWindow(dpy, win);
1720 XFlush(dpy);
1723 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1725 WWindow *wwin;
1726 WAppIcon *aicon;
1728 int head;
1729 const int heads = wXineramaHeads(scr);
1731 struct HeadVars {
1732 int pf; /* primary axis */
1733 int sf; /* secondary axis */
1734 int fullW;
1735 int fullH;
1736 int pi, si;
1737 int sx1, sx2, sy1, sy2; /* screen boundary */
1738 int sw, sh;
1739 int xo, yo;
1740 int xs, ys;
1741 } *vars;
1743 int isize = wPreferences.icon_size;
1745 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1747 for (head = 0; head < heads; ++head) {
1748 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1749 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1750 vars[head].pi = vars[head].si = 0;
1751 vars[head].sx1 = rect.pos.x;
1752 vars[head].sy1 = rect.pos.y;
1753 vars[head].sw = rect.size.width;
1754 vars[head].sh = rect.size.height;
1755 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1756 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1757 vars[head].sw = isize * (vars[head].sw / isize);
1758 vars[head].sh = isize * (vars[head].sh / isize);
1759 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1760 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1762 /* icon yard boundaries */
1763 if (wPreferences.icon_yard & IY_VERT) {
1764 vars[head].pf = vars[head].fullH;
1765 vars[head].sf = vars[head].fullW;
1766 } else {
1767 vars[head].pf = vars[head].fullW;
1768 vars[head].sf = vars[head].fullH;
1770 if (wPreferences.icon_yard & IY_RIGHT) {
1771 vars[head].xo = vars[head].sx2 - isize;
1772 vars[head].xs = -1;
1773 } else {
1774 vars[head].xo = vars[head].sx1;
1775 vars[head].xs = 1;
1777 if (wPreferences.icon_yard & IY_TOP) {
1778 vars[head].yo = vars[head].sy1;
1779 vars[head].ys = 1;
1780 } else {
1781 vars[head].yo = vars[head].sy2 - isize;
1782 vars[head].ys = -1;
1786 #define X ((wPreferences.icon_yard & IY_VERT) \
1787 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1788 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1790 #define Y ((wPreferences.icon_yard & IY_VERT) \
1791 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1792 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1794 /* arrange application icons */
1795 aicon = scr->app_icon_list;
1796 /* reverse them to avoid unnecessarily sliding of icons */
1797 while (aicon && aicon->next)
1798 aicon = aicon->next;
1800 while (aicon) {
1801 if (!aicon->docked) {
1802 /* CHECK: can icon be NULL here ? */
1803 /* The intention here is to place the AppIcon on the head that
1804 * contains most of the applications _main_ window. */
1805 head = wGetHeadForWindow(aicon->icon->owner);
1807 if (aicon->x_pos != X || aicon->y_pos != Y) {
1808 #ifdef ANIMATIONS
1809 if (!wPreferences.no_animations)
1810 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1811 #endif /* ANIMATIONS */
1813 wAppIconMove(aicon, X, Y);
1814 vars[head].pi++;
1815 if (vars[head].pi >= vars[head].pf) {
1816 vars[head].pi = 0;
1817 vars[head].si++;
1820 aicon = aicon->prev;
1823 /* arrange miniwindows */
1824 wwin = scr->focused_window;
1825 /* reverse them to avoid unnecessarily shuffling */
1826 while (wwin && wwin->prev)
1827 wwin = wwin->prev;
1829 while (wwin) {
1830 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1831 (wwin->frame->workspace == scr->current_workspace ||
1832 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1834 head = wGetHeadForWindow(wwin);
1836 if (arrangeAll || !wwin->flags.icon_moved) {
1837 if (wwin->icon_x != X || wwin->icon_y != Y)
1838 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1840 wwin->icon_x = X;
1841 wwin->icon_y = Y;
1843 vars[head].pi++;
1844 if (vars[head].pi >= vars[head].pf) {
1845 vars[head].pi = 0;
1846 vars[head].si++;
1850 if (arrangeAll) {
1851 wwin->flags.icon_moved = 0;
1853 /* we reversed the order, so we use next */
1854 wwin = wwin->next;
1857 wfree(vars);
1860 void wSelectWindow(WWindow *wwin, Bool flag)
1862 WScreen *scr = wwin->screen_ptr;
1864 if (flag) {
1865 wwin->flags.selected = 1;
1866 if (wwin->frame->selected_border_pixel)
1867 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1868 else
1869 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1871 if (!HAS_BORDER(wwin)) {
1872 XSetWindowBorderWidth(dpy, wwin->frame->core->window, FRAME_BORDER_WIDTH);
1875 if (!scr->selected_windows)
1876 scr->selected_windows = WMCreateArray(4);
1877 WMAddToArray(scr->selected_windows, wwin);
1878 } else {
1879 wwin->flags.selected = 0;
1880 if (wwin->frame->border_pixel)
1881 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1882 else
1883 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1885 if (!HAS_BORDER(wwin)) {
1886 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1889 if (scr->selected_windows) {
1890 WMRemoveFromArray(scr->selected_windows, wwin);
1895 void wMakeWindowVisible(WWindow *wwin)
1897 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1898 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1900 if (wwin->flags.shaded) {
1901 wUnshadeWindow(wwin);
1903 if (wwin->flags.hidden) {
1904 WApplication *app;
1906 app = wApplicationOf(wwin->main_window);
1907 if (app) {
1908 /* trick to get focus to this window */
1909 app->last_focused = wwin;
1910 wUnhideApplication(app, False, False);
1913 if (wwin->flags.miniaturized) {
1914 wDeiconifyWindow(wwin);
1915 } else {
1916 if (!WFLAGP(wwin, no_focusable))
1917 wSetFocusTo(wwin->screen_ptr, wwin);
1918 wRaiseFrame(wwin->frame->core);
1923 * Do the animation while shading (called with what = SHADE)
1924 * or unshading (what = UNSHADE).
1926 #ifdef ANIMATIONS
1927 static void shade_animate(WWindow *wwin, Bool what)
1929 int y, s, w, h;
1930 time_t time0 = time(NULL);
1932 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1933 return;
1935 switch(what) {
1936 case SHADE:
1937 if (!wwin->screen_ptr->flags.startup) {
1938 /* do the shading animation */
1939 h = wwin->frame->core->height;
1940 s = h / SHADE_STEPS;
1941 if (s < 1)
1942 s = 1;
1943 w = wwin->frame->core->width;
1944 y = wwin->frame->top_width;
1945 while (h > wwin->frame->top_width + 1) {
1946 XMoveWindow(dpy, wwin->client_win, 0, y);
1947 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1948 XFlush(dpy);
1950 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1951 break;
1953 if (SHADE_DELAY > 0) {
1954 wusleep(SHADE_DELAY * 1000L);
1955 } else {
1956 wusleep(10);
1958 h -= s;
1959 y -= s;
1961 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1963 break;
1965 case UNSHADE:
1966 h = wwin->frame->top_width + wwin->frame->bottom_width;
1967 y = wwin->frame->top_width - wwin->client.height;
1968 s = abs(y) / SHADE_STEPS;
1969 if (s < 1)
1970 s = 1;
1971 w = wwin->frame->core->width;
1972 XMoveWindow(dpy, wwin->client_win, 0, y);
1973 if (s > 0) {
1974 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1975 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1976 XMoveWindow(dpy, wwin->client_win, 0, y);
1977 XFlush(dpy);
1978 if (SHADE_DELAY > 0) {
1979 wusleep(SHADE_DELAY * 2000L / 3);
1980 } else {
1981 wusleep(10);
1983 h += s;
1984 y += s;
1986 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1987 break;
1990 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1991 break;
1994 #endif