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
);
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
);
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
);
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
);
176 wfree(aicon
->wm_class
);
178 if (aicon
== scr
->app_icon_list
) {
180 aicon
->next
->prev
= NULL
;
181 scr
->app_icon_list
= aicon
->next
;
184 aicon
->next
->prev
= aicon
->prev
;
186 aicon
->prev
->next
= aicon
->next
;
189 aicon
->destroyed
= 1;
193 static void drawCorner(WIcon
* icon
)
195 WScreen
*scr
= icon
->core
->screen_ptr
;
204 XFillPolygon(dpy
, icon
->core
->window
, scr
->icon_title_texture
->normal_gc
,
205 points
, 3, Convex
, CoordModeOrigin
);
206 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 0, 12);
207 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 12, 0);
210 void wAppIconMove(WAppIcon
* aicon
, int x
, int y
)
212 XMoveWindow(dpy
, aicon
->icon
->core
->window
, x
, y
);
218 static void updateDockNumbers(WScreen
* scr
)
222 WAppIcon
*dicon
= scr
->dock
->icon_array
[0];
224 ws_numbers
= wmalloc(20);
225 snprintf(ws_numbers
, 20, "%i [ %i ]", scr
->current_workspace
+ 1, ((scr
->current_workspace
/ 10) + 1));
226 length
= strlen(ws_numbers
);
228 XClearArea(dpy
, dicon
->icon
->core
->window
, 2, 2, 50, WMFontHeight(scr
->icon_title_font
) + 1, False
);
230 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->black
,
231 scr
->icon_title_font
, 4, 3, ws_numbers
, length
);
233 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->white
,
234 scr
->icon_title_font
, 3, 2, ws_numbers
, length
);
238 #endif /* WS_INDICATOR */
240 void wAppIconPaint(WAppIcon
* aicon
)
243 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
245 if (aicon
->icon
->owner
)
246 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
250 wIconPaint(aicon
->icon
);
253 if (aicon
->docked
&& scr
->dock
&& scr
->dock
== aicon
->dock
&& aicon
->yindex
== 0)
254 updateDockNumbers(scr
);
256 if (scr
->dock_dots
&& aicon
->docked
&& !aicon
->running
&& aicon
->command
!= NULL
) {
257 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
258 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
259 XCopyArea(dpy
, scr
->dock_dots
->image
, aicon
->icon
->core
->window
,
260 scr
->copy_gc
, 0, 0, scr
->dock_dots
->width
, scr
->dock_dots
->height
, 0, 0);
263 if (wapp
&& wapp
->flags
.hidden
) {
264 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
265 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
266 XCopyArea(dpy
, scr
->dock_dots
->image
,
267 aicon
->icon
->core
->window
, scr
->copy_gc
, 0, 0, 7, scr
->dock_dots
->height
, 0, 0);
269 #endif /* HIDDENDOT */
271 if (aicon
->omnipresent
)
272 drawCorner(aicon
->icon
);
274 XSetClipMask(dpy
, scr
->copy_gc
, None
);
275 if (aicon
->launching
)
276 XFillRectangle(dpy
, aicon
->icon
->core
->window
, scr
->stipple_gc
,
277 0, 0, wPreferences
.icon_size
, wPreferences
.icon_size
);
280 Bool
wAppIconSave(WAppIcon
*aicon
)
284 if (!aicon
->docked
|| aicon
->attracted
) return True
;
286 path
= wIconStore(aicon
->icon
);
290 wApplicationSaveIconPathFor(path
, aicon
->wm_instance
, aicon
->wm_class
);
296 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
298 /* main_window may not have the full command line; try to find one which does */
299 static void relaunchApplication(WApplication
*wapp
)
302 WWindow
*wlist
, *next
;
304 scr
= wapp
->main_window_desc
->screen_ptr
;
305 wlist
= scr
->focused_window
;
315 if (wlist
->main_window
== wapp
->main_window
) {
316 if (RelaunchWindow(wlist
))
324 static void relaunchCallback(WMenu
* menu
, WMenuEntry
* entry
)
326 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
328 relaunchApplication(wapp
);
331 static void hideCallback(WMenu
* menu
, WMenuEntry
* entry
)
333 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
335 if (wapp
->flags
.hidden
) {
336 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
337 wUnhideApplication(wapp
, False
, False
);
339 wHideApplication(wapp
);
343 static void unhideHereCallback(WMenu
* menu
, WMenuEntry
* entry
)
345 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
347 wUnhideApplication(wapp
, False
, True
);
350 static void setIconCallback(WMenu
* menu
, WMenuEntry
* entry
)
352 WAppIcon
*icon
= ((WApplication
*) entry
->clientdata
)->app_icon
;
357 assert(icon
!= NULL
);
363 scr
= icon
->icon
->core
->screen_ptr
;
367 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
369 if (result
&& !icon
->destroyed
) {
370 if (file
&& *file
== 0) {
374 if (!wIconChangeImageFile(icon
->icon
, file
)) {
375 wMessageDialog(scr
, _("Error"),
376 _("Could not open specified icon file"), _("OK"), NULL
, NULL
);
378 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
388 static void killCallback(WMenu
* menu
, WMenuEntry
* entry
)
390 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
391 WFakeGroupLeader
*fPtr
;
394 char *basename(const char *shortname
);
396 if (!WCHECK_STATE(WSTATE_NORMAL
))
399 WCHANGE_STATE(WSTATE_MODAL
);
401 assert(entry
->clientdata
!= NULL
);
403 shortname
= basename(wapp
->app_icon
->wm_instance
);
405 buffer
= wstrconcat(wapp
->app_icon
? shortname
: NULL
,
406 _(" will be forcibly closed.\n"
407 "Any unsaved changes will be lost.\n" "Please confirm."));
409 fPtr
= wapp
->main_window_desc
->fake_group
;
411 wretain(wapp
->main_window_desc
);
412 if (wPreferences
.dont_confirm_kill
413 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
414 buffer
, _("Yes"), _("No"), NULL
) == WAPRDefault
) {
416 WWindow
*wwin
, *twin
;
418 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
421 if (wwin
->fake_group
== fPtr
)
425 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
426 wClientKill(wapp
->main_window_desc
);
429 wrelease(wapp
->main_window_desc
);
431 WCHANGE_STATE(WSTATE_NORMAL
);
434 static WMenu
*createApplicationMenu(WScreen
* scr
)
438 menu
= wMenuCreate(scr
, NULL
, False
);
439 wMenuAddCallback(menu
, _("Launch"), relaunchCallback
, NULL
);
440 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
441 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
442 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
443 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
448 static void openApplicationMenu(WApplication
* wapp
, int x
, int y
)
451 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
454 if (!scr
->icon_menu
) {
455 scr
->icon_menu
= createApplicationMenu(scr
);
456 wfree(scr
->icon_menu
->entries
[1]->text
);
459 menu
= scr
->icon_menu
;
461 if (wapp
->flags
.hidden
)
462 menu
->entries
[1]->text
= _("Unhide");
464 menu
->entries
[1]->text
= _("Hide");
466 menu
->flags
.realized
= 0;
469 x
-= menu
->frame
->core
->width
/ 2;
470 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
471 x
= scr
->scr_width
- menu
->frame
->core
->width
;
476 /* set client data */
477 for (i
= 0; i
< menu
->entry_no
; i
++)
478 menu
->entries
[i
]->clientdata
= wapp
;
480 wMenuMapAt(menu
, x
, y
, False
);
483 /******************************************************************/
485 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
)
487 wAppIconPaint(desc
->parent
);
490 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
)
492 WAppIcon
*aicon
= desc
->parent
;
494 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
497 assert(aicon
->icon
->owner
!= NULL
);
499 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
501 if (event
->xbutton
.state
& ControlMask
) {
502 relaunchApplication(wapp
);
506 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
507 /* go to the last workspace that the user worked on the app */
508 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
509 wWorkspaceChange(scr
, wapp
->last_workspace
);
511 wUnhideApplication(wapp
, event
->xbutton
.button
== Button2
, unhideHere
);
513 if (event
->xbutton
.state
& MOD_MASK
)
514 wHideOtherApplications(aicon
->icon
->owner
);
517 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
519 WAppIcon
*aicon
= desc
->parent
;
520 WIcon
*icon
= aicon
->icon
;
522 int x
= aicon
->x_pos
, y
= aicon
->y_pos
;
523 int dx
= event
->xbutton
.x
, dy
= event
->xbutton
.y
;
526 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
527 WScreen
*scr
= icon
->core
->screen_ptr
;
528 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
529 int shad_x
= 0, shad_y
= 0, docking
= 0, dockable
, collapsed
= 0;
531 int clickButton
= event
->xbutton
.button
;
534 Bool movingSingle
= False
;
537 Bool hasMoved
= False
;
539 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
542 if (IsDoubleClick(scr
, event
)) {
543 /* Middle or right mouse actions were handled on first click */
544 if (event
->xbutton
.button
== Button1
)
545 iconDblClick(desc
, event
);
549 if (event
->xbutton
.button
== Button2
) {
550 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
555 relaunchApplication(wapp
);
559 if (event
->xbutton
.button
== Button3
) {
560 WObjDescriptor
*desc
;
561 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
566 if (event
->xbutton
.send_event
&&
567 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
568 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
569 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
570 wwarning("pointer grab failed for appicon menu");
574 openApplicationMenu(wapp
, event
->xbutton
.x_root
, event
->xbutton
.y_root
);
576 /* allow drag select of menu */
577 desc
= &scr
->icon_menu
->menu
->descriptor
;
578 event
->xbutton
.send_event
= True
;
579 (*desc
->handle_mousedown
) (desc
, event
);
583 if (event
->xbutton
.state
& MOD_MASK
)
584 wLowerFrame(icon
->core
);
586 wRaiseFrame(icon
->core
);
588 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
589 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
590 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
)
591 wwarning("pointer grab failed for appicon move");
593 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
596 dockable
= canBeDocked(icon
->owner
);
598 wins
[0] = icon
->core
->window
;
599 wins
[1] = scr
->dock_shadow
;
600 XRestackWindows(dpy
, wins
, 2);
602 if (icon
->pixmap
!= None
)
603 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
605 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
606 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
, ghost
);
607 XClearWindow(dpy
, scr
->dock_shadow
);
611 WMMaskEvent(dpy
, PointerMotionMask
| ButtonReleaseMask
| ButtonPressMask
612 | ButtonMotionMask
| ExposureMask
| EnterWindowMask
, &ev
);
619 /* It means the cursor moved so fast that it entered
620 * something else (if moving slowly, it would have
621 * stayed in the appIcon that is being moved. Ignore
622 * such "spurious" EnterNotifiy's */
628 if (abs(dx
- ev
.xmotion
.x
) >= MOVE_THRESHOLD
629 || abs(dy
- ev
.xmotion
.y
) >= MOVE_THRESHOLD
) {
630 XChangeActivePointerGrab(dpy
, ButtonMotionMask
631 | ButtonReleaseMask
| ButtonPressMask
,
632 wCursor
[WCUR_MOVE
], CurrentTime
);
638 x
= ev
.xmotion
.x_root
- dx
;
639 y
= ev
.xmotion
.y_root
- dy
;
642 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
644 wAppIconMove(aicon
, x
, y
);
647 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
, &ix
, &iy
, False
)) {
648 shad_x
= scr
->dock
->x_pos
+ ix
* wPreferences
.icon_size
;
649 shad_y
= scr
->dock
->y_pos
+ iy
* wPreferences
.icon_size
;
651 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
652 scr
->last_dock
->collapsed
= 1;
653 wDockHideIcons(scr
->last_dock
);
656 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
657 scr
->dock
->collapsed
= 0;
658 wDockShowIcons(scr
->dock
);
661 if (scr
->dock
->auto_raise_lower
)
662 wDockRaise(scr
->dock
);
664 scr
->last_dock
= scr
->dock
;
666 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
668 XMapWindow(dpy
, scr
->dock_shadow
);
671 } else if (workspace
->clip
&&
672 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
, &ix
, &iy
, False
)) {
673 shad_x
= workspace
->clip
->x_pos
+ ix
* wPreferences
.icon_size
;
674 shad_y
= workspace
->clip
->y_pos
+ iy
* wPreferences
.icon_size
;
676 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
677 scr
->last_dock
->collapsed
= 1;
678 wDockHideIcons(scr
->last_dock
);
681 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
682 workspace
->clip
->collapsed
= 0;
683 wDockShowIcons(workspace
->clip
);
686 if (workspace
->clip
->auto_raise_lower
)
687 wDockRaise(workspace
->clip
);
689 scr
->last_dock
= workspace
->clip
;
691 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
693 XMapWindow(dpy
, scr
->dock_shadow
);
696 } else if (docking
) {
697 XUnmapWindow(dpy
, scr
->dock_shadow
);
707 if (ev
.xbutton
.button
!= clickButton
)
709 XUngrabPointer(dpy
, CurrentTime
);
714 /* icon is trying to be docked */
715 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
716 XUnmapWindow(dpy
, scr
->dock_shadow
);
717 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
718 if (scr
->last_dock
->auto_collapse
)
721 if (workspace
->clip
&&
722 workspace
->clip
!= scr
->last_dock
&& workspace
->clip
->auto_raise_lower
)
723 wDockLower(workspace
->clip
);
726 /* If icon could not be docked, slide it back to the old
728 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
732 /* move back to its place */
733 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
734 wAppIconMove(aicon
, oldX
, oldY
);
736 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
740 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
741 wDockLower(workspace
->clip
);
744 scr
->last_dock
->collapsed
= 1;
745 wDockHideIcons(scr
->last_dock
);
750 XFreePixmap(dpy
, ghost
);
751 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
754 if (wPreferences
.auto_arrange_icons
)
755 wArrangeIcons(scr
, True
);
757 if (wPreferences
.single_click
&& !hasMoved
)
758 iconDblClick(desc
, event
);