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
;
68 extern WPreferences wPreferences
;
70 #define BORDER_SPACE 10
72 #define ICON_TILE_SIZE 64
73 #define LABEL_HEIGHT 25
74 #define SCREEN_BORDER_SPACING 2*20
75 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
77 #define ICON_SELECTED (1<<1)
78 #define ICON_DIM (1<<2)
80 static int canReceiveFocus(WWindow
*wwin
)
82 if (wwin
->frame
->workspace
!= wwin
->screen_ptr
->current_workspace
)
85 if (wPreferences
.cycle_active_head_only
&&
86 wGetHeadForWindow(wwin
) != wGetHeadForPointerLocation(wwin
->screen_ptr
))
89 if (WFLAGP(wwin
, no_focusable
))
92 if (!wwin
->flags
.mapped
) {
93 if (!wwin
->flags
.shaded
&& !wwin
->flags
.miniaturized
&& !wwin
->flags
.hidden
)
102 static Bool
sameWindowClass(WWindow
*wwin
, WWindow
*curwin
)
104 if (!wwin
->wm_class
|| !curwin
->wm_class
)
106 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
112 static void changeImage(WSwitchPanel
*panel
, int idecks
, int selected
, Bool dim
, Bool force
)
114 WMFrame
*icon
= WMGetFromArray(panel
->icons
, idecks
);
115 RImage
*image
= WMGetFromArray(panel
->images
, idecks
);
116 char flags
= (char) WMGetFromArray(panel
->flags
, idecks
);
120 desired
|= ICON_SELECTED
;
124 if (flags
== desired
&& !force
)
127 WMReplaceInArray(panel
->flags
, idecks
, desired
);
129 if (!panel
->bg
&& !panel
->tile
&& !selected
)
130 WMSetFrameRelief(icon
, WRFlat
);
134 int opaq
= (dim
) ? 75 : 255;
139 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
142 pos
= WMGetViewPosition(WMWidgetView(icon
));
143 back
= panel
->tileTmp
;
145 RCopyArea(back
, panel
->bg
,
146 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
147 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
150 WMScreen
*wscr
= WMWidgetScreen(icon
);
152 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
153 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
154 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
155 RFillImage(back
, &color
);
160 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
161 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
164 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
165 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
168 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
169 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
170 XClearWindow(dpy
, WMWidgetXID(icon
));
174 if (!panel
->bg
&& !panel
->tile
&& selected
)
175 WMSetFrameRelief(icon
, WRSimple
);
178 static void addIconForWindow(WSwitchPanel
*panel
, WMWidget
*parent
, WWindow
*wwin
, int x
, int y
)
180 WMFrame
*icon
= WMCreateFrame(parent
);
181 RImage
*image
= NULL
;
183 WMSetFrameRelief(icon
, WRFlat
);
184 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
185 WMMoveWidget(icon
, x
, y
);
187 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
188 image
= RRetainImage(wwin
->net_icon_image
);
190 /* get_icon_image() includes the default icon image */
192 image
= get_icon_image(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
, ICON_TILE_SIZE
);
194 /* We must resize the icon size (~64) to the switchpanel icon size (~48) */
195 image
= wIconValidateIconSize(image
, ICON_SIZE
);
197 WMAddToArray(panel
->images
, image
);
198 WMAddToArray(panel
->icons
, icon
);
201 static void scrollIcons(WSwitchPanel
*panel
, int delta
)
203 int nfirst
= panel
->firstVisible
+ delta
;
205 int count
= WMGetArrayItemCount(panel
->windows
);
208 if (count
<= panel
->visibleCount
)
213 else if (nfirst
>= count
- panel
->visibleCount
)
214 nfirst
= count
- panel
->visibleCount
;
216 if (nfirst
== panel
->firstVisible
)
219 WMMoveWidget(panel
->iconBox
, -nfirst
* ICON_TILE_SIZE
, 0);
221 panel
->firstVisible
= nfirst
;
223 for (i
= panel
->firstVisible
; i
< panel
->firstVisible
+ panel
->visibleCount
; i
++) {
224 if (i
== panel
->current
)
226 dim
= ((char) WMGetFromArray(panel
->flags
, i
) & ICON_DIM
);
227 changeImage(panel
, i
, 0, dim
, True
);
236 static RImage
*assemblePuzzleImage(RImage
**images
, int width
, int height
)
238 RImage
*img
= RCreateImage(width
, height
, 1);
250 RFillImage(img
, &color
);
252 tw
= width
- images
[0]->width
- images
[2]->width
;
253 th
= height
- images
[0]->height
- images
[6]->height
;
255 if (tw
<= 0 || th
<= 0)
260 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
261 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 0);
266 tmp
= RSmoothScaleImage(images
[7], tw
, images
[7]->height
);
267 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[6]->width
, height
- images
[6]->height
);
272 tmp
= RSmoothScaleImage(images
[3], images
[3]->width
, th
);
273 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 if (tw
> 0 && th
> 0) {
284 tmp
= RSmoothScaleImage(images
[4], tw
, th
);
285 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, images
[0]->height
);
290 RCopyArea(img
, images
[0], 0, 0, images
[0]->width
, images
[0]->height
, 0, 0);
291 RCopyArea(img
, images
[2], 0, 0, images
[2]->width
, images
[2]->height
, width
- images
[2]->width
, 0);
292 RCopyArea(img
, images
[6], 0, 0, images
[6]->width
, images
[6]->height
, 0, height
- images
[6]->height
);
293 RCopyArea(img
, images
[8], 0, 0, images
[8]->width
, images
[8]->height
,
294 width
- images
[8]->width
, height
- images
[8]->height
);
299 static RImage
*createBackImage(int width
, int height
)
301 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
304 static RImage
*getTile(void)
308 if (!wPreferences
.swtileImage
)
311 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
313 return wPreferences
.swtileImage
;
318 static void drawTitle(WSwitchPanel
*panel
, int idecks
, char *title
)
321 int width
= WMWidgetWidth(panel
->win
);
325 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
331 if (strcmp(ntitle
, title
) != 0) {
334 int w
= WMWidthOfString(panel
->font
, ntitle
, strlen(ntitle
));
336 x
= BORDER_SPACE
+ (idecks
- panel
->firstVisible
) * ICON_TILE_SIZE
+
337 ICON_TILE_SIZE
/ 2 - w
/ 2;
338 if (x
< BORDER_SPACE
)
340 else if (x
+ w
> width
- BORDER_SPACE
)
341 x
= width
- BORDER_SPACE
- w
;
345 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
347 WMDrawString(panel
->scr
->wmscreen
,
348 WMWidgetXID(panel
->win
),
349 panel
->white
, panel
->font
,
351 WMWidgetHeight(panel
->win
) - BORDER_SPACE
- LABEL_HEIGHT
+
352 WMFontHeight(panel
->font
) / 2, ntitle
, strlen(ntitle
));
355 WMSetLabelText(panel
->label
, ntitle
);
362 static WMArray
*makeWindowListArray(WWindow
*curwin
, int include_unmapped
, Bool class_only
)
364 WMArray
*windows
= WMCreateArray(10);
368 for (fl
= 0; fl
< 2; fl
++) {
369 for (wwin
= curwin
; wwin
; wwin
= wwin
->prev
) {
370 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
371 (wwin
->flags
.mapped
|| include_unmapped
)) {
373 if (!sameWindowClass(wwin
, curwin
))
376 if (!WFLAGP(wwin
, skip_switchpanel
))
377 WMAddToArray(windows
, wwin
);
381 /* start over from the beginning of the list */
385 for (wwin
= curwin
; wwin
&& wwin
!= curwin
; wwin
= wwin
->prev
) {
386 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
387 (wwin
->flags
.mapped
|| include_unmapped
)) {
389 if (!sameWindowClass(wwin
, curwin
))
391 if (!WFLAGP(wwin
, skip_switchpanel
))
392 WMAddToArray(windows
, wwin
);
400 static WMArray
*makeWindowFlagsArray(int count
)
402 WMArray
*flags
= WMCreateArray(1);
405 for (i
= 0; i
< count
; i
++)
406 WMAddToArray(flags
, (char) 0);
411 WSwitchPanel
*wInitSwitchPanel(WScreen
*scr
, WWindow
*curwin
, Bool class_only
)
414 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
416 int i
, width
, height
, iconsThatFitCount
, count
;
417 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
420 panel
->windows
= makeWindowListArray(curwin
, wPreferences
.swtileImage
!= NULL
, class_only
);
421 count
= WMGetArrayItemCount(panel
->windows
);
423 panel
->flags
= makeWindowFlagsArray(count
);
426 WMFreeArray(panel
->windows
);
431 width
= ICON_TILE_SIZE
* count
;
432 iconsThatFitCount
= count
;
434 if (width
> rect
.size
.width
) {
435 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
436 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
439 panel
->visibleCount
= iconsThatFitCount
;
441 if (!wPreferences
.swtileImage
)
444 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
446 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
447 panel
->tile
= getTile();
448 if (panel
->tile
&& wPreferences
.swbackImage
[8])
449 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
451 if (!panel
->tileTmp
|| !panel
->tile
) {
453 RReleaseImage(panel
->bg
);
456 RReleaseImage(panel
->tile
);
459 RReleaseImage(panel
->tileTmp
);
460 panel
->tileTmp
= NULL
;
463 panel
->white
= WMWhiteColor(scr
->wmscreen
);
464 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
465 panel
->icons
= WMCreateArray(count
);
466 panel
->images
= WMCreateArray(count
);
468 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
471 WMFrame
*frame
= WMCreateFrame(panel
->win
);
472 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
473 WMSetFrameRelief(frame
, WRSimple
);
474 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
476 panel
->label
= WMCreateLabel(panel
->win
);
477 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
478 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
479 WMSetLabelRelief(panel
->label
, WRSimple
);
480 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
481 WMSetLabelFont(panel
->label
, panel
->font
);
482 WMSetLabelTextColor(panel
->label
, panel
->white
);
484 WMReleaseColor(darkGray
);
488 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
490 viewport
= WMCreateFrame(panel
->win
);
491 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
492 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
493 WMSetFrameRelief(viewport
, WRFlat
);
495 panel
->iconBox
= WMCreateFrame(viewport
);
496 WMMoveWidget(panel
->iconBox
, 0, 0);
497 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
498 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
500 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
501 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
504 WMMapSubwidgets(panel
->win
);
505 WMRealizeWidget(panel
->win
);
507 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
508 changeImage(panel
, i
, 0, False
, True
);
514 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
516 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
519 if (mask
&& wShapeSupported
)
520 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
523 XFreePixmap(dpy
, pixmap
);
526 XFreePixmap(dpy
, mask
);
531 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
532 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
533 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
536 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
537 if (panel
->current
>= 0)
538 changeImage(panel
, panel
->current
, 1, False
, False
);
540 WMMapWidget(panel
->win
);
545 void wSwitchPanelDestroy(WSwitchPanel
*panel
)
551 Window info_win
= panel
->scr
->info_window
;
553 ev
.xclient
.type
= ClientMessage
;
554 ev
.xclient
.message_type
= _XA_WM_IGNORE_FOCUS_EVENTS
;
555 ev
.xclient
.format
= 32;
556 ev
.xclient
.data
.l
[0] = True
;
558 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
559 WMUnmapWidget(panel
->win
);
561 ev
.xclient
.data
.l
[0] = False
;
562 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
566 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
568 RReleaseImage(image
);
570 WMFreeArray(panel
->images
);
574 WMDestroyWidget(panel
->win
);
577 WMFreeArray(panel
->icons
);
580 WMFreeArray(panel
->flags
);
582 WMFreeArray(panel
->windows
);
585 RReleaseImage(panel
->tile
);
588 RReleaseImage(panel
->tileTmp
);
591 RReleaseImage(panel
->bg
);
594 WMReleaseFont(panel
->font
);
597 WMReleaseColor(panel
->white
);
602 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
*panel
, int back
, int ignore_minimized
, Bool class_only
)
604 WWindow
*wwin
, *curwin
, *tmpwin
;
605 int count
= WMGetArrayItemCount(panel
->windows
);
606 int orig
= panel
->current
;
613 if (!wPreferences
.cycle_ignore_minimized
)
614 ignore_minimized
= False
;
616 if (ignore_minimized
&& canReceiveFocus(WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
)) < 0)
617 ignore_minimized
= False
;
619 curwin
= WMGetFromArray(panel
->windows
, orig
);
627 panel
->current
= (count
+ panel
->current
) % count
;
628 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
632 if (panel
->current
== orig
)
634 } while (!sameWindowClass(wwin
, curwin
));
635 } while (ignore_minimized
&& panel
->current
!= orig
&& canReceiveFocus(wwin
) < 0);
637 WM_ITERATE_ARRAY(panel
->windows
, tmpwin
, i
) {
638 if (i
== panel
->current
)
640 if (!class_only
|| sameWindowClass(tmpwin
, curwin
))
641 changeImage(panel
, i
, 0, False
, False
);
645 changeImage(panel
, i
, 0, True
, False
);
650 if (panel
->current
< panel
->firstVisible
)
651 scrollIcons(panel
, panel
->current
- panel
->firstVisible
);
652 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
653 scrollIcons(panel
, panel
->current
- panel
->firstVisible
- panel
->visibleCount
+ 1);
656 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
657 if (panel
->current
!= orig
)
658 changeImage(panel
, orig
, 0, dim
, False
);
659 changeImage(panel
, panel
->current
, 1, False
, False
);
665 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
*panel
, int back
)
668 int count
= WMGetArrayItemCount(panel
->windows
);
676 panel
->current
= count
- 1;
677 scrollIcons(panel
, count
);
680 scrollIcons(panel
, -count
);
683 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
684 title
= wwin
->frame
->title
;
687 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
688 changeImage(panel
, i
, i
== panel
->current
, False
, False
);
690 drawTitle(panel
, panel
->current
, title
);
696 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
*panel
, XEvent
*event
)
705 if (event
->type
== MotionNotify
) {
706 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
707 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
714 if (focus
>= 0 && panel
->current
!= focus
) {
717 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
718 changeImage(panel
, i
, i
== focus
, False
, False
);
720 panel
->current
= focus
;
722 wwin
= WMGetFromArray(panel
->windows
, focus
);
724 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
732 Window
wSwitchPanelGetWindow(WSwitchPanel
*swpanel
)
737 return WMWidgetXID(swpanel
->win
);