wmaker: Changed math on floating point by integer operation
[wmaker-crm.git] / src / actions.c
blobe492419aa4a64da6fef1f90facfd2caee59c4f34
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
33 #include "WindowMaker.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "colormap.h"
39 #include "application.h"
40 #include "actions.h"
41 #include "stacking.h"
42 #include "appicon.h"
43 #include "dock.h"
44 #include "appmenu.h"
45 #include "winspector.h"
46 #include "workspace.h"
47 #include "xinerama.h"
48 #include "usermenu.h"
49 #include "placement.h"
50 #include "misc.h"
51 #include "event.h"
53 /****** Global Variables ******/
55 int ignore_wks_change = 0;
56 extern Time LastTimestamp;
57 extern Time LastFocusChange;
58 extern WPreferences wPreferences;
59 extern Atom _XA_WM_TAKE_FOCUS;
61 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
62 unsigned int *new_width, unsigned int *new_height);
63 static void save_old_geometry(WWindow *wwin, int directions);
65 /******* Local Variables *******/
66 static struct {
67 int steps;
68 int delay;
69 } shadePars[5] = {
71 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
72 SHADE_STEPS_F, SHADE_DELAY_F}, {
73 SHADE_STEPS_M, SHADE_DELAY_M}, {
74 SHADE_STEPS_S, SHADE_DELAY_S}, {
75 SHADE_STEPS_US, SHADE_DELAY_US}};
77 #define UNSHADE 0
78 #define SHADE 1
79 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
80 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
82 static int compareTimes(Time t1, Time t2)
84 Time diff;
85 if (t1 == t2)
86 return 0;
87 diff = t1 - t2;
88 return (diff < 60000) ? 1 : -1;
91 #ifdef ANIMATIONS
92 static void shade_animate(WWindow *wwin, Bool what);
93 #else
94 static void shade_animate(WWindow *wwin, Bool what) { }
95 #endif
98 *----------------------------------------------------------------------
99 * wSetFocusTo--
100 * Changes the window focus to the one passed as argument.
101 * If the window to focus is not already focused, it will be brought
102 * to the head of the list of windows. Previously focused window is
103 * unfocused.
105 * Side effects:
106 * Window list may be reordered and the window focus is changed.
108 *----------------------------------------------------------------------
110 void wSetFocusTo(WScreen *scr, WWindow *wwin)
112 static WScreen *old_scr = NULL;
114 WWindow *old_focused;
115 WWindow *focused = scr->focused_window;
116 Time timestamp = LastTimestamp;
117 WApplication *oapp = NULL, *napp = NULL;
118 int wasfocused;
120 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
121 return;
123 if (!old_scr)
124 old_scr = scr;
126 old_focused = old_scr->focused_window;
128 LastFocusChange = timestamp;
130 if (old_focused)
131 oapp = wApplicationOf(old_focused->main_window);
133 if (wwin == NULL) {
134 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
135 if (old_focused)
136 wWindowUnfocus(old_focused);
138 if (oapp) {
139 wAppMenuUnmap(oapp->menu);
140 if (wPreferences.highlight_active_app)
141 wApplicationDeactivate(oapp);
144 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
145 return;
148 if (old_scr != scr && old_focused)
149 wWindowUnfocus(old_focused);
151 wasfocused = wwin->flags.focused;
152 napp = wApplicationOf(wwin->main_window);
154 /* remember last workspace where the app has been */
155 if (napp)
156 napp->last_workspace = wwin->frame->workspace;
158 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
159 /* install colormap if colormap mode is lock mode */
160 if (wPreferences.colormap_mode == WCM_CLICK)
161 wColormapInstallForWindow(scr, wwin);
163 /* set input focus */
164 switch (wwin->focus_mode) {
165 case WFM_NO_INPUT:
166 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
167 break;
168 case WFM_PASSIVE:
169 case WFM_LOCALLY_ACTIVE:
170 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
171 break;
172 case WFM_GLOBALLY_ACTIVE:
173 break;
176 XFlush(dpy);
177 if (wwin->protocols.TAKE_FOCUS)
178 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
180 XSync(dpy, False);
181 } else {
182 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
185 if (WFLAGP(wwin, no_focusable))
186 return;
188 /* if this is not the focused window focus it */
189 if (focused != wwin) {
190 /* change the focus window list order */
191 if (wwin->prev)
192 wwin->prev->next = wwin->next;
194 if (wwin->next)
195 wwin->next->prev = wwin->prev;
197 wwin->prev = focused;
198 focused->next = wwin;
199 wwin->next = NULL;
200 scr->focused_window = wwin;
202 if (oapp && oapp != napp) {
203 wAppMenuUnmap(oapp->menu);
204 if (wPreferences.highlight_active_app)
205 wApplicationDeactivate(oapp);
209 wWindowFocus(wwin, focused);
211 if (napp && !wasfocused) {
212 #ifdef USER_MENU
213 wUserMenuRefreshInstances(napp->menu, wwin);
214 #endif /* USER_MENU */
216 if (wwin->flags.mapped)
217 wAppMenuMap(napp->menu, wwin);
219 if (napp && wPreferences.highlight_active_app)
220 wApplicationActivate(napp);
222 XFlush(dpy);
223 old_scr = scr;
226 void wShadeWindow(WWindow *wwin)
229 if (wwin->flags.shaded)
230 return;
232 XLowerWindow(dpy, wwin->client_win);
233 shade_animate(wwin, SHADE);
235 wwin->flags.skip_next_animation = 0;
236 wwin->flags.shaded = 1;
237 wwin->flags.mapped = 0;
238 /* prevent window withdrawal when getting UnmapNotify */
239 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
240 XUnmapWindow(dpy, wwin->client_win);
241 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
243 /* for the client it's just like iconification */
244 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
246 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
247 wWindowSynthConfigureNotify(wwin);
250 wClientSetState(wwin, IconicState, None);
253 WMPostNotificationName(WMNChangedState, wwin, "shade");
255 #ifdef ANIMATIONS
256 if (!wwin->screen_ptr->flags.startup) {
257 /* Catch up with events not processed while animation was running */
258 ProcessPendingEvents();
260 #endif
263 void wUnshadeWindow(WWindow *wwin)
266 if (!wwin->flags.shaded)
267 return;
269 wwin->flags.shaded = 0;
270 wwin->flags.mapped = 1;
271 XMapWindow(dpy, wwin->client_win);
273 shade_animate(wwin, UNSHADE);
275 wwin->flags.skip_next_animation = 0;
276 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
277 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
279 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
280 wWindowSynthConfigureNotify(wwin);
282 /* if the window is focused, set the focus again as it was disabled during
283 * shading */
284 if (wwin->flags.focused)
285 wSetFocusTo(wwin->screen_ptr, wwin);
287 WMPostNotificationName(WMNChangedState, wwin, "shade");
290 /* Set the old coordinates using the current values */
291 static void save_old_geometry(WWindow *wwin, int directions)
293 /* never been saved? */
294 if (! wwin->old_geometry.width)
295 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
296 if (! wwin->old_geometry.height)
297 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
299 if (directions & SAVE_GEOMETRY_X)
300 wwin->old_geometry.x = wwin->frame_x;
301 if (directions & SAVE_GEOMETRY_Y)
302 wwin->old_geometry.y = wwin->frame_y;
303 if (directions & SAVE_GEOMETRY_WIDTH)
304 wwin->old_geometry.width = wwin->client.width;
305 if (directions & SAVE_GEOMETRY_HEIGHT)
306 wwin->old_geometry.height = wwin->client.height;
309 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
311 WMRect old_geom_rect;
312 int old_head;
313 Bool same_head;
315 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
316 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
317 same_head = (wGetHeadForWindow(wwin) == old_head);
318 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
319 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
320 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
321 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
324 /* Remember geometry for unmaximizing */
325 void update_saved_geometry(WWindow *wwin)
327 /* NOT if we aren't already maximized
328 * we'll save geometry when maximizing */
329 if (!wwin->flags.maximized)
330 return;
332 /* NOT if we are fully maximized */
333 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
334 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
335 (wwin->flags.maximized & MAX_VERTICAL)))
336 return;
338 /* save the co-ordinate in the axis in which we AREN'T maximized */
339 if (wwin->flags.maximized & MAX_HORIZONTAL)
340 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
341 if (wwin->flags.maximized & MAX_VERTICAL)
342 save_old_geometry(wwin, SAVE_GEOMETRY_X);
345 void wMaximizeWindow(WWindow *wwin, int directions)
347 unsigned int new_width, new_height, half_scr_width, half_scr_height;
348 int new_x = 0;
349 int new_y = 0;
350 int maximus_x = 0;
351 int maximus_y = 0;
352 unsigned int maximus_width = 0;
353 unsigned int maximus_height = 0;
354 WArea usableArea, totalArea;
355 Bool has_border = 1;
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 = wwin->screen_ptr->frame_border_width * 2 * has_border;
367 /* save old coordinates before we change the current values */
368 if (!wwin->flags.maximized)
369 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
371 totalArea.x2 = wwin->screen_ptr->scr_width;
372 totalArea.y2 = wwin->screen_ptr->scr_height;
373 totalArea.x1 = 0;
374 totalArea.y1 = 0;
375 usableArea = totalArea;
377 if (!(directions & MAX_IGNORE_XINERAMA)) {
378 WScreen *scr = wwin->screen_ptr;
379 int head;
381 if (directions & MAX_KEYBOARD)
382 head = wGetHeadForWindow(wwin);
383 else
384 head = wGetHeadForPointerLocation(scr);
386 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
389 /* Only save directions, not kbd or xinerama hints */
390 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
392 if (WFLAGP(wwin, full_maximize)) {
393 usableArea = totalArea;
395 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
396 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
398 if (wwin->flags.shaded) {
399 wwin->flags.skip_next_animation = 1;
400 wUnshadeWindow(wwin);
403 if (directions & MAX_MAXIMUS) {
404 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
405 new_width = maximus_width - adj_size;
406 new_height = maximus_height - adj_size;
407 new_x = maximus_x;
408 new_y = maximus_y;
409 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
410 new_height += wwin->frame->bottom_width - 1;
411 new_y -= wwin->frame->top_width;
414 wwin->maximus_x = new_x;
415 wwin->maximus_y = new_y;
416 wwin->flags.old_maximized |= MAX_MAXIMUS;
417 } else {
418 /* set default values if no option set then */
419 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
420 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
421 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
423 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
424 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
425 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
428 /* left|right position */
429 if (directions & MAX_LEFTHALF) {
430 new_width = half_scr_width - adj_size;
431 new_x = usableArea.x1;
432 } else if (directions & MAX_RIGHTHALF) {
433 new_width = half_scr_width - adj_size;
434 new_x = usableArea.x1 + half_scr_width;
436 /* top|bottom position */
437 if (directions & MAX_TOPHALF) {
438 new_height = half_scr_height - adj_size;
439 new_y = usableArea.y1;
440 } else if (directions & MAX_BOTTOMHALF) {
441 new_height = half_scr_height - adj_size;
442 new_y = usableArea.y1 + half_scr_height;
445 /* vertical|horizontal position */
446 if (directions & MAX_HORIZONTAL) {
447 new_width = usableArea.x2 - usableArea.x1 - adj_size;
448 new_x = usableArea.x1;
450 if (directions & MAX_VERTICAL) {
451 new_height = usableArea.y2 - usableArea.y1 - adj_size;
452 new_y = usableArea.y1;
453 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
454 new_y -= wwin->frame->top_width;
458 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
459 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
461 /* set maximization state */
462 wwin->flags.maximized = directions;
463 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
464 wwin->flags.maximized = MAX_MAXIMUS;
466 wWindowConstrainSize(wwin, &new_width, &new_height);
468 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
469 usableArea.y2 - usableArea.y1, &new_width, &new_height);
471 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
472 wWindowSynthConfigureNotify(wwin);
474 WMPostNotificationName(WMNChangedState, wwin, "maximize");
477 /* generic (un)maximizer */
478 void handleMaximize(WWindow *wwin, int directions)
480 int current = wwin->flags.maximized;
481 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
482 int effective = requested ^ current;
483 int flags = directions & ~requested;
485 if (!effective) {
486 /* allow wMaximizeWindow to restore the Maximusized size */
487 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
488 !(requested & MAX_MAXIMUS))
489 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
490 else
491 wUnmaximizeWindow(wwin);
492 /* these alone mean vertical|horizontal toggle */
493 } else if ((effective == MAX_LEFTHALF) ||
494 (effective == MAX_RIGHTHALF) ||
495 (effective == MAX_TOPHALF) ||
496 (effective == MAX_BOTTOMHALF))
497 wUnmaximizeWindow(wwin);
498 else {
499 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
500 (requested == MAX_MAXIMUS))
501 effective = requested;
502 else {
503 if (requested & MAX_LEFTHALF) {
504 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
505 effective |= MAX_VERTICAL;
506 else
507 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
508 effective |= MAX_LEFTHALF;
509 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
510 } else if (requested & MAX_RIGHTHALF) {
511 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
512 effective |= MAX_VERTICAL;
513 else
514 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
515 effective |= MAX_RIGHTHALF;
516 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
518 if (requested & MAX_TOPHALF) {
519 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
520 effective |= MAX_HORIZONTAL;
521 else
522 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
523 effective |= MAX_TOPHALF;
524 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
525 } else if (requested & MAX_BOTTOMHALF) {
526 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
527 effective |= MAX_HORIZONTAL;
528 else
529 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
530 effective |= MAX_BOTTOMHALF;
531 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
533 if (requested & MAX_HORIZONTAL)
534 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
535 if (requested & MAX_VERTICAL)
536 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
537 effective &= ~MAX_MAXIMUS;
539 wMaximizeWindow(wwin, effective | flags);
543 /* the window boundary coordinates */
544 typedef struct {
545 int left;
546 int right;
547 int bottom;
548 int top;
549 int width;
550 int height;
551 } win_coords;
553 static void set_window_coords(WWindow *wwin, win_coords *obs)
555 obs->left = wwin->frame_x;
556 obs->top = wwin->frame_y;
557 obs->width = wwin->frame->core->width;
558 obs->height = wwin->frame->core->height;
559 obs->bottom = obs->top + obs->height;
560 obs->right = obs->left + obs->width;
564 * Maximus: tiled maximization (maximize without overlapping other windows)
566 * The original window 'orig' will be maximized to new coordinates 'new'.
567 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
569 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
570 unsigned int *new_width, unsigned int *new_height)
572 WWindow *tmp;
573 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
574 short int adjust_height;
575 int x_intsect, y_intsect;
576 /* the obstructing, original and new windows */
577 win_coords obs, orig, new;
579 /* set the original coordinate positions of the window to be Maximumized */
580 if (wwin->flags.maximized) {
581 /* window is already maximized; consider original geometry */
582 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
583 orig.bottom = orig.top + orig.height;
584 orig.right = orig.left + orig.width;
586 else
587 set_window_coords(wwin, &orig);
589 /* Try to fully maximize first, then readjust later */
590 new.left = usableArea.x1;
591 new.right = usableArea.x2;
592 new.top = usableArea.y1;
593 new.bottom = usableArea.y2;
595 if (HAS_TITLEBAR(wwin))
596 tbar_height_0 = TITLEBAR_HEIGHT;
597 if (HAS_RESIZEBAR(wwin))
598 rbar_height_0 = RESIZEBAR_HEIGHT;
599 if (HAS_BORDER(wwin))
600 bd_width_0 = wwin->screen_ptr->frame_border_width;
602 /* the length to be subtracted if the window has titlebar, etc */
603 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
605 tmp = wwin;
606 /* The focused window is always the last in the list */
607 while (tmp->prev) {
608 /* ignore windows in other workspaces etc */
609 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
610 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
611 tmp = tmp->prev;
612 continue;
614 tmp = tmp->prev;
616 /* Set the coordinates of obstructing window */
617 set_window_coords(tmp, &obs);
619 /* Try to maximize in the y direction first */
620 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
621 if (x_intsect != 0) {
622 /* TODO: Consider the case when coords are equal */
623 if (obs.bottom < orig.top && obs.bottom > new.top) {
624 /* w_0 is below the bottom of w_j */
625 new.top = obs.bottom + 1;
627 if (orig.bottom < obs.top && obs.top < new.bottom) {
628 /* The bottom of w_0 is above the top of w_j */
629 new.bottom = obs.top - 1;
634 tmp = wwin;
635 while (tmp->prev) {
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_window_coords(tmp, &obs);
646 * Use the new.top and new.height instead of original values
647 * as they may have different intersections with the obstructing windows
649 new.height = new.bottom - new.top - adjust_height;
650 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
651 if (y_intsect != 0) {
652 if (obs.right < orig.left && obs.right > new.left) {
653 /* w_0 is completely to the right of w_j */
654 new.left = obs.right + 1;
656 if (orig.right < obs.left && obs.left < new.right) {
657 /* w_0 is completely to the left of w_j */
658 new.right = obs.left - 1;
663 *new_x = new.left;
664 *new_y = new.top;
665 /* xcalc needs -7 here, but other apps don't */
666 *new_height = new.bottom - new.top - adjust_height - 1;;
667 *new_width = new.right - new.left;
670 void wUnmaximizeWindow(WWindow *wwin)
672 int x, y, w, h;
674 if (!wwin->flags.maximized)
675 return;
677 if (wwin->flags.shaded) {
678 wwin->flags.skip_next_animation = 1;
679 wUnshadeWindow(wwin);
682 /* Use old coordinates if they are set, current values otherwise */
683 remember_geometry(wwin, &x, &y, &w, &h);
685 /* unMaximusize relative to original position */
686 if (wwin->flags.maximized & MAX_MAXIMUS) {
687 x += wwin->frame_x - wwin->maximus_x;
688 y += wwin->frame_y - wwin->maximus_y;
691 wwin->flags.maximized = 0;
692 wwin->flags.old_maximized = 0;
693 wWindowConfigure(wwin, x, y, w, h);
694 wWindowSynthConfigureNotify(wwin);
696 WMPostNotificationName(WMNChangedState, wwin, "maximize");
699 void wFullscreenWindow(WWindow *wwin)
701 int head;
702 WMRect rect;
704 if (wwin->flags.fullscreen)
705 return;
707 wwin->flags.fullscreen = True;
709 wWindowConfigureBorders(wwin);
711 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
713 wwin->bfs_geometry.x = wwin->frame_x;
714 wwin->bfs_geometry.y = wwin->frame_y;
715 wwin->bfs_geometry.width = wwin->frame->core->width;
716 wwin->bfs_geometry.height = wwin->frame->core->height;
718 head = wGetHeadForWindow(wwin);
719 rect = wGetRectForHead(wwin->screen_ptr, head);
720 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
722 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
725 void wUnfullscreenWindow(WWindow *wwin)
727 if (!wwin->flags.fullscreen)
728 return;
730 wwin->flags.fullscreen = False;
732 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
733 if (WFLAGP(wwin, sunken)) {
734 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
735 } else if (WFLAGP(wwin, floating)) {
736 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
737 } else {
738 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
742 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
743 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
745 wWindowConfigureBorders(wwin);
747 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
748 wFrameWindowPaint(wwin->frame);
751 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
754 #ifdef ANIMATIONS
755 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
757 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
758 float cx, cy, cw, ch;
759 float xstep, ystep, wstep, hstep;
760 XPoint points[5];
761 float dx, dch, midy;
762 float angle, final_angle, delta;
764 xstep = (float)(fx - x) / steps;
765 ystep = (float)(fy - y) / steps;
766 wstep = (float)(fw - w) / steps;
767 hstep = (float)(fh - h) / steps;
769 cx = (float)x;
770 cy = (float)y;
771 cw = (float)w;
772 ch = (float)h;
774 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
775 delta = (float)(final_angle / FRAMES);
776 for (angle = 0;; angle += delta) {
777 if (angle > final_angle)
778 angle = final_angle;
780 dx = (cw / 10) - ((cw / 5) * sin(angle));
781 dch = (ch / 2) * cos(angle);
782 midy = cy + (ch / 2);
784 points[0].x = cx + dx;
785 points[0].y = midy - dch;
786 points[1].x = cx + cw - dx;
787 points[1].y = points[0].y;
788 points[2].x = cx + cw + dx;
789 points[2].y = midy + dch;
790 points[3].x = cx - dx;
791 points[3].y = points[2].y;
792 points[4].x = points[0].x;
793 points[4].y = points[0].y;
795 XGrabServer(dpy);
796 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
797 XFlush(dpy);
798 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
800 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
801 XUngrabServer(dpy);
802 cx += xstep;
803 cy += ystep;
804 cw += wstep;
805 ch += hstep;
806 if (angle >= final_angle)
807 break;
810 XFlush(dpy);
813 #undef FRAMES
815 static void
816 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
818 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
819 float cx, cy, cw, ch;
820 float xstep, ystep, wstep, hstep;
821 XPoint points[5];
822 float angle, final_angle, a, d, delta;
824 x += w / 2;
825 y += h / 2;
826 fx += fw / 2;
827 fy += fh / 2;
829 xstep = (float)(fx - x) / steps;
830 ystep = (float)(fy - y) / steps;
831 wstep = (float)(fw - w) / steps;
832 hstep = (float)(fh - h) / steps;
834 cx = (float)x;
835 cy = (float)y;
836 cw = (float)w;
837 ch = (float)h;
839 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
840 delta = (float)(final_angle / FRAMES);
841 for (angle = 0;; angle += delta) {
842 if (angle > final_angle)
843 angle = final_angle;
845 a = atan(ch / cw);
846 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
848 points[0].x = cx + cos(angle - a) * d;
849 points[0].y = cy + sin(angle - a) * d;
850 points[1].x = cx + cos(angle + a) * d;
851 points[1].y = cy + sin(angle + a) * d;
852 points[2].x = cx + cos(angle - a + WM_PI) * d;
853 points[2].y = cy + sin(angle - a + WM_PI) * d;
854 points[3].x = cx + cos(angle + a + WM_PI) * d;
855 points[3].y = cy + sin(angle + a + WM_PI) * d;
856 points[4].x = cx + cos(angle - a) * d;
857 points[4].y = cy + sin(angle - a) * d;
858 XGrabServer(dpy);
859 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
860 XFlush(dpy);
861 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
863 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
864 XUngrabServer(dpy);
865 cx += xstep;
866 cy += ystep;
867 cw += wstep;
868 ch += hstep;
869 if (angle >= final_angle)
870 break;
873 XFlush(dpy);
876 #undef FRAMES
878 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
880 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
881 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
882 float xstep, ystep, wstep, hstep;
883 int i, j;
885 xstep = (float)(fx - x) / steps;
886 ystep = (float)(fy - y) / steps;
887 wstep = (float)(fw - w) / steps;
888 hstep = (float)(fh - h) / steps;
890 for (j = 0; j < FRAMES; j++) {
891 cx[j] = (float)x;
892 cy[j] = (float)y;
893 cw[j] = (float)w;
894 ch[j] = (float)h;
896 XGrabServer(dpy);
897 for (i = 0; i < steps; i++) {
898 for (j = 0; j < FRAMES; j++) {
899 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
900 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
902 XFlush(dpy);
903 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
905 for (j = 0; j < FRAMES; j++) {
906 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
907 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
908 if (j < FRAMES - 1) {
909 cx[j] = cx[j + 1];
910 cy[j] = cy[j + 1];
911 cw[j] = cw[j + 1];
912 ch[j] = ch[j + 1];
913 } else {
914 cx[j] += xstep;
915 cy[j] += ystep;
916 cw[j] += wstep;
917 ch[j] += hstep;
922 for (j = 0; j < FRAMES; j++) {
923 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
925 XFlush(dpy);
926 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
928 for (j = 0; j < FRAMES; j++) {
929 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
932 XUngrabServer(dpy);
935 #undef FRAMES
937 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
939 int style = wPreferences.iconification_style; /* Catch the value */
940 int steps;
942 if (style == WIS_NONE)
943 return;
945 if (style == WIS_RANDOM) {
946 style = rand() % 3;
949 switch (style) {
950 case WIS_TWIST:
951 steps = MINIATURIZE_ANIMATION_STEPS_T;
952 if (steps > 0)
953 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
954 break;
955 case WIS_FLIP:
956 steps = MINIATURIZE_ANIMATION_STEPS_F;
957 if (steps > 0)
958 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
959 break;
960 case WIS_ZOOM:
961 default:
962 steps = MINIATURIZE_ANIMATION_STEPS_Z;
963 if (steps > 0)
964 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
965 break;
968 #endif /* ANIMATIONS */
970 static void flushExpose(void)
972 XEvent tmpev;
974 while (XCheckTypedEvent(dpy, Expose, &tmpev))
975 WMHandleEvent(&tmpev);
976 XSync(dpy, 0);
979 static void unmapTransientsFor(WWindow *wwin)
981 WWindow *tmp;
983 tmp = wwin->screen_ptr->focused_window;
984 while (tmp) {
985 /* unmap the transients for this transient */
986 if (tmp != wwin && tmp->transient_for == wwin->client_win
987 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
988 unmapTransientsFor(tmp);
989 tmp->flags.miniaturized = 1;
990 if (!tmp->flags.shaded) {
991 wWindowUnmap(tmp);
992 } else {
993 XUnmapWindow(dpy, tmp->frame->core->window);
996 if (!tmp->flags.shaded)
998 wClientSetState(tmp, IconicState, None);
1000 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1002 tmp = tmp->prev;
1006 static void mapTransientsFor(WWindow *wwin)
1008 WWindow *tmp;
1010 tmp = wwin->screen_ptr->focused_window;
1011 while (tmp) {
1012 /* recursively map the transients for this transient */
1013 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1014 && tmp->icon == NULL) {
1015 mapTransientsFor(tmp);
1016 tmp->flags.miniaturized = 0;
1017 if (!tmp->flags.shaded) {
1018 wWindowMap(tmp);
1019 } else {
1020 XMapWindow(dpy, tmp->frame->core->window);
1022 tmp->flags.semi_focused = 0;
1024 if (!tmp->flags.shaded)
1026 wClientSetState(tmp, NormalState, None);
1028 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1030 tmp = tmp->prev;
1034 static WWindow *recursiveTransientFor(WWindow * wwin)
1036 int i;
1038 if (!wwin)
1039 return None;
1041 /* hackish way to detect transient_for cycle */
1042 i = wwin->screen_ptr->window_count + 1;
1044 while (wwin && wwin->transient_for != None && i > 0) {
1045 wwin = wWindowFor(wwin->transient_for);
1046 i--;
1048 if (i == 0 && wwin) {
1049 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1050 return NULL;
1053 return wwin;
1056 void wIconifyWindow(WWindow * wwin)
1058 XWindowAttributes attribs;
1059 int present;
1061 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1062 return; /* the window doesn't exist anymore */
1064 if (wwin->flags.miniaturized)
1065 return; /* already miniaturized */
1067 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1068 WWindow *owner = wWindowFor(wwin->transient_for);
1070 if (owner && owner->flags.miniaturized)
1071 return;
1074 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1076 /* if the window is in another workspace, simplify process */
1077 if (present) {
1078 /* icon creation may take a while */
1079 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1080 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1081 GrabModeAsync, None, None, CurrentTime);
1084 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1085 if (!wwin->flags.icon_moved)
1086 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1088 wwin->icon = icon_create_for_wwindow(wwin);
1089 wwin->icon->mapped = 1;
1092 wwin->flags.miniaturized = 1;
1093 wwin->flags.mapped = 0;
1095 /* unmap transients */
1096 unmapTransientsFor(wwin);
1098 if (present) {
1099 XUngrabPointer(dpy, CurrentTime);
1100 wWindowUnmap(wwin);
1101 /* let all Expose events arrive so that we can repaint
1102 * something before the animation starts (and the server is grabbed) */
1103 XSync(dpy, 0);
1105 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1106 wClientSetState(wwin, IconicState, None);
1107 else
1108 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1110 flushExpose();
1111 #ifdef ANIMATIONS
1112 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1113 && !wPreferences.no_animations) {
1114 int ix, iy, iw, ih;
1116 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1117 ix = wwin->icon_x;
1118 iy = wwin->icon_y;
1119 iw = wwin->icon->core->width;
1120 ih = wwin->icon->core->height;
1121 } else {
1122 if (wwin->flags.net_handle_icon) {
1123 ix = wwin->icon_x;
1124 iy = wwin->icon_y;
1125 iw = wwin->icon_w;
1126 ih = wwin->icon_h;
1127 } else {
1128 ix = 0;
1129 iy = 0;
1130 iw = wwin->screen_ptr->scr_width;
1131 ih = wwin->screen_ptr->scr_height;
1134 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1135 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1137 #endif
1140 wwin->flags.skip_next_animation = 0;
1142 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1143 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1144 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1145 XMapWindow(dpy, wwin->icon->core->window);
1147 AddToStackList(wwin->icon->core);
1148 wLowerFrame(wwin->icon->core);
1151 if (present) {
1152 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1155 * It doesn't seem to be working and causes button event hangup
1156 * when deiconifying a transient window.
1157 setupIconGrabs(wwin->icon);
1159 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1160 && wPreferences.focus_mode == WKF_CLICK) {
1161 WWindow *tmp;
1163 tmp = wwin->prev;
1164 while (tmp) {
1165 if (!WFLAGP(tmp, no_focusable)
1166 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1167 && (wwin->frame->workspace == tmp->frame->workspace))
1168 break;
1169 tmp = tmp->prev;
1171 wSetFocusTo(wwin->screen_ptr, tmp);
1172 } else if (wPreferences.focus_mode != WKF_CLICK) {
1173 wSetFocusTo(wwin->screen_ptr, NULL);
1175 #ifdef ANIMATIONS
1176 if (!wwin->screen_ptr->flags.startup) {
1177 /* Catch up with events not processed while animation was running */
1178 Window clientwin = wwin->client_win;
1180 ProcessPendingEvents();
1182 /* the window can disappear while ProcessPendingEvents() runs */
1183 if (!wWindowFor(clientwin)) {
1184 return;
1187 #endif
1190 /* maybe we want to do this regardless of net_handle_icon
1191 * it seems to me we might break behaviour this way.
1193 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1194 && !wwin->flags.net_handle_icon)
1195 wIconSelect(wwin->icon);
1197 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1199 if (wPreferences.auto_arrange_icons)
1200 wArrangeIcons(wwin->screen_ptr, True);
1203 void wDeiconifyWindow(WWindow *wwin)
1205 /* Let's avoid changing workspace while deiconifying */
1206 ignore_wks_change = 1;
1208 /* we're hiding for show_desktop */
1209 int netwm_hidden = wwin->flags.net_show_desktop &&
1210 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1212 if (!netwm_hidden)
1213 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1215 if (!wwin->flags.miniaturized)
1216 return;
1218 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1219 WWindow *owner = recursiveTransientFor(wwin);
1221 if (owner && owner->flags.miniaturized) {
1222 wDeiconifyWindow(owner);
1223 wSetFocusTo(wwin->screen_ptr, wwin);
1224 wRaiseFrame(wwin->frame->core);
1225 return;
1229 wwin->flags.miniaturized = 0;
1231 if (!netwm_hidden && !wwin->flags.shaded)
1232 wwin->flags.mapped = 1;
1234 if (!netwm_hidden || wPreferences.sticky_icons) {
1235 /* maybe we want to do this regardless of net_handle_icon
1236 * it seems to me we might break behaviour this way.
1238 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1239 && wwin->icon != NULL) {
1240 if (wwin->icon->selected)
1241 wIconSelect(wwin->icon);
1243 XUnmapWindow(dpy, wwin->icon->core->window);
1247 /* if the window is in another workspace, do it silently */
1248 if (!netwm_hidden) {
1249 #ifdef ANIMATIONS
1250 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1251 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1252 int ix, iy, iw, ih;
1254 if (!wPreferences.disable_miniwindows
1255 && !wwin->flags.net_handle_icon) {
1256 ix = wwin->icon_x;
1257 iy = wwin->icon_y;
1258 iw = wwin->icon->core->width;
1259 ih = wwin->icon->core->height;
1260 } else {
1261 if (wwin->flags.net_handle_icon) {
1262 ix = wwin->icon_x;
1263 iy = wwin->icon_y;
1264 iw = wwin->icon_w;
1265 ih = wwin->icon_h;
1266 } else {
1267 ix = 0;
1268 iy = 0;
1269 iw = wwin->screen_ptr->scr_width;
1270 ih = wwin->screen_ptr->scr_height;
1273 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1274 wwin->frame_x, wwin->frame_y,
1275 wwin->frame->core->width, wwin->frame->core->height);
1277 #endif /* ANIMATIONS */
1278 wwin->flags.skip_next_animation = 0;
1279 XGrabServer(dpy);
1280 if (!wwin->flags.shaded)
1281 XMapWindow(dpy, wwin->client_win);
1283 XMapWindow(dpy, wwin->frame->core->window);
1284 wRaiseFrame(wwin->frame->core);
1285 if (!wwin->flags.shaded)
1286 wClientSetState(wwin, NormalState, None);
1288 mapTransientsFor(wwin);
1291 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1292 && !wwin->flags.net_handle_icon) {
1293 RemoveFromStackList(wwin->icon->core);
1294 /* removeIconGrabs(wwin->icon); */
1295 wIconDestroy(wwin->icon);
1296 wwin->icon = NULL;
1299 if (!netwm_hidden) {
1300 XUngrabServer(dpy);
1302 wSetFocusTo(wwin->screen_ptr, wwin);
1304 #ifdef ANIMATIONS
1305 if (!wwin->screen_ptr->flags.startup) {
1306 /* Catch up with events not processed while animation was running */
1307 Window clientwin = wwin->client_win;
1309 ProcessPendingEvents();
1311 /* the window can disappear while ProcessPendingEvents() runs */
1312 if (!wWindowFor(clientwin))
1313 return;
1315 #endif
1318 if (wPreferences.auto_arrange_icons)
1319 wArrangeIcons(wwin->screen_ptr, True);
1321 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1323 /* In case we were shaded and iconified, also unshade */
1324 if (!netwm_hidden)
1325 wUnshadeWindow(wwin);
1327 ignore_wks_change = 0;
1330 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1332 if (wwin->flags.miniaturized) {
1333 if (wwin->icon) {
1334 XUnmapWindow(dpy, wwin->icon->core->window);
1335 wwin->icon->mapped = 0;
1337 wwin->flags.hidden = 1;
1339 WMPostNotificationName(WMNChangedState, wwin, "hide");
1340 return;
1343 if (wwin->flags.inspector_open) {
1344 wHideInspectorForWindow(wwin);
1347 wwin->flags.hidden = 1;
1348 wWindowUnmap(wwin);
1350 wClientSetState(wwin, IconicState, icon->icon_win);
1351 flushExpose();
1353 #ifdef ANIMATIONS
1354 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1355 !wwin->flags.skip_next_animation && animate) {
1356 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1357 wwin->frame->core->width, wwin->frame->core->height,
1358 icon_x, icon_y, icon->core->width, icon->core->height);
1360 #endif
1361 wwin->flags.skip_next_animation = 0;
1363 WMPostNotificationName(WMNChangedState, wwin, "hide");
1366 void wHideAll(WScreen *scr)
1368 WWindow *wwin;
1369 WWindow **windows;
1370 WMenu *menu;
1371 unsigned int wcount = 0;
1372 int i;
1374 if (!scr)
1375 return;
1377 menu = scr->switch_menu;
1379 windows = wmalloc(sizeof(WWindow *));
1381 if (menu != NULL) {
1382 for (i = 0; i < menu->entry_no; i++) {
1383 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1384 wcount++;
1385 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1387 } else {
1388 wwin = scr->focused_window;
1390 while (wwin) {
1391 windows[wcount] = wwin;
1392 wcount++;
1393 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1394 wwin = wwin->prev;
1399 for (i = 0; i < wcount; i++) {
1400 wwin = windows[i];
1401 if (wwin->frame->workspace == scr->current_workspace
1402 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1403 && !wwin->flags.internal_window
1404 && !WFLAGP(wwin, no_miniaturizable)
1406 wwin->flags.skip_next_animation = 1;
1407 wIconifyWindow(wwin);
1411 wfree(windows);
1414 void wHideOtherApplications(WWindow *awin)
1416 WWindow *wwin;
1417 WApplication *tapp;
1419 if (!awin)
1420 return;
1421 wwin = awin->screen_ptr->focused_window;
1423 while (wwin) {
1424 if (wwin != awin
1425 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1426 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1427 && !wwin->flags.internal_window
1428 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1430 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1431 if (!WFLAGP(wwin, no_miniaturizable)) {
1432 wwin->flags.skip_next_animation = 1;
1433 wIconifyWindow(wwin);
1435 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1436 tapp = wApplicationOf(wwin->main_window);
1437 if (tapp) {
1438 tapp->flags.skip_next_animation = 1;
1439 wHideApplication(tapp);
1440 } else {
1441 if (!WFLAGP(wwin, no_miniaturizable)) {
1442 wwin->flags.skip_next_animation = 1;
1443 wIconifyWindow(wwin);
1448 wwin = wwin->prev;
1451 wSetFocusTo(awin->screen_ptr, awin);
1455 void wHideApplication(WApplication *wapp)
1457 WScreen *scr;
1458 WWindow *wlist;
1459 int hadfocus;
1460 int animate;
1462 if (!wapp) {
1463 wwarning("trying to hide a non grouped window");
1464 return;
1466 if (!wapp->main_window_desc) {
1467 wwarning("group leader not found for window group");
1468 return;
1470 scr = wapp->main_window_desc->screen_ptr;
1471 hadfocus = 0;
1472 wlist = scr->focused_window;
1473 if (!wlist)
1474 return;
1476 if (wlist->main_window == wapp->main_window)
1477 wapp->last_focused = wlist;
1478 else
1479 wapp->last_focused = NULL;
1481 animate = !wapp->flags.skip_next_animation;
1483 while (wlist) {
1484 if (wlist->main_window == wapp->main_window) {
1485 if (wlist->flags.focused) {
1486 hadfocus = 1;
1488 if (wapp->app_icon) {
1489 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1490 wapp->app_icon->y_pos, wlist, animate);
1491 animate = False;
1494 wlist = wlist->prev;
1497 wapp->flags.skip_next_animation = 0;
1499 if (hadfocus) {
1500 if (wPreferences.focus_mode == WKF_CLICK) {
1501 wlist = scr->focused_window;
1502 while (wlist) {
1503 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1504 && (wlist->flags.mapped || wlist->flags.shaded))
1505 break;
1506 wlist = wlist->prev;
1508 wSetFocusTo(scr, wlist);
1509 } else {
1510 wSetFocusTo(scr, NULL);
1514 wapp->flags.hidden = 1;
1516 if (wPreferences.auto_arrange_icons)
1517 wArrangeIcons(scr, True);
1519 #ifdef HIDDENDOT
1520 if (wapp->app_icon)
1521 wAppIconPaint(wapp->app_icon);
1522 #endif
1525 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1527 if (bringToCurrentWS)
1528 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1530 wwin->flags.hidden = 0;
1532 #ifdef ANIMATIONS
1533 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1534 animateResize(wwin->screen_ptr, icon_x, icon_y,
1535 icon->core->width, icon->core->height,
1536 wwin->frame_x, wwin->frame_y,
1537 wwin->frame->core->width, wwin->frame->core->height);
1539 #endif
1540 wwin->flags.skip_next_animation = 0;
1541 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1542 XMapWindow(dpy, wwin->client_win);
1543 XMapWindow(dpy, wwin->frame->core->window);
1544 wClientSetState(wwin, NormalState, None);
1545 wwin->flags.mapped = 1;
1546 wRaiseFrame(wwin->frame->core);
1548 if (wwin->flags.inspector_open) {
1549 wUnhideInspectorForWindow(wwin);
1552 WMPostNotificationName(WMNChangedState, wwin, "hide");
1555 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1557 WScreen *scr;
1558 WWindow *wlist, *next;
1559 WWindow *focused = NULL;
1560 int animate;
1562 if (!wapp)
1563 return;
1565 scr = wapp->main_window_desc->screen_ptr;
1566 wlist = scr->focused_window;
1567 if (!wlist)
1568 return;
1570 /* goto beginning of list */
1571 while (wlist->prev)
1572 wlist = wlist->prev;
1574 animate = !wapp->flags.skip_next_animation;
1576 while (wlist) {
1577 next = wlist->next;
1579 if (wlist->main_window == wapp->main_window) {
1580 if (wlist->flags.focused)
1581 focused = wlist;
1582 else if (!focused || !focused->flags.focused)
1583 focused = wlist;
1585 if (wlist->flags.miniaturized) {
1586 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1587 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1588 if (!wlist->icon->mapped) {
1589 int x, y;
1591 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1592 if (wlist->icon_x != x || wlist->icon_y != y) {
1593 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1595 wlist->icon_x = x;
1596 wlist->icon_y = y;
1597 XMapWindow(dpy, wlist->icon->core->window);
1598 wlist->icon->mapped = 1;
1600 wRaiseFrame(wlist->icon->core);
1602 if (bringToCurrentWS)
1603 wWindowChangeWorkspace(wlist, scr->current_workspace);
1604 wlist->flags.hidden = 0;
1605 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1606 wDeiconifyWindow(wlist);
1608 WMPostNotificationName(WMNChangedState, wlist, "hide");
1609 } else if (wlist->flags.shaded) {
1610 if (bringToCurrentWS)
1611 wWindowChangeWorkspace(wlist, scr->current_workspace);
1612 wlist->flags.hidden = 0;
1613 wRaiseFrame(wlist->frame->core);
1614 if (wlist->frame->workspace == scr->current_workspace) {
1615 XMapWindow(dpy, wlist->frame->core->window);
1616 if (miniwindows) {
1617 wUnshadeWindow(wlist);
1620 WMPostNotificationName(WMNChangedState, wlist, "hide");
1621 } else if (wlist->flags.hidden) {
1622 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1623 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1624 animate = False;
1625 } else {
1626 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1627 wWindowChangeWorkspace(wlist, scr->current_workspace);
1629 wRaiseFrame(wlist->frame->core);
1632 wlist = next;
1635 wapp->flags.skip_next_animation = 0;
1636 wapp->flags.hidden = 0;
1638 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1639 wRaiseFrame(wapp->last_focused->frame->core);
1640 wSetFocusTo(scr, wapp->last_focused);
1641 } else if (focused) {
1642 wSetFocusTo(scr, focused);
1644 wapp->last_focused = NULL;
1645 if (wPreferences.auto_arrange_icons)
1646 wArrangeIcons(scr, True);
1648 #ifdef HIDDENDOT
1649 wAppIconPaint(wapp->app_icon);
1650 #endif
1653 void wShowAllWindows(WScreen *scr)
1655 WWindow *wwin, *old_foc;
1656 WApplication *wapp;
1658 old_foc = wwin = scr->focused_window;
1659 while (wwin) {
1660 if (!wwin->flags.internal_window &&
1661 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1662 if (wwin->flags.miniaturized) {
1663 wwin->flags.skip_next_animation = 1;
1664 wDeiconifyWindow(wwin);
1665 } else if (wwin->flags.hidden) {
1666 wapp = wApplicationOf(wwin->main_window);
1667 if (wapp) {
1668 wUnhideApplication(wapp, False, False);
1669 } else {
1670 wwin->flags.skip_next_animation = 1;
1671 wDeiconifyWindow(wwin);
1675 wwin = wwin->prev;
1677 wSetFocusTo(scr, old_foc);
1678 /*wRaiseFrame(old_foc->frame->core); */
1681 void wRefreshDesktop(WScreen *scr)
1683 Window win;
1684 XSetWindowAttributes attr;
1686 attr.backing_store = NotUseful;
1687 attr.save_under = False;
1688 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1689 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1690 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1691 XMapRaised(dpy, win);
1692 XDestroyWindow(dpy, win);
1693 XFlush(dpy);
1696 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1698 WWindow *wwin;
1699 WAppIcon *aicon;
1701 int head;
1702 const int heads = wXineramaHeads(scr);
1704 struct HeadVars {
1705 int pf; /* primary axis */
1706 int sf; /* secondary axis */
1707 int fullW;
1708 int fullH;
1709 int pi, si;
1710 int sx1, sx2, sy1, sy2; /* screen boundary */
1711 int sw, sh;
1712 int xo, yo;
1713 int xs, ys;
1714 } *vars;
1716 int isize = wPreferences.icon_size;
1718 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1720 for (head = 0; head < heads; ++head) {
1721 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1722 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1723 vars[head].pi = vars[head].si = 0;
1724 vars[head].sx1 = rect.pos.x;
1725 vars[head].sy1 = rect.pos.y;
1726 vars[head].sw = rect.size.width;
1727 vars[head].sh = rect.size.height;
1728 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1729 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1730 vars[head].sw = isize * (vars[head].sw / isize);
1731 vars[head].sh = isize * (vars[head].sh / isize);
1732 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1733 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1735 /* icon yard boundaries */
1736 if (wPreferences.icon_yard & IY_VERT) {
1737 vars[head].pf = vars[head].fullH;
1738 vars[head].sf = vars[head].fullW;
1739 } else {
1740 vars[head].pf = vars[head].fullW;
1741 vars[head].sf = vars[head].fullH;
1743 if (wPreferences.icon_yard & IY_RIGHT) {
1744 vars[head].xo = vars[head].sx2 - isize;
1745 vars[head].xs = -1;
1746 } else {
1747 vars[head].xo = vars[head].sx1;
1748 vars[head].xs = 1;
1750 if (wPreferences.icon_yard & IY_TOP) {
1751 vars[head].yo = vars[head].sy1;
1752 vars[head].ys = 1;
1753 } else {
1754 vars[head].yo = vars[head].sy2 - isize;
1755 vars[head].ys = -1;
1759 #define X ((wPreferences.icon_yard & IY_VERT) \
1760 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1761 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1763 #define Y ((wPreferences.icon_yard & IY_VERT) \
1764 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1765 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1767 /* arrange application icons */
1768 aicon = scr->app_icon_list;
1769 /* reverse them to avoid unnecessarily sliding of icons */
1770 while (aicon && aicon->next)
1771 aicon = aicon->next;
1773 while (aicon) {
1774 if (!aicon->docked) {
1775 /* CHECK: can icon be NULL here ? */
1776 /* The intention here is to place the AppIcon on the head that
1777 * contains most of the applications _main_ window. */
1778 head = wGetHeadForWindow(aicon->icon->owner);
1780 if (aicon->x_pos != X || aicon->y_pos != Y) {
1781 #ifdef ANIMATIONS
1782 if (!wPreferences.no_animations)
1783 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1784 #endif /* ANIMATIONS */
1786 wAppIconMove(aicon, X, Y);
1787 vars[head].pi++;
1788 if (vars[head].pi >= vars[head].pf) {
1789 vars[head].pi = 0;
1790 vars[head].si++;
1793 aicon = aicon->prev;
1796 /* arrange miniwindows */
1797 wwin = scr->focused_window;
1798 /* reverse them to avoid unnecessarily shuffling */
1799 while (wwin && wwin->prev)
1800 wwin = wwin->prev;
1802 while (wwin) {
1803 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1804 (wwin->frame->workspace == scr->current_workspace ||
1805 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1807 head = wGetHeadForWindow(wwin);
1809 if (arrangeAll || !wwin->flags.icon_moved) {
1810 if (wwin->icon_x != X || wwin->icon_y != Y)
1811 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1813 wwin->icon_x = X;
1814 wwin->icon_y = Y;
1816 vars[head].pi++;
1817 if (vars[head].pi >= vars[head].pf) {
1818 vars[head].pi = 0;
1819 vars[head].si++;
1823 if (arrangeAll) {
1824 wwin->flags.icon_moved = 0;
1826 /* we reversed the order, so we use next */
1827 wwin = wwin->next;
1830 wfree(vars);
1833 void wSelectWindow(WWindow *wwin, Bool flag)
1835 WScreen *scr = wwin->screen_ptr;
1837 if (flag) {
1838 wwin->flags.selected = 1;
1839 if (wwin->frame->selected_border_pixel)
1840 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1841 else
1842 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1844 if (!HAS_BORDER(wwin)) {
1845 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1848 if (!scr->selected_windows)
1849 scr->selected_windows = WMCreateArray(4);
1850 WMAddToArray(scr->selected_windows, wwin);
1851 } else {
1852 wwin->flags.selected = 0;
1853 if (wwin->frame->border_pixel)
1854 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1855 else
1856 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1858 if (!HAS_BORDER(wwin)) {
1859 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1862 if (scr->selected_windows) {
1863 WMRemoveFromArray(scr->selected_windows, wwin);
1868 void wMakeWindowVisible(WWindow *wwin)
1870 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1871 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1873 if (wwin->flags.shaded) {
1874 wUnshadeWindow(wwin);
1876 if (wwin->flags.hidden) {
1877 WApplication *app;
1879 app = wApplicationOf(wwin->main_window);
1880 if (app) {
1881 /* trick to get focus to this window */
1882 app->last_focused = wwin;
1883 wUnhideApplication(app, False, False);
1886 if (wwin->flags.miniaturized) {
1887 wDeiconifyWindow(wwin);
1888 } else {
1889 if (!WFLAGP(wwin, no_focusable))
1890 wSetFocusTo(wwin->screen_ptr, wwin);
1891 wRaiseFrame(wwin->frame->core);
1896 * Do the animation while shading (called with what = SHADE)
1897 * or unshading (what = UNSHADE).
1899 #ifdef ANIMATIONS
1900 static void shade_animate(WWindow *wwin, Bool what)
1902 int y, s, w, h;
1903 time_t time0 = time(NULL);
1905 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1906 return;
1908 switch(what) {
1909 case SHADE:
1910 if (!wwin->screen_ptr->flags.startup) {
1911 /* do the shading animation */
1912 h = wwin->frame->core->height;
1913 s = h / SHADE_STEPS;
1914 if (s < 1)
1915 s = 1;
1916 w = wwin->frame->core->width;
1917 y = wwin->frame->top_width;
1918 while (h > wwin->frame->top_width + 1) {
1919 XMoveWindow(dpy, wwin->client_win, 0, y);
1920 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1921 XFlush(dpy);
1923 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1924 break;
1926 if (SHADE_DELAY > 0) {
1927 wusleep(SHADE_DELAY * 1000L);
1928 } else {
1929 wusleep(10);
1931 h -= s;
1932 y -= s;
1934 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1936 break;
1938 case UNSHADE:
1939 h = wwin->frame->top_width + wwin->frame->bottom_width;
1940 y = wwin->frame->top_width - wwin->client.height;
1941 s = abs(y) / SHADE_STEPS;
1942 if (s < 1)
1943 s = 1;
1944 w = wwin->frame->core->width;
1945 XMoveWindow(dpy, wwin->client_win, 0, y);
1946 if (s > 0) {
1947 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1948 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1949 XMoveWindow(dpy, wwin->client_win, 0, y);
1950 XFlush(dpy);
1951 if (SHADE_DELAY > 0) {
1952 wusleep(SHADE_DELAY * 2000L / 3);
1953 } else {
1954 wusleep(10);
1956 h += s;
1957 y += s;
1959 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1960 break;
1963 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1964 break;
1967 #endif