Radio and Check buttons now work
[wmaker-crm.git] / src / switchmenu.c
blob9d5c6d25d13ab3b970577caa44dd75ca79b93d59
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997 Shige Abe
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
34 #include "window.h"
35 #include "actions.h"
36 #include "client.h"
37 #include "funcs.h"
38 #include "stacking.h"
39 #include "workspace.h"
40 #include "framewin.h"
42 /********* Global Variables *******/
43 extern WPreferences wPreferences;
45 static int initialized = 0;
46 static void observer(void *self, WMNotification * notif);
47 static void wsobserver(void *self, WMNotification * notif);
50 * FocusWindow
52 * - Needs to check if already in the right workspace before
53 * calling wChangeWorkspace?
55 * Order:
56 * Switch to correct workspace
57 * Unshade if shaded
58 * If iconified then deiconify else focus/raise.
60 static void focusWindow(WMenu * menu, WMenuEntry * entry)
62 WWindow *wwin;
63 WScreen *scr;
64 int x, y, move = 0;
66 wwin = (WWindow *) entry->clientdata;
67 scr = wwin->screen_ptr;
69 wMakeWindowVisible(wwin);
71 x = wwin->frame_x;
72 y = wwin->frame_y;
74 /* bring window back to visible area */
75 move = wScreenBringInside(scr, &x, &y, wwin->frame->core->width, wwin->frame->core->height);
77 if (move) {
78 wWindowConfigure(wwin, x, y, wwin->client.width, wwin->client.height);
82 void InitializeSwitchMenu(void)
84 if (!initialized) {
85 initialized = 1;
87 WMAddNotificationObserver(observer, NULL, WMNManaged, NULL);
88 WMAddNotificationObserver(observer, NULL, WMNUnmanaged, NULL);
89 WMAddNotificationObserver(observer, NULL, WMNChangedWorkspace, NULL);
90 WMAddNotificationObserver(observer, NULL, WMNChangedState, NULL);
91 WMAddNotificationObserver(observer, NULL, WMNChangedFocus, NULL);
92 WMAddNotificationObserver(observer, NULL, WMNChangedStacking, NULL);
93 WMAddNotificationObserver(observer, NULL, WMNChangedName, NULL);
95 WMAddNotificationObserver(wsobserver, NULL, WMNWorkspaceChanged, NULL);
96 WMAddNotificationObserver(wsobserver, NULL, WMNWorkspaceNameChanged, NULL);
102 * Open switch menu
105 void OpenSwitchMenu(WScreen * scr, int x, int y, int keyboard)
107 WMenu *switchmenu = scr->switch_menu;
108 WWindow *wwin;
110 if (switchmenu) {
111 if (switchmenu->flags.mapped) {
112 if (!switchmenu->flags.buttoned) {
113 wMenuUnmap(switchmenu);
114 } else {
115 wRaiseFrame(switchmenu->frame->core);
117 if (keyboard)
118 wMenuMapAt(switchmenu, 0, 0, True);
119 else
120 wMenuMapCopyAt(switchmenu, x - switchmenu->frame->core->width / 2, y);
122 } else {
123 if (keyboard && x == scr->scr_width / 2 && y == scr->scr_height / 2) {
124 y = y - switchmenu->frame->core->height / 2;
126 wMenuMapAt(switchmenu, x - switchmenu->frame->core->width / 2, y, keyboard);
128 return;
130 switchmenu = wMenuCreate(scr, _("Windows"), True);
131 scr->switch_menu = switchmenu;
133 wwin = scr->focused_window;
134 while (wwin) {
135 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
137 wwin = wwin->prev;
140 if (switchmenu) {
141 int newx, newy;
143 if (!switchmenu->flags.realized)
144 wMenuRealize(switchmenu);
146 if (keyboard && x == 0 && y == 0) {
147 newx = newy = 0;
148 } else if (keyboard && x == scr->scr_width / 2 && y == scr->scr_height / 2) {
149 newx = x - switchmenu->frame->core->width / 2;
150 newy = y - switchmenu->frame->core->height / 2;
151 } else {
152 newx = x - switchmenu->frame->core->width / 2;
153 newy = y;
155 wMenuMapAt(switchmenu, newx, newy, keyboard);
159 static int menuIndexForWindow(WMenu * menu, WWindow * wwin, int old_pos)
161 int idx;
163 if (menu->entry_no <= old_pos)
164 return -1;
166 #define WS(i) ((WWindow*)menu->entries[i]->clientdata)->frame->workspace
167 if (old_pos >= 0) {
168 if (WS(old_pos) >= wwin->frame->workspace
169 && (old_pos == 0 || WS(old_pos - 1) <= wwin->frame->workspace)) {
170 return old_pos;
173 #undef WS
175 for (idx = 0; idx < menu->entry_no; idx++) {
176 WWindow *tw = (WWindow *) menu->entries[idx]->clientdata;
178 if (!IS_OMNIPRESENT(tw)
179 && tw->frame->workspace > wwin->frame->workspace) {
180 break;
184 return idx;
188 * Update switch menu
190 void UpdateSwitchMenu(WScreen * scr, WWindow * wwin, int action)
192 WMenu *switchmenu = scr->switch_menu;
193 WMenuEntry *entry;
194 char title[MAX_MENU_TEXT_LENGTH + 6];
195 int len = sizeof(title);
196 int i;
197 int checkVisibility = 0;
199 if (!wwin->screen_ptr->switch_menu)
200 return;
202 * This menu is updated under the following conditions:
204 * 1. When a window is created.
205 * 2. When a window is destroyed.
207 * 3. When a window changes it's title.
208 * 4. When a window changes its workspace.
210 if (action == ACTION_ADD) {
211 char *t;
212 int idx;
214 if (wwin->flags.internal_window || WFLAGP(wwin, skip_window_list) || IS_GNUSTEP_MENU(wwin)) {
215 return;
218 if (wwin->frame->title)
219 snprintf(title, len, "%s", wwin->frame->title);
220 else
221 snprintf(title, len, "%s", DEF_WINDOW_TITLE);
222 t = ShrinkString(scr->menu_entry_font, title, MAX_WINDOWLIST_WIDTH);
224 if (IS_OMNIPRESENT(wwin))
225 idx = -1;
226 else {
227 idx = menuIndexForWindow(switchmenu, wwin, -1);
230 entry = wMenuInsertCallback(switchmenu, idx, t, focusWindow, wwin);
231 wfree(t);
233 entry->flags.indicator = 1;
234 entry->rtext = wmalloc(MAX_WORKSPACENAME_WIDTH + 8);
235 if (IS_OMNIPRESENT(wwin))
236 snprintf(entry->rtext, MAX_WORKSPACENAME_WIDTH, "[*]");
237 else
238 snprintf(entry->rtext, MAX_WORKSPACENAME_WIDTH, "[%s]",
239 scr->workspaces[wwin->frame->workspace]->name);
241 if (wwin->flags.hidden) {
242 entry->flags.indicator_type = MI_HIDDEN;
243 entry->flags.indicator_on = 1;
244 } else if (wwin->flags.miniaturized) {
245 entry->flags.indicator_type = MI_MINIWINDOW;
246 entry->flags.indicator_on = 1;
247 } else if (wwin->flags.focused) {
248 entry->flags.indicator_type = MI_DIAMOND;
249 entry->flags.indicator_on = 1;
250 } else if (wwin->flags.shaded) {
251 entry->flags.indicator_type = MI_SHADED;
252 entry->flags.indicator_on = 1;
255 wMenuRealize(switchmenu);
256 checkVisibility = 1;
257 } else {
258 char *t;
259 for (i = 0; i < switchmenu->entry_no; i++) {
260 entry = switchmenu->entries[i];
261 /* this is the entry that was changed */
262 if (entry->clientdata == wwin) {
263 switch (action) {
264 case ACTION_REMOVE:
265 wMenuRemoveItem(switchmenu, i);
266 wMenuRealize(switchmenu);
267 checkVisibility = 1;
268 break;
270 case ACTION_CHANGE:
271 if (entry->text)
272 wfree(entry->text);
274 if (wwin->frame->title)
275 snprintf(title, MAX_MENU_TEXT_LENGTH, "%s", wwin->frame->title);
276 else
277 snprintf(title, MAX_MENU_TEXT_LENGTH, "%s", DEF_WINDOW_TITLE);
279 t = ShrinkString(scr->menu_entry_font, title, MAX_WINDOWLIST_WIDTH);
280 entry->text = t;
282 wMenuRealize(switchmenu);
283 checkVisibility = 1;
284 break;
286 case ACTION_CHANGE_WORKSPACE:
287 if (entry->rtext) {
288 int idx = -1;
289 char *t, *rt;
290 int it, ion;
292 if (IS_OMNIPRESENT(wwin)) {
293 snprintf(entry->rtext, MAX_WORKSPACENAME_WIDTH, "[*]");
294 } else {
295 snprintf(entry->rtext, MAX_WORKSPACENAME_WIDTH,
296 "[%s]",
297 scr->workspaces[wwin->frame->workspace]->name);
300 rt = entry->rtext;
301 entry->rtext = NULL;
302 t = entry->text;
303 entry->text = NULL;
305 it = entry->flags.indicator_type;
306 ion = entry->flags.indicator_on;
308 if (!IS_OMNIPRESENT(wwin) && idx < 0) {
309 idx = menuIndexForWindow(switchmenu, wwin, i);
312 wMenuRemoveItem(switchmenu, i);
314 entry = wMenuInsertCallback(switchmenu, idx, t, focusWindow, wwin);
315 wfree(t);
316 entry->rtext = rt;
317 entry->flags.indicator = 1;
318 entry->flags.indicator_type = it;
319 entry->flags.indicator_on = ion;
321 wMenuRealize(switchmenu);
322 checkVisibility = 1;
323 break;
325 case ACTION_CHANGE_STATE:
326 if (wwin->flags.hidden) {
327 entry->flags.indicator_type = MI_HIDDEN;
328 entry->flags.indicator_on = 1;
329 } else if (wwin->flags.miniaturized) {
330 entry->flags.indicator_type = MI_MINIWINDOW;
331 entry->flags.indicator_on = 1;
332 } else if (wwin->flags.shaded && !wwin->flags.focused) {
333 entry->flags.indicator_type = MI_SHADED;
334 entry->flags.indicator_on = 1;
335 } else {
336 entry->flags.indicator_on = wwin->flags.focused;
337 entry->flags.indicator_type = MI_DIAMOND;
339 break;
341 break;
345 if (checkVisibility) {
346 int tmp;
348 tmp = switchmenu->frame->top_width + 5;
349 /* if menu got unreachable, bring it to a visible place */
350 if (switchmenu->frame_x < tmp - (int)switchmenu->frame->core->width) {
351 wMenuMove(switchmenu, tmp - (int)switchmenu->frame->core->width,
352 switchmenu->frame_y, False);
355 wMenuPaint(switchmenu);
358 static void UpdateSwitchMenuWorkspace(WScreen *scr, int workspace)
360 WMenu *menu = scr->switch_menu;
361 int i;
362 WWindow *wwin;
364 if (!menu)
365 return;
367 for (i = 0; i < menu->entry_no; i++) {
368 wwin = (WWindow *) menu->entries[i]->clientdata;
370 if (wwin->frame->workspace == workspace && !IS_OMNIPRESENT(wwin)) {
371 if (IS_OMNIPRESENT(wwin))
372 snprintf(menu->entries[i]->rtext, MAX_WORKSPACENAME_WIDTH, "[*]");
373 else
374 snprintf(menu->entries[i]->rtext, MAX_WORKSPACENAME_WIDTH, "[%s]",
375 scr->workspaces[wwin->frame->workspace]->name);
376 menu->flags.realized = 0;
379 if (!menu->flags.realized)
380 wMenuRealize(menu);
383 static void observer(void *self, WMNotification * notif)
385 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
386 const char *name = WMGetNotificationName(notif);
387 void *data = WMGetNotificationClientData(notif);
389 if (!wwin)
390 return;
392 if (strcmp(name, WMNManaged) == 0)
393 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
394 else if (strcmp(name, WMNUnmanaged) == 0)
395 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_REMOVE);
396 else if (strcmp(name, WMNChangedWorkspace) == 0)
397 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_WORKSPACE);
398 else if (strcmp(name, WMNChangedFocus) == 0)
399 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
400 else if (strcmp(name, WMNChangedName) == 0)
401 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
402 else if (strcmp(name, WMNChangedState) == 0) {
403 if (strcmp((char *)data, "omnipresent") == 0) {
404 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_WORKSPACE);
405 } else {
406 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
411 static void wsobserver(void *self, WMNotification * notif)
413 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
414 const char *name = WMGetNotificationName(notif);
415 void *data = WMGetNotificationClientData(notif);
417 if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
418 UpdateSwitchMenuWorkspace(scr, (uintptr_t)data);
419 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {