WPrefs: Code formatting in TexturePanel.c; minimizes checkpatch.pl warnings.
[wmaker-crm.git] / src / winmenu.c
blob2aa318740fda3a99e9f08cb6acb5f47fdbc4e6ca
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);
485 smenu->entries[WO_KEEP_ON_TOP]->rtext = GetShortcutKey(wKeyBindings[WKBD_KEEP_ON_TOP]);
487 /* keep at bottom check */
488 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
489 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
490 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
491 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
492 smenu->entries[WO_KEEP_AT_BOTTOM]->rtext = GetShortcutKey(wKeyBindings[WKBD_KEEP_AT_BOTTOM]);
494 /* omnipresent check */
495 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
496 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
497 smenu->entries[WO_OMNIPRESENT]->rtext = GetShortcutKey(wKeyBindings[WKBD_OMNIPRESENT]);
499 smenu->flags.realized = 0;
500 wMenuRealize(smenu);
503 static void updateMaximizeMenu(WMenu * menu, WWindow * wwin)
505 WMenu *smenu = menu->cascades[menu->entries[MC_OTHERMAX]->cascade];
506 int i;
508 for (i = 0; i < smenu->entry_no; i++) {
509 smenu->entries[i]->clientdata = wwin;
510 smenu->entries[i]->rtext = GetShortcutKey(wKeyBindings[menu_maximize_entries[i].shortcut_idx]);
513 smenu->flags.realized = 0;
514 wMenuRealize(smenu);
517 static WMenu *makeWorkspaceMenu(WScreen * scr)
519 WMenu *menu;
521 menu = wMenuCreate(scr, NULL, False);
522 if (!menu) {
523 wwarning(_("could not create submenu for window menu"));
524 return NULL;
527 updateWorkspaceMenu(menu);
530 * The Workspace Menu is made visible in the screen structure because
531 * it is updated when there is a change on workspaces. This was done
532 * to be efficient, avoiding re-generating completely the window menu
533 * and its sub-menus every time it is needed.
535 scr->workspace_submenu = menu;
537 return menu;
540 static WMenu *makeOptionsMenu(WScreen * scr)
542 WMenu *menu;
543 WMenuEntry *entry;
544 int i;
546 menu = wMenuCreate(scr, NULL, False);
547 if (!menu) {
548 wwarning(_("could not create submenu for window menu"));
549 return NULL;
552 for (i = 0; i < wlengthof(menu_options_entries); i++) {
553 entry = wMenuAddCallback(menu, _(menu_options_entries[i]), execWindowOptionCommand, NULL);
554 entry->flags.indicator = 1;
555 entry->flags.indicator_type = MI_CHECK;
558 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
559 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
560 entry->flags.indicator = 1;
563 return menu;
566 static WMenu *makeMaximizeMenu(WScreen * scr)
568 WMenu *menu;
569 int i;
571 menu = wMenuCreate(scr, NULL, False);
572 if (!menu) {
573 wwarning(_("could not create submenu for window menu"));
574 return NULL;
577 for (i = 0; i < wlengthof(menu_maximize_entries); i++)
578 wMenuAddCallback(menu, _(menu_maximize_entries[i].label), execMaximizeCommand, NULL);
580 return menu;
583 static WMenu *createWindowMenu(WScreen * scr)
585 WMenu *menu;
586 int i;
588 menu = wMenuCreate(scr, NULL, False);
590 for (i = 0; i < wlengthof(window_menu_entries); i++) {
591 WMenuEntry *entry;
593 entry = wMenuAddCallback(menu, _(window_menu_entries[i].label),
594 (window_menu_entries[i].generate_submenu == NULL)?execMenuCommand:NULL,
595 NULL);
596 if (window_menu_entries[i].generate_submenu != NULL) {
597 WMenu *submenu;
599 submenu = window_menu_entries[i].generate_submenu(scr);
600 wMenuEntrySetCascade(menu, entry, submenu);
604 return menu;
607 void CloseWindowMenu(WScreen * scr)
609 if (scr->window_menu) {
610 if (scr->window_menu->flags.mapped)
611 wMenuUnmap(scr->window_menu);
613 if (scr->window_menu->entries[0]->clientdata) {
614 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
616 wwin->flags.menu_open_for_me = 0;
618 scr->window_menu->entries[0]->clientdata = NULL;
622 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
624 WApplication *wapp = wApplicationOf(wwin->main_window);
625 WScreen *scr = wwin->screen_ptr;
626 int i;
628 updateOptionsMenu(menu, wwin);
629 updateMaximizeMenu(menu, wwin);
631 updateMakeShortcutMenu(menu, wwin);
633 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
635 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
637 if (wwin->flags.miniaturized) {
638 static char *text = NULL;
639 if (!text)
640 text = _("Deminiaturize");
642 menu->entries[MC_MINIATURIZE]->text = text;
643 } else {
644 static char *text = NULL;
645 if (!text)
646 text = _("Miniaturize");
648 menu->entries[MC_MINIATURIZE]->text = text;
651 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
653 if (wwin->flags.maximized) {
654 static char *text = NULL;
655 if (!text)
656 text = _("Unmaximize");
658 menu->entries[MC_MAXIMIZE]->text = text;
659 updateUnmaximizeShortcut(menu->entries[MC_MAXIMIZE], wwin->flags.maximized);
660 } else {
661 static char *text = NULL;
662 if (!text)
663 text = _("Maximize");
665 menu->entries[MC_MAXIMIZE]->text = text;
666 menu->entries[MC_MAXIMIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
668 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
670 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
671 && !wwin->flags.miniaturized);
673 if (wwin->flags.shaded) {
674 static char *text = NULL;
675 if (!text)
676 text = _("Unshade");
678 menu->entries[MC_SHADE]->text = text;
679 } else {
680 static char *text = NULL;
681 if (!text)
682 text = _("Shade");
684 menu->entries[MC_SHADE]->text = text;
687 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
688 && !wwin->flags.miniaturized);
690 if (wwin->flags.selected) {
691 static char *text = NULL;
692 if (!text)
693 text = _("Deselect");
695 menu->entries[MC_SELECT]->text = text;
696 } else {
697 static char *text = NULL;
698 if (!text)
699 text = _("Select");
701 menu->entries[MC_SELECT]->text = text;
704 wMenuSetEnabled(menu, MC_CHANGEWKSPC, !IS_OMNIPRESENT(wwin));
706 if (!wwin->flags.inspector_open) {
707 wMenuSetEnabled(menu, MC_PROPERTIES, True);
708 } else {
709 wMenuSetEnabled(menu, MC_PROPERTIES, False);
712 /* Update shortcut labels except for (Un)Maximize which is
713 * handled separately.
715 menu->entries[MC_MINIATURIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
716 menu->entries[MC_SHADE]->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
717 menu->entries[MC_HIDE]->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
718 menu->entries[MC_MOVERESIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
719 menu->entries[MC_SELECT]->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
720 menu->entries[MC_RELAUNCH]->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
721 menu->entries[MC_CLOSE]->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
723 /* set the client data of the entries to the window */
724 for (i = 0; i < menu->entry_no; i++) {
725 menu->entries[i]->clientdata = wwin;
728 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
729 scr->workspace_submenu->entries[i]->clientdata = wwin;
730 if (i == scr->current_workspace)
731 wMenuSetEnabled(scr->workspace_submenu, i, False);
732 else
733 wMenuSetEnabled(scr->workspace_submenu, i, True);
736 menu->flags.realized = 0;
737 wMenuRealize(menu);
740 static WMenu *open_window_menu_core(WWindow *wwin)
742 WScreen *scr = wwin->screen_ptr;
743 WMenu *menu;
745 wwin->flags.menu_open_for_me = 1;
747 if (!scr->window_menu) {
748 scr->window_menu = createWindowMenu(scr);
750 /* hack to save some memory allocation/deallocation */
751 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
752 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
753 wfree(scr->window_menu->entries[MC_SHADE]->text);
754 wfree(scr->window_menu->entries[MC_SELECT]->text);
755 } else {
756 updateWorkspaceMenu(scr->workspace_submenu);
759 menu = scr->window_menu;
760 if (menu->flags.mapped) {
761 wMenuUnmap(menu);
762 if (menu->entries[0]->clientdata == wwin)
763 return NULL;
766 updateMenuForWindow(menu, wwin);
768 return menu;
771 static void prepare_menu_position(WMenu *menu, int *x, int *y)
773 WMRect rect;
775 rect = wGetRectForHead(menu->frame->screen_ptr,
776 wGetHeadForPointerLocation(menu->frame->screen_ptr));
777 if (*x < rect.pos.x - menu->frame->core->width / 2)
778 *x = rect.pos.x - menu->frame->core->width / 2;
779 if (*y < rect.pos.y)
780 *y = rect.pos.y;
783 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
785 WMenu *menu;
787 menu = open_window_menu_core(wwin);
788 if (!menu)
789 return;
791 /* Specific menu position */
792 x -= menu->frame->core->width / 2;
793 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
794 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
795 if (x < wwin->frame_x)
796 x = wwin->frame_x;
798 /* Common menu position */
799 prepare_menu_position(menu, &x, &y);
801 if (!wwin->flags.internal_window)
802 wMenuMapAt(menu, x, y, keyboard);
805 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
807 int i;
808 WMenu *menu;
809 WScreen *scr = wwin->screen_ptr;
811 menu = open_window_menu_core(wwin);
812 if (!menu)
813 return;
815 /* Specific menu position */
816 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
817 scr->workspace_submenu->entries[i]->clientdata = wwin;
818 wMenuSetEnabled(scr->workspace_submenu, i, True);
821 x -= menu->frame->core->width / 2;
823 /* Common menu position */
824 prepare_menu_position(menu, &x, &y);
826 if (!wwin->flags.internal_window)
827 wMenuMapAt(menu, x, y, keyboard);
830 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
832 WMenu *menu;
834 menu = open_window_menu_core(wwin);
835 if (!menu)
836 return;
838 x -= menu->frame->core->width / 2;
840 wMenuMapAt(menu, x, y, False);
843 void DestroyWindowMenu(WScreen *scr)
845 if (scr->window_menu) {
846 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
847 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
848 scr->window_menu->entries[MC_SHADE]->text = NULL;
849 scr->window_menu->entries[MC_SELECT]->text = NULL;
850 wMenuDestroy(scr->window_menu, True);
851 scr->window_menu = NULL;