Added 'head' parameter to wMaximizeWindow
[wmaker-crm.git] / src / winmenu.c
blobaf7fe62707b82f97fce89dc288218a67626fc590
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 wGetHeadForWindow(wwin));
282 break;
284 case MC_SHADE:
285 if (wwin->flags.shaded)
286 wUnshadeWindow(wwin);
287 else
288 wShadeWindow(wwin);
289 break;
291 case MC_SELECT:
292 if (!wwin->flags.miniaturized)
293 wSelectWindow(wwin, !wwin->flags.selected);
294 else
295 wIconSelect(wwin->icon);
296 break;
298 case MC_MOVERESIZE:
299 wKeyboardMoveResizeWindow(wwin);
300 break;
302 case MC_PROPERTIES:
303 wShowInspectorForWindow(wwin);
304 break;
306 case MC_RELAUNCH:
307 (void) RelaunchWindow(wwin);
308 break;
310 case MC_HIDE:
311 wapp = wApplicationOf(wwin->main_window);
312 wHideApplication(wapp);
313 break;
317 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
319 WWindow *wwin = (WWindow *) entry->clientdata;
321 /* Parameter not used, but tell the compiler that it is ok */
322 (void) menu;
324 wSelectWindow(wwin, False);
325 wWindowChangeWorkspace(wwin, entry->order);
328 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
330 WWindow *wwin = (WWindow *) entry->clientdata;
331 WScreen *scr = wwin->screen_ptr;
332 int index = entry->order - wlengthof(menu_options_entries);
334 /* Parameter not used, but tell the compiler that it is ok */
335 (void) menu;
337 if (scr->shortcutWindows[index]) {
338 WMFreeArray(scr->shortcutWindows[index]);
339 scr->shortcutWindows[index] = NULL;
342 if (wwin->flags.selected && scr->selected_windows) {
343 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
344 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
345 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
346 } else {
347 scr->shortcutWindows[index] = WMCreateArray(4);
348 WMAddToArray(scr->shortcutWindows[index], wwin);
351 wSelectWindow(wwin, !wwin->flags.selected);
352 XFlush(dpy);
353 wusleep(3000);
354 wSelectWindow(wwin, !wwin->flags.selected);
355 XFlush(dpy);
358 static void updateWorkspaceMenu(WMenu * menu)
360 WScreen *scr = menu->frame->screen_ptr;
361 char title[MAX_WORKSPACENAME_WIDTH + 1];
362 WMenuEntry *entry;
363 int i;
365 for (i = 0; i < scr->workspace_count; i++) {
366 if (i < menu->entry_no) {
368 entry = menu->entries[i];
369 if (strcmp(entry->text, scr->workspaces[i]->name) != 0) {
370 wfree(entry->text);
371 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
372 title[MAX_WORKSPACENAME_WIDTH] = 0;
373 menu->entries[i]->text = wstrdup(title);
374 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
375 menu->flags.realized = 0;
377 } else {
378 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
379 title[MAX_WORKSPACENAME_WIDTH] = 0;
381 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
382 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
384 menu->flags.realized = 0;
387 /* workspace shortcut labels */
388 if (i / 10 == scr->current_workspace / 10)
389 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
390 else
391 entry->rtext = NULL;
394 if (!menu->flags.realized)
395 wMenuRealize(menu);
398 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
400 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
401 int i;
402 char *buffer;
403 int buflen;
404 KeyCode kcode;
406 if (!smenu)
407 return;
409 buflen = strlen(_("Set Shortcut")) + 16;
410 buffer = wmalloc(buflen);
412 for (i = wlengthof(menu_options_entries); i < smenu->entry_no; i++) {
413 int shortcutNo = i - wlengthof(menu_options_entries);
414 WMenuEntry *entry = smenu->entries[i];
415 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
417 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
419 if (!shortSelWindows) {
420 entry->flags.indicator_on = 0;
421 } else {
422 entry->flags.indicator_on = 1;
423 if (WMCountInArray(shortSelWindows, wwin))
424 entry->flags.indicator_type = MI_DIAMOND;
425 else
426 entry->flags.indicator_type = MI_CHECK;
429 if (strcmp(buffer, entry->text) != 0) {
430 wfree(entry->text);
431 entry->text = wstrdup(buffer);
432 smenu->flags.realized = 0;
435 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
437 if (kcode) {
438 char *tmp;
440 tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]);
441 if (tmp == NULL) {
442 if (entry->rtext != NULL) {
443 /* There was a shortcut, but there is no more */
444 wfree(entry->rtext);
445 entry->rtext = NULL;
446 smenu->flags.realized = 0;
448 } else if (entry->rtext == NULL) {
449 /* There was no shortcut, but there is one now */
450 entry->rtext = tmp;
451 smenu->flags.realized = 0;
452 } else if (strcmp(tmp, entry->rtext) != 0) {
453 /* There was a shortcut, but it has changed */
454 wfree(entry->rtext);
455 entry->rtext = tmp;
456 smenu->flags.realized = 0;
457 } else {
458 /* There was a shortcut but it did not change */
459 wfree(tmp);
461 wMenuSetEnabled(smenu, i, True);
462 } else {
463 wMenuSetEnabled(smenu, i, False);
464 if (entry->rtext) {
465 wfree(entry->rtext);
466 entry->rtext = NULL;
467 smenu->flags.realized = 0;
470 entry->clientdata = wwin;
472 wfree(buffer);
473 if (!smenu->flags.realized)
474 wMenuRealize(smenu);
477 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
479 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
481 /* keep on top check */
482 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
483 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
484 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
485 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
486 smenu->entries[WO_KEEP_ON_TOP]->rtext = GetShortcutKey(wKeyBindings[WKBD_KEEP_ON_TOP]);
488 /* keep at bottom check */
489 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
490 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
491 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
492 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
493 smenu->entries[WO_KEEP_AT_BOTTOM]->rtext = GetShortcutKey(wKeyBindings[WKBD_KEEP_AT_BOTTOM]);
495 /* omnipresent check */
496 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
497 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
498 smenu->entries[WO_OMNIPRESENT]->rtext = GetShortcutKey(wKeyBindings[WKBD_OMNIPRESENT]);
500 smenu->flags.realized = 0;
501 wMenuRealize(smenu);
504 static void updateMaximizeMenu(WMenu * menu, WWindow * wwin)
506 WMenu *smenu = menu->cascades[menu->entries[MC_OTHERMAX]->cascade];
507 int i;
509 for (i = 0; i < smenu->entry_no; i++) {
510 smenu->entries[i]->clientdata = wwin;
511 smenu->entries[i]->rtext = GetShortcutKey(wKeyBindings[menu_maximize_entries[i].shortcut_idx]);
514 smenu->flags.realized = 0;
515 wMenuRealize(smenu);
518 static WMenu *makeWorkspaceMenu(WScreen * scr)
520 WMenu *menu;
522 menu = wMenuCreate(scr, NULL, False);
523 if (!menu) {
524 wwarning(_("could not create submenu for window menu"));
525 return NULL;
528 updateWorkspaceMenu(menu);
531 * The Workspace Menu is made visible in the screen structure because
532 * it is updated when there is a change on workspaces. This was done
533 * to be efficient, avoiding re-generating completely the window menu
534 * and its sub-menus every time it is needed.
536 scr->workspace_submenu = menu;
538 return menu;
541 static WMenu *makeOptionsMenu(WScreen * scr)
543 WMenu *menu;
544 WMenuEntry *entry;
545 int i;
547 menu = wMenuCreate(scr, NULL, False);
548 if (!menu) {
549 wwarning(_("could not create submenu for window menu"));
550 return NULL;
553 for (i = 0; i < wlengthof(menu_options_entries); i++) {
554 entry = wMenuAddCallback(menu, _(menu_options_entries[i]), execWindowOptionCommand, NULL);
555 entry->flags.indicator = 1;
556 entry->flags.indicator_type = MI_CHECK;
559 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
560 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
561 entry->flags.indicator = 1;
564 return menu;
567 static WMenu *makeMaximizeMenu(WScreen * scr)
569 WMenu *menu;
570 int i;
572 menu = wMenuCreate(scr, NULL, False);
573 if (!menu) {
574 wwarning(_("could not create submenu for window menu"));
575 return NULL;
578 for (i = 0; i < wlengthof(menu_maximize_entries); i++)
579 wMenuAddCallback(menu, _(menu_maximize_entries[i].label), execMaximizeCommand, NULL);
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;