fixed flickering when switching windows
[wmaker-crm.git] / src / winmenu.c
blob5430461de35e0586d3d309d28cbdacb7b68c796b
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_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 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
102 break;
107 static void
108 execMenuCommand(WMenu *menu, WMenuEntry *entry)
110 WWindow *wwin = (WWindow*)entry->clientdata;
111 WApplication *wapp;
113 CloseWindowMenu(menu->frame->screen_ptr);
115 switch (entry->order) {
116 case MC_CLOSE:
117 /* send delete message */
118 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
119 break;
121 case MC_KILL:
122 wretain(wwin);
123 if (wPreferences.dont_confirm_kill
124 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
125 _("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
126 _("Yes"), _("No"), NULL)==WAPRDefault) {
127 if (!wwin->flags.destroyed)
128 wClientKill(wwin);
130 wrelease(wwin);
131 break;
133 case MC_MINIATURIZE:
134 if (wwin->flags.miniaturized) {
135 wDeiconifyWindow(wwin);
136 } else{
137 if (wwin->protocols.MINIATURIZE_WINDOW) {
138 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
139 LastTimestamp);
140 } else {
141 wIconifyWindow(wwin);
144 break;
146 case MC_MAXIMIZE:
147 if (wwin->flags.maximized)
148 wUnmaximizeWindow(wwin);
149 else
150 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
151 break;
153 case MC_SHADE:
154 if (wwin->flags.shaded)
155 wUnshadeWindow(wwin);
156 else
157 wShadeWindow(wwin);
158 break;
160 case MC_SELECT:
161 if (!wwin->flags.miniaturized)
162 wSelectWindow(wwin, !wwin->flags.selected);
163 else
164 wIconSelect(wwin->icon);
165 break;
167 case MC_MOVERESIZE:
168 wKeyboardMoveResizeWindow(wwin);
169 break;
171 case MC_PROPERTIES:
172 wShowInspectorForWindow(wwin);
173 break;
175 case MC_HIDE:
176 wapp = wApplicationOf(wwin->main_window);
177 wHideApplication(wapp);
178 break;
183 static void
184 switchWSCommand(WMenu *menu, WMenuEntry *entry)
186 WWindow *wwin = (WWindow*)entry->clientdata;
188 wSelectWindow(wwin, False);
189 wWindowChangeWorkspace(wwin, entry->order);
193 static void
194 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
196 WWindow *wwin = (WWindow*)entry->clientdata;
197 WScreen *scr = wwin->screen_ptr;
198 int index = entry->order-WO_ENTRIES;
200 if (scr->shortcutWindows[index]) {
201 WMFreeArray(scr->shortcutWindows[index]);
202 scr->shortcutWindows[index] = NULL;
205 if (wwin->flags.selected && scr->selected_windows) {
206 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
207 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
208 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
209 } else {
210 scr->shortcutWindows[index] = WMCreateArray(4);
211 WMAddToArray(scr->shortcutWindows[index], wwin);
214 wSelectWindow(wwin, !wwin->flags.selected);
215 XFlush(dpy);
216 wusleep(3000);
217 wSelectWindow(wwin, !wwin->flags.selected);
218 XFlush(dpy);
222 static void
223 updateWorkspaceMenu(WMenu *menu)
225 WScreen *scr = menu->frame->screen_ptr;
226 char title[MAX_WORKSPACENAME_WIDTH+1];
227 int i;
229 if (!menu)
230 return;
232 for (i=0; i<scr->workspace_count; i++) {
233 if (i < menu->entry_no) {
234 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
235 wfree(menu->entries[i]->text);
236 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
237 title[MAX_WORKSPACENAME_WIDTH] = 0;
238 menu->entries[i]->text = wstrdup(title);
239 menu->flags.realized = 0;
241 } else {
242 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
243 title[MAX_WORKSPACENAME_WIDTH] = 0;
245 wMenuAddCallback(menu, title, switchWSCommand, NULL);
247 menu->flags.realized = 0;
251 if (!menu->flags.realized)
252 wMenuRealize(menu);
256 static void
257 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 = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
299 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
300 if (entry->rtext)
301 wfree(entry->rtext);
302 entry->rtext = wstrdup(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);
322 static void
323 updateOptionsMenu(WMenu *menu, WWindow *wwin)
325 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
327 /* keep on top check */
328 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
329 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
330 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
331 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
333 /* keep at bottom check */
334 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
335 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
336 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
337 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
339 /* omnipresent check */
340 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
341 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
343 smenu->flags.realized = 0;
344 wMenuRealize(smenu);
348 static WMenu*
349 makeWorkspaceMenu(WScreen *scr)
351 WMenu *menu;
353 menu = wMenuCreate(scr, NULL, False);
354 if (!menu) {
355 wwarning(_("could not create submenu for window menu"));
356 return NULL;
359 updateWorkspaceMenu(menu);
361 return menu;
365 static WMenu*
366 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
369 WMenu *menu;
371 int i;
373 menu = wMenuCreate(scr, NULL, False);
374 if (!menu) {
375 wwarning(_("could not create submenu for window menu"));
376 return NULL;
380 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
381 WMenuEntry *entry;
382 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
384 entry->flags.indicator = 1;
387 return menu;
392 static WMenu*
393 makeOptionsMenu(WScreen *scr)
395 WMenu *menu;
396 WMenuEntry *entry;
398 menu = wMenuCreate(scr, NULL, False);
399 if (!menu) {
400 wwarning(_("could not create submenu for window menu"));
401 return NULL;
404 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
405 NULL);
406 entry->flags.indicator = 1;
407 entry->flags.indicator_type = MI_CHECK;
409 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
410 NULL);
411 entry->flags.indicator = 1;
412 entry->flags.indicator_type = MI_CHECK;
414 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
415 NULL);
416 entry->flags.indicator = 1;
417 entry->flags.indicator_type = MI_CHECK;
419 return menu;
423 static WMenu*
424 createWindowMenu(WScreen *scr)
426 WMenu *menu;
427 KeyCode kcode;
428 WMenuEntry *entry;
429 char *tmp;
431 menu = wMenuCreate(scr, NULL, False);
433 * Warning: If you make some change that affects the order of the
434 * entries, you must update the command #defines in the top of
435 * this file.
437 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
438 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
439 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
441 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
442 entry->rtext = wstrdup(tmp);
445 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
446 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
447 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
449 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
450 entry->rtext = wstrdup(tmp);
453 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
454 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
455 kcode = wKeyBindings[WKBD_SHADE].keycode;
457 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
458 entry->rtext = wstrdup(tmp);
461 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
462 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
463 kcode = wKeyBindings[WKBD_HIDE].keycode;
465 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
466 entry->rtext = wstrdup(tmp);
469 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
470 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
471 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
473 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
474 entry->rtext = wstrdup(tmp);
477 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
478 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
479 kcode = wKeyBindings[WKBD_SELECT].keycode;
481 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
482 entry->rtext = wstrdup(tmp);
485 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
486 scr->workspace_submenu = makeWorkspaceMenu(scr);
487 if (scr->workspace_submenu)
488 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
490 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
492 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
493 wMenuEntrySetCascade(menu, entry,
494 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
497 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
498 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
501 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
502 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
503 kcode = wKeyBindings[WKBD_CLOSE].keycode;
504 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
505 entry->rtext = wstrdup(tmp);
508 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
510 return menu;
514 void
515 CloseWindowMenu(WScreen *scr)
517 if (scr->window_menu) {
518 if (scr->window_menu->flags.mapped)
519 wMenuUnmap(scr->window_menu);
521 if (scr->window_menu->entries[0]->clientdata) {
522 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
524 wwin->flags.menu_open_for_me = 0;
526 scr->window_menu->entries[0]->clientdata = NULL;
532 static void
533 updateMenuForWindow(WMenu *menu, WWindow *wwin)
535 WApplication *wapp = wApplicationOf(wwin->main_window);
536 WScreen *scr = wwin->screen_ptr;
537 int i;
539 updateOptionsMenu(menu, wwin);
541 updateMakeShortcutMenu(menu, wwin);
543 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
544 && !WFLAGP(wapp->main_window_desc, no_appicon));
546 wMenuSetEnabled(menu, MC_CLOSE,
547 (wwin->protocols.DELETE_WINDOW
548 && !WFLAGP(wwin, no_closable)));
550 if (wwin->flags.miniaturized) {
551 static char *text = NULL;
552 if (!text) text = _("Deminiaturize");
554 menu->entries[MC_MINIATURIZE]->text = text;
555 } else {
556 static char *text = NULL;
557 if (!text) text = _("Miniaturize");
559 menu->entries[MC_MINIATURIZE]->text = text;
562 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
564 if (wwin->flags.maximized) {
565 static char *text = NULL;
566 if (!text) text = _("Unmaximize");
568 menu->entries[MC_MAXIMIZE]->text = text;
569 } else {
570 static char *text = NULL;
571 if (!text) text = _("Maximize");
573 menu->entries[MC_MAXIMIZE]->text = text;
575 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
578 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
579 && !wwin->flags.miniaturized);
581 if (wwin->flags.shaded) {
582 static char *text = NULL;
583 if (!text) text = _("Unshade");
585 menu->entries[MC_SHADE]->text = text;
586 } else {
587 static char *text = NULL;
588 if (!text) text = _("Shade");
590 menu->entries[MC_SHADE]->text = text;
593 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
594 && !wwin->flags.miniaturized);
596 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
598 if (!wwin->flags.inspector_open) {
599 wMenuSetEnabled(menu, MC_PROPERTIES, True);
600 } else {
601 wMenuSetEnabled(menu, MC_PROPERTIES, False);
604 /* set the client data of the entries to the window */
605 for (i = 0; i < menu->entry_no; i++) {
606 menu->entries[i]->clientdata = wwin;
609 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
610 scr->workspace_submenu->entries[i]->clientdata = wwin;
611 if (i == scr->current_workspace) {
612 wMenuSetEnabled(scr->workspace_submenu, i, False);
613 } else {
614 wMenuSetEnabled(scr->workspace_submenu, i, True);
618 menu->flags.realized = 0;
619 wMenuRealize(menu);
623 void
624 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
626 WMenu *menu;
627 WScreen *scr = wwin->screen_ptr;
629 wwin->flags.menu_open_for_me = 1;
631 if (!scr->window_menu) {
632 scr->window_menu = createWindowMenu(scr);
634 /* hack to save some memory allocation/deallocation */
635 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
636 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
637 wfree(scr->window_menu->entries[MC_SHADE]->text);
638 } else {
639 updateWorkspaceMenu(scr->workspace_submenu);
642 menu = scr->window_menu;
643 if (menu->flags.mapped) {
644 wMenuUnmap(menu);
645 if (menu->entries[0]->clientdata==wwin) {
646 return;
650 updateMenuForWindow(menu, wwin);
652 x -= menu->frame->core->width/2;
653 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
654 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
655 if (x < wwin->frame_x)
656 x = wwin->frame_x;
658 if (!wwin->flags.internal_window)
659 wMenuMapAt(menu, x, y, keyboard);
663 void
664 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
666 WMenu *menu;
667 WScreen *scr = wwin->screen_ptr;
669 wwin->flags.menu_open_for_me = 1;
671 if (!scr->window_menu) {
672 scr->window_menu = createWindowMenu(scr);
674 /* hack to save some memory allocation/deallocation */
675 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
676 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
677 wfree(scr->window_menu->entries[MC_SHADE]->text);
678 } else {
679 updateWorkspaceMenu(scr->workspace_submenu);
682 menu = scr->window_menu;
683 if (menu->flags.mapped) {
684 wMenuUnmap(menu);
685 if (menu->entries[0]->clientdata==wwin) {
686 return;
690 updateMenuForWindow(menu, wwin);
692 x -= menu->frame->core->width/2;
694 wMenuMapAt(menu, x, y, False);