Escape minus sign ("-") in manpages.
[wmaker-crm.git] / src / workspace.c
blob474da1ab40e43ec159d86249ffcf96ac589b7933
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 wVEdgeContext;
59 extern void ProcessPendingEvents();
61 static WMPropList *dWorkspaces = NULL;
62 static WMPropList *dClip, *dName;
64 static void make_keys(void)
66 if (dWorkspaces != NULL)
67 return;
69 dWorkspaces = WMCreatePLString("Workspaces");
70 dName = WMCreatePLString("Name");
71 dClip = WMCreatePLString("Clip");
74 void wWorkspaceMake(WScreen * scr, int count)
76 while (count > 0) {
77 wWorkspaceNew(scr);
78 count--;
82 int wWorkspaceNew(WScreen * scr)
84 WWorkspace *wspace, **list;
85 int i;
87 if (scr->workspace_count < MAX_WORKSPACES) {
88 scr->workspace_count++;
90 wspace = wmalloc(sizeof(WWorkspace));
91 wspace->name = NULL;
93 if (!wspace->name) {
94 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
95 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
98 if (!wPreferences.flags.noclip) {
99 wspace->clip = wDockCreate(scr, WM_CLIP);
100 } else
101 wspace->clip = NULL;
103 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
105 for (i = 0; i < scr->workspace_count - 1; i++) {
106 list[i] = scr->workspaces[i];
108 list[i] = wspace;
109 if (scr->workspaces)
110 wfree(scr->workspaces);
111 scr->workspaces = list;
113 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
114 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
115 wNETWMUpdateDesktop(scr);
116 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
117 XFlush(dpy);
119 return scr->workspace_count - 1;
122 return -1;
125 Bool wWorkspaceDelete(WScreen * scr, int workspace)
127 WWindow *tmp;
128 WWorkspace **list;
129 int i, j;
131 if (workspace <= 0)
132 return False;
134 /* verify if workspace is in use by some window */
135 tmp = scr->focused_window;
136 while (tmp) {
137 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
138 return False;
139 tmp = tmp->prev;
142 if (!wPreferences.flags.noclip) {
143 wDockDestroy(scr->workspaces[workspace]->clip);
144 scr->workspaces[workspace]->clip = NULL;
147 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
148 j = 0;
149 for (i = 0; i < scr->workspace_count; i++) {
150 if (i != workspace) {
151 list[j++] = scr->workspaces[i];
152 } else {
153 if (scr->workspaces[i]->name)
154 wfree(scr->workspaces[i]->name);
155 wfree(scr->workspaces[i]);
158 wfree(scr->workspaces);
159 scr->workspaces = list;
161 scr->workspace_count--;
163 /* update menu */
164 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
165 /* clip workspace menu */
166 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
168 /* update also window menu */
169 if (scr->workspace_submenu) {
170 WMenu *menu = scr->workspace_submenu;
172 i = menu->entry_no;
173 while (i > scr->workspace_count)
174 wMenuRemoveItem(menu, --i);
175 wMenuRealize(menu);
177 /* and clip menu */
178 if (scr->clip_submenu) {
179 WMenu *menu = scr->clip_submenu;
181 i = menu->entry_no;
182 while (i > scr->workspace_count)
183 wMenuRemoveItem(menu, --i);
184 wMenuRealize(menu);
186 wNETWMUpdateDesktop(scr);
187 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
189 if (scr->current_workspace >= scr->workspace_count)
190 wWorkspaceChange(scr, scr->workspace_count - 1);
192 return True;
195 typedef struct WorkspaceNameData {
196 int count;
197 RImage *back;
198 RImage *text;
199 time_t timeout;
200 } WorkspaceNameData;
202 static void hideWorkspaceName(void *data)
204 WScreen *scr = (WScreen *) data;
206 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
207 || time(NULL) > scr->workspace_name_data->timeout) {
208 XUnmapWindow(dpy, scr->workspace_name);
210 if (scr->workspace_name_data) {
211 RReleaseImage(scr->workspace_name_data->back);
212 RReleaseImage(scr->workspace_name_data->text);
213 wfree(scr->workspace_name_data);
215 scr->workspace_name_data = NULL;
217 scr->workspace_name_timer = NULL;
218 } else {
219 RImage *img = RCloneImage(scr->workspace_name_data->back);
220 Pixmap pix;
222 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
224 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
225 scr->workspace_name_data->count * 255 / 10);
227 RConvertImage(scr->rcontext, img, &pix);
229 RReleaseImage(img);
231 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
232 XClearWindow(dpy, scr->workspace_name);
233 XFreePixmap(dpy, pix);
234 XFlush(dpy);
236 scr->workspace_name_data->count--;
240 static void showWorkspaceName(WScreen * scr, int workspace)
242 WorkspaceNameData *data;
243 RXImage *ximg;
244 Pixmap text, mask;
245 int w, h;
246 int px, py;
247 char *name = scr->workspaces[workspace]->name;
248 int len = strlen(name);
249 int x, y;
250 #ifdef XINERAMA
251 int head;
252 WMRect rect;
253 int xx, yy;
254 #endif
256 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
257 return;
260 if (scr->workspace_name_timer) {
261 WMDeleteTimerHandler(scr->workspace_name_timer);
262 XUnmapWindow(dpy, scr->workspace_name);
263 XFlush(dpy);
265 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
267 if (scr->workspace_name_data) {
268 RReleaseImage(scr->workspace_name_data->back);
269 RReleaseImage(scr->workspace_name_data->text);
270 wfree(scr->workspace_name_data);
273 data = wmalloc(sizeof(WorkspaceNameData));
274 data->back = NULL;
276 w = WMWidthOfString(scr->workspace_name_font, name, len);
277 h = WMFontHeight(scr->workspace_name_font);
279 #ifdef XINERAMA
280 head = wGetHeadForPointerLocation(scr);
281 rect = wGetRectForHead(scr, head);
282 if (scr->xine_info.count) {
283 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
284 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
286 else {
287 xx = (scr->scr_width - (w + 4)) / 2;
288 yy = (scr->scr_height - (h + 4)) / 2;
290 #endif
292 switch (wPreferences.workspace_name_display_position) {
293 case WD_TOP:
294 #ifdef XINERAMA
295 px = xx;
296 #else
297 px = (scr->scr_width - (w + 4)) / 2;
298 #endif
299 py = WORKSPACE_NAME_DISPLAY_PADDING;
300 break;
301 case WD_BOTTOM:
302 #ifdef XINERAMA
303 px = xx;
304 #else
305 px = (scr->scr_width - (w + 4)) / 2;
306 #endif
307 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
308 break;
309 case WD_TOPLEFT:
310 px = WORKSPACE_NAME_DISPLAY_PADDING;
311 py = WORKSPACE_NAME_DISPLAY_PADDING;
312 break;
313 case WD_TOPRIGHT:
314 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
315 py = WORKSPACE_NAME_DISPLAY_PADDING;
316 break;
317 case WD_BOTTOMLEFT:
318 px = WORKSPACE_NAME_DISPLAY_PADDING;
319 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
320 break;
321 case WD_BOTTOMRIGHT:
322 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
323 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
324 break;
325 case WD_CENTER:
326 default:
327 #ifdef XINERAMA
328 px = xx;
329 py = yy;
330 #else
331 px = (scr->scr_width - (w + 4)) / 2;
332 py = (scr->scr_height - (h + 4)) / 2;
333 #endif
334 break;
336 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
337 XMoveWindow(dpy, scr->workspace_name, px, py);
339 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
340 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
342 /*XSetForeground(dpy, scr->mono_gc, 0);
343 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
345 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
347 for (x = 0; x <= 4; x++) {
348 for (y = 0; y <= 4; y++) {
349 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
353 XSetForeground(dpy, scr->mono_gc, 1);
354 XSetBackground(dpy, scr->mono_gc, 0);
356 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
358 /*XSetForeground(dpy, scr->mono_gc, 1); */
359 XSetBackground(dpy, scr->mono_gc, 1);
361 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
363 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
365 #ifdef SHAPE
366 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
367 #endif
368 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
369 XClearWindow(dpy, scr->workspace_name);
371 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
373 XFreePixmap(dpy, text);
374 XFreePixmap(dpy, mask);
376 if (!data->text) {
377 XMapRaised(dpy, scr->workspace_name);
378 XFlush(dpy);
380 goto erro;
383 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
385 if (!ximg || !ximg->image) {
386 goto erro;
389 XMapRaised(dpy, scr->workspace_name);
390 XFlush(dpy);
392 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
393 RDestroyXImage(scr->rcontext, ximg);
395 if (!data->back) {
396 goto erro;
399 data->count = 10;
401 /* set a timeout for the effect */
402 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
404 scr->workspace_name_data = data;
406 return;
408 erro:
409 if (scr->workspace_name_timer)
410 WMDeleteTimerHandler(scr->workspace_name_timer);
412 if (data->text)
413 RReleaseImage(data->text);
414 if (data->back)
415 RReleaseImage(data->back);
416 wfree(data);
418 scr->workspace_name_data = NULL;
420 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
421 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
424 void wWorkspaceChange(WScreen *scr, int workspace)
426 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
427 return;
429 if (workspace != scr->current_workspace)
430 wWorkspaceForceChange(scr, workspace);
433 void wWorkspaceRelativeChange(WScreen * scr, int amount)
435 int w;
437 /* While the deiconify animation is going on the window is
438 * still "flying" to its final position and we don't want to
439 * change workspace before the animation finishes, otherwise
440 * the window will land in the new workspace */
441 if (ignore_wks_change)
442 return;
444 w = scr->current_workspace + amount;
446 if (amount < 0) {
447 if (w >= 0) {
448 wWorkspaceChange(scr, w);
449 } else if (wPreferences.ws_cycle) {
450 wWorkspaceChange(scr, scr->workspace_count + w);
452 } else if (amount > 0) {
453 if (w < scr->workspace_count) {
454 wWorkspaceChange(scr, w);
455 } else if (wPreferences.ws_advance) {
456 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
457 } else if (wPreferences.ws_cycle) {
458 wWorkspaceChange(scr, w % scr->workspace_count);
463 void wWorkspaceForceChange(WScreen * scr, int workspace)
465 WWindow *tmp, *foc = NULL, *foc2 = NULL;
467 if (workspace >= MAX_WORKSPACES || workspace < 0)
468 return;
470 SendHelperMessage(scr, 'C', workspace + 1, NULL);
472 if (workspace > scr->workspace_count - 1) {
473 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
476 wClipUpdateForWorkspaceChange(scr, workspace);
478 scr->current_workspace = workspace;
480 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
482 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
484 if ((tmp = scr->focused_window) != NULL) {
485 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
486 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
487 foc = tmp;
490 /* foc2 = tmp; will fix annoyance with gnome panel
491 * but will create annoyance for every other application
493 while (tmp) {
494 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
495 /* unmap windows not on this workspace */
496 if ((tmp->flags.mapped || tmp->flags.shaded) &&
497 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
498 wWindowUnmap(tmp);
500 /* also unmap miniwindows not on this workspace */
501 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
502 tmp->icon && !IS_OMNIPRESENT(tmp)) {
503 XUnmapWindow(dpy, tmp->icon->core->window);
504 tmp->icon->mapped = 0;
506 /* update current workspace of omnipresent windows */
507 if (IS_OMNIPRESENT(tmp)) {
508 WApplication *wapp = wApplicationOf(tmp->main_window);
510 tmp->frame->workspace = workspace;
512 if (wapp) {
513 wapp->last_workspace = workspace;
515 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
516 foc2 = tmp;
519 } else {
520 /* change selected windows' workspace */
521 if (tmp->flags.selected) {
522 wWindowChangeWorkspace(tmp, workspace);
523 if (!tmp->flags.miniaturized && !foc) {
524 foc = tmp;
526 } else {
527 if (!tmp->flags.hidden) {
528 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
529 /* remap windows that are on this workspace */
530 wWindowMap(tmp);
531 if (!foc && !WFLAGP(tmp, no_focusable)) {
532 foc = tmp;
535 /* Also map miniwindow if not omnipresent */
536 if (!wPreferences.sticky_icons &&
537 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
538 tmp->icon->mapped = 1;
539 XMapWindow(dpy, tmp->icon->core->window);
544 tmp = tmp->prev;
547 /* Gobble up events unleashed by our mapping & unmapping.
548 * These may trigger various grab-initiated focus &
549 * crossing events. However, we don't care about them,
550 * and ignore their focus implications altogether to avoid
551 * flicker.
553 scr->flags.ignore_focus_events = 1;
554 ProcessPendingEvents();
555 scr->flags.ignore_focus_events = 0;
557 if (!foc)
558 foc = foc2;
560 if (scr->focused_window->flags.mapped && !foc) {
561 foc = scr->focused_window;
563 if (wPreferences.focus_mode == WKF_CLICK) {
564 wSetFocusTo(scr, foc);
565 } else {
566 unsigned int mask;
567 int foo;
568 Window bar, win;
569 WWindow *tmp;
571 tmp = NULL;
572 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
573 tmp = wWindowFor(win);
576 /* If there's a window under the pointer, focus it.
577 * (we ate all other focus events above, so it's
578 * certainly not focused). Otherwise focus last
579 * focused, or the root (depending on sloppiness)
581 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
582 wSetFocusTo(scr, foc);
583 } else {
584 wSetFocusTo(scr, tmp);
589 /* We need to always arrange icons when changing workspace, even if
590 * no autoarrange icons, because else the icons in different workspaces
591 * can be superposed.
592 * This can be avoided if appicons are also workspace specific.
594 if (!wPreferences.sticky_icons)
595 wArrangeIcons(scr, False);
597 if (scr->dock)
598 wAppIconPaint(scr->dock->icon_array[0]);
600 if (scr->clip_icon) {
601 if (scr->workspaces[workspace]->clip->auto_collapse ||
602 scr->workspaces[workspace]->clip->auto_raise_lower) {
603 /* to handle enter notify. This will also */
604 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
605 XMapWindow(dpy, scr->clip_icon->icon->core->window);
606 } else {
607 wClipIconPaint(scr->clip_icon);
610 wScreenUpdateUsableArea(scr);
611 wNETWMUpdateDesktop(scr);
612 showWorkspaceName(scr, workspace);
614 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
616 /* XSync(dpy, False); */
619 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
621 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
624 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
626 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
629 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
631 int ws;
633 ws = wWorkspaceNew(menu->frame->screen_ptr);
634 /* autochange workspace */
635 if (ws >= 0)
636 wWorkspaceChange(menu->frame->screen_ptr, ws);
639 if (ws<9) {
640 int kcode;
641 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
642 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
643 entry->rtext =
644 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
646 } */
649 static char *cropline(char *line)
651 char *start, *end;
653 if (strlen(line) == 0)
654 return line;
656 start = line;
657 end = &(line[strlen(line)]) - 1;
658 while (isspace(*line) && *line != 0)
659 line++;
660 while (isspace(*end) && end != line) {
661 *end = 0;
662 end--;
664 return line;
667 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
669 char buf[MAX_WORKSPACENAME_WIDTH + 1];
670 char *tmp;
672 if (workspace >= scr->workspace_count)
673 return;
675 /* trim white spaces */
676 tmp = cropline(name);
678 if (strlen(tmp) == 0) {
679 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
680 } else {
681 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
683 buf[MAX_WORKSPACENAME_WIDTH] = 0;
685 /* update workspace */
686 wfree(scr->workspaces[workspace]->name);
687 scr->workspaces[workspace]->name = wstrdup(buf);
689 if (scr->clip_ws_menu) {
690 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
691 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
692 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
693 wMenuRealize(scr->clip_ws_menu);
696 if (scr->workspace_menu) {
697 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
698 wfree(scr->workspace_menu->entries[workspace + 2]->text);
699 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
700 wMenuRealize(scr->workspace_menu);
704 if (scr->clip_icon)
705 wClipIconPaint(scr->clip_icon);
707 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
710 /* callback for when menu entry is edited */
711 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
713 char *tmp;
715 tmp = entry->text;
716 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
719 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
721 WMenu *wsmenu;
723 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
724 if (!wsmenu) {
725 wwarning(_("could not create Workspace menu"));
726 return NULL;
729 /* callback to be called when an entry is edited */
730 wsmenu->on_edit = onMenuEntryEdited;
732 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
733 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
735 return wsmenu;
738 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
740 int i;
741 long ws;
742 char title[MAX_WORKSPACENAME_WIDTH + 1];
743 WMenuEntry *entry;
744 int tmp;
746 if (!menu)
747 return;
749 if (menu->entry_no < scr->workspace_count + 2) {
750 /* new workspace(s) added */
751 i = scr->workspace_count - (menu->entry_no - 2);
752 ws = menu->entry_no - 2;
753 while (i > 0) {
754 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
756 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
757 entry->flags.indicator = 1;
758 entry->flags.editable = 1;
760 i--;
761 ws++;
763 } else if (menu->entry_no > scr->workspace_count + 2) {
764 /* removed workspace(s) */
765 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
766 wMenuRemoveItem(menu, i);
769 wMenuRealize(menu);
771 for (i = 0; i < scr->workspace_count; i++) {
772 menu->entries[i + 2]->flags.indicator_on = 0;
774 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
776 /* don't let user destroy current workspace */
777 if (scr->current_workspace == scr->workspace_count - 1) {
778 wMenuSetEnabled(menu, 1, False);
779 } else {
780 wMenuSetEnabled(menu, 1, True);
783 tmp = menu->frame->top_width + 5;
784 /* if menu got unreachable, bring it to a visible place */
785 if (menu->frame_x < tmp - (int)menu->frame->core->width)
786 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
788 wMenuPaint(menu);
791 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
793 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
794 int i;
796 make_keys();
798 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
799 parr = WMCreatePLArray(NULL);
800 for (i = 0; i < scr->workspace_count; i++) {
801 pstr = WMCreatePLString(scr->workspaces[i]->name);
802 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
803 WMReleasePropList(pstr);
804 if (!wPreferences.flags.noclip) {
805 pstr = wClipSaveWorkspaceState(scr, i);
806 WMPutInPLDictionary(wks_state, dClip, pstr);
807 WMReleasePropList(pstr);
808 } else if (old_wks_state != NULL) {
809 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
810 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
811 WMPutInPLDictionary(wks_state, dClip, bar);
815 WMAddToPLArray(parr, wks_state);
816 WMReleasePropList(wks_state);
818 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
819 WMReleasePropList(parr);
822 void wWorkspaceRestoreState(WScreen * scr)
824 WMPropList *parr, *pstr, *wks_state, *clip_state;
825 int i, j, wscount;
827 make_keys();
829 if (scr->session_state == NULL)
830 return;
832 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
834 if (!parr)
835 return;
837 wscount = scr->workspace_count;
838 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
839 wks_state = WMGetFromPLArray(parr, i);
840 if (WMIsPLDictionary(wks_state))
841 pstr = WMGetFromPLDictionary(wks_state, dName);
842 else
843 pstr = wks_state;
844 if (i >= scr->workspace_count)
845 wWorkspaceNew(scr);
846 if (scr->workspace_menu) {
847 wfree(scr->workspace_menu->entries[i + 2]->text);
848 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
849 scr->workspace_menu->flags.realized = 0;
851 wfree(scr->workspaces[i]->name);
852 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
853 if (!wPreferences.flags.noclip) {
854 int added_omnipresent_icons = 0;
855 clip_state = WMGetFromPLDictionary(wks_state, dClip);
856 if (scr->workspaces[i]->clip)
857 wDockDestroy(scr->workspaces[i]->clip);
858 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
859 if (i > 0)
860 wDockHideIcons(scr->workspaces[i]->clip);
862 /* We set the global icons here, because scr->workspaces[i]->clip
863 * was not valid in wDockRestoreState().
864 * There we only set icon->omnipresent to know which icons we
865 * need to set here.
867 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
868 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
869 int k;
871 if (!aicon || !aicon->omnipresent)
872 continue;
873 aicon->omnipresent = 0;
874 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
875 continue;
876 if (i == 0)
877 continue;
879 /* Move this appicon from workspace i to workspace 0 */
880 scr->workspaces[i]->clip->icon_array[j] = NULL;
881 scr->workspaces[i]->clip->icon_count--;
883 added_omnipresent_icons++;
884 /* If there are too many omnipresent appicons, we are in trouble */
885 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
886 <= scr->workspaces[0]->clip->max_icons);
887 /* Find first free spot on workspace 0 */
888 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
889 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
890 break;
891 scr->workspaces[0]->clip->icon_array[k] = aicon;
892 aicon->dock = scr->workspaces[0]->clip;
894 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
897 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);