Undim switchpanel icons when selecting windows directly.
[wmaker-crm.git] / src / switchpanel.c
blobcfb003320fa62bacb7313ab44e50803c9d010b47
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 <stdlib.h>
24 #include <string.h>
25 #include <sys/time.h>
27 #include "WindowMaker.h"
28 #include "screen.h"
29 #include "framewin.h"
30 #include "icon.h"
31 #include "window.h"
32 #include "defaults.h"
33 #include "switchpanel.h"
34 #include "misc.h"
35 #include "xinerama.h"
37 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
39 #ifdef SHAPE
40 #include <X11/extensions/shape.h>
42 extern Bool wShapeSupported;
43 #endif
45 struct SwitchPanel {
46 WScreen *scr;
47 WMWindow *win;
48 WMFrame *iconBox;
50 WMArray *icons;
51 WMArray *images;
52 WMArray *windows;
53 RImage *bg;
54 int current;
55 int firstVisible;
56 int visibleCount;
58 WMLabel *label;
60 RImage *tileTmp;
61 RImage *tile;
63 WMFont *font;
64 WMColor *white;
67 extern WPreferences wPreferences;
69 #define BORDER_SPACE 10
70 #define ICON_SIZE 48
71 #define ICON_TILE_SIZE 64
72 #define LABEL_HEIGHT 25
73 #define SCREEN_BORDER_SPACING 2*20
74 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
76 static int canReceiveFocus(WWindow *wwin)
78 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
79 return 0;
81 if (wPreferences.cycle_active_head_only &&
82 wGetHeadForWindow(wwin) != wGetHeadForPointerLocation(wwin->screen_ptr))
83 return 0;
85 if (WFLAGP(wwin, no_focusable))
86 return 0;
88 if (!wwin->flags.mapped) {
89 if (!wwin->flags.shaded && !wwin->flags.miniaturized && !wwin->flags.hidden)
90 return 0;
91 else
92 return -1;
95 return 1;
98 static Bool sameWindowClass(WWindow *wwin, WWindow *curwin)
100 if (!wwin->wm_class || !curwin->wm_class)
101 return False;
102 if (strcmp(wwin->wm_class, curwin->wm_class))
103 return False;
105 return True;
108 static void changeImage(WSwitchPanel *panel, int idecks, int selected, Bool dim)
110 WMFrame *icon = WMGetFromArray(panel->icons, idecks);
111 RImage *image = WMGetFromArray(panel->images, idecks);
113 if (!panel->bg && !panel->tile && !selected)
114 WMSetFrameRelief(icon, WRFlat);
116 if (image && icon) {
117 RImage *back;
118 int opaq = (dim) ? 75 : 255;
119 RImage *tile;
120 WMPoint pos;
121 Pixmap p;
123 if (canReceiveFocus(WMGetFromArray(panel->windows, idecks)) < 0)
124 opaq = 50;
126 pos = WMGetViewPosition(WMWidgetView(icon));
127 back = panel->tileTmp;
128 if (panel->bg) {
129 RCopyArea(back, panel->bg,
130 BORDER_SPACE + pos.x - panel->firstVisible * ICON_TILE_SIZE,
131 BORDER_SPACE + pos.y, back->width, back->height, 0, 0);
132 } else {
133 RColor color;
134 WMScreen *wscr = WMWidgetScreen(icon);
135 color.red = 255;
136 color.red = WMRedComponentOfColor(WMGrayColor(wscr)) >> 8;
137 color.green = WMGreenComponentOfColor(WMGrayColor(wscr)) >> 8;
138 color.blue = WMBlueComponentOfColor(WMGrayColor(wscr)) >> 8;
139 RFillImage(back, &color);
142 if (selected) {
143 tile = panel->tile;
144 RCombineArea(back, tile, 0, 0, tile->width, tile->height,
145 (back->width - tile->width) / 2, (back->height - tile->height) / 2);
148 RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
149 (back->width - image->width) / 2, (back->height - image->height) / 2,
150 opaq);
152 RConvertImage(panel->scr->rcontext, back, &p);
153 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
154 XClearWindow(dpy, WMWidgetXID(icon));
155 XFreePixmap(dpy, p);
158 if (!panel->bg && !panel->tile && selected)
159 WMSetFrameRelief(icon, WRSimple);
162 static void addIconForWindow(WSwitchPanel *panel, WMWidget *parent, WWindow *wwin, int x, int y)
164 WMFrame *icon = WMCreateFrame(parent);
165 RImage *image = NULL;
167 WMSetFrameRelief(icon, WRFlat);
168 WMResizeWidget(icon, ICON_TILE_SIZE, ICON_TILE_SIZE);
169 WMMoveWidget(icon, x, y);
171 if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
172 image = RRetainImage(wwin->net_icon_image);
174 /* get_icon_image() includes the default icon image */
175 if (!image)
176 image = get_icon_image(panel->scr, wwin->wm_instance, wwin->wm_class, ICON_TILE_SIZE);
178 /* We must resize the icon size (~64) to the switchpanel icon size (~48) */
179 image = wIconValidateIconSize(image, ICON_SIZE);
181 WMAddToArray(panel->images, image);
182 WMAddToArray(panel->icons, icon);
185 static void scrollIcons(WSwitchPanel *panel, int delta)
187 int nfirst = panel->firstVisible + delta;
188 int i;
189 int count = WMGetArrayItemCount(panel->windows);
191 if (count <= panel->visibleCount)
192 return;
194 if (nfirst < 0)
195 nfirst = 0;
196 else if (nfirst >= count - panel->visibleCount)
197 nfirst = count - panel->visibleCount;
199 if (nfirst == panel->firstVisible)
200 return;
202 WMMoveWidget(panel->iconBox, -nfirst * ICON_TILE_SIZE, 0);
204 panel->firstVisible = nfirst;
206 for (i = panel->firstVisible; i < panel->firstVisible + panel->visibleCount; i++)
207 changeImage(panel, i, i == panel->current, 0);
211 * 0 1 2
212 * 3 4 5
213 * 6 7 8
215 static RImage *assemblePuzzleImage(RImage **images, int width, int height)
217 RImage *img = RCreateImage(width, height, 1);
218 RImage *tmp;
219 int tw, th;
220 RColor color;
221 if (!img)
222 return NULL;
224 color.red = 0;
225 color.green = 0;
226 color.blue = 0;
227 color.alpha = 255;
229 RFillImage(img, &color);
231 tw = width - images[0]->width - images[2]->width;
232 th = height - images[0]->height - images[6]->height;
234 if (tw <= 0 || th <= 0)
235 return NULL;
237 /* top */
238 if (tw > 0) {
239 tmp = RSmoothScaleImage(images[1], tw, images[1]->height);
240 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, 0);
241 RReleaseImage(tmp);
243 /* bottom */
244 if (tw > 0) {
245 tmp = RSmoothScaleImage(images[7], tw, images[7]->height);
246 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[6]->width, height - images[6]->height);
247 RReleaseImage(tmp);
249 /* left */
250 if (th > 0) {
251 tmp = RSmoothScaleImage(images[3], images[3]->width, th);
252 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, 0, images[0]->height);
253 RReleaseImage(tmp);
255 /* right */
256 if (th > 0) {
257 tmp = RSmoothScaleImage(images[5], images[5]->width, th);
258 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, width - images[5]->width, images[2]->height);
259 RReleaseImage(tmp);
261 /* center */
262 if (tw > 0 && th > 0) {
263 tmp = RSmoothScaleImage(images[4], tw, th);
264 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, images[0]->height);
265 RReleaseImage(tmp);
268 /* corners */
269 RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height, 0, 0);
270 RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height, width - images[2]->width, 0);
271 RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height, 0, height - images[6]->height);
272 RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
273 width - images[8]->width, height - images[8]->height);
275 return img;
278 static RImage *createBackImage(int width, int height)
280 return assemblePuzzleImage(wPreferences.swbackImage, width, height);
283 static RImage *getTile(void)
285 RImage *stile;
287 if (!wPreferences.swtileImage)
288 return NULL;
290 stile = RScaleImage(wPreferences.swtileImage, ICON_TILE_SIZE, ICON_TILE_SIZE);
291 if (!stile)
292 return wPreferences.swtileImage;
294 return stile;
297 static void drawTitle(WSwitchPanel *panel, int idecks, char *title)
299 char *ntitle;
300 int width = WMWidgetWidth(panel->win);
301 int x;
303 if (title)
304 ntitle = ShrinkString(panel->font, title, width - 2 * BORDER_SPACE);
305 else
306 ntitle = NULL;
308 if (panel->bg) {
309 if (ntitle) {
310 if (strcmp(ntitle, title) != 0) {
311 x = BORDER_SPACE;
312 } else {
313 int w = WMWidthOfString(panel->font, ntitle, strlen(ntitle));
315 x = BORDER_SPACE + (idecks - panel->firstVisible) * ICON_TILE_SIZE +
316 ICON_TILE_SIZE / 2 - w / 2;
317 if (x < BORDER_SPACE)
318 x = BORDER_SPACE;
319 else if (x + w > width - BORDER_SPACE)
320 x = width - BORDER_SPACE - w;
324 XClearWindow(dpy, WMWidgetXID(panel->win));
325 if (ntitle)
326 WMDrawString(panel->scr->wmscreen,
327 WMWidgetXID(panel->win),
328 panel->white, panel->font,
330 WMWidgetHeight(panel->win) - BORDER_SPACE - LABEL_HEIGHT +
331 WMFontHeight(panel->font) / 2, ntitle, strlen(ntitle));
332 } else {
333 if (ntitle)
334 WMSetLabelText(panel->label, ntitle);
337 if (ntitle)
338 free(ntitle);
341 static WMArray *makeWindowListArray(WWindow *curwin, int include_unmapped, Bool class_only)
343 WMArray *windows = WMCreateArray(10);
344 int fl;
345 WWindow *wwin;
347 for (fl = 0; fl < 2; fl++) {
348 for (wwin = curwin; wwin; wwin = wwin->prev) {
349 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
350 (wwin->flags.mapped || include_unmapped)) {
351 if (class_only)
352 if (!sameWindowClass(wwin, curwin))
353 continue;
355 if (!WFLAGP(wwin, skip_switchpanel))
356 WMAddToArray(windows, wwin);
359 wwin = curwin;
360 /* start over from the beginning of the list */
361 while (wwin->next)
362 wwin = wwin->next;
364 for (wwin = curwin; wwin && wwin != curwin; wwin = wwin->prev) {
365 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
366 (wwin->flags.mapped || include_unmapped)) {
367 if (class_only)
368 if (!sameWindowClass(wwin, curwin))
369 continue;
370 if (!WFLAGP(wwin, skip_switchpanel))
371 WMAddToArray(windows, wwin);
376 return windows;
379 WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, Bool class_only)
381 WWindow *wwin;
382 WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
383 WMFrame *viewport;
384 int i, width, height, iconsThatFitCount, count;
385 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
387 panel->scr = scr;
388 panel->windows = makeWindowListArray(curwin, wPreferences.swtileImage != NULL, class_only);
389 count = WMGetArrayItemCount(panel->windows);
391 if (count == 0) {
392 WMFreeArray(panel->windows);
393 wfree(panel);
394 return NULL;
397 width = ICON_TILE_SIZE * count;
398 iconsThatFitCount = count;
400 if (width > rect.size.width) {
401 iconsThatFitCount = (rect.size.width - SCREEN_BORDER_SPACING) / ICON_TILE_SIZE;
402 width = iconsThatFitCount * ICON_TILE_SIZE;
405 panel->visibleCount = iconsThatFitCount;
407 if (!wPreferences.swtileImage)
408 return panel;
410 height = LABEL_HEIGHT + ICON_TILE_SIZE;
412 panel->tileTmp = RCreateImage(ICON_TILE_SIZE, ICON_TILE_SIZE, 1);
413 panel->tile = getTile();
414 if (panel->tile && wPreferences.swbackImage[8])
415 panel->bg = createBackImage(width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
417 if (!panel->tileTmp || !panel->tile) {
418 if (panel->bg)
419 RReleaseImage(panel->bg);
420 panel->bg = NULL;
421 if (panel->tile)
422 RReleaseImage(panel->tile);
423 panel->tile = NULL;
424 if (panel->tileTmp)
425 RReleaseImage(panel->tileTmp);
426 panel->tileTmp = NULL;
429 panel->white = WMWhiteColor(scr->wmscreen);
430 panel->font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
431 panel->icons = WMCreateArray(count);
432 panel->images = WMCreateArray(count);
434 panel->win = WMCreateWindow(scr->wmscreen, "");
436 if (!panel->bg) {
437 WMFrame *frame = WMCreateFrame(panel->win);
438 WMColor *darkGray = WMDarkGrayColor(scr->wmscreen);
439 WMSetFrameRelief(frame, WRSimple);
440 WMSetViewExpandsToParent(WMWidgetView(frame), 0, 0, 0, 0);
442 panel->label = WMCreateLabel(panel->win);
443 WMResizeWidget(panel->label, width, LABEL_HEIGHT);
444 WMMoveWidget(panel->label, BORDER_SPACE, BORDER_SPACE + ICON_TILE_SIZE + 5);
445 WMSetLabelRelief(panel->label, WRSimple);
446 WMSetWidgetBackgroundColor(panel->label, darkGray);
447 WMSetLabelFont(panel->label, panel->font);
448 WMSetLabelTextColor(panel->label, panel->white);
450 WMReleaseColor(darkGray);
451 height += 5;
454 WMResizeWidget(panel->win, width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
456 viewport = WMCreateFrame(panel->win);
457 WMResizeWidget(viewport, width, ICON_TILE_SIZE);
458 WMMoveWidget(viewport, BORDER_SPACE, BORDER_SPACE);
459 WMSetFrameRelief(viewport, WRFlat);
461 panel->iconBox = WMCreateFrame(viewport);
462 WMMoveWidget(panel->iconBox, 0, 0);
463 WMResizeWidget(panel->iconBox, ICON_TILE_SIZE * count, ICON_TILE_SIZE);
464 WMSetFrameRelief(panel->iconBox, WRFlat);
466 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
467 addIconForWindow(panel, panel->iconBox, wwin, i * ICON_TILE_SIZE, 0);
470 WMMapSubwidgets(panel->win);
471 WMRealizeWidget(panel->win);
473 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
474 changeImage(panel, i, 0, False);
477 if (panel->bg) {
478 Pixmap pixmap, mask;
480 RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
482 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
484 #ifdef SHAPE
485 if (mask && wShapeSupported)
486 XShapeCombineMask(dpy, WMWidgetXID(panel->win), ShapeBounding, 0, 0, mask, ShapeSet);
487 #endif
488 if (pixmap)
489 XFreePixmap(dpy, pixmap);
491 if (mask)
492 XFreePixmap(dpy, mask);
496 WMPoint center;
497 center = wGetPointToCenterRectInHead(scr, wGetHeadForPointerLocation(scr),
498 width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
499 WMMoveWidget(panel->win, center.x, center.y);
502 panel->current = WMGetFirstInArray(panel->windows, curwin);
503 if (panel->current >= 0)
504 changeImage(panel, panel->current, 1, False);
506 WMMapWidget(panel->win);
508 return panel;
511 void wSwitchPanelDestroy(WSwitchPanel *panel)
513 int i;
514 RImage *image;
516 if (panel->win) {
517 Window info_win = panel->scr->info_window;
518 XEvent ev;
519 ev.xclient.type = ClientMessage;
520 ev.xclient.message_type = _XA_WM_IGNORE_FOCUS_EVENTS;
521 ev.xclient.format = 32;
522 ev.xclient.data.l[0] = True;
524 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
525 WMUnmapWidget(panel->win);
527 ev.xclient.data.l[0] = False;
528 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
531 if (panel->images) {
532 WM_ITERATE_ARRAY(panel->images, image, i) {
533 if (image)
534 RReleaseImage(image);
536 WMFreeArray(panel->images);
539 if (panel->win)
540 WMDestroyWidget(panel->win);
542 if (panel->icons)
543 WMFreeArray(panel->icons);
545 WMFreeArray(panel->windows);
547 if (panel->tile)
548 RReleaseImage(panel->tile);
550 if (panel->tileTmp)
551 RReleaseImage(panel->tileTmp);
553 if (panel->bg)
554 RReleaseImage(panel->bg);
556 if (panel->font)
557 WMReleaseFont(panel->font);
559 if (panel->white)
560 WMReleaseColor(panel->white);
562 wfree(panel);
565 WWindow *wSwitchPanelSelectNext(WSwitchPanel *panel, int back, int ignore_minimized, Bool class_only)
567 WWindow *wwin, *curwin, *tmpwin;
568 int count = WMGetArrayItemCount(panel->windows);
569 int orig = panel->current;
570 int i;
572 if (count == 0)
573 return NULL;
575 if (panel->win)
576 changeImage(panel, panel->current, 0, False);
578 if (!wPreferences.cycle_ignore_minimized)
579 ignore_minimized = False;
581 if (ignore_minimized && canReceiveFocus(WMGetFromArray(panel->windows, (count + panel->current) % count)) < 0)
582 ignore_minimized = False;
584 curwin = WMGetFromArray(panel->windows, orig);
585 do {
586 do {
587 if (back)
588 panel->current--;
589 else
590 panel->current++;
592 panel->current= (count + panel->current) % count;
593 wwin = WMGetFromArray(panel->windows, panel->current);
595 if (!class_only)
596 break;
597 if (panel->current == orig)
598 break;
599 } while (!sameWindowClass(wwin, curwin));
600 } while (ignore_minimized && panel->current != orig && canReceiveFocus(wwin) < 0);
602 WM_ITERATE_ARRAY(panel->windows, tmpwin, i) {
603 if (!class_only || sameWindowClass(tmpwin, curwin))
604 changeImage(panel, i, 0, False);
605 else
606 changeImage(panel, i, 0, True);
610 if (panel->current < panel->firstVisible)
611 scrollIcons(panel, panel->current - panel->firstVisible);
612 else if (panel->current - panel->firstVisible >= panel->visibleCount)
613 scrollIcons(panel, panel->current - panel->firstVisible - panel->visibleCount + 1);
615 if (panel->win) {
616 drawTitle(panel, panel->current, wwin->frame->title);
618 changeImage(panel, panel->current, 1, False);
621 return wwin;
624 WWindow *wSwitchPanelSelectFirst(WSwitchPanel *panel, int back)
626 WWindow *wwin;
627 int count = WMGetArrayItemCount(panel->windows);
628 int i;
630 if (count == 0)
631 return NULL;
633 if (panel->win) {
634 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
635 changeImage(panel, i, 0, False);
639 if (back) {
640 panel->current = count - 1;
641 scrollIcons(panel, count);
642 } else {
643 panel->current = 0;
644 scrollIcons(panel, -count);
647 wwin = WMGetFromArray(panel->windows, panel->current);
649 if (panel->win) {
650 drawTitle(panel, panel->current, wwin->frame->title);
651 changeImage(panel, panel->current, 1, False);
654 return wwin;
657 WWindow *wSwitchPanelHandleEvent(WSwitchPanel *panel, XEvent *event)
659 WMFrame *icon;
660 int i;
661 int focus = -1;
663 if (!panel->win)
664 return NULL;
666 if (event->type == MotionNotify) {
667 WM_ITERATE_ARRAY(panel->icons, icon, i) {
668 if (WMWidgetXID(icon) == event->xmotion.window) {
669 focus = i;
670 break;
675 if (focus >= 0 && panel->current != focus) {
676 WWindow *wwin;
678 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
679 changeImage(panel, i, 0, False);
681 changeImage(panel, focus, 1, False);
682 panel->current = focus;
684 wwin = WMGetFromArray(panel->windows, focus);
686 drawTitle(panel, panel->current, wwin->frame->title);
688 return wwin;
691 return NULL;
694 Window wSwitchPanelGetWindow(WSwitchPanel *swpanel)
696 if (!swpanel->win)
697 return None;
699 return WMWidgetXID(swpanel->win);