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"
34 #include "switchpanel.h"
39 #include <X11/extensions/shape.h>
41 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 static int canReceiveFocus(WWindow
* wwin
)
79 if (wwin
->frame
->workspace
!= wwin
->screen_ptr
->current_workspace
)
81 if (!wwin
->flags
.mapped
) {
82 if (!wwin
->flags
.shaded
&& !wwin
->flags
.miniaturized
&& !wwin
->flags
.hidden
)
87 if (WFLAGP(wwin
, no_focusable
))
92 static void changeImage(WSwitchPanel
* panel
, int idecks
, int selected
)
94 WMFrame
*icon
= WMGetFromArray(panel
->icons
, idecks
);
95 RImage
*image
= WMGetFromArray(panel
->images
, idecks
);
97 if (!panel
->bg
&& !panel
->tile
) {
99 WMSetFrameRelief(icon
, WRFlat
);
109 if (canReceiveFocus(WMGetFromArray(panel
->windows
, idecks
)) < 0)
112 pos
= WMGetViewPosition(WMWidgetView(icon
));
113 back
= panel
->tileTmp
;
115 RCopyArea(back
, panel
->bg
,
116 BORDER_SPACE
+ pos
.x
- panel
->firstVisible
* ICON_TILE_SIZE
,
117 BORDER_SPACE
+ pos
.y
, back
->width
, back
->height
, 0, 0);
120 WMScreen
*wscr
= WMWidgetScreen(icon
);
122 color
.red
= WMRedComponentOfColor(WMGrayColor(wscr
)) >> 8;
123 color
.green
= WMGreenComponentOfColor(WMGrayColor(wscr
)) >> 8;
124 color
.blue
= WMBlueComponentOfColor(WMGrayColor(wscr
)) >> 8;
125 RFillImage(back
, &color
);
129 RCombineArea(back
, tile
, 0, 0, tile
->width
, tile
->height
,
130 (back
->width
- tile
->width
) / 2, (back
->height
- tile
->height
) / 2);
132 RCombineAreaWithOpaqueness(back
, image
, 0, 0, image
->width
, image
->height
,
133 (back
->width
- image
->width
) / 2, (back
->height
- image
->height
) / 2,
136 RConvertImage(panel
->scr
->rcontext
, back
, &p
);
137 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(icon
), p
);
138 XClearWindow(dpy
, WMWidgetXID(icon
));
142 if (!panel
->bg
&& !panel
->tile
) {
144 WMSetFrameRelief(icon
, WRSimple
);
148 static RImage
*scaleDownIfNeeded(RImage
* image
)
150 if (image
&& ((image
->width
- ICON_SIZE
) > 2 || (image
->height
- ICON_SIZE
) > 2)) {
152 nimage
= RScaleImage(image
, ICON_SIZE
, (image
->height
* ICON_SIZE
/ image
->width
));
153 RReleaseImage(image
);
159 static void addIconForWindow(WSwitchPanel
* panel
, WMWidget
* parent
, WWindow
* wwin
, int x
, int y
)
161 WMFrame
*icon
= WMCreateFrame(parent
);
162 RImage
*image
= NULL
;
164 WMSetFrameRelief(icon
, WRFlat
);
165 WMResizeWidget(icon
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
166 WMMoveWidget(icon
, x
, y
);
168 if (!WFLAGP(wwin
, always_user_icon
) && wwin
->net_icon_image
)
169 image
= RRetainImage(wwin
->net_icon_image
);
171 // Make this use a caching thing. When there are many windows,
172 // it's very likely that most of them are instances of the same thing,
173 // so caching them should get performance acceptable in these cases.
175 image
= wDefaultGetImage(panel
->scr
, wwin
->wm_instance
, wwin
->wm_class
);
177 if (!image
&& !panel
->defIcon
) {
178 char *file
= wDefaultGetIconFile(panel
->scr
, NULL
, NULL
, False
);
180 char *path
= FindImage(wPreferences
.icon_path
, file
);
182 image
= RLoadImage(panel
->scr
->rcontext
, path
, 0);
187 panel
->defIcon
= scaleDownIfNeeded(image
);
190 if (!image
&& panel
->defIcon
)
191 image
= RRetainImage(panel
->defIcon
);
193 image
= scaleDownIfNeeded(image
);
195 WMAddToArray(panel
->images
, image
);
196 WMAddToArray(panel
->icons
, icon
);
199 static void scrollIcons(WSwitchPanel
* panel
, int delta
)
201 int nfirst
= panel
->firstVisible
+ delta
;
203 int count
= WMGetArrayItemCount(panel
->windows
);
205 // struct timeval tv1, tv2;
207 if (count
<= panel
->visibleCount
)
212 else if (nfirst
>= count
- panel
->visibleCount
)
213 nfirst
= count
- panel
->visibleCount
;
215 if (nfirst
== panel
->firstVisible
)
218 ox = -panel->firstVisible * ICON_TILE_SIZE;
219 nx = -nfirst * ICON_TILE_SIZE;
220 for (i= 0; i < SCROLL_STEPS; i++) {
222 gettimeofday(&tv1, NULL);
223 WMMoveWidget(panel->iconBox, (nx*i + ox*(SCROLL_STEPS-i))/(SCROLL_STEPS-1), 0);
225 gettimeofday(&tv2, NULL);
226 diff = (tv2.tv_sec-tv1.tv_sec)*10000+(tv2.tv_usec-tv1.tv_usec)/100;
231 WMMoveWidget(panel
->iconBox
, -nfirst
* ICON_TILE_SIZE
, 0);
233 panel
->firstVisible
= nfirst
;
235 for (i
= panel
->firstVisible
; i
< panel
->firstVisible
+ panel
->visibleCount
; i
++) {
236 changeImage(panel
, i
, i
== panel
->current
);
245 static RImage
*assemblePuzzleImage(RImage
** images
, int width
, int height
)
247 RImage
*img
= RCreateImage(width
, height
, 1);
259 RFillImage(img
, &color
);
261 tw
= width
- images
[0]->width
- images
[2]->width
;
262 th
= height
- images
[0]->height
- images
[6]->height
;
264 if (tw
<= 0 || th
<= 0) {
271 tmp
= RSmoothScaleImage(images
[1], tw
, images
[1]->height
);
272 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, 0);
277 tmp
= RSmoothScaleImage(images
[7], tw
, images
[7]->height
);
278 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[6]->width
, height
- images
[6]->height
);
283 tmp
= RSmoothScaleImage(images
[3], images
[3]->width
, th
);
284 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, 0, images
[0]->height
);
289 tmp
= RSmoothScaleImage(images
[5], images
[5]->width
, th
);
290 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, width
- images
[5]->width
, images
[2]->height
);
294 if (tw
> 0 && th
> 0) {
295 tmp
= RSmoothScaleImage(images
[4], tw
, th
);
296 RCopyArea(img
, tmp
, 0, 0, tmp
->width
, tmp
->height
, images
[0]->width
, images
[0]->height
);
301 RCopyArea(img
, images
[0], 0, 0, images
[0]->width
, images
[0]->height
, 0, 0);
303 RCopyArea(img
, images
[2], 0, 0, images
[2]->width
, images
[2]->height
, width
- images
[2]->width
, 0);
305 RCopyArea(img
, images
[6], 0, 0, images
[6]->width
, images
[6]->height
, 0, height
- images
[6]->height
);
307 RCopyArea(img
, images
[8], 0, 0, images
[8]->width
, images
[8]->height
,
308 width
- images
[8]->width
, height
- images
[8]->height
);
313 static RImage
*createBackImage(WScreen
* scr
, int width
, int height
)
315 return assemblePuzzleImage(wPreferences
.swbackImage
, width
, height
);
318 static RImage
*getTile(WSwitchPanel
* panel
)
322 if (!wPreferences
.swtileImage
)
325 stile
= RScaleImage(wPreferences
.swtileImage
, ICON_TILE_SIZE
, ICON_TILE_SIZE
);
327 return wPreferences
.swtileImage
;
332 static void drawTitle(WSwitchPanel
* panel
, int idecks
, char *title
)
335 int width
= WMWidgetWidth(panel
->win
);
339 ntitle
= ShrinkString(panel
->font
, title
, width
- 2 * BORDER_SPACE
);
345 if (strcmp(ntitle
, title
) != 0)
348 int w
= WMWidthOfString(panel
->font
, ntitle
, strlen(ntitle
));
350 x
= BORDER_SPACE
+ (idecks
- panel
->firstVisible
) * ICON_TILE_SIZE
+
351 ICON_TILE_SIZE
/ 2 - w
/ 2;
352 if (x
< BORDER_SPACE
)
354 else if (x
+ w
> width
- BORDER_SPACE
)
355 x
= width
- BORDER_SPACE
- w
;
358 XClearWindow(dpy
, WMWidgetXID(panel
->win
));
360 WMDrawString(panel
->scr
->wmscreen
,
361 WMWidgetXID(panel
->win
),
362 panel
->white
, panel
->font
,
364 WMWidgetHeight(panel
->win
) - BORDER_SPACE
- LABEL_HEIGHT
+
365 WMFontHeight(panel
->font
) / 2, ntitle
, strlen(ntitle
));
368 WMSetLabelText(panel
->label
, ntitle
);
374 static WMArray
*makeWindowListArray(WScreen
* scr
, WWindow
* curwin
, int workspace
, int include_unmapped
)
376 WMArray
*windows
= WMCreateArray(10);
380 for (fl
= 0; fl
< 2; fl
++) {
381 for (wwin
= curwin
; wwin
; wwin
= wwin
->prev
) {
382 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
383 (!WFLAGP(wwin
, skip_window_list
) || wwin
->flags
.internal_window
) &&
384 (wwin
->flags
.mapped
|| include_unmapped
)) {
385 WMAddToArray(windows
, wwin
);
389 /* start over from the beginning of the list */
393 for (wwin
= curwin
; wwin
&& wwin
!= curwin
; wwin
= wwin
->prev
) {
394 if (((!fl
&& canReceiveFocus(wwin
) > 0) || (fl
&& canReceiveFocus(wwin
) < 0)) &&
395 (!WFLAGP(wwin
, skip_window_list
) || wwin
->flags
.internal_window
) &&
396 (wwin
->flags
.mapped
|| include_unmapped
)) {
397 WMAddToArray(windows
, wwin
);
405 WSwitchPanel
*wInitSwitchPanel(WScreen
* scr
, WWindow
* curwin
, int workspace
)
408 WSwitchPanel
*panel
= wmalloc(sizeof(WSwitchPanel
));
412 int iconsThatFitCount
;
415 rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
417 memset(panel
, 0, sizeof(WSwitchPanel
));
421 panel
->windows
= makeWindowListArray(scr
, curwin
, workspace
, wPreferences
.swtileImage
!= 0);
422 count
= WMGetArrayItemCount(panel
->windows
);
425 WMFreeArray(panel
->windows
);
430 width
= ICON_TILE_SIZE
* count
;
431 iconsThatFitCount
= count
;
433 if (width
> rect
.size
.width
) {
434 iconsThatFitCount
= (rect
.size
.width
- SCREEN_BORDER_SPACING
) / ICON_TILE_SIZE
;
435 width
= iconsThatFitCount
* ICON_TILE_SIZE
;
438 panel
->visibleCount
= iconsThatFitCount
;
440 if (!wPreferences
.swtileImage
)
443 height
= LABEL_HEIGHT
+ ICON_TILE_SIZE
;
445 panel
->tileTmp
= RCreateImage(ICON_TILE_SIZE
, ICON_TILE_SIZE
, 1);
446 panel
->tile
= getTile(panel
);
447 if (panel
->tile
&& wPreferences
.swbackImage
[8]) {
448 panel
->bg
= createBackImage(scr
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
450 if (!panel
->tileTmp
|| !panel
->tile
) {
452 RReleaseImage(panel
->bg
);
455 RReleaseImage(panel
->tile
);
458 RReleaseImage(panel
->tileTmp
);
459 panel
->tileTmp
= NULL
;
462 panel
->white
= WMWhiteColor(scr
->wmscreen
);
463 panel
->font
= WMBoldSystemFontOfSize(scr
->wmscreen
, 12);
464 panel
->icons
= WMCreateArray(count
);
465 panel
->images
= WMCreateArray(count
);
467 panel
->win
= WMCreateWindow(scr
->wmscreen
, "");
470 WMFrame
*frame
= WMCreateFrame(panel
->win
);
471 WMColor
*darkGray
= WMDarkGrayColor(scr
->wmscreen
);
472 WMSetFrameRelief(frame
, WRSimple
);
473 WMSetViewExpandsToParent(WMWidgetView(frame
), 0, 0, 0, 0);
475 panel
->label
= WMCreateLabel(panel
->win
);
476 WMResizeWidget(panel
->label
, width
, LABEL_HEIGHT
);
477 WMMoveWidget(panel
->label
, BORDER_SPACE
, BORDER_SPACE
+ ICON_TILE_SIZE
+ 5);
478 WMSetLabelRelief(panel
->label
, WRSimple
);
479 WMSetWidgetBackgroundColor(panel
->label
, darkGray
);
480 WMSetLabelFont(panel
->label
, panel
->font
);
481 WMSetLabelTextColor(panel
->label
, panel
->white
);
483 WMReleaseColor(darkGray
);
487 WMResizeWidget(panel
->win
, width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
489 viewport
= WMCreateFrame(panel
->win
);
490 WMResizeWidget(viewport
, width
, ICON_TILE_SIZE
);
491 WMMoveWidget(viewport
, BORDER_SPACE
, BORDER_SPACE
);
492 WMSetFrameRelief(viewport
, WRFlat
);
494 panel
->iconBox
= WMCreateFrame(viewport
);
495 WMMoveWidget(panel
->iconBox
, 0, 0);
496 WMResizeWidget(panel
->iconBox
, ICON_TILE_SIZE
* count
, ICON_TILE_SIZE
);
497 WMSetFrameRelief(panel
->iconBox
, WRFlat
);
499 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
500 addIconForWindow(panel
, panel
->iconBox
, wwin
, i
* ICON_TILE_SIZE
, 0);
503 WMMapSubwidgets(panel
->win
);
504 WMRealizeWidget(panel
->win
);
506 WM_ITERATE_ARRAY(panel
->windows
, wwin
, i
) {
507 changeImage(panel
, i
, 0);
513 RConvertImageMask(scr
->rcontext
, panel
->bg
, &pixmap
, &mask
, 250);
515 XSetWindowBackgroundPixmap(dpy
, WMWidgetXID(panel
->win
), pixmap
);
518 if (mask
&& wShapeSupported
)
519 XShapeCombineMask(dpy
, WMWidgetXID(panel
->win
), ShapeBounding
, 0, 0, mask
, ShapeSet
);
523 XFreePixmap(dpy
, pixmap
);
525 XFreePixmap(dpy
, mask
);
530 center
= wGetPointToCenterRectInHead(scr
, wGetHeadForPointerLocation(scr
),
531 width
+ 2 * BORDER_SPACE
, height
+ 2 * BORDER_SPACE
);
532 WMMoveWidget(panel
->win
, center
.x
, center
.y
);
535 panel
->current
= WMGetFirstInArray(panel
->windows
, curwin
);
536 if (panel
->current
>= 0)
537 changeImage(panel
, panel
->current
, 1);
539 WMMapWidget(panel
->win
);
544 void wSwitchPanelDestroy(WSwitchPanel
* panel
)
550 WMUnmapWidget(panel
->win
);
553 WM_ITERATE_ARRAY(panel
->images
, image
, i
) {
555 RReleaseImage(image
);
557 WMFreeArray(panel
->images
);
560 WMDestroyWidget(panel
->win
);
562 WMFreeArray(panel
->icons
);
563 WMFreeArray(panel
->windows
);
565 RReleaseImage(panel
->defIcon
);
567 RReleaseImage(panel
->tile
);
569 RReleaseImage(panel
->tileTmp
);
571 RReleaseImage(panel
->bg
);
573 WMReleaseFont(panel
->font
);
575 WMReleaseColor(panel
->white
);
579 WWindow
*wSwitchPanelSelectNext(WSwitchPanel
* panel
, int back
)
582 int count
= WMGetArrayItemCount(panel
->windows
);
588 changeImage(panel
, panel
->current
, 0);
595 wwin
= WMGetFromArray(panel
->windows
, (count
+ panel
->current
) % count
);
598 if (panel
->current
< 0)
599 scrollIcons(panel
, count
);
600 else if (panel
->current
< panel
->firstVisible
)
601 scrollIcons(panel
, -1);
603 if (panel
->current
>= count
)
604 scrollIcons(panel
, -count
);
605 else if (panel
->current
- panel
->firstVisible
>= panel
->visibleCount
)
606 scrollIcons(panel
, 1);
609 panel
->current
= (count
+ panel
->current
) % count
;
612 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
614 changeImage(panel
, panel
->current
, 1);
620 WWindow
*wSwitchPanelSelectFirst(WSwitchPanel
* panel
, int back
)
623 int count
= WMGetArrayItemCount(panel
->windows
);
629 changeImage(panel
, panel
->current
, 0);
632 panel
->current
= count
- 1;
633 scrollIcons(panel
, count
);
636 scrollIcons(panel
, -count
);
639 wwin
= WMGetFromArray(panel
->windows
, panel
->current
);
642 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
644 changeImage(panel
, panel
->current
, 1);
649 WWindow
*wSwitchPanelHandleEvent(WSwitchPanel
* panel
, XEvent
* event
)
658 /* if (event->type == LeaveNotify) {
659 if (event->xcrossing.window == WMWidgetXID(panel->win))
661 } else */ if (event
->type
== MotionNotify
) {
663 WM_ITERATE_ARRAY(panel
->icons
, icon
, i
) {
664 if (WMWidgetXID(icon
) == event
->xmotion
.window
) {
670 if (focus
>= 0 && panel
->current
!= focus
) {
673 changeImage(panel
, panel
->current
, 0);
674 changeImage(panel
, focus
, 1);
675 panel
->current
= focus
;
677 wwin
= WMGetFromArray(panel
->windows
, focus
);
679 drawTitle(panel
, panel
->current
, wwin
->frame
->title
);
687 Window
wSwitchPanelGetWindow(WSwitchPanel
* swpanel
)
691 return WMWidgetXID(swpanel
->win
);