Variable last_workspace moved to workspace object in global namespace
[wmaker-crm.git] / src / winmenu.c
blob3dc3730552f619a2d5083623be5e5d13110f16c8
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 #define MC_MAXIMIZE 0
52 #define MC_MINIATURIZE 1
53 #define MC_SHADE 2
54 #define MC_HIDE 3
55 #define MC_MOVERESIZE 4
56 #define MC_SELECT 5
57 #define MC_DUMMY_MOVETO 6
58 #define MC_PROPERTIES 7
59 #define MC_OPTIONS 8
60 #define MC_SHORTCUT 8
61 #define MC_RELAUNCH 9
63 #define MC_CLOSE 10
64 #define MC_KILL 11
66 #define WO_KEEP_ON_TOP 0
67 #define WO_KEEP_AT_BOTTOM 1
68 #define WO_OMNIPRESENT 2
69 #define WO_ENTRIES 3
71 /**** Global data ***/
72 extern WShortKey wKeyBindings[WKBD_LAST];
75 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
77 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
79 WWindow *wwin = (WWindow *) entry->clientdata;
81 switch (entry->order) {
82 case WO_KEEP_ON_TOP:
83 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
84 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
85 else
86 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
87 break;
89 case WO_KEEP_AT_BOTTOM:
90 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
91 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
92 else
93 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
94 break;
96 case WO_OMNIPRESENT:
97 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
98 break;
102 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
104 WWindow *wwin = (WWindow *) entry->clientdata;
105 WApplication *wapp;
107 CloseWindowMenu(menu->frame->screen_ptr);
109 switch (entry->order) {
110 case MC_CLOSE:
111 /* send delete message */
112 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
113 w_global.timestamp.last_event);
114 break;
116 case MC_KILL:
117 wretain(wwin);
118 if (wPreferences.dont_confirm_kill
119 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
121 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
122 _("Yes"), _("No"), NULL) == WAPRDefault) {
123 if (!wwin->flags.destroyed)
124 wClientKill(wwin);
126 wrelease(wwin);
127 break;
129 case MC_MINIATURIZE:
130 if (wwin->flags.miniaturized) {
131 wDeiconifyWindow(wwin);
132 } else {
133 if (wwin->protocols.MINIATURIZE_WINDOW) {
134 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
135 w_global.timestamp.last_event);
136 } else {
137 wIconifyWindow(wwin);
140 break;
142 case MC_MAXIMIZE:
143 if (wwin->flags.maximized)
144 wUnmaximizeWindow(wwin);
145 else
146 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
147 break;
149 case MC_SHADE:
150 if (wwin->flags.shaded)
151 wUnshadeWindow(wwin);
152 else
153 wShadeWindow(wwin);
154 break;
156 case MC_SELECT:
157 if (!wwin->flags.miniaturized)
158 wSelectWindow(wwin, !wwin->flags.selected);
159 else
160 wIconSelect(wwin->icon);
161 break;
163 case MC_MOVERESIZE:
164 wKeyboardMoveResizeWindow(wwin);
165 break;
167 case MC_PROPERTIES:
168 wShowInspectorForWindow(wwin);
169 break;
171 case MC_RELAUNCH:
172 (void) RelaunchWindow(wwin);
173 break;
175 case MC_HIDE:
176 wapp = wApplicationOf(wwin->main_window);
177 wHideApplication(wapp);
178 break;
182 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
184 WWindow *wwin = (WWindow *) entry->clientdata;
186 wSelectWindow(wwin, False);
187 wWindowChangeWorkspace(wwin, entry->order);
190 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
192 WWindow *wwin = (WWindow *) entry->clientdata;
193 WScreen *scr = wwin->screen_ptr;
194 int index = entry->order - WO_ENTRIES;
196 if (scr->shortcutWindows[index]) {
197 WMFreeArray(scr->shortcutWindows[index]);
198 scr->shortcutWindows[index] = NULL;
201 if (wwin->flags.selected && scr->selected_windows) {
202 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
203 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
204 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
205 } else {
206 scr->shortcutWindows[index] = WMCreateArray(4);
207 WMAddToArray(scr->shortcutWindows[index], wwin);
210 wSelectWindow(wwin, !wwin->flags.selected);
211 XFlush(dpy);
212 wusleep(3000);
213 wSelectWindow(wwin, !wwin->flags.selected);
214 XFlush(dpy);
217 static void updateWorkspaceMenu(WMenu * menu)
219 WScreen *scr = menu->frame->screen_ptr;
220 char title[MAX_WORKSPACENAME_WIDTH + 1];
221 WMenuEntry *entry;
222 int i;
224 for (i = 0; i < w_global.workspace.count; i++) {
225 if (i < menu->entry_no) {
227 entry = menu->entries[i];
228 if (strcmp(entry->text, scr->workspaces[i]->name) != 0) {
229 wfree(entry->text);
230 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
231 title[MAX_WORKSPACENAME_WIDTH] = 0;
232 menu->entries[i]->text = wstrdup(title);
233 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
234 menu->flags.realized = 0;
236 } else {
237 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
238 title[MAX_WORKSPACENAME_WIDTH] = 0;
240 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
241 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
243 menu->flags.realized = 0;
246 /* workspace shortcut labels */
247 if (i / 10 == scr->current_workspace / 10)
248 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
249 else
250 entry->rtext = NULL;
253 if (!menu->flags.realized)
254 wMenuRealize(menu);
257 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
259 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
260 int i;
261 char *buffer;
262 int buflen;
263 KeyCode kcode;
265 if (!smenu)
266 return;
268 buflen = strlen(_("Set Shortcut")) + 16;
269 buffer = wmalloc(buflen);
271 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
272 char *tmp;
273 int shortcutNo = i - WO_ENTRIES;
274 WMenuEntry *entry = smenu->entries[i];
275 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
277 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
279 if (!shortSelWindows) {
280 entry->flags.indicator_on = 0;
281 } else {
282 entry->flags.indicator_on = 1;
283 if (WMCountInArray(shortSelWindows, wwin))
284 entry->flags.indicator_type = MI_DIAMOND;
285 else
286 entry->flags.indicator_type = MI_CHECK;
289 if (strcmp(buffer, entry->text) != 0) {
290 wfree(entry->text);
291 entry->text = wstrdup(buffer);
292 smenu->flags.realized = 0;
295 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
297 if (kcode) {
298 if ((tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]))
299 && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) {
300 if (entry->rtext)
301 wfree(entry->rtext);
302 entry->rtext = tmp;
303 smenu->flags.realized = 0;
305 wMenuSetEnabled(smenu, i, True);
306 } else {
307 wMenuSetEnabled(smenu, i, False);
308 if (entry->rtext) {
309 wfree(entry->rtext);
310 entry->rtext = NULL;
311 smenu->flags.realized = 0;
314 entry->clientdata = wwin;
316 wfree(buffer);
317 if (!smenu->flags.realized)
318 wMenuRealize(smenu);
321 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
323 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
325 /* keep on top check */
326 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
327 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
328 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
329 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
331 /* keep at bottom check */
332 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
333 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
334 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
335 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
337 /* omnipresent check */
338 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
339 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
341 smenu->flags.realized = 0;
342 wMenuRealize(smenu);
345 static WMenu *makeWorkspaceMenu(WScreen * scr)
347 WMenu *menu;
349 menu = wMenuCreate(scr, NULL, False);
350 if (!menu) {
351 wwarning(_("could not create submenu for window menu"));
352 return NULL;
355 updateWorkspaceMenu(menu);
357 return menu;
360 static WMenu *makeMakeShortcutMenu(WScreen * scr, WMenu * menu)
363 WMenu *menu;
365 int i;
367 menu = wMenuCreate(scr, NULL, False);
368 if (!menu) {
369 wwarning(_("could not create submenu for window menu"));
370 return NULL;
374 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
375 WMenuEntry *entry;
376 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
378 entry->flags.indicator = 1;
381 return menu;
384 static WMenu *makeOptionsMenu(WScreen * scr)
386 WMenu *menu;
387 WMenuEntry *entry;
389 menu = wMenuCreate(scr, NULL, False);
390 if (!menu) {
391 wwarning(_("could not create submenu for window menu"));
392 return NULL;
395 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
396 entry->flags.indicator = 1;
397 entry->flags.indicator_type = MI_CHECK;
399 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
400 entry->flags.indicator = 1;
401 entry->flags.indicator_type = MI_CHECK;
403 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
404 entry->flags.indicator = 1;
405 entry->flags.indicator_type = MI_CHECK;
407 return menu;
410 static WMenu *createWindowMenu(WScreen * scr)
412 WMenu *menu;
413 WMenuEntry *entry;
415 menu = wMenuCreate(scr, NULL, False);
417 * Warning: If you make some change that affects the order of the
418 * entries, you must update the command #defines in the top of
419 * this file.
421 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
422 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
424 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
425 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
427 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
428 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
430 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
431 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
433 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
434 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
436 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
437 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
439 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
440 scr->workspace_submenu = makeWorkspaceMenu(scr);
441 if (scr->workspace_submenu)
442 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
444 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
446 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
447 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
450 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
451 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
454 entry = wMenuAddCallback(menu, _("Launch"), execMenuCommand, NULL);
455 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
457 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
458 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
460 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
462 return menu;
465 void CloseWindowMenu(WScreen * scr)
467 if (scr->window_menu) {
468 if (scr->window_menu->flags.mapped)
469 wMenuUnmap(scr->window_menu);
471 if (scr->window_menu->entries[0]->clientdata) {
472 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
474 wwin->flags.menu_open_for_me = 0;
476 scr->window_menu->entries[0]->clientdata = NULL;
480 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
482 WApplication *wapp = wApplicationOf(wwin->main_window);
483 WScreen *scr = wwin->screen_ptr;
484 int i;
486 updateOptionsMenu(menu, wwin);
488 updateMakeShortcutMenu(menu, wwin);
490 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
492 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
494 if (wwin->flags.miniaturized) {
495 static char *text = NULL;
496 if (!text)
497 text = _("Deminiaturize");
499 menu->entries[MC_MINIATURIZE]->text = text;
500 } else {
501 static char *text = NULL;
502 if (!text)
503 text = _("Miniaturize");
505 menu->entries[MC_MINIATURIZE]->text = text;
508 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
510 if (wwin->flags.maximized) {
511 static char *text = NULL;
512 if (!text)
513 text = _("Unmaximize");
515 menu->entries[MC_MAXIMIZE]->text = text;
516 } else {
517 static char *text = NULL;
518 if (!text)
519 text = _("Maximize");
521 menu->entries[MC_MAXIMIZE]->text = text;
523 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
525 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
526 && !wwin->flags.miniaturized);
528 if (wwin->flags.shaded) {
529 static char *text = NULL;
530 if (!text)
531 text = _("Unshade");
533 menu->entries[MC_SHADE]->text = text;
534 } else {
535 static char *text = NULL;
536 if (!text)
537 text = _("Shade");
539 menu->entries[MC_SHADE]->text = text;
542 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
543 && !wwin->flags.miniaturized);
545 if (wwin->flags.selected) {
546 static char *text = NULL;
547 if (!text)
548 text = _("Deselect");
550 menu->entries[MC_SELECT]->text = text;
551 } else {
552 static char *text = NULL;
553 if (!text)
554 text = _("Select");
556 menu->entries[MC_SELECT]->text = text;
559 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
561 if (!wwin->flags.inspector_open) {
562 wMenuSetEnabled(menu, MC_PROPERTIES, True);
563 } else {
564 wMenuSetEnabled(menu, MC_PROPERTIES, False);
567 /* set the client data of the entries to the window */
568 for (i = 0; i < menu->entry_no; i++) {
569 menu->entries[i]->clientdata = wwin;
572 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
573 scr->workspace_submenu->entries[i]->clientdata = wwin;
574 if (i == scr->current_workspace)
575 wMenuSetEnabled(scr->workspace_submenu, i, False);
576 else
577 wMenuSetEnabled(scr->workspace_submenu, i, True);
580 menu->flags.realized = 0;
581 wMenuRealize(menu);
584 static WMenu *open_window_menu_core(WWindow *wwin, int x, int y)
586 WScreen *scr = wwin->screen_ptr;
587 WMenu *menu;
589 wwin->flags.menu_open_for_me = 1;
591 if (!scr->window_menu) {
592 scr->window_menu = createWindowMenu(scr);
594 /* hack to save some memory allocation/deallocation */
595 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
596 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
597 wfree(scr->window_menu->entries[MC_SHADE]->text);
598 wfree(scr->window_menu->entries[MC_SELECT]->text);
599 } else {
600 updateWorkspaceMenu(scr->workspace_submenu);
603 menu = scr->window_menu;
604 if (menu->flags.mapped) {
605 wMenuUnmap(menu);
606 if (menu->entries[0]->clientdata == wwin)
607 return NULL;
610 updateMenuForWindow(menu, wwin);
612 return menu;
615 static void prepare_menu_position(WMenu *menu, int x, int y)
617 WMRect rect;
619 rect = wGetRectForHead(menu->frame->screen_ptr,
620 wGetHeadForPointerLocation(menu->frame->screen_ptr));
621 if (x < rect.pos.x - menu->frame->core->width / 2)
622 x = rect.pos.x - menu->frame->core->width / 2;
623 if (y < rect.pos.y)
624 y = rect.pos.y;
627 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
629 WMenu *menu;
631 menu = open_window_menu_core(wwin, x, y);
632 if (!menu)
633 return;
635 /* Specific menu position */
636 x -= menu->frame->core->width / 2;
637 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
638 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
639 if (x < wwin->frame_x)
640 x = wwin->frame_x;
642 /* Common menu position */
643 prepare_menu_position(menu, x, y);
645 if (!wwin->flags.internal_window)
646 wMenuMapAt(menu, x, y, keyboard);
649 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
651 int i;
652 WMenu *menu;
653 WScreen *scr = wwin->screen_ptr;
655 menu = open_window_menu_core(wwin, x, y);
656 if (!menu)
657 return;
659 /* Specific menu position */
660 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
661 scr->workspace_submenu->entries[i]->clientdata = wwin;
662 wMenuSetEnabled(scr->workspace_submenu, i, True);
665 x -= menu->frame->core->width / 2;
667 /* Common menu position */
668 prepare_menu_position(menu, x, y);
670 if (!wwin->flags.internal_window)
671 wMenuMapAt(menu, x, y, keyboard);
674 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
676 WMenu *menu;
678 menu = open_window_menu_core(wwin, x, y);
679 if (!menu)
680 return;
682 x -= menu->frame->core->width / 2;
684 wMenuMapAt(menu, x, y, False);
687 void DestroyWindowMenu(WScreen *scr)
689 if (scr->window_menu) {
690 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
691 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
692 scr->window_menu->entries[MC_SHADE]->text = NULL;
693 scr->window_menu->entries[MC_SELECT]->text = NULL;
694 wMenuDestroy(scr->window_menu, True);
695 scr->window_menu = NULL;