updated estonian translation
[wmaker-crm.git] / src / workspace.c
blob1dadb037d2dc6c0bfb65159cd6137483d0da001d
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;
135 wspace->width = scr->scr_width;
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 RReleaseImage(scr->workspace_name_data->back);
257 RReleaseImage(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 RReleaseImage(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 RReleaseImage(scr->workspace_name_data->back);
315 RReleaseImage(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 RReleaseImage(data->text);
432 if (data->back)
433 RReleaseImage(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 (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
529 tmp->icon && !IS_OMNIPRESENT(tmp)) {
531 XUnmapWindow(dpy, tmp->icon->core->window);
532 tmp->icon->mapped = 0;
534 /* update current workspace of omnipresent windows */
535 if (IS_OMNIPRESENT(tmp)) {
536 WApplication *wapp = wApplicationOf(tmp->main_window);
538 tmp->frame->workspace = workspace;
540 if (wapp) {
541 wapp->last_workspace = workspace;
543 if (!foc2)
544 foc2 = tmp;
546 } else {
547 /* change selected windows' workspace */
548 if (tmp->flags.selected) {
549 wWindowChangeWorkspace(tmp, workspace);
550 if (!tmp->flags.miniaturized && !foc) {
551 foc = tmp;
553 } else {
554 if (!tmp->flags.hidden) {
555 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
556 /* remap windows that are on this workspace */
557 wWindowMap(tmp);
558 if (!foc && !WFLAGP(tmp, skip_window_list))
559 foc = tmp;
561 /* Also map miniwindow if not omnipresent */
562 if (!wPreferences.sticky_icons &&
563 tmp->flags.miniaturized &&
564 !IS_OMNIPRESENT(tmp) && tmp->icon) {
565 tmp->icon->mapped = 1;
566 XMapWindow(dpy, tmp->icon->core->window);
571 tmp = tmp->prev;
574 if (!foc)
575 foc = foc2;
577 if (scr->focused_window->flags.mapped && !foc) {
578 foc = scr->focused_window;
580 if (wPreferences.focus_mode == WKF_CLICK) {
581 wSetFocusTo(scr, foc);
582 } else {
583 unsigned int mask;
584 int foo;
585 Window bar, win;
586 WWindow *tmp;
588 tmp = NULL;
589 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
590 &foo, &foo, &foo, &foo, &mask)) {
591 tmp = wWindowFor(win);
593 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
594 wSetFocusTo(scr, foc);
595 } else {
596 wSetFocusTo(scr, tmp);
601 /* We need to always arrange icons when changing workspace, even if
602 * no autoarrange icons, because else the icons in different workspaces
603 * can be superposed.
604 * This can be avoided if appicons are also workspace specific.
606 if (!wPreferences.sticky_icons)
607 wArrangeIcons(scr, False);
609 if (scr->dock)
610 wAppIconPaint(scr->dock->icon_array[0]);
612 if (scr->clip_icon) {
613 if (scr->workspaces[workspace]->clip->auto_collapse ||
614 scr->workspaces[workspace]->clip->auto_raise_lower) {
615 /* to handle enter notify. This will also */
616 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
617 XMapWindow(dpy, scr->clip_icon->icon->core->window);
618 } else {
619 wClipIconPaint(scr->clip_icon);
623 showWorkspaceName(scr, workspace);
625 #ifdef GNOME_STUFF
626 wGNOMEUpdateCurrentWorkspaceHint(scr);
627 #endif
628 #ifdef KWM_HINTS
629 wKWMUpdateCurrentWorkspaceHint(scr);
630 #endif
631 /* XSync(dpy, False); */
634 #ifdef VIRTUAL_DESKTOP
635 /* TODO:
636 * 1) Allow border around each window so the scrolling
637 * won't just stop at the border.
638 * 2) Make pager.
641 void wWorkspaceManageEdge(WScreen *scr)
643 int w;
644 int vmask;
645 XSetWindowAttributes attribs;
647 /* puts("wWorkspaceManageEdge()"); */
648 if (wPreferences.vedge_thickness) {
649 initVDesk = True;
650 for (w = 0; w < scr->workspace_count; w++) {
651 /* puts("reset workspace"); */
652 wWorkspaceSetViewPort(scr, w, 0, 0);
655 vmask = CWEventMask|CWOverrideRedirect;
656 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
657 attribs.override_redirect = True;
658 scr->virtual_edge_u =
659 XCreateWindow(dpy, scr->root_win, 0, 0,
660 scr->scr_width, wPreferences.vedge_thickness, 0,
661 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
662 scr->virtual_edge_d =
663 XCreateWindow(dpy, scr->root_win, 0, scr->scr_height-wPreferences.vedge_thickness,
664 scr->scr_width, wPreferences.vedge_thickness, 0,
665 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
666 scr->virtual_edge_l =
667 XCreateWindow(dpy, scr->root_win, 0, 0,
668 wPreferences.vedge_thickness, scr->scr_height, 0,
669 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
670 scr->virtual_edge_r =
671 XCreateWindow(dpy, scr->root_win, scr->scr_width-wPreferences.vedge_thickness, 0,
672 wPreferences.vedge_thickness, scr->scr_height, 0,
673 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
674 XMapWindow(dpy, scr->virtual_edge_u);
675 XMapWindow(dpy, scr->virtual_edge_d);
676 XMapWindow(dpy, scr->virtual_edge_l);
677 XMapWindow(dpy, scr->virtual_edge_r);
678 wWorkspaceRaiseEdge(scr);
682 void wWorkspaceRaiseEdge(WScreen *scr)
684 puts("raise edge");
685 if (wPreferences.vedge_thickness && initVDesk) {
686 XRaiseWindow(dpy, scr->virtual_edge_u);
687 XRaiseWindow(dpy, scr->virtual_edge_d);
688 XRaiseWindow(dpy, scr->virtual_edge_l);
689 XRaiseWindow(dpy, scr->virtual_edge_r);
693 void wWorkspaceLowerEdge(WScreen *scr)
695 puts("lower edge");
696 if (wPreferences.vedge_thickness && initVDesk) {
697 XLowerWindow(dpy, scr->virtual_edge_u);
698 XLowerWindow(dpy, scr->virtual_edge_d);
699 XLowerWindow(dpy, scr->virtual_edge_l);
700 XLowerWindow(dpy, scr->virtual_edge_r);
704 void wWorkspaceResizeViewPort(WScreen *scr, int workspace, int width, int height)
706 scr->workspaces[workspace]->width = WMAX(width,scr->scr_width);
707 scr->workspaces[workspace]->height = WMAX(height,scr->scr_height);
710 void updateWorkspaceGeometry(WScreen *scr, int workspace, int *view_x, int *view_y) {
711 int most_left, most_right, most_top, most_bottom;
712 WWindow *wwin;
714 /* adjust workspace layout */
715 wwin = scr->focused_window;
716 most_right = scr->scr_width;
717 most_bottom = scr->scr_height;
718 most_left = 0;
719 most_top = 0;
720 for(;wwin; wwin = wwin->prev) {
721 if (wwin->frame->workspace == workspace) {
722 if (!wwin->flags.miniaturized
723 && !wwin->flags.hidden) {
724 if (wwin->frame_x < most_left) { /* record positions, should this be cached? */
725 most_left = wwin->frame_x;
727 if ((int)wwin->frame_x + (int)wwin->frame->core->width > most_right) {
728 most_right = wwin->frame_x + wwin->frame->core->width;
730 if (wwin->frame_y < most_top) {
731 most_top = wwin->frame_y;
733 if (wwin->frame_y + wwin->frame->core->height > most_bottom) {
734 most_bottom = wwin->frame_y + wwin->frame->core->height;
740 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
741 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
743 *view_x += -most_left - scr->workspaces[workspace]->view_x;
744 scr->workspaces[workspace]->view_x = -most_left;
746 *view_y += -most_top - scr->workspaces[workspace]->view_y;
747 scr->workspaces[workspace]->view_y = -most_top;
751 typedef struct _delay_configure {
752 WWindow *wwin;
753 int delay_count;
754 } _delay_configure;
756 void _sendConfigureNotify (_delay_configure *delay) {
757 WWindow *wwin;
759 delay->delay_count--;
760 if (!delay->delay_count) {
761 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
762 wWindowSynthConfigureNotify(wwin);
767 Bool wWorkspaceSetViewPort(WScreen *scr, int workspace, int view_x, int view_y)
769 Bool adjust_flag = False;
770 int diff_x, diff_y;
771 static _delay_configure delay_configure = {NULL, 0};
772 WWindow *wwin;
774 /*printf("wWorkspaceSetViewPort %d %d\n", view_x, view_y);*/
776 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
778 if (view_x + scr->scr_width > scr->workspaces[workspace]->width) {
779 /* puts("right edge of vdesk"); */
780 view_x = scr->workspaces[workspace]->width - scr->scr_width;
782 if (view_x < 0) {
783 /* puts("left edge of vdesk"); */
784 view_x = 0;
786 if (view_y + scr->scr_height > scr->workspaces[workspace]->height) {
787 /* puts("right edge of vdesk"); */
788 view_y = scr->workspaces[workspace]->height - scr->scr_height;
790 if (view_y < 0) {
791 /* puts("left edge of vdesk"); */
792 view_y = 0;
795 diff_x = scr->workspaces[workspace]->view_x - view_x;
796 diff_y = scr->workspaces[workspace]->view_y - view_y;
797 if (!diff_x && !diff_y)
798 return False;
800 scr->workspaces[workspace]->view_x = view_x;
801 scr->workspaces[workspace]->view_y = view_y;
804 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
805 if (wwin->frame->workspace == workspace) {
806 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
809 if (1) { /* if delay*/
810 delay_configure.delay_count++;
811 delay_configure.wwin = scr->focused_window;
812 WMAddTimerHandler(200, (WMCallback *)_sendConfigureNotify, &delay_configure);
815 return adjust_flag;
818 void wWorkspaceGetViewPosition(WScreen *scr, int workspace, int *view_x, int *view_y) {
819 if (view_x) *view_x = scr->workspaces[workspace]->view_x;
820 if (view_y) *view_y = scr->workspaces[workspace]->view_y;
823 #endif
825 static void
826 switchWSCommand(WMenu *menu, WMenuEntry *entry)
828 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
833 static void
834 deleteWSCommand(WMenu *menu, WMenuEntry *entry)
836 wWorkspaceDelete(menu->frame->screen_ptr,
837 menu->frame->screen_ptr->workspace_count-1);
842 static void
843 newWSCommand(WMenu *menu, WMenuEntry *foo)
845 int ws;
847 ws = wWorkspaceNew(menu->frame->screen_ptr);
848 /* autochange workspace*/
849 if (ws>=0)
850 wWorkspaceChange(menu->frame->screen_ptr, ws);
854 if (ws<9) {
855 int kcode;
856 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
857 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
858 entry->rtext =
859 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
865 static char*
866 cropline(char *line)
868 char *start, *end;
870 if (strlen(line)==0)
871 return line;
873 start = line;
874 end = &(line[strlen(line)])-1;
875 while (isspace(*line) && *line!=0) line++;
876 while (isspace(*end) && end!=line) {
877 *end=0;
878 end--;
880 return line;
884 void
885 wWorkspaceRename(WScreen *scr, int workspace, char *name)
887 char buf[MAX_WORKSPACENAME_WIDTH+1];
888 char *tmp;
890 if (workspace >= scr->workspace_count)
891 return;
893 /* trim white spaces */
894 tmp = cropline(name);
896 if (strlen(tmp)==0) {
897 sprintf(buf, _("Workspace %i"), workspace+1);
898 } else {
899 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
901 buf[MAX_WORKSPACENAME_WIDTH] = 0;
903 /* update workspace */
904 wfree(scr->workspaces[workspace]->name);
905 scr->workspaces[workspace]->name = wstrdup(buf);
907 if (scr->clip_ws_menu) {
908 if (strcmp(scr->clip_ws_menu->entries[workspace+2]->text, buf)!=0) {
909 wfree(scr->clip_ws_menu->entries[workspace+2]->text);
910 scr->clip_ws_menu->entries[workspace+2]->text = wstrdup(buf);
911 wMenuRealize(scr->clip_ws_menu);
914 if (scr->workspace_menu) {
915 if (strcmp(scr->workspace_menu->entries[workspace+2]->text, buf)!=0) {
916 wfree(scr->workspace_menu->entries[workspace+2]->text);
917 scr->workspace_menu->entries[workspace+2]->text = wstrdup(buf);
918 wMenuRealize(scr->workspace_menu);
922 UpdateSwitchMenuWorkspace(scr, workspace);
924 if (scr->clip_icon)
925 wClipIconPaint(scr->clip_icon);
927 #ifdef GNOME_STUFF
928 wGNOMEUpdateWorkspaceNamesHint(scr);
929 #endif
930 #ifdef KWM_HINTS
931 wKWMUpdateWorkspaceNameHint(scr, workspace);
932 #endif
938 /* callback for when menu entry is edited */
939 static void
940 onMenuEntryEdited(WMenu *menu, WMenuEntry *entry)
942 char *tmp;
944 tmp = entry->text;
945 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
949 WMenu*
950 wWorkspaceMenuMake(WScreen *scr, Bool titled)
952 WMenu *wsmenu;
954 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
955 if (!wsmenu) {
956 wwarning(_("could not create Workspace menu"));
957 return NULL;
960 /* callback to be called when an entry is edited */
961 wsmenu->on_edit = onMenuEntryEdited;
963 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
964 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
966 return wsmenu;
971 void
972 wWorkspaceMenuUpdate(WScreen *scr, WMenu *menu)
974 int i;
975 long ws;
976 char title[MAX_WORKSPACENAME_WIDTH+1];
977 WMenuEntry *entry;
978 int tmp;
980 if (!menu)
981 return;
983 if (menu->entry_no < scr->workspace_count+2) {
984 /* new workspace(s) added */
985 i = scr->workspace_count-(menu->entry_no-2);
986 ws = menu->entry_no - 2;
987 while (i>0) {
988 strcpy(title, scr->workspaces[ws]->name);
990 entry = wMenuAddCallback(menu, title, switchWSCommand, (void*)ws);
991 entry->flags.indicator = 1;
992 entry->flags.editable = 1;
994 i--;
995 ws++;
997 } else if (menu->entry_no > scr->workspace_count+2) {
998 /* removed workspace(s) */
999 for (i = menu->entry_no-1; i >= scr->workspace_count+2; i--) {
1000 wMenuRemoveItem(menu, i);
1003 wMenuRealize(menu);
1005 for (i=0; i<scr->workspace_count; i++) {
1006 menu->entries[i+2]->flags.indicator_on = 0;
1008 menu->entries[scr->current_workspace+2]->flags.indicator_on = 1;
1010 /* don't let user destroy current workspace */
1011 if (scr->current_workspace == scr->workspace_count-1) {
1012 wMenuSetEnabled(menu, 1, False);
1013 } else {
1014 wMenuSetEnabled(menu, 1, True);
1017 tmp = menu->frame->top_width + 5;
1018 /* if menu got unreachable, bring it to a visible place */
1019 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1020 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1022 wMenuPaint(menu);
1026 void
1027 wWorkspaceSaveState(WScreen *scr, proplist_t old_state)
1029 proplist_t parr, pstr;
1030 proplist_t wks_state, old_wks_state;
1031 proplist_t foo, bar;
1032 int i;
1034 make_keys();
1036 old_wks_state = PLGetDictionaryEntry(old_state, dWorkspaces);
1037 parr = PLMakeArrayFromElements(NULL);
1038 for (i=0; i < scr->workspace_count; i++) {
1039 pstr = PLMakeString(scr->workspaces[i]->name);
1040 wks_state = PLMakeDictionaryFromEntries(dName, pstr, NULL);
1041 PLRelease(pstr);
1042 if (!wPreferences.flags.noclip) {
1043 pstr = wClipSaveWorkspaceState(scr, i);
1044 PLInsertDictionaryEntry(wks_state, dClip, pstr);
1045 PLRelease(pstr);
1046 } else if (old_wks_state!=NULL) {
1047 if ((foo = PLGetArrayElement(old_wks_state, i))!=NULL) {
1048 if ((bar = PLGetDictionaryEntry(foo, dClip))!=NULL) {
1049 PLInsertDictionaryEntry(wks_state, dClip, bar);
1053 PLAppendArrayElement(parr, wks_state);
1054 PLRelease(wks_state);
1056 PLInsertDictionaryEntry(scr->session_state, dWorkspaces, parr);
1057 PLRelease(parr);
1061 void
1062 wWorkspaceRestoreState(WScreen *scr)
1064 proplist_t parr, pstr, wks_state;
1065 proplist_t clip_state;
1066 int i, j, wscount;
1068 make_keys();
1070 parr = PLGetDictionaryEntry(scr->session_state, dWorkspaces);
1072 if (!parr)
1073 return;
1075 wscount = scr->workspace_count;
1076 for (i=0; i < WMIN(PLGetNumberOfElements(parr), MAX_WORKSPACES); i++) {
1077 wks_state = PLGetArrayElement(parr, i);
1078 if (PLIsDictionary(wks_state))
1079 pstr = PLGetDictionaryEntry(wks_state, dName);
1080 else
1081 pstr = wks_state;
1082 if (i >= scr->workspace_count)
1083 wWorkspaceNew(scr);
1084 if (scr->workspace_menu) {
1085 wfree(scr->workspace_menu->entries[i+2]->text);
1086 scr->workspace_menu->entries[i+2]->text = wstrdup(PLGetString(pstr));
1087 scr->workspace_menu->flags.realized = 0;
1089 wfree(scr->workspaces[i]->name);
1090 scr->workspaces[i]->name = wstrdup(PLGetString(pstr));
1091 if (!wPreferences.flags.noclip) {
1092 clip_state = PLGetDictionaryEntry(wks_state, dClip);
1093 if (scr->workspaces[i]->clip)
1094 wDockDestroy(scr->workspaces[i]->clip);
1095 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state,
1096 WM_CLIP);
1097 if (i>0)
1098 wDockHideIcons(scr->workspaces[i]->clip);
1100 /* We set the global icons here, because scr->workspaces[i]->clip
1101 * was not valid in wDockRestoreState().
1102 * There we only set icon->omnipresent to know which icons we
1103 * need to set here.
1105 for (j=0; j<scr->workspaces[i]->clip->max_icons; j++) {
1106 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1108 if (aicon && aicon->omnipresent) {
1109 aicon->omnipresent = 0;
1110 wClipMakeIconOmnipresent(aicon, True);
1111 XMapWindow(dpy, aicon->icon->core->window);
1115 #ifdef KWM_HINTS
1116 wKWMUpdateWorkspaceNameHint(scr, i);
1117 #endif
1119 #ifdef GNOME_STUFF
1120 wGNOMEUpdateWorkspaceNamesHint(scr);
1121 #endif