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
))
371 if (!WFLAGP(wwin
, skip_switchpanel
))
372 WMAddToArray(windows
, wwin
);
379 static WMArray
*makeWindowFlagsArray(int count
)
381 WMArray
*flags
= WMCreateArray(count
);
384 for (i
= 0; i
< count
; i
++)
385 WMAddToArray(flags
, (void *) 0);
390 WSwitchPanel
*wInitSwitchPanel(WScreen
*scr
, WWindow
*curwin
, Bool class_only
)
393 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
395 int i
, width
, height
, iconsThatFitCount
, count
;
396 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
399 panel
->windows
= makeWindowListArray(scr
, wPreferences
.swtileImage
!= NULL
, class_only
);
400 count
= WMGetArrayItemCount(panel
->windows
);
402 panel
->flags
= makeWindowFlagsArray(count
);
405 WMFreeArray(panel
->windows
);
410 width
= ICON_TILE_SIZE
* count
;
411 iconsThatFitCount
= count
;
413 if (width
> rect
.size
.width
) {
414 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
415 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
418 panel
->visibleCount
= iconsThatFitCount
;
420 if (!wPreferences
.swtileImage
)
423 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
425 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
426 panel
->tile
= getTile();
427 if (panel
->tile
&& wPreferences
.swbackImage
[8])
428 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
430 if (!panel
->tileTmp
|| !panel
->tile
) {
432 RReleaseImage(panel
->bg
);
435 RReleaseImage(panel
->tile
);
438 RReleaseImage(panel
->tileTmp
);
439 panel
->tileTmp
= NULL
;
442 panel
->white
= WMWhiteColor(scr
->wmscreen
);
443 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
444 panel
->icons
= WMCreateArray(count
);
445 panel
->images
= WMCreateArray(count
);
447 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
450 WMFrame
*frame
= WMCreateFrame(panel
->win
);
451 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
452 WMSetFrameRelief(frame
, WRSimple
);
453 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
455 panel
->label
= WMCreateLabel(panel
->win
);
456 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
457 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
458 WMSetLabelRelief(panel
->label
, WRSimple
);
459 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
460 WMSetLabelFont(panel
->label
, panel
->font
);
461 WMSetLabelTextColor(panel
->label
, panel
->white
);
463 WMReleaseColor(darkGray
);
467 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
469 viewport
= WMCreateFrame(panel
->win
);
470 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
471 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
472 WMSetFrameRelief(viewport
, WRFlat
);
474 panel
->iconBox
= WMCreateFrame(viewport
);
475 WMMoveWidget(panel
->iconBox
, 0, 0);
476 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
477 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
479 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
480 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
483 WMMapSubwidgets(panel
->win
);
484 WMRealizeWidget(panel
->win
);
486 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
487 changeImage(panel
, i
, 0, False
, True
);
493 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
495 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
498 if (mask
&& w_global
.xext
.shape
.supported
)
499 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
502 XFreePixmap(dpy
, pixmap
);
505 XFreePixmap(dpy
, mask
);
510 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
511 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
512 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
515 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
516 if (panel
->current
>= 0)
517 changeImage(panel
, panel
->current
, 1, False
, False
);
519 WMMapWidget(panel
->win
);
524 void wSwitchPanelDestroy(WSwitchPanel
*panel
)
530 Window info_win
= panel
->scr
->info_window
;
532 ev
.xclient
.type
= ClientMessage
;
533 ev
.xclient
.message_type
= w_global
.atom
.wm
.ignore_focus_events
;
534 ev
.xclient
.format
= 32;
535 ev
.xclient
.data
.l
[0] = True
;
537 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
538 WMUnmapWidget(panel
->win
);
540 ev
.xclient
.data
.l
[0] = False
;
541 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
545 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
547 RReleaseImage(image
);
549 WMFreeArray(panel
->images
);
553 WMDestroyWidget(panel
->win
);
556 WMFreeArray(panel
->icons
);
559 WMFreeArray(panel
->flags
);
561 WMFreeArray(panel
->windows
);
564 RReleaseImage(panel
->tile
);
567 RReleaseImage(panel
->tileTmp
);
570 RReleaseImage(panel
->bg
);
573 WMReleaseFont(panel
->font
);
576 WMReleaseColor(panel
->white
);
581 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
*panel
, int back
, int ignore_minimized
, Bool class_only
)
583 WWindow
*wwin
, *curwin
, *tmpwin
;
584 int count
= WMGetArrayItemCount(panel
->windows
);
585 int orig
= panel
->current
;
592 if (!wPreferences
.cycle_ignore_minimized
)
593 ignore_minimized
= False
;
595 if (ignore_minimized
&& canReceiveFocus(WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
)) < 0)
596 ignore_minimized
= False
;
598 curwin
= WMGetFromArray(panel
->windows
, orig
);
606 panel
->current
= (count
+ panel
->current
) % count
;
607 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
611 if (panel
->current
== orig
)
613 } while (!sameWindowClass(wwin
, curwin
));
614 } while (ignore_minimized
&& panel
->current
!= orig
&& canReceiveFocus(wwin
) < 0);
616 WM_ITERATE_ARRAY(panel
->windows
, tmpwin
, i
) {
617 if (i
== panel
->current
)
619 if (!class_only
|| sameWindowClass(tmpwin
, curwin
))
620 changeImage(panel
, i
, 0, False
, False
);
624 changeImage(panel
, i
, 0, True
, False
);
629 if (panel
->current
< panel
->firstVisible
)
630 scrollIcons(panel
, panel
->current
- panel
->firstVisible
);
631 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
632 scrollIcons(panel
, panel
->current
- panel
->firstVisible
- panel
->visibleCount
+ 1);
635 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
636 if (panel
->current
!= orig
)
637 changeImage(panel
, orig
, 0, dim
, False
);
638 changeImage(panel
, panel
->current
, 1, False
, False
);
644 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
*panel
, int back
)
647 int count
= WMGetArrayItemCount(panel
->windows
);
655 panel
->current
= count
- 1;
656 scrollIcons(panel
, count
);
659 scrollIcons(panel
, -count
);
662 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
663 title
= wwin
->frame
->title
;
666 for (WMArrayFirst(panel
->windows
, &(i
)); (i
) != WANotFound
; WMArrayNext(panel
->windows
, &(i
)))
667 changeImage(panel
, i
, i
== panel
->current
, False
, False
);
669 drawTitle(panel
, panel
->current
, title
);
675 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
*panel
, XEvent
*event
)
684 if (event
->type
== MotionNotify
) {
685 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
686 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
693 if (focus
>= 0 && panel
->current
!= focus
) {
696 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
697 changeImage(panel
, i
, i
== focus
, False
, False
);
699 panel
->current
= focus
;
701 wwin
= WMGetFromArray(panel
->windows
, focus
);
703 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
711 Window
wSwitchPanelGetWindow(WSwitchPanel
*swpanel
)
716 return WMWidgetXID(swpanel
->win
);