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,
29
#include <X11/extensions/shape.h>
31
extern Bool wShapeSupported;
34
#include "WindowMaker.h"
40
#include "switchpanel.h"
72
extern WPreferences wPreferences;
74
#define BORDER_SPACE 10
76
#define ICON_TILE_SIZE 64
77
#define LABEL_HEIGHT 25
78
#define SCREEN_BORDER_SPACING 2*20
79
#define SCROLL_STEPS (ICON_TILE_SIZE/2)
81
static int canReceiveFocus(WWindow *wwin)
83
if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
85
if (!wwin->flags.mapped)
87
if (!wwin->flags.shaded && !wwin->flags.miniaturized && !wwin->flags.hidden)
92
if (WFLAGP(wwin, no_focusable))
98
static void changeImage(WSwitchPanel *panel, int index, int selected)
100
WMFrame *icon = WMGetFromArray(panel->icons, index);
101
RImage *image= WMGetFromArray(panel->images, index);
103
if (!panel->bg && !panel->tile) {
105
WMSetFrameRelief(icon, WRFlat);
115
if (canReceiveFocus(WMGetFromArray(panel->windows, index)) < 0)
118
pos= WMGetViewPosition(WMWidgetView(icon));
119
back= panel->tileTmp;
121
RCopyArea(back, panel->bg,
122
BORDER_SPACE+pos.x-panel->firstVisible*ICON_TILE_SIZE, BORDER_SPACE+pos.y,
123
back->width, back->height,
128
WMScreen *wscr= WMWidgetScreen(icon);
130
color.red = WMRedComponentOfColor(WMGrayColor(wscr))>>8;
131
color.green = WMGreenComponentOfColor(WMGrayColor(wscr))>>8;
132
color.blue = WMBlueComponentOfColor(WMGrayColor(wscr))>>8;
133
RFillImage(back, &color);
138
RCombineArea(back, tile, 0, 0, tile->width, tile->height,
139
(back->width - tile->width)/2, (back->height - tile->height)/2);
141
RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
142
(back->width - image->width)/2, (back->height - image->height)/2,
145
RConvertImage(panel->scr->rcontext, back, &p);
146
XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
147
XClearWindow(dpy, WMWidgetXID(icon));
151
if (!panel->bg && !panel->tile) {
153
WMSetFrameRelief(icon, WRSimple);
158
static RImage *scaleDownIfNeeded(RImage *image)
160
if (image && ((image->width - ICON_SIZE) > 2 || (image->height - ICON_SIZE) > 2)) {
162
nimage= RScaleImage(image, ICON_SIZE, (image->height * ICON_SIZE / image->width));
163
RReleaseImage(image);
170
static void addIconForWindow(WSwitchPanel *panel, WMWidget *parent, WWindow *wwin,
173
WMFrame *icon= WMCreateFrame(parent);
174
RImage *image = NULL;
176
WMSetFrameRelief(icon, WRFlat);
177
WMResizeWidget(icon, ICON_TILE_SIZE, ICON_TILE_SIZE);
178
WMMoveWidget(icon, x, y);
180
if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
181
image = RRetainImage(wwin->net_icon_image);
183
// Make this use a caching thing. When there are many windows,
184
// it's very likely that most of them are instances of the same thing,
185
// so caching them should get performance acceptable in these cases.
187
image = wDefaultGetImage(panel->scr, wwin->wm_instance, wwin->wm_class);
189
if (!image && !panel->defIcon) {
190
char *file = wDefaultGetIconFile(panel->scr, NULL, NULL, False);
192
char *path = FindImage(wPreferences.icon_path, file);
194
image = RLoadImage(panel->scr->rcontext, path, 0);
199
panel->defIcon= scaleDownIfNeeded(image);
202
if (!image && panel->defIcon)
203
image= RRetainImage(panel->defIcon);
205
image= scaleDownIfNeeded(image);
207
WMAddToArray(panel->images, image);
208
WMAddToArray(panel->icons, icon);
212
static void scrollIcons(WSwitchPanel *panel, int delta)
214
int nfirst= panel->firstVisible + delta;
216
int count= WMGetArrayItemCount(panel->windows);
218
// struct timeval tv1, tv2;
220
if (count <= panel->visibleCount)
225
else if (nfirst >= count-panel->visibleCount)
226
nfirst= count-panel->visibleCount;
228
if (nfirst == panel->firstVisible)
231
ox = -panel->firstVisible * ICON_TILE_SIZE;
232
nx = -nfirst * ICON_TILE_SIZE;
233
for (i= 0; i < SCROLL_STEPS; i++) {
235
gettimeofday(&tv1, NULL);
236
WMMoveWidget(panel->iconBox, (nx*i + ox*(SCROLL_STEPS-i))/(SCROLL_STEPS-1), 0);
238
gettimeofday(&tv2, NULL);
239
diff = (tv2.tv_sec-tv1.tv_sec)*10000+(tv2.tv_usec-tv1.tv_usec)/100;
244
WMMoveWidget(panel->iconBox, -nfirst*ICON_TILE_SIZE, 0);
246
panel->firstVisible= nfirst;
248
for (i= panel->firstVisible; i < panel->firstVisible+panel->visibleCount; i++) {
249
changeImage(panel, i, i == panel->current);
259
static RImage *assemblePuzzleImage(RImage **images, int width, int height)
261
RImage *img= RCreateImage(width, height, 1);
273
RFillImage(img, &color);
275
tw= width-images[0]->width-images[2]->width;
276
th= height-images[0]->height-images[6]->height;
278
if (tw <= 0 || th <= 0) {
285
tmp= RSmoothScaleImage(images[1], tw, images[1]->height);
286
RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height,
287
images[0]->width, 0);
292
tmp= RSmoothScaleImage(images[7], tw, images[7]->height);
293
RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height,
294
images[6]->width, height-images[6]->height);
299
tmp= RSmoothScaleImage(images[3], images[3]->width, th);
300
RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height,
301
0, images[0]->height);
306
tmp= RSmoothScaleImage(images[5], images[5]->width, th);
307
RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height,
308
width-images[5]->width, images[2]->height);
312
if (tw > 0 && th > 0) {
313
tmp= RSmoothScaleImage(images[4], tw, th);
314
RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height,
315
images[0]->width, images[0]->height);
320
RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height,
323
RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height,
324
width-images[2]->width, 0);
326
RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height,
327
0, height-images[6]->height);
329
RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
330
width-images[8]->width, height-images[8]->height);
335
static RImage *createBackImage(WScreen *scr, int width, int height)
337
return assemblePuzzleImage(wPreferences.swbackImage, width, height);
341
static RImage *getTile(WSwitchPanel *panel)
345
if (!wPreferences.swtileImage)
348
stile = RScaleImage(wPreferences.swtileImage, ICON_TILE_SIZE, ICON_TILE_SIZE);
350
return wPreferences.swtileImage;
357
drawTitle(WSwitchPanel *panel, int index, char *title)
360
int width= WMWidgetWidth(panel->win);
363
ntitle= ShrinkString(panel->font, title, width-2*BORDER_SPACE);
366
if (strcmp(ntitle, title)!=0)
370
int w= WMWidthOfString(panel->font, ntitle, strlen(ntitle));
372
x= BORDER_SPACE+(index-panel->firstVisible)*ICON_TILE_SIZE + ICON_TILE_SIZE/2 - w/2;
373
if (x < BORDER_SPACE)
375
else if (x + w > width-BORDER_SPACE)
376
x= width-BORDER_SPACE-w;
379
XClearWindow(dpy, WMWidgetXID(panel->win));
380
WMDrawString(panel->scr->wmscreen,
381
WMWidgetXID(panel->win),
382
panel->white, panel->font,
383
x, WMWidgetHeight(panel->win) - BORDER_SPACE - LABEL_HEIGHT + WMFontHeight(panel->font)/2,
384
ntitle, strlen(ntitle));
386
WMSetLabelText(panel->label, ntitle);
393
static WMArray *makeWindowListArray(WScreen *scr, WWindow *curwin, int workspace)
395
WMArray *windows= WMCreateArray(10);
399
for (fl= 0; fl < 2; fl++) {
400
for (wwin= curwin; wwin; wwin= wwin->prev) {
401
if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
402
(!WFLAGP(wwin, skip_window_list) || wwin->flags.internal_window)) {
403
WMAddToArray(windows, wwin);
407
/* start over from the beginning of the list */
411
for (wwin= curwin; wwin && wwin != curwin; wwin= wwin->prev) {
412
if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
413
(!WFLAGP(wwin, skip_window_list) || wwin->flags.internal_window)) {
414
WMAddToArray(windows, wwin);
426
WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, int workspace)
429
WSwitchPanel *panel= wmalloc(sizeof(WSwitchPanel));
433
int iconsThatFitCount;
436
rect= wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
438
memset(panel, 0, sizeof(WSwitchPanel));
442
panel->windows= makeWindowListArray(scr, curwin, workspace);
443
count= WMGetArrayItemCount(panel->windows);
446
WMFreeArray(panel->windows);
451
width= ICON_TILE_SIZE*count;
452
iconsThatFitCount= count;
454
if (width > rect.size.width) {
455
iconsThatFitCount = (WMScreenWidth(scr->wmscreen)-SCREEN_BORDER_SPACING)/ICON_TILE_SIZE;
456
width= iconsThatFitCount*ICON_TILE_SIZE;
459
panel->visibleCount= iconsThatFitCount;
461
height= LABEL_HEIGHT + ICON_TILE_SIZE;
463
panel->tileTmp= RCreateImage(ICON_TILE_SIZE, ICON_TILE_SIZE, 1);
464
panel->tile= getTile(panel);
465
if (panel->tile && wPreferences.swbackImage[8]) {
466
panel->bg= createBackImage(scr, width+2*BORDER_SPACE, height+2*BORDER_SPACE);
468
if (!panel->tileTmp || !panel->tile) {
470
RReleaseImage(panel->bg);
473
RReleaseImage(panel->tile);
476
RReleaseImage(panel->tileTmp);
477
panel->tileTmp= NULL;
480
panel->white= WMWhiteColor(scr->wmscreen);
481
panel->font= WMBoldSystemFontOfSize(scr->wmscreen, 12);
482
panel->icons= WMCreateArray(count);
483
panel->images= WMCreateArray(count);
485
panel->win = WMCreateWindow(scr->wmscreen, "");
488
WMFrame *frame = WMCreateFrame(panel->win);
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, WMDarkGrayColor(scr->wmscreen));
497
WMSetLabelFont(panel->label, panel->font);
498
WMSetLabelTextColor(panel->label, panel->white);
503
WMResizeWidget(panel->win, width + 2*BORDER_SPACE, height + 2*BORDER_SPACE);
505
viewport= WMCreateFrame(panel->win);
506
WMResizeWidget(viewport, width, ICON_TILE_SIZE);
507
WMMoveWidget(viewport, BORDER_SPACE, BORDER_SPACE);
508
WMSetFrameRelief(viewport, WRFlat);
510
panel->iconBox= WMCreateFrame(viewport);
511
WMMoveWidget(panel->iconBox, 0, 0);
512
WMResizeWidget(panel->iconBox, ICON_TILE_SIZE*count, ICON_TILE_SIZE);
513
WMSetFrameRelief(panel->iconBox, WRFlat);
515
WM_ITERATE_ARRAY(panel->windows, wwin, i) {
516
addIconForWindow(panel, panel->iconBox, wwin, i*ICON_TILE_SIZE, 0);
519
WMMapSubwidgets(panel->win);
520
WMRealizeWidget(panel->win);
522
WM_ITERATE_ARRAY(panel->windows, wwin, i) {
523
changeImage(panel, i, 0);
529
RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
531
XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
534
if (mask && wShapeSupported)
535
XShapeCombineMask(dpy, WMWidgetXID(panel->win),
536
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);
562
void wSwitchPanelDestroy(WSwitchPanel *panel)
568
WMUnmapWidget(panel->win);
571
WM_ITERATE_ARRAY(panel->images, image, i) {
573
RReleaseImage(image);
575
WMFreeArray(panel->images);
578
WMDestroyWidget(panel->win);
580
WMFreeArray(panel->icons);
581
WMFreeArray(panel->windows);
583
RReleaseImage(panel->defIcon);
585
RReleaseImage(panel->tile);
587
RReleaseImage(panel->tileTmp);
589
RReleaseImage(panel->bg);
591
WMReleaseFont(panel->font);
596
WWindow *wSwitchPanelSelectNext(WSwitchPanel *panel, int back)
599
int count = WMGetArrayItemCount(panel->windows);
605
changeImage(panel, panel->current, 0);
612
wwin = WMGetFromArray(panel->windows, (count+panel->current)%count);
616
if (panel->current < 0)
617
scrollIcons(panel, count);
618
else if (panel->current < panel->firstVisible)
619
scrollIcons(panel, -1);
623
if (panel->current >= count)
624
scrollIcons(panel, -count);
625
else if (panel->current - panel->firstVisible >= panel->visibleCount)
626
scrollIcons(panel, 1);
629
panel->current= (count+panel->current)%count;
632
drawTitle(panel, panel->current, wwin->frame->title);
634
changeImage(panel, panel->current, 1);
641
WWindow *wSwitchPanelSelectFirst(WSwitchPanel *panel, int back)
644
int count = WMGetArrayItemCount(panel->windows);
650
changeImage(panel, panel->current, 0);
653
panel->current = count-1;
654
scrollIcons(panel, count);
657
scrollIcons(panel, -count);
660
wwin = WMGetFromArray(panel->windows, panel->current);
663
drawTitle(panel, panel->current, wwin->frame->title);
665
changeImage(panel, panel->current, 1);
671
WWindow *wSwitchPanelHandleEvent(WSwitchPanel *panel, XEvent *event)
679
if (event->type == MotionNotify) {
682
WM_ITERATE_ARRAY(panel->icons, icon, i) {
683
if (WMWidgetXID(icon) == event->xmotion.window) {
692
if (focus >= 0 && panel->current != focus) {
695
changeImage(panel, panel->current, 0);
696
changeImage(panel, focus, 1);
697
panel->current= focus;
699
wwin= WMGetFromArray(panel->windows, focus);
701
drawTitle(panel, panel->current, wwin->frame->title);
711
Window wSwitchPanelGetWindow(WSwitchPanel *swpanel)
715
return WMWidgetXID(swpanel->win);