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;
193 static void drawCorner(WIcon
* icon
, WWindow
* wwin
, int active
)
195 WScreen
*scr
= wwin
->screen_ptr
;
206 gc
= scr
->focused_texture
->any
.gc
;
208 gc
= scr
->unfocused_texture
->any
.gc
;
210 XFillPolygon(dpy
, icon
->core
->window
, gc
, points
, 3, Convex
, CoordModeOrigin
);
212 #endif /* NEWAPPICON */
214 static void drawCorner(WIcon
* icon
)
216 WScreen
*scr
= icon
->core
->screen_ptr
;
225 XFillPolygon(dpy
, icon
->core
->window
, scr
->icon_title_texture
->normal_gc
,
226 points
, 3, Convex
, CoordModeOrigin
);
227 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 0, 12);
228 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 12, 0);
229 /* drawing the second line gives a weird concave look. -Dan */
231 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 1, 11);
232 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 11, 1);
236 void wAppIconMove(WAppIcon
* aicon
, int x
, int y
)
238 XMoveWindow(dpy
, aicon
->icon
->core
->window
, x
, y
);
244 static void updateDockNumbers(WScreen
* scr
)
248 WAppIcon
*dicon
= scr
->dock
->icon_array
[0];
250 ws_numbers
= wmalloc(20);
251 snprintf(ws_numbers
, 20, "%i [ %i ]", scr
->current_workspace
+ 1, ((scr
->current_workspace
/ 10) + 1));
252 length
= strlen(ws_numbers
);
254 XClearArea(dpy
, dicon
->icon
->core
->window
, 2, 2, 50, WMFontHeight(scr
->icon_title_font
) + 1, False
);
256 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->black
,
257 scr
->icon_title_font
, 4, 3, ws_numbers
, length
);
259 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->white
,
260 scr
->icon_title_font
, 3, 2, ws_numbers
, length
);
264 #endif /* WS_INDICATOR */
266 void wAppIconPaint(WAppIcon
* aicon
)
269 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
271 if (aicon
->icon
->owner
)
272 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
276 wIconPaint(aicon
->icon
);
279 if (aicon
->docked
&& scr
->dock
&& scr
->dock
== aicon
->dock
&& aicon
->yindex
== 0)
280 updateDockNumbers(scr
);
282 if (scr
->dock_dots
&& aicon
->docked
&& !aicon
->running
&& aicon
->command
!= NULL
) {
283 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
284 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
285 XCopyArea(dpy
, scr
->dock_dots
->image
, aicon
->icon
->core
->window
,
286 scr
->copy_gc
, 0, 0, scr
->dock_dots
->width
, scr
->dock_dots
->height
, 0, 0);
289 if (wapp
&& wapp
->flags
.hidden
) {
290 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
291 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
292 XCopyArea(dpy
, scr
->dock_dots
->image
,
293 aicon
->icon
->core
->window
, scr
->copy_gc
, 0, 0, 7, scr
->dock_dots
->height
, 0, 0);
295 #endif /* HIDDENDOT */
297 if (aicon
->omnipresent
)
298 drawCorner(aicon
->icon
);
300 XSetClipMask(dpy
, scr
->copy_gc
, None
);
301 if (aicon
->launching
) {
302 XFillRectangle(dpy
, aicon
->icon
->core
->window
, scr
->stipple_gc
,
303 0, 0, wPreferences
.icon_size
, wPreferences
.icon_size
);
307 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
309 static void hideCallback(WMenu
* menu
, WMenuEntry
* entry
)
311 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
313 if (wapp
->flags
.hidden
) {
314 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
315 wUnhideApplication(wapp
, False
, False
);
317 wHideApplication(wapp
);
321 static void unhideHereCallback(WMenu
* menu
, WMenuEntry
* entry
)
323 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
325 wUnhideApplication(wapp
, False
, True
);
328 static void setIconCallback(WMenu
* menu
, WMenuEntry
* entry
)
330 WAppIcon
*icon
= ((WApplication
*) entry
->clientdata
)->app_icon
;
335 assert(icon
!= NULL
);
340 scr
= icon
->icon
->core
->screen_ptr
;
344 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
346 if (result
&& !icon
->destroyed
) {
347 if (file
&& *file
== 0) {
351 if (!wIconChangeImageFile(icon
->icon
, file
)) {
352 wMessageDialog(scr
, _("Error"),
353 _("Could not open specified icon file"), _("OK"), NULL
, NULL
);
355 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
365 static void killCallback(WMenu
* menu
, WMenuEntry
* entry
)
367 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
368 WFakeGroupLeader
*fPtr
;
371 char *basename(const char *shortname
);
373 if (!WCHECK_STATE(WSTATE_NORMAL
))
376 WCHANGE_STATE(WSTATE_MODAL
);
378 assert(entry
->clientdata
!= NULL
);
380 shortname
= basename(wapp
->app_icon
->wm_instance
);
382 buffer
= wstrconcat(wapp
->app_icon
? shortname
: NULL
,
383 _(" will be forcibly closed.\n"
384 "Any unsaved changes will be lost.\n" "Please confirm."));
386 fPtr
= wapp
->main_window_desc
->fake_group
;
388 wretain(wapp
->main_window_desc
);
389 if (wPreferences
.dont_confirm_kill
390 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
391 buffer
, _("Yes"), _("No"), NULL
) == WAPRDefault
) {
393 WWindow
*wwin
, *twin
;
395 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
398 if (wwin
->fake_group
== fPtr
) {
403 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
404 wClientKill(wapp
->main_window_desc
);
407 wrelease(wapp
->main_window_desc
);
411 WCHANGE_STATE(WSTATE_NORMAL
);
414 static WMenu
*createApplicationMenu(WScreen
* scr
)
418 menu
= wMenuCreate(scr
, NULL
, False
);
419 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
420 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
421 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
422 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
427 static void openApplicationMenu(WApplication
* wapp
, int x
, int y
)
430 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
433 if (!scr
->icon_menu
) {
434 scr
->icon_menu
= createApplicationMenu(scr
);
435 wfree(scr
->icon_menu
->entries
[1]->text
);
438 menu
= scr
->icon_menu
;
440 if (wapp
->flags
.hidden
) {
441 menu
->entries
[1]->text
= _("Unhide");
443 menu
->entries
[1]->text
= _("Hide");
446 menu
->flags
.realized
= 0;
449 x
-= menu
->frame
->core
->width
/ 2;
450 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
451 x
= scr
->scr_width
- menu
->frame
->core
->width
;
455 /* set client data */
456 for (i
= 0; i
< menu
->entry_no
; i
++) {
457 menu
->entries
[i
]->clientdata
= wapp
;
459 wMenuMapAt(menu
, x
, y
, False
);
462 /******************************************************************/
464 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
)
466 wAppIconPaint(desc
->parent
);
469 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
)
471 WAppIcon
*aicon
= desc
->parent
;
473 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
476 assert(aicon
->icon
->owner
!= NULL
);
478 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
481 wwarning("could not find application descriptor for app icon!!");
486 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
488 /* go to the last workspace that the user worked on the app */
489 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
490 wWorkspaceChange(scr
, wapp
->last_workspace
);
492 wUnhideApplication(wapp
, event
->xbutton
.button
== Button2
, unhideHere
);
494 if (event
->xbutton
.state
& MOD_MASK
) {
495 wHideOtherApplications(aicon
->icon
->owner
);
499 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
501 WAppIcon
*aicon
= desc
->parent
;
502 WIcon
*icon
= aicon
->icon
;
504 int x
= aicon
->x_pos
, y
= aicon
->y_pos
;
505 int dx
= event
->xbutton
.x
, dy
= event
->xbutton
.y
;
508 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
509 WScreen
*scr
= icon
->core
->screen_ptr
;
510 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
511 int shad_x
= 0, shad_y
= 0, docking
= 0, dockable
, collapsed
= 0;
513 int clickButton
= event
->xbutton
.button
;
516 Bool movingSingle
= False
;
519 Bool hasMoved
= False
;
521 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
524 if (IsDoubleClick(scr
, event
)) {
525 iconDblClick(desc
, event
);
529 if (event
->xbutton
.button
== Button3
) {
530 WObjDescriptor
*desc
;
531 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
536 if (event
->xbutton
.send_event
&&
537 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
538 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
539 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
540 wwarning("pointer grab failed for appicon menu");
544 openApplicationMenu(wapp
, event
->xbutton
.x_root
, event
->xbutton
.y_root
);
546 /* allow drag select of menu */
547 desc
= &scr
->icon_menu
->menu
->descriptor
;
548 event
->xbutton
.send_event
= True
;
549 (*desc
->handle_mousedown
) (desc
, event
);
555 if (event
->xbutton
.state
& MOD_MASK
)
556 wLowerFrame(icon
->core
);
558 wRaiseFrame(icon
->core
);
560 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
561 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
562 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
563 wwarning("pointer grab failed for appicon move");
566 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
569 dockable
= canBeDocked(icon
->owner
);
571 wins
[0] = icon
->core
->window
;
572 wins
[1] = scr
->dock_shadow
;
573 XRestackWindows(dpy
, wins
, 2);
575 if (icon
->pixmap
!= None
)
576 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
578 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
579 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
, ghost
);
580 XClearWindow(dpy
, scr
->dock_shadow
);
584 WMMaskEvent(dpy
, PointerMotionMask
| ButtonReleaseMask
| ButtonPressMask
585 | ButtonMotionMask
| ExposureMask
| EnterWindowMask
, &ev
);
592 /* It means the cursor moved so fast that it entered
593 * something else (if moving slowly, it would have
594 * stayed in the appIcon that is being moved. Ignore
595 * such "spurious" EnterNotifiy's */
601 if (abs(dx
- ev
.xmotion
.x
) >= MOVE_THRESHOLD
602 || abs(dy
- ev
.xmotion
.y
) >= MOVE_THRESHOLD
) {
603 XChangeActivePointerGrab(dpy
, ButtonMotionMask
604 | ButtonReleaseMask
| ButtonPressMask
,
605 wCursor
[WCUR_MOVE
], CurrentTime
);
611 x
= ev
.xmotion
.x_root
- dx
;
612 y
= ev
.xmotion
.y_root
- dy
;
615 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
617 wAppIconMove(aicon
, x
, y
);
621 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
, &ix
, &iy
, False
)) {
622 shad_x
= scr
->dock
->x_pos
+ ix
* wPreferences
.icon_size
;
623 shad_y
= scr
->dock
->y_pos
+ iy
* wPreferences
.icon_size
;
625 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
626 scr
->last_dock
->collapsed
= 1;
627 wDockHideIcons(scr
->last_dock
);
630 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
631 scr
->dock
->collapsed
= 0;
632 wDockShowIcons(scr
->dock
);
635 if (scr
->dock
->auto_raise_lower
)
636 wDockRaise(scr
->dock
);
638 scr
->last_dock
= scr
->dock
;
640 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
642 XMapWindow(dpy
, scr
->dock_shadow
);
645 } else if (workspace
->clip
&&
646 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
, &ix
, &iy
, False
)) {
647 shad_x
= workspace
->clip
->x_pos
+ ix
* wPreferences
.icon_size
;
648 shad_y
= workspace
->clip
->y_pos
+ iy
* wPreferences
.icon_size
;
650 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
651 scr
->last_dock
->collapsed
= 1;
652 wDockHideIcons(scr
->last_dock
);
655 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
656 workspace
->clip
->collapsed
= 0;
657 wDockShowIcons(workspace
->clip
);
660 if (workspace
->clip
->auto_raise_lower
)
661 wDockRaise(workspace
->clip
);
663 scr
->last_dock
= workspace
->clip
;
665 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
667 XMapWindow(dpy
, scr
->dock_shadow
);
670 } else if (docking
) {
671 XUnmapWindow(dpy
, scr
->dock_shadow
);
682 if (ev
.xbutton
.button
!= clickButton
)
684 XUngrabPointer(dpy
, CurrentTime
);
689 /* icon is trying to be docked */
690 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
691 XUnmapWindow(dpy
, scr
->dock_shadow
);
692 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
693 if (scr
->last_dock
->auto_collapse
) {
696 if (workspace
->clip
&&
697 workspace
->clip
!= scr
->last_dock
&& workspace
->clip
->auto_raise_lower
)
698 wDockLower(workspace
->clip
);
701 /* If icon could not be docked, slide it back to the old
703 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
707 /* move back to its place */
708 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
709 wAppIconMove(aicon
, oldX
, oldY
);
711 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
715 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
716 wDockLower(workspace
->clip
);
719 scr
->last_dock
->collapsed
= 1;
720 wDockHideIcons(scr
->last_dock
);
725 XFreePixmap(dpy
, ghost
);
726 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
729 if (wPreferences
.auto_arrange_icons
)
730 wArrangeIcons(scr
, True
);
732 if (wPreferences
.single_click
&& !hasMoved
)
733 iconDblClick(desc
, event
);
740 puts("End icon move");