debian: Simplify debian/rules.
[wmaker-crm.git] / src / winmenu.c
blobc7da09d3a4cebc13cd353eb0b912e5c1dac6f548
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 "misc.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"
49 #include "winmenu.h"
52 static WMenu *makeWorkspaceMenu(WScreen *scr);
53 static WMenu *makeOptionsMenu(WScreen *scr);
54 static WMenu *makeMaximizeMenu(WScreen *scr);
57 * Define the Menu entry that will be present in the Window menu
59 * The order of this list defines the order in which they will appear;
60 * make sure to keep the structure below aligned with the list because
61 * the constant index is used in many places.
63 enum
65 MC_MAXIMIZE,
66 MC_OTHERMAX,
67 MC_MINIATURIZE,
68 MC_SHADE,
69 MC_HIDE,
70 MC_MOVERESIZE,
71 MC_SELECT,
72 MC_CHANGEWKSPC,
73 MC_PROPERTIES,
74 MC_OPTIONS,
75 MC_RELAUNCH,
76 MC_CLOSE,
77 MC_KILL
80 static const struct {
81 const char *label;
82 WMenu *(*generate_submenu)(WScreen *scr);
83 } window_menu_entries[] = {
84 [MC_MAXIMIZE] = { N_("Maximize"), NULL },
85 [MC_OTHERMAX] = { N_("Other maximization"), makeMaximizeMenu },
86 [MC_MINIATURIZE] = { N_("Miniaturize"), NULL },
87 [MC_SHADE] = { N_("Shade"), NULL },
88 [MC_HIDE] = { N_("Hide"), NULL },
89 [MC_MOVERESIZE] = { N_("Resize/Move"), NULL },
90 [MC_SELECT] = { N_("Select"), NULL },
91 [MC_CHANGEWKSPC] = { N_("Move To"), makeWorkspaceMenu },
92 [MC_PROPERTIES] = { N_("Attributes..."), NULL },
93 [MC_OPTIONS] = { N_("Options"), makeOptionsMenu },
94 [MC_RELAUNCH] = { N_("Launch"), NULL },
95 [MC_CLOSE] = { N_("Close"), NULL },
96 [MC_KILL] = { N_("Kill"), NULL }
100 * Defines the menu entries for the Options sub-menu
102 * These options will be placed at the beginning of the menu, the rest will
103 * be populated with the Window Shortcut possibilities
105 enum
107 WO_KEEP_ON_TOP,
108 WO_KEEP_AT_BOTTOM,
109 WO_OMNIPRESENT
112 static const char *const menu_options_entries[] = {
113 [WO_KEEP_ON_TOP] = N_("Keep on top"),
114 [WO_KEEP_AT_BOTTOM] = N_("Keep at bottom"),
115 [WO_OMNIPRESENT] = N_("Omnipresent")
119 * Defines the menu entries for the Other maximization sub-menu
121 static const struct {
122 const char *label;
123 unsigned int shortcut_idx;
124 int maxim_direction;
125 } menu_maximize_entries[] = {
126 { N_("Maximize vertically"), WKBD_VMAXIMIZE, MAX_VERTICAL },
127 { N_("Maximize horizontally"), WKBD_HMAXIMIZE, MAX_HORIZONTAL },
128 { N_("Maximize left half"), WKBD_LHMAXIMIZE, MAX_VERTICAL | MAX_LEFTHALF },
129 { N_("Maximize right half"), WKBD_RHMAXIMIZE, MAX_VERTICAL | MAX_RIGHTHALF },
130 { N_("Maximize top half"), WKBD_THMAXIMIZE, MAX_HORIZONTAL | MAX_TOPHALF },
131 { N_("Maximize bottom half"), WKBD_BHMAXIMIZE, MAX_HORIZONTAL | MAX_BOTTOMHALF },
132 { N_("Maximize left top corner"), WKBD_LTCMAXIMIZE, MAX_LEFTHALF | MAX_TOPHALF },
133 { N_("Maximize right top corner"), WKBD_RTCMAXIMIZE, MAX_RIGHTHALF | MAX_TOPHALF },
134 { N_("Maximize left bottom corner"), WKBD_LBCMAXIMIZE, MAX_LEFTHALF | MAX_BOTTOMHALF },
135 { N_("Maximize right bottom corner"), WKBD_RBCMAXIMIZE, MAX_RIGHTHALF | MAX_BOTTOMHALF },
136 { N_("Maximus: tiled maximization"), WKBD_MAXIMUS, MAX_MAXIMUS }
139 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
141 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
143 WWindow *wwin = (WWindow *) entry->clientdata;
145 /* Parameter not used, but tell the compiler that it is ok */
146 (void) menu;
148 switch (entry->order) {
149 case WO_KEEP_ON_TOP:
150 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
151 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
152 else
153 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
154 break;
156 case WO_KEEP_AT_BOTTOM:
157 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
158 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
159 else
160 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
161 break;
163 case WO_OMNIPRESENT:
164 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
165 break;
169 static void execMaximizeCommand(WMenu * menu, WMenuEntry * entry)
171 WWindow *wwin = (WWindow *) entry->clientdata;
173 /* Parameter not used, but tell the compiler that it is ok */
174 (void) menu;
176 handleMaximize(wwin, menu_maximize_entries[entry->order].maxim_direction);
179 static void updateUnmaximizeShortcut(WMenuEntry * entry, int flags)
181 int key;
183 switch (flags & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS)) {
184 case MAX_HORIZONTAL:
185 key = WKBD_HMAXIMIZE;
186 break;
188 case MAX_VERTICAL:
189 key = WKBD_VMAXIMIZE;
190 break;
192 case MAX_LEFTHALF | MAX_VERTICAL:
193 key = WKBD_LHMAXIMIZE;
194 break;
196 case MAX_RIGHTHALF | MAX_VERTICAL:
197 key = WKBD_RHMAXIMIZE;
198 break;
200 case MAX_TOPHALF | MAX_HORIZONTAL:
201 key = WKBD_THMAXIMIZE;
202 break;
204 case MAX_BOTTOMHALF | MAX_HORIZONTAL:
205 key = WKBD_BHMAXIMIZE;
206 break;
208 case MAX_LEFTHALF | MAX_TOPHALF:
209 key = WKBD_LTCMAXIMIZE;
210 break;
212 case MAX_RIGHTHALF | MAX_TOPHALF:
213 key = WKBD_RTCMAXIMIZE;
214 break;
216 case MAX_LEFTHALF | MAX_BOTTOMHALF:
217 key = WKBD_LBCMAXIMIZE;
218 break;
220 case MAX_RIGHTHALF | MAX_BOTTOMHALF:
221 key = WKBD_RBCMAXIMIZE;
222 break;
224 case MAX_MAXIMUS:
225 key = WKBD_MAXIMUS;
226 break;
228 default:
229 key = WKBD_MAXIMIZE;
230 break;
233 entry->rtext = GetShortcutKey(wKeyBindings[key]);
236 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
238 WWindow *wwin = (WWindow *) entry->clientdata;
239 WApplication *wapp;
241 CloseWindowMenu(menu->frame->screen_ptr);
243 switch (entry->order) {
244 case MC_CLOSE:
245 /* send delete message */
246 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
247 w_global.timestamp.last_event);
248 break;
250 case MC_KILL:
251 wretain(wwin);
252 if (wPreferences.dont_confirm_kill
253 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
255 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
256 _("Yes"), _("No"), NULL) == WAPRDefault) {
257 if (!wwin->flags.destroyed)
258 wClientKill(wwin);
260 wrelease(wwin);
261 break;
263 case MC_MINIATURIZE:
264 if (wwin->flags.miniaturized) {
265 wDeiconifyWindow(wwin);
266 } else {
267 if (wwin->protocols.MINIATURIZE_WINDOW) {
268 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
269 w_global.timestamp.last_event);
270 } else {
271 wIconifyWindow(wwin);
274 break;
276 case MC_MAXIMIZE:
277 if (wwin->flags.maximized)
278 wUnmaximizeWindow(wwin);
279 else
280 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
281 break;
283 case MC_SHADE:
284 if (wwin->flags.shaded)
285 wUnshadeWindow(wwin);
286 else
287 wShadeWindow(wwin);
288 break;
290 case MC_SELECT:
291 if (!wwin->flags.miniaturized)
292 wSelectWindow(wwin, !wwin->flags.selected);
293 else
294 wIconSelect(wwin->icon);
295 break;
297 case MC_MOVERESIZE:
298 wKeyboardMoveResizeWindow(wwin);
299 break;
301 case MC_PROPERTIES:
302 wShowInspectorForWindow(wwin);
303 break;
305 case MC_RELAUNCH:
306 (void) RelaunchWindow(wwin);
307 break;
309 case MC_HIDE:
310 wapp = wApplicationOf(wwin->main_window);
311 wHideApplication(wapp);
312 break;
316 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
318 WWindow *wwin = (WWindow *) entry->clientdata;
320 /* Parameter not used, but tell the compiler that it is ok */
321 (void) menu;
323 wSelectWindow(wwin, False);
324 wWindowChangeWorkspace(wwin, entry->order);
327 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
329 WWindow *wwin = (WWindow *) entry->clientdata;
330 WScreen *scr = wwin->screen_ptr;
331 int index = entry->order - wlengthof(menu_options_entries);
333 /* Parameter not used, but tell the compiler that it is ok */
334 (void) menu;
336 if (scr->shortcutWindows[index]) {
337 WMFreeArray(scr->shortcutWindows[index]);
338 scr->shortcutWindows[index] = NULL;
341 if (wwin->flags.selected && scr->selected_windows) {
342 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
343 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
344 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
345 } else {
346 scr->shortcutWindows[index] = WMCreateArray(4);
347 WMAddToArray(scr->shortcutWindows[index], wwin);
350 wSelectWindow(wwin, !wwin->flags.selected);
351 XFlush(dpy);
352 wusleep(3000);
353 wSelectWindow(wwin, !wwin->flags.selected);
354 XFlush(dpy);
357 static void updateWorkspaceMenu(WMenu * menu)
359 WScreen *scr = menu->frame->screen_ptr;
360 char title[MAX_WORKSPACENAME_WIDTH + 1];
361 WMenuEntry *entry;
362 int i;
364 for (i = 0; i < scr->workspace_count; i++) {
365 if (i < menu->entry_no) {
367 entry = menu->entries[i];
368 if (strcmp(entry->text, scr->workspaces[i]->name) != 0) {
369 wfree(entry->text);
370 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
371 title[MAX_WORKSPACENAME_WIDTH] = 0;
372 menu->entries[i]->text = wstrdup(title);
373 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
374 menu->flags.realized = 0;
376 } else {
377 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
378 title[MAX_WORKSPACENAME_WIDTH] = 0;
380 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
381 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
383 menu->flags.realized = 0;
386 /* workspace shortcut labels */
387 if (i / 10 == scr->current_workspace / 10)
388 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
389 else
390 entry->rtext = NULL;
393 if (!menu->flags.realized)
394 wMenuRealize(menu);
397 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
399 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
400 int i;
401 char *buffer;
402 int buflen;
403 KeyCode kcode;
405 if (!smenu)
406 return;
408 buflen = strlen(_("Set Shortcut")) + 16;
409 buffer = wmalloc(buflen);
411 for (i = wlengthof(menu_options_entries); i < smenu->entry_no; i++) {
412 int shortcutNo = i - wlengthof(menu_options_entries);
413 WMenuEntry *entry = smenu->entries[i];
414 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
416 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
418 if (!shortSelWindows) {
419 entry->flags.indicator_on = 0;
420 } else {
421 entry->flags.indicator_on = 1;
422 if (WMCountInArray(shortSelWindows, wwin))
423 entry->flags.indicator_type = MI_DIAMOND;
424 else
425 entry->flags.indicator_type = MI_CHECK;
428 if (strcmp(buffer, entry->text) != 0) {
429 wfree(entry->text);
430 entry->text = wstrdup(buffer);
431 smenu->flags.realized = 0;
434 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
436 if (kcode) {
437 char *tmp;
439 tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]);
440 if (tmp == NULL) {
441 if (entry->rtext != NULL) {
442 /* There was a shortcut, but there is no more */
443 wfree(entry->rtext);
444 entry->rtext = NULL;
445 smenu->flags.realized = 0;
447 } else if (entry->rtext == NULL) {
448 /* There was no shortcut, but there is one now */
449 entry->rtext = tmp;
450 smenu->flags.realized = 0;
451 } else if (strcmp(tmp, entry->rtext) != 0) {
452 /* There was a shortcut, but it has changed */
453 wfree(entry->rtext);
454 entry->rtext = tmp;
455 smenu->flags.realized = 0;
456 } else {
457 /* There was a shortcut but it did not change */
458 wfree(tmp);
460 wMenuSetEnabled(smenu, i, True);
461 } else {
462 wMenuSetEnabled(smenu, i, False);
463 if (entry->rtext) {
464 wfree(entry->rtext);
465 entry->rtext = NULL;
466 smenu->flags.realized = 0;
469 entry->clientdata = wwin;
471 wfree(buffer);
472 if (!smenu->flags.realized)
473 wMenuRealize(smenu);
476 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
478 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
480 /* keep on top check */
481 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
482 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
483 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
484 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
486 /* keep at bottom check */
487 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
488 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
489 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
490 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
492 /* omnipresent check */
493 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
494 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
496 smenu->flags.realized = 0;
497 wMenuRealize(smenu);
500 static void updateMaximizeMenu(WMenu * menu, WWindow * wwin)
502 WMenu *smenu = menu->cascades[menu->entries[MC_OTHERMAX]->cascade];
503 int i;
505 for (i = 0; i < smenu->entry_no; i++) {
506 smenu->entries[i]->clientdata = wwin;
507 smenu->entries[i]->rtext = GetShortcutKey(wKeyBindings[menu_maximize_entries[i].shortcut_idx]);
510 smenu->flags.realized = 0;
511 wMenuRealize(smenu);
514 static WMenu *makeWorkspaceMenu(WScreen * scr)
516 WMenu *menu;
518 menu = wMenuCreate(scr, NULL, False);
519 if (!menu) {
520 wwarning(_("could not create submenu for window menu"));
521 return NULL;
524 updateWorkspaceMenu(menu);
527 * The Workspace Menu is made visible in the screen structure because
528 * it is updated when there is a change on workspaces. This was done
529 * to be efficient, avoiding re-generating completely the window menu
530 * and its sub-menus everytime it is needed.
532 scr->workspace_submenu = menu;
534 return menu;
537 static WMenu *makeOptionsMenu(WScreen * scr)
539 WMenu *menu;
540 WMenuEntry *entry;
541 int i;
543 menu = wMenuCreate(scr, NULL, False);
544 if (!menu) {
545 wwarning(_("could not create submenu for window menu"));
546 return NULL;
549 for (i = 0; i < wlengthof(menu_options_entries); i++) {
550 entry = wMenuAddCallback(menu, _(menu_options_entries[i]), execWindowOptionCommand, NULL);
551 entry->flags.indicator = 1;
552 entry->flags.indicator_type = MI_CHECK;
555 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
556 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
557 entry->flags.indicator = 1;
560 return menu;
563 static WMenu *makeMaximizeMenu(WScreen * scr)
565 WMenu *menu;
566 int i;
568 menu = wMenuCreate(scr, NULL, False);
569 if (!menu) {
570 wwarning(_("could not create submenu for window menu"));
571 return NULL;
574 for (i = 0; i < wlengthof(menu_maximize_entries); i++)
575 wMenuAddCallback(menu, _(menu_maximize_entries[i].label), execMaximizeCommand, NULL);
577 return menu;
580 static WMenu *createWindowMenu(WScreen * scr)
582 WMenu *menu;
583 int i;
585 menu = wMenuCreate(scr, NULL, False);
587 for (i = 0; i < wlengthof(window_menu_entries); i++) {
588 WMenuEntry *entry;
590 entry = wMenuAddCallback(menu, _(window_menu_entries[i].label),
591 (window_menu_entries[i].generate_submenu == NULL)?execMenuCommand:NULL,
592 NULL);
593 if (window_menu_entries[i].generate_submenu != NULL) {
594 WMenu *submenu;
596 submenu = window_menu_entries[i].generate_submenu(scr);
597 wMenuEntrySetCascade(menu, entry, submenu);
601 return menu;
604 void CloseWindowMenu(WScreen * scr)
606 if (scr->window_menu) {
607 if (scr->window_menu->flags.mapped)
608 wMenuUnmap(scr->window_menu);
610 if (scr->window_menu->entries[0]->clientdata) {
611 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
613 wwin->flags.menu_open_for_me = 0;
615 scr->window_menu->entries[0]->clientdata = NULL;
619 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
621 WApplication *wapp = wApplicationOf(wwin->main_window);
622 WScreen *scr = wwin->screen_ptr;
623 int i;
625 updateOptionsMenu(menu, wwin);
626 updateMaximizeMenu(menu, wwin);
628 updateMakeShortcutMenu(menu, wwin);
630 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
632 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
634 if (wwin->flags.miniaturized) {
635 static char *text = NULL;
636 if (!text)
637 text = _("Deminiaturize");
639 menu->entries[MC_MINIATURIZE]->text = text;
640 } else {
641 static char *text = NULL;
642 if (!text)
643 text = _("Miniaturize");
645 menu->entries[MC_MINIATURIZE]->text = text;
648 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
650 if (wwin->flags.maximized) {
651 static char *text = NULL;
652 if (!text)
653 text = _("Unmaximize");
655 menu->entries[MC_MAXIMIZE]->text = text;
656 updateUnmaximizeShortcut(menu->entries[MC_MAXIMIZE], wwin->flags.maximized);
657 } else {
658 static char *text = NULL;
659 if (!text)
660 text = _("Maximize");
662 menu->entries[MC_MAXIMIZE]->text = text;
663 menu->entries[MC_MAXIMIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
665 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
667 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
668 && !wwin->flags.miniaturized);
670 if (wwin->flags.shaded) {
671 static char *text = NULL;
672 if (!text)
673 text = _("Unshade");
675 menu->entries[MC_SHADE]->text = text;
676 } else {
677 static char *text = NULL;
678 if (!text)
679 text = _("Shade");
681 menu->entries[MC_SHADE]->text = text;
684 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
685 && !wwin->flags.miniaturized);
687 if (wwin->flags.selected) {
688 static char *text = NULL;
689 if (!text)
690 text = _("Deselect");
692 menu->entries[MC_SELECT]->text = text;
693 } else {
694 static char *text = NULL;
695 if (!text)
696 text = _("Select");
698 menu->entries[MC_SELECT]->text = text;
701 wMenuSetEnabled(menu, MC_CHANGEWKSPC, !IS_OMNIPRESENT(wwin));
703 if (!wwin->flags.inspector_open) {
704 wMenuSetEnabled(menu, MC_PROPERTIES, True);
705 } else {
706 wMenuSetEnabled(menu, MC_PROPERTIES, False);
709 /* Update shortcut labels except for (Un)Maximize which is
710 * handled separately.
712 menu->entries[MC_MINIATURIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
713 menu->entries[MC_SHADE]->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
714 menu->entries[MC_HIDE]->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
715 menu->entries[MC_MOVERESIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
716 menu->entries[MC_SELECT]->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
717 menu->entries[MC_RELAUNCH]->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
718 menu->entries[MC_CLOSE]->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
720 /* set the client data of the entries to the window */
721 for (i = 0; i < menu->entry_no; i++) {
722 menu->entries[i]->clientdata = wwin;
725 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
726 scr->workspace_submenu->entries[i]->clientdata = wwin;
727 if (i == scr->current_workspace)
728 wMenuSetEnabled(scr->workspace_submenu, i, False);
729 else
730 wMenuSetEnabled(scr->workspace_submenu, i, True);
733 menu->flags.realized = 0;
734 wMenuRealize(menu);
737 static WMenu *open_window_menu_core(WWindow *wwin)
739 WScreen *scr = wwin->screen_ptr;
740 WMenu *menu;
742 wwin->flags.menu_open_for_me = 1;
744 if (!scr->window_menu) {
745 scr->window_menu = createWindowMenu(scr);
747 /* hack to save some memory allocation/deallocation */
748 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
749 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
750 wfree(scr->window_menu->entries[MC_SHADE]->text);
751 wfree(scr->window_menu->entries[MC_SELECT]->text);
752 } else {
753 updateWorkspaceMenu(scr->workspace_submenu);
756 menu = scr->window_menu;
757 if (menu->flags.mapped) {
758 wMenuUnmap(menu);
759 if (menu->entries[0]->clientdata == wwin)
760 return NULL;
763 updateMenuForWindow(menu, wwin);
765 return menu;
768 static void prepare_menu_position(WMenu *menu, int *x, int *y)
770 WMRect rect;
772 rect = wGetRectForHead(menu->frame->screen_ptr,
773 wGetHeadForPointerLocation(menu->frame->screen_ptr));
774 if (*x < rect.pos.x - menu->frame->core->width / 2)
775 *x = rect.pos.x - menu->frame->core->width / 2;
776 if (*y < rect.pos.y)
777 *y = rect.pos.y;
780 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
782 WMenu *menu;
784 menu = open_window_menu_core(wwin);
785 if (!menu)
786 return;
788 /* Specific menu position */
789 x -= menu->frame->core->width / 2;
790 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
791 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
792 if (x < wwin->frame_x)
793 x = wwin->frame_x;
795 /* Common menu position */
796 prepare_menu_position(menu, &x, &y);
798 if (!wwin->flags.internal_window)
799 wMenuMapAt(menu, x, y, keyboard);
802 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
804 int i;
805 WMenu *menu;
806 WScreen *scr = wwin->screen_ptr;
808 menu = open_window_menu_core(wwin);
809 if (!menu)
810 return;
812 /* Specific menu position */
813 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
814 scr->workspace_submenu->entries[i]->clientdata = wwin;
815 wMenuSetEnabled(scr->workspace_submenu, i, True);
818 x -= menu->frame->core->width / 2;
820 /* Common menu position */
821 prepare_menu_position(menu, &x, &y);
823 if (!wwin->flags.internal_window)
824 wMenuMapAt(menu, x, y, keyboard);
827 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
829 WMenu *menu;
831 menu = open_window_menu_core(wwin);
832 if (!menu)
833 return;
835 x -= menu->frame->core->width / 2;
837 wMenuMapAt(menu, x, y, False);
840 void DestroyWindowMenu(WScreen *scr)
842 if (scr->window_menu) {
843 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
844 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
845 scr->window_menu->entries[MC_SHADE]->text = NULL;
846 scr->window_menu->entries[MC_SELECT]->text = NULL;
847 wMenuDestroy(scr->window_menu, True);
848 scr->window_menu = NULL;