debian: Import version 0.95.6-1.
[wmaker-crm.git] / src / workspace.c
blobc6052b3fb97c39ace801024bea1134fa54de6f3c
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 tmp = scr->focused_window;
496 if (tmp != NULL) {
497 WWindow **toUnmap;
498 int toUnmapSize, toUnmapCount;
500 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
501 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
502 foc = tmp;
505 toUnmapSize = 16;
506 toUnmapCount = 0;
507 toUnmap = wmalloc(toUnmapSize * sizeof(WWindow *));
509 /* foc2 = tmp; will fix annoyance with gnome panel
510 * but will create annoyance for every other application
512 while (tmp) {
513 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
514 /* unmap windows not on this workspace */
515 if ((tmp->flags.mapped || tmp->flags.shaded) &&
516 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
517 if (toUnmapCount == toUnmapSize)
519 toUnmapSize *= 2;
520 toUnmap = wrealloc(toUnmap, toUnmapSize * sizeof(WWindow *));
522 toUnmap[toUnmapCount++] = tmp;
524 /* also unmap miniwindows not on this workspace */
525 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
526 tmp->icon && !IS_OMNIPRESENT(tmp)) {
527 XUnmapWindow(dpy, tmp->icon->core->window);
528 tmp->icon->mapped = 0;
530 /* update current workspace of omnipresent windows */
531 if (IS_OMNIPRESENT(tmp)) {
532 WApplication *wapp = wApplicationOf(tmp->main_window);
534 tmp->frame->workspace = workspace;
536 if (wapp) {
537 wapp->last_workspace = workspace;
539 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
540 foc2 = tmp;
543 } else {
544 /* change selected windows' workspace */
545 if (tmp->flags.selected) {
546 wWindowChangeWorkspace(tmp, workspace);
547 if (!tmp->flags.miniaturized && !foc) {
548 foc = tmp;
550 } else {
551 if (!tmp->flags.hidden) {
552 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
553 /* remap windows that are on this workspace */
554 wWindowMap(tmp);
555 if (!foc && !WFLAGP(tmp, no_focusable)) {
556 foc = tmp;
559 /* Also map miniwindow if not omnipresent */
560 if (!wPreferences.sticky_icons &&
561 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
562 tmp->icon->mapped = 1;
563 XMapWindow(dpy, tmp->icon->core->window);
568 tmp = tmp->prev;
571 while (toUnmapCount > 0)
573 wWindowUnmap(toUnmap[--toUnmapCount]);
575 wfree(toUnmap);
577 /* Gobble up events unleashed by our mapping & unmapping.
578 * These may trigger various grab-initiated focus &
579 * crossing events. However, we don't care about them,
580 * and ignore their focus implications altogether to avoid
581 * flicker.
583 scr->flags.ignore_focus_events = 1;
584 ProcessPendingEvents();
585 scr->flags.ignore_focus_events = 0;
587 if (!foc)
588 foc = foc2;
591 * Check that the window we want to focus still exists, because the application owning it
592 * could decide to unmap/destroy it in response to unmap any of its other window following
593 * the workspace change, this happening during our 'ProcessPendingEvents' loop.
595 if (foc != NULL) {
596 WWindow *parse;
597 Bool found;
599 found = False;
600 for (parse = scr->focused_window; parse != NULL; parse = parse->prev) {
601 if (parse == foc) {
602 found = True;
603 break;
606 if (!found)
607 foc = NULL;
610 if (scr->focused_window->flags.mapped && !foc) {
611 foc = scr->focused_window;
613 if (wPreferences.focus_mode == WKF_CLICK) {
614 wSetFocusTo(scr, foc);
615 } else {
616 unsigned int mask;
617 int foo;
618 Window bar, win;
619 WWindow *tmp;
621 tmp = NULL;
622 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
623 tmp = wWindowFor(win);
626 /* If there's a window under the pointer, focus it.
627 * (we ate all other focus events above, so it's
628 * certainly not focused). Otherwise focus last
629 * focused, or the root (depending on sloppiness)
631 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
632 wSetFocusTo(scr, foc);
633 } else {
634 wSetFocusTo(scr, tmp);
639 /* We need to always arrange icons when changing workspace, even if
640 * no autoarrange icons, because else the icons in different workspaces
641 * can be superposed.
642 * This can be avoided if appicons are also workspace specific.
644 if (!wPreferences.sticky_icons)
645 wArrangeIcons(scr, False);
647 if (scr->dock)
648 wAppIconPaint(scr->dock->icon_array[0]);
650 if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
651 scr->workspaces[workspace]->clip->auto_raise_lower)) {
652 /* to handle enter notify. This will also */
653 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
654 XMapWindow(dpy, scr->clip_icon->icon->core->window);
656 else if (scr->clip_icon != NULL) {
657 wClipIconPaint(scr->clip_icon);
659 wScreenUpdateUsableArea(scr);
660 wNETWMUpdateDesktop(scr);
661 showWorkspaceName(scr, workspace);
663 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
665 /* XSync(dpy, False); */
668 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
670 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
673 static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
675 /* Parameter not used, but tell the compiler that it is ok */
676 (void) entry;
678 wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
681 static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
683 /* Parameter not used, but tell the compiler that it is ok */
684 (void) entry;
686 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
689 static void newWSCommand(WMenu *menu, WMenuEntry *foo)
691 int ws;
693 /* Parameter not used, but tell the compiler that it is ok */
694 (void) foo;
696 ws = wWorkspaceNew(menu->frame->screen_ptr);
698 /* autochange workspace */
699 if (ws >= 0)
700 wWorkspaceChange(menu->frame->screen_ptr, ws);
703 void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
705 char buf[MAX_WORKSPACENAME_WIDTH + 1];
706 char *tmp;
708 if (workspace >= scr->workspace_count)
709 return;
711 /* trim white spaces */
712 tmp = wtrimspace(name);
714 if (strlen(tmp) == 0) {
715 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
716 } else {
717 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
719 buf[MAX_WORKSPACENAME_WIDTH] = 0;
720 wfree(tmp);
722 /* update workspace */
723 wfree(scr->workspaces[workspace]->name);
724 scr->workspaces[workspace]->name = wstrdup(buf);
726 if (scr->clip_ws_menu) {
727 if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
728 wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
729 scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
730 wMenuRealize(scr->clip_ws_menu);
733 if (scr->workspace_menu) {
734 if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
735 wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
736 scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
737 wMenuRealize(scr->workspace_menu);
741 if (scr->clip_icon)
742 wClipIconPaint(scr->clip_icon);
744 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
747 /* callback for when menu entry is edited */
748 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
750 char *tmp;
752 tmp = entry->text;
753 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
756 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
758 WMenu *wsmenu;
759 WMenuEntry *entry;
761 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
762 if (!wsmenu) {
763 wwarning(_("could not create Workspace menu"));
764 return NULL;
767 /* callback to be called when an entry is edited */
768 wsmenu->on_edit = onMenuEntryEdited;
770 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
771 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
773 entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
774 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
776 return wsmenu;
779 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
781 int i;
782 long ws;
783 char title[MAX_WORKSPACENAME_WIDTH + 1];
784 WMenuEntry *entry;
785 int tmp;
787 if (!menu)
788 return;
790 if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
791 /* new workspace(s) added */
792 i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
793 ws = menu->entry_no - MC_WORKSPACE1;
794 while (i > 0) {
795 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
797 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
798 entry->flags.indicator = 1;
799 entry->flags.editable = 1;
801 i--;
802 ws++;
804 } else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
805 /* removed workspace(s) */
806 for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
807 wMenuRemoveItem(menu, i);
810 for (i = 0; i < scr->workspace_count; i++) {
811 /* workspace shortcut labels */
812 if (i / 10 == scr->current_workspace / 10)
813 menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
814 else
815 menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
817 menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
819 menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
820 wMenuRealize(menu);
822 /* don't let user destroy current workspace */
823 if (scr->current_workspace == scr->workspace_count - 1)
824 wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
825 else
826 wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
828 /* back to last workspace */
829 if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
830 wMenuSetEnabled(menu, MC_LAST_USED, True);
831 else
832 wMenuSetEnabled(menu, MC_LAST_USED, False);
834 tmp = menu->frame->top_width + 5;
835 /* if menu got unreachable, bring it to a visible place */
836 if (menu->frame_x < tmp - (int)menu->frame->core->width)
837 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
839 wMenuPaint(menu);
842 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
844 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
845 int i;
847 make_keys();
849 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
850 parr = WMCreatePLArray(NULL);
851 for (i = 0; i < scr->workspace_count; i++) {
852 pstr = WMCreatePLString(scr->workspaces[i]->name);
853 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
854 WMReleasePropList(pstr);
855 if (!wPreferences.flags.noclip) {
856 pstr = wClipSaveWorkspaceState(scr, i);
857 WMPutInPLDictionary(wks_state, dClip, pstr);
858 WMReleasePropList(pstr);
859 } else if (old_wks_state != NULL) {
860 foo = WMGetFromPLArray(old_wks_state, i);
861 if (foo != NULL) {
862 bar = WMGetFromPLDictionary(foo, dClip);
863 if (bar != NULL)
864 WMPutInPLDictionary(wks_state, dClip, bar);
867 WMAddToPLArray(parr, wks_state);
868 WMReleasePropList(wks_state);
870 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
871 WMReleasePropList(parr);
874 void wWorkspaceRestoreState(WScreen *scr)
876 WMPropList *parr, *pstr, *wks_state, *clip_state;
877 int i, j;
879 make_keys();
881 if (scr->session_state == NULL)
882 return;
884 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
886 if (!parr)
887 return;
889 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
890 wks_state = WMGetFromPLArray(parr, i);
891 if (WMIsPLDictionary(wks_state))
892 pstr = WMGetFromPLDictionary(wks_state, dName);
893 else
894 pstr = wks_state;
896 if (i >= scr->workspace_count)
897 wWorkspaceNew(scr);
899 if (scr->workspace_menu) {
900 wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
901 scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
902 scr->workspace_menu->flags.realized = 0;
905 wfree(scr->workspaces[i]->name);
906 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
907 if (!wPreferences.flags.noclip) {
908 int added_omnipresent_icons = 0;
910 clip_state = WMGetFromPLDictionary(wks_state, dClip);
911 if (scr->workspaces[i]->clip)
912 wDockDestroy(scr->workspaces[i]->clip);
914 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
915 if (i > 0)
916 wDockHideIcons(scr->workspaces[i]->clip);
918 /* We set the global icons here, because scr->workspaces[i]->clip
919 * was not valid in wDockRestoreState().
920 * There we only set icon->omnipresent to know which icons we
921 * need to set here.
923 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
924 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
925 int k;
927 if (!aicon || !aicon->omnipresent)
928 continue;
929 aicon->omnipresent = 0;
930 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
931 continue;
932 if (i == 0)
933 continue;
935 /* Move this appicon from workspace i to workspace 0 */
936 scr->workspaces[i]->clip->icon_array[j] = NULL;
937 scr->workspaces[i]->clip->icon_count--;
939 added_omnipresent_icons++;
940 /* If there are too many omnipresent appicons, we are in trouble */
941 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
942 <= scr->workspaces[0]->clip->max_icons);
943 /* Find first free spot on workspace 0 */
944 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
945 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
946 break;
947 scr->workspaces[0]->clip->icon_array[k] = aicon;
948 aicon->dock = scr->workspaces[0]->clip;
950 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
953 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
957 /* Returns the workspace number for a given workspace name */
958 int wGetWorkspaceNumber(WScreen *scr, const char *value)
960 int w, i;
962 if (sscanf(value, "%i", &w) != 1) {
963 w = -1;
964 for (i = 0; i < scr->workspace_count; i++) {
965 if (strcmp(scr->workspaces[i]->name, value) == 0) {
966 w = i;
967 break;
970 } else {
971 w--;
974 return w;