- Fixed some non-portable shell constructs used in installed scripts
[wmaker-crm.git] / src / switchmenu.c
blob4d1cdbc867a04ca4a58c89bbcc0b14df26f2f4d8
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,
106 x-switchmenu->frame->core->width/2, y);
108 } else {
109 wMenuMapAt(switchmenu, x-switchmenu->frame->core->width/2, y,
110 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 int newx, newy;
128 if (!switchmenu->flags.realized)
129 wMenuRealize(switchmenu);
131 if (keyboard && x==0 && y==0) {
132 newx = newy = 0;
133 } else if (keyboard && x==scr->scr_width/2 && y==scr->scr_height/2) {
134 newx = x - switchmenu->frame->core->width/2;
135 newy = y - switchmenu->frame->core->height/2;
136 } else {
137 newx = x - switchmenu->frame->core->width/2;
138 newy = y;
140 wMenuMapAt(switchmenu, newx, newy, keyboard);
145 static int
146 menuIndexForWindow(WMenu *menu, WWindow *wwin, int old_pos)
148 int idx;
150 if (menu->entry_no == 0)
151 return -1;
153 #define WS(i) ((WWindow*)menu->entries[i]->clientdata)->frame->workspace
154 if (old_pos >= 0) {
155 if (WS(old_pos) >= wwin->frame->workspace
156 && (old_pos == 0 || WS(old_pos-1) <= wwin->frame->workspace)) {
157 return old_pos;
160 #undef WS
162 for (idx = 0; idx < menu->entry_no; idx++) {
163 WWindow *tw = (WWindow*)menu->entries[idx]->clientdata;
165 if (!IS_OMNIPRESENT(tw)
166 && tw->frame->workspace > wwin->frame->workspace) {
167 break;
171 return idx;
176 * Update switch menu
178 void
179 UpdateSwitchMenu(WScreen *scr, WWindow *wwin, int action)
181 WMenu *switchmenu = scr->switch_menu;
182 WMenuEntry *entry;
183 char title[MAX_MENU_TEXT_LENGTH+6];
184 int i;
185 int checkVisibility = 0;
187 if (!wwin->screen_ptr->switch_menu)
188 return;
190 * This menu is updated under the following conditions:
192 * 1. When a window is created.
193 * 2. When a window is destroyed.
195 * 3. When a window changes it's title.
196 * 4. When a window changes its workspace.
198 if (action == ACTION_ADD) {
199 char *t;
200 int idx;
202 if (wwin->flags.internal_window || WFLAGP(wwin, skip_window_list))
203 return;
205 if (wwin->frame->title)
206 sprintf(title, "%s", wwin->frame->title);
207 else
208 sprintf(title, "%s", DEF_WINDOW_TITLE);
209 t = ShrinkString(scr->menu_entry_font, title, MAX_WINDOWLIST_WIDTH);
211 if (IS_OMNIPRESENT(wwin))
212 idx = -1;
213 else {
214 idx = menuIndexForWindow(switchmenu, wwin, -1);
217 entry = wMenuInsertCallback(switchmenu, idx, t, focusWindow, wwin);
218 wfree(t);
220 entry->flags.indicator = 1;
221 entry->rtext = wmalloc(MAX_WORKSPACENAME_WIDTH+8);
222 if (IS_OMNIPRESENT(wwin))
223 sprintf(entry->rtext, "[*]");
224 else
225 sprintf(entry->rtext, "[%s]",
226 scr->workspaces[wwin->frame->workspace]->name);
228 if (wwin->flags.hidden) {
229 entry->flags.indicator_type = MI_HIDDEN;
230 entry->flags.indicator_on = 1;
231 } else if (wwin->flags.miniaturized) {
232 entry->flags.indicator_type = MI_MINIWINDOW;
233 entry->flags.indicator_on = 1;
234 } else if (wwin->flags.focused) {
235 entry->flags.indicator_type = MI_DIAMOND;
236 entry->flags.indicator_on = 1;
237 } else if (wwin->flags.shaded) {
238 entry->flags.indicator_type = MI_SHADED;
239 entry->flags.indicator_on = 1;
242 wMenuRealize(switchmenu);
243 checkVisibility = 1;
244 } else {
245 char *t;
246 for (i=0; i<switchmenu->entry_no; i++) {
247 entry = switchmenu->entries[i];
248 /* this is the entry that was changed */
249 if (entry->clientdata == wwin) {
250 switch (action) {
251 case ACTION_REMOVE:
252 wMenuRemoveItem(switchmenu, i);
253 wMenuRealize(switchmenu);
254 checkVisibility = 1;
255 break;
257 case ACTION_CHANGE:
258 if (entry->text)
259 wfree(entry->text);
261 if (wwin->frame->title)
262 snprintf(title, MAX_MENU_TEXT_LENGTH, "%s",
263 wwin->frame->title);
264 else
265 sprintf(title, "%s", DEF_WINDOW_TITLE);
267 t = ShrinkString(scr->menu_entry_font, title, MAX_WINDOWLIST_WIDTH);
268 entry->text = t;
270 wMenuRealize(switchmenu);
271 checkVisibility = 1;
272 break;
274 case ACTION_CHANGE_WORKSPACE:
275 if (entry->rtext) {
276 int idx = -1;
277 char *t, *rt;
278 int it, ion;
280 if (IS_OMNIPRESENT(wwin)) {
281 sprintf(entry->rtext, "[*]");
282 } else {
283 sprintf(entry->rtext, "[%s]",
284 scr->workspaces[wwin->frame->workspace]->name);
287 rt = entry->rtext;
288 entry->rtext = NULL;
289 t = entry->text;
290 entry->text = NULL;
292 it = entry->flags.indicator_type;
293 ion = entry->flags.indicator_on;
295 wMenuRemoveItem(switchmenu, i);
297 if (!IS_OMNIPRESENT(wwin) && idx < 0) {
298 idx = menuIndexForWindow(switchmenu, wwin, i);
301 entry = wMenuInsertCallback(switchmenu, idx, t,
302 focusWindow, wwin);
303 wfree(t);
304 entry->rtext = rt;
305 entry->flags.indicator = 1;
306 entry->flags.indicator_type = it;
307 entry->flags.indicator_on = ion;
309 wMenuRealize(switchmenu);
310 checkVisibility = 1;
311 break;
314 case ACTION_CHANGE_STATE:
315 if (wwin->flags.hidden) {
316 entry->flags.indicator_type = MI_HIDDEN;
317 entry->flags.indicator_on = 1;
318 } else if (wwin->flags.miniaturized) {
319 entry->flags.indicator_type = MI_MINIWINDOW;
320 entry->flags.indicator_on = 1;
321 } else if (wwin->flags.shaded && !wwin->flags.focused) {
322 entry->flags.indicator_type = MI_SHADED;
323 entry->flags.indicator_on = 1;
324 } else {
325 entry->flags.indicator_on = wwin->flags.focused;
326 entry->flags.indicator_type = MI_DIAMOND;
328 break;
330 break;
334 if (checkVisibility) {
335 int tmp;
337 tmp = switchmenu->frame->top_width + 5;
338 /* if menu got unreachable, bring it to a visible place */
339 if (switchmenu->frame_x < tmp - (int)switchmenu->frame->core->width) {
340 wMenuMove(switchmenu, tmp - (int)switchmenu->frame->core->width,
341 switchmenu->frame_y, False);
344 wMenuPaint(switchmenu);
349 void
350 UpdateSwitchMenuWorkspace(WScreen *scr, int workspace)
352 WMenu *menu = scr->switch_menu;
353 int i;
354 WWindow *wwin;
356 if (!menu)
357 return;
359 for (i=0; i<menu->entry_no; i++) {
360 wwin = (WWindow*)menu->entries[i]->clientdata;
362 if (wwin->frame->workspace==workspace
363 && !IS_OMNIPRESENT(wwin)) {
364 if (IS_OMNIPRESENT(wwin))
365 sprintf(menu->entries[i]->rtext, "[*]");
366 else
367 sprintf(menu->entries[i]->rtext, "[%s]",
368 scr->workspaces[wwin->frame->workspace]->name);
369 menu->flags.realized = 0;
372 if (!menu->flags.realized)
373 wMenuRealize(menu);
377 #endif /* !LITE */