Add new cycling option
[wmaker-crm.git] / src / switchpanel.c
blob46f5d4785659f9cd99b50b679e9133afacfd1278
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.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 "window.h"
32 #include "defaults.h"
33 #include "switchpanel.h"
34 #include "funcs.h"
35 #include "xinerama.h"
36 #include "window.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 RImage *bg;
55 int current;
56 int firstVisible;
57 int visibleCount;
59 WMLabel *label;
61 RImage *defIcon;
63 RImage *tileTmp;
64 RImage *tile;
66 WMFont *font;
67 WMColor *white;
70 extern WPreferences wPreferences;
72 #define BORDER_SPACE 10
73 #define ICON_SIZE 48
74 #define ICON_TILE_SIZE 64
75 #define LABEL_HEIGHT 25
76 #define SCREEN_BORDER_SPACING 2*20
77 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
79 static int canReceiveFocus(WWindow * wwin)
81 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
82 return 0;
84 if (wPreferences.cycle_active_head_only &&
85 wGetHeadForWindow(wwin) != wGetHeadForPointerLocation(wwin->screen_ptr))
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;
94 if (WFLAGP(wwin, no_focusable))
95 return 0;
96 return 1;
99 static void changeImage(WSwitchPanel * panel, int idecks, int selected)
101 WMFrame *icon = WMGetFromArray(panel->icons, idecks);
102 RImage *image = WMGetFromArray(panel->images, idecks);
104 if (!panel->bg && !panel->tile) {
105 if (!selected)
106 WMSetFrameRelief(icon, WRFlat);
109 if (image && icon) {
110 RImage *back;
111 int opaq = 255;
112 RImage *tile;
113 WMPoint pos;
114 Pixmap p;
116 if (canReceiveFocus(WMGetFromArray(panel->windows, idecks)) < 0)
117 opaq = 50;
119 pos = WMGetViewPosition(WMWidgetView(icon));
120 back = panel->tileTmp;
121 if (panel->bg)
122 RCopyArea(back, panel->bg,
123 BORDER_SPACE + pos.x - panel->firstVisible * ICON_TILE_SIZE,
124 BORDER_SPACE + pos.y, back->width, back->height, 0, 0);
125 else {
126 RColor color;
127 WMScreen *wscr = WMWidgetScreen(icon);
128 color.red = 255;
129 color.red = WMRedComponentOfColor(WMGrayColor(wscr)) >> 8;
130 color.green = WMGreenComponentOfColor(WMGrayColor(wscr)) >> 8;
131 color.blue = WMBlueComponentOfColor(WMGrayColor(wscr)) >> 8;
132 RFillImage(back, &color);
134 if (selected) {
135 tile = panel->tile;
136 RCombineArea(back, tile, 0, 0, tile->width, tile->height,
137 (back->width - tile->width) / 2, (back->height - tile->height) / 2);
139 RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
140 (back->width - image->width) / 2, (back->height - image->height) / 2,
141 opaq);
143 RConvertImage(panel->scr->rcontext, back, &p);
144 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
145 XClearWindow(dpy, WMWidgetXID(icon));
146 XFreePixmap(dpy, p);
149 if (!panel->bg && !panel->tile) {
150 if (selected)
151 WMSetFrameRelief(icon, WRSimple);
155 static RImage *scaleDownIfNeeded(RImage * image)
157 if (image && ((image->width - ICON_SIZE) > 2 || (image->height - ICON_SIZE) > 2)) {
158 RImage *nimage;
159 nimage = RScaleImage(image, ICON_SIZE, (image->height * ICON_SIZE / image->width));
160 RReleaseImage(image);
161 image = nimage;
163 return image;
166 static void addIconForWindow(WSwitchPanel * panel, WMWidget * parent, WWindow * wwin, int x, int y)
168 WMFrame *icon = WMCreateFrame(parent);
169 RImage *image = NULL;
171 WMSetFrameRelief(icon, WRFlat);
172 WMResizeWidget(icon, ICON_TILE_SIZE, ICON_TILE_SIZE);
173 WMMoveWidget(icon, x, y);
175 if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
176 image = RRetainImage(wwin->net_icon_image);
178 // Make this use a caching thing. When there are many windows,
179 // it's very likely that most of them are instances of the same thing,
180 // so caching them should get performance acceptable in these cases.
181 if (!image)
182 image = wDefaultGetImage(panel->scr, wwin->wm_instance, wwin->wm_class);
184 if (!image && !panel->defIcon) {
185 char *file = wDefaultGetIconFile(panel->scr, NULL, NULL, False);
186 if (file) {
187 char *path = FindImage(wPreferences.icon_path, file);
188 if (path) {
189 image = RLoadImage(panel->scr->rcontext, path, 0);
190 wfree(path);
193 if (image)
194 panel->defIcon = scaleDownIfNeeded(image);
195 image = NULL;
197 if (!image && panel->defIcon)
198 image = RRetainImage(panel->defIcon);
200 image = scaleDownIfNeeded(image);
202 WMAddToArray(panel->images, image);
203 WMAddToArray(panel->icons, icon);
206 static void scrollIcons(WSwitchPanel * panel, int delta)
208 int nfirst = panel->firstVisible + delta;
209 int i;
210 int count = WMGetArrayItemCount(panel->windows);
211 // int nx, ox;
212 // struct timeval tv1, tv2;
214 if (count <= panel->visibleCount)
215 return;
217 if (nfirst < 0)
218 nfirst = 0;
219 else if (nfirst >= count - panel->visibleCount)
220 nfirst = count - panel->visibleCount;
222 if (nfirst == panel->firstVisible)
223 return;
225 ox = -panel->firstVisible * ICON_TILE_SIZE;
226 nx = -nfirst * ICON_TILE_SIZE;
227 for (i= 0; i < SCROLL_STEPS; i++) {
228 unsigned int diff;
229 gettimeofday(&tv1, NULL);
230 WMMoveWidget(panel->iconBox, (nx*i + ox*(SCROLL_STEPS-i))/(SCROLL_STEPS-1), 0);
231 XSync(dpy, False);
232 gettimeofday(&tv2, NULL);
233 diff = (tv2.tv_sec-tv1.tv_sec)*10000+(tv2.tv_usec-tv1.tv_usec)/100;
234 if (diff < 200)
235 wusleep(300-diff);
238 WMMoveWidget(panel->iconBox, -nfirst * ICON_TILE_SIZE, 0);
240 panel->firstVisible = nfirst;
242 for (i = panel->firstVisible; i < panel->firstVisible + panel->visibleCount; i++) {
243 changeImage(panel, i, i == panel->current);
248 * 0 1 2
249 * 3 4 5
250 * 6 7 8
252 static RImage *assemblePuzzleImage(RImage ** images, int width, int height)
254 RImage *img = RCreateImage(width, height, 1);
255 RImage *tmp;
256 int tw, th;
257 RColor color;
258 if (!img)
259 return NULL;
261 color.red = 0;
262 color.green = 0;
263 color.blue = 0;
264 color.alpha = 255;
266 RFillImage(img, &color);
268 tw = width - images[0]->width - images[2]->width;
269 th = height - images[0]->height - images[6]->height;
271 if (tw <= 0 || th <= 0) {
272 //XXX
273 return NULL;
276 /* top */
277 if (tw > 0) {
278 tmp = RSmoothScaleImage(images[1], tw, images[1]->height);
279 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, 0);
280 RReleaseImage(tmp);
282 /* bottom */
283 if (tw > 0) {
284 tmp = RSmoothScaleImage(images[7], tw, images[7]->height);
285 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[6]->width, height - images[6]->height);
286 RReleaseImage(tmp);
288 /* left */
289 if (th > 0) {
290 tmp = RSmoothScaleImage(images[3], images[3]->width, th);
291 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, 0, images[0]->height);
292 RReleaseImage(tmp);
294 /* right */
295 if (th > 0) {
296 tmp = RSmoothScaleImage(images[5], images[5]->width, th);
297 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, width - images[5]->width, images[2]->height);
298 RReleaseImage(tmp);
300 /* center */
301 if (tw > 0 && th > 0) {
302 tmp = RSmoothScaleImage(images[4], tw, th);
303 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, images[0]->height);
304 RReleaseImage(tmp);
307 /* corners */
308 RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height, 0, 0);
310 RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height, width - images[2]->width, 0);
312 RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height, 0, height - images[6]->height);
314 RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
315 width - images[8]->width, height - images[8]->height);
317 return img;
320 static RImage *createBackImage(int width, int height)
322 return assemblePuzzleImage(wPreferences.swbackImage, width, height);
325 static RImage *getTile(void)
327 RImage *stile;
329 if (!wPreferences.swtileImage)
330 return NULL;
332 stile = RScaleImage(wPreferences.swtileImage, ICON_TILE_SIZE, ICON_TILE_SIZE);
333 if (!stile)
334 return wPreferences.swtileImage;
336 return stile;
339 static void drawTitle(WSwitchPanel * panel, int idecks, char *title)
341 char *ntitle;
342 int width = WMWidgetWidth(panel->win);
343 int x;
345 if (title)
346 ntitle = ShrinkString(panel->font, title, width - 2 * BORDER_SPACE);
347 else
348 ntitle = NULL;
350 if (panel->bg) {
351 if (ntitle) {
352 if (strcmp(ntitle, title) != 0)
353 x = BORDER_SPACE;
354 else {
355 int w = WMWidthOfString(panel->font, ntitle, strlen(ntitle));
357 x = BORDER_SPACE + (idecks - panel->firstVisible) * ICON_TILE_SIZE +
358 ICON_TILE_SIZE / 2 - w / 2;
359 if (x < BORDER_SPACE)
360 x = BORDER_SPACE;
361 else if (x + w > width - BORDER_SPACE)
362 x = width - BORDER_SPACE - w;
365 XClearWindow(dpy, WMWidgetXID(panel->win));
366 if (ntitle)
367 WMDrawString(panel->scr->wmscreen,
368 WMWidgetXID(panel->win),
369 panel->white, panel->font,
371 WMWidgetHeight(panel->win) - BORDER_SPACE - LABEL_HEIGHT +
372 WMFontHeight(panel->font) / 2, ntitle, strlen(ntitle));
373 } else {
374 if (ntitle)
375 WMSetLabelText(panel->label, ntitle);
377 if (ntitle)
378 free(ntitle);
381 static WMArray *makeWindowListArray(WWindow *curwin, int include_unmapped, Bool class_only)
383 WMArray *windows = WMCreateArray(10);
384 int fl;
385 WWindow *wwin;
387 for (fl = 0; fl < 2; fl++) {
388 for (wwin = curwin; wwin; wwin = wwin->prev) {
389 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
390 (!WFLAGP(wwin, skip_window_list) || wwin->flags.internal_window) &&
391 (wwin->flags.mapped || include_unmapped)) {
392 if (class_only) {
393 if (!wwin->wm_class || !curwin->wm_class)
394 continue;
395 if (strcmp(wwin->wm_class, curwin->wm_class))
396 continue;
398 WMAddToArray(windows, wwin);
401 wwin = curwin;
402 /* start over from the beginning of the list */
403 while (wwin->next)
404 wwin = wwin->next;
406 for (wwin = curwin; wwin && wwin != curwin; wwin = wwin->prev) {
407 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
408 (!WFLAGP(wwin, skip_window_list) || wwin->flags.internal_window) &&
409 (wwin->flags.mapped || include_unmapped)) {
410 if (class_only) {
411 if (!wwin->wm_class || !curwin->wm_class)
412 continue;
413 if (strcmp(wwin->wm_class, curwin->wm_class))
414 continue;
416 WMAddToArray(windows, wwin);
421 return windows;
424 WSwitchPanel *wInitSwitchPanel(WScreen * scr, WWindow * curwin, Bool class_only)
426 WWindow *wwin;
427 WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
428 WMFrame *viewport;
429 int i;
430 int width, height;
431 int iconsThatFitCount;
432 int count;
433 WMRect rect;
434 rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
436 memset(panel, 0, sizeof(WSwitchPanel));
438 panel->scr = scr;
440 panel->windows = makeWindowListArray(curwin, wPreferences.swtileImage != 0, class_only);
441 count = WMGetArrayItemCount(panel->windows);
443 if (count == 0) {
444 WMFreeArray(panel->windows);
445 wfree(panel);
446 return NULL;
449 width = ICON_TILE_SIZE * count;
450 iconsThatFitCount = count;
452 if (width > rect.size.width) {
453 iconsThatFitCount = (rect.size.width - SCREEN_BORDER_SPACING) / ICON_TILE_SIZE;
454 width = iconsThatFitCount * ICON_TILE_SIZE;
457 panel->visibleCount = iconsThatFitCount;
459 if (!wPreferences.swtileImage)
460 return panel;
462 height = LABEL_HEIGHT + ICON_TILE_SIZE;
464 panel->tileTmp = RCreateImage(ICON_TILE_SIZE, ICON_TILE_SIZE, 1);
465 panel->tile = getTile();
466 if (panel->tile && wPreferences.swbackImage[8]) {
467 panel->bg = createBackImage(width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
469 if (!panel->tileTmp || !panel->tile) {
470 if (panel->bg)
471 RReleaseImage(panel->bg);
472 panel->bg = NULL;
473 if (panel->tile)
474 RReleaseImage(panel->tile);
475 panel->tile = NULL;
476 if (panel->tileTmp)
477 RReleaseImage(panel->tileTmp);
478 panel->tileTmp = NULL;
481 panel->white = WMWhiteColor(scr->wmscreen);
482 panel->font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
483 panel->icons = WMCreateArray(count);
484 panel->images = WMCreateArray(count);
486 panel->win = WMCreateWindow(scr->wmscreen, "");
488 if (!panel->bg) {
489 WMFrame *frame = WMCreateFrame(panel->win);
490 WMColor *darkGray = WMDarkGrayColor(scr->wmscreen);
491 WMSetFrameRelief(frame, WRSimple);
492 WMSetViewExpandsToParent(WMWidgetView(frame), 0, 0, 0, 0);
494 panel->label = WMCreateLabel(panel->win);
495 WMResizeWidget(panel->label, width, LABEL_HEIGHT);
496 WMMoveWidget(panel->label, BORDER_SPACE, BORDER_SPACE + ICON_TILE_SIZE + 5);
497 WMSetLabelRelief(panel->label, WRSimple);
498 WMSetWidgetBackgroundColor(panel->label, darkGray);
499 WMSetLabelFont(panel->label, panel->font);
500 WMSetLabelTextColor(panel->label, panel->white);
502 WMReleaseColor(darkGray);
503 height += 5;
506 WMResizeWidget(panel->win, width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
508 viewport = WMCreateFrame(panel->win);
509 WMResizeWidget(viewport, width, ICON_TILE_SIZE);
510 WMMoveWidget(viewport, BORDER_SPACE, BORDER_SPACE);
511 WMSetFrameRelief(viewport, WRFlat);
513 panel->iconBox = WMCreateFrame(viewport);
514 WMMoveWidget(panel->iconBox, 0, 0);
515 WMResizeWidget(panel->iconBox, ICON_TILE_SIZE * count, ICON_TILE_SIZE);
516 WMSetFrameRelief(panel->iconBox, WRFlat);
518 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
519 addIconForWindow(panel, panel->iconBox, wwin, i * ICON_TILE_SIZE, 0);
522 WMMapSubwidgets(panel->win);
523 WMRealizeWidget(panel->win);
525 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
526 changeImage(panel, i, 0);
529 if (panel->bg) {
530 Pixmap pixmap, mask;
532 RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
534 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
536 #ifdef SHAPE
537 if (mask && wShapeSupported)
538 XShapeCombineMask(dpy, WMWidgetXID(panel->win), ShapeBounding, 0, 0, mask, ShapeSet);
539 #endif
541 if (pixmap)
542 XFreePixmap(dpy, pixmap);
543 if (mask)
544 XFreePixmap(dpy, mask);
548 WMPoint center;
549 center = wGetPointToCenterRectInHead(scr, wGetHeadForPointerLocation(scr),
550 width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
551 WMMoveWidget(panel->win, center.x, center.y);
554 panel->current = WMGetFirstInArray(panel->windows, curwin);
555 if (panel->current >= 0)
556 changeImage(panel, panel->current, 1);
558 WMMapWidget(panel->win);
560 return panel;
563 void wSwitchPanelDestroy(WSwitchPanel * panel)
565 int i;
566 RImage *image;
568 if (panel->win) {
569 Window info_win = panel->scr->info_window;
570 XEvent ev;
571 ev.xclient.type = ClientMessage;
572 ev.xclient.message_type = _XA_WM_IGNORE_FOCUS_EVENTS;
573 ev.xclient.format = 32;
574 ev.xclient.data.l[0] = True;
576 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
577 WMUnmapWidget(panel->win);
579 ev.xclient.data.l[0] = False;
580 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
583 if (panel->images) {
584 WM_ITERATE_ARRAY(panel->images, image, i) {
585 if (image)
586 RReleaseImage(image);
588 WMFreeArray(panel->images);
590 if (panel->win)
591 WMDestroyWidget(panel->win);
592 if (panel->icons)
593 WMFreeArray(panel->icons);
594 WMFreeArray(panel->windows);
595 if (panel->defIcon)
596 RReleaseImage(panel->defIcon);
597 if (panel->tile)
598 RReleaseImage(panel->tile);
599 if (panel->tileTmp)
600 RReleaseImage(panel->tileTmp);
601 if (panel->bg)
602 RReleaseImage(panel->bg);
603 if (panel->font)
604 WMReleaseFont(panel->font);
605 if (panel->white)
606 WMReleaseColor(panel->white);
607 wfree(panel);
610 WWindow *wSwitchPanelSelectNext(WSwitchPanel * panel, int back)
612 WWindow *wwin;
613 int count = WMGetArrayItemCount(panel->windows);
615 if (count == 0)
616 return NULL;
618 if (panel->win)
619 changeImage(panel, panel->current, 0);
621 if (back)
622 panel->current--;
623 else
624 panel->current++;
626 wwin = WMGetFromArray(panel->windows, (count + panel->current) % count);
628 if (back) {
629 if (panel->current < 0)
630 scrollIcons(panel, count);
631 else if (panel->current < panel->firstVisible)
632 scrollIcons(panel, -1);
633 } else {
634 if (panel->current >= count)
635 scrollIcons(panel, -count);
636 else if (panel->current - panel->firstVisible >= panel->visibleCount)
637 scrollIcons(panel, 1);
640 panel->current = (count + panel->current) % count;
642 if (panel->win) {
643 drawTitle(panel, panel->current, wwin->frame->title);
645 changeImage(panel, panel->current, 1);
648 return wwin;
651 WWindow *wSwitchPanelSelectFirst(WSwitchPanel * panel, int back)
653 WWindow *wwin;
654 int count = WMGetArrayItemCount(panel->windows);
656 if (count == 0)
657 return NULL;
659 if (panel->win)
660 changeImage(panel, panel->current, 0);
662 if (back) {
663 panel->current = count - 1;
664 scrollIcons(panel, count);
665 } else {
666 panel->current = 0;
667 scrollIcons(panel, -count);
670 wwin = WMGetFromArray(panel->windows, panel->current);
672 if (panel->win) {
673 drawTitle(panel, panel->current, wwin->frame->title);
675 changeImage(panel, panel->current, 1);
677 return wwin;
680 WWindow *wSwitchPanelHandleEvent(WSwitchPanel * panel, XEvent * event)
682 WMFrame *icon;
683 int i;
684 int focus = -1;
686 if (!panel->win)
687 return NULL;
689 /* if (event->type == LeaveNotify) {
690 if (event->xcrossing.window == WMWidgetXID(panel->win))
691 focus= 0;
692 } else */ if (event->type == MotionNotify) {
694 WM_ITERATE_ARRAY(panel->icons, icon, i) {
695 if (WMWidgetXID(icon) == event->xmotion.window) {
696 focus = i;
697 break;
701 if (focus >= 0 && panel->current != focus) {
702 WWindow *wwin;
704 changeImage(panel, panel->current, 0);
705 changeImage(panel, focus, 1);
706 panel->current = focus;
708 wwin = WMGetFromArray(panel->windows, focus);
710 drawTitle(panel, panel->current, wwin->frame->title);
712 return wwin;
715 return NULL;
718 Window wSwitchPanelGetWindow(WSwitchPanel * swpanel)
720 if (!swpanel->win)
721 return None;
722 return WMWidgetXID(swpanel->win);