wmaker: Changed math on floating point by integer operation
[wmaker-crm.git] / src / winmenu.c
blob07735d23b86e6f96ae148c60f9923e56aaee6fb1
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.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/XKBlib.h>
33 #include "WindowMaker.h"
34 #include "actions.h"
35 #include "menu.h"
36 #include "main.h"
37 #include "window.h"
38 #include "client.h"
39 #include "application.h"
40 #include "keybind.h"
41 #include "misc.h"
42 #include "framewin.h"
43 #include "workspace.h"
44 #include "winspector.h"
45 #include "dialog.h"
46 #include "stacking.h"
47 #include "icon.h"
48 #include "xinerama.h"
49 #include "winmenu.h"
51 #define MC_MAXIMIZE 0
52 #define MC_MINIATURIZE 1
53 #define MC_SHADE 2
54 #define MC_HIDE 3
55 #define MC_MOVERESIZE 4
56 #define MC_SELECT 5
57 #define MC_DUMMY_MOVETO 6
58 #define MC_PROPERTIES 7
59 #define MC_OPTIONS 8
60 #define MC_SHORTCUT 8
61 #define MC_RELAUNCH 9
63 #define MC_CLOSE 10
64 #define MC_KILL 11
66 #define WO_KEEP_ON_TOP 0
67 #define WO_KEEP_AT_BOTTOM 1
68 #define WO_OMNIPRESENT 2
69 #define WO_ENTRIES 3
71 /**** Global data ***/
72 extern Time LastTimestamp;
73 extern Atom _XA_WM_DELETE_WINDOW;
74 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
76 extern WShortKey wKeyBindings[WKBD_LAST];
78 extern WPreferences wPreferences;
80 static void updateOptionsMenu(WMenu * menu, WWindow * wwin);
82 static void execWindowOptionCommand(WMenu * menu, WMenuEntry * entry)
84 WWindow *wwin = (WWindow *) entry->clientdata;
86 switch (entry->order) {
87 case WO_KEEP_ON_TOP:
88 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
89 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
90 else
91 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
92 break;
94 case WO_KEEP_AT_BOTTOM:
95 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
96 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
97 else
98 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
99 break;
101 case WO_OMNIPRESENT:
102 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
103 break;
107 static void execMenuCommand(WMenu * menu, WMenuEntry * entry)
109 WWindow *wwin = (WWindow *) entry->clientdata;
110 WApplication *wapp;
112 CloseWindowMenu(menu->frame->screen_ptr);
114 switch (entry->order) {
115 case MC_CLOSE:
116 /* send delete message */
117 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
118 break;
120 case MC_KILL:
121 wretain(wwin);
122 if (wPreferences.dont_confirm_kill
123 || 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)
128 wClientKill(wwin);
130 wrelease(wwin);
131 break;
133 case MC_MINIATURIZE:
134 if (wwin->flags.miniaturized) {
135 wDeiconifyWindow(wwin);
136 } else {
137 if (wwin->protocols.MINIATURIZE_WINDOW) {
138 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
139 } else {
140 wIconifyWindow(wwin);
143 break;
145 case MC_MAXIMIZE:
146 if (wwin->flags.maximized)
147 wUnmaximizeWindow(wwin);
148 else
149 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL);
150 break;
152 case MC_SHADE:
153 if (wwin->flags.shaded)
154 wUnshadeWindow(wwin);
155 else
156 wShadeWindow(wwin);
157 break;
159 case MC_SELECT:
160 if (!wwin->flags.miniaturized)
161 wSelectWindow(wwin, !wwin->flags.selected);
162 else
163 wIconSelect(wwin->icon);
164 break;
166 case MC_MOVERESIZE:
167 wKeyboardMoveResizeWindow(wwin);
168 break;
170 case MC_PROPERTIES:
171 wShowInspectorForWindow(wwin);
172 break;
174 case MC_RELAUNCH:
175 (void) RelaunchWindow(wwin);
176 break;
178 case MC_HIDE:
179 wapp = wApplicationOf(wwin->main_window);
180 wHideApplication(wapp);
181 break;
185 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
187 WWindow *wwin = (WWindow *) entry->clientdata;
189 wSelectWindow(wwin, False);
190 wWindowChangeWorkspace(wwin, entry->order);
193 static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
195 WWindow *wwin = (WWindow *) entry->clientdata;
196 WScreen *scr = wwin->screen_ptr;
197 int index = entry->order - WO_ENTRIES;
199 if (scr->shortcutWindows[index]) {
200 WMFreeArray(scr->shortcutWindows[index]);
201 scr->shortcutWindows[index] = NULL;
204 if (wwin->flags.selected && scr->selected_windows) {
205 scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
206 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
207 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
208 } else {
209 scr->shortcutWindows[index] = WMCreateArray(4);
210 WMAddToArray(scr->shortcutWindows[index], wwin);
213 wSelectWindow(wwin, !wwin->flags.selected);
214 XFlush(dpy);
215 wusleep(3000);
216 wSelectWindow(wwin, !wwin->flags.selected);
217 XFlush(dpy);
220 static void updateWorkspaceMenu(WMenu * menu)
222 WScreen *scr = menu->frame->screen_ptr;
223 char title[MAX_WORKSPACENAME_WIDTH + 1];
224 WMenuEntry *entry;
225 int i;
227 for (i = 0; i < scr->workspace_count; i++) {
228 if (i < menu->entry_no) {
230 entry = menu->entries[i];
231 if (strcmp(entry->text, scr->workspaces[i]->name) != 0) {
232 wfree(entry->text);
233 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
234 title[MAX_WORKSPACENAME_WIDTH] = 0;
235 menu->entries[i]->text = wstrdup(title);
236 menu->entries[i]->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
237 menu->flags.realized = 0;
239 } else {
240 strncpy(title, scr->workspaces[i]->name, MAX_WORKSPACENAME_WIDTH);
241 title[MAX_WORKSPACENAME_WIDTH] = 0;
243 entry = wMenuAddCallback(menu, title, switchWSCommand, NULL);
244 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + i]);
246 menu->flags.realized = 0;
249 /* workspace shortcut labels */
250 if (i / 10 == scr->current_workspace / 10)
251 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVE_WORKSPACE1 + (i % 10)]);
252 else
253 entry->rtext = NULL;
256 if (!menu->flags.realized)
257 wMenuRealize(menu);
260 static void updateMakeShortcutMenu(WMenu * menu, WWindow * wwin)
262 WMenu *smenu = menu->cascades[menu->entries[MC_SHORTCUT]->cascade];
263 int i;
264 char *buffer;
265 int buflen;
266 KeyCode kcode;
268 if (!smenu)
269 return;
271 buflen = strlen(_("Set Shortcut")) + 16;
272 buffer = wmalloc(buflen);
274 for (i = WO_ENTRIES; i < smenu->entry_no; i++) {
275 char *tmp;
276 int shortcutNo = i - WO_ENTRIES;
277 WMenuEntry *entry = smenu->entries[i];
278 WMArray *shortSelWindows = wwin->screen_ptr->shortcutWindows[shortcutNo];
280 snprintf(buffer, buflen, "%s %i", _("Set Shortcut"), shortcutNo + 1);
282 if (!shortSelWindows) {
283 entry->flags.indicator_on = 0;
284 } else {
285 entry->flags.indicator_on = 1;
286 if (WMCountInArray(shortSelWindows, wwin))
287 entry->flags.indicator_type = MI_DIAMOND;
288 else
289 entry->flags.indicator_type = MI_CHECK;
292 if (strcmp(buffer, entry->text) != 0) {
293 wfree(entry->text);
294 entry->text = wstrdup(buffer);
295 smenu->flags.realized = 0;
298 kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode;
300 if (kcode) {
301 if ((tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]))
302 && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) {
303 if (entry->rtext)
304 wfree(entry->rtext);
305 entry->rtext = tmp;
306 smenu->flags.realized = 0;
308 wMenuSetEnabled(smenu, i, True);
309 } else {
310 wMenuSetEnabled(smenu, i, False);
311 if (entry->rtext) {
312 wfree(entry->rtext);
313 entry->rtext = NULL;
314 smenu->flags.realized = 0;
317 entry->clientdata = wwin;
319 wfree(buffer);
320 if (!smenu->flags.realized)
321 wMenuRealize(smenu);
324 static void updateOptionsMenu(WMenu * menu, WWindow * wwin)
326 WMenu *smenu = menu->cascades[menu->entries[MC_OPTIONS]->cascade];
328 /* keep on top check */
329 smenu->entries[WO_KEEP_ON_TOP]->clientdata = wwin;
330 smenu->entries[WO_KEEP_ON_TOP]->flags.indicator_on =
331 (wwin->frame->core->stacking->window_level == WMFloatingLevel) ? 1 : 0;
332 wMenuSetEnabled(smenu, WO_KEEP_ON_TOP, !wwin->flags.miniaturized);
334 /* keep at bottom check */
335 smenu->entries[WO_KEEP_AT_BOTTOM]->clientdata = wwin;
336 smenu->entries[WO_KEEP_AT_BOTTOM]->flags.indicator_on =
337 (wwin->frame->core->stacking->window_level == WMSunkenLevel) ? 1 : 0;
338 wMenuSetEnabled(smenu, WO_KEEP_AT_BOTTOM, !wwin->flags.miniaturized);
340 /* omnipresent check */
341 smenu->entries[WO_OMNIPRESENT]->clientdata = wwin;
342 smenu->entries[WO_OMNIPRESENT]->flags.indicator_on = IS_OMNIPRESENT(wwin);
344 smenu->flags.realized = 0;
345 wMenuRealize(smenu);
348 static WMenu *makeWorkspaceMenu(WScreen * scr)
350 WMenu *menu;
352 menu = wMenuCreate(scr, NULL, False);
353 if (!menu) {
354 wwarning(_("could not create submenu for window menu"));
355 return NULL;
358 updateWorkspaceMenu(menu);
360 return menu;
363 static WMenu *makeMakeShortcutMenu(WScreen * scr, WMenu * menu)
366 WMenu *menu;
368 int i;
370 menu = wMenuCreate(scr, NULL, False);
371 if (!menu) {
372 wwarning(_("could not create submenu for window menu"));
373 return NULL;
377 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
378 WMenuEntry *entry;
379 entry = wMenuAddCallback(menu, "", makeShortcutCommand, NULL);
381 entry->flags.indicator = 1;
384 return menu;
387 static WMenu *makeOptionsMenu(WScreen * scr)
389 WMenu *menu;
390 WMenuEntry *entry;
392 menu = wMenuCreate(scr, NULL, False);
393 if (!menu) {
394 wwarning(_("could not create submenu for window menu"));
395 return NULL;
398 entry = wMenuAddCallback(menu, _("Keep on top"), execWindowOptionCommand, NULL);
399 entry->flags.indicator = 1;
400 entry->flags.indicator_type = MI_CHECK;
402 entry = wMenuAddCallback(menu, _("Keep at bottom"), execWindowOptionCommand, NULL);
403 entry->flags.indicator = 1;
404 entry->flags.indicator_type = MI_CHECK;
406 entry = wMenuAddCallback(menu, _("Omnipresent"), execWindowOptionCommand, NULL);
407 entry->flags.indicator = 1;
408 entry->flags.indicator_type = MI_CHECK;
410 return menu;
413 static WMenu *createWindowMenu(WScreen * scr)
415 WMenu *menu;
416 WMenuEntry *entry;
418 menu = wMenuCreate(scr, NULL, False);
420 * Warning: If you make some change that affects the order of the
421 * entries, you must update the command #defines in the top of
422 * this file.
424 entry = wMenuAddCallback(menu, _("Maximize"), execMenuCommand, NULL);
425 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MAXIMIZE]);
427 entry = wMenuAddCallback(menu, _("Miniaturize"), execMenuCommand, NULL);
428 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MINIATURIZE]);
430 entry = wMenuAddCallback(menu, _("Shade"), execMenuCommand, NULL);
431 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SHADE]);
433 entry = wMenuAddCallback(menu, _("Hide"), execMenuCommand, NULL);
434 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_HIDE]);
436 entry = wMenuAddCallback(menu, _("Resize/Move"), execMenuCommand, NULL);
437 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_MOVERESIZE]);
439 entry = wMenuAddCallback(menu, _("Select"), execMenuCommand, NULL);
440 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_SELECT]);
442 entry = wMenuAddCallback(menu, _("Move To"), NULL, NULL);
443 scr->workspace_submenu = makeWorkspaceMenu(scr);
444 if (scr->workspace_submenu)
445 wMenuEntrySetCascade(menu, entry, scr->workspace_submenu);
447 entry = wMenuAddCallback(menu, _("Attributes..."), execMenuCommand, NULL);
449 entry = wMenuAddCallback(menu, _("Options"), NULL, NULL);
450 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr, makeOptionsMenu(scr)));
453 entry = wMenuAddCallback(menu, _("Select Shortcut"), NULL, NULL);
454 wMenuEntrySetCascade(menu, entry, makeMakeShortcutMenu(scr));
457 entry = wMenuAddCallback(menu, _("Launch"), execMenuCommand, NULL);
458 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_RELAUNCH]);
460 entry = wMenuAddCallback(menu, _("Close"), execMenuCommand, NULL);
461 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_CLOSE]);
463 entry = wMenuAddCallback(menu, _("Kill"), execMenuCommand, NULL);
465 return menu;
468 void CloseWindowMenu(WScreen * scr)
470 if (scr->window_menu) {
471 if (scr->window_menu->flags.mapped)
472 wMenuUnmap(scr->window_menu);
474 if (scr->window_menu->entries[0]->clientdata) {
475 WWindow *wwin = (WWindow *) scr->window_menu->entries[0]->clientdata;
477 wwin->flags.menu_open_for_me = 0;
479 scr->window_menu->entries[0]->clientdata = NULL;
483 static void updateMenuForWindow(WMenu * menu, WWindow * wwin)
485 WApplication *wapp = wApplicationOf(wwin->main_window);
486 WScreen *scr = wwin->screen_ptr;
487 int i;
489 updateOptionsMenu(menu, wwin);
491 updateMakeShortcutMenu(menu, wwin);
493 wMenuSetEnabled(menu, MC_HIDE, wapp != NULL && !WFLAGP(wapp->main_window_desc, no_appicon));
495 wMenuSetEnabled(menu, MC_CLOSE, (wwin->protocols.DELETE_WINDOW && !WFLAGP(wwin, no_closable)));
497 if (wwin->flags.miniaturized) {
498 static char *text = NULL;
499 if (!text)
500 text = _("Deminiaturize");
502 menu->entries[MC_MINIATURIZE]->text = text;
503 } else {
504 static char *text = NULL;
505 if (!text)
506 text = _("Miniaturize");
508 menu->entries[MC_MINIATURIZE]->text = text;
511 wMenuSetEnabled(menu, MC_MINIATURIZE, !WFLAGP(wwin, no_miniaturizable));
513 if (wwin->flags.maximized) {
514 static char *text = NULL;
515 if (!text)
516 text = _("Unmaximize");
518 menu->entries[MC_MAXIMIZE]->text = text;
519 } else {
520 static char *text = NULL;
521 if (!text)
522 text = _("Maximize");
524 menu->entries[MC_MAXIMIZE]->text = text;
526 wMenuSetEnabled(menu, MC_MAXIMIZE, IS_RESIZABLE(wwin));
528 wMenuSetEnabled(menu, MC_MOVERESIZE, IS_RESIZABLE(wwin)
529 && !wwin->flags.miniaturized);
531 if (wwin->flags.shaded) {
532 static char *text = NULL;
533 if (!text)
534 text = _("Unshade");
536 menu->entries[MC_SHADE]->text = text;
537 } else {
538 static char *text = NULL;
539 if (!text)
540 text = _("Shade");
542 menu->entries[MC_SHADE]->text = text;
545 wMenuSetEnabled(menu, MC_SHADE, !WFLAGP(wwin, no_shadeable)
546 && !wwin->flags.miniaturized);
548 if (wwin->flags.selected) {
549 static char *text = NULL;
550 if (!text)
551 text = _("Deselect");
553 menu->entries[MC_SELECT]->text = text;
554 } else {
555 static char *text = NULL;
556 if (!text)
557 text = _("Select");
559 menu->entries[MC_SELECT]->text = text;
562 wMenuSetEnabled(menu, MC_DUMMY_MOVETO, !IS_OMNIPRESENT(wwin));
564 if (!wwin->flags.inspector_open) {
565 wMenuSetEnabled(menu, MC_PROPERTIES, True);
566 } else {
567 wMenuSetEnabled(menu, MC_PROPERTIES, False);
570 /* set the client data of the entries to the window */
571 for (i = 0; i < menu->entry_no; i++) {
572 menu->entries[i]->clientdata = wwin;
575 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
576 scr->workspace_submenu->entries[i]->clientdata = wwin;
577 if (i == scr->current_workspace) {
578 wMenuSetEnabled(scr->workspace_submenu, i, False);
579 } else {
580 wMenuSetEnabled(scr->workspace_submenu, i, True);
584 menu->flags.realized = 0;
585 wMenuRealize(menu);
588 static WMenu *open_window_menu_core(WWindow *wwin, int x, int y)
590 WScreen *scr = wwin->screen_ptr;
591 WMenu *menu;
593 wwin->flags.menu_open_for_me = 1;
595 if (!scr->window_menu) {
596 scr->window_menu = createWindowMenu(scr);
598 /* hack to save some memory allocation/deallocation */
599 wfree(scr->window_menu->entries[MC_MINIATURIZE]->text);
600 wfree(scr->window_menu->entries[MC_MAXIMIZE]->text);
601 wfree(scr->window_menu->entries[MC_SHADE]->text);
602 wfree(scr->window_menu->entries[MC_SELECT]->text);
603 } else {
604 updateWorkspaceMenu(scr->workspace_submenu);
607 menu = scr->window_menu;
608 if (menu->flags.mapped) {
609 wMenuUnmap(menu);
610 if (menu->entries[0]->clientdata == wwin)
611 return NULL;
614 updateMenuForWindow(menu, wwin);
616 return menu;
619 static void prepare_menu_position(WMenu *menu, int x, int y)
621 WMRect rect;
623 rect = wGetRectForHead(menu->frame->screen_ptr,
624 wGetHeadForPointerLocation(menu->frame->screen_ptr));
625 if (x < rect.pos.x - menu->frame->core->width / 2)
626 x = rect.pos.x - menu->frame->core->width / 2;
627 if (y < rect.pos.y)
628 y = rect.pos.y;
631 void OpenWindowMenu(WWindow *wwin, int x, int y, int keyboard)
633 WMenu *menu;
635 menu = open_window_menu_core(wwin, x, y);
636 if (!menu)
637 return;
639 /* Specific menu position */
640 x -= menu->frame->core->width / 2;
641 if (x + menu->frame->core->width > wwin->frame_x + wwin->frame->core->width)
642 x = wwin->frame_x + wwin->frame->core->width - menu->frame->core->width;
643 if (x < wwin->frame_x)
644 x = wwin->frame_x;
646 /* Common menu position */
647 prepare_menu_position(menu, x, y);
649 if (!wwin->flags.internal_window)
650 wMenuMapAt(menu, x, y, keyboard);
653 void OpenWindowMenu2(WWindow *wwin, int x, int y, int keyboard)
655 int i;
656 WMenu *menu;
657 WScreen *scr = wwin->screen_ptr;
659 menu = open_window_menu_core(wwin, x, y);
660 if (!menu)
661 return;
663 /* Specific menu position */
664 for (i = 0; i < scr->workspace_submenu->entry_no; i++) {
665 scr->workspace_submenu->entries[i]->clientdata = wwin;
666 wMenuSetEnabled(scr->workspace_submenu, i, True);
669 x -= menu->frame->core->width / 2;
671 /* Common menu position */
672 prepare_menu_position(menu, x, y);
674 if (!wwin->flags.internal_window)
675 wMenuMapAt(menu, x, y, keyboard);
678 void OpenMiniwindowMenu(WWindow * wwin, int x, int y)
680 WMenu *menu;
682 menu = open_window_menu_core(wwin, x, y);
683 if (!menu)
684 return;
686 x -= menu->frame->core->width / 2;
688 wMenuMapAt(menu, x, y, False);
691 void DestroyWindowMenu(WScreen *scr)
693 if (scr->window_menu) {
694 scr->window_menu->entries[MC_MINIATURIZE]->text = NULL;
695 scr->window_menu->entries[MC_MAXIMIZE]->text = NULL;
696 scr->window_menu->entries[MC_SHADE]->text = NULL;
697 scr->window_menu->entries[MC_SELECT]->text = NULL;
698 wMenuDestroy(scr->window_menu, True);
699 scr->window_menu = NULL;