Fixed calls to hermes lib to work correctly with all depths
[wmaker-crm.git] / src / winmenu.c
blob41b66ab6f3530abc382f3aa69d95574b1467784a
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 #ifdef GNOME_STUFF
104 wGNOMEUpdateClientStateHint(wwin, False);
105 #endif
106 break;
111 static void
112 execMenuCommand(WMenu *menu, WMenuEntry *entry)
114 WWindow *wwin = (WWindow*)entry->clientdata;
115 WApplication *wapp;
117 CloseWindowMenu(menu->frame->screen_ptr);
119 switch (entry->order) {
120 case MC_CLOSE:
121 /* send delete message */
122 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
123 break;
125 case MC_KILL:
126 wretain(wwin);
127 if (wPreferences.dont_confirm_kill
128 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
129 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
130 _("Yes"), _("No"), NULL)==WAPRDefault) {
131 if (!wwin->flags.destroyed)
132 wClientKill(wwin);
134 wrelease(wwin);
135 break;
137 case MC_MINIATURIZE:
138 if (wwin->flags.miniaturized) {
139 wDeiconifyWindow(wwin);
140 } else{
141 if (wwin->protocols.MINIATURIZE_WINDOW) {
142 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
143 LastTimestamp);
144 } else {
145 wIconifyWindow(wwin);
148 break;
150 case MC_MAXIMIZE:
151 if (wwin->flags.maximized)
152 wUnmaximizeWindow(wwin);
153 else
154 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
155 break;
157 case MC_SHADE:
158 if (wwin->flags.shaded)
159 wUnshadeWindow(wwin);
160 else
161 wShadeWindow(wwin);
162 break;
164 case MC_SELECT:
165 if (!wwin->flags.miniaturized)
166 wSelectWindow(wwin, !wwin->flags.selected);
167 else
168 wIconSelect(wwin->icon);
169 break;
171 case MC_MOVERESIZE:
172 wKeyboardMoveResizeWindow(wwin);
173 break;
175 case MC_PROPERTIES:
176 wShowInspectorForWindow(wwin);
177 break;
179 case MC_HIDE:
180 wapp = wApplicationOf(wwin->main_window);
181 wHideApplication(wapp);
182 break;
188 static void
189 switchWSCommand(WMenu *menu, WMenuEntry *entry)
191 WWindow *wwin = (WWindow*)entry->clientdata;
193 wSelectWindow(wwin, False);
194 wWindowChangeWorkspace(wwin, entry->order);
198 static void
199 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
201 WWindow *wwin = (WWindow*)entry->clientdata;
202 WScreen *scr = wwin->screen_ptr;
203 int index = entry->order-WO_ENTRIES;
205 if (scr->shortcutWindows[index]) {
206 WMFreeArray(scr->shortcutWindows[index]);
207 scr->shortcutWindows[index] = NULL;
210 if (wwin->flags.selected && scr->selected_windows) {
211 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
212 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
213 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
214 } else {
215 scr->shortcutWindows[index] = WMCreateArray(4);
216 WMAddToArray(scr->shortcutWindows[index], wwin);
219 wSelectWindow(wwin, !wwin->flags.selected);
220 XFlush(dpy);
221 wusleep(3000);
222 wSelectWindow(wwin, !wwin->flags.selected);
223 XFlush(dpy);
227 static void
228 updateWorkspaceMenu(WMenu *menu)
230 WScreen *scr = menu->frame->screen_ptr;
231 char title[MAX_WORKSPACENAME_WIDTH+1];
232 int i;
234 if (!menu)
235 return;
237 for (i=0; i<scr->workspace_count; i++) {
238 if (i < menu->entry_no) {
239 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
240 wfree(menu->entries[i]->text);
241 strcpy(title, scr->workspaces[i]->name);
242 menu->entries[i]->text = wstrdup(title);
243 menu->flags.realized = 0;
245 } else {
246 strcpy(title, scr->workspaces[i]->name);
248 wMenuAddCallback(menu, title, switchWSCommand, NULL);
250 menu->flags.realized = 0;
254 if (!menu->flags.realized)
255 wMenuRealize(menu);
259 static void
260 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
262 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
263 int i;
264 char *buffer;
265 KeyCode kcode;
267 if (!smenu)
268 return;
270 buffer = wmalloc(strlen(_("Set Shortcut"))+16);
272 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
273 char *tmp;
274 int shortcutNo = i-WO_ENTRIES;
275 WMenuEntry *entry = smenu->entries[i];
276 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
278 sprintf(buffer, "%s %i", _("Set Shortcut"), shortcutNo+1);
280 if (!shortSelWindows) {
281 entry->flags.indicator_on = 0;
282 } else {
283 entry->flags.indicator_on = 1;
284 if (WMCountInArray(shortSelWindows, wwin))
285 entry->flags.indicator_type = MI_DIAMOND;
286 else
287 entry->flags.indicator_type = MI_CHECK;
290 if (strcmp(buffer, entry->text)!=0) {
291 wfree(entry->text);
292 entry->text = wstrdup(buffer);
293 smenu->flags.realized = 0;
296 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
298 if (kcode) {
299 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
300 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
301 if (entry->rtext)
302 wfree(entry->rtext);
303 entry->rtext = wstrdup(tmp);
304 smenu->flags.realized = 0;
306 wMenuSetEnabled(smenu, i, True);
307 } else {
308 wMenuSetEnabled(smenu, i, False);
309 if (entry->rtext) {
310 wfree(entry->rtext);
311 entry->rtext = NULL;
312 smenu->flags.realized = 0;
315 entry->clientdata = wwin;
317 wfree(buffer);
318 if (!smenu->flags.realized)
319 wMenuRealize(smenu);
323 static void
324 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);
349 static WMenu*
350 makeWorkspaceMenu(WScreen *scr)
352 WMenu *menu;
354 menu = wMenuCreate(scr, NULL, False);
355 if (!menu) {
356 wwarning(_("could not create submenu for window menu"));
357 return NULL;
360 updateWorkspaceMenu(menu);
362 return menu;
366 static WMenu*
367 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
370 WMenu *menu;
372 int i;
374 menu = wMenuCreate(scr, NULL, False);
375 if (!menu) {
376 wwarning(_("could not create submenu for window menu"));
377 return NULL;
381 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
382 WMenuEntry *entry;
383 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
385 entry->flags.indicator = 1;
388 return menu;
393 static WMenu*
394 makeOptionsMenu(WScreen *scr)
396 WMenu *menu;
397 WMenuEntry *entry;
399 menu = wMenuCreate(scr, NULL, False);
400 if (!menu) {
401 wwarning(_("could not create submenu for window menu"));
402 return NULL;
405 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
406 NULL);
407 entry->flags.indicator = 1;
408 entry->flags.indicator_type = MI_CHECK;
410 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
411 NULL);
412 entry->flags.indicator = 1;
413 entry->flags.indicator_type = MI_CHECK;
415 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
416 NULL);
417 entry->flags.indicator = 1;
418 entry->flags.indicator_type = MI_CHECK;
420 return menu;
424 static WMenu*
425 createWindowMenu(WScreen *scr)
427 WMenu *menu;
428 KeyCode kcode;
429 WMenuEntry *entry;
430 char *tmp;
432 menu = wMenuCreate(scr, NULL, False);
434 * Warning: If you make some change that affects the order of the
435 * entries, you must update the command #defines in the top of
436 * this file.
438 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
439 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
440 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
442 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
443 entry->rtext = wstrdup(tmp);
446 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
448 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
449 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
451 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
452 entry->rtext = wstrdup(tmp);
455 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
456 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
457 kcode = wKeyBindings[WKBD_SHADE].keycode;
459 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
460 entry->rtext = wstrdup(tmp);
463 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
464 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
465 kcode = wKeyBindings[WKBD_HIDE].keycode;
467 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
468 entry->rtext = wstrdup(tmp);
471 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
472 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
473 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
475 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
476 entry->rtext = wstrdup(tmp);
479 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
480 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
481 kcode = wKeyBindings[WKBD_SELECT].keycode;
483 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
484 entry->rtext = wstrdup(tmp);
487 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
488 scr->workspace_submenu = makeWorkspaceMenu(scr);
489 if (scr->workspace_submenu)
490 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
492 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
494 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
495 wMenuEntrySetCascade(menu, entry,
496 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
499 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
500 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
503 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
504 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
505 kcode = wKeyBindings[WKBD_CLOSE].keycode;
506 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
507 entry->rtext = wstrdup(tmp);
510 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
512 return menu;
516 void
517 CloseWindowMenu(WScreen *scr)
519 if (scr->window_menu) {
520 if (scr->window_menu->flags.mapped)
521 wMenuUnmap(scr->window_menu);
523 if (scr->window_menu->entries[0]->clientdata) {
524 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
526 wwin->flags.menu_open_for_me = 0;
528 scr->window_menu->entries[0]->clientdata = NULL;
534 static void
535 updateMenuForWindow(WMenu *menu, WWindow *wwin)
537 WApplication *wapp = wApplicationOf(wwin->main_window);
538 WScreen *scr = wwin->screen_ptr;
539 int i;
541 updateOptionsMenu(menu, wwin);
543 updateMakeShortcutMenu(menu, wwin);
545 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
546 && !WFLAGP(wapp->main_window_desc, no_appicon));
548 wMenuSetEnabled(menu, MC_CLOSE,
549 (wwin->protocols.DELETE_WINDOW
550 && !WFLAGP(wwin, no_closable)));
552 if (wwin->flags.miniaturized) {
553 static char *text = NULL;
554 if (!text) text = _("Deminiaturize");
556 menu->entries[MC_MINIATURIZE]->text = text;
557 } else {
558 static char *text = NULL;
559 if (!text) text = _("Miniaturize");
561 menu->entries[MC_MINIATURIZE]->text = text;
564 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
566 if (wwin->flags.maximized) {
567 static char *text = NULL;
568 if (!text) text = _("Unmaximize");
570 menu->entries[MC_MAXIMIZE]->text = text;
571 } else {
572 static char *text = NULL;
573 if (!text) text = _("Maximize");
575 menu->entries[MC_MAXIMIZE]->text = text;
577 wMenuSetEnabled(menu, MC_MAXIMIZE, !WFLAGP(wwin, no_resizable));
580 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable)
581 && !wwin->flags.miniaturized);
583 if (wwin->flags.shaded) {
584 static char *text = NULL;
585 if (!text) text = _("Unshade");
587 menu->entries[MC_SHADE]->text = text;
588 } else {
589 static char *text = NULL;
590 if (!text) text = _("Shade");
592 menu->entries[MC_SHADE]->text = text;
595 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
596 && !wwin->flags.miniaturized);
598 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
600 if (!wwin->flags.inspector_open) {
601 wMenuSetEnabled(menu, MC_PROPERTIES, True);
602 } else {
603 wMenuSetEnabled(menu, MC_PROPERTIES, False);
606 /* set the client data of the entries to the window */
607 for (i = 0; i < menu->entry_no; i++) {
608 menu->entries[i]->clientdata = wwin;
611 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
612 scr->workspace_submenu->entries[i]->clientdata = wwin;
613 if (i == scr->current_workspace) {
614 wMenuSetEnabled(scr->workspace_submenu, i, False);
615 } else {
616 wMenuSetEnabled(scr->workspace_submenu, i, True);
620 menu->flags.realized = 0;
621 wMenuRealize(menu);
625 void
626 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
628 WMenu *menu;
629 WScreen *scr = wwin->screen_ptr;
631 wwin->flags.menu_open_for_me = 1;
633 if (!scr->window_menu) {
634 scr->window_menu = createWindowMenu(scr);
636 /* hack to save some memory allocation/deallocation */
637 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
638 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
639 wfree(scr->window_menu->entries[MC_SHADE]->text);
640 } else {
641 updateWorkspaceMenu(scr->workspace_submenu);
644 menu = scr->window_menu;
645 if (menu->flags.mapped) {
646 wMenuUnmap(menu);
647 if (menu->entries[0]->clientdata==wwin) {
648 return;
652 updateMenuForWindow(menu, wwin);
654 x -= menu->frame->core->width/2;
655 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
656 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
657 if (x < wwin->frame_x)
658 x = wwin->frame_x;
660 if (!wwin->flags.internal_window)
661 wMenuMapAt(menu, x, y, keyboard);
665 void
666 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
668 WMenu *menu;
669 WScreen *scr = wwin->screen_ptr;
671 wwin->flags.menu_open_for_me = 1;
673 if (!scr->window_menu) {
674 scr->window_menu = createWindowMenu(scr);
676 /* hack to save some memory allocation/deallocation */
677 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
678 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
679 wfree(scr->window_menu->entries[MC_SHADE]->text);
680 } else {
681 updateWorkspaceMenu(scr->workspace_submenu);
684 menu = scr->window_menu;
685 if (menu->flags.mapped) {
686 wMenuUnmap(menu);
687 if (menu->entries[0]->clientdata==wwin) {
688 return;
692 updateMenuForWindow(menu, wwin);
694 x -= menu->frame->core->width/2;
696 wMenuMapAt(menu, x, y, False);