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"
41 #define IS_GNUSTEP_MENU(w) ((w)->wm_gnustep_attr && \
42 ((w)->wm_gnustep_attr->flags & GSWindowLevelAttr) && \
43 ((w)->wm_gnustep_attr->window_level == WMMainMenuWindowLevel || \
44 (w)->wm_gnustep_attr->window_level == WMSubmenuWindowLevel))
46 /********* Global Variables *******/
47 extern WPreferences wPreferences
;
49 static int initialized
= 0;
50 static void observer(void *self
, WMNotification
* notif
);
51 static void wsobserver(void *self
, WMNotification
* notif
);
56 * - Needs to check if already in the right workspace before
57 * calling wChangeWorkspace?
60 * Switch to correct workspace
62 * If iconified then deiconify else focus/raise.
64 static void focusWindow(WMenu
* menu
, WMenuEntry
* entry
)
70 wwin
= (WWindow
*) entry
->clientdata
;
71 scr
= wwin
->screen_ptr
;
73 wMakeWindowVisible(wwin
);
78 /* bring window back to visible area */
79 move
= wScreenBringInside(scr
, &x
, &y
, wwin
->frame
->core
->width
, wwin
->frame
->core
->height
);
82 wWindowConfigure(wwin
, x
, y
, wwin
->client
.width
, wwin
->client
.height
);
86 void InitializeSwitchMenu(void)
91 WMAddNotificationObserver(observer
, NULL
, WMNManaged
, NULL
);
92 WMAddNotificationObserver(observer
, NULL
, WMNUnmanaged
, NULL
);
93 WMAddNotificationObserver(observer
, NULL
, WMNChangedWorkspace
, NULL
);
94 WMAddNotificationObserver(observer
, NULL
, WMNChangedState
, NULL
);
95 WMAddNotificationObserver(observer
, NULL
, WMNChangedFocus
, NULL
);
96 WMAddNotificationObserver(observer
, NULL
, WMNChangedStacking
, NULL
);
97 WMAddNotificationObserver(observer
, NULL
, WMNChangedName
, NULL
);
99 WMAddNotificationObserver(wsobserver
, NULL
, WMNWorkspaceChanged
, NULL
);
100 WMAddNotificationObserver(wsobserver
, NULL
, WMNWorkspaceNameChanged
, NULL
);
109 void OpenSwitchMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
111 WMenu
*switchmenu
= scr
->switch_menu
;
115 if (switchmenu
->flags
.mapped
) {
116 if (!switchmenu
->flags
.buttoned
) {
117 wMenuUnmap(switchmenu
);
119 wRaiseFrame(switchmenu
->frame
->core
);
122 wMenuMapAt(switchmenu
, 0, 0, True
);
124 wMenuMapCopyAt(switchmenu
, x
- switchmenu
->frame
->core
->width
/ 2, y
);
127 if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
128 y
= y
- switchmenu
->frame
->core
->height
/ 2;
130 wMenuMapAt(switchmenu
, x
- switchmenu
->frame
->core
->width
/ 2, y
, keyboard
);
134 switchmenu
= wMenuCreate(scr
, _("Windows"), True
);
135 scr
->switch_menu
= switchmenu
;
137 wwin
= scr
->focused_window
;
139 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
147 if (!switchmenu
->flags
.realized
)
148 wMenuRealize(switchmenu
);
150 if (keyboard
&& x
== 0 && y
== 0) {
152 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
153 newx
= x
- switchmenu
->frame
->core
->width
/ 2;
154 newy
= y
- switchmenu
->frame
->core
->height
/ 2;
156 newx
= x
- switchmenu
->frame
->core
->width
/ 2;
159 wMenuMapAt(switchmenu
, newx
, newy
, keyboard
);
163 static int menuIndexForWindow(WMenu
* menu
, WWindow
* wwin
, int old_pos
)
167 if (menu
->entry_no
<= old_pos
)
170 #define WS(i) ((WWindow*)menu->entries[i]->clientdata)->frame->workspace
172 if (WS(old_pos
) >= wwin
->frame
->workspace
173 && (old_pos
== 0 || WS(old_pos
- 1) <= wwin
->frame
->workspace
)) {
179 for (idx
= 0; idx
< menu
->entry_no
; idx
++) {
180 WWindow
*tw
= (WWindow
*) menu
->entries
[idx
]->clientdata
;
182 if (!IS_OMNIPRESENT(tw
)
183 && tw
->frame
->workspace
> wwin
->frame
->workspace
) {
194 void UpdateSwitchMenu(WScreen
* scr
, WWindow
* wwin
, int action
)
196 WMenu
*switchmenu
= scr
->switch_menu
;
198 char title
[MAX_MENU_TEXT_LENGTH
+ 6];
199 int len
= sizeof(title
);
201 int checkVisibility
= 0;
203 if (!wwin
->screen_ptr
->switch_menu
)
206 * This menu is updated under the following conditions:
208 * 1. When a window is created.
209 * 2. When a window is destroyed.
211 * 3. When a window changes it's title.
212 * 4. When a window changes its workspace.
214 if (action
== ACTION_ADD
) {
218 if (wwin
->flags
.internal_window
|| WFLAGP(wwin
, skip_window_list
) || IS_GNUSTEP_MENU(wwin
)) {
222 if (wwin
->frame
->title
)
223 snprintf(title
, len
, "%s", wwin
->frame
->title
);
225 snprintf(title
, len
, "%s", DEF_WINDOW_TITLE
);
226 t
= ShrinkString(scr
->menu_entry_font
, title
, MAX_WINDOWLIST_WIDTH
);
228 if (IS_OMNIPRESENT(wwin
))
231 idx
= menuIndexForWindow(switchmenu
, wwin
, -1);
234 entry
= wMenuInsertCallback(switchmenu
, idx
, t
, focusWindow
, wwin
);
237 entry
->flags
.indicator
= 1;
238 entry
->rtext
= wmalloc(MAX_WORKSPACENAME_WIDTH
+ 8);
239 if (IS_OMNIPRESENT(wwin
))
240 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
242 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[%s]",
243 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
245 if (wwin
->flags
.hidden
) {
246 entry
->flags
.indicator_type
= MI_HIDDEN
;
247 entry
->flags
.indicator_on
= 1;
248 } else if (wwin
->flags
.miniaturized
) {
249 entry
->flags
.indicator_type
= MI_MINIWINDOW
;
250 entry
->flags
.indicator_on
= 1;
251 } else if (wwin
->flags
.focused
) {
252 entry
->flags
.indicator_type
= MI_DIAMOND
;
253 entry
->flags
.indicator_on
= 1;
254 } else if (wwin
->flags
.shaded
) {
255 entry
->flags
.indicator_type
= MI_SHADED
;
256 entry
->flags
.indicator_on
= 1;
259 wMenuRealize(switchmenu
);
263 for (i
= 0; i
< switchmenu
->entry_no
; i
++) {
264 entry
= switchmenu
->entries
[i
];
265 /* this is the entry that was changed */
266 if (entry
->clientdata
== wwin
) {
269 wMenuRemoveItem(switchmenu
, i
);
270 wMenuRealize(switchmenu
);
278 if (wwin
->frame
->title
)
279 snprintf(title
, MAX_MENU_TEXT_LENGTH
, "%s", wwin
->frame
->title
);
281 snprintf(title
, MAX_MENU_TEXT_LENGTH
, "%s", DEF_WINDOW_TITLE
);
283 t
= ShrinkString(scr
->menu_entry_font
, title
, MAX_WINDOWLIST_WIDTH
);
286 wMenuRealize(switchmenu
);
290 case ACTION_CHANGE_WORKSPACE
:
296 if (IS_OMNIPRESENT(wwin
)) {
297 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
299 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
,
301 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
309 it
= entry
->flags
.indicator_type
;
310 ion
= entry
->flags
.indicator_on
;
312 if (!IS_OMNIPRESENT(wwin
) && idx
< 0) {
313 idx
= menuIndexForWindow(switchmenu
, wwin
, i
);
316 wMenuRemoveItem(switchmenu
, i
);
318 entry
= wMenuInsertCallback(switchmenu
, idx
, t
, focusWindow
, wwin
);
321 entry
->flags
.indicator
= 1;
322 entry
->flags
.indicator_type
= it
;
323 entry
->flags
.indicator_on
= ion
;
325 wMenuRealize(switchmenu
);
329 case ACTION_CHANGE_STATE
:
330 if (wwin
->flags
.hidden
) {
331 entry
->flags
.indicator_type
= MI_HIDDEN
;
332 entry
->flags
.indicator_on
= 1;
333 } else if (wwin
->flags
.miniaturized
) {
334 entry
->flags
.indicator_type
= MI_MINIWINDOW
;
335 entry
->flags
.indicator_on
= 1;
336 } else if (wwin
->flags
.shaded
&& !wwin
->flags
.focused
) {
337 entry
->flags
.indicator_type
= MI_SHADED
;
338 entry
->flags
.indicator_on
= 1;
340 entry
->flags
.indicator_on
= wwin
->flags
.focused
;
341 entry
->flags
.indicator_type
= MI_DIAMOND
;
349 if (checkVisibility
) {
352 tmp
= switchmenu
->frame
->top_width
+ 5;
353 /* if menu got unreachable, bring it to a visible place */
354 if (switchmenu
->frame_x
< tmp
- (int)switchmenu
->frame
->core
->width
) {
355 wMenuMove(switchmenu
, tmp
- (int)switchmenu
->frame
->core
->width
,
356 switchmenu
->frame_y
, False
);
359 wMenuPaint(switchmenu
);
362 static void UpdateSwitchMenuWorkspace(WScreen
*scr
, int workspace
)
364 WMenu
*menu
= scr
->switch_menu
;
371 for (i
= 0; i
< menu
->entry_no
; i
++) {
372 wwin
= (WWindow
*) menu
->entries
[i
]->clientdata
;
374 if (wwin
->frame
->workspace
== workspace
&& !IS_OMNIPRESENT(wwin
)) {
375 if (IS_OMNIPRESENT(wwin
))
376 snprintf(menu
->entries
[i
]->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
378 snprintf(menu
->entries
[i
]->rtext
, MAX_WORKSPACENAME_WIDTH
, "[%s]",
379 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
380 menu
->flags
.realized
= 0;
383 if (!menu
->flags
.realized
)
387 static void observer(void *self
, WMNotification
* notif
)
389 WWindow
*wwin
= (WWindow
*) WMGetNotificationObject(notif
);
390 const char *name
= WMGetNotificationName(notif
);
391 void *data
= WMGetNotificationClientData(notif
);
396 if (strcmp(name
, WMNManaged
) == 0)
397 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_ADD
);
398 else if (strcmp(name
, WMNUnmanaged
) == 0)
399 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_REMOVE
);
400 else if (strcmp(name
, WMNChangedWorkspace
) == 0)
401 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
402 else if (strcmp(name
, WMNChangedFocus
) == 0)
403 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_STATE
);
404 else if (strcmp(name
, WMNChangedName
) == 0)
405 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
406 else if (strcmp(name
, WMNChangedState
) == 0) {
407 if (strcmp((char *)data
, "omnipresent") == 0) {
408 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
410 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_STATE
);
415 static void wsobserver(void *self
, WMNotification
* notif
)
417 WScreen
*scr
= (WScreen
*) WMGetNotificationObject(notif
);
418 const char *name
= WMGetNotificationName(notif
);
419 void *data
= WMGetNotificationClientData(notif
);
421 if (strcmp(name
, WMNWorkspaceNameChanged
) == 0) {
422 UpdateSwitchMenuWorkspace(scr
, (uintptr_t)data
);
423 } else if (strcmp(name
, WMNWorkspaceChanged
) == 0) {