NEWS: Hot corners feature description
[wmaker-crm.git] / src / winmenu.c
blob3d6f59fd0d5c73e4ab26be20bb71e90cc2a99a45
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 unsigned int indicator;
123 const char *label;
124 unsigned int shortcut_idx;
125 int maxim_direction;
126 } menu_maximize_entries[] = {
127 { MI_SNAP_V, N_("Vertically"), WKBD_VMAXIMIZE, MAX_VERTICAL },
128 { MI_SNAP_H, N_("Horizontally"), WKBD_HMAXIMIZE, MAX_HORIZONTAL },
129 { MI_SNAP_LH, N_("Left half"), WKBD_LHMAXIMIZE, MAX_VERTICAL | MAX_LEFTHALF },
130 { MI_SNAP_RH, N_("Right half"), WKBD_RHMAXIMIZE, MAX_VERTICAL | MAX_RIGHTHALF },
131 { MI_SNAP_TH, N_("Top half"), WKBD_THMAXIMIZE, MAX_HORIZONTAL | MAX_TOPHALF },
132 { MI_SNAP_BH, N_("Bottom half"), WKBD_BHMAXIMIZE, MAX_HORIZONTAL | MAX_BOTTOMHALF },
133 { MI_SNAP_TL, N_("Top left"), WKBD_LTCMAXIMIZE, MAX_LEFTHALF | MAX_TOPHALF },
134 { MI_SNAP_TR, N_("Top right"), WKBD_RTCMAXIMIZE, MAX_RIGHTHALF | MAX_TOPHALF },
135 { MI_SNAP_BL, N_("Bottom left"), WKBD_LBCMAXIMIZE, MAX_LEFTHALF | MAX_BOTTOMHALF },
136 { MI_SNAP_BR, N_("Bottom right"), WKBD_RBCMAXIMIZE, MAX_RIGHTHALF | MAX_BOTTOMHALF },
137 { MI_SNAP_TILED, N_("Tiled"), WKBD_MAXIMUS, MAX_MAXIMUS }
140 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
142 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
144 WWindow *wwin = (WWindow *) entry->clientdata;
146 /* Parameter not used, but tell the compiler that it is ok */
147 (void) menu;
149 switch (entry->order) {
150 case WO_KEEP_ON_TOP:
151 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
152 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
153 else
154 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
155 break;
157 case WO_KEEP_AT_BOTTOM:
158 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
159 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
160 else
161 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
162 break;
164 case WO_OMNIPRESENT:
165 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
166 break;
170 static void execMaximizeCommand(WMenu * menu, WMenuEntry * entry)
172 WWindow *wwin = (WWindow *) entry->clientdata;
174 /* Parameter not used, but tell the compiler that it is ok */
175 (void) menu;
177 handleMaximize(wwin, menu_maximize_entries[entry->order].maxim_direction);
180 static void updateUnmaximizeShortcut(WMenuEntry * entry, int flags)
182 int key;
184 switch (flags & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS)) {
185 case MAX_HORIZONTAL:
186 key = WKBD_HMAXIMIZE;
187 break;
189 case MAX_VERTICAL:
190 key = WKBD_VMAXIMIZE;
191 break;
193 case MAX_LEFTHALF | MAX_VERTICAL:
194 key = WKBD_LHMAXIMIZE;
195 break;
197 case MAX_RIGHTHALF | MAX_VERTICAL:
198 key = WKBD_RHMAXIMIZE;
199 break;
201 case MAX_TOPHALF | MAX_HORIZONTAL:
202 key = WKBD_THMAXIMIZE;
203 break;
205 case MAX_BOTTOMHALF | MAX_HORIZONTAL:
206 key = WKBD_BHMAXIMIZE;
207 break;
209 case MAX_LEFTHALF | MAX_TOPHALF:
210 key = WKBD_LTCMAXIMIZE;
211 break;
213 case MAX_RIGHTHALF | MAX_TOPHALF:
214 key = WKBD_RTCMAXIMIZE;
215 break;
217 case MAX_LEFTHALF | MAX_BOTTOMHALF:
218 key = WKBD_LBCMAXIMIZE;
219 break;
221 case MAX_RIGHTHALF | MAX_BOTTOMHALF:
222 key = WKBD_RBCMAXIMIZE;
223 break;
225 case MAX_MAXIMUS:
226 key = WKBD_MAXIMUS;
227 break;
229 default:
230 key = WKBD_MAXIMIZE;
231 break;
234 entry->rtext = GetShortcutKey(wKeyBindings[key]);
237 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
239 WWindow *wwin = (WWindow *) entry->clientdata;
240 WApplication *wapp;
242 CloseWindowMenu(menu->frame->screen_ptr);
244 switch (entry->order) {
245 case MC_CLOSE:
246 /* send delete message */
247 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
248 w_global.timestamp.last_event);
249 break;
251 case MC_KILL:
252 wretain(wwin);
253 if (wPreferences.dont_confirm_kill
254 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
256 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
257 _("Yes"), _("No"), NULL) == WAPRDefault) {
258 if (!wwin->flags.destroyed)
259 wClientKill(wwin);
261 wrelease(wwin);
262 break;
264 case MC_MINIATURIZE:
265 if (wwin->flags.miniaturized) {
266 wDeiconifyWindow(wwin);
267 } else {
268 if (wwin->protocols.MINIATURIZE_WINDOW) {
269 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
270 w_global.timestamp.last_event);
271 } else {
272 wIconifyWindow(wwin);
275 break;
277 case MC_MAXIMIZE:
278 if (wwin->flags.maximized)
279 wUnmaximizeWindow(wwin);
280 else
281 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL,
282 wGetHeadForWindow(wwin));
283 break;
285 case MC_SHADE:
286 if (wwin->flags.shaded)
287 wUnshadeWindow(wwin);
288 else
289 wShadeWindow(wwin);
290 break;
292 case MC_SELECT:
293 if (!wwin->flags.miniaturized)
294 wSelectWindow(wwin, !wwin->flags.selected);
295 else
296 wIconSelect(wwin->icon);
297 break;
299 case MC_MOVERESIZE:
300 wKeyboardMoveResizeWindow(wwin);
301 break;
303 case MC_PROPERTIES:
304 wShowInspectorForWindow(wwin);
305 break;
307 case MC_RELAUNCH:
308 (void) RelaunchWindow(wwin);
309 break;
311 case MC_HIDE:
312 wapp = wApplicationOf(wwin->main_window);
313 wHideApplication(wapp);
314 break;
318 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
320 WWindow *wwin = (WWindow *) entry->clientdata;
322 /* Parameter not used, but tell the compiler that it is ok */
323 (void) menu;
325 wSelectWindow(wwin, False);
326 wWindowChangeWorkspace(wwin, entry->order);
329 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
331 WWindow *wwin = (WWindow *) entry->clientdata;
332 WScreen *scr = wwin->screen_ptr;
333 int index = entry->order - wlengthof(menu_options_entries);
335 /* Parameter not used, but tell the compiler that it is ok */
336 (void) menu;
338 if (scr->shortcutWindows[index]) {
339 WMFreeArray(scr->shortcutWindows[index]);
340 scr->shortcutWindows[index] = NULL;
343 if (wwin->flags.selected && scr->selected_windows) {
344 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
345 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
346 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
347 } else {
348 scr->shortcutWindows[index] = WMCreateArray(4);
349 WMAddToArray(scr->shortcutWindows[index], wwin);
352 wSelectWindow(wwin, !wwin->flags.selected);
353 XFlush(dpy);
354 wusleep(3000);
355 wSelectWindow(wwin, !wwin->flags.selected);
356 XFlush(dpy);
359 static void updateWorkspaceMenu(WMenu * menu)
361 WScreen *scr = menu->frame->screen_ptr;
362 char title[MAX_WORKSPACENAME_WIDTH + 1];
363 WMenuEntry *entry;
364 int i;
366 for (i = 0; i < scr->workspace_count; i++) {
367 if (i < menu->entry_no) {
369 entry = menu->entries[i];
370 if (strcmp(entry->text, scr->workspaces[i]->name) != 0) {
371 wfree(entry->text);
372 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
373 title[MAX_WORKSPACENAME_WIDTH] = 0;
374 menu->entries[i]->text = wstrdup(title);
375 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
376 menu->flags.realized = 0;
378 } else {
379 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
380 title[MAX_WORKSPACENAME_WIDTH] = 0;
382 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
383 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
385 menu->flags.realized = 0;
388 /* workspace shortcut labels */
389 if (i / 10 == scr->current_workspace / 10)
390 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
391 else
392 entry->rtext = NULL;
395 if (!menu->flags.realized)
396 wMenuRealize(menu);
399 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
401 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
402 int i;
403 char buffer[64];
404 KeyCode kcode;
406 if (!smenu)
407 return;
409 for (i = wlengthof(menu_options_entries); i < smenu->entry_no; i++) {
410 int shortcutNo = i - wlengthof(menu_options_entries);
411 WMenuEntry *entry = smenu->entries[i];
412 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
414 snprintf(buffer, sizeof(buffer), _("Set Shortcut %i"), shortcutNo + 1);
416 if (!shortSelWindows) {
417 entry->flags.indicator_on = 0;
418 } else {
419 entry->flags.indicator_on = 1;
420 if (WMCountInArray(shortSelWindows, wwin))
421 entry->flags.indicator_type = MI_DIAMOND;
422 else
423 entry->flags.indicator_type = MI_CHECK;
426 if (strcmp(buffer, entry->text) != 0) {
427 wfree(entry->text);
428 entry->text = wstrdup(buffer);
429 smenu->flags.realized = 0;
432 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
434 if (kcode) {
435 char *tmp;
437 tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]);
438 if (tmp == NULL) {
439 if (entry->rtext != NULL) {
440 /* There was a shortcut, but there is no more */
441 wfree(entry->rtext);
442 entry->rtext = NULL;
443 smenu->flags.realized = 0;
445 } else if (entry->rtext == NULL) {
446 /* There was no shortcut, but there is one now */
447 entry->rtext = tmp;
448 smenu->flags.realized = 0;
449 } else if (strcmp(tmp, entry->rtext) != 0) {
450 /* There was a shortcut, but it has changed */
451 wfree(entry->rtext);
452 entry->rtext = tmp;
453 smenu->flags.realized = 0;
454 } else {
455 /* There was a shortcut but it did not change */
456 wfree(tmp);
458 wMenuSetEnabled(smenu, i, True);
459 } else {
460 wMenuSetEnabled(smenu, i, False);
461 if (entry->rtext) {
462 wfree(entry->rtext);
463 entry->rtext = NULL;
464 smenu->flags.realized = 0;
467 entry->clientdata = wwin;
469 if (!smenu->flags.realized)
470 wMenuRealize(smenu);
473 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
475 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
477 /* keep on top check */
478 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
479 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
480 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
481 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
482 smenu->entries[WO_KEEP_ON_TOP]->rtext = GetShortcutKey(wKeyBindings[WKBD_KEEP_ON_TOP]);
484 /* keep at bottom check */
485 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
486 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
487 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
488 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
489 smenu->entries[WO_KEEP_AT_BOTTOM]->rtext = GetShortcutKey(wKeyBindings[WKBD_KEEP_AT_BOTTOM]);
491 /* omnipresent check */
492 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
493 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
494 smenu->entries[WO_OMNIPRESENT]->rtext = GetShortcutKey(wKeyBindings[WKBD_OMNIPRESENT]);
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 every time 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 WMenuEntry *entry;
567 int i;
569 menu = wMenuCreate(scr, NULL, False);
570 if (!menu) {
571 wwarning(_("could not create submenu for window menu"));
572 return NULL;
575 for (i = 0; i < wlengthof(menu_maximize_entries); i++) {
576 entry = wMenuAddCallback(menu, _(menu_maximize_entries[i].label), execMaximizeCommand, NULL);
577 entry->flags.indicator = entry->flags.indicator_on = 1;
578 entry->flags.indicator_type = menu_maximize_entries[i].indicator;
581 return menu;
584 static WMenu *createWindowMenu(WScreen * scr)
586 WMenu *menu;
587 int i;
589 menu = wMenuCreate(scr, NULL, False);
591 for (i = 0; i < wlengthof(window_menu_entries); i++) {
592 WMenuEntry *entry;
594 entry = wMenuAddCallback(menu, _(window_menu_entries[i].label),
595 (window_menu_entries[i].generate_submenu == NULL)?execMenuCommand:NULL,
596 NULL);
597 if (window_menu_entries[i].generate_submenu != NULL) {
598 WMenu *submenu;
600 submenu = window_menu_entries[i].generate_submenu(scr);
601 wMenuEntrySetCascade(menu, entry, submenu);
605 return menu;
608 void CloseWindowMenu(WScreen * scr)
610 if (scr->window_menu) {
611 if (scr->window_menu->flags.mapped)
612 wMenuUnmap(scr->window_menu);
614 if (scr->window_menu->entries[0]->clientdata) {
615 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
617 wwin->flags.menu_open_for_me = 0;
619 scr->window_menu->entries[0]->clientdata = NULL;
623 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
625 WApplication *wapp = wApplicationOf(wwin->main_window);
626 WScreen *scr = wwin->screen_ptr;
627 int i;
629 updateOptionsMenu(menu, wwin);
630 updateMaximizeMenu(menu, wwin);
632 updateMakeShortcutMenu(menu, wwin);
634 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
636 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
638 if (wwin->flags.miniaturized) {
639 static char *text = NULL;
640 if (!text)
641 text = _("Deminiaturize");
643 menu->entries[MC_MINIATURIZE]->text = text;
644 } else {
645 static char *text = NULL;
646 if (!text)
647 text = _("Miniaturize");
649 menu->entries[MC_MINIATURIZE]->text = text;
652 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
654 if (wwin->flags.maximized) {
655 static char *text = NULL;
656 if (!text)
657 text = _("Unmaximize");
659 menu->entries[MC_MAXIMIZE]->text = text;
660 updateUnmaximizeShortcut(menu->entries[MC_MAXIMIZE], wwin->flags.maximized);
661 } else {
662 static char *text = NULL;
663 if (!text)
664 text = _("Maximize");
666 menu->entries[MC_MAXIMIZE]->text = text;
667 menu->entries[MC_MAXIMIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
669 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
671 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
672 && !wwin->flags.miniaturized);
674 if (wwin->flags.shaded) {
675 static char *text = NULL;
676 if (!text)
677 text = _("Unshade");
679 menu->entries[MC_SHADE]->text = text;
680 } else {
681 static char *text = NULL;
682 if (!text)
683 text = _("Shade");
685 menu->entries[MC_SHADE]->text = text;
688 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
689 && !wwin->flags.miniaturized);
691 if (wwin->flags.selected) {
692 static char *text = NULL;
693 if (!text)
694 text = _("Deselect");
696 menu->entries[MC_SELECT]->text = text;
697 } else {
698 static char *text = NULL;
699 if (!text)
700 text = _("Select");
702 menu->entries[MC_SELECT]->text = text;
705 wMenuSetEnabled(menu, MC_CHANGEWKSPC, !IS_OMNIPRESENT(wwin));
707 if (!wwin->flags.inspector_open) {
708 wMenuSetEnabled(menu, MC_PROPERTIES, True);
709 } else {
710 wMenuSetEnabled(menu, MC_PROPERTIES, False);
713 /* Update shortcut labels except for (Un)Maximize which is
714 * handled separately.
716 menu->entries[MC_MINIATURIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
717 menu->entries[MC_SHADE]->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
718 menu->entries[MC_HIDE]->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
719 menu->entries[MC_MOVERESIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
720 menu->entries[MC_SELECT]->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
721 menu->entries[MC_RELAUNCH]->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
722 menu->entries[MC_CLOSE]->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
724 /* set the client data of the entries to the window */
725 for (i = 0; i < menu->entry_no; i++) {
726 menu->entries[i]->clientdata = wwin;
729 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
730 scr->workspace_submenu->entries[i]->clientdata = wwin;
731 if (i == scr->current_workspace)
732 wMenuSetEnabled(scr->workspace_submenu, i, False);
733 else
734 wMenuSetEnabled(scr->workspace_submenu, i, True);
737 menu->flags.realized = 0;
738 wMenuRealize(menu);
741 static WMenu *open_window_menu_core(WWindow *wwin)
743 WScreen *scr = wwin->screen_ptr;
744 WMenu *menu;
746 wwin->flags.menu_open_for_me = 1;
748 if (!scr->window_menu) {
749 scr->window_menu = createWindowMenu(scr);
751 /* hack to save some memory allocation/deallocation */
752 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
753 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
754 wfree(scr->window_menu->entries[MC_SHADE]->text);
755 wfree(scr->window_menu->entries[MC_SELECT]->text);
756 } else {
757 updateWorkspaceMenu(scr->workspace_submenu);
760 menu = scr->window_menu;
761 if (menu->flags.mapped) {
762 wMenuUnmap(menu);
763 if (menu->entries[0]->clientdata == wwin)
764 return NULL;
767 updateMenuForWindow(menu, wwin);
769 return menu;
772 static void prepare_menu_position(WMenu *menu, int *x, int *y)
774 WMRect rect;
776 rect = wGetRectForHead(menu->frame->screen_ptr,
777 wGetHeadForPointerLocation(menu->frame->screen_ptr));
778 if (*x < rect.pos.x - menu->frame->core->width / 2)
779 *x = rect.pos.x - menu->frame->core->width / 2;
780 if (*y < rect.pos.y)
781 *y = rect.pos.y;
784 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
786 WMenu *menu;
788 menu = open_window_menu_core(wwin);
789 if (!menu)
790 return;
792 /* Specific menu position */
793 x -= menu->frame->core->width / 2;
794 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
795 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
796 if (x < wwin->frame_x)
797 x = wwin->frame_x;
799 /* Common menu position */
800 prepare_menu_position(menu, &x, &y);
802 if (!wwin->flags.internal_window)
803 wMenuMapAt(menu, x, y, keyboard);
806 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
808 int i;
809 WMenu *menu;
810 WScreen *scr = wwin->screen_ptr;
812 menu = open_window_menu_core(wwin);
813 if (!menu)
814 return;
816 /* Specific menu position */
817 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
818 scr->workspace_submenu->entries[i]->clientdata = wwin;
819 wMenuSetEnabled(scr->workspace_submenu, i, True);
822 x -= menu->frame->core->width / 2;
824 /* Common menu position */
825 prepare_menu_position(menu, &x, &y);
827 if (!wwin->flags.internal_window)
828 wMenuMapAt(menu, x, y, keyboard);
831 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
833 WMenu *menu;
835 menu = open_window_menu_core(wwin);
836 if (!menu)
837 return;
839 x -= menu->frame->core->width / 2;
841 wMenuMapAt(menu, x, y, False);
844 void DestroyWindowMenu(WScreen *scr)
846 if (scr->window_menu) {
847 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
848 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
849 scr->window_menu->entries[MC_SHADE]->text = NULL;
850 scr->window_menu->entries[MC_SELECT]->text = NULL;
851 wMenuDestroy(scr->window_menu, True);
852 scr->window_menu = NULL;