WPrefs: Code formatting in TexturePanel.c; minimizes checkpatch.pl warnings.
[wmaker-crm.git] / src / workspace.c
blob23da1ef1dff290fb5fc7e31952cd3a468f4d832a
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"
53 #include "dialog.h"
55 #define MC_NEW 0
56 #define MC_DESTROY_LAST 1
57 #define MC_LAST_USED 2
58 /* index of the first workspace menu entry */
59 #define MC_WORKSPACE1 3
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 char buf[256];
148 snprintf(buf, sizeof(buf), _("Workspace \"%s\" in use; cannot delete"),
149 scr->workspaces[workspace]->name);
150 wMessageDialog(scr, _("Error"), buf, _("OK"), NULL, NULL);
151 return False;
153 tmp = tmp->prev;
156 if (!wPreferences.flags.noclip) {
157 wDockDestroy(scr->workspaces[workspace]->clip);
158 scr->workspaces[workspace]->clip = NULL;
161 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
162 j = 0;
163 for (i = 0; i < scr->workspace_count; i++) {
164 if (i != workspace) {
165 list[j++] = scr->workspaces[i];
166 } else {
167 if (scr->workspaces[i]->name)
168 wfree(scr->workspaces[i]->name);
169 if (scr->workspaces[i]->map)
170 RReleaseImage(scr->workspaces[i]->map);
171 wfree(scr->workspaces[i]);
174 wfree(scr->workspaces);
175 scr->workspaces = list;
177 scr->workspace_count--;
179 /* update menu */
180 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
181 /* clip workspace menu */
182 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
184 /* update also window menu */
185 if (scr->workspace_submenu) {
186 WMenu *menu = scr->workspace_submenu;
188 i = menu->entry_no;
189 while (i > scr->workspace_count)
190 wMenuRemoveItem(menu, --i);
191 wMenuRealize(menu);
193 /* and clip menu */
194 if (scr->clip_submenu) {
195 WMenu *menu = scr->clip_submenu;
197 i = menu->entry_no;
198 while (i > scr->workspace_count)
199 wMenuRemoveItem(menu, --i);
200 wMenuRealize(menu);
202 wNETWMUpdateDesktop(scr);
203 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
205 if (scr->current_workspace >= scr->workspace_count)
206 wWorkspaceChange(scr, scr->workspace_count - 1);
207 if (scr->last_workspace >= scr->workspace_count)
208 scr->last_workspace = 0;
210 return True;
213 typedef struct WorkspaceNameData {
214 int count;
215 RImage *back;
216 RImage *text;
217 time_t timeout;
218 } WorkspaceNameData;
220 static void hideWorkspaceName(void *data)
222 WScreen *scr = (WScreen *) data;
224 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
225 || time(NULL) > scr->workspace_name_data->timeout) {
226 XUnmapWindow(dpy, scr->workspace_name);
228 if (scr->workspace_name_data) {
229 RReleaseImage(scr->workspace_name_data->back);
230 RReleaseImage(scr->workspace_name_data->text);
231 wfree(scr->workspace_name_data);
233 scr->workspace_name_data = NULL;
235 scr->workspace_name_timer = NULL;
236 } else {
237 RImage *img = RCloneImage(scr->workspace_name_data->back);
238 Pixmap pix;
240 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
242 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
243 scr->workspace_name_data->count * 255 / 10);
245 RConvertImage(scr->rcontext, img, &pix);
247 RReleaseImage(img);
249 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
250 XClearWindow(dpy, scr->workspace_name);
251 XFreePixmap(dpy, pix);
252 XFlush(dpy);
254 scr->workspace_name_data->count--;
258 static void showWorkspaceName(WScreen * scr, int workspace)
260 WorkspaceNameData *data;
261 RXImage *ximg;
262 Pixmap text, mask;
263 int w, h;
264 int px, py;
265 char *name = scr->workspaces[workspace]->name;
266 int len = strlen(name);
267 int x, y;
268 #ifdef USE_XINERAMA
269 int head;
270 WMRect rect;
271 int xx, yy;
272 #endif
274 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2)
275 return;
277 if (scr->workspace_name_timer) {
278 WMDeleteTimerHandler(scr->workspace_name_timer);
279 XUnmapWindow(dpy, scr->workspace_name);
280 XFlush(dpy);
282 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
284 if (scr->workspace_name_data) {
285 RReleaseImage(scr->workspace_name_data->back);
286 RReleaseImage(scr->workspace_name_data->text);
287 wfree(scr->workspace_name_data);
290 data = wmalloc(sizeof(WorkspaceNameData));
291 data->back = NULL;
293 w = WMWidthOfString(scr->workspace_name_font, name, len);
294 h = WMFontHeight(scr->workspace_name_font);
296 #ifdef USE_XINERAMA
297 head = wGetHeadForPointerLocation(scr);
298 rect = wGetRectForHead(scr, head);
299 if (scr->xine_info.count) {
300 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
301 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
303 else {
304 xx = (scr->scr_width - (w + 4)) / 2;
305 yy = (scr->scr_height - (h + 4)) / 2;
307 #endif
309 switch (wPreferences.workspace_name_display_position) {
310 case WD_TOP:
311 #ifdef USE_XINERAMA
312 px = xx;
313 #else
314 px = (scr->scr_width - (w + 4)) / 2;
315 #endif
316 py = WORKSPACE_NAME_DISPLAY_PADDING;
317 break;
318 case WD_BOTTOM:
319 #ifdef USE_XINERAMA
320 px = xx;
321 #else
322 px = (scr->scr_width - (w + 4)) / 2;
323 #endif
324 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
325 break;
326 case WD_TOPLEFT:
327 px = WORKSPACE_NAME_DISPLAY_PADDING;
328 py = WORKSPACE_NAME_DISPLAY_PADDING;
329 break;
330 case WD_TOPRIGHT:
331 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
332 py = WORKSPACE_NAME_DISPLAY_PADDING;
333 break;
334 case WD_BOTTOMLEFT:
335 px = WORKSPACE_NAME_DISPLAY_PADDING;
336 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
337 break;
338 case WD_BOTTOMRIGHT:
339 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
340 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
341 break;
342 case WD_CENTER:
343 default:
344 #ifdef USE_XINERAMA
345 px = xx;
346 py = yy;
347 #else
348 px = (scr->scr_width - (w + 4)) / 2;
349 py = (scr->scr_height - (h + 4)) / 2;
350 #endif
351 break;
353 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
354 XMoveWindow(dpy, scr->workspace_name, px, py);
356 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
357 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
359 /*XSetForeground(dpy, scr->mono_gc, 0);
360 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
362 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
364 for (x = 0; x <= 4; x++)
365 for (y = 0; y <= 4; y++)
366 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
368 XSetForeground(dpy, scr->mono_gc, 1);
369 XSetBackground(dpy, scr->mono_gc, 0);
371 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
373 /*XSetForeground(dpy, scr->mono_gc, 1); */
374 XSetBackground(dpy, scr->mono_gc, 1);
376 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
378 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
380 #ifdef USE_XSHAPE
381 if (w_global.xext.shape.supported)
382 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
383 #endif
384 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
385 XClearWindow(dpy, scr->workspace_name);
387 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
389 XFreePixmap(dpy, text);
390 XFreePixmap(dpy, mask);
392 if (!data->text) {
393 XMapRaised(dpy, scr->workspace_name);
394 XFlush(dpy);
396 goto erro;
399 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
400 if (!ximg)
401 goto erro;
403 XMapRaised(dpy, scr->workspace_name);
404 XFlush(dpy);
406 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
407 RDestroyXImage(scr->rcontext, ximg);
409 if (!data->back) {
410 goto erro;
413 data->count = 10;
415 /* set a timeout for the effect */
416 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
418 scr->workspace_name_data = data;
420 return;
422 erro:
423 if (scr->workspace_name_timer)
424 WMDeleteTimerHandler(scr->workspace_name_timer);
426 if (data->text)
427 RReleaseImage(data->text);
428 if (data->back)
429 RReleaseImage(data->back);
430 wfree(data);
432 scr->workspace_name_data = NULL;
434 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
435 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
438 void wWorkspaceChange(WScreen *scr, int workspace)
440 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
441 return;
443 if (workspace != scr->current_workspace)
444 wWorkspaceForceChange(scr, workspace);
447 void wWorkspaceRelativeChange(WScreen * scr, int amount)
449 int w;
451 /* While the deiconify animation is going on the window is
452 * still "flying" to its final position and we don't want to
453 * change workspace before the animation finishes, otherwise
454 * the window will land in the new workspace */
455 if (w_global.ignore_workspace_change)
456 return;
458 w = scr->current_workspace + amount;
460 if (amount < 0) {
461 if (w >= 0) {
462 wWorkspaceChange(scr, w);
463 } else if (wPreferences.ws_cycle) {
464 wWorkspaceChange(scr, scr->workspace_count + w);
466 } else if (amount > 0) {
467 if (w < scr->workspace_count) {
468 wWorkspaceChange(scr, w);
469 } else if (wPreferences.ws_advance) {
470 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
471 } else if (wPreferences.ws_cycle) {
472 wWorkspaceChange(scr, w % scr->workspace_count);
477 void wWorkspaceForceChange(WScreen * scr, int workspace)
479 WWindow *tmp, *foc = NULL, *foc2 = NULL;
481 if (workspace >= MAX_WORKSPACES || workspace < 0)
482 return;
484 if (wPreferences.enable_workspace_pager && !w_global.process_workspacemap_event)
485 wWorkspaceMapUpdate(scr);
487 SendHelperMessage(scr, 'C', workspace + 1, NULL);
489 if (workspace > scr->workspace_count - 1)
490 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
492 wClipUpdateForWorkspaceChange(scr, workspace);
494 scr->last_workspace = scr->current_workspace;
495 scr->current_workspace = workspace;
497 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
499 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
501 tmp = scr->focused_window;
502 if (tmp != NULL) {
503 WWindow **toUnmap;
504 int toUnmapSize, toUnmapCount;
506 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
507 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
508 foc = tmp;
511 toUnmapSize = 16;
512 toUnmapCount = 0;
513 toUnmap = wmalloc(toUnmapSize * sizeof(WWindow *));
515 /* foc2 = tmp; will fix annoyance with gnome panel
516 * but will create annoyance for every other application
518 while (tmp) {
519 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
520 /* unmap windows not on this workspace */
521 if ((tmp->flags.mapped || tmp->flags.shaded) &&
522 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
523 if (toUnmapCount == toUnmapSize)
525 toUnmapSize *= 2;
526 toUnmap = wrealloc(toUnmap, toUnmapSize * sizeof(WWindow *));
528 toUnmap[toUnmapCount++] = tmp;
530 /* also unmap miniwindows not on this workspace */
531 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
532 tmp->icon && !IS_OMNIPRESENT(tmp)) {
533 XUnmapWindow(dpy, tmp->icon->core->window);
534 tmp->icon->mapped = 0;
536 /* update current workspace of omnipresent windows */
537 if (IS_OMNIPRESENT(tmp)) {
538 WApplication *wapp = wApplicationOf(tmp->main_window);
540 tmp->frame->workspace = workspace;
542 if (wapp) {
543 wapp->last_workspace = workspace;
545 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
546 foc2 = tmp;
549 } else {
550 /* change selected windows' workspace */
551 if (tmp->flags.selected) {
552 wWindowChangeWorkspace(tmp, workspace);
553 if (!tmp->flags.miniaturized && !foc) {
554 foc = tmp;
556 } else {
557 if (!tmp->flags.hidden) {
558 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
559 /* remap windows that are on this workspace */
560 wWindowMap(tmp);
561 if (!foc && !WFLAGP(tmp, no_focusable)) {
562 foc = tmp;
565 /* Also map miniwindow if not omnipresent */
566 if (!wPreferences.sticky_icons &&
567 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
568 tmp->icon->mapped = 1;
569 XMapWindow(dpy, tmp->icon->core->window);
574 tmp = tmp->prev;
577 while (toUnmapCount > 0)
579 wWindowUnmap(toUnmap[--toUnmapCount]);
581 wfree(toUnmap);
583 /* Gobble up events unleashed by our mapping & unmapping.
584 * These may trigger various grab-initiated focus &
585 * crossing events. However, we don't care about them,
586 * and ignore their focus implications altogether to avoid
587 * flicker.
589 scr->flags.ignore_focus_events = 1;
590 ProcessPendingEvents();
591 scr->flags.ignore_focus_events = 0;
593 if (!foc)
594 foc = foc2;
597 * Check that the window we want to focus still exists, because the application owning it
598 * could decide to unmap/destroy it in response to unmap any of its other window following
599 * the workspace change, this happening during our 'ProcessPendingEvents' loop.
601 if (foc != NULL) {
602 WWindow *parse;
603 Bool found;
605 found = False;
606 for (parse = scr->focused_window; parse != NULL; parse = parse->prev) {
607 if (parse == foc) {
608 found = True;
609 break;
612 if (!found)
613 foc = NULL;
616 if (scr->focused_window->flags.mapped && !foc) {
617 foc = scr->focused_window;
619 if (wPreferences.focus_mode == WKF_CLICK) {
620 wSetFocusTo(scr, foc);
621 } else {
622 unsigned int mask;
623 int foo;
624 Window bar, win;
625 WWindow *tmp;
627 tmp = NULL;
628 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
629 tmp = wWindowFor(win);
632 /* If there's a window under the pointer, focus it.
633 * (we ate all other focus events above, so it's
634 * certainly not focused). Otherwise focus last
635 * focused, or the root (depending on sloppiness)
637 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
638 wSetFocusTo(scr, foc);
639 } else {
640 wSetFocusTo(scr, tmp);
645 /* We need to always arrange icons when changing workspace, even if
646 * no autoarrange icons, because else the icons in different workspaces
647 * can be superposed.
648 * This can be avoided if appicons are also workspace specific.
650 if (!wPreferences.sticky_icons)
651 wArrangeIcons(scr, False);
653 if (scr->dock)
654 wAppIconPaint(scr->dock->icon_array[0]);
656 if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
657 scr->workspaces[workspace]->clip->auto_raise_lower)) {
658 /* to handle enter notify. This will also */
659 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
660 XMapWindow(dpy, scr->clip_icon->icon->core->window);
662 else if (scr->clip_icon != NULL) {
663 wClipIconPaint(scr->clip_icon);
665 wScreenUpdateUsableArea(scr);
666 wNETWMUpdateDesktop(scr);
667 showWorkspaceName(scr, workspace);
669 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
671 /* XSync(dpy, False); */
674 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
676 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
679 static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
681 /* Parameter not used, but tell the compiler that it is ok */
682 (void) entry;
684 wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
687 static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
689 /* Parameter not used, but tell the compiler that it is ok */
690 (void) entry;
692 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
695 static void newWSCommand(WMenu *menu, WMenuEntry *foo)
697 int ws;
699 /* Parameter not used, but tell the compiler that it is ok */
700 (void) foo;
702 ws = wWorkspaceNew(menu->frame->screen_ptr);
704 /* autochange workspace */
705 if (ws >= 0)
706 wWorkspaceChange(menu->frame->screen_ptr, ws);
709 void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
711 char buf[MAX_WORKSPACENAME_WIDTH + 1];
712 char *tmp;
714 if (workspace >= scr->workspace_count)
715 return;
717 /* trim white spaces */
718 tmp = wtrimspace(name);
720 if (strlen(tmp) == 0) {
721 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
722 } else {
723 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
725 buf[MAX_WORKSPACENAME_WIDTH] = 0;
726 wfree(tmp);
728 /* update workspace */
729 wfree(scr->workspaces[workspace]->name);
730 scr->workspaces[workspace]->name = wstrdup(buf);
732 if (scr->clip_ws_menu) {
733 if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
734 wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
735 scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
736 wMenuRealize(scr->clip_ws_menu);
739 if (scr->workspace_menu) {
740 if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
741 wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
742 scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
743 wMenuRealize(scr->workspace_menu);
747 if (scr->clip_icon)
748 wClipIconPaint(scr->clip_icon);
750 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
753 /* callback for when menu entry is edited */
754 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
756 char *tmp;
758 tmp = entry->text;
759 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
762 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
764 WMenu *wsmenu;
765 WMenuEntry *entry;
767 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
768 if (!wsmenu) {
769 wwarning(_("could not create Workspace menu"));
770 return NULL;
773 /* callback to be called when an entry is edited */
774 wsmenu->on_edit = onMenuEntryEdited;
776 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
777 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
779 entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
780 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
782 return wsmenu;
785 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
787 int i;
788 long ws;
789 char title[MAX_WORKSPACENAME_WIDTH + 1];
790 WMenuEntry *entry;
791 int tmp;
793 if (!menu)
794 return;
796 if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
797 /* new workspace(s) added */
798 i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
799 ws = menu->entry_no - MC_WORKSPACE1;
800 while (i > 0) {
801 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
803 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
804 entry->flags.indicator = 1;
805 entry->flags.editable = 1;
807 i--;
808 ws++;
810 } else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
811 /* removed workspace(s) */
812 for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
813 wMenuRemoveItem(menu, i);
816 for (i = 0; i < scr->workspace_count; i++) {
817 /* workspace shortcut labels */
818 if (i / 10 == scr->current_workspace / 10)
819 menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
820 else
821 menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
823 menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
825 menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
826 wMenuRealize(menu);
828 /* don't let user destroy current workspace */
829 if (scr->current_workspace == scr->workspace_count - 1)
830 wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
831 else
832 wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
834 /* back to last workspace */
835 if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
836 wMenuSetEnabled(menu, MC_LAST_USED, True);
837 else
838 wMenuSetEnabled(menu, MC_LAST_USED, False);
840 tmp = menu->frame->top_width + 5;
841 /* if menu got unreachable, bring it to a visible place */
842 if (menu->frame_x < tmp - (int)menu->frame->core->width)
843 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
845 wMenuPaint(menu);
848 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
850 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
851 int i;
853 make_keys();
855 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
856 parr = WMCreatePLArray(NULL);
857 for (i = 0; i < scr->workspace_count; i++) {
858 pstr = WMCreatePLString(scr->workspaces[i]->name);
859 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
860 WMReleasePropList(pstr);
861 if (!wPreferences.flags.noclip) {
862 pstr = wClipSaveWorkspaceState(scr, i);
863 WMPutInPLDictionary(wks_state, dClip, pstr);
864 WMReleasePropList(pstr);
865 } else if (old_wks_state != NULL) {
866 foo = WMGetFromPLArray(old_wks_state, i);
867 if (foo != NULL) {
868 bar = WMGetFromPLDictionary(foo, dClip);
869 if (bar != NULL)
870 WMPutInPLDictionary(wks_state, dClip, bar);
873 WMAddToPLArray(parr, wks_state);
874 WMReleasePropList(wks_state);
876 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
877 WMReleasePropList(parr);
880 void wWorkspaceRestoreState(WScreen *scr)
882 WMPropList *parr, *pstr, *wks_state, *clip_state;
883 int i, j;
885 make_keys();
887 if (scr->session_state == NULL)
888 return;
890 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
892 if (!parr)
893 return;
895 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
896 wks_state = WMGetFromPLArray(parr, i);
897 if (WMIsPLDictionary(wks_state))
898 pstr = WMGetFromPLDictionary(wks_state, dName);
899 else
900 pstr = wks_state;
902 if (i >= scr->workspace_count)
903 wWorkspaceNew(scr);
905 if (scr->workspace_menu) {
906 wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
907 scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
908 scr->workspace_menu->flags.realized = 0;
911 wfree(scr->workspaces[i]->name);
912 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
913 if (!wPreferences.flags.noclip) {
914 int added_omnipresent_icons = 0;
916 clip_state = WMGetFromPLDictionary(wks_state, dClip);
917 if (scr->workspaces[i]->clip)
918 wDockDestroy(scr->workspaces[i]->clip);
920 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
921 if (i > 0)
922 wDockHideIcons(scr->workspaces[i]->clip);
924 /* We set the global icons here, because scr->workspaces[i]->clip
925 * was not valid in wDockRestoreState().
926 * There we only set icon->omnipresent to know which icons we
927 * need to set here.
929 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
930 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
931 int k;
933 if (!aicon || !aicon->omnipresent)
934 continue;
935 aicon->omnipresent = 0;
936 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
937 continue;
938 if (i == 0)
939 continue;
941 /* Move this appicon from workspace i to workspace 0 */
942 scr->workspaces[i]->clip->icon_array[j] = NULL;
943 scr->workspaces[i]->clip->icon_count--;
945 added_omnipresent_icons++;
946 /* If there are too many omnipresent appicons, we are in trouble */
947 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
948 <= scr->workspaces[0]->clip->max_icons);
949 /* Find first free spot on workspace 0 */
950 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
951 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
952 break;
953 scr->workspaces[0]->clip->icon_array[k] = aicon;
954 aicon->dock = scr->workspaces[0]->clip;
956 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
959 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
963 /* Returns the workspace number for a given workspace name */
964 int wGetWorkspaceNumber(WScreen *scr, const char *value)
966 int w, i;
968 if (sscanf(value, "%i", &w) != 1) {
969 w = -1;
970 for (i = 0; i < scr->workspace_count; i++) {
971 if (strcmp(scr->workspaces[i]->name, value) == 0) {
972 w = i;
973 break;
976 } else {
977 w--;
980 return w;