Maximus: Take border into account
[wmaker-crm.git] / src / actions.c
blob52b4fc972a1a48ab5eec1c2cfacff448342ebff4
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "framewin.h"
37 #include "window.h"
38 #include "client.h"
39 #include "icon.h"
40 #include "funcs.h"
41 #include "application.h"
42 #include "actions.h"
43 #include "stacking.h"
44 #include "appicon.h"
45 #include "dock.h"
46 #include "appmenu.h"
47 #include "winspector.h"
48 #include "workspace.h"
49 #include "xinerama.h"
51 /****** Global Variables ******/
52 extern Time LastTimestamp;
53 extern Time LastFocusChange;
55 extern Cursor wCursor[WCUR_LAST];
57 extern WPreferences wPreferences;
59 extern Atom _XA_WM_TAKE_FOCUS;
61 extern void ProcessPendingEvents();
62 extern int calcIntersectionLength(int p1, int l1, int p2, int l2);
64 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
65 unsigned int *new_width, unsigned int *new_height);
66 static void save_old_geometry(WWindow *wwin);
68 /******* Local Variables *******/
69 static struct {
70 int steps;
71 int delay;
72 } shadePars[5] = {
74 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
75 SHADE_STEPS_F, SHADE_DELAY_F}, {
76 SHADE_STEPS_M, SHADE_DELAY_M}, {
77 SHADE_STEPS_S, SHADE_DELAY_S}, {
78 SHADE_STEPS_US, SHADE_DELAY_US}};
80 #define UNSHADE 0
81 #define SHADE 1
82 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
83 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
85 static int compareTimes(Time t1, Time t2)
87 Time diff;
88 if (t1 == t2)
89 return 0;
90 diff = t1 - t2;
91 return (diff < 60000) ? 1 : -1;
95 *----------------------------------------------------------------------
96 * wSetFocusTo--
97 * Changes the window focus to the one passed as argument.
98 * If the window to focus is not already focused, it will be brought
99 * to the head of the list of windows. Previously focused window is
100 * unfocused.
102 * Side effects:
103 * Window list may be reordered and the window focus is changed.
105 *----------------------------------------------------------------------
107 void wSetFocusTo(WScreen * scr, WWindow * wwin)
109 static WScreen *old_scr = NULL;
111 WWindow *old_focused;
112 WWindow *focused = scr->focused_window;
113 Time timestamp = LastTimestamp;
114 WApplication *oapp = NULL, *napp = NULL;
115 int wasfocused;
117 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
118 return;
120 if (!old_scr)
121 old_scr = scr;
122 old_focused = old_scr->focused_window;
124 LastFocusChange = timestamp;
126 if (old_focused)
127 oapp = wApplicationOf(old_focused->main_window);
129 if (wwin == NULL) {
130 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
131 if (old_focused) {
132 wWindowUnfocus(old_focused);
134 if (oapp) {
135 wAppMenuUnmap(oapp->menu);
136 #ifdef NEWAPPICON
137 wApplicationDeactivate(oapp);
138 #endif
141 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
142 return;
143 } else if (old_scr != scr && old_focused) {
144 wWindowUnfocus(old_focused);
147 wasfocused = wwin->flags.focused;
148 napp = wApplicationOf(wwin->main_window);
150 /* remember last workspace where the app has been */
151 if (napp) {
152 /*napp->last_workspace = wwin->screen_ptr->current_workspace; */
153 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;
167 case WFM_PASSIVE:
168 case WFM_LOCALLY_ACTIVE:
169 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
170 break;
172 case WFM_GLOBALLY_ACTIVE:
173 break;
175 XFlush(dpy);
176 if (wwin->protocols.TAKE_FOCUS) {
177 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
179 XSync(dpy, False);
180 } else {
181 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
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 #ifdef NEWAPPICON
203 wApplicationDeactivate(oapp);
204 #endif
208 wWindowFocus(wwin, focused);
210 if (napp && !wasfocused) {
211 #ifdef USER_MENU
212 wUserMenuRefreshInstances(napp->menu, wwin);
213 #endif /* USER_MENU */
215 if (wwin->flags.mapped)
216 wAppMenuMap(napp->menu, wwin);
217 #ifdef NEWAPPICON
218 wApplicationActivate(napp);
219 #endif
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);
283 wClientSetState(wwin, NormalState, None);
285 /* if the window is focused, set the focus again as it was disabled during
286 * shading */
287 if (wwin->flags.focused)
288 wSetFocusTo(wwin->screen_ptr, wwin);
290 WMPostNotificationName(WMNChangedState, wwin, "shade");
293 /* Set the old coordinates using the current values */
294 static void save_old_geometry(WWindow *wwin)
296 wwin->old_geometry.width = wwin->client.width;
297 wwin->old_geometry.height = wwin->client.height;
298 wwin->old_geometry.x = wwin->frame_x;
299 wwin->old_geometry.y = wwin->frame_y;
302 void wMaximizeWindow(WWindow * wwin, int directions)
304 int new_x, new_y;
305 unsigned int new_width, new_height, half_scr_width;
306 WArea usableArea, totalArea;
307 Bool has_border = 1;
308 int adj_size;
310 if (!IS_RESIZABLE(wwin))
311 return;
313 if (!HAS_BORDER(wwin))
314 has_border = 0;
316 /* the size to adjust the geometry */
317 adj_size = FRAME_BORDER_WIDTH * 2 * has_border;
319 /* save old coordinates before we change the current values */
320 save_old_geometry(wwin);
322 totalArea.x1 = 0;
323 totalArea.y1 = 0;
324 totalArea.x2 = wwin->screen_ptr->scr_width;
325 totalArea.y2 = wwin->screen_ptr->scr_height;
326 usableArea = totalArea;
328 if (!(directions & MAX_IGNORE_XINERAMA)) {
329 WScreen *scr = wwin->screen_ptr;
330 int head;
332 if (directions & MAX_KEYBOARD)
333 head = wGetHeadForWindow(wwin);
334 else
335 head = wGetHeadForPointerLocation(scr);
337 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
340 /* Only save directions, not kbd or xinerama hints */
341 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
343 if (WFLAGP(wwin, full_maximize)) {
344 usableArea = totalArea;
346 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
348 if (wwin->flags.shaded) {
349 wwin->flags.skip_next_animation = 1;
350 wUnshadeWindow(wwin);
353 if (directions & MAX_HORIZONTAL) {
354 new_width = usableArea.x2 - usableArea.x1 - adj_size;
355 new_x = usableArea.x1;
356 } else if (directions & MAX_LEFTHALF) {
357 new_width = half_scr_width - adj_size;
358 new_x = usableArea.x1;
359 } else if (directions & MAX_RIGHTHALF) {
360 new_width = half_scr_width - adj_size;
361 new_x = usableArea.x1 + half_scr_width;
362 } else {
363 new_x = wwin->frame_x;
364 new_width = wwin->frame->core->width;
367 if (directions & MAX_VERTICAL) {
368 new_height = usableArea.y2 - usableArea.y1 - adj_size;
369 new_y = usableArea.y1;
370 if (WFLAGP(wwin, full_maximize)) {
371 new_y -= wwin->frame->top_width;
372 new_height += wwin->frame->bottom_width - 1;
374 } else {
375 new_y = wwin->frame_y;
376 new_height = wwin->frame->core->height;
379 if (!WFLAGP(wwin, full_maximize)) {
380 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
383 if (directions & MAX_MAXIMUS) {
384 find_Maximus_geometry(wwin, usableArea, &new_x, &new_y, &new_width, &new_height);
385 new_width -= adj_size;
386 new_height -= adj_size;
387 if (WFLAGP(wwin, full_maximize) && new_y == 0) {
388 new_y -= wwin->frame->top_width;
389 new_height += wwin->frame->top_width - 1;
393 wWindowConstrainSize(wwin, &new_width, &new_height);
395 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
396 usableArea.y2 - usableArea.y1, &new_width, &new_height);
398 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
400 WMPostNotificationName(WMNChangedState, wwin, "maximize");
402 /* set maximization state */
403 wwin->flags.maximized = directions;
407 * Maximus: tiled maximization (maximize without overlapping other windows)
409 * The window to be maximized will be denoted by w_0 (sub-index zero)
410 * while the windows which will stop the maximization of w_0 are denoted by w_j.
412 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
413 unsigned int *new_width, unsigned int *new_height)
415 WWindow *tmp;
416 int x_0 = wwin->frame_x;
417 int y_0 = wwin->frame_y;
418 int width_0 = wwin->frame->core->width;
419 int height_0 = wwin->frame->core->height;
420 int botton_0 = y_0 + height_0;
421 int right_border_0 = x_0 + width_0;
422 int new_x_0, new_y_0, new_botton_0, new_right_border_0, new_height_0;
423 int x_j, y_j, width_j, height_j, botton_j, top_j, right_border_j;
424 int x_intsect, y_intsect;
425 short int tbar_height_0 = 0, rbar_height_0 = 0;
426 short int bd_width_0 = 0, bd_width_j = 0;
427 short int adjust_height;
429 /* Try to fully maximize first, then readjust later */
430 new_x_0 = usableArea.x1;
431 new_y_0 = usableArea.y1;
432 new_botton_0 = usableArea.y2;
433 new_right_border_0 = usableArea.x2;
435 if (HAS_TITLEBAR(wwin))
436 tbar_height_0 = TITLEBAR_HEIGHT;
437 if (HAS_RESIZEBAR(wwin))
438 rbar_height_0 = RESIZEBAR_HEIGHT;
439 if (HAS_BORDER(wwin))
440 bd_width_0 = FRAME_BORDER_WIDTH;
442 /* the lengths to be subtracted if w_0 has titlebar, etc */
443 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
445 tmp = wwin;
446 /* The focused window is always the last in the list */
447 while (tmp->prev) {
448 /* ignore windows in other workspaces etc */
449 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
450 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
451 tmp = tmp->prev;
452 continue;
454 tmp = tmp->prev;
456 if (HAS_BORDER(tmp))
457 bd_width_j = FRAME_BORDER_WIDTH;
458 else
459 bd_width_j = 0;
462 * Set the w_j window coordinates. It is convenient
463 * to not use "corrected" sizes for width and height,
464 * otherwise bottom_j and right_border_j would be
465 * incorrect.
467 x_j = tmp->frame_x - bd_width_j;
468 y_j = tmp->frame_y - bd_width_j;
469 width_j = tmp->frame->core->width;
470 height_j = tmp->frame->core->height;
471 botton_j = y_j + height_j + bd_width_j;
472 top_j = y_j - bd_width_j;
473 right_border_j = x_j + width_j + bd_width_j;
475 /* Try to maximize in the y direction first */
476 x_intsect = calcIntersectionLength(x_0, width_0, x_j, width_j);
477 if (x_intsect != 0) {
478 /* TODO: Consider the case when coords are equal */
479 if (botton_j < y_0 && botton_j > new_y_0) {
480 /* w_0 is below the botton of w_j */
481 new_y_0 = botton_j;
483 if (botton_0 < top_j && top_j < new_botton_0) {
484 /* The botton of w_0 is above the top of w_j */
485 new_botton_0 = top_j;
490 tmp = wwin;
491 while (tmp->prev) {
492 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
493 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
494 tmp = tmp->prev;
495 continue;
497 tmp = tmp->prev;
499 if (HAS_BORDER(tmp))
500 bd_width_j = FRAME_BORDER_WIDTH;
501 else
502 bd_width_j = 0;
504 /* set the w_j window coordinates */
505 x_j = tmp->frame_x - bd_width_j;
506 y_j = tmp->frame_y - bd_width_j;
507 width_j = tmp->frame->core->width;
508 height_j = tmp->frame->core->height;
509 botton_j = y_j + height_j + bd_width_j;
510 top_j = y_j - bd_width_j;
511 right_border_j = x_j + width_j + bd_width_j;
514 * Use the updated y coordinates from the above step to account
515 * the possibility that the new value of y_0 will have different
516 * intersections with w_j
518 new_height_0 = new_botton_0 - new_y_0 - adjust_height;
519 y_intsect = calcIntersectionLength(new_y_0, new_height_0, y_j, height_j);
520 if (y_intsect != 0) {
521 if (right_border_j < x_0 && right_border_j > new_x_0) {
522 /* w_0 is completely to the right of w_j */
523 new_x_0 = right_border_j + 1;
525 if (right_border_0 < x_j && x_j < new_right_border_0) {
526 /* w_0 is completely to the left of w_j */
527 new_right_border_0 = x_j - 1;
532 new_height_0 = new_botton_0 - new_y_0 - adjust_height;
533 *new_x = new_x_0;
534 *new_y = new_y_0;
535 *new_height = new_height_0;
536 *new_width = new_right_border_0 - new_x_0;
539 void wUnmaximizeWindow(WWindow * wwin)
541 int x, y, w, h;
542 WMRect old_geom_rect;
543 int old_head;
544 Bool same_head;
546 if (!wwin->flags.maximized)
547 return;
549 if (wwin->flags.shaded) {
550 wwin->flags.skip_next_animation = 1;
551 wUnshadeWindow(wwin);
553 /* Use old coordinates if they are set, current values otherwise */
554 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
555 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
556 same_head = (wGetHeadForWindow(wwin) == old_head);
557 x = (wwin->old_geometry.x && same_head) ? wwin->old_geometry.x : wwin->frame_x;
558 y = (wwin->old_geometry.y && same_head) ? wwin->old_geometry.y : wwin->frame_y;
559 w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
560 h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
562 wwin->flags.maximized = 0;
563 wWindowConfigure(wwin, x, y, w, h);
565 WMPostNotificationName(WMNChangedState, wwin, "maximize");
568 void wFullscreenWindow(WWindow * wwin)
570 int head;
571 WMRect rect;
573 if (wwin->flags.fullscreen)
574 return;
576 wwin->flags.fullscreen = True;
578 wWindowConfigureBorders(wwin);
580 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
582 wwin->bfs_geometry.x = wwin->frame_x;
583 wwin->bfs_geometry.y = wwin->frame_y;
584 wwin->bfs_geometry.width = wwin->frame->core->width;
585 wwin->bfs_geometry.height = wwin->frame->core->height;
587 head = wGetHeadForWindow(wwin);
588 rect = wGetRectForHead(wwin->screen_ptr, head);
589 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
591 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
594 void wUnfullscreenWindow(WWindow * wwin)
596 if (!wwin->flags.fullscreen)
597 return;
599 wwin->flags.fullscreen = False;
601 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
602 if (WFLAGP(wwin, sunken)) {
603 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
604 } else if (WFLAGP(wwin, floating)) {
605 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
606 } else {
607 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
611 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
612 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
614 wWindowConfigureBorders(wwin);
616 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
617 wFrameWindowPaint(wwin->frame);
620 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
623 #ifdef ANIMATIONS
624 static void animateResizeFlip(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
626 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
627 float cx, cy, cw, ch;
628 float xstep, ystep, wstep, hstep;
629 XPoint points[5];
630 float dx, dch, midy;
631 float angle, final_angle, delta;
633 xstep = (float)(fx - x) / steps;
634 ystep = (float)(fy - y) / steps;
635 wstep = (float)(fw - w) / steps;
636 hstep = (float)(fh - h) / steps;
638 cx = (float)x;
639 cy = (float)y;
640 cw = (float)w;
641 ch = (float)h;
643 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
644 delta = (float)(final_angle / FRAMES);
645 for (angle = 0;; angle += delta) {
646 if (angle > final_angle)
647 angle = final_angle;
649 dx = (cw / 10) - ((cw / 5) * sin(angle));
650 dch = (ch / 2) * cos(angle);
651 midy = cy + (ch / 2);
653 points[0].x = cx + dx;
654 points[0].y = midy - dch;
655 points[1].x = cx + cw - dx;
656 points[1].y = points[0].y;
657 points[2].x = cx + cw + dx;
658 points[2].y = midy + dch;
659 points[3].x = cx - dx;
660 points[3].y = points[2].y;
661 points[4].x = points[0].x;
662 points[4].y = points[0].y;
664 XGrabServer(dpy);
665 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
666 XFlush(dpy);
667 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
669 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
670 XUngrabServer(dpy);
671 cx += xstep;
672 cy += ystep;
673 cw += wstep;
674 ch += hstep;
675 if (angle >= final_angle)
676 break;
679 XFlush(dpy);
682 #undef FRAMES
684 static void
685 animateResizeTwist(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
687 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
688 float cx, cy, cw, ch;
689 float xstep, ystep, wstep, hstep;
690 XPoint points[5];
691 float angle, final_angle, a, d, delta;
693 x += w / 2;
694 y += h / 2;
695 fx += fw / 2;
696 fy += fh / 2;
698 xstep = (float)(fx - x) / steps;
699 ystep = (float)(fy - y) / steps;
700 wstep = (float)(fw - w) / steps;
701 hstep = (float)(fh - h) / steps;
703 cx = (float)x;
704 cy = (float)y;
705 cw = (float)w;
706 ch = (float)h;
708 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
709 delta = (float)(final_angle / FRAMES);
710 for (angle = 0;; angle += delta) {
711 if (angle > final_angle)
712 angle = final_angle;
714 a = atan(ch / cw);
715 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
717 points[0].x = cx + cos(angle - a) * d;
718 points[0].y = cy + sin(angle - a) * d;
719 points[1].x = cx + cos(angle + a) * d;
720 points[1].y = cy + sin(angle + a) * d;
721 points[2].x = cx + cos(angle - a + WM_PI) * d;
722 points[2].y = cy + sin(angle - a + WM_PI) * d;
723 points[3].x = cx + cos(angle + a + WM_PI) * d;
724 points[3].y = cy + sin(angle + a + WM_PI) * d;
725 points[4].x = cx + cos(angle - a) * d;
726 points[4].y = cy + sin(angle - a) * d;
727 XGrabServer(dpy);
728 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
729 XFlush(dpy);
730 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
732 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
733 XUngrabServer(dpy);
734 cx += xstep;
735 cy += ystep;
736 cw += wstep;
737 ch += hstep;
738 if (angle >= final_angle)
739 break;
742 XFlush(dpy);
745 #undef FRAMES
747 static void animateResizeZoom(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
749 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
750 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
751 float xstep, ystep, wstep, hstep;
752 int i, j;
754 xstep = (float)(fx - x) / steps;
755 ystep = (float)(fy - y) / steps;
756 wstep = (float)(fw - w) / steps;
757 hstep = (float)(fh - h) / steps;
759 for (j = 0; j < FRAMES; j++) {
760 cx[j] = (float)x;
761 cy[j] = (float)y;
762 cw[j] = (float)w;
763 ch[j] = (float)h;
765 XGrabServer(dpy);
766 for (i = 0; i < steps; i++) {
767 for (j = 0; j < FRAMES; j++) {
768 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
769 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
771 XFlush(dpy);
772 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
774 for (j = 0; j < FRAMES; j++) {
775 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
776 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
777 if (j < FRAMES - 1) {
778 cx[j] = cx[j + 1];
779 cy[j] = cy[j + 1];
780 cw[j] = cw[j + 1];
781 ch[j] = ch[j + 1];
782 } else {
783 cx[j] += xstep;
784 cy[j] += ystep;
785 cw[j] += wstep;
786 ch[j] += hstep;
791 for (j = 0; j < FRAMES; j++) {
792 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
794 XFlush(dpy);
795 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
797 for (j = 0; j < FRAMES; j++) {
798 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
801 XUngrabServer(dpy);
804 #undef FRAMES
806 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
808 int style = wPreferences.iconification_style; /* Catch the value */
809 int steps;
811 if (style == WIS_NONE)
812 return;
814 if (style == WIS_RANDOM) {
815 style = rand() % 3;
818 switch (style) {
819 case WIS_TWIST:
820 steps = MINIATURIZE_ANIMATION_STEPS_T;
821 if (steps > 0)
822 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
823 break;
824 case WIS_FLIP:
825 steps = MINIATURIZE_ANIMATION_STEPS_F;
826 if (steps > 0)
827 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
828 break;
829 case WIS_ZOOM:
830 default:
831 steps = MINIATURIZE_ANIMATION_STEPS_Z;
832 if (steps > 0)
833 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
834 break;
837 #endif /* ANIMATIONS */
839 static void flushExpose()
841 XEvent tmpev;
843 while (XCheckTypedEvent(dpy, Expose, &tmpev))
844 WMHandleEvent(&tmpev);
845 XSync(dpy, 0);
848 static void unmapTransientsFor(WWindow * wwin)
850 WWindow *tmp;
852 tmp = wwin->screen_ptr->focused_window;
853 while (tmp) {
854 /* unmap the transients for this transient */
855 if (tmp != wwin && tmp->transient_for == wwin->client_win
856 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
857 unmapTransientsFor(tmp);
858 tmp->flags.miniaturized = 1;
859 if (!tmp->flags.shaded) {
860 wWindowUnmap(tmp);
861 } else {
862 XUnmapWindow(dpy, tmp->frame->core->window);
865 if (!tmp->flags.shaded)
867 wClientSetState(tmp, IconicState, None);
869 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
871 tmp = tmp->prev;
875 static void mapTransientsFor(WWindow * wwin)
877 WWindow *tmp;
879 tmp = wwin->screen_ptr->focused_window;
880 while (tmp) {
881 /* recursively map the transients for this transient */
882 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
883 && tmp->icon == NULL) {
884 mapTransientsFor(tmp);
885 tmp->flags.miniaturized = 0;
886 if (!tmp->flags.shaded) {
887 wWindowMap(tmp);
888 } else {
889 XMapWindow(dpy, tmp->frame->core->window);
891 tmp->flags.semi_focused = 0;
893 if (!tmp->flags.shaded)
895 wClientSetState(tmp, NormalState, None);
897 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
899 tmp = tmp->prev;
903 #if 0
904 static void setupIconGrabs(WIcon * icon)
906 /* setup passive grabs on the icon */
907 XGrabButton(dpy, Button1, AnyModifier, icon->core->window, True,
908 ButtonPressMask, GrabModeSync, GrabModeAsync, None, None);
909 XGrabButton(dpy, Button2, AnyModifier, icon->core->window, True,
910 ButtonPressMask, GrabModeSync, GrabModeAsync, None, None);
911 XGrabButton(dpy, Button3, AnyModifier, icon->core->window, True,
912 ButtonPressMask, GrabModeSync, GrabModeAsync, None, None);
913 XSync(dpy, 0);
915 #endif
917 static WWindow *recursiveTransientFor(WWindow * wwin)
919 int i;
921 if (!wwin)
922 return None;
924 /* hackish way to detect transient_for cycle */
925 i = wwin->screen_ptr->window_count + 1;
927 while (wwin && wwin->transient_for != None && i > 0) {
928 wwin = wWindowFor(wwin->transient_for);
929 i--;
931 if (i == 0 && wwin) {
932 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
933 return NULL;
936 return wwin;
939 #if 0
940 static void removeIconGrabs(WIcon * icon)
942 /* remove passive grabs on the icon */
943 XUngrabButton(dpy, Button1, AnyModifier, icon->core->window);
944 XUngrabButton(dpy, Button2, AnyModifier, icon->core->window);
945 XUngrabButton(dpy, Button3, AnyModifier, icon->core->window);
946 XSync(dpy, 0);
948 #endif
950 void wIconifyWindow(WWindow * wwin)
952 XWindowAttributes attribs;
953 int present;
955 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs)) {
956 /* the window doesn't exist anymore */
957 return;
960 if (wwin->flags.miniaturized) {
961 return;
964 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
965 WWindow *owner = wWindowFor(wwin->transient_for);
967 if (owner && owner->flags.miniaturized)
968 return;
971 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
973 /* if the window is in another workspace, simplify process */
974 if (present) {
975 /* icon creation may take a while */
976 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
977 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
978 GrabModeAsync, None, None, CurrentTime);
981 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
982 if (!wwin->flags.icon_moved) {
983 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
985 wwin->icon = wIconCreate(wwin);
987 wwin->icon->mapped = 1;
990 wwin->flags.miniaturized = 1;
991 wwin->flags.mapped = 0;
993 /* unmap transients */
995 unmapTransientsFor(wwin);
997 if (present) {
998 XUngrabPointer(dpy, CurrentTime);
999 wWindowUnmap(wwin);
1000 /* let all Expose events arrive so that we can repaint
1001 * something before the animation starts (and the server is grabbed) */
1002 XSync(dpy, 0);
1004 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1005 wClientSetState(wwin, IconicState, None);
1006 else
1007 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1009 flushExpose();
1010 #ifdef ANIMATIONS
1011 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1012 && !wPreferences.no_animations) {
1013 int ix, iy, iw, ih;
1015 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1016 ix = wwin->icon_x;
1017 iy = wwin->icon_y;
1018 iw = wwin->icon->core->width;
1019 ih = wwin->icon->core->height;
1020 } else {
1021 if (wwin->flags.net_handle_icon) {
1022 ix = wwin->icon_x;
1023 iy = wwin->icon_y;
1024 iw = wwin->icon_w;
1025 ih = wwin->icon_h;
1026 } else {
1027 ix = 0;
1028 iy = 0;
1029 iw = wwin->screen_ptr->scr_width;
1030 ih = wwin->screen_ptr->scr_height;
1033 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1034 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1036 #endif
1039 wwin->flags.skip_next_animation = 0;
1041 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1042 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1043 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1045 XMapWindow(dpy, wwin->icon->core->window);
1047 AddToStackList(wwin->icon->core);
1049 wLowerFrame(wwin->icon->core);
1052 if (present) {
1053 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1056 * It doesn't seem to be working and causes button event hangup
1057 * when deiconifying a transient window.
1058 setupIconGrabs(wwin->icon);
1060 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1061 && wPreferences.focus_mode == WKF_CLICK) {
1062 WWindow *tmp;
1064 tmp = wwin->prev;
1065 while (tmp) {
1066 if (!WFLAGP(tmp, no_focusable)
1067 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1068 && (wwin->frame->workspace == tmp->frame->workspace))
1069 break;
1070 tmp = tmp->prev;
1072 wSetFocusTo(wwin->screen_ptr, tmp);
1073 } else if (wPreferences.focus_mode != WKF_CLICK) {
1074 wSetFocusTo(wwin->screen_ptr, NULL);
1076 #ifdef ANIMATIONS
1077 if (!wwin->screen_ptr->flags.startup) {
1078 /* Catch up with events not processed while animation was running */
1079 Window clientwin = wwin->client_win;
1081 ProcessPendingEvents();
1083 /* the window can disappear while ProcessPendingEvents() runs */
1084 if (!wWindowFor(clientwin)) {
1085 return;
1088 #endif
1091 /* maybe we want to do this regardless of net_handle_icon
1092 * it seems to me we might break behaviour this way.
1094 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1095 && !wwin->flags.net_handle_icon)
1096 wIconSelect(wwin->icon);
1098 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1100 if (wPreferences.auto_arrange_icons)
1101 wArrangeIcons(wwin->screen_ptr, True);
1104 void wDeiconifyWindow(WWindow * wwin)
1106 /* we're hiding for show_desktop */
1107 int netwm_hidden = wwin->flags.net_show_desktop &&
1108 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1110 if (!netwm_hidden)
1111 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1113 if (!wwin->flags.miniaturized)
1114 return;
1116 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1117 WWindow *owner = recursiveTransientFor(wwin);
1119 if (owner && owner->flags.miniaturized) {
1120 wDeiconifyWindow(owner);
1121 wSetFocusTo(wwin->screen_ptr, wwin);
1122 wRaiseFrame(wwin->frame->core);
1123 return;
1127 wwin->flags.miniaturized = 0;
1129 if (!netwm_hidden && !wwin->flags.shaded) {
1130 wwin->flags.mapped = 1;
1133 if (!netwm_hidden || wPreferences.sticky_icons) {
1134 /* maybe we want to do this regardless of net_handle_icon
1135 * it seems to me we might break behaviour this way.
1137 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1138 && wwin->icon != NULL) {
1139 if (wwin->icon->selected)
1140 wIconSelect(wwin->icon);
1142 XUnmapWindow(dpy, wwin->icon->core->window);
1146 /* if the window is in another workspace, do it silently */
1147 if (!netwm_hidden) {
1148 #ifdef ANIMATIONS
1149 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1150 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1151 int ix, iy, iw, ih;
1153 if (!wPreferences.disable_miniwindows
1154 && !wwin->flags.net_handle_icon) {
1155 ix = wwin->icon_x;
1156 iy = wwin->icon_y;
1157 iw = wwin->icon->core->width;
1158 ih = wwin->icon->core->height;
1159 } else {
1160 if (wwin->flags.net_handle_icon) {
1161 ix = wwin->icon_x;
1162 iy = wwin->icon_y;
1163 iw = wwin->icon_w;
1164 ih = wwin->icon_h;
1165 } else {
1166 ix = 0;
1167 iy = 0;
1168 iw = wwin->screen_ptr->scr_width;
1169 ih = wwin->screen_ptr->scr_height;
1172 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1173 wwin->frame_x, wwin->frame_y,
1174 wwin->frame->core->width, wwin->frame->core->height);
1176 #endif /* ANIMATIONS */
1177 wwin->flags.skip_next_animation = 0;
1178 XGrabServer(dpy);
1179 if (!wwin->flags.shaded) {
1180 XMapWindow(dpy, wwin->client_win);
1182 XMapWindow(dpy, wwin->frame->core->window);
1183 wRaiseFrame(wwin->frame->core);
1184 if (!wwin->flags.shaded) {
1185 wClientSetState(wwin, NormalState, None);
1187 mapTransientsFor(wwin);
1190 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1191 && !wwin->flags.net_handle_icon) {
1192 RemoveFromStackList(wwin->icon->core);
1193 /* removeIconGrabs(wwin->icon); */
1194 wIconDestroy(wwin->icon);
1195 wwin->icon = NULL;
1198 if (!netwm_hidden) {
1199 XUngrabServer(dpy);
1201 wSetFocusTo(wwin->screen_ptr, wwin);
1203 #ifdef ANIMATIONS
1204 if (!wwin->screen_ptr->flags.startup) {
1205 /* Catch up with events not processed while animation was running */
1206 Window clientwin = wwin->client_win;
1208 ProcessPendingEvents();
1210 /* the window can disappear while ProcessPendingEvents() runs */
1211 if (!wWindowFor(clientwin)) {
1212 return;
1215 #endif
1218 if (wPreferences.auto_arrange_icons) {
1219 wArrangeIcons(wwin->screen_ptr, True);
1222 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1224 /* In case we were shaded and iconified, also unshade */
1225 if (!netwm_hidden)
1226 wUnshadeWindow(wwin);
1229 static void hideWindow(WIcon * icon, int icon_x, int icon_y, WWindow * wwin, int animate)
1231 if (wwin->flags.miniaturized) {
1232 if (wwin->icon) {
1233 XUnmapWindow(dpy, wwin->icon->core->window);
1234 wwin->icon->mapped = 0;
1236 wwin->flags.hidden = 1;
1238 WMPostNotificationName(WMNChangedState, wwin, "hide");
1239 return;
1242 if (wwin->flags.inspector_open) {
1243 wHideInspectorForWindow(wwin);
1246 wwin->flags.hidden = 1;
1247 wWindowUnmap(wwin);
1249 wClientSetState(wwin, IconicState, icon->icon_win);
1250 flushExpose();
1252 #ifdef ANIMATIONS
1253 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1254 !wwin->flags.skip_next_animation && animate) {
1255 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1256 wwin->frame->core->width, wwin->frame->core->height,
1257 icon_x, icon_y, icon->core->width, icon->core->height);
1259 #endif
1260 wwin->flags.skip_next_animation = 0;
1262 WMPostNotificationName(WMNChangedState, wwin, "hide");
1265 void wHideOtherApplications(WWindow * awin)
1267 WWindow *wwin;
1268 WApplication *tapp;
1270 if (!awin)
1271 return;
1272 wwin = awin->screen_ptr->focused_window;
1274 while (wwin) {
1275 if (wwin != awin
1276 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1277 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1278 && !wwin->flags.internal_window
1279 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1281 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1282 if (!WFLAGP(wwin, no_miniaturizable)) {
1283 wwin->flags.skip_next_animation = 1;
1284 wIconifyWindow(wwin);
1286 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1287 tapp = wApplicationOf(wwin->main_window);
1288 if (tapp) {
1289 tapp->flags.skip_next_animation = 1;
1290 wHideApplication(tapp);
1291 } else {
1292 if (!WFLAGP(wwin, no_miniaturizable)) {
1293 wwin->flags.skip_next_animation = 1;
1294 wIconifyWindow(wwin);
1299 wwin = wwin->prev;
1302 wSetFocusTo(awin->screen_ptr, awin);
1306 void wHideApplication(WApplication * wapp)
1308 WScreen *scr;
1309 WWindow *wlist;
1310 int hadfocus;
1311 int animate;
1313 if (!wapp) {
1314 wwarning("trying to hide a non grouped window");
1315 return;
1317 if (!wapp->main_window_desc) {
1318 wwarning("group leader not found for window group");
1319 return;
1321 scr = wapp->main_window_desc->screen_ptr;
1322 hadfocus = 0;
1323 wlist = scr->focused_window;
1324 if (!wlist)
1325 return;
1327 if (wlist->main_window == wapp->main_window)
1328 wapp->last_focused = wlist;
1329 else
1330 wapp->last_focused = NULL;
1332 animate = !wapp->flags.skip_next_animation;
1334 while (wlist) {
1335 if (wlist->main_window == wapp->main_window) {
1336 if (wlist->flags.focused) {
1337 hadfocus = 1;
1339 if (wapp->app_icon) {
1340 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1341 wapp->app_icon->y_pos, wlist, animate);
1342 animate = False;
1345 wlist = wlist->prev;
1348 wapp->flags.skip_next_animation = 0;
1350 if (hadfocus) {
1351 if (wPreferences.focus_mode == WKF_CLICK) {
1352 wlist = scr->focused_window;
1353 while (wlist) {
1354 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1355 && (wlist->flags.mapped || wlist->flags.shaded))
1356 break;
1357 wlist = wlist->prev;
1359 wSetFocusTo(scr, wlist);
1360 } else {
1361 wSetFocusTo(scr, NULL);
1365 wapp->flags.hidden = 1;
1367 if (wPreferences.auto_arrange_icons) {
1368 wArrangeIcons(scr, True);
1370 #ifdef HIDDENDOT
1371 if (wapp->app_icon)
1372 wAppIconPaint(wapp->app_icon);
1373 #endif
1376 static void unhideWindow(WIcon * icon, int icon_x, int icon_y, WWindow * wwin, int animate, int bringToCurrentWS)
1378 if (bringToCurrentWS)
1379 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1381 wwin->flags.hidden = 0;
1383 #ifdef ANIMATIONS
1384 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1385 animateResize(wwin->screen_ptr, icon_x, icon_y,
1386 icon->core->width, icon->core->height,
1387 wwin->frame_x, wwin->frame_y,
1388 wwin->frame->core->width, wwin->frame->core->height);
1390 #endif
1391 wwin->flags.skip_next_animation = 0;
1392 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1393 XMapWindow(dpy, wwin->client_win);
1394 XMapWindow(dpy, wwin->frame->core->window);
1395 wClientSetState(wwin, NormalState, None);
1396 wwin->flags.mapped = 1;
1397 wRaiseFrame(wwin->frame->core);
1399 if (wwin->flags.inspector_open) {
1400 wUnhideInspectorForWindow(wwin);
1403 WMPostNotificationName(WMNChangedState, wwin, "hide");
1406 void wUnhideApplication(WApplication * wapp, Bool miniwindows, Bool bringToCurrentWS)
1408 WScreen *scr;
1409 WWindow *wlist, *next;
1410 WWindow *focused = NULL;
1411 int animate;
1413 if (!wapp)
1414 return;
1416 scr = wapp->main_window_desc->screen_ptr;
1417 wlist = scr->focused_window;
1418 if (!wlist)
1419 return;
1421 /* goto beginning of list */
1422 while (wlist->prev)
1423 wlist = wlist->prev;
1425 animate = !wapp->flags.skip_next_animation;
1427 while (wlist) {
1428 next = wlist->next;
1430 if (wlist->main_window == wapp->main_window) {
1431 if (wlist->flags.focused)
1432 focused = wlist;
1433 else if (!focused || !focused->flags.focused)
1434 focused = wlist;
1436 if (wlist->flags.miniaturized) {
1437 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1438 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1439 if (!wlist->icon->mapped) {
1440 int x, y;
1442 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1443 if (wlist->icon_x != x || wlist->icon_y != y) {
1444 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1446 wlist->icon_x = x;
1447 wlist->icon_y = y;
1448 XMapWindow(dpy, wlist->icon->core->window);
1449 wlist->icon->mapped = 1;
1451 wRaiseFrame(wlist->icon->core);
1453 if (bringToCurrentWS)
1454 wWindowChangeWorkspace(wlist, scr->current_workspace);
1455 wlist->flags.hidden = 0;
1456 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1457 wDeiconifyWindow(wlist);
1459 WMPostNotificationName(WMNChangedState, wlist, "hide");
1460 } else if (wlist->flags.shaded) {
1461 if (bringToCurrentWS)
1462 wWindowChangeWorkspace(wlist, scr->current_workspace);
1463 wlist->flags.hidden = 0;
1464 if (wlist->frame->workspace == scr->current_workspace) {
1465 XMapWindow(dpy, wlist->frame->core->window);
1466 if (miniwindows) {
1467 wUnshadeWindow(wlist);
1468 wRaiseFrame(wlist->frame->core);
1471 WMPostNotificationName(WMNChangedState, wlist, "hide");
1472 } else if (wlist->flags.hidden) {
1473 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1474 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1475 animate = False;
1476 } else {
1477 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1478 wWindowChangeWorkspace(wlist, scr->current_workspace);
1480 wRaiseFrame(wlist->frame->core);
1483 wlist = next;
1486 wapp->flags.skip_next_animation = 0;
1487 wapp->flags.hidden = 0;
1489 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1490 wRaiseFrame(wapp->last_focused->frame->core);
1491 wSetFocusTo(scr, wapp->last_focused);
1492 } else if (focused) {
1493 wSetFocusTo(scr, focused);
1495 wapp->last_focused = NULL;
1496 if (wPreferences.auto_arrange_icons) {
1497 wArrangeIcons(scr, True);
1499 #ifdef HIDDENDOT
1500 wAppIconPaint(wapp->app_icon);
1501 #endif
1504 void wShowAllWindows(WScreen * scr)
1506 WWindow *wwin, *old_foc;
1507 WApplication *wapp;
1509 old_foc = wwin = scr->focused_window;
1510 while (wwin) {
1511 if (!wwin->flags.internal_window &&
1512 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1513 if (wwin->flags.miniaturized) {
1514 wwin->flags.skip_next_animation = 1;
1515 wDeiconifyWindow(wwin);
1516 } else if (wwin->flags.hidden) {
1517 wapp = wApplicationOf(wwin->main_window);
1518 if (wapp) {
1519 wUnhideApplication(wapp, False, False);
1520 } else {
1521 wwin->flags.skip_next_animation = 1;
1522 wDeiconifyWindow(wwin);
1526 wwin = wwin->prev;
1528 wSetFocusTo(scr, old_foc);
1529 /*wRaiseFrame(old_foc->frame->core); */
1532 void wRefreshDesktop(WScreen * scr)
1534 Window win;
1535 XSetWindowAttributes attr;
1537 attr.backing_store = NotUseful;
1538 attr.save_under = False;
1539 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1540 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1541 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1542 XMapRaised(dpy, win);
1543 XDestroyWindow(dpy, win);
1544 XFlush(dpy);
1547 void wArrangeIcons(WScreen * scr, Bool arrangeAll)
1549 WWindow *wwin;
1550 WAppIcon *aicon;
1552 int head;
1553 const int heads = wXineramaHeads(scr);
1555 struct HeadVars {
1556 int pf; /* primary axis */
1557 int sf; /* secondary axis */
1558 int fullW;
1559 int fullH;
1560 int pi, si;
1561 int sx1, sx2, sy1, sy2; /* screen boundary */
1562 int sw, sh;
1563 int xo, yo;
1564 int xs, ys;
1565 } *vars;
1567 int isize = wPreferences.icon_size;
1569 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1571 for (head = 0; head < heads; ++head) {
1572 #if 0
1573 WMRect rect = wGetRectForHead(scr, head);
1574 #else
1575 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1576 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1577 #endif
1579 vars[head].pi = vars[head].si = 0;
1580 vars[head].sx1 = rect.pos.x;
1581 vars[head].sy1 = rect.pos.y;
1582 vars[head].sw = rect.size.width;
1583 vars[head].sh = rect.size.height;
1584 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1585 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1587 #if 0
1588 if (scr->dock) {
1589 if (scr->dock->on_right_side)
1590 vars[head].sx2 -= isize + DOCK_EXTRA_SPACE;
1591 else
1592 vars[head].sx1 += isize + DOCK_EXTRA_SPACE;
1594 #endif
1596 vars[head].sw = isize * (vars[head].sw / isize);
1597 vars[head].sh = isize * (vars[head].sh / isize);
1598 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1599 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1601 /* icon yard boundaries */
1602 if (wPreferences.icon_yard & IY_VERT) {
1603 vars[head].pf = vars[head].fullH;
1604 vars[head].sf = vars[head].fullW;
1605 } else {
1606 vars[head].pf = vars[head].fullW;
1607 vars[head].sf = vars[head].fullH;
1609 if (wPreferences.icon_yard & IY_RIGHT) {
1610 vars[head].xo = vars[head].sx2 - isize;
1611 vars[head].xs = -1;
1612 } else {
1613 vars[head].xo = vars[head].sx1;
1614 vars[head].xs = 1;
1616 if (wPreferences.icon_yard & IY_TOP) {
1617 vars[head].yo = vars[head].sy1;
1618 vars[head].ys = 1;
1619 } else {
1620 vars[head].yo = vars[head].sy2 - isize;
1621 vars[head].ys = -1;
1625 #define X ((wPreferences.icon_yard & IY_VERT) \
1626 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1627 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1629 #define Y ((wPreferences.icon_yard & IY_VERT) \
1630 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1631 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1633 /* arrange application icons */
1634 aicon = scr->app_icon_list;
1635 /* reverse them to avoid unnecessarily sliding of icons */
1636 while (aicon && aicon->next)
1637 aicon = aicon->next;
1639 while (aicon) {
1640 if (!aicon->docked) {
1641 /* CHECK: can icon be NULL here ? */
1642 /* The intention here is to place the AppIcon on the head that
1643 * contains most of the applications _main_ window. */
1644 head = wGetHeadForWindow(aicon->icon->owner);
1646 if (aicon->x_pos != X || aicon->y_pos != Y) {
1647 #ifdef ANIMATIONS
1648 if (!wPreferences.no_animations) {
1649 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1651 #endif /* ANIMATIONS */
1653 wAppIconMove(aicon, X, Y);
1654 vars[head].pi++;
1655 if (vars[head].pi >= vars[head].pf) {
1656 vars[head].pi = 0;
1657 vars[head].si++;
1660 aicon = aicon->prev;
1663 /* arrange miniwindows */
1664 wwin = scr->focused_window;
1665 /* reverse them to avoid unnecessarily shuffling */
1666 while (wwin && wwin->prev)
1667 wwin = wwin->prev;
1669 while (wwin) {
1670 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1671 (wwin->frame->workspace == scr->current_workspace ||
1672 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1674 head = wGetHeadForWindow(wwin);
1676 if (arrangeAll || !wwin->flags.icon_moved) {
1677 if (wwin->icon_x != X || wwin->icon_y != Y) {
1678 #ifdef ANIMATIONS
1679 if (wPreferences.no_animations) {
1680 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1681 } else {
1682 SlideWindow(wwin->icon->core->window, wwin->icon_x,
1683 wwin->icon_y, X, Y);
1685 #else
1686 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1687 #endif /* ANIMATIONS */
1689 wwin->icon_x = X;
1690 wwin->icon_y = Y;
1692 vars[head].pi++;
1693 if (vars[head].pi >= vars[head].pf) {
1694 vars[head].pi = 0;
1695 vars[head].si++;
1699 if (arrangeAll) {
1700 wwin->flags.icon_moved = 0;
1702 /* we reversed the order, so we use next */
1703 wwin = wwin->next;
1706 wfree(vars);
1709 void wSelectWindow(WWindow * wwin, Bool flag)
1711 WScreen *scr = wwin->screen_ptr;
1713 if (flag) {
1714 wwin->flags.selected = 1;
1715 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1717 if (!HAS_BORDER(wwin)) {
1718 XSetWindowBorderWidth(dpy, wwin->frame->core->window, FRAME_BORDER_WIDTH);
1721 if (!scr->selected_windows)
1722 scr->selected_windows = WMCreateArray(4);
1723 WMAddToArray(scr->selected_windows, wwin);
1724 } else {
1725 wwin->flags.selected = 0;
1726 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1728 if (!HAS_BORDER(wwin)) {
1729 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1732 if (scr->selected_windows) {
1733 WMRemoveFromArray(scr->selected_windows, wwin);
1738 void wMakeWindowVisible(WWindow * wwin)
1740 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1741 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1743 if (wwin->flags.shaded) {
1744 wUnshadeWindow(wwin);
1746 if (wwin->flags.hidden) {
1747 WApplication *app;
1749 app = wApplicationOf(wwin->main_window);
1750 if (app) {
1751 /* trick to get focus to this window */
1752 app->last_focused = wwin;
1753 wUnhideApplication(app, False, False);
1756 if (wwin->flags.miniaturized) {
1757 wDeiconifyWindow(wwin);
1758 } else {
1759 if (!WFLAGP(wwin, no_focusable))
1760 wSetFocusTo(wwin->screen_ptr, wwin);
1761 wRaiseFrame(wwin->frame->core);
1766 * Do the animation while shading (called with what = SHADE)
1767 * or unshading (what = UNSHADE).
1769 #ifdef ANIMATIONS
1770 static void shade_animate(WWindow *wwin, Bool what)
1772 int y, s, w, h;
1773 time_t time0 = time(NULL);
1775 if (wwin->flags.skip_next_animation && wPreferences.no_animations)
1776 return;
1778 switch(what) {
1779 case SHADE:
1780 if (!wwin->screen_ptr->flags.startup) {
1781 /* do the shading animation */
1782 h = wwin->frame->core->height;
1783 s = h / SHADE_STEPS;
1784 if (s < 1)
1785 s = 1;
1786 w = wwin->frame->core->width;
1787 y = wwin->frame->top_width;
1788 while (h > wwin->frame->top_width + 1) {
1789 XMoveWindow(dpy, wwin->client_win, 0, y);
1790 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1791 XFlush(dpy);
1793 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1794 break;
1796 if (SHADE_DELAY > 0) {
1797 wusleep(SHADE_DELAY * 1000L);
1798 } else {
1799 wusleep(10);
1801 h -= s;
1802 y -= s;
1804 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1806 break;
1808 case UNSHADE:
1809 h = wwin->frame->top_width + wwin->frame->bottom_width;
1810 y = wwin->frame->top_width - wwin->client.height;
1811 s = abs(y) / SHADE_STEPS;
1812 if (s < 1)
1813 s = 1;
1814 w = wwin->frame->core->width;
1815 XMoveWindow(dpy, wwin->client_win, 0, y);
1816 if (s > 0) {
1817 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1818 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1819 XMoveWindow(dpy, wwin->client_win, 0, y);
1820 XFlush(dpy);
1821 if (SHADE_DELAY > 0) {
1822 wusleep(SHADE_DELAY * 2000L / 3);
1823 } else {
1824 wusleep(10);
1826 h += s;
1827 y += s;
1829 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1830 break;
1833 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1834 break;
1837 #else
1838 static void shade_animate(WWindow *wwin, Bool what) { return; }
1839 #endif /* ANIMATIONS */