wDockAttachIcon paint argument
[wmaker-crm.git] / src / appicon.c
blob8a6841d975c7dfe20795992ecb7ea4510604f12f
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.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <errno.h>
33 #include "WindowMaker.h"
34 #include "window.h"
35 #include "icon.h"
36 #include "application.h"
37 #include "appicon.h"
38 #include "actions.h"
39 #include "stacking.h"
40 #include "dock.h"
41 #include "main.h"
42 #include "defaults.h"
43 #include "workspace.h"
44 #include "superfluous.h"
45 #include "menu.h"
46 #include "framewin.h"
47 #include "dialog.h"
48 #include "xinerama.h"
49 #include "client.h"
50 #ifdef XDND
51 #include "xdnd.h"
52 #endif
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;
62 extern WDDomain *WDWindowAttributes;
63 extern XContext wWinContext;
65 #define MOD_MASK wPreferences.modifier_mask
67 void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
68 static void iconDblClick(WObjDescriptor * desc, XEvent * event);
69 static void iconExpose(WObjDescriptor * desc, XEvent * event);
70 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class);
71 static WAppIcon *wAppIconCreate(WWindow * leader_win);
72 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon);
73 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon);
75 /* This function is used if the application is a .app. It checks if it has an icon in it
76 * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
78 void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
80 char *iconPath = NULL;
81 char *tmp = NULL;
83 if (strstr(path, ".app")) {
84 tmp = wmalloc(strlen(path) + 16);
86 if (scr->flags.supports_tiff) {
87 strcpy(tmp, path);
88 strcat(tmp, ".tiff");
89 if (access(tmp, R_OK) == 0)
90 iconPath = tmp;
93 if (!iconPath) {
94 strcpy(tmp, path);
95 strcat(tmp, ".xpm");
96 if (access(tmp, R_OK) == 0)
97 iconPath = tmp;
100 if (!iconPath)
101 wfree(tmp);
103 if (iconPath) {
104 wApplicationSaveIconPathFor(iconPath, wm_instance, wm_class);
105 wfree(iconPath);
110 WAppIcon *wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance, char *wm_class, int tile)
112 WAppIcon *aicon;
114 aicon = wmalloc(sizeof(WAppIcon));
115 wretain(aicon);
116 aicon->yindex = -1;
117 aicon->xindex = -1;
119 add_to_appicon_list(scr, aicon);
121 if (command)
122 aicon->command = wstrdup(command);
124 if (wm_class)
125 aicon->wm_class = wstrdup(wm_class);
127 if (wm_instance)
128 aicon->wm_instance = wstrdup(wm_instance);
130 aicon->icon = icon_create_for_dock(scr, command, wm_instance, wm_class, tile);
132 #ifdef XDND
133 wXDNDMakeAwareness(aicon->icon->core->window);
134 #endif
136 /* will be overriden by dock */
137 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
138 aicon->icon->core->descriptor.handle_expose = iconExpose;
139 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
140 aicon->icon->core->descriptor.parent = aicon;
141 AddToStackList(aicon->icon->core);
143 return aicon;
146 void makeAppIconFor(WApplication *wapp)
148 /* If app_icon, work is done, return */
149 if (wapp->app_icon)
150 return;
152 /* Create the icon */
153 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
154 wIconUpdate(wapp->app_icon->icon);
156 /* Now, paint the icon */
157 if (!WFLAGP(wapp->main_window_desc, no_appicon))
158 paint_app_icon(wapp);
161 void unpaint_app_icon(WApplication *wapp)
163 WAppIcon *aicon;
164 WScreen *scr;
165 WDock *clip;
167 if (!wapp || !wapp->app_icon)
168 return;
170 aicon = wapp->app_icon;
172 /* If the icon is docked, don't continue */
173 if (aicon->docked)
174 return;
176 scr = wapp->main_window_desc->screen_ptr;
177 clip = scr->workspaces[scr->current_workspace]->clip;
179 if (!clip || !aicon->attracted || !clip->collapsed)
180 XUnmapWindow(dpy, aicon->icon->core->window);
182 /* We want to avoid having it on the list because otherwise
183 * there will be a hole when the icons are arranged with
184 * wArrangeIcons() */
185 remove_from_appicon_list(scr, aicon);
187 if (wPreferences.auto_arrange_icons && !aicon->attracted)
188 wArrangeIcons(scr, True);
191 void paint_app_icon(WApplication *wapp)
193 WIcon *icon;
194 WScreen *scr = wapp->main_window_desc->screen_ptr;
195 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
196 int x = 0, y = 0;
197 Bool update_icon = False;
199 if (!wapp || !wapp->app_icon)
200 return;
202 icon = wapp->app_icon->icon;
203 wapp->app_icon->main_window = wapp->main_window;
205 /* If the icon is docked, don't continue */
206 if (wapp->app_icon->docked)
207 return;
209 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
210 wapp->app_icon->attracted = 1;
211 if (!icon->shadowed) {
212 icon->shadowed = 1;
213 update_icon = True;
215 wDockAttachIcon(clip, wapp->app_icon, x, y, update_icon);
216 } else {
217 /* We must know if the icon is painted in the screen,
218 * because if painted, then PlaceIcon will return the next
219 * space on the screen, and the icon will move */
220 if (wapp->app_icon->next == NULL && wapp->app_icon->prev == NULL) {
221 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
222 wAppIconMove(wapp->app_icon, x, y);
223 wLowerFrame(icon->core);
227 /* If we want appicon (no_appicon is not set) and the icon is not
228 * in the appicon_list, we must add it. Else, we want to avoid
229 * having it on the list */
230 if (!WFLAGP(wapp->main_window_desc, no_appicon) &&
231 wapp->app_icon->next == NULL && wapp->app_icon->prev == NULL)
232 add_to_appicon_list(scr, wapp->app_icon);
234 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
235 XMapWindow(dpy, icon->core->window);
237 if (wPreferences.auto_arrange_icons && !wapp->app_icon->attracted)
238 wArrangeIcons(scr, True);
241 void removeAppIconFor(WApplication * wapp)
243 if (!wapp->app_icon)
244 return;
246 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
247 wapp->app_icon->running = 0;
248 /* since we keep it, we don't care if it was attracted or not */
249 wapp->app_icon->attracted = 0;
250 wapp->app_icon->icon->shadowed = 0;
251 wapp->app_icon->main_window = None;
252 wapp->app_icon->pid = 0;
253 wapp->app_icon->icon->owner = NULL;
254 wapp->app_icon->icon->icon_win = None;
255 wapp->app_icon->icon->force_paint = 1;
256 wAppIconPaint(wapp->app_icon);
257 } else if (wapp->app_icon->docked) {
258 wapp->app_icon->running = 0;
259 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
260 } else {
261 wAppIconDestroy(wapp->app_icon);
264 wapp->app_icon = NULL;
266 if (wPreferences.auto_arrange_icons)
267 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
270 static WAppIcon *wAppIconCreate(WWindow *leader_win)
272 WAppIcon *aicon;
274 aicon = wmalloc(sizeof(WAppIcon));
275 wretain(aicon);
276 aicon->yindex = -1;
277 aicon->xindex = -1;
278 aicon->prev = NULL;
279 aicon->next = NULL;
281 if (leader_win->wm_class)
282 aicon->wm_class = wstrdup(leader_win->wm_class);
284 if (leader_win->wm_instance)
285 aicon->wm_instance = wstrdup(leader_win->wm_instance);
287 aicon->icon = icon_create_for_wwindow(leader_win);
288 #ifdef XDND
289 wXDNDMakeAwareness(aicon->icon->core->window);
290 #endif
292 /* will be overriden if docked */
293 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
294 aicon->icon->core->descriptor.handle_expose = iconExpose;
295 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
296 aicon->icon->core->descriptor.parent = aicon;
297 AddToStackList(aicon->icon->core);
298 aicon->icon->show_title = 0;
300 return aicon;
303 void wAppIconDestroy(WAppIcon * aicon)
305 WScreen *scr = aicon->icon->core->screen_ptr;
307 RemoveFromStackList(aicon->icon->core);
308 wIconDestroy(aicon->icon);
309 if (aicon->command)
310 wfree(aicon->command);
311 #ifdef XDND
312 if (aicon->dnd_command)
313 wfree(aicon->dnd_command);
314 #endif
315 if (aicon->wm_instance)
316 wfree(aicon->wm_instance);
318 if (aicon->wm_class)
319 wfree(aicon->wm_class);
321 remove_from_appicon_list(scr, aicon);
323 aicon->destroyed = 1;
324 wrelease(aicon);
327 static void drawCorner(WIcon * icon)
329 WScreen *scr = icon->core->screen_ptr;
330 XPoint points[3];
332 points[0].x = 1;
333 points[0].y = 1;
334 points[1].x = 12;
335 points[1].y = 1;
336 points[2].x = 1;
337 points[2].y = 12;
338 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
339 points, 3, Convex, CoordModeOrigin);
340 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
341 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
344 void wAppIconMove(WAppIcon * aicon, int x, int y)
346 XMoveWindow(dpy, aicon->icon->core->window, x, y);
347 aicon->x_pos = x;
348 aicon->y_pos = y;
351 #ifdef WS_INDICATOR
352 static void updateDockNumbers(WScreen * scr)
354 int length;
355 char *ws_numbers;
356 WAppIcon *dicon = scr->dock->icon_array[0];
358 ws_numbers = wmalloc(20);
359 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
360 length = strlen(ws_numbers);
362 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
364 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
365 scr->icon_title_font, 4, 3, ws_numbers, length);
367 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
368 scr->icon_title_font, 3, 2, ws_numbers, length);
370 wfree(ws_numbers);
372 #endif /* WS_INDICATOR */
374 void wAppIconPaint(WAppIcon * aicon)
376 WApplication *wapp;
377 WScreen *scr = aicon->icon->core->screen_ptr;
379 if (aicon->icon->owner)
380 wapp = wApplicationOf(aicon->icon->owner->main_window);
381 else
382 wapp = NULL;
384 wIconPaint(aicon->icon);
386 # ifdef WS_INDICATOR
387 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
388 updateDockNumbers(scr);
389 # endif
390 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
391 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
392 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
393 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
394 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
396 #ifdef HIDDENDOT
397 if (wapp && wapp->flags.hidden) {
398 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
399 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
400 XCopyArea(dpy, scr->dock_dots->image,
401 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
403 #endif /* HIDDENDOT */
405 if (aicon->omnipresent)
406 drawCorner(aicon->icon);
408 XSetClipMask(dpy, scr->copy_gc, None);
409 if (aicon->launching)
410 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
411 0, 0, wPreferences.icon_size, wPreferences.icon_size);
414 /* Save the application icon, if it's a dockapp then use it with dock = True */
415 void save_appicon(WAppIcon *aicon, Bool dock)
417 char *path;
419 if (!aicon)
420 return;
422 if (dock && (!aicon->docked || aicon->attracted))
423 return;
425 path = wIconStore(aicon->icon);
426 if (!path)
427 return;
429 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
430 wfree(path);
433 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
435 /* main_window may not have the full command line; try to find one which does */
436 static void relaunchApplication(WApplication *wapp)
438 WScreen *scr;
439 WWindow *wlist, *next;
441 scr = wapp->main_window_desc->screen_ptr;
442 wlist = scr->focused_window;
443 if (! wlist)
444 return;
446 while (wlist->prev)
447 wlist = wlist->prev;
449 while (wlist) {
450 next = wlist->next;
452 if (wlist->main_window == wapp->main_window) {
453 if (RelaunchWindow(wlist))
454 return;
457 wlist = next;
461 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
463 WApplication *wapp = (WApplication *) entry->clientdata;
465 relaunchApplication(wapp);
468 static void hideCallback(WMenu * menu, WMenuEntry * entry)
470 WApplication *wapp = (WApplication *) entry->clientdata;
472 if (wapp->flags.hidden) {
473 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
474 wUnhideApplication(wapp, False, False);
475 } else {
476 wHideApplication(wapp);
480 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
482 WApplication *wapp = (WApplication *) entry->clientdata;
484 wUnhideApplication(wapp, False, True);
487 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
489 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
490 char *file = NULL;
491 WScreen *scr;
492 int result;
494 assert(icon != NULL);
496 if (icon->editing)
497 return;
499 icon->editing = 1;
500 scr = icon->icon->core->screen_ptr;
502 wretain(icon);
504 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
506 if (result && !icon->destroyed) {
507 if (file && *file == 0) {
508 wfree(file);
509 file = NULL;
511 if (!wIconChangeImageFile(icon->icon, file)) {
512 wMessageDialog(scr, _("Error"),
513 _("Could not open specified icon file"), _("OK"), NULL, NULL);
514 } else {
515 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
516 wAppIconPaint(icon);
518 if (file)
519 wfree(file);
521 icon->editing = 0;
522 wrelease(icon);
525 static void killCallback(WMenu * menu, WMenuEntry * entry)
527 WApplication *wapp = (WApplication *) entry->clientdata;
528 WFakeGroupLeader *fPtr;
529 char *buffer;
530 char *shortname;
531 char *basename(const char *shortname);
533 if (!WCHECK_STATE(WSTATE_NORMAL))
534 return;
536 WCHANGE_STATE(WSTATE_MODAL);
538 assert(entry->clientdata != NULL);
540 shortname = basename(wapp->app_icon->wm_instance);
542 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
543 _(" will be forcibly closed.\n"
544 "Any unsaved changes will be lost.\n" "Please confirm."));
546 fPtr = wapp->main_window_desc->fake_group;
548 wretain(wapp->main_window_desc);
549 if (wPreferences.dont_confirm_kill
550 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
551 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
552 if (fPtr != NULL) {
553 WWindow *wwin, *twin;
555 wwin = wapp->main_window_desc->screen_ptr->focused_window;
556 while (wwin) {
557 twin = wwin->prev;
558 if (wwin->fake_group == fPtr)
559 wClientKill(wwin);
560 wwin = twin;
562 } else if (!wapp->main_window_desc->flags.destroyed) {
563 wClientKill(wapp->main_window_desc);
566 wrelease(wapp->main_window_desc);
567 wfree(buffer);
568 WCHANGE_STATE(WSTATE_NORMAL);
571 static WMenu *createApplicationMenu(WScreen * scr)
573 WMenu *menu;
575 menu = wMenuCreate(scr, NULL, False);
576 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
577 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
578 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
579 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
580 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
582 return menu;
585 static void openApplicationMenu(WApplication * wapp, int x, int y)
587 WMenu *menu;
588 WScreen *scr = wapp->main_window_desc->screen_ptr;
589 int i;
591 if (!scr->icon_menu) {
592 scr->icon_menu = createApplicationMenu(scr);
593 wfree(scr->icon_menu->entries[1]->text);
596 menu = scr->icon_menu;
598 if (wapp->flags.hidden)
599 menu->entries[1]->text = _("Unhide");
600 else
601 menu->entries[1]->text = _("Hide");
603 menu->flags.realized = 0;
604 wMenuRealize(menu);
606 x -= menu->frame->core->width / 2;
607 if (x + menu->frame->core->width > scr->scr_width)
608 x = scr->scr_width - menu->frame->core->width;
610 if (x < 0)
611 x = 0;
613 /* set client data */
614 for (i = 0; i < menu->entry_no; i++)
615 menu->entries[i]->clientdata = wapp;
617 wMenuMapAt(menu, x, y, False);
620 /******************************************************************/
622 static void iconExpose(WObjDescriptor * desc, XEvent * event)
624 wAppIconPaint(desc->parent);
627 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
629 WAppIcon *aicon = desc->parent;
630 WApplication *wapp;
631 WScreen *scr = aicon->icon->core->screen_ptr;
632 int unhideHere;
634 assert(aicon->icon->owner != NULL);
636 wapp = wApplicationOf(aicon->icon->owner->main_window);
638 if (event->xbutton.state & ControlMask) {
639 relaunchApplication(wapp);
640 return;
643 unhideHere = (event->xbutton.state & ShiftMask);
644 /* go to the last workspace that the user worked on the app */
645 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
646 wWorkspaceChange(scr, wapp->last_workspace);
648 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
650 if (event->xbutton.state & MOD_MASK)
651 wHideOtherApplications(aicon->icon->owner);
654 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
656 WAppIcon *aicon = desc->parent;
657 WIcon *icon = aicon->icon;
658 XEvent ev;
659 int x = aicon->x_pos, y = aicon->y_pos;
660 int dx = event->xbutton.x, dy = event->xbutton.y;
661 int grabbed = 0;
662 int done = 0;
663 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
664 WScreen *scr = icon->core->screen_ptr;
665 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
666 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
667 int ix, iy;
668 int clickButton = event->xbutton.button;
669 Pixmap ghost = None;
670 Window wins[2];
671 Bool movingSingle = False;
672 int oldX = x;
673 int oldY = y;
674 Bool hasMoved = False;
676 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
677 return;
679 if (IsDoubleClick(scr, event)) {
680 /* Middle or right mouse actions were handled on first click */
681 if (event->xbutton.button == Button1)
682 iconDblClick(desc, event);
683 return;
686 if (event->xbutton.button == Button2) {
687 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
689 if (wapp)
690 relaunchApplication(wapp);
692 return;
695 if (event->xbutton.button == Button3) {
696 WObjDescriptor *desc;
697 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
699 if (!wapp)
700 return;
702 if (event->xbutton.send_event &&
703 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
704 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
705 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
706 wwarning("pointer grab failed for appicon menu");
707 return;
710 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
712 /* allow drag select of menu */
713 desc = &scr->icon_menu->menu->descriptor;
714 event->xbutton.send_event = True;
715 (*desc->handle_mousedown) (desc, event);
716 return;
719 if (event->xbutton.state & MOD_MASK)
720 wLowerFrame(icon->core);
721 else
722 wRaiseFrame(icon->core);
724 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
725 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
726 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
727 wwarning("pointer grab failed for appicon move");
729 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
730 dockable = 0;
731 else
732 dockable = canBeDocked(icon->owner);
734 wins[0] = icon->core->window;
735 wins[1] = scr->dock_shadow;
736 XRestackWindows(dpy, wins, 2);
737 if (superfluous) {
738 if (icon->pixmap != None)
739 ghost = MakeGhostIcon(scr, icon->pixmap);
740 else
741 ghost = MakeGhostIcon(scr, icon->core->window);
742 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
743 XClearWindow(dpy, scr->dock_shadow);
746 while (!done) {
747 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
748 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
749 switch (ev.type) {
750 case Expose:
751 WMHandleEvent(&ev);
752 break;
754 case EnterNotify:
755 /* It means the cursor moved so fast that it entered
756 * something else (if moving slowly, it would have
757 * stayed in the appIcon that is being moved. Ignore
758 * such "spurious" EnterNotifiy's */
759 break;
761 case MotionNotify:
762 hasMoved = True;
763 if (!grabbed) {
764 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
765 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
766 XChangeActivePointerGrab(dpy, ButtonMotionMask
767 | ButtonReleaseMask | ButtonPressMask,
768 wCursor[WCUR_MOVE], CurrentTime);
769 grabbed = 1;
770 } else {
771 break;
774 x = ev.xmotion.x_root - dx;
775 y = ev.xmotion.y_root - dy;
777 if (movingSingle)
778 XMoveWindow(dpy, icon->core->window, x, y);
779 else
780 wAppIconMove(aicon, x, y);
782 if (dockable) {
783 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
784 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
785 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
787 if (scr->last_dock != scr->dock && collapsed) {
788 scr->last_dock->collapsed = 1;
789 wDockHideIcons(scr->last_dock);
790 collapsed = 0;
792 if (!collapsed && (collapsed = scr->dock->collapsed)) {
793 scr->dock->collapsed = 0;
794 wDockShowIcons(scr->dock);
797 if (scr->dock->auto_raise_lower)
798 wDockRaise(scr->dock);
800 scr->last_dock = scr->dock;
802 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
803 if (!docking)
804 XMapWindow(dpy, scr->dock_shadow);
806 docking = 1;
807 } else if (workspace->clip &&
808 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
809 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
810 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
812 if (scr->last_dock != workspace->clip && collapsed) {
813 scr->last_dock->collapsed = 1;
814 wDockHideIcons(scr->last_dock);
815 collapsed = 0;
817 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
818 workspace->clip->collapsed = 0;
819 wDockShowIcons(workspace->clip);
822 if (workspace->clip->auto_raise_lower)
823 wDockRaise(workspace->clip);
825 scr->last_dock = workspace->clip;
827 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
828 if (!docking)
829 XMapWindow(dpy, scr->dock_shadow);
831 docking = 1;
832 } else if (docking) {
833 XUnmapWindow(dpy, scr->dock_shadow);
834 docking = 0;
837 break;
839 case ButtonPress:
840 break;
842 case ButtonRelease:
843 if (ev.xbutton.button != clickButton)
844 break;
845 XUngrabPointer(dpy, CurrentTime);
847 if (docking) {
848 Bool docked;
850 /* icon is trying to be docked */
851 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
852 XUnmapWindow(dpy, scr->dock_shadow);
853 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy, False);
854 if (scr->last_dock->auto_collapse)
855 collapsed = 0;
857 if (workspace->clip &&
858 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
859 wDockLower(workspace->clip);
861 if (!docked) {
862 /* If icon could not be docked, slide it back to the old
863 * position */
864 SlideWindow(icon->core->window, x, y, oldX, oldY);
866 } else {
867 if (movingSingle) {
868 /* move back to its place */
869 SlideWindow(icon->core->window, x, y, oldX, oldY);
870 wAppIconMove(aicon, oldX, oldY);
871 } else {
872 XMoveWindow(dpy, icon->core->window, x, y);
873 aicon->x_pos = x;
874 aicon->y_pos = y;
876 if (workspace->clip && workspace->clip->auto_raise_lower)
877 wDockLower(workspace->clip);
879 if (collapsed) {
880 scr->last_dock->collapsed = 1;
881 wDockHideIcons(scr->last_dock);
882 collapsed = 0;
884 if (superfluous) {
885 if (ghost != None)
886 XFreePixmap(dpy, ghost);
887 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
890 if (wPreferences.auto_arrange_icons)
891 wArrangeIcons(scr, True);
893 if (wPreferences.single_click && !hasMoved)
894 iconDblClick(desc, event);
896 done = 1;
897 break;
902 /* This function save the application icon and store the path in the Dictionary */
903 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
905 WMPropList *dict = WDWindowAttributes->dictionary;
906 WMPropList *adict, *key, *iconk;
907 WMPropList *val;
908 char *tmp;
910 tmp = get_name_for_instance_class(wm_instance, wm_class);
911 key = WMCreatePLString(tmp);
912 wfree(tmp);
914 adict = WMGetFromPLDictionary(dict, key);
915 iconk = WMCreatePLString("Icon");
917 if (adict) {
918 val = WMGetFromPLDictionary(adict, iconk);
919 } else {
920 /* no dictionary for app, so create one */
921 adict = WMCreatePLDictionary(NULL, NULL);
922 WMPutInPLDictionary(dict, key, adict);
923 WMReleasePropList(adict);
924 val = NULL;
927 if (!val) {
928 val = WMCreatePLString(iconPath);
929 WMPutInPLDictionary(adict, iconk, val);
930 WMReleasePropList(val);
931 } else {
932 val = NULL;
935 WMReleasePropList(key);
936 WMReleasePropList(iconk);
938 if (val && !wPreferences.flags.noupdates)
939 UpdateDomainFile(WDWindowAttributes);
942 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
944 WAppIcon *aicon = NULL;
946 aicon = wDockFindIconForWindow(dock, main_window);
947 if (!aicon) {
948 wDockTrackWindowLaunch(dock, main_window);
949 aicon = wDockFindIconForWindow(dock, main_window);
951 return aicon;
954 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
956 WScreen *scr = wwin->screen_ptr;
957 wapp->app_icon = NULL;
959 if (scr->last_dock)
960 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
962 /* check main dock if we did not find it in last dock */
963 if (!wapp->app_icon && scr->dock)
964 wapp->app_icon = findDockIconFor(scr->dock, main_window);
966 /* finally check clips */
967 if (!wapp->app_icon) {
968 int i;
969 for (i = 0; i < scr->workspace_count; i++) {
970 WDock *dock = scr->workspaces[i]->clip;
971 if (dock)
972 wapp->app_icon = findDockIconFor(dock, main_window);
973 if (wapp->app_icon)
974 break;
978 /* If created, then set some flags */
979 if (wapp->app_icon) {
980 WWindow *mainw = wapp->main_window_desc;
982 wapp->app_icon->running = 1;
983 wapp->app_icon->icon->force_paint = 1;
984 wapp->app_icon->icon->owner = mainw;
985 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
986 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
988 wAppIconPaint(wapp->app_icon);
989 save_appicon(wapp->app_icon, True);
993 /* Add the appicon to the appiconlist */
994 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
996 appicon->prev = NULL;
997 appicon->next = scr->app_icon_list;
998 if (scr->app_icon_list)
999 scr->app_icon_list->prev = appicon;
1001 scr->app_icon_list = appicon;
1004 /* Remove the appicon from the appiconlist */
1005 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
1007 if (appicon == scr->app_icon_list) {
1008 if (appicon->next)
1009 appicon->next->prev = NULL;
1010 scr->app_icon_list = appicon->next;
1011 } else {
1012 if (appicon->next)
1013 appicon->next->prev = appicon->prev;
1014 if (appicon->prev)
1015 appicon->prev->next = appicon->next;
1018 appicon->prev = NULL;
1019 appicon->next = NULL;
1022 /* Return the AppIcon associated with a given (Xlib) Window. */
1023 WAppIcon *wAppIconFor(Window window)
1025 WObjDescriptor *desc;
1027 if (window == None)
1028 return NULL;
1030 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
1031 return NULL;
1033 if (desc->parent_type == WCLASS_APPICON || desc->parent_type == WCLASS_DOCK_ICON)
1034 return desc->parent;
1036 return NULL;