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.
27 #include "WindowMaker.h"
32 #include "switchpanel.h"
36 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
39 #include <X11/extensions/shape.h>
41 extern Bool wShapeSupported
;
66 extern WPreferences wPreferences
;
68 #define BORDER_SPACE 10
70 #define ICON_TILE_SIZE 64
71 #define LABEL_HEIGHT 25
72 #define SCREEN_BORDER_SPACING 2*20
73 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
75 static int canReceiveFocus(WWindow
*wwin
)
77 if (wwin
->frame
->workspace
!= wwin
->screen_ptr
->current_workspace
)
80 if (wPreferences
.cycle_active_head_only
&&
81 wGetHeadForWindow(wwin
) != wGetHeadForPointerLocation(wwin
->screen_ptr
))
84 if (WFLAGP(wwin
, no_focusable
))
87 if (!wwin
->flags
.mapped
) {
88 if (!wwin
->flags
.shaded
&& !wwin
->flags
.miniaturized
&& !wwin
->flags
.hidden
)
97 static void changeImage(WSwitchPanel
*panel
, int idecks
, int selected
)
99 WMFrame
*icon
= WMGetFromArray(panel
->icons
, idecks
);
100 RImage
*image
= WMGetFromArray(panel
->images
, idecks
);
102 if (!panel
->bg
&& !panel
->tile
&& !selected
)
103 WMSetFrameRelief(icon
, WRFlat
);
112 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
115 pos
= WMGetViewPosition(WMWidgetView(icon
));
116 back
= panel
->tileTmp
;
118 RCopyArea(back
, panel
->bg
,
119 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
120 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
123 WMScreen
*wscr
= WMWidgetScreen(icon
);
125 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
126 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
127 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
128 RFillImage(back
, &color
);
133 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
134 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
137 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
138 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
141 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
142 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
143 XClearWindow(dpy
, WMWidgetXID(icon
));
147 if (!panel
->bg
&& !panel
->tile
&& selected
)
148 WMSetFrameRelief(icon
, WRSimple
);
151 static RImage
*scaleDownIfNeeded(RImage
*image
)
153 if (image
&& ((image
->width
- ICON_SIZE
) > 2 || (image
->height
- ICON_SIZE
) > 2)) {
155 nimage
= RScaleImage(image
, ICON_SIZE
, (image
->height
* ICON_SIZE
/ image
->width
));
156 RReleaseImage(image
);
163 static void addIconForWindow(WSwitchPanel
*panel
, WMWidget
*parent
, WWindow
*wwin
, int x
, int y
)
165 WMFrame
*icon
= WMCreateFrame(parent
);
166 RImage
*image
= NULL
;
168 WMSetFrameRelief(icon
, WRFlat
);
169 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
170 WMMoveWidget(icon
, x
, y
);
172 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
173 image
= RRetainImage(wwin
->net_icon_image
);
175 /* wDefaultGetImage() includes the default icon image */
177 image
= wDefaultGetImage(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
, ICON_TILE_SIZE
);
179 image
= scaleDownIfNeeded(image
);
181 WMAddToArray(panel
->images
, image
);
182 WMAddToArray(panel
->icons
, icon
);
185 static void scrollIcons(WSwitchPanel
*panel
, int delta
)
187 int nfirst
= panel
->firstVisible
+ delta
;
189 int count
= WMGetArrayItemCount(panel
->windows
);
191 if (count
<= panel
->visibleCount
)
196 else if (nfirst
>= count
- panel
->visibleCount
)
197 nfirst
= count
- panel
->visibleCount
;
199 if (nfirst
== panel
->firstVisible
)
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
);
215 static RImage
*assemblePuzzleImage(RImage
**images
, int width
, int height
)
217 RImage
*img
= RCreateImage(width
, height
, 1);
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)
239 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
240 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 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
);
251 tmp
= RSmoothScaleImage(images
[3], images
[3]->width
, th
);
252 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, 0, images
[0]->height
);
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
);
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
);
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
);
278 static RImage
*createBackImage(int width
, int height
)
280 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
283 static RImage
*getTile(void)
287 if (!wPreferences
.swtileImage
)
290 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
292 return wPreferences
.swtileImage
;
297 static void drawTitle(WSwitchPanel
*panel
, int idecks
, char *title
)
300 int width
= WMWidgetWidth(panel
->win
);
304 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
310 if (strcmp(ntitle
, title
) != 0) {
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
)
319 else if (x
+ w
> width
- BORDER_SPACE
)
320 x
= width
- BORDER_SPACE
- w
;
324 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
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
));
334 WMSetLabelText(panel
->label
, ntitle
);
341 static WMArray
*makeWindowListArray(WWindow
*curwin
, int include_unmapped
, Bool class_only
)
343 WMArray
*windows
= WMCreateArray(10);
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
)) {
352 if (!wwin
->wm_class
|| !curwin
->wm_class
)
354 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
358 if (!WFLAGP(wwin
, skip_switchpanel
))
359 WMAddToArray(windows
, wwin
);
363 /* start over from the beginning of the list */
367 for (wwin
= curwin
; wwin
&& wwin
!= curwin
; wwin
= wwin
->prev
) {
368 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
369 (wwin
->flags
.mapped
|| include_unmapped
)) {
371 if (!wwin
->wm_class
|| !curwin
->wm_class
)
373 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
377 if (!WFLAGP(wwin
, skip_switchpanel
))
378 WMAddToArray(windows
, wwin
);
386 WSwitchPanel
*wInitSwitchPanel(WScreen
*scr
, WWindow
*curwin
, Bool class_only
)
389 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
391 int i
, width
, height
, iconsThatFitCount
, count
;
392 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
395 panel
->windows
= makeWindowListArray(curwin
, wPreferences
.swtileImage
!= NULL
, class_only
);
396 count
= WMGetArrayItemCount(panel
->windows
);
399 WMFreeArray(panel
->windows
);
404 width
= ICON_TILE_SIZE
* count
;
405 iconsThatFitCount
= count
;
407 if (width
> rect
.size
.width
) {
408 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
409 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
412 panel
->visibleCount
= iconsThatFitCount
;
414 if (!wPreferences
.swtileImage
)
417 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
419 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
420 panel
->tile
= getTile();
421 if (panel
->tile
&& wPreferences
.swbackImage
[8])
422 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
424 if (!panel
->tileTmp
|| !panel
->tile
) {
426 RReleaseImage(panel
->bg
);
429 RReleaseImage(panel
->tile
);
432 RReleaseImage(panel
->tileTmp
);
433 panel
->tileTmp
= NULL
;
436 panel
->white
= WMWhiteColor(scr
->wmscreen
);
437 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
438 panel
->icons
= WMCreateArray(count
);
439 panel
->images
= WMCreateArray(count
);
441 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
444 WMFrame
*frame
= WMCreateFrame(panel
->win
);
445 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
446 WMSetFrameRelief(frame
, WRSimple
);
447 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
449 panel
->label
= WMCreateLabel(panel
->win
);
450 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
451 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
452 WMSetLabelRelief(panel
->label
, WRSimple
);
453 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
454 WMSetLabelFont(panel
->label
, panel
->font
);
455 WMSetLabelTextColor(panel
->label
, panel
->white
);
457 WMReleaseColor(darkGray
);
461 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
463 viewport
= WMCreateFrame(panel
->win
);
464 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
465 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
466 WMSetFrameRelief(viewport
, WRFlat
);
468 panel
->iconBox
= WMCreateFrame(viewport
);
469 WMMoveWidget(panel
->iconBox
, 0, 0);
470 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
471 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
473 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
474 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
477 WMMapSubwidgets(panel
->win
);
478 WMRealizeWidget(panel
->win
);
480 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
481 changeImage(panel
, i
, 0);
487 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
489 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
492 if (mask
&& wShapeSupported
)
493 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
496 XFreePixmap(dpy
, pixmap
);
499 XFreePixmap(dpy
, mask
);
504 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
505 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
506 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
509 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
510 if (panel
->current
>= 0)
511 changeImage(panel
, panel
->current
, 1);
513 WMMapWidget(panel
->win
);
518 void wSwitchPanelDestroy(WSwitchPanel
*panel
)
524 Window info_win
= panel
->scr
->info_window
;
526 ev
.xclient
.type
= ClientMessage
;
527 ev
.xclient
.message_type
= _XA_WM_IGNORE_FOCUS_EVENTS
;
528 ev
.xclient
.format
= 32;
529 ev
.xclient
.data
.l
[0] = True
;
531 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
532 WMUnmapWidget(panel
->win
);
534 ev
.xclient
.data
.l
[0] = False
;
535 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
539 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
541 RReleaseImage(image
);
543 WMFreeArray(panel
->images
);
547 WMDestroyWidget(panel
->win
);
550 WMFreeArray(panel
->icons
);
552 WMFreeArray(panel
->windows
);
555 RReleaseImage(panel
->tile
);
558 RReleaseImage(panel
->tileTmp
);
561 RReleaseImage(panel
->bg
);
564 WMReleaseFont(panel
->font
);
567 WMReleaseColor(panel
->white
);
572 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
*panel
, int back
)
575 int count
= WMGetArrayItemCount(panel
->windows
);
581 changeImage(panel
, panel
->current
, 0);
588 wwin
= WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
);
591 if (panel
->current
< 0)
592 scrollIcons(panel
, count
);
593 else if (panel
->current
< panel
->firstVisible
)
594 scrollIcons(panel
, -1);
596 if (panel
->current
>= count
)
597 scrollIcons(panel
, -count
);
598 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
599 scrollIcons(panel
, 1);
602 panel
->current
= (count
+ panel
->current
) % count
;
605 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
607 changeImage(panel
, panel
->current
, 1);
613 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
*panel
, int back
)
616 int count
= WMGetArrayItemCount(panel
->windows
);
622 changeImage(panel
, panel
->current
, 0);
625 panel
->current
= count
- 1;
626 scrollIcons(panel
, count
);
629 scrollIcons(panel
, -count
);
632 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
635 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
636 changeImage(panel
, panel
->current
, 1);
642 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
*panel
, XEvent
*event
)
651 if (event
->type
== MotionNotify
) {
652 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
653 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
660 if (focus
>= 0 && panel
->current
!= focus
) {
663 changeImage(panel
, panel
->current
, 0);
664 changeImage(panel
, focus
, 1);
665 panel
->current
= focus
;
667 wwin
= WMGetFromArray(panel
->windows
, focus
);
669 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
677 Window
wSwitchPanelGetWindow(WSwitchPanel
*swpanel
)
682 return WMWidgetXID(swpanel
->win
);