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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include <X11/Xutil.h>
33 #include "WindowMaker.h"
39 #include "workspace.h"
42 /********* Global Variables *******/
43 extern WPreferences wPreferences
;
45 static int initialized
= 0;
46 static void observer(void *self
, WMNotification
* notif
);
47 static void wsobserver(void *self
, WMNotification
* notif
);
52 * - Needs to check if already in the right workspace before
53 * calling wChangeWorkspace?
56 * Switch to correct workspace
58 * If iconified then deiconify else focus/raise.
60 static void focusWindow(WMenu
* menu
, WMenuEntry
* entry
)
66 wwin
= (WWindow
*) entry
->clientdata
;
67 scr
= wwin
->screen_ptr
;
69 wMakeWindowVisible(wwin
);
74 /* bring window back to visible area */
75 move
= wScreenBringInside(scr
, &x
, &y
, wwin
->frame
->core
->width
, wwin
->frame
->core
->height
);
78 wWindowConfigure(wwin
, x
, y
, wwin
->client
.width
, wwin
->client
.height
);
82 void InitializeSwitchMenu(void)
87 WMAddNotificationObserver(observer
, NULL
, WMNManaged
, NULL
);
88 WMAddNotificationObserver(observer
, NULL
, WMNUnmanaged
, NULL
);
89 WMAddNotificationObserver(observer
, NULL
, WMNChangedWorkspace
, NULL
);
90 WMAddNotificationObserver(observer
, NULL
, WMNChangedState
, NULL
);
91 WMAddNotificationObserver(observer
, NULL
, WMNChangedFocus
, NULL
);
92 WMAddNotificationObserver(observer
, NULL
, WMNChangedStacking
, NULL
);
93 WMAddNotificationObserver(observer
, NULL
, WMNChangedName
, NULL
);
95 WMAddNotificationObserver(wsobserver
, NULL
, WMNWorkspaceChanged
, NULL
);
96 WMAddNotificationObserver(wsobserver
, NULL
, WMNWorkspaceNameChanged
, NULL
);
105 void OpenSwitchMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
107 WMenu
*switchmenu
= scr
->switch_menu
;
111 if (switchmenu
->flags
.mapped
) {
112 if (!switchmenu
->flags
.buttoned
) {
113 wMenuUnmap(switchmenu
);
115 wRaiseFrame(switchmenu
->frame
->core
);
118 wMenuMapAt(switchmenu
, 0, 0, True
);
120 wMenuMapCopyAt(switchmenu
, x
- switchmenu
->frame
->core
->width
/ 2, y
);
123 if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
124 y
= y
- switchmenu
->frame
->core
->height
/ 2;
126 wMenuMapAt(switchmenu
, x
- switchmenu
->frame
->core
->width
/ 2, y
, keyboard
);
130 switchmenu
= wMenuCreate(scr
, _("Windows"), True
);
131 scr
->switch_menu
= switchmenu
;
133 wwin
= scr
->focused_window
;
135 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
143 if (!switchmenu
->flags
.realized
)
144 wMenuRealize(switchmenu
);
146 if (keyboard
&& x
== 0 && y
== 0) {
148 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
149 newx
= x
- switchmenu
->frame
->core
->width
/ 2;
150 newy
= y
- switchmenu
->frame
->core
->height
/ 2;
152 newx
= x
- switchmenu
->frame
->core
->width
/ 2;
155 wMenuMapAt(switchmenu
, newx
, newy
, keyboard
);
159 static int menuIndexForWindow(WMenu
* menu
, WWindow
* wwin
, int old_pos
)
163 if (menu
->entry_no
<= old_pos
)
166 #define WS(i) ((WWindow*)menu->entries[i]->clientdata)->frame->workspace
168 if (WS(old_pos
) >= wwin
->frame
->workspace
169 && (old_pos
== 0 || WS(old_pos
- 1) <= wwin
->frame
->workspace
)) {
175 for (idx
= 0; idx
< menu
->entry_no
; idx
++) {
176 WWindow
*tw
= (WWindow
*) menu
->entries
[idx
]->clientdata
;
178 if (!IS_OMNIPRESENT(tw
)
179 && tw
->frame
->workspace
> wwin
->frame
->workspace
) {
190 void UpdateSwitchMenu(WScreen
* scr
, WWindow
* wwin
, int action
)
192 WMenu
*switchmenu
= scr
->switch_menu
;
194 char title
[MAX_MENU_TEXT_LENGTH
+ 6];
195 int len
= sizeof(title
);
197 int checkVisibility
= 0;
199 if (!wwin
->screen_ptr
->switch_menu
)
202 * This menu is updated under the following conditions:
204 * 1. When a window is created.
205 * 2. When a window is destroyed.
207 * 3. When a window changes it's title.
208 * 4. When a window changes its workspace.
210 if (action
== ACTION_ADD
) {
214 if (wwin
->flags
.internal_window
|| WFLAGP(wwin
, skip_window_list
) || IS_GNUSTEP_MENU(wwin
)) {
218 if (wwin
->frame
->title
)
219 snprintf(title
, len
, "%s", wwin
->frame
->title
);
221 snprintf(title
, len
, "%s", DEF_WINDOW_TITLE
);
222 t
= ShrinkString(scr
->menu_entry_font
, title
, MAX_WINDOWLIST_WIDTH
);
224 if (IS_OMNIPRESENT(wwin
))
227 idx
= menuIndexForWindow(switchmenu
, wwin
, -1);
230 entry
= wMenuInsertCallback(switchmenu
, idx
, t
, focusWindow
, wwin
);
233 entry
->flags
.indicator
= 1;
234 entry
->rtext
= wmalloc(MAX_WORKSPACENAME_WIDTH
+ 8);
235 if (IS_OMNIPRESENT(wwin
))
236 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
238 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[%s]",
239 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
241 if (wwin
->flags
.hidden
) {
242 entry
->flags
.indicator_type
= MI_HIDDEN
;
243 entry
->flags
.indicator_on
= 1;
244 } else if (wwin
->flags
.miniaturized
) {
245 entry
->flags
.indicator_type
= MI_MINIWINDOW
;
246 entry
->flags
.indicator_on
= 1;
247 } else if (wwin
->flags
.focused
) {
248 entry
->flags
.indicator_type
= MI_DIAMOND
;
249 entry
->flags
.indicator_on
= 1;
250 } else if (wwin
->flags
.shaded
) {
251 entry
->flags
.indicator_type
= MI_SHADED
;
252 entry
->flags
.indicator_on
= 1;
255 wMenuRealize(switchmenu
);
259 for (i
= 0; i
< switchmenu
->entry_no
; i
++) {
260 entry
= switchmenu
->entries
[i
];
261 /* this is the entry that was changed */
262 if (entry
->clientdata
== wwin
) {
265 wMenuRemoveItem(switchmenu
, i
);
266 wMenuRealize(switchmenu
);
274 if (wwin
->frame
->title
)
275 snprintf(title
, MAX_MENU_TEXT_LENGTH
, "%s", wwin
->frame
->title
);
277 snprintf(title
, MAX_MENU_TEXT_LENGTH
, "%s", DEF_WINDOW_TITLE
);
279 t
= ShrinkString(scr
->menu_entry_font
, title
, MAX_WINDOWLIST_WIDTH
);
282 wMenuRealize(switchmenu
);
286 case ACTION_CHANGE_WORKSPACE
:
292 if (IS_OMNIPRESENT(wwin
)) {
293 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
295 snprintf(entry
->rtext
, MAX_WORKSPACENAME_WIDTH
,
297 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
305 it
= entry
->flags
.indicator_type
;
306 ion
= entry
->flags
.indicator_on
;
308 if (!IS_OMNIPRESENT(wwin
) && idx
< 0) {
309 idx
= menuIndexForWindow(switchmenu
, wwin
, i
);
312 wMenuRemoveItem(switchmenu
, i
);
314 entry
= wMenuInsertCallback(switchmenu
, idx
, t
, focusWindow
, wwin
);
317 entry
->flags
.indicator
= 1;
318 entry
->flags
.indicator_type
= it
;
319 entry
->flags
.indicator_on
= ion
;
321 wMenuRealize(switchmenu
);
325 case ACTION_CHANGE_STATE
:
326 if (wwin
->flags
.hidden
) {
327 entry
->flags
.indicator_type
= MI_HIDDEN
;
328 entry
->flags
.indicator_on
= 1;
329 } else if (wwin
->flags
.miniaturized
) {
330 entry
->flags
.indicator_type
= MI_MINIWINDOW
;
331 entry
->flags
.indicator_on
= 1;
332 } else if (wwin
->flags
.shaded
&& !wwin
->flags
.focused
) {
333 entry
->flags
.indicator_type
= MI_SHADED
;
334 entry
->flags
.indicator_on
= 1;
336 entry
->flags
.indicator_on
= wwin
->flags
.focused
;
337 entry
->flags
.indicator_type
= MI_DIAMOND
;
345 if (checkVisibility
) {
348 tmp
= switchmenu
->frame
->top_width
+ 5;
349 /* if menu got unreachable, bring it to a visible place */
350 if (switchmenu
->frame_x
< tmp
- (int)switchmenu
->frame
->core
->width
) {
351 wMenuMove(switchmenu
, tmp
- (int)switchmenu
->frame
->core
->width
,
352 switchmenu
->frame_y
, False
);
355 wMenuPaint(switchmenu
);
358 static void UpdateSwitchMenuWorkspace(WScreen
*scr
, int workspace
)
360 WMenu
*menu
= scr
->switch_menu
;
367 for (i
= 0; i
< menu
->entry_no
; i
++) {
368 wwin
= (WWindow
*) menu
->entries
[i
]->clientdata
;
370 if (wwin
->frame
->workspace
== workspace
&& !IS_OMNIPRESENT(wwin
)) {
371 if (IS_OMNIPRESENT(wwin
))
372 snprintf(menu
->entries
[i
]->rtext
, MAX_WORKSPACENAME_WIDTH
, "[*]");
374 snprintf(menu
->entries
[i
]->rtext
, MAX_WORKSPACENAME_WIDTH
, "[%s]",
375 scr
->workspaces
[wwin
->frame
->workspace
]->name
);
376 menu
->flags
.realized
= 0;
379 if (!menu
->flags
.realized
)
383 static void observer(void *self
, WMNotification
* notif
)
385 WWindow
*wwin
= (WWindow
*) WMGetNotificationObject(notif
);
386 const char *name
= WMGetNotificationName(notif
);
387 void *data
= WMGetNotificationClientData(notif
);
392 if (strcmp(name
, WMNManaged
) == 0)
393 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_ADD
);
394 else if (strcmp(name
, WMNUnmanaged
) == 0)
395 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_REMOVE
);
396 else if (strcmp(name
, WMNChangedWorkspace
) == 0)
397 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
398 else if (strcmp(name
, WMNChangedFocus
) == 0)
399 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_STATE
);
400 else if (strcmp(name
, WMNChangedName
) == 0)
401 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
402 else if (strcmp(name
, WMNChangedState
) == 0) {
403 if (strcmp((char *)data
, "omnipresent") == 0) {
404 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_WORKSPACE
);
406 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE_STATE
);
411 static void wsobserver(void *self
, WMNotification
* notif
)
413 WScreen
*scr
= (WScreen
*) WMGetNotificationObject(notif
);
414 const char *name
= WMGetNotificationName(notif
);
415 void *data
= WMGetNotificationClientData(notif
);
417 if (strcmp(name
, WMNWorkspaceNameChanged
) == 0) {
418 UpdateSwitchMenuWorkspace(scr
, (uintptr_t)data
);
419 } else if (strcmp(name
, WMNWorkspaceChanged
) == 0) {