1 /* winmenu.c - command menu for windows
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 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,
31 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
39 #include "application.h"
42 #include "workspace.h"
43 #include "winspector.h"
49 #define MC_MINIATURIZE 1
52 #define MC_MOVERESIZE 4
54 #define MC_DUMMY_MOVETO 6
55 #define MC_PROPERTIES 7
63 #define WO_KEEP_ON_TOP 0
64 #define WO_KEEP_AT_BOTTOM 1
65 #define WO_OMNIPRESENT 2
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
);
80 execWindowOptionCommand(WMenu
*menu
, WMenuEntry
*entry
)
82 WWindow
*wwin
= (WWindow
*)entry
->clientdata
;
84 switch (entry
->order
) {
86 if(wwin
->frame
->core
->stacking
->window_level
!=WMFloatingLevel
)
87 ChangeStackingLevel(wwin
->frame
->core
, WMFloatingLevel
);
89 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
92 case WO_KEEP_AT_BOTTOM
:
93 if(wwin
->frame
->core
->stacking
->window_level
!=WMSunkenLevel
)
94 ChangeStackingLevel(wwin
->frame
->core
, WMSunkenLevel
);
96 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
100 wwin
->flags
.omnipresent
^=1;
101 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
108 execMenuCommand(WMenu
*menu
, WMenuEntry
*entry
)
110 WWindow
*wwin
= (WWindow
*)entry
->clientdata
;
113 CloseWindowMenu(menu
->frame
->screen_ptr
);
115 switch (entry
->order
) {
117 /* send delete message */
118 wClientSendProtocol(wwin
, _XA_WM_DELETE_WINDOW
, LastTimestamp
);
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
)
134 if (wwin
->flags
.miniaturized
) {
135 wDeiconifyWindow(wwin
);
137 if (wwin
->protocols
.MINIATURIZE_WINDOW
) {
138 wClientSendProtocol(wwin
, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
,
141 wIconifyWindow(wwin
);
147 if (wwin
->flags
.maximized
)
148 wUnmaximizeWindow(wwin
);
150 wMaximizeWindow(wwin
, MAX_VERTICAL
|MAX_HORIZONTAL
);
154 if (wwin
->flags
.shaded
)
155 wUnshadeWindow(wwin
);
161 if (!wwin
->flags
.miniaturized
)
162 wSelectWindow(wwin
, !wwin
->flags
.selected
);
164 wIconSelect(wwin
->icon
);
168 wKeyboardMoveResizeWindow(wwin
);
172 wShowInspectorForWindow(wwin
);
176 wapp
= wApplicationOf(wwin
->main_window
);
177 wHideApplication(wapp
);
185 switchWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
187 WWindow
*wwin
= (WWindow
*)entry
->clientdata
;
189 wSelectWindow(wwin
, False
);
190 wWindowChangeWorkspace(wwin
, entry
->order
);
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
);
204 wSelectWindow(wwin
, !wwin
->flags
.selected
);
210 updateWorkspaceMenu(WMenu
*menu
)
212 WScreen
*scr
= menu
->frame
->screen_ptr
;
213 char title
[MAX_WORKSPACENAME_WIDTH
+1];
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;
228 strcpy(title
, scr
->workspaces
[i
]->name
);
230 wMenuAddCallback(menu
, title
, switchWSCommand
, NULL
);
232 menu
->flags
.realized
= 0;
236 if (!menu
->flags
.realized
)
242 updateMakeShortcutMenu(WMenu
*menu
, WWindow
*wwin
)
244 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_SHORTCUT
]->cascade
];
252 buffer
= wmalloc(strlen(_("Set Shortcut"))+16);
254 for (i
=WO_ENTRIES
; i
<smenu
->entry_no
; i
++) {
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);
263 entry
->flags
.indicator_on
= 0;
265 entry
->flags
.indicator_on
= 1;
267 entry
->flags
.indicator_type
= MI_CHECK
;
269 entry
->flags
.indicator_type
= MI_DIAMOND
;
272 if (strcmp(buffer
, entry
->text
)!=0) {
274 entry
->text
= wstrdup(buffer
);
275 smenu
->flags
.realized
= 0;
278 kcode
= wKeyBindings
[WKBD_WINDOW1
+shortcutNo
].keycode
;
281 if ((tmp
= XKeysymToString(XKeycodeToKeysym(dpy
, kcode
, 0)))
282 && (!entry
->rtext
|| strcmp(tmp
, entry
->rtext
)!=0)) {
285 entry
->rtext
= wstrdup(tmp
);
286 smenu
->flags
.realized
= 0;
288 wMenuSetEnabled(smenu
, i
, True
);
290 wMenuSetEnabled(smenu
, i
, False
);
294 smenu
->flags
.realized
= 0;
297 entry
->clientdata
= wwin
;
300 if (!smenu
->flags
.realized
)
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;
332 makeWorkspaceMenu(WScreen
*scr
)
336 menu
= wMenuCreate(scr
, NULL
, False
);
338 wwarning(_("could not create submenu for window menu"));
342 updateWorkspaceMenu(menu
);
349 makeMakeShortcutMenu(WScreen
*scr
, WMenu
*menu
)
356 menu = wMenuCreate(scr, NULL, False);
358 wwarning(_("could not create submenu for window menu"));
363 for (i
=0; i
<MAX_WINDOW_SHORTCUTS
; i
++) {
366 entry
= wMenuAddCallback(menu
, "", makeShortcutCommand
, NULL
);
368 entry
->flags
.indicator
= 1;
377 makeOptionsMenu(WScreen
*scr
)
382 menu
= wMenuCreate(scr
, NULL
, False
);
384 wwarning(_("could not create submenu for window menu"));
388 entry
= wMenuAddCallback(menu
, _("Keep on top"), execWindowOptionCommand
,
390 entry
->flags
.indicator
= 1;
391 entry
->flags
.indicator_type
= MI_CHECK
;
393 entry
= wMenuAddCallback(menu
, _("Keep at bottom"), execWindowOptionCommand
,
395 entry
->flags
.indicator
= 1;
396 entry
->flags
.indicator_type
= MI_CHECK
;
398 entry
= wMenuAddCallback(menu
, _("Omnipresent"), execWindowOptionCommand
,
400 entry
->flags
.indicator
= 1;
401 entry
->flags
.indicator_type
= MI_CHECK
;
408 createWindowMenu(WScreen
*scr
)
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
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
);
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
;
518 updateMenuForWindow(WMenu
*menu
, WWindow
*wwin
)
520 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
521 WScreen
*scr
= wwin
->screen_ptr
;
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
;
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
;
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
;
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
);
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
);
599 wMenuSetEnabled(scr
->workspace_submenu
, i
, True
);
603 menu
->flags
.realized
= 0;
609 OpenWindowMenu(WWindow
*wwin
, int x
, int y
, int keyboard
)
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
);
624 updateWorkspaceMenu(scr
->workspace_submenu
);
627 menu
= scr
->window_menu
;
628 if (menu
->flags
.mapped
) {
630 if (menu
->entries
[0]->clientdata
==wwin
) {
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
)
643 if (!wwin
->flags
.internal_window
)
644 wMenuMapAt(menu
, x
, y
, keyboard
);
649 OpenMiniwindowMenu(WWindow
*wwin
, int x
, int y
)
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
);
664 updateWorkspaceMenu(scr
->workspace_submenu
);
667 menu
= scr
->window_menu
;
668 if (menu
->flags
.mapped
) {
670 if (menu
->entries
[0]->clientdata
==wwin
) {
675 updateMenuForWindow(menu
, wwin
);
677 x
-= menu
->frame
->core
->width
/2;
679 wMenuMapAt(menu
, x
, y
, False
);