Try to reduce flicker in text widget.
[wmaker-crm.git] / src / winmenu.c
blob1dcc66994d2a28a2cfd34136d0f210a8701ff211
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"
48 #define MC_MAXIMIZE 0
49 #define MC_MINIATURIZE 1
50 #define MC_SHADE 2
51 #define MC_HIDE 3
52 #define MC_MOVERESIZE 4
53 #define MC_SELECT 5
54 #define MC_DUMMY_MOVETO 6
55 #define MC_PROPERTIES 7
56 #define MC_OPTIONS 8
57 #define MC_SHORTCUT 8
59 #define MC_CLOSE 9
60 #define MC_KILL 10
63 #define WO_KEEP_ON_TOP 0
64 #define WO_KEEP_AT_BOTTOM 1
65 #define WO_OMNIPRESENT 2
66 #define WO_ENTRIES 3
68 /**** Global data ***/
69 extern Time LastTimestamp;
70 extern Atom _XA_WM_DELETE_WINDOW;
71 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
73 extern WShortKey wKeyBindings[WKBD_LAST];
75 extern WPreferences wPreferences;
77 static void updateOptionsMenu(WMenu *menu, WWindow *wwin);
79 static void
80 execWindowOptionCommand(WMenu *menu, WMenuEntry *entry)
82 WWindow *wwin = (WWindow*)entry->clientdata;
84 switch (entry->order) {
85 case WO_KEEP_ON_TOP:
86 if(wwin->frame->core->stacking->window_level!=WMFloatingLevel)
87 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
88 else
89 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
90 break;
92 case WO_KEEP_AT_BOTTOM:
93 if(wwin->frame->core->stacking->window_level!=WMSunkenLevel)
94 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
95 else
96 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
97 break;
99 case WO_OMNIPRESENT:
100 wwin->flags.omnipresent^=1;
101 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_WORKSPACE);
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;
184 static void
185 switchWSCommand(WMenu *menu, WMenuEntry *entry)
187 WWindow *wwin = (WWindow*)entry->clientdata;
189 wSelectWindow(wwin, False);
190 wWindowChangeWorkspace(wwin, entry->order);
194 static void
195 makeShortcutCommand(WMenu *menu, WMenuEntry *entry)
197 WWindow *wwin = (WWindow*)entry->clientdata;
199 wwin->screen_ptr->shortcutWindow[entry->order-WO_ENTRIES] = wwin;
201 wSelectWindow(wwin, !wwin->flags.selected);
202 XFlush(dpy);
203 wusleep(3000);
204 wSelectWindow(wwin, !wwin->flags.selected);
205 XFlush(dpy);
209 static void
210 updateWorkspaceMenu(WMenu *menu)
212 WScreen *scr = menu->frame->screen_ptr;
213 char title[MAX_WORKSPACENAME_WIDTH+1];
214 int i;
216 if (!menu)
217 return;
219 for (i=0; i<scr->workspace_count; i++) {
220 if (i < menu->entry_no) {
221 if (strcmp(menu->entries[i]->text,scr->workspaces[i]->name)!=0) {
222 free(menu->entries[i]->text);
223 strcpy(title, scr->workspaces[i]->name);
224 menu->entries[i]->text = wstrdup(title);
225 menu->flags.realized = 0;
227 } else {
228 strcpy(title, scr->workspaces[i]->name);
230 wMenuAddCallback(menu, title, switchWSCommand, NULL);
232 menu->flags.realized = 0;
236 if (!menu->flags.realized)
237 wMenuRealize(menu);
241 static void
242 updateMakeShortcutMenu(WMenu *menu, WWindow *wwin)
244 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
245 int i;
246 char *buffer;
247 KeyCode kcode;
249 if (!smenu)
250 return;
252 buffer = wmalloc(strlen(_("Set Shortcut"))+16);
254 for (i=WO_ENTRIES; i<smenu->entry_no; i++) {
255 char *tmp;
256 int shortcutNo = i-WO_ENTRIES;
257 WWindow *twin = wwin->screen_ptr->shortcutWindow[shortcutNo];
258 WMenuEntry *entry = smenu->entries[i];
260 sprintf(buffer, "%s %i", _("Set Shortcut"), shortcutNo+1);
262 if (!twin) {
263 entry->flags.indicator_on = 0;
264 } else {
265 entry->flags.indicator_on = 1;
266 if (twin != wwin)
267 entry->flags.indicator_type = MI_CHECK;
268 else
269 entry->flags.indicator_type = MI_DIAMOND;
272 if (strcmp(buffer, entry->text)!=0) {
273 free(entry->text);
274 entry->text = wstrdup(buffer);
275 smenu->flags.realized = 0;
278 kcode = wKeyBindings[WKBD_WINDOW1+shortcutNo].keycode;
280 if (kcode) {
281 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
282 && (!entry->rtext || strcmp(tmp, entry->rtext)!=0)) {
283 if (entry->rtext)
284 free(entry->rtext);
285 entry->rtext = wstrdup(tmp);
286 smenu->flags.realized = 0;
288 wMenuSetEnabled(smenu, i, True);
289 } else {
290 wMenuSetEnabled(smenu, i, False);
291 if (entry->rtext) {
292 free(entry->rtext);
293 entry->rtext = NULL;
294 smenu->flags.realized = 0;
297 entry->clientdata = wwin;
299 free(buffer);
300 if (!smenu->flags.realized)
301 wMenuRealize(smenu);
305 static void
306 updateOptionsMenu(WMenu *menu, WWindow *wwin)
308 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
310 /* keep on top check */
311 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
312 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
313 (wwin->frame->core->stacking->window_level == WMFloatingLevel)?1:0;
314 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
316 /* keep at bottom check */
317 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
318 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
319 (wwin->frame->core->stacking->window_level == WMSunkenLevel)?1:0;
320 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
322 /* omnipresent check */
323 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
324 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
326 smenu->flags.realized = 0;
327 wMenuRealize(smenu);
331 static WMenu*
332 makeWorkspaceMenu(WScreen *scr)
334 WMenu *menu;
336 menu = wMenuCreate(scr, NULL, False);
337 if (!menu) {
338 wwarning(_("could not create submenu for window menu"));
339 return NULL;
342 updateWorkspaceMenu(menu);
344 return menu;
348 static WMenu*
349 makeMakeShortcutMenu(WScreen *scr, WMenu *menu)
352 WMenu *menu;
354 int i;
356 menu = wMenuCreate(scr, NULL, False);
357 if (!menu) {
358 wwarning(_("could not create submenu for window menu"));
359 return NULL;
363 for (i=0; i<MAX_WINDOW_SHORTCUTS; i++) {
364 WMenuEntry *entry;
366 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
368 entry->flags.indicator = 1;
371 return menu;
376 static WMenu*
377 makeOptionsMenu(WScreen *scr)
379 WMenu *menu;
380 WMenuEntry *entry;
382 menu = wMenuCreate(scr, NULL, False);
383 if (!menu) {
384 wwarning(_("could not create submenu for window menu"));
385 return NULL;
388 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand,
389 NULL);
390 entry->flags.indicator = 1;
391 entry->flags.indicator_type = MI_CHECK;
393 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand,
394 NULL);
395 entry->flags.indicator = 1;
396 entry->flags.indicator_type = MI_CHECK;
398 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand,
399 NULL);
400 entry->flags.indicator = 1;
401 entry->flags.indicator_type = MI_CHECK;
403 return menu;
407 static WMenu*
408 createWindowMenu(WScreen *scr)
410 WMenu *menu;
411 KeyCode kcode;
412 WMenuEntry *entry;
413 char *tmp;
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 if (wKeyBindings[WKBD_MAXIMIZE].keycode!=0) {
423 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
425 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
426 entry->rtext = wstrdup(tmp);
429 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
431 if (wKeyBindings[WKBD_MINIATURIZE].keycode!=0) {
432 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
434 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
435 entry->rtext = wstrdup(tmp);
438 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
439 if (wKeyBindings[WKBD_SHADE].keycode!=0) {
440 kcode = wKeyBindings[WKBD_SHADE].keycode;
442 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
443 entry->rtext = wstrdup(tmp);
446 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
447 if (wKeyBindings[WKBD_HIDE].keycode!=0) {
448 kcode = wKeyBindings[WKBD_HIDE].keycode;
450 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
451 entry->rtext = wstrdup(tmp);
454 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
455 if (wKeyBindings[WKBD_MOVERESIZE].keycode!=0) {
456 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
458 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
459 entry->rtext = wstrdup(tmp);
462 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
463 if (wKeyBindings[WKBD_SELECT].keycode!=0) {
464 kcode = wKeyBindings[WKBD_SELECT].keycode;
466 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
467 entry->rtext = wstrdup(tmp);
470 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
471 scr->workspace_submenu = makeWorkspaceMenu(scr);
472 if (scr->workspace_submenu)
473 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
475 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
477 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
478 wMenuEntrySetCascade(menu, entry,
479 makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
482 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
483 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
486 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
487 if (wKeyBindings[WKBD_CLOSE].keycode!=0) {
488 kcode = wKeyBindings[WKBD_CLOSE].keycode;
489 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
490 entry->rtext = wstrdup(tmp);
493 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
495 return menu;
499 void
500 CloseWindowMenu(WScreen *scr)
502 if (scr->window_menu) {
503 if (scr->window_menu->flags.mapped)
504 wMenuUnmap(scr->window_menu);
506 if (scr->window_menu->entries[0]->clientdata) {
507 WWindow *wwin = (WWindow*)scr->window_menu->entries[0]->clientdata;
509 wwin->flags.menu_open_for_me = 0;
511 scr->window_menu->entries[0]->clientdata = NULL;
517 static void
518 updateMenuForWindow(WMenu *menu, WWindow *wwin)
520 WApplication *wapp = wApplicationOf(wwin->main_window);
521 WScreen *scr = wwin->screen_ptr;
522 int i;
524 updateOptionsMenu(menu, wwin);
526 updateMakeShortcutMenu(menu, wwin);
528 wMenuSetEnabled(menu, MC_HIDE, wapp!=NULL
529 && !WFLAGP(wapp->main_window_desc, no_appicon));
531 wMenuSetEnabled(menu, MC_CLOSE,
532 (wwin->protocols.DELETE_WINDOW
533 && !WFLAGP(wwin, no_closable)));
535 if (wwin->flags.miniaturized) {
536 static char *text = NULL;
537 if (!text) text = _("Deminiaturize");
539 menu->entries[MC_MINIATURIZE]->text = text;
540 } else {
541 static char *text = NULL;
542 if (!text) text = _("Miniaturize");
544 menu->entries[MC_MINIATURIZE]->text = text;
547 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
549 if (wwin->flags.maximized) {
550 static char *text = NULL;
551 if (!text) text = _("Unmaximize");
553 menu->entries[MC_MAXIMIZE]->text = text;
554 } else {
555 static char *text = NULL;
556 if (!text) text = _("Maximize");
558 menu->entries[MC_MAXIMIZE]->text = text;
560 wMenuSetEnabled(menu, MC_MAXIMIZE, !WFLAGP(wwin, no_resizable));
563 wMenuSetEnabled(menu, MC_MOVERESIZE, !WFLAGP(wwin, no_resizable)
564 && !wwin->flags.miniaturized);
566 if (wwin->flags.shaded) {
567 static char *text = NULL;
568 if (!text) text = _("Unshade");
570 menu->entries[MC_SHADE]->text = text;
571 } else {
572 static char *text = NULL;
573 if (!text) text = _("Shade");
575 menu->entries[MC_SHADE]->text = text;
578 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
579 && !wwin->flags.miniaturized);
581 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
583 if (!wwin->flags.inspector_open) {
584 wMenuSetEnabled(menu, MC_PROPERTIES, True);
585 } else {
586 wMenuSetEnabled(menu, MC_PROPERTIES, False);
589 /* set the client data of the entries to the window */
590 for (i = 0; i < menu->entry_no; i++) {
591 menu->entries[i]->clientdata = wwin;
594 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
595 scr->workspace_submenu->entries[i]->clientdata = wwin;
596 if (i == scr->current_workspace) {
597 wMenuSetEnabled(scr->workspace_submenu, i, False);
598 } else {
599 wMenuSetEnabled(scr->workspace_submenu, i, True);
603 menu->flags.realized = 0;
604 wMenuRealize(menu);
608 void
609 OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
611 WMenu *menu;
612 WScreen *scr = wwin->screen_ptr;
614 wwin->flags.menu_open_for_me = 1;
616 if (!scr->window_menu) {
617 scr->window_menu = createWindowMenu(scr);
619 /* hack to save some memory allocation/deallocation */
620 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
621 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
622 free(scr->window_menu->entries[MC_SHADE]->text);
623 } else {
624 updateWorkspaceMenu(scr->workspace_submenu);
627 menu = scr->window_menu;
628 if (menu->flags.mapped) {
629 wMenuUnmap(menu);
630 if (menu->entries[0]->clientdata==wwin) {
631 return;
635 updateMenuForWindow(menu, wwin);
637 x -= menu->frame->core->width/2;
638 if (x + menu->frame->core->width > wwin->frame_x+wwin->frame->core->width)
639 x = wwin->frame_x+wwin->frame->core->width - menu->frame->core->width;
640 if (x < wwin->frame_x)
641 x = wwin->frame_x;
643 if (!wwin->flags.internal_window)
644 wMenuMapAt(menu, x, y, keyboard);
648 void
649 OpenMiniwindowMenu(WWindow *wwin, int x, int y)
651 WMenu *menu;
652 WScreen *scr = wwin->screen_ptr;
654 wwin->flags.menu_open_for_me = 1;
656 if (!scr->window_menu) {
657 scr->window_menu = createWindowMenu(scr);
659 /* hack to save some memory allocation/deallocation */
660 free(scr->window_menu->entries[MC_MINIATURIZE]->text);
661 free(scr->window_menu->entries[MC_MAXIMIZE]->text);
662 free(scr->window_menu->entries[MC_SHADE]->text);
663 } else {
664 updateWorkspaceMenu(scr->workspace_submenu);
667 menu = scr->window_menu;
668 if (menu->flags.mapped) {
669 wMenuUnmap(menu);
670 if (menu->entries[0]->clientdata==wwin) {
671 return;
675 updateMenuForWindow(menu, wwin);
677 x -= menu->frame->core->width/2;
679 wMenuMapAt(menu, x, y, False);