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"
55 #define MC_DESTROY_LAST 1
56 #define MC_LAST_USED 2
57 /* index of the first workspace menu entry */
58 #define MC_WORKSPACE1 3
60 #define WORKSPACE_NAME_DISPLAY_PADDING 32
62 static WMPropList
*dWorkspaces
= NULL
;
63 static WMPropList
*dClip
, *dName
;
65 static void make_keys(void)
67 if (dWorkspaces
!= NULL
)
70 dWorkspaces
= WMCreatePLString("Workspaces");
71 dName
= WMCreatePLString("Name");
72 dClip
= WMCreatePLString("Clip");
75 void wWorkspaceMake(WScreen
* scr
, int count
)
83 int wWorkspaceNew(WScreen
*scr
)
85 WWorkspace
*wspace
, **list
;
88 if (scr
->workspace_count
< MAX_WORKSPACES
) {
89 scr
->workspace_count
++;
91 wspace
= wmalloc(sizeof(WWorkspace
));
96 static const char *new_name
= NULL
;
97 static size_t name_length
;
99 if (new_name
== NULL
) {
100 new_name
= _("Workspace %i");
101 name_length
= strlen(new_name
) + 8;
103 wspace
->name
= wmalloc(name_length
);
104 snprintf(wspace
->name
, name_length
, new_name
, scr
->workspace_count
);
107 if (!wPreferences
.flags
.noclip
)
108 wspace
->clip
= wDockCreate(scr
, WM_CLIP
, NULL
);
110 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
112 for (i
= 0; i
< scr
->workspace_count
- 1; i
++)
113 list
[i
] = scr
->workspaces
[i
];
117 wfree(scr
->workspaces
);
119 scr
->workspaces
= list
;
121 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
122 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
123 wNETWMUpdateDesktop(scr
);
124 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
127 return scr
->workspace_count
- 1;
133 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
142 /* verify if workspace is in use by some window */
143 tmp
= scr
->focused_window
;
145 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
150 if (!wPreferences
.flags
.noclip
) {
151 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
152 scr
->workspaces
[workspace
]->clip
= NULL
;
155 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
157 for (i
= 0; i
< scr
->workspace_count
; i
++) {
158 if (i
!= workspace
) {
159 list
[j
++] = scr
->workspaces
[i
];
161 if (scr
->workspaces
[i
]->name
)
162 wfree(scr
->workspaces
[i
]->name
);
163 if (scr
->workspaces
[i
]->map
)
164 RReleaseImage(scr
->workspaces
[i
]->map
);
165 wfree(scr
->workspaces
[i
]);
168 wfree(scr
->workspaces
);
169 scr
->workspaces
= list
;
171 scr
->workspace_count
--;
174 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
175 /* clip workspace menu */
176 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
178 /* update also window menu */
179 if (scr
->workspace_submenu
) {
180 WMenu
*menu
= scr
->workspace_submenu
;
183 while (i
> scr
->workspace_count
)
184 wMenuRemoveItem(menu
, --i
);
188 if (scr
->clip_submenu
) {
189 WMenu
*menu
= scr
->clip_submenu
;
192 while (i
> scr
->workspace_count
)
193 wMenuRemoveItem(menu
, --i
);
196 wNETWMUpdateDesktop(scr
);
197 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
199 if (scr
->current_workspace
>= scr
->workspace_count
)
200 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
201 if (scr
->last_workspace
>= scr
->workspace_count
)
202 scr
->last_workspace
= 0;
207 typedef struct WorkspaceNameData
{
214 static void hideWorkspaceName(void *data
)
216 WScreen
*scr
= (WScreen
*) data
;
218 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
219 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
220 XUnmapWindow(dpy
, scr
->workspace_name
);
222 if (scr
->workspace_name_data
) {
223 RReleaseImage(scr
->workspace_name_data
->back
);
224 RReleaseImage(scr
->workspace_name_data
->text
);
225 wfree(scr
->workspace_name_data
);
227 scr
->workspace_name_data
= NULL
;
229 scr
->workspace_name_timer
= NULL
;
231 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
234 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
236 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
237 scr
->workspace_name_data
->count
* 255 / 10);
239 RConvertImage(scr
->rcontext
, img
, &pix
);
243 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
244 XClearWindow(dpy
, scr
->workspace_name
);
245 XFreePixmap(dpy
, pix
);
248 scr
->workspace_name_data
->count
--;
252 static void showWorkspaceName(WScreen
* scr
, int workspace
)
254 WorkspaceNameData
*data
;
259 char *name
= scr
->workspaces
[workspace
]->name
;
260 int len
= strlen(name
);
268 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2)
271 if (scr
->workspace_name_timer
) {
272 WMDeleteTimerHandler(scr
->workspace_name_timer
);
273 XUnmapWindow(dpy
, scr
->workspace_name
);
276 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
278 if (scr
->workspace_name_data
) {
279 RReleaseImage(scr
->workspace_name_data
->back
);
280 RReleaseImage(scr
->workspace_name_data
->text
);
281 wfree(scr
->workspace_name_data
);
284 data
= wmalloc(sizeof(WorkspaceNameData
));
287 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
288 h
= WMFontHeight(scr
->workspace_name_font
);
291 head
= wGetHeadForPointerLocation(scr
);
292 rect
= wGetRectForHead(scr
, head
);
293 if (scr
->xine_info
.count
) {
294 xx
= rect
.pos
.x
+ (scr
->xine_info
.screens
[head
].size
.width
- (w
+ 4)) / 2;
295 yy
= rect
.pos
.y
+ (scr
->xine_info
.screens
[head
].size
.height
- (h
+ 4)) / 2;
298 xx
= (scr
->scr_width
- (w
+ 4)) / 2;
299 yy
= (scr
->scr_height
- (h
+ 4)) / 2;
303 switch (wPreferences
.workspace_name_display_position
) {
308 px
= (scr
->scr_width
- (w
+ 4)) / 2;
310 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
316 px
= (scr
->scr_width
- (w
+ 4)) / 2;
318 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
321 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
322 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
325 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
326 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
329 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
330 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
333 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
334 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
342 px
= (scr
->scr_width
- (w
+ 4)) / 2;
343 py
= (scr
->scr_height
- (h
+ 4)) / 2;
347 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
348 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
350 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
351 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
353 /*XSetForeground(dpy, scr->mono_gc, 0);
354 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
356 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
358 for (x
= 0; x
<= 4; x
++)
359 for (y
= 0; y
<= 4; y
++)
360 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, x
, y
, name
, len
);
362 XSetForeground(dpy
, scr
->mono_gc
, 1);
363 XSetBackground(dpy
, scr
->mono_gc
, 0);
365 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+ 4, h
+ 4, 0, 0, 1 << (scr
->w_depth
- 1));
367 /*XSetForeground(dpy, scr->mono_gc, 1); */
368 XSetBackground(dpy
, scr
->mono_gc
, 1);
370 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
372 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, 2, 2, name
, len
);
375 if (w_global
.xext
.shape
.supported
)
376 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
378 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
379 XClearWindow(dpy
, scr
->workspace_name
);
381 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
383 XFreePixmap(dpy
, text
);
384 XFreePixmap(dpy
, mask
);
387 XMapRaised(dpy
, scr
->workspace_name
);
393 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
397 XMapRaised(dpy
, scr
->workspace_name
);
400 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
401 RDestroyXImage(scr
->rcontext
, ximg
);
409 /* set a timeout for the effect */
410 data
->timeout
= time(NULL
) + 2 + (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
* data
->count
) / 1000;
412 scr
->workspace_name_data
= data
;
417 if (scr
->workspace_name_timer
)
418 WMDeleteTimerHandler(scr
->workspace_name_timer
);
421 RReleaseImage(data
->text
);
423 RReleaseImage(data
->back
);
426 scr
->workspace_name_data
= NULL
;
428 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
429 10 * WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
432 void wWorkspaceChange(WScreen
*scr
, int workspace
)
434 if (scr
->flags
.startup
|| scr
->flags
.startup2
|| scr
->flags
.ignore_focus_events
)
437 if (workspace
!= scr
->current_workspace
)
438 wWorkspaceForceChange(scr
, workspace
);
441 void wWorkspaceRelativeChange(WScreen
* scr
, int amount
)
445 /* While the deiconify animation is going on the window is
446 * still "flying" to its final position and we don't want to
447 * change workspace before the animation finishes, otherwise
448 * the window will land in the new workspace */
449 if (w_global
.ignore_workspace_change
)
452 w
= scr
->current_workspace
+ amount
;
456 wWorkspaceChange(scr
, w
);
457 } else if (wPreferences
.ws_cycle
) {
458 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
460 } else if (amount
> 0) {
461 if (w
< scr
->workspace_count
) {
462 wWorkspaceChange(scr
, w
);
463 } else if (wPreferences
.ws_advance
) {
464 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
- 1));
465 } else if (wPreferences
.ws_cycle
) {
466 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
471 void wWorkspaceForceChange(WScreen
* scr
, int workspace
)
473 WWindow
*tmp
, *foc
= NULL
, *foc2
= NULL
;
475 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
478 if (!wPreferences
.disable_workspace_pager
&& !w_global
.process_workspacemap_event
)
479 wWorkspaceMapUpdate(scr
);
481 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
483 if (workspace
> scr
->workspace_count
- 1)
484 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
486 wClipUpdateForWorkspaceChange(scr
, workspace
);
488 scr
->last_workspace
= scr
->current_workspace
;
489 scr
->current_workspace
= workspace
;
491 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
493 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
495 if ((tmp
= scr
->focused_window
) != NULL
) {
497 int toUnmapSize
, toUnmapCount
;
499 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
500 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
506 toUnmap
= wmalloc(toUnmapSize
* sizeof(WWindow
*));
508 /* foc2 = tmp; will fix annoyance with gnome panel
509 * but will create annoyance for every other application
512 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
513 /* unmap windows not on this workspace */
514 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
515 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
516 if (toUnmapCount
== toUnmapSize
)
519 toUnmap
= wrealloc(toUnmap
, toUnmapSize
* sizeof(WWindow
*));
521 toUnmap
[toUnmapCount
++] = tmp
;
523 /* also unmap miniwindows not on this workspace */
524 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
525 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
526 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
527 tmp
->icon
->mapped
= 0;
529 /* update current workspace of omnipresent windows */
530 if (IS_OMNIPRESENT(tmp
)) {
531 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
533 tmp
->frame
->workspace
= workspace
;
536 wapp
->last_workspace
= workspace
;
538 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
543 /* change selected windows' workspace */
544 if (tmp
->flags
.selected
) {
545 wWindowChangeWorkspace(tmp
, workspace
);
546 if (!tmp
->flags
.miniaturized
&& !foc
) {
550 if (!tmp
->flags
.hidden
) {
551 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
552 /* remap windows that are on this workspace */
554 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
558 /* Also map miniwindow if not omnipresent */
559 if (!wPreferences
.sticky_icons
&&
560 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
561 tmp
->icon
->mapped
= 1;
562 XMapWindow(dpy
, tmp
->icon
->core
->window
);
570 while (toUnmapCount
> 0)
572 wWindowUnmap(toUnmap
[--toUnmapCount
]);
576 /* Gobble up events unleashed by our mapping & unmapping.
577 * These may trigger various grab-initiated focus &
578 * crossing events. However, we don't care about them,
579 * and ignore their focus implications altogether to avoid
582 scr
->flags
.ignore_focus_events
= 1;
583 ProcessPendingEvents();
584 scr
->flags
.ignore_focus_events
= 0;
589 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
590 foc
= scr
->focused_window
;
592 if (wPreferences
.focus_mode
== WKF_CLICK
) {
593 wSetFocusTo(scr
, foc
);
601 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
602 tmp
= wWindowFor(win
);
605 /* If there's a window under the pointer, focus it.
606 * (we ate all other focus events above, so it's
607 * certainly not focused). Otherwise focus last
608 * focused, or the root (depending on sloppiness)
610 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
611 wSetFocusTo(scr
, foc
);
613 wSetFocusTo(scr
, tmp
);
618 /* We need to always arrange icons when changing workspace, even if
619 * no autoarrange icons, because else the icons in different workspaces
621 * This can be avoided if appicons are also workspace specific.
623 if (!wPreferences
.sticky_icons
)
624 wArrangeIcons(scr
, False
);
627 wAppIconPaint(scr
->dock
->icon_array
[0]);
629 if (!wPreferences
.flags
.noclip
&& (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
630 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
)) {
631 /* to handle enter notify. This will also */
632 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
633 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
635 else if (scr
->clip_icon
!= NULL
) {
636 wClipIconPaint(scr
->clip_icon
);
638 wScreenUpdateUsableArea(scr
);
639 wNETWMUpdateDesktop(scr
);
640 showWorkspaceName(scr
, workspace
);
642 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
644 /* XSync(dpy, False); */
647 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
649 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
652 static void lastWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
654 /* Parameter not used, but tell the compiler that it is ok */
657 wWorkspaceChange(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->last_workspace
);
660 static void deleteWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
662 /* Parameter not used, but tell the compiler that it is ok */
665 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
668 static void newWSCommand(WMenu
*menu
, WMenuEntry
*foo
)
672 /* Parameter not used, but tell the compiler that it is ok */
675 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
677 /* autochange workspace */
679 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
682 void wWorkspaceRename(WScreen
*scr
, int workspace
, const char *name
)
684 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
687 if (workspace
>= scr
->workspace_count
)
690 /* trim white spaces */
691 tmp
= wtrimspace(name
);
693 if (strlen(tmp
) == 0) {
694 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
696 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
698 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
701 /* update workspace */
702 wfree(scr
->workspaces
[workspace
]->name
);
703 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
705 if (scr
->clip_ws_menu
) {
706 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
, buf
) != 0) {
707 wfree(scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
);
708 scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
= wstrdup(buf
);
709 wMenuRealize(scr
->clip_ws_menu
);
712 if (scr
->workspace_menu
) {
713 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
, buf
) != 0) {
714 wfree(scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
);
715 scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
= wstrdup(buf
);
716 wMenuRealize(scr
->workspace_menu
);
721 wClipIconPaint(scr
->clip_icon
);
723 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
726 /* callback for when menu entry is edited */
727 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
732 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
735 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
740 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
742 wwarning(_("could not create Workspace menu"));
746 /* callback to be called when an entry is edited */
747 wsmenu
->on_edit
= onMenuEntryEdited
;
749 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
750 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
752 entry
= wMenuAddCallback(wsmenu
, _("Last Used"), lastWSCommand
, NULL
);
753 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_LASTWORKSPACE
]);
758 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
762 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
769 if (menu
->entry_no
< scr
->workspace_count
+ MC_WORKSPACE1
) {
770 /* new workspace(s) added */
771 i
= scr
->workspace_count
- (menu
->entry_no
- MC_WORKSPACE1
);
772 ws
= menu
->entry_no
- MC_WORKSPACE1
;
774 wstrlcpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
776 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
777 entry
->flags
.indicator
= 1;
778 entry
->flags
.editable
= 1;
783 } else if (menu
->entry_no
> scr
->workspace_count
+ MC_WORKSPACE1
) {
784 /* removed workspace(s) */
785 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ MC_WORKSPACE1
; i
--)
786 wMenuRemoveItem(menu
, i
);
789 for (i
= 0; i
< scr
->workspace_count
; i
++) {
790 /* workspace shortcut labels */
791 if (i
/ 10 == scr
->current_workspace
/ 10)
792 menu
->entries
[i
+ MC_WORKSPACE1
]->rtext
= GetShortcutKey(wKeyBindings
[WKBD_WORKSPACE1
+ (i
% 10)]);
794 menu
->entries
[i
+ MC_WORKSPACE1
]->rtext
= NULL
;
796 menu
->entries
[i
+ MC_WORKSPACE1
]->flags
.indicator_on
= 0;
798 menu
->entries
[scr
->current_workspace
+ MC_WORKSPACE1
]->flags
.indicator_on
= 1;
801 /* don't let user destroy current workspace */
802 if (scr
->current_workspace
== scr
->workspace_count
- 1)
803 wMenuSetEnabled(menu
, MC_DESTROY_LAST
, False
);
805 wMenuSetEnabled(menu
, MC_DESTROY_LAST
, True
);
807 /* back to last workspace */
808 if (scr
->workspace_count
&& scr
->last_workspace
!= scr
->current_workspace
)
809 wMenuSetEnabled(menu
, MC_LAST_USED
, True
);
811 wMenuSetEnabled(menu
, MC_LAST_USED
, False
);
813 tmp
= menu
->frame
->top_width
+ 5;
814 /* if menu got unreachable, bring it to a visible place */
815 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
816 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
821 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
823 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
828 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
829 parr
= WMCreatePLArray(NULL
);
830 for (i
= 0; i
< scr
->workspace_count
; i
++) {
831 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
832 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
833 WMReleasePropList(pstr
);
834 if (!wPreferences
.flags
.noclip
) {
835 pstr
= wClipSaveWorkspaceState(scr
, i
);
836 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
837 WMReleasePropList(pstr
);
838 } else if (old_wks_state
!= NULL
) {
839 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
840 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
841 WMPutInPLDictionary(wks_state
, dClip
, bar
);
845 WMAddToPLArray(parr
, wks_state
);
846 WMReleasePropList(wks_state
);
848 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
849 WMReleasePropList(parr
);
852 void wWorkspaceRestoreState(WScreen
*scr
)
854 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
859 if (scr
->session_state
== NULL
)
862 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
867 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
868 wks_state
= WMGetFromPLArray(parr
, i
);
869 if (WMIsPLDictionary(wks_state
))
870 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
874 if (i
>= scr
->workspace_count
)
877 if (scr
->workspace_menu
) {
878 wfree(scr
->workspace_menu
->entries
[i
+ MC_WORKSPACE1
]->text
);
879 scr
->workspace_menu
->entries
[i
+ MC_WORKSPACE1
]->text
= wstrdup(WMGetFromPLString(pstr
));
880 scr
->workspace_menu
->flags
.realized
= 0;
883 wfree(scr
->workspaces
[i
]->name
);
884 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
885 if (!wPreferences
.flags
.noclip
) {
886 int added_omnipresent_icons
= 0;
888 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
889 if (scr
->workspaces
[i
]->clip
)
890 wDockDestroy(scr
->workspaces
[i
]->clip
);
892 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
894 wDockHideIcons(scr
->workspaces
[i
]->clip
);
896 /* We set the global icons here, because scr->workspaces[i]->clip
897 * was not valid in wDockRestoreState().
898 * There we only set icon->omnipresent to know which icons we
901 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
902 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
905 if (!aicon
|| !aicon
->omnipresent
)
907 aicon
->omnipresent
= 0;
908 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
913 /* Move this appicon from workspace i to workspace 0 */
914 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
915 scr
->workspaces
[i
]->clip
->icon_count
--;
917 added_omnipresent_icons
++;
918 /* If there are too many omnipresent appicons, we are in trouble */
919 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
920 <= scr
->workspaces
[0]->clip
->max_icons
);
921 /* Find first free spot on workspace 0 */
922 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
923 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
925 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
926 aicon
->dock
= scr
->workspaces
[0]->clip
;
928 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
931 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);
935 /* Returns the workspace number for a given workspace name */
936 int wGetWorkspaceNumber(WScreen
*scr
, const char *value
)
940 if (sscanf(value
, "%i", &w
) != 1) {
942 for (i
= 0; i
< scr
->workspace_count
; i
++) {
943 if (strcmp(scr
->workspaces
[i
]->name
, value
) == 0) {