forget something in v edge
[wmaker-crm.git] / src / workspace.c
blob5297c4b4c9e908de688f94d038a88cb1db40aa61
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 GNOME_STUFF
133 wGNOMEUpdateWorkspaceHints(scr);
134 #endif
135 #ifdef KWM_HINTS
136 if (!scr->flags.kwm_syncing_count) {
137 wKWMUpdateWorkspaceCountHint(scr);
138 wKWMUpdateWorkspaceNameHint(scr, scr->workspace_count-1);
140 #ifdef not_used
141 wKWMSetUsableAreaHint(scr, scr->workspace_count-1);
142 #endif
143 #endif
144 XFlush(dpy);
146 return scr->workspace_count-1;
148 return -1;
153 Bool
154 wWorkspaceDelete(WScreen *scr, int workspace)
156 WWindow *tmp;
157 WWorkspace **list;
158 int i, j;
161 if (workspace<=0)
162 return False;
164 /* verify if workspace is in use by some window */
165 tmp = scr->focused_window;
166 while (tmp) {
167 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace==workspace)
168 return False;
169 tmp = tmp->prev;
172 if (!wPreferences.flags.noclip) {
173 wDockDestroy(scr->workspaces[workspace]->clip);
174 scr->workspaces[workspace]->clip = NULL;
177 list = wmalloc(sizeof(WWorkspace*)*(scr->workspace_count-1));
178 j = 0;
179 for (i=0; i<scr->workspace_count; i++) {
180 if (i!=workspace)
181 list[j++] = scr->workspaces[i];
182 else {
183 if (scr->workspaces[i]->name)
184 wfree(scr->workspaces[i]->name);
185 wfree(scr->workspaces[i]);
188 wfree(scr->workspaces);
189 scr->workspaces = list;
191 scr->workspace_count--;
194 /* update menu */
195 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
196 /* clip workspace menu */
197 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
199 /* update also window menu */
200 if (scr->workspace_submenu) {
201 WMenu *menu = scr->workspace_submenu;
203 i = menu->entry_no;
204 while (i>scr->workspace_count)
205 wMenuRemoveItem(menu, --i);
206 wMenuRealize(menu);
208 /* and clip menu */
209 if (scr->clip_submenu) {
210 WMenu *menu = scr->clip_submenu;
212 i = menu->entry_no;
213 while (i>scr->workspace_count)
214 wMenuRemoveItem(menu, --i);
215 wMenuRealize(menu);
218 #ifdef GNOME_STUFF
219 wGNOMEUpdateWorkspaceHints(scr);
220 #endif
221 #ifdef KWM_HINTS
222 wKWMUpdateWorkspaceCountHint(scr);
223 #endif
225 if (scr->current_workspace >= scr->workspace_count)
226 wWorkspaceChange(scr, scr->workspace_count-1);
228 return True;
232 typedef struct WorkspaceNameData {
233 int count;
234 RImage *back;
235 RImage *text;
236 time_t timeout;
237 } WorkspaceNameData;
241 static void
242 hideWorkpaceName(void *data)
244 WScreen *scr = (WScreen*)data;
246 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
247 || time(NULL) > scr->workspace_name_data->timeout) {
248 XUnmapWindow(dpy, scr->workspace_name);
250 if (scr->workspace_name_data) {
251 RDestroyImage(scr->workspace_name_data->back);
252 RDestroyImage(scr->workspace_name_data->text);
253 wfree(scr->workspace_name_data);
255 scr->workspace_name_data = NULL;
257 scr->workspace_name_timer = NULL;
258 } else {
259 RImage *img = RCloneImage(scr->workspace_name_data->back);
260 Pixmap pix;
262 scr->workspace_name_timer =
263 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkpaceName,
264 scr);
266 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
267 scr->workspace_name_data->count*255/10);
269 RConvertImage(scr->rcontext, img, &pix);
271 RDestroyImage(img);
273 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
274 XClearWindow(dpy, scr->workspace_name);
275 XFreePixmap(dpy, pix);
276 XFlush(dpy);
278 scr->workspace_name_data->count--;
284 static void
285 showWorkspaceName(WScreen *scr, int workspace)
287 WorkspaceNameData *data;
288 RXImage *ximg;
289 Pixmap text, mask;
290 int w, h;
291 int px, py;
292 char *name = scr->workspaces[workspace]->name;
293 int len = strlen(name);
294 int x, y;
296 if (wPreferences.workspace_name_display_position == WD_NONE
297 || scr->workspace_count < 2)
298 return;
300 if (scr->workspace_name_timer) {
301 WMDeleteTimerHandler(scr->workspace_name_timer);
302 XUnmapWindow(dpy, scr->workspace_name);
303 XFlush(dpy);
305 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY,
306 hideWorkpaceName, scr);
308 if (scr->workspace_name_data) {
309 RDestroyImage(scr->workspace_name_data->back);
310 RDestroyImage(scr->workspace_name_data->text);
311 wfree(scr->workspace_name_data);
314 data = wmalloc(sizeof(WorkspaceNameData));
316 w = WMWidthOfString(scr->workspace_name_font, name, len);
317 h = WMFontHeight(scr->workspace_name_font);
319 switch (wPreferences.workspace_name_display_position) {
320 case WD_TOP:
321 px = (scr->scr_width - (w+4))/2;
322 py = 0;
323 break;
324 case WD_BOTTOM:
325 px = (scr->scr_width - (w+4))/2;
326 py = scr->scr_height - (h+4);
327 break;
328 case WD_TOPLEFT:
329 px = 0;
330 py = 0;
331 break;
332 case WD_TOPRIGHT:
333 px = scr->scr_width - (w+4);
334 py = 0;
335 break;
336 case WD_BOTTOMLEFT:
337 px = 0;
338 py = scr->scr_height - (h+4);
339 break;
340 case WD_BOTTOMRIGHT:
341 px = scr->scr_width - (w+4);
342 py = scr->scr_height - (h+4);
343 break;
344 case WD_CENTER:
345 default:
346 px = (scr->scr_width - (w+4))/2;
347 py = (scr->scr_height - (h+4))/2;
348 break;
350 XResizeWindow(dpy, scr->workspace_name, w+4, h+4);
351 XMoveWindow(dpy, scr->workspace_name, px, py);
353 text = XCreatePixmap(dpy, scr->w_win, w+4, h+4, scr->w_depth);
354 mask = XCreatePixmap(dpy, scr->w_win, w+4, h+4, 1);
356 XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
357 XFillRectangle(dpy, text, scr->draw_gc, 0, 0, w+4, h+4);
359 XSetForeground(dpy, scr->mono_gc, 0);
360 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);
362 XSetForeground(dpy, scr->mono_gc, 1);
363 for (x = 0; x <= 4; x++) {
364 for (y = 0; y <= 4; y++) {
365 WMDrawString(scr->wmscreen, mask, scr->mono_gc,
366 scr->workspace_name_font, x, y, name, len);
370 XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
371 WMDrawString(scr->wmscreen, text, scr->draw_gc, scr->workspace_name_font,
372 2, 2, scr->workspaces[workspace]->name,
373 strlen(scr->workspaces[workspace]->name));
374 #ifdef SHAPE
375 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask,
376 ShapeSet);
377 #endif
378 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
379 XClearWindow(dpy, scr->workspace_name);
381 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
383 XFreePixmap(dpy, text);
384 XFreePixmap(dpy, mask);
386 if (!data->text) {
387 XMapRaised(dpy, scr->workspace_name);
388 XFlush(dpy);
390 goto erro;
393 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py,
394 data->text->width, data->text->height);
396 if (!ximg || !ximg->image) {
397 goto erro;
400 XMapRaised(dpy, scr->workspace_name);
401 XFlush(dpy);
403 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
404 RDestroyXImage(scr->rcontext, ximg);
406 if (!data->back) {
407 goto erro;
410 data->count = 10;
412 /* set a timeout for the effect */
413 data->timeout = time(NULL) + 2 +
414 (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY*data->count)/1000;
416 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 RDestroyImage(data->text);
427 if (data->back)
428 RDestroyImage(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,
435 hideWorkpaceName, scr);
439 void
440 wWorkspaceChange(WScreen *scr, int workspace)
442 if (scr->flags.startup || scr->flags.startup2) {
443 return;
446 if (workspace != scr->current_workspace) {
447 wWorkspaceForceChange(scr, workspace);
448 } /*else {
449 showWorkspaceName(scr, workspace);
454 void
455 wWorkspaceRelativeChange(WScreen *scr, int amount)
457 int w;
459 w = scr->current_workspace + amount;
461 if (amount < 0) {
463 if (w >= 0)
464 wWorkspaceChange(scr, w);
465 else if (wPreferences.ws_cycle)
466 wWorkspaceChange(scr, scr->workspace_count + w);
468 } else if (amount > 0) {
470 if (w < scr->workspace_count)
471 wWorkspaceChange(scr, w);
472 else if (wPreferences.ws_advance)
473 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES-1));
474 else if (wPreferences.ws_cycle)
475 wWorkspaceChange(scr, w % scr->workspace_count);
481 void
482 wWorkspaceForceChange(WScreen *scr, int workspace)
484 WWindow *tmp, *foc=NULL, *foc2=NULL;
486 if (workspace >= MAX_WORKSPACES || workspace < 0)
487 return;
489 SendHelperMessage(scr, 'C', workspace+1, NULL);
491 if (workspace > scr->workspace_count-1) {
492 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
495 wClipUpdateForWorkspaceChange(scr, workspace);
497 scr->current_workspace = workspace;
499 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
501 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
503 if ((tmp = scr->focused_window)!= NULL) {
504 if ((IS_OMNIPRESENT(tmp) && !WFLAGP(tmp, skip_window_list))
505 || tmp->flags.changing_workspace) {
506 foc = tmp;
509 /* foc2 = tmp; will fix annoyance with gnome panel
510 * but will create annoyance for every other application
513 while (tmp) {
514 if (tmp->frame->workspace!=workspace && !tmp->flags.selected) {
515 /* unmap windows not on this workspace */
516 if ((tmp->flags.mapped||tmp->flags.shaded)
517 && !IS_OMNIPRESENT(tmp)
518 && !tmp->flags.changing_workspace) {
520 wWindowUnmap(tmp);
522 /* also unmap miniwindows not on this workspace */
523 if (tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp)
524 && tmp->icon) {
525 if (!wPreferences.sticky_icons) {
526 XUnmapWindow(dpy, tmp->icon->core->window);
527 tmp->icon->mapped = 0;
529 #if 0
530 else {
531 tmp->icon->mapped = 1;
532 /* Why is this here? -Alfredo */
533 XMapWindow(dpy, tmp->icon->core->window);
535 #endif
537 /* update current workspace of omnipresent windows */
538 if (IS_OMNIPRESENT(tmp)) {
539 WApplication *wapp = wApplicationOf(tmp->main_window);
541 tmp->frame->workspace = workspace;
543 if (wapp) {
544 wapp->last_workspace = workspace;
546 if (!foc2)
547 foc2 = tmp;
549 } else {
550 /* change selected windows' workspace */
551 if (tmp->flags.selected) {
552 wWindowChangeWorkspace(tmp, workspace);
553 if (!tmp->flags.miniaturized && !foc) {
554 foc = tmp;
556 } else {
557 if (!tmp->flags.hidden) {
558 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
559 /* remap windows that are on this workspace */
560 wWindowMap(tmp);
561 if (!foc && !WFLAGP(tmp, skip_window_list))
562 foc = tmp;
564 /* Also map miniwindow if not omnipresent */
565 if (!wPreferences.sticky_icons &&
566 tmp->flags.miniaturized &&
567 !IS_OMNIPRESENT(tmp) && tmp->icon) {
568 tmp->icon->mapped = 1;
569 XMapWindow(dpy, tmp->icon->core->window);
574 tmp = tmp->prev;
577 if (!foc)
578 foc = foc2;
580 if (scr->focused_window->flags.mapped && !foc) {
581 foc = scr->focused_window;
583 if (wPreferences.focus_mode == WKF_CLICK) {
584 wSetFocusTo(scr, foc);
585 } else {
586 unsigned int mask;
587 int foo;
588 Window bar, win;
589 WWindow *tmp;
591 tmp = NULL;
592 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
593 &foo, &foo, &foo, &foo, &mask)) {
594 tmp = wWindowFor(win);
596 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
597 wSetFocusTo(scr, foc);
598 } else {
599 wSetFocusTo(scr, tmp);
604 /* We need to always arrange icons when changing workspace, even if
605 * no autoarrange icons, because else the icons in different workspaces
606 * can be superposed.
607 * This can be avoided if appicons are also workspace specific.
609 if (!wPreferences.sticky_icons)
610 wArrangeIcons(scr, False);
612 if (scr->dock)
613 wAppIconPaint(scr->dock->icon_array[0]);
615 if (scr->clip_icon) {
616 if (scr->workspaces[workspace]->clip->auto_collapse ||
617 scr->workspaces[workspace]->clip->auto_raise_lower) {
618 /* to handle enter notify. This will also */
619 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
620 XMapWindow(dpy, scr->clip_icon->icon->core->window);
621 } else {
622 wClipIconPaint(scr->clip_icon);
626 showWorkspaceName(scr, workspace);
628 #ifdef GNOME_STUFF
629 wGNOMEUpdateCurrentWorkspaceHint(scr);
630 #endif
631 #ifdef KWM_HINTS
632 wKWMUpdateCurrentWorkspaceHint(scr);
633 #endif
634 /* XSync(dpy, False); */
637 #ifdef VIRTUAL_DESKTOP
639 void wWorkspaceManageEdge(WScreen *scr)
641 int w;
642 int vmask;
643 XSetWindowAttributes attribs;
645 puts("wWorkspaceManageEdge()");
646 if (wPreferences.vedge_thickness) {
647 initVDesk = True;
648 for (w = 0; w < scr->workspace_count; w++) {
649 puts("reset workspace");
650 wWorkspaceResizeViewPort(scr, w, wPreferences.vedge_maxwidth, wPreferences.vedge_maxheight);
651 wWorkspaceSetViewPort(scr, w, 0, 0);
654 vmask = CWEventMask|CWOverrideRedirect;
655 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
656 attribs.override_redirect = True;
657 scr->virtual_edge_u =
658 XCreateWindow(dpy, scr->root_win, 0, 0,
659 scr->scr_width, wPreferences.vedge_thickness, 0,
660 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
661 scr->virtual_edge_d =
662 XCreateWindow(dpy, scr->root_win, 0, scr->scr_height-wPreferences.vedge_thickness,
663 scr->scr_width, wPreferences.vedge_thickness, 0,
664 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
665 scr->virtual_edge_l =
666 XCreateWindow(dpy, scr->root_win, 0, 0,
667 wPreferences.vedge_thickness, scr->scr_height, 0,
668 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
669 scr->virtual_edge_r =
670 XCreateWindow(dpy, scr->root_win, scr->scr_width-wPreferences.vedge_thickness, 0,
671 wPreferences.vedge_thickness, scr->scr_height, 0,
672 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
673 XMapWindow(dpy, scr->virtual_edge_u);
674 XMapWindow(dpy, scr->virtual_edge_d);
675 XMapWindow(dpy, scr->virtual_edge_l);
676 XMapWindow(dpy, scr->virtual_edge_r);
677 wWorkspaceRaiseEdge(scr);
681 void wWorkspaceRaiseEdge(WScreen *scr)
683 if (wPreferences.vedge_thickness && initVDesk) {
684 XRaiseWindow(dpy, scr->virtual_edge_u);
685 XRaiseWindow(dpy, scr->virtual_edge_d);
686 XRaiseWindow(dpy, scr->virtual_edge_l);
687 XRaiseWindow(dpy, scr->virtual_edge_r);
691 void wWorkspaceResizeViewPort(WScreen *scr, int workspace, int width, int height)
693 scr->workspaces[workspace]->width = WMAX(width,scr->scr_width);
694 scr->workspaces[workspace]->height = WMAX(height,scr->scr_height);
698 void wWorkspaceAdjustViewPort(WScreen *scr, int workspace, int view_x, int view_y)
701 int diff_x, diff_y;
702 int lay_left, lay_right, lay_top, lay_bottom;
703 WWindow *wwin;
705 lay_left = lay_top = 0;
706 lay_right = scr->scr_width;
707 lay_bottom = scr->scr_height;
709 diff_x = scr->workspaces[workspace]->view_x - view_x;
710 diff_y = scr->workspaces[workspace]->view_y - view_y;
712 if (!diff_x && !diff_y) return;
714 wwin = scr->focused_window;
715 while (wwin) {
716 if (wwin->frame_x < lay_left) { /* record positions */
717 lay_left = wwin->frame_x;
718 } else if (wwin->frame_x + wwin->frame->core->width > lay_right) {
719 lay_right = wwin->frame_x + wwin->frame->core->width;
722 if (wwin->frame_y < lay_top) {
723 lay_top = wwin->frame_y;
724 } else if (wwin->frame_y + wwin->frame->core->height > lay_bottom) {
725 lay_top = wwin->frame_y + wwin->frame->core->height;
729 if (wwin->frame->workspace == workspace) {
730 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
731 wWindowSynthConfigureNotify(wwin);
733 wwin = wwin->prev;
736 if (scr->workspaces[workspace]->view_x < 0) {
737 scr->workspaces[workspace]->width -= view_x;
738 scr->workspaces[workspace]->view_x = 0;
739 } else if (scr->workspaces[workspace]->view_x > scr->workspaces[workspace]->width - scr->scr_width) {
740 scr->workspaces[workspace]->width = scr->workspaces[workspace]->view_x + scr->scr_width;
743 if (scr->workspaces[workspace]->view_x > lay_left) {
744 /* should do lay_left + some very big edge here so users can have their own blank area*/
745 scr->workspaces[workspace]->width -= scr->workspaces[workspace]->view_x - lay_left;
746 scr->workspaces[workspace]->view_x = lay_left;
748 if (scr->workspaces[workspace]->view_x + scr->scr_width > lay_right) {
749 scr->workspaces[workspace]->width -= scr->workspaces[workspace]->view_x + scr->scr_width - lay_right;
752 if (scr->workspaces[workspace]->view_y < 0) {
753 scr->workspaces[workspace]->height -= view_y;
754 scr->workspaces[workspace]->view_y = 0;
755 } else if (scr->workspaces[workspace]->view_y > scr->workspaces[workspace]->height - scr->scr_height) {
756 scr->workspaces[workspace]->height = scr->workspaces[workspace]->view_y + scr->scr_height;
759 if (scr->workspaces[workspace]->view_y > lay_top) {
760 /* should do lay_left + some very big edge here so users can have their own blank area*/
761 scr->workspaces[workspace]->height -= scr->workspaces[workspace]->view_y - lay_top;
762 scr->workspaces[workspace]->view_y = lay_top;
764 if (scr->workspaces[workspace]->view_y + scr->scr_height > lay_bottom) {
765 scr->workspaces[workspace]->height -= scr->workspaces[workspace]->view_y + scr->scr_height - lay_left;
768 scr->workspaces[workspace]->view_x = WMIN(view_x, scr->workspaces[workspace]->width - scr->scr_width);
769 scr->workspaces[workspace]->view_y = WMIN(view_y, scr->workspaces[workspace]->height - scr->scr_height);
773 printf("set view %d %d, %d\n",
774 workspace, scr->workspaces[workspace]->view_x, scr->workspaces[workspace]->view_y);
779 void wWorkspaceSetViewPort(WScreen *scr, int workspace, int view_x, int view_y)
782 if (view_x < 0) view_x = 0;
783 else if (view_x > scr->workspaces[workspace]->width - scr->scr_width)
784 view_x = scr->workspaces[workspace]->width - scr->scr_width;
786 if (view_y < 0) view_y = 0;
787 else if (view_y > scr->workspaces[workspace]->width - scr->scr_height)
788 view_y = scr->workspaces[workspace]->width - scr->scr_height;
790 wWorkspaceAdjustViewPort(scr, workspace, view_x, view_y);
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