Remove wmlib
[wmaker-crm.git] / src / workspace.c
blobd9d9e64702b74606ad969e7cea2d03e6d907910c
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 "framewin.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "funcs.h"
44 #include "menu.h"
45 #include "application.h"
46 #include "dock.h"
47 #include "actions.h"
48 #include "workspace.h"
49 #include "appicon.h"
50 #include "wmspec.h"
51 #include "xinerama.h"
53 #define MAX_SHORTCUT_LENGTH 32
54 #define WORKSPACE_NAME_DISPLAY_PADDING 32
56 extern int ignore_wks_change;
57 extern WPreferences wPreferences;
58 extern XContext wWinContext;
59 extern XContext wVEdgeContext;
61 extern void ProcessPendingEvents();
63 static WMPropList *dWorkspaces = NULL;
64 static WMPropList *dClip, *dName;
66 static void make_keys()
68 if (dWorkspaces != NULL)
69 return;
71 dWorkspaces = WMCreatePLString("Workspaces");
72 dName = WMCreatePLString("Name");
73 dClip = WMCreatePLString("Clip");
76 void wWorkspaceMake(WScreen * scr, int count)
78 while (count > 0) {
79 wWorkspaceNew(scr);
80 count--;
84 int wWorkspaceNew(WScreen * scr)
86 WWorkspace *wspace, **list;
87 int i;
89 if (scr->workspace_count < MAX_WORKSPACES) {
90 scr->workspace_count++;
92 wspace = wmalloc(sizeof(WWorkspace));
93 wspace->name = NULL;
95 if (!wspace->name) {
96 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
97 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
100 if (!wPreferences.flags.noclip) {
101 wspace->clip = wDockCreate(scr, WM_CLIP);
102 } else
103 wspace->clip = NULL;
105 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
107 for (i = 0; i < scr->workspace_count - 1; i++) {
108 list[i] = scr->workspaces[i];
110 list[i] = wspace;
111 if (scr->workspaces)
112 wfree(scr->workspaces);
113 scr->workspaces = list;
115 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
116 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
117 wNETWMUpdateDesktop(scr);
118 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
119 XFlush(dpy);
121 return scr->workspace_count - 1;
124 return -1;
127 Bool wWorkspaceDelete(WScreen * scr, int workspace)
129 WWindow *tmp;
130 WWorkspace **list;
131 int i, j;
133 if (workspace <= 0)
134 return False;
136 /* verify if workspace is in use by some window */
137 tmp = scr->focused_window;
138 while (tmp) {
139 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
140 return False;
141 tmp = tmp->prev;
144 if (!wPreferences.flags.noclip) {
145 wDockDestroy(scr->workspaces[workspace]->clip);
146 scr->workspaces[workspace]->clip = NULL;
149 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
150 j = 0;
151 for (i = 0; i < scr->workspace_count; i++) {
152 if (i != workspace) {
153 list[j++] = scr->workspaces[i];
154 } else {
155 if (scr->workspaces[i]->name)
156 wfree(scr->workspaces[i]->name);
157 wfree(scr->workspaces[i]);
160 wfree(scr->workspaces);
161 scr->workspaces = list;
163 scr->workspace_count--;
165 /* update menu */
166 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
167 /* clip workspace menu */
168 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
170 /* update also window menu */
171 if (scr->workspace_submenu) {
172 WMenu *menu = scr->workspace_submenu;
174 i = menu->entry_no;
175 while (i > scr->workspace_count)
176 wMenuRemoveItem(menu, --i);
177 wMenuRealize(menu);
179 /* and clip menu */
180 if (scr->clip_submenu) {
181 WMenu *menu = scr->clip_submenu;
183 i = menu->entry_no;
184 while (i > scr->workspace_count)
185 wMenuRemoveItem(menu, --i);
186 wMenuRealize(menu);
188 wNETWMUpdateDesktop(scr);
189 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
191 if (scr->current_workspace >= scr->workspace_count)
192 wWorkspaceChange(scr, scr->workspace_count - 1);
194 return True;
197 typedef struct WorkspaceNameData {
198 int count;
199 RImage *back;
200 RImage *text;
201 time_t timeout;
202 } WorkspaceNameData;
204 static void hideWorkspaceName(void *data)
206 WScreen *scr = (WScreen *) data;
208 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
209 || time(NULL) > scr->workspace_name_data->timeout) {
210 XUnmapWindow(dpy, scr->workspace_name);
212 if (scr->workspace_name_data) {
213 RReleaseImage(scr->workspace_name_data->back);
214 RReleaseImage(scr->workspace_name_data->text);
215 wfree(scr->workspace_name_data);
217 scr->workspace_name_data = NULL;
219 scr->workspace_name_timer = NULL;
220 } else {
221 RImage *img = RCloneImage(scr->workspace_name_data->back);
222 Pixmap pix;
224 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
226 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
227 scr->workspace_name_data->count * 255 / 10);
229 RConvertImage(scr->rcontext, img, &pix);
231 RReleaseImage(img);
233 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
234 XClearWindow(dpy, scr->workspace_name);
235 XFreePixmap(dpy, pix);
236 XFlush(dpy);
238 scr->workspace_name_data->count--;
242 static void showWorkspaceName(WScreen * scr, int workspace)
244 WorkspaceNameData *data;
245 RXImage *ximg;
246 Pixmap text, mask;
247 int w, h;
248 int px, py;
249 char *name = scr->workspaces[workspace]->name;
250 int len = strlen(name);
251 int x, y;
252 #ifdef XINERAMA
253 int head;
254 WMRect rect;
255 int xx, yy;
256 #endif
258 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
259 return;
262 if (scr->workspace_name_timer) {
263 WMDeleteTimerHandler(scr->workspace_name_timer);
264 XUnmapWindow(dpy, scr->workspace_name);
265 XFlush(dpy);
267 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
269 if (scr->workspace_name_data) {
270 RReleaseImage(scr->workspace_name_data->back);
271 RReleaseImage(scr->workspace_name_data->text);
272 wfree(scr->workspace_name_data);
275 data = wmalloc(sizeof(WorkspaceNameData));
276 data->back = NULL;
278 w = WMWidthOfString(scr->workspace_name_font, name, len);
279 h = WMFontHeight(scr->workspace_name_font);
281 #ifdef XINERAMA
282 head = wGetHeadForPointerLocation(scr);
283 rect = wGetRectForHead(scr, head);
284 if (scr->xine_info.count) {
285 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
286 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
288 else {
289 xx = (scr->scr_width - (w + 4)) / 2;
290 yy = (scr->scr_height - (h + 4)) / 2;
292 #endif
294 switch (wPreferences.workspace_name_display_position) {
295 case WD_TOP:
296 #ifdef XINERAMA
297 px = xx;
298 #else
299 px = (scr->scr_width - (w + 4)) / 2;
300 #endif
301 py = WORKSPACE_NAME_DISPLAY_PADDING;
302 break;
303 case WD_BOTTOM:
304 #ifdef XINERAMA
305 px = xx;
306 #else
307 px = (scr->scr_width - (w + 4)) / 2;
308 #endif
309 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
310 break;
311 case WD_TOPLEFT:
312 px = WORKSPACE_NAME_DISPLAY_PADDING;
313 py = WORKSPACE_NAME_DISPLAY_PADDING;
314 break;
315 case WD_TOPRIGHT:
316 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
317 py = WORKSPACE_NAME_DISPLAY_PADDING;
318 break;
319 case WD_BOTTOMLEFT:
320 px = WORKSPACE_NAME_DISPLAY_PADDING;
321 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
322 break;
323 case WD_BOTTOMRIGHT:
324 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
325 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
326 break;
327 case WD_CENTER:
328 default:
329 #ifdef XINERAMA
330 px = xx;
331 py = yy;
332 #else
333 px = (scr->scr_width - (w + 4)) / 2;
334 py = (scr->scr_height - (h + 4)) / 2;
335 #endif
336 break;
338 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
339 XMoveWindow(dpy, scr->workspace_name, px, py);
341 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
342 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
344 /*XSetForeground(dpy, scr->mono_gc, 0);
345 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
347 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
349 for (x = 0; x <= 4; x++) {
350 for (y = 0; y <= 4; y++) {
351 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
355 XSetForeground(dpy, scr->mono_gc, 1);
356 XSetBackground(dpy, scr->mono_gc, 0);
358 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
360 /*XSetForeground(dpy, scr->mono_gc, 1); */
361 XSetBackground(dpy, scr->mono_gc, 1);
363 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
365 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
367 #ifdef SHAPE
368 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
369 #endif
370 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
371 XClearWindow(dpy, scr->workspace_name);
373 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
375 XFreePixmap(dpy, text);
376 XFreePixmap(dpy, mask);
378 if (!data->text) {
379 XMapRaised(dpy, scr->workspace_name);
380 XFlush(dpy);
382 goto erro;
385 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
387 if (!ximg || !ximg->image) {
388 goto erro;
391 XMapRaised(dpy, scr->workspace_name);
392 XFlush(dpy);
394 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
395 RDestroyXImage(scr->rcontext, ximg);
397 if (!data->back) {
398 goto erro;
401 data->count = 10;
403 /* set a timeout for the effect */
404 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
406 scr->workspace_name_data = data;
408 return;
410 erro:
411 if (scr->workspace_name_timer)
412 WMDeleteTimerHandler(scr->workspace_name_timer);
414 if (data->text)
415 RReleaseImage(data->text);
416 if (data->back)
417 RReleaseImage(data->back);
418 wfree(data);
420 scr->workspace_name_data = NULL;
422 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
423 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
426 void wWorkspaceChange(WScreen *scr, int workspace)
428 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
429 return;
431 if (workspace != scr->current_workspace)
432 wWorkspaceForceChange(scr, workspace);
435 void wWorkspaceRelativeChange(WScreen * scr, int amount)
437 int w;
439 /* While the deiconify animation is going on the window is
440 * still "flying" to its final position and we don't want to
441 * change workspace before the animation finishes, otherwise
442 * the window will land in the new workspace */
443 if (ignore_wks_change)
444 return;
446 w = scr->current_workspace + amount;
448 if (amount < 0) {
449 if (w >= 0) {
450 wWorkspaceChange(scr, w);
451 } else if (wPreferences.ws_cycle) {
452 wWorkspaceChange(scr, scr->workspace_count + w);
454 } else if (amount > 0) {
455 if (w < scr->workspace_count) {
456 wWorkspaceChange(scr, w);
457 } else if (wPreferences.ws_advance) {
458 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
459 } else if (wPreferences.ws_cycle) {
460 wWorkspaceChange(scr, w % scr->workspace_count);
465 void wWorkspaceForceChange(WScreen * scr, int workspace)
467 WWindow *tmp, *foc = NULL, *foc2 = NULL;
469 if (workspace >= MAX_WORKSPACES || workspace < 0)
470 return;
472 SendHelperMessage(scr, 'C', workspace + 1, NULL);
474 if (workspace > scr->workspace_count - 1) {
475 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
478 wClipUpdateForWorkspaceChange(scr, workspace);
480 scr->current_workspace = workspace;
482 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
484 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
486 if ((tmp = scr->focused_window) != NULL) {
487 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
488 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
489 foc = tmp;
492 /* foc2 = tmp; will fix annoyance with gnome panel
493 * but will create annoyance for every other application
495 while (tmp) {
496 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
497 /* unmap windows not on this workspace */
498 if ((tmp->flags.mapped || tmp->flags.shaded) &&
499 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
500 wWindowUnmap(tmp);
502 /* also unmap miniwindows not on this workspace */
503 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
504 tmp->icon && !IS_OMNIPRESENT(tmp)) {
505 XUnmapWindow(dpy, tmp->icon->core->window);
506 tmp->icon->mapped = 0;
508 /* update current workspace of omnipresent windows */
509 if (IS_OMNIPRESENT(tmp)) {
510 WApplication *wapp = wApplicationOf(tmp->main_window);
512 tmp->frame->workspace = workspace;
514 if (wapp) {
515 wapp->last_workspace = workspace;
517 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
518 foc2 = tmp;
521 } else {
522 /* change selected windows' workspace */
523 if (tmp->flags.selected) {
524 wWindowChangeWorkspace(tmp, workspace);
525 if (!tmp->flags.miniaturized && !foc) {
526 foc = tmp;
528 } else {
529 if (!tmp->flags.hidden) {
530 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
531 /* remap windows that are on this workspace */
532 wWindowMap(tmp);
533 if (!foc && !WFLAGP(tmp, no_focusable)) {
534 foc = tmp;
537 /* Also map miniwindow if not omnipresent */
538 if (!wPreferences.sticky_icons &&
539 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
540 tmp->icon->mapped = 1;
541 XMapWindow(dpy, tmp->icon->core->window);
546 tmp = tmp->prev;
549 /* Gobble up events unleashed by our mapping & unmapping.
550 * These may trigger various grab-initiated focus &
551 * crossing events. However, we don't care about them,
552 * and ignore their focus implications altogether to avoid
553 * flicker.
555 scr->flags.ignore_focus_events = 1;
556 ProcessPendingEvents();
557 scr->flags.ignore_focus_events = 0;
559 if (!foc)
560 foc = foc2;
562 if (scr->focused_window->flags.mapped && !foc) {
563 foc = scr->focused_window;
565 if (wPreferences.focus_mode == WKF_CLICK) {
566 wSetFocusTo(scr, foc);
567 } else {
568 unsigned int mask;
569 int foo;
570 Window bar, win;
571 WWindow *tmp;
573 tmp = NULL;
574 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
575 tmp = wWindowFor(win);
578 /* If there's a window under the pointer, focus it.
579 * (we ate all other focus events above, so it's
580 * certainly not focused). Otherwise focus last
581 * focused, or the root (depending on sloppiness)
583 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
584 wSetFocusTo(scr, foc);
585 } else {
586 wSetFocusTo(scr, tmp);
591 /* We need to always arrange icons when changing workspace, even if
592 * no autoarrange icons, because else the icons in different workspaces
593 * can be superposed.
594 * This can be avoided if appicons are also workspace specific.
596 if (!wPreferences.sticky_icons)
597 wArrangeIcons(scr, False);
599 if (scr->dock)
600 wAppIconPaint(scr->dock->icon_array[0]);
602 if (scr->clip_icon) {
603 if (scr->workspaces[workspace]->clip->auto_collapse ||
604 scr->workspaces[workspace]->clip->auto_raise_lower) {
605 /* to handle enter notify. This will also */
606 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
607 XMapWindow(dpy, scr->clip_icon->icon->core->window);
608 } else {
609 wClipIconPaint(scr->clip_icon);
612 wScreenUpdateUsableArea(scr);
613 wNETWMUpdateDesktop(scr);
614 showWorkspaceName(scr, workspace);
616 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
618 /* XSync(dpy, False); */
621 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
623 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
626 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
628 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
631 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
633 int ws;
635 ws = wWorkspaceNew(menu->frame->screen_ptr);
636 /* autochange workspace */
637 if (ws >= 0)
638 wWorkspaceChange(menu->frame->screen_ptr, ws);
641 if (ws<9) {
642 int kcode;
643 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
644 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
645 entry->rtext =
646 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
648 } */
651 static char *cropline(char *line)
653 char *start, *end;
655 if (strlen(line) == 0)
656 return line;
658 start = line;
659 end = &(line[strlen(line)]) - 1;
660 while (isspace(*line) && *line != 0)
661 line++;
662 while (isspace(*end) && end != line) {
663 *end = 0;
664 end--;
666 return line;
669 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
671 char buf[MAX_WORKSPACENAME_WIDTH + 1];
672 char *tmp;
674 if (workspace >= scr->workspace_count)
675 return;
677 /* trim white spaces */
678 tmp = cropline(name);
680 if (strlen(tmp) == 0) {
681 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
682 } else {
683 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
685 buf[MAX_WORKSPACENAME_WIDTH] = 0;
687 /* update workspace */
688 wfree(scr->workspaces[workspace]->name);
689 scr->workspaces[workspace]->name = wstrdup(buf);
691 if (scr->clip_ws_menu) {
692 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
693 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
694 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
695 wMenuRealize(scr->clip_ws_menu);
698 if (scr->workspace_menu) {
699 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
700 wfree(scr->workspace_menu->entries[workspace + 2]->text);
701 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
702 wMenuRealize(scr->workspace_menu);
706 if (scr->clip_icon)
707 wClipIconPaint(scr->clip_icon);
709 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
712 /* callback for when menu entry is edited */
713 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
715 char *tmp;
717 tmp = entry->text;
718 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
721 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
723 WMenu *wsmenu;
725 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
726 if (!wsmenu) {
727 wwarning(_("could not create Workspace menu"));
728 return NULL;
731 /* callback to be called when an entry is edited */
732 wsmenu->on_edit = onMenuEntryEdited;
734 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
735 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
737 return wsmenu;
740 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
742 int i;
743 long ws;
744 char title[MAX_WORKSPACENAME_WIDTH + 1];
745 WMenuEntry *entry;
746 int tmp;
748 if (!menu)
749 return;
751 if (menu->entry_no < scr->workspace_count + 2) {
752 /* new workspace(s) added */
753 i = scr->workspace_count - (menu->entry_no - 2);
754 ws = menu->entry_no - 2;
755 while (i > 0) {
756 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
758 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
759 entry->flags.indicator = 1;
760 entry->flags.editable = 1;
762 i--;
763 ws++;
765 } else if (menu->entry_no > scr->workspace_count + 2) {
766 /* removed workspace(s) */
767 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
768 wMenuRemoveItem(menu, i);
771 wMenuRealize(menu);
773 for (i = 0; i < scr->workspace_count; i++) {
774 menu->entries[i + 2]->flags.indicator_on = 0;
776 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
778 /* don't let user destroy current workspace */
779 if (scr->current_workspace == scr->workspace_count - 1) {
780 wMenuSetEnabled(menu, 1, False);
781 } else {
782 wMenuSetEnabled(menu, 1, True);
785 tmp = menu->frame->top_width + 5;
786 /* if menu got unreachable, bring it to a visible place */
787 if (menu->frame_x < tmp - (int)menu->frame->core->width)
788 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
790 wMenuPaint(menu);
793 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
795 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
796 int i;
798 make_keys();
800 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
801 parr = WMCreatePLArray(NULL);
802 for (i = 0; i < scr->workspace_count; i++) {
803 pstr = WMCreatePLString(scr->workspaces[i]->name);
804 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
805 WMReleasePropList(pstr);
806 if (!wPreferences.flags.noclip) {
807 pstr = wClipSaveWorkspaceState(scr, i);
808 WMPutInPLDictionary(wks_state, dClip, pstr);
809 WMReleasePropList(pstr);
810 } else if (old_wks_state != NULL) {
811 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
812 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
813 WMPutInPLDictionary(wks_state, dClip, bar);
817 WMAddToPLArray(parr, wks_state);
818 WMReleasePropList(wks_state);
820 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
821 WMReleasePropList(parr);
824 void wWorkspaceRestoreState(WScreen * scr)
826 WMPropList *parr, *pstr, *wks_state, *clip_state;
827 int i, j, wscount;
829 make_keys();
831 if (scr->session_state == NULL)
832 return;
834 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
836 if (!parr)
837 return;
839 wscount = scr->workspace_count;
840 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
841 wks_state = WMGetFromPLArray(parr, i);
842 if (WMIsPLDictionary(wks_state))
843 pstr = WMGetFromPLDictionary(wks_state, dName);
844 else
845 pstr = wks_state;
846 if (i >= scr->workspace_count)
847 wWorkspaceNew(scr);
848 if (scr->workspace_menu) {
849 wfree(scr->workspace_menu->entries[i + 2]->text);
850 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
851 scr->workspace_menu->flags.realized = 0;
853 wfree(scr->workspaces[i]->name);
854 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
855 if (!wPreferences.flags.noclip) {
856 int added_omnipresent_icons = 0;
857 clip_state = WMGetFromPLDictionary(wks_state, dClip);
858 if (scr->workspaces[i]->clip)
859 wDockDestroy(scr->workspaces[i]->clip);
860 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
861 if (i > 0)
862 wDockHideIcons(scr->workspaces[i]->clip);
864 /* We set the global icons here, because scr->workspaces[i]->clip
865 * was not valid in wDockRestoreState().
866 * There we only set icon->omnipresent to know which icons we
867 * need to set here.
869 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
870 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
871 int k;
873 if (!aicon || !aicon->omnipresent)
874 continue;
875 aicon->omnipresent = 0;
876 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
877 continue;
878 if (i == 0)
879 continue;
881 /* Move this appicon from workspace i to workspace 0 */
882 scr->workspaces[i]->clip->icon_array[j] = NULL;
883 scr->workspaces[i]->clip->icon_count--;
885 added_omnipresent_icons++;
886 /* If there are too many omnipresent appicons, we are in trouble */
887 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
888 <= scr->workspaces[0]->clip->max_icons);
889 /* Find first free spot on workspace 0 */
890 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
891 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
892 break;
893 scr->workspaces[0]->clip->icon_array[k] = aicon;
894 aicon->dock = scr->workspaces[0]->clip;
896 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
899 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);