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"
33 #include "switchpanel.h"
37 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
40 #include <X11/extensions/shape.h>
42 extern Bool wShapeSupported
;
67 extern WPreferences wPreferences
;
69 #define BORDER_SPACE 10
71 #define ICON_TILE_SIZE 64
72 #define LABEL_HEIGHT 25
73 #define SCREEN_BORDER_SPACING 2*20
74 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
76 static int canReceiveFocus(WWindow
*wwin
)
78 if (wwin
->frame
->workspace
!= wwin
->screen_ptr
->current_workspace
)
81 if (wPreferences
.cycle_active_head_only
&&
82 wGetHeadForWindow(wwin
) != wGetHeadForPointerLocation(wwin
->screen_ptr
))
85 if (WFLAGP(wwin
, no_focusable
))
88 if (!wwin
->flags
.mapped
) {
89 if (!wwin
->flags
.shaded
&& !wwin
->flags
.miniaturized
&& !wwin
->flags
.hidden
)
98 static void changeImage(WSwitchPanel
*panel
, int idecks
, int selected
)
100 WMFrame
*icon
= WMGetFromArray(panel
->icons
, idecks
);
101 RImage
*image
= WMGetFromArray(panel
->images
, idecks
);
103 if (!panel
->bg
&& !panel
->tile
&& !selected
)
104 WMSetFrameRelief(icon
, WRFlat
);
113 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
116 pos
= WMGetViewPosition(WMWidgetView(icon
));
117 back
= panel
->tileTmp
;
119 RCopyArea(back
, panel
->bg
,
120 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
121 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
124 WMScreen
*wscr
= WMWidgetScreen(icon
);
126 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
127 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
128 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
129 RFillImage(back
, &color
);
134 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
135 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
138 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
139 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
142 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
143 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
144 XClearWindow(dpy
, WMWidgetXID(icon
));
148 if (!panel
->bg
&& !panel
->tile
&& selected
)
149 WMSetFrameRelief(icon
, WRSimple
);
152 static void addIconForWindow(WSwitchPanel
*panel
, WMWidget
*parent
, WWindow
*wwin
, int x
, int y
)
154 WMFrame
*icon
= WMCreateFrame(parent
);
155 RImage
*image
= NULL
;
157 WMSetFrameRelief(icon
, WRFlat
);
158 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
159 WMMoveWidget(icon
, x
, y
);
161 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
162 image
= RRetainImage(wwin
->net_icon_image
);
164 /* wDefaultGetImage() includes the default icon image */
166 image
= wDefaultGetImage(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
, ICON_TILE_SIZE
);
168 /* We must resize the icon size (~64) to the switchpanel icon size (~48) */
169 image
= wIconValidateIconSize(image
, ICON_SIZE
);
171 WMAddToArray(panel
->images
, image
);
172 WMAddToArray(panel
->icons
, icon
);
175 static void scrollIcons(WSwitchPanel
*panel
, int delta
)
177 int nfirst
= panel
->firstVisible
+ delta
;
179 int count
= WMGetArrayItemCount(panel
->windows
);
181 if (count
<= panel
->visibleCount
)
186 else if (nfirst
>= count
- panel
->visibleCount
)
187 nfirst
= count
- panel
->visibleCount
;
189 if (nfirst
== panel
->firstVisible
)
192 WMMoveWidget(panel
->iconBox
, -nfirst
* ICON_TILE_SIZE
, 0);
194 panel
->firstVisible
= nfirst
;
196 for (i
= panel
->firstVisible
; i
< panel
->firstVisible
+ panel
->visibleCount
; i
++)
197 changeImage(panel
, i
, i
== panel
->current
);
205 static RImage
*assemblePuzzleImage(RImage
**images
, int width
, int height
)
207 RImage
*img
= RCreateImage(width
, height
, 1);
219 RFillImage(img
, &color
);
221 tw
= width
- images
[0]->width
- images
[2]->width
;
222 th
= height
- images
[0]->height
- images
[6]->height
;
224 if (tw
<= 0 || th
<= 0)
229 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
230 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 0);
235 tmp
= RSmoothScaleImage(images
[7], tw
, images
[7]->height
);
236 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[6]->width
, height
- images
[6]->height
);
241 tmp
= RSmoothScaleImage(images
[3], images
[3]->width
, th
);
242 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, 0, images
[0]->height
);
247 tmp
= RSmoothScaleImage(images
[5], images
[5]->width
, th
);
248 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, width
- images
[5]->width
, images
[2]->height
);
252 if (tw
> 0 && th
> 0) {
253 tmp
= RSmoothScaleImage(images
[4], tw
, th
);
254 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, images
[0]->height
);
259 RCopyArea(img
, images
[0], 0, 0, images
[0]->width
, images
[0]->height
, 0, 0);
260 RCopyArea(img
, images
[2], 0, 0, images
[2]->width
, images
[2]->height
, width
- images
[2]->width
, 0);
261 RCopyArea(img
, images
[6], 0, 0, images
[6]->width
, images
[6]->height
, 0, height
- images
[6]->height
);
262 RCopyArea(img
, images
[8], 0, 0, images
[8]->width
, images
[8]->height
,
263 width
- images
[8]->width
, height
- images
[8]->height
);
268 static RImage
*createBackImage(int width
, int height
)
270 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
273 static RImage
*getTile(void)
277 if (!wPreferences
.swtileImage
)
280 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
282 return wPreferences
.swtileImage
;
287 static void drawTitle(WSwitchPanel
*panel
, int idecks
, char *title
)
290 int width
= WMWidgetWidth(panel
->win
);
294 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
300 if (strcmp(ntitle
, title
) != 0) {
303 int w
= WMWidthOfString(panel
->font
, ntitle
, strlen(ntitle
));
305 x
= BORDER_SPACE
+ (idecks
- panel
->firstVisible
) * ICON_TILE_SIZE
+
306 ICON_TILE_SIZE
/ 2 - w
/ 2;
307 if (x
< BORDER_SPACE
)
309 else if (x
+ w
> width
- BORDER_SPACE
)
310 x
= width
- BORDER_SPACE
- w
;
314 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
316 WMDrawString(panel
->scr
->wmscreen
,
317 WMWidgetXID(panel
->win
),
318 panel
->white
, panel
->font
,
320 WMWidgetHeight(panel
->win
) - BORDER_SPACE
- LABEL_HEIGHT
+
321 WMFontHeight(panel
->font
) / 2, ntitle
, strlen(ntitle
));
324 WMSetLabelText(panel
->label
, ntitle
);
331 static WMArray
*makeWindowListArray(WWindow
*curwin
, int include_unmapped
, Bool class_only
)
333 WMArray
*windows
= WMCreateArray(10);
337 for (fl
= 0; fl
< 2; fl
++) {
338 for (wwin
= curwin
; wwin
; wwin
= wwin
->prev
) {
339 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
340 (wwin
->flags
.mapped
|| include_unmapped
)) {
342 if (!wwin
->wm_class
|| !curwin
->wm_class
)
344 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
348 if (!WFLAGP(wwin
, skip_switchpanel
))
349 WMAddToArray(windows
, wwin
);
353 /* start over from the beginning of the list */
357 for (wwin
= curwin
; wwin
&& wwin
!= curwin
; wwin
= wwin
->prev
) {
358 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
359 (wwin
->flags
.mapped
|| include_unmapped
)) {
361 if (!wwin
->wm_class
|| !curwin
->wm_class
)
363 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
367 if (!WFLAGP(wwin
, skip_switchpanel
))
368 WMAddToArray(windows
, wwin
);
376 WSwitchPanel
*wInitSwitchPanel(WScreen
*scr
, WWindow
*curwin
, Bool class_only
)
379 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
381 int i
, width
, height
, iconsThatFitCount
, count
;
382 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
385 panel
->windows
= makeWindowListArray(curwin
, wPreferences
.swtileImage
!= NULL
, class_only
);
386 count
= WMGetArrayItemCount(panel
->windows
);
389 WMFreeArray(panel
->windows
);
394 width
= ICON_TILE_SIZE
* count
;
395 iconsThatFitCount
= count
;
397 if (width
> rect
.size
.width
) {
398 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
399 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
402 panel
->visibleCount
= iconsThatFitCount
;
404 if (!wPreferences
.swtileImage
)
407 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
409 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
410 panel
->tile
= getTile();
411 if (panel
->tile
&& wPreferences
.swbackImage
[8])
412 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
414 if (!panel
->tileTmp
|| !panel
->tile
) {
416 RReleaseImage(panel
->bg
);
419 RReleaseImage(panel
->tile
);
422 RReleaseImage(panel
->tileTmp
);
423 panel
->tileTmp
= NULL
;
426 panel
->white
= WMWhiteColor(scr
->wmscreen
);
427 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
428 panel
->icons
= WMCreateArray(count
);
429 panel
->images
= WMCreateArray(count
);
431 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
434 WMFrame
*frame
= WMCreateFrame(panel
->win
);
435 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
436 WMSetFrameRelief(frame
, WRSimple
);
437 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
439 panel
->label
= WMCreateLabel(panel
->win
);
440 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
441 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
442 WMSetLabelRelief(panel
->label
, WRSimple
);
443 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
444 WMSetLabelFont(panel
->label
, panel
->font
);
445 WMSetLabelTextColor(panel
->label
, panel
->white
);
447 WMReleaseColor(darkGray
);
451 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
453 viewport
= WMCreateFrame(panel
->win
);
454 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
455 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
456 WMSetFrameRelief(viewport
, WRFlat
);
458 panel
->iconBox
= WMCreateFrame(viewport
);
459 WMMoveWidget(panel
->iconBox
, 0, 0);
460 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
461 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
463 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
464 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
467 WMMapSubwidgets(panel
->win
);
468 WMRealizeWidget(panel
->win
);
470 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
471 changeImage(panel
, i
, 0);
477 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
479 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
482 if (mask
&& wShapeSupported
)
483 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
486 XFreePixmap(dpy
, pixmap
);
489 XFreePixmap(dpy
, mask
);
494 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
495 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
496 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
499 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
500 if (panel
->current
>= 0)
501 changeImage(panel
, panel
->current
, 1);
503 WMMapWidget(panel
->win
);
508 void wSwitchPanelDestroy(WSwitchPanel
*panel
)
514 Window info_win
= panel
->scr
->info_window
;
516 ev
.xclient
.type
= ClientMessage
;
517 ev
.xclient
.message_type
= _XA_WM_IGNORE_FOCUS_EVENTS
;
518 ev
.xclient
.format
= 32;
519 ev
.xclient
.data
.l
[0] = True
;
521 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
522 WMUnmapWidget(panel
->win
);
524 ev
.xclient
.data
.l
[0] = False
;
525 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
529 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
531 RReleaseImage(image
);
533 WMFreeArray(panel
->images
);
537 WMDestroyWidget(panel
->win
);
540 WMFreeArray(panel
->icons
);
542 WMFreeArray(panel
->windows
);
545 RReleaseImage(panel
->tile
);
548 RReleaseImage(panel
->tileTmp
);
551 RReleaseImage(panel
->bg
);
554 WMReleaseFont(panel
->font
);
557 WMReleaseColor(panel
->white
);
562 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
*panel
, int back
)
565 int count
= WMGetArrayItemCount(panel
->windows
);
571 changeImage(panel
, panel
->current
, 0);
578 wwin
= WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
);
581 if (panel
->current
< 0)
582 scrollIcons(panel
, count
);
583 else if (panel
->current
< panel
->firstVisible
)
584 scrollIcons(panel
, -1);
586 if (panel
->current
>= count
)
587 scrollIcons(panel
, -count
);
588 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
589 scrollIcons(panel
, 1);
592 panel
->current
= (count
+ panel
->current
) % count
;
595 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
597 changeImage(panel
, panel
->current
, 1);
603 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
*panel
, int back
)
606 int count
= WMGetArrayItemCount(panel
->windows
);
612 changeImage(panel
, panel
->current
, 0);
615 panel
->current
= count
- 1;
616 scrollIcons(panel
, count
);
619 scrollIcons(panel
, -count
);
622 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
625 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
626 changeImage(panel
, panel
->current
, 1);
632 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
*panel
, XEvent
*event
)
641 if (event
->type
== MotionNotify
) {
642 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
643 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
650 if (focus
>= 0 && panel
->current
!= focus
) {
653 changeImage(panel
, panel
->current
, 0);
654 changeImage(panel
, focus
, 1);
655 panel
->current
= focus
;
657 wwin
= WMGetFromArray(panel
->windows
, focus
);
659 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
667 Window
wSwitchPanelGetWindow(WSwitchPanel
*swpanel
)
672 return WMWidgetXID(swpanel
->win
);