changed indentation to use spaces only
[wmaker-crm.git] / src / winmenu.c
blobec700d594c4735f9e249c4d8803bde57a70354fa
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
34 #include "actions.h"
35 #include "menu.h"
36 #include "funcs.h"
37 #include "window.h"
38 #include "client.h"
39 #include "application.h"
40 #include "keybind.h"
41 #include "framewin.h"
42 #include "workspace.h"
43 #include "winspector.h"
44 #include "dialog.h"
45 #include "stacking.h"
46 #include "icon.h"
49 #define MC_MAXIMIZE 0
50 #define MC_MINIATURIZE 1
51 #define MC_SHADE 2
52 #define MC_HIDE 3
53 #define MC_HIDE_OTHERS 4
54 #define MC_MOVERESIZE 5
55 #define MC_SELECT 6
56 #define MC_DUMMY_MOVETO 7
57 #define MC_PROPERTIES 8
58 #define MC_OPTIONS 9
59 #define MC_SHORTCUT 9
61 #define MC_CLOSE 10
62 #define MC_KILL 11
65 #define WO_KEEP_ON_TOP 0
66 #define WO_KEEP_AT_BOTTOM 1
67 #define WO_OMNIPRESENT 2
68 #define WO_ENTRIES 3
70 /**** Global data ***/
71 extern Time LastTimestamp;
72 extern Atom _XA_WM_DELETE_WINDOW;
73 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
75 extern WShortKey wKeyBindings[WKBD_LAST];
77 extern WPreferences wPreferences;
79 static void updateOptionsMenu(WMenu *menu, WWindow *wwin);
81 static void
82 execWindowOptionCommand(WMenu *menu, WMenuEntry *entry)
84 WWindow *wwin = (WWindow*)entry->clientdata;
86 switch (entry->order) {
87 case WO_KEEP_ON_TOP:
88 if(wwin->frame->core->stacking->window_level!=WMFloatingLevel)
89 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
90 else
91 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
92 break;
94 case WO_KEEP_AT_BOTTOM:
95 if(wwin->frame->core->stacking->window_level!=WMSunkenLevel)
96 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
97 else
98 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
99 break;
101 case WO_OMNIPRESENT:
102 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
103 break;
108 static void
109 execMenuCommand(WMenu *menu, WMenuEntry *entry)
111 WWindow *wwin = (WWindow*)entry->clientdata;
112 WApplication *wapp;
114 CloseWindowMenu(menu->frame->screen_ptr);
116 switch (entry->order) {
117 case MC_CLOSE:
118 /* send delete message */
119 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
120 break;
122 case MC_KILL:
123 wretain(wwin);
124 if (wPreferences.dont_confirm_kill
125 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
126 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
127 _("Yes"), _("No"), NULL)==WAPRDefault) {
128 if (!wwin->flags.destroyed)
129 wClientKill(wwin);
131 wrelease(wwin);
132 break;
134 case MC_MINIATURIZE:
135 if (wwin->flags.miniaturized) {
136 wDeiconifyWindow(wwin);
137 } else{
138 if (wwin->protocols.MINIATURIZE_WINDOW) {
139 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
140 LastTimestamp);
141 } else {
142 wIconifyWindow(wwin);
145 break;
147 case MC_MAXIMIZE:
148 if (wwin->flags.maximized)
149 wUnmaximizeWindow(wwin);
150 else
151 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
152 break;
154 case MC_SHADE:
155 if (wwin->flags.shaded)
156 wUnshadeWindow(wwin);
157 else
158 wShadeWindow(wwin);
159 break;
161 case MC_SELECT:
162 if (!wwin->flags.miniaturized)
163 wSelectWindow(wwin, !wwin->flags.selected);
164 else
165 wIconSelect(wwin->icon);
166 break;
168 case MC_MOVERESIZE:
169 wKeyboardMoveResizeWindow(wwin);
170 break;
172 case MC_PROPERTIES:
173 wShowInspectorForWindow(wwin);
174 break;
176 case MC_HIDE:
177 wapp = wApplicationOf(wwin->main_window);
178 wHideApplication(wapp);
179 break;
181 case MC_HIDE_OTHERS:
182 wHideOtherApplications(wwin);
183 break;
189 static void
190 switchWSCommand(WMenu *menu, WMenuEntry *entry)
192 WWindow *wwin = (WWindow*)entry->clientdata;
194 wSelectWindow(wwin, False);
195 wWindowChangeWorkspace(wwin, entry->order);
199 static void
200 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
202 WWindow *wwin = (WWindow*)entry->clientdata;
203 WScreen *scr = wwin->screen_ptr;
204 int index = entry->order-WO_ENTRIES;
206 if (scr->shortcutWindows[index]) {
207 WMFreeArray(scr->shortcutWindows[index]);
208 scr->shortcutWindows[index] = NULL;
211 if (wwin->flags.selected && scr->selected_windows) {
212 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
213 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
214 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
215 } else {
216 scr->shortcutWindows[index] = WMCreateArray(4);
217 WMAddToArray(scr->shortcutWindows[index], wwin);
220 wSelectWindow(wwin, !wwin->flags.selected);
221 XFlush(dpy);
222 wusleep(3000);
223 wSelectWindow(wwin, !wwin->flags.selected);
224 XFlush(dpy);
228 static void
229 updateWorkspaceMenu(WMenu *menu)
231 WScreen *scr = menu->frame->screen_ptr;
232 char title[MAX_WORKSPACENAME_WIDTH+1];
233 int i;
235 if (!menu)
236 return;
238 for (i=0; i<scr->workspace_count; i++) {
239 if (i < menu->entry_no) {
240 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
241 wfree(menu->entries[i]->text);
242 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
243 title[MAX_WORKSPACENAME_WIDTH] = 0;
244 menu->entries[i]->text = wstrdup(title);
245 menu->flags.realized = 0;
247 } else {
248 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
249 title[MAX_WORKSPACENAME_WIDTH] = 0;
251 wMenuAddCallback(menu, title, switchWSCommand, NULL);
253 menu->flags.realized = 0;
257 if (!menu->flags.realized)
258 wMenuRealize(menu);
262 static void
263 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
265 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
266 int i;
267 char *buffer;
268 int buflen;
269 KeyCode kcode;
271 if (!smenu)
272 return;
274 buflen = strlen(_("Set Shortcut"))+16;
275 buffer = wmalloc(buflen);
277 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
278 char *tmp;
279 int shortcutNo = i-WO_ENTRIES;
280 WMenuEntry *entry = smenu->entries[i];
281 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
283 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo+1);
285 if (!shortSelWindows) {
286 entry->flags.indicator_on = 0;
287 } else {
288 entry->flags.indicator_on = 1;
289 if (WMCountInArray(shortSelWindows, wwin))
290 entry->flags.indicator_type = MI_DIAMOND;
291 else
292 entry->flags.indicator_type = MI_CHECK;
295 if (strcmp(buffer, entry->text)!=0) {
296 wfree(entry->text);
297 entry->text = wstrdup(buffer);
298 smenu->flags.realized = 0;
301 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
303 if (kcode) {
304 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
305 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
306 if (entry->rtext)
307 wfree(entry->rtext);
308 entry->rtext = wstrdup(tmp);
309 smenu->flags.realized = 0;
311 wMenuSetEnabled(smenu, i, True);
312 } else {
313 wMenuSetEnabled(smenu, i, False);
314 if (entry->rtext) {
315 wfree(entry->rtext);
316 entry->rtext = NULL;
317 smenu->flags.realized = 0;
320 entry->clientdata = wwin;
322 wfree(buffer);
323 if (!smenu->flags.realized)
324 wMenuRealize(smenu);
328 static void
329 updateOptionsMenu(WMenu *menu, WWindow *wwin)
331 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
333 /* keep on top check */
334 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
335 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
336 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
337 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
339 /* keep at bottom check */
340 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
341 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
342 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
343 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
345 /* omnipresent check */
346 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
347 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
349 smenu->flags.realized = 0;
350 wMenuRealize(smenu);
354 static WMenu*
355 makeWorkspaceMenu(WScreen *scr)
357 WMenu *menu;
359 menu = wMenuCreate(scr, NULL, False);
360 if (!menu) {
361 wwarning(_("could not create submenu for window menu"));
362 return NULL;
365 updateWorkspaceMenu(menu);
367 return menu;
371 static WMenu*
372 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
375 WMenu *menu;
377 int i;
379 menu = wMenuCreate(scr, NULL, False);
380 if (!menu) {
381 wwarning(_("could not create submenu for window menu"));
382 return NULL;
386 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
387 WMenuEntry *entry;
388 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
390 entry->flags.indicator = 1;
393 return menu;
398 static WMenu*
399 makeOptionsMenu(WScreen *scr)
401 WMenu *menu;
402 WMenuEntry *entry;
404 menu = wMenuCreate(scr, NULL, False);
405 if (!menu) {
406 wwarning(_("could not create submenu for window menu"));
407 return NULL;
410 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
411 NULL);
412 entry->flags.indicator = 1;
413 entry->flags.indicator_type = MI_CHECK;
415 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
416 NULL);
417 entry->flags.indicator = 1;
418 entry->flags.indicator_type = MI_CHECK;
420 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
421 NULL);
422 entry->flags.indicator = 1;
423 entry->flags.indicator_type = MI_CHECK;
425 return menu;
429 static WMenu*
430 createWindowMenu(WScreen *scr)
432 WMenu *menu;
433 KeyCode kcode;
434 WMenuEntry *entry;
435 char *tmp;
437 menu = wMenuCreate(scr, NULL, False);
439 * Warning: If you make some change that affects the order of the
440 * entries, you must update the command #defines in the top of
441 * this file.
443 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
444 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
445 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
447 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
448 entry->rtext = wstrdup(tmp);
451 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
452 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
453 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
455 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
456 entry->rtext = wstrdup(tmp);
459 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
460 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
461 kcode = wKeyBindings[WKBD_SHADE].keycode;
463 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
464 entry->rtext = wstrdup(tmp);
467 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
468 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
469 kcode = wKeyBindings[WKBD_HIDE].keycode;
471 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
472 entry->rtext = wstrdup(tmp);
475 entry = wMenuAddCallback(menu, _("Hide Others"), execMenuCommand, NULL);
476 if (wKeyBindings[WKBD_HIDE_OTHERS].keycode!=0) {
477 kcode = wKeyBindings[WKBD_HIDE_OTHERS].keycode;
479 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
480 entry->rtext = wstrdup(tmp);
483 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
484 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
485 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
487 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
488 entry->rtext = wstrdup(tmp);
491 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
492 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
493 kcode = wKeyBindings[WKBD_SELECT].keycode;
495 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
496 entry->rtext = wstrdup(tmp);
499 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
500 scr->workspace_submenu = makeWorkspaceMenu(scr);
501 if (scr->workspace_submenu)
502 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
504 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
506 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
507 wMenuEntrySetCascade(menu, entry,
508 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
511 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
512 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
515 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
516 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
517 kcode = wKeyBindings[WKBD_CLOSE].keycode;
518 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
519 entry->rtext = wstrdup(tmp);
522 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
524 return menu;
528 void
529 CloseWindowMenu(WScreen *scr)
531 if (scr->window_menu) {
532 if (scr->window_menu->flags.mapped)
533 wMenuUnmap(scr->window_menu);
535 if (scr->window_menu->entries[0]->clientdata) {
536 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
538 wwin->flags.menu_open_for_me = 0;
540 scr->window_menu->entries[0]->clientdata = NULL;
546 static void
547 updateMenuForWindow(WMenu *menu, WWindow *wwin)
549 WApplication *wapp = wApplicationOf(wwin->main_window);
550 WScreen *scr = wwin->screen_ptr;
551 int i;
553 updateOptionsMenu(menu, wwin);
555 updateMakeShortcutMenu(menu, wwin);
557 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
558 && !WFLAGP(wapp->main_window_desc, no_appicon));
560 wMenuSetEnabled(menu, MC_CLOSE,
561 (wwin->protocols.DELETE_WINDOW
562 && !WFLAGP(wwin, no_closable)));
564 if (wwin->flags.miniaturized) {
565 static char *text = NULL;
566 if (!text) text = _("Deminiaturize");
568 menu->entries[MC_MINIATURIZE]->text = text;
569 } else {
570 static char *text = NULL;
571 if (!text) text = _("Miniaturize");
573 menu->entries[MC_MINIATURIZE]->text = text;
576 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
578 if (wwin->flags.maximized) {
579 static char *text = NULL;
580 if (!text) text = _("Unmaximize");
582 menu->entries[MC_MAXIMIZE]->text = text;
583 } else {
584 static char *text = NULL;
585 if (!text) text = _("Maximize");
587 menu->entries[MC_MAXIMIZE]->text = text;
589 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
592 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
593 && !wwin->flags.miniaturized);
595 if (wwin->flags.shaded) {
596 static char *text = NULL;
597 if (!text) text = _("Unshade");
599 menu->entries[MC_SHADE]->text = text;
600 } else {
601 static char *text = NULL;
602 if (!text) text = _("Shade");
604 menu->entries[MC_SHADE]->text = text;
607 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
608 && !wwin->flags.miniaturized);
610 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
612 if (!wwin->flags.inspector_open) {
613 wMenuSetEnabled(menu, MC_PROPERTIES, True);
614 } else {
615 wMenuSetEnabled(menu, MC_PROPERTIES, False);
618 /* set the client data of the entries to the window */
619 for (i = 0; i < menu->entry_no; i++) {
620 menu->entries[i]->clientdata = wwin;
623 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
624 scr->workspace_submenu->entries[i]->clientdata = wwin;
625 if (i == scr->current_workspace) {
626 wMenuSetEnabled(scr->workspace_submenu, i, False);
627 } else {
628 wMenuSetEnabled(scr->workspace_submenu, i, True);
632 menu->flags.realized = 0;
633 wMenuRealize(menu);
637 void
638 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
640 WMenu *menu;
641 WScreen *scr = wwin->screen_ptr;
643 wwin->flags.menu_open_for_me = 1;
645 if (!scr->window_menu) {
646 scr->window_menu = createWindowMenu(scr);
648 /* hack to save some memory allocation/deallocation */
649 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
650 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
651 wfree(scr->window_menu->entries[MC_SHADE]->text);
652 } else {
653 updateWorkspaceMenu(scr->workspace_submenu);
656 menu = scr->window_menu;
657 if (menu->flags.mapped) {
658 wMenuUnmap(menu);
659 if (menu->entries[0]->clientdata==wwin) {
660 return;
664 updateMenuForWindow(menu, wwin);
666 x -= menu->frame->core->width/2;
667 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
668 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
669 if (x < wwin->frame_x)
670 x = wwin->frame_x;
672 if (!wwin->flags.internal_window)
673 wMenuMapAt(menu, x, y, keyboard);
677 void
678 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
680 WMenu *menu;
681 WScreen *scr = wwin->screen_ptr;
683 wwin->flags.menu_open_for_me = 1;
685 if (!scr->window_menu) {
686 scr->window_menu = createWindowMenu(scr);
688 /* hack to save some memory allocation/deallocation */
689 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
690 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
691 wfree(scr->window_menu->entries[MC_SHADE]->text);
692 } else {
693 updateWorkspaceMenu(scr->workspace_submenu);
696 menu = scr->window_menu;
697 if (menu->flags.mapped) {
698 wMenuUnmap(menu);
699 if (menu->entries[0]->clientdata==wwin) {
700 return;
704 updateMenuForWindow(menu, wwin);
706 x -= menu->frame->core->width/2;
708 wMenuMapAt(menu, x, y, False);