more dnd changes
[wmaker-crm.git] / src / cycling.c
blob79294fce314b2ca7654ced910f3d3333686d7482
1 /* cycling.c- window cycling
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 2000 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
29 #include "WindowMaker.h"
30 #include "GNUstep.h"
31 #include "screen.h"
32 #include "wcore.h"
33 #include "window.h"
34 #include "framewin.h"
35 #include "keybind.h"
36 #include "actions.h"
37 #include "stacking.h"
38 #include "funcs.h"
40 /* Globals */
41 extern WPreferences wPreferences;
43 extern WShortKey wKeyBindings[WKBD_LAST];
52 static WWindow*
53 nextToFocusAfter(WWindow *wwin)
55 WWindow *tmp = wwin->prev;
57 while (tmp) {
58 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
59 return tmp;
61 tmp = tmp->prev;
64 tmp = wwin;
65 /* start over from the beginning of the list */
66 while (tmp->next)
67 tmp = tmp->next;
69 while (tmp && tmp != wwin) {
70 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
71 return tmp;
73 tmp = tmp->prev;
76 return wwin;
80 static WWindow*
81 nextToFocusBefore(WWindow *wwin)
83 WWindow *tmp = wwin->next;
85 while (tmp) {
86 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
87 return tmp;
89 tmp = tmp->next;
92 /* start over from the beginning of the list */
93 tmp = wwin;
94 while (tmp->prev)
95 tmp = tmp->prev;
97 while (tmp && tmp != wwin) {
98 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
100 return tmp;
102 tmp = tmp->next;
105 return wwin;
109 void
110 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
112 WScreen *scr = wScreenForRootWindow(event->xkey.root);
113 Bool done = False;
114 Bool openedSwitchMenu = False;
115 WWindow *newFocused;
116 WWindow *oldFocused;
117 int modifiers;
118 XModifierKeymap *keymap;
119 Bool somethingElse = False;
120 XEvent ev;
122 if (!wwin)
123 return;
125 keymap = XGetModifierMapping(dpy);
128 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
129 CurrentTime);
131 if (next) {
132 newFocused = nextToFocusAfter(wwin);
133 } else {
134 newFocused = nextToFocusBefore(wwin);
137 scr->flags.doing_alt_tab = 1;
140 if (wPreferences.circ_raise)
141 XRaiseWindow(dpy, newFocused->frame->core->window);
142 wWindowFocus(newFocused, scr->focused_window);
143 oldFocused = newFocused;
145 #if 0
146 if (wPreferences.popup_switchmenu &&
147 (!scr->switch_menu || !scr->switch_menu->flags.mapped)) {
149 OpenSwitchMenu(scr, scr->scr_width/2, scr->scr_height/2, False);
150 openedSwitchMenu = True;
152 #endif
153 while (!done) {
154 WMMaskEvent(dpy,KeyPressMask|KeyReleaseMask|ExposureMask, &ev);
156 if (ev.type != KeyRelease && ev.type != KeyPress) {
157 WMHandleEvent(&ev);
158 continue;
160 /* ignore CapsLock */
161 modifiers = ev.xkey.state & ValidModMask;
163 if (ev.type == KeyPress) {
164 if (wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
165 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers) {
167 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
168 newFocused = nextToFocusAfter(newFocused);
169 wWindowFocus(newFocused, oldFocused);
170 oldFocused = newFocused;
172 if (wPreferences.circ_raise) {
173 /* restore order */
174 CommitStacking(scr);
175 XRaiseWindow(dpy, newFocused->frame->core->window);
178 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
180 } else if (wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
181 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers) {
183 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
184 newFocused = nextToFocusBefore(newFocused);
185 wWindowFocus(newFocused, oldFocused);
186 oldFocused = newFocused;
188 if (wPreferences.circ_raise) {
189 /* restore order */
190 CommitStacking(scr);
191 XRaiseWindow(dpy, newFocused->frame->core->window);
193 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
195 } else {
196 somethingElse = True;
197 done = True;
199 } else if (ev.type == KeyRelease) {
200 int i;
202 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
203 if (keymap->modifiermap[i] == ev.xkey.keycode &&
204 wKeyBindings[WKBD_FOCUSNEXT].modifier
205 & 1<<(i/keymap->max_keypermod)) {
206 done = True;
207 break;
212 XFreeModifiermap(keymap);
214 XUngrabKeyboard(dpy, CurrentTime);
215 wSetFocusTo(scr, newFocused);
217 if (wPreferences.circ_raise) {
218 wRaiseFrame(newFocused->frame->core);
219 CommitStacking(scr);
222 scr->flags.doing_alt_tab = 0;
223 if (openedSwitchMenu)
224 OpenSwitchMenu(scr, scr->scr_width/2, scr->scr_height/2, False);
226 if (somethingElse) {
227 WMHandleEvent(&ev);
236 static WWindow*
237 nextFocusWindow(WScreen *scr)
239 WWindow *tmp, *wwin, *closest, *min;
240 Window d;
242 if (!(wwin = scr->focused_window))
243 return NULL;
244 tmp = wwin->prev;
245 closest = NULL;
246 min = wwin;
247 d = 0xffffffff;
248 while (tmp) {
249 if (wWindowCanReceiveFocus(tmp)
250 && (!WFLAGP(tmp, skip_window_list)|| tmp->flags.internal_window)) {
251 if (min->client_win > tmp->client_win)
252 min = tmp;
253 if (tmp->client_win > wwin->client_win
254 && (!closest
255 || (tmp->client_win - wwin->client_win) < d)) {
256 closest = tmp;
257 d = tmp->client_win - wwin->client_win;
260 tmp = tmp->prev;
262 if (!closest||closest==wwin)
263 return min;
264 return closest;
268 static WWindow*
269 prevFocusWindow(WScreen *scr)
271 WWindow *tmp, *wwin, *closest, *max;
272 Window d;
274 if (!(wwin = scr->focused_window))
275 return NULL;
276 tmp = wwin->prev;
277 closest = NULL;
278 max = wwin;
279 d = 0xffffffff;
280 while (tmp) {
281 if (wWindowCanReceiveFocus(tmp) &&
282 (!WFLAGP(tmp, skip_window_list) || tmp->flags.internal_window)) {
283 if (max->client_win < tmp->client_win)
284 max = tmp;
285 if (tmp->client_win < wwin->client_win
286 && (!closest
287 || (wwin->client_win - tmp->client_win) < d)) {
288 closest = tmp;
289 d = wwin->client_win - tmp->client_win;
292 tmp = tmp->prev;
294 if (!closest||closest==wwin)
295 return max;
296 return closest;
300 void CycleWindow(WScreen *scr, Bool forward)
302 WWindow *wwin;
304 if (forward)
305 wwin = nextFocusWindow(scr);
306 else
307 wwin = prevFocusWindow(scr);
309 if (wwin != NULL)
310 wSetFocusTo(scr, wwin);