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
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
);
184 switchWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
186 WWindow
*wwin
= (WWindow
*)entry
->clientdata
;
188 wSelectWindow(wwin
, False
);
189 wWindowChangeWorkspace(wwin
, entry
->order
);
194 makeShortcutCommand(WMenu
*menu
, WMenuEntry
*entry
)
196 WWindow
*wwin
= (WWindow
*)entry
->clientdata
;
197 WScreen
*scr
= wwin
->screen_ptr
;
198 int index
= entry
->order
-WO_ENTRIES
;
200 if (scr
->shortcutWindows
[index
]) {
201 WMFreeArray(scr
->shortcutWindows
[index
]);
202 scr
->shortcutWindows
[index
] = NULL
;
205 if (wwin
->flags
.selected
&& scr
->selected_windows
) {
206 scr
->shortcutWindows
[index
] = WMDuplicateArray(scr
->selected_windows
);
207 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
208 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
210 scr
->shortcutWindows
[index
] = WMCreateArray(4);
211 WMAddToArray(scr
->shortcutWindows
[index
], wwin
);
214 wSelectWindow(wwin
, !wwin
->flags
.selected
);
217 wSelectWindow(wwin
, !wwin
->flags
.selected
);
223 updateWorkspaceMenu(WMenu
*menu
)
225 WScreen
*scr
= menu
->frame
->screen_ptr
;
226 char title
[MAX_WORKSPACENAME_WIDTH
+1];
232 for (i
=0; i
<scr
->workspace_count
; i
++) {
233 if (i
< menu
->entry_no
) {
234 if (strcmp(menu
->entries
[i
]->text
,scr
->workspaces
[i
]->name
)!=0) {
235 wfree(menu
->entries
[i
]->text
);
236 strncpy(title
, scr
->workspaces
[i
]->name
, MAX_WORKSPACENAME_WIDTH
);
237 title
[MAX_WORKSPACENAME_WIDTH
] = 0;
238 menu
->entries
[i
]->text
= wstrdup(title
);
239 menu
->flags
.realized
= 0;
242 strncpy(title
, scr
->workspaces
[i
]->name
, MAX_WORKSPACENAME_WIDTH
);
243 title
[MAX_WORKSPACENAME_WIDTH
] = 0;
245 wMenuAddCallback(menu
, title
, switchWSCommand
, NULL
);
247 menu
->flags
.realized
= 0;
251 if (!menu
->flags
.realized
)
257 updateMakeShortcutMenu(WMenu
*menu
, WWindow
*wwin
)
259 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_SHORTCUT
]->cascade
];
268 buflen
= strlen(_("Set Shortcut"))+16;
269 buffer
= wmalloc(buflen
);
271 for (i
=WO_ENTRIES
; i
<smenu
->entry_no
; i
++) {
273 int shortcutNo
= i
-WO_ENTRIES
;
274 WMenuEntry
*entry
= smenu
->entries
[i
];
275 WMArray
*shortSelWindows
= wwin
->screen_ptr
->shortcutWindows
[shortcutNo
];
277 snprintf(buffer
, buflen
, "%s %i", _("Set Shortcut"), shortcutNo
+1);
279 if (!shortSelWindows
) {
280 entry
->flags
.indicator_on
= 0;
282 entry
->flags
.indicator_on
= 1;
283 if (WMCountInArray(shortSelWindows
, wwin
))
284 entry
->flags
.indicator_type
= MI_DIAMOND
;
286 entry
->flags
.indicator_type
= MI_CHECK
;
289 if (strcmp(buffer
, entry
->text
)!=0) {
291 entry
->text
= wstrdup(buffer
);
292 smenu
->flags
.realized
= 0;
295 kcode
= wKeyBindings
[WKBD_WINDOW1
+shortcutNo
].keycode
;
298 if ((tmp
= XKeysymToString(XKeycodeToKeysym(dpy
, kcode
, 0)))
299 && (!entry
->rtext
|| strcmp(tmp
, entry
->rtext
)!=0)) {
302 entry
->rtext
= wstrdup(tmp
);
303 smenu
->flags
.realized
= 0;
305 wMenuSetEnabled(smenu
, i
, True
);
307 wMenuSetEnabled(smenu
, i
, False
);
311 smenu
->flags
.realized
= 0;
314 entry
->clientdata
= wwin
;
317 if (!smenu
->flags
.realized
)
323 updateOptionsMenu(WMenu
*menu
, WWindow
*wwin
)
325 WMenu
*smenu
= menu
->cascades
[menu
->entries
[MC_OPTIONS
]->cascade
];
327 /* keep on top check */
328 smenu
->entries
[WO_KEEP_ON_TOP
]->clientdata
= wwin
;
329 smenu
->entries
[WO_KEEP_ON_TOP
]->flags
.indicator_on
=
330 (wwin
->frame
->core
->stacking
->window_level
== WMFloatingLevel
)?1:0;
331 wMenuSetEnabled(smenu
, WO_KEEP_ON_TOP
, !wwin
->flags
.miniaturized
);
333 /* keep at bottom check */
334 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->clientdata
= wwin
;
335 smenu
->entries
[WO_KEEP_AT_BOTTOM
]->flags
.indicator_on
=
336 (wwin
->frame
->core
->stacking
->window_level
== WMSunkenLevel
)?1:0;
337 wMenuSetEnabled(smenu
, WO_KEEP_AT_BOTTOM
, !wwin
->flags
.miniaturized
);
339 /* omnipresent check */
340 smenu
->entries
[WO_OMNIPRESENT
]->clientdata
= wwin
;
341 smenu
->entries
[WO_OMNIPRESENT
]->flags
.indicator_on
= IS_OMNIPRESENT(wwin
);
343 smenu
->flags
.realized
= 0;
349 makeWorkspaceMenu(WScreen
*scr
)
353 menu
= wMenuCreate(scr
, NULL
, False
);
355 wwarning(_("could not create submenu for window menu"));
359 updateWorkspaceMenu(menu
);
366 makeMakeShortcutMenu(WScreen
*scr
, WMenu
*menu
)
373 menu = wMenuCreate(scr, NULL, False);
375 wwarning(_("could not create submenu for window menu"));
380 for (i
=0; i
<MAX_WINDOW_SHORTCUTS
; i
++) {
382 entry
= wMenuAddCallback(menu
, "", makeShortcutCommand
, NULL
);
384 entry
->flags
.indicator
= 1;
393 makeOptionsMenu(WScreen
*scr
)
398 menu
= wMenuCreate(scr
, NULL
, False
);
400 wwarning(_("could not create submenu for window menu"));
404 entry
= wMenuAddCallback(menu
, _("Keep on top"), execWindowOptionCommand
,
406 entry
->flags
.indicator
= 1;
407 entry
->flags
.indicator_type
= MI_CHECK
;
409 entry
= wMenuAddCallback(menu
, _("Keep at bottom"), execWindowOptionCommand
,
411 entry
->flags
.indicator
= 1;
412 entry
->flags
.indicator_type
= MI_CHECK
;
414 entry
= wMenuAddCallback(menu
, _("Omnipresent"), execWindowOptionCommand
,
416 entry
->flags
.indicator
= 1;
417 entry
->flags
.indicator_type
= MI_CHECK
;
424 createWindowMenu(WScreen
*scr
)
431 menu
= wMenuCreate(scr
, NULL
, False
);
433 * Warning: If you make some change that affects the order of the
434 * entries, you must update the command #defines in the top of
437 entry
= wMenuAddCallback(menu
, _("Maximize"), execMenuCommand
, NULL
);
438 if (wKeyBindings
[WKBD_MAXIMIZE
].keycode
!=0) {
439 kcode
= wKeyBindings
[WKBD_MAXIMIZE
].keycode
;
441 if (kcode
&& (tmp
= XKeysymToString(XKeycodeToKeysym(dpy
, kcode
, 0))))
442 entry
->rtext
= wstrdup(tmp
);
445 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
, IS_RESIZABLE(wwin
));
578 wMenuSetEnabled(menu
, MC_MOVERESIZE
, IS_RESIZABLE(wwin
)
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
);