wmaker: moved the variable 'process_workspacemap_event' to the global namespace
[wmaker-crm.git] / src / workspace.c
blob9a820574b8459471eec8f4c06332068fa11971de
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 MAX_SHORTCUT_LENGTH 32
61 #define WORKSPACE_NAME_DISPLAY_PADDING 32
63 static WMPropList *dWorkspaces = NULL;
64 static WMPropList *dClip, *dName;
66 static void make_keys(void)
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;
94 wspace->clip = NULL;
96 if (!wspace->name) {
97 static const char *new_name = NULL;
98 static size_t name_length;
100 if (new_name == NULL) {
101 new_name = _("Workspace %i");
102 name_length = strlen(new_name) + 8;
104 wspace->name = wmalloc(name_length);
105 snprintf(wspace->name, name_length, new_name, scr->workspace_count);
108 if (!wPreferences.flags.noclip)
109 wspace->clip = wDockCreate(scr, WM_CLIP, NULL);
111 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
113 for (i = 0; i < scr->workspace_count - 1; i++)
114 list[i] = scr->workspaces[i];
116 list[i] = wspace;
117 if (scr->workspaces)
118 wfree(scr->workspaces);
120 scr->workspaces = list;
122 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
123 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
124 wNETWMUpdateDesktop(scr);
125 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
126 XFlush(dpy);
128 return scr->workspace_count - 1;
131 return -1;
134 Bool wWorkspaceDelete(WScreen * scr, int workspace)
136 WWindow *tmp;
137 WWorkspace **list;
138 int i, j;
140 if (workspace <= 0)
141 return False;
143 /* verify if workspace is in use by some window */
144 tmp = scr->focused_window;
145 while (tmp) {
146 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
147 return False;
148 tmp = tmp->prev;
151 if (!wPreferences.flags.noclip) {
152 wDockDestroy(scr->workspaces[workspace]->clip);
153 scr->workspaces[workspace]->clip = NULL;
156 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
157 j = 0;
158 for (i = 0; i < scr->workspace_count; i++) {
159 if (i != workspace) {
160 list[j++] = scr->workspaces[i];
161 } else {
162 if (scr->workspaces[i]->name)
163 wfree(scr->workspaces[i]->name);
164 if (scr->workspaces[i]->map)
165 RReleaseImage(scr->workspaces[i]->map);
166 wfree(scr->workspaces[i]);
169 wfree(scr->workspaces);
170 scr->workspaces = list;
172 scr->workspace_count--;
174 /* update menu */
175 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
176 /* clip workspace menu */
177 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
179 /* update also window menu */
180 if (scr->workspace_submenu) {
181 WMenu *menu = scr->workspace_submenu;
183 i = menu->entry_no;
184 while (i > scr->workspace_count)
185 wMenuRemoveItem(menu, --i);
186 wMenuRealize(menu);
188 /* and clip menu */
189 if (scr->clip_submenu) {
190 WMenu *menu = scr->clip_submenu;
192 i = menu->entry_no;
193 while (i > scr->workspace_count)
194 wMenuRemoveItem(menu, --i);
195 wMenuRealize(menu);
197 wNETWMUpdateDesktop(scr);
198 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
200 if (scr->current_workspace >= scr->workspace_count)
201 wWorkspaceChange(scr, scr->workspace_count - 1);
202 if (scr->last_workspace >= scr->workspace_count)
203 scr->last_workspace = 0;
205 return True;
208 typedef struct WorkspaceNameData {
209 int count;
210 RImage *back;
211 RImage *text;
212 time_t timeout;
213 } WorkspaceNameData;
215 static void hideWorkspaceName(void *data)
217 WScreen *scr = (WScreen *) data;
219 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
220 || time(NULL) > scr->workspace_name_data->timeout) {
221 XUnmapWindow(dpy, scr->workspace_name);
223 if (scr->workspace_name_data) {
224 RReleaseImage(scr->workspace_name_data->back);
225 RReleaseImage(scr->workspace_name_data->text);
226 wfree(scr->workspace_name_data);
228 scr->workspace_name_data = NULL;
230 scr->workspace_name_timer = NULL;
231 } else {
232 RImage *img = RCloneImage(scr->workspace_name_data->back);
233 Pixmap pix;
235 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
237 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
238 scr->workspace_name_data->count * 255 / 10);
240 RConvertImage(scr->rcontext, img, &pix);
242 RReleaseImage(img);
244 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
245 XClearWindow(dpy, scr->workspace_name);
246 XFreePixmap(dpy, pix);
247 XFlush(dpy);
249 scr->workspace_name_data->count--;
253 static void showWorkspaceName(WScreen * scr, int workspace)
255 WorkspaceNameData *data;
256 RXImage *ximg;
257 Pixmap text, mask;
258 int w, h;
259 int px, py;
260 char *name = scr->workspaces[workspace]->name;
261 int len = strlen(name);
262 int x, y;
263 #ifdef USE_XINERAMA
264 int head;
265 WMRect rect;
266 int xx, yy;
267 #endif
269 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2)
270 return;
272 if (scr->workspace_name_timer) {
273 WMDeleteTimerHandler(scr->workspace_name_timer);
274 XUnmapWindow(dpy, scr->workspace_name);
275 XFlush(dpy);
277 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
279 if (scr->workspace_name_data) {
280 RReleaseImage(scr->workspace_name_data->back);
281 RReleaseImage(scr->workspace_name_data->text);
282 wfree(scr->workspace_name_data);
285 data = wmalloc(sizeof(WorkspaceNameData));
286 data->back = NULL;
288 w = WMWidthOfString(scr->workspace_name_font, name, len);
289 h = WMFontHeight(scr->workspace_name_font);
291 #ifdef USE_XINERAMA
292 head = wGetHeadForPointerLocation(scr);
293 rect = wGetRectForHead(scr, head);
294 if (scr->xine_info.count) {
295 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
296 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
298 else {
299 xx = (scr->scr_width - (w + 4)) / 2;
300 yy = (scr->scr_height - (h + 4)) / 2;
302 #endif
304 switch (wPreferences.workspace_name_display_position) {
305 case WD_TOP:
306 #ifdef USE_XINERAMA
307 px = xx;
308 #else
309 px = (scr->scr_width - (w + 4)) / 2;
310 #endif
311 py = WORKSPACE_NAME_DISPLAY_PADDING;
312 break;
313 case WD_BOTTOM:
314 #ifdef USE_XINERAMA
315 px = xx;
316 #else
317 px = (scr->scr_width - (w + 4)) / 2;
318 #endif
319 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
320 break;
321 case WD_TOPLEFT:
322 px = WORKSPACE_NAME_DISPLAY_PADDING;
323 py = WORKSPACE_NAME_DISPLAY_PADDING;
324 break;
325 case WD_TOPRIGHT:
326 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
327 py = WORKSPACE_NAME_DISPLAY_PADDING;
328 break;
329 case WD_BOTTOMLEFT:
330 px = WORKSPACE_NAME_DISPLAY_PADDING;
331 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
332 break;
333 case WD_BOTTOMRIGHT:
334 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
335 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
336 break;
337 case WD_CENTER:
338 default:
339 #ifdef USE_XINERAMA
340 px = xx;
341 py = yy;
342 #else
343 px = (scr->scr_width - (w + 4)) / 2;
344 py = (scr->scr_height - (h + 4)) / 2;
345 #endif
346 break;
348 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
349 XMoveWindow(dpy, scr->workspace_name, px, py);
351 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
352 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
354 /*XSetForeground(dpy, scr->mono_gc, 0);
355 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
357 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
359 for (x = 0; x <= 4; x++)
360 for (y = 0; y <= 4; y++)
361 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
363 XSetForeground(dpy, scr->mono_gc, 1);
364 XSetBackground(dpy, scr->mono_gc, 0);
366 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
368 /*XSetForeground(dpy, scr->mono_gc, 1); */
369 XSetBackground(dpy, scr->mono_gc, 1);
371 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
373 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
375 #ifdef USE_XSHAPE
376 if (w_global.xext.shape.supported)
377 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
378 #endif
379 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
380 XClearWindow(dpy, scr->workspace_name);
382 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
384 XFreePixmap(dpy, text);
385 XFreePixmap(dpy, mask);
387 if (!data->text) {
388 XMapRaised(dpy, scr->workspace_name);
389 XFlush(dpy);
391 goto erro;
394 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
395 if (!ximg)
396 goto erro;
398 XMapRaised(dpy, scr->workspace_name);
399 XFlush(dpy);
401 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
402 RDestroyXImage(scr->rcontext, ximg);
404 if (!data->back) {
405 goto erro;
408 data->count = 10;
410 /* set a timeout for the effect */
411 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
413 scr->workspace_name_data = data;
415 return;
417 erro:
418 if (scr->workspace_name_timer)
419 WMDeleteTimerHandler(scr->workspace_name_timer);
421 if (data->text)
422 RReleaseImage(data->text);
423 if (data->back)
424 RReleaseImage(data->back);
425 wfree(data);
427 scr->workspace_name_data = NULL;
429 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
430 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
433 void wWorkspaceChange(WScreen *scr, int workspace)
435 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
436 return;
438 if (workspace != scr->current_workspace)
439 wWorkspaceForceChange(scr, workspace);
442 void wWorkspaceRelativeChange(WScreen * scr, int amount)
444 int w;
446 /* While the deiconify animation is going on the window is
447 * still "flying" to its final position and we don't want to
448 * change workspace before the animation finishes, otherwise
449 * the window will land in the new workspace */
450 if (w_global.ignore_workspace_change)
451 return;
453 w = scr->current_workspace + amount;
455 if (amount < 0) {
456 if (w >= 0) {
457 wWorkspaceChange(scr, w);
458 } else if (wPreferences.ws_cycle) {
459 wWorkspaceChange(scr, scr->workspace_count + w);
461 } else if (amount > 0) {
462 if (w < scr->workspace_count) {
463 wWorkspaceChange(scr, w);
464 } else if (wPreferences.ws_advance) {
465 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
466 } else if (wPreferences.ws_cycle) {
467 wWorkspaceChange(scr, w % scr->workspace_count);
472 void wWorkspaceForceChange(WScreen * scr, int workspace)
474 WWindow *tmp, *foc = NULL, *foc2 = NULL;
476 if (workspace >= MAX_WORKSPACES || workspace < 0)
477 return;
479 if (!wPreferences.disable_workspace_pager && !w_global.process_workspacemap_event)
480 wWorkspaceMapUpdate(scr);
482 SendHelperMessage(scr, 'C', workspace + 1, NULL);
484 if (workspace > scr->workspace_count - 1)
485 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
487 wClipUpdateForWorkspaceChange(scr, workspace);
489 scr->last_workspace = scr->current_workspace;
490 scr->current_workspace = workspace;
492 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
494 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
496 if ((tmp = scr->focused_window) != 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;
590 if (scr->focused_window->flags.mapped && !foc) {
591 foc = scr->focused_window;
593 if (wPreferences.focus_mode == WKF_CLICK) {
594 wSetFocusTo(scr, foc);
595 } else {
596 unsigned int mask;
597 int foo;
598 Window bar, win;
599 WWindow *tmp;
601 tmp = NULL;
602 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
603 tmp = wWindowFor(win);
606 /* If there's a window under the pointer, focus it.
607 * (we ate all other focus events above, so it's
608 * certainly not focused). Otherwise focus last
609 * focused, or the root (depending on sloppiness)
611 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
612 wSetFocusTo(scr, foc);
613 } else {
614 wSetFocusTo(scr, tmp);
619 /* We need to always arrange icons when changing workspace, even if
620 * no autoarrange icons, because else the icons in different workspaces
621 * can be superposed.
622 * This can be avoided if appicons are also workspace specific.
624 if (!wPreferences.sticky_icons)
625 wArrangeIcons(scr, False);
627 if (scr->dock)
628 wAppIconPaint(scr->dock->icon_array[0]);
630 if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
631 scr->workspaces[workspace]->clip->auto_raise_lower)) {
632 /* to handle enter notify. This will also */
633 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
634 XMapWindow(dpy, scr->clip_icon->icon->core->window);
636 else if (scr->clip_icon != NULL) {
637 wClipIconPaint(scr->clip_icon);
639 wScreenUpdateUsableArea(scr);
640 wNETWMUpdateDesktop(scr);
641 showWorkspaceName(scr, workspace);
643 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
645 /* XSync(dpy, False); */
648 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
650 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
653 static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
655 /* Parameter not used, but tell the compiler that it is ok */
656 (void) entry;
658 wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
661 static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
663 /* Parameter not used, but tell the compiler that it is ok */
664 (void) entry;
666 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
669 static void newWSCommand(WMenu *menu, WMenuEntry *foo)
671 int ws;
673 /* Parameter not used, but tell the compiler that it is ok */
674 (void) foo;
676 ws = wWorkspaceNew(menu->frame->screen_ptr);
678 /* autochange workspace */
679 if (ws >= 0)
680 wWorkspaceChange(menu->frame->screen_ptr, ws);
683 void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
685 char buf[MAX_WORKSPACENAME_WIDTH + 1];
686 char *tmp;
688 if (workspace >= scr->workspace_count)
689 return;
691 /* trim white spaces */
692 tmp = wtrimspace(name);
694 if (strlen(tmp) == 0) {
695 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
696 } else {
697 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
699 buf[MAX_WORKSPACENAME_WIDTH] = 0;
700 wfree(tmp);
702 /* update workspace */
703 wfree(scr->workspaces[workspace]->name);
704 scr->workspaces[workspace]->name = wstrdup(buf);
706 if (scr->clip_ws_menu) {
707 if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
708 wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
709 scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
710 wMenuRealize(scr->clip_ws_menu);
713 if (scr->workspace_menu) {
714 if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
715 wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
716 scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
717 wMenuRealize(scr->workspace_menu);
721 if (scr->clip_icon)
722 wClipIconPaint(scr->clip_icon);
724 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
727 /* callback for when menu entry is edited */
728 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
730 char *tmp;
732 tmp = entry->text;
733 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
736 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
738 WMenu *wsmenu;
739 WMenuEntry *entry;
741 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
742 if (!wsmenu) {
743 wwarning(_("could not create Workspace menu"));
744 return NULL;
747 /* callback to be called when an entry is edited */
748 wsmenu->on_edit = onMenuEntryEdited;
750 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
751 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
753 entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
754 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
756 return wsmenu;
759 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
761 int i;
762 long ws;
763 char title[MAX_WORKSPACENAME_WIDTH + 1];
764 WMenuEntry *entry;
765 int tmp;
767 if (!menu)
768 return;
770 if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
771 /* new workspace(s) added */
772 i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
773 ws = menu->entry_no - MC_WORKSPACE1;
774 while (i > 0) {
775 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
777 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
778 entry->flags.indicator = 1;
779 entry->flags.editable = 1;
781 i--;
782 ws++;
784 } else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
785 /* removed workspace(s) */
786 for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
787 wMenuRemoveItem(menu, i);
790 for (i = 0; i < scr->workspace_count; i++) {
791 /* workspace shortcut labels */
792 if (i / 10 == scr->current_workspace / 10)
793 menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
794 else
795 menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
797 menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
799 menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
800 wMenuRealize(menu);
802 /* don't let user destroy current workspace */
803 if (scr->current_workspace == scr->workspace_count - 1)
804 wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
805 else
806 wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
808 /* back to last workspace */
809 if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
810 wMenuSetEnabled(menu, MC_LAST_USED, True);
811 else
812 wMenuSetEnabled(menu, MC_LAST_USED, False);
814 tmp = menu->frame->top_width + 5;
815 /* if menu got unreachable, bring it to a visible place */
816 if (menu->frame_x < tmp - (int)menu->frame->core->width)
817 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
819 wMenuPaint(menu);
822 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
824 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
825 int i;
827 make_keys();
829 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
830 parr = WMCreatePLArray(NULL);
831 for (i = 0; i < scr->workspace_count; i++) {
832 pstr = WMCreatePLString(scr->workspaces[i]->name);
833 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
834 WMReleasePropList(pstr);
835 if (!wPreferences.flags.noclip) {
836 pstr = wClipSaveWorkspaceState(scr, i);
837 WMPutInPLDictionary(wks_state, dClip, pstr);
838 WMReleasePropList(pstr);
839 } else if (old_wks_state != NULL) {
840 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
841 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
842 WMPutInPLDictionary(wks_state, dClip, bar);
846 WMAddToPLArray(parr, wks_state);
847 WMReleasePropList(wks_state);
849 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
850 WMReleasePropList(parr);
853 void wWorkspaceRestoreState(WScreen *scr)
855 WMPropList *parr, *pstr, *wks_state, *clip_state;
856 int i, j;
858 make_keys();
860 if (scr->session_state == NULL)
861 return;
863 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
865 if (!parr)
866 return;
868 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
869 wks_state = WMGetFromPLArray(parr, i);
870 if (WMIsPLDictionary(wks_state))
871 pstr = WMGetFromPLDictionary(wks_state, dName);
872 else
873 pstr = wks_state;
875 if (i >= scr->workspace_count)
876 wWorkspaceNew(scr);
878 if (scr->workspace_menu) {
879 wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
880 scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
881 scr->workspace_menu->flags.realized = 0;
884 wfree(scr->workspaces[i]->name);
885 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
886 if (!wPreferences.flags.noclip) {
887 int added_omnipresent_icons = 0;
889 clip_state = WMGetFromPLDictionary(wks_state, dClip);
890 if (scr->workspaces[i]->clip)
891 wDockDestroy(scr->workspaces[i]->clip);
893 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
894 if (i > 0)
895 wDockHideIcons(scr->workspaces[i]->clip);
897 /* We set the global icons here, because scr->workspaces[i]->clip
898 * was not valid in wDockRestoreState().
899 * There we only set icon->omnipresent to know which icons we
900 * need to set here.
902 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
903 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
904 int k;
906 if (!aicon || !aicon->omnipresent)
907 continue;
908 aicon->omnipresent = 0;
909 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
910 continue;
911 if (i == 0)
912 continue;
914 /* Move this appicon from workspace i to workspace 0 */
915 scr->workspaces[i]->clip->icon_array[j] = NULL;
916 scr->workspaces[i]->clip->icon_count--;
918 added_omnipresent_icons++;
919 /* If there are too many omnipresent appicons, we are in trouble */
920 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
921 <= scr->workspaces[0]->clip->max_icons);
922 /* Find first free spot on workspace 0 */
923 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
924 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
925 break;
926 scr->workspaces[0]->clip->icon_array[k] = aicon;
927 aicon->dock = scr->workspaces[0]->clip;
929 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
932 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
936 /* Returns the workspace number for a given workspace name */
937 int wGetWorkspaceNumber(WScreen *scr, const char *value)
939 int w, i;
941 if (sscanf(value, "%i", &w) != 1) {
942 w = -1;
943 for (i = 0; i < scr->workspace_count; i++) {
944 if (strcmp(scr->workspaces[i]->name, value) == 0) {
945 w = i;
946 break;
949 } else {
950 w--;
953 return w;