Change to the linux kernel coding style
[wmaker-crm.git] / src / winmenu.c
1 /* winmenu.c - command menu for windows
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #include "wconfig.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32
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"
47
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
58
59 #define MC_CLOSE 9
60 #define MC_KILL 10
61
62 #define WO_KEEP_ON_TOP 0
63 #define WO_KEEP_AT_BOTTOM 1
64 #define WO_OMNIPRESENT 2
65 #define WO_ENTRIES 3
66
67 /**** Global data ***/
68 extern Time LastTimestamp;
69 extern Atom _XA_WM_DELETE_WINDOW;
70 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
71
72 extern WShortKey wKeyBindings[WKBD_LAST];
73
74 extern WPreferences wPreferences;
75
76 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
77
78 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
79 {
80 WWindow *wwin = (WWindow *) entry->clientdata;
81
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;
89
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;
96
97 case WO_OMNIPRESENT:
98 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
99 break;
100 }
101 }
102
103 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
104 {
105 WWindow *wwin = (WWindow *) entry->clientdata;
106 WApplication *wapp;
107
108 CloseWindowMenu(menu->frame->screen_ptr);
109
110 switch (entry->order) {
111 case MC_CLOSE:
112 /* send delete message */
113 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
114 break;
115
116 case MC_KILL:
117 wretain(wwin);
118 if (wPreferences.dont_confirm_kill
119 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
120 _
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);
125 }
126 wrelease(wwin);
127 break;
128
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);
137 }
138 }
139 break;
140
141 case MC_MAXIMIZE:
142 if (wwin->flags.maximized)
143 wUnmaximizeWindow(wwin);
144 else
145 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
146 break;
147
148 case MC_SHADE:
149 if (wwin->flags.shaded)
150 wUnshadeWindow(wwin);
151 else
152 wShadeWindow(wwin);
153 break;
154
155 case MC_SELECT:
156 if (!wwin->flags.miniaturized)
157 wSelectWindow(wwin, !wwin->flags.selected);
158 else
159 wIconSelect(wwin->icon);
160 break;
161
162 case MC_MOVERESIZE:
163 wKeyboardMoveResizeWindow(wwin);
164 break;
165
166 case MC_PROPERTIES:
167 wShowInspectorForWindow(wwin);
168 break;
169
170 case MC_HIDE:
171 wapp = wApplicationOf(wwin->main_window);
172 wHideApplication(wapp);
173 break;
174 }
175 }
176
177 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
178 {
179 WWindow *wwin = (WWindow *) entry->clientdata;
180
181 wSelectWindow(wwin, False);
182 wWindowChangeWorkspace(wwin, entry->order);
183 }
184
185 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
186 {
187 WWindow *wwin = (WWindow *) entry->clientdata;
188 WScreen *scr = wwin->screen_ptr;
189 int index = entry->order - WO_ENTRIES;
190
191 if (scr->shortcutWindows[index]) {
192 WMFreeArray(scr->shortcutWindows[index]);
193 scr->shortcutWindows[index] = NULL;
194 }
195
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);
203 }
204
205 wSelectWindow(wwin, !wwin->flags.selected);
206 XFlush(dpy);
207 wusleep(3000);
208 wSelectWindow(wwin, !wwin->flags.selected);
209 XFlush(dpy);
210 }
211
212 static void updateWorkspaceMenu(WMenu * menu)
213 {
214 WScreen *scr = menu->frame->screen_ptr;
215 char title[MAX_WORKSPACENAME_WIDTH + 1];
216 int i;
217
218 if (!menu)
219 return;
220
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;
229 }
230 } else {
231 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
232 title[MAX_WORKSPACENAME_WIDTH] = 0;
233
234 wMenuAddCallback(menu, title, switchWSCommand, NULL);
235
236 menu->flags.realized = 0;
237 }
238 }
239
240 if (!menu->flags.realized)
241 wMenuRealize(menu);
242 }
243
244 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
245 {
246 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
247 int i;
248 char *buffer;
249 int buflen;
250 KeyCode kcode;
251
252 if (!smenu)
253 return;
254
255 buflen = strlen(_("Set Shortcut")) + 16;
256 buffer = wmalloc(buflen);
257
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];
263
264 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
265
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;
274 }
275
276 if (strcmp(buffer, entry->text) != 0) {
277 wfree(entry->text);
278 entry->text = wstrdup(buffer);
279 smenu->flags.realized = 0;
280 }
281
282 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
283
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;
291 }
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;
299 }
300 }
301 entry->clientdata = wwin;
302 }
303 wfree(buffer);
304 if (!smenu->flags.realized)
305 wMenuRealize(smenu);
306 }
307
308 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
309 {
310 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
311
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);
317
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);
323
324 /* omnipresent check */
325 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
326 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
327
328 smenu->flags.realized = 0;
329 wMenuRealize(smenu);
330 }
331
332 static WMenu *makeWorkspaceMenu(WScreen * scr)
333 {
334 WMenu *menu;
335
336 menu = wMenuCreate(scr, NULL, False);
337 if (!menu) {
338 wwarning(_("could not create submenu for window menu"));
339 return NULL;
340 }
341
342 updateWorkspaceMenu(menu);
343
344 return menu;
345 }
346
347 static WMenu *makeMakeShortcutMenu(WScreen * scr, WMenu * menu)
348 {
349 /*
350 WMenu *menu;
351 */
352 int i;
353 /*
354 menu = wMenuCreate(scr, NULL, False);
355 if (!menu) {
356 wwarning(_("could not create submenu for window menu"));
357 return NULL;
358 }
359 */
360
361 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
362 WMenuEntry *entry;
363 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
364
365 entry->flags.indicator = 1;
366 }
367
368 return menu;
369 }
370
371 static WMenu *makeOptionsMenu(WScreen * scr)
372 {
373 WMenu *menu;
374 WMenuEntry *entry;
375
376 menu = wMenuCreate(scr, NULL, False);
377 if (!menu) {
378 wwarning(_("could not create submenu for window menu"));
379 return NULL;
380 }
381
382 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
383 entry->flags.indicator = 1;
384 entry->flags.indicator_type = MI_CHECK;
385
386 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
387 entry->flags.indicator = 1;
388 entry->flags.indicator_type = MI_CHECK;
389
390 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
391 entry->flags.indicator = 1;
392 entry->flags.indicator_type = MI_CHECK;
393
394 return menu;
395 }
396
397 static WMenu *createWindowMenu(WScreen * scr)
398 {
399 WMenu *menu;
400 KeyCode kcode;
401 WMenuEntry *entry;
402 char *tmp;
403
404 menu = wMenuCreate(scr, NULL, False);
405 /*
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.
409 */
410 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
411 if (wKeyBindings[WKBD_MAXIMIZE].keycode != 0) {
412 kcode = wKeyBindings[WKBD_MAXIMIZE].keycode;
413
414 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
415 entry->rtext = wstrdup(tmp);
416 }
417
418 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
419 if (wKeyBindings[WKBD_MINIATURIZE].keycode != 0) {
420 kcode = wKeyBindings[WKBD_MINIATURIZE].keycode;
421
422 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
423 entry->rtext = wstrdup(tmp);
424 }
425
426 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
427 if (wKeyBindings[WKBD_SHADE].keycode != 0) {
428 kcode = wKeyBindings[WKBD_SHADE].keycode;
429
430 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
431 entry->rtext = wstrdup(tmp);
432 }
433
434 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
435 if (wKeyBindings[WKBD_HIDE].keycode != 0) {
436 kcode = wKeyBindings[WKBD_HIDE].keycode;
437
438 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
439 entry->rtext = wstrdup(tmp);
440 }
441
442 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
443 if (wKeyBindings[WKBD_MOVERESIZE].keycode != 0) {
444 kcode = wKeyBindings[WKBD_MOVERESIZE].keycode;
445
446 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
447 entry->rtext = wstrdup(tmp);
448 }
449
450 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
451 if (wKeyBindings[WKBD_SELECT].keycode != 0) {
452 kcode = wKeyBindings[WKBD_SELECT].keycode;
453
454 if (kcode && (tmp = XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0))))
455 entry->rtext = wstrdup(tmp);
456 }
457
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);
462
463 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
464
465 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
466 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
467
468 /*
469 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
470 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
471 */
472
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);
478 }
479
480 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
481
482 return menu;
483 }
484
485 void CloseWindowMenu(WScreen * scr)
486 {
487 if (scr->window_menu) {
488 if (scr->window_menu->flags.mapped)
489 wMenuUnmap(scr->window_menu);
490
491 if (scr->window_menu->entries[0]->clientdata) {
492 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
493
494 wwin->flags.menu_open_for_me = 0;
495 }
496 scr->window_menu->entries[0]->clientdata = NULL;
497 }
498 }
499
500 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
501 {
502 WApplication *wapp = wApplicationOf(wwin->main_window);
503 WScreen *scr = wwin->screen_ptr;
504 int i;
505
506 updateOptionsMenu(menu, wwin);
507
508 updateMakeShortcutMenu(menu, wwin);
509
510 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
511
512 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
513
514 if (wwin->flags.miniaturized) {
515 static char *text = NULL;
516 if (!text)
517 text = _("Deminiaturize");
518
519 menu->entries[MC_MINIATURIZE]->text = text;
520 } else {
521 static char *text = NULL;
522 if (!text)
523 text = _("Miniaturize");
524
525 menu->entries[MC_MINIATURIZE]->text = text;
526 }
527
528 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
529
530 if (wwin->flags.maximized) {
531 static char *text = NULL;
532 if (!text)
533 text = _("Unmaximize");
534
535 menu->entries[MC_MAXIMIZE]->text = text;
536 } else {
537 static char *text = NULL;
538 if (!text)
539 text = _("Maximize");
540
541 menu->entries[MC_MAXIMIZE]->text = text;
542 }
543 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
544
545 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
546 && !wwin->flags.miniaturized);
547
548 if (wwin->flags.shaded) {
549 static char *text = NULL;
550 if (!text)
551 text = _("Unshade");
552
553 menu->entries[MC_SHADE]->text = text;
554 } else {
555 static char *text = NULL;
556 if (!text)
557 text = _("Shade");
558
559 menu->entries[MC_SHADE]->text = text;
560 }
561
562 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
563 && !wwin->flags.miniaturized);
564
565 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
566
567 if (!wwin->flags.inspector_open) {
568 wMenuSetEnabled(menu, MC_PROPERTIES, True);
569 } else {
570 wMenuSetEnabled(menu, MC_PROPERTIES, False);
571 }
572
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;
576 }
577
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);
584 }
585 }
586
587 menu->flags.realized = 0;
588 wMenuRealize(menu);
589 }
590
591 void OpenWindowMenu(WWindow * wwin, int x, int y, int keyboard)
592 {
593 WMenu *menu;
594 WScreen *scr = wwin->screen_ptr;
595
596 wwin->flags.menu_open_for_me = 1;
597
598 if (!scr->window_menu) {
599 scr->window_menu = createWindowMenu(scr);
600
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);
607 }
608
609 menu = scr->window_menu;
610 if (menu->flags.mapped) {
611 wMenuUnmap(menu);
612 if (menu->entries[0]->clientdata == wwin) {
613 return;
614 }
615 }
616
617 updateMenuForWindow(menu, wwin);
618
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;
624
625 if (!wwin->flags.internal_window)
626 wMenuMapAt(menu, x, y, keyboard);
627 }
628
629 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
630 {
631 WMenu *menu;
632 WScreen *scr = wwin->screen_ptr;
633
634 wwin->flags.menu_open_for_me = 1;
635
636 if (!scr->window_menu) {
637 scr->window_menu = createWindowMenu(scr);
638
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);
645 }
646
647 menu = scr->window_menu;
648 if (menu->flags.mapped) {
649 wMenuUnmap(menu);
650 if (menu->entries[0]->clientdata == wwin) {
651 return;
652 }
653 }
654
655 updateMenuForWindow(menu, wwin);
656
657 x -= menu->frame->core->width / 2;
658
659 wMenuMapAt(menu, x, y, False);
660 }