wAppIconPaint argument removed
[wmaker-crm.git] / src / workspace.c
blob7b62798bdea08beee51bb30d654608097496cd1d
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.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #ifdef SHAPE
26 #include <X11/extensions/shape.h>
27 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <time.h>
36 #include <sys/time.h>
38 #include "WindowMaker.h"
39 #include "framewin.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "funcs.h"
43 #include "menu.h"
44 #include "application.h"
45 #include "dock.h"
46 #include "actions.h"
47 #include "workspace.h"
48 #include "appicon.h"
49 #include "wmspec.h"
50 #include "xinerama.h"
52 #define MAX_SHORTCUT_LENGTH 32
53 #define WORKSPACE_NAME_DISPLAY_PADDING 32
55 extern int ignore_wks_change;
56 extern WPreferences wPreferences;
57 extern XContext wVEdgeContext;
58 extern void ProcessPendingEvents();
60 static WMPropList *dWorkspaces = NULL;
61 static WMPropList *dClip, *dName;
63 static void make_keys(void)
65 if (dWorkspaces != NULL)
66 return;
68 dWorkspaces = WMCreatePLString("Workspaces");
69 dName = WMCreatePLString("Name");
70 dClip = WMCreatePLString("Clip");
73 void wWorkspaceMake(WScreen * scr, int count)
75 while (count > 0) {
76 wWorkspaceNew(scr);
77 count--;
81 int wWorkspaceNew(WScreen * scr)
83 WWorkspace *wspace, **list;
84 int i;
86 if (scr->workspace_count < MAX_WORKSPACES) {
87 scr->workspace_count++;
89 wspace = wmalloc(sizeof(WWorkspace));
90 wspace->name = NULL;
92 if (!wspace->name) {
93 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
94 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
97 if (!wPreferences.flags.noclip) {
98 wspace->clip = wDockCreate(scr, WM_CLIP);
99 } else
100 wspace->clip = NULL;
102 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
104 for (i = 0; i < scr->workspace_count - 1; i++) {
105 list[i] = scr->workspaces[i];
107 list[i] = wspace;
108 if (scr->workspaces)
109 wfree(scr->workspaces);
110 scr->workspaces = list;
112 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
113 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
114 wNETWMUpdateDesktop(scr);
115 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
116 XFlush(dpy);
118 return scr->workspace_count - 1;
121 return -1;
124 Bool wWorkspaceDelete(WScreen * scr, int workspace)
126 WWindow *tmp;
127 WWorkspace **list;
128 int i, j;
130 if (workspace <= 0)
131 return False;
133 /* verify if workspace is in use by some window */
134 tmp = scr->focused_window;
135 while (tmp) {
136 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
137 return False;
138 tmp = tmp->prev;
141 if (!wPreferences.flags.noclip) {
142 wDockDestroy(scr->workspaces[workspace]->clip);
143 scr->workspaces[workspace]->clip = NULL;
146 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
147 j = 0;
148 for (i = 0; i < scr->workspace_count; i++) {
149 if (i != workspace) {
150 list[j++] = scr->workspaces[i];
151 } else {
152 if (scr->workspaces[i]->name)
153 wfree(scr->workspaces[i]->name);
154 wfree(scr->workspaces[i]);
157 wfree(scr->workspaces);
158 scr->workspaces = list;
160 scr->workspace_count--;
162 /* update menu */
163 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
164 /* clip workspace menu */
165 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
167 /* update also window menu */
168 if (scr->workspace_submenu) {
169 WMenu *menu = scr->workspace_submenu;
171 i = menu->entry_no;
172 while (i > scr->workspace_count)
173 wMenuRemoveItem(menu, --i);
174 wMenuRealize(menu);
176 /* and clip menu */
177 if (scr->clip_submenu) {
178 WMenu *menu = scr->clip_submenu;
180 i = menu->entry_no;
181 while (i > scr->workspace_count)
182 wMenuRemoveItem(menu, --i);
183 wMenuRealize(menu);
185 wNETWMUpdateDesktop(scr);
186 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
188 if (scr->current_workspace >= scr->workspace_count)
189 wWorkspaceChange(scr, scr->workspace_count - 1);
191 return True;
194 typedef struct WorkspaceNameData {
195 int count;
196 RImage *back;
197 RImage *text;
198 time_t timeout;
199 } WorkspaceNameData;
201 static void hideWorkspaceName(void *data)
203 WScreen *scr = (WScreen *) data;
205 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
206 || time(NULL) > scr->workspace_name_data->timeout) {
207 XUnmapWindow(dpy, scr->workspace_name);
209 if (scr->workspace_name_data) {
210 RReleaseImage(scr->workspace_name_data->back);
211 RReleaseImage(scr->workspace_name_data->text);
212 wfree(scr->workspace_name_data);
214 scr->workspace_name_data = NULL;
216 scr->workspace_name_timer = NULL;
217 } else {
218 RImage *img = RCloneImage(scr->workspace_name_data->back);
219 Pixmap pix;
221 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
223 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
224 scr->workspace_name_data->count * 255 / 10);
226 RConvertImage(scr->rcontext, img, &pix);
228 RReleaseImage(img);
230 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
231 XClearWindow(dpy, scr->workspace_name);
232 XFreePixmap(dpy, pix);
233 XFlush(dpy);
235 scr->workspace_name_data->count--;
239 static void showWorkspaceName(WScreen * scr, int workspace)
241 WorkspaceNameData *data;
242 RXImage *ximg;
243 Pixmap text, mask;
244 int w, h;
245 int px, py;
246 char *name = scr->workspaces[workspace]->name;
247 int len = strlen(name);
248 int x, y;
249 #ifdef XINERAMA
250 int head;
251 WMRect rect;
252 int xx, yy;
253 #endif
255 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
256 return;
259 if (scr->workspace_name_timer) {
260 WMDeleteTimerHandler(scr->workspace_name_timer);
261 XUnmapWindow(dpy, scr->workspace_name);
262 XFlush(dpy);
264 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
266 if (scr->workspace_name_data) {
267 RReleaseImage(scr->workspace_name_data->back);
268 RReleaseImage(scr->workspace_name_data->text);
269 wfree(scr->workspace_name_data);
272 data = wmalloc(sizeof(WorkspaceNameData));
273 data->back = NULL;
275 w = WMWidthOfString(scr->workspace_name_font, name, len);
276 h = WMFontHeight(scr->workspace_name_font);
278 #ifdef XINERAMA
279 head = wGetHeadForPointerLocation(scr);
280 rect = wGetRectForHead(scr, head);
281 if (scr->xine_info.count) {
282 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
283 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
285 else {
286 xx = (scr->scr_width - (w + 4)) / 2;
287 yy = (scr->scr_height - (h + 4)) / 2;
289 #endif
291 switch (wPreferences.workspace_name_display_position) {
292 case WD_TOP:
293 #ifdef XINERAMA
294 px = xx;
295 #else
296 px = (scr->scr_width - (w + 4)) / 2;
297 #endif
298 py = WORKSPACE_NAME_DISPLAY_PADDING;
299 break;
300 case WD_BOTTOM:
301 #ifdef XINERAMA
302 px = xx;
303 #else
304 px = (scr->scr_width - (w + 4)) / 2;
305 #endif
306 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
307 break;
308 case WD_TOPLEFT:
309 px = WORKSPACE_NAME_DISPLAY_PADDING;
310 py = WORKSPACE_NAME_DISPLAY_PADDING;
311 break;
312 case WD_TOPRIGHT:
313 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
314 py = WORKSPACE_NAME_DISPLAY_PADDING;
315 break;
316 case WD_BOTTOMLEFT:
317 px = WORKSPACE_NAME_DISPLAY_PADDING;
318 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
319 break;
320 case WD_BOTTOMRIGHT:
321 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
322 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
323 break;
324 case WD_CENTER:
325 default:
326 #ifdef XINERAMA
327 px = xx;
328 py = yy;
329 #else
330 px = (scr->scr_width - (w + 4)) / 2;
331 py = (scr->scr_height - (h + 4)) / 2;
332 #endif
333 break;
335 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
336 XMoveWindow(dpy, scr->workspace_name, px, py);
338 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
339 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
341 /*XSetForeground(dpy, scr->mono_gc, 0);
342 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
344 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
346 for (x = 0; x <= 4; x++) {
347 for (y = 0; y <= 4; y++) {
348 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
352 XSetForeground(dpy, scr->mono_gc, 1);
353 XSetBackground(dpy, scr->mono_gc, 0);
355 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
357 /*XSetForeground(dpy, scr->mono_gc, 1); */
358 XSetBackground(dpy, scr->mono_gc, 1);
360 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
362 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
364 #ifdef SHAPE
365 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
366 #endif
367 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
368 XClearWindow(dpy, scr->workspace_name);
370 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
372 XFreePixmap(dpy, text);
373 XFreePixmap(dpy, mask);
375 if (!data->text) {
376 XMapRaised(dpy, scr->workspace_name);
377 XFlush(dpy);
379 goto erro;
382 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
384 if (!ximg || !ximg->image) {
385 goto erro;
388 XMapRaised(dpy, scr->workspace_name);
389 XFlush(dpy);
391 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
392 RDestroyXImage(scr->rcontext, ximg);
394 if (!data->back) {
395 goto erro;
398 data->count = 10;
400 /* set a timeout for the effect */
401 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
403 scr->workspace_name_data = data;
405 return;
407 erro:
408 if (scr->workspace_name_timer)
409 WMDeleteTimerHandler(scr->workspace_name_timer);
411 if (data->text)
412 RReleaseImage(data->text);
413 if (data->back)
414 RReleaseImage(data->back);
415 wfree(data);
417 scr->workspace_name_data = NULL;
419 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
420 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
423 void wWorkspaceChange(WScreen *scr, int workspace)
425 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
426 return;
428 if (workspace != scr->current_workspace)
429 wWorkspaceForceChange(scr, workspace);
432 void wWorkspaceRelativeChange(WScreen * scr, int amount)
434 int w;
436 /* While the deiconify animation is going on the window is
437 * still "flying" to its final position and we don't want to
438 * change workspace before the animation finishes, otherwise
439 * the window will land in the new workspace */
440 if (ignore_wks_change)
441 return;
443 w = scr->current_workspace + amount;
445 if (amount < 0) {
446 if (w >= 0) {
447 wWorkspaceChange(scr, w);
448 } else if (wPreferences.ws_cycle) {
449 wWorkspaceChange(scr, scr->workspace_count + w);
451 } else if (amount > 0) {
452 if (w < scr->workspace_count) {
453 wWorkspaceChange(scr, w);
454 } else if (wPreferences.ws_advance) {
455 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
456 } else if (wPreferences.ws_cycle) {
457 wWorkspaceChange(scr, w % scr->workspace_count);
462 void wWorkspaceForceChange(WScreen * scr, int workspace)
464 WWindow *tmp, *foc = NULL, *foc2 = NULL;
466 if (workspace >= MAX_WORKSPACES || workspace < 0)
467 return;
469 SendHelperMessage(scr, 'C', workspace + 1, NULL);
471 if (workspace > scr->workspace_count - 1) {
472 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
475 wClipUpdateForWorkspaceChange(scr, workspace);
477 scr->current_workspace = workspace;
479 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
481 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
483 if ((tmp = scr->focused_window) != NULL) {
484 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
485 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
486 foc = tmp;
489 /* foc2 = tmp; will fix annoyance with gnome panel
490 * but will create annoyance for every other application
492 while (tmp) {
493 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
494 /* unmap windows not on this workspace */
495 if ((tmp->flags.mapped || tmp->flags.shaded) &&
496 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
497 wWindowUnmap(tmp);
499 /* also unmap miniwindows not on this workspace */
500 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
501 tmp->icon && !IS_OMNIPRESENT(tmp)) {
502 XUnmapWindow(dpy, tmp->icon->core->window);
503 tmp->icon->mapped = 0;
505 /* update current workspace of omnipresent windows */
506 if (IS_OMNIPRESENT(tmp)) {
507 WApplication *wapp = wApplicationOf(tmp->main_window);
509 tmp->frame->workspace = workspace;
511 if (wapp) {
512 wapp->last_workspace = workspace;
514 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
515 foc2 = tmp;
518 } else {
519 /* change selected windows' workspace */
520 if (tmp->flags.selected) {
521 wWindowChangeWorkspace(tmp, workspace);
522 if (!tmp->flags.miniaturized && !foc) {
523 foc = tmp;
525 } else {
526 if (!tmp->flags.hidden) {
527 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
528 /* remap windows that are on this workspace */
529 wWindowMap(tmp);
530 if (!foc && !WFLAGP(tmp, no_focusable)) {
531 foc = tmp;
534 /* Also map miniwindow if not omnipresent */
535 if (!wPreferences.sticky_icons &&
536 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
537 tmp->icon->mapped = 1;
538 XMapWindow(dpy, tmp->icon->core->window);
543 tmp = tmp->prev;
546 /* Gobble up events unleashed by our mapping & unmapping.
547 * These may trigger various grab-initiated focus &
548 * crossing events. However, we don't care about them,
549 * and ignore their focus implications altogether to avoid
550 * flicker.
552 scr->flags.ignore_focus_events = 1;
553 ProcessPendingEvents();
554 scr->flags.ignore_focus_events = 0;
556 if (!foc)
557 foc = foc2;
559 if (scr->focused_window->flags.mapped && !foc) {
560 foc = scr->focused_window;
562 if (wPreferences.focus_mode == WKF_CLICK) {
563 wSetFocusTo(scr, foc);
564 } else {
565 unsigned int mask;
566 int foo;
567 Window bar, win;
568 WWindow *tmp;
570 tmp = NULL;
571 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
572 tmp = wWindowFor(win);
575 /* If there's a window under the pointer, focus it.
576 * (we ate all other focus events above, so it's
577 * certainly not focused). Otherwise focus last
578 * focused, or the root (depending on sloppiness)
580 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
581 wSetFocusTo(scr, foc);
582 } else {
583 wSetFocusTo(scr, tmp);
588 /* We need to always arrange icons when changing workspace, even if
589 * no autoarrange icons, because else the icons in different workspaces
590 * can be superposed.
591 * This can be avoided if appicons are also workspace specific.
593 if (!wPreferences.sticky_icons)
594 wArrangeIcons(scr, False);
596 if (scr->dock)
597 wAppIconPaint(scr->dock->icon_array[0]);
599 if (scr->clip_icon) {
600 if (scr->workspaces[workspace]->clip->auto_collapse ||
601 scr->workspaces[workspace]->clip->auto_raise_lower) {
602 /* to handle enter notify. This will also */
603 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
604 XMapWindow(dpy, scr->clip_icon->icon->core->window);
605 } else {
606 wClipIconPaint(scr->clip_icon);
609 wScreenUpdateUsableArea(scr);
610 wNETWMUpdateDesktop(scr);
611 showWorkspaceName(scr, workspace);
613 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
615 /* XSync(dpy, False); */
618 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
620 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
623 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
625 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
628 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
630 int ws;
632 ws = wWorkspaceNew(menu->frame->screen_ptr);
633 /* autochange workspace */
634 if (ws >= 0)
635 wWorkspaceChange(menu->frame->screen_ptr, ws);
638 if (ws<9) {
639 int kcode;
640 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
641 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
642 entry->rtext =
643 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
645 } */
648 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
650 char buf[MAX_WORKSPACENAME_WIDTH + 1];
651 char *tmp;
653 if (workspace >= scr->workspace_count)
654 return;
656 /* trim white spaces */
657 tmp = wtrimspace(name);
659 if (strlen(tmp) == 0) {
660 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
661 } else {
662 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
664 buf[MAX_WORKSPACENAME_WIDTH] = 0;
665 wfree(tmp);
667 /* update workspace */
668 wfree(scr->workspaces[workspace]->name);
669 scr->workspaces[workspace]->name = wstrdup(buf);
671 if (scr->clip_ws_menu) {
672 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
673 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
674 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
675 wMenuRealize(scr->clip_ws_menu);
678 if (scr->workspace_menu) {
679 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
680 wfree(scr->workspace_menu->entries[workspace + 2]->text);
681 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
682 wMenuRealize(scr->workspace_menu);
686 if (scr->clip_icon)
687 wClipIconPaint(scr->clip_icon);
689 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
692 /* callback for when menu entry is edited */
693 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
695 char *tmp;
697 tmp = entry->text;
698 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
701 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
703 WMenu *wsmenu;
705 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
706 if (!wsmenu) {
707 wwarning(_("could not create Workspace menu"));
708 return NULL;
711 /* callback to be called when an entry is edited */
712 wsmenu->on_edit = onMenuEntryEdited;
714 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
715 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
717 return wsmenu;
720 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
722 int i;
723 long ws;
724 char title[MAX_WORKSPACENAME_WIDTH + 1];
725 WMenuEntry *entry;
726 int tmp;
728 if (!menu)
729 return;
731 if (menu->entry_no < scr->workspace_count + 2) {
732 /* new workspace(s) added */
733 i = scr->workspace_count - (menu->entry_no - 2);
734 ws = menu->entry_no - 2;
735 while (i > 0) {
736 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
738 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
739 entry->flags.indicator = 1;
740 entry->flags.editable = 1;
742 i--;
743 ws++;
745 } else if (menu->entry_no > scr->workspace_count + 2) {
746 /* removed workspace(s) */
747 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
748 wMenuRemoveItem(menu, i);
751 wMenuRealize(menu);
753 for (i = 0; i < scr->workspace_count; i++) {
754 menu->entries[i + 2]->flags.indicator_on = 0;
756 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
758 /* don't let user destroy current workspace */
759 if (scr->current_workspace == scr->workspace_count - 1) {
760 wMenuSetEnabled(menu, 1, False);
761 } else {
762 wMenuSetEnabled(menu, 1, True);
765 tmp = menu->frame->top_width + 5;
766 /* if menu got unreachable, bring it to a visible place */
767 if (menu->frame_x < tmp - (int)menu->frame->core->width)
768 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
770 wMenuPaint(menu);
773 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
775 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
776 int i;
778 make_keys();
780 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
781 parr = WMCreatePLArray(NULL);
782 for (i = 0; i < scr->workspace_count; i++) {
783 pstr = WMCreatePLString(scr->workspaces[i]->name);
784 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
785 WMReleasePropList(pstr);
786 if (!wPreferences.flags.noclip) {
787 pstr = wClipSaveWorkspaceState(scr, i);
788 WMPutInPLDictionary(wks_state, dClip, pstr);
789 WMReleasePropList(pstr);
790 } else if (old_wks_state != NULL) {
791 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
792 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
793 WMPutInPLDictionary(wks_state, dClip, bar);
797 WMAddToPLArray(parr, wks_state);
798 WMReleasePropList(wks_state);
800 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
801 WMReleasePropList(parr);
804 void wWorkspaceRestoreState(WScreen * scr)
806 WMPropList *parr, *pstr, *wks_state, *clip_state;
807 int i, j;
809 make_keys();
811 if (scr->session_state == NULL)
812 return;
814 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
816 if (!parr)
817 return;
819 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
820 wks_state = WMGetFromPLArray(parr, i);
821 if (WMIsPLDictionary(wks_state))
822 pstr = WMGetFromPLDictionary(wks_state, dName);
823 else
824 pstr = wks_state;
825 if (i >= scr->workspace_count)
826 wWorkspaceNew(scr);
827 if (scr->workspace_menu) {
828 wfree(scr->workspace_menu->entries[i + 2]->text);
829 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
830 scr->workspace_menu->flags.realized = 0;
832 wfree(scr->workspaces[i]->name);
833 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
834 if (!wPreferences.flags.noclip) {
835 int added_omnipresent_icons = 0;
836 clip_state = WMGetFromPLDictionary(wks_state, dClip);
837 if (scr->workspaces[i]->clip)
838 wDockDestroy(scr->workspaces[i]->clip);
839 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
840 if (i > 0)
841 wDockHideIcons(scr->workspaces[i]->clip);
843 /* We set the global icons here, because scr->workspaces[i]->clip
844 * was not valid in wDockRestoreState().
845 * There we only set icon->omnipresent to know which icons we
846 * need to set here.
848 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
849 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
850 int k;
852 if (!aicon || !aicon->omnipresent)
853 continue;
854 aicon->omnipresent = 0;
855 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
856 continue;
857 if (i == 0)
858 continue;
860 /* Move this appicon from workspace i to workspace 0 */
861 scr->workspaces[i]->clip->icon_array[j] = NULL;
862 scr->workspaces[i]->clip->icon_count--;
864 added_omnipresent_icons++;
865 /* If there are too many omnipresent appicons, we are in trouble */
866 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
867 <= scr->workspaces[0]->clip->max_icons);
868 /* Find first free spot on workspace 0 */
869 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
870 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
871 break;
872 scr->workspaces[0]->clip->icon_array[k] = aicon;
873 aicon->dock = scr->workspaces[0]->clip;
875 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
878 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
882 /* Returns the workspace number for a given workspace name */
883 int wGetWorkspaceNumber(WScreen * scr, char * value)
885 int w, i;
887 if (sscanf(value, "%i", &w) != 1) {
888 w = -1;
889 for (i = 0; i < scr->workspace_count; i++) {
890 if (strcmp(scr->workspaces[i]->name, value) == 0) {
891 w = i;
892 break;
895 } else {
896 w--;
899 return w;