1 /* appicon.c- icon for applications (not mini-window)
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998 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"
51 #include <WINGs/WINGsP.h>
55 * icon_file for the dock is got from the preferences file by
56 * using the classname/instancename
59 /**** Global variables ****/
60 extern Cursor wCursor
[WCUR_LAST
];
61 extern WPreferences wPreferences
;
63 #define MOD_MASK wPreferences.modifier_mask
65 void appIconMouseDown(WObjDescriptor
*desc
, XEvent
*event
);
66 static void iconDblClick(WObjDescriptor
*desc
, XEvent
*event
);
67 static void iconExpose(WObjDescriptor
*desc
, XEvent
*event
);
72 wAppIconCreateForDock(WScreen
*scr
, char *command
, char *wm_instance
,
73 char *wm_class
, int tile
)
78 dicon
= wmalloc(sizeof(WAppIcon
));
80 memset(dicon
, 0, sizeof(WAppIcon
));
85 dicon
->next
= scr
->app_icon_list
;
86 if (scr
->app_icon_list
) {
87 scr
->app_icon_list
->prev
= dicon
;
89 scr
->app_icon_list
= dicon
;
92 dicon
->command
= wstrdup(command
);
95 dicon
->wm_class
= wstrdup(wm_class
);
97 dicon
->wm_instance
= wstrdup(wm_instance
);
99 path
= wDefaultGetIconFile(scr
, wm_instance
, wm_class
, True
);
100 if (!path
&& command
) {
101 wApplicationExtractDirPackIcon(scr
, command
, wm_instance
, wm_class
);
103 path
= wDefaultGetIconFile(scr
, wm_instance
, wm_class
, False
);
107 path
= FindImage(wPreferences
.icon_path
, path
);
109 dicon
->icon
= wIconCreateWithIconFile(scr
, path
, tile
);
113 wXDNDMakeAwareness(dicon
->icon
->core
->window
);
116 #ifdef DEMATERIALIZE_ICON
118 XSetWindowAttributes attribs
;
119 attribs
.save_under
= True
;
120 XChangeWindowAttributes(dpy
, dicon
->icon
->core
->window
,
121 CWSaveUnder
, &attribs
);
125 /* will be overriden by dock */
126 dicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
127 dicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
128 dicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
129 dicon
->icon
->core
->descriptor
.parent
= dicon
;
130 AddToStackList(dicon
->icon
->core
);
138 wAppIconCreate(WWindow
*leader_win
)
141 WScreen
*scr
= leader_win
->screen_ptr
;
143 aicon
= wmalloc(sizeof(WAppIcon
));
145 memset(aicon
, 0, sizeof(WAppIcon
));
151 aicon
->next
= scr
->app_icon_list
;
152 if (scr
->app_icon_list
) {
153 scr
->app_icon_list
->prev
= aicon
;
155 scr
->app_icon_list
= aicon
;
157 if (leader_win
->wm_class
)
158 aicon
->wm_class
= wstrdup(leader_win
->wm_class
);
159 if (leader_win
->wm_instance
)
160 aicon
->wm_instance
= wstrdup(leader_win
->wm_instance
);
162 aicon
->icon
= wIconCreate(leader_win
);
163 #ifdef DEMATERIALIZE_ICON
165 XSetWindowAttributes attribs
;
166 attribs
.save_under
= True
;
167 XChangeWindowAttributes(dpy
, aicon
->icon
->core
->window
,
168 CWSaveUnder
, &attribs
);
172 wXDNDMakeAwareness(aicon
->icon
->core
->window
);
175 /* will be overriden if docked */
176 aicon
->icon
->core
->descriptor
.handle_mousedown
= appIconMouseDown
;
177 aicon
->icon
->core
->descriptor
.handle_expose
= iconExpose
;
178 aicon
->icon
->core
->descriptor
.parent_type
= WCLASS_APPICON
;
179 aicon
->icon
->core
->descriptor
.parent
= aicon
;
180 AddToStackList(aicon
->icon
->core
);
181 aicon
->icon
->show_title
= 0;
182 wIconUpdate(aicon
->icon
);
189 wAppIconDestroy(WAppIcon
*aicon
)
191 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
193 RemoveFromStackList(aicon
->icon
->core
);
194 wIconDestroy(aicon
->icon
);
196 wfree(aicon
->command
);
198 if (aicon
->dnd_command
)
199 wfree(aicon
->dnd_command
);
201 if (aicon
->wm_instance
)
202 wfree(aicon
->wm_instance
);
204 wfree(aicon
->wm_class
);
206 if (aicon
== scr
->app_icon_list
) {
208 aicon
->next
->prev
= NULL
;
209 scr
->app_icon_list
= aicon
->next
;
213 aicon
->next
->prev
= aicon
->prev
;
215 aicon
->prev
->next
= aicon
->next
;
218 aicon
->destroyed
= 1;
226 drawCorner(WIcon
*icon
, WWindow
*wwin
, int active
)
228 WScreen
*scr
= wwin
->screen_ptr
;
239 gc
=scr
->focused_texture
->any
.gc
;
241 gc
=scr
->unfocused_texture
->any
.gc
;
243 XFillPolygon(dpy
, icon
->core
->window
, gc
, points
, 3,
244 Convex
, CoordModeOrigin
);
246 #endif /* NEWAPPICON */
250 drawCorner(WIcon
*icon
)
252 WScreen
*scr
= icon
->core
->screen_ptr
;
261 XFillPolygon(dpy
, icon
->core
->window
, scr
->icon_title_texture
->normal_gc
,
262 points
, 3, Convex
, CoordModeOrigin
);
263 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
,
265 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
,
267 /* drawing the second line gives a weird concave look. -Dan */
269 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
,
271 XDrawLine(dpy
, icon
->core
->window
, scr
->icon_title_texture
->light_gc
,
278 wAppIconMove(WAppIcon
*aicon
, int x
, int y
)
282 app
= wApplicationOf(aicon
->icon
->owner
->main_window
);
284 XMoveWindow(dpy
, aicon
->icon
->core
->window
, x
, y
);
292 updateDockNumbers(WScreen
*scr
)
297 XGCValues my_gc_values
;
298 unsigned long my_v_mask
= (GCForeground
);
299 WAppIcon
*dicon
= scr
->dock
->icon_array
[0];
301 my_gc_values
.foreground
= scr
->white_pixel
;
302 numbers_gc
= XCreateGC(dpy
, dicon
->icon
->core
->window
,
303 my_v_mask
, &my_gc_values
);
305 ws_numbers
= wmalloc(20);
306 snprintf(ws_numbers
, 20, "%i [ %i ]", scr
->current_workspace
+1,
307 ((scr
->current_workspace
/10)+1));
308 length
= strlen(ws_numbers
);
310 XClearArea(dpy
, dicon
->icon
->core
->window
, 2, 2, 50,
311 WMFontHeight(scr
->icon_title_font
)+1, False
);
313 XSetForeground(dpy
, numbers_gc
, scr
->black_pixel
);
314 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, numbers_gc
,
315 scr
->icon_title_font
, 4, 3, ws_numbers
, length
);
317 XSetForeground(dpy
, numbers_gc
, scr
->white_pixel
);
318 WMDrawString(scr
->wmscreen
, dicon
->icon
->core
->window
, numbers_gc
,
319 scr
->icon_title_font
, 3, 2, ws_numbers
, length
);
321 XFreeGC(dpy
, numbers_gc
);
324 #endif /* WS_INDICATOR */
328 wAppIconPaint(WAppIcon
*aicon
)
331 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
333 if (aicon
->icon
->owner
)
334 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
338 wIconPaint(aicon
->icon
);
342 if (aicon
->docked
&& scr
->dock
&& scr
->dock
==aicon
->dock
&&
344 updateDockNumbers(scr
);
346 if (scr
->dock_dots
&& aicon
->docked
&& !aicon
->running
347 && aicon
->command
!=NULL
) {
348 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
349 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
350 XCopyArea(dpy
, scr
->dock_dots
->image
, aicon
->icon
->core
->window
,
351 scr
->copy_gc
, 0, 0, scr
->dock_dots
->width
,
352 scr
->dock_dots
->height
, 0, 0);
356 if (wapp
&& wapp
->flags
.hidden
) {
357 XSetClipMask(dpy
, scr
->copy_gc
, scr
->dock_dots
->mask
);
358 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
359 XCopyArea(dpy
, scr
->dock_dots
->image
,
360 aicon
->icon
->core
->window
,
361 scr
->copy_gc
, 0, 0, 7,
362 scr
->dock_dots
->height
, 0, 0);
364 #endif /* HIDDENDOT */
366 if (aicon
->omnipresent
)
367 drawCorner(aicon
->icon
);
369 XSetClipMask(dpy
, scr
->copy_gc
, None
);
370 if (aicon
->launching
) {
371 XFillRectangle(dpy
, aicon
->icon
->core
->window
, scr
->stipple_gc
,
372 0, 0, wPreferences
.icon_size
, wPreferences
.icon_size
);
378 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
383 hideCallback(WMenu
*menu
, WMenuEntry
*entry
)
385 WApplication
*wapp
= (WApplication
*)entry
->clientdata
;
387 if (wapp
->flags
.hidden
) {
388 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
389 wUnhideApplication(wapp
, False
, False
);
391 wHideApplication(wapp
);
397 unhideHereCallback(WMenu
*menu
, WMenuEntry
*entry
)
399 WApplication
*wapp
= (WApplication
*)entry
->clientdata
;
401 wUnhideApplication(wapp
, False
, True
);
406 setIconCallback(WMenu
*menu
, WMenuEntry
*entry
)
408 WAppIcon
*icon
= ((WApplication
*)entry
->clientdata
)->app_icon
;
418 scr
= icon
->icon
->core
->screen_ptr
;
422 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
424 if (result
&& !icon
->destroyed
) {
429 if (!wIconChangeImageFile(icon
->icon
, file
)) {
430 wMessageDialog(scr
, _("Error"),
431 _("Could not open specified icon file"),
432 _("OK"), NULL
, NULL
);
434 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
446 killCallback(WMenu
*menu
, WMenuEntry
*entry
)
448 WApplication
*wapp
= (WApplication
*)entry
->clientdata
;
449 WFakeGroupLeader
*fPtr
;
452 if (!WCHECK_STATE(WSTATE_NORMAL
))
455 WCHANGE_STATE(WSTATE_MODAL
);
457 assert(entry
->clientdata
!=NULL
);
459 buffer
= wstrconcat(wapp
->app_icon
? wapp
->app_icon
->wm_class
: NULL
,
460 _(" will be forcibly closed.\n"
461 "Any unsaved changes will be lost.\n"
464 fPtr
= wapp
->main_window_desc
->fake_group
;
466 wretain(wapp
->main_window_desc
);
467 if (wPreferences
.dont_confirm_kill
468 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
469 buffer
, _("Yes"), _("No"), NULL
)==WAPRDefault
) {
471 WWindow
*wwin
, *twin
;
473 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
476 if (wwin
->fake_group
== fPtr
) {
481 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
482 wClientKill(wapp
->main_window_desc
);
485 wrelease(wapp
->main_window_desc
);
489 WCHANGE_STATE(WSTATE_NORMAL
);
494 createApplicationMenu(WScreen
*scr
)
498 menu
= wMenuCreate(scr
, NULL
, False
);
499 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
500 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
501 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
502 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
509 openApplicationMenu(WApplication
*wapp
, int x
, int y
)
512 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
515 if (!scr
->icon_menu
) {
516 scr
->icon_menu
= createApplicationMenu(scr
);
517 wfree(scr
->icon_menu
->entries
[1]->text
);
520 menu
= scr
->icon_menu
;
522 if (wapp
->flags
.hidden
) {
523 menu
->entries
[1]->text
= _("Unhide");
525 menu
->entries
[1]->text
= _("Hide");
528 menu
->flags
.realized
= 0;
531 x
-= menu
->frame
->core
->width
/2;
532 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
533 x
= scr
->scr_width
- menu
->frame
->core
->width
;
537 /* set client data */
538 for (i
= 0; i
< menu
->entry_no
; i
++) {
539 menu
->entries
[i
]->clientdata
= wapp
;
541 wMenuMapAt(menu
, x
, y
, False
);
545 /******************************************************************/
548 iconExpose(WObjDescriptor
*desc
, XEvent
*event
)
550 wAppIconPaint(desc
->parent
);
555 iconDblClick(WObjDescriptor
*desc
, XEvent
*event
)
557 WAppIcon
*aicon
= desc
->parent
;
559 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
562 assert(aicon
->icon
->owner
!=NULL
);
564 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
567 wwarning("could not find application descriptor for app icon!!");
572 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
574 /* go to the last workspace that the user worked on the app */
575 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
576 wWorkspaceChange(scr
, wapp
->last_workspace
);
578 wUnhideApplication(wapp
, event
->xbutton
.button
==Button2
, unhideHere
);
580 if (event
->xbutton
.state
& MOD_MASK
) {
581 wHideOtherApplications(aicon
->icon
->owner
);
588 appIconMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
590 WAppIcon
*aicon
= desc
->parent
;
591 WIcon
*icon
= aicon
->icon
;
593 int x
=aicon
->x_pos
, y
=aicon
->y_pos
;
594 int dx
=event
->xbutton
.x
, dy
=event
->xbutton
.y
;
597 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
598 WScreen
*scr
= icon
->core
->screen_ptr
;
599 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
600 int shad_x
= 0, shad_y
= 0, docking
=0, dockable
, collapsed
= 0;
602 int clickButton
= event
->xbutton
.button
;
605 Bool movingSingle
= False
;
609 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
612 if (IsDoubleClick(scr
, event
)) {
613 iconDblClick(desc
, event
);
617 if (event
->xbutton
.button
== Button3
) {
618 WObjDescriptor
*desc
;
619 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
624 if (event
->xbutton
.send_event
&&
625 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
626 |ButtonReleaseMask
|ButtonPressMask
, GrabModeAsync
,
627 GrabModeAsync
, None
, None
, CurrentTime
) !=GrabSuccess
) {
628 wwarning("pointer grab failed for appicon menu");
632 openApplicationMenu(wapp
, event
->xbutton
.x_root
,
633 event
->xbutton
.y_root
);
635 /* allow drag select of menu */
636 desc
= &scr
->icon_menu
->menu
->descriptor
;
637 event
->xbutton
.send_event
= True
;
638 (*desc
->handle_mousedown
)(desc
, event
);
645 if (event
->xbutton
.state
& MOD_MASK
)
646 wLowerFrame(icon
->core
);
648 wRaiseFrame(icon
->core
);
650 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
651 |ButtonReleaseMask
|ButtonPressMask
, GrabModeAsync
,
652 GrabModeAsync
, None
, None
, CurrentTime
) !=GrabSuccess
) {
653 wwarning("pointer grab failed for appicon move");
656 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
659 dockable
= canBeDocked(icon
->owner
);
661 wins
[0] = icon
->core
->window
;
662 wins
[1] = scr
->dock_shadow
;
663 XRestackWindows(dpy
, wins
, 2);
665 if (icon
->pixmap
!=None
)
666 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
668 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
669 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
,
671 XClearWindow(dpy
, scr
->dock_shadow
);
675 WMMaskEvent(dpy
, PointerMotionMask
|ButtonReleaseMask
|ButtonPressMask
676 |ButtonMotionMask
|ExposureMask
, &ev
);
684 if (abs(dx
-ev
.xmotion
.x
)>=MOVE_THRESHOLD
685 || abs(dy
-ev
.xmotion
.y
)>=MOVE_THRESHOLD
) {
686 XChangeActivePointerGrab(dpy
, ButtonMotionMask
687 |ButtonReleaseMask
|ButtonPressMask
,
688 wCursor
[WCUR_MOVE
], CurrentTime
);
694 x
= ev
.xmotion
.x_root
- dx
;
695 y
= ev
.xmotion
.y_root
- dy
;
698 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
700 wAppIconMove(aicon
, x
, y
);
704 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
,
706 shad_x
= scr
->dock
->x_pos
+ ix
*wPreferences
.icon_size
;
707 shad_y
= scr
->dock
->y_pos
+ iy
*wPreferences
.icon_size
;
709 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
710 scr
->last_dock
->collapsed
= 1;
711 wDockHideIcons(scr
->last_dock
);
714 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
715 scr
->dock
->collapsed
= 0;
716 wDockShowIcons(scr
->dock
);
719 if (scr
->dock
->auto_raise_lower
)
720 wDockRaise(scr
->dock
);
722 scr
->last_dock
= scr
->dock
;
724 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
726 XMapWindow(dpy
, scr
->dock_shadow
);
729 } else if (workspace
->clip
&&
730 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
,
732 shad_x
= workspace
->clip
->x_pos
+ ix
*wPreferences
.icon_size
;
733 shad_y
= workspace
->clip
->y_pos
+ iy
*wPreferences
.icon_size
;
735 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
736 scr
->last_dock
->collapsed
= 1;
737 wDockHideIcons(scr
->last_dock
);
740 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
741 workspace
->clip
->collapsed
= 0;
742 wDockShowIcons(workspace
->clip
);
745 if (workspace
->clip
->auto_raise_lower
)
746 wDockRaise(workspace
->clip
);
748 scr
->last_dock
= workspace
->clip
;
750 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
752 XMapWindow(dpy
, scr
->dock_shadow
);
755 } else if (docking
) {
756 XUnmapWindow(dpy
, scr
->dock_shadow
);
767 if (ev
.xbutton
.button
!= clickButton
)
769 XUngrabPointer(dpy
, CurrentTime
);
774 /* icon is trying to be docked */
775 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
776 XUnmapWindow(dpy
, scr
->dock_shadow
);
777 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
778 if (scr
->last_dock
->auto_collapse
) {
781 if (workspace
->clip
&&
782 workspace
->clip
!= scr
->last_dock
&&
783 workspace
->clip
->auto_raise_lower
)
784 wDockLower(workspace
->clip
);
787 /* If icon could not be docked, slide it back to the old
789 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
792 wSoundPlay(WSOUND_DOCK
);
795 /* move back to its place */
796 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
797 wAppIconMove(aicon
, oldX
, oldY
);
799 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
803 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
804 wDockLower(workspace
->clip
);
807 scr
->last_dock
->collapsed
= 1;
808 wDockHideIcons(scr
->last_dock
);
813 XFreePixmap(dpy
, ghost
);
814 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
817 if (wPreferences
.auto_arrange_icons
)
818 wArrangeIcons(scr
, True
);
825 puts("End icon move");