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"
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 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
286 static void hideCallback(WMenu
* menu
, WMenuEntry
* entry
)
288 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
290 if (wapp
->flags
.hidden
) {
291 wWorkspaceChange(menu
->menu
->screen_ptr
, wapp
->last_workspace
);
292 wUnhideApplication(wapp
, False
, False
);
294 wHideApplication(wapp
);
298 static void unhideHereCallback(WMenu
* menu
, WMenuEntry
* entry
)
300 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
302 wUnhideApplication(wapp
, False
, True
);
305 static void setIconCallback(WMenu
* menu
, WMenuEntry
* entry
)
307 WAppIcon
*icon
= ((WApplication
*) entry
->clientdata
)->app_icon
;
312 assert(icon
!= NULL
);
317 scr
= icon
->icon
->core
->screen_ptr
;
321 result
= wIconChooserDialog(scr
, &file
, icon
->wm_instance
, icon
->wm_class
);
323 if (result
&& !icon
->destroyed
) {
324 if (file
&& *file
== 0) {
328 if (!wIconChangeImageFile(icon
->icon
, file
)) {
329 wMessageDialog(scr
, _("Error"),
330 _("Could not open specified icon file"), _("OK"), NULL
, NULL
);
332 wDefaultChangeIcon(scr
, icon
->wm_instance
, icon
->wm_class
, file
);
342 static void killCallback(WMenu
* menu
, WMenuEntry
* entry
)
344 WApplication
*wapp
= (WApplication
*) entry
->clientdata
;
345 WFakeGroupLeader
*fPtr
;
348 char *basename(const char *shortname
);
350 if (!WCHECK_STATE(WSTATE_NORMAL
))
353 WCHANGE_STATE(WSTATE_MODAL
);
355 assert(entry
->clientdata
!= NULL
);
357 shortname
= basename(wapp
->app_icon
->wm_instance
);
359 buffer
= wstrconcat(wapp
->app_icon
? shortname
: NULL
,
360 _(" will be forcibly closed.\n"
361 "Any unsaved changes will be lost.\n" "Please confirm."));
363 fPtr
= wapp
->main_window_desc
->fake_group
;
365 wretain(wapp
->main_window_desc
);
366 if (wPreferences
.dont_confirm_kill
367 || wMessageDialog(menu
->frame
->screen_ptr
, _("Kill Application"),
368 buffer
, _("Yes"), _("No"), NULL
) == WAPRDefault
) {
370 WWindow
*wwin
, *twin
;
372 wwin
= wapp
->main_window_desc
->screen_ptr
->focused_window
;
375 if (wwin
->fake_group
== fPtr
) {
380 } else if (!wapp
->main_window_desc
->flags
.destroyed
) {
381 wClientKill(wapp
->main_window_desc
);
384 wrelease(wapp
->main_window_desc
);
388 WCHANGE_STATE(WSTATE_NORMAL
);
391 static WMenu
*createApplicationMenu(WScreen
* scr
)
395 menu
= wMenuCreate(scr
, NULL
, False
);
396 wMenuAddCallback(menu
, _("Unhide Here"), unhideHereCallback
, NULL
);
397 wMenuAddCallback(menu
, _("Hide"), hideCallback
, NULL
);
398 wMenuAddCallback(menu
, _("Set Icon..."), setIconCallback
, NULL
);
399 wMenuAddCallback(menu
, _("Kill"), killCallback
, NULL
);
404 static void openApplicationMenu(WApplication
* wapp
, int x
, int y
)
407 WScreen
*scr
= wapp
->main_window_desc
->screen_ptr
;
410 if (!scr
->icon_menu
) {
411 scr
->icon_menu
= createApplicationMenu(scr
);
412 wfree(scr
->icon_menu
->entries
[1]->text
);
415 menu
= scr
->icon_menu
;
417 if (wapp
->flags
.hidden
) {
418 menu
->entries
[1]->text
= _("Unhide");
420 menu
->entries
[1]->text
= _("Hide");
423 menu
->flags
.realized
= 0;
426 x
-= menu
->frame
->core
->width
/ 2;
427 if (x
+ menu
->frame
->core
->width
> scr
->scr_width
)
428 x
= scr
->scr_width
- menu
->frame
->core
->width
;
432 /* set client data */
433 for (i
= 0; i
< menu
->entry_no
; i
++) {
434 menu
->entries
[i
]->clientdata
= wapp
;
436 wMenuMapAt(menu
, x
, y
, False
);
439 /******************************************************************/
441 static void iconExpose(WObjDescriptor
* desc
, XEvent
* event
)
443 wAppIconPaint(desc
->parent
);
446 static void iconDblClick(WObjDescriptor
* desc
, XEvent
* event
)
448 WAppIcon
*aicon
= desc
->parent
;
450 WScreen
*scr
= aicon
->icon
->core
->screen_ptr
;
453 assert(aicon
->icon
->owner
!= NULL
);
455 wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
456 unhideHere
= (event
->xbutton
.state
& ShiftMask
);
458 /* go to the last workspace that the user worked on the app */
459 if (!unhideHere
&& wapp
->last_workspace
!= scr
->current_workspace
)
460 wWorkspaceChange(scr
, wapp
->last_workspace
);
462 wUnhideApplication(wapp
, event
->xbutton
.button
== Button2
, unhideHere
);
464 if (event
->xbutton
.state
& MOD_MASK
) {
465 wHideOtherApplications(aicon
->icon
->owner
);
469 void appIconMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
471 WAppIcon
*aicon
= desc
->parent
;
472 WIcon
*icon
= aicon
->icon
;
474 int x
= aicon
->x_pos
, y
= aicon
->y_pos
;
475 int dx
= event
->xbutton
.x
, dy
= event
->xbutton
.y
;
478 int superfluous
= wPreferences
.superfluous
; /* we catch it to avoid problems */
479 WScreen
*scr
= icon
->core
->screen_ptr
;
480 WWorkspace
*workspace
= scr
->workspaces
[scr
->current_workspace
];
481 int shad_x
= 0, shad_y
= 0, docking
= 0, dockable
, collapsed
= 0;
483 int clickButton
= event
->xbutton
.button
;
486 Bool movingSingle
= False
;
489 Bool hasMoved
= False
;
491 if (aicon
->editing
|| WCHECK_STATE(WSTATE_MODAL
))
494 if (IsDoubleClick(scr
, event
)) {
495 iconDblClick(desc
, event
);
499 if (event
->xbutton
.button
== Button3
) {
500 WObjDescriptor
*desc
;
501 WApplication
*wapp
= wApplicationOf(aicon
->icon
->owner
->main_window
);
506 if (event
->xbutton
.send_event
&&
507 XGrabPointer(dpy
, aicon
->icon
->core
->window
, True
, ButtonMotionMask
508 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
509 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
510 wwarning("pointer grab failed for appicon menu");
514 openApplicationMenu(wapp
, event
->xbutton
.x_root
, event
->xbutton
.y_root
);
516 /* allow drag select of menu */
517 desc
= &scr
->icon_menu
->menu
->descriptor
;
518 event
->xbutton
.send_event
= True
;
519 (*desc
->handle_mousedown
) (desc
, event
);
523 if (event
->xbutton
.state
& MOD_MASK
)
524 wLowerFrame(icon
->core
);
526 wRaiseFrame(icon
->core
);
528 if (XGrabPointer(dpy
, icon
->core
->window
, True
, ButtonMotionMask
529 | ButtonReleaseMask
| ButtonPressMask
, GrabModeAsync
,
530 GrabModeAsync
, None
, None
, CurrentTime
) != GrabSuccess
) {
531 wwarning("pointer grab failed for appicon move");
534 if (wPreferences
.flags
.nodock
&& wPreferences
.flags
.noclip
)
537 dockable
= canBeDocked(icon
->owner
);
539 wins
[0] = icon
->core
->window
;
540 wins
[1] = scr
->dock_shadow
;
541 XRestackWindows(dpy
, wins
, 2);
543 if (icon
->pixmap
!= None
)
544 ghost
= MakeGhostIcon(scr
, icon
->pixmap
);
546 ghost
= MakeGhostIcon(scr
, icon
->core
->window
);
547 XSetWindowBackgroundPixmap(dpy
, scr
->dock_shadow
, ghost
);
548 XClearWindow(dpy
, scr
->dock_shadow
);
552 WMMaskEvent(dpy
, PointerMotionMask
| ButtonReleaseMask
| ButtonPressMask
553 | ButtonMotionMask
| ExposureMask
| EnterWindowMask
, &ev
);
560 /* It means the cursor moved so fast that it entered
561 * something else (if moving slowly, it would have
562 * stayed in the appIcon that is being moved. Ignore
563 * such "spurious" EnterNotifiy's */
569 if (abs(dx
- ev
.xmotion
.x
) >= MOVE_THRESHOLD
570 || abs(dy
- ev
.xmotion
.y
) >= MOVE_THRESHOLD
) {
571 XChangeActivePointerGrab(dpy
, ButtonMotionMask
572 | ButtonReleaseMask
| ButtonPressMask
,
573 wCursor
[WCUR_MOVE
], CurrentTime
);
579 x
= ev
.xmotion
.x_root
- dx
;
580 y
= ev
.xmotion
.y_root
- dy
;
583 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
585 wAppIconMove(aicon
, x
, y
);
589 if (scr
->dock
&& wDockSnapIcon(scr
->dock
, aicon
, x
, y
, &ix
, &iy
, False
)) {
590 shad_x
= scr
->dock
->x_pos
+ ix
* wPreferences
.icon_size
;
591 shad_y
= scr
->dock
->y_pos
+ iy
* wPreferences
.icon_size
;
593 if (scr
->last_dock
!= scr
->dock
&& collapsed
) {
594 scr
->last_dock
->collapsed
= 1;
595 wDockHideIcons(scr
->last_dock
);
598 if (!collapsed
&& (collapsed
= scr
->dock
->collapsed
)) {
599 scr
->dock
->collapsed
= 0;
600 wDockShowIcons(scr
->dock
);
603 if (scr
->dock
->auto_raise_lower
)
604 wDockRaise(scr
->dock
);
606 scr
->last_dock
= scr
->dock
;
608 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
610 XMapWindow(dpy
, scr
->dock_shadow
);
613 } else if (workspace
->clip
&&
614 wDockSnapIcon(workspace
->clip
, aicon
, x
, y
, &ix
, &iy
, False
)) {
615 shad_x
= workspace
->clip
->x_pos
+ ix
* wPreferences
.icon_size
;
616 shad_y
= workspace
->clip
->y_pos
+ iy
* wPreferences
.icon_size
;
618 if (scr
->last_dock
!= workspace
->clip
&& collapsed
) {
619 scr
->last_dock
->collapsed
= 1;
620 wDockHideIcons(scr
->last_dock
);
623 if (!collapsed
&& (collapsed
= workspace
->clip
->collapsed
)) {
624 workspace
->clip
->collapsed
= 0;
625 wDockShowIcons(workspace
->clip
);
628 if (workspace
->clip
->auto_raise_lower
)
629 wDockRaise(workspace
->clip
);
631 scr
->last_dock
= workspace
->clip
;
633 XMoveWindow(dpy
, scr
->dock_shadow
, shad_x
, shad_y
);
635 XMapWindow(dpy
, scr
->dock_shadow
);
638 } else if (docking
) {
639 XUnmapWindow(dpy
, scr
->dock_shadow
);
650 if (ev
.xbutton
.button
!= clickButton
)
652 XUngrabPointer(dpy
, CurrentTime
);
657 /* icon is trying to be docked */
658 SlideWindow(icon
->core
->window
, x
, y
, shad_x
, shad_y
);
659 XUnmapWindow(dpy
, scr
->dock_shadow
);
660 docked
= wDockAttachIcon(scr
->last_dock
, aicon
, ix
, iy
);
661 if (scr
->last_dock
->auto_collapse
) {
664 if (workspace
->clip
&&
665 workspace
->clip
!= scr
->last_dock
&& workspace
->clip
->auto_raise_lower
)
666 wDockLower(workspace
->clip
);
669 /* If icon could not be docked, slide it back to the old
671 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
675 /* move back to its place */
676 SlideWindow(icon
->core
->window
, x
, y
, oldX
, oldY
);
677 wAppIconMove(aicon
, oldX
, oldY
);
679 XMoveWindow(dpy
, icon
->core
->window
, x
, y
);
683 if (workspace
->clip
&& workspace
->clip
->auto_raise_lower
)
684 wDockLower(workspace
->clip
);
687 scr
->last_dock
->collapsed
= 1;
688 wDockHideIcons(scr
->last_dock
);
693 XFreePixmap(dpy
, ghost
);
694 XSetWindowBackground(dpy
, scr
->dock_shadow
, scr
->white_pixel
);
697 if (wPreferences
.auto_arrange_icons
)
698 wArrangeIcons(scr
, True
);
700 if (wPreferences
.single_click
&& !hasMoved
)
701 iconDblClick(desc
, event
);