1 /* appicon.c- icon for applications (not mini-window)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 #include <X11/Xutil.h>
31 #include "WindowMaker.h"
41 #include "workspace.h"
42 #include "superfluous.h"
52 * icon_file for the dock is got from the preferences file by
53 * using the classname/instancename
56 /**** Global variables ****/
57 extern Cursor wCursor
[WCUR_LAST
];
58 extern WPreferences wPreferences
;
60 #define MOD_MASK wPreferences.modifier_mask
62 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
);
63 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
);
64 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
);
66 WAppIcon
*wAppIconCreateForDock(WScreen
* scr
, char *command
, char *wm_instance
, char *wm_class
, int tile
)
71 dicon
= wmalloc(sizeof(WAppIcon
));
73 memset(dicon
, 0, sizeof(WAppIcon
));
78 dicon
->next
= scr
->app_icon_list
;
79 if (scr
->app_icon_list
) {
80 scr
->app_icon_list
->prev
= dicon
;
82 scr
->app_icon_list
= dicon
;
85 dicon
->command
= wstrdup(command
);
88 dicon
->wm_class
= wstrdup(wm_class
);
90 dicon
->wm_instance
= wstrdup(wm_instance
);
92 path
= wDefaultGetIconFile(scr
, wm_instance
, wm_class
, True
);
93 if (!path
&& command
) {
94 wApplicationExtractDirPackIcon(scr
, command
, wm_instance
, wm_class
);
96 path
= wDefaultGetIconFile(scr
, wm_instance
, wm_class
, False
);
100 path
= FindImage(wPreferences
.icon_path
, path
);
102 dicon
->icon
= wIconCreateWithIconFile(scr
, path
, tile
);
106 wXDNDMakeAwareness(dicon
->icon
->core
->window
);
109 /* will be overriden by dock */
110 dicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
111 dicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
112 dicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
113 dicon
->icon
->core
->descriptor
.parent
= dicon
;
114 AddToStackList(dicon
->icon
->core
);
119 WAppIcon
*wAppIconCreate(WWindow
* leader_win
)
122 WScreen
*scr
= leader_win
->screen_ptr
;
124 aicon
= wmalloc(sizeof(WAppIcon
));
126 memset(aicon
, 0, sizeof(WAppIcon
));
132 aicon
->next
= scr
->app_icon_list
;
133 if (scr
->app_icon_list
) {
134 scr
->app_icon_list
->prev
= aicon
;
136 scr
->app_icon_list
= aicon
;
138 if (leader_win
->wm_class
)
139 aicon
->wm_class
= wstrdup(leader_win
->wm_class
);
140 if (leader_win
->wm_instance
)
141 aicon
->wm_instance
= wstrdup(leader_win
->wm_instance
);
143 aicon
->icon
= wIconCreate(leader_win
);
145 wXDNDMakeAwareness(aicon
->icon
->core
->window
);
148 /* will be overriden if docked */
149 aicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
150 aicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
151 aicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
152 aicon
->icon
->core
->descriptor
.parent
= aicon
;
153 AddToStackList(aicon
->icon
->core
);
154 aicon
->icon
->show_title
= 0;
155 wIconUpdate(aicon
->icon
);
160 void wAppIconDestroy(WAppIcon
* aicon
)
162 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
164 RemoveFromStackList(aicon
->icon
->core
);
165 wIconDestroy(aicon
->icon
);
167 wfree(aicon
->command
);
169 if (aicon
->dnd_command
)
170 wfree(aicon
->dnd_command
);
172 if (aicon
->wm_instance
)
173 wfree(aicon
->wm_instance
);
175 wfree(aicon
->wm_class
);
177 if (aicon
== scr
->app_icon_list
) {
179 aicon
->next
->prev
= NULL
;
180 scr
->app_icon_list
= aicon
->next
;
183 aicon
->next
->prev
= aicon
->prev
;
185 aicon
->prev
->next
= aicon
->next
;
188 aicon
->destroyed
= 1;
192 static void drawCorner(WIcon
* icon
)
194 WScreen
*scr
= icon
->core
->screen_ptr
;
203 XFillPolygon(dpy
, icon
->core
->window
, scr
->icon_title_texture
->normal_gc
,
204 points
, 3, Convex
, CoordModeOrigin
);
205 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 0, 12);
206 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 12, 0);
207 /* drawing the second line gives a weird concave look. -Dan */
209 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 1, 11);
210 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 11, 1);
214 void wAppIconMove(WAppIcon
* aicon
, int x
, int y
)
216 XMoveWindow(dpy
, aicon
->icon
->core
->window
, x
, y
);
222 static void updateDockNumbers(WScreen
* scr
)
226 WAppIcon
*dicon
= scr
->dock
->icon_array
[0];
228 ws_numbers
= wmalloc(20);
229 snprintf(ws_numbers
, 20, "%i [ %i ]", scr
->current_workspace
+ 1, ((scr
->current_workspace
/ 10) + 1));
230 length
= strlen(ws_numbers
);
232 XClearArea(dpy
, dicon
->icon
->core
->window
, 2, 2, 50, WMFontHeight(scr
->icon_title_font
) + 1, False
);
234 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->black
,
235 scr
->icon_title_font
, 4, 3, ws_numbers
, length
);
237 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->white
,
238 scr
->icon_title_font
, 3, 2, ws_numbers
, length
);
242 #endif /* WS_INDICATOR */
244 void wAppIconPaint(WAppIcon
* aicon
)
247 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
249 if (aicon
->icon
->owner
)
250 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
254 wIconPaint(aicon
->icon
);
257 if (aicon
->docked
&& scr
->dock
&& scr
->dock
== aicon
->dock
&& aicon
->yindex
== 0)
258 updateDockNumbers(scr
);
260 if (scr
->dock_dots
&& aicon
->docked
&& !aicon
->running
&& aicon
->command
!= NULL
) {
261 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
262 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
263 XCopyArea(dpy
, scr
->dock_dots
->image
, aicon
->icon
->core
->window
,
264 scr
->copy_gc
, 0, 0, scr
->dock_dots
->width
, scr
->dock_dots
->height
, 0, 0);
267 if (wapp
&& wapp
->flags
.hidden
) {
268 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
269 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
270 XCopyArea(dpy
, scr
->dock_dots
->image
,
271 aicon
->icon
->core
->window
, scr
->copy_gc
, 0, 0, 7, scr
->dock_dots
->height
, 0, 0);
273 #endif /* HIDDENDOT */
275 if (aicon
->omnipresent
)
276 drawCorner(aicon
->icon
);
278 XSetClipMask(dpy
, scr
->copy_gc
, None
);
279 if (aicon
->launching
) {
280 XFillRectangle(dpy
, aicon
->icon
->core
->window
, scr
->stipple_gc
,
281 0, 0, wPreferences
.icon_size
, wPreferences
.icon_size
);
285 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
287 static void hideCallback(WMenu
* menu
, WMenuEntry
* entry
)
289 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
291 if (wapp
->flags
.hidden
) {
292 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
293 wUnhideApplication(wapp
, False
, False
);
295 wHideApplication(wapp
);
299 static void unhideHereCallback(WMenu
* menu
, WMenuEntry
* entry
)
301 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
303 wUnhideApplication(wapp
, False
, True
);
306 static void setIconCallback(WMenu
* menu
, WMenuEntry
* entry
)
308 WAppIcon
*icon
= ((WApplication
*) entry
->clientdata
)->app_icon
;
313 assert(icon
!= NULL
);
318 scr
= icon
->icon
->core
->screen_ptr
;
322 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
324 if (result
&& !icon
->destroyed
) {
325 if (file
&& *file
== 0) {
329 if (!wIconChangeImageFile(icon
->icon
, file
)) {
330 wMessageDialog(scr
, _("Error"),
331 _("Could not open specified icon file"), _("OK"), NULL
, NULL
);
333 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
343 static void killCallback(WMenu
* menu
, WMenuEntry
* entry
)
345 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
346 WFakeGroupLeader
*fPtr
;
349 char *basename(const char *shortname
);
351 if (!WCHECK_STATE(WSTATE_NORMAL
))
354 WCHANGE_STATE(WSTATE_MODAL
);
356 assert(entry
->clientdata
!= NULL
);
358 shortname
= basename(wapp
->app_icon
->wm_instance
);
360 buffer
= wstrconcat(wapp
->app_icon
? shortname
: NULL
,
361 _(" will be forcibly closed.\n"
362 "Any unsaved changes will be lost.\n" "Please confirm."));
364 fPtr
= wapp
->main_window_desc
->fake_group
;
366 wretain(wapp
->main_window_desc
);
367 if (wPreferences
.dont_confirm_kill
368 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
369 buffer
, _("Yes"), _("No"), NULL
) == WAPRDefault
) {
371 WWindow
*wwin
, *twin
;
373 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
376 if (wwin
->fake_group
== fPtr
) {
381 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
382 wClientKill(wapp
->main_window_desc
);
385 wrelease(wapp
->main_window_desc
);
389 WCHANGE_STATE(WSTATE_NORMAL
);
392 static WMenu
*createApplicationMenu(WScreen
* scr
)
396 menu
= wMenuCreate(scr
, NULL
, False
);
397 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
398 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
399 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
400 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
405 static void openApplicationMenu(WApplication
* wapp
, int x
, int y
)
408 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
411 if (!scr
->icon_menu
) {
412 scr
->icon_menu
= createApplicationMenu(scr
);
413 wfree(scr
->icon_menu
->entries
[1]->text
);
416 menu
= scr
->icon_menu
;
418 if (wapp
->flags
.hidden
) {
419 menu
->entries
[1]->text
= _("Unhide");
421 menu
->entries
[1]->text
= _("Hide");
424 menu
->flags
.realized
= 0;
427 x
-= menu
->frame
->core
->width
/ 2;
428 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
429 x
= scr
->scr_width
- menu
->frame
->core
->width
;
433 /* set client data */
434 for (i
= 0; i
< menu
->entry_no
; i
++) {
435 menu
->entries
[i
]->clientdata
= wapp
;
437 wMenuMapAt(menu
, x
, y
, False
);
440 /******************************************************************/
442 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
)
444 wAppIconPaint(desc
->parent
);
447 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
)
449 WAppIcon
*aicon
= desc
->parent
;
451 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
454 assert(aicon
->icon
->owner
!= NULL
);
456 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
459 wwarning("could not find application descriptor for app icon!!");
464 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
466 /* go to the last workspace that the user worked on the app */
467 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
468 wWorkspaceChange(scr
, wapp
->last_workspace
);
470 wUnhideApplication(wapp
, event
->xbutton
.button
== Button2
, unhideHere
);
472 if (event
->xbutton
.state
& MOD_MASK
) {
473 wHideOtherApplications(aicon
->icon
->owner
);
477 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
479 WAppIcon
*aicon
= desc
->parent
;
480 WIcon
*icon
= aicon
->icon
;
482 int x
= aicon
->x_pos
, y
= aicon
->y_pos
;
483 int dx
= event
->xbutton
.x
, dy
= event
->xbutton
.y
;
486 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
487 WScreen
*scr
= icon
->core
->screen_ptr
;
488 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
489 int shad_x
= 0, shad_y
= 0, docking
= 0, dockable
, collapsed
= 0;
491 int clickButton
= event
->xbutton
.button
;
494 Bool movingSingle
= False
;
497 Bool hasMoved
= False
;
499 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
502 if (IsDoubleClick(scr
, event
)) {
503 iconDblClick(desc
, event
);
507 if (event
->xbutton
.button
== Button3
) {
508 WObjDescriptor
*desc
;
509 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
514 if (event
->xbutton
.send_event
&&
515 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
516 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
517 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
518 wwarning("pointer grab failed for appicon menu");
522 openApplicationMenu(wapp
, event
->xbutton
.x_root
, event
->xbutton
.y_root
);
524 /* allow drag select of menu */
525 desc
= &scr
->icon_menu
->menu
->descriptor
;
526 event
->xbutton
.send_event
= True
;
527 (*desc
->handle_mousedown
) (desc
, event
);
533 if (event
->xbutton
.state
& MOD_MASK
)
534 wLowerFrame(icon
->core
);
536 wRaiseFrame(icon
->core
);
538 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
539 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
540 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
541 wwarning("pointer grab failed for appicon move");
544 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
547 dockable
= canBeDocked(icon
->owner
);
549 wins
[0] = icon
->core
->window
;
550 wins
[1] = scr
->dock_shadow
;
551 XRestackWindows(dpy
, wins
, 2);
553 if (icon
->pixmap
!= None
)
554 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
556 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
557 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
, ghost
);
558 XClearWindow(dpy
, scr
->dock_shadow
);
562 WMMaskEvent(dpy
, PointerMotionMask
| ButtonReleaseMask
| ButtonPressMask
563 | ButtonMotionMask
| ExposureMask
| EnterWindowMask
, &ev
);
570 /* It means the cursor moved so fast that it entered
571 * something else (if moving slowly, it would have
572 * stayed in the appIcon that is being moved. Ignore
573 * such "spurious" EnterNotifiy's */
579 if (abs(dx
- ev
.xmotion
.x
) >= MOVE_THRESHOLD
580 || abs(dy
- ev
.xmotion
.y
) >= MOVE_THRESHOLD
) {
581 XChangeActivePointerGrab(dpy
, ButtonMotionMask
582 | ButtonReleaseMask
| ButtonPressMask
,
583 wCursor
[WCUR_MOVE
], CurrentTime
);
589 x
= ev
.xmotion
.x_root
- dx
;
590 y
= ev
.xmotion
.y_root
- dy
;
593 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
595 wAppIconMove(aicon
, x
, y
);
599 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
, &ix
, &iy
, False
)) {
600 shad_x
= scr
->dock
->x_pos
+ ix
* wPreferences
.icon_size
;
601 shad_y
= scr
->dock
->y_pos
+ iy
* wPreferences
.icon_size
;
603 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
604 scr
->last_dock
->collapsed
= 1;
605 wDockHideIcons(scr
->last_dock
);
608 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
609 scr
->dock
->collapsed
= 0;
610 wDockShowIcons(scr
->dock
);
613 if (scr
->dock
->auto_raise_lower
)
614 wDockRaise(scr
->dock
);
616 scr
->last_dock
= scr
->dock
;
618 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
620 XMapWindow(dpy
, scr
->dock_shadow
);
623 } else if (workspace
->clip
&&
624 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
, &ix
, &iy
, False
)) {
625 shad_x
= workspace
->clip
->x_pos
+ ix
* wPreferences
.icon_size
;
626 shad_y
= workspace
->clip
->y_pos
+ iy
* wPreferences
.icon_size
;
628 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
629 scr
->last_dock
->collapsed
= 1;
630 wDockHideIcons(scr
->last_dock
);
633 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
634 workspace
->clip
->collapsed
= 0;
635 wDockShowIcons(workspace
->clip
);
638 if (workspace
->clip
->auto_raise_lower
)
639 wDockRaise(workspace
->clip
);
641 scr
->last_dock
= workspace
->clip
;
643 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
645 XMapWindow(dpy
, scr
->dock_shadow
);
648 } else if (docking
) {
649 XUnmapWindow(dpy
, scr
->dock_shadow
);
660 if (ev
.xbutton
.button
!= clickButton
)
662 XUngrabPointer(dpy
, CurrentTime
);
667 /* icon is trying to be docked */
668 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
669 XUnmapWindow(dpy
, scr
->dock_shadow
);
670 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
671 if (scr
->last_dock
->auto_collapse
) {
674 if (workspace
->clip
&&
675 workspace
->clip
!= scr
->last_dock
&& workspace
->clip
->auto_raise_lower
)
676 wDockLower(workspace
->clip
);
679 /* If icon could not be docked, slide it back to the old
681 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
685 /* move back to its place */
686 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
687 wAppIconMove(aicon
, oldX
, oldY
);
689 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
693 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
694 wDockLower(workspace
->clip
);
697 scr
->last_dock
->collapsed
= 1;
698 wDockHideIcons(scr
->last_dock
);
703 XFreePixmap(dpy
, ghost
);
704 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
707 if (wPreferences
.auto_arrange_icons
)
708 wArrangeIcons(scr
, True
);
710 if (wPreferences
.single_click
&& !hasMoved
)
711 iconDblClick(desc
, event
);
718 puts("End icon move");