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"
50 #define MC_MINIATURIZE 1
53 #define MC_MOVERESIZE 4
55 #define MC_DUMMY_MOVETO 6
56 #define MC_PROPERTIES 7
64 #define WO_KEEP_ON_TOP 0
65 #define WO_KEEP_AT_BOTTOM 1
66 #define WO_OMNIPRESENT 2
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
);
81 execWindowOptionCommand(WMenu
*menu
, WMenuEntry
*entry
)
83 WWindow
*wwin
= (WWindow
*)entry
->clientdata
;
85 switch (entry
->order
) {
87 if(wwin
->frame
->core
->stacking
->window_level
!=WMFloatingLevel
)
88 ChangeStackingLevel(wwin
->frame
->core
, WMFloatingLevel
);
90 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
93 case WO_KEEP_AT_BOTTOM
:
94 if(wwin
->frame
->core
->stacking
->window_level
!=WMSunkenLevel
)
95 ChangeStackingLevel(wwin
->frame
->core
, WMSunkenLevel
);
97 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
101 wWindowSetOmnipresent(wwin
, !wwin
->flags
.omnipresent
);
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
;
198 WScreen
*scr
= wwin
->screen_ptr
;
199 int index
= entry
->order
-WO_ENTRIES
;
201 if (scr
->shortcutWindows
[index
]) {
202 WMFreeArray(scr
->shortcutWindows
[index
]);
203 scr
->shortcutWindows
[index
] = NULL
;
206 if (wwin
->flags
.selected
&& scr
->selected_windows
) {
207 scr
->shortcutWindows
[index
] = WMDuplicateArray(scr
->selected_windows
);
208 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
209 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
211 scr
->shortcutWindows
[index
] = WMCreateArray(4);
212 WMAddToArray(scr
->shortcutWindows
[index
], wwin
);
215 wSelectWindow(wwin
, !wwin
->flags
.selected
);
218 wSelectWindow(wwin
, !wwin
->flags
.selected
);
224 updateWorkspaceMenu(WMenu
*menu
)
226 WScreen
*scr
= menu
->frame
->screen_ptr
;
227 char title
[MAX_WORKSPACENAME_WIDTH
+1];
233 for (i
=0; i
<scr
->workspace_count
; i
++) {
234 if (i
< menu
->entry_no
) {
235 if (strcmp(menu
->entries
[i
]->text
,scr
->workspaces
[i
]->name
)!=0) {
236 wfree(menu
->entries
[i
]->text
);
237 strcpy(title
, scr
->workspaces
[i
]->name
);
238 menu
->entries
[i
]->text
= wstrdup(title
);
239 menu
->flags
.realized
= 0;
242 strcpy(title
, scr
->workspaces
[i
]->name
);
244 wMenuAddCallback(menu
, title
, switchWSCommand
, NULL
);
246 menu
->flags
.realized
= 0;
250 if (!menu
->flags
.realized
)
256 updateMakeShortcutMenu(WMenu
*menu
, WWindow
*wwin
)
258 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_SHORTCUT
]->cascade
];
267 buflen
= strlen(_("Set Shortcut"))+16;
268 buffer
= wmalloc(buflen
);
270 for (i
=WO_ENTRIES
; i
<smenu
->entry_no
; i
++) {
272 int shortcutNo
= i
-WO_ENTRIES
;
273 WMenuEntry
*entry
= smenu
->entries
[i
];
274 WMArray
*shortSelWindows
= wwin
->screen_ptr
->shortcutWindows
[shortcutNo
];
276 snprintf(buffer
, buflen
, "%s %i", _("Set Shortcut"), shortcutNo
+1);
278 if (!shortSelWindows
) {
279 entry
->flags
.indicator_on
= 0;
281 entry
->flags
.indicator_on
= 1;
282 if (WMCountInArray(shortSelWindows
, wwin
))
283 entry
->flags
.indicator_type
= MI_DIAMOND
;
285 entry
->flags
.indicator_type
= MI_CHECK
;
288 if (strcmp(buffer
, entry
->text
)!=0) {
290 entry
->text
= wstrdup(buffer
);
291 smenu
->flags
.realized
= 0;
294 kcode
= wKeyBindings
[WKBD_WINDOW1
+shortcutNo
].keycode
;
297 if ((tmp
= XKeysymToString(XKeycodeToKeysym(dpy
, kcode
, 0)))
298 && (!entry
->rtext
|| strcmp(tmp
, entry
->rtext
)!=0)) {
301 entry
->rtext
= wstrdup(tmp
);
302 smenu
->flags
.realized
= 0;
304 wMenuSetEnabled(smenu
, i
, True
);
306 wMenuSetEnabled(smenu
, i
, False
);
310 smenu
->flags
.realized
= 0;
313 entry
->clientdata
= wwin
;
316 if (!smenu
->flags
.realized
)
322 updateOptionsMenu(WMenu
*menu
, WWindow
*wwin
)
324 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_OPTIONS
]->cascade
];
326 /* keep on top check */
327 smenu
->entries
[WO_KEEP_ON_TOP
]->clientdata
= wwin
;
328 smenu
->entries
[WO_KEEP_ON_TOP
]->flags
.indicator_on
=
329 (wwin
->frame
->core
->stacking
->window_level
== WMFloatingLevel
)?1:0;
330 wMenuSetEnabled(smenu
, WO_KEEP_ON_TOP
, !wwin
->flags
.miniaturized
);
332 /* keep at bottom check */
333 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->clientdata
= wwin
;
334 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->flags
.indicator_on
=
335 (wwin
->frame
->core
->stacking
->window_level
== WMSunkenLevel
)?1:0;
336 wMenuSetEnabled(smenu
, WO_KEEP_AT_BOTTOM
, !wwin
->flags
.miniaturized
);
338 /* omnipresent check */
339 smenu
->entries
[WO_OMNIPRESENT
]->clientdata
= wwin
;
340 smenu
->entries
[WO_OMNIPRESENT
]->flags
.indicator_on
= IS_OMNIPRESENT(wwin
);
342 smenu
->flags
.realized
= 0;
348 makeWorkspaceMenu(WScreen
*scr
)
352 menu
= wMenuCreate(scr
, NULL
, False
);
354 wwarning(_("could not create submenu for window menu"));
358 updateWorkspaceMenu(menu
);
365 makeMakeShortcutMenu(WScreen
*scr
, WMenu
*menu
)
372 menu = wMenuCreate(scr, NULL, False);
374 wwarning(_("could not create submenu for window menu"));
379 for (i
=0; i
<MAX_WINDOW_SHORTCUTS
; i
++) {
381 entry
= wMenuAddCallback(menu
, "", makeShortcutCommand
, NULL
);
383 entry
->flags
.indicator
= 1;
392 makeOptionsMenu(WScreen
*scr
)
397 menu
= wMenuCreate(scr
, NULL
, False
);
399 wwarning(_("could not create submenu for window menu"));
403 entry
= wMenuAddCallback(menu
, _("Keep on top"), execWindowOptionCommand
,
405 entry
->flags
.indicator
= 1;
406 entry
->flags
.indicator_type
= MI_CHECK
;
408 entry
= wMenuAddCallback(menu
, _("Keep at bottom"), execWindowOptionCommand
,
410 entry
->flags
.indicator
= 1;
411 entry
->flags
.indicator_type
= MI_CHECK
;
413 entry
= wMenuAddCallback(menu
, _("Omnipresent"), execWindowOptionCommand
,
415 entry
->flags
.indicator
= 1;
416 entry
->flags
.indicator_type
= MI_CHECK
;
423 createWindowMenu(WScreen
*scr
)
430 menu
= wMenuCreate(scr
, NULL
, False
);
432 * Warning: If you make some change that affects the order of the
433 * entries, you must update the command #defines in the top of
436 entry
= wMenuAddCallback(menu
, _("Maximize"), execMenuCommand
, NULL
);
437 if (wKeyBindings
[WKBD_MAXIMIZE
].keycode
!=0) {
438 kcode
= wKeyBindings
[WKBD_MAXIMIZE
].keycode
;
440 if (kcode
&& (tmp
= XKeysymToString(XKeycodeToKeysym(dpy
, kcode
, 0))))
441 entry
->rtext
= wstrdup(tmp
);
444 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
);
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
;
533 updateMenuForWindow(WMenu
*menu
, WWindow
*wwin
)
535 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
536 WScreen
*scr
= wwin
->screen_ptr
;
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
;
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
;
570 static char *text
= NULL
;
571 if (!text
) text
= _("Maximize");
573 menu
->entries
[MC_MAXIMIZE
]->text
= text
;
575 wMenuSetEnabled(menu
, MC_MAXIMIZE
, !WFLAGP(wwin
, no_resizable
));
578 wMenuSetEnabled(menu
, MC_MOVERESIZE
, !WFLAGP(wwin
, no_resizable
)
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
;
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
);
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
);
614 wMenuSetEnabled(scr
->workspace_submenu
, i
, True
);
618 menu
->flags
.realized
= 0;
624 OpenWindowMenu(WWindow
*wwin
, int x
, int y
, int keyboard
)
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
);
639 updateWorkspaceMenu(scr
->workspace_submenu
);
642 menu
= scr
->window_menu
;
643 if (menu
->flags
.mapped
) {
645 if (menu
->entries
[0]->clientdata
==wwin
) {
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
)
658 if (!wwin
->flags
.internal_window
)
659 wMenuMapAt(menu
, x
, y
, keyboard
);
664 OpenMiniwindowMenu(WWindow
*wwin
, int x
, int y
)
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
);
679 updateWorkspaceMenu(scr
->workspace_submenu
);
682 menu
= scr
->window_menu
;
683 if (menu
->flags
.mapped
) {
685 if (menu
->entries
[0]->clientdata
==wwin
) {
690 updateMenuForWindow(menu
, wwin
);
692 x
-= menu
->frame
->core
->width
/2;
694 wMenuMapAt(menu
, x
, y
, False
);