Avoid icon change to default on winspector save
[wmaker-crm.git] / src / appicon.c
blob902559c2d6bf7afe8c13b8d455d1d6121766d469
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, NULL);
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 (wPreferences.highlight_active_app)
247 wIconSetHighlited(wapp->app_icon->icon, False);
248 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
249 wapp->app_icon->running = 0;
250 /* since we keep it, we don't care if it was attracted or not */
251 wapp->app_icon->attracted = 0;
252 wapp->app_icon->icon->shadowed = 0;
253 wapp->app_icon->main_window = None;
254 wapp->app_icon->pid = 0;
255 wapp->app_icon->icon->owner = NULL;
256 wapp->app_icon->icon->icon_win = None;
258 /* Update the icon images */
259 wIconUpdate(wapp->app_icon->icon, NULL);
261 /* Paint it */
262 wAppIconPaint(wapp->app_icon);
263 } else if (wapp->app_icon->docked) {
264 wapp->app_icon->running = 0;
265 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
266 } else {
267 wAppIconDestroy(wapp->app_icon);
270 wapp->app_icon = NULL;
272 if (wPreferences.auto_arrange_icons)
273 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
276 static WAppIcon *wAppIconCreate(WWindow *leader_win)
278 WAppIcon *aicon;
280 aicon = wmalloc(sizeof(WAppIcon));
281 wretain(aicon);
282 aicon->yindex = -1;
283 aicon->xindex = -1;
284 aicon->prev = NULL;
285 aicon->next = NULL;
287 if (leader_win->wm_class)
288 aicon->wm_class = wstrdup(leader_win->wm_class);
290 if (leader_win->wm_instance)
291 aicon->wm_instance = wstrdup(leader_win->wm_instance);
293 aicon->icon = icon_create_for_wwindow(leader_win);
294 #ifdef XDND
295 wXDNDMakeAwareness(aicon->icon->core->window);
296 #endif
298 /* will be overriden if docked */
299 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
300 aicon->icon->core->descriptor.handle_expose = iconExpose;
301 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
302 aicon->icon->core->descriptor.parent = aicon;
303 AddToStackList(aicon->icon->core);
304 aicon->icon->show_title = 0;
306 return aicon;
309 void wAppIconDestroy(WAppIcon * aicon)
311 WScreen *scr = aicon->icon->core->screen_ptr;
313 RemoveFromStackList(aicon->icon->core);
314 wIconDestroy(aicon->icon);
315 if (aicon->command)
316 wfree(aicon->command);
317 #ifdef XDND
318 if (aicon->dnd_command)
319 wfree(aicon->dnd_command);
320 #endif
321 if (aicon->wm_instance)
322 wfree(aicon->wm_instance);
324 if (aicon->wm_class)
325 wfree(aicon->wm_class);
327 remove_from_appicon_list(scr, aicon);
329 aicon->destroyed = 1;
330 wrelease(aicon);
333 static void drawCorner(WIcon * icon)
335 WScreen *scr = icon->core->screen_ptr;
336 XPoint points[3];
338 points[0].x = 1;
339 points[0].y = 1;
340 points[1].x = 12;
341 points[1].y = 1;
342 points[2].x = 1;
343 points[2].y = 12;
344 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
345 points, 3, Convex, CoordModeOrigin);
346 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
347 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
350 void wAppIconMove(WAppIcon * aicon, int x, int y)
352 XMoveWindow(dpy, aicon->icon->core->window, x, y);
353 aicon->x_pos = x;
354 aicon->y_pos = y;
357 #ifdef WS_INDICATOR
358 static void updateDockNumbers(WScreen * scr)
360 int length;
361 char *ws_numbers;
362 WAppIcon *dicon = scr->dock->icon_array[0];
364 ws_numbers = wmalloc(20);
365 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
366 length = strlen(ws_numbers);
368 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
370 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
371 scr->icon_title_font, 4, 3, ws_numbers, length);
373 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
374 scr->icon_title_font, 3, 2, ws_numbers, length);
376 wfree(ws_numbers);
378 #endif /* WS_INDICATOR */
380 void wAppIconPaint(WAppIcon *aicon)
382 WApplication *wapp;
383 WScreen *scr = aicon->icon->core->screen_ptr;
385 if (aicon->icon->owner)
386 wapp = wApplicationOf(aicon->icon->owner->main_window);
387 else
388 wapp = NULL;
390 wIconPaint(aicon->icon);
392 # ifdef WS_INDICATOR
393 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
394 updateDockNumbers(scr);
395 # endif
396 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
397 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
398 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
399 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
400 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
402 #ifdef HIDDENDOT
403 if (wapp && wapp->flags.hidden) {
404 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
405 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
406 XCopyArea(dpy, scr->dock_dots->image,
407 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
409 #endif /* HIDDENDOT */
411 if (aicon->omnipresent)
412 drawCorner(aicon->icon);
414 XSetClipMask(dpy, scr->copy_gc, None);
415 if (aicon->launching)
416 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
417 0, 0, wPreferences.icon_size, wPreferences.icon_size);
420 /* Save the application icon, if it's a dockapp then use it with dock = True */
421 void save_appicon(WAppIcon *aicon, Bool dock)
423 char *path;
425 if (!aicon)
426 return;
428 if (dock && (!aicon->docked || aicon->attracted))
429 return;
431 path = wIconStore(aicon->icon);
432 if (!path)
433 return;
435 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
436 wfree(path);
439 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
441 /* main_window may not have the full command line; try to find one which does */
442 static void relaunchApplication(WApplication *wapp)
444 WScreen *scr;
445 WWindow *wlist, *next;
447 scr = wapp->main_window_desc->screen_ptr;
448 wlist = scr->focused_window;
449 if (! wlist)
450 return;
452 while (wlist->prev)
453 wlist = wlist->prev;
455 while (wlist) {
456 next = wlist->next;
458 if (wlist->main_window == wapp->main_window) {
459 if (RelaunchWindow(wlist))
460 return;
463 wlist = next;
467 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
469 WApplication *wapp = (WApplication *) entry->clientdata;
471 relaunchApplication(wapp);
474 static void hideCallback(WMenu * menu, WMenuEntry * entry)
476 WApplication *wapp = (WApplication *) entry->clientdata;
478 if (wapp->flags.hidden) {
479 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
480 wUnhideApplication(wapp, False, False);
481 } else {
482 wHideApplication(wapp);
486 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
488 WApplication *wapp = (WApplication *) entry->clientdata;
490 wUnhideApplication(wapp, False, True);
493 static void setIconCallback(WMenu *menu, WMenuEntry *entry)
495 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
496 char *file = NULL;
497 WScreen *scr;
498 int result;
500 assert(icon != NULL);
502 if (icon->editing)
503 return;
505 icon->editing = 1;
506 scr = icon->icon->core->screen_ptr;
508 wretain(icon);
510 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
512 if (result && !icon->destroyed) {
513 if (file && *file == 0) {
514 wfree(file);
515 file = NULL;
517 if (!wIconChangeImageFile(icon->icon, file)) {
518 wMessageDialog(scr, _("Error"),
519 _("Could not open specified icon file"), _("OK"), NULL, NULL);
520 } else {
521 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
522 wAppIconPaint(icon);
524 if (file)
525 wfree(file);
527 icon->editing = 0;
528 wrelease(icon);
531 static void killCallback(WMenu * menu, WMenuEntry * entry)
533 WApplication *wapp = (WApplication *) entry->clientdata;
534 WFakeGroupLeader *fPtr;
535 char *buffer;
536 char *shortname;
537 char *basename(const char *shortname);
539 if (!WCHECK_STATE(WSTATE_NORMAL))
540 return;
542 WCHANGE_STATE(WSTATE_MODAL);
544 assert(entry->clientdata != NULL);
546 shortname = basename(wapp->app_icon->wm_instance);
548 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
549 _(" will be forcibly closed.\n"
550 "Any unsaved changes will be lost.\n" "Please confirm."));
552 fPtr = wapp->main_window_desc->fake_group;
554 wretain(wapp->main_window_desc);
555 if (wPreferences.dont_confirm_kill
556 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
557 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
558 if (fPtr != NULL) {
559 WWindow *wwin, *twin;
561 wwin = wapp->main_window_desc->screen_ptr->focused_window;
562 while (wwin) {
563 twin = wwin->prev;
564 if (wwin->fake_group == fPtr)
565 wClientKill(wwin);
566 wwin = twin;
568 } else if (!wapp->main_window_desc->flags.destroyed) {
569 wClientKill(wapp->main_window_desc);
572 wrelease(wapp->main_window_desc);
573 wfree(buffer);
574 WCHANGE_STATE(WSTATE_NORMAL);
577 static WMenu *createApplicationMenu(WScreen *scr)
579 WMenu *menu;
581 menu = wMenuCreate(scr, NULL, False);
582 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
583 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
584 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
585 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
586 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
588 return menu;
591 static void openApplicationMenu(WApplication * wapp, int x, int y)
593 WMenu *menu;
594 WScreen *scr = wapp->main_window_desc->screen_ptr;
595 int i;
597 if (!scr->icon_menu) {
598 scr->icon_menu = createApplicationMenu(scr);
599 wfree(scr->icon_menu->entries[1]->text);
602 menu = scr->icon_menu;
604 if (wapp->flags.hidden)
605 menu->entries[1]->text = _("Unhide");
606 else
607 menu->entries[1]->text = _("Hide");
609 menu->flags.realized = 0;
610 wMenuRealize(menu);
612 x -= menu->frame->core->width / 2;
613 if (x + menu->frame->core->width > scr->scr_width)
614 x = scr->scr_width - menu->frame->core->width;
616 if (x < 0)
617 x = 0;
619 /* set client data */
620 for (i = 0; i < menu->entry_no; i++)
621 menu->entries[i]->clientdata = wapp;
623 wMenuMapAt(menu, x, y, False);
626 /******************************************************************/
628 static void iconExpose(WObjDescriptor *desc, XEvent *event)
630 wAppIconPaint(desc->parent);
633 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
635 WAppIcon *aicon = desc->parent;
636 WApplication *wapp;
637 WScreen *scr = aicon->icon->core->screen_ptr;
638 int unhideHere;
640 assert(aicon->icon->owner != NULL);
642 wapp = wApplicationOf(aicon->icon->owner->main_window);
644 if (event->xbutton.state & ControlMask) {
645 relaunchApplication(wapp);
646 return;
649 unhideHere = (event->xbutton.state & ShiftMask);
650 /* go to the last workspace that the user worked on the app */
651 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
652 wWorkspaceChange(scr, wapp->last_workspace);
654 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
656 if (event->xbutton.state & MOD_MASK)
657 wHideOtherApplications(aicon->icon->owner);
660 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
662 WAppIcon *aicon = desc->parent;
663 WIcon *icon = aicon->icon;
664 XEvent ev;
665 int x = aicon->x_pos, y = aicon->y_pos;
666 int dx = event->xbutton.x, dy = event->xbutton.y;
667 int grabbed = 0;
668 int done = 0;
669 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
670 WScreen *scr = icon->core->screen_ptr;
671 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
672 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
673 int ix, iy;
674 int clickButton = event->xbutton.button;
675 Pixmap ghost = None;
676 Window wins[2];
677 Bool movingSingle = False;
678 int oldX = x;
679 int oldY = y;
680 Bool hasMoved = False;
682 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
683 return;
685 if (IsDoubleClick(scr, event)) {
686 /* Middle or right mouse actions were handled on first click */
687 if (event->xbutton.button == Button1)
688 iconDblClick(desc, event);
689 return;
692 if (event->xbutton.button == Button2) {
693 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
695 if (wapp)
696 relaunchApplication(wapp);
698 return;
701 if (event->xbutton.button == Button3) {
702 WObjDescriptor *desc;
703 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
705 if (!wapp)
706 return;
708 if (event->xbutton.send_event &&
709 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
710 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
711 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
712 wwarning("pointer grab failed for appicon menu");
713 return;
716 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
718 /* allow drag select of menu */
719 desc = &scr->icon_menu->menu->descriptor;
720 event->xbutton.send_event = True;
721 (*desc->handle_mousedown) (desc, event);
722 return;
725 if (event->xbutton.state & MOD_MASK)
726 wLowerFrame(icon->core);
727 else
728 wRaiseFrame(icon->core);
730 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
731 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
732 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
733 wwarning("pointer grab failed for appicon move");
735 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
736 dockable = 0;
737 else
738 dockable = canBeDocked(icon->owner);
740 wins[0] = icon->core->window;
741 wins[1] = scr->dock_shadow;
742 XRestackWindows(dpy, wins, 2);
743 if (superfluous) {
744 if (icon->pixmap != None)
745 ghost = MakeGhostIcon(scr, icon->pixmap);
746 else
747 ghost = MakeGhostIcon(scr, icon->core->window);
748 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
749 XClearWindow(dpy, scr->dock_shadow);
752 while (!done) {
753 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
754 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
755 switch (ev.type) {
756 case Expose:
757 WMHandleEvent(&ev);
758 break;
760 case EnterNotify:
761 /* It means the cursor moved so fast that it entered
762 * something else (if moving slowly, it would have
763 * stayed in the appIcon that is being moved. Ignore
764 * such "spurious" EnterNotifiy's */
765 break;
767 case MotionNotify:
768 hasMoved = True;
769 if (!grabbed) {
770 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
771 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
772 XChangeActivePointerGrab(dpy, ButtonMotionMask
773 | ButtonReleaseMask | ButtonPressMask,
774 wCursor[WCUR_MOVE], CurrentTime);
775 grabbed = 1;
776 } else {
777 break;
780 x = ev.xmotion.x_root - dx;
781 y = ev.xmotion.y_root - dy;
783 if (movingSingle)
784 XMoveWindow(dpy, icon->core->window, x, y);
785 else
786 wAppIconMove(aicon, x, y);
788 if (dockable) {
789 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
790 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
791 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
793 if (scr->last_dock != scr->dock && collapsed) {
794 scr->last_dock->collapsed = 1;
795 wDockHideIcons(scr->last_dock);
796 collapsed = 0;
798 if (!collapsed && (collapsed = scr->dock->collapsed)) {
799 scr->dock->collapsed = 0;
800 wDockShowIcons(scr->dock);
803 if (scr->dock->auto_raise_lower)
804 wDockRaise(scr->dock);
806 scr->last_dock = scr->dock;
808 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
809 if (!docking)
810 XMapWindow(dpy, scr->dock_shadow);
812 docking = 1;
813 } else if (workspace->clip &&
814 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
815 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
816 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
818 if (scr->last_dock != workspace->clip && collapsed) {
819 scr->last_dock->collapsed = 1;
820 wDockHideIcons(scr->last_dock);
821 collapsed = 0;
823 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
824 workspace->clip->collapsed = 0;
825 wDockShowIcons(workspace->clip);
828 if (workspace->clip->auto_raise_lower)
829 wDockRaise(workspace->clip);
831 scr->last_dock = workspace->clip;
833 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
834 if (!docking)
835 XMapWindow(dpy, scr->dock_shadow);
837 docking = 1;
838 } else if (docking) {
839 XUnmapWindow(dpy, scr->dock_shadow);
840 docking = 0;
843 break;
845 case ButtonPress:
846 break;
848 case ButtonRelease:
849 if (ev.xbutton.button != clickButton)
850 break;
851 XUngrabPointer(dpy, CurrentTime);
853 if (docking) {
854 Bool docked;
856 /* icon is trying to be docked */
857 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
858 XUnmapWindow(dpy, scr->dock_shadow);
859 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy, False);
860 if (scr->last_dock->auto_collapse)
861 collapsed = 0;
863 if (workspace->clip &&
864 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
865 wDockLower(workspace->clip);
867 if (!docked) {
868 /* If icon could not be docked, slide it back to the old
869 * position */
870 SlideWindow(icon->core->window, x, y, oldX, oldY);
872 } else {
873 if (movingSingle) {
874 /* move back to its place */
875 SlideWindow(icon->core->window, x, y, oldX, oldY);
876 wAppIconMove(aicon, oldX, oldY);
877 } else {
878 XMoveWindow(dpy, icon->core->window, x, y);
879 aicon->x_pos = x;
880 aicon->y_pos = y;
882 if (workspace->clip && workspace->clip->auto_raise_lower)
883 wDockLower(workspace->clip);
885 if (collapsed) {
886 scr->last_dock->collapsed = 1;
887 wDockHideIcons(scr->last_dock);
888 collapsed = 0;
890 if (superfluous) {
891 if (ghost != None)
892 XFreePixmap(dpy, ghost);
893 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
896 if (wPreferences.auto_arrange_icons)
897 wArrangeIcons(scr, True);
899 if (wPreferences.single_click && !hasMoved)
900 iconDblClick(desc, event);
902 done = 1;
903 break;
908 /* This function save the application icon and store the path in the Dictionary */
909 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
911 WMPropList *dict = WDWindowAttributes->dictionary;
912 WMPropList *adict, *key, *iconk;
913 WMPropList *val;
914 char *tmp;
916 tmp = get_name_for_instance_class(wm_instance, wm_class);
917 key = WMCreatePLString(tmp);
918 wfree(tmp);
920 adict = WMGetFromPLDictionary(dict, key);
921 iconk = WMCreatePLString("Icon");
923 if (adict) {
924 val = WMGetFromPLDictionary(adict, iconk);
925 } else {
926 /* no dictionary for app, so create one */
927 adict = WMCreatePLDictionary(NULL, NULL);
928 WMPutInPLDictionary(dict, key, adict);
929 WMReleasePropList(adict);
930 val = NULL;
933 if (!val) {
934 val = WMCreatePLString(iconPath);
935 WMPutInPLDictionary(adict, iconk, val);
936 WMReleasePropList(val);
937 } else {
938 val = NULL;
941 WMReleasePropList(key);
942 WMReleasePropList(iconk);
944 if (val && !wPreferences.flags.noupdates)
945 UpdateDomainFile(WDWindowAttributes);
948 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
950 WAppIcon *aicon = NULL;
952 aicon = wDockFindIconForWindow(dock, main_window);
953 if (!aicon) {
954 wDockTrackWindowLaunch(dock, main_window);
955 aicon = wDockFindIconForWindow(dock, main_window);
957 return aicon;
960 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
962 WScreen *scr = wwin->screen_ptr;
963 wapp->app_icon = NULL;
965 if (scr->last_dock)
966 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
968 /* check main dock if we did not find it in last dock */
969 if (!wapp->app_icon && scr->dock)
970 wapp->app_icon = findDockIconFor(scr->dock, main_window);
972 /* finally check clips */
973 if (!wapp->app_icon) {
974 int i;
975 for (i = 0; i < scr->workspace_count; i++) {
976 WDock *dock = scr->workspaces[i]->clip;
977 if (dock)
978 wapp->app_icon = findDockIconFor(dock, main_window);
979 if (wapp->app_icon)
980 break;
984 /* If created, then set some flags */
985 if (wapp->app_icon) {
986 WWindow *mainw = wapp->main_window_desc;
988 wapp->app_icon->running = 1;
989 wapp->app_icon->icon->owner = mainw;
990 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
991 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
993 /* Update the icon images */
994 wIconUpdate(wapp->app_icon->icon, NULL);
996 /* Paint it */
997 wAppIconPaint(wapp->app_icon);
998 save_appicon(wapp->app_icon, True);
1002 /* Add the appicon to the appiconlist */
1003 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
1005 appicon->prev = NULL;
1006 appicon->next = scr->app_icon_list;
1007 if (scr->app_icon_list)
1008 scr->app_icon_list->prev = appicon;
1010 scr->app_icon_list = appicon;
1013 /* Remove the appicon from the appiconlist */
1014 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
1016 if (appicon == scr->app_icon_list) {
1017 if (appicon->next)
1018 appicon->next->prev = NULL;
1019 scr->app_icon_list = appicon->next;
1020 } else {
1021 if (appicon->next)
1022 appicon->next->prev = appicon->prev;
1023 if (appicon->prev)
1024 appicon->prev->next = appicon->next;
1027 appicon->prev = NULL;
1028 appicon->next = NULL;
1031 /* Return the AppIcon associated with a given (Xlib) Window. */
1032 WAppIcon *wAppIconFor(Window window)
1034 WObjDescriptor *desc;
1036 if (window == None)
1037 return NULL;
1039 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
1040 return NULL;
1042 if (desc->parent_type == WCLASS_APPICON || desc->parent_type == WCLASS_DOCK_ICON)
1043 return desc->parent;
1045 return NULL;