Removed dup code at get_default_image
[wmaker-crm.git] / src / appicon.c
blob77c5e304fe15bfc76515b6a5704457541ed9edab
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;
198 if (!wapp || !wapp->app_icon)
199 return;
201 icon = wapp->app_icon->icon;
202 wapp->app_icon->main_window = wapp->main_window;
204 /* If the icon is docked, don't continue */
205 if (wapp->app_icon->docked)
206 return;
208 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
209 wapp->app_icon->attracted = 1;
210 if (!icon->shadowed) {
211 icon->shadowed = 1;
212 icon->force_paint = 1;
214 wDockAttachIcon(clip, wapp->app_icon, x, y);
215 } else {
216 /* We must know if the icon is painted in the screen,
217 * because if painted, then PlaceIcon will return the next
218 * space on the screen, and the icon will move */
219 if (wapp->app_icon->next == NULL && wapp->app_icon->prev == NULL) {
220 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
221 wAppIconMove(wapp->app_icon, x, y);
222 wLowerFrame(icon->core);
226 /* If we want appicon (no_appicon is not set) and the icon is not
227 * in the appicon_list, we must add it. Else, we want to avoid
228 * having it on the list */
229 if (!WFLAGP(wapp->main_window_desc, no_appicon) &&
230 wapp->app_icon->next == NULL && wapp->app_icon->prev == NULL)
231 add_to_appicon_list(scr, wapp->app_icon);
233 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
234 XMapWindow(dpy, icon->core->window);
236 if (wPreferences.auto_arrange_icons && !wapp->app_icon->attracted)
237 wArrangeIcons(scr, True);
240 void removeAppIconFor(WApplication * wapp)
242 if (!wapp->app_icon)
243 return;
245 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
246 wapp->app_icon->running = 0;
247 /* since we keep it, we don't care if it was attracted or not */
248 wapp->app_icon->attracted = 0;
249 wapp->app_icon->icon->shadowed = 0;
250 wapp->app_icon->main_window = None;
251 wapp->app_icon->pid = 0;
252 wapp->app_icon->icon->owner = NULL;
253 wapp->app_icon->icon->icon_win = None;
254 wapp->app_icon->icon->force_paint = 1;
255 wAppIconPaint(wapp->app_icon);
256 } else if (wapp->app_icon->docked) {
257 wapp->app_icon->running = 0;
258 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
259 } else {
260 wAppIconDestroy(wapp->app_icon);
263 wapp->app_icon = NULL;
265 if (wPreferences.auto_arrange_icons)
266 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
269 static WAppIcon *wAppIconCreate(WWindow *leader_win)
271 WAppIcon *aicon;
273 aicon = wmalloc(sizeof(WAppIcon));
274 wretain(aicon);
275 aicon->yindex = -1;
276 aicon->xindex = -1;
277 aicon->prev = NULL;
278 aicon->next = NULL;
280 if (leader_win->wm_class)
281 aicon->wm_class = wstrdup(leader_win->wm_class);
283 if (leader_win->wm_instance)
284 aicon->wm_instance = wstrdup(leader_win->wm_instance);
286 aicon->icon = icon_create_for_wwindow(leader_win);
287 #ifdef XDND
288 wXDNDMakeAwareness(aicon->icon->core->window);
289 #endif
291 /* will be overriden if docked */
292 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
293 aicon->icon->core->descriptor.handle_expose = iconExpose;
294 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
295 aicon->icon->core->descriptor.parent = aicon;
296 AddToStackList(aicon->icon->core);
297 aicon->icon->show_title = 0;
299 return aicon;
302 void wAppIconDestroy(WAppIcon * aicon)
304 WScreen *scr = aicon->icon->core->screen_ptr;
306 RemoveFromStackList(aicon->icon->core);
307 wIconDestroy(aicon->icon);
308 if (aicon->command)
309 wfree(aicon->command);
310 #ifdef XDND
311 if (aicon->dnd_command)
312 wfree(aicon->dnd_command);
313 #endif
314 if (aicon->wm_instance)
315 wfree(aicon->wm_instance);
317 if (aicon->wm_class)
318 wfree(aicon->wm_class);
320 remove_from_appicon_list(scr, aicon);
322 aicon->destroyed = 1;
323 wrelease(aicon);
326 static void drawCorner(WIcon * icon)
328 WScreen *scr = icon->core->screen_ptr;
329 XPoint points[3];
331 points[0].x = 1;
332 points[0].y = 1;
333 points[1].x = 12;
334 points[1].y = 1;
335 points[2].x = 1;
336 points[2].y = 12;
337 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
338 points, 3, Convex, CoordModeOrigin);
339 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
340 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
343 void wAppIconMove(WAppIcon * aicon, int x, int y)
345 XMoveWindow(dpy, aicon->icon->core->window, x, y);
346 aicon->x_pos = x;
347 aicon->y_pos = y;
350 #ifdef WS_INDICATOR
351 static void updateDockNumbers(WScreen * scr)
353 int length;
354 char *ws_numbers;
355 WAppIcon *dicon = scr->dock->icon_array[0];
357 ws_numbers = wmalloc(20);
358 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
359 length = strlen(ws_numbers);
361 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
363 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
364 scr->icon_title_font, 4, 3, ws_numbers, length);
366 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
367 scr->icon_title_font, 3, 2, ws_numbers, length);
369 wfree(ws_numbers);
371 #endif /* WS_INDICATOR */
373 void wAppIconPaint(WAppIcon * aicon)
375 WApplication *wapp;
376 WScreen *scr = aicon->icon->core->screen_ptr;
378 if (aicon->icon->owner)
379 wapp = wApplicationOf(aicon->icon->owner->main_window);
380 else
381 wapp = NULL;
383 wIconPaint(aicon->icon);
385 # ifdef WS_INDICATOR
386 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
387 updateDockNumbers(scr);
388 # endif
389 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
390 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
391 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
392 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
393 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
395 #ifdef HIDDENDOT
396 if (wapp && wapp->flags.hidden) {
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,
400 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
402 #endif /* HIDDENDOT */
404 if (aicon->omnipresent)
405 drawCorner(aicon->icon);
407 XSetClipMask(dpy, scr->copy_gc, None);
408 if (aicon->launching)
409 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
410 0, 0, wPreferences.icon_size, wPreferences.icon_size);
413 /* Save the application icon, if it's a dockapp then use it with dock = True */
414 void save_appicon(WAppIcon *aicon, Bool dock)
416 char *path;
418 if (!aicon)
419 return;
421 if (dock && (!aicon->docked || aicon->attracted))
422 return;
424 path = wIconStore(aicon->icon);
425 if (!path)
426 return;
428 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
429 wfree(path);
432 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
434 /* main_window may not have the full command line; try to find one which does */
435 static void relaunchApplication(WApplication *wapp)
437 WScreen *scr;
438 WWindow *wlist, *next;
440 scr = wapp->main_window_desc->screen_ptr;
441 wlist = scr->focused_window;
442 if (! wlist)
443 return;
445 while (wlist->prev)
446 wlist = wlist->prev;
448 while (wlist) {
449 next = wlist->next;
451 if (wlist->main_window == wapp->main_window) {
452 if (RelaunchWindow(wlist))
453 return;
456 wlist = next;
460 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
462 WApplication *wapp = (WApplication *) entry->clientdata;
464 relaunchApplication(wapp);
467 static void hideCallback(WMenu * menu, WMenuEntry * entry)
469 WApplication *wapp = (WApplication *) entry->clientdata;
471 if (wapp->flags.hidden) {
472 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
473 wUnhideApplication(wapp, False, False);
474 } else {
475 wHideApplication(wapp);
479 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
481 WApplication *wapp = (WApplication *) entry->clientdata;
483 wUnhideApplication(wapp, False, True);
486 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
488 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
489 char *file = NULL;
490 WScreen *scr;
491 int result;
493 assert(icon != NULL);
495 if (icon->editing)
496 return;
498 icon->editing = 1;
499 scr = icon->icon->core->screen_ptr;
501 wretain(icon);
503 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
505 if (result && !icon->destroyed) {
506 if (file && *file == 0) {
507 wfree(file);
508 file = NULL;
510 if (!wIconChangeImageFile(icon->icon, file)) {
511 wMessageDialog(scr, _("Error"),
512 _("Could not open specified icon file"), _("OK"), NULL, NULL);
513 } else {
514 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
515 wAppIconPaint(icon);
517 if (file)
518 wfree(file);
520 icon->editing = 0;
521 wrelease(icon);
524 static void killCallback(WMenu * menu, WMenuEntry * entry)
526 WApplication *wapp = (WApplication *) entry->clientdata;
527 WFakeGroupLeader *fPtr;
528 char *buffer;
529 char *shortname;
530 char *basename(const char *shortname);
532 if (!WCHECK_STATE(WSTATE_NORMAL))
533 return;
535 WCHANGE_STATE(WSTATE_MODAL);
537 assert(entry->clientdata != NULL);
539 shortname = basename(wapp->app_icon->wm_instance);
541 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
542 _(" will be forcibly closed.\n"
543 "Any unsaved changes will be lost.\n" "Please confirm."));
545 fPtr = wapp->main_window_desc->fake_group;
547 wretain(wapp->main_window_desc);
548 if (wPreferences.dont_confirm_kill
549 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
550 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
551 if (fPtr != NULL) {
552 WWindow *wwin, *twin;
554 wwin = wapp->main_window_desc->screen_ptr->focused_window;
555 while (wwin) {
556 twin = wwin->prev;
557 if (wwin->fake_group == fPtr)
558 wClientKill(wwin);
559 wwin = twin;
561 } else if (!wapp->main_window_desc->flags.destroyed) {
562 wClientKill(wapp->main_window_desc);
565 wrelease(wapp->main_window_desc);
566 wfree(buffer);
567 WCHANGE_STATE(WSTATE_NORMAL);
570 static WMenu *createApplicationMenu(WScreen * scr)
572 WMenu *menu;
574 menu = wMenuCreate(scr, NULL, False);
575 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
576 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
577 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
578 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
579 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
581 return menu;
584 static void openApplicationMenu(WApplication * wapp, int x, int y)
586 WMenu *menu;
587 WScreen *scr = wapp->main_window_desc->screen_ptr;
588 int i;
590 if (!scr->icon_menu) {
591 scr->icon_menu = createApplicationMenu(scr);
592 wfree(scr->icon_menu->entries[1]->text);
595 menu = scr->icon_menu;
597 if (wapp->flags.hidden)
598 menu->entries[1]->text = _("Unhide");
599 else
600 menu->entries[1]->text = _("Hide");
602 menu->flags.realized = 0;
603 wMenuRealize(menu);
605 x -= menu->frame->core->width / 2;
606 if (x + menu->frame->core->width > scr->scr_width)
607 x = scr->scr_width - menu->frame->core->width;
609 if (x < 0)
610 x = 0;
612 /* set client data */
613 for (i = 0; i < menu->entry_no; i++)
614 menu->entries[i]->clientdata = wapp;
616 wMenuMapAt(menu, x, y, False);
619 /******************************************************************/
621 static void iconExpose(WObjDescriptor * desc, XEvent * event)
623 wAppIconPaint(desc->parent);
626 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
628 WAppIcon *aicon = desc->parent;
629 WApplication *wapp;
630 WScreen *scr = aicon->icon->core->screen_ptr;
631 int unhideHere;
633 assert(aicon->icon->owner != NULL);
635 wapp = wApplicationOf(aicon->icon->owner->main_window);
637 if (event->xbutton.state & ControlMask) {
638 relaunchApplication(wapp);
639 return;
642 unhideHere = (event->xbutton.state & ShiftMask);
643 /* go to the last workspace that the user worked on the app */
644 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
645 wWorkspaceChange(scr, wapp->last_workspace);
647 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
649 if (event->xbutton.state & MOD_MASK)
650 wHideOtherApplications(aicon->icon->owner);
653 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
655 WAppIcon *aicon = desc->parent;
656 WIcon *icon = aicon->icon;
657 XEvent ev;
658 int x = aicon->x_pos, y = aicon->y_pos;
659 int dx = event->xbutton.x, dy = event->xbutton.y;
660 int grabbed = 0;
661 int done = 0;
662 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
663 WScreen *scr = icon->core->screen_ptr;
664 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
665 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
666 int ix, iy;
667 int clickButton = event->xbutton.button;
668 Pixmap ghost = None;
669 Window wins[2];
670 Bool movingSingle = False;
671 int oldX = x;
672 int oldY = y;
673 Bool hasMoved = False;
675 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
676 return;
678 if (IsDoubleClick(scr, event)) {
679 /* Middle or right mouse actions were handled on first click */
680 if (event->xbutton.button == Button1)
681 iconDblClick(desc, event);
682 return;
685 if (event->xbutton.button == Button2) {
686 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
688 if (wapp)
689 relaunchApplication(wapp);
691 return;
694 if (event->xbutton.button == Button3) {
695 WObjDescriptor *desc;
696 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
698 if (!wapp)
699 return;
701 if (event->xbutton.send_event &&
702 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
703 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
704 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
705 wwarning("pointer grab failed for appicon menu");
706 return;
709 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
711 /* allow drag select of menu */
712 desc = &scr->icon_menu->menu->descriptor;
713 event->xbutton.send_event = True;
714 (*desc->handle_mousedown) (desc, event);
715 return;
718 if (event->xbutton.state & MOD_MASK)
719 wLowerFrame(icon->core);
720 else
721 wRaiseFrame(icon->core);
723 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
724 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
725 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
726 wwarning("pointer grab failed for appicon move");
728 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
729 dockable = 0;
730 else
731 dockable = canBeDocked(icon->owner);
733 wins[0] = icon->core->window;
734 wins[1] = scr->dock_shadow;
735 XRestackWindows(dpy, wins, 2);
736 if (superfluous) {
737 if (icon->pixmap != None)
738 ghost = MakeGhostIcon(scr, icon->pixmap);
739 else
740 ghost = MakeGhostIcon(scr, icon->core->window);
741 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
742 XClearWindow(dpy, scr->dock_shadow);
745 while (!done) {
746 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
747 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
748 switch (ev.type) {
749 case Expose:
750 WMHandleEvent(&ev);
751 break;
753 case EnterNotify:
754 /* It means the cursor moved so fast that it entered
755 * something else (if moving slowly, it would have
756 * stayed in the appIcon that is being moved. Ignore
757 * such "spurious" EnterNotifiy's */
758 break;
760 case MotionNotify:
761 hasMoved = True;
762 if (!grabbed) {
763 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
764 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
765 XChangeActivePointerGrab(dpy, ButtonMotionMask
766 | ButtonReleaseMask | ButtonPressMask,
767 wCursor[WCUR_MOVE], CurrentTime);
768 grabbed = 1;
769 } else {
770 break;
773 x = ev.xmotion.x_root - dx;
774 y = ev.xmotion.y_root - dy;
776 if (movingSingle)
777 XMoveWindow(dpy, icon->core->window, x, y);
778 else
779 wAppIconMove(aicon, x, y);
781 if (dockable) {
782 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
783 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
784 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
786 if (scr->last_dock != scr->dock && collapsed) {
787 scr->last_dock->collapsed = 1;
788 wDockHideIcons(scr->last_dock);
789 collapsed = 0;
791 if (!collapsed && (collapsed = scr->dock->collapsed)) {
792 scr->dock->collapsed = 0;
793 wDockShowIcons(scr->dock);
796 if (scr->dock->auto_raise_lower)
797 wDockRaise(scr->dock);
799 scr->last_dock = scr->dock;
801 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
802 if (!docking)
803 XMapWindow(dpy, scr->dock_shadow);
805 docking = 1;
806 } else if (workspace->clip &&
807 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
808 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
809 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
811 if (scr->last_dock != workspace->clip && collapsed) {
812 scr->last_dock->collapsed = 1;
813 wDockHideIcons(scr->last_dock);
814 collapsed = 0;
816 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
817 workspace->clip->collapsed = 0;
818 wDockShowIcons(workspace->clip);
821 if (workspace->clip->auto_raise_lower)
822 wDockRaise(workspace->clip);
824 scr->last_dock = workspace->clip;
826 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
827 if (!docking)
828 XMapWindow(dpy, scr->dock_shadow);
830 docking = 1;
831 } else if (docking) {
832 XUnmapWindow(dpy, scr->dock_shadow);
833 docking = 0;
836 break;
838 case ButtonPress:
839 break;
841 case ButtonRelease:
842 if (ev.xbutton.button != clickButton)
843 break;
844 XUngrabPointer(dpy, CurrentTime);
846 if (docking) {
847 Bool docked;
849 /* icon is trying to be docked */
850 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
851 XUnmapWindow(dpy, scr->dock_shadow);
852 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
853 if (scr->last_dock->auto_collapse)
854 collapsed = 0;
856 if (workspace->clip &&
857 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
858 wDockLower(workspace->clip);
860 if (!docked) {
861 /* If icon could not be docked, slide it back to the old
862 * position */
863 SlideWindow(icon->core->window, x, y, oldX, oldY);
865 } else {
866 if (movingSingle) {
867 /* move back to its place */
868 SlideWindow(icon->core->window, x, y, oldX, oldY);
869 wAppIconMove(aicon, oldX, oldY);
870 } else {
871 XMoveWindow(dpy, icon->core->window, x, y);
872 aicon->x_pos = x;
873 aicon->y_pos = y;
875 if (workspace->clip && workspace->clip->auto_raise_lower)
876 wDockLower(workspace->clip);
878 if (collapsed) {
879 scr->last_dock->collapsed = 1;
880 wDockHideIcons(scr->last_dock);
881 collapsed = 0;
883 if (superfluous) {
884 if (ghost != None)
885 XFreePixmap(dpy, ghost);
886 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
889 if (wPreferences.auto_arrange_icons)
890 wArrangeIcons(scr, True);
892 if (wPreferences.single_click && !hasMoved)
893 iconDblClick(desc, event);
895 done = 1;
896 break;
901 /* This function save the application icon and store the path in the Dictionary */
902 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
904 WMPropList *dict = WDWindowAttributes->dictionary;
905 WMPropList *adict, *key, *iconk;
906 WMPropList *val;
907 char *tmp;
909 tmp = get_name_for_instance_class(wm_instance, wm_class);
910 key = WMCreatePLString(tmp);
911 wfree(tmp);
913 adict = WMGetFromPLDictionary(dict, key);
914 iconk = WMCreatePLString("Icon");
916 if (adict) {
917 val = WMGetFromPLDictionary(adict, iconk);
918 } else {
919 /* no dictionary for app, so create one */
920 adict = WMCreatePLDictionary(NULL, NULL);
921 WMPutInPLDictionary(dict, key, adict);
922 WMReleasePropList(adict);
923 val = NULL;
926 if (!val) {
927 val = WMCreatePLString(iconPath);
928 WMPutInPLDictionary(adict, iconk, val);
929 WMReleasePropList(val);
930 } else {
931 val = NULL;
934 WMReleasePropList(key);
935 WMReleasePropList(iconk);
937 if (val && !wPreferences.flags.noupdates)
938 UpdateDomainFile(WDWindowAttributes);
941 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
943 WAppIcon *aicon = NULL;
945 aicon = wDockFindIconForWindow(dock, main_window);
946 if (!aicon) {
947 wDockTrackWindowLaunch(dock, main_window);
948 aicon = wDockFindIconForWindow(dock, main_window);
950 return aicon;
953 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
955 WScreen *scr = wwin->screen_ptr;
956 wapp->app_icon = NULL;
958 if (scr->last_dock)
959 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
961 /* check main dock if we did not find it in last dock */
962 if (!wapp->app_icon && scr->dock)
963 wapp->app_icon = findDockIconFor(scr->dock, main_window);
965 /* finally check clips */
966 if (!wapp->app_icon) {
967 int i;
968 for (i = 0; i < scr->workspace_count; i++) {
969 WDock *dock = scr->workspaces[i]->clip;
970 if (dock)
971 wapp->app_icon = findDockIconFor(dock, main_window);
972 if (wapp->app_icon)
973 break;
977 /* If created, then set some flags */
978 if (wapp->app_icon) {
979 WWindow *mainw = wapp->main_window_desc;
981 wapp->app_icon->running = 1;
982 wapp->app_icon->icon->force_paint = 1;
983 wapp->app_icon->icon->owner = mainw;
984 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
985 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
987 wAppIconPaint(wapp->app_icon);
988 save_appicon(wapp->app_icon, True);
992 /* Add the appicon to the appiconlist */
993 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
995 appicon->prev = NULL;
996 appicon->next = scr->app_icon_list;
997 if (scr->app_icon_list)
998 scr->app_icon_list->prev = appicon;
1000 scr->app_icon_list = appicon;
1003 /* Remove the appicon from the appiconlist */
1004 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
1006 if (appicon == scr->app_icon_list) {
1007 if (appicon->next)
1008 appicon->next->prev = NULL;
1009 scr->app_icon_list = appicon->next;
1010 } else {
1011 if (appicon->next)
1012 appicon->next->prev = appicon->prev;
1013 if (appicon->prev)
1014 appicon->prev->next = appicon->next;
1017 appicon->prev = NULL;
1018 appicon->next = NULL;
1021 /* Return the AppIcon associated with a given (Xlib) Window. */
1022 WAppIcon *wAppIconFor(Window window)
1024 WObjDescriptor *desc;
1026 if (window == None)
1027 return NULL;
1029 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
1030 return NULL;
1032 if (desc->parent_type == WCLASS_APPICON || desc->parent_type == WCLASS_DOCK_ICON)
1033 return desc->parent;
1035 return NULL;