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"
53 #define MC_DESTROY_LAST 1
54 #define MC_LAST_USED 2
55 /* index of the first workspace menu entry */
56 #define MC_WORKSPACE1 3
58 #define MAX_SHORTCUT_LENGTH 32
59 #define WORKSPACE_NAME_DISPLAY_PADDING 32
61 extern int ignore_wks_change
;
62 extern WPreferences wPreferences
;
63 extern XContext wVEdgeContext
;
64 extern void ProcessPendingEvents();
65 extern WShortKey wKeyBindings
[WKBD_LAST
];
67 static WMPropList
*dWorkspaces
= NULL
;
68 static WMPropList
*dClip
, *dName
;
70 static void make_keys(void)
72 if (dWorkspaces
!= NULL
)
75 dWorkspaces
= WMCreatePLString("Workspaces");
76 dName
= WMCreatePLString("Name");
77 dClip
= WMCreatePLString("Clip");
80 void wWorkspaceMake(WScreen
* scr
, int count
)
88 int wWorkspaceNew(WScreen
*scr
)
90 WWorkspace
*wspace
, **list
;
93 if (scr
->workspace_count
< MAX_WORKSPACES
) {
94 scr
->workspace_count
++;
96 wspace
= wmalloc(sizeof(WWorkspace
));
101 wspace
->name
= wmalloc(strlen(_("Workspace %i")) + 8);
102 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
105 if (!wPreferences
.flags
.noclip
)
106 wspace
->clip
= wDockCreate(scr
, WM_CLIP
, NULL
);
108 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
110 for (i
= 0; i
< scr
->workspace_count
- 1; i
++)
111 list
[i
] = scr
->workspaces
[i
];
115 wfree(scr
->workspaces
);
117 scr
->workspaces
= list
;
119 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
120 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
121 wNETWMUpdateDesktop(scr
);
122 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
125 return scr
->workspace_count
- 1;
131 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
140 /* verify if workspace is in use by some window */
141 tmp
= scr
->focused_window
;
143 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
148 if (!wPreferences
.flags
.noclip
) {
149 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
150 scr
->workspaces
[workspace
]->clip
= NULL
;
153 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
155 for (i
= 0; i
< scr
->workspace_count
; i
++) {
156 if (i
!= workspace
) {
157 list
[j
++] = scr
->workspaces
[i
];
159 if (scr
->workspaces
[i
]->name
)
160 wfree(scr
->workspaces
[i
]->name
);
161 wfree(scr
->workspaces
[i
]);
164 wfree(scr
->workspaces
);
165 scr
->workspaces
= list
;
167 scr
->workspace_count
--;
170 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
171 /* clip workspace menu */
172 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
174 /* update also window menu */
175 if (scr
->workspace_submenu
) {
176 WMenu
*menu
= scr
->workspace_submenu
;
179 while (i
> scr
->workspace_count
)
180 wMenuRemoveItem(menu
, --i
);
184 if (scr
->clip_submenu
) {
185 WMenu
*menu
= scr
->clip_submenu
;
188 while (i
> scr
->workspace_count
)
189 wMenuRemoveItem(menu
, --i
);
192 wNETWMUpdateDesktop(scr
);
193 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
195 if (scr
->current_workspace
>= scr
->workspace_count
)
196 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
197 if (scr
->last_workspace
>= scr
->workspace_count
)
198 scr
->last_workspace
= 0;
203 typedef struct WorkspaceNameData
{
210 static void hideWorkspaceName(void *data
)
212 WScreen
*scr
= (WScreen
*) data
;
214 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
215 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
216 XUnmapWindow(dpy
, scr
->workspace_name
);
218 if (scr
->workspace_name_data
) {
219 RReleaseImage(scr
->workspace_name_data
->back
);
220 RReleaseImage(scr
->workspace_name_data
->text
);
221 wfree(scr
->workspace_name_data
);
223 scr
->workspace_name_data
= NULL
;
225 scr
->workspace_name_timer
= NULL
;
227 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
230 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
232 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
233 scr
->workspace_name_data
->count
* 255 / 10);
235 RConvertImage(scr
->rcontext
, img
, &pix
);
239 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
240 XClearWindow(dpy
, scr
->workspace_name
);
241 XFreePixmap(dpy
, pix
);
244 scr
->workspace_name_data
->count
--;
248 static void showWorkspaceName(WScreen
* scr
, int workspace
)
250 WorkspaceNameData
*data
;
255 char *name
= scr
->workspaces
[workspace
]->name
;
256 int len
= strlen(name
);
264 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2) {
268 if (scr
->workspace_name_timer
) {
269 WMDeleteTimerHandler(scr
->workspace_name_timer
);
270 XUnmapWindow(dpy
, scr
->workspace_name
);
273 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
275 if (scr
->workspace_name_data
) {
276 RReleaseImage(scr
->workspace_name_data
->back
);
277 RReleaseImage(scr
->workspace_name_data
->text
);
278 wfree(scr
->workspace_name_data
);
281 data
= wmalloc(sizeof(WorkspaceNameData
));
284 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
285 h
= WMFontHeight(scr
->workspace_name_font
);
288 head
= wGetHeadForPointerLocation(scr
);
289 rect
= wGetRectForHead(scr
, head
);
290 if (scr
->xine_info
.count
) {
291 xx
= rect
.pos
.x
+ (scr
->xine_info
.screens
[head
].size
.width
- (w
+ 4)) / 2;
292 yy
= rect
.pos
.y
+ (scr
->xine_info
.screens
[head
].size
.height
- (h
+ 4)) / 2;
295 xx
= (scr
->scr_width
- (w
+ 4)) / 2;
296 yy
= (scr
->scr_height
- (h
+ 4)) / 2;
300 switch (wPreferences
.workspace_name_display_position
) {
305 px
= (scr
->scr_width
- (w
+ 4)) / 2;
307 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
313 px
= (scr
->scr_width
- (w
+ 4)) / 2;
315 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
318 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
319 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
322 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
323 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
326 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
327 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
330 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
331 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
339 px
= (scr
->scr_width
- (w
+ 4)) / 2;
340 py
= (scr
->scr_height
- (h
+ 4)) / 2;
344 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
345 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
347 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
348 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
350 /*XSetForeground(dpy, scr->mono_gc, 0);
351 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
353 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
355 for (x
= 0; x
<= 4; x
++) {
356 for (y
= 0; y
<= 4; y
++) {
357 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, x
, y
, name
, len
);
361 XSetForeground(dpy
, scr
->mono_gc
, 1);
362 XSetBackground(dpy
, scr
->mono_gc
, 0);
364 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+ 4, h
+ 4, 0, 0, 1 << (scr
->w_depth
- 1));
366 /*XSetForeground(dpy, scr->mono_gc, 1); */
367 XSetBackground(dpy
, scr
->mono_gc
, 1);
369 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
371 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, 2, 2, name
, len
);
374 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
376 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
377 XClearWindow(dpy
, scr
->workspace_name
);
379 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
381 XFreePixmap(dpy
, text
);
382 XFreePixmap(dpy
, mask
);
385 XMapRaised(dpy
, scr
->workspace_name
);
391 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
393 if (!ximg
|| !ximg
->image
) {
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 (ignore_wks_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 int toUnmapSize
, toUnmapCount
;
477 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
480 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
482 if (workspace
> scr
->workspace_count
- 1) {
483 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
);
497 toUnmap
= wmalloc(toUnmapSize
* sizeof(WWindow
*));
499 if ((tmp
= scr
->focused_window
) != NULL
) {
500 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
501 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
505 /* foc2 = tmp; will fix annoyance with gnome panel
506 * but will create annoyance for every other application
509 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
510 /* unmap windows not on this workspace */
511 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
512 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
513 if (toUnmapCount
== toUnmapSize
)
516 toUnmap
= wrealloc(toUnmap
, toUnmapSize
* sizeof(WWindow
*));
518 toUnmap
[toUnmapCount
++] = tmp
;
520 /* also unmap miniwindows not on this workspace */
521 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
522 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
523 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
524 tmp
->icon
->mapped
= 0;
526 /* update current workspace of omnipresent windows */
527 if (IS_OMNIPRESENT(tmp
)) {
528 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
530 tmp
->frame
->workspace
= workspace
;
533 wapp
->last_workspace
= workspace
;
535 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
540 /* change selected windows' workspace */
541 if (tmp
->flags
.selected
) {
542 wWindowChangeWorkspace(tmp
, workspace
);
543 if (!tmp
->flags
.miniaturized
&& !foc
) {
547 if (!tmp
->flags
.hidden
) {
548 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
549 /* remap windows that are on this workspace */
551 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
555 /* Also map miniwindow if not omnipresent */
556 if (!wPreferences
.sticky_icons
&&
557 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
558 tmp
->icon
->mapped
= 1;
559 XMapWindow(dpy
, tmp
->icon
->core
->window
);
567 while (toUnmapCount
> 0)
569 wWindowUnmap(toUnmap
[--toUnmapCount
]);
573 /* Gobble up events unleashed by our mapping & unmapping.
574 * These may trigger various grab-initiated focus &
575 * crossing events. However, we don't care about them,
576 * and ignore their focus implications altogether to avoid
579 scr
->flags
.ignore_focus_events
= 1;
580 ProcessPendingEvents();
581 scr
->flags
.ignore_focus_events
= 0;
586 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
587 foc
= scr
->focused_window
;
589 if (wPreferences
.focus_mode
== WKF_CLICK
) {
590 wSetFocusTo(scr
, foc
);
598 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
599 tmp
= wWindowFor(win
);
602 /* If there's a window under the pointer, focus it.
603 * (we ate all other focus events above, so it's
604 * certainly not focused). Otherwise focus last
605 * focused, or the root (depending on sloppiness)
607 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
608 wSetFocusTo(scr
, foc
);
610 wSetFocusTo(scr
, tmp
);
615 /* We need to always arrange icons when changing workspace, even if
616 * no autoarrange icons, because else the icons in different workspaces
618 * This can be avoided if appicons are also workspace specific.
620 if (!wPreferences
.sticky_icons
)
621 wArrangeIcons(scr
, False
);
624 wAppIconPaint(scr
->dock
->icon_array
[0]);
626 if (!wPreferences
.flags
.noclip
&& (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
627 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
)) {
628 /* to handle enter notify. This will also */
629 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
630 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
632 else if (scr
->clip_icon
!= NULL
) {
633 wClipIconPaint(scr
->clip_icon
);
635 wScreenUpdateUsableArea(scr
);
636 wNETWMUpdateDesktop(scr
);
637 showWorkspaceName(scr
, workspace
);
639 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
641 /* XSync(dpy, False); */
644 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
646 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
649 static void lastWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
651 wWorkspaceChange(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->last_workspace
);
654 static void deleteWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
656 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
659 static void newWSCommand(WMenu
*menu
, WMenuEntry
*foo
)
663 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
665 /* autochange workspace */
667 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
670 void wWorkspaceRename(WScreen
* scr
, int workspace
, char *name
)
672 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
675 if (workspace
>= scr
->workspace_count
)
678 /* trim white spaces */
679 tmp
= wtrimspace(name
);
681 if (strlen(tmp
) == 0) {
682 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
684 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
686 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
689 /* update workspace */
690 wfree(scr
->workspaces
[workspace
]->name
);
691 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
693 if (scr
->clip_ws_menu
) {
694 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
, buf
) != 0) {
695 wfree(scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
);
696 scr
->clip_ws_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
= wstrdup(buf
);
697 wMenuRealize(scr
->clip_ws_menu
);
700 if (scr
->workspace_menu
) {
701 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
, buf
) != 0) {
702 wfree(scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
);
703 scr
->workspace_menu
->entries
[workspace
+ MC_WORKSPACE1
]->text
= wstrdup(buf
);
704 wMenuRealize(scr
->workspace_menu
);
709 wClipIconPaint(scr
->clip_icon
);
711 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
714 /* callback for when menu entry is edited */
715 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
720 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
723 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
728 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
730 wwarning(_("could not create Workspace menu"));
734 /* callback to be called when an entry is edited */
735 wsmenu
->on_edit
= onMenuEntryEdited
;
737 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
738 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
740 entry
= wMenuAddCallback(wsmenu
, _("Last Used"), lastWSCommand
, NULL
);
741 entry
->rtext
= GetShortcutKey(wKeyBindings
[WKBD_LASTWORKSPACE
]);
746 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
750 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
757 if (menu
->entry_no
< scr
->workspace_count
+ MC_WORKSPACE1
) {
758 /* new workspace(s) added */
759 i
= scr
->workspace_count
- (menu
->entry_no
- MC_WORKSPACE1
);
760 ws
= menu
->entry_no
- MC_WORKSPACE1
;
762 wstrlcpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
764 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
765 entry
->flags
.indicator
= 1;
766 entry
->flags
.editable
= 1;
771 } else if (menu
->entry_no
> scr
->workspace_count
+ MC_WORKSPACE1
) {
772 /* removed workspace(s) */
773 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ MC_WORKSPACE1
; i
--) {
774 wMenuRemoveItem(menu
, i
);
778 for (i
= 0; i
< scr
->workspace_count
; i
++) {
779 /* workspace shortcut labels */
780 if (i
/ 10 == scr
->current_workspace
/ 10)
781 menu
->entries
[i
+ MC_WORKSPACE1
]->rtext
= GetShortcutKey(wKeyBindings
[WKBD_WORKSPACE1
+ (i
% 10)]);
783 menu
->entries
[i
+ MC_WORKSPACE1
]->rtext
= NULL
;
785 menu
->entries
[i
+ MC_WORKSPACE1
]->flags
.indicator_on
= 0;
787 menu
->entries
[scr
->current_workspace
+ MC_WORKSPACE1
]->flags
.indicator_on
= 1;
790 /* don't let user destroy current workspace */
791 if (scr
->current_workspace
== scr
->workspace_count
- 1) {
792 wMenuSetEnabled(menu
, MC_DESTROY_LAST
, False
);
794 wMenuSetEnabled(menu
, MC_DESTROY_LAST
, True
);
797 /* back to last workspace */
798 if (scr
->workspace_count
&& scr
->last_workspace
!= scr
->current_workspace
)
799 wMenuSetEnabled(menu
, MC_LAST_USED
, True
);
801 wMenuSetEnabled(menu
, MC_LAST_USED
, False
);
803 tmp
= menu
->frame
->top_width
+ 5;
804 /* if menu got unreachable, bring it to a visible place */
805 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
806 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
811 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
813 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
818 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
819 parr
= WMCreatePLArray(NULL
);
820 for (i
= 0; i
< scr
->workspace_count
; i
++) {
821 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
822 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
823 WMReleasePropList(pstr
);
824 if (!wPreferences
.flags
.noclip
) {
825 pstr
= wClipSaveWorkspaceState(scr
, i
);
826 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
827 WMReleasePropList(pstr
);
828 } else if (old_wks_state
!= NULL
) {
829 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
830 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
831 WMPutInPLDictionary(wks_state
, dClip
, bar
);
835 WMAddToPLArray(parr
, wks_state
);
836 WMReleasePropList(wks_state
);
838 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
839 WMReleasePropList(parr
);
842 void wWorkspaceRestoreState(WScreen
*scr
)
844 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
849 if (scr
->session_state
== NULL
)
852 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
857 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
858 wks_state
= WMGetFromPLArray(parr
, i
);
859 if (WMIsPLDictionary(wks_state
))
860 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
864 if (i
>= scr
->workspace_count
)
867 if (scr
->workspace_menu
) {
868 wfree(scr
->workspace_menu
->entries
[i
+ MC_WORKSPACE1
]->text
);
869 scr
->workspace_menu
->entries
[i
+ MC_WORKSPACE1
]->text
= wstrdup(WMGetFromPLString(pstr
));
870 scr
->workspace_menu
->flags
.realized
= 0;
873 wfree(scr
->workspaces
[i
]->name
);
874 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
875 if (!wPreferences
.flags
.noclip
) {
876 int added_omnipresent_icons
= 0;
878 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
879 if (scr
->workspaces
[i
]->clip
)
880 wDockDestroy(scr
->workspaces
[i
]->clip
);
882 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
884 wDockHideIcons(scr
->workspaces
[i
]->clip
);
886 /* We set the global icons here, because scr->workspaces[i]->clip
887 * was not valid in wDockRestoreState().
888 * There we only set icon->omnipresent to know which icons we
891 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
892 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
895 if (!aicon
|| !aicon
->omnipresent
)
897 aicon
->omnipresent
= 0;
898 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
903 /* Move this appicon from workspace i to workspace 0 */
904 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
905 scr
->workspaces
[i
]->clip
->icon_count
--;
907 added_omnipresent_icons
++;
908 /* If there are too many omnipresent appicons, we are in trouble */
909 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
910 <= scr
->workspaces
[0]->clip
->max_icons
);
911 /* Find first free spot on workspace 0 */
912 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
913 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
915 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
916 aicon
->dock
= scr
->workspaces
[0]->clip
;
918 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
921 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);
925 /* Returns the workspace number for a given workspace name */
926 int wGetWorkspaceNumber(WScreen
* scr
, char * value
)
930 if (sscanf(value
, "%i", &w
) != 1) {
932 for (i
= 0; i
< scr
->workspace_count
; i
++) {
933 if (strcmp(scr
->workspaces
[i
]->name
, value
) == 0) {