Small fix for wmaker nightly build script 2
[wmaker-crm.git] / src / workspace.c
blob8135881ae5ae2d53ad0c90e3a0f094870f0297c4
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <time.h>
37 #include <sys/time.h>
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "funcs.h"
45 #include "menu.h"
46 #include "application.h"
47 #include "dock.h"
48 #include "actions.h"
49 #include "workspace.h"
50 #include "appicon.h"
51 #include "wmspec.h"
52 #include "xinerama.h"
54 #define MAX_SHORTCUT_LENGTH 32
55 #define WORKSPACE_NAME_DISPLAY_PADDING 32
57 extern int ignore_wks_change;
58 extern WPreferences wPreferences;
59 extern XContext wWinContext;
60 extern XContext wVEdgeContext;
62 extern void ProcessPendingEvents();
64 static WMPropList *dWorkspaces = NULL;
65 static WMPropList *dClip, *dName;
67 static void make_keys()
69 if (dWorkspaces != NULL)
70 return;
72 dWorkspaces = WMCreatePLString("Workspaces");
73 dName = WMCreatePLString("Name");
74 dClip = WMCreatePLString("Clip");
77 void wWorkspaceMake(WScreen * scr, int count)
79 while (count > 0) {
80 wWorkspaceNew(scr);
81 count--;
85 int wWorkspaceNew(WScreen * scr)
87 WWorkspace *wspace, **list;
88 int i;
90 if (scr->workspace_count < MAX_WORKSPACES) {
91 scr->workspace_count++;
93 wspace = wmalloc(sizeof(WWorkspace));
94 wspace->name = NULL;
96 if (!wspace->name) {
97 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
98 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
101 if (!wPreferences.flags.noclip) {
102 wspace->clip = wDockCreate(scr, WM_CLIP);
103 } else
104 wspace->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];
111 list[i] = wspace;
112 if (scr->workspaces)
113 wfree(scr->workspaces);
114 scr->workspaces = list;
116 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
117 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
118 wNETWMUpdateDesktop(scr);
119 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
120 XFlush(dpy);
122 return scr->workspace_count - 1;
125 return -1;
128 Bool wWorkspaceDelete(WScreen * scr, int workspace)
130 WWindow *tmp;
131 WWorkspace **list;
132 int i, j;
134 if (workspace <= 0)
135 return False;
137 /* verify if workspace is in use by some window */
138 tmp = scr->focused_window;
139 while (tmp) {
140 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
141 return False;
142 tmp = tmp->prev;
145 if (!wPreferences.flags.noclip) {
146 wDockDestroy(scr->workspaces[workspace]->clip);
147 scr->workspaces[workspace]->clip = NULL;
150 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
151 j = 0;
152 for (i = 0; i < scr->workspace_count; i++) {
153 if (i != workspace) {
154 list[j++] = scr->workspaces[i];
155 } else {
156 if (scr->workspaces[i]->name)
157 wfree(scr->workspaces[i]->name);
158 wfree(scr->workspaces[i]);
161 wfree(scr->workspaces);
162 scr->workspaces = list;
164 scr->workspace_count--;
166 /* update menu */
167 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
168 /* clip workspace menu */
169 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
171 /* update also window menu */
172 if (scr->workspace_submenu) {
173 WMenu *menu = scr->workspace_submenu;
175 i = menu->entry_no;
176 while (i > scr->workspace_count)
177 wMenuRemoveItem(menu, --i);
178 wMenuRealize(menu);
180 /* and clip menu */
181 if (scr->clip_submenu) {
182 WMenu *menu = scr->clip_submenu;
184 i = menu->entry_no;
185 while (i > scr->workspace_count)
186 wMenuRemoveItem(menu, --i);
187 wMenuRealize(menu);
189 wNETWMUpdateDesktop(scr);
190 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
192 if (scr->current_workspace >= scr->workspace_count)
193 wWorkspaceChange(scr, scr->workspace_count - 1);
195 return True;
198 typedef struct WorkspaceNameData {
199 int count;
200 RImage *back;
201 RImage *text;
202 time_t timeout;
203 } WorkspaceNameData;
205 static void hideWorkspaceName(void *data)
207 WScreen *scr = (WScreen *) data;
209 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
210 || time(NULL) > scr->workspace_name_data->timeout) {
211 XUnmapWindow(dpy, scr->workspace_name);
213 if (scr->workspace_name_data) {
214 RReleaseImage(scr->workspace_name_data->back);
215 RReleaseImage(scr->workspace_name_data->text);
216 wfree(scr->workspace_name_data);
218 scr->workspace_name_data = NULL;
220 scr->workspace_name_timer = NULL;
221 } else {
222 RImage *img = RCloneImage(scr->workspace_name_data->back);
223 Pixmap pix;
225 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
227 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
228 scr->workspace_name_data->count * 255 / 10);
230 RConvertImage(scr->rcontext, img, &pix);
232 RReleaseImage(img);
234 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
235 XClearWindow(dpy, scr->workspace_name);
236 XFreePixmap(dpy, pix);
237 XFlush(dpy);
239 scr->workspace_name_data->count--;
243 static void showWorkspaceName(WScreen * scr, int workspace)
245 WorkspaceNameData *data;
246 RXImage *ximg;
247 Pixmap text, mask;
248 int w, h;
249 int px, py;
250 char *name = scr->workspaces[workspace]->name;
251 int len = strlen(name);
252 int x, y;
253 #ifdef XINERAMA
254 int head;
255 WMRect rect;
256 int xx, yy;
257 #endif
259 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
260 return;
263 if (scr->workspace_name_timer) {
264 WMDeleteTimerHandler(scr->workspace_name_timer);
265 XUnmapWindow(dpy, scr->workspace_name);
266 XFlush(dpy);
268 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
270 if (scr->workspace_name_data) {
271 RReleaseImage(scr->workspace_name_data->back);
272 RReleaseImage(scr->workspace_name_data->text);
273 wfree(scr->workspace_name_data);
276 data = wmalloc(sizeof(WorkspaceNameData));
277 data->back = NULL;
279 w = WMWidthOfString(scr->workspace_name_font, name, len);
280 h = WMFontHeight(scr->workspace_name_font);
282 #ifdef XINERAMA
283 head = wGetHeadForPointerLocation(scr);
284 rect = wGetRectForHead(scr, head);
285 if (scr->xine_info.count) {
286 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
287 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
289 else {
290 xx = (scr->scr_width - (w + 4)) / 2;
291 yy = (scr->scr_height - (h + 4)) / 2;
293 #endif
295 switch (wPreferences.workspace_name_display_position) {
296 case WD_TOP:
297 #ifdef XINERAMA
298 px = xx;
299 #else
300 px = (scr->scr_width - (w + 4)) / 2;
301 #endif
302 py = WORKSPACE_NAME_DISPLAY_PADDING;
303 break;
304 case WD_BOTTOM:
305 #ifdef XINERAMA
306 px = xx;
307 #else
308 px = (scr->scr_width - (w + 4)) / 2;
309 #endif
310 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
311 break;
312 case WD_TOPLEFT:
313 px = WORKSPACE_NAME_DISPLAY_PADDING;
314 py = WORKSPACE_NAME_DISPLAY_PADDING;
315 break;
316 case WD_TOPRIGHT:
317 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
318 py = WORKSPACE_NAME_DISPLAY_PADDING;
319 break;
320 case WD_BOTTOMLEFT:
321 px = WORKSPACE_NAME_DISPLAY_PADDING;
322 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
323 break;
324 case WD_BOTTOMRIGHT:
325 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
326 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
327 break;
328 case WD_CENTER:
329 default:
330 #ifdef XINERAMA
331 px = xx;
332 py = yy;
333 #else
334 px = (scr->scr_width - (w + 4)) / 2;
335 py = (scr->scr_height - (h + 4)) / 2;
336 #endif
337 break;
339 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
340 XMoveWindow(dpy, scr->workspace_name, px, py);
342 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
343 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
345 /*XSetForeground(dpy, scr->mono_gc, 0);
346 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
348 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
350 for (x = 0; x <= 4; x++) {
351 for (y = 0; y <= 4; y++) {
352 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
356 XSetForeground(dpy, scr->mono_gc, 1);
357 XSetBackground(dpy, scr->mono_gc, 0);
359 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
361 /*XSetForeground(dpy, scr->mono_gc, 1); */
362 XSetBackground(dpy, scr->mono_gc, 1);
364 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
366 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
368 #ifdef SHAPE
369 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
370 #endif
371 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
372 XClearWindow(dpy, scr->workspace_name);
374 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
376 XFreePixmap(dpy, text);
377 XFreePixmap(dpy, mask);
379 if (!data->text) {
380 XMapRaised(dpy, scr->workspace_name);
381 XFlush(dpy);
383 goto erro;
386 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
388 if (!ximg || !ximg->image) {
389 goto erro;
392 XMapRaised(dpy, scr->workspace_name);
393 XFlush(dpy);
395 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
396 RDestroyXImage(scr->rcontext, ximg);
398 if (!data->back) {
399 goto erro;
402 data->count = 10;
404 /* set a timeout for the effect */
405 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
407 scr->workspace_name_data = data;
409 return;
411 erro:
412 if (scr->workspace_name_timer)
413 WMDeleteTimerHandler(scr->workspace_name_timer);
415 if (data->text)
416 RReleaseImage(data->text);
417 if (data->back)
418 RReleaseImage(data->back);
419 wfree(data);
421 scr->workspace_name_data = NULL;
423 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
424 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
427 void wWorkspaceChange(WScreen *scr, int workspace)
429 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
430 return;
432 if (workspace != scr->current_workspace)
433 wWorkspaceForceChange(scr, workspace);
436 void wWorkspaceRelativeChange(WScreen * scr, int amount)
438 int w;
440 /* While the deiconify animation is going on the window is
441 * still "flying" to its final position and we don't want to
442 * change workspace before the animation finishes, otherwise
443 * the window will land in the new workspace */
444 if (ignore_wks_change)
445 return;
447 w = scr->current_workspace + amount;
449 if (amount < 0) {
450 if (w >= 0) {
451 wWorkspaceChange(scr, w);
452 } else if (wPreferences.ws_cycle) {
453 wWorkspaceChange(scr, scr->workspace_count + w);
455 } else if (amount > 0) {
456 if (w < scr->workspace_count) {
457 wWorkspaceChange(scr, w);
458 } else if (wPreferences.ws_advance) {
459 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
460 } else if (wPreferences.ws_cycle) {
461 wWorkspaceChange(scr, w % scr->workspace_count);
466 void wWorkspaceForceChange(WScreen * scr, int workspace)
468 WWindow *tmp, *foc = NULL, *foc2 = NULL;
470 if (workspace >= MAX_WORKSPACES || workspace < 0)
471 return;
473 SendHelperMessage(scr, 'C', workspace + 1, NULL);
475 if (workspace > scr->workspace_count - 1) {
476 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
479 wClipUpdateForWorkspaceChange(scr, workspace);
481 scr->current_workspace = workspace;
483 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
485 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
487 if ((tmp = scr->focused_window) != NULL) {
488 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
489 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
490 foc = tmp;
493 /* foc2 = tmp; will fix annoyance with gnome panel
494 * but will create annoyance for every other application
496 while (tmp) {
497 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
498 /* unmap windows not on this workspace */
499 if ((tmp->flags.mapped || tmp->flags.shaded) &&
500 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
501 wWindowUnmap(tmp);
503 /* also unmap miniwindows not on this workspace */
504 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
505 tmp->icon && !IS_OMNIPRESENT(tmp)) {
506 XUnmapWindow(dpy, tmp->icon->core->window);
507 tmp->icon->mapped = 0;
509 /* update current workspace of omnipresent windows */
510 if (IS_OMNIPRESENT(tmp)) {
511 WApplication *wapp = wApplicationOf(tmp->main_window);
513 tmp->frame->workspace = workspace;
515 if (wapp) {
516 wapp->last_workspace = workspace;
518 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
519 foc2 = tmp;
522 } else {
523 /* change selected windows' workspace */
524 if (tmp->flags.selected) {
525 wWindowChangeWorkspace(tmp, workspace);
526 if (!tmp->flags.miniaturized && !foc) {
527 foc = tmp;
529 } else {
530 if (!tmp->flags.hidden) {
531 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
532 /* remap windows that are on this workspace */
533 wWindowMap(tmp);
534 if (!foc && !WFLAGP(tmp, no_focusable)) {
535 foc = tmp;
538 /* Also map miniwindow if not omnipresent */
539 if (!wPreferences.sticky_icons &&
540 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
541 tmp->icon->mapped = 1;
542 XMapWindow(dpy, tmp->icon->core->window);
547 tmp = tmp->prev;
550 /* Gobble up events unleashed by our mapping & unmapping.
551 * These may trigger various grab-initiated focus &
552 * crossing events. However, we don't care about them,
553 * and ignore their focus implications altogether to avoid
554 * flicker.
556 scr->flags.ignore_focus_events = 1;
557 ProcessPendingEvents();
558 scr->flags.ignore_focus_events = 0;
560 if (!foc)
561 foc = foc2;
563 if (scr->focused_window->flags.mapped && !foc) {
564 foc = scr->focused_window;
566 if (wPreferences.focus_mode == WKF_CLICK) {
567 wSetFocusTo(scr, foc);
568 } else {
569 unsigned int mask;
570 int foo;
571 Window bar, win;
572 WWindow *tmp;
574 tmp = NULL;
575 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
576 tmp = wWindowFor(win);
579 /* If there's a window under the pointer, focus it.
580 * (we ate all other focus events above, so it's
581 * certainly not focused). Otherwise focus last
582 * focused, or the root (depending on sloppiness)
584 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
585 wSetFocusTo(scr, foc);
586 } else {
587 wSetFocusTo(scr, tmp);
592 /* We need to always arrange icons when changing workspace, even if
593 * no autoarrange icons, because else the icons in different workspaces
594 * can be superposed.
595 * This can be avoided if appicons are also workspace specific.
597 if (!wPreferences.sticky_icons)
598 wArrangeIcons(scr, False);
600 if (scr->dock)
601 wAppIconPaint(scr->dock->icon_array[0]);
603 if (scr->clip_icon) {
604 if (scr->workspaces[workspace]->clip->auto_collapse ||
605 scr->workspaces[workspace]->clip->auto_raise_lower) {
606 /* to handle enter notify. This will also */
607 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
608 XMapWindow(dpy, scr->clip_icon->icon->core->window);
609 } else {
610 wClipIconPaint(scr->clip_icon);
613 wScreenUpdateUsableArea(scr);
614 wNETWMUpdateDesktop(scr);
615 showWorkspaceName(scr, workspace);
617 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
619 /* XSync(dpy, False); */
622 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
624 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
627 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
629 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
632 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
634 int ws;
636 ws = wWorkspaceNew(menu->frame->screen_ptr);
637 /* autochange workspace */
638 if (ws >= 0)
639 wWorkspaceChange(menu->frame->screen_ptr, ws);
642 if (ws<9) {
643 int kcode;
644 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
645 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
646 entry->rtext =
647 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
649 } */
652 static char *cropline(char *line)
654 char *start, *end;
656 if (strlen(line) == 0)
657 return line;
659 start = line;
660 end = &(line[strlen(line)]) - 1;
661 while (isspace(*line) && *line != 0)
662 line++;
663 while (isspace(*end) && end != line) {
664 *end = 0;
665 end--;
667 return line;
670 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
672 char buf[MAX_WORKSPACENAME_WIDTH + 1];
673 char *tmp;
675 if (workspace >= scr->workspace_count)
676 return;
678 /* trim white spaces */
679 tmp = cropline(name);
681 if (strlen(tmp) == 0) {
682 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
683 } else {
684 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
686 buf[MAX_WORKSPACENAME_WIDTH] = 0;
688 /* update workspace */
689 wfree(scr->workspaces[workspace]->name);
690 scr->workspaces[workspace]->name = wstrdup(buf);
692 if (scr->clip_ws_menu) {
693 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
694 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
695 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
696 wMenuRealize(scr->clip_ws_menu);
699 if (scr->workspace_menu) {
700 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
701 wfree(scr->workspace_menu->entries[workspace + 2]->text);
702 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
703 wMenuRealize(scr->workspace_menu);
707 if (scr->clip_icon)
708 wClipIconPaint(scr->clip_icon);
710 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
713 /* callback for when menu entry is edited */
714 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
716 char *tmp;
718 tmp = entry->text;
719 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
722 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
724 WMenu *wsmenu;
726 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
727 if (!wsmenu) {
728 wwarning(_("could not create Workspace menu"));
729 return NULL;
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 return wsmenu;
741 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
743 int i;
744 long ws;
745 char title[MAX_WORKSPACENAME_WIDTH + 1];
746 WMenuEntry *entry;
747 int tmp;
749 if (!menu)
750 return;
752 if (menu->entry_no < scr->workspace_count + 2) {
753 /* new workspace(s) added */
754 i = scr->workspace_count - (menu->entry_no - 2);
755 ws = menu->entry_no - 2;
756 while (i > 0) {
757 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
759 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
760 entry->flags.indicator = 1;
761 entry->flags.editable = 1;
763 i--;
764 ws++;
766 } else if (menu->entry_no > scr->workspace_count + 2) {
767 /* removed workspace(s) */
768 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
769 wMenuRemoveItem(menu, i);
772 wMenuRealize(menu);
774 for (i = 0; i < scr->workspace_count; i++) {
775 menu->entries[i + 2]->flags.indicator_on = 0;
777 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
779 /* don't let user destroy current workspace */
780 if (scr->current_workspace == scr->workspace_count - 1) {
781 wMenuSetEnabled(menu, 1, False);
782 } else {
783 wMenuSetEnabled(menu, 1, True);
786 tmp = menu->frame->top_width + 5;
787 /* if menu got unreachable, bring it to a visible place */
788 if (menu->frame_x < tmp - (int)menu->frame->core->width)
789 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
791 wMenuPaint(menu);
794 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
796 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
797 int i;
799 make_keys();
801 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
802 parr = WMCreatePLArray(NULL);
803 for (i = 0; i < scr->workspace_count; i++) {
804 pstr = WMCreatePLString(scr->workspaces[i]->name);
805 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
806 WMReleasePropList(pstr);
807 if (!wPreferences.flags.noclip) {
808 pstr = wClipSaveWorkspaceState(scr, i);
809 WMPutInPLDictionary(wks_state, dClip, pstr);
810 WMReleasePropList(pstr);
811 } else if (old_wks_state != NULL) {
812 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
813 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
814 WMPutInPLDictionary(wks_state, dClip, bar);
818 WMAddToPLArray(parr, wks_state);
819 WMReleasePropList(wks_state);
821 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
822 WMReleasePropList(parr);
825 void wWorkspaceRestoreState(WScreen * scr)
827 WMPropList *parr, *pstr, *wks_state, *clip_state;
828 int i, j, wscount;
830 make_keys();
832 if (scr->session_state == NULL)
833 return;
835 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
837 if (!parr)
838 return;
840 wscount = scr->workspace_count;
841 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
842 wks_state = WMGetFromPLArray(parr, i);
843 if (WMIsPLDictionary(wks_state))
844 pstr = WMGetFromPLDictionary(wks_state, dName);
845 else
846 pstr = wks_state;
847 if (i >= scr->workspace_count)
848 wWorkspaceNew(scr);
849 if (scr->workspace_menu) {
850 wfree(scr->workspace_menu->entries[i + 2]->text);
851 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
852 scr->workspace_menu->flags.realized = 0;
854 wfree(scr->workspaces[i]->name);
855 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
856 if (!wPreferences.flags.noclip) {
857 int added_omnipresent_icons = 0;
858 clip_state = WMGetFromPLDictionary(wks_state, dClip);
859 if (scr->workspaces[i]->clip)
860 wDockDestroy(scr->workspaces[i]->clip);
861 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
862 if (i > 0)
863 wDockHideIcons(scr->workspaces[i]->clip);
865 /* We set the global icons here, because scr->workspaces[i]->clip
866 * was not valid in wDockRestoreState().
867 * There we only set icon->omnipresent to know which icons we
868 * need to set here.
870 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
871 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
872 int k;
874 if (!aicon || !aicon->omnipresent)
875 continue;
876 aicon->omnipresent = 0;
877 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
878 continue;
879 if (i == 0)
880 continue;
882 /* Move this appicon from workspace i to workspace 0 */
883 scr->workspaces[i]->clip->icon_array[j] = NULL;
884 scr->workspaces[i]->clip->icon_count--;
886 added_omnipresent_icons++;
887 /* If there are too many omnipresent appicons, we are in trouble */
888 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
889 <= scr->workspaces[0]->clip->max_icons);
890 /* Find first free spot on workspace 0 */
891 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
892 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
893 break;
894 scr->workspaces[0]->clip->icon_array[k] = aicon;
895 aicon->dock = scr->workspaces[0]->clip;
897 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
900 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);