wmaker: Marked args as unused at places where conditional code is used
[wmaker-crm.git] / src / winmenu.c
blobf2a10ee8b05e49576ed79a4e8718a5de61770507
1 /* winmenu.c - command menu for windows
3 * Window Maker window manager
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/XKBlib.h>
33 #include "WindowMaker.h"
34 #include "actions.h"
35 #include "menu.h"
36 #include "main.h"
37 #include "window.h"
38 #include "client.h"
39 #include "application.h"
40 #include "keybind.h"
41 #include "misc.h"
42 #include "framewin.h"
43 #include "workspace.h"
44 #include "winspector.h"
45 #include "dialog.h"
46 #include "stacking.h"
47 #include "icon.h"
48 #include "xinerama.h"
49 #include "winmenu.h"
51 #define MC_MAXIMIZE 0
52 #define MC_MINIATURIZE 1
53 #define MC_SHADE 2
54 #define MC_HIDE 3
55 #define MC_MOVERESIZE 4
56 #define MC_SELECT 5
57 #define MC_DUMMY_MOVETO 6
58 #define MC_PROPERTIES 7
59 #define MC_OPTIONS 8
60 #define MC_SHORTCUT 8
61 #define MC_RELAUNCH 9
63 #define MC_CLOSE 10
64 #define MC_KILL 11
66 #define WO_KEEP_ON_TOP 0
67 #define WO_KEEP_AT_BOTTOM 1
68 #define WO_OMNIPRESENT 2
69 #define WO_ENTRIES 3
72 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
74 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
76 WWindow *wwin = (WWindow *) entry->clientdata;
78 switch (entry->order) {
79 case WO_KEEP_ON_TOP:
80 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
81 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
82 else
83 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
84 break;
86 case WO_KEEP_AT_BOTTOM:
87 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
88 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
89 else
90 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
91 break;
93 case WO_OMNIPRESENT:
94 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
95 break;
99 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
101 WWindow *wwin = (WWindow *) entry->clientdata;
102 WApplication *wapp;
104 CloseWindowMenu(menu->frame->screen_ptr);
106 switch (entry->order) {
107 case MC_CLOSE:
108 /* send delete message */
109 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
110 w_global.timestamp.last_event);
111 break;
113 case MC_KILL:
114 wretain(wwin);
115 if (wPreferences.dont_confirm_kill
116 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
118 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
119 _("Yes"), _("No"), NULL) == WAPRDefault) {
120 if (!wwin->flags.destroyed)
121 wClientKill(wwin);
123 wrelease(wwin);
124 break;
126 case MC_MINIATURIZE:
127 if (wwin->flags.miniaturized) {
128 wDeiconifyWindow(wwin);
129 } else {
130 if (wwin->protocols.MINIATURIZE_WINDOW) {
131 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
132 w_global.timestamp.last_event);
133 } else {
134 wIconifyWindow(wwin);
137 break;
139 case MC_MAXIMIZE:
140 if (wwin->flags.maximized)
141 wUnmaximizeWindow(wwin);
142 else
143 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
144 break;
146 case MC_SHADE:
147 if (wwin->flags.shaded)
148 wUnshadeWindow(wwin);
149 else
150 wShadeWindow(wwin);
151 break;
153 case MC_SELECT:
154 if (!wwin->flags.miniaturized)
155 wSelectWindow(wwin, !wwin->flags.selected);
156 else
157 wIconSelect(wwin->icon);
158 break;
160 case MC_MOVERESIZE:
161 wKeyboardMoveResizeWindow(wwin);
162 break;
164 case MC_PROPERTIES:
165 wShowInspectorForWindow(wwin);
166 break;
168 case MC_RELAUNCH:
169 (void) RelaunchWindow(wwin);
170 break;
172 case MC_HIDE:
173 wapp = wApplicationOf(wwin->main_window);
174 wHideApplication(wapp);
175 break;
179 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
181 WWindow *wwin = (WWindow *) entry->clientdata;
183 wSelectWindow(wwin, False);
184 wWindowChangeWorkspace(wwin, entry->order);
187 static void makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
189 WWindow *wwin = (WWindow *) entry->clientdata;
190 WScreen *scr = wwin->screen_ptr;
191 int index = entry->order - WO_ENTRIES;
193 if (w_global.shortcut.windows[index]) {
194 WMFreeArray(w_global.shortcut.windows[index]);
195 w_global.shortcut.windows[index] = NULL;
198 if (wwin->flags.selected && scr->selected_windows) {
199 w_global.shortcut.windows[index] = WMDuplicateArray(scr->selected_windows);
200 } else {
201 w_global.shortcut.windows[index] = WMCreateArray(4);
202 WMAddToArray(w_global.shortcut.windows[index], wwin);
205 wSelectWindow(wwin, !wwin->flags.selected);
206 XFlush(dpy);
207 wusleep(3000);
208 wSelectWindow(wwin, !wwin->flags.selected);
209 XFlush(dpy);
212 static void updateWorkspaceMenu(WMenu * menu)
214 char title[MAX_WORKSPACENAME_WIDTH + 1];
215 WMenuEntry *entry;
216 int i;
218 for (i = 0; i < w_global.workspace.count; i++) {
219 if (i < menu->entry_no) {
221 entry = menu->entries[i];
222 if (strcmp(entry->text, w_global.workspace.array[i]->name) != 0) {
223 wfree(entry->text);
224 strncpy(title, w_global.workspace.array[i]->name, MAX_WORKSPACENAME_WIDTH);
225 title[MAX_WORKSPACENAME_WIDTH] = 0;
226 menu->entries[i]->text = wstrdup(title);
227 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
228 menu->flags.realized = 0;
230 } else {
231 strncpy(title, w_global.workspace.array[i]->name, MAX_WORKSPACENAME_WIDTH);
232 title[MAX_WORKSPACENAME_WIDTH] = 0;
234 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
235 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
237 menu->flags.realized = 0;
240 /* workspace shortcut labels */
241 if (i / 10 == w_global.workspace.current / 10)
242 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
243 else
244 entry->rtext = NULL;
247 if (!menu->flags.realized)
248 wMenuRealize(menu);
251 static void updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
253 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
254 int i;
255 char *buffer;
256 int buflen;
257 KeyCode kcode;
259 if (!smenu)
260 return;
262 buflen = strlen(_("Set Shortcut")) + 16;
263 buffer = wmalloc(buflen);
265 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
266 char *tmp;
267 int shortcutNo = i - WO_ENTRIES;
268 WMenuEntry *entry = smenu->entries[i];
269 WMArray *shortSelWindows = w_global.shortcut.windows[shortcutNo];
271 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
273 if (!shortSelWindows) {
274 entry->flags.indicator_on = 0;
275 } else {
276 entry->flags.indicator_on = 1;
277 if (WMCountInArray(shortSelWindows, wwin))
278 entry->flags.indicator_type = MI_DIAMOND;
279 else
280 entry->flags.indicator_type = MI_CHECK;
283 if (strcmp(buffer, entry->text) != 0) {
284 wfree(entry->text);
285 entry->text = wstrdup(buffer);
286 smenu->flags.realized = 0;
289 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
291 if (kcode) {
292 if ((tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]))
293 && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) {
294 if (entry->rtext)
295 wfree(entry->rtext);
296 entry->rtext = tmp;
297 smenu->flags.realized = 0;
299 wMenuSetEnabled(smenu, i, True);
300 } else {
301 wMenuSetEnabled(smenu, i, False);
302 if (entry->rtext) {
303 wfree(entry->rtext);
304 entry->rtext = NULL;
305 smenu->flags.realized = 0;
308 entry->clientdata = wwin;
310 wfree(buffer);
311 if (!smenu->flags.realized)
312 wMenuRealize(smenu);
315 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
317 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
319 /* keep on top check */
320 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
321 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
322 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
323 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
325 /* keep at bottom check */
326 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
327 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
328 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
329 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
331 /* omnipresent check */
332 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
333 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
335 smenu->flags.realized = 0;
336 wMenuRealize(smenu);
339 static WMenu *makeWorkspaceMenu(WScreen * scr)
341 WMenu *menu;
343 menu = wMenuCreate(scr, NULL, False);
344 if (!menu) {
345 wwarning(_("could not create submenu for window menu"));
346 return NULL;
349 updateWorkspaceMenu(menu);
351 return menu;
354 static WMenu *makeMakeShortcutMenu(WMenu *menu)
356 int i;
358 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
359 WMenuEntry *entry;
360 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
362 entry->flags.indicator = 1;
365 return menu;
368 static WMenu *makeOptionsMenu(WScreen * scr)
370 WMenu *menu;
371 WMenuEntry *entry;
373 menu = wMenuCreate(scr, NULL, False);
374 if (!menu) {
375 wwarning(_("could not create submenu for window menu"));
376 return NULL;
379 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
380 entry->flags.indicator = 1;
381 entry->flags.indicator_type = MI_CHECK;
383 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
384 entry->flags.indicator = 1;
385 entry->flags.indicator_type = MI_CHECK;
387 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
388 entry->flags.indicator = 1;
389 entry->flags.indicator_type = MI_CHECK;
391 return menu;
394 static WMenu *createWindowMenu(WScreen * scr)
396 WMenu *menu;
397 WMenuEntry *entry;
399 menu = wMenuCreate(scr, NULL, False);
401 * Warning: If you make some change that affects the order of the
402 * entries, you must update the command #defines in the top of
403 * this file.
405 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
406 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
408 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
409 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
411 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
412 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
414 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
415 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
417 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
418 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
420 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
421 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
423 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
424 w_global.workspace.submenu = makeWorkspaceMenu(scr);
425 if (w_global.workspace.submenu)
426 wMenuEntrySetCascade(menu, entry, w_global.workspace.submenu);
428 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
430 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
431 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(makeOptionsMenu(scr)));
433 entry = wMenuAddCallback(menu, _("Launch"), execMenuCommand, NULL);
434 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
436 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
437 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
439 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
441 return menu;
444 void CloseWindowMenu(WScreen * scr)
446 if (scr->window_menu) {
447 if (scr->window_menu->flags.mapped)
448 wMenuUnmap(scr->window_menu);
450 if (scr->window_menu->entries[0]->clientdata) {
451 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
453 wwin->flags.menu_open_for_me = 0;
455 scr->window_menu->entries[0]->clientdata = NULL;
459 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
461 WApplication *wapp = wApplicationOf(wwin->main_window);
462 int i;
464 updateOptionsMenu(menu, wwin);
466 updateMakeShortcutMenu(menu, wwin);
468 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
470 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
472 if (wwin->flags.miniaturized) {
473 static char *text = NULL;
474 if (!text)
475 text = _("Deminiaturize");
477 menu->entries[MC_MINIATURIZE]->text = text;
478 } else {
479 static char *text = NULL;
480 if (!text)
481 text = _("Miniaturize");
483 menu->entries[MC_MINIATURIZE]->text = text;
486 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
488 if (wwin->flags.maximized) {
489 static char *text = NULL;
490 if (!text)
491 text = _("Unmaximize");
493 menu->entries[MC_MAXIMIZE]->text = text;
494 } else {
495 static char *text = NULL;
496 if (!text)
497 text = _("Maximize");
499 menu->entries[MC_MAXIMIZE]->text = text;
501 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
503 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
504 && !wwin->flags.miniaturized);
506 if (wwin->flags.shaded) {
507 static char *text = NULL;
508 if (!text)
509 text = _("Unshade");
511 menu->entries[MC_SHADE]->text = text;
512 } else {
513 static char *text = NULL;
514 if (!text)
515 text = _("Shade");
517 menu->entries[MC_SHADE]->text = text;
520 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
521 && !wwin->flags.miniaturized);
523 if (wwin->flags.selected) {
524 static char *text = NULL;
525 if (!text)
526 text = _("Deselect");
528 menu->entries[MC_SELECT]->text = text;
529 } else {
530 static char *text = NULL;
531 if (!text)
532 text = _("Select");
534 menu->entries[MC_SELECT]->text = text;
537 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
539 if (!wwin->flags.inspector_open) {
540 wMenuSetEnabled(menu, MC_PROPERTIES, True);
541 } else {
542 wMenuSetEnabled(menu, MC_PROPERTIES, False);
545 /* set the client data of the entries to the window */
546 for (i = 0; i < menu->entry_no; i++) {
547 menu->entries[i]->clientdata = wwin;
550 for (i = 0; i < w_global.workspace.submenu->entry_no; i++) {
551 w_global.workspace.submenu->entries[i]->clientdata = wwin;
553 if (i == w_global.workspace.current)
554 wMenuSetEnabled(w_global.workspace.submenu, i, False);
555 else
556 wMenuSetEnabled(w_global.workspace.submenu, i, True);
559 menu->flags.realized = 0;
560 wMenuRealize(menu);
563 static WMenu *open_window_menu_core(WWindow *wwin)
565 WScreen *scr = wwin->screen_ptr;
566 WMenu *menu;
568 wwin->flags.menu_open_for_me = 1;
570 if (!scr->window_menu) {
571 scr->window_menu = createWindowMenu(scr);
573 /* hack to save some memory allocation/deallocation */
574 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
575 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
576 wfree(scr->window_menu->entries[MC_SHADE]->text);
577 wfree(scr->window_menu->entries[MC_SELECT]->text);
578 } else {
579 updateWorkspaceMenu(w_global.workspace.submenu);
582 menu = scr->window_menu;
583 if (menu->flags.mapped) {
584 wMenuUnmap(menu);
585 if (menu->entries[0]->clientdata == wwin)
586 return NULL;
589 updateMenuForWindow(menu, wwin);
591 return menu;
594 static void prepare_menu_position(WMenu *menu, int x, int y)
596 WMRect rect;
598 rect = wGetRectForHead(menu->frame->screen_ptr,
599 wGetHeadForPointerLocation(menu->frame->screen_ptr));
600 if (x < rect.pos.x - menu->frame->core->width / 2)
601 x = rect.pos.x - menu->frame->core->width / 2;
602 if (y < rect.pos.y)
603 y = rect.pos.y;
606 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
608 WMenu *menu;
610 menu = open_window_menu_core(wwin);
611 if (!menu)
612 return;
614 /* Specific menu position */
615 x -= menu->frame->core->width / 2;
616 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
617 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
618 if (x < wwin->frame_x)
619 x = wwin->frame_x;
621 /* Common menu position */
622 prepare_menu_position(menu, x, y);
624 if (!wwin->flags.internal_window)
625 wMenuMapAt(menu, x, y, keyboard);
628 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
630 int i;
631 WMenu *menu;
633 menu = open_window_menu_core(wwin);
634 if (!menu)
635 return;
637 /* Specific menu position */
638 for (i = 0; i < w_global.workspace.submenu->entry_no; i++) {
639 w_global.workspace.submenu->entries[i]->clientdata = wwin;
640 wMenuSetEnabled(w_global.workspace.submenu, i, True);
643 x -= menu->frame->core->width / 2;
645 /* Common menu position */
646 prepare_menu_position(menu, x, y);
648 if (!wwin->flags.internal_window)
649 wMenuMapAt(menu, x, y, keyboard);
652 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
654 WMenu *menu;
656 menu = open_window_menu_core(wwin);
657 if (!menu)
658 return;
660 x -= menu->frame->core->width / 2;
662 wMenuMapAt(menu, x, y, False);
665 void DestroyWindowMenu(WScreen *scr)
667 if (scr->window_menu) {
668 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
669 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
670 scr->window_menu->entries[MC_SHADE]->text = NULL;
671 scr->window_menu->entries[MC_SELECT]->text = NULL;
672 wMenuDestroy(scr->window_menu, True);
673 scr->window_menu = NULL;