fixed bug in positioning of window list menu when opened by kbd
[wmaker-crm.git] / src / winmenu.c
blob5452a35622661f7e0008d72e0d0cd090d9d26a63
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 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
102 break;
107 static void
108 execMenuCommand(WMenu *menu, WMenuEntry *entry)
110 WWindow *wwin = (WWindow*)entry->clientdata;
111 WApplication *wapp;
113 CloseWindowMenu(menu->frame->screen_ptr);
115 switch (entry->order) {
116 case MC_CLOSE:
117 /* send delete message */
118 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
119 break;
121 case MC_KILL:
122 wretain(wwin);
123 if (wPreferences.dont_confirm_kill
124 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
125 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
126 _("Yes"), _("No"), NULL)==WAPRDefault) {
127 if (!wwin->flags.destroyed)
128 wClientKill(wwin);
130 wrelease(wwin);
131 break;
133 case MC_MINIATURIZE:
134 if (wwin->flags.miniaturized) {
135 wDeiconifyWindow(wwin);
136 } else{
137 if (wwin->protocols.MINIATURIZE_WINDOW) {
138 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
139 LastTimestamp);
140 } else {
141 wIconifyWindow(wwin);
144 break;
146 case MC_MAXIMIZE:
147 if (wwin->flags.maximized)
148 wUnmaximizeWindow(wwin);
149 else
150 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
151 break;
153 case MC_SHADE:
154 if (wwin->flags.shaded)
155 wUnshadeWindow(wwin);
156 else
157 wShadeWindow(wwin);
158 break;
160 case MC_SELECT:
161 if (!wwin->flags.miniaturized)
162 wSelectWindow(wwin, !wwin->flags.selected);
163 else
164 wIconSelect(wwin->icon);
165 break;
167 case MC_MOVERESIZE:
168 wKeyboardMoveResizeWindow(wwin);
169 break;
171 case MC_PROPERTIES:
172 wShowInspectorForWindow(wwin);
173 break;
175 case MC_HIDE:
176 wapp = wApplicationOf(wwin->main_window);
177 wHideApplication(wapp);
178 break;
184 static void
185 switchWSCommand(WMenu *menu, WMenuEntry *entry)
187 WWindow *wwin = (WWindow*)entry->clientdata;
189 wSelectWindow(wwin, False);
190 wWindowChangeWorkspace(wwin, entry->order);
194 static void
195 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
197 WWindow *wwin = (WWindow*)entry->clientdata;
198 WScreen *scr = wwin->screen_ptr;
199 int index = entry->order-WO_ENTRIES;
201 if (scr->shortcutWindows[index]) {
202 WMFreeArray(scr->shortcutWindows[index]);
203 scr->shortcutWindows[index] = NULL;
206 if (wwin->flags.selected && scr->selected_windows) {
207 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
208 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
209 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
210 } else {
211 scr->shortcutWindows[index] = WMCreateArray(4);
212 WMAddToArray(scr->shortcutWindows[index], wwin);
215 wSelectWindow(wwin, !wwin->flags.selected);
216 XFlush(dpy);
217 wusleep(3000);
218 wSelectWindow(wwin, !wwin->flags.selected);
219 XFlush(dpy);
223 static void
224 updateWorkspaceMenu(WMenu *menu)
226 WScreen *scr = menu->frame->screen_ptr;
227 char title[MAX_WORKSPACENAME_WIDTH+1];
228 int i;
230 if (!menu)
231 return;
233 for (i=0; i<scr->workspace_count; i++) {
234 if (i < menu->entry_no) {
235 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
236 wfree(menu->entries[i]->text);
237 strcpy(title, scr->workspaces[i]->name);
238 menu->entries[i]->text = wstrdup(title);
239 menu->flags.realized = 0;
241 } else {
242 strcpy(title, scr->workspaces[i]->name);
244 wMenuAddCallback(menu, title, switchWSCommand, NULL);
246 menu->flags.realized = 0;
250 if (!menu->flags.realized)
251 wMenuRealize(menu);
255 static void
256 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
258 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
259 int i;
260 char *buffer;
261 int buflen;
262 KeyCode kcode;
264 if (!smenu)
265 return;
267 buflen = strlen(_("Set Shortcut"))+16;
268 buffer = wmalloc(buflen);
270 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
271 char *tmp;
272 int shortcutNo = i-WO_ENTRIES;
273 WMenuEntry *entry = smenu->entries[i];
274 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
276 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo+1);
278 if (!shortSelWindows) {
279 entry->flags.indicator_on = 0;
280 } else {
281 entry->flags.indicator_on = 1;
282 if (WMCountInArray(shortSelWindows, wwin))
283 entry->flags.indicator_type = MI_DIAMOND;
284 else
285 entry->flags.indicator_type = MI_CHECK;
288 if (strcmp(buffer, entry->text)!=0) {
289 wfree(entry->text);
290 entry->text = wstrdup(buffer);
291 smenu->flags.realized = 0;
294 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
296 if (kcode) {
297 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
298 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
299 if (entry->rtext)
300 wfree(entry->rtext);
301 entry->rtext = wstrdup(tmp);
302 smenu->flags.realized = 0;
304 wMenuSetEnabled(smenu, i, True);
305 } else {
306 wMenuSetEnabled(smenu, i, False);
307 if (entry->rtext) {
308 wfree(entry->rtext);
309 entry->rtext = NULL;
310 smenu->flags.realized = 0;
313 entry->clientdata = wwin;
315 wfree(buffer);
316 if (!smenu->flags.realized)
317 wMenuRealize(smenu);
321 static void
322 updateOptionsMenu(WMenu *menu, WWindow *wwin)
324 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
326 /* keep on top check */
327 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
328 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
329 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
330 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
332 /* keep at bottom check */
333 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
334 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
335 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
336 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
338 /* omnipresent check */
339 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
340 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
342 smenu->flags.realized = 0;
343 wMenuRealize(smenu);
347 static WMenu*
348 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;
364 static WMenu*
365 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
368 WMenu *menu;
370 int i;
372 menu = wMenuCreate(scr, NULL, False);
373 if (!menu) {
374 wwarning(_("could not create submenu for window menu"));
375 return NULL;
379 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
380 WMenuEntry *entry;
381 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
383 entry->flags.indicator = 1;
386 return menu;
391 static WMenu*
392 makeOptionsMenu(WScreen *scr)
394 WMenu *menu;
395 WMenuEntry *entry;
397 menu = wMenuCreate(scr, NULL, False);
398 if (!menu) {
399 wwarning(_("could not create submenu for window menu"));
400 return NULL;
403 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
404 NULL);
405 entry->flags.indicator = 1;
406 entry->flags.indicator_type = MI_CHECK;
408 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
409 NULL);
410 entry->flags.indicator = 1;
411 entry->flags.indicator_type = MI_CHECK;
413 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
414 NULL);
415 entry->flags.indicator = 1;
416 entry->flags.indicator_type = MI_CHECK;
418 return menu;
422 static WMenu*
423 createWindowMenu(WScreen *scr)
425 WMenu *menu;
426 KeyCode kcode;
427 WMenuEntry *entry;
428 char *tmp;
430 menu = wMenuCreate(scr, NULL, False);
432 * Warning: If you make some change that affects the order of the
433 * entries, you must update the command #defines in the top of
434 * this file.
436 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
437 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
438 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
440 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
441 entry->rtext = wstrdup(tmp);
444 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
446 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
447 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
449 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
450 entry->rtext = wstrdup(tmp);
453 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
454 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
455 kcode = wKeyBindings[WKBD_SHADE].keycode;
457 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
458 entry->rtext = wstrdup(tmp);
461 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
462 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
463 kcode = wKeyBindings[WKBD_HIDE].keycode;
465 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
466 entry->rtext = wstrdup(tmp);
469 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
470 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
471 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
473 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
474 entry->rtext = wstrdup(tmp);
477 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
478 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
479 kcode = wKeyBindings[WKBD_SELECT].keycode;
481 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
482 entry->rtext = wstrdup(tmp);
485 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
486 scr->workspace_submenu = makeWorkspaceMenu(scr);
487 if (scr->workspace_submenu)
488 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
490 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
492 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
493 wMenuEntrySetCascade(menu, entry,
494 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
497 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
498 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
501 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
502 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
503 kcode = wKeyBindings[WKBD_CLOSE].keycode;
504 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
505 entry->rtext = wstrdup(tmp);
508 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
510 return menu;
514 void
515 CloseWindowMenu(WScreen *scr)
517 if (scr->window_menu) {
518 if (scr->window_menu->flags.mapped)
519 wMenuUnmap(scr->window_menu);
521 if (scr->window_menu->entries[0]->clientdata) {
522 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
524 wwin->flags.menu_open_for_me = 0;
526 scr->window_menu->entries[0]->clientdata = NULL;
532 static void
533 updateMenuForWindow(WMenu *menu, WWindow *wwin)
535 WApplication *wapp = wApplicationOf(wwin->main_window);
536 WScreen *scr = wwin->screen_ptr;
537 int i;
539 updateOptionsMenu(menu, wwin);
541 updateMakeShortcutMenu(menu, wwin);
543 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
544 && !WFLAGP(wapp->main_window_desc, no_appicon));
546 wMenuSetEnabled(menu, MC_CLOSE,
547 (wwin->protocols.DELETE_WINDOW
548 && !WFLAGP(wwin, no_closable)));
550 if (wwin->flags.miniaturized) {
551 static char *text = NULL;
552 if (!text) text = _("Deminiaturize");
554 menu->entries[MC_MINIATURIZE]->text = text;
555 } else {
556 static char *text = NULL;
557 if (!text) text = _("Miniaturize");
559 menu->entries[MC_MINIATURIZE]->text = text;
562 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
564 if (wwin->flags.maximized) {
565 static char *text = NULL;
566 if (!text) text = _("Unmaximize");
568 menu->entries[MC_MAXIMIZE]->text = text;
569 } else {
570 static char *text = NULL;
571 if (!text) text = _("Maximize");
573 menu->entries[MC_MAXIMIZE]->text = text;
575 wMenuSetEnabled(menu, MC_MAXIMIZE, !WFLAGP(wwin, no_resizable));
578 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable)
579 && !wwin->flags.miniaturized);
581 if (wwin->flags.shaded) {
582 static char *text = NULL;
583 if (!text) text = _("Unshade");
585 menu->entries[MC_SHADE]->text = text;
586 } else {
587 static char *text = NULL;
588 if (!text) text = _("Shade");
590 menu->entries[MC_SHADE]->text = text;
593 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
594 && !wwin->flags.miniaturized);
596 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
598 if (!wwin->flags.inspector_open) {
599 wMenuSetEnabled(menu, MC_PROPERTIES, True);
600 } else {
601 wMenuSetEnabled(menu, MC_PROPERTIES, False);
604 /* set the client data of the entries to the window */
605 for (i = 0; i < menu->entry_no; i++) {
606 menu->entries[i]->clientdata = wwin;
609 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
610 scr->workspace_submenu->entries[i]->clientdata = wwin;
611 if (i == scr->current_workspace) {
612 wMenuSetEnabled(scr->workspace_submenu, i, False);
613 } else {
614 wMenuSetEnabled(scr->workspace_submenu, i, True);
618 menu->flags.realized = 0;
619 wMenuRealize(menu);
623 void
624 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
626 WMenu *menu;
627 WScreen *scr = wwin->screen_ptr;
629 wwin->flags.menu_open_for_me = 1;
631 if (!scr->window_menu) {
632 scr->window_menu = createWindowMenu(scr);
634 /* hack to save some memory allocation/deallocation */
635 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
636 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
637 wfree(scr->window_menu->entries[MC_SHADE]->text);
638 } else {
639 updateWorkspaceMenu(scr->workspace_submenu);
642 menu = scr->window_menu;
643 if (menu->flags.mapped) {
644 wMenuUnmap(menu);
645 if (menu->entries[0]->clientdata==wwin) {
646 return;
650 updateMenuForWindow(menu, wwin);
652 x -= menu->frame->core->width/2;
653 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
654 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
655 if (x < wwin->frame_x)
656 x = wwin->frame_x;
658 if (!wwin->flags.internal_window)
659 wMenuMapAt(menu, x, y, keyboard);
663 void
664 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
666 WMenu *menu;
667 WScreen *scr = wwin->screen_ptr;
669 wwin->flags.menu_open_for_me = 1;
671 if (!scr->window_menu) {
672 scr->window_menu = createWindowMenu(scr);
674 /* hack to save some memory allocation/deallocation */
675 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
676 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
677 wfree(scr->window_menu->entries[MC_SHADE]->text);
678 } else {
679 updateWorkspaceMenu(scr->workspace_submenu);
682 menu = scr->window_menu;
683 if (menu->flags.mapped) {
684 wMenuUnmap(menu);
685 if (menu->entries[0]->clientdata==wwin) {
686 return;
690 updateMenuForWindow(menu, wwin);
692 x -= menu->frame->core->width/2;
694 wMenuMapAt(menu, x, y, False);