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.
28 #include "WindowMaker.h"
34 #include "switchpanel.h"
38 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
41 #include <X11/extensions/shape.h>
43 extern Bool wShapeSupported
;
69 extern WPreferences wPreferences
;
71 #define BORDER_SPACE 10
73 #define ICON_TILE_SIZE 64
74 #define LABEL_HEIGHT 25
75 #define SCREEN_BORDER_SPACING 2*20
76 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
78 #define ICON_SELECTED (1<<1)
79 #define ICON_DIM (1<<2)
81 static int canReceiveFocus(WWindow
*wwin
)
83 if (wwin
->frame
->workspace
!= wwin
->screen_ptr
->current_workspace
)
86 if (wPreferences
.cycle_active_head_only
&&
87 wGetHeadForWindow(wwin
) != wGetHeadForPointerLocation(wwin
->screen_ptr
))
90 if (WFLAGP(wwin
, no_focusable
))
93 if (!wwin
->flags
.mapped
) {
94 if (!wwin
->flags
.shaded
&& !wwin
->flags
.miniaturized
&& !wwin
->flags
.hidden
)
103 static Bool
sameWindowClass(WWindow
*wwin
, WWindow
*curwin
)
105 if (!wwin
->wm_class
|| !curwin
->wm_class
)
107 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
113 static void changeImage(WSwitchPanel
*panel
, int idecks
, int selected
, Bool dim
, Bool force
)
115 WMFrame
*icon
= WMGetFromArray(panel
->icons
, idecks
);
116 RImage
*image
= WMGetFromArray(panel
->images
, idecks
);
117 char flags
= (char) (uintptr_t) WMGetFromArray(panel
->flags
, idecks
);
121 desired
|= ICON_SELECTED
;
125 if (flags
== desired
&& !force
)
128 WMReplaceInArray(panel
->flags
, idecks
, (void *) (uintptr_t) desired
);
130 if (!panel
->bg
&& !panel
->tile
&& !selected
)
131 WMSetFrameRelief(icon
, WRFlat
);
135 int opaq
= (dim
) ? 75 : 255;
140 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
143 pos
= WMGetViewPosition(WMWidgetView(icon
));
144 back
= panel
->tileTmp
;
146 RCopyArea(back
, panel
->bg
,
147 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
148 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
151 WMScreen
*wscr
= WMWidgetScreen(icon
);
153 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
154 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
155 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
156 RFillImage(back
, &color
);
161 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
162 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
165 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
166 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
169 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
170 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
171 XClearWindow(dpy
, WMWidgetXID(icon
));
175 if (!panel
->bg
&& !panel
->tile
&& selected
)
176 WMSetFrameRelief(icon
, WRSimple
);
179 static void addIconForWindow(WSwitchPanel
*panel
, WMWidget
*parent
, WWindow
*wwin
, int x
, int y
)
181 WMFrame
*icon
= WMCreateFrame(parent
);
182 RImage
*image
= NULL
;
184 WMSetFrameRelief(icon
, WRFlat
);
185 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
186 WMMoveWidget(icon
, x
, y
);
188 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
189 image
= RRetainImage(wwin
->net_icon_image
);
191 /* get_icon_image() includes the default icon image */
193 image
= get_icon_image(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
, ICON_TILE_SIZE
);
195 /* We must resize the icon size (~64) to the switchpanel icon size (~48) */
196 image
= wIconValidateIconSize(image
, ICON_SIZE
);
198 WMAddToArray(panel
->images
, image
);
199 WMAddToArray(panel
->icons
, icon
);
202 static void scrollIcons(WSwitchPanel
*panel
, int delta
)
204 int nfirst
= panel
->firstVisible
+ delta
;
206 int count
= WMGetArrayItemCount(panel
->windows
);
209 if (count
<= panel
->visibleCount
)
214 else if (nfirst
>= count
- panel
->visibleCount
)
215 nfirst
= count
- panel
->visibleCount
;
217 if (nfirst
== panel
->firstVisible
)
220 WMMoveWidget(panel
->iconBox
, -nfirst
* ICON_TILE_SIZE
, 0);
222 panel
->firstVisible
= nfirst
;
224 for (i
= panel
->firstVisible
; i
< panel
->firstVisible
+ panel
->visibleCount
; i
++) {
225 if (i
== panel
->current
)
227 dim
= ((char) (uintptr_t) WMGetFromArray(panel
->flags
, i
) & ICON_DIM
);
228 changeImage(panel
, i
, 0, dim
, True
);
237 static RImage
*assemblePuzzleImage(RImage
**images
, int width
, int height
)
239 RImage
*img
= RCreateImage(width
, height
, 1);
251 RFillImage(img
, &color
);
253 tw
= width
- images
[0]->width
- images
[2]->width
;
254 th
= height
- images
[0]->height
- images
[6]->height
;
256 if (tw
<= 0 || th
<= 0)
261 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
262 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 0);
267 tmp
= RSmoothScaleImage(images
[7], tw
, images
[7]->height
);
268 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[6]->width
, height
- images
[6]->height
);
273 tmp
= RSmoothScaleImage(images
[3], images
[3]->width
, th
);
274 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, 0, images
[0]->height
);
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
);
284 if (tw
> 0 && th
> 0) {
285 tmp
= RSmoothScaleImage(images
[4], tw
, th
);
286 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, images
[0]->height
);
291 RCopyArea(img
, images
[0], 0, 0, images
[0]->width
, images
[0]->height
, 0, 0);
292 RCopyArea(img
, images
[2], 0, 0, images
[2]->width
, images
[2]->height
, width
- images
[2]->width
, 0);
293 RCopyArea(img
, images
[6], 0, 0, images
[6]->width
, images
[6]->height
, 0, height
- images
[6]->height
);
294 RCopyArea(img
, images
[8], 0, 0, images
[8]->width
, images
[8]->height
,
295 width
- images
[8]->width
, height
- images
[8]->height
);
300 static RImage
*createBackImage(int width
, int height
)
302 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
305 static RImage
*getTile(void)
309 if (!wPreferences
.swtileImage
)
312 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
314 return wPreferences
.swtileImage
;
319 static void drawTitle(WSwitchPanel
*panel
, int idecks
, const char *title
)
322 int width
= WMWidgetWidth(panel
->win
);
326 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
332 if (strcmp(ntitle
, title
) != 0) {
335 int w
= WMWidthOfString(panel
->font
, ntitle
, strlen(ntitle
));
337 x
= BORDER_SPACE
+ (idecks
- panel
->firstVisible
) * ICON_TILE_SIZE
+
338 ICON_TILE_SIZE
/ 2 - w
/ 2;
339 if (x
< BORDER_SPACE
)
341 else if (x
+ w
> width
- BORDER_SPACE
)
342 x
= width
- BORDER_SPACE
- w
;
346 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
348 WMDrawString(panel
->scr
->wmscreen
,
349 WMWidgetXID(panel
->win
),
350 panel
->white
, panel
->font
,
352 WMWidgetHeight(panel
->win
) - BORDER_SPACE
- LABEL_HEIGHT
+
353 WMFontHeight(panel
->font
) / 2, ntitle
, strlen(ntitle
));
356 WMSetLabelText(panel
->label
, ntitle
);
363 static WMArray
*makeWindowListArray(WWindow
*curwin
, int include_unmapped
, Bool class_only
)
365 WMArray
*windows
= WMCreateArray(10);
369 for (fl
= 0; fl
< 2; fl
++) {
370 for (wwin
= curwin
; wwin
; wwin
= wwin
->prev
) {
371 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
372 (wwin
->flags
.mapped
|| include_unmapped
)) {
374 if (!sameWindowClass(wwin
, curwin
))
377 if (!WFLAGP(wwin
, skip_switchpanel
))
378 WMAddToArray(windows
, wwin
);
382 /* start over from the beginning of the list */
386 for (wwin
= curwin
; wwin
&& wwin
!= curwin
; wwin
= wwin
->prev
) {
387 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
388 (wwin
->flags
.mapped
|| include_unmapped
)) {
390 if (!sameWindowClass(wwin
, curwin
))
392 if (!WFLAGP(wwin
, skip_switchpanel
))
393 WMAddToArray(windows
, wwin
);
401 static WMArray
*makeWindowFlagsArray(int count
)
403 WMArray
*flags
= WMCreateArray(1);
406 for (i
= 0; i
< count
; i
++)
407 WMAddToArray(flags
, (char) 0);
412 WSwitchPanel
*wInitSwitchPanel(WScreen
*scr
, WWindow
*curwin
, Bool class_only
)
415 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
417 int i
, width
, height
, iconsThatFitCount
, count
;
418 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
421 panel
->windows
= makeWindowListArray(curwin
, wPreferences
.swtileImage
!= NULL
, class_only
);
422 count
= WMGetArrayItemCount(panel
->windows
);
424 panel
->flags
= makeWindowFlagsArray(count
);
427 WMFreeArray(panel
->windows
);
432 width
= ICON_TILE_SIZE
* count
;
433 iconsThatFitCount
= count
;
435 if (width
> rect
.size
.width
) {
436 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
437 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
440 panel
->visibleCount
= iconsThatFitCount
;
442 if (!wPreferences
.swtileImage
)
445 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
447 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
448 panel
->tile
= getTile();
449 if (panel
->tile
&& wPreferences
.swbackImage
[8])
450 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
452 if (!panel
->tileTmp
|| !panel
->tile
) {
454 RReleaseImage(panel
->bg
);
457 RReleaseImage(panel
->tile
);
460 RReleaseImage(panel
->tileTmp
);
461 panel
->tileTmp
= NULL
;
464 panel
->white
= WMWhiteColor(scr
->wmscreen
);
465 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
466 panel
->icons
= WMCreateArray(count
);
467 panel
->images
= WMCreateArray(count
);
469 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
472 WMFrame
*frame
= WMCreateFrame(panel
->win
);
473 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
474 WMSetFrameRelief(frame
, WRSimple
);
475 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
477 panel
->label
= WMCreateLabel(panel
->win
);
478 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
479 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
480 WMSetLabelRelief(panel
->label
, WRSimple
);
481 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
482 WMSetLabelFont(panel
->label
, panel
->font
);
483 WMSetLabelTextColor(panel
->label
, panel
->white
);
485 WMReleaseColor(darkGray
);
489 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
491 viewport
= WMCreateFrame(panel
->win
);
492 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
493 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
494 WMSetFrameRelief(viewport
, WRFlat
);
496 panel
->iconBox
= WMCreateFrame(viewport
);
497 WMMoveWidget(panel
->iconBox
, 0, 0);
498 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
499 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
501 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
502 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
505 WMMapSubwidgets(panel
->win
);
506 WMRealizeWidget(panel
->win
);
508 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
509 changeImage(panel
, i
, 0, False
, True
);
515 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
517 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
520 if (mask
&& wShapeSupported
)
521 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
524 XFreePixmap(dpy
, pixmap
);
527 XFreePixmap(dpy
, mask
);
532 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
533 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
534 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
537 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
538 if (panel
->current
>= 0)
539 changeImage(panel
, panel
->current
, 1, False
, False
);
541 WMMapWidget(panel
->win
);
546 void wSwitchPanelDestroy(WSwitchPanel
*panel
)
552 Window info_win
= panel
->scr
->info_window
;
554 ev
.xclient
.type
= ClientMessage
;
555 ev
.xclient
.message_type
= _XA_WM_IGNORE_FOCUS_EVENTS
;
556 ev
.xclient
.format
= 32;
557 ev
.xclient
.data
.l
[0] = True
;
559 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
560 WMUnmapWidget(panel
->win
);
562 ev
.xclient
.data
.l
[0] = False
;
563 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
567 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
569 RReleaseImage(image
);
571 WMFreeArray(panel
->images
);
575 WMDestroyWidget(panel
->win
);
578 WMFreeArray(panel
->icons
);
581 WMFreeArray(panel
->flags
);
583 WMFreeArray(panel
->windows
);
586 RReleaseImage(panel
->tile
);
589 RReleaseImage(panel
->tileTmp
);
592 RReleaseImage(panel
->bg
);
595 WMReleaseFont(panel
->font
);
598 WMReleaseColor(panel
->white
);
603 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
*panel
, int back
, int ignore_minimized
, Bool class_only
)
605 WWindow
*wwin
, *curwin
, *tmpwin
;
606 int count
= WMGetArrayItemCount(panel
->windows
);
607 int orig
= panel
->current
;
614 if (!wPreferences
.cycle_ignore_minimized
)
615 ignore_minimized
= False
;
617 if (ignore_minimized
&& canReceiveFocus(WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
)) < 0)
618 ignore_minimized
= False
;
620 curwin
= WMGetFromArray(panel
->windows
, orig
);
628 panel
->current
= (count
+ panel
->current
) % count
;
629 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
633 if (panel
->current
== orig
)
635 } while (!sameWindowClass(wwin
, curwin
));
636 } while (ignore_minimized
&& panel
->current
!= orig
&& canReceiveFocus(wwin
) < 0);
638 WM_ITERATE_ARRAY(panel
->windows
, tmpwin
, i
) {
639 if (i
== panel
->current
)
641 if (!class_only
|| sameWindowClass(tmpwin
, curwin
))
642 changeImage(panel
, i
, 0, False
, False
);
646 changeImage(panel
, i
, 0, True
, False
);
651 if (panel
->current
< panel
->firstVisible
)
652 scrollIcons(panel
, panel
->current
- panel
->firstVisible
);
653 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
654 scrollIcons(panel
, panel
->current
- panel
->firstVisible
- panel
->visibleCount
+ 1);
657 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
658 if (panel
->current
!= orig
)
659 changeImage(panel
, orig
, 0, dim
, False
);
660 changeImage(panel
, panel
->current
, 1, False
, False
);
666 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
*panel
, int back
)
669 int count
= WMGetArrayItemCount(panel
->windows
);
677 panel
->current
= count
- 1;
678 scrollIcons(panel
, count
);
681 scrollIcons(panel
, -count
);
684 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
685 title
= wwin
->frame
->title
;
688 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
689 changeImage(panel
, i
, i
== panel
->current
, False
, False
);
691 drawTitle(panel
, panel
->current
, title
);
697 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
*panel
, XEvent
*event
)
706 if (event
->type
== MotionNotify
) {
707 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
708 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
715 if (focus
>= 0 && panel
->current
!= focus
) {
718 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
719 changeImage(panel
, i
, i
== focus
, False
, False
);
721 panel
->current
= focus
;
723 wwin
= WMGetFromArray(panel
->windows
, focus
);
725 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
733 Window
wSwitchPanelGetWindow(WSwitchPanel
*swpanel
)
738 return WMWidgetXID(swpanel
->win
);