wmaker: Marked args as unused for compiler in menu callback code
[wmaker-crm.git] / src / winmenu.c
blob04db90dd3ca74018a0a963aa1433d6f0f5b7487c
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 /* Parameter not used, but tell the compiler that it is ok */
79 (void) menu;
81 switch (entry->order) {
82 case WO_KEEP_ON_TOP:
83 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
84 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
85 else
86 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
87 break;
89 case WO_KEEP_AT_BOTTOM:
90 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
91 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
92 else
93 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
94 break;
96 case WO_OMNIPRESENT:
97 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
98 break;
102 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
104 WWindow *wwin = (WWindow *) entry->clientdata;
105 WApplication *wapp;
107 CloseWindowMenu(menu->frame->screen_ptr);
109 switch (entry->order) {
110 case MC_CLOSE:
111 /* send delete message */
112 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
113 w_global.timestamp.last_event);
114 break;
116 case MC_KILL:
117 wretain(wwin);
118 if (wPreferences.dont_confirm_kill
119 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
121 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
122 _("Yes"), _("No"), NULL) == WAPRDefault) {
123 if (!wwin->flags.destroyed)
124 wClientKill(wwin);
126 wrelease(wwin);
127 break;
129 case MC_MINIATURIZE:
130 if (wwin->flags.miniaturized) {
131 wDeiconifyWindow(wwin);
132 } else {
133 if (wwin->protocols.MINIATURIZE_WINDOW) {
134 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
135 w_global.timestamp.last_event);
136 } else {
137 wIconifyWindow(wwin);
140 break;
142 case MC_MAXIMIZE:
143 if (wwin->flags.maximized)
144 wUnmaximizeWindow(wwin);
145 else
146 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
147 break;
149 case MC_SHADE:
150 if (wwin->flags.shaded)
151 wUnshadeWindow(wwin);
152 else
153 wShadeWindow(wwin);
154 break;
156 case MC_SELECT:
157 if (!wwin->flags.miniaturized)
158 wSelectWindow(wwin, !wwin->flags.selected);
159 else
160 wIconSelect(wwin->icon);
161 break;
163 case MC_MOVERESIZE:
164 wKeyboardMoveResizeWindow(wwin);
165 break;
167 case MC_PROPERTIES:
168 wShowInspectorForWindow(wwin);
169 break;
171 case MC_RELAUNCH:
172 (void) RelaunchWindow(wwin);
173 break;
175 case MC_HIDE:
176 wapp = wApplicationOf(wwin->main_window);
177 wHideApplication(wapp);
178 break;
182 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
184 WWindow *wwin = (WWindow *) entry->clientdata;
186 /* Parameter not used, but tell the compiler that it is ok */
187 (void) menu;
189 wSelectWindow(wwin, False);
190 wWindowChangeWorkspace(wwin, entry->order);
193 static void makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
195 WWindow *wwin = (WWindow *) entry->clientdata;
196 WScreen *scr = wwin->screen_ptr;
197 int index = entry->order - WO_ENTRIES;
199 /* Parameter not used, but tell the compiler that it is ok */
200 (void) menu;
202 if (w_global.shortcut.windows[index]) {
203 WMFreeArray(w_global.shortcut.windows[index]);
204 w_global.shortcut.windows[index] = NULL;
207 if (wwin->flags.selected && scr->selected_windows) {
208 w_global.shortcut.windows[index] = WMDuplicateArray(scr->selected_windows);
209 } else {
210 w_global.shortcut.windows[index] = WMCreateArray(4);
211 WMAddToArray(w_global.shortcut.windows[index], wwin);
214 wSelectWindow(wwin, !wwin->flags.selected);
215 XFlush(dpy);
216 wusleep(3000);
217 wSelectWindow(wwin, !wwin->flags.selected);
218 XFlush(dpy);
221 static void updateWorkspaceMenu(WMenu * menu)
223 char title[MAX_WORKSPACENAME_WIDTH + 1];
224 WMenuEntry *entry;
225 int i;
227 for (i = 0; i < w_global.workspace.count; i++) {
228 if (i < menu->entry_no) {
230 entry = menu->entries[i];
231 if (strcmp(entry->text, w_global.workspace.array[i]->name) != 0) {
232 wfree(entry->text);
233 strncpy(title, w_global.workspace.array[i]->name, MAX_WORKSPACENAME_WIDTH);
234 title[MAX_WORKSPACENAME_WIDTH] = 0;
235 menu->entries[i]->text = wstrdup(title);
236 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
237 menu->flags.realized = 0;
239 } else {
240 strncpy(title, w_global.workspace.array[i]->name, MAX_WORKSPACENAME_WIDTH);
241 title[MAX_WORKSPACENAME_WIDTH] = 0;
243 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
244 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
246 menu->flags.realized = 0;
249 /* workspace shortcut labels */
250 if (i / 10 == w_global.workspace.current / 10)
251 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
252 else
253 entry->rtext = NULL;
256 if (!menu->flags.realized)
257 wMenuRealize(menu);
260 static void updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
262 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
263 int i;
264 char *buffer;
265 int buflen;
266 KeyCode kcode;
268 if (!smenu)
269 return;
271 buflen = strlen(_("Set Shortcut")) + 16;
272 buffer = wmalloc(buflen);
274 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
275 char *tmp;
276 int shortcutNo = i - WO_ENTRIES;
277 WMenuEntry *entry = smenu->entries[i];
278 WMArray *shortSelWindows = w_global.shortcut.windows[shortcutNo];
280 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
282 if (!shortSelWindows) {
283 entry->flags.indicator_on = 0;
284 } else {
285 entry->flags.indicator_on = 1;
286 if (WMCountInArray(shortSelWindows, wwin))
287 entry->flags.indicator_type = MI_DIAMOND;
288 else
289 entry->flags.indicator_type = MI_CHECK;
292 if (strcmp(buffer, entry->text) != 0) {
293 wfree(entry->text);
294 entry->text = wstrdup(buffer);
295 smenu->flags.realized = 0;
298 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
300 if (kcode) {
301 if ((tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]))
302 && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) {
303 if (entry->rtext)
304 wfree(entry->rtext);
305 entry->rtext = tmp;
306 smenu->flags.realized = 0;
308 wMenuSetEnabled(smenu, i, True);
309 } else {
310 wMenuSetEnabled(smenu, i, False);
311 if (entry->rtext) {
312 wfree(entry->rtext);
313 entry->rtext = NULL;
314 smenu->flags.realized = 0;
317 entry->clientdata = wwin;
319 wfree(buffer);
320 if (!smenu->flags.realized)
321 wMenuRealize(smenu);
324 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
326 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
328 /* keep on top check */
329 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
330 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
331 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
332 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
334 /* keep at bottom check */
335 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
336 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
337 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
338 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
340 /* omnipresent check */
341 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
342 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
344 smenu->flags.realized = 0;
345 wMenuRealize(smenu);
348 static WMenu *makeWorkspaceMenu(WScreen * scr)
350 WMenu *menu;
352 menu = wMenuCreate(scr, NULL, False);
353 if (!menu) {
354 wwarning(_("could not create submenu for window menu"));
355 return NULL;
358 updateWorkspaceMenu(menu);
360 return menu;
363 static WMenu *makeMakeShortcutMenu(WMenu *menu)
365 int i;
367 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
368 WMenuEntry *entry;
369 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
371 entry->flags.indicator = 1;
374 return menu;
377 static WMenu *makeOptionsMenu(WScreen * scr)
379 WMenu *menu;
380 WMenuEntry *entry;
382 menu = wMenuCreate(scr, NULL, False);
383 if (!menu) {
384 wwarning(_("could not create submenu for window menu"));
385 return NULL;
388 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
389 entry->flags.indicator = 1;
390 entry->flags.indicator_type = MI_CHECK;
392 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
393 entry->flags.indicator = 1;
394 entry->flags.indicator_type = MI_CHECK;
396 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
397 entry->flags.indicator = 1;
398 entry->flags.indicator_type = MI_CHECK;
400 return menu;
403 static WMenu *createWindowMenu(WScreen * scr)
405 WMenu *menu;
406 WMenuEntry *entry;
408 menu = wMenuCreate(scr, NULL, False);
410 * Warning: If you make some change that affects the order of the
411 * entries, you must update the command #defines in the top of
412 * this file.
414 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
415 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
417 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
418 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
420 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
421 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
423 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
424 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
426 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
427 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
429 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
430 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
432 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
433 w_global.workspace.submenu = makeWorkspaceMenu(scr);
434 if (w_global.workspace.submenu)
435 wMenuEntrySetCascade(menu, entry, w_global.workspace.submenu);
437 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
439 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
440 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(makeOptionsMenu(scr)));
442 entry = wMenuAddCallback(menu, _("Launch"), execMenuCommand, NULL);
443 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
445 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
446 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
448 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
450 return menu;
453 void CloseWindowMenu(WScreen * scr)
455 if (scr->window_menu) {
456 if (scr->window_menu->flags.mapped)
457 wMenuUnmap(scr->window_menu);
459 if (scr->window_menu->entries[0]->clientdata) {
460 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
462 wwin->flags.menu_open_for_me = 0;
464 scr->window_menu->entries[0]->clientdata = NULL;
468 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
470 WApplication *wapp = wApplicationOf(wwin->main_window);
471 int i;
473 updateOptionsMenu(menu, wwin);
475 updateMakeShortcutMenu(menu, wwin);
477 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
479 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
481 if (wwin->flags.miniaturized) {
482 static char *text = NULL;
483 if (!text)
484 text = _("Deminiaturize");
486 menu->entries[MC_MINIATURIZE]->text = text;
487 } else {
488 static char *text = NULL;
489 if (!text)
490 text = _("Miniaturize");
492 menu->entries[MC_MINIATURIZE]->text = text;
495 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
497 if (wwin->flags.maximized) {
498 static char *text = NULL;
499 if (!text)
500 text = _("Unmaximize");
502 menu->entries[MC_MAXIMIZE]->text = text;
503 } else {
504 static char *text = NULL;
505 if (!text)
506 text = _("Maximize");
508 menu->entries[MC_MAXIMIZE]->text = text;
510 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
512 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
513 && !wwin->flags.miniaturized);
515 if (wwin->flags.shaded) {
516 static char *text = NULL;
517 if (!text)
518 text = _("Unshade");
520 menu->entries[MC_SHADE]->text = text;
521 } else {
522 static char *text = NULL;
523 if (!text)
524 text = _("Shade");
526 menu->entries[MC_SHADE]->text = text;
529 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
530 && !wwin->flags.miniaturized);
532 if (wwin->flags.selected) {
533 static char *text = NULL;
534 if (!text)
535 text = _("Deselect");
537 menu->entries[MC_SELECT]->text = text;
538 } else {
539 static char *text = NULL;
540 if (!text)
541 text = _("Select");
543 menu->entries[MC_SELECT]->text = text;
546 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
548 if (!wwin->flags.inspector_open) {
549 wMenuSetEnabled(menu, MC_PROPERTIES, True);
550 } else {
551 wMenuSetEnabled(menu, MC_PROPERTIES, False);
554 /* set the client data of the entries to the window */
555 for (i = 0; i < menu->entry_no; i++) {
556 menu->entries[i]->clientdata = wwin;
559 for (i = 0; i < w_global.workspace.submenu->entry_no; i++) {
560 w_global.workspace.submenu->entries[i]->clientdata = wwin;
562 if (i == w_global.workspace.current)
563 wMenuSetEnabled(w_global.workspace.submenu, i, False);
564 else
565 wMenuSetEnabled(w_global.workspace.submenu, i, True);
568 menu->flags.realized = 0;
569 wMenuRealize(menu);
572 static WMenu *open_window_menu_core(WWindow *wwin)
574 WScreen *scr = wwin->screen_ptr;
575 WMenu *menu;
577 wwin->flags.menu_open_for_me = 1;
579 if (!scr->window_menu) {
580 scr->window_menu = createWindowMenu(scr);
582 /* hack to save some memory allocation/deallocation */
583 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
584 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
585 wfree(scr->window_menu->entries[MC_SHADE]->text);
586 wfree(scr->window_menu->entries[MC_SELECT]->text);
587 } else {
588 updateWorkspaceMenu(w_global.workspace.submenu);
591 menu = scr->window_menu;
592 if (menu->flags.mapped) {
593 wMenuUnmap(menu);
594 if (menu->entries[0]->clientdata == wwin)
595 return NULL;
598 updateMenuForWindow(menu, wwin);
600 return menu;
603 static void prepare_menu_position(WMenu *menu, int x, int y)
605 WMRect rect;
607 rect = wGetRectForHead(menu->frame->screen_ptr,
608 wGetHeadForPointerLocation(menu->frame->screen_ptr));
609 if (x < rect.pos.x - menu->frame->core->width / 2)
610 x = rect.pos.x - menu->frame->core->width / 2;
611 if (y < rect.pos.y)
612 y = rect.pos.y;
615 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
617 WMenu *menu;
619 menu = open_window_menu_core(wwin);
620 if (!menu)
621 return;
623 /* Specific menu position */
624 x -= menu->frame->core->width / 2;
625 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
626 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
627 if (x < wwin->frame_x)
628 x = wwin->frame_x;
630 /* Common menu position */
631 prepare_menu_position(menu, x, y);
633 if (!wwin->flags.internal_window)
634 wMenuMapAt(menu, x, y, keyboard);
637 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
639 int i;
640 WMenu *menu;
642 menu = open_window_menu_core(wwin);
643 if (!menu)
644 return;
646 /* Specific menu position */
647 for (i = 0; i < w_global.workspace.submenu->entry_no; i++) {
648 w_global.workspace.submenu->entries[i]->clientdata = wwin;
649 wMenuSetEnabled(w_global.workspace.submenu, i, True);
652 x -= menu->frame->core->width / 2;
654 /* Common menu position */
655 prepare_menu_position(menu, x, y);
657 if (!wwin->flags.internal_window)
658 wMenuMapAt(menu, x, y, keyboard);
661 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
663 WMenu *menu;
665 menu = open_window_menu_core(wwin);
666 if (!menu)
667 return;
669 x -= menu->frame->core->width / 2;
671 wMenuMapAt(menu, x, y, False);
674 void DestroyWindowMenu(WScreen *scr)
676 if (scr->window_menu) {
677 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
678 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
679 scr->window_menu->entries[MC_SHADE]->text = NULL;
680 scr->window_menu->entries[MC_SELECT]->text = NULL;
681 wMenuDestroy(scr->window_menu, True);
682 scr->window_menu = NULL;