- Replaced all free() with wfree() where appropriate
[wmaker-crm.git] / src / winmenu.c
blob35c0b4698a4280840adcb14cc9f6d46d953c3baa
1 /* winmenu.c - command menu for windows
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
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_MOVERESIZE 4
54 #define MC_SELECT 5
55 #define MC_DUMMY_MOVETO 6
56 #define MC_PROPERTIES 7
57 #define MC_OPTIONS 8
58 #define MC_SHORTCUT 8
60 #define MC_CLOSE 9
61 #define MC_KILL 10
64 #define WO_KEEP_ON_TOP 0
65 #define WO_KEEP_AT_BOTTOM 1
66 #define WO_OMNIPRESENT 2
67 #define WO_ENTRIES 3
69 /**** Global data ***/
70 extern Time LastTimestamp;
71 extern Atom _XA_WM_DELETE_WINDOW;
72 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
74 extern WShortKey wKeyBindings[WKBD_LAST];
76 extern WPreferences wPreferences;
78 static void updateOptionsMenu(WMenu *menu, WWindow *wwin);
80 static void
81 execWindowOptionCommand(WMenu *menu, WMenuEntry *entry)
83 WWindow *wwin = (WWindow*)entry->clientdata;
85 switch (entry->order) {
86 case WO_KEEP_ON_TOP:
87 if(wwin->frame->core->stacking->window_level!=WMFloatingLevel)
88 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
89 else
90 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
91 break;
93 case WO_KEEP_AT_BOTTOM:
94 if(wwin->frame->core->stacking->window_level!=WMSunkenLevel)
95 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
96 else
97 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
98 break;
100 case WO_OMNIPRESENT:
101 wwin->flags.omnipresent^=1;
102 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_WORKSPACE);
103 #ifdef GNOME_STUFF
104 wGNOMEUpdateClientStateHint(wwin, False);
105 #endif
106 break;
111 static void
112 execMenuCommand(WMenu *menu, WMenuEntry *entry)
114 WWindow *wwin = (WWindow*)entry->clientdata;
115 WApplication *wapp;
117 CloseWindowMenu(menu->frame->screen_ptr);
119 switch (entry->order) {
120 case MC_CLOSE:
121 /* send delete message */
122 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
123 break;
125 case MC_KILL:
126 wretain(wwin);
127 if (wPreferences.dont_confirm_kill
128 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
129 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
130 _("Yes"), _("No"), NULL)==WAPRDefault) {
131 if (!wwin->flags.destroyed)
132 wClientKill(wwin);
134 wrelease(wwin);
135 break;
137 case MC_MINIATURIZE:
138 if (wwin->flags.miniaturized) {
139 wDeiconifyWindow(wwin);
140 } else{
141 if (wwin->protocols.MINIATURIZE_WINDOW) {
142 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
143 LastTimestamp);
144 } else {
145 wIconifyWindow(wwin);
148 break;
150 case MC_MAXIMIZE:
151 if (wwin->flags.maximized)
152 wUnmaximizeWindow(wwin);
153 else
154 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
155 break;
157 case MC_SHADE:
158 if (wwin->flags.shaded)
159 wUnshadeWindow(wwin);
160 else
161 wShadeWindow(wwin);
162 break;
164 case MC_SELECT:
165 if (!wwin->flags.miniaturized)
166 wSelectWindow(wwin, !wwin->flags.selected);
167 else
168 wIconSelect(wwin->icon);
169 break;
171 case MC_MOVERESIZE:
172 wKeyboardMoveResizeWindow(wwin);
173 break;
175 case MC_PROPERTIES:
176 wShowInspectorForWindow(wwin);
177 break;
179 case MC_HIDE:
180 wapp = wApplicationOf(wwin->main_window);
181 wHideApplication(wapp);
182 break;
188 static void
189 switchWSCommand(WMenu *menu, WMenuEntry *entry)
191 WWindow *wwin = (WWindow*)entry->clientdata;
193 wSelectWindow(wwin, False);
194 wWindowChangeWorkspace(wwin, entry->order);
198 static void
199 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
201 WWindow *wwin = (WWindow*)entry->clientdata;
202 WScreen *scr = wwin->screen_ptr;
203 int index = entry->order-WO_ENTRIES;
205 if (scr->shortcutWindows[index]) {
206 WMFreeBag(scr->shortcutWindows[index]);
207 scr->shortcutWindows[index] = NULL;
210 if (wwin->flags.selected && scr->selected_windows) {
211 WMBag *selwin = scr->selected_windows;
212 int i;
214 scr->shortcutWindows[index] = WMCreateBag(4);
216 for (i = 0; i < WMGetBagItemCount(selwin); i++) {
217 WWindow *tmp = WMGetFromBag(selwin, i);
219 WMPutInBag(scr->shortcutWindows[index], tmp);
221 } else {
222 scr->shortcutWindows[index] = WMCreateBag(4);
224 WMPutInBag(scr->shortcutWindows[index], wwin);
227 wSelectWindow(wwin, !wwin->flags.selected);
228 XFlush(dpy);
229 wusleep(3000);
230 wSelectWindow(wwin, !wwin->flags.selected);
231 XFlush(dpy);
235 static void
236 updateWorkspaceMenu(WMenu *menu)
238 WScreen *scr = menu->frame->screen_ptr;
239 char title[MAX_WORKSPACENAME_WIDTH+1];
240 int i;
242 if (!menu)
243 return;
245 for (i=0; i<scr->workspace_count; i++) {
246 if (i < menu->entry_no) {
247 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
248 wfree(menu->entries[i]->text);
249 strcpy(title, scr->workspaces[i]->name);
250 menu->entries[i]->text = wstrdup(title);
251 menu->flags.realized = 0;
253 } else {
254 strcpy(title, scr->workspaces[i]->name);
256 wMenuAddCallback(menu, title, switchWSCommand, NULL);
258 menu->flags.realized = 0;
262 if (!menu->flags.realized)
263 wMenuRealize(menu);
267 static void
268 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
270 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
271 int i;
272 char *buffer;
273 KeyCode kcode;
275 if (!smenu)
276 return;
278 buffer = wmalloc(strlen(_("Set Shortcut"))+16);
280 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
281 char *tmp;
282 int shortcutNo = i-WO_ENTRIES;
283 WMenuEntry *entry = smenu->entries[i];
284 WMBag *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
286 sprintf(buffer, "%s %i", _("Set Shortcut"), shortcutNo+1);
288 if (!shortSelWindows) {
289 entry->flags.indicator_on = 0;
290 } else {
291 entry->flags.indicator_on = 1;
292 if (WMCountInBag(shortSelWindows, wwin))
293 entry->flags.indicator_type = MI_DIAMOND;
294 else
295 entry->flags.indicator_type = MI_CHECK;
298 if (strcmp(buffer, entry->text)!=0) {
299 wfree(entry->text);
300 entry->text = wstrdup(buffer);
301 smenu->flags.realized = 0;
304 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
306 if (kcode) {
307 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
308 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
309 if (entry->rtext)
310 wfree(entry->rtext);
311 entry->rtext = wstrdup(tmp);
312 smenu->flags.realized = 0;
314 wMenuSetEnabled(smenu, i, True);
315 } else {
316 wMenuSetEnabled(smenu, i, False);
317 if (entry->rtext) {
318 wfree(entry->rtext);
319 entry->rtext = NULL;
320 smenu->flags.realized = 0;
323 entry->clientdata = wwin;
325 wfree(buffer);
326 if (!smenu->flags.realized)
327 wMenuRealize(smenu);
331 static void
332 updateOptionsMenu(WMenu *menu, WWindow *wwin)
334 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
336 /* keep on top check */
337 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
338 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
339 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
340 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
342 /* keep at bottom check */
343 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
344 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
345 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
346 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
348 /* omnipresent check */
349 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
350 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
352 smenu->flags.realized = 0;
353 wMenuRealize(smenu);
357 static WMenu*
358 makeWorkspaceMenu(WScreen *scr)
360 WMenu *menu;
362 menu = wMenuCreate(scr, NULL, False);
363 if (!menu) {
364 wwarning(_("could not create submenu for window menu"));
365 return NULL;
368 updateWorkspaceMenu(menu);
370 return menu;
374 static WMenu*
375 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
378 WMenu *menu;
380 int i;
382 menu = wMenuCreate(scr, NULL, False);
383 if (!menu) {
384 wwarning(_("could not create submenu for window menu"));
385 return NULL;
389 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
390 WMenuEntry *entry;
392 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
394 entry->flags.indicator = 1;
397 return menu;
402 static WMenu*
403 makeOptionsMenu(WScreen *scr)
405 WMenu *menu;
406 WMenuEntry *entry;
408 menu = wMenuCreate(scr, NULL, False);
409 if (!menu) {
410 wwarning(_("could not create submenu for window menu"));
411 return NULL;
414 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
415 NULL);
416 entry->flags.indicator = 1;
417 entry->flags.indicator_type = MI_CHECK;
419 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
420 NULL);
421 entry->flags.indicator = 1;
422 entry->flags.indicator_type = MI_CHECK;
424 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
425 NULL);
426 entry->flags.indicator = 1;
427 entry->flags.indicator_type = MI_CHECK;
429 return menu;
433 static WMenu*
434 createWindowMenu(WScreen *scr)
436 WMenu *menu;
437 KeyCode kcode;
438 WMenuEntry *entry;
439 char *tmp;
441 menu = wMenuCreate(scr, NULL, False);
443 * Warning: If you make some change that affects the order of the
444 * entries, you must update the command #defines in the top of
445 * this file.
447 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
448 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
449 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
451 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
452 entry->rtext = wstrdup(tmp);
455 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
457 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
458 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
460 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
461 entry->rtext = wstrdup(tmp);
464 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
465 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
466 kcode = wKeyBindings[WKBD_SHADE].keycode;
468 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
469 entry->rtext = wstrdup(tmp);
472 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
473 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
474 kcode = wKeyBindings[WKBD_HIDE].keycode;
476 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
477 entry->rtext = wstrdup(tmp);
480 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
481 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
482 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
484 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
485 entry->rtext = wstrdup(tmp);
488 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
489 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
490 kcode = wKeyBindings[WKBD_SELECT].keycode;
492 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
493 entry->rtext = wstrdup(tmp);
496 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
497 scr->workspace_submenu = makeWorkspaceMenu(scr);
498 if (scr->workspace_submenu)
499 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
501 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
503 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
504 wMenuEntrySetCascade(menu, entry,
505 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
508 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
509 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
512 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
513 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
514 kcode = wKeyBindings[WKBD_CLOSE].keycode;
515 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
516 entry->rtext = wstrdup(tmp);
519 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
521 return menu;
525 void
526 CloseWindowMenu(WScreen *scr)
528 if (scr->window_menu) {
529 if (scr->window_menu->flags.mapped)
530 wMenuUnmap(scr->window_menu);
532 if (scr->window_menu->entries[0]->clientdata) {
533 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
535 wwin->flags.menu_open_for_me = 0;
537 scr->window_menu->entries[0]->clientdata = NULL;
543 static void
544 updateMenuForWindow(WMenu *menu, WWindow *wwin)
546 WApplication *wapp = wApplicationOf(wwin->main_window);
547 WScreen *scr = wwin->screen_ptr;
548 int i;
550 updateOptionsMenu(menu, wwin);
552 updateMakeShortcutMenu(menu, wwin);
554 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
555 && !WFLAGP(wapp->main_window_desc, no_appicon));
557 wMenuSetEnabled(menu, MC_CLOSE,
558 (wwin->protocols.DELETE_WINDOW
559 && !WFLAGP(wwin, no_closable)));
561 if (wwin->flags.miniaturized) {
562 static char *text = NULL;
563 if (!text) text = _("Deminiaturize");
565 menu->entries[MC_MINIATURIZE]->text = text;
566 } else {
567 static char *text = NULL;
568 if (!text) text = _("Miniaturize");
570 menu->entries[MC_MINIATURIZE]->text = text;
573 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
575 if (wwin->flags.maximized) {
576 static char *text = NULL;
577 if (!text) text = _("Unmaximize");
579 menu->entries[MC_MAXIMIZE]->text = text;
580 } else {
581 static char *text = NULL;
582 if (!text) text = _("Maximize");
584 menu->entries[MC_MAXIMIZE]->text = text;
586 wMenuSetEnabled(menu, MC_MAXIMIZE, !WFLAGP(wwin, no_resizable));
589 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable)
590 && !wwin->flags.miniaturized);
592 if (wwin->flags.shaded) {
593 static char *text = NULL;
594 if (!text) text = _("Unshade");
596 menu->entries[MC_SHADE]->text = text;
597 } else {
598 static char *text = NULL;
599 if (!text) text = _("Shade");
601 menu->entries[MC_SHADE]->text = text;
604 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
605 && !wwin->flags.miniaturized);
607 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
609 if (!wwin->flags.inspector_open) {
610 wMenuSetEnabled(menu, MC_PROPERTIES, True);
611 } else {
612 wMenuSetEnabled(menu, MC_PROPERTIES, False);
615 /* set the client data of the entries to the window */
616 for (i = 0; i < menu->entry_no; i++) {
617 menu->entries[i]->clientdata = wwin;
620 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
621 scr->workspace_submenu->entries[i]->clientdata = wwin;
622 if (i == scr->current_workspace) {
623 wMenuSetEnabled(scr->workspace_submenu, i, False);
624 } else {
625 wMenuSetEnabled(scr->workspace_submenu, i, True);
629 menu->flags.realized = 0;
630 wMenuRealize(menu);
634 void
635 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
637 WMenu *menu;
638 WScreen *scr = wwin->screen_ptr;
640 wwin->flags.menu_open_for_me = 1;
642 if (!scr->window_menu) {
643 scr->window_menu = createWindowMenu(scr);
645 /* hack to save some memory allocation/deallocation */
646 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
647 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
648 wfree(scr->window_menu->entries[MC_SHADE]->text);
649 } else {
650 updateWorkspaceMenu(scr->workspace_submenu);
653 menu = scr->window_menu;
654 if (menu->flags.mapped) {
655 wMenuUnmap(menu);
656 if (menu->entries[0]->clientdata==wwin) {
657 return;
661 updateMenuForWindow(menu, wwin);
663 x -= menu->frame->core->width/2;
664 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
665 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
666 if (x < wwin->frame_x)
667 x = wwin->frame_x;
669 if (!wwin->flags.internal_window)
670 wMenuMapAt(menu, x, y, keyboard);
674 void
675 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
677 WMenu *menu;
678 WScreen *scr = wwin->screen_ptr;
680 wwin->flags.menu_open_for_me = 1;
682 if (!scr->window_menu) {
683 scr->window_menu = createWindowMenu(scr);
685 /* hack to save some memory allocation/deallocation */
686 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
687 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
688 wfree(scr->window_menu->entries[MC_SHADE]->text);
689 } else {
690 updateWorkspaceMenu(scr->workspace_submenu);
693 menu = scr->window_menu;
694 if (menu->flags.mapped) {
695 wMenuUnmap(menu);
696 if (menu->entries[0]->clientdata==wwin) {
697 return;
701 updateMenuForWindow(menu, wwin);
703 x -= menu->frame->core->width/2;
705 wMenuMapAt(menu, x, y, False);