cleaning
[wmaker-crm.git] / src / workspace.c
blob99ca3c8906099fbc1179779b9778dd230caca9f4
1 /* workspace.c- Workspace management
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <time.h>
37 #include "WindowMaker.h"
38 #include "wcore.h"
39 #include "framewin.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "funcs.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 #ifdef GNOME_STUFF
50 #include "gnome.h"
51 #endif
52 #ifdef KWM_HINTS
53 #include "kwm.h"
54 #endif
56 #include <proplist.h>
59 extern WPreferences wPreferences;
60 extern XContext wWinContext;
63 static proplist_t dWorkspaces=NULL;
64 static proplist_t dClip, dName;
66 #ifdef VIRTUAL_DESKTOP
67 static BOOL initVDesk = False;
68 #endif
70 static void
71 make_keys()
73 if (dWorkspaces!=NULL)
74 return;
76 dWorkspaces = PLMakeString("Workspaces");
77 dName = PLMakeString("Name");
78 dClip = PLMakeString("Clip");
82 void
83 wWorkspaceMake(WScreen *scr, int count)
85 while (count>0) {
86 wWorkspaceNew(scr);
87 count--;
92 int
93 wWorkspaceNew(WScreen *scr)
95 WWorkspace *wspace, **list;
96 int i;
98 if (scr->workspace_count < MAX_WORKSPACES) {
99 scr->workspace_count++;
101 wspace = wmalloc(sizeof(WWorkspace));
102 wspace->name = NULL;
104 #ifdef KWM_HINTS
105 if (scr->flags.kwm_syncing_count) {
106 wspace->name = wKWMGetWorkspaceName(scr, scr->workspace_count-1);
108 #endif
109 if (!wspace->name) {
110 wspace->name = wmalloc(strlen(_("Workspace %i"))+8);
111 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
115 if (!wPreferences.flags.noclip) {
116 wspace->clip = wDockCreate(scr, WM_CLIP);
117 } else
118 wspace->clip = NULL;
120 list = wmalloc(sizeof(WWorkspace*)*scr->workspace_count);
122 for (i=0; i<scr->workspace_count-1; i++) {
123 list[i] = scr->workspaces[i];
125 list[i] = wspace;
126 if (scr->workspaces)
127 wfree(scr->workspaces);
128 scr->workspaces = list;
130 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
131 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
132 #ifdef VIRTUAL_DESKTOP
133 wspace->view_x = wspace->view_y = 0;
134 wspace->height = scr->scr_height * 2;
135 wspace->width = scr->scr_width * 2;
136 #endif
137 #ifdef GNOME_STUFF
138 wGNOMEUpdateWorkspaceHints(scr);
139 #endif
140 #ifdef KWM_HINTS
141 if (!scr->flags.kwm_syncing_count) {
142 wKWMUpdateWorkspaceCountHint(scr);
143 wKWMUpdateWorkspaceNameHint(scr, scr->workspace_count-1);
145 #ifdef not_used
146 wKWMSetUsableAreaHint(scr, scr->workspace_count-1);
147 #endif
148 #endif
149 XFlush(dpy);
151 return scr->workspace_count-1;
153 return -1;
158 Bool
159 wWorkspaceDelete(WScreen *scr, int workspace)
161 WWindow *tmp;
162 WWorkspace **list;
163 int i, j;
166 if (workspace<=0)
167 return False;
169 /* verify if workspace is in use by some window */
170 tmp = scr->focused_window;
171 while (tmp) {
172 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace==workspace)
173 return False;
174 tmp = tmp->prev;
177 if (!wPreferences.flags.noclip) {
178 wDockDestroy(scr->workspaces[workspace]->clip);
179 scr->workspaces[workspace]->clip = NULL;
182 list = wmalloc(sizeof(WWorkspace*)*(scr->workspace_count-1));
183 j = 0;
184 for (i=0; i<scr->workspace_count; i++) {
185 if (i!=workspace)
186 list[j++] = scr->workspaces[i];
187 else {
188 if (scr->workspaces[i]->name)
189 wfree(scr->workspaces[i]->name);
190 wfree(scr->workspaces[i]);
193 wfree(scr->workspaces);
194 scr->workspaces = list;
196 scr->workspace_count--;
199 /* update menu */
200 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
201 /* clip workspace menu */
202 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
204 /* update also window menu */
205 if (scr->workspace_submenu) {
206 WMenu *menu = scr->workspace_submenu;
208 i = menu->entry_no;
209 while (i>scr->workspace_count)
210 wMenuRemoveItem(menu, --i);
211 wMenuRealize(menu);
213 /* and clip menu */
214 if (scr->clip_submenu) {
215 WMenu *menu = scr->clip_submenu;
217 i = menu->entry_no;
218 while (i>scr->workspace_count)
219 wMenuRemoveItem(menu, --i);
220 wMenuRealize(menu);
223 #ifdef GNOME_STUFF
224 wGNOMEUpdateWorkspaceHints(scr);
225 #endif
226 #ifdef KWM_HINTS
227 wKWMUpdateWorkspaceCountHint(scr);
228 #endif
230 if (scr->current_workspace >= scr->workspace_count)
231 wWorkspaceChange(scr, scr->workspace_count-1);
233 return True;
237 typedef struct WorkspaceNameData {
238 int count;
239 RImage *back;
240 RImage *text;
241 time_t timeout;
242 } WorkspaceNameData;
246 static void
247 hideWorkpaceName(void *data)
249 WScreen *scr = (WScreen*)data;
251 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
252 || time(NULL) > scr->workspace_name_data->timeout) {
253 XUnmapWindow(dpy, scr->workspace_name);
255 if (scr->workspace_name_data) {
256 RDestroyImage(scr->workspace_name_data->back);
257 RDestroyImage(scr->workspace_name_data->text);
258 wfree(scr->workspace_name_data);
260 scr->workspace_name_data = NULL;
262 scr->workspace_name_timer = NULL;
263 } else {
264 RImage *img = RCloneImage(scr->workspace_name_data->back);
265 Pixmap pix;
267 scr->workspace_name_timer =
268 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkpaceName,
269 scr);
271 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
272 scr->workspace_name_data->count*255/10);
274 RConvertImage(scr->rcontext, img, &pix);
276 RDestroyImage(img);
278 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
279 XClearWindow(dpy, scr->workspace_name);
280 XFreePixmap(dpy, pix);
281 XFlush(dpy);
283 scr->workspace_name_data->count--;
289 static void
290 showWorkspaceName(WScreen *scr, int workspace)
292 WorkspaceNameData *data;
293 RXImage *ximg;
294 Pixmap text, mask;
295 int w, h;
296 int px, py;
297 char *name = scr->workspaces[workspace]->name;
298 int len = strlen(name);
299 int x, y;
301 if (wPreferences.workspace_name_display_position == WD_NONE
302 || scr->workspace_count < 2)
303 return;
305 if (scr->workspace_name_timer) {
306 WMDeleteTimerHandler(scr->workspace_name_timer);
307 XUnmapWindow(dpy, scr->workspace_name);
308 XFlush(dpy);
310 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY,
311 hideWorkpaceName, scr);
313 if (scr->workspace_name_data) {
314 RDestroyImage(scr->workspace_name_data->back);
315 RDestroyImage(scr->workspace_name_data->text);
316 wfree(scr->workspace_name_data);
319 data = wmalloc(sizeof(WorkspaceNameData));
321 w = WMWidthOfString(scr->workspace_name_font, name, len);
322 h = WMFontHeight(scr->workspace_name_font);
324 switch (wPreferences.workspace_name_display_position) {
325 case WD_TOP:
326 px = (scr->scr_width - (w+4))/2;
327 py = 0;
328 break;
329 case WD_BOTTOM:
330 px = (scr->scr_width - (w+4))/2;
331 py = scr->scr_height - (h+4);
332 break;
333 case WD_TOPLEFT:
334 px = 0;
335 py = 0;
336 break;
337 case WD_TOPRIGHT:
338 px = scr->scr_width - (w+4);
339 py = 0;
340 break;
341 case WD_BOTTOMLEFT:
342 px = 0;
343 py = scr->scr_height - (h+4);
344 break;
345 case WD_BOTTOMRIGHT:
346 px = scr->scr_width - (w+4);
347 py = scr->scr_height - (h+4);
348 break;
349 case WD_CENTER:
350 default:
351 px = (scr->scr_width - (w+4))/2;
352 py = (scr->scr_height - (h+4))/2;
353 break;
355 XResizeWindow(dpy, scr->workspace_name, w+4, h+4);
356 XMoveWindow(dpy, scr->workspace_name, px, py);
358 text = XCreatePixmap(dpy, scr->w_win, w+4, h+4, scr->w_depth);
359 mask = XCreatePixmap(dpy, scr->w_win, w+4, h+4, 1);
361 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
362 XFillRectangle(dpy, text, scr->draw_gc, 0, 0, w+4, h+4);
364 XSetForeground(dpy, scr->mono_gc, 0);
365 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);
367 XSetForeground(dpy, scr->mono_gc, 1);
368 for (x = 0; x <= 4; x++) {
369 for (y = 0; y <= 4; y++) {
370 WMDrawString(scr->wmscreen, mask, scr->mono_gc,
371 scr->workspace_name_font, x, y, name, len);
375 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
376 WMDrawString(scr->wmscreen, text, scr->draw_gc, scr->workspace_name_font,
377 2, 2, scr->workspaces[workspace]->name,
378 strlen(scr->workspaces[workspace]->name));
379 #ifdef SHAPE
380 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask,
381 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,
399 data->text->width, data->text->height);
401 if (!ximg || !ximg->image) {
402 goto erro;
405 XMapRaised(dpy, scr->workspace_name);
406 XFlush(dpy);
408 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
409 RDestroyXImage(scr->rcontext, ximg);
411 if (!data->back) {
412 goto erro;
415 data->count = 10;
417 /* set a timeout for the effect */
418 data->timeout = time(NULL) + 2 +
419 (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY*data->count)/1000;
421 scr->workspace_name_data = data;
424 return;
426 erro:
427 if (scr->workspace_name_timer)
428 WMDeleteTimerHandler(scr->workspace_name_timer);
430 if (data->text)
431 RDestroyImage(data->text);
432 if (data->back)
433 RDestroyImage(data->back);
434 wfree(data);
436 scr->workspace_name_data = NULL;
438 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
439 10*WORKSPACE_NAME_FADE_DELAY,
440 hideWorkpaceName, scr);
444 void
445 wWorkspaceChange(WScreen *scr, int workspace)
447 if (scr->flags.startup || scr->flags.startup2) {
448 return;
451 if (workspace != scr->current_workspace) {
452 wWorkspaceForceChange(scr, workspace);
453 } /*else {
454 showWorkspaceName(scr, workspace);
459 void
460 wWorkspaceRelativeChange(WScreen *scr, int amount)
462 int w;
464 w = scr->current_workspace + amount;
466 if (amount < 0) {
468 if (w >= 0)
469 wWorkspaceChange(scr, w);
470 else if (wPreferences.ws_cycle)
471 wWorkspaceChange(scr, scr->workspace_count + w);
473 } else if (amount > 0) {
475 if (w < scr->workspace_count)
476 wWorkspaceChange(scr, w);
477 else if (wPreferences.ws_advance)
478 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES-1));
479 else if (wPreferences.ws_cycle)
480 wWorkspaceChange(scr, w % scr->workspace_count);
486 void
487 wWorkspaceForceChange(WScreen *scr, int workspace)
489 WWindow *tmp, *foc=NULL, *foc2=NULL;
491 if (workspace >= MAX_WORKSPACES || workspace < 0)
492 return;
494 SendHelperMessage(scr, 'C', workspace+1, NULL);
496 if (workspace > scr->workspace_count-1) {
497 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
500 wClipUpdateForWorkspaceChange(scr, workspace);
502 scr->current_workspace = workspace;
504 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
506 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
508 if ((tmp = scr->focused_window)!= NULL) {
509 if ((IS_OMNIPRESENT(tmp) && !WFLAGP(tmp, skip_window_list))
510 || tmp->flags.changing_workspace) {
511 foc = tmp;
514 /* foc2 = tmp; will fix annoyance with gnome panel
515 * 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)
523 && !tmp->flags.changing_workspace) {
525 wWindowUnmap(tmp);
527 /* also unmap miniwindows not on this workspace */
528 if (tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp)
529 && tmp->icon) {
530 if (!wPreferences.sticky_icons) {
531 XUnmapWindow(dpy, tmp->icon->core->window);
532 tmp->icon->mapped = 0;
534 #if 0
535 else {
536 tmp->icon->mapped = 1;
537 /* Why is this here? -Alfredo */
538 XMapWindow(dpy, tmp->icon->core->window);
540 #endif
542 /* update current workspace of omnipresent windows */
543 if (IS_OMNIPRESENT(tmp)) {
544 WApplication *wapp = wApplicationOf(tmp->main_window);
546 tmp->frame->workspace = workspace;
548 if (wapp) {
549 wapp->last_workspace = workspace;
551 if (!foc2)
552 foc2 = tmp;
554 } else {
555 /* change selected windows' workspace */
556 if (tmp->flags.selected) {
557 wWindowChangeWorkspace(tmp, workspace);
558 if (!tmp->flags.miniaturized && !foc) {
559 foc = tmp;
561 } else {
562 if (!tmp->flags.hidden) {
563 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
564 /* remap windows that are on this workspace */
565 wWindowMap(tmp);
566 if (!foc && !WFLAGP(tmp, skip_window_list))
567 foc = tmp;
569 /* Also map miniwindow if not omnipresent */
570 if (!wPreferences.sticky_icons &&
571 tmp->flags.miniaturized &&
572 !IS_OMNIPRESENT(tmp) && tmp->icon) {
573 tmp->icon->mapped = 1;
574 XMapWindow(dpy, tmp->icon->core->window);
579 tmp = tmp->prev;
582 if (!foc)
583 foc = foc2;
585 if (scr->focused_window->flags.mapped && !foc) {
586 foc = scr->focused_window;
588 if (wPreferences.focus_mode == WKF_CLICK) {
589 wSetFocusTo(scr, foc);
590 } else {
591 unsigned int mask;
592 int foo;
593 Window bar, win;
594 WWindow *tmp;
596 tmp = NULL;
597 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
598 &foo, &foo, &foo, &foo, &mask)) {
599 tmp = wWindowFor(win);
601 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
602 wSetFocusTo(scr, foc);
603 } else {
604 wSetFocusTo(scr, tmp);
609 /* We need to always arrange icons when changing workspace, even if
610 * no autoarrange icons, because else the icons in different workspaces
611 * can be superposed.
612 * This can be avoided if appicons are also workspace specific.
614 if (!wPreferences.sticky_icons)
615 wArrangeIcons(scr, False);
617 if (scr->dock)
618 wAppIconPaint(scr->dock->icon_array[0]);
620 if (scr->clip_icon) {
621 if (scr->workspaces[workspace]->clip->auto_collapse ||
622 scr->workspaces[workspace]->clip->auto_raise_lower) {
623 /* to handle enter notify. This will also */
624 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
625 XMapWindow(dpy, scr->clip_icon->icon->core->window);
626 } else {
627 wClipIconPaint(scr->clip_icon);
631 showWorkspaceName(scr, workspace);
633 #ifdef GNOME_STUFF
634 wGNOMEUpdateCurrentWorkspaceHint(scr);
635 #endif
636 #ifdef KWM_HINTS
637 wKWMUpdateCurrentWorkspaceHint(scr);
638 #endif
639 /* XSync(dpy, False); */
642 #ifdef VIRTUAL_DESKTOP
643 /* TODO:
644 * 1) Allow border around each window so the scrolling
645 * won't just stop at the border.
646 * 2) Make pager.
649 void wWorkspaceManageEdge(WScreen *scr)
651 int w;
652 int vmask;
653 XSetWindowAttributes attribs;
655 puts("wWorkspaceManageEdge()");
656 if (wPreferences.vedge_thickness) {
657 initVDesk = True;
658 for (w = 0; w < scr->workspace_count; w++) {
659 puts("reset workspace");
660 wWorkspaceSetViewPort(scr, w, 0, 0);
663 vmask = CWEventMask|CWOverrideRedirect;
664 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
665 attribs.override_redirect = True;
666 scr->virtual_edge_u =
667 XCreateWindow(dpy, scr->root_win, 0, 0,
668 scr->scr_width, wPreferences.vedge_thickness, 0,
669 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
670 scr->virtual_edge_d =
671 XCreateWindow(dpy, scr->root_win, 0, scr->scr_height-wPreferences.vedge_thickness,
672 scr->scr_width, wPreferences.vedge_thickness, 0,
673 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
674 scr->virtual_edge_l =
675 XCreateWindow(dpy, scr->root_win, 0, 0,
676 wPreferences.vedge_thickness, scr->scr_height, 0,
677 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
678 scr->virtual_edge_r =
679 XCreateWindow(dpy, scr->root_win, scr->scr_width-wPreferences.vedge_thickness, 0,
680 wPreferences.vedge_thickness, scr->scr_height, 0,
681 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
682 XMapWindow(dpy, scr->virtual_edge_u);
683 XMapWindow(dpy, scr->virtual_edge_d);
684 XMapWindow(dpy, scr->virtual_edge_l);
685 XMapWindow(dpy, scr->virtual_edge_r);
686 wWorkspaceRaiseEdge(scr);
690 void wWorkspaceRaiseEdge(WScreen *scr)
692 if (wPreferences.vedge_thickness && initVDesk) {
693 XRaiseWindow(dpy, scr->virtual_edge_u);
694 XRaiseWindow(dpy, scr->virtual_edge_d);
695 XRaiseWindow(dpy, scr->virtual_edge_l);
696 XRaiseWindow(dpy, scr->virtual_edge_r);
700 void wWorkspaceResizeViewPort(WScreen *scr, int workspace, int width, int height)
702 scr->workspaces[workspace]->width = WMAX(width,scr->scr_width);
703 scr->workspaces[workspace]->height = WMAX(height,scr->scr_height);
706 void updateWorkspaceGeometry(WScreen *scr, int workspace, int *view_x, int *view_y) {
707 int most_left, most_right, most_top, most_bottom;
708 WWindow *wwin;
710 /* adjust workspace layout */
711 wwin = scr->focused_window;
712 most_right = scr->scr_width;
713 most_bottom = scr->scr_height;
714 most_left = 0;
715 most_top = 0;
716 for(;wwin; wwin = wwin->prev) {
717 if (wwin->frame->workspace == workspace) {
718 if (!wwin->flags.miniaturized
719 && !wwin->flags.hidden) {
720 if (wwin->frame_x < most_left) { /* record positions, should this be cached? */
721 most_left = wwin->frame_x;
723 if ((int)wwin->frame_x + (int)wwin->frame->core->width > most_right) {
724 most_right = wwin->frame_x + wwin->frame->core->width;
726 if (wwin->frame_y < most_top) {
727 most_top = wwin->frame_y;
729 if (wwin->frame_y + wwin->frame->core->height > most_bottom) {
730 most_bottom = wwin->frame_y + wwin->frame->core->height;
736 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
737 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
739 *view_x += -most_left - scr->workspaces[workspace]->view_x;
740 scr->workspaces[workspace]->view_x = -most_left;
742 *view_y += -most_top - scr->workspaces[workspace]->view_y;
743 scr->workspaces[workspace]->view_y = -most_top;
747 Bool wWorkspaceSetViewPort(WScreen *scr, int workspace, int view_x, int view_y)
749 Bool adjust_flag = False;
750 int diff_x, diff_y;
751 WWindow *wwin;
753 printf("wWorkspaceSetViewPort %d %d\n", view_x, view_y);
755 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
757 if (view_x + scr->scr_width > scr->workspaces[workspace]->width) {
758 /* puts("right edge of vdesk"); */
759 view_x = scr->workspaces[workspace]->width - scr->scr_width;
761 if (view_x < 0) {
762 /* puts("left edge of vdesk"); */
763 view_x = 0;
765 if (view_y + scr->scr_height > scr->workspaces[workspace]->height) {
766 /* puts("right edge of vdesk"); */
767 view_y = scr->workspaces[workspace]->height - scr->scr_height;
769 if (view_y < 0) {
770 /* puts("left edge of vdesk"); */
771 view_y = 0;
774 diff_x = scr->workspaces[workspace]->view_x - view_x;
775 diff_y = scr->workspaces[workspace]->view_y - view_y;
776 if (!diff_x && !diff_y)
777 return False;
779 scr->workspaces[workspace]->view_x = view_x;
780 scr->workspaces[workspace]->view_y = view_y;
783 for( wwin = scr->focused_window; wwin; wwin = wwin->prev) {
784 if (wwin->frame->workspace == workspace) {
785 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
786 wWindowSynthConfigureNotify(wwin);
790 return adjust_flag;
793 void wWorkspaceGetViewPosition(WScreen *scr, int workspace, int *view_x, int *view_y) {
794 if (view_x) *view_x = scr->workspaces[workspace]->view_x;
795 if (view_y) *view_y = scr->workspaces[workspace]->view_y;
798 #endif
800 static void
801 switchWSCommand(WMenu *menu, WMenuEntry *entry)
803 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
808 static void
809 deleteWSCommand(WMenu *menu, WMenuEntry *entry)
811 wWorkspaceDelete(menu->frame->screen_ptr,
812 menu->frame->screen_ptr->workspace_count-1);
817 static void
818 newWSCommand(WMenu *menu, WMenuEntry *foo)
820 int ws;
822 ws = wWorkspaceNew(menu->frame->screen_ptr);
823 /* autochange workspace*/
824 if (ws>=0)
825 wWorkspaceChange(menu->frame->screen_ptr, ws);
829 if (ws<9) {
830 int kcode;
831 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
832 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
833 entry->rtext =
834 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
840 static char*
841 cropline(char *line)
843 char *start, *end;
845 if (strlen(line)==0)
846 return line;
848 start = line;
849 end = &(line[strlen(line)])-1;
850 while (isspace(*line) && *line!=0) line++;
851 while (isspace(*end) && end!=line) {
852 *end=0;
853 end--;
855 return line;
859 void
860 wWorkspaceRename(WScreen *scr, int workspace, char *name)
862 char buf[MAX_WORKSPACENAME_WIDTH+1];
863 char *tmp;
865 if (workspace >= scr->workspace_count)
866 return;
868 /* trim white spaces */
869 tmp = cropline(name);
871 if (strlen(tmp)==0) {
872 sprintf(buf, _("Workspace %i"), workspace+1);
873 } else {
874 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
876 buf[MAX_WORKSPACENAME_WIDTH] = 0;
878 /* update workspace */
879 wfree(scr->workspaces[workspace]->name);
880 scr->workspaces[workspace]->name = wstrdup(buf);
882 if (scr->clip_ws_menu) {
883 if (strcmp(scr->clip_ws_menu->entries[workspace+2]->text, buf)!=0) {
884 wfree(scr->clip_ws_menu->entries[workspace+2]->text);
885 scr->clip_ws_menu->entries[workspace+2]->text = wstrdup(buf);
886 wMenuRealize(scr->clip_ws_menu);
889 if (scr->workspace_menu) {
890 if (strcmp(scr->workspace_menu->entries[workspace+2]->text, buf)!=0) {
891 wfree(scr->workspace_menu->entries[workspace+2]->text);
892 scr->workspace_menu->entries[workspace+2]->text = wstrdup(buf);
893 wMenuRealize(scr->workspace_menu);
897 UpdateSwitchMenuWorkspace(scr, workspace);
899 if (scr->clip_icon)
900 wClipIconPaint(scr->clip_icon);
902 #ifdef GNOME_STUFF
903 wGNOMEUpdateWorkspaceNamesHint(scr);
904 #endif
905 #ifdef KWM_HINTS
906 wKWMUpdateWorkspaceNameHint(scr, workspace);
907 #endif
913 /* callback for when menu entry is edited */
914 static void
915 onMenuEntryEdited(WMenu *menu, WMenuEntry *entry)
917 char *tmp;
919 tmp = entry->text;
920 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
924 WMenu*
925 wWorkspaceMenuMake(WScreen *scr, Bool titled)
927 WMenu *wsmenu;
929 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
930 if (!wsmenu) {
931 wwarning(_("could not create Workspace menu"));
932 return NULL;
935 /* callback to be called when an entry is edited */
936 wsmenu->on_edit = onMenuEntryEdited;
938 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
939 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
941 return wsmenu;
946 void
947 wWorkspaceMenuUpdate(WScreen *scr, WMenu *menu)
949 int i;
950 long ws;
951 char title[MAX_WORKSPACENAME_WIDTH+1];
952 WMenuEntry *entry;
953 int tmp;
955 if (!menu)
956 return;
958 if (menu->entry_no < scr->workspace_count+2) {
959 /* new workspace(s) added */
960 i = scr->workspace_count-(menu->entry_no-2);
961 ws = menu->entry_no - 2;
962 while (i>0) {
963 strcpy(title, scr->workspaces[ws]->name);
965 entry = wMenuAddCallback(menu, title, switchWSCommand, (void*)ws);
966 entry->flags.indicator = 1;
967 entry->flags.editable = 1;
969 i--;
970 ws++;
972 } else if (menu->entry_no > scr->workspace_count+2) {
973 /* removed workspace(s) */
974 for (i = menu->entry_no-1; i >= scr->workspace_count+2; i--) {
975 wMenuRemoveItem(menu, i);
978 wMenuRealize(menu);
980 for (i=0; i<scr->workspace_count; i++) {
981 menu->entries[i+2]->flags.indicator_on = 0;
983 menu->entries[scr->current_workspace+2]->flags.indicator_on = 1;
985 /* don't let user destroy current workspace */
986 if (scr->current_workspace == scr->workspace_count-1) {
987 wMenuSetEnabled(menu, 1, False);
988 } else {
989 wMenuSetEnabled(menu, 1, True);
992 tmp = menu->frame->top_width + 5;
993 /* if menu got unreachable, bring it to a visible place */
994 if (menu->frame_x < tmp - (int)menu->frame->core->width)
995 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
997 wMenuPaint(menu);
1001 void
1002 wWorkspaceSaveState(WScreen *scr, proplist_t old_state)
1004 proplist_t parr, pstr;
1005 proplist_t wks_state, old_wks_state;
1006 proplist_t foo, bar;
1007 int i;
1009 make_keys();
1011 old_wks_state = PLGetDictionaryEntry(old_state, dWorkspaces);
1012 parr = PLMakeArrayFromElements(NULL);
1013 for (i=0; i < scr->workspace_count; i++) {
1014 pstr = PLMakeString(scr->workspaces[i]->name);
1015 wks_state = PLMakeDictionaryFromEntries(dName, pstr, NULL);
1016 PLRelease(pstr);
1017 if (!wPreferences.flags.noclip) {
1018 pstr = wClipSaveWorkspaceState(scr, i);
1019 PLInsertDictionaryEntry(wks_state, dClip, pstr);
1020 PLRelease(pstr);
1021 } else if (old_wks_state!=NULL) {
1022 if ((foo = PLGetArrayElement(old_wks_state, i))!=NULL) {
1023 if ((bar = PLGetDictionaryEntry(foo, dClip))!=NULL) {
1024 PLInsertDictionaryEntry(wks_state, dClip, bar);
1028 PLAppendArrayElement(parr, wks_state);
1029 PLRelease(wks_state);
1031 PLInsertDictionaryEntry(scr->session_state, dWorkspaces, parr);
1032 PLRelease(parr);
1036 void
1037 wWorkspaceRestoreState(WScreen *scr)
1039 proplist_t parr, pstr, wks_state;
1040 proplist_t clip_state;
1041 int i, j, wscount;
1043 make_keys();
1045 parr = PLGetDictionaryEntry(scr->session_state, dWorkspaces);
1047 if (!parr)
1048 return;
1050 wscount = scr->workspace_count;
1051 for (i=0; i < WMIN(PLGetNumberOfElements(parr), MAX_WORKSPACES); i++) {
1052 wks_state = PLGetArrayElement(parr, i);
1053 if (PLIsDictionary(wks_state))
1054 pstr = PLGetDictionaryEntry(wks_state, dName);
1055 else
1056 pstr = wks_state;
1057 if (i >= scr->workspace_count)
1058 wWorkspaceNew(scr);
1059 if (scr->workspace_menu) {
1060 wfree(scr->workspace_menu->entries[i+2]->text);
1061 scr->workspace_menu->entries[i+2]->text = wstrdup(PLGetString(pstr));
1062 scr->workspace_menu->flags.realized = 0;
1064 wfree(scr->workspaces[i]->name);
1065 scr->workspaces[i]->name = wstrdup(PLGetString(pstr));
1066 if (!wPreferences.flags.noclip) {
1067 clip_state = PLGetDictionaryEntry(wks_state, dClip);
1068 if (scr->workspaces[i]->clip)
1069 wDockDestroy(scr->workspaces[i]->clip);
1070 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state,
1071 WM_CLIP);
1072 if (i>0)
1073 wDockHideIcons(scr->workspaces[i]->clip);
1075 /* We set the global icons here, because scr->workspaces[i]->clip
1076 * was not valid in wDockRestoreState().
1077 * There we only set icon->omnipresent to know which icons we
1078 * need to set here.
1080 for (j=0; j<scr->workspaces[i]->clip->max_icons; j++) {
1081 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1083 if (aicon && aicon->omnipresent) {
1084 aicon->omnipresent = 0;
1085 wClipMakeIconOmnipresent(aicon, True);
1086 XMapWindow(dpy, aicon->icon->core->window);
1090 #ifdef KWM_HINTS
1091 wKWMUpdateWorkspaceNameHint(scr, i);
1092 #endif
1094 #ifdef GNOME_STUFF
1095 wGNOMEUpdateWorkspaceNamesHint(scr);
1096 #endif