Make two text strings translatable
[wmaker-crm.git] / src / winmenu.c
blob5cf568b19e68aed3629b0bdcd574d27037bef061
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"
51 enum
53 MC_MAXIMIZE,
54 MC_OTHERMAX,
55 MC_MINIATURIZE,
56 MC_SHADE,
57 MC_HIDE,
58 MC_MOVERESIZE,
59 MC_SELECT,
60 MC_DUMMY_MOVETO,
61 MC_PROPERTIES,
62 MC_OPTIONS,
63 MC_RELAUNCH,
64 MC_CLOSE,
65 MC_KILL
68 enum
70 WO_KEEP_ON_TOP,
71 WO_KEEP_AT_BOTTOM,
72 WO_OMNIPRESENT,
73 WO_ENTRIES
76 static const struct {
77 const char *label;
78 unsigned int shortcut_idx;
79 int maxim_direction;
80 } menu_maximize_entries[] = {
81 { N_("Maximize vertically"), WKBD_VMAXIMIZE, MAX_VERTICAL },
82 { N_("Maximize horizontally"), WKBD_HMAXIMIZE, MAX_HORIZONTAL },
83 { N_("Maximize left half"), WKBD_LHMAXIMIZE, MAX_VERTICAL | MAX_LEFTHALF },
84 { N_("Maximize right half"), WKBD_RHMAXIMIZE, MAX_VERTICAL | MAX_RIGHTHALF },
85 { N_("Maximize top half"), WKBD_THMAXIMIZE, MAX_HORIZONTAL | MAX_TOPHALF },
86 { N_("Maximize bottom half"), WKBD_BHMAXIMIZE, MAX_HORIZONTAL | MAX_BOTTOMHALF },
87 { N_("Maximize left top corner"), WKBD_LTCMAXIMIZE, MAX_LEFTHALF | MAX_TOPHALF },
88 { N_("Maximize right top corner"), WKBD_RTCMAXIMIZE, MAX_RIGHTHALF | MAX_TOPHALF },
89 { N_("Maximize left bottom corner"), WKBD_LBCMAXIMIZE, MAX_LEFTHALF | MAX_BOTTOMHALF },
90 { N_("Maximize right bottom corner"), WKBD_RBCMAXIMIZE, MAX_RIGHTHALF | MAX_BOTTOMHALF },
91 { N_("Maximus: tiled maximization"), WKBD_MAXIMUS, MAX_MAXIMUS }
94 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
96 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
98 WWindow *wwin = (WWindow *) entry->clientdata;
100 /* Parameter not used, but tell the compiler that it is ok */
101 (void) menu;
103 switch (entry->order) {
104 case WO_KEEP_ON_TOP:
105 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
106 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
107 else
108 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
109 break;
111 case WO_KEEP_AT_BOTTOM:
112 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
113 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
114 else
115 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
116 break;
118 case WO_OMNIPRESENT:
119 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
120 break;
124 static void execMaximizeCommand(WMenu * menu, WMenuEntry * entry)
126 WWindow *wwin = (WWindow *) entry->clientdata;
128 /* Parameter not used, but tell the compiler that it is ok */
129 (void) menu;
131 handleMaximize(wwin, menu_maximize_entries[entry->order].maxim_direction);
134 static void updateUnmaximizeShortcut(WMenuEntry * entry, int flags)
136 int key;
138 switch (flags & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS)) {
139 case MAX_HORIZONTAL:
140 key = WKBD_HMAXIMIZE;
141 break;
143 case MAX_VERTICAL:
144 key = WKBD_VMAXIMIZE;
145 break;
147 case MAX_LEFTHALF | MAX_VERTICAL:
148 key = WKBD_LHMAXIMIZE;
149 break;
151 case MAX_RIGHTHALF | MAX_VERTICAL:
152 key = WKBD_RHMAXIMIZE;
153 break;
155 case MAX_TOPHALF | MAX_HORIZONTAL:
156 key = WKBD_THMAXIMIZE;
157 break;
159 case MAX_BOTTOMHALF | MAX_HORIZONTAL:
160 key = WKBD_BHMAXIMIZE;
161 break;
163 case MAX_LEFTHALF | MAX_TOPHALF:
164 key = WKBD_LTCMAXIMIZE;
165 break;
167 case MAX_RIGHTHALF | MAX_TOPHALF:
168 key = WKBD_RTCMAXIMIZE;
169 break;
171 case MAX_LEFTHALF | MAX_BOTTOMHALF:
172 key = WKBD_LBCMAXIMIZE;
173 break;
175 case MAX_RIGHTHALF | MAX_BOTTOMHALF:
176 key = WKBD_RBCMAXIMIZE;
177 break;
179 case MAX_MAXIMUS:
180 key = WKBD_MAXIMUS;
181 break;
183 default:
184 key = WKBD_MAXIMIZE;
185 break;
188 entry->rtext = GetShortcutKey(wKeyBindings[key]);
191 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
193 WWindow *wwin = (WWindow *) entry->clientdata;
194 WApplication *wapp;
196 CloseWindowMenu(menu->frame->screen_ptr);
198 switch (entry->order) {
199 case MC_CLOSE:
200 /* send delete message */
201 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
202 w_global.timestamp.last_event);
203 break;
205 case MC_KILL:
206 wretain(wwin);
207 if (wPreferences.dont_confirm_kill
208 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
210 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
211 _("Yes"), _("No"), NULL) == WAPRDefault) {
212 if (!wwin->flags.destroyed)
213 wClientKill(wwin);
215 wrelease(wwin);
216 break;
218 case MC_MINIATURIZE:
219 if (wwin->flags.miniaturized) {
220 wDeiconifyWindow(wwin);
221 } else {
222 if (wwin->protocols.MINIATURIZE_WINDOW) {
223 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
224 w_global.timestamp.last_event);
225 } else {
226 wIconifyWindow(wwin);
229 break;
231 case MC_MAXIMIZE:
232 if (wwin->flags.maximized)
233 wUnmaximizeWindow(wwin);
234 else
235 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
236 break;
238 case MC_SHADE:
239 if (wwin->flags.shaded)
240 wUnshadeWindow(wwin);
241 else
242 wShadeWindow(wwin);
243 break;
245 case MC_SELECT:
246 if (!wwin->flags.miniaturized)
247 wSelectWindow(wwin, !wwin->flags.selected);
248 else
249 wIconSelect(wwin->icon);
250 break;
252 case MC_MOVERESIZE:
253 wKeyboardMoveResizeWindow(wwin);
254 break;
256 case MC_PROPERTIES:
257 wShowInspectorForWindow(wwin);
258 break;
260 case MC_RELAUNCH:
261 (void) RelaunchWindow(wwin);
262 break;
264 case MC_HIDE:
265 wapp = wApplicationOf(wwin->main_window);
266 wHideApplication(wapp);
267 break;
271 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
273 WWindow *wwin = (WWindow *) entry->clientdata;
275 /* Parameter not used, but tell the compiler that it is ok */
276 (void) menu;
278 wSelectWindow(wwin, False);
279 wWindowChangeWorkspace(wwin, entry->order);
282 static void makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
284 WWindow *wwin = (WWindow *) entry->clientdata;
285 WScreen *scr = wwin->screen_ptr;
286 int index = entry->order - WO_ENTRIES;
288 /* Parameter not used, but tell the compiler that it is ok */
289 (void) menu;
291 if (w_global.shortcut.windows[index]) {
292 WMFreeArray(w_global.shortcut.windows[index]);
293 w_global.shortcut.windows[index] = NULL;
296 if (wwin->flags.selected && scr->selected_windows) {
297 w_global.shortcut.windows[index] = WMDuplicateArray(scr->selected_windows);
298 } else {
299 w_global.shortcut.windows[index] = WMCreateArray(4);
300 WMAddToArray(w_global.shortcut.windows[index], wwin);
303 wSelectWindow(wwin, !wwin->flags.selected);
304 XFlush(dpy);
305 wusleep(3000);
306 wSelectWindow(wwin, !wwin->flags.selected);
307 XFlush(dpy);
310 static void updateWorkspaceMenu(WMenu * menu)
312 char title[MAX_WORKSPACENAME_WIDTH + 1];
313 WMenuEntry *entry;
314 int i;
316 for (i = 0; i < w_global.workspace.count; i++) {
317 if (i < menu->entry_no) {
319 entry = menu->entries[i];
320 if (strcmp(entry->text, w_global.workspace.array[i]->name) != 0) {
321 wfree(entry->text);
322 strncpy(title, w_global.workspace.array[i]->name, MAX_WORKSPACENAME_WIDTH);
323 title[MAX_WORKSPACENAME_WIDTH] = 0;
324 menu->entries[i]->text = wstrdup(title);
325 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
326 menu->flags.realized = 0;
328 } else {
329 strncpy(title, w_global.workspace.array[i]->name, MAX_WORKSPACENAME_WIDTH);
330 title[MAX_WORKSPACENAME_WIDTH] = 0;
332 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
333 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
335 menu->flags.realized = 0;
338 /* workspace shortcut labels */
339 if (i / 10 == w_global.workspace.current / 10)
340 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
341 else
342 entry->rtext = NULL;
345 if (!menu->flags.realized)
346 wMenuRealize(menu);
349 static void updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
351 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
352 int i;
353 char *buffer;
354 int buflen;
355 KeyCode kcode;
357 if (!smenu)
358 return;
360 buflen = strlen(_("Set Shortcut")) + 16;
361 buffer = wmalloc(buflen);
363 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
364 int shortcutNo = i - WO_ENTRIES;
365 WMenuEntry *entry = smenu->entries[i];
366 WMArray *shortSelWindows = w_global.shortcut.windows[shortcutNo];
368 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
370 if (!shortSelWindows) {
371 entry->flags.indicator_on = 0;
372 } else {
373 entry->flags.indicator_on = 1;
374 if (WMCountInArray(shortSelWindows, wwin))
375 entry->flags.indicator_type = MI_DIAMOND;
376 else
377 entry->flags.indicator_type = MI_CHECK;
380 if (strcmp(buffer, entry->text) != 0) {
381 wfree(entry->text);
382 entry->text = wstrdup(buffer);
383 smenu->flags.realized = 0;
386 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
388 if (kcode) {
389 char *tmp;
391 tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]);
392 if (tmp == NULL) {
393 if (entry->rtext != NULL) {
394 /* There was a shortcut, but there is no more */
395 wfree(entry->rtext);
396 entry->rtext = NULL;
397 smenu->flags.realized = 0;
399 } else if (entry->rtext == NULL) {
400 /* There was no shortcut, but there is one now */
401 entry->rtext = tmp;
402 smenu->flags.realized = 0;
403 } else if (strcmp(tmp, entry->rtext) != 0) {
404 /* There was a shortcut, but it has changed */
405 wfree(entry->rtext);
406 entry->rtext = tmp;
407 smenu->flags.realized = 0;
408 } else {
409 /* There was a shortcut but it did not change */
410 wfree(tmp);
412 wMenuSetEnabled(smenu, i, True);
413 } else {
414 wMenuSetEnabled(smenu, i, False);
415 if (entry->rtext) {
416 wfree(entry->rtext);
417 entry->rtext = NULL;
418 smenu->flags.realized = 0;
421 entry->clientdata = wwin;
423 wfree(buffer);
424 if (!smenu->flags.realized)
425 wMenuRealize(smenu);
428 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
430 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
432 /* keep on top check */
433 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
434 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
435 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
436 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
438 /* keep at bottom check */
439 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
440 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
441 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
442 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
444 /* omnipresent check */
445 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
446 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
448 smenu->flags.realized = 0;
449 wMenuRealize(smenu);
452 static void updateMaximizeMenu(WMenu * menu, WWindow * wwin)
454 WMenu *smenu = menu->cascades[menu->entries[MC_OTHERMAX]->cascade];
455 int i;
457 for (i = 0; i < smenu->entry_no; i++) {
458 smenu->entries[i]->clientdata = wwin;
459 smenu->entries[i]->rtext = GetShortcutKey(wKeyBindings[menu_maximize_entries[i].shortcut_idx]);
462 smenu->flags.realized = 0;
463 wMenuRealize(smenu);
466 static WMenu *makeWorkspaceMenu(WScreen * scr)
468 WMenu *menu;
470 menu = wMenuCreate(scr, NULL, False);
471 if (!menu) {
472 wwarning(_("could not create submenu for window menu"));
473 return NULL;
476 updateWorkspaceMenu(menu);
478 return menu;
481 static WMenu *makeMakeShortcutMenu(WMenu *menu)
483 int i;
485 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
486 WMenuEntry *entry;
487 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
489 entry->flags.indicator = 1;
492 return menu;
495 static WMenu *makeOptionsMenu(WScreen * scr)
497 WMenu *menu;
498 WMenuEntry *entry;
500 menu = wMenuCreate(scr, NULL, False);
501 if (!menu) {
502 wwarning(_("could not create submenu for window menu"));
503 return NULL;
506 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
507 entry->flags.indicator = 1;
508 entry->flags.indicator_type = MI_CHECK;
510 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
511 entry->flags.indicator = 1;
512 entry->flags.indicator_type = MI_CHECK;
514 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
515 entry->flags.indicator = 1;
516 entry->flags.indicator_type = MI_CHECK;
518 return menu;
521 static WMenu *makeMaximizeMenu(WScreen * scr)
523 WMenu *menu;
524 int i;
526 menu = wMenuCreate(scr, NULL, False);
527 if (!menu) {
528 wwarning(_("could not create submenu for window menu"));
529 return NULL;
532 for (i = 0; i < wlengthof(menu_maximize_entries); i++)
533 wMenuAddCallback(menu, _(menu_maximize_entries[i].label), execMaximizeCommand, NULL);
535 return menu;
538 static WMenu *createWindowMenu(WScreen * scr)
540 WMenu *menu;
541 WMenuEntry *entry;
543 menu = wMenuCreate(scr, NULL, False);
545 * Warning: If you make some change that affects the order of the
546 * entries, you must update the command enum in the top of
547 * this file.
549 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
551 entry = wMenuAddCallback(menu, _("Other maximization"), NULL, NULL);
552 wMenuEntrySetCascade(menu, entry, makeMaximizeMenu(scr));
554 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
556 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
558 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
560 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
562 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
564 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
565 w_global.workspace.submenu = makeWorkspaceMenu(scr);
566 if (w_global.workspace.submenu)
567 wMenuEntrySetCascade(menu, entry, w_global.workspace.submenu);
569 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
571 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
572 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(makeOptionsMenu(scr)));
574 entry = wMenuAddCallback(menu, _("Launch"), execMenuCommand, NULL);
576 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
578 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
580 return menu;
583 void CloseWindowMenu(WScreen * scr)
585 if (scr->window_menu) {
586 if (scr->window_menu->flags.mapped)
587 wMenuUnmap(scr->window_menu);
589 if (scr->window_menu->entries[0]->clientdata) {
590 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
592 wwin->flags.menu_open_for_me = 0;
594 scr->window_menu->entries[0]->clientdata = NULL;
598 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
600 WApplication *wapp = wApplicationOf(wwin->main_window);
601 int i;
603 updateOptionsMenu(menu, wwin);
604 updateMaximizeMenu(menu, wwin);
606 updateMakeShortcutMenu(menu, wwin);
608 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
610 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
612 if (wwin->flags.miniaturized) {
613 static char *text = NULL;
614 if (!text)
615 text = _("Deminiaturize");
617 menu->entries[MC_MINIATURIZE]->text = text;
618 } else {
619 static char *text = NULL;
620 if (!text)
621 text = _("Miniaturize");
623 menu->entries[MC_MINIATURIZE]->text = text;
626 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
628 if (wwin->flags.maximized) {
629 static char *text = NULL;
630 if (!text)
631 text = _("Unmaximize");
633 menu->entries[MC_MAXIMIZE]->text = text;
634 updateUnmaximizeShortcut(menu->entries[MC_MAXIMIZE], wwin->flags.maximized);
635 } else {
636 static char *text = NULL;
637 if (!text)
638 text = _("Maximize");
640 menu->entries[MC_MAXIMIZE]->text = text;
641 menu->entries[MC_MAXIMIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
643 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
645 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
646 && !wwin->flags.miniaturized);
648 if (wwin->flags.shaded) {
649 static char *text = NULL;
650 if (!text)
651 text = _("Unshade");
653 menu->entries[MC_SHADE]->text = text;
654 } else {
655 static char *text = NULL;
656 if (!text)
657 text = _("Shade");
659 menu->entries[MC_SHADE]->text = text;
662 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
663 && !wwin->flags.miniaturized);
665 if (wwin->flags.selected) {
666 static char *text = NULL;
667 if (!text)
668 text = _("Deselect");
670 menu->entries[MC_SELECT]->text = text;
671 } else {
672 static char *text = NULL;
673 if (!text)
674 text = _("Select");
676 menu->entries[MC_SELECT]->text = text;
679 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
681 if (!wwin->flags.inspector_open) {
682 wMenuSetEnabled(menu, MC_PROPERTIES, True);
683 } else {
684 wMenuSetEnabled(menu, MC_PROPERTIES, False);
687 /* Update shortcut labels except for (Un)Maximize which is
688 * handled separately.
690 menu->entries[MC_MINIATURIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
691 menu->entries[MC_SHADE]->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
692 menu->entries[MC_HIDE]->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
693 menu->entries[MC_MOVERESIZE]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
694 menu->entries[MC_SELECT]->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
695 menu->entries[MC_RELAUNCH]->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
696 menu->entries[MC_CLOSE]->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
698 /* set the client data of the entries to the window */
699 for (i = 0; i < menu->entry_no; i++) {
700 menu->entries[i]->clientdata = wwin;
703 for (i = 0; i < w_global.workspace.submenu->entry_no; i++) {
704 w_global.workspace.submenu->entries[i]->clientdata = wwin;
706 if (i == w_global.workspace.current)
707 wMenuSetEnabled(w_global.workspace.submenu, i, False);
708 else
709 wMenuSetEnabled(w_global.workspace.submenu, i, True);
712 menu->flags.realized = 0;
713 wMenuRealize(menu);
716 static WMenu *open_window_menu_core(WWindow *wwin)
718 WScreen *scr = wwin->screen_ptr;
719 WMenu *menu;
721 wwin->flags.menu_open_for_me = 1;
723 if (!scr->window_menu) {
724 scr->window_menu = createWindowMenu(scr);
726 /* hack to save some memory allocation/deallocation */
727 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
728 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
729 wfree(scr->window_menu->entries[MC_SHADE]->text);
730 wfree(scr->window_menu->entries[MC_SELECT]->text);
731 } else {
732 updateWorkspaceMenu(w_global.workspace.submenu);
735 menu = scr->window_menu;
736 if (menu->flags.mapped) {
737 wMenuUnmap(menu);
738 if (menu->entries[0]->clientdata == wwin)
739 return NULL;
742 updateMenuForWindow(menu, wwin);
744 return menu;
747 static void prepare_menu_position(WMenu *menu, int *x, int *y)
749 WMRect rect;
751 rect = wGetRectForHead(menu->frame->screen_ptr,
752 wGetHeadForPointerLocation(menu->frame->screen_ptr));
753 if (*x < rect.pos.x - menu->frame->core->width / 2)
754 *x = rect.pos.x - menu->frame->core->width / 2;
755 if (*y < rect.pos.y)
756 *y = rect.pos.y;
759 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
761 WMenu *menu;
763 menu = open_window_menu_core(wwin);
764 if (!menu)
765 return;
767 /* Specific menu position */
768 x -= menu->frame->core->width / 2;
769 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
770 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
771 if (x < wwin->frame_x)
772 x = wwin->frame_x;
774 /* Common menu position */
775 prepare_menu_position(menu, &x, &y);
777 if (!wwin->flags.internal_window)
778 wMenuMapAt(menu, x, y, keyboard);
781 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
783 int i;
784 WMenu *menu;
786 menu = open_window_menu_core(wwin);
787 if (!menu)
788 return;
790 /* Specific menu position */
791 for (i = 0; i < w_global.workspace.submenu->entry_no; i++) {
792 w_global.workspace.submenu->entries[i]->clientdata = wwin;
793 wMenuSetEnabled(w_global.workspace.submenu, i, True);
796 x -= menu->frame->core->width / 2;
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 OpenMiniwindowMenu(WWindow * wwin, int x, int y)
807 WMenu *menu;
809 menu = open_window_menu_core(wwin);
810 if (!menu)
811 return;
813 x -= menu->frame->core->width / 2;
815 wMenuMapAt(menu, x, y, False);
818 void DestroyWindowMenu(WScreen *scr)
820 if (scr->window_menu) {
821 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
822 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
823 scr->window_menu->entries[MC_SHADE]->text = NULL;
824 scr->window_menu->entries[MC_SELECT]->text = NULL;
825 wMenuDestroy(scr->window_menu, True);
826 scr->window_menu = NULL;