Update Serbian translation from master branch
[wmaker-crm.git] / src / switchpanel.c
blobcb6cda1080a2454deef68c734f88ce07be5371dc
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;
64 WMColor *gray;
67 /* these values will be updated whenever the switch panel
68 * is created to to match the size defined in switch_panel_icon_size
69 * and the selected font size */
70 static short int icon_size;
71 static short int border_space;
72 static short int icon_tile_size;
73 static short int label_height;
75 #define SCREEN_BORDER_SPACING 2*20
77 #define ICON_SELECTED (1<<1)
78 #define ICON_DIM (1<<2)
80 static int canReceiveFocus(WWindow *wwin)
82 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
83 return 0;
85 if (wPreferences.cycle_active_head_only &&
86 wGetHeadForWindow(wwin) != wGetHeadForPointerLocation(wwin->screen_ptr))
87 return 0;
89 if (WFLAGP(wwin, no_focusable))
90 return 0;
92 if (!wwin->flags.mapped) {
93 if (!wwin->flags.shaded && !wwin->flags.miniaturized && !wwin->flags.hidden)
94 return 0;
95 else
96 return -1;
99 return 1;
102 static Bool sameWindowClass(WWindow *wwin, WWindow *curwin)
104 if (!wwin->wm_class || !curwin->wm_class)
105 return False;
106 if (strcmp(wwin->wm_class, curwin->wm_class))
107 return False;
109 return True;
112 static void changeImage(WSwitchPanel *panel, int idecks, int selected, Bool dim, Bool force)
114 WMFrame *icon = NULL;
115 RImage *image = NULL;
116 int flags;
117 int desired = 0;
119 /* This whole function is a no-op if we aren't drawing the panel */
120 if (!wPreferences.swtileImage)
121 return;
123 icon = WMGetFromArray(panel->icons, idecks);
124 image = WMGetFromArray(panel->images, idecks);
125 flags = (int) (uintptr_t) WMGetFromArray(panel->flags, idecks);
127 if (selected)
128 desired |= ICON_SELECTED;
129 if (dim)
130 desired |= ICON_DIM;
132 if (flags == desired && !force)
133 return;
135 WMReplaceInArray(panel->flags, idecks, (void *) (uintptr_t) desired);
137 if (!panel->bg && !panel->tile && !selected)
138 WMSetFrameRelief(icon, WRFlat);
140 if (image && icon) {
141 RImage *back;
142 int opaq = (dim) ? 75 : 255;
143 RImage *tile;
144 WMPoint pos;
145 Pixmap p;
147 if (canReceiveFocus(WMGetFromArray(panel->windows, idecks)) < 0)
148 opaq = 50;
150 pos = WMGetViewPosition(WMWidgetView(icon));
151 back = panel->tileTmp;
152 if (panel->bg) {
153 RCopyArea(back, panel->bg,
154 border_space + pos.x - panel->firstVisible * icon_tile_size,
155 border_space + pos.y, back->width, back->height, 0, 0);
156 } else {
157 RColor color;
158 color.red = WMRedComponentOfColor(panel->gray) >> 8;
159 color.green = WMGreenComponentOfColor(panel->gray) >> 8;
160 color.blue = WMBlueComponentOfColor(panel->gray) >> 8;
161 color.alpha = 255;
162 RFillImage(back, &color);
165 if (selected) {
166 tile = panel->tile;
167 RCombineArea(back, tile, 0, 0, tile->width, tile->height,
168 (back->width - tile->width) / 2, (back->height - tile->height) / 2);
171 RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
172 (back->width - image->width) / 2, (back->height - image->height) / 2,
173 opaq);
175 RConvertImage(panel->scr->rcontext, back, &p);
176 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
177 XClearWindow(dpy, WMWidgetXID(icon));
178 XFreePixmap(dpy, p);
181 if (!panel->bg && !panel->tile && selected)
182 WMSetFrameRelief(icon, WRSimple);
185 static void addIconForWindow(WSwitchPanel *panel, WMWidget *parent, WWindow *wwin, int x, int y)
187 WMFrame *icon = WMCreateFrame(parent);
188 RImage *image = NULL;
190 WMSetFrameRelief(icon, WRFlat);
191 WMResizeWidget(icon, icon_tile_size, icon_tile_size);
192 WMMoveWidget(icon, x, y);
194 if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
195 image = RRetainImage(wwin->net_icon_image);
197 /* get_icon_image() includes the default icon image */
198 if (!image)
199 image = get_icon_image(panel->scr, wwin->wm_instance, wwin->wm_class, icon_tile_size);
201 /* We must resize the icon size (~64) to the switch panel icon size (~48) */
202 image = wIconValidateIconSize(image, icon_size);
204 WMAddToArray(panel->images, image);
205 WMAddToArray(panel->icons, icon);
208 static void scrollIcons(WSwitchPanel *panel, int delta)
210 int nfirst = panel->firstVisible + delta;
211 int i;
212 int count = WMGetArrayItemCount(panel->windows);
213 Bool dim;
215 if (count <= panel->visibleCount)
216 return;
218 if (nfirst < 0)
219 nfirst = 0;
220 else if (nfirst >= count - panel->visibleCount)
221 nfirst = count - panel->visibleCount;
223 if (nfirst == panel->firstVisible)
224 return;
226 WMMoveWidget(panel->iconBox, -nfirst * icon_tile_size, 0);
228 panel->firstVisible = nfirst;
230 for (i = panel->firstVisible; i < panel->firstVisible + panel->visibleCount; i++) {
231 if (i == panel->current)
232 continue;
233 dim = ((int) (uintptr_t) WMGetFromArray(panel->flags, i) & ICON_DIM);
234 changeImage(panel, i, 0, dim, True);
239 * 0 1 2
240 * 3 4 5
241 * 6 7 8
243 static RImage *assemblePuzzleImage(RImage **images, int width, int height)
245 RImage *img;
246 RImage *tmp;
247 int tw, th;
248 RColor color;
250 tw = width - images[0]->width - images[2]->width;
251 th = height - images[0]->height - images[6]->height;
253 if (tw <= 0 || th <= 0)
254 return NULL;
256 img = RCreateImage(width, height, 1);
257 if (!img)
258 return NULL;
260 color.red = 0;
261 color.green = 0;
262 color.blue = 0;
263 color.alpha = 255;
264 RFillImage(img, &color);
266 /* top */
267 tmp = RSmoothScaleImage(images[1], tw, images[1]->height);
268 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, 0);
269 RReleaseImage(tmp);
271 /* bottom */
272 tmp = RSmoothScaleImage(images[7], tw, images[7]->height);
273 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[6]->width, height - images[6]->height);
274 RReleaseImage(tmp);
276 /* left */
277 tmp = RSmoothScaleImage(images[3], images[3]->width, th);
278 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, 0, images[0]->height);
279 RReleaseImage(tmp);
281 /* right */
282 tmp = RSmoothScaleImage(images[5], images[5]->width, th);
283 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, width - images[5]->width, images[2]->height);
284 RReleaseImage(tmp);
286 /* center */
287 tmp = RSmoothScaleImage(images[4], tw, th);
288 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, images[0]->height);
289 RReleaseImage(tmp);
291 /* corners */
292 RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height, 0, 0);
293 RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height, width - images[2]->width, 0);
294 RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height, 0, height - images[6]->height);
295 RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
296 width - images[8]->width, height - images[8]->height);
298 return img;
301 static RImage *createBackImage(int width, int height)
303 return assemblePuzzleImage(wPreferences.swbackImage, width, height);
306 static RImage *getTile(void)
308 RImage *stile;
310 if (!wPreferences.swtileImage)
311 return NULL;
313 stile = RScaleImage(wPreferences.swtileImage, icon_tile_size, icon_tile_size);
314 if (!stile)
315 return wPreferences.swtileImage;
317 return stile;
320 static void drawTitle(WSwitchPanel *panel, int idecks, const char *title)
322 char *ntitle;
323 int width = WMWidgetWidth(panel->win);
324 int x;
326 if (title)
327 ntitle = ShrinkString(panel->font, title, width - 2 * border_space);
328 else
329 ntitle = NULL;
331 if (panel->bg) {
332 if (ntitle) {
333 if (strcmp(ntitle, title) != 0) {
334 x = border_space;
335 } else {
336 int w = WMWidthOfString(panel->font, ntitle, strlen(ntitle));
338 x = border_space + (idecks - panel->firstVisible) * icon_tile_size+
339 icon_tile_size/ 2 - w / 2;
340 if (x < border_space)
341 x = border_space;
342 else if (x + w > width - border_space)
343 x = width - border_space - w;
347 XClearWindow(dpy, WMWidgetXID(panel->win));
348 if (ntitle)
349 WMDrawString(panel->scr->wmscreen,
350 WMWidgetXID(panel->win),
351 panel->white, panel->font,
353 WMWidgetHeight(panel->win) - border_space - label_height +
354 WMFontHeight(panel->font) / 2, ntitle, strlen(ntitle));
355 } else {
356 if (ntitle)
357 WMSetLabelText(panel->label, ntitle);
360 if (ntitle)
361 free(ntitle);
364 static WMArray *makeWindowListArray(WScreen *scr, int include_unmapped, Bool class_only)
366 WMArray *windows = WMCreateArray(10);
367 WWindow *wwin = scr->focused_window;
369 while (wwin) {
370 if ((canReceiveFocus(wwin) != 0) &&
371 (wwin->flags.mapped || wwin->flags.shaded || include_unmapped)) {
372 if (class_only)
373 if (!sameWindowClass(scr->focused_window, wwin)) {
374 wwin = wwin->prev;
375 continue;
377 if (!WFLAGP(wwin, skip_switchpanel))
378 WMAddToArray(windows, wwin);
380 wwin = wwin->prev;
382 return windows;
385 static WMArray *makeWindowFlagsArray(int count)
387 WMArray *flags = WMCreateArray(count);
388 int i;
390 for (i = 0; i < count; i++)
391 WMAddToArray(flags, (void *) 0);
393 return flags;
396 WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, Bool class_only)
398 int wmScaleWidth, wmScaleHeight;
399 WMGetScaleBaseFromSystemFont(scr->wmscreen, &wmScaleWidth, &wmScaleHeight);
401 icon_size = wPreferences.switch_panel_icon_size;
402 icon_tile_size = (short int)(((float)icon_size * 1.2F) + 0.5F);
403 border_space = WMScaleY(10);
404 label_height = WMScaleY(25);
406 WWindow *wwin;
407 WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
408 WMFrame *viewport;
409 int i, width, height, iconsThatFitCount, count;
410 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
412 panel->scr = scr;
413 panel->windows = makeWindowListArray(scr, wPreferences.swtileImage != NULL, class_only);
414 count = WMGetArrayItemCount(panel->windows);
415 if (count)
416 panel->flags = makeWindowFlagsArray(count);
418 if (count == 0) {
419 WMFreeArray(panel->windows);
420 wfree(panel);
421 return NULL;
424 width = icon_tile_size* count;
425 iconsThatFitCount = count;
427 if (width > (int)rect.size.width) {
428 iconsThatFitCount = (rect.size.width - SCREEN_BORDER_SPACING) / icon_tile_size;
429 width = iconsThatFitCount * icon_tile_size;
432 panel->visibleCount = iconsThatFitCount;
434 if (!wPreferences.swtileImage)
435 return panel;
437 height = label_height + icon_tile_size;
439 panel->tileTmp = RCreateImage(icon_tile_size, icon_tile_size, 1);
440 panel->tile = getTile();
441 if (panel->tile && wPreferences.swbackImage[8])
442 panel->bg = createBackImage(width + 2 * border_space, height + 2 * border_space);
444 if (!panel->tileTmp || !panel->tile) {
445 if (panel->bg)
446 RReleaseImage(panel->bg);
447 panel->bg = NULL;
448 if (panel->tile)
449 RReleaseImage(panel->tile);
450 panel->tile = NULL;
451 if (panel->tileTmp)
452 RReleaseImage(panel->tileTmp);
453 panel->tileTmp = NULL;
456 panel->white = WMWhiteColor(scr->wmscreen);
457 panel->gray = WMGrayColor(scr->wmscreen);
458 panel->font = WMBoldSystemFontOfSize(scr->wmscreen, WMScaleY(12));
459 panel->icons = WMCreateArray(count);
460 panel->images = WMCreateArray(count);
462 panel->win = WMCreateWindow(scr->wmscreen, "");
464 if (!panel->bg) {
465 WMFrame *frame = WMCreateFrame(panel->win);
466 WMColor *darkGray = WMDarkGrayColor(scr->wmscreen);
467 WMSetFrameRelief(frame, WRSimple);
468 WMSetViewExpandsToParent(WMWidgetView(frame), 0, 0, 0, 0);
470 panel->label = WMCreateLabel(panel->win);
471 WMResizeWidget(panel->label, width, label_height);
472 WMMoveWidget(panel->label, border_space, border_space + icon_tile_size+ 5);
473 WMSetLabelRelief(panel->label, WRSimple);
474 WMSetWidgetBackgroundColor(panel->label, darkGray);
475 WMSetLabelFont(panel->label, panel->font);
476 WMSetLabelTextColor(panel->label, panel->white);
478 WMReleaseColor(darkGray);
479 height += 5;
482 WMResizeWidget(panel->win, width + 2 * border_space, height + 2 * border_space);
484 viewport = WMCreateFrame(panel->win);
485 WMResizeWidget(viewport, width, icon_tile_size);
486 WMMoveWidget(viewport, border_space, border_space);
487 WMSetFrameRelief(viewport, WRFlat);
489 panel->iconBox = WMCreateFrame(viewport);
490 WMMoveWidget(panel->iconBox, 0, 0);
491 WMResizeWidget(panel->iconBox, icon_tile_size* count, icon_tile_size);
492 WMSetFrameRelief(panel->iconBox, WRFlat);
494 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
495 addIconForWindow(panel, panel->iconBox, wwin, i * icon_tile_size, 0);
498 WMMapSubwidgets(panel->win);
499 WMRealizeWidget(panel->win);
501 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
502 changeImage(panel, i, 0, False, True);
505 if (panel->bg) {
506 Pixmap pixmap, mask;
508 RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
510 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
512 #ifdef USE_XSHAPE
513 if (mask && w_global.xext.shape.supported)
514 XShapeCombineMask(dpy, WMWidgetXID(panel->win), ShapeBounding, 0, 0, mask, ShapeSet);
515 #endif
516 if (pixmap)
517 XFreePixmap(dpy, pixmap);
519 if (mask)
520 XFreePixmap(dpy, mask);
524 WMPoint center;
525 center = wGetPointToCenterRectInHead(scr, wGetHeadForPointerLocation(scr),
526 width + 2 * border_space, height + 2 * border_space);
527 WMMoveWidget(panel->win, center.x, center.y);
530 panel->current = WMGetFirstInArray(panel->windows, curwin);
531 if (panel->current >= 0)
532 changeImage(panel, panel->current, 1, False, False);
534 WMMapWidget(panel->win);
536 return panel;
539 void wSwitchPanelDestroy(WSwitchPanel *panel)
541 int i;
542 RImage *image;
544 if (panel->win) {
545 Window info_win = panel->scr->info_window;
546 XEvent ev;
547 ev.xclient.type = ClientMessage;
548 ev.xclient.message_type = w_global.atom.wm.ignore_focus_events;
549 ev.xclient.format = 32;
550 ev.xclient.data.l[0] = True;
552 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
553 WMUnmapWidget(panel->win);
555 ev.xclient.data.l[0] = False;
556 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
559 if (panel->images) {
560 WM_ITERATE_ARRAY(panel->images, image, i) {
561 if (image)
562 RReleaseImage(image);
564 WMFreeArray(panel->images);
567 if (panel->win)
568 WMDestroyWidget(panel->win);
570 if (panel->icons)
571 WMFreeArray(panel->icons);
573 if (panel->flags)
574 WMFreeArray(panel->flags);
576 WMFreeArray(panel->windows);
578 if (panel->tile)
579 RReleaseImage(panel->tile);
581 if (panel->tileTmp)
582 RReleaseImage(panel->tileTmp);
584 if (panel->bg)
585 RReleaseImage(panel->bg);
587 if (panel->font)
588 WMReleaseFont(panel->font);
590 if (panel->white)
591 WMReleaseColor(panel->white);
593 if (panel->gray)
594 WMReleaseColor(panel->gray);
596 wfree(panel);
599 WWindow *wSwitchPanelSelectNext(WSwitchPanel *panel, int back, int ignore_minimized, Bool class_only)
601 WWindow *wwin, *curwin, *tmpwin;
602 int count = WMGetArrayItemCount(panel->windows);
603 int orig = panel->current;
604 int i;
605 Bool dim = False;
607 if (count == 0)
608 return NULL;
610 if (!wPreferences.cycle_ignore_minimized)
611 ignore_minimized = False;
613 if (ignore_minimized && canReceiveFocus(WMGetFromArray(panel->windows, (count + panel->current) % count)) < 0)
614 ignore_minimized = False;
616 curwin = WMGetFromArray(panel->windows, orig);
617 do {
618 do {
619 if (back)
620 panel->current--;
621 else
622 panel->current++;
624 panel->current = (count + panel->current) % count;
625 wwin = WMGetFromArray(panel->windows, panel->current);
627 if (!class_only)
628 break;
629 if (panel->current == orig)
630 break;
631 } while (!sameWindowClass(wwin, curwin));
632 } while (ignore_minimized && panel->current != orig && canReceiveFocus(wwin) < 0);
634 WM_ITERATE_ARRAY(panel->windows, tmpwin, i) {
635 if (i == panel->current)
636 continue;
637 if (!class_only || sameWindowClass(tmpwin, curwin))
638 changeImage(panel, i, 0, False, False);
639 else {
640 if (i == orig)
641 dim = True;
642 changeImage(panel, i, 0, True, False);
647 if (panel->current < panel->firstVisible)
648 scrollIcons(panel, panel->current - panel->firstVisible);
649 else if (panel->current - panel->firstVisible >= panel->visibleCount)
650 scrollIcons(panel, panel->current - panel->firstVisible - panel->visibleCount + 1);
652 if (panel->win) {
653 drawTitle(panel, panel->current, wwin->frame->title);
654 if (panel->current != orig)
655 changeImage(panel, orig, 0, dim, False);
656 changeImage(panel, panel->current, 1, False, False);
659 return wwin;
662 WWindow *wSwitchPanelSelectFirst(WSwitchPanel *panel, int back)
664 WWindow *wwin;
665 int count = WMGetArrayItemCount(panel->windows);
666 char *title;
667 int i;
669 if (count == 0)
670 return NULL;
672 if (back) {
673 panel->current = count - 1;
674 scrollIcons(panel, count);
675 } else {
676 panel->current = 0;
677 scrollIcons(panel, -count);
680 wwin = WMGetFromArray(panel->windows, panel->current);
681 title = wwin->frame->title;
683 if (panel->win) {
684 for (WMArrayFirst(panel->windows, &(i)); (i) != WANotFound; WMArrayNext(panel->windows, &(i)))
685 changeImage(panel, i, i == panel->current, False, False);
687 drawTitle(panel, panel->current, title);
690 return wwin;
693 WWindow *wSwitchPanelHandleEvent(WSwitchPanel *panel, XEvent *event)
695 WMFrame *icon;
696 int i;
697 int focus = -1;
699 if (!panel->win)
700 return NULL;
702 if (event->type == MotionNotify) {
703 WM_ITERATE_ARRAY(panel->icons, icon, i) {
704 if (WMWidgetXID(icon) == event->xmotion.window) {
705 focus = i;
706 break;
711 if (focus >= 0 && panel->current != focus) {
712 WWindow *wwin;
714 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
715 changeImage(panel, i, i == focus, False, False);
717 panel->current = focus;
719 wwin = WMGetFromArray(panel->windows, focus);
721 drawTitle(panel, panel->current, wwin->frame->title);
723 return wwin;
726 return NULL;
729 Window wSwitchPanelGetWindow(WSwitchPanel *swpanel)
731 if (!swpanel->win)
732 return None;
734 return WMWidgetXID(swpanel->win);