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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 #include "WindowMaker.h"
33 #include "switchpanel.h"
38 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
41 #include <X11/extensions/shape.h>
43 extern Bool wShapeSupported
;
70 extern WPreferences wPreferences
;
72 #define BORDER_SPACE 10
74 #define ICON_TILE_SIZE 64
75 #define LABEL_HEIGHT 25
76 #define SCREEN_BORDER_SPACING 2*20
77 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
79 static int canReceiveFocus(WWindow
* wwin
)
81 if (wwin
->frame
->workspace
!= wwin
->screen_ptr
->current_workspace
)
84 if (wPreferences
.cycle_active_head_only
&&
85 wGetHeadForWindow(wwin
) != wGetHeadForPointerLocation(wwin
->screen_ptr
))
88 if (!wwin
->flags
.mapped
) {
89 if (!wwin
->flags
.shaded
&& !wwin
->flags
.miniaturized
&& !wwin
->flags
.hidden
)
94 if (WFLAGP(wwin
, no_focusable
))
99 static void changeImage(WSwitchPanel
* panel
, int idecks
, int selected
)
101 WMFrame
*icon
= WMGetFromArray(panel
->icons
, idecks
);
102 RImage
*image
= WMGetFromArray(panel
->images
, idecks
);
104 if (!panel
->bg
&& !panel
->tile
) {
106 WMSetFrameRelief(icon
, WRFlat
);
116 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
119 pos
= WMGetViewPosition(WMWidgetView(icon
));
120 back
= panel
->tileTmp
;
122 RCopyArea(back
, panel
->bg
,
123 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
124 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
127 WMScreen
*wscr
= WMWidgetScreen(icon
);
129 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
130 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
131 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
132 RFillImage(back
, &color
);
136 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
137 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
139 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
140 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
143 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
144 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
145 XClearWindow(dpy
, WMWidgetXID(icon
));
149 if (!panel
->bg
&& !panel
->tile
) {
151 WMSetFrameRelief(icon
, WRSimple
);
155 static RImage
*scaleDownIfNeeded(RImage
* image
)
157 if (image
&& ((image
->width
- ICON_SIZE
) > 2 || (image
->height
- ICON_SIZE
) > 2)) {
159 nimage
= RScaleImage(image
, ICON_SIZE
, (image
->height
* ICON_SIZE
/ image
->width
));
160 RReleaseImage(image
);
166 static void addIconForWindow(WSwitchPanel
* panel
, WMWidget
* parent
, WWindow
* wwin
, int x
, int y
)
168 WMFrame
*icon
= WMCreateFrame(parent
);
169 RImage
*image
= NULL
;
171 WMSetFrameRelief(icon
, WRFlat
);
172 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
173 WMMoveWidget(icon
, x
, y
);
175 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
176 image
= RRetainImage(wwin
->net_icon_image
);
178 // Make this use a caching thing. When there are many windows,
179 // it's very likely that most of them are instances of the same thing,
180 // so caching them should get performance acceptable in these cases.
182 image
= wDefaultGetImage(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
);
184 if (!image
&& !panel
->defIcon
) {
185 char *file
= wDefaultGetIconFile(panel
->scr
, NULL
, NULL
, False
);
187 char *path
= FindImage(wPreferences
.icon_path
, file
);
189 image
= RLoadImage(panel
->scr
->rcontext
, path
, 0);
194 panel
->defIcon
= scaleDownIfNeeded(image
);
197 if (!image
&& panel
->defIcon
)
198 image
= RRetainImage(panel
->defIcon
);
200 image
= scaleDownIfNeeded(image
);
202 WMAddToArray(panel
->images
, image
);
203 WMAddToArray(panel
->icons
, icon
);
206 static void scrollIcons(WSwitchPanel
* panel
, int delta
)
208 int nfirst
= panel
->firstVisible
+ delta
;
210 int count
= WMGetArrayItemCount(panel
->windows
);
212 // struct timeval tv1, tv2;
214 if (count
<= panel
->visibleCount
)
219 else if (nfirst
>= count
- panel
->visibleCount
)
220 nfirst
= count
- panel
->visibleCount
;
222 if (nfirst
== panel
->firstVisible
)
225 ox = -panel->firstVisible * ICON_TILE_SIZE;
226 nx = -nfirst * ICON_TILE_SIZE;
227 for (i= 0; i < SCROLL_STEPS; i++) {
229 gettimeofday(&tv1, NULL);
230 WMMoveWidget(panel->iconBox, (nx*i + ox*(SCROLL_STEPS-i))/(SCROLL_STEPS-1), 0);
232 gettimeofday(&tv2, NULL);
233 diff = (tv2.tv_sec-tv1.tv_sec)*10000+(tv2.tv_usec-tv1.tv_usec)/100;
238 WMMoveWidget(panel
->iconBox
, -nfirst
* ICON_TILE_SIZE
, 0);
240 panel
->firstVisible
= nfirst
;
242 for (i
= panel
->firstVisible
; i
< panel
->firstVisible
+ panel
->visibleCount
; i
++) {
243 changeImage(panel
, i
, i
== panel
->current
);
252 static RImage
*assemblePuzzleImage(RImage
** images
, int width
, int height
)
254 RImage
*img
= RCreateImage(width
, height
, 1);
266 RFillImage(img
, &color
);
268 tw
= width
- images
[0]->width
- images
[2]->width
;
269 th
= height
- images
[0]->height
- images
[6]->height
;
271 if (tw
<= 0 || th
<= 0) {
278 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
279 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 0);
284 tmp
= RSmoothScaleImage(images
[7], tw
, images
[7]->height
);
285 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[6]->width
, height
- images
[6]->height
);
290 tmp
= RSmoothScaleImage(images
[3], images
[3]->width
, th
);
291 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, 0, images
[0]->height
);
296 tmp
= RSmoothScaleImage(images
[5], images
[5]->width
, th
);
297 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, width
- images
[5]->width
, images
[2]->height
);
301 if (tw
> 0 && th
> 0) {
302 tmp
= RSmoothScaleImage(images
[4], tw
, th
);
303 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, images
[0]->height
);
308 RCopyArea(img
, images
[0], 0, 0, images
[0]->width
, images
[0]->height
, 0, 0);
310 RCopyArea(img
, images
[2], 0, 0, images
[2]->width
, images
[2]->height
, width
- images
[2]->width
, 0);
312 RCopyArea(img
, images
[6], 0, 0, images
[6]->width
, images
[6]->height
, 0, height
- images
[6]->height
);
314 RCopyArea(img
, images
[8], 0, 0, images
[8]->width
, images
[8]->height
,
315 width
- images
[8]->width
, height
- images
[8]->height
);
320 static RImage
*createBackImage(int width
, int height
)
322 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
325 static RImage
*getTile(void)
329 if (!wPreferences
.swtileImage
)
332 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
334 return wPreferences
.swtileImage
;
339 static void drawTitle(WSwitchPanel
* panel
, int idecks
, char *title
)
342 int width
= WMWidgetWidth(panel
->win
);
346 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
352 if (strcmp(ntitle
, title
) != 0)
355 int w
= WMWidthOfString(panel
->font
, ntitle
, strlen(ntitle
));
357 x
= BORDER_SPACE
+ (idecks
- panel
->firstVisible
) * ICON_TILE_SIZE
+
358 ICON_TILE_SIZE
/ 2 - w
/ 2;
359 if (x
< BORDER_SPACE
)
361 else if (x
+ w
> width
- BORDER_SPACE
)
362 x
= width
- BORDER_SPACE
- w
;
365 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
367 WMDrawString(panel
->scr
->wmscreen
,
368 WMWidgetXID(panel
->win
),
369 panel
->white
, panel
->font
,
371 WMWidgetHeight(panel
->win
) - BORDER_SPACE
- LABEL_HEIGHT
+
372 WMFontHeight(panel
->font
) / 2, ntitle
, strlen(ntitle
));
375 WMSetLabelText(panel
->label
, ntitle
);
381 static WMArray
*makeWindowListArray(WWindow
*curwin
, int include_unmapped
, Bool class_only
)
383 WMArray
*windows
= WMCreateArray(10);
387 for (fl
= 0; fl
< 2; fl
++) {
388 for (wwin
= curwin
; wwin
; wwin
= wwin
->prev
) {
389 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
390 (wwin
->flags
.mapped
|| include_unmapped
)) {
392 if (!wwin
->wm_class
|| !curwin
->wm_class
)
394 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
397 WMAddToArray(windows
, wwin
);
401 /* start over from the beginning of the list */
405 for (wwin
= curwin
; wwin
&& wwin
!= curwin
; wwin
= wwin
->prev
) {
406 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
407 (wwin
->flags
.mapped
|| include_unmapped
)) {
409 if (!wwin
->wm_class
|| !curwin
->wm_class
)
411 if (strcmp(wwin
->wm_class
, curwin
->wm_class
))
414 WMAddToArray(windows
, wwin
);
422 WSwitchPanel
*wInitSwitchPanel(WScreen
* scr
, WWindow
* curwin
, Bool class_only
)
425 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
429 int iconsThatFitCount
;
432 rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
434 memset(panel
, 0, sizeof(WSwitchPanel
));
438 panel
->windows
= makeWindowListArray(curwin
, wPreferences
.swtileImage
!= NULL
, class_only
);
439 count
= WMGetArrayItemCount(panel
->windows
);
442 WMFreeArray(panel
->windows
);
447 width
= ICON_TILE_SIZE
* count
;
448 iconsThatFitCount
= count
;
450 if (width
> rect
.size
.width
) {
451 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
452 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
455 panel
->visibleCount
= iconsThatFitCount
;
457 if (!wPreferences
.swtileImage
)
460 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
462 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
463 panel
->tile
= getTile();
464 if (panel
->tile
&& wPreferences
.swbackImage
[8]) {
465 panel
->bg
= createBackImage(width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
467 if (!panel
->tileTmp
|| !panel
->tile
) {
469 RReleaseImage(panel
->bg
);
472 RReleaseImage(panel
->tile
);
475 RReleaseImage(panel
->tileTmp
);
476 panel
->tileTmp
= NULL
;
479 panel
->white
= WMWhiteColor(scr
->wmscreen
);
480 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
481 panel
->icons
= WMCreateArray(count
);
482 panel
->images
= WMCreateArray(count
);
484 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
487 WMFrame
*frame
= WMCreateFrame(panel
->win
);
488 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
489 WMSetFrameRelief(frame
, WRSimple
);
490 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
492 panel
->label
= WMCreateLabel(panel
->win
);
493 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
494 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
495 WMSetLabelRelief(panel
->label
, WRSimple
);
496 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
497 WMSetLabelFont(panel
->label
, panel
->font
);
498 WMSetLabelTextColor(panel
->label
, panel
->white
);
500 WMReleaseColor(darkGray
);
504 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
506 viewport
= WMCreateFrame(panel
->win
);
507 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
508 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
509 WMSetFrameRelief(viewport
, WRFlat
);
511 panel
->iconBox
= WMCreateFrame(viewport
);
512 WMMoveWidget(panel
->iconBox
, 0, 0);
513 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
514 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
516 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
517 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
520 WMMapSubwidgets(panel
->win
);
521 WMRealizeWidget(panel
->win
);
523 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
524 changeImage(panel
, i
, 0);
530 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
532 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
535 if (mask
&& wShapeSupported
)
536 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
540 XFreePixmap(dpy
, pixmap
);
542 XFreePixmap(dpy
, mask
);
547 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
548 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
549 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
552 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
553 if (panel
->current
>= 0)
554 changeImage(panel
, panel
->current
, 1);
556 WMMapWidget(panel
->win
);
561 void wSwitchPanelDestroy(WSwitchPanel
* panel
)
567 Window info_win
= panel
->scr
->info_window
;
569 ev
.xclient
.type
= ClientMessage
;
570 ev
.xclient
.message_type
= _XA_WM_IGNORE_FOCUS_EVENTS
;
571 ev
.xclient
.format
= 32;
572 ev
.xclient
.data
.l
[0] = True
;
574 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
575 WMUnmapWidget(panel
->win
);
577 ev
.xclient
.data
.l
[0] = False
;
578 XSendEvent(dpy
, info_win
, True
, EnterWindowMask
, &ev
);
582 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
584 RReleaseImage(image
);
586 WMFreeArray(panel
->images
);
589 WMDestroyWidget(panel
->win
);
591 WMFreeArray(panel
->icons
);
592 WMFreeArray(panel
->windows
);
594 RReleaseImage(panel
->defIcon
);
596 RReleaseImage(panel
->tile
);
598 RReleaseImage(panel
->tileTmp
);
600 RReleaseImage(panel
->bg
);
602 WMReleaseFont(panel
->font
);
604 WMReleaseColor(panel
->white
);
608 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
* panel
, int back
)
611 int count
= WMGetArrayItemCount(panel
->windows
);
617 changeImage(panel
, panel
->current
, 0);
624 wwin
= WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
);
627 if (panel
->current
< 0)
628 scrollIcons(panel
, count
);
629 else if (panel
->current
< panel
->firstVisible
)
630 scrollIcons(panel
, -1);
632 if (panel
->current
>= count
)
633 scrollIcons(panel
, -count
);
634 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
635 scrollIcons(panel
, 1);
638 panel
->current
= (count
+ panel
->current
) % count
;
641 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
643 changeImage(panel
, panel
->current
, 1);
649 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
* panel
, int back
)
652 int count
= WMGetArrayItemCount(panel
->windows
);
658 changeImage(panel
, panel
->current
, 0);
661 panel
->current
= count
- 1;
662 scrollIcons(panel
, count
);
665 scrollIcons(panel
, -count
);
668 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
671 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
673 changeImage(panel
, panel
->current
, 1);
678 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
* panel
, XEvent
* event
)
687 /* if (event->type == LeaveNotify) {
688 if (event->xcrossing.window == WMWidgetXID(panel->win))
690 } else */ if (event
->type
== MotionNotify
) {
692 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
693 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
699 if (focus
>= 0 && panel
->current
!= focus
) {
702 changeImage(panel
, panel
->current
, 0);
703 changeImage(panel
, focus
, 1);
704 panel
->current
= focus
;
706 wwin
= WMGetFromArray(panel
->windows
, focus
);
708 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
716 Window
wSwitchPanelGetWindow(WSwitchPanel
* swpanel
)
720 return WMWidgetXID(swpanel
->win
);