wmaker: fix incorrect type for variable in the global preference structure
[wmaker-crm.git] / src / workspace.c
blob69db16cfa6e444ff790a0dd361ae3252a5865017
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 USE_XSHAPE
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 "misc.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"
51 #include "event.h"
52 #include "wsmap.h"
54 #define MC_NEW 0
55 #define MC_DESTROY_LAST 1
56 #define MC_LAST_USED 2
57 /* index of the first workspace menu entry */
58 #define MC_WORKSPACE1 3
60 #define WORKSPACE_NAME_DISPLAY_PADDING 32
62 static WMPropList *dWorkspaces = NULL;
63 static WMPropList *dClip, *dName;
65 static void make_keys(void)
67 if (dWorkspaces != NULL)
68 return;
70 dWorkspaces = WMCreatePLString("Workspaces");
71 dName = WMCreatePLString("Name");
72 dClip = WMCreatePLString("Clip");
75 void wWorkspaceMake(WScreen * scr, int count)
77 while (count > 0) {
78 wWorkspaceNew(scr);
79 count--;
83 int wWorkspaceNew(WScreen *scr)
85 WWorkspace *wspace, **list;
86 int i;
88 if (scr->workspace_count < MAX_WORKSPACES) {
89 scr->workspace_count++;
91 wspace = wmalloc(sizeof(WWorkspace));
92 wspace->name = NULL;
93 wspace->clip = NULL;
95 if (!wspace->name) {
96 static const char *new_name = NULL;
97 static size_t name_length;
99 if (new_name == NULL) {
100 new_name = _("Workspace %i");
101 name_length = strlen(new_name) + 8;
103 wspace->name = wmalloc(name_length);
104 snprintf(wspace->name, name_length, new_name, scr->workspace_count);
107 if (!wPreferences.flags.noclip)
108 wspace->clip = wDockCreate(scr, WM_CLIP, NULL);
110 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
112 for (i = 0; i < scr->workspace_count - 1; i++)
113 list[i] = scr->workspaces[i];
115 list[i] = wspace;
116 if (scr->workspaces)
117 wfree(scr->workspaces);
119 scr->workspaces = list;
121 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
122 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
123 wNETWMUpdateDesktop(scr);
124 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
125 XFlush(dpy);
127 return scr->workspace_count - 1;
130 return -1;
133 Bool wWorkspaceDelete(WScreen * scr, int workspace)
135 WWindow *tmp;
136 WWorkspace **list;
137 int i, j;
139 if (workspace <= 0)
140 return False;
142 /* verify if workspace is in use by some window */
143 tmp = scr->focused_window;
144 while (tmp) {
145 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
146 return False;
147 tmp = tmp->prev;
150 if (!wPreferences.flags.noclip) {
151 wDockDestroy(scr->workspaces[workspace]->clip);
152 scr->workspaces[workspace]->clip = NULL;
155 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
156 j = 0;
157 for (i = 0; i < scr->workspace_count; i++) {
158 if (i != workspace) {
159 list[j++] = scr->workspaces[i];
160 } else {
161 if (scr->workspaces[i]->name)
162 wfree(scr->workspaces[i]->name);
163 if (scr->workspaces[i]->map)
164 RReleaseImage(scr->workspaces[i]->map);
165 wfree(scr->workspaces[i]);
168 wfree(scr->workspaces);
169 scr->workspaces = list;
171 scr->workspace_count--;
173 /* update menu */
174 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
175 /* clip workspace menu */
176 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
178 /* update also window menu */
179 if (scr->workspace_submenu) {
180 WMenu *menu = scr->workspace_submenu;
182 i = menu->entry_no;
183 while (i > scr->workspace_count)
184 wMenuRemoveItem(menu, --i);
185 wMenuRealize(menu);
187 /* and clip menu */
188 if (scr->clip_submenu) {
189 WMenu *menu = scr->clip_submenu;
191 i = menu->entry_no;
192 while (i > scr->workspace_count)
193 wMenuRemoveItem(menu, --i);
194 wMenuRealize(menu);
196 wNETWMUpdateDesktop(scr);
197 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
199 if (scr->current_workspace >= scr->workspace_count)
200 wWorkspaceChange(scr, scr->workspace_count - 1);
201 if (scr->last_workspace >= scr->workspace_count)
202 scr->last_workspace = 0;
204 return True;
207 typedef struct WorkspaceNameData {
208 int count;
209 RImage *back;
210 RImage *text;
211 time_t timeout;
212 } WorkspaceNameData;
214 static void hideWorkspaceName(void *data)
216 WScreen *scr = (WScreen *) data;
218 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
219 || time(NULL) > scr->workspace_name_data->timeout) {
220 XUnmapWindow(dpy, scr->workspace_name);
222 if (scr->workspace_name_data) {
223 RReleaseImage(scr->workspace_name_data->back);
224 RReleaseImage(scr->workspace_name_data->text);
225 wfree(scr->workspace_name_data);
227 scr->workspace_name_data = NULL;
229 scr->workspace_name_timer = NULL;
230 } else {
231 RImage *img = RCloneImage(scr->workspace_name_data->back);
232 Pixmap pix;
234 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
236 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
237 scr->workspace_name_data->count * 255 / 10);
239 RConvertImage(scr->rcontext, img, &pix);
241 RReleaseImage(img);
243 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
244 XClearWindow(dpy, scr->workspace_name);
245 XFreePixmap(dpy, pix);
246 XFlush(dpy);
248 scr->workspace_name_data->count--;
252 static void showWorkspaceName(WScreen * scr, int workspace)
254 WorkspaceNameData *data;
255 RXImage *ximg;
256 Pixmap text, mask;
257 int w, h;
258 int px, py;
259 char *name = scr->workspaces[workspace]->name;
260 int len = strlen(name);
261 int x, y;
262 #ifdef USE_XINERAMA
263 int head;
264 WMRect rect;
265 int xx, yy;
266 #endif
268 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2)
269 return;
271 if (scr->workspace_name_timer) {
272 WMDeleteTimerHandler(scr->workspace_name_timer);
273 XUnmapWindow(dpy, scr->workspace_name);
274 XFlush(dpy);
276 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
278 if (scr->workspace_name_data) {
279 RReleaseImage(scr->workspace_name_data->back);
280 RReleaseImage(scr->workspace_name_data->text);
281 wfree(scr->workspace_name_data);
284 data = wmalloc(sizeof(WorkspaceNameData));
285 data->back = NULL;
287 w = WMWidthOfString(scr->workspace_name_font, name, len);
288 h = WMFontHeight(scr->workspace_name_font);
290 #ifdef USE_XINERAMA
291 head = wGetHeadForPointerLocation(scr);
292 rect = wGetRectForHead(scr, head);
293 if (scr->xine_info.count) {
294 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
295 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
297 else {
298 xx = (scr->scr_width - (w + 4)) / 2;
299 yy = (scr->scr_height - (h + 4)) / 2;
301 #endif
303 switch (wPreferences.workspace_name_display_position) {
304 case WD_TOP:
305 #ifdef USE_XINERAMA
306 px = xx;
307 #else
308 px = (scr->scr_width - (w + 4)) / 2;
309 #endif
310 py = WORKSPACE_NAME_DISPLAY_PADDING;
311 break;
312 case WD_BOTTOM:
313 #ifdef USE_XINERAMA
314 px = xx;
315 #else
316 px = (scr->scr_width - (w + 4)) / 2;
317 #endif
318 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
319 break;
320 case WD_TOPLEFT:
321 px = WORKSPACE_NAME_DISPLAY_PADDING;
322 py = WORKSPACE_NAME_DISPLAY_PADDING;
323 break;
324 case WD_TOPRIGHT:
325 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
326 py = WORKSPACE_NAME_DISPLAY_PADDING;
327 break;
328 case WD_BOTTOMLEFT:
329 px = WORKSPACE_NAME_DISPLAY_PADDING;
330 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
331 break;
332 case WD_BOTTOMRIGHT:
333 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
334 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
335 break;
336 case WD_CENTER:
337 default:
338 #ifdef USE_XINERAMA
339 px = xx;
340 py = yy;
341 #else
342 px = (scr->scr_width - (w + 4)) / 2;
343 py = (scr->scr_height - (h + 4)) / 2;
344 #endif
345 break;
347 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
348 XMoveWindow(dpy, scr->workspace_name, px, py);
350 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
351 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
353 /*XSetForeground(dpy, scr->mono_gc, 0);
354 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
356 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
358 for (x = 0; x <= 4; x++)
359 for (y = 0; y <= 4; y++)
360 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
362 XSetForeground(dpy, scr->mono_gc, 1);
363 XSetBackground(dpy, scr->mono_gc, 0);
365 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
367 /*XSetForeground(dpy, scr->mono_gc, 1); */
368 XSetBackground(dpy, scr->mono_gc, 1);
370 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
372 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
374 #ifdef USE_XSHAPE
375 if (w_global.xext.shape.supported)
376 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
377 #endif
378 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
379 XClearWindow(dpy, scr->workspace_name);
381 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
383 XFreePixmap(dpy, text);
384 XFreePixmap(dpy, mask);
386 if (!data->text) {
387 XMapRaised(dpy, scr->workspace_name);
388 XFlush(dpy);
390 goto erro;
393 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
394 if (!ximg)
395 goto erro;
397 XMapRaised(dpy, scr->workspace_name);
398 XFlush(dpy);
400 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
401 RDestroyXImage(scr->rcontext, ximg);
403 if (!data->back) {
404 goto erro;
407 data->count = 10;
409 /* set a timeout for the effect */
410 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
412 scr->workspace_name_data = data;
414 return;
416 erro:
417 if (scr->workspace_name_timer)
418 WMDeleteTimerHandler(scr->workspace_name_timer);
420 if (data->text)
421 RReleaseImage(data->text);
422 if (data->back)
423 RReleaseImage(data->back);
424 wfree(data);
426 scr->workspace_name_data = NULL;
428 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
429 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
432 void wWorkspaceChange(WScreen *scr, int workspace)
434 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
435 return;
437 if (workspace != scr->current_workspace)
438 wWorkspaceForceChange(scr, workspace);
441 void wWorkspaceRelativeChange(WScreen * scr, int amount)
443 int w;
445 /* While the deiconify animation is going on the window is
446 * still "flying" to its final position and we don't want to
447 * change workspace before the animation finishes, otherwise
448 * the window will land in the new workspace */
449 if (w_global.ignore_workspace_change)
450 return;
452 w = scr->current_workspace + amount;
454 if (amount < 0) {
455 if (w >= 0) {
456 wWorkspaceChange(scr, w);
457 } else if (wPreferences.ws_cycle) {
458 wWorkspaceChange(scr, scr->workspace_count + w);
460 } else if (amount > 0) {
461 if (w < scr->workspace_count) {
462 wWorkspaceChange(scr, w);
463 } else if (wPreferences.ws_advance) {
464 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
465 } else if (wPreferences.ws_cycle) {
466 wWorkspaceChange(scr, w % scr->workspace_count);
471 void wWorkspaceForceChange(WScreen * scr, int workspace)
473 WWindow *tmp, *foc = NULL, *foc2 = NULL;
475 if (workspace >= MAX_WORKSPACES || workspace < 0)
476 return;
478 if (!wPreferences.disable_workspace_pager && !w_global.process_workspacemap_event)
479 wWorkspaceMapUpdate(scr);
481 SendHelperMessage(scr, 'C', workspace + 1, NULL);
483 if (workspace > scr->workspace_count - 1)
484 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
486 wClipUpdateForWorkspaceChange(scr, workspace);
488 scr->last_workspace = scr->current_workspace;
489 scr->current_workspace = workspace;
491 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
493 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
495 if ((tmp = scr->focused_window) != NULL) {
496 WWindow **toUnmap;
497 int toUnmapSize, toUnmapCount;
499 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
500 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
501 foc = tmp;
504 toUnmapSize = 16;
505 toUnmapCount = 0;
506 toUnmap = wmalloc(toUnmapSize * sizeof(WWindow *));
508 /* foc2 = tmp; will fix annoyance with gnome panel
509 * but will create annoyance for every other application
511 while (tmp) {
512 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
513 /* unmap windows not on this workspace */
514 if ((tmp->flags.mapped || tmp->flags.shaded) &&
515 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
516 if (toUnmapCount == toUnmapSize)
518 toUnmapSize *= 2;
519 toUnmap = wrealloc(toUnmap, toUnmapSize * sizeof(WWindow *));
521 toUnmap[toUnmapCount++] = tmp;
523 /* also unmap miniwindows not on this workspace */
524 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
525 tmp->icon && !IS_OMNIPRESENT(tmp)) {
526 XUnmapWindow(dpy, tmp->icon->core->window);
527 tmp->icon->mapped = 0;
529 /* update current workspace of omnipresent windows */
530 if (IS_OMNIPRESENT(tmp)) {
531 WApplication *wapp = wApplicationOf(tmp->main_window);
533 tmp->frame->workspace = workspace;
535 if (wapp) {
536 wapp->last_workspace = workspace;
538 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
539 foc2 = tmp;
542 } else {
543 /* change selected windows' workspace */
544 if (tmp->flags.selected) {
545 wWindowChangeWorkspace(tmp, workspace);
546 if (!tmp->flags.miniaturized && !foc) {
547 foc = tmp;
549 } else {
550 if (!tmp->flags.hidden) {
551 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
552 /* remap windows that are on this workspace */
553 wWindowMap(tmp);
554 if (!foc && !WFLAGP(tmp, no_focusable)) {
555 foc = tmp;
558 /* Also map miniwindow if not omnipresent */
559 if (!wPreferences.sticky_icons &&
560 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
561 tmp->icon->mapped = 1;
562 XMapWindow(dpy, tmp->icon->core->window);
567 tmp = tmp->prev;
570 while (toUnmapCount > 0)
572 wWindowUnmap(toUnmap[--toUnmapCount]);
574 wfree(toUnmap);
576 /* Gobble up events unleashed by our mapping & unmapping.
577 * These may trigger various grab-initiated focus &
578 * crossing events. However, we don't care about them,
579 * and ignore their focus implications altogether to avoid
580 * flicker.
582 scr->flags.ignore_focus_events = 1;
583 ProcessPendingEvents();
584 scr->flags.ignore_focus_events = 0;
586 if (!foc)
587 foc = foc2;
590 * Check that the window we want to focus still exists, because the application owning it
591 * could decide to unmap/destroy it in response to unmap any of its other window following
592 * the workspace change, this happening during our 'ProcessPendingEvents' loop.
594 if (foc != NULL) {
595 WWindow *parse;
596 Bool found;
598 found = False;
599 for (parse = scr->focused_window; parse != NULL; parse = parse->prev) {
600 if (parse == foc) {
601 found = True;
602 break;
605 if (!found)
606 foc = NULL;
609 if (scr->focused_window->flags.mapped && !foc) {
610 foc = scr->focused_window;
612 if (wPreferences.focus_mode == WKF_CLICK) {
613 wSetFocusTo(scr, foc);
614 } else {
615 unsigned int mask;
616 int foo;
617 Window bar, win;
618 WWindow *tmp;
620 tmp = NULL;
621 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
622 tmp = wWindowFor(win);
625 /* If there's a window under the pointer, focus it.
626 * (we ate all other focus events above, so it's
627 * certainly not focused). Otherwise focus last
628 * focused, or the root (depending on sloppiness)
630 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
631 wSetFocusTo(scr, foc);
632 } else {
633 wSetFocusTo(scr, tmp);
638 /* We need to always arrange icons when changing workspace, even if
639 * no autoarrange icons, because else the icons in different workspaces
640 * can be superposed.
641 * This can be avoided if appicons are also workspace specific.
643 if (!wPreferences.sticky_icons)
644 wArrangeIcons(scr, False);
646 if (scr->dock)
647 wAppIconPaint(scr->dock->icon_array[0]);
649 if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
650 scr->workspaces[workspace]->clip->auto_raise_lower)) {
651 /* to handle enter notify. This will also */
652 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
653 XMapWindow(dpy, scr->clip_icon->icon->core->window);
655 else if (scr->clip_icon != NULL) {
656 wClipIconPaint(scr->clip_icon);
658 wScreenUpdateUsableArea(scr);
659 wNETWMUpdateDesktop(scr);
660 showWorkspaceName(scr, workspace);
662 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
664 /* XSync(dpy, False); */
667 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
669 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
672 static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
674 /* Parameter not used, but tell the compiler that it is ok */
675 (void) entry;
677 wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
680 static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
682 /* Parameter not used, but tell the compiler that it is ok */
683 (void) entry;
685 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
688 static void newWSCommand(WMenu *menu, WMenuEntry *foo)
690 int ws;
692 /* Parameter not used, but tell the compiler that it is ok */
693 (void) foo;
695 ws = wWorkspaceNew(menu->frame->screen_ptr);
697 /* autochange workspace */
698 if (ws >= 0)
699 wWorkspaceChange(menu->frame->screen_ptr, ws);
702 void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
704 char buf[MAX_WORKSPACENAME_WIDTH + 1];
705 char *tmp;
707 if (workspace >= scr->workspace_count)
708 return;
710 /* trim white spaces */
711 tmp = wtrimspace(name);
713 if (strlen(tmp) == 0) {
714 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
715 } else {
716 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
718 buf[MAX_WORKSPACENAME_WIDTH] = 0;
719 wfree(tmp);
721 /* update workspace */
722 wfree(scr->workspaces[workspace]->name);
723 scr->workspaces[workspace]->name = wstrdup(buf);
725 if (scr->clip_ws_menu) {
726 if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
727 wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
728 scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
729 wMenuRealize(scr->clip_ws_menu);
732 if (scr->workspace_menu) {
733 if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
734 wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
735 scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
736 wMenuRealize(scr->workspace_menu);
740 if (scr->clip_icon)
741 wClipIconPaint(scr->clip_icon);
743 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
746 /* callback for when menu entry is edited */
747 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
749 char *tmp;
751 tmp = entry->text;
752 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
755 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
757 WMenu *wsmenu;
758 WMenuEntry *entry;
760 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
761 if (!wsmenu) {
762 wwarning(_("could not create Workspace menu"));
763 return NULL;
766 /* callback to be called when an entry is edited */
767 wsmenu->on_edit = onMenuEntryEdited;
769 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
770 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
772 entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
773 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
775 return wsmenu;
778 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
780 int i;
781 long ws;
782 char title[MAX_WORKSPACENAME_WIDTH + 1];
783 WMenuEntry *entry;
784 int tmp;
786 if (!menu)
787 return;
789 if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
790 /* new workspace(s) added */
791 i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
792 ws = menu->entry_no - MC_WORKSPACE1;
793 while (i > 0) {
794 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
796 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
797 entry->flags.indicator = 1;
798 entry->flags.editable = 1;
800 i--;
801 ws++;
803 } else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
804 /* removed workspace(s) */
805 for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
806 wMenuRemoveItem(menu, i);
809 for (i = 0; i < scr->workspace_count; i++) {
810 /* workspace shortcut labels */
811 if (i / 10 == scr->current_workspace / 10)
812 menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
813 else
814 menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
816 menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
818 menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
819 wMenuRealize(menu);
821 /* don't let user destroy current workspace */
822 if (scr->current_workspace == scr->workspace_count - 1)
823 wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
824 else
825 wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
827 /* back to last workspace */
828 if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
829 wMenuSetEnabled(menu, MC_LAST_USED, True);
830 else
831 wMenuSetEnabled(menu, MC_LAST_USED, False);
833 tmp = menu->frame->top_width + 5;
834 /* if menu got unreachable, bring it to a visible place */
835 if (menu->frame_x < tmp - (int)menu->frame->core->width)
836 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
838 wMenuPaint(menu);
841 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
843 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
844 int i;
846 make_keys();
848 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
849 parr = WMCreatePLArray(NULL);
850 for (i = 0; i < scr->workspace_count; i++) {
851 pstr = WMCreatePLString(scr->workspaces[i]->name);
852 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
853 WMReleasePropList(pstr);
854 if (!wPreferences.flags.noclip) {
855 pstr = wClipSaveWorkspaceState(scr, i);
856 WMPutInPLDictionary(wks_state, dClip, pstr);
857 WMReleasePropList(pstr);
858 } else if (old_wks_state != NULL) {
859 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
860 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
861 WMPutInPLDictionary(wks_state, dClip, bar);
865 WMAddToPLArray(parr, wks_state);
866 WMReleasePropList(wks_state);
868 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
869 WMReleasePropList(parr);
872 void wWorkspaceRestoreState(WScreen *scr)
874 WMPropList *parr, *pstr, *wks_state, *clip_state;
875 int i, j;
877 make_keys();
879 if (scr->session_state == NULL)
880 return;
882 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
884 if (!parr)
885 return;
887 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
888 wks_state = WMGetFromPLArray(parr, i);
889 if (WMIsPLDictionary(wks_state))
890 pstr = WMGetFromPLDictionary(wks_state, dName);
891 else
892 pstr = wks_state;
894 if (i >= scr->workspace_count)
895 wWorkspaceNew(scr);
897 if (scr->workspace_menu) {
898 wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
899 scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
900 scr->workspace_menu->flags.realized = 0;
903 wfree(scr->workspaces[i]->name);
904 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
905 if (!wPreferences.flags.noclip) {
906 int added_omnipresent_icons = 0;
908 clip_state = WMGetFromPLDictionary(wks_state, dClip);
909 if (scr->workspaces[i]->clip)
910 wDockDestroy(scr->workspaces[i]->clip);
912 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
913 if (i > 0)
914 wDockHideIcons(scr->workspaces[i]->clip);
916 /* We set the global icons here, because scr->workspaces[i]->clip
917 * was not valid in wDockRestoreState().
918 * There we only set icon->omnipresent to know which icons we
919 * need to set here.
921 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
922 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
923 int k;
925 if (!aicon || !aicon->omnipresent)
926 continue;
927 aicon->omnipresent = 0;
928 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
929 continue;
930 if (i == 0)
931 continue;
933 /* Move this appicon from workspace i to workspace 0 */
934 scr->workspaces[i]->clip->icon_array[j] = NULL;
935 scr->workspaces[i]->clip->icon_count--;
937 added_omnipresent_icons++;
938 /* If there are too many omnipresent appicons, we are in trouble */
939 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
940 <= scr->workspaces[0]->clip->max_icons);
941 /* Find first free spot on workspace 0 */
942 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
943 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
944 break;
945 scr->workspaces[0]->clip->icon_array[k] = aicon;
946 aicon->dock = scr->workspaces[0]->clip;
948 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
951 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
955 /* Returns the workspace number for a given workspace name */
956 int wGetWorkspaceNumber(WScreen *scr, const char *value)
958 int w, i;
960 if (sscanf(value, "%i", &w) != 1) {
961 w = -1;
962 for (i = 0; i < scr->workspace_count; i++) {
963 if (strcmp(scr->workspaces[i]->name, value) == 0) {
964 w = i;
965 break;
968 } else {
969 w--;
972 return w;