Ignore WM_NORMAL_HINTS resize increment for maximized windows
[wmaker-crm.git] / src / workspace.c
blob640d481d575d33ded6d26c7b871a409f37e15833
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_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 char buf[256];
147 snprintf(buf, sizeof(buf), _("Workspace \"%s\" in use; cannot delete"),
148 scr->workspaces[workspace]->name);
149 wMessageDialog(scr, _("Error"), buf, _("OK"), NULL, NULL);
150 return False;
152 tmp = tmp->prev;
155 if (!wPreferences.flags.noclip) {
156 wDockDestroy(scr->workspaces[workspace]->clip);
157 scr->workspaces[workspace]->clip = NULL;
160 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
161 j = 0;
162 for (i = 0; i < scr->workspace_count; i++) {
163 if (i != workspace) {
164 list[j++] = scr->workspaces[i];
165 } else {
166 if (scr->workspaces[i]->name)
167 wfree(scr->workspaces[i]->name);
168 if (scr->workspaces[i]->map)
169 RReleaseImage(scr->workspaces[i]->map);
170 wfree(scr->workspaces[i]);
173 wfree(scr->workspaces);
174 scr->workspaces = list;
176 scr->workspace_count--;
178 /* update menu */
179 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
180 /* clip workspace menu */
181 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
183 /* update also window menu */
184 if (scr->workspace_submenu) {
185 WMenu *menu = scr->workspace_submenu;
187 i = menu->entry_no;
188 while (i > scr->workspace_count)
189 wMenuRemoveItem(menu, --i);
190 wMenuRealize(menu);
192 /* and clip menu */
193 if (scr->clip_submenu) {
194 WMenu *menu = scr->clip_submenu;
196 i = menu->entry_no;
197 while (i > scr->workspace_count)
198 wMenuRemoveItem(menu, --i);
199 wMenuRealize(menu);
201 wNETWMUpdateDesktop(scr);
202 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
204 if (scr->current_workspace >= scr->workspace_count)
205 wWorkspaceChange(scr, scr->workspace_count - 1);
206 if (scr->last_workspace >= scr->workspace_count)
207 scr->last_workspace = 0;
209 return True;
212 typedef struct WorkspaceNameData {
213 int count;
214 RImage *back;
215 RImage *text;
216 time_t timeout;
217 } WorkspaceNameData;
219 static void hideWorkspaceName(void *data)
221 WScreen *scr = (WScreen *) data;
223 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
224 || time(NULL) > scr->workspace_name_data->timeout) {
225 XUnmapWindow(dpy, scr->workspace_name);
227 if (scr->workspace_name_data) {
228 RReleaseImage(scr->workspace_name_data->back);
229 RReleaseImage(scr->workspace_name_data->text);
230 wfree(scr->workspace_name_data);
232 scr->workspace_name_data = NULL;
234 scr->workspace_name_timer = NULL;
235 } else {
236 RImage *img = RCloneImage(scr->workspace_name_data->back);
237 Pixmap pix;
239 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
241 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
242 scr->workspace_name_data->count * 255 / 10);
244 RConvertImage(scr->rcontext, img, &pix);
246 RReleaseImage(img);
248 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
249 XClearWindow(dpy, scr->workspace_name);
250 XFreePixmap(dpy, pix);
251 XFlush(dpy);
253 scr->workspace_name_data->count--;
257 static void showWorkspaceName(WScreen * scr, int workspace)
259 WorkspaceNameData *data;
260 RXImage *ximg;
261 Pixmap text, mask;
262 int w, h;
263 int px, py;
264 char *name = scr->workspaces[workspace]->name;
265 int len = strlen(name);
266 int x, y;
267 #ifdef USE_XINERAMA
268 int head;
269 WMRect rect;
270 int xx, yy;
271 #endif
273 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2)
274 return;
276 if (scr->workspace_name_timer) {
277 WMDeleteTimerHandler(scr->workspace_name_timer);
278 XUnmapWindow(dpy, scr->workspace_name);
279 XFlush(dpy);
281 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
283 if (scr->workspace_name_data) {
284 RReleaseImage(scr->workspace_name_data->back);
285 RReleaseImage(scr->workspace_name_data->text);
286 wfree(scr->workspace_name_data);
289 data = wmalloc(sizeof(WorkspaceNameData));
290 data->back = NULL;
292 w = WMWidthOfString(scr->workspace_name_font, name, len);
293 h = WMFontHeight(scr->workspace_name_font);
295 #ifdef USE_XINERAMA
296 head = wGetHeadForPointerLocation(scr);
297 rect = wGetRectForHead(scr, head);
298 if (scr->xine_info.count) {
299 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
300 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
302 else {
303 xx = (scr->scr_width - (w + 4)) / 2;
304 yy = (scr->scr_height - (h + 4)) / 2;
306 #endif
308 switch (wPreferences.workspace_name_display_position) {
309 case WD_TOP:
310 #ifdef USE_XINERAMA
311 px = xx;
312 #else
313 px = (scr->scr_width - (w + 4)) / 2;
314 #endif
315 py = WORKSPACE_NAME_DISPLAY_PADDING;
316 break;
317 case WD_BOTTOM:
318 #ifdef USE_XINERAMA
319 px = xx;
320 #else
321 px = (scr->scr_width - (w + 4)) / 2;
322 #endif
323 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
324 break;
325 case WD_TOPLEFT:
326 px = WORKSPACE_NAME_DISPLAY_PADDING;
327 py = WORKSPACE_NAME_DISPLAY_PADDING;
328 break;
329 case WD_TOPRIGHT:
330 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
331 py = WORKSPACE_NAME_DISPLAY_PADDING;
332 break;
333 case WD_BOTTOMLEFT:
334 px = WORKSPACE_NAME_DISPLAY_PADDING;
335 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
336 break;
337 case WD_BOTTOMRIGHT:
338 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
339 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
340 break;
341 case WD_CENTER:
342 default:
343 #ifdef USE_XINERAMA
344 px = xx;
345 py = yy;
346 #else
347 px = (scr->scr_width - (w + 4)) / 2;
348 py = (scr->scr_height - (h + 4)) / 2;
349 #endif
350 break;
352 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
353 XMoveWindow(dpy, scr->workspace_name, px, py);
355 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
356 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
358 /*XSetForeground(dpy, scr->mono_gc, 0);
359 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
361 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
363 for (x = 0; x <= 4; x++)
364 for (y = 0; y <= 4; y++)
365 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
367 XSetForeground(dpy, scr->mono_gc, 1);
368 XSetBackground(dpy, scr->mono_gc, 0);
370 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
372 /*XSetForeground(dpy, scr->mono_gc, 1); */
373 XSetBackground(dpy, scr->mono_gc, 1);
375 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
377 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
379 #ifdef USE_XSHAPE
380 if (w_global.xext.shape.supported)
381 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
382 #endif
383 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
384 XClearWindow(dpy, scr->workspace_name);
386 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
388 XFreePixmap(dpy, text);
389 XFreePixmap(dpy, mask);
391 if (!data->text) {
392 XMapRaised(dpy, scr->workspace_name);
393 XFlush(dpy);
395 goto erro;
398 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
399 if (!ximg)
400 goto erro;
402 XMapRaised(dpy, scr->workspace_name);
403 XFlush(dpy);
405 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
406 RDestroyXImage(scr->rcontext, ximg);
408 if (!data->back) {
409 goto erro;
412 data->count = 10;
414 /* set a timeout for the effect */
415 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
417 scr->workspace_name_data = data;
419 return;
421 erro:
422 if (scr->workspace_name_timer)
423 WMDeleteTimerHandler(scr->workspace_name_timer);
425 if (data->text)
426 RReleaseImage(data->text);
427 if (data->back)
428 RReleaseImage(data->back);
429 wfree(data);
431 scr->workspace_name_data = NULL;
433 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
434 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
437 void wWorkspaceChange(WScreen *scr, int workspace)
439 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
440 return;
442 if (workspace != scr->current_workspace)
443 wWorkspaceForceChange(scr, workspace);
446 void wWorkspaceRelativeChange(WScreen * scr, int amount)
448 int w;
450 /* While the deiconify animation is going on the window is
451 * still "flying" to its final position and we don't want to
452 * change workspace before the animation finishes, otherwise
453 * the window will land in the new workspace */
454 if (w_global.ignore_workspace_change)
455 return;
457 w = scr->current_workspace + amount;
459 if (amount < 0) {
460 if (w >= 0) {
461 wWorkspaceChange(scr, w);
462 } else if (wPreferences.ws_cycle) {
463 wWorkspaceChange(scr, scr->workspace_count + w);
465 } else if (amount > 0) {
466 if (w < scr->workspace_count) {
467 wWorkspaceChange(scr, w);
468 } else if (wPreferences.ws_advance) {
469 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
470 } else if (wPreferences.ws_cycle) {
471 wWorkspaceChange(scr, w % scr->workspace_count);
476 void wWorkspaceForceChange(WScreen * scr, int workspace)
478 WWindow *tmp, *foc = NULL, *foc2 = NULL;
480 if (workspace >= MAX_WORKSPACES || workspace < 0)
481 return;
483 if (wPreferences.enable_workspace_pager && !w_global.process_workspacemap_event)
484 wWorkspaceMapUpdate(scr);
486 SendHelperMessage(scr, 'C', workspace + 1, NULL);
488 if (workspace > scr->workspace_count - 1)
489 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
491 wClipUpdateForWorkspaceChange(scr, workspace);
493 scr->last_workspace = scr->current_workspace;
494 scr->current_workspace = workspace;
496 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
498 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
500 tmp = scr->focused_window;
501 if (tmp != NULL) {
502 WWindow **toUnmap;
503 int toUnmapSize, toUnmapCount;
505 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
506 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
507 foc = tmp;
510 toUnmapSize = 16;
511 toUnmapCount = 0;
512 toUnmap = wmalloc(toUnmapSize * sizeof(WWindow *));
514 /* foc2 = tmp; will fix annoyance with gnome panel
515 * but will create annoyance for every other application
517 while (tmp) {
518 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
519 /* unmap windows not on this workspace */
520 if ((tmp->flags.mapped || tmp->flags.shaded) &&
521 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
522 if (toUnmapCount == toUnmapSize)
524 toUnmapSize *= 2;
525 toUnmap = wrealloc(toUnmap, toUnmapSize * sizeof(WWindow *));
527 toUnmap[toUnmapCount++] = tmp;
529 /* also unmap miniwindows not on this workspace */
530 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
531 tmp->icon && !IS_OMNIPRESENT(tmp)) {
532 XUnmapWindow(dpy, tmp->icon->core->window);
533 tmp->icon->mapped = 0;
535 /* update current workspace of omnipresent windows */
536 if (IS_OMNIPRESENT(tmp)) {
537 WApplication *wapp = wApplicationOf(tmp->main_window);
539 tmp->frame->workspace = workspace;
541 if (wapp) {
542 wapp->last_workspace = workspace;
544 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
545 foc2 = tmp;
548 } else {
549 /* change selected windows' workspace */
550 if (tmp->flags.selected) {
551 wWindowChangeWorkspace(tmp, workspace);
552 if (!tmp->flags.miniaturized && !foc) {
553 foc = tmp;
555 } else {
556 if (!tmp->flags.hidden) {
557 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
558 /* remap windows that are on this workspace */
559 wWindowMap(tmp);
560 if (!foc && !WFLAGP(tmp, no_focusable)) {
561 foc = tmp;
564 /* Also map miniwindow if not omnipresent */
565 if (!wPreferences.sticky_icons &&
566 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
567 tmp->icon->mapped = 1;
568 XMapWindow(dpy, tmp->icon->core->window);
573 tmp = tmp->prev;
576 while (toUnmapCount > 0)
578 wWindowUnmap(toUnmap[--toUnmapCount]);
580 wfree(toUnmap);
582 /* Gobble up events unleashed by our mapping & unmapping.
583 * These may trigger various grab-initiated focus &
584 * crossing events. However, we don't care about them,
585 * and ignore their focus implications altogether to avoid
586 * flicker.
588 scr->flags.ignore_focus_events = 1;
589 ProcessPendingEvents();
590 scr->flags.ignore_focus_events = 0;
592 if (!foc)
593 foc = foc2;
596 * Check that the window we want to focus still exists, because the application owning it
597 * could decide to unmap/destroy it in response to unmap any of its other window following
598 * the workspace change, this happening during our 'ProcessPendingEvents' loop.
600 if (foc != NULL) {
601 WWindow *parse;
602 Bool found;
604 found = False;
605 for (parse = scr->focused_window; parse != NULL; parse = parse->prev) {
606 if (parse == foc) {
607 found = True;
608 break;
611 if (!found)
612 foc = NULL;
615 if (scr->focused_window->flags.mapped && !foc) {
616 foc = scr->focused_window;
618 if (wPreferences.focus_mode == WKF_CLICK) {
619 wSetFocusTo(scr, foc);
620 } else {
621 unsigned int mask;
622 int foo;
623 Window bar, win;
624 WWindow *tmp;
626 tmp = NULL;
627 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
628 tmp = wWindowFor(win);
631 /* If there's a window under the pointer, focus it.
632 * (we ate all other focus events above, so it's
633 * certainly not focused). Otherwise focus last
634 * focused, or the root (depending on sloppiness)
636 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
637 wSetFocusTo(scr, foc);
638 } else {
639 wSetFocusTo(scr, tmp);
644 /* We need to always arrange icons when changing workspace, even if
645 * no autoarrange icons, because else the icons in different workspaces
646 * can be superposed.
647 * This can be avoided if appicons are also workspace specific.
649 if (!wPreferences.sticky_icons)
650 wArrangeIcons(scr, False);
652 if (scr->dock)
653 wAppIconPaint(scr->dock->icon_array[0]);
655 if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
656 scr->workspaces[workspace]->clip->auto_raise_lower)) {
657 /* to handle enter notify. This will also */
658 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
659 XMapWindow(dpy, scr->clip_icon->icon->core->window);
661 else if (scr->clip_icon != NULL) {
662 wClipIconPaint(scr->clip_icon);
664 wScreenUpdateUsableArea(scr);
665 wNETWMUpdateDesktop(scr);
666 showWorkspaceName(scr, workspace);
668 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
670 /* XSync(dpy, False); */
673 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
675 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
678 static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
680 /* Parameter not used, but tell the compiler that it is ok */
681 (void) entry;
683 wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
686 static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
688 /* Parameter not used, but tell the compiler that it is ok */
689 (void) entry;
691 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
694 static void newWSCommand(WMenu *menu, WMenuEntry *foo)
696 int ws;
698 /* Parameter not used, but tell the compiler that it is ok */
699 (void) foo;
701 ws = wWorkspaceNew(menu->frame->screen_ptr);
703 /* autochange workspace */
704 if (ws >= 0)
705 wWorkspaceChange(menu->frame->screen_ptr, ws);
708 void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
710 char buf[MAX_WORKSPACENAME_WIDTH + 1];
711 char *tmp;
713 if (workspace >= scr->workspace_count)
714 return;
716 /* trim white spaces */
717 tmp = wtrimspace(name);
719 if (strlen(tmp) == 0) {
720 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
721 } else {
722 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
724 buf[MAX_WORKSPACENAME_WIDTH] = 0;
725 wfree(tmp);
727 /* update workspace */
728 wfree(scr->workspaces[workspace]->name);
729 scr->workspaces[workspace]->name = wstrdup(buf);
731 if (scr->clip_ws_menu) {
732 if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
733 wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
734 scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
735 wMenuRealize(scr->clip_ws_menu);
738 if (scr->workspace_menu) {
739 if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
740 wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
741 scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
742 wMenuRealize(scr->workspace_menu);
746 if (scr->clip_icon)
747 wClipIconPaint(scr->clip_icon);
749 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
752 /* callback for when menu entry is edited */
753 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
755 char *tmp;
757 tmp = entry->text;
758 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
761 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
763 WMenu *wsmenu;
764 WMenuEntry *entry;
766 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
767 if (!wsmenu) {
768 wwarning(_("could not create Workspace menu"));
769 return NULL;
772 /* callback to be called when an entry is edited */
773 wsmenu->on_edit = onMenuEntryEdited;
775 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
776 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
778 entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
779 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
781 return wsmenu;
784 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
786 int i;
787 long ws;
788 char title[MAX_WORKSPACENAME_WIDTH + 1];
789 WMenuEntry *entry;
790 int tmp;
792 if (!menu)
793 return;
795 if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
796 /* new workspace(s) added */
797 i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
798 ws = menu->entry_no - MC_WORKSPACE1;
799 while (i > 0) {
800 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
802 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
803 entry->flags.indicator = 1;
804 entry->flags.editable = 1;
806 i--;
807 ws++;
809 } else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
810 /* removed workspace(s) */
811 for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
812 wMenuRemoveItem(menu, i);
815 for (i = 0; i < scr->workspace_count; i++) {
816 /* workspace shortcut labels */
817 if (i / 10 == scr->current_workspace / 10)
818 menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
819 else
820 menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
822 menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
824 menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
825 wMenuRealize(menu);
827 /* don't let user destroy current workspace */
828 if (scr->current_workspace == scr->workspace_count - 1)
829 wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
830 else
831 wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
833 /* back to last workspace */
834 if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
835 wMenuSetEnabled(menu, MC_LAST_USED, True);
836 else
837 wMenuSetEnabled(menu, MC_LAST_USED, False);
839 tmp = menu->frame->top_width + 5;
840 /* if menu got unreachable, bring it to a visible place */
841 if (menu->frame_x < tmp - (int)menu->frame->core->width)
842 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
844 wMenuPaint(menu);
847 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
849 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
850 int i;
852 make_keys();
854 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
855 parr = WMCreatePLArray(NULL);
856 for (i = 0; i < scr->workspace_count; i++) {
857 pstr = WMCreatePLString(scr->workspaces[i]->name);
858 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
859 WMReleasePropList(pstr);
860 if (!wPreferences.flags.noclip) {
861 pstr = wClipSaveWorkspaceState(scr, i);
862 WMPutInPLDictionary(wks_state, dClip, pstr);
863 WMReleasePropList(pstr);
864 } else if (old_wks_state != NULL) {
865 foo = WMGetFromPLArray(old_wks_state, i);
866 if (foo != NULL) {
867 bar = WMGetFromPLDictionary(foo, dClip);
868 if (bar != NULL)
869 WMPutInPLDictionary(wks_state, dClip, bar);
872 WMAddToPLArray(parr, wks_state);
873 WMReleasePropList(wks_state);
875 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
876 WMReleasePropList(parr);
879 void wWorkspaceRestoreState(WScreen *scr)
881 WMPropList *parr, *pstr, *wks_state, *clip_state;
882 int i, j;
884 make_keys();
886 if (scr->session_state == NULL)
887 return;
889 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
891 if (!parr)
892 return;
894 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
895 wks_state = WMGetFromPLArray(parr, i);
896 if (WMIsPLDictionary(wks_state))
897 pstr = WMGetFromPLDictionary(wks_state, dName);
898 else
899 pstr = wks_state;
901 if (i >= scr->workspace_count)
902 wWorkspaceNew(scr);
904 if (scr->workspace_menu) {
905 wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
906 scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
907 scr->workspace_menu->flags.realized = 0;
910 wfree(scr->workspaces[i]->name);
911 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
912 if (!wPreferences.flags.noclip) {
913 int added_omnipresent_icons = 0;
915 clip_state = WMGetFromPLDictionary(wks_state, dClip);
916 if (scr->workspaces[i]->clip)
917 wDockDestroy(scr->workspaces[i]->clip);
919 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
920 if (i > 0)
921 wDockHideIcons(scr->workspaces[i]->clip);
923 /* We set the global icons here, because scr->workspaces[i]->clip
924 * was not valid in wDockRestoreState().
925 * There we only set icon->omnipresent to know which icons we
926 * need to set here.
928 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
929 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
930 int k;
932 if (!aicon || !aicon->omnipresent)
933 continue;
934 aicon->omnipresent = 0;
935 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
936 continue;
937 if (i == 0)
938 continue;
940 /* Move this appicon from workspace i to workspace 0 */
941 scr->workspaces[i]->clip->icon_array[j] = NULL;
942 scr->workspaces[i]->clip->icon_count--;
944 added_omnipresent_icons++;
945 /* If there are too many omnipresent appicons, we are in trouble */
946 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
947 <= scr->workspaces[0]->clip->max_icons);
948 /* Find first free spot on workspace 0 */
949 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
950 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
951 break;
952 scr->workspaces[0]->clip->icon_array[k] = aicon;
953 aicon->dock = scr->workspaces[0]->clip;
955 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
958 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
962 /* Returns the workspace number for a given workspace name */
963 int wGetWorkspaceNumber(WScreen *scr, const char *value)
965 int w, i;
967 if (sscanf(value, "%i", &w) != 1) {
968 w = -1;
969 for (i = 0; i < scr->workspace_count; i++) {
970 if (strcmp(scr->workspaces[i]->name, value) == 0) {
971 w = i;
972 break;
975 } else {
976 w--;
979 return w;