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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <X11/Xutil.h>
32 #include "WindowMaker.h"
38 #include "application.h"
41 #include "workspace.h"
42 #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
);
79 static void execWindowOptionCommand(WMenu
* menu
, WMenuEntry
* entry
)
81 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
83 switch (entry
->order
) {
85 if (wwin
->frame
->core
->stacking
->window_level
!= WMFloatingLevel
)
86 ChangeStackingLevel(wwin
->frame
->core
, WMFloatingLevel
);
88 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
91 case WO_KEEP_AT_BOTTOM
:
92 if (wwin
->frame
->core
->stacking
->window_level
!= WMSunkenLevel
)
93 ChangeStackingLevel(wwin
->frame
->core
, WMSunkenLevel
);
95 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
99 wWindowSetOmnipresent(wwin
, !wwin
->flags
.omnipresent
);
104 static void execMenuCommand(WMenu
* menu
, WMenuEntry
* entry
)
106 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
109 CloseWindowMenu(menu
->frame
->screen_ptr
);
111 switch (entry
->order
) {
113 /* send delete message */
114 wClientSendProtocol(wwin
, _XA_WM_DELETE_WINDOW
, LastTimestamp
);
119 if (wPreferences
.dont_confirm_kill
120 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
122 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
123 _("Yes"), _("No"), NULL
) == WAPRDefault
) {
124 if (!wwin
->flags
.destroyed
)
131 if (wwin
->flags
.miniaturized
) {
132 wDeiconifyWindow(wwin
);
134 if (wwin
->protocols
.MINIATURIZE_WINDOW
) {
135 wClientSendProtocol(wwin
, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
, LastTimestamp
);
137 wIconifyWindow(wwin
);
143 if (wwin
->flags
.maximized
)
144 wUnmaximizeWindow(wwin
);
146 wMaximizeWindow(wwin
, MAX_VERTICAL
| MAX_HORIZONTAL
);
150 if (wwin
->flags
.shaded
)
151 wUnshadeWindow(wwin
);
157 if (!wwin
->flags
.miniaturized
)
158 wSelectWindow(wwin
, !wwin
->flags
.selected
);
160 wIconSelect(wwin
->icon
);
164 wKeyboardMoveResizeWindow(wwin
);
168 wShowInspectorForWindow(wwin
);
172 (void) RelaunchWindow(wwin
);
176 wapp
= wApplicationOf(wwin
->main_window
);
177 wHideApplication(wapp
);
182 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
184 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
186 wSelectWindow(wwin
, False
);
187 wWindowChangeWorkspace(wwin
, entry
->order
);
190 static void makeShortcutCommand(WMenu
* menu
, WMenuEntry
* entry
)
192 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
193 WScreen
*scr
= wwin
->screen_ptr
;
194 int index
= entry
->order
- WO_ENTRIES
;
196 if (scr
->shortcutWindows
[index
]) {
197 WMFreeArray(scr
->shortcutWindows
[index
]);
198 scr
->shortcutWindows
[index
] = NULL
;
201 if (wwin
->flags
.selected
&& scr
->selected_windows
) {
202 scr
->shortcutWindows
[index
] = WMDuplicateArray(scr
->selected_windows
);
203 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
204 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
206 scr
->shortcutWindows
[index
] = WMCreateArray(4);
207 WMAddToArray(scr
->shortcutWindows
[index
], wwin
);
210 wSelectWindow(wwin
, !wwin
->flags
.selected
);
213 wSelectWindow(wwin
, !wwin
->flags
.selected
);
217 static void updateWorkspaceMenu(WMenu
* menu
)
219 WScreen
*scr
= menu
->frame
->screen_ptr
;
220 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
223 for (i
= 0; i
< scr
->workspace_count
; i
++) {
224 if (i
< menu
->entry_no
) {
225 if (strcmp(menu
->entries
[i
]->text
, scr
->workspaces
[i
]->name
) != 0) {
226 wfree(menu
->entries
[i
]->text
);
227 strncpy(title
, scr
->workspaces
[i
]->name
, MAX_WORKSPACENAME_WIDTH
);
228 title
[MAX_WORKSPACENAME_WIDTH
] = 0;
229 menu
->entries
[i
]->text
= wstrdup(title
);
230 menu
->flags
.realized
= 0;
233 strncpy(title
, scr
->workspaces
[i
]->name
, MAX_WORKSPACENAME_WIDTH
);
234 title
[MAX_WORKSPACENAME_WIDTH
] = 0;
236 wMenuAddCallback(menu
, title
, switchWSCommand
, NULL
);
238 menu
->flags
.realized
= 0;
242 if (!menu
->flags
.realized
)
246 static char *getShortcutString(WShortKey key
)
249 char *k
= XKeysymToString(XKeycodeToKeysym(dpy
, key
.keycode
, 0));
252 char **m
= wPreferences
.modifier_labels
;
253 if (key
.modifier
& ControlMask
) tmp
= wstrappend(tmp
, m
[1] ? m
[1] : "Ctrl+");
254 if (key
.modifier
& ShiftMask
) tmp
= wstrappend(tmp
, m
[0] ? m
[0] : "Shift+");
255 if (key
.modifier
& Mod1Mask
) tmp
= wstrappend(tmp
, m
[2] ? m
[2] : "Mod1+");
256 if (key
.modifier
& Mod2Mask
) tmp
= wstrappend(tmp
, m
[3] ? m
[3] : "Mod2+");
257 if (key
.modifier
& Mod3Mask
) tmp
= wstrappend(tmp
, m
[4] ? m
[4] : "Mod3+");
258 if (key
.modifier
& Mod4Mask
) tmp
= wstrappend(tmp
, m
[5] ? m
[5] : "Mod4+");
259 if (key
.modifier
& Mod5Mask
) tmp
= wstrappend(tmp
, m
[6] ? m
[6] : "Mod5+");
260 tmp
= wstrappend(tmp
, k
);
265 static void updateMakeShortcutMenu(WMenu
* menu
, WWindow
* wwin
)
267 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_SHORTCUT
]->cascade
];
276 buflen
= strlen(_("Set Shortcut")) + 16;
277 buffer
= wmalloc(buflen
);
279 for (i
= WO_ENTRIES
; i
< smenu
->entry_no
; i
++) {
281 int shortcutNo
= i
- WO_ENTRIES
;
282 WMenuEntry
*entry
= smenu
->entries
[i
];
283 WMArray
*shortSelWindows
= wwin
->screen_ptr
->shortcutWindows
[shortcutNo
];
285 snprintf(buffer
, buflen
, "%s %i", _("Set Shortcut"), shortcutNo
+ 1);
287 if (!shortSelWindows
) {
288 entry
->flags
.indicator_on
= 0;
290 entry
->flags
.indicator_on
= 1;
291 if (WMCountInArray(shortSelWindows
, wwin
))
292 entry
->flags
.indicator_type
= MI_DIAMOND
;
294 entry
->flags
.indicator_type
= MI_CHECK
;
297 if (strcmp(buffer
, entry
->text
) != 0) {
299 entry
->text
= wstrdup(buffer
);
300 smenu
->flags
.realized
= 0;
303 kcode
= wKeyBindings
[WKBD_WINDOW1
+ shortcutNo
].keycode
;
306 if ((tmp
= getShortcutString(wKeyBindings
[WKBD_WINDOW1
+ shortcutNo
]))
307 && (!entry
->rtext
|| strcmp(tmp
, entry
->rtext
) != 0)) {
311 smenu
->flags
.realized
= 0;
313 wMenuSetEnabled(smenu
, i
, True
);
315 wMenuSetEnabled(smenu
, i
, False
);
319 smenu
->flags
.realized
= 0;
322 entry
->clientdata
= wwin
;
325 if (!smenu
->flags
.realized
)
329 static void updateOptionsMenu(WMenu
* menu
, WWindow
* wwin
)
331 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_OPTIONS
]->cascade
];
333 /* keep on top check */
334 smenu
->entries
[WO_KEEP_ON_TOP
]->clientdata
= wwin
;
335 smenu
->entries
[WO_KEEP_ON_TOP
]->flags
.indicator_on
=
336 (wwin
->frame
->core
->stacking
->window_level
== WMFloatingLevel
) ? 1 : 0;
337 wMenuSetEnabled(smenu
, WO_KEEP_ON_TOP
, !wwin
->flags
.miniaturized
);
339 /* keep at bottom check */
340 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->clientdata
= wwin
;
341 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->flags
.indicator_on
=
342 (wwin
->frame
->core
->stacking
->window_level
== WMSunkenLevel
) ? 1 : 0;
343 wMenuSetEnabled(smenu
, WO_KEEP_AT_BOTTOM
, !wwin
->flags
.miniaturized
);
345 /* omnipresent check */
346 smenu
->entries
[WO_OMNIPRESENT
]->clientdata
= wwin
;
347 smenu
->entries
[WO_OMNIPRESENT
]->flags
.indicator_on
= IS_OMNIPRESENT(wwin
);
349 smenu
->flags
.realized
= 0;
353 static WMenu
*makeWorkspaceMenu(WScreen
* scr
)
357 menu
= wMenuCreate(scr
, NULL
, False
);
359 wwarning(_("could not create submenu for window menu"));
363 updateWorkspaceMenu(menu
);
368 static WMenu
*makeMakeShortcutMenu(WScreen
* scr
, WMenu
* menu
)
375 menu = wMenuCreate(scr, NULL, False);
377 wwarning(_("could not create submenu for window menu"));
382 for (i
= 0; i
< MAX_WINDOW_SHORTCUTS
; i
++) {
384 entry
= wMenuAddCallback(menu
, "", makeShortcutCommand
, NULL
);
386 entry
->flags
.indicator
= 1;
392 static WMenu
*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
, NULL
);
404 entry
->flags
.indicator
= 1;
405 entry
->flags
.indicator_type
= MI_CHECK
;
407 entry
= wMenuAddCallback(menu
, _("Keep at bottom"), execWindowOptionCommand
, NULL
);
408 entry
->flags
.indicator
= 1;
409 entry
->flags
.indicator_type
= MI_CHECK
;
411 entry
= wMenuAddCallback(menu
, _("Omnipresent"), execWindowOptionCommand
, NULL
);
412 entry
->flags
.indicator
= 1;
413 entry
->flags
.indicator_type
= MI_CHECK
;
418 static WMenu
*createWindowMenu(WScreen
* scr
)
423 menu
= wMenuCreate(scr
, NULL
, False
);
425 * Warning: If you make some change that affects the order of the
426 * entries, you must update the command #defines in the top of
429 entry
= wMenuAddCallback(menu
, _("Maximize"), execMenuCommand
, NULL
);
430 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_MAXIMIZE
]);
432 entry
= wMenuAddCallback(menu
, _("Miniaturize"), execMenuCommand
, NULL
);
433 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_MINIATURIZE
]);
435 entry
= wMenuAddCallback(menu
, _("Shade"), execMenuCommand
, NULL
);
436 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_SHADE
]);
438 entry
= wMenuAddCallback(menu
, _("Hide"), execMenuCommand
, NULL
);
439 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_HIDE
]);
441 entry
= wMenuAddCallback(menu
, _("Resize/Move"), execMenuCommand
, NULL
);
442 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_MOVERESIZE
]);
444 entry
= wMenuAddCallback(menu
, _("Select"), execMenuCommand
, NULL
);
445 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_SELECT
]);
447 entry
= wMenuAddCallback(menu
, _("Move To"), NULL
, NULL
);
448 scr
->workspace_submenu
= makeWorkspaceMenu(scr
);
449 if (scr
->workspace_submenu
)
450 wMenuEntrySetCascade(menu
, entry
, scr
->workspace_submenu
);
452 entry
= wMenuAddCallback(menu
, _("Attributes..."), execMenuCommand
, NULL
);
454 entry
= wMenuAddCallback(menu
, _("Options"), NULL
, NULL
);
455 wMenuEntrySetCascade(menu
, entry
, makeMakeShortcutMenu(scr
, makeOptionsMenu(scr
)));
458 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
459 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
462 entry
= wMenuAddCallback(menu
, _("Launch"), execMenuCommand
, NULL
);
463 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_RELAUNCH
]);
465 entry
= wMenuAddCallback(menu
, _("Close"), execMenuCommand
, NULL
);
466 entry
->rtext
= getShortcutString(wKeyBindings
[WKBD_CLOSE
]);
468 entry
= wMenuAddCallback(menu
, _("Kill"), execMenuCommand
, NULL
);
473 void CloseWindowMenu(WScreen
* scr
)
475 if (scr
->window_menu
) {
476 if (scr
->window_menu
->flags
.mapped
)
477 wMenuUnmap(scr
->window_menu
);
479 if (scr
->window_menu
->entries
[0]->clientdata
) {
480 WWindow
*wwin
= (WWindow
*) scr
->window_menu
->entries
[0]->clientdata
;
482 wwin
->flags
.menu_open_for_me
= 0;
484 scr
->window_menu
->entries
[0]->clientdata
= NULL
;
488 static void updateMenuForWindow(WMenu
* menu
, WWindow
* wwin
)
490 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
491 WScreen
*scr
= wwin
->screen_ptr
;
494 updateOptionsMenu(menu
, wwin
);
496 updateMakeShortcutMenu(menu
, wwin
);
498 wMenuSetEnabled(menu
, MC_HIDE
, wapp
!= NULL
&& !WFLAGP(wapp
->main_window_desc
, no_appicon
));
500 wMenuSetEnabled(menu
, MC_CLOSE
, (wwin
->protocols
.DELETE_WINDOW
&& !WFLAGP(wwin
, no_closable
)));
502 if (wwin
->flags
.miniaturized
) {
503 static char *text
= NULL
;
505 text
= _("Deminiaturize");
507 menu
->entries
[MC_MINIATURIZE
]->text
= text
;
509 static char *text
= NULL
;
511 text
= _("Miniaturize");
513 menu
->entries
[MC_MINIATURIZE
]->text
= text
;
516 wMenuSetEnabled(menu
, MC_MINIATURIZE
, !WFLAGP(wwin
, no_miniaturizable
));
518 if (wwin
->flags
.maximized
) {
519 static char *text
= NULL
;
521 text
= _("Unmaximize");
523 menu
->entries
[MC_MAXIMIZE
]->text
= text
;
525 static char *text
= NULL
;
527 text
= _("Maximize");
529 menu
->entries
[MC_MAXIMIZE
]->text
= text
;
531 wMenuSetEnabled(menu
, MC_MAXIMIZE
, IS_RESIZABLE(wwin
));
533 wMenuSetEnabled(menu
, MC_MOVERESIZE
, IS_RESIZABLE(wwin
)
534 && !wwin
->flags
.miniaturized
);
536 if (wwin
->flags
.shaded
) {
537 static char *text
= NULL
;
541 menu
->entries
[MC_SHADE
]->text
= text
;
543 static char *text
= NULL
;
547 menu
->entries
[MC_SHADE
]->text
= text
;
550 wMenuSetEnabled(menu
, MC_SHADE
, !WFLAGP(wwin
, no_shadeable
)
551 && !wwin
->flags
.miniaturized
);
553 wMenuSetEnabled(menu
, MC_DUMMY_MOVETO
, !IS_OMNIPRESENT(wwin
));
555 if (!wwin
->flags
.inspector_open
) {
556 wMenuSetEnabled(menu
, MC_PROPERTIES
, True
);
558 wMenuSetEnabled(menu
, MC_PROPERTIES
, False
);
561 /* set the client data of the entries to the window */
562 for (i
= 0; i
< menu
->entry_no
; i
++) {
563 menu
->entries
[i
]->clientdata
= wwin
;
566 for (i
= 0; i
< scr
->workspace_submenu
->entry_no
; i
++) {
567 scr
->workspace_submenu
->entries
[i
]->clientdata
= wwin
;
568 if (i
== scr
->current_workspace
) {
569 wMenuSetEnabled(scr
->workspace_submenu
, i
, False
);
571 wMenuSetEnabled(scr
->workspace_submenu
, i
, True
);
575 menu
->flags
.realized
= 0;
579 void OpenWindowMenu(WWindow
* wwin
, int x
, int y
, int keyboard
)
582 WScreen
*scr
= wwin
->screen_ptr
;
585 wwin
->flags
.menu_open_for_me
= 1;
587 if (!scr
->window_menu
) {
588 scr
->window_menu
= createWindowMenu(scr
);
590 /* hack to save some memory allocation/deallocation */
591 wfree(scr
->window_menu
->entries
[MC_MINIATURIZE
]->text
);
592 wfree(scr
->window_menu
->entries
[MC_MAXIMIZE
]->text
);
593 wfree(scr
->window_menu
->entries
[MC_SHADE
]->text
);
595 updateWorkspaceMenu(scr
->workspace_submenu
);
598 menu
= scr
->window_menu
;
599 if (menu
->flags
.mapped
) {
601 if (menu
->entries
[0]->clientdata
== wwin
) {
606 updateMenuForWindow(menu
, wwin
);
608 x
-= menu
->frame
->core
->width
/ 2;
609 if (x
+ menu
->frame
->core
->width
> wwin
->frame_x
+ wwin
->frame
->core
->width
)
610 x
= wwin
->frame_x
+ wwin
->frame
->core
->width
- menu
->frame
->core
->width
;
611 if (x
< wwin
->frame_x
)
614 rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
615 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
616 if (x
< rect
.pos
.x
- menu
->frame
->core
->width
/ 2)
617 x
= rect
.pos
.x
- menu
->frame
->core
->width
/ 2;
621 if (!wwin
->flags
.internal_window
)
622 wMenuMapAt(menu
, x
, y
, keyboard
);
625 void OpenWindowMenu2(WWindow
*wwin
, int x
, int y
, int keyboard
)
629 WScreen
*scr
= wwin
->screen_ptr
;
632 wwin
->flags
.menu_open_for_me
= 1;
634 if (!scr
->window_menu
) {
635 scr
->window_menu
= createWindowMenu(scr
);
637 /* hack to save some memory allocation/deallocation */
638 wfree(scr
->window_menu
->entries
[MC_MINIATURIZE
]->text
);
639 wfree(scr
->window_menu
->entries
[MC_MAXIMIZE
]->text
);
640 wfree(scr
->window_menu
->entries
[MC_SHADE
]->text
);
642 updateWorkspaceMenu(scr
->workspace_submenu
);
645 menu
= scr
->window_menu
;
646 if (menu
->flags
.mapped
) {
648 if (menu
->entries
[0]->clientdata
== wwin
) {
653 updateMenuForWindow(menu
, wwin
);
655 for (i
= 0; i
< scr
->workspace_submenu
->entry_no
; i
++) {
656 scr
->workspace_submenu
->entries
[i
]->clientdata
= wwin
;
657 wMenuSetEnabled(scr
->workspace_submenu
, i
, True
);
660 x
-= menu
->frame
->core
->width
/ 2;
662 rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
663 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
664 if (x
< rect
.pos
.x
- menu
->frame
->core
->width
/ 2)
665 x
= rect
.pos
.x
- menu
->frame
->core
->width
/ 2;
669 if (!wwin
->flags
.internal_window
)
670 wMenuMapAt(menu
, x
, y
, keyboard
);
673 void OpenMiniwindowMenu(WWindow
* wwin
, int x
, int y
)
676 WScreen
*scr
= wwin
->screen_ptr
;
678 wwin
->flags
.menu_open_for_me
= 1;
680 if (!scr
->window_menu
) {
681 scr
->window_menu
= createWindowMenu(scr
);
683 /* hack to save some memory allocation/deallocation */
684 wfree(scr
->window_menu
->entries
[MC_MINIATURIZE
]->text
);
685 wfree(scr
->window_menu
->entries
[MC_MAXIMIZE
]->text
);
686 wfree(scr
->window_menu
->entries
[MC_SHADE
]->text
);
688 updateWorkspaceMenu(scr
->workspace_submenu
);
691 menu
= scr
->window_menu
;
692 if (menu
->flags
.mapped
) {
694 if (menu
->entries
[0]->clientdata
== wwin
) {
699 updateMenuForWindow(menu
, wwin
);
701 x
-= menu
->frame
->core
->width
/ 2;
703 wMenuMapAt(menu
, x
, y
, False
);
706 void DestroyWindowMenu(WScreen
*scr
)
708 if (scr
->window_menu
) {
709 scr
->window_menu
->entries
[MC_MINIATURIZE
]->text
= NULL
;
710 scr
->window_menu
->entries
[MC_MAXIMIZE
]->text
= NULL
;
711 scr
->window_menu
->entries
[MC_SHADE
]->text
= NULL
;
712 wMenuDestroy(scr
->window_menu
, True
);
713 scr
->window_menu
= NULL
;