fixed directory OPEN_MENU sorting bug and shortcut selection window problems
[wmaker-crm.git] / src / winmenu.c
blob65ffd728dd9629858ba4227987ef7d779067aaed
1 /* winmenu.c - command menu for windows
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 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.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
34 #include "actions.h"
35 #include "menu.h"
36 #include "funcs.h"
37 #include "window.h"
38 #include "client.h"
39 #include "application.h"
40 #include "keybind.h"
41 #include "framewin.h"
42 #include "workspace.h"
43 #include "winspector.h"
44 #include "dialog.h"
45 #include "stacking.h"
46 #include "icon.h"
49 #define MC_MAXIMIZE 0
50 #define MC_MINIATURIZE 1
51 #define MC_SHADE 2
52 #define MC_HIDE 3
53 #define MC_MOVERESIZE 4
54 #define MC_SELECT 5
55 #define MC_DUMMY_MOVETO 6
56 #define MC_PROPERTIES 7
57 #define MC_OPTIONS 8
58 #define MC_SHORTCUT 8
60 #define MC_CLOSE 9
61 #define MC_KILL 10
64 #define WO_KEEP_ON_TOP 0
65 #define WO_KEEP_AT_BOTTOM 1
66 #define WO_OMNIPRESENT 2
67 #define WO_ENTRIES 3
69 /**** Global data ***/
70 extern Time LastTimestamp;
71 extern Atom _XA_WM_DELETE_WINDOW;
72 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
74 extern WShortKey wKeyBindings[WKBD_LAST];
76 extern WPreferences wPreferences;
78 static void updateOptionsMenu(WMenu *menu, WWindow *wwin);
80 static void
81 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 wwin->flags.omnipresent^=1;
102 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_WORKSPACE);
103 break;
108 static void
109 execMenuCommand(WMenu *menu, WMenuEntry *entry)
111 WWindow *wwin = (WWindow*)entry->clientdata;
112 WApplication *wapp;
114 CloseWindowMenu(menu->frame->screen_ptr);
116 switch (entry->order) {
117 case MC_CLOSE:
118 /* send delete message */
119 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
120 break;
122 case MC_KILL:
123 wretain(wwin);
124 if (wPreferences.dont_confirm_kill
125 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
126 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
127 _("Yes"), _("No"), NULL)==WAPRDefault) {
128 if (!wwin->flags.destroyed)
129 wClientKill(wwin);
131 wrelease(wwin);
132 break;
134 case MC_MINIATURIZE:
135 if (wwin->flags.miniaturized) {
136 wDeiconifyWindow(wwin);
137 } else{
138 if (wwin->protocols.MINIATURIZE_WINDOW) {
139 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
140 LastTimestamp);
141 } else {
142 wIconifyWindow(wwin);
145 break;
147 case MC_MAXIMIZE:
148 if (wwin->flags.maximized)
149 wUnmaximizeWindow(wwin);
150 else
151 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
152 break;
154 case MC_SHADE:
155 if (wwin->flags.shaded)
156 wUnshadeWindow(wwin);
157 else
158 wShadeWindow(wwin);
159 break;
161 case MC_SELECT:
162 if (!wwin->flags.miniaturized)
163 wSelectWindow(wwin, !wwin->flags.selected);
164 else
165 wIconSelect(wwin->icon);
166 break;
168 case MC_MOVERESIZE:
169 wKeyboardMoveResizeWindow(wwin);
170 break;
172 case MC_PROPERTIES:
173 wShowInspectorForWindow(wwin);
174 break;
176 case MC_HIDE:
177 wapp = wApplicationOf(wwin->main_window);
178 wHideApplication(wapp);
179 break;
185 static void
186 switchWSCommand(WMenu *menu, WMenuEntry *entry)
188 WWindow *wwin = (WWindow*)entry->clientdata;
190 wSelectWindow(wwin, False);
191 wWindowChangeWorkspace(wwin, entry->order);
195 static void
196 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
198 WWindow *wwin = (WWindow*)entry->clientdata;
199 WScreen *scr = wwin->screen_ptr;
200 int index = entry->order-WO_ENTRIES;
202 if (scr->shortcutWindows[index]) {
203 WMFreeBag(scr->shortcutWindows[index]);
204 scr->shortcutWindows[index] = NULL;
207 if (wwin->flags.selected && scr->selected_windows) {
208 WMBag *selwin = scr->selected_windows;
209 int i;
211 scr->shortcutWindows[index] = WMCreateBag(4);
213 for (i = 0; i < WMGetBagItemCount(selwin); i++) {
214 WWindow *tmp = WMGetFromBag(selwin, i);
216 WMPutInBag(scr->shortcutWindows[index], tmp);
220 wSelectWindow(wwin, !wwin->flags.selected);
221 XFlush(dpy);
222 wusleep(3000);
223 wSelectWindow(wwin, !wwin->flags.selected);
224 XFlush(dpy);
228 static void
229 updateWorkspaceMenu(WMenu *menu)
231 WScreen *scr = menu->frame->screen_ptr;
232 char title[MAX_WORKSPACENAME_WIDTH+1];
233 int i;
235 if (!menu)
236 return;
238 for (i=0; i<scr->workspace_count; i++) {
239 if (i < menu->entry_no) {
240 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
241 free(menu->entries[i]->text);
242 strcpy(title, scr->workspaces[i]->name);
243 menu->entries[i]->text = wstrdup(title);
244 menu->flags.realized = 0;
246 } else {
247 strcpy(title, scr->workspaces[i]->name);
249 wMenuAddCallback(menu, title, switchWSCommand, NULL);
251 menu->flags.realized = 0;
255 if (!menu->flags.realized)
256 wMenuRealize(menu);
260 static void
261 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
263 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
264 int i;
265 char *buffer;
266 KeyCode kcode;
268 if (!smenu)
269 return;
271 buffer = wmalloc(strlen(_("Set Shortcut"))+16);
273 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
274 char *tmp;
275 int shortcutNo = i-WO_ENTRIES;
276 WMenuEntry *entry = smenu->entries[i];
277 WMBag *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
279 sprintf(buffer, "%s %i", _("Set Shortcut"), shortcutNo+1);
281 if (!shortSelWindows) {
282 entry->flags.indicator_on = 0;
283 } else {
284 entry->flags.indicator_on = 1;
285 if (WMCountInBag(shortSelWindows, wwin))
286 entry->flags.indicator_type = MI_DIAMOND;
287 else
288 entry->flags.indicator_type = MI_CHECK;
291 if (strcmp(buffer, entry->text)!=0) {
292 free(entry->text);
293 entry->text = wstrdup(buffer);
294 smenu->flags.realized = 0;
297 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
299 if (kcode) {
300 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
301 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
302 if (entry->rtext)
303 free(entry->rtext);
304 entry->rtext = wstrdup(tmp);
305 smenu->flags.realized = 0;
307 wMenuSetEnabled(smenu, i, True);
308 } else {
309 wMenuSetEnabled(smenu, i, False);
310 if (entry->rtext) {
311 free(entry->rtext);
312 entry->rtext = NULL;
313 smenu->flags.realized = 0;
316 entry->clientdata = wwin;
318 free(buffer);
319 if (!smenu->flags.realized)
320 wMenuRealize(smenu);
324 static void
325 updateOptionsMenu(WMenu *menu, WWindow *wwin)
327 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
329 /* keep on top check */
330 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
331 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
332 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
333 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
335 /* keep at bottom check */
336 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
337 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
338 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
339 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
341 /* omnipresent check */
342 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
343 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
345 smenu->flags.realized = 0;
346 wMenuRealize(smenu);
350 static WMenu*
351 makeWorkspaceMenu(WScreen *scr)
353 WMenu *menu;
355 menu = wMenuCreate(scr, NULL, False);
356 if (!menu) {
357 wwarning(_("could not create submenu for window menu"));
358 return NULL;
361 updateWorkspaceMenu(menu);
363 return menu;
367 static WMenu*
368 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
371 WMenu *menu;
373 int i;
375 menu = wMenuCreate(scr, NULL, False);
376 if (!menu) {
377 wwarning(_("could not create submenu for window menu"));
378 return NULL;
382 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
383 WMenuEntry *entry;
385 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
387 entry->flags.indicator = 1;
390 return menu;
395 static WMenu*
396 makeOptionsMenu(WScreen *scr)
398 WMenu *menu;
399 WMenuEntry *entry;
401 menu = wMenuCreate(scr, NULL, False);
402 if (!menu) {
403 wwarning(_("could not create submenu for window menu"));
404 return NULL;
407 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
408 NULL);
409 entry->flags.indicator = 1;
410 entry->flags.indicator_type = MI_CHECK;
412 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
413 NULL);
414 entry->flags.indicator = 1;
415 entry->flags.indicator_type = MI_CHECK;
417 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
418 NULL);
419 entry->flags.indicator = 1;
420 entry->flags.indicator_type = MI_CHECK;
422 return menu;
426 static WMenu*
427 createWindowMenu(WScreen *scr)
429 WMenu *menu;
430 KeyCode kcode;
431 WMenuEntry *entry;
432 char *tmp;
434 menu = wMenuCreate(scr, NULL, False);
436 * Warning: If you make some change that affects the order of the
437 * entries, you must update the command #defines in the top of
438 * this file.
440 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
441 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
442 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
444 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
445 entry->rtext = wstrdup(tmp);
448 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
450 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
451 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
453 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
454 entry->rtext = wstrdup(tmp);
457 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
458 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
459 kcode = wKeyBindings[WKBD_SHADE].keycode;
461 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
462 entry->rtext = wstrdup(tmp);
465 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
466 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
467 kcode = wKeyBindings[WKBD_HIDE].keycode;
469 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
470 entry->rtext = wstrdup(tmp);
473 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
474 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
475 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
477 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
478 entry->rtext = wstrdup(tmp);
481 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
482 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
483 kcode = wKeyBindings[WKBD_SELECT].keycode;
485 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
486 entry->rtext = wstrdup(tmp);
489 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
490 scr->workspace_submenu = makeWorkspaceMenu(scr);
491 if (scr->workspace_submenu)
492 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
494 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
496 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
497 wMenuEntrySetCascade(menu, entry,
498 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
501 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
502 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
505 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
506 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
507 kcode = wKeyBindings[WKBD_CLOSE].keycode;
508 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
509 entry->rtext = wstrdup(tmp);
512 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
514 return menu;
518 void
519 CloseWindowMenu(WScreen *scr)
521 if (scr->window_menu) {
522 if (scr->window_menu->flags.mapped)
523 wMenuUnmap(scr->window_menu);
525 if (scr->window_menu->entries[0]->clientdata) {
526 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
528 wwin->flags.menu_open_for_me = 0;
530 scr->window_menu->entries[0]->clientdata = NULL;
536 static void
537 updateMenuForWindow(WMenu *menu, WWindow *wwin)
539 WApplication *wapp = wApplicationOf(wwin->main_window);
540 WScreen *scr = wwin->screen_ptr;
541 int i;
543 updateOptionsMenu(menu, wwin);
545 updateMakeShortcutMenu(menu, wwin);
547 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
548 && !WFLAGP(wapp->main_window_desc, no_appicon));
550 wMenuSetEnabled(menu, MC_CLOSE,
551 (wwin->protocols.DELETE_WINDOW
552 && !WFLAGP(wwin, no_closable)));
554 if (wwin->flags.miniaturized) {
555 static char *text = NULL;
556 if (!text) text = _("Deminiaturize");
558 menu->entries[MC_MINIATURIZE]->text = text;
559 } else {
560 static char *text = NULL;
561 if (!text) text = _("Miniaturize");
563 menu->entries[MC_MINIATURIZE]->text = text;
566 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
568 if (wwin->flags.maximized) {
569 static char *text = NULL;
570 if (!text) text = _("Unmaximize");
572 menu->entries[MC_MAXIMIZE]->text = text;
573 } else {
574 static char *text = NULL;
575 if (!text) text = _("Maximize");
577 menu->entries[MC_MAXIMIZE]->text = text;
579 wMenuSetEnabled(menu, MC_MAXIMIZE, !WFLAGP(wwin, no_resizable));
582 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable)
583 && !wwin->flags.miniaturized);
585 if (wwin->flags.shaded) {
586 static char *text = NULL;
587 if (!text) text = _("Unshade");
589 menu->entries[MC_SHADE]->text = text;
590 } else {
591 static char *text = NULL;
592 if (!text) text = _("Shade");
594 menu->entries[MC_SHADE]->text = text;
597 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
598 && !wwin->flags.miniaturized);
600 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
602 if (!wwin->flags.inspector_open) {
603 wMenuSetEnabled(menu, MC_PROPERTIES, True);
604 } else {
605 wMenuSetEnabled(menu, MC_PROPERTIES, False);
608 /* set the client data of the entries to the window */
609 for (i = 0; i < menu->entry_no; i++) {
610 menu->entries[i]->clientdata = wwin;
613 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
614 scr->workspace_submenu->entries[i]->clientdata = wwin;
615 if (i == scr->current_workspace) {
616 wMenuSetEnabled(scr->workspace_submenu, i, False);
617 } else {
618 wMenuSetEnabled(scr->workspace_submenu, i, True);
622 menu->flags.realized = 0;
623 wMenuRealize(menu);
627 void
628 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
630 WMenu *menu;
631 WScreen *scr = wwin->screen_ptr;
633 wwin->flags.menu_open_for_me = 1;
635 if (!scr->window_menu) {
636 scr->window_menu = createWindowMenu(scr);
638 /* hack to save some memory allocation/deallocation */
639 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
640 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
641 free(scr->window_menu->entries[MC_SHADE]->text);
642 } else {
643 updateWorkspaceMenu(scr->workspace_submenu);
646 menu = scr->window_menu;
647 if (menu->flags.mapped) {
648 wMenuUnmap(menu);
649 if (menu->entries[0]->clientdata==wwin) {
650 return;
654 updateMenuForWindow(menu, wwin);
656 x -= menu->frame->core->width/2;
657 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
658 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
659 if (x < wwin->frame_x)
660 x = wwin->frame_x;
662 if (!wwin->flags.internal_window)
663 wMenuMapAt(menu, x, y, keyboard);
667 void
668 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
670 WMenu *menu;
671 WScreen *scr = wwin->screen_ptr;
673 wwin->flags.menu_open_for_me = 1;
675 if (!scr->window_menu) {
676 scr->window_menu = createWindowMenu(scr);
678 /* hack to save some memory allocation/deallocation */
679 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
680 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
681 free(scr->window_menu->entries[MC_SHADE]->text);
682 } else {
683 updateWorkspaceMenu(scr->workspace_submenu);
686 menu = scr->window_menu;
687 if (menu->flags.mapped) {
688 wMenuUnmap(menu);
689 if (menu->entries[0]->clientdata==wwin) {
690 return;
694 updateMenuForWindow(menu, wwin);
696 x -= menu->frame->core->width/2;
698 wMenuMapAt(menu, x, y, False);