1 /* workspace.c- Workspace management
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.
24 #include <X11/Xutil.h>
26 #include <X11/extensions/shape.h>
38 #include "WindowMaker.h"
44 #include "application.h"
47 #include "workspace.h"
54 #define MC_DESTROY_LAST 1
55 #define MC_LAST_USED 2
56 /* index of the first workspace menu entry */
57 #define MC_WORKSPACE1 3
59 #define MAX_SHORTCUT_LENGTH 32
60 #define WORKSPACE_NAME_DISPLAY_PADDING 32
62 extern int ignore_wks_change
;
63 extern WShortKey wKeyBindings
[WKBD_LAST
];
65 static WMPropList
*dWorkspaces
= NULL
;
66 static WMPropList
*dClip
, *dName
;
68 static void make_keys(void)
70 if (dWorkspaces
!= NULL
)
73 dWorkspaces
= WMCreatePLString("Workspaces");
74 dName
= WMCreatePLString("Name");
75 dClip
= WMCreatePLString("Clip");
78 void wWorkspaceMake(WScreen
* scr
, int count
)
86 int wWorkspaceNew(WScreen
*scr
)
88 WWorkspace
*wspace
, **list
;
91 if (scr
->workspace_count
< MAX_WORKSPACES
) {
92 scr
->workspace_count
++;
94 wspace
= wmalloc(sizeof(WWorkspace
));
99 wspace
->name
= wmalloc(strlen(_("Workspace %i")) + 8);
100 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
103 if (!wPreferences
.flags
.noclip
)
104 wspace
->clip
= wDockCreate(scr
, WM_CLIP
, NULL
);
106 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
108 for (i
= 0; i
< scr
->workspace_count
- 1; i
++)
109 list
[i
] = scr
->workspaces
[i
];
113 wfree(scr
->workspaces
);
115 scr
->workspaces
= list
;
117 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
118 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
119 wNETWMUpdateDesktop(scr
);
120 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
123 return scr
->workspace_count
- 1;
129 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
138 /* verify if workspace is in use by some window */
139 tmp
= scr
->focused_window
;
141 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
146 if (!wPreferences
.flags
.noclip
) {
147 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
148 scr
->workspaces
[workspace
]->clip
= NULL
;
151 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
153 for (i
= 0; i
< scr
->workspace_count
; i
++) {
154 if (i
!= workspace
) {
155 list
[j
++] = scr
->workspaces
[i
];
157 if (scr
->workspaces
[i
]->name
)
158 wfree(scr
->workspaces
[i
]->name
);
159 wfree(scr
->workspaces
[i
]);
162 wfree(scr
->workspaces
);
163 scr
->workspaces
= list
;
165 scr
->workspace_count
--;
168 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
169 /* clip workspace menu */
170 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
172 /* update also window menu */
173 if (scr
->workspace_submenu
) {
174 WMenu
*menu
= scr
->workspace_submenu
;
177 while (i
> scr
->workspace_count
)
178 wMenuRemoveItem(menu
, --i
);
182 if (scr
->clip_submenu
) {
183 WMenu
*menu
= scr
->clip_submenu
;
186 while (i
> scr
->workspace_count
)
187 wMenuRemoveItem(menu
, --i
);
190 wNETWMUpdateDesktop(scr
);
191 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
193 if (scr
->current_workspace
>= scr
->workspace_count
)
194 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
195 if (scr
->last_workspace
>= scr
->workspace_count
)
196 scr
->last_workspace
= 0;
201 typedef struct WorkspaceNameData
{
208 static void hideWorkspaceName(void *data
)
210 WScreen
*scr
= (WScreen
*) data
;
212 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
213 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
214 XUnmapWindow(dpy
, scr
->workspace_name
);
216 if (scr
->workspace_name_data
) {
217 RReleaseImage(scr
->workspace_name_data
->back
);
218 RReleaseImage(scr
->workspace_name_data
->text
);
219 wfree(scr
->workspace_name_data
);
221 scr
->workspace_name_data
= NULL
;
223 scr
->workspace_name_timer
= NULL
;
225 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
228 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
230 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
231 scr
->workspace_name_data
->count
* 255 / 10);
233 RConvertImage(scr
->rcontext
, img
, &pix
);
237 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
238 XClearWindow(dpy
, scr
->workspace_name
);
239 XFreePixmap(dpy
, pix
);
242 scr
->workspace_name_data
->count
--;
246 static void showWorkspaceName(WScreen
* scr
, int workspace
)
248 WorkspaceNameData
*data
;
253 char *name
= scr
->workspaces
[workspace
]->name
;
254 int len
= strlen(name
);
262 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2) {
266 if (scr
->workspace_name_timer
) {
267 WMDeleteTimerHandler(scr
->workspace_name_timer
);
268 XUnmapWindow(dpy
, scr
->workspace_name
);
271 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
273 if (scr
->workspace_name_data
) {
274 RReleaseImage(scr
->workspace_name_data
->back
);
275 RReleaseImage(scr
->workspace_name_data
->text
);
276 wfree(scr
->workspace_name_data
);
279 data
= wmalloc(sizeof(WorkspaceNameData
));
282 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
283 h
= WMFontHeight(scr
->workspace_name_font
);
286 head
= wGetHeadForPointerLocation(scr
);
287 rect
= wGetRectForHead(scr
, head
);
288 if (scr
->xine_info
.count
) {
289 xx
= rect
.pos
.x
+ (scr
->xine_info
.screens
[head
].size
.width
- (w
+ 4)) / 2;
290 yy
= rect
.pos
.y
+ (scr
->xine_info
.screens
[head
].size
.height
- (h
+ 4)) / 2;
293 xx
= (scr
->scr_width
- (w
+ 4)) / 2;
294 yy
= (scr
->scr_height
- (h
+ 4)) / 2;
298 switch (wPreferences
.workspace_name_display_position
) {
303 px
= (scr
->scr_width
- (w
+ 4)) / 2;
305 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
311 px
= (scr
->scr_width
- (w
+ 4)) / 2;
313 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
316 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
317 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
320 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
321 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
324 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
325 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
328 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
329 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
337 px
= (scr
->scr_width
- (w
+ 4)) / 2;
338 py
= (scr
->scr_height
- (h
+ 4)) / 2;
342 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
343 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
345 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
346 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
348 /*XSetForeground(dpy, scr->mono_gc, 0);
349 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
351 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
353 for (x
= 0; x
<= 4; x
++) {
354 for (y
= 0; y
<= 4; y
++) {
355 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, x
, y
, name
, len
);
359 XSetForeground(dpy
, scr
->mono_gc
, 1);
360 XSetBackground(dpy
, scr
->mono_gc
, 0);
362 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+ 4, h
+ 4, 0, 0, 1 << (scr
->w_depth
- 1));
364 /*XSetForeground(dpy, scr->mono_gc, 1); */
365 XSetBackground(dpy
, scr
->mono_gc
, 1);
367 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
369 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, 2, 2, name
, len
);
372 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
374 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
375 XClearWindow(dpy
, scr
->workspace_name
);
377 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
379 XFreePixmap(dpy
, text
);
380 XFreePixmap(dpy
, mask
);
383 XMapRaised(dpy
, scr
->workspace_name
);
389 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
391 if (!ximg
|| !ximg
->image
) {
395 XMapRaised(dpy
, scr
->workspace_name
);
398 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
399 RDestroyXImage(scr
->rcontext
, ximg
);
407 /* set a timeout for the effect */
408 data
->timeout
= time(NULL
) + 2 + (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
* data
->count
) / 1000;
410 scr
->workspace_name_data
= data
;
415 if (scr
->workspace_name_timer
)
416 WMDeleteTimerHandler(scr
->workspace_name_timer
);
419 RReleaseImage(data
->text
);
421 RReleaseImage(data
->back
);
424 scr
->workspace_name_data
= NULL
;
426 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
427 10 * WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
430 void wWorkspaceChange(WScreen
*scr
, int workspace
)
432 if (scr
->flags
.startup
|| scr
->flags
.startup2
|| scr
->flags
.ignore_focus_events
)
435 if (workspace
!= scr
->current_workspace
)
436 wWorkspaceForceChange(scr
, workspace
);
439 void wWorkspaceRelativeChange(WScreen
* scr
, int amount
)
443 /* While the deiconify animation is going on the window is
444 * still "flying" to its final position and we don't want to
445 * change workspace before the animation finishes, otherwise
446 * the window will land in the new workspace */
447 if (ignore_wks_change
)
450 w
= scr
->current_workspace
+ amount
;
454 wWorkspaceChange(scr
, w
);
455 } else if (wPreferences
.ws_cycle
) {
456 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
458 } else if (amount
> 0) {
459 if (w
< scr
->workspace_count
) {
460 wWorkspaceChange(scr
, w
);
461 } else if (wPreferences
.ws_advance
) {
462 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
- 1));
463 } else if (wPreferences
.ws_cycle
) {
464 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
469 void wWorkspaceForceChange(WScreen
* scr
, int workspace
)
471 WWindow
*tmp
, *foc
= NULL
, *foc2
= NULL
;
473 int toUnmapSize
, toUnmapCount
;
475 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
478 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
480 if (workspace
> scr
->workspace_count
- 1) {
481 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
484 wClipUpdateForWorkspaceChange(scr
, workspace
);
486 scr
->last_workspace
= scr
->current_workspace
;
487 scr
->current_workspace
= workspace
;
489 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
491 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
495 toUnmap
= wmalloc(toUnmapSize
* sizeof(WWindow
*));
497 if ((tmp
= scr
->focused_window
) != NULL
) {
498 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
499 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
503 /* foc2 = tmp; will fix annoyance with gnome panel
504 * but will create annoyance for every other application
507 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
508 /* unmap windows not on this workspace */
509 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
510 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
511 if (toUnmapCount
== toUnmapSize
)
514 toUnmap
= wrealloc(toUnmap
, toUnmapSize
* sizeof(WWindow
*));
516 toUnmap
[toUnmapCount
++] = tmp
;
518 /* also unmap miniwindows not on this workspace */
519 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
520 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
521 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
522 tmp
->icon
->mapped
= 0;
524 /* update current workspace of omnipresent windows */
525 if (IS_OMNIPRESENT(tmp
)) {
526 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
528 tmp
->frame
->workspace
= workspace
;
531 wapp
->last_workspace
= workspace
;
533 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
538 /* change selected windows' workspace */
539 if (tmp
->flags
.selected
) {
540 wWindowChangeWorkspace(tmp
, workspace
);
541 if (!tmp
->flags
.miniaturized
&& !foc
) {
545 if (!tmp
->flags
.hidden
) {
546 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
547 /* remap windows that are on this workspace */
549 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
553 /* Also map miniwindow if not omnipresent */
554 if (!wPreferences
.sticky_icons
&&
555 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
556 tmp
->icon
->mapped
= 1;
557 XMapWindow(dpy
, tmp
->icon
->core
->window
);
565 while (toUnmapCount
> 0)
567 wWindowUnmap(toUnmap
[--toUnmapCount
]);
571 /* Gobble up events unleashed by our mapping & unmapping.
572 * These may trigger various grab-initiated focus &
573 * crossing events. However, we don't care about them,
574 * and ignore their focus implications altogether to avoid
577 scr
->flags
.ignore_focus_events
= 1;
578 ProcessPendingEvents();
579 scr
->flags
.ignore_focus_events
= 0;
584 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
585 foc
= scr
->focused_window
;
587 if (wPreferences
.focus_mode
== WKF_CLICK
) {
588 wSetFocusTo(scr
, foc
);
596 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
597 tmp
= wWindowFor(win
);
600 /* If there's a window under the pointer, focus it.
601 * (we ate all other focus events above, so it's
602 * certainly not focused). Otherwise focus last
603 * focused, or the root (depending on sloppiness)
605 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
606 wSetFocusTo(scr
, foc
);
608 wSetFocusTo(scr
, tmp
);
613 /* We need to always arrange icons when changing workspace, even if
614 * no autoarrange icons, because else the icons in different workspaces
616 * This can be avoided if appicons are also workspace specific.
618 if (!wPreferences
.sticky_icons
)
619 wArrangeIcons(scr
, False
);
622 wAppIconPaint(scr
->dock
->icon_array
[0]);
624 if (!wPreferences
.flags
.noclip
&& (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
625 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
)) {
626 /* to handle enter notify. This will also */
627 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
628 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
630 else if (scr
->clip_icon
!= NULL
) {
631 wClipIconPaint(scr
->clip_icon
);
633 wScreenUpdateUsableArea(scr
);
634 wNETWMUpdateDesktop(scr
);
635 showWorkspaceName(scr
, workspace
);
637 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
639 /* XSync(dpy, False); */
642 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
644 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
647 static void lastWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
649 wWorkspaceChange(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->last_workspace
);
652 static void deleteWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
654 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
657 static void newWSCommand(WMenu
*menu
, WMenuEntry
*foo
)
661 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
663 /* autochange workspace */
665 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
668 void wWorkspaceRename(WScreen
*scr
, int workspace
, const char *name
)
670 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
673 if (workspace
>= scr
->workspace_count
)
676 /* trim white spaces */
677 tmp
= wtrimspace(name
);
679 if (strlen(tmp
) == 0) {
680 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
682 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
684 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
687 /* update workspace */
688 wfree(scr
->workspaces
[workspace
]->name
);
689 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
691 if (scr
->clip_ws_menu
) {
692 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
, buf
) != 0) {
693 wfree(scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
);
694 scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
= wstrdup(buf
);
695 wMenuRealize(scr
->clip_ws_menu
);
698 if (scr
->workspace_menu
) {
699 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
, buf
) != 0) {
700 wfree(scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
);
701 scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
= wstrdup(buf
);
702 wMenuRealize(scr
->workspace_menu
);
707 wClipIconPaint(scr
->clip_icon
);
709 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
712 /* callback for when menu entry is edited */
713 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
718 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
721 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
726 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
728 wwarning(_("could not create Workspace menu"));
732 /* callback to be called when an entry is edited */
733 wsmenu
->on_edit
= onMenuEntryEdited
;
735 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
736 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
738 entry
= wMenuAddCallback(wsmenu
, _("Last Used"), lastWSCommand
, NULL
);
739 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_LASTWORKSPACE
]);
744 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
748 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
755 if (menu
->entry_no
< scr
->workspace_count
+ MC_WORKSPACE1
) {
756 /* new workspace(s) added */
757 i
= scr
->workspace_count
- (menu
->entry_no
- MC_WORKSPACE1
);
758 ws
= menu
->entry_no
- MC_WORKSPACE1
;
760 wstrlcpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
762 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
763 entry
->flags
.indicator
= 1;
764 entry
->flags
.editable
= 1;
769 } else if (menu
->entry_no
> scr
->workspace_count
+ MC_WORKSPACE1
) {
770 /* removed workspace(s) */
771 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ MC_WORKSPACE1
; i
--) {
772 wMenuRemoveItem(menu
, i
);
776 for (i
= 0; i
< scr
->workspace_count
; i
++) {
777 /* workspace shortcut labels */
778 if (i
/ 10 == scr
->current_workspace
/ 10)
779 menu
->entries
[i
+ MC_WORKSPACE1
]->rtext
= GetShortcutKey(wKeyBindings
[WKBD_WORKSPACE1
+ (i
% 10)]);
781 menu
->entries
[i
+ MC_WORKSPACE1
]->rtext
= NULL
;
783 menu
->entries
[i
+ MC_WORKSPACE1
]->flags
.indicator_on
= 0;
785 menu
->entries
[scr
->current_workspace
+ MC_WORKSPACE1
]->flags
.indicator_on
= 1;
788 /* don't let user destroy current workspace */
789 if (scr
->current_workspace
== scr
->workspace_count
- 1) {
790 wMenuSetEnabled(menu
, MC_DESTROY_LAST
, False
);
792 wMenuSetEnabled(menu
, MC_DESTROY_LAST
, True
);
795 /* back to last workspace */
796 if (scr
->workspace_count
&& scr
->last_workspace
!= scr
->current_workspace
)
797 wMenuSetEnabled(menu
, MC_LAST_USED
, True
);
799 wMenuSetEnabled(menu
, MC_LAST_USED
, False
);
801 tmp
= menu
->frame
->top_width
+ 5;
802 /* if menu got unreachable, bring it to a visible place */
803 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
804 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
809 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
811 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
816 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
817 parr
= WMCreatePLArray(NULL
);
818 for (i
= 0; i
< scr
->workspace_count
; i
++) {
819 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
820 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
821 WMReleasePropList(pstr
);
822 if (!wPreferences
.flags
.noclip
) {
823 pstr
= wClipSaveWorkspaceState(scr
, i
);
824 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
825 WMReleasePropList(pstr
);
826 } else if (old_wks_state
!= NULL
) {
827 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
828 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
829 WMPutInPLDictionary(wks_state
, dClip
, bar
);
833 WMAddToPLArray(parr
, wks_state
);
834 WMReleasePropList(wks_state
);
836 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
837 WMReleasePropList(parr
);
840 void wWorkspaceRestoreState(WScreen
*scr
)
842 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
847 if (scr
->session_state
== NULL
)
850 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
855 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
856 wks_state
= WMGetFromPLArray(parr
, i
);
857 if (WMIsPLDictionary(wks_state
))
858 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
862 if (i
>= scr
->workspace_count
)
865 if (scr
->workspace_menu
) {
866 wfree(scr
->workspace_menu
->entries
[i
+ MC_WORKSPACE1
]->text
);
867 scr
->workspace_menu
->entries
[i
+ MC_WORKSPACE1
]->text
= wstrdup(WMGetFromPLString(pstr
));
868 scr
->workspace_menu
->flags
.realized
= 0;
871 wfree(scr
->workspaces
[i
]->name
);
872 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
873 if (!wPreferences
.flags
.noclip
) {
874 int added_omnipresent_icons
= 0;
876 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
877 if (scr
->workspaces
[i
]->clip
)
878 wDockDestroy(scr
->workspaces
[i
]->clip
);
880 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
882 wDockHideIcons(scr
->workspaces
[i
]->clip
);
884 /* We set the global icons here, because scr->workspaces[i]->clip
885 * was not valid in wDockRestoreState().
886 * There we only set icon->omnipresent to know which icons we
889 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
890 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
893 if (!aicon
|| !aicon
->omnipresent
)
895 aicon
->omnipresent
= 0;
896 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
901 /* Move this appicon from workspace i to workspace 0 */
902 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
903 scr
->workspaces
[i
]->clip
->icon_count
--;
905 added_omnipresent_icons
++;
906 /* If there are too many omnipresent appicons, we are in trouble */
907 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
908 <= scr
->workspaces
[0]->clip
->max_icons
);
909 /* Find first free spot on workspace 0 */
910 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
911 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
913 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
914 aicon
->dock
= scr
->workspaces
[0]->clip
;
916 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
919 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);
923 /* Returns the workspace number for a given workspace name */
924 int wGetWorkspaceNumber(WScreen
*scr
, const char *value
)
928 if (sscanf(value
, "%i", &w
) != 1) {
930 for (i
= 0; i
< scr
->workspace_count
; i
++) {
931 if (strcmp(scr
->workspaces
[i
]->name
, value
) == 0) {