Add delay to send configure notify in virtual edge and
[wmaker-crm.git] / src / workspace.c
blobc76a12cebad008fe7ea374aba5094e4c926e6e78
1 /* workspace.c- Workspace management
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 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
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 typedef struct _delay_configure {
748 WWindow *wwin;
749 int delay_count;
750 } _delay_configure;
752 void _sendConfigureNotify (_delay_configure *delay) {
753 WWindow *wwin;
755 delay->delay_count--;
756 if (!delay->delay_count) {
757 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
758 wWindowSynthConfigureNotify(wwin);
763 Bool wWorkspaceSetViewPort(WScreen *scr, int workspace, int view_x, int view_y)
765 Bool adjust_flag = False;
766 int diff_x, diff_y;
767 static _delay_configure delay_configure = {NULL, 0};
768 WWindow *wwin;
770 /*printf("wWorkspaceSetViewPort %d %d\n", view_x, view_y);*/
772 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
774 if (view_x + scr->scr_width > scr->workspaces[workspace]->width) {
775 /* puts("right edge of vdesk"); */
776 view_x = scr->workspaces[workspace]->width - scr->scr_width;
778 if (view_x < 0) {
779 /* puts("left edge of vdesk"); */
780 view_x = 0;
782 if (view_y + scr->scr_height > scr->workspaces[workspace]->height) {
783 /* puts("right edge of vdesk"); */
784 view_y = scr->workspaces[workspace]->height - scr->scr_height;
786 if (view_y < 0) {
787 /* puts("left edge of vdesk"); */
788 view_y = 0;
791 diff_x = scr->workspaces[workspace]->view_x - view_x;
792 diff_y = scr->workspaces[workspace]->view_y - view_y;
793 if (!diff_x && !diff_y)
794 return False;
796 scr->workspaces[workspace]->view_x = view_x;
797 scr->workspaces[workspace]->view_y = view_y;
800 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
801 if (wwin->frame->workspace == workspace) {
802 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
805 if (1) { /* if delay*/
806 delay_configure.delay_count++;
807 delay_configure.wwin = scr->focused_window;
808 WMAddTimerHandler(200, (WMCallback *)_sendConfigureNotify, &delay_configure);
811 return adjust_flag;
814 void wWorkspaceGetViewPosition(WScreen *scr, int workspace, int *view_x, int *view_y) {
815 if (view_x) *view_x = scr->workspaces[workspace]->view_x;
816 if (view_y) *view_y = scr->workspaces[workspace]->view_y;
819 #endif
821 static void
822 switchWSCommand(WMenu *menu, WMenuEntry *entry)
824 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
829 static void
830 deleteWSCommand(WMenu *menu, WMenuEntry *entry)
832 wWorkspaceDelete(menu->frame->screen_ptr,
833 menu->frame->screen_ptr->workspace_count-1);
838 static void
839 newWSCommand(WMenu *menu, WMenuEntry *foo)
841 int ws;
843 ws = wWorkspaceNew(menu->frame->screen_ptr);
844 /* autochange workspace*/
845 if (ws>=0)
846 wWorkspaceChange(menu->frame->screen_ptr, ws);
850 if (ws<9) {
851 int kcode;
852 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
853 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
854 entry->rtext =
855 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
861 static char*
862 cropline(char *line)
864 char *start, *end;
866 if (strlen(line)==0)
867 return line;
869 start = line;
870 end = &(line[strlen(line)])-1;
871 while (isspace(*line) && *line!=0) line++;
872 while (isspace(*end) && end!=line) {
873 *end=0;
874 end--;
876 return line;
880 void
881 wWorkspaceRename(WScreen *scr, int workspace, char *name)
883 char buf[MAX_WORKSPACENAME_WIDTH+1];
884 char *tmp;
886 if (workspace >= scr->workspace_count)
887 return;
889 /* trim white spaces */
890 tmp = cropline(name);
892 if (strlen(tmp)==0) {
893 sprintf(buf, _("Workspace %i"), workspace+1);
894 } else {
895 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
897 buf[MAX_WORKSPACENAME_WIDTH] = 0;
899 /* update workspace */
900 wfree(scr->workspaces[workspace]->name);
901 scr->workspaces[workspace]->name = wstrdup(buf);
903 if (scr->clip_ws_menu) {
904 if (strcmp(scr->clip_ws_menu->entries[workspace+2]->text, buf)!=0) {
905 wfree(scr->clip_ws_menu->entries[workspace+2]->text);
906 scr->clip_ws_menu->entries[workspace+2]->text = wstrdup(buf);
907 wMenuRealize(scr->clip_ws_menu);
910 if (scr->workspace_menu) {
911 if (strcmp(scr->workspace_menu->entries[workspace+2]->text, buf)!=0) {
912 wfree(scr->workspace_menu->entries[workspace+2]->text);
913 scr->workspace_menu->entries[workspace+2]->text = wstrdup(buf);
914 wMenuRealize(scr->workspace_menu);
918 UpdateSwitchMenuWorkspace(scr, workspace);
920 if (scr->clip_icon)
921 wClipIconPaint(scr->clip_icon);
923 #ifdef GNOME_STUFF
924 wGNOMEUpdateWorkspaceNamesHint(scr);
925 #endif
926 #ifdef KWM_HINTS
927 wKWMUpdateWorkspaceNameHint(scr, workspace);
928 #endif
934 /* callback for when menu entry is edited */
935 static void
936 onMenuEntryEdited(WMenu *menu, WMenuEntry *entry)
938 char *tmp;
940 tmp = entry->text;
941 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
945 WMenu*
946 wWorkspaceMenuMake(WScreen *scr, Bool titled)
948 WMenu *wsmenu;
950 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
951 if (!wsmenu) {
952 wwarning(_("could not create Workspace menu"));
953 return NULL;
956 /* callback to be called when an entry is edited */
957 wsmenu->on_edit = onMenuEntryEdited;
959 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
960 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
962 return wsmenu;
967 void
968 wWorkspaceMenuUpdate(WScreen *scr, WMenu *menu)
970 int i;
971 long ws;
972 char title[MAX_WORKSPACENAME_WIDTH+1];
973 WMenuEntry *entry;
974 int tmp;
976 if (!menu)
977 return;
979 if (menu->entry_no < scr->workspace_count+2) {
980 /* new workspace(s) added */
981 i = scr->workspace_count-(menu->entry_no-2);
982 ws = menu->entry_no - 2;
983 while (i>0) {
984 strcpy(title, scr->workspaces[ws]->name);
986 entry = wMenuAddCallback(menu, title, switchWSCommand, (void*)ws);
987 entry->flags.indicator = 1;
988 entry->flags.editable = 1;
990 i--;
991 ws++;
993 } else if (menu->entry_no > scr->workspace_count+2) {
994 /* removed workspace(s) */
995 for (i = menu->entry_no-1; i >= scr->workspace_count+2; i--) {
996 wMenuRemoveItem(menu, i);
999 wMenuRealize(menu);
1001 for (i=0; i<scr->workspace_count; i++) {
1002 menu->entries[i+2]->flags.indicator_on = 0;
1004 menu->entries[scr->current_workspace+2]->flags.indicator_on = 1;
1006 /* don't let user destroy current workspace */
1007 if (scr->current_workspace == scr->workspace_count-1) {
1008 wMenuSetEnabled(menu, 1, False);
1009 } else {
1010 wMenuSetEnabled(menu, 1, True);
1013 tmp = menu->frame->top_width + 5;
1014 /* if menu got unreachable, bring it to a visible place */
1015 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1016 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1018 wMenuPaint(menu);
1022 void
1023 wWorkspaceSaveState(WScreen *scr, proplist_t old_state)
1025 proplist_t parr, pstr;
1026 proplist_t wks_state, old_wks_state;
1027 proplist_t foo, bar;
1028 int i;
1030 make_keys();
1032 old_wks_state = PLGetDictionaryEntry(old_state, dWorkspaces);
1033 parr = PLMakeArrayFromElements(NULL);
1034 for (i=0; i < scr->workspace_count; i++) {
1035 pstr = PLMakeString(scr->workspaces[i]->name);
1036 wks_state = PLMakeDictionaryFromEntries(dName, pstr, NULL);
1037 PLRelease(pstr);
1038 if (!wPreferences.flags.noclip) {
1039 pstr = wClipSaveWorkspaceState(scr, i);
1040 PLInsertDictionaryEntry(wks_state, dClip, pstr);
1041 PLRelease(pstr);
1042 } else if (old_wks_state!=NULL) {
1043 if ((foo = PLGetArrayElement(old_wks_state, i))!=NULL) {
1044 if ((bar = PLGetDictionaryEntry(foo, dClip))!=NULL) {
1045 PLInsertDictionaryEntry(wks_state, dClip, bar);
1049 PLAppendArrayElement(parr, wks_state);
1050 PLRelease(wks_state);
1052 PLInsertDictionaryEntry(scr->session_state, dWorkspaces, parr);
1053 PLRelease(parr);
1057 void
1058 wWorkspaceRestoreState(WScreen *scr)
1060 proplist_t parr, pstr, wks_state;
1061 proplist_t clip_state;
1062 int i, j, wscount;
1064 make_keys();
1066 parr = PLGetDictionaryEntry(scr->session_state, dWorkspaces);
1068 if (!parr)
1069 return;
1071 wscount = scr->workspace_count;
1072 for (i=0; i < WMIN(PLGetNumberOfElements(parr), MAX_WORKSPACES); i++) {
1073 wks_state = PLGetArrayElement(parr, i);
1074 if (PLIsDictionary(wks_state))
1075 pstr = PLGetDictionaryEntry(wks_state, dName);
1076 else
1077 pstr = wks_state;
1078 if (i >= scr->workspace_count)
1079 wWorkspaceNew(scr);
1080 if (scr->workspace_menu) {
1081 wfree(scr->workspace_menu->entries[i+2]->text);
1082 scr->workspace_menu->entries[i+2]->text = wstrdup(PLGetString(pstr));
1083 scr->workspace_menu->flags.realized = 0;
1085 wfree(scr->workspaces[i]->name);
1086 scr->workspaces[i]->name = wstrdup(PLGetString(pstr));
1087 if (!wPreferences.flags.noclip) {
1088 clip_state = PLGetDictionaryEntry(wks_state, dClip);
1089 if (scr->workspaces[i]->clip)
1090 wDockDestroy(scr->workspaces[i]->clip);
1091 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state,
1092 WM_CLIP);
1093 if (i>0)
1094 wDockHideIcons(scr->workspaces[i]->clip);
1096 /* We set the global icons here, because scr->workspaces[i]->clip
1097 * was not valid in wDockRestoreState().
1098 * There we only set icon->omnipresent to know which icons we
1099 * need to set here.
1101 for (j=0; j<scr->workspaces[i]->clip->max_icons; j++) {
1102 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1104 if (aicon && aicon->omnipresent) {
1105 aicon->omnipresent = 0;
1106 wClipMakeIconOmnipresent(aicon, True);
1107 XMapWindow(dpy, aicon->icon->core->window);
1111 #ifdef KWM_HINTS
1112 wKWMUpdateWorkspaceNameHint(scr, i);
1113 #endif
1115 #ifdef GNOME_STUFF
1116 wGNOMEUpdateWorkspaceNamesHint(scr);
1117 #endif