2 * Window Maker window manager
4 * Copyright (c) 1997 Shige Abe
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>
32 #include "WindowMaker.h"
38 #include "workspace.h"
40 #include "switchmenu.h"
42 #define IS_GNUSTEP_MENU(w) ((w)->wm_gnustep_attr && \
43 ((w)->wm_gnustep_attr->flags & GSWindowLevelAttr) && \
44 ((w)->wm_gnustep_attr->window_level == WMMainMenuWindowLevel || \
45 (w)->wm_gnustep_attr->window_level == WMSubmenuWindowLevel))
47 static int initialized
= 0;
48 static void observer(void *self
, WMNotification
* notif
);
49 static void wsobserver(void *self
, WMNotification
* notif
);
54 * - Needs to check if already in the right workspace before
55 * calling wChangeWorkspace?
58 * Switch to correct workspace
60 * If iconified then deiconify else focus/raise.
62 static void focusWindow(WMenu
* menu
, WMenuEntry
* entry
)
66 /* Parameter not used, but tell the compiler that it is ok */
69 wwin
= (WWindow
*) entry
->clientdata
;
70 wWindowSingleFocus(wwin
);
73 void InitializeSwitchMenu(void)
78 WMAddNotificationObserver(observer
, NULL
, WMNManaged
, NULL
);
79 WMAddNotificationObserver(observer
, NULL
, WMNUnmanaged
, NULL
);
80 WMAddNotificationObserver(observer
, NULL
, WMNChangedWorkspace
, NULL
);
81 WMAddNotificationObserver(observer
, NULL
, WMNChangedState
, NULL
);
82 WMAddNotificationObserver(observer
, NULL
, WMNChangedFocus
, NULL
);
83 WMAddNotificationObserver(observer
, NULL
, WMNChangedStacking
, NULL
);
84 WMAddNotificationObserver(observer
, NULL
, WMNChangedName
, NULL
);
86 WMAddNotificationObserver(wsobserver
, NULL
, WMNWorkspaceChanged
, NULL
);
87 WMAddNotificationObserver(wsobserver
, NULL
, WMNWorkspaceNameChanged
, NULL
);
96 void OpenSwitchMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
98 WMenu
*switchmenu
= scr
->switch_menu
;
102 if (switchmenu
->flags
.mapped
) {
103 if (!switchmenu
->flags
.buttoned
) {
104 wMenuUnmap(switchmenu
);
106 wRaiseFrame(switchmenu
->frame
->core
);
109 wMenuMapAt(switchmenu
, 0, 0, True
);
111 wMenuMapCopyAt(switchmenu
, x
- switchmenu
->frame
->core
->width
/ 2, y
);
114 if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
115 y
= y
- switchmenu
->frame
->core
->height
/ 2;
117 wMenuMapAt(switchmenu
, x
- switchmenu
->frame
->core
->width
/ 2, y
, keyboard
);
121 switchmenu
= wMenuCreate(scr
, _("Windows"), True
);
122 scr
->switch_menu
= switchmenu
;
124 wwin
= scr
->focused_window
;
126 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
134 if (!switchmenu
->flags
.realized
)
135 wMenuRealize(switchmenu
);
137 if (keyboard
&& x
== 0 && y
== 0) {
139 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
140 newx
= x
- switchmenu
->frame
->core
->width
/ 2;
141 newy
= y
- switchmenu
->frame
->core
->height
/ 2;
143 newx
= x
- switchmenu
->frame
->core
->width
/ 2;
146 wMenuMapAt(switchmenu
, newx
, newy
, keyboard
);
150 static int menuIndexForWindow(WMenu
* menu
, WWindow
* wwin
, int old_pos
)
154 if (menu
->entry_no
<= old_pos
)
157 for (idx
= 0, move_down
= 0; idx
< menu
->entry_no
; idx
++) {
158 WWindow
*tw
= (WWindow
*) menu
->entries
[idx
]->clientdata
;
160 /* Is the window moving down in the menu? If so, we'll need to
161 adjust its new index by 1. */
165 if (IS_OMNIPRESENT(tw
) || (tw
!= wwin
&&
166 tw
->frame
->workspace
>= wwin
->frame
->workspace
))
167 return idx
- move_down
;
176 void UpdateSwitchMenu(WScreen
* scr
, WWindow
* wwin
, int action
)
178 WMenu
*switchmenu
= scr
->switch_menu
;
180 char title
[MAX_MENU_TEXT_LENGTH
+ 6];
181 int len
= sizeof(title
);
183 int checkVisibility
= 0;
185 if (!wwin
->screen_ptr
->switch_menu
)
188 * This menu is updated under the following conditions:
190 * 1. When a window is created.
191 * 2. When a window is destroyed.
193 * 3. When a window changes it's title.
194 * 4. When a window changes its workspace.
196 if (action
== ACTION_ADD
) {
200 if (wwin
->flags
.internal_window
|| WFLAGP(wwin
, skip_window_list
) || IS_GNUSTEP_MENU(wwin
)) {
204 if (wwin
->frame
->title
)
205 snprintf(title
, len
, "%s", wwin
->frame
->title
);
207 snprintf(title
, len
, "%s", DEF_WINDOW_TITLE
);
208 t
= ShrinkString(scr
->menu_entry_font
, title
, MAX_WINDOWLIST_WIDTH
);
210 if (IS_OMNIPRESENT(wwin
))
213 idx
= menuIndexForWindow(switchmenu
, wwin
, 0);
216 entry
= wMenuInsertCallback(switchmenu
, idx
, t
, focusWindow
, wwin
);
219 entry
->flags
.indicator
= 1;
220 entry
->rtext
= wmalloc(MAX_WORKSPACENAME_WIDTH
+ 8);
221 if (IS_OMNIPRESENT(wwin
))
222 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
224 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[%s]",
225 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
227 if (wwin
->flags
.hidden
) {
228 entry
->flags
.indicator_type
= MI_HIDDEN
;
229 entry
->flags
.indicator_on
= 1;
230 } else if (wwin
->flags
.miniaturized
) {
231 entry
->flags
.indicator_type
= MI_MINIWINDOW
;
232 entry
->flags
.indicator_on
= 1;
233 } else if (wwin
->flags
.focused
) {
234 entry
->flags
.indicator_type
= MI_DIAMOND
;
235 entry
->flags
.indicator_on
= 1;
236 } else if (wwin
->flags
.shaded
) {
237 entry
->flags
.indicator_type
= MI_SHADED
;
238 entry
->flags
.indicator_on
= 1;
241 wMenuRealize(switchmenu
);
245 for (i
= 0; i
< switchmenu
->entry_no
; i
++) {
246 entry
= switchmenu
->entries
[i
];
247 /* this is the entry that was changed */
248 if (entry
->clientdata
== wwin
) {
251 wMenuRemoveItem(switchmenu
, i
);
252 wMenuRealize(switchmenu
);
260 if (wwin
->frame
->title
)
261 snprintf(title
, MAX_MENU_TEXT_LENGTH
, "%s", wwin
->frame
->title
);
263 snprintf(title
, MAX_MENU_TEXT_LENGTH
, "%s", DEF_WINDOW_TITLE
);
265 t
= ShrinkString(scr
->menu_entry_font
, title
, MAX_WINDOWLIST_WIDTH
);
268 wMenuRealize(switchmenu
);
272 case ACTION_CHANGE_WORKSPACE
:
278 if (IS_OMNIPRESENT(wwin
)) {
279 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
281 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
,
283 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
291 it
= entry
->flags
.indicator_type
;
292 ion
= entry
->flags
.indicator_on
;
294 if (!IS_OMNIPRESENT(wwin
) && idx
< 0) {
295 idx
= menuIndexForWindow(switchmenu
, wwin
, i
);
298 wMenuRemoveItem(switchmenu
, i
);
300 entry
= wMenuInsertCallback(switchmenu
, idx
, t
, focusWindow
, wwin
);
303 entry
->flags
.indicator
= 1;
304 entry
->flags
.indicator_type
= it
;
305 entry
->flags
.indicator_on
= ion
;
307 wMenuRealize(switchmenu
);
311 case ACTION_CHANGE_STATE
:
312 if (wwin
->flags
.hidden
) {
313 entry
->flags
.indicator_type
= MI_HIDDEN
;
314 entry
->flags
.indicator_on
= 1;
315 } else if (wwin
->flags
.miniaturized
) {
316 entry
->flags
.indicator_type
= MI_MINIWINDOW
;
317 entry
->flags
.indicator_on
= 1;
318 } else if (wwin
->flags
.shaded
&& !wwin
->flags
.focused
) {
319 entry
->flags
.indicator_type
= MI_SHADED
;
320 entry
->flags
.indicator_on
= 1;
322 entry
->flags
.indicator_on
= wwin
->flags
.focused
;
323 entry
->flags
.indicator_type
= MI_DIAMOND
;
331 if (checkVisibility
) {
334 tmp
= switchmenu
->frame
->top_width
+ 5;
335 /* if menu got unreachable, bring it to a visible place */
336 if (switchmenu
->frame_x
< tmp
- (int)switchmenu
->frame
->core
->width
) {
337 wMenuMove(switchmenu
, tmp
- (int)switchmenu
->frame
->core
->width
,
338 switchmenu
->frame_y
, False
);
341 wMenuPaint(switchmenu
);
344 static void UpdateSwitchMenuWorkspace(WScreen
*scr
, int workspace
)
346 WMenu
*menu
= scr
->switch_menu
;
353 for (i
= 0; i
< menu
->entry_no
; i
++) {
354 wwin
= (WWindow
*) menu
->entries
[i
]->clientdata
;
356 if (wwin
->frame
->workspace
== workspace
&& !IS_OMNIPRESENT(wwin
)) {
357 if (IS_OMNIPRESENT(wwin
))
358 snprintf(menu
->entries
[i
]->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
360 snprintf(menu
->entries
[i
]->rtext
, MAX_WORKSPACENAME_WIDTH
, "[%s]",
361 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
362 menu
->flags
.realized
= 0;
365 if (!menu
->flags
.realized
)
369 static void observer(void *self
, WMNotification
* notif
)
371 WWindow
*wwin
= (WWindow
*) WMGetNotificationObject(notif
);
372 const char *name
= WMGetNotificationName(notif
);
373 void *data
= WMGetNotificationClientData(notif
);
375 /* Parameter not used, but tell the compiler that it is ok */
381 if (strcmp(name
, WMNManaged
) == 0)
382 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_ADD
);
383 else if (strcmp(name
, WMNUnmanaged
) == 0)
384 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_REMOVE
);
385 else if (strcmp(name
, WMNChangedWorkspace
) == 0)
386 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
387 else if (strcmp(name
, WMNChangedFocus
) == 0)
388 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_STATE
);
389 else if (strcmp(name
, WMNChangedName
) == 0)
390 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
391 else if (strcmp(name
, WMNChangedState
) == 0) {
392 if (strcmp((char *)data
, "omnipresent") == 0) {
393 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
395 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_STATE
);
400 static void wsobserver(void *self
, WMNotification
* notif
)
402 WScreen
*scr
= (WScreen
*) WMGetNotificationObject(notif
);
403 const char *name
= WMGetNotificationName(notif
);
404 void *data
= WMGetNotificationClientData(notif
);
406 /* Parameter not used, but tell the compiler that it is ok */
409 if (strcmp(name
, WMNWorkspaceNameChanged
) == 0) {
410 UpdateSwitchMenuWorkspace(scr
, (uintptr_t)data
);
411 } else if (strcmp(name
, WMNWorkspaceChanged
) == 0) {