Added GetShortcutKey().
[wmaker-crm.git] / src / winmenu.c
blob983f7e86d5eea1feb44af2c29be041e6182fa392
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 "funcs.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"
50 #define MC_MAXIMIZE 0
51 #define MC_MINIATURIZE 1
52 #define MC_SHADE 2
53 #define MC_HIDE 3
54 #define MC_MOVERESIZE 4
55 #define MC_SELECT 5
56 #define MC_DUMMY_MOVETO 6
57 #define MC_PROPERTIES 7
58 #define MC_OPTIONS 8
59 #define MC_SHORTCUT 8
60 #define MC_RELAUNCH 9
62 #define MC_CLOSE 10
63 #define MC_KILL 11
65 #define WO_KEEP_ON_TOP 0
66 #define WO_KEEP_AT_BOTTOM 1
67 #define WO_OMNIPRESENT 2
68 #define WO_ENTRIES 3
70 /**** Global data ***/
71 extern Time LastTimestamp;
72 extern Atom _XA_WM_DELETE_WINDOW;
73 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
75 extern WShortKey wKeyBindings[WKBD_LAST];
77 extern WPreferences wPreferences;
79 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
81 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
83 WWindow *wwin = (WWindow *) entry->clientdata;
85 switch (entry->order) {
86 case WO_KEEP_ON_TOP:
87 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
88 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
89 else
90 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
91 break;
93 case WO_KEEP_AT_BOTTOM:
94 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
95 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
96 else
97 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
98 break;
100 case WO_OMNIPRESENT:
101 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
102 break;
106 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
108 WWindow *wwin = (WWindow *) entry->clientdata;
109 WApplication *wapp;
111 CloseWindowMenu(menu->frame->screen_ptr);
113 switch (entry->order) {
114 case MC_CLOSE:
115 /* send delete message */
116 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
117 break;
119 case MC_KILL:
120 wretain(wwin);
121 if (wPreferences.dont_confirm_kill
122 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
124 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
125 _("Yes"), _("No"), NULL) == WAPRDefault) {
126 if (!wwin->flags.destroyed)
127 wClientKill(wwin);
129 wrelease(wwin);
130 break;
132 case MC_MINIATURIZE:
133 if (wwin->flags.miniaturized) {
134 wDeiconifyWindow(wwin);
135 } else {
136 if (wwin->protocols.MINIATURIZE_WINDOW) {
137 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
138 } else {
139 wIconifyWindow(wwin);
142 break;
144 case MC_MAXIMIZE:
145 if (wwin->flags.maximized)
146 wUnmaximizeWindow(wwin);
147 else
148 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
149 break;
151 case MC_SHADE:
152 if (wwin->flags.shaded)
153 wUnshadeWindow(wwin);
154 else
155 wShadeWindow(wwin);
156 break;
158 case MC_SELECT:
159 if (!wwin->flags.miniaturized)
160 wSelectWindow(wwin, !wwin->flags.selected);
161 else
162 wIconSelect(wwin->icon);
163 break;
165 case MC_MOVERESIZE:
166 wKeyboardMoveResizeWindow(wwin);
167 break;
169 case MC_PROPERTIES:
170 wShowInspectorForWindow(wwin);
171 break;
173 case MC_RELAUNCH:
174 (void) RelaunchWindow(wwin);
175 break;
177 case MC_HIDE:
178 wapp = wApplicationOf(wwin->main_window);
179 wHideApplication(wapp);
180 break;
184 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
186 WWindow *wwin = (WWindow *) entry->clientdata;
188 wSelectWindow(wwin, False);
189 wWindowChangeWorkspace(wwin, entry->order);
192 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
194 WWindow *wwin = (WWindow *) entry->clientdata;
195 WScreen *scr = wwin->screen_ptr;
196 int index = entry->order - WO_ENTRIES;
198 if (scr->shortcutWindows[index]) {
199 WMFreeArray(scr->shortcutWindows[index]);
200 scr->shortcutWindows[index] = NULL;
203 if (wwin->flags.selected && scr->selected_windows) {
204 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
205 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
206 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
207 } else {
208 scr->shortcutWindows[index] = WMCreateArray(4);
209 WMAddToArray(scr->shortcutWindows[index], wwin);
212 wSelectWindow(wwin, !wwin->flags.selected);
213 XFlush(dpy);
214 wusleep(3000);
215 wSelectWindow(wwin, !wwin->flags.selected);
216 XFlush(dpy);
219 static void updateWorkspaceMenu(WMenu * menu)
221 WScreen *scr = menu->frame->screen_ptr;
222 char title[MAX_WORKSPACENAME_WIDTH + 1];
223 int i;
225 for (i = 0; i < scr->workspace_count; i++) {
226 if (i < menu->entry_no) {
227 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
228 wfree(menu->entries[i]->text);
229 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
230 title[MAX_WORKSPACENAME_WIDTH] = 0;
231 menu->entries[i]->text = wstrdup(title);
232 menu->flags.realized = 0;
234 } else {
235 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
236 title[MAX_WORKSPACENAME_WIDTH] = 0;
238 wMenuAddCallback(menu, title, switchWSCommand, NULL);
240 menu->flags.realized = 0;
244 if (!menu->flags.realized)
245 wMenuRealize(menu);
248 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
250 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
251 int i;
252 char *buffer;
253 int buflen;
254 KeyCode kcode;
256 if (!smenu)
257 return;
259 buflen = strlen(_("Set Shortcut")) + 16;
260 buffer = wmalloc(buflen);
262 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
263 char *tmp;
264 int shortcutNo = i - WO_ENTRIES;
265 WMenuEntry *entry = smenu->entries[i];
266 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
268 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
270 if (!shortSelWindows) {
271 entry->flags.indicator_on = 0;
272 } else {
273 entry->flags.indicator_on = 1;
274 if (WMCountInArray(shortSelWindows, wwin))
275 entry->flags.indicator_type = MI_DIAMOND;
276 else
277 entry->flags.indicator_type = MI_CHECK;
280 if (strcmp(buffer, entry->text) != 0) {
281 wfree(entry->text);
282 entry->text = wstrdup(buffer);
283 smenu->flags.realized = 0;
286 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
288 if (kcode) {
289 if ((tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]))
290 && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) {
291 if (entry->rtext)
292 wfree(entry->rtext);
293 entry->rtext = tmp;
294 smenu->flags.realized = 0;
296 wMenuSetEnabled(smenu, i, True);
297 } else {
298 wMenuSetEnabled(smenu, i, False);
299 if (entry->rtext) {
300 wfree(entry->rtext);
301 entry->rtext = NULL;
302 smenu->flags.realized = 0;
305 entry->clientdata = wwin;
307 wfree(buffer);
308 if (!smenu->flags.realized)
309 wMenuRealize(smenu);
312 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
314 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
316 /* keep on top check */
317 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
318 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
319 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
320 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
322 /* keep at bottom check */
323 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
324 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
325 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
326 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
328 /* omnipresent check */
329 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
330 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
332 smenu->flags.realized = 0;
333 wMenuRealize(smenu);
336 static WMenu *makeWorkspaceMenu(WScreen * scr)
338 WMenu *menu;
340 menu = wMenuCreate(scr, NULL, False);
341 if (!menu) {
342 wwarning(_("could not create submenu for window menu"));
343 return NULL;
346 updateWorkspaceMenu(menu);
348 return menu;
351 static WMenu *makeMakeShortcutMenu(WScreen * scr, WMenu * menu)
354 WMenu *menu;
356 int i;
358 menu = wMenuCreate(scr, NULL, False);
359 if (!menu) {
360 wwarning(_("could not create submenu for window menu"));
361 return NULL;
365 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
366 WMenuEntry *entry;
367 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
369 entry->flags.indicator = 1;
372 return menu;
375 static WMenu *makeOptionsMenu(WScreen * scr)
377 WMenu *menu;
378 WMenuEntry *entry;
380 menu = wMenuCreate(scr, NULL, False);
381 if (!menu) {
382 wwarning(_("could not create submenu for window menu"));
383 return NULL;
386 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
387 entry->flags.indicator = 1;
388 entry->flags.indicator_type = MI_CHECK;
390 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
391 entry->flags.indicator = 1;
392 entry->flags.indicator_type = MI_CHECK;
394 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
395 entry->flags.indicator = 1;
396 entry->flags.indicator_type = MI_CHECK;
398 return menu;
401 static WMenu *createWindowMenu(WScreen * scr)
403 WMenu *menu;
404 WMenuEntry *entry;
406 menu = wMenuCreate(scr, NULL, False);
408 * Warning: If you make some change that affects the order of the
409 * entries, you must update the command #defines in the top of
410 * this file.
412 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
413 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
415 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
416 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
418 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
419 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
421 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
422 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
424 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
425 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
427 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
428 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
430 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
431 scr->workspace_submenu = makeWorkspaceMenu(scr);
432 if (scr->workspace_submenu)
433 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
435 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
437 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
438 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
441 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
442 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
445 entry = wMenuAddCallback(menu, _("Launch"), execMenuCommand, NULL);
446 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
448 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
449 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
451 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
453 return menu;
456 void CloseWindowMenu(WScreen * scr)
458 if (scr->window_menu) {
459 if (scr->window_menu->flags.mapped)
460 wMenuUnmap(scr->window_menu);
462 if (scr->window_menu->entries[0]->clientdata) {
463 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
465 wwin->flags.menu_open_for_me = 0;
467 scr->window_menu->entries[0]->clientdata = NULL;
471 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
473 WApplication *wapp = wApplicationOf(wwin->main_window);
474 WScreen *scr = wwin->screen_ptr;
475 int i;
477 updateOptionsMenu(menu, wwin);
479 updateMakeShortcutMenu(menu, wwin);
481 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
483 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
485 if (wwin->flags.miniaturized) {
486 static char *text = NULL;
487 if (!text)
488 text = _("Deminiaturize");
490 menu->entries[MC_MINIATURIZE]->text = text;
491 } else {
492 static char *text = NULL;
493 if (!text)
494 text = _("Miniaturize");
496 menu->entries[MC_MINIATURIZE]->text = text;
499 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
501 if (wwin->flags.maximized) {
502 static char *text = NULL;
503 if (!text)
504 text = _("Unmaximize");
506 menu->entries[MC_MAXIMIZE]->text = text;
507 } else {
508 static char *text = NULL;
509 if (!text)
510 text = _("Maximize");
512 menu->entries[MC_MAXIMIZE]->text = text;
514 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
516 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
517 && !wwin->flags.miniaturized);
519 if (wwin->flags.shaded) {
520 static char *text = NULL;
521 if (!text)
522 text = _("Unshade");
524 menu->entries[MC_SHADE]->text = text;
525 } else {
526 static char *text = NULL;
527 if (!text)
528 text = _("Shade");
530 menu->entries[MC_SHADE]->text = text;
533 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
534 && !wwin->flags.miniaturized);
536 if (wwin->flags.selected) {
537 static char *text = NULL;
538 if (!text)
539 text = _("Deselect");
541 menu->entries[MC_SELECT]->text = text;
542 } else {
543 static char *text = NULL;
544 if (!text)
545 text = _("Select");
547 menu->entries[MC_SELECT]->text = text;
550 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
552 if (!wwin->flags.inspector_open) {
553 wMenuSetEnabled(menu, MC_PROPERTIES, True);
554 } else {
555 wMenuSetEnabled(menu, MC_PROPERTIES, False);
558 /* set the client data of the entries to the window */
559 for (i = 0; i < menu->entry_no; i++) {
560 menu->entries[i]->clientdata = wwin;
563 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
564 scr->workspace_submenu->entries[i]->clientdata = wwin;
565 if (i == scr->current_workspace) {
566 wMenuSetEnabled(scr->workspace_submenu, i, False);
567 } else {
568 wMenuSetEnabled(scr->workspace_submenu, i, True);
572 menu->flags.realized = 0;
573 wMenuRealize(menu);
576 static WMenu *open_window_menu_core(WWindow *wwin, int x, int y)
578 WScreen *scr = wwin->screen_ptr;
579 WMenu *menu;
581 wwin->flags.menu_open_for_me = 1;
583 if (!scr->window_menu) {
584 scr->window_menu = createWindowMenu(scr);
586 /* hack to save some memory allocation/deallocation */
587 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
588 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
589 wfree(scr->window_menu->entries[MC_SHADE]->text);
590 wfree(scr->window_menu->entries[MC_SELECT]->text);
591 } else {
592 updateWorkspaceMenu(scr->workspace_submenu);
595 menu = scr->window_menu;
596 if (menu->flags.mapped) {
597 wMenuUnmap(menu);
598 if (menu->entries[0]->clientdata == wwin)
599 return NULL;
602 updateMenuForWindow(menu, wwin);
604 return menu;
607 static void prepare_menu_position(WMenu *menu, int x, int y)
609 WMRect rect;
611 rect = wGetRectForHead(menu->frame->screen_ptr,
612 wGetHeadForPointerLocation(menu->frame->screen_ptr));
613 if (x < rect.pos.x - menu->frame->core->width / 2)
614 x = rect.pos.x - menu->frame->core->width / 2;
615 if (y < rect.pos.y)
616 y = rect.pos.y;
619 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
621 WMenu *menu;
623 menu = open_window_menu_core(wwin, x, y);
624 if (!menu)
625 return;
627 /* Specific menu position */
628 x -= menu->frame->core->width / 2;
629 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
630 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
631 if (x < wwin->frame_x)
632 x = wwin->frame_x;
634 /* Common menu position */
635 prepare_menu_position(menu, x, y);
637 if (!wwin->flags.internal_window)
638 wMenuMapAt(menu, x, y, keyboard);
641 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
643 int i;
644 WMenu *menu;
645 WScreen *scr = wwin->screen_ptr;
647 menu = open_window_menu_core(wwin, x, y);
648 if (!menu)
649 return;
651 /* Specific menu position */
652 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
653 scr->workspace_submenu->entries[i]->clientdata = wwin;
654 wMenuSetEnabled(scr->workspace_submenu, i, True);
657 x -= menu->frame->core->width / 2;
659 /* Common menu position */
660 prepare_menu_position(menu, x, y);
662 if (!wwin->flags.internal_window)
663 wMenuMapAt(menu, x, y, keyboard);
666 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
668 WMenu *menu;
670 menu = open_window_menu_core(wwin, x, y);
671 if (!menu)
672 return;
674 x -= menu->frame->core->width / 2;
676 wMenuMapAt(menu, x, y, False);
679 void DestroyWindowMenu(WScreen *scr)
681 if (scr->window_menu) {
682 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
683 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
684 scr->window_menu->entries[MC_SHADE]->text = NULL;
685 scr->window_menu->entries[MC_SELECT]->text = NULL;
686 wMenuDestroy(scr->window_menu, True);
687 scr->window_menu = NULL;