wmaker: remove unnecessary null pointer checks in handle_event of wsmap
[wmaker-crm.git] / src / switchpanel.c
blobf048ac52e116338eae41d21f443df29febc91b1e
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"
39 #ifdef USE_XSHAPE
40 #include <X11/extensions/shape.h>
41 #endif
43 struct SwitchPanel {
44 WScreen *scr;
45 WMWindow *win;
46 WMFrame *iconBox;
48 WMArray *icons;
49 WMArray *images;
50 WMArray *windows;
51 WMArray *flags;
52 RImage *bg;
53 int current;
54 int firstVisible;
55 int visibleCount;
57 WMLabel *label;
59 RImage *tileTmp;
60 RImage *tile;
62 WMFont *font;
63 WMColor *white;
66 #define BORDER_SPACE 10
67 #define ICON_SIZE 48
68 #define ICON_TILE_SIZE 64
69 #define LABEL_HEIGHT 25
70 #define SCREEN_BORDER_SPACING 2*20
71 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
73 #define ICON_SELECTED (1<<1)
74 #define ICON_DIM (1<<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, Bool force)
110 WMFrame *icon = NULL;
111 RImage *image = NULL;
112 int flags;
113 int desired = 0;
115 /* This whole function is a no-op if we aren't drawing the panel */
116 if (!wPreferences.swtileImage)
117 return;
119 icon = WMGetFromArray(panel->icons, idecks);
120 image = WMGetFromArray(panel->images, idecks);
121 flags = (int) (uintptr_t) WMGetFromArray(panel->flags, idecks);
123 if (selected)
124 desired |= ICON_SELECTED;
125 if (dim)
126 desired |= ICON_DIM;
128 if (flags == desired && !force)
129 return;
131 WMReplaceInArray(panel->flags, idecks, (void *) (uintptr_t) desired);
133 if (!panel->bg && !panel->tile && !selected)
134 WMSetFrameRelief(icon, WRFlat);
136 if (image && icon) {
137 RImage *back;
138 int opaq = (dim) ? 75 : 255;
139 RImage *tile;
140 WMPoint pos;
141 Pixmap p;
143 if (canReceiveFocus(WMGetFromArray(panel->windows, idecks)) < 0)
144 opaq = 50;
146 pos = WMGetViewPosition(WMWidgetView(icon));
147 back = panel->tileTmp;
148 if (panel->bg) {
149 RCopyArea(back, panel->bg,
150 BORDER_SPACE + pos.x - panel->firstVisible * ICON_TILE_SIZE,
151 BORDER_SPACE + pos.y, back->width, back->height, 0, 0);
152 } else {
153 RColor color;
154 WMScreen *wscr = WMWidgetScreen(icon);
155 color.red = 255;
156 color.red = WMRedComponentOfColor(WMGrayColor(wscr)) >> 8;
157 color.green = WMGreenComponentOfColor(WMGrayColor(wscr)) >> 8;
158 color.blue = WMBlueComponentOfColor(WMGrayColor(wscr)) >> 8;
159 RFillImage(back, &color);
162 if (selected) {
163 tile = panel->tile;
164 RCombineArea(back, tile, 0, 0, tile->width, tile->height,
165 (back->width - tile->width) / 2, (back->height - tile->height) / 2);
168 RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
169 (back->width - image->width) / 2, (back->height - image->height) / 2,
170 opaq);
172 RConvertImage(panel->scr->rcontext, back, &p);
173 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
174 XClearWindow(dpy, WMWidgetXID(icon));
175 XFreePixmap(dpy, p);
178 if (!panel->bg && !panel->tile && selected)
179 WMSetFrameRelief(icon, WRSimple);
182 static void addIconForWindow(WSwitchPanel *panel, WMWidget *parent, WWindow *wwin, int x, int y)
184 WMFrame *icon = WMCreateFrame(parent);
185 RImage *image = NULL;
187 WMSetFrameRelief(icon, WRFlat);
188 WMResizeWidget(icon, ICON_TILE_SIZE, ICON_TILE_SIZE);
189 WMMoveWidget(icon, x, y);
191 if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
192 image = RRetainImage(wwin->net_icon_image);
194 /* get_icon_image() includes the default icon image */
195 if (!image)
196 image = get_icon_image(panel->scr, wwin->wm_instance, wwin->wm_class, ICON_TILE_SIZE);
198 /* We must resize the icon size (~64) to the switch panel icon size (~48) */
199 image = wIconValidateIconSize(image, ICON_SIZE);
201 WMAddToArray(panel->images, image);
202 WMAddToArray(panel->icons, icon);
205 static void scrollIcons(WSwitchPanel *panel, int delta)
207 int nfirst = panel->firstVisible + delta;
208 int i;
209 int count = WMGetArrayItemCount(panel->windows);
210 Bool dim;
212 if (count <= panel->visibleCount)
213 return;
215 if (nfirst < 0)
216 nfirst = 0;
217 else if (nfirst >= count - panel->visibleCount)
218 nfirst = count - panel->visibleCount;
220 if (nfirst == panel->firstVisible)
221 return;
223 WMMoveWidget(panel->iconBox, -nfirst * ICON_TILE_SIZE, 0);
225 panel->firstVisible = nfirst;
227 for (i = panel->firstVisible; i < panel->firstVisible + panel->visibleCount; i++) {
228 if (i == panel->current)
229 continue;
230 dim = ((int) (uintptr_t) WMGetFromArray(panel->flags, i) & ICON_DIM);
231 changeImage(panel, i, 0, dim, True);
236 * 0 1 2
237 * 3 4 5
238 * 6 7 8
240 static RImage *assemblePuzzleImage(RImage **images, int width, int height)
242 RImage *img;
243 RImage *tmp;
244 int tw, th;
245 RColor color;
247 tw = width - images[0]->width - images[2]->width;
248 th = height - images[0]->height - images[6]->height;
250 if (tw <= 0 || th <= 0)
251 return NULL;
253 img = RCreateImage(width, height, 1);
254 if (!img)
255 return NULL;
257 color.red = 0;
258 color.green = 0;
259 color.blue = 0;
260 color.alpha = 255;
261 RFillImage(img, &color);
263 /* top */
264 tmp = RSmoothScaleImage(images[1], tw, images[1]->height);
265 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, 0);
266 RReleaseImage(tmp);
268 /* bottom */
269 tmp = RSmoothScaleImage(images[7], tw, images[7]->height);
270 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[6]->width, height - images[6]->height);
271 RReleaseImage(tmp);
273 /* left */
274 tmp = RSmoothScaleImage(images[3], images[3]->width, th);
275 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, 0, images[0]->height);
276 RReleaseImage(tmp);
278 /* right */
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 tmp = RSmoothScaleImage(images[4], tw, th);
285 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, images[0]->height);
286 RReleaseImage(tmp);
288 /* corners */
289 RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height, 0, 0);
290 RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height, width - images[2]->width, 0);
291 RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height, 0, height - images[6]->height);
292 RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
293 width - images[8]->width, height - images[8]->height);
295 return img;
298 static RImage *createBackImage(int width, int height)
300 return assemblePuzzleImage(wPreferences.swbackImage, width, height);
303 static RImage *getTile(void)
305 RImage *stile;
307 if (!wPreferences.swtileImage)
308 return NULL;
310 stile = RScaleImage(wPreferences.swtileImage, ICON_TILE_SIZE, ICON_TILE_SIZE);
311 if (!stile)
312 return wPreferences.swtileImage;
314 return stile;
317 static void drawTitle(WSwitchPanel *panel, int idecks, const char *title)
319 char *ntitle;
320 int width = WMWidgetWidth(panel->win);
321 int x;
323 if (title)
324 ntitle = ShrinkString(panel->font, title, width - 2 * BORDER_SPACE);
325 else
326 ntitle = NULL;
328 if (panel->bg) {
329 if (ntitle) {
330 if (strcmp(ntitle, title) != 0) {
331 x = BORDER_SPACE;
332 } else {
333 int w = WMWidthOfString(panel->font, ntitle, strlen(ntitle));
335 x = BORDER_SPACE + (idecks - panel->firstVisible) * ICON_TILE_SIZE +
336 ICON_TILE_SIZE / 2 - w / 2;
337 if (x < BORDER_SPACE)
338 x = BORDER_SPACE;
339 else if (x + w > width - BORDER_SPACE)
340 x = width - BORDER_SPACE - w;
344 XClearWindow(dpy, WMWidgetXID(panel->win));
345 if (ntitle)
346 WMDrawString(panel->scr->wmscreen,
347 WMWidgetXID(panel->win),
348 panel->white, panel->font,
350 WMWidgetHeight(panel->win) - BORDER_SPACE - LABEL_HEIGHT +
351 WMFontHeight(panel->font) / 2, ntitle, strlen(ntitle));
352 } else {
353 if (ntitle)
354 WMSetLabelText(panel->label, ntitle);
357 if (ntitle)
358 free(ntitle);
361 static WMArray *makeWindowListArray(WScreen *scr, int include_unmapped, Bool class_only)
363 WMArray *windows = WMCreateArray(10);
364 WWindow *wwin = scr->focused_window;
366 while (wwin) {
367 if ((canReceiveFocus(wwin) != 0) &&
368 (wwin->flags.mapped || wwin->flags.shaded || include_unmapped)) {
369 if (class_only)
370 if (!sameWindowClass(scr->focused_window, wwin))
371 continue;
372 if (!WFLAGP(wwin, skip_switchpanel))
373 WMAddToArray(windows, wwin);
375 wwin = wwin->prev;
377 return windows;
380 static WMArray *makeWindowFlagsArray(int count)
382 WMArray *flags = WMCreateArray(count);
383 int i;
385 for (i = 0; i < count; i++)
386 WMAddToArray(flags, (void *) 0);
388 return flags;
391 WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, Bool class_only)
393 WWindow *wwin;
394 WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
395 WMFrame *viewport;
396 int i, width, height, iconsThatFitCount, count;
397 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
399 panel->scr = scr;
400 panel->windows = makeWindowListArray(scr, wPreferences.swtileImage != NULL, class_only);
401 count = WMGetArrayItemCount(panel->windows);
402 if (count)
403 panel->flags = makeWindowFlagsArray(count);
405 if (count == 0) {
406 WMFreeArray(panel->windows);
407 wfree(panel);
408 return NULL;
411 width = ICON_TILE_SIZE * count;
412 iconsThatFitCount = count;
414 if (width > rect.size.width) {
415 iconsThatFitCount = (rect.size.width - SCREEN_BORDER_SPACING) / ICON_TILE_SIZE;
416 width = iconsThatFitCount * ICON_TILE_SIZE;
419 panel->visibleCount = iconsThatFitCount;
421 if (!wPreferences.swtileImage)
422 return panel;
424 height = LABEL_HEIGHT + ICON_TILE_SIZE;
426 panel->tileTmp = RCreateImage(ICON_TILE_SIZE, ICON_TILE_SIZE, 1);
427 panel->tile = getTile();
428 if (panel->tile && wPreferences.swbackImage[8])
429 panel->bg = createBackImage(width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
431 if (!panel->tileTmp || !panel->tile) {
432 if (panel->bg)
433 RReleaseImage(panel->bg);
434 panel->bg = NULL;
435 if (panel->tile)
436 RReleaseImage(panel->tile);
437 panel->tile = NULL;
438 if (panel->tileTmp)
439 RReleaseImage(panel->tileTmp);
440 panel->tileTmp = NULL;
443 panel->white = WMWhiteColor(scr->wmscreen);
444 panel->font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
445 panel->icons = WMCreateArray(count);
446 panel->images = WMCreateArray(count);
448 panel->win = WMCreateWindow(scr->wmscreen, "");
450 if (!panel->bg) {
451 WMFrame *frame = WMCreateFrame(panel->win);
452 WMColor *darkGray = WMDarkGrayColor(scr->wmscreen);
453 WMSetFrameRelief(frame, WRSimple);
454 WMSetViewExpandsToParent(WMWidgetView(frame), 0, 0, 0, 0);
456 panel->label = WMCreateLabel(panel->win);
457 WMResizeWidget(panel->label, width, LABEL_HEIGHT);
458 WMMoveWidget(panel->label, BORDER_SPACE, BORDER_SPACE + ICON_TILE_SIZE + 5);
459 WMSetLabelRelief(panel->label, WRSimple);
460 WMSetWidgetBackgroundColor(panel->label, darkGray);
461 WMSetLabelFont(panel->label, panel->font);
462 WMSetLabelTextColor(panel->label, panel->white);
464 WMReleaseColor(darkGray);
465 height += 5;
468 WMResizeWidget(panel->win, width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
470 viewport = WMCreateFrame(panel->win);
471 WMResizeWidget(viewport, width, ICON_TILE_SIZE);
472 WMMoveWidget(viewport, BORDER_SPACE, BORDER_SPACE);
473 WMSetFrameRelief(viewport, WRFlat);
475 panel->iconBox = WMCreateFrame(viewport);
476 WMMoveWidget(panel->iconBox, 0, 0);
477 WMResizeWidget(panel->iconBox, ICON_TILE_SIZE * count, ICON_TILE_SIZE);
478 WMSetFrameRelief(panel->iconBox, WRFlat);
480 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
481 addIconForWindow(panel, panel->iconBox, wwin, i * ICON_TILE_SIZE, 0);
484 WMMapSubwidgets(panel->win);
485 WMRealizeWidget(panel->win);
487 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
488 changeImage(panel, i, 0, False, True);
491 if (panel->bg) {
492 Pixmap pixmap, mask;
494 RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
496 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
498 #ifdef USE_XSHAPE
499 if (mask && w_global.xext.shape.supported)
500 XShapeCombineMask(dpy, WMWidgetXID(panel->win), ShapeBounding, 0, 0, mask, ShapeSet);
501 #endif
502 if (pixmap)
503 XFreePixmap(dpy, pixmap);
505 if (mask)
506 XFreePixmap(dpy, mask);
510 WMPoint center;
511 center = wGetPointToCenterRectInHead(scr, wGetHeadForPointerLocation(scr),
512 width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
513 WMMoveWidget(panel->win, center.x, center.y);
516 panel->current = WMGetFirstInArray(panel->windows, curwin);
517 if (panel->current >= 0)
518 changeImage(panel, panel->current, 1, False, False);
520 WMMapWidget(panel->win);
522 return panel;
525 void wSwitchPanelDestroy(WSwitchPanel *panel)
527 int i;
528 RImage *image;
530 if (panel->win) {
531 Window info_win = panel->scr->info_window;
532 XEvent ev;
533 ev.xclient.type = ClientMessage;
534 ev.xclient.message_type = w_global.atom.wm.ignore_focus_events;
535 ev.xclient.format = 32;
536 ev.xclient.data.l[0] = True;
538 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
539 WMUnmapWidget(panel->win);
541 ev.xclient.data.l[0] = False;
542 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
545 if (panel->images) {
546 WM_ITERATE_ARRAY(panel->images, image, i) {
547 if (image)
548 RReleaseImage(image);
550 WMFreeArray(panel->images);
553 if (panel->win)
554 WMDestroyWidget(panel->win);
556 if (panel->icons)
557 WMFreeArray(panel->icons);
559 if (panel->flags)
560 WMFreeArray(panel->flags);
562 WMFreeArray(panel->windows);
564 if (panel->tile)
565 RReleaseImage(panel->tile);
567 if (panel->tileTmp)
568 RReleaseImage(panel->tileTmp);
570 if (panel->bg)
571 RReleaseImage(panel->bg);
573 if (panel->font)
574 WMReleaseFont(panel->font);
576 if (panel->white)
577 WMReleaseColor(panel->white);
579 wfree(panel);
582 WWindow *wSwitchPanelSelectNext(WSwitchPanel *panel, int back, int ignore_minimized, Bool class_only)
584 WWindow *wwin, *curwin, *tmpwin;
585 int count = WMGetArrayItemCount(panel->windows);
586 int orig = panel->current;
587 int i;
588 Bool dim = False;
590 if (count == 0)
591 return NULL;
593 if (!wPreferences.cycle_ignore_minimized)
594 ignore_minimized = False;
596 if (ignore_minimized && canReceiveFocus(WMGetFromArray(panel->windows, (count + panel->current) % count)) < 0)
597 ignore_minimized = False;
599 curwin = WMGetFromArray(panel->windows, orig);
600 do {
601 do {
602 if (back)
603 panel->current--;
604 else
605 panel->current++;
607 panel->current = (count + panel->current) % count;
608 wwin = WMGetFromArray(panel->windows, panel->current);
610 if (!class_only)
611 break;
612 if (panel->current == orig)
613 break;
614 } while (!sameWindowClass(wwin, curwin));
615 } while (ignore_minimized && panel->current != orig && canReceiveFocus(wwin) < 0);
617 WM_ITERATE_ARRAY(panel->windows, tmpwin, i) {
618 if (i == panel->current)
619 continue;
620 if (!class_only || sameWindowClass(tmpwin, curwin))
621 changeImage(panel, i, 0, False, False);
622 else {
623 if (i == orig)
624 dim = True;
625 changeImage(panel, i, 0, True, False);
630 if (panel->current < panel->firstVisible)
631 scrollIcons(panel, panel->current - panel->firstVisible);
632 else if (panel->current - panel->firstVisible >= panel->visibleCount)
633 scrollIcons(panel, panel->current - panel->firstVisible - panel->visibleCount + 1);
635 if (panel->win) {
636 drawTitle(panel, panel->current, wwin->frame->title);
637 if (panel->current != orig)
638 changeImage(panel, orig, 0, dim, False);
639 changeImage(panel, panel->current, 1, False, False);
642 return wwin;
645 WWindow *wSwitchPanelSelectFirst(WSwitchPanel *panel, int back)
647 WWindow *wwin;
648 int count = WMGetArrayItemCount(panel->windows);
649 char *title;
650 int i;
652 if (count == 0)
653 return NULL;
655 if (back) {
656 panel->current = count - 1;
657 scrollIcons(panel, count);
658 } else {
659 panel->current = 0;
660 scrollIcons(panel, -count);
663 wwin = WMGetFromArray(panel->windows, panel->current);
664 title = wwin->frame->title;
666 if (panel->win) {
667 for (WMArrayFirst(panel->windows, &(i)); (i) != WANotFound; WMArrayNext(panel->windows, &(i)))
668 changeImage(panel, i, i == panel->current, False, False);
670 drawTitle(panel, panel->current, title);
673 return wwin;
676 WWindow *wSwitchPanelHandleEvent(WSwitchPanel *panel, XEvent *event)
678 WMFrame *icon;
679 int i;
680 int focus = -1;
682 if (!panel->win)
683 return NULL;
685 if (event->type == MotionNotify) {
686 WM_ITERATE_ARRAY(panel->icons, icon, i) {
687 if (WMWidgetXID(icon) == event->xmotion.window) {
688 focus = i;
689 break;
694 if (focus >= 0 && panel->current != focus) {
695 WWindow *wwin;
697 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
698 changeImage(panel, i, i == focus, False, False);
700 panel->current = focus;
702 wwin = WMGetFromArray(panel->windows, focus);
704 drawTitle(panel, panel->current, wwin->frame->title);
706 return wwin;
709 return NULL;
712 Window wSwitchPanelGetWindow(WSwitchPanel *swpanel)
714 if (!swpanel->win)
715 return None;
717 return WMWidgetXID(swpanel->win);