fixed many bugs, removed linked list
[wmaker-crm.git] / src / winmenu.c
blob9f967104b90bc3008eefe7ef52554cb335de6584
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 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;
185 static void
186 switchWSCommand(WMenu *menu, WMenuEntry *entry)
188 WWindow *wwin = (WWindow*)entry->clientdata;
190 wSelectWindow(wwin, False);
191 wWindowChangeWorkspace(wwin, entry->order);
195 static void
196 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
198 WWindow *wwin = (WWindow*)entry->clientdata;
199 WScreen *scr = wwin->screen_ptr;
201 scr->shortcutWindow[entry->order-WO_ENTRIES] = wwin;
203 if (scr->shortcutSelectedWindows[entry->order-WO_ENTRIES])
204 WMFreeBag(scr->shortcutSelectedWindows[entry->order-WO_ENTRIES]);
206 if (wwin->flags.selected && scr->selected_windows) {
207 WMBag *bag;
208 int i;
210 scr->shortcutSelectedWindows[entry->order-WO_ENTRIES] = WMCreateBag(4);
212 bag = scr->selected_windows;
214 for (i = 0; i < WMGetBagItemCount(bag); i++) {
215 WWindow *tmp = WMGetFromBag(bag, i);
217 WMPutInBag(scr->shortcutSelectedWindows[entry->order-WO_ENTRIES],
218 tmp);
221 else {
222 scr->shortcutSelectedWindows[entry->order-WO_ENTRIES] = NULL;
225 wSelectWindow(wwin, !wwin->flags.selected);
226 XFlush(dpy);
227 wusleep(3000);
228 wSelectWindow(wwin, !wwin->flags.selected);
229 XFlush(dpy);
233 static void
234 updateWorkspaceMenu(WMenu *menu)
236 WScreen *scr = menu->frame->screen_ptr;
237 char title[MAX_WORKSPACENAME_WIDTH+1];
238 int i;
240 if (!menu)
241 return;
243 for (i=0; i<scr->workspace_count; i++) {
244 if (i < menu->entry_no) {
245 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
246 free(menu->entries[i]->text);
247 strcpy(title, scr->workspaces[i]->name);
248 menu->entries[i]->text = wstrdup(title);
249 menu->flags.realized = 0;
251 } else {
252 strcpy(title, scr->workspaces[i]->name);
254 wMenuAddCallback(menu, title, switchWSCommand, NULL);
256 menu->flags.realized = 0;
260 if (!menu->flags.realized)
261 wMenuRealize(menu);
265 static void
266 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
268 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
269 int i;
270 char *buffer;
271 KeyCode kcode;
273 if (!smenu)
274 return;
276 buffer = wmalloc(strlen(_("Set Shortcut"))+16);
278 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
279 char *tmp;
280 int shortcutNo = i-WO_ENTRIES;
281 WWindow *twin = wwin->screen_ptr->shortcutWindow[shortcutNo];
282 WMenuEntry *entry = smenu->entries[i];
283 WMBag *shortSelWindows = wwin->screen_ptr->shortcutSelectedWindows[shortcutNo];
285 sprintf(buffer, "%s %i", _("Set Shortcut"), shortcutNo+1);
287 if (!twin && !shortSelWindows) {
288 entry->flags.indicator_on = 0;
289 } else {
290 entry->flags.indicator_on = 1;
291 if (twin == wwin)
292 entry->flags.indicator_type = MI_DIAMOND;
293 else if (shortSelWindows && WMCountInBag(shortSelWindows, wwin))
294 entry->flags.indicator_type = MI_HIDDEN;
295 else
296 entry->flags.indicator_type = MI_CHECK;
299 if (strcmp(buffer, entry->text)!=0) {
300 free(entry->text);
301 entry->text = wstrdup(buffer);
302 smenu->flags.realized = 0;
305 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
307 if (kcode) {
308 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
309 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
310 if (entry->rtext)
311 free(entry->rtext);
312 entry->rtext = wstrdup(tmp);
313 smenu->flags.realized = 0;
315 wMenuSetEnabled(smenu, i, True);
316 } else {
317 wMenuSetEnabled(smenu, i, False);
318 if (entry->rtext) {
319 free(entry->rtext);
320 entry->rtext = NULL;
321 smenu->flags.realized = 0;
324 entry->clientdata = wwin;
326 free(buffer);
327 if (!smenu->flags.realized)
328 wMenuRealize(smenu);
332 static void
333 updateOptionsMenu(WMenu *menu, WWindow *wwin)
335 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
337 /* keep on top check */
338 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
339 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
340 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
341 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
343 /* keep at bottom check */
344 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
345 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
346 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
347 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
349 /* omnipresent check */
350 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
351 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
353 smenu->flags.realized = 0;
354 wMenuRealize(smenu);
358 static WMenu*
359 makeWorkspaceMenu(WScreen *scr)
361 WMenu *menu;
363 menu = wMenuCreate(scr, NULL, False);
364 if (!menu) {
365 wwarning(_("could not create submenu for window menu"));
366 return NULL;
369 updateWorkspaceMenu(menu);
371 return menu;
375 static WMenu*
376 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
379 WMenu *menu;
381 int i;
383 menu = wMenuCreate(scr, NULL, False);
384 if (!menu) {
385 wwarning(_("could not create submenu for window menu"));
386 return NULL;
390 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
391 WMenuEntry *entry;
393 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
395 entry->flags.indicator = 1;
398 return menu;
403 static WMenu*
404 makeOptionsMenu(WScreen *scr)
406 WMenu *menu;
407 WMenuEntry *entry;
409 menu = wMenuCreate(scr, NULL, False);
410 if (!menu) {
411 wwarning(_("could not create submenu for window menu"));
412 return NULL;
415 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
416 NULL);
417 entry->flags.indicator = 1;
418 entry->flags.indicator_type = MI_CHECK;
420 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
421 NULL);
422 entry->flags.indicator = 1;
423 entry->flags.indicator_type = MI_CHECK;
425 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
426 NULL);
427 entry->flags.indicator = 1;
428 entry->flags.indicator_type = MI_CHECK;
430 return menu;
434 static WMenu*
435 createWindowMenu(WScreen *scr)
437 WMenu *menu;
438 KeyCode kcode;
439 WMenuEntry *entry;
440 char *tmp;
442 menu = wMenuCreate(scr, NULL, False);
444 * Warning: If you make some change that affects the order of the
445 * entries, you must update the command #defines in the top of
446 * this file.
448 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
449 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
450 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
452 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
453 entry->rtext = wstrdup(tmp);
456 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
458 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
459 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
461 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
462 entry->rtext = wstrdup(tmp);
465 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
466 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
467 kcode = wKeyBindings[WKBD_SHADE].keycode;
469 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
470 entry->rtext = wstrdup(tmp);
473 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
474 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
475 kcode = wKeyBindings[WKBD_HIDE].keycode;
477 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
478 entry->rtext = wstrdup(tmp);
481 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
482 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
483 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
485 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
486 entry->rtext = wstrdup(tmp);
489 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
490 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
491 kcode = wKeyBindings[WKBD_SELECT].keycode;
493 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
494 entry->rtext = wstrdup(tmp);
497 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
498 scr->workspace_submenu = makeWorkspaceMenu(scr);
499 if (scr->workspace_submenu)
500 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
502 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
504 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
505 wMenuEntrySetCascade(menu, entry,
506 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
509 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
510 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
513 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
514 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
515 kcode = wKeyBindings[WKBD_CLOSE].keycode;
516 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
517 entry->rtext = wstrdup(tmp);
520 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
522 return menu;
526 void
527 CloseWindowMenu(WScreen *scr)
529 if (scr->window_menu) {
530 if (scr->window_menu->flags.mapped)
531 wMenuUnmap(scr->window_menu);
533 if (scr->window_menu->entries[0]->clientdata) {
534 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
536 wwin->flags.menu_open_for_me = 0;
538 scr->window_menu->entries[0]->clientdata = NULL;
544 static void
545 updateMenuForWindow(WMenu *menu, WWindow *wwin)
547 WApplication *wapp = wApplicationOf(wwin->main_window);
548 WScreen *scr = wwin->screen_ptr;
549 int i;
551 updateOptionsMenu(menu, wwin);
553 updateMakeShortcutMenu(menu, wwin);
555 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
556 && !WFLAGP(wapp->main_window_desc, no_appicon));
558 wMenuSetEnabled(menu, MC_CLOSE,
559 (wwin->protocols.DELETE_WINDOW
560 && !WFLAGP(wwin, no_closable)));
562 if (wwin->flags.miniaturized) {
563 static char *text = NULL;
564 if (!text) text = _("Deminiaturize");
566 menu->entries[MC_MINIATURIZE]->text = text;
567 } else {
568 static char *text = NULL;
569 if (!text) text = _("Miniaturize");
571 menu->entries[MC_MINIATURIZE]->text = text;
574 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
576 if (wwin->flags.maximized) {
577 static char *text = NULL;
578 if (!text) text = _("Unmaximize");
580 menu->entries[MC_MAXIMIZE]->text = text;
581 } else {
582 static char *text = NULL;
583 if (!text) text = _("Maximize");
585 menu->entries[MC_MAXIMIZE]->text = text;
587 wMenuSetEnabled(menu, MC_MAXIMIZE, !WFLAGP(wwin, no_resizable));
590 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable)
591 && !wwin->flags.miniaturized);
593 if (wwin->flags.shaded) {
594 static char *text = NULL;
595 if (!text) text = _("Unshade");
597 menu->entries[MC_SHADE]->text = text;
598 } else {
599 static char *text = NULL;
600 if (!text) text = _("Shade");
602 menu->entries[MC_SHADE]->text = text;
605 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
606 && !wwin->flags.miniaturized);
608 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
610 if (!wwin->flags.inspector_open) {
611 wMenuSetEnabled(menu, MC_PROPERTIES, True);
612 } else {
613 wMenuSetEnabled(menu, MC_PROPERTIES, False);
616 /* set the client data of the entries to the window */
617 for (i = 0; i < menu->entry_no; i++) {
618 menu->entries[i]->clientdata = wwin;
621 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
622 scr->workspace_submenu->entries[i]->clientdata = wwin;
623 if (i == scr->current_workspace) {
624 wMenuSetEnabled(scr->workspace_submenu, i, False);
625 } else {
626 wMenuSetEnabled(scr->workspace_submenu, i, True);
630 menu->flags.realized = 0;
631 wMenuRealize(menu);
635 void
636 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
638 WMenu *menu;
639 WScreen *scr = wwin->screen_ptr;
641 wwin->flags.menu_open_for_me = 1;
643 if (!scr->window_menu) {
644 scr->window_menu = createWindowMenu(scr);
646 /* hack to save some memory allocation/deallocation */
647 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
648 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
649 free(scr->window_menu->entries[MC_SHADE]->text);
650 } else {
651 updateWorkspaceMenu(scr->workspace_submenu);
654 menu = scr->window_menu;
655 if (menu->flags.mapped) {
656 wMenuUnmap(menu);
657 if (menu->entries[0]->clientdata==wwin) {
658 return;
662 updateMenuForWindow(menu, wwin);
664 x -= menu->frame->core->width/2;
665 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
666 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
667 if (x < wwin->frame_x)
668 x = wwin->frame_x;
670 if (!wwin->flags.internal_window)
671 wMenuMapAt(menu, x, y, keyboard);
675 void
676 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
678 WMenu *menu;
679 WScreen *scr = wwin->screen_ptr;
681 wwin->flags.menu_open_for_me = 1;
683 if (!scr->window_menu) {
684 scr->window_menu = createWindowMenu(scr);
686 /* hack to save some memory allocation/deallocation */
687 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
688 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
689 free(scr->window_menu->entries[MC_SHADE]->text);
690 } else {
691 updateWorkspaceMenu(scr->workspace_submenu);
694 menu = scr->window_menu;
695 if (menu->flags.mapped) {
696 wMenuUnmap(menu);
697 if (menu->entries[0]->clientdata==wwin) {
698 return;
702 updateMenuForWindow(menu, wwin);
704 x -= menu->frame->core->width/2;
706 wMenuMapAt(menu, x, y, False);