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"
40 #include <X11/extensions/shape.h>
66 #define BORDER_SPACE 10
68 #define ICON_TILE_SIZE 64
69 #define LABEL_HEIGHT 25
70 #define SCREEN_BORDER_SPACING 2*20
72 #define ICON_SELECTED (1<<1)
73 #define ICON_DIM (1<<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 Bool
sameWindowClass(WWindow
*wwin
, WWindow
*curwin
)
99 if (!wwin
->wm_class
|| !curwin
->wm_class
)
101 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
107 static void changeImage(WSwitchPanel
*panel
, int idecks
, int selected
, Bool dim
, Bool force
)
109 WMFrame
*icon
= NULL
;
110 RImage
*image
= NULL
;
114 /* This whole function is a no-op if we aren't drawing the panel */
115 if (!wPreferences
.swtileImage
)
118 icon
= WMGetFromArray(panel
->icons
, idecks
);
119 image
= WMGetFromArray(panel
->images
, idecks
);
120 flags
= (int) (uintptr_t) WMGetFromArray(panel
->flags
, idecks
);
123 desired
|= ICON_SELECTED
;
127 if (flags
== desired
&& !force
)
130 WMReplaceInArray(panel
->flags
, idecks
, (void *) (uintptr_t) desired
);
132 if (!panel
->bg
&& !panel
->tile
&& !selected
)
133 WMSetFrameRelief(icon
, WRFlat
);
137 int opaq
= (dim
) ? 75 : 255;
142 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
145 pos
= WMGetViewPosition(WMWidgetView(icon
));
146 back
= panel
->tileTmp
;
148 RCopyArea(back
, panel
->bg
,
149 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
150 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
153 WMScreen
*wscr
= WMWidgetScreen(icon
);
155 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
156 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
157 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
158 RFillImage(back
, &color
);
163 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
164 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
167 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
168 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
171 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
172 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
173 XClearWindow(dpy
, WMWidgetXID(icon
));
177 if (!panel
->bg
&& !panel
->tile
&& selected
)
178 WMSetFrameRelief(icon
, WRSimple
);
181 static void addIconForWindow(WSwitchPanel
*panel
, WMWidget
*parent
, WWindow
*wwin
, int x
, int y
)
183 WMFrame
*icon
= WMCreateFrame(parent
);
184 RImage
*image
= NULL
;
186 WMSetFrameRelief(icon
, WRFlat
);
187 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
188 WMMoveWidget(icon
, x
, y
);
190 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
191 image
= RRetainImage(wwin
->net_icon_image
);
193 /* get_icon_image() includes the default icon image */
195 image
= get_icon_image(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
, ICON_TILE_SIZE
);
197 /* We must resize the icon size (~64) to the switch panel icon size (~48) */
198 image
= wIconValidateIconSize(image
, ICON_SIZE
);
200 WMAddToArray(panel
->images
, image
);
201 WMAddToArray(panel
->icons
, icon
);
204 static void scrollIcons(WSwitchPanel
*panel
, int delta
)
206 int nfirst
= panel
->firstVisible
+ delta
;
208 int count
= WMGetArrayItemCount(panel
->windows
);
211 if (count
<= panel
->visibleCount
)
216 else if (nfirst
>= count
- panel
->visibleCount
)
217 nfirst
= count
- panel
->visibleCount
;
219 if (nfirst
== panel
->firstVisible
)
222 WMMoveWidget(panel
->iconBox
, -nfirst
* ICON_TILE_SIZE
, 0);
224 panel
->firstVisible
= nfirst
;
226 for (i
= panel
->firstVisible
; i
< panel
->firstVisible
+ panel
->visibleCount
; i
++) {
227 if (i
== panel
->current
)
229 dim
= ((int) (uintptr_t) WMGetFromArray(panel
->flags
, i
) & ICON_DIM
);
230 changeImage(panel
, i
, 0, dim
, True
);
239 static RImage
*assemblePuzzleImage(RImage
**images
, int width
, int height
)
246 tw
= width
- images
[0]->width
- images
[2]->width
;
247 th
= height
- images
[0]->height
- images
[6]->height
;
249 if (tw
<= 0 || th
<= 0)
252 img
= RCreateImage(width
, height
, 1);
260 RFillImage(img
, &color
);
263 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
264 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 0);
268 tmp
= RSmoothScaleImage(images
[7], tw
, images
[7]->height
);
269 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
);
278 tmp
= RSmoothScaleImage(images
[5], images
[5]->width
, th
);
279 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, width
- images
[5]->width
, images
[2]->height
);
283 tmp
= RSmoothScaleImage(images
[4], tw
, th
);
284 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, images
[0]->height
);
288 RCopyArea(img
, images
[0], 0, 0, images
[0]->width
, images
[0]->height
, 0, 0);
289 RCopyArea(img
, images
[2], 0, 0, images
[2]->width
, images
[2]->height
, width
- images
[2]->width
, 0);
290 RCopyArea(img
, images
[6], 0, 0, images
[6]->width
, images
[6]->height
, 0, height
- images
[6]->height
);
291 RCopyArea(img
, images
[8], 0, 0, images
[8]->width
, images
[8]->height
,
292 width
- images
[8]->width
, height
- images
[8]->height
);
297 static RImage
*createBackImage(int width
, int height
)
299 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
302 static RImage
*getTile(void)
306 if (!wPreferences
.swtileImage
)
309 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
311 return wPreferences
.swtileImage
;
316 static void drawTitle(WSwitchPanel
*panel
, int idecks
, const char *title
)
319 int width
= WMWidgetWidth(panel
->win
);
323 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
329 if (strcmp(ntitle
, title
) != 0) {
332 int w
= WMWidthOfString(panel
->font
, ntitle
, strlen(ntitle
));
334 x
= BORDER_SPACE
+ (idecks
- panel
->firstVisible
) * ICON_TILE_SIZE
+
335 ICON_TILE_SIZE
/ 2 - w
/ 2;
336 if (x
< BORDER_SPACE
)
338 else if (x
+ w
> width
- BORDER_SPACE
)
339 x
= width
- BORDER_SPACE
- w
;
343 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
345 WMDrawString(panel
->scr
->wmscreen
,
346 WMWidgetXID(panel
->win
),
347 panel
->white
, panel
->font
,
349 WMWidgetHeight(panel
->win
) - BORDER_SPACE
- LABEL_HEIGHT
+
350 WMFontHeight(panel
->font
) / 2, ntitle
, strlen(ntitle
));
353 WMSetLabelText(panel
->label
, ntitle
);
360 static WMArray
*makeWindowListArray(WScreen
*scr
, int include_unmapped
, Bool class_only
)
362 WMArray
*windows
= WMCreateArray(10);
363 WWindow
*wwin
= scr
->focused_window
;
366 if ((canReceiveFocus(wwin
) != 0) &&
367 (wwin
->flags
.mapped
|| wwin
->flags
.shaded
|| include_unmapped
)) {
369 if (!sameWindowClass(scr
->focused_window
, wwin
)) {
373 if (!WFLAGP(wwin
, skip_switchpanel
))
374 WMAddToArray(windows
, wwin
);
381 static WMArray
*makeWindowFlagsArray(int count
)
383 WMArray
*flags
= WMCreateArray(count
);
386 for (i
= 0; i
< count
; i
++)
387 WMAddToArray(flags
, (void *) 0);
392 WSwitchPanel
*wInitSwitchPanel(WScreen
*scr
, WWindow
*curwin
, Bool class_only
)
395 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
397 int i
, width
, height
, iconsThatFitCount
, count
;
398 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
401 panel
->windows
= makeWindowListArray(scr
, wPreferences
.swtileImage
!= NULL
, class_only
);
402 count
= WMGetArrayItemCount(panel
->windows
);
404 panel
->flags
= makeWindowFlagsArray(count
);
407 WMFreeArray(panel
->windows
);
412 width
= ICON_TILE_SIZE
* count
;
413 iconsThatFitCount
= count
;
415 if (width
> rect
.size
.width
) {
416 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
417 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
420 panel
->visibleCount
= iconsThatFitCount
;
422 if (!wPreferences
.swtileImage
)
425 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
427 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
428 panel
->tile
= getTile();
429 if (panel
->tile
&& wPreferences
.swbackImage
[8])
430 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
432 if (!panel
->tileTmp
|| !panel
->tile
) {
434 RReleaseImage(panel
->bg
);
437 RReleaseImage(panel
->tile
);
440 RReleaseImage(panel
->tileTmp
);
441 panel
->tileTmp
= NULL
;
444 panel
->white
= WMWhiteColor(scr
->wmscreen
);
445 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
446 panel
->icons
= WMCreateArray(count
);
447 panel
->images
= WMCreateArray(count
);
449 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
452 WMFrame
*frame
= WMCreateFrame(panel
->win
);
453 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
454 WMSetFrameRelief(frame
, WRSimple
);
455 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
457 panel
->label
= WMCreateLabel(panel
->win
);
458 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
459 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
460 WMSetLabelRelief(panel
->label
, WRSimple
);
461 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
462 WMSetLabelFont(panel
->label
, panel
->font
);
463 WMSetLabelTextColor(panel
->label
, panel
->white
);
465 WMReleaseColor(darkGray
);
469 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
471 viewport
= WMCreateFrame(panel
->win
);
472 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
473 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
474 WMSetFrameRelief(viewport
, WRFlat
);
476 panel
->iconBox
= WMCreateFrame(viewport
);
477 WMMoveWidget(panel
->iconBox
, 0, 0);
478 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
479 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
481 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
482 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
485 WMMapSubwidgets(panel
->win
);
486 WMRealizeWidget(panel
->win
);
488 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
489 changeImage(panel
, i
, 0, False
, True
);
495 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
497 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
500 if (mask
&& w_global
.xext
.shape
.supported
)
501 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
504 XFreePixmap(dpy
, pixmap
);
507 XFreePixmap(dpy
, mask
);
512 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
513 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
514 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
517 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
518 if (panel
->current
>= 0)
519 changeImage(panel
, panel
->current
, 1, False
, False
);
521 WMMapWidget(panel
->win
);
526 void wSwitchPanelDestroy(WSwitchPanel
*panel
)
532 Window info_win
= panel
->scr
->info_window
;
534 ev
.xclient
.type
= ClientMessage
;
535 ev
.xclient
.message_type
= w_global
.atom
.wm
.ignore_focus_events
;
536 ev
.xclient
.format
= 32;
537 ev
.xclient
.data
.l
[0] = True
;
539 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
540 WMUnmapWidget(panel
->win
);
542 ev
.xclient
.data
.l
[0] = False
;
543 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
547 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
549 RReleaseImage(image
);
551 WMFreeArray(panel
->images
);
555 WMDestroyWidget(panel
->win
);
558 WMFreeArray(panel
->icons
);
561 WMFreeArray(panel
->flags
);
563 WMFreeArray(panel
->windows
);
566 RReleaseImage(panel
->tile
);
569 RReleaseImage(panel
->tileTmp
);
572 RReleaseImage(panel
->bg
);
575 WMReleaseFont(panel
->font
);
578 WMReleaseColor(panel
->white
);
583 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
*panel
, int back
, int ignore_minimized
, Bool class_only
)
585 WWindow
*wwin
, *curwin
, *tmpwin
;
586 int count
= WMGetArrayItemCount(panel
->windows
);
587 int orig
= panel
->current
;
594 if (!wPreferences
.cycle_ignore_minimized
)
595 ignore_minimized
= False
;
597 if (ignore_minimized
&& canReceiveFocus(WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
)) < 0)
598 ignore_minimized
= False
;
600 curwin
= WMGetFromArray(panel
->windows
, orig
);
608 panel
->current
= (count
+ panel
->current
) % count
;
609 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
613 if (panel
->current
== orig
)
615 } while (!sameWindowClass(wwin
, curwin
));
616 } while (ignore_minimized
&& panel
->current
!= orig
&& canReceiveFocus(wwin
) < 0);
618 WM_ITERATE_ARRAY(panel
->windows
, tmpwin
, i
) {
619 if (i
== panel
->current
)
621 if (!class_only
|| sameWindowClass(tmpwin
, curwin
))
622 changeImage(panel
, i
, 0, False
, False
);
626 changeImage(panel
, i
, 0, True
, False
);
631 if (panel
->current
< panel
->firstVisible
)
632 scrollIcons(panel
, panel
->current
- panel
->firstVisible
);
633 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
634 scrollIcons(panel
, panel
->current
- panel
->firstVisible
- panel
->visibleCount
+ 1);
637 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
638 if (panel
->current
!= orig
)
639 changeImage(panel
, orig
, 0, dim
, False
);
640 changeImage(panel
, panel
->current
, 1, False
, False
);
646 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
*panel
, int back
)
649 int count
= WMGetArrayItemCount(panel
->windows
);
657 panel
->current
= count
- 1;
658 scrollIcons(panel
, count
);
661 scrollIcons(panel
, -count
);
664 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
665 title
= wwin
->frame
->title
;
668 for (WMArrayFirst(panel
->windows
, &(i
)); (i
) != WANotFound
; WMArrayNext(panel
->windows
, &(i
)))
669 changeImage(panel
, i
, i
== panel
->current
, False
, False
);
671 drawTitle(panel
, panel
->current
, title
);
677 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
*panel
, XEvent
*event
)
686 if (event
->type
== MotionNotify
) {
687 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
688 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
695 if (focus
>= 0 && panel
->current
!= focus
) {
698 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
699 changeImage(panel
, i
, i
== focus
, False
, False
);
701 panel
->current
= focus
;
703 wwin
= WMGetFromArray(panel
->windows
, focus
);
705 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
713 Window
wSwitchPanelGetWindow(WSwitchPanel
*swpanel
)
718 return WMWidgetXID(swpanel
->win
);