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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <X11/Xutil.h>
30 #include "WindowMaker.h"
33 #include "application.h"
40 #include "workspace.h"
41 #include "superfluous.h"
51 * icon_file for the dock is got from the preferences file by
52 * using the classname/instancename
55 /**** Global variables ****/
56 extern Cursor wCursor
[WCUR_LAST
];
57 extern WPreferences wPreferences
;
59 #define MOD_MASK wPreferences.modifier_mask
61 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
);
62 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
);
63 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
);
65 WAppIcon
*wAppIconCreateForDock(WScreen
* scr
, char *command
, char *wm_instance
, char *wm_class
, int tile
)
70 dicon
= wmalloc(sizeof(WAppIcon
));
72 memset(dicon
, 0, sizeof(WAppIcon
));
77 dicon
->next
= scr
->app_icon_list
;
78 if (scr
->app_icon_list
) {
79 scr
->app_icon_list
->prev
= dicon
;
81 scr
->app_icon_list
= dicon
;
84 dicon
->command
= wstrdup(command
);
87 dicon
->wm_class
= wstrdup(wm_class
);
89 dicon
->wm_instance
= wstrdup(wm_instance
);
91 path
= wDefaultGetIconFile(scr
, wm_instance
, wm_class
, True
);
92 if (!path
&& command
) {
93 wApplicationExtractDirPackIcon(scr
, command
, wm_instance
, wm_class
);
95 path
= wDefaultGetIconFile(scr
, wm_instance
, wm_class
, False
);
99 path
= FindImage(wPreferences
.icon_path
, path
);
101 dicon
->icon
= wIconCreateWithIconFile(scr
, path
, tile
);
105 wXDNDMakeAwareness(dicon
->icon
->core
->window
);
108 /* will be overriden by dock */
109 dicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
110 dicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
111 dicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
112 dicon
->icon
->core
->descriptor
.parent
= dicon
;
113 AddToStackList(dicon
->icon
->core
);
118 WAppIcon
*wAppIconCreate(WWindow
* leader_win
)
121 WScreen
*scr
= leader_win
->screen_ptr
;
123 aicon
= wmalloc(sizeof(WAppIcon
));
125 memset(aicon
, 0, sizeof(WAppIcon
));
131 aicon
->next
= scr
->app_icon_list
;
132 if (scr
->app_icon_list
) {
133 scr
->app_icon_list
->prev
= aicon
;
135 scr
->app_icon_list
= aicon
;
137 if (leader_win
->wm_class
)
138 aicon
->wm_class
= wstrdup(leader_win
->wm_class
);
139 if (leader_win
->wm_instance
)
140 aicon
->wm_instance
= wstrdup(leader_win
->wm_instance
);
142 aicon
->icon
= wIconCreate(leader_win
);
144 wXDNDMakeAwareness(aicon
->icon
->core
->window
);
147 /* will be overriden if docked */
148 aicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
149 aicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
150 aicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
151 aicon
->icon
->core
->descriptor
.parent
= aicon
;
152 AddToStackList(aicon
->icon
->core
);
153 aicon
->icon
->show_title
= 0;
154 wIconUpdate(aicon
->icon
);
159 void wAppIconDestroy(WAppIcon
* aicon
)
161 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
163 RemoveFromStackList(aicon
->icon
->core
);
164 wIconDestroy(aicon
->icon
);
166 wfree(aicon
->command
);
168 if (aicon
->dnd_command
)
169 wfree(aicon
->dnd_command
);
171 if (aicon
->wm_instance
)
172 wfree(aicon
->wm_instance
);
174 wfree(aicon
->wm_class
);
176 if (aicon
== scr
->app_icon_list
) {
178 aicon
->next
->prev
= NULL
;
179 scr
->app_icon_list
= aicon
->next
;
182 aicon
->next
->prev
= aicon
->prev
;
184 aicon
->prev
->next
= aicon
->next
;
187 aicon
->destroyed
= 1;
191 static void drawCorner(WIcon
* icon
)
193 WScreen
*scr
= icon
->core
->screen_ptr
;
202 XFillPolygon(dpy
, icon
->core
->window
, scr
->icon_title_texture
->normal_gc
,
203 points
, 3, Convex
, CoordModeOrigin
);
204 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 0, 12);
205 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 12, 0);
206 /* drawing the second line gives a weird concave look. -Dan */
208 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 1, 11);
209 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 11, 1);
213 void wAppIconMove(WAppIcon
* aicon
, int x
, int y
)
215 XMoveWindow(dpy
, aicon
->icon
->core
->window
, x
, y
);
221 static void updateDockNumbers(WScreen
* scr
)
225 WAppIcon
*dicon
= scr
->dock
->icon_array
[0];
227 ws_numbers
= wmalloc(20);
228 snprintf(ws_numbers
, 20, "%i [ %i ]", scr
->current_workspace
+ 1, ((scr
->current_workspace
/ 10) + 1));
229 length
= strlen(ws_numbers
);
231 XClearArea(dpy
, dicon
->icon
->core
->window
, 2, 2, 50, WMFontHeight(scr
->icon_title_font
) + 1, False
);
233 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->black
,
234 scr
->icon_title_font
, 4, 3, ws_numbers
, length
);
236 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->white
,
237 scr
->icon_title_font
, 3, 2, ws_numbers
, length
);
241 #endif /* WS_INDICATOR */
243 void wAppIconPaint(WAppIcon
* aicon
)
246 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
248 if (aicon
->icon
->owner
)
249 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
253 wIconPaint(aicon
->icon
);
256 if (aicon
->docked
&& scr
->dock
&& scr
->dock
== aicon
->dock
&& aicon
->yindex
== 0)
257 updateDockNumbers(scr
);
259 if (scr
->dock_dots
&& aicon
->docked
&& !aicon
->running
&& aicon
->command
!= NULL
) {
260 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
261 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
262 XCopyArea(dpy
, scr
->dock_dots
->image
, aicon
->icon
->core
->window
,
263 scr
->copy_gc
, 0, 0, scr
->dock_dots
->width
, scr
->dock_dots
->height
, 0, 0);
266 if (wapp
&& wapp
->flags
.hidden
) {
267 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
268 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
269 XCopyArea(dpy
, scr
->dock_dots
->image
,
270 aicon
->icon
->core
->window
, scr
->copy_gc
, 0, 0, 7, scr
->dock_dots
->height
, 0, 0);
272 #endif /* HIDDENDOT */
274 if (aicon
->omnipresent
)
275 drawCorner(aicon
->icon
);
277 XSetClipMask(dpy
, scr
->copy_gc
, None
);
278 if (aicon
->launching
) {
279 XFillRectangle(dpy
, aicon
->icon
->core
->window
, scr
->stipple_gc
,
280 0, 0, wPreferences
.icon_size
, wPreferences
.icon_size
);
284 Bool
wAppIconSave(WAppIcon
*aicon
)
288 if (!aicon
->docked
|| aicon
->attracted
) return True
;
290 path
= wIconStore(aicon
->icon
);
294 wApplicationSaveIconPathFor(path
, aicon
->wm_instance
, aicon
->wm_class
);
300 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
302 static void hideCallback(WMenu
* menu
, WMenuEntry
* entry
)
304 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
306 if (wapp
->flags
.hidden
) {
307 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
308 wUnhideApplication(wapp
, False
, False
);
310 wHideApplication(wapp
);
314 static void unhideHereCallback(WMenu
* menu
, WMenuEntry
* entry
)
316 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
318 wUnhideApplication(wapp
, False
, True
);
321 static void setIconCallback(WMenu
* menu
, WMenuEntry
* entry
)
323 WAppIcon
*icon
= ((WApplication
*) entry
->clientdata
)->app_icon
;
328 assert(icon
!= NULL
);
333 scr
= icon
->icon
->core
->screen_ptr
;
337 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
339 if (result
&& !icon
->destroyed
) {
340 if (file
&& *file
== 0) {
344 if (!wIconChangeImageFile(icon
->icon
, file
)) {
345 wMessageDialog(scr
, _("Error"),
346 _("Could not open specified icon file"), _("OK"), NULL
, NULL
);
348 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
358 static void killCallback(WMenu
* menu
, WMenuEntry
* entry
)
360 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
361 WFakeGroupLeader
*fPtr
;
364 char *basename(const char *shortname
);
366 if (!WCHECK_STATE(WSTATE_NORMAL
))
369 WCHANGE_STATE(WSTATE_MODAL
);
371 assert(entry
->clientdata
!= NULL
);
373 shortname
= basename(wapp
->app_icon
->wm_instance
);
375 buffer
= wstrconcat(wapp
->app_icon
? shortname
: NULL
,
376 _(" will be forcibly closed.\n"
377 "Any unsaved changes will be lost.\n" "Please confirm."));
379 fPtr
= wapp
->main_window_desc
->fake_group
;
381 wretain(wapp
->main_window_desc
);
382 if (wPreferences
.dont_confirm_kill
383 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
384 buffer
, _("Yes"), _("No"), NULL
) == WAPRDefault
) {
386 WWindow
*wwin
, *twin
;
388 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
391 if (wwin
->fake_group
== fPtr
) {
396 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
397 wClientKill(wapp
->main_window_desc
);
400 wrelease(wapp
->main_window_desc
);
404 WCHANGE_STATE(WSTATE_NORMAL
);
407 static WMenu
*createApplicationMenu(WScreen
* scr
)
411 menu
= wMenuCreate(scr
, NULL
, False
);
412 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
413 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
414 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
415 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
420 static void openApplicationMenu(WApplication
* wapp
, int x
, int y
)
423 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
426 if (!scr
->icon_menu
) {
427 scr
->icon_menu
= createApplicationMenu(scr
);
428 wfree(scr
->icon_menu
->entries
[1]->text
);
431 menu
= scr
->icon_menu
;
433 if (wapp
->flags
.hidden
) {
434 menu
->entries
[1]->text
= _("Unhide");
436 menu
->entries
[1]->text
= _("Hide");
439 menu
->flags
.realized
= 0;
442 x
-= menu
->frame
->core
->width
/ 2;
443 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
444 x
= scr
->scr_width
- menu
->frame
->core
->width
;
448 /* set client data */
449 for (i
= 0; i
< menu
->entry_no
; i
++) {
450 menu
->entries
[i
]->clientdata
= wapp
;
452 wMenuMapAt(menu
, x
, y
, False
);
455 /******************************************************************/
457 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
)
459 wAppIconPaint(desc
->parent
);
462 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
)
464 WAppIcon
*aicon
= desc
->parent
;
466 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
469 assert(aicon
->icon
->owner
!= NULL
);
471 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
472 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
474 /* go to the last workspace that the user worked on the app */
475 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
476 wWorkspaceChange(scr
, wapp
->last_workspace
);
478 wUnhideApplication(wapp
, event
->xbutton
.button
== Button2
, unhideHere
);
480 if (event
->xbutton
.state
& MOD_MASK
) {
481 wHideOtherApplications(aicon
->icon
->owner
);
485 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
487 WAppIcon
*aicon
= desc
->parent
;
488 WIcon
*icon
= aicon
->icon
;
490 int x
= aicon
->x_pos
, y
= aicon
->y_pos
;
491 int dx
= event
->xbutton
.x
, dy
= event
->xbutton
.y
;
494 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
495 WScreen
*scr
= icon
->core
->screen_ptr
;
496 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
497 int shad_x
= 0, shad_y
= 0, docking
= 0, dockable
, collapsed
= 0;
499 int clickButton
= event
->xbutton
.button
;
502 Bool movingSingle
= False
;
505 Bool hasMoved
= False
;
507 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
510 if (IsDoubleClick(scr
, event
)) {
511 iconDblClick(desc
, event
);
515 if (event
->xbutton
.button
== Button3
) {
516 WObjDescriptor
*desc
;
517 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
522 if (event
->xbutton
.send_event
&&
523 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
524 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
525 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
526 wwarning("pointer grab failed for appicon menu");
530 openApplicationMenu(wapp
, event
->xbutton
.x_root
, event
->xbutton
.y_root
);
532 /* allow drag select of menu */
533 desc
= &scr
->icon_menu
->menu
->descriptor
;
534 event
->xbutton
.send_event
= True
;
535 (*desc
->handle_mousedown
) (desc
, event
);
539 if (event
->xbutton
.state
& MOD_MASK
)
540 wLowerFrame(icon
->core
);
542 wRaiseFrame(icon
->core
);
544 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
545 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
546 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
547 wwarning("pointer grab failed for appicon move");
550 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
553 dockable
= canBeDocked(icon
->owner
);
555 wins
[0] = icon
->core
->window
;
556 wins
[1] = scr
->dock_shadow
;
557 XRestackWindows(dpy
, wins
, 2);
559 if (icon
->pixmap
!= None
)
560 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
562 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
563 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
, ghost
);
564 XClearWindow(dpy
, scr
->dock_shadow
);
568 WMMaskEvent(dpy
, PointerMotionMask
| ButtonReleaseMask
| ButtonPressMask
569 | ButtonMotionMask
| ExposureMask
| EnterWindowMask
, &ev
);
576 /* It means the cursor moved so fast that it entered
577 * something else (if moving slowly, it would have
578 * stayed in the appIcon that is being moved. Ignore
579 * such "spurious" EnterNotifiy's */
585 if (abs(dx
- ev
.xmotion
.x
) >= MOVE_THRESHOLD
586 || abs(dy
- ev
.xmotion
.y
) >= MOVE_THRESHOLD
) {
587 XChangeActivePointerGrab(dpy
, ButtonMotionMask
588 | ButtonReleaseMask
| ButtonPressMask
,
589 wCursor
[WCUR_MOVE
], CurrentTime
);
595 x
= ev
.xmotion
.x_root
- dx
;
596 y
= ev
.xmotion
.y_root
- dy
;
599 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
601 wAppIconMove(aicon
, x
, y
);
605 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
, &ix
, &iy
, False
)) {
606 shad_x
= scr
->dock
->x_pos
+ ix
* wPreferences
.icon_size
;
607 shad_y
= scr
->dock
->y_pos
+ iy
* wPreferences
.icon_size
;
609 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
610 scr
->last_dock
->collapsed
= 1;
611 wDockHideIcons(scr
->last_dock
);
614 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
615 scr
->dock
->collapsed
= 0;
616 wDockShowIcons(scr
->dock
);
619 if (scr
->dock
->auto_raise_lower
)
620 wDockRaise(scr
->dock
);
622 scr
->last_dock
= scr
->dock
;
624 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
626 XMapWindow(dpy
, scr
->dock_shadow
);
629 } else if (workspace
->clip
&&
630 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
, &ix
, &iy
, False
)) {
631 shad_x
= workspace
->clip
->x_pos
+ ix
* wPreferences
.icon_size
;
632 shad_y
= workspace
->clip
->y_pos
+ iy
* wPreferences
.icon_size
;
634 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
635 scr
->last_dock
->collapsed
= 1;
636 wDockHideIcons(scr
->last_dock
);
639 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
640 workspace
->clip
->collapsed
= 0;
641 wDockShowIcons(workspace
->clip
);
644 if (workspace
->clip
->auto_raise_lower
)
645 wDockRaise(workspace
->clip
);
647 scr
->last_dock
= workspace
->clip
;
649 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
651 XMapWindow(dpy
, scr
->dock_shadow
);
654 } else if (docking
) {
655 XUnmapWindow(dpy
, scr
->dock_shadow
);
666 if (ev
.xbutton
.button
!= clickButton
)
668 XUngrabPointer(dpy
, CurrentTime
);
673 /* icon is trying to be docked */
674 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
675 XUnmapWindow(dpy
, scr
->dock_shadow
);
676 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
677 if (scr
->last_dock
->auto_collapse
) {
680 if (workspace
->clip
&&
681 workspace
->clip
!= scr
->last_dock
&& workspace
->clip
->auto_raise_lower
)
682 wDockLower(workspace
->clip
);
685 /* If icon could not be docked, slide it back to the old
687 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
691 /* move back to its place */
692 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
693 wAppIconMove(aicon
, oldX
, oldY
);
695 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
699 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
700 wDockLower(workspace
->clip
);
703 scr
->last_dock
->collapsed
= 1;
704 wDockHideIcons(scr
->last_dock
);
709 XFreePixmap(dpy
, ghost
);
710 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
713 if (wPreferences
.auto_arrange_icons
)
714 wArrangeIcons(scr
, True
);
716 if (wPreferences
.single_click
&& !hasMoved
)
717 iconDblClick(desc
, event
);