Change to the linux kernel coding style
[wmaker-crm.git] / src / winmenu.c
blob36db40f10127e92877afb37c4399552580795e26
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,
20 * USA.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
34 #include "actions.h"
35 #include "menu.h"
36 #include "funcs.h"
37 #include "window.h"
38 #include "client.h"
39 #include "application.h"
40 #include "keybind.h"
41 #include "framewin.h"
42 #include "workspace.h"
43 #include "winspector.h"
44 #include "dialog.h"
45 #include "stacking.h"
46 #include "icon.h"
48 #define MC_MAXIMIZE 0
49 #define MC_MINIATURIZE 1
50 #define MC_SHADE 2
51 #define MC_HIDE 3
52 #define MC_MOVERESIZE 4
53 #define MC_SELECT 5
54 #define MC_DUMMY_MOVETO 6
55 #define MC_PROPERTIES 7
56 #define MC_OPTIONS 8
57 #define MC_SHORTCUT 8
59 #define MC_CLOSE 9
60 #define MC_KILL 10
62 #define WO_KEEP_ON_TOP 0
63 #define WO_KEEP_AT_BOTTOM 1
64 #define WO_OMNIPRESENT 2
65 #define WO_ENTRIES 3
67 /**** Global data ***/
68 extern Time LastTimestamp;
69 extern Atom _XA_WM_DELETE_WINDOW;
70 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
72 extern WShortKey wKeyBindings[WKBD_LAST];
74 extern WPreferences wPreferences;
76 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
78 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
80 WWindow *wwin = (WWindow *) entry->clientdata;
82 switch (entry->order) {
83 case WO_KEEP_ON_TOP:
84 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
85 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
86 else
87 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
88 break;
90 case WO_KEEP_AT_BOTTOM:
91 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
92 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
93 else
94 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
95 break;
97 case WO_OMNIPRESENT:
98 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
99 break;
103 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
105 WWindow *wwin = (WWindow *) entry->clientdata;
106 WApplication *wapp;
108 CloseWindowMenu(menu->frame->screen_ptr);
110 switch (entry->order) {
111 case MC_CLOSE:
112 /* send delete message */
113 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
114 break;
116 case MC_KILL:
117 wretain(wwin);
118 if (wPreferences.dont_confirm_kill
119 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
121 ("This will kill the application.\nAny unsaved changes will be lost.\nPlease confirm."),
122 _("Yes"), _("No"), NULL) == WAPRDefault) {
123 if (!wwin->flags.destroyed)
124 wClientKill(wwin);
126 wrelease(wwin);
127 break;
129 case MC_MINIATURIZE:
130 if (wwin->flags.miniaturized) {
131 wDeiconifyWindow(wwin);
132 } else {
133 if (wwin->protocols.MINIATURIZE_WINDOW) {
134 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
135 } else {
136 wIconifyWindow(wwin);
139 break;
141 case MC_MAXIMIZE:
142 if (wwin->flags.maximized)
143 wUnmaximizeWindow(wwin);
144 else
145 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
146 break;
148 case MC_SHADE:
149 if (wwin->flags.shaded)
150 wUnshadeWindow(wwin);
151 else
152 wShadeWindow(wwin);
153 break;
155 case MC_SELECT:
156 if (!wwin->flags.miniaturized)
157 wSelectWindow(wwin, !wwin->flags.selected);
158 else
159 wIconSelect(wwin->icon);
160 break;
162 case MC_MOVERESIZE:
163 wKeyboardMoveResizeWindow(wwin);
164 break;
166 case MC_PROPERTIES:
167 wShowInspectorForWindow(wwin);
168 break;
170 case MC_HIDE:
171 wapp = wApplicationOf(wwin->main_window);
172 wHideApplication(wapp);
173 break;
177 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
179 WWindow *wwin = (WWindow *) entry->clientdata;
181 wSelectWindow(wwin, False);
182 wWindowChangeWorkspace(wwin, entry->order);
185 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
187 WWindow *wwin = (WWindow *) entry->clientdata;
188 WScreen *scr = wwin->screen_ptr;
189 int index = entry->order - WO_ENTRIES;
191 if (scr->shortcutWindows[index]) {
192 WMFreeArray(scr->shortcutWindows[index]);
193 scr->shortcutWindows[index] = NULL;
196 if (wwin->flags.selected && scr->selected_windows) {
197 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
198 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
199 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
200 } else {
201 scr->shortcutWindows[index] = WMCreateArray(4);
202 WMAddToArray(scr->shortcutWindows[index], wwin);
205 wSelectWindow(wwin, !wwin->flags.selected);
206 XFlush(dpy);
207 wusleep(3000);
208 wSelectWindow(wwin, !wwin->flags.selected);
209 XFlush(dpy);
212 static void updateWorkspaceMenu(WMenu * menu)
214 WScreen *scr = menu->frame->screen_ptr;
215 char title[MAX_WORKSPACENAME_WIDTH + 1];
216 int i;
218 if (!menu)
219 return;
221 for (i = 0; i < scr->workspace_count; i++) {
222 if (i < menu->entry_no) {
223 if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
224 wfree(menu->entries[i]->text);
225 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
226 title[MAX_WORKSPACENAME_WIDTH] = 0;
227 menu->entries[i]->text = wstrdup(title);
228 menu->flags.realized = 0;
230 } else {
231 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
232 title[MAX_WORKSPACENAME_WIDTH] = 0;
234 wMenuAddCallback(menu, title, switchWSCommand, NULL);
236 menu->flags.realized = 0;
240 if (!menu->flags.realized)
241 wMenuRealize(menu);
244 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
246 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
247 int i;
248 char *buffer;
249 int buflen;
250 KeyCode kcode;
252 if (!smenu)
253 return;
255 buflen = strlen(_("Set Shortcut")) + 16;
256 buffer = wmalloc(buflen);
258 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
259 char *tmp;
260 int shortcutNo = i - WO_ENTRIES;
261 WMenuEntry *entry = smenu->entries[i];
262 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
264 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
266 if (!shortSelWindows) {
267 entry->flags.indicator_on = 0;
268 } else {
269 entry->flags.indicator_on = 1;
270 if (WMCountInArray(shortSelWindows, wwin))
271 entry->flags.indicator_type = MI_DIAMOND;
272 else
273 entry->flags.indicator_type = MI_CHECK;
276 if (strcmp(buffer, entry->text) != 0) {
277 wfree(entry->text);
278 entry->text = wstrdup(buffer);
279 smenu->flags.realized = 0;
282 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
284 if (kcode) {
285 if ((tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)))
286 && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) {
287 if (entry->rtext)
288 wfree(entry->rtext);
289 entry->rtext = wstrdup(tmp);
290 smenu->flags.realized = 0;
292 wMenuSetEnabled(smenu, i, True);
293 } else {
294 wMenuSetEnabled(smenu, i, False);
295 if (entry->rtext) {
296 wfree(entry->rtext);
297 entry->rtext = NULL;
298 smenu->flags.realized = 0;
301 entry->clientdata = wwin;
303 wfree(buffer);
304 if (!smenu->flags.realized)
305 wMenuRealize(smenu);
308 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
310 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
312 /* keep on top check */
313 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
314 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
315 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
316 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
318 /* keep at bottom check */
319 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
320 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
321 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
322 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
324 /* omnipresent check */
325 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
326 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
328 smenu->flags.realized = 0;
329 wMenuRealize(smenu);
332 static WMenu *makeWorkspaceMenu(WScreen * scr)
334 WMenu *menu;
336 menu = wMenuCreate(scr, NULL, False);
337 if (!menu) {
338 wwarning(_("could not create submenu for window menu"));
339 return NULL;
342 updateWorkspaceMenu(menu);
344 return menu;
347 static WMenu *makeMakeShortcutMenu(WScreen * scr, WMenu * menu)
350 WMenu *menu;
352 int i;
354 menu = wMenuCreate(scr, NULL, False);
355 if (!menu) {
356 wwarning(_("could not create submenu for window menu"));
357 return NULL;
361 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
362 WMenuEntry *entry;
363 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
365 entry->flags.indicator = 1;
368 return menu;
371 static WMenu *makeOptionsMenu(WScreen * scr)
373 WMenu *menu;
374 WMenuEntry *entry;
376 menu = wMenuCreate(scr, NULL, False);
377 if (!menu) {
378 wwarning(_("could not create submenu for window menu"));
379 return NULL;
382 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
383 entry->flags.indicator = 1;
384 entry->flags.indicator_type = MI_CHECK;
386 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
387 entry->flags.indicator = 1;
388 entry->flags.indicator_type = MI_CHECK;
390 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
391 entry->flags.indicator = 1;
392 entry->flags.indicator_type = MI_CHECK;
394 return menu;
397 static WMenu *createWindowMenu(WScreen * scr)
399 WMenu *menu;
400 KeyCode kcode;
401 WMenuEntry *entry;
402 char *tmp;
404 menu = wMenuCreate(scr, NULL, False);
406 * Warning: If you make some change that affects the order of the
407 * entries, you must update the command #defines in the top of
408 * this file.
410 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
411 if (wKeyBindings[WKBD_MAXIMIZE].keycode != 0) {
412 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
414 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
415 entry->rtext = wstrdup(tmp);
418 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
419 if (wKeyBindings[WKBD_MINIATURIZE].keycode != 0) {
420 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
422 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
423 entry->rtext = wstrdup(tmp);
426 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
427 if (wKeyBindings[WKBD_SHADE].keycode != 0) {
428 kcode = wKeyBindings[WKBD_SHADE].keycode;
430 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
431 entry->rtext = wstrdup(tmp);
434 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
435 if (wKeyBindings[WKBD_HIDE].keycode != 0) {
436 kcode = wKeyBindings[WKBD_HIDE].keycode;
438 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
439 entry->rtext = wstrdup(tmp);
442 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
443 if (wKeyBindings[WKBD_MOVERESIZE].keycode != 0) {
444 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
446 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
447 entry->rtext = wstrdup(tmp);
450 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
451 if (wKeyBindings[WKBD_SELECT].keycode != 0) {
452 kcode = wKeyBindings[WKBD_SELECT].keycode;
454 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
455 entry->rtext = wstrdup(tmp);
458 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
459 scr->workspace_submenu = makeWorkspaceMenu(scr);
460 if (scr->workspace_submenu)
461 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
463 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
465 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
466 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
469 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
470 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
473 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
474 if (wKeyBindings[WKBD_CLOSE].keycode != 0) {
475 kcode = wKeyBindings[WKBD_CLOSE].keycode;
476 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
477 entry->rtext = wstrdup(tmp);
480 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
482 return menu;
485 void CloseWindowMenu(WScreen * scr)
487 if (scr->window_menu) {
488 if (scr->window_menu->flags.mapped)
489 wMenuUnmap(scr->window_menu);
491 if (scr->window_menu->entries[0]->clientdata) {
492 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
494 wwin->flags.menu_open_for_me = 0;
496 scr->window_menu->entries[0]->clientdata = NULL;
500 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
502 WApplication *wapp = wApplicationOf(wwin->main_window);
503 WScreen *scr = wwin->screen_ptr;
504 int i;
506 updateOptionsMenu(menu, wwin);
508 updateMakeShortcutMenu(menu, wwin);
510 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
512 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
514 if (wwin->flags.miniaturized) {
515 static char *text = NULL;
516 if (!text)
517 text = _("Deminiaturize");
519 menu->entries[MC_MINIATURIZE]->text = text;
520 } else {
521 static char *text = NULL;
522 if (!text)
523 text = _("Miniaturize");
525 menu->entries[MC_MINIATURIZE]->text = text;
528 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
530 if (wwin->flags.maximized) {
531 static char *text = NULL;
532 if (!text)
533 text = _("Unmaximize");
535 menu->entries[MC_MAXIMIZE]->text = text;
536 } else {
537 static char *text = NULL;
538 if (!text)
539 text = _("Maximize");
541 menu->entries[MC_MAXIMIZE]->text = text;
543 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
545 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
546 && !wwin->flags.miniaturized);
548 if (wwin->flags.shaded) {
549 static char *text = NULL;
550 if (!text)
551 text = _("Unshade");
553 menu->entries[MC_SHADE]->text = text;
554 } else {
555 static char *text = NULL;
556 if (!text)
557 text = _("Shade");
559 menu->entries[MC_SHADE]->text = text;
562 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
563 && !wwin->flags.miniaturized);
565 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
567 if (!wwin->flags.inspector_open) {
568 wMenuSetEnabled(menu, MC_PROPERTIES, True);
569 } else {
570 wMenuSetEnabled(menu, MC_PROPERTIES, False);
573 /* set the client data of the entries to the window */
574 for (i = 0; i < menu->entry_no; i++) {
575 menu->entries[i]->clientdata = wwin;
578 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
579 scr->workspace_submenu->entries[i]->clientdata = wwin;
580 if (i == scr->current_workspace) {
581 wMenuSetEnabled(scr->workspace_submenu, i, False);
582 } else {
583 wMenuSetEnabled(scr->workspace_submenu, i, True);
587 menu->flags.realized = 0;
588 wMenuRealize(menu);
591 void OpenWindowMenu(WWindow * wwin, int x, int y, int keyboard)
593 WMenu *menu;
594 WScreen *scr = wwin->screen_ptr;
596 wwin->flags.menu_open_for_me = 1;
598 if (!scr->window_menu) {
599 scr->window_menu = createWindowMenu(scr);
601 /* hack to save some memory allocation/deallocation */
602 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
603 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
604 wfree(scr->window_menu->entries[MC_SHADE]->text);
605 } else {
606 updateWorkspaceMenu(scr->workspace_submenu);
609 menu = scr->window_menu;
610 if (menu->flags.mapped) {
611 wMenuUnmap(menu);
612 if (menu->entries[0]->clientdata == wwin) {
613 return;
617 updateMenuForWindow(menu, wwin);
619 x -= menu->frame->core->width / 2;
620 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
621 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
622 if (x < wwin->frame_x)
623 x = wwin->frame_x;
625 if (!wwin->flags.internal_window)
626 wMenuMapAt(menu, x, y, keyboard);
629 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
631 WMenu *menu;
632 WScreen *scr = wwin->screen_ptr;
634 wwin->flags.menu_open_for_me = 1;
636 if (!scr->window_menu) {
637 scr->window_menu = createWindowMenu(scr);
639 /* hack to save some memory allocation/deallocation */
640 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
641 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
642 wfree(scr->window_menu->entries[MC_SHADE]->text);
643 } else {
644 updateWorkspaceMenu(scr->workspace_submenu);
647 menu = scr->window_menu;
648 if (menu->flags.mapped) {
649 wMenuUnmap(menu);
650 if (menu->entries[0]->clientdata == wwin) {
651 return;
655 updateMenuForWindow(menu, wwin);
657 x -= menu->frame->core->width / 2;
659 wMenuMapAt(menu, x, y, False);