758c09dc1312899957c96156a8d74308559a3092
[wmaker-crm.git] / src / switchpanel.c
blob758c09dc1312899957c96156a8d74308559a3092
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2004 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
28 #include "WindowMaker.h"
29 #include "screen.h"
30 #include "framewin.h"
31 #include "icon.h"
32 #include "window.h"
33 #include "defaults.h"
34 #include "switchpanel.h"
35 #include "misc.h"
36 #include "xinerama.h"
38 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
40 #ifdef SHAPE
41 #include <X11/extensions/shape.h>
43 extern Bool wShapeSupported;
44 #endif
46 struct SwitchPanel {
47 WScreen *scr;
48 WMWindow *win;
49 WMFrame *iconBox;
51 WMArray *icons;
52 WMArray *images;
53 WMArray *windows;
54 WMArray *flags;
55 RImage *bg;
56 int current;
57 int firstVisible;
58 int visibleCount;
60 WMLabel *label;
62 RImage *tileTmp;
63 RImage *tile;
65 WMFont *font;
66 WMColor *white;
69 extern WPreferences wPreferences;
71 #define BORDER_SPACE 10
72 #define ICON_SIZE 48
73 #define ICON_TILE_SIZE 64
74 #define LABEL_HEIGHT 25
75 #define SCREEN_BORDER_SPACING 2*20
76 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
78 #define ICON_SELECTED (1<<1)
79 #define ICON_DIM (1<<2)
81 static int canReceiveFocus(WWindow *wwin)
83 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
84 return 0;
86 if (wPreferences.cycle_active_head_only &&
87 wGetHeadForWindow(wwin) != wGetHeadForPointerLocation(wwin->screen_ptr))
88 return 0;
90 if (WFLAGP(wwin, no_focusable))
91 return 0;
93 if (!wwin->flags.mapped) {
94 if (!wwin->flags.shaded && !wwin->flags.miniaturized && !wwin->flags.hidden)
95 return 0;
96 else
97 return -1;
100 return 1;
103 static Bool sameWindowClass(WWindow *wwin, WWindow *curwin)
105 if (!wwin->wm_class || !curwin->wm_class)
106 return False;
107 if (strcmp(wwin->wm_class, curwin->wm_class))
108 return False;
110 return True;
113 static void changeImage(WSwitchPanel *panel, int idecks, int selected, Bool dim, Bool force)
115 WMFrame *icon = WMGetFromArray(panel->icons, idecks);
116 RImage *image = WMGetFromArray(panel->images, idecks);
117 char flags = (char) (uintptr_t) WMGetFromArray(panel->flags, idecks);
118 char desired = 0;
120 if (selected)
121 desired |= ICON_SELECTED;
122 if (dim)
123 desired |= ICON_DIM;
125 if (flags == desired && !force)
126 return;
128 WMReplaceInArray(panel->flags, idecks, (void *) (uintptr_t) desired);
130 if (!panel->bg && !panel->tile && !selected)
131 WMSetFrameRelief(icon, WRFlat);
133 if (image && icon) {
134 RImage *back;
135 int opaq = (dim) ? 75 : 255;
136 RImage *tile;
137 WMPoint pos;
138 Pixmap p;
140 if (canReceiveFocus(WMGetFromArray(panel->windows, idecks)) < 0)
141 opaq = 50;
143 pos = WMGetViewPosition(WMWidgetView(icon));
144 back = panel->tileTmp;
145 if (panel->bg) {
146 RCopyArea(back, panel->bg,
147 BORDER_SPACE + pos.x - panel->firstVisible * ICON_TILE_SIZE,
148 BORDER_SPACE + pos.y, back->width, back->height, 0, 0);
149 } else {
150 RColor color;
151 WMScreen *wscr = WMWidgetScreen(icon);
152 color.red = 255;
153 color.red = WMRedComponentOfColor(WMGrayColor(wscr)) >> 8;
154 color.green = WMGreenComponentOfColor(WMGrayColor(wscr)) >> 8;
155 color.blue = WMBlueComponentOfColor(WMGrayColor(wscr)) >> 8;
156 RFillImage(back, &color);
159 if (selected) {
160 tile = panel->tile;
161 RCombineArea(back, tile, 0, 0, tile->width, tile->height,
162 (back->width - tile->width) / 2, (back->height - tile->height) / 2);
165 RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
166 (back->width - image->width) / 2, (back->height - image->height) / 2,
167 opaq);
169 RConvertImage(panel->scr->rcontext, back, &p);
170 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
171 XClearWindow(dpy, WMWidgetXID(icon));
172 XFreePixmap(dpy, p);
175 if (!panel->bg && !panel->tile && selected)
176 WMSetFrameRelief(icon, WRSimple);
179 static void addIconForWindow(WSwitchPanel *panel, WMWidget *parent, WWindow *wwin, int x, int y)
181 WMFrame *icon = WMCreateFrame(parent);
182 RImage *image = NULL;
184 WMSetFrameRelief(icon, WRFlat);
185 WMResizeWidget(icon, ICON_TILE_SIZE, ICON_TILE_SIZE);
186 WMMoveWidget(icon, x, y);
188 if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
189 image = RRetainImage(wwin->net_icon_image);
191 /* get_icon_image() includes the default icon image */
192 if (!image)
193 image = get_icon_image(panel->scr, wwin->wm_instance, wwin->wm_class, ICON_TILE_SIZE);
195 /* We must resize the icon size (~64) to the switchpanel icon size (~48) */
196 image = wIconValidateIconSize(image, ICON_SIZE);
198 WMAddToArray(panel->images, image);
199 WMAddToArray(panel->icons, icon);
202 static void scrollIcons(WSwitchPanel *panel, int delta)
204 int nfirst = panel->firstVisible + delta;
205 int i;
206 int count = WMGetArrayItemCount(panel->windows);
207 Bool dim;
209 if (count <= panel->visibleCount)
210 return;
212 if (nfirst < 0)
213 nfirst = 0;
214 else if (nfirst >= count - panel->visibleCount)
215 nfirst = count - panel->visibleCount;
217 if (nfirst == panel->firstVisible)
218 return;
220 WMMoveWidget(panel->iconBox, -nfirst * ICON_TILE_SIZE, 0);
222 panel->firstVisible = nfirst;
224 for (i = panel->firstVisible; i < panel->firstVisible + panel->visibleCount; i++) {
225 if (i == panel->current)
226 continue;
227 dim = ((char) (uintptr_t) WMGetFromArray(panel->flags, i) & ICON_DIM);
228 changeImage(panel, i, 0, dim, True);
233 * 0 1 2
234 * 3 4 5
235 * 6 7 8
237 static RImage *assemblePuzzleImage(RImage **images, int width, int height)
239 RImage *img = RCreateImage(width, height, 1);
240 RImage *tmp;
241 int tw, th;
242 RColor color;
243 if (!img)
244 return NULL;
246 color.red = 0;
247 color.green = 0;
248 color.blue = 0;
249 color.alpha = 255;
251 RFillImage(img, &color);
253 tw = width - images[0]->width - images[2]->width;
254 th = height - images[0]->height - images[6]->height;
256 if (tw <= 0 || th <= 0)
257 return NULL;
259 /* top */
260 if (tw > 0) {
261 tmp = RSmoothScaleImage(images[1], tw, images[1]->height);
262 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, 0);
263 RReleaseImage(tmp);
265 /* bottom */
266 if (tw > 0) {
267 tmp = RSmoothScaleImage(images[7], tw, images[7]->height);
268 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[6]->width, height - images[6]->height);
269 RReleaseImage(tmp);
271 /* left */
272 if (th > 0) {
273 tmp = RSmoothScaleImage(images[3], images[3]->width, th);
274 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, 0, images[0]->height);
275 RReleaseImage(tmp);
277 /* right */
278 if (th > 0) {
279 tmp = RSmoothScaleImage(images[5], images[5]->width, th);
280 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, width - images[5]->width, images[2]->height);
281 RReleaseImage(tmp);
283 /* center */
284 if (tw > 0 && th > 0) {
285 tmp = RSmoothScaleImage(images[4], tw, th);
286 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, images[0]->height);
287 RReleaseImage(tmp);
290 /* corners */
291 RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height, 0, 0);
292 RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height, width - images[2]->width, 0);
293 RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height, 0, height - images[6]->height);
294 RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
295 width - images[8]->width, height - images[8]->height);
297 return img;
300 static RImage *createBackImage(int width, int height)
302 return assemblePuzzleImage(wPreferences.swbackImage, width, height);
305 static RImage *getTile(void)
307 RImage *stile;
309 if (!wPreferences.swtileImage)
310 return NULL;
312 stile = RScaleImage(wPreferences.swtileImage, ICON_TILE_SIZE, ICON_TILE_SIZE);
313 if (!stile)
314 return wPreferences.swtileImage;
316 return stile;
319 static void drawTitle(WSwitchPanel *panel, int idecks, char *title)
321 char *ntitle;
322 int width = WMWidgetWidth(panel->win);
323 int x;
325 if (title)
326 ntitle = ShrinkString(panel->font, title, width - 2 * BORDER_SPACE);
327 else
328 ntitle = NULL;
330 if (panel->bg) {
331 if (ntitle) {
332 if (strcmp(ntitle, title) != 0) {
333 x = BORDER_SPACE;
334 } else {
335 int w = WMWidthOfString(panel->font, ntitle, strlen(ntitle));
337 x = BORDER_SPACE + (idecks - panel->firstVisible) * ICON_TILE_SIZE +
338 ICON_TILE_SIZE / 2 - w / 2;
339 if (x < BORDER_SPACE)
340 x = BORDER_SPACE;
341 else if (x + w > width - BORDER_SPACE)
342 x = width - BORDER_SPACE - w;
346 XClearWindow(dpy, WMWidgetXID(panel->win));
347 if (ntitle)
348 WMDrawString(panel->scr->wmscreen,
349 WMWidgetXID(panel->win),
350 panel->white, panel->font,
352 WMWidgetHeight(panel->win) - BORDER_SPACE - LABEL_HEIGHT +
353 WMFontHeight(panel->font) / 2, ntitle, strlen(ntitle));
354 } else {
355 if (ntitle)
356 WMSetLabelText(panel->label, ntitle);
359 if (ntitle)
360 free(ntitle);
363 static WMArray *makeWindowListArray(WWindow *curwin, int include_unmapped, Bool class_only)
365 WMArray *windows = WMCreateArray(10);
366 int fl;
367 WWindow *wwin;
369 for (fl = 0; fl < 2; fl++) {
370 for (wwin = curwin; wwin; wwin = wwin->prev) {
371 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
372 (wwin->flags.mapped || include_unmapped)) {
373 if (class_only)
374 if (!sameWindowClass(wwin, curwin))
375 continue;
377 if (!WFLAGP(wwin, skip_switchpanel))
378 WMAddToArray(windows, wwin);
381 wwin = curwin;
382 /* start over from the beginning of the list */
383 while (wwin->next)
384 wwin = wwin->next;
386 for (wwin = curwin; wwin && wwin != curwin; wwin = wwin->prev) {
387 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
388 (wwin->flags.mapped || include_unmapped)) {
389 if (class_only)
390 if (!sameWindowClass(wwin, curwin))
391 continue;
392 if (!WFLAGP(wwin, skip_switchpanel))
393 WMAddToArray(windows, wwin);
398 return windows;
401 static WMArray *makeWindowFlagsArray(int count)
403 WMArray *flags = WMCreateArray(1);
404 int i;
406 for (i = 0; i < count; i++)
407 WMAddToArray(flags, (char) 0);
409 return flags;
412 WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, Bool class_only)
414 WWindow *wwin;
415 WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
416 WMFrame *viewport;
417 int i, width, height, iconsThatFitCount, count;
418 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
420 panel->scr = scr;
421 panel->windows = makeWindowListArray(curwin, wPreferences.swtileImage != NULL, class_only);
422 count = WMGetArrayItemCount(panel->windows);
423 if (count)
424 panel->flags = makeWindowFlagsArray(count);
426 if (count == 0) {
427 WMFreeArray(panel->windows);
428 wfree(panel);
429 return NULL;
432 width = ICON_TILE_SIZE * count;
433 iconsThatFitCount = count;
435 if (width > rect.size.width) {
436 iconsThatFitCount = (rect.size.width - SCREEN_BORDER_SPACING) / ICON_TILE_SIZE;
437 width = iconsThatFitCount * ICON_TILE_SIZE;
440 panel->visibleCount = iconsThatFitCount;
442 if (!wPreferences.swtileImage)
443 return panel;
445 height = LABEL_HEIGHT + ICON_TILE_SIZE;
447 panel->tileTmp = RCreateImage(ICON_TILE_SIZE, ICON_TILE_SIZE, 1);
448 panel->tile = getTile();
449 if (panel->tile && wPreferences.swbackImage[8])
450 panel->bg = createBackImage(width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
452 if (!panel->tileTmp || !panel->tile) {
453 if (panel->bg)
454 RReleaseImage(panel->bg);
455 panel->bg = NULL;
456 if (panel->tile)
457 RReleaseImage(panel->tile);
458 panel->tile = NULL;
459 if (panel->tileTmp)
460 RReleaseImage(panel->tileTmp);
461 panel->tileTmp = NULL;
464 panel->white = WMWhiteColor(scr->wmscreen);
465 panel->font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
466 panel->icons = WMCreateArray(count);
467 panel->images = WMCreateArray(count);
469 panel->win = WMCreateWindow(scr->wmscreen, "");
471 if (!panel->bg) {
472 WMFrame *frame = WMCreateFrame(panel->win);
473 WMColor *darkGray = WMDarkGrayColor(scr->wmscreen);
474 WMSetFrameRelief(frame, WRSimple);
475 WMSetViewExpandsToParent(WMWidgetView(frame), 0, 0, 0, 0);
477 panel->label = WMCreateLabel(panel->win);
478 WMResizeWidget(panel->label, width, LABEL_HEIGHT);
479 WMMoveWidget(panel->label, BORDER_SPACE, BORDER_SPACE + ICON_TILE_SIZE + 5);
480 WMSetLabelRelief(panel->label, WRSimple);
481 WMSetWidgetBackgroundColor(panel->label, darkGray);
482 WMSetLabelFont(panel->label, panel->font);
483 WMSetLabelTextColor(panel->label, panel->white);
485 WMReleaseColor(darkGray);
486 height += 5;
489 WMResizeWidget(panel->win, width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
491 viewport = WMCreateFrame(panel->win);
492 WMResizeWidget(viewport, width, ICON_TILE_SIZE);
493 WMMoveWidget(viewport, BORDER_SPACE, BORDER_SPACE);
494 WMSetFrameRelief(viewport, WRFlat);
496 panel->iconBox = WMCreateFrame(viewport);
497 WMMoveWidget(panel->iconBox, 0, 0);
498 WMResizeWidget(panel->iconBox, ICON_TILE_SIZE * count, ICON_TILE_SIZE);
499 WMSetFrameRelief(panel->iconBox, WRFlat);
501 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
502 addIconForWindow(panel, panel->iconBox, wwin, i * ICON_TILE_SIZE, 0);
505 WMMapSubwidgets(panel->win);
506 WMRealizeWidget(panel->win);
508 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
509 changeImage(panel, i, 0, False, True);
512 if (panel->bg) {
513 Pixmap pixmap, mask;
515 RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
517 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
519 #ifdef SHAPE
520 if (mask && wShapeSupported)
521 XShapeCombineMask(dpy, WMWidgetXID(panel->win), ShapeBounding, 0, 0, mask, ShapeSet);
522 #endif
523 if (pixmap)
524 XFreePixmap(dpy, pixmap);
526 if (mask)
527 XFreePixmap(dpy, mask);
531 WMPoint center;
532 center = wGetPointToCenterRectInHead(scr, wGetHeadForPointerLocation(scr),
533 width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
534 WMMoveWidget(panel->win, center.x, center.y);
537 panel->current = WMGetFirstInArray(panel->windows, curwin);
538 if (panel->current >= 0)
539 changeImage(panel, panel->current, 1, False, False);
541 WMMapWidget(panel->win);
543 return panel;
546 void wSwitchPanelDestroy(WSwitchPanel *panel)
548 int i;
549 RImage *image;
551 if (panel->win) {
552 Window info_win = panel->scr->info_window;
553 XEvent ev;
554 ev.xclient.type = ClientMessage;
555 ev.xclient.message_type = _XA_WM_IGNORE_FOCUS_EVENTS;
556 ev.xclient.format = 32;
557 ev.xclient.data.l[0] = True;
559 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
560 WMUnmapWidget(panel->win);
562 ev.xclient.data.l[0] = False;
563 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
566 if (panel->images) {
567 WM_ITERATE_ARRAY(panel->images, image, i) {
568 if (image)
569 RReleaseImage(image);
571 WMFreeArray(panel->images);
574 if (panel->win)
575 WMDestroyWidget(panel->win);
577 if (panel->icons)
578 WMFreeArray(panel->icons);
580 if (panel->flags)
581 WMFreeArray(panel->flags);
583 WMFreeArray(panel->windows);
585 if (panel->tile)
586 RReleaseImage(panel->tile);
588 if (panel->tileTmp)
589 RReleaseImage(panel->tileTmp);
591 if (panel->bg)
592 RReleaseImage(panel->bg);
594 if (panel->font)
595 WMReleaseFont(panel->font);
597 if (panel->white)
598 WMReleaseColor(panel->white);
600 wfree(panel);
603 WWindow *wSwitchPanelSelectNext(WSwitchPanel *panel, int back, int ignore_minimized, Bool class_only)
605 WWindow *wwin, *curwin, *tmpwin;
606 int count = WMGetArrayItemCount(panel->windows);
607 int orig = panel->current;
608 int i;
609 Bool dim = False;
611 if (count == 0)
612 return NULL;
614 if (!wPreferences.cycle_ignore_minimized)
615 ignore_minimized = False;
617 if (ignore_minimized && canReceiveFocus(WMGetFromArray(panel->windows, (count + panel->current) % count)) < 0)
618 ignore_minimized = False;
620 curwin = WMGetFromArray(panel->windows, orig);
621 do {
622 do {
623 if (back)
624 panel->current--;
625 else
626 panel->current++;
628 panel->current= (count + panel->current) % count;
629 wwin = WMGetFromArray(panel->windows, panel->current);
631 if (!class_only)
632 break;
633 if (panel->current == orig)
634 break;
635 } while (!sameWindowClass(wwin, curwin));
636 } while (ignore_minimized && panel->current != orig && canReceiveFocus(wwin) < 0);
638 WM_ITERATE_ARRAY(panel->windows, tmpwin, i) {
639 if (i == panel->current)
640 continue;
641 if (!class_only || sameWindowClass(tmpwin, curwin))
642 changeImage(panel, i, 0, False, False);
643 else {
644 if (i == orig)
645 dim = True;
646 changeImage(panel, i, 0, True, False);
651 if (panel->current < panel->firstVisible)
652 scrollIcons(panel, panel->current - panel->firstVisible);
653 else if (panel->current - panel->firstVisible >= panel->visibleCount)
654 scrollIcons(panel, panel->current - panel->firstVisible - panel->visibleCount + 1);
656 if (panel->win) {
657 drawTitle(panel, panel->current, wwin->frame->title);
658 if (panel->current != orig)
659 changeImage(panel, orig, 0, dim, False);
660 changeImage(panel, panel->current, 1, False, False);
663 return wwin;
666 WWindow *wSwitchPanelSelectFirst(WSwitchPanel *panel, int back)
668 WWindow *wwin;
669 int count = WMGetArrayItemCount(panel->windows);
670 char *title;
671 int i;
673 if (count == 0)
674 return NULL;
676 if (back) {
677 panel->current = count - 1;
678 scrollIcons(panel, count);
679 } else {
680 panel->current = 0;
681 scrollIcons(panel, -count);
684 wwin = WMGetFromArray(panel->windows, panel->current);
685 title = wwin->frame->title;
687 if (panel->win) {
688 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
689 changeImage(panel, i, i == panel->current, False, False);
691 drawTitle(panel, panel->current, title);
694 return wwin;
697 WWindow *wSwitchPanelHandleEvent(WSwitchPanel *panel, XEvent *event)
699 WMFrame *icon;
700 int i;
701 int focus = -1;
703 if (!panel->win)
704 return NULL;
706 if (event->type == MotionNotify) {
707 WM_ITERATE_ARRAY(panel->icons, icon, i) {
708 if (WMWidgetXID(icon) == event->xmotion.window) {
709 focus = i;
710 break;
715 if (focus >= 0 && panel->current != focus) {
716 WWindow *wwin;
718 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
719 changeImage(panel, i, i == focus, False, False);
721 panel->current = focus;
723 wwin = WMGetFromArray(panel->windows, focus);
725 drawTitle(panel, panel->current, wwin->frame->title);
727 return wwin;
730 return NULL;
733 Window wSwitchPanelGetWindow(WSwitchPanel *swpanel)
735 if (!swpanel->win)
736 return None;
738 return WMWidgetXID(swpanel->win);