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 #ifdef DEMATERIALIZE_ICON
111 XSetWindowAttributes attribs
;
112 attribs
.save_under
= True
;
113 XChangeWindowAttributes(dpy
, dicon
->icon
->core
->window
, CWSaveUnder
, &attribs
);
117 /* will be overriden by dock */
118 dicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
119 dicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
120 dicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
121 dicon
->icon
->core
->descriptor
.parent
= dicon
;
122 AddToStackList(dicon
->icon
->core
);
127 WAppIcon
*wAppIconCreate(WWindow
* leader_win
)
130 WScreen
*scr
= leader_win
->screen_ptr
;
132 aicon
= wmalloc(sizeof(WAppIcon
));
134 memset(aicon
, 0, sizeof(WAppIcon
));
140 aicon
->next
= scr
->app_icon_list
;
141 if (scr
->app_icon_list
) {
142 scr
->app_icon_list
->prev
= aicon
;
144 scr
->app_icon_list
= aicon
;
146 if (leader_win
->wm_class
)
147 aicon
->wm_class
= wstrdup(leader_win
->wm_class
);
148 if (leader_win
->wm_instance
)
149 aicon
->wm_instance
= wstrdup(leader_win
->wm_instance
);
151 aicon
->icon
= wIconCreate(leader_win
);
152 #ifdef DEMATERIALIZE_ICON
154 XSetWindowAttributes attribs
;
155 attribs
.save_under
= True
;
156 XChangeWindowAttributes(dpy
, aicon
->icon
->core
->window
, CWSaveUnder
, &attribs
);
160 wXDNDMakeAwareness(aicon
->icon
->core
->window
);
163 /* will be overriden if docked */
164 aicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
165 aicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
166 aicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
167 aicon
->icon
->core
->descriptor
.parent
= aicon
;
168 AddToStackList(aicon
->icon
->core
);
169 aicon
->icon
->show_title
= 0;
170 wIconUpdate(aicon
->icon
);
175 void wAppIconDestroy(WAppIcon
* aicon
)
177 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
179 RemoveFromStackList(aicon
->icon
->core
);
180 wIconDestroy(aicon
->icon
);
182 wfree(aicon
->command
);
184 if (aicon
->dnd_command
)
185 wfree(aicon
->dnd_command
);
187 if (aicon
->wm_instance
)
188 wfree(aicon
->wm_instance
);
190 wfree(aicon
->wm_class
);
192 if (aicon
== scr
->app_icon_list
) {
194 aicon
->next
->prev
= NULL
;
195 scr
->app_icon_list
= aicon
->next
;
198 aicon
->next
->prev
= aicon
->prev
;
200 aicon
->prev
->next
= aicon
->next
;
203 aicon
->destroyed
= 1;
208 static void drawCorner(WIcon
* icon
, WWindow
* wwin
, int active
)
210 WScreen
*scr
= wwin
->screen_ptr
;
221 gc
= scr
->focused_texture
->any
.gc
;
223 gc
= scr
->unfocused_texture
->any
.gc
;
225 XFillPolygon(dpy
, icon
->core
->window
, gc
, points
, 3, Convex
, CoordModeOrigin
);
227 #endif /* NEWAPPICON */
229 static void drawCorner(WIcon
* icon
)
231 WScreen
*scr
= icon
->core
->screen_ptr
;
240 XFillPolygon(dpy
, icon
->core
->window
, scr
->icon_title_texture
->normal_gc
,
241 points
, 3, Convex
, CoordModeOrigin
);
242 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 0, 12);
243 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 0, 0, 12, 0);
244 /* drawing the second line gives a weird concave look. -Dan */
246 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 1, 11);
247 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
, 1, 1, 11, 1);
251 void wAppIconMove(WAppIcon
* aicon
, int x
, int y
)
253 XMoveWindow(dpy
, aicon
->icon
->core
->window
, x
, y
);
259 static void updateDockNumbers(WScreen
* scr
)
263 WAppIcon
*dicon
= scr
->dock
->icon_array
[0];
265 ws_numbers
= wmalloc(20);
266 snprintf(ws_numbers
, 20, "%i [ %i ]", scr
->current_workspace
+ 1, ((scr
->current_workspace
/ 10) + 1));
267 length
= strlen(ws_numbers
);
269 XClearArea(dpy
, dicon
->icon
->core
->window
, 2, 2, 50, WMFontHeight(scr
->icon_title_font
) + 1, False
);
271 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->black
,
272 scr
->icon_title_font
, 4, 3, ws_numbers
, length
);
274 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, scr
->white
,
275 scr
->icon_title_font
, 3, 2, ws_numbers
, length
);
279 #endif /* WS_INDICATOR */
281 void wAppIconPaint(WAppIcon
* aicon
)
284 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
286 if (aicon
->icon
->owner
)
287 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
291 wIconPaint(aicon
->icon
);
294 if (aicon
->docked
&& scr
->dock
&& scr
->dock
== aicon
->dock
&& aicon
->yindex
== 0)
295 updateDockNumbers(scr
);
297 if (scr
->dock_dots
&& aicon
->docked
&& !aicon
->running
&& aicon
->command
!= NULL
) {
298 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
299 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
300 XCopyArea(dpy
, scr
->dock_dots
->image
, aicon
->icon
->core
->window
,
301 scr
->copy_gc
, 0, 0, scr
->dock_dots
->width
, scr
->dock_dots
->height
, 0, 0);
304 if (wapp
&& wapp
->flags
.hidden
) {
305 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
306 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
307 XCopyArea(dpy
, scr
->dock_dots
->image
,
308 aicon
->icon
->core
->window
, scr
->copy_gc
, 0, 0, 7, scr
->dock_dots
->height
, 0, 0);
310 #endif /* HIDDENDOT */
312 if (aicon
->omnipresent
)
313 drawCorner(aicon
->icon
);
315 XSetClipMask(dpy
, scr
->copy_gc
, None
);
316 if (aicon
->launching
) {
317 XFillRectangle(dpy
, aicon
->icon
->core
->window
, scr
->stipple_gc
,
318 0, 0, wPreferences
.icon_size
, wPreferences
.icon_size
);
322 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
324 static void hideCallback(WMenu
* menu
, WMenuEntry
* entry
)
326 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
328 if (wapp
->flags
.hidden
) {
329 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
330 wUnhideApplication(wapp
, False
, False
);
332 wHideApplication(wapp
);
336 static void unhideHereCallback(WMenu
* menu
, WMenuEntry
* entry
)
338 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
340 wUnhideApplication(wapp
, False
, True
);
343 static void setIconCallback(WMenu
* menu
, WMenuEntry
* entry
)
345 WAppIcon
*icon
= ((WApplication
*) entry
->clientdata
)->app_icon
;
350 assert(icon
!= NULL
);
355 scr
= icon
->icon
->core
->screen_ptr
;
359 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
361 if (result
&& !icon
->destroyed
) {
362 if (file
&& *file
== 0) {
366 if (!wIconChangeImageFile(icon
->icon
, file
)) {
367 wMessageDialog(scr
, _("Error"),
368 _("Could not open specified icon file"), _("OK"), NULL
, NULL
);
370 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
380 static void killCallback(WMenu
* menu
, WMenuEntry
* entry
)
382 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
383 WFakeGroupLeader
*fPtr
;
386 char *basename(const char *shortname
);
388 if (!WCHECK_STATE(WSTATE_NORMAL
))
391 WCHANGE_STATE(WSTATE_MODAL
);
393 assert(entry
->clientdata
!= NULL
);
395 shortname
= basename(wapp
->app_icon
->wm_instance
);
397 buffer
= wstrconcat(wapp
->app_icon
? shortname
: NULL
,
398 _(" will be forcibly closed.\n"
399 "Any unsaved changes will be lost.\n" "Please confirm."));
401 fPtr
= wapp
->main_window_desc
->fake_group
;
403 wretain(wapp
->main_window_desc
);
404 if (wPreferences
.dont_confirm_kill
405 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
406 buffer
, _("Yes"), _("No"), NULL
) == WAPRDefault
) {
408 WWindow
*wwin
, *twin
;
410 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
413 if (wwin
->fake_group
== fPtr
) {
418 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
419 wClientKill(wapp
->main_window_desc
);
422 wrelease(wapp
->main_window_desc
);
426 WCHANGE_STATE(WSTATE_NORMAL
);
429 static WMenu
*createApplicationMenu(WScreen
* scr
)
433 menu
= wMenuCreate(scr
, NULL
, False
);
434 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
435 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
436 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
437 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
442 static void openApplicationMenu(WApplication
* wapp
, int x
, int y
)
445 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
448 if (!scr
->icon_menu
) {
449 scr
->icon_menu
= createApplicationMenu(scr
);
450 wfree(scr
->icon_menu
->entries
[1]->text
);
453 menu
= scr
->icon_menu
;
455 if (wapp
->flags
.hidden
) {
456 menu
->entries
[1]->text
= _("Unhide");
458 menu
->entries
[1]->text
= _("Hide");
461 menu
->flags
.realized
= 0;
464 x
-= menu
->frame
->core
->width
/ 2;
465 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
466 x
= scr
->scr_width
- menu
->frame
->core
->width
;
470 /* set client data */
471 for (i
= 0; i
< menu
->entry_no
; i
++) {
472 menu
->entries
[i
]->clientdata
= wapp
;
474 wMenuMapAt(menu
, x
, y
, False
);
477 /******************************************************************/
479 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
)
481 wAppIconPaint(desc
->parent
);
484 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
)
486 WAppIcon
*aicon
= desc
->parent
;
488 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
491 assert(aicon
->icon
->owner
!= NULL
);
493 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
496 wwarning("could not find application descriptor for app icon!!");
501 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
503 /* go to the last workspace that the user worked on the app */
504 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
505 wWorkspaceChange(scr
, wapp
->last_workspace
);
507 wUnhideApplication(wapp
, event
->xbutton
.button
== Button2
, unhideHere
);
509 if (event
->xbutton
.state
& MOD_MASK
) {
510 wHideOtherApplications(aicon
->icon
->owner
);
514 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
516 WAppIcon
*aicon
= desc
->parent
;
517 WIcon
*icon
= aicon
->icon
;
519 int x
= aicon
->x_pos
, y
= aicon
->y_pos
;
520 int dx
= event
->xbutton
.x
, dy
= event
->xbutton
.y
;
523 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
524 WScreen
*scr
= icon
->core
->screen_ptr
;
525 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
526 int shad_x
= 0, shad_y
= 0, docking
= 0, dockable
, collapsed
= 0;
528 int clickButton
= event
->xbutton
.button
;
531 Bool movingSingle
= False
;
534 Bool hasMoved
= False
;
536 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
539 if (IsDoubleClick(scr
, event
)) {
540 iconDblClick(desc
, event
);
544 if (event
->xbutton
.button
== Button3
) {
545 WObjDescriptor
*desc
;
546 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
551 if (event
->xbutton
.send_event
&&
552 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
553 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
554 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
555 wwarning("pointer grab failed for appicon menu");
559 openApplicationMenu(wapp
, event
->xbutton
.x_root
, event
->xbutton
.y_root
);
561 /* allow drag select of menu */
562 desc
= &scr
->icon_menu
->menu
->descriptor
;
563 event
->xbutton
.send_event
= True
;
564 (*desc
->handle_mousedown
) (desc
, event
);
570 if (event
->xbutton
.state
& MOD_MASK
)
571 wLowerFrame(icon
->core
);
573 wRaiseFrame(icon
->core
);
575 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
576 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
577 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
578 wwarning("pointer grab failed for appicon move");
581 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
584 dockable
= canBeDocked(icon
->owner
);
586 wins
[0] = icon
->core
->window
;
587 wins
[1] = scr
->dock_shadow
;
588 XRestackWindows(dpy
, wins
, 2);
590 if (icon
->pixmap
!= None
)
591 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
593 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
594 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
, ghost
);
595 XClearWindow(dpy
, scr
->dock_shadow
);
599 WMMaskEvent(dpy
, PointerMotionMask
| ButtonReleaseMask
| ButtonPressMask
600 | ButtonMotionMask
| ExposureMask
| EnterWindowMask
, &ev
);
607 /* It means the cursor moved so fast that it entered
608 * something else (if moving slowly, it would have
609 * stayed in the appIcon that is being moved. Ignore
610 * such "spurious" EnterNotifiy's */
616 if (abs(dx
- ev
.xmotion
.x
) >= MOVE_THRESHOLD
617 || abs(dy
- ev
.xmotion
.y
) >= MOVE_THRESHOLD
) {
618 XChangeActivePointerGrab(dpy
, ButtonMotionMask
619 | ButtonReleaseMask
| ButtonPressMask
,
620 wCursor
[WCUR_MOVE
], CurrentTime
);
626 x
= ev
.xmotion
.x_root
- dx
;
627 y
= ev
.xmotion
.y_root
- dy
;
630 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
632 wAppIconMove(aicon
, x
, y
);
636 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
, &ix
, &iy
, False
)) {
637 shad_x
= scr
->dock
->x_pos
+ ix
* wPreferences
.icon_size
;
638 shad_y
= scr
->dock
->y_pos
+ iy
* wPreferences
.icon_size
;
640 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
641 scr
->last_dock
->collapsed
= 1;
642 wDockHideIcons(scr
->last_dock
);
645 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
646 scr
->dock
->collapsed
= 0;
647 wDockShowIcons(scr
->dock
);
650 if (scr
->dock
->auto_raise_lower
)
651 wDockRaise(scr
->dock
);
653 scr
->last_dock
= scr
->dock
;
655 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
657 XMapWindow(dpy
, scr
->dock_shadow
);
660 } else if (workspace
->clip
&&
661 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
, &ix
, &iy
, False
)) {
662 shad_x
= workspace
->clip
->x_pos
+ ix
* wPreferences
.icon_size
;
663 shad_y
= workspace
->clip
->y_pos
+ iy
* wPreferences
.icon_size
;
665 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
666 scr
->last_dock
->collapsed
= 1;
667 wDockHideIcons(scr
->last_dock
);
670 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
671 workspace
->clip
->collapsed
= 0;
672 wDockShowIcons(workspace
->clip
);
675 if (workspace
->clip
->auto_raise_lower
)
676 wDockRaise(workspace
->clip
);
678 scr
->last_dock
= workspace
->clip
;
680 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
682 XMapWindow(dpy
, scr
->dock_shadow
);
685 } else if (docking
) {
686 XUnmapWindow(dpy
, scr
->dock_shadow
);
697 if (ev
.xbutton
.button
!= clickButton
)
699 XUngrabPointer(dpy
, CurrentTime
);
704 /* icon is trying to be docked */
705 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
706 XUnmapWindow(dpy
, scr
->dock_shadow
);
707 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
708 if (scr
->last_dock
->auto_collapse
) {
711 if (workspace
->clip
&&
712 workspace
->clip
!= scr
->last_dock
&& workspace
->clip
->auto_raise_lower
)
713 wDockLower(workspace
->clip
);
716 /* If icon could not be docked, slide it back to the old
718 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
722 /* move back to its place */
723 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
724 wAppIconMove(aicon
, oldX
, oldY
);
726 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
730 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
731 wDockLower(workspace
->clip
);
734 scr
->last_dock
->collapsed
= 1;
735 wDockHideIcons(scr
->last_dock
);
740 XFreePixmap(dpy
, ghost
);
741 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
744 if (wPreferences
.auto_arrange_icons
)
745 wArrangeIcons(scr
, True
);
747 if (wPreferences
.single_click
&& !hasMoved
)
748 iconDblClick(desc
, event
);
755 puts("End icon move");