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>
31 #include <X11/XKBlib.h>
33 #include "WindowMaker.h"
39 #include "application.h"
43 #include "workspace.h"
44 #include "winspector.h"
52 #define MC_MINIATURIZE 1
55 #define MC_MOVERESIZE 4
57 #define MC_DUMMY_MOVETO 6
58 #define MC_PROPERTIES 7
66 #define WO_KEEP_ON_TOP 0
67 #define WO_KEEP_AT_BOTTOM 1
68 #define WO_OMNIPRESENT 2
72 static void updateOptionsMenu(WMenu
* menu
, WWindow
* wwin
);
74 static void execWindowOptionCommand(WMenu
* menu
, WMenuEntry
* entry
)
76 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
78 switch (entry
->order
) {
80 if (wwin
->frame
->core
->stacking
->window_level
!= WMFloatingLevel
)
81 ChangeStackingLevel(wwin
->frame
->core
, WMFloatingLevel
);
83 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
86 case WO_KEEP_AT_BOTTOM
:
87 if (wwin
->frame
->core
->stacking
->window_level
!= WMSunkenLevel
)
88 ChangeStackingLevel(wwin
->frame
->core
, WMSunkenLevel
);
90 ChangeStackingLevel(wwin
->frame
->core
, WMNormalLevel
);
94 wWindowSetOmnipresent(wwin
, !wwin
->flags
.omnipresent
);
99 static void execMenuCommand(WMenu
* menu
, WMenuEntry
* entry
)
101 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
104 CloseWindowMenu(menu
->frame
->screen_ptr
);
106 switch (entry
->order
) {
108 /* send delete message */
109 wClientSendProtocol(wwin
, w_global
.atom
.wm
.delete_window
,
110 w_global
.timestamp
.last_event
);
115 if (wPreferences
.dont_confirm_kill
116 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
118 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
119 _("Yes"), _("No"), NULL
) == WAPRDefault
) {
120 if (!wwin
->flags
.destroyed
)
127 if (wwin
->flags
.miniaturized
) {
128 wDeiconifyWindow(wwin
);
130 if (wwin
->protocols
.MINIATURIZE_WINDOW
) {
131 wClientSendProtocol(wwin
, w_global
.atom
.gnustep
.wm_miniaturize_window
,
132 w_global
.timestamp
.last_event
);
134 wIconifyWindow(wwin
);
140 if (wwin
->flags
.maximized
)
141 wUnmaximizeWindow(wwin
);
143 wMaximizeWindow(wwin
, MAX_VERTICAL
| MAX_HORIZONTAL
);
147 if (wwin
->flags
.shaded
)
148 wUnshadeWindow(wwin
);
154 if (!wwin
->flags
.miniaturized
)
155 wSelectWindow(wwin
, !wwin
->flags
.selected
);
157 wIconSelect(wwin
->icon
);
161 wKeyboardMoveResizeWindow(wwin
);
165 wShowInspectorForWindow(wwin
);
169 (void) RelaunchWindow(wwin
);
173 wapp
= wApplicationOf(wwin
->main_window
);
174 wHideApplication(wapp
);
179 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
181 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
183 wSelectWindow(wwin
, False
);
184 wWindowChangeWorkspace(wwin
, entry
->order
);
187 static void makeShortcutCommand(WMenu
*menu
, WMenuEntry
*entry
)
189 WWindow
*wwin
= (WWindow
*) entry
->clientdata
;
190 WScreen
*scr
= wwin
->screen_ptr
;
191 int index
= entry
->order
- WO_ENTRIES
;
193 if (w_global
.shortcut
.windows
[index
]) {
194 WMFreeArray(w_global
.shortcut
.windows
[index
]);
195 w_global
.shortcut
.windows
[index
] = NULL
;
198 if (wwin
->flags
.selected
&& scr
->selected_windows
) {
199 w_global
.shortcut
.windows
[index
] = WMDuplicateArray(scr
->selected_windows
);
201 w_global
.shortcut
.windows
[index
] = WMCreateArray(4);
202 WMAddToArray(w_global
.shortcut
.windows
[index
], wwin
);
205 wSelectWindow(wwin
, !wwin
->flags
.selected
);
208 wSelectWindow(wwin
, !wwin
->flags
.selected
);
212 static void updateWorkspaceMenu(WMenu
* menu
)
214 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
218 for (i
= 0; i
< w_global
.workspace
.count
; i
++) {
219 if (i
< menu
->entry_no
) {
221 entry
= menu
->entries
[i
];
222 if (strcmp(entry
->text
, w_global
.workspace
.array
[i
]->name
) != 0) {
224 strncpy(title
, w_global
.workspace
.array
[i
]->name
, MAX_WORKSPACENAME_WIDTH
);
225 title
[MAX_WORKSPACENAME_WIDTH
] = 0;
226 menu
->entries
[i
]->text
= wstrdup(title
);
227 menu
->entries
[i
]->rtext
= GetShortcutKey(wKeyBindings
[WKBD_MOVE_WORKSPACE1
+ i
]);
228 menu
->flags
.realized
= 0;
231 strncpy(title
, w_global
.workspace
.array
[i
]->name
, MAX_WORKSPACENAME_WIDTH
);
232 title
[MAX_WORKSPACENAME_WIDTH
] = 0;
234 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, NULL
);
235 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_MOVE_WORKSPACE1
+ i
]);
237 menu
->flags
.realized
= 0;
240 /* workspace shortcut labels */
241 if (i
/ 10 == w_global
.workspace
.current
/ 10)
242 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_MOVE_WORKSPACE1
+ (i
% 10)]);
247 if (!menu
->flags
.realized
)
251 static void updateMakeShortcutMenu(WMenu
*menu
, WWindow
*wwin
)
253 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_SHORTCUT
]->cascade
];
262 buflen
= strlen(_("Set Shortcut")) + 16;
263 buffer
= wmalloc(buflen
);
265 for (i
= WO_ENTRIES
; i
< smenu
->entry_no
; i
++) {
267 int shortcutNo
= i
- WO_ENTRIES
;
268 WMenuEntry
*entry
= smenu
->entries
[i
];
269 WMArray
*shortSelWindows
= w_global
.shortcut
.windows
[shortcutNo
];
271 snprintf(buffer
, buflen
, "%s %i", _("Set Shortcut"), shortcutNo
+ 1);
273 if (!shortSelWindows
) {
274 entry
->flags
.indicator_on
= 0;
276 entry
->flags
.indicator_on
= 1;
277 if (WMCountInArray(shortSelWindows
, wwin
))
278 entry
->flags
.indicator_type
= MI_DIAMOND
;
280 entry
->flags
.indicator_type
= MI_CHECK
;
283 if (strcmp(buffer
, entry
->text
) != 0) {
285 entry
->text
= wstrdup(buffer
);
286 smenu
->flags
.realized
= 0;
289 kcode
= wKeyBindings
[WKBD_WINDOW1
+ shortcutNo
].keycode
;
292 if ((tmp
= GetShortcutKey(wKeyBindings
[WKBD_WINDOW1
+ shortcutNo
]))
293 && (!entry
->rtext
|| strcmp(tmp
, entry
->rtext
) != 0)) {
297 smenu
->flags
.realized
= 0;
299 wMenuSetEnabled(smenu
, i
, True
);
301 wMenuSetEnabled(smenu
, i
, False
);
305 smenu
->flags
.realized
= 0;
308 entry
->clientdata
= wwin
;
311 if (!smenu
->flags
.realized
)
315 static void updateOptionsMenu(WMenu
* menu
, WWindow
* wwin
)
317 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_OPTIONS
]->cascade
];
319 /* keep on top check */
320 smenu
->entries
[WO_KEEP_ON_TOP
]->clientdata
= wwin
;
321 smenu
->entries
[WO_KEEP_ON_TOP
]->flags
.indicator_on
=
322 (wwin
->frame
->core
->stacking
->window_level
== WMFloatingLevel
) ? 1 : 0;
323 wMenuSetEnabled(smenu
, WO_KEEP_ON_TOP
, !wwin
->flags
.miniaturized
);
325 /* keep at bottom check */
326 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->clientdata
= wwin
;
327 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->flags
.indicator_on
=
328 (wwin
->frame
->core
->stacking
->window_level
== WMSunkenLevel
) ? 1 : 0;
329 wMenuSetEnabled(smenu
, WO_KEEP_AT_BOTTOM
, !wwin
->flags
.miniaturized
);
331 /* omnipresent check */
332 smenu
->entries
[WO_OMNIPRESENT
]->clientdata
= wwin
;
333 smenu
->entries
[WO_OMNIPRESENT
]->flags
.indicator_on
= IS_OMNIPRESENT(wwin
);
335 smenu
->flags
.realized
= 0;
339 static WMenu
*makeWorkspaceMenu(WScreen
* scr
)
343 menu
= wMenuCreate(scr
, NULL
, False
);
345 wwarning(_("could not create submenu for window menu"));
349 updateWorkspaceMenu(menu
);
354 static WMenu
*makeMakeShortcutMenu(WMenu
*menu
)
358 for (i
= 0; i
< MAX_WINDOW_SHORTCUTS
; i
++) {
360 entry
= wMenuAddCallback(menu
, "", makeShortcutCommand
, NULL
);
362 entry
->flags
.indicator
= 1;
368 static WMenu
*makeOptionsMenu(WScreen
* scr
)
373 menu
= wMenuCreate(scr
, NULL
, False
);
375 wwarning(_("could not create submenu for window menu"));
379 entry
= wMenuAddCallback(menu
, _("Keep on top"), execWindowOptionCommand
, NULL
);
380 entry
->flags
.indicator
= 1;
381 entry
->flags
.indicator_type
= MI_CHECK
;
383 entry
= wMenuAddCallback(menu
, _("Keep at bottom"), execWindowOptionCommand
, NULL
);
384 entry
->flags
.indicator
= 1;
385 entry
->flags
.indicator_type
= MI_CHECK
;
387 entry
= wMenuAddCallback(menu
, _("Omnipresent"), execWindowOptionCommand
, NULL
);
388 entry
->flags
.indicator
= 1;
389 entry
->flags
.indicator_type
= MI_CHECK
;
394 static WMenu
*createWindowMenu(WScreen
* scr
)
399 menu
= wMenuCreate(scr
, NULL
, False
);
401 * Warning: If you make some change that affects the order of the
402 * entries, you must update the command #defines in the top of
405 entry
= wMenuAddCallback(menu
, _("Maximize"), execMenuCommand
, NULL
);
406 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_MAXIMIZE
]);
408 entry
= wMenuAddCallback(menu
, _("Miniaturize"), execMenuCommand
, NULL
);
409 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_MINIATURIZE
]);
411 entry
= wMenuAddCallback(menu
, _("Shade"), execMenuCommand
, NULL
);
412 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_SHADE
]);
414 entry
= wMenuAddCallback(menu
, _("Hide"), execMenuCommand
, NULL
);
415 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_HIDE
]);
417 entry
= wMenuAddCallback(menu
, _("Resize/Move"), execMenuCommand
, NULL
);
418 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_MOVERESIZE
]);
420 entry
= wMenuAddCallback(menu
, _("Select"), execMenuCommand
, NULL
);
421 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_SELECT
]);
423 entry
= wMenuAddCallback(menu
, _("Move To"), NULL
, NULL
);
424 w_global
.workspace
.submenu
= makeWorkspaceMenu(scr
);
425 if (w_global
.workspace
.submenu
)
426 wMenuEntrySetCascade(menu
, entry
, w_global
.workspace
.submenu
);
428 entry
= wMenuAddCallback(menu
, _("Attributes..."), execMenuCommand
, NULL
);
430 entry
= wMenuAddCallback(menu
, _("Options"), NULL
, NULL
);
431 wMenuEntrySetCascade(menu
, entry
, makeMakeShortcutMenu(makeOptionsMenu(scr
)));
433 entry
= wMenuAddCallback(menu
, _("Launch"), execMenuCommand
, NULL
);
434 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_RELAUNCH
]);
436 entry
= wMenuAddCallback(menu
, _("Close"), execMenuCommand
, NULL
);
437 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_CLOSE
]);
439 entry
= wMenuAddCallback(menu
, _("Kill"), execMenuCommand
, NULL
);
444 void CloseWindowMenu(WScreen
* scr
)
446 if (scr
->window_menu
) {
447 if (scr
->window_menu
->flags
.mapped
)
448 wMenuUnmap(scr
->window_menu
);
450 if (scr
->window_menu
->entries
[0]->clientdata
) {
451 WWindow
*wwin
= (WWindow
*) scr
->window_menu
->entries
[0]->clientdata
;
453 wwin
->flags
.menu_open_for_me
= 0;
455 scr
->window_menu
->entries
[0]->clientdata
= NULL
;
459 static void updateMenuForWindow(WMenu
* menu
, WWindow
* wwin
)
461 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
464 updateOptionsMenu(menu
, wwin
);
466 updateMakeShortcutMenu(menu
, wwin
);
468 wMenuSetEnabled(menu
, MC_HIDE
, wapp
!= NULL
&& !WFLAGP(wapp
->main_window_desc
, no_appicon
));
470 wMenuSetEnabled(menu
, MC_CLOSE
, (wwin
->protocols
.DELETE_WINDOW
&& !WFLAGP(wwin
, no_closable
)));
472 if (wwin
->flags
.miniaturized
) {
473 static char *text
= NULL
;
475 text
= _("Deminiaturize");
477 menu
->entries
[MC_MINIATURIZE
]->text
= text
;
479 static char *text
= NULL
;
481 text
= _("Miniaturize");
483 menu
->entries
[MC_MINIATURIZE
]->text
= text
;
486 wMenuSetEnabled(menu
, MC_MINIATURIZE
, !WFLAGP(wwin
, no_miniaturizable
));
488 if (wwin
->flags
.maximized
) {
489 static char *text
= NULL
;
491 text
= _("Unmaximize");
493 menu
->entries
[MC_MAXIMIZE
]->text
= text
;
495 static char *text
= NULL
;
497 text
= _("Maximize");
499 menu
->entries
[MC_MAXIMIZE
]->text
= text
;
501 wMenuSetEnabled(menu
, MC_MAXIMIZE
, IS_RESIZABLE(wwin
));
503 wMenuSetEnabled(menu
, MC_MOVERESIZE
, IS_RESIZABLE(wwin
)
504 && !wwin
->flags
.miniaturized
);
506 if (wwin
->flags
.shaded
) {
507 static char *text
= NULL
;
511 menu
->entries
[MC_SHADE
]->text
= text
;
513 static char *text
= NULL
;
517 menu
->entries
[MC_SHADE
]->text
= text
;
520 wMenuSetEnabled(menu
, MC_SHADE
, !WFLAGP(wwin
, no_shadeable
)
521 && !wwin
->flags
.miniaturized
);
523 if (wwin
->flags
.selected
) {
524 static char *text
= NULL
;
526 text
= _("Deselect");
528 menu
->entries
[MC_SELECT
]->text
= text
;
530 static char *text
= NULL
;
534 menu
->entries
[MC_SELECT
]->text
= text
;
537 wMenuSetEnabled(menu
, MC_DUMMY_MOVETO
, !IS_OMNIPRESENT(wwin
));
539 if (!wwin
->flags
.inspector_open
) {
540 wMenuSetEnabled(menu
, MC_PROPERTIES
, True
);
542 wMenuSetEnabled(menu
, MC_PROPERTIES
, False
);
545 /* set the client data of the entries to the window */
546 for (i
= 0; i
< menu
->entry_no
; i
++) {
547 menu
->entries
[i
]->clientdata
= wwin
;
550 for (i
= 0; i
< w_global
.workspace
.submenu
->entry_no
; i
++) {
551 w_global
.workspace
.submenu
->entries
[i
]->clientdata
= wwin
;
553 if (i
== w_global
.workspace
.current
)
554 wMenuSetEnabled(w_global
.workspace
.submenu
, i
, False
);
556 wMenuSetEnabled(w_global
.workspace
.submenu
, i
, True
);
559 menu
->flags
.realized
= 0;
563 static WMenu
*open_window_menu_core(WWindow
*wwin
)
565 WScreen
*scr
= wwin
->screen_ptr
;
568 wwin
->flags
.menu_open_for_me
= 1;
570 if (!scr
->window_menu
) {
571 scr
->window_menu
= createWindowMenu(scr
);
573 /* hack to save some memory allocation/deallocation */
574 wfree(scr
->window_menu
->entries
[MC_MINIATURIZE
]->text
);
575 wfree(scr
->window_menu
->entries
[MC_MAXIMIZE
]->text
);
576 wfree(scr
->window_menu
->entries
[MC_SHADE
]->text
);
577 wfree(scr
->window_menu
->entries
[MC_SELECT
]->text
);
579 updateWorkspaceMenu(w_global
.workspace
.submenu
);
582 menu
= scr
->window_menu
;
583 if (menu
->flags
.mapped
) {
585 if (menu
->entries
[0]->clientdata
== wwin
)
589 updateMenuForWindow(menu
, wwin
);
594 static void prepare_menu_position(WMenu
*menu
, int x
, int y
)
598 rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
599 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
600 if (x
< rect
.pos
.x
- menu
->frame
->core
->width
/ 2)
601 x
= rect
.pos
.x
- menu
->frame
->core
->width
/ 2;
606 void OpenWindowMenu(WWindow
*wwin
, int x
, int y
, int keyboard
)
610 menu
= open_window_menu_core(wwin
);
614 /* Specific menu position */
615 x
-= menu
->frame
->core
->width
/ 2;
616 if (x
+ menu
->frame
->core
->width
> wwin
->frame_x
+ wwin
->frame
->core
->width
)
617 x
= wwin
->frame_x
+ wwin
->frame
->core
->width
- menu
->frame
->core
->width
;
618 if (x
< wwin
->frame_x
)
621 /* Common menu position */
622 prepare_menu_position(menu
, x
, y
);
624 if (!wwin
->flags
.internal_window
)
625 wMenuMapAt(menu
, x
, y
, keyboard
);
628 void OpenWindowMenu2(WWindow
*wwin
, int x
, int y
, int keyboard
)
633 menu
= open_window_menu_core(wwin
);
637 /* Specific menu position */
638 for (i
= 0; i
< w_global
.workspace
.submenu
->entry_no
; i
++) {
639 w_global
.workspace
.submenu
->entries
[i
]->clientdata
= wwin
;
640 wMenuSetEnabled(w_global
.workspace
.submenu
, i
, True
);
643 x
-= menu
->frame
->core
->width
/ 2;
645 /* Common menu position */
646 prepare_menu_position(menu
, x
, y
);
648 if (!wwin
->flags
.internal_window
)
649 wMenuMapAt(menu
, x
, y
, keyboard
);
652 void OpenMiniwindowMenu(WWindow
* wwin
, int x
, int y
)
656 menu
= open_window_menu_core(wwin
);
660 x
-= menu
->frame
->core
->width
/ 2;
662 wMenuMapAt(menu
, x
, y
, False
);
665 void DestroyWindowMenu(WScreen
*scr
)
667 if (scr
->window_menu
) {
668 scr
->window_menu
->entries
[MC_MINIATURIZE
]->text
= NULL
;
669 scr
->window_menu
->entries
[MC_MAXIMIZE
]->text
= NULL
;
670 scr
->window_menu
->entries
[MC_SHADE
]->text
= NULL
;
671 scr
->window_menu
->entries
[MC_SELECT
]->text
= NULL
;
672 wMenuDestroy(scr
->window_menu
, True
);
673 scr
->window_menu
= NULL
;