Code update for Window Maker version 0.50.0
[wmaker-crm.git] / src / switchmenu.c
blob0574b67a978df0254597b31ca5cc9037b2d57bcd
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997 Shige Abe and
5 * 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.
24 #include "wconfig.h"
26 #ifndef LITE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
35 #include "WindowMaker.h"
36 #include "window.h"
37 #include "actions.h"
38 #include "client.h"
39 #include "funcs.h"
40 #include "stacking.h"
41 #include "workspace.h"
42 #include "framewin.h"
44 /********* Global Variables *******/
45 extern WPreferences wPreferences;
46 extern Time LastTimestamp;
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
61 focusWindow(WMenu *menu, WMenuEntry *entry)
63 WWindow *wwin;
64 WScreen *scr;
65 int x, y, move=0;
67 wwin = (WWindow*)entry->clientdata;
68 scr = wwin->screen_ptr;
70 wMakeWindowVisible(wwin);
72 x = wwin->frame_x;
73 y = wwin->frame_y;
75 /* bring window back to visible area */
76 move = wScreenBringInside(scr, &x, &y, wwin->frame->core->width,
77 wwin->frame->core->height);
79 if (move) {
80 wWindowConfigure(wwin, x, y, wwin->client.width, wwin->client.height);
86 * Open switch menu
89 void
90 OpenSwitchMenu(WScreen *scr, int x, int y, int keyboard)
92 WMenu *switchmenu = scr->switch_menu;
93 WWindow *wwin;
95 if (switchmenu) {
96 if (switchmenu->flags.mapped) {
97 if (!switchmenu->flags.buttoned) {
98 wMenuUnmap(switchmenu);
99 } else {
100 wRaiseFrame(switchmenu->frame->core);
102 if (keyboard)
103 wMenuMapAt(switchmenu, 0, 0, True);
104 else
105 wMenuMapCopyAt(switchmenu, x-switchmenu->frame->core->width/2,
106 y-switchmenu->frame->top_width/2);
108 } else {
109 wMenuMapAt(switchmenu, x-switchmenu->frame->core->width/2,
110 y-switchmenu->frame->top_width/2, keyboard);
112 return;
114 switchmenu = wMenuCreate(scr,_("Windows"),True);
115 scr->switch_menu = switchmenu;
118 wwin = scr->focused_window;
119 while (wwin) {
120 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
122 wwin = wwin->prev;
125 if (switchmenu) {
126 if (!switchmenu->flags.realized)
127 wMenuRealize(switchmenu);
128 wMenuMapAt(switchmenu, x-switchmenu->frame->core->width/2,
129 y-switchmenu->frame->top_width/2, keyboard);
134 static int
135 menuIndexForWindow(WMenu *menu, WWindow *wwin, int old_pos)
137 int idx;
139 if (menu->entry_no == 0)
140 return -1;
142 #define WS(i) ((WWindow*)menu->entries[i]->clientdata)->frame->workspace
143 if (old_pos >= 0) {
144 if (WS(old_pos) >= wwin->frame->workspace
145 && (old_pos == 0 || WS(old_pos-1) <= wwin->frame->workspace)) {
146 return old_pos;
149 #undef WS
151 for (idx = 0; idx < menu->entry_no; idx++) {
152 WWindow *tw = (WWindow*)menu->entries[idx]->clientdata;
154 if (!IS_OMNIPRESENT(tw)
155 && tw->frame->workspace > wwin->frame->workspace) {
156 break;
160 return idx;
166 * Update switch menu
170 void
171 UpdateSwitchMenu(WScreen *scr, WWindow *wwin, int action)
173 WMenu *switchmenu = scr->switch_menu;
174 WMenuEntry *entry;
175 char title[MAX_MENU_TEXT_LENGTH+6];
176 int i;
177 int checkVisibility = 0;
179 if (!wwin->screen_ptr->switch_menu)
180 return;
182 * This menu is updated under the following conditions:
184 * 1. When a window is created.
185 * 2. When a window is destroyed.
187 * 3. When a window changes it's title.
188 * 4. When a window changes its workspace.
190 if (action == ACTION_ADD) {
191 char *t;
192 int idx;
194 if (wwin->flags.internal_window || WFLAGP(wwin, skip_window_list))
195 return;
197 if (wwin->frame->title)
198 sprintf(title, "%s", wwin->frame->title);
199 else
200 sprintf(title, "%s", DEF_WINDOW_TITLE);
201 t = ShrinkString(scr->menu_entry_font, title, MAX_WINDOWLIST_WIDTH);
203 if (IS_OMNIPRESENT(wwin))
204 idx = -1;
205 else {
206 idx = menuIndexForWindow(switchmenu, wwin, -1);
209 entry = wMenuInsertCallback(switchmenu, idx, t, focusWindow, wwin);
210 free(t);
212 entry->flags.indicator = 1;
213 entry->rtext = wmalloc(MAX_WORKSPACENAME_WIDTH+8);
214 if (IS_OMNIPRESENT(wwin))
215 sprintf(entry->rtext, "[*]");
216 else
217 sprintf(entry->rtext, "[%s]",
218 scr->workspaces[wwin->frame->workspace]->name);
220 if (wwin->flags.hidden) {
221 entry->flags.indicator_type = MI_HIDDEN;
222 entry->flags.indicator_on = 1;
223 } else if (wwin->flags.miniaturized) {
224 entry->flags.indicator_type = MI_MINIWINDOW;
225 entry->flags.indicator_on = 1;
226 } else if (wwin->flags.focused) {
227 entry->flags.indicator_type = MI_DIAMOND;
228 entry->flags.indicator_on = 1;
229 } else if (wwin->flags.shaded) {
230 entry->flags.indicator_type = MI_SHADED;
231 entry->flags.indicator_on = 1;
234 wMenuRealize(switchmenu);
235 checkVisibility = 1;
236 } else {
237 char *t;
238 for (i=0; i<switchmenu->entry_no; i++) {
239 entry = switchmenu->entries[i];
240 /* this is the entry that was changed */
241 if (entry->clientdata == wwin) {
242 switch (action) {
243 case ACTION_REMOVE:
244 wMenuRemoveItem(switchmenu, i);
245 wMenuRealize(switchmenu);
246 checkVisibility = 1;
247 break;
249 case ACTION_CHANGE:
250 if (entry->text)
251 free(entry->text);
253 if (wwin->frame->title)
254 sprintf(title, "%s", wwin->frame->title);
255 else
256 sprintf(title, "%s", DEF_WINDOW_TITLE);
258 t = ShrinkString(scr->menu_entry_font, title,
259 MAX_WINDOWLIST_WIDTH);
260 entry->text = t;
262 wMenuRealize(switchmenu);
263 checkVisibility = 1;
264 break;
266 case ACTION_CHANGE_WORKSPACE:
267 if (entry->rtext) {
268 int idx = -1;
269 char *t, *rt;
270 int it, ion;
272 if (IS_OMNIPRESENT(wwin)) {
273 sprintf(entry->rtext, "[*]");
274 } else {
275 sprintf(entry->rtext, "[%s]",
276 scr->workspaces[wwin->frame->workspace]->name);
279 rt = entry->rtext;
280 entry->rtext = NULL;
281 t = entry->text;
282 entry->text = NULL;
284 it = entry->flags.indicator_type;
285 ion = entry->flags.indicator_on;
287 wMenuRemoveItem(switchmenu, i);
289 if (!IS_OMNIPRESENT(wwin) && idx < 0) {
290 idx = menuIndexForWindow(switchmenu, wwin, i);
293 entry = wMenuInsertCallback(switchmenu, idx, t,
294 focusWindow, wwin);
295 free(t);
296 entry->rtext = rt;
297 entry->flags.indicator = 1;
298 entry->flags.indicator_type = it;
299 entry->flags.indicator_on = ion;
301 wMenuRealize(switchmenu);
302 checkVisibility = 1;
303 break;
306 case ACTION_CHANGE_STATE:
307 if (wwin->flags.hidden) {
308 entry->flags.indicator_type = MI_HIDDEN;
309 entry->flags.indicator_on = 1;
310 } else if (wwin->flags.miniaturized) {
311 entry->flags.indicator_type = MI_MINIWINDOW;
312 entry->flags.indicator_on = 1;
313 } else if (wwin->flags.shaded && !wwin->flags.focused) {
314 entry->flags.indicator_type = MI_SHADED;
315 entry->flags.indicator_on = 1;
316 } else {
317 entry->flags.indicator_on = wwin->flags.focused;
318 entry->flags.indicator_type = MI_DIAMOND;
320 wMenuPaint(switchmenu);
321 break;
323 break;
327 if (checkVisibility) {
328 int tmp;
330 tmp = switchmenu->frame->top_width + 5;
331 /* if menu got unreachable, bring it to a visible place */
332 if (switchmenu->frame_x < tmp - (int)switchmenu->frame->core->width) {
333 wMenuMove(switchmenu, tmp - (int)switchmenu->frame->core->width,
334 switchmenu->frame_y, False);
337 wMenuPaint(switchmenu);
342 void
343 UpdateSwitchMenuWorkspace(WScreen *scr, int workspace)
345 WMenu *menu = scr->switch_menu;
346 int i;
347 WWindow *wwin;
349 if (!menu)
350 return;
352 for (i=0; i<menu->entry_no; i++) {
353 wwin = (WWindow*)menu->entries[i]->clientdata;
355 if (wwin->frame->workspace==workspace
356 && !IS_OMNIPRESENT(wwin)) {
357 if (IS_OMNIPRESENT(wwin))
358 sprintf(menu->entries[i]->rtext, "[*]");
359 else
360 sprintf(menu->entries[i]->rtext, "[%s]",
361 scr->workspaces[wwin->frame->workspace]->name);
362 menu->flags.realized = 0;
365 if (!menu->flags.realized)
366 wMenuRealize(menu);
370 #endif /* !LITE */