Code update for Window Maker version 0.50.0
[wmaker-crm.git] / src / winmenu.c
blob3695b3d89ea075e66115cae1dea16b8f1ce40867
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"
47 #define MC_MAXIMIZE 0
48 #define MC_MINIATURIZE 1
49 #define MC_SHADE 2
50 #define MC_HIDE 3
51 #define MC_MOVERESIZE 4
52 #define MC_SELECT 5
53 #define MC_DUMMY_MOVETO 6
54 #define MC_PROPERTIES 7
55 #define MC_OPTIONS 8
56 #define MC_SHORTCUT 8
58 #define MC_CLOSE 9
59 #define MC_KILL 10
62 #define WO_KEEP_ON_TOP 0
63 #define WO_KEEP_AT_BOTTOM 1
64 #define WO_OMNIPRESENT 2
65 #define WO_ENTRIES 3
67 /**** Global data ***/
68 extern Time LastTimestamp;
69 extern Atom _XA_WM_DELETE_WINDOW;
70 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
72 extern WShortKey wKeyBindings[WKBD_LAST];
74 extern WPreferences wPreferences;
76 static void updateOptionsMenu(WMenu *menu, WWindow *wwin);
78 static void
79 execWindowOptionCommand(WMenu *menu, WMenuEntry *entry)
81 WWindow *wwin = (WWindow*)entry->clientdata;
83 switch (entry->order) {
84 case WO_KEEP_ON_TOP:
85 if(wwin->frame->core->stacking->window_level!=WMFloatingLevel)
86 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
87 else
88 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
89 break;
91 case WO_KEEP_AT_BOTTOM:
92 if(wwin->frame->core->stacking->window_level!=WMSunkenLevel)
93 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
94 else
95 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
96 break;
98 case WO_OMNIPRESENT:
99 wwin->flags.omnipresent^=1;
100 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_WORKSPACE);
101 break;
106 static void
107 execMenuCommand(WMenu *menu, WMenuEntry *entry)
109 WWindow *wwin = (WWindow*)entry->clientdata;
110 WApplication *wapp;
112 CloseWindowMenu(menu->frame->screen_ptr);
114 switch (entry->order) {
115 case MC_CLOSE:
116 /* send delete message */
117 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
118 break;
120 case MC_KILL:
121 wretain(wwin);
122 if (wPreferences.dont_confirm_kill
123 || 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,
138 LastTimestamp);
139 } else {
140 wIconifyWindow(wwin);
143 break;
145 case MC_MAXIMIZE:
146 if (wwin->flags.maximized)
147 wUnmaximizeWindow(wwin);
148 else
149 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
150 break;
152 case MC_SHADE:
153 if (wwin->flags.shaded)
154 wUnshadeWindow(wwin);
155 else
156 wShadeWindow(wwin);
157 break;
159 case MC_SELECT:
160 wSelectWindow(wwin, !wwin->flags.selected);
161 break;
163 case MC_MOVERESIZE:
164 wKeyboardMoveResizeWindow(wwin);
165 break;
167 case MC_PROPERTIES:
168 if (wwin->wm_class || wwin->wm_instance)
169 wShowInspectorForWindow(wwin);
170 break;
172 case MC_HIDE:
173 wapp = wApplicationOf(wwin->main_window);
174 wHideApplication(wapp);
175 break;
181 static void
182 switchWSCommand(WMenu *menu, WMenuEntry *entry)
184 WWindow *wwin = (WWindow*)entry->clientdata;
186 wSelectWindow(wwin, False);
187 wWindowChangeWorkspace(wwin, entry->order);
191 static void
192 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
194 WWindow *wwin = (WWindow*)entry->clientdata;
196 wwin->screen_ptr->shortcutWindow[entry->order-WO_ENTRIES] = wwin;
198 wSelectWindow(wwin, !wwin->flags.selected);
199 XFlush(dpy);
200 wusleep(3000);
201 wSelectWindow(wwin, !wwin->flags.selected);
202 XFlush(dpy);
206 static void
207 updateWorkspaceMenu(WMenu *menu)
209 WScreen *scr = menu->frame->screen_ptr;
210 char title[MAX_WORKSPACENAME_WIDTH+1];
211 int i;
213 if (!menu)
214 return;
216 for (i=0; i<scr->workspace_count; i++) {
217 if (i < menu->entry_no) {
218 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
219 free(menu->entries[i]->text);
220 strcpy(title, scr->workspaces[i]->name);
221 menu->entries[i]->text = wstrdup(title);
222 menu->flags.realized = 0;
224 } else {
225 strcpy(title, scr->workspaces[i]->name);
227 wMenuAddCallback(menu, title, switchWSCommand, NULL);
229 menu->flags.realized = 0;
233 if (!menu->flags.realized)
234 wMenuRealize(menu);
238 static void
239 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
241 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
242 int i;
243 char *buffer;
244 KeyCode kcode;
246 if (!smenu)
247 return;
249 buffer = wmalloc(strlen(_("Set Shortcut"))+16);
251 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
252 char *tmp;
253 int shortcutNo = i-WO_ENTRIES;
254 WWindow *twin = wwin->screen_ptr->shortcutWindow[shortcutNo];
255 WMenuEntry *entry = smenu->entries[i];
257 sprintf(buffer, "%s %i", _("Set Shortcut"), shortcutNo+1);
259 if (!twin) {
260 entry->flags.indicator_on = 0;
261 } else {
262 entry->flags.indicator_on = 1;
263 if (twin != wwin)
264 entry->flags.indicator_type = MI_CHECK;
265 else
266 entry->flags.indicator_type = MI_DIAMOND;
269 if (strcmp(buffer, entry->text)!=0) {
270 free(entry->text);
271 entry->text = wstrdup(buffer);
272 smenu->flags.realized = 0;
275 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
277 if (kcode) {
278 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
279 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
280 if (entry->rtext)
281 free(entry->rtext);
282 entry->rtext = wstrdup(tmp);
283 smenu->flags.realized = 0;
285 wMenuSetEnabled(smenu, i, True);
286 } else {
287 wMenuSetEnabled(smenu, i, False);
288 if (entry->rtext) {
289 free(entry->rtext);
290 entry->rtext = NULL;
291 smenu->flags.realized = 0;
294 entry->clientdata = wwin;
296 free(buffer);
297 if (!smenu->flags.realized)
298 wMenuRealize(smenu);
302 static void
303 updateOptionsMenu(WMenu *menu, WWindow *wwin)
305 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
307 /* keep on top check */
308 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
309 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
310 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
312 /* keep at bottom check */
313 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
314 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
315 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
317 /* omnipresent check */
318 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
319 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
321 smenu->flags.realized = 0;
322 wMenuRealize(smenu);
326 static WMenu*
327 makeWorkspaceMenu(WScreen *scr)
329 WMenu *menu;
331 menu = wMenuCreate(scr, NULL, False);
332 if (!menu) {
333 wwarning(_("could not create submenu for window menu"));
334 return NULL;
337 updateWorkspaceMenu(menu);
339 return menu;
343 static WMenu*
344 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
347 WMenu *menu;
349 int i;
351 menu = wMenuCreate(scr, NULL, False);
352 if (!menu) {
353 wwarning(_("could not create submenu for window menu"));
354 return NULL;
358 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
359 WMenuEntry *entry;
361 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
363 entry->flags.indicator = 1;
366 return menu;
371 static WMenu*
372 makeOptionsMenu(WScreen *scr)
374 WMenu *menu;
375 WMenuEntry *entry;
377 menu = wMenuCreate(scr, NULL, False);
378 if (!menu) {
379 wwarning(_("could not create submenu for window menu"));
380 return NULL;
383 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
384 NULL);
385 entry->flags.indicator = 1;
386 entry->flags.indicator_type = MI_CHECK;
388 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
389 NULL);
390 entry->flags.indicator = 1;
391 entry->flags.indicator_type = MI_CHECK;
393 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
394 NULL);
395 entry->flags.indicator = 1;
396 entry->flags.indicator_type = MI_CHECK;
398 return menu;
402 static WMenu*
403 createWindowMenu(WScreen *scr)
405 WMenu *menu;
406 KeyCode kcode;
407 WMenuEntry *entry;
408 char *tmp;
410 menu = wMenuCreate(scr, NULL, False);
412 * Warning: If you make some change that affects the order of the
413 * entries, you must update the command #defines in the top of
414 * this file.
416 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
417 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
418 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
420 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
421 entry->rtext = wstrdup(tmp);
424 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
426 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
427 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
429 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
430 entry->rtext = wstrdup(tmp);
433 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
434 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
435 kcode = wKeyBindings[WKBD_SHADE].keycode;
437 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
438 entry->rtext = wstrdup(tmp);
441 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
442 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
443 kcode = wKeyBindings[WKBD_HIDE].keycode;
445 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
446 entry->rtext = wstrdup(tmp);
449 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
450 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
451 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
453 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
454 entry->rtext = wstrdup(tmp);
457 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
458 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
459 kcode = wKeyBindings[WKBD_SELECT].keycode;
461 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
462 entry->rtext = wstrdup(tmp);
465 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
466 scr->workspace_submenu = makeWorkspaceMenu(scr);
467 if (scr->workspace_submenu)
468 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
470 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
472 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
473 wMenuEntrySetCascade(menu, entry,
474 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
477 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
478 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
481 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
482 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
483 kcode = wKeyBindings[WKBD_CLOSE].keycode;
484 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
485 entry->rtext = wstrdup(tmp);
488 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
490 return menu;
494 void
495 CloseWindowMenu(WScreen *scr)
497 if (scr->window_menu) {
498 if (scr->window_menu->flags.mapped)
499 wMenuUnmap(scr->window_menu);
501 if (scr->window_menu->entries[0]->clientdata) {
502 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
504 wwin->flags.menu_open_for_me = 0;
506 scr->window_menu->entries[0]->clientdata = NULL;
512 static void
513 updateMenuForWindow(WMenu *menu, WWindow *wwin)
515 WApplication *wapp = wApplicationOf(wwin->main_window);
516 WScreen *scr = wwin->screen_ptr;
517 int i;
519 updateOptionsMenu(menu, wwin);
521 updateMakeShortcutMenu(menu, wwin);
523 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
524 && !WFLAGP(wapp->main_window_desc, no_appicon));
526 wMenuSetEnabled(menu, MC_CLOSE,
527 (wwin->protocols.DELETE_WINDOW
528 && !WFLAGP(wwin, no_closable)));
530 if (wwin->flags.miniaturized) {
531 static char *text = _("Deminiaturize");
533 menu->entries[MC_MINIATURIZE]->text = text;
534 } else {
535 static char *text = _("Miniaturize");
537 menu->entries[MC_MINIATURIZE]->text = text;
540 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
542 if (wwin->flags.maximized) {
543 static char *text = _("Unmaximize");
545 menu->entries[MC_MAXIMIZE]->text = text;
546 } else {
547 static char *text = _("Maximize");
549 menu->entries[MC_MAXIMIZE]->text = text;
552 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable));
554 if (wwin->flags.shaded) {
555 static char *text = _("Unshade");
557 menu->entries[MC_SHADE]->text = text;
558 } else {
559 static char *text = _("Shade");
561 menu->entries[MC_SHADE]->text = text;
564 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
565 && !wwin->flags.miniaturized);
567 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
569 if ((wwin->wm_class || wwin->wm_instance) && !wwin->flags.inspector_open) {
570 wMenuSetEnabled(menu, MC_PROPERTIES, True);
571 } else {
572 wMenuSetEnabled(menu, MC_PROPERTIES, False);
575 /* set the client data of the entries to the window */
576 for (i = 0; i < menu->entry_no; i++) {
577 menu->entries[i]->clientdata = wwin;
580 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
581 scr->workspace_submenu->entries[i]->clientdata = wwin;
582 if (i == scr->current_workspace) {
583 wMenuSetEnabled(scr->workspace_submenu, i, False);
584 } else {
585 wMenuSetEnabled(scr->workspace_submenu, i, True);
589 menu->flags.realized = 0;
590 wMenuRealize(menu);
594 void
595 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
597 WMenu *menu;
598 WScreen *scr = wwin->screen_ptr;
600 wwin->flags.menu_open_for_me = 1;
602 if (!scr->window_menu) {
603 scr->window_menu = createWindowMenu(scr);
605 /* hack to save some memory allocation/deallocation */
606 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
607 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
608 free(scr->window_menu->entries[MC_SHADE]->text);
609 } else {
610 updateWorkspaceMenu(scr->workspace_submenu);
613 menu = scr->window_menu;
614 if (menu->flags.mapped) {
615 wMenuUnmap(menu);
616 if (menu->entries[0]->clientdata==wwin) {
617 return;
621 updateMenuForWindow(menu, wwin);
623 x -= menu->frame->core->width/2;
624 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
625 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
626 if (x < wwin->frame_x)
627 x = wwin->frame_x;
629 if (!wwin->flags.internal_window)
630 wMenuMapAt(menu, x, y, keyboard);
634 #if 0
635 void
636 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
638 WMenu *menu;
639 WScreen *scr = wwin->screen_ptr;
641 wwin->flags.menu_open_for_me = 1;
643 if (!scr->window_menu) {
644 scr->window_menu = createWindowMenu(scr);
646 /* hack to save some memory allocation/deallocation */
647 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
648 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
649 free(scr->window_menu->entries[MC_SHADE]->text);
650 } else {
651 updateWorkspaceMenu(scr->workspace_submenu);
654 menu = scr->window_menu;
655 if (menu->flags.mapped) {
656 wMenuUnmap(menu);
657 if (menu->entries[0]->clientdata==wwin) {
658 return;
662 updateMenuForWindow(menu, wwin);
664 x -= menu->frame->core->width/2;
666 wMenuMapAt(menu, x, y, False);
668 #endif