Update icon images before calling wIconUpdate
[wmaker-crm.git] / src / appicon.c
blob8e8c22b6643b3f560c949e09b39fa609b12123f0
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;
256 /* Update the icon images */
257 wIconUpdate(wapp->app_icon->icon);
259 /* Paint it */
260 wAppIconPaint(wapp->app_icon, False);
261 } else if (wapp->app_icon->docked) {
262 wapp->app_icon->running = 0;
263 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
264 } else {
265 wAppIconDestroy(wapp->app_icon);
268 wapp->app_icon = NULL;
270 if (wPreferences.auto_arrange_icons)
271 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
274 static WAppIcon *wAppIconCreate(WWindow *leader_win)
276 WAppIcon *aicon;
278 aicon = wmalloc(sizeof(WAppIcon));
279 wretain(aicon);
280 aicon->yindex = -1;
281 aicon->xindex = -1;
282 aicon->prev = NULL;
283 aicon->next = NULL;
285 if (leader_win->wm_class)
286 aicon->wm_class = wstrdup(leader_win->wm_class);
288 if (leader_win->wm_instance)
289 aicon->wm_instance = wstrdup(leader_win->wm_instance);
291 aicon->icon = icon_create_for_wwindow(leader_win);
292 #ifdef XDND
293 wXDNDMakeAwareness(aicon->icon->core->window);
294 #endif
296 /* will be overriden if docked */
297 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
298 aicon->icon->core->descriptor.handle_expose = iconExpose;
299 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
300 aicon->icon->core->descriptor.parent = aicon;
301 AddToStackList(aicon->icon->core);
302 aicon->icon->show_title = 0;
304 return aicon;
307 void wAppIconDestroy(WAppIcon * aicon)
309 WScreen *scr = aicon->icon->core->screen_ptr;
311 RemoveFromStackList(aicon->icon->core);
312 wIconDestroy(aicon->icon);
313 if (aicon->command)
314 wfree(aicon->command);
315 #ifdef XDND
316 if (aicon->dnd_command)
317 wfree(aicon->dnd_command);
318 #endif
319 if (aicon->wm_instance)
320 wfree(aicon->wm_instance);
322 if (aicon->wm_class)
323 wfree(aicon->wm_class);
325 remove_from_appicon_list(scr, aicon);
327 aicon->destroyed = 1;
328 wrelease(aicon);
331 static void drawCorner(WIcon * icon)
333 WScreen *scr = icon->core->screen_ptr;
334 XPoint points[3];
336 points[0].x = 1;
337 points[0].y = 1;
338 points[1].x = 12;
339 points[1].y = 1;
340 points[2].x = 1;
341 points[2].y = 12;
342 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
343 points, 3, Convex, CoordModeOrigin);
344 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
345 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
348 void wAppIconMove(WAppIcon * aicon, int x, int y)
350 XMoveWindow(dpy, aicon->icon->core->window, x, y);
351 aicon->x_pos = x;
352 aicon->y_pos = y;
355 #ifdef WS_INDICATOR
356 static void updateDockNumbers(WScreen * scr)
358 int length;
359 char *ws_numbers;
360 WAppIcon *dicon = scr->dock->icon_array[0];
362 ws_numbers = wmalloc(20);
363 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
364 length = strlen(ws_numbers);
366 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
368 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
369 scr->icon_title_font, 4, 3, ws_numbers, length);
371 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
372 scr->icon_title_font, 3, 2, ws_numbers, length);
374 wfree(ws_numbers);
376 #endif /* WS_INDICATOR */
378 void wAppIconPaint(WAppIcon *aicon, Bool update_icon)
380 WApplication *wapp;
381 WScreen *scr = aicon->icon->core->screen_ptr;
383 if (aicon->icon->owner)
384 wapp = wApplicationOf(aicon->icon->owner->main_window);
385 else
386 wapp = NULL;
388 if (update_icon)
389 wIconUpdate(aicon->icon);
391 wIconPaint(aicon->icon);
393 # ifdef WS_INDICATOR
394 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
395 updateDockNumbers(scr);
396 # endif
397 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
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, aicon->icon->core->window,
401 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
403 #ifdef HIDDENDOT
404 if (wapp && wapp->flags.hidden) {
405 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
406 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
407 XCopyArea(dpy, scr->dock_dots->image,
408 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
410 #endif /* HIDDENDOT */
412 if (aicon->omnipresent)
413 drawCorner(aicon->icon);
415 XSetClipMask(dpy, scr->copy_gc, None);
416 if (aicon->launching)
417 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
418 0, 0, wPreferences.icon_size, wPreferences.icon_size);
421 /* Save the application icon, if it's a dockapp then use it with dock = True */
422 void save_appicon(WAppIcon *aicon, Bool dock)
424 char *path;
426 if (!aicon)
427 return;
429 if (dock && (!aicon->docked || aicon->attracted))
430 return;
432 path = wIconStore(aicon->icon);
433 if (!path)
434 return;
436 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
437 wfree(path);
440 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
442 /* main_window may not have the full command line; try to find one which does */
443 static void relaunchApplication(WApplication *wapp)
445 WScreen *scr;
446 WWindow *wlist, *next;
448 scr = wapp->main_window_desc->screen_ptr;
449 wlist = scr->focused_window;
450 if (! wlist)
451 return;
453 while (wlist->prev)
454 wlist = wlist->prev;
456 while (wlist) {
457 next = wlist->next;
459 if (wlist->main_window == wapp->main_window) {
460 if (RelaunchWindow(wlist))
461 return;
464 wlist = next;
468 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
470 WApplication *wapp = (WApplication *) entry->clientdata;
472 relaunchApplication(wapp);
475 static void hideCallback(WMenu * menu, WMenuEntry * entry)
477 WApplication *wapp = (WApplication *) entry->clientdata;
479 if (wapp->flags.hidden) {
480 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
481 wUnhideApplication(wapp, False, False);
482 } else {
483 wHideApplication(wapp);
487 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
489 WApplication *wapp = (WApplication *) entry->clientdata;
491 wUnhideApplication(wapp, False, True);
494 static void setIconCallback(WMenu *menu, WMenuEntry *entry)
496 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
497 char *file = NULL;
498 WScreen *scr;
499 int result;
501 assert(icon != NULL);
503 if (icon->editing)
504 return;
506 icon->editing = 1;
507 scr = icon->icon->core->screen_ptr;
509 wretain(icon);
511 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
513 if (result && !icon->destroyed) {
514 if (file && *file == 0) {
515 wfree(file);
516 file = NULL;
518 if (!wIconChangeImageFile(icon->icon, file)) {
519 wMessageDialog(scr, _("Error"),
520 _("Could not open specified icon file"), _("OK"), NULL, NULL);
521 } else {
522 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
523 /* The image was updated previously at wIconChangeImageFile,
524 * so we don't need update it here again */
525 wAppIconPaint(icon, False);
527 if (file)
528 wfree(file);
530 icon->editing = 0;
531 wrelease(icon);
534 static void killCallback(WMenu * menu, WMenuEntry * entry)
536 WApplication *wapp = (WApplication *) entry->clientdata;
537 WFakeGroupLeader *fPtr;
538 char *buffer;
539 char *shortname;
540 char *basename(const char *shortname);
542 if (!WCHECK_STATE(WSTATE_NORMAL))
543 return;
545 WCHANGE_STATE(WSTATE_MODAL);
547 assert(entry->clientdata != NULL);
549 shortname = basename(wapp->app_icon->wm_instance);
551 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
552 _(" will be forcibly closed.\n"
553 "Any unsaved changes will be lost.\n" "Please confirm."));
555 fPtr = wapp->main_window_desc->fake_group;
557 wretain(wapp->main_window_desc);
558 if (wPreferences.dont_confirm_kill
559 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
560 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
561 if (fPtr != NULL) {
562 WWindow *wwin, *twin;
564 wwin = wapp->main_window_desc->screen_ptr->focused_window;
565 while (wwin) {
566 twin = wwin->prev;
567 if (wwin->fake_group == fPtr)
568 wClientKill(wwin);
569 wwin = twin;
571 } else if (!wapp->main_window_desc->flags.destroyed) {
572 wClientKill(wapp->main_window_desc);
575 wrelease(wapp->main_window_desc);
576 wfree(buffer);
577 WCHANGE_STATE(WSTATE_NORMAL);
580 static WMenu *createApplicationMenu(WScreen *scr)
582 WMenu *menu;
584 menu = wMenuCreate(scr, NULL, False);
585 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
586 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
587 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
588 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
589 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
591 return menu;
594 static void openApplicationMenu(WApplication * wapp, int x, int y)
596 WMenu *menu;
597 WScreen *scr = wapp->main_window_desc->screen_ptr;
598 int i;
600 if (!scr->icon_menu) {
601 scr->icon_menu = createApplicationMenu(scr);
602 wfree(scr->icon_menu->entries[1]->text);
605 menu = scr->icon_menu;
607 if (wapp->flags.hidden)
608 menu->entries[1]->text = _("Unhide");
609 else
610 menu->entries[1]->text = _("Hide");
612 menu->flags.realized = 0;
613 wMenuRealize(menu);
615 x -= menu->frame->core->width / 2;
616 if (x + menu->frame->core->width > scr->scr_width)
617 x = scr->scr_width - menu->frame->core->width;
619 if (x < 0)
620 x = 0;
622 /* set client data */
623 for (i = 0; i < menu->entry_no; i++)
624 menu->entries[i]->clientdata = wapp;
626 wMenuMapAt(menu, x, y, False);
629 /******************************************************************/
631 static void iconExpose(WObjDescriptor *desc, XEvent *event)
633 wAppIconPaint(desc->parent, False);
636 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
638 WAppIcon *aicon = desc->parent;
639 WApplication *wapp;
640 WScreen *scr = aicon->icon->core->screen_ptr;
641 int unhideHere;
643 assert(aicon->icon->owner != NULL);
645 wapp = wApplicationOf(aicon->icon->owner->main_window);
647 if (event->xbutton.state & ControlMask) {
648 relaunchApplication(wapp);
649 return;
652 unhideHere = (event->xbutton.state & ShiftMask);
653 /* go to the last workspace that the user worked on the app */
654 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
655 wWorkspaceChange(scr, wapp->last_workspace);
657 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
659 if (event->xbutton.state & MOD_MASK)
660 wHideOtherApplications(aicon->icon->owner);
663 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
665 WAppIcon *aicon = desc->parent;
666 WIcon *icon = aicon->icon;
667 XEvent ev;
668 int x = aicon->x_pos, y = aicon->y_pos;
669 int dx = event->xbutton.x, dy = event->xbutton.y;
670 int grabbed = 0;
671 int done = 0;
672 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
673 WScreen *scr = icon->core->screen_ptr;
674 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
675 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
676 int ix, iy;
677 int clickButton = event->xbutton.button;
678 Pixmap ghost = None;
679 Window wins[2];
680 Bool movingSingle = False;
681 int oldX = x;
682 int oldY = y;
683 Bool hasMoved = False;
685 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
686 return;
688 if (IsDoubleClick(scr, event)) {
689 /* Middle or right mouse actions were handled on first click */
690 if (event->xbutton.button == Button1)
691 iconDblClick(desc, event);
692 return;
695 if (event->xbutton.button == Button2) {
696 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
698 if (wapp)
699 relaunchApplication(wapp);
701 return;
704 if (event->xbutton.button == Button3) {
705 WObjDescriptor *desc;
706 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
708 if (!wapp)
709 return;
711 if (event->xbutton.send_event &&
712 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
713 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
714 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
715 wwarning("pointer grab failed for appicon menu");
716 return;
719 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
721 /* allow drag select of menu */
722 desc = &scr->icon_menu->menu->descriptor;
723 event->xbutton.send_event = True;
724 (*desc->handle_mousedown) (desc, event);
725 return;
728 if (event->xbutton.state & MOD_MASK)
729 wLowerFrame(icon->core);
730 else
731 wRaiseFrame(icon->core);
733 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
734 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
735 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
736 wwarning("pointer grab failed for appicon move");
738 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
739 dockable = 0;
740 else
741 dockable = canBeDocked(icon->owner);
743 wins[0] = icon->core->window;
744 wins[1] = scr->dock_shadow;
745 XRestackWindows(dpy, wins, 2);
746 if (superfluous) {
747 if (icon->pixmap != None)
748 ghost = MakeGhostIcon(scr, icon->pixmap);
749 else
750 ghost = MakeGhostIcon(scr, icon->core->window);
751 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
752 XClearWindow(dpy, scr->dock_shadow);
755 while (!done) {
756 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
757 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
758 switch (ev.type) {
759 case Expose:
760 WMHandleEvent(&ev);
761 break;
763 case EnterNotify:
764 /* It means the cursor moved so fast that it entered
765 * something else (if moving slowly, it would have
766 * stayed in the appIcon that is being moved. Ignore
767 * such "spurious" EnterNotifiy's */
768 break;
770 case MotionNotify:
771 hasMoved = True;
772 if (!grabbed) {
773 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
774 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
775 XChangeActivePointerGrab(dpy, ButtonMotionMask
776 | ButtonReleaseMask | ButtonPressMask,
777 wCursor[WCUR_MOVE], CurrentTime);
778 grabbed = 1;
779 } else {
780 break;
783 x = ev.xmotion.x_root - dx;
784 y = ev.xmotion.y_root - dy;
786 if (movingSingle)
787 XMoveWindow(dpy, icon->core->window, x, y);
788 else
789 wAppIconMove(aicon, x, y);
791 if (dockable) {
792 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
793 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
794 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
796 if (scr->last_dock != scr->dock && collapsed) {
797 scr->last_dock->collapsed = 1;
798 wDockHideIcons(scr->last_dock);
799 collapsed = 0;
801 if (!collapsed && (collapsed = scr->dock->collapsed)) {
802 scr->dock->collapsed = 0;
803 wDockShowIcons(scr->dock);
806 if (scr->dock->auto_raise_lower)
807 wDockRaise(scr->dock);
809 scr->last_dock = scr->dock;
811 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
812 if (!docking)
813 XMapWindow(dpy, scr->dock_shadow);
815 docking = 1;
816 } else if (workspace->clip &&
817 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
818 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
819 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
821 if (scr->last_dock != workspace->clip && collapsed) {
822 scr->last_dock->collapsed = 1;
823 wDockHideIcons(scr->last_dock);
824 collapsed = 0;
826 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
827 workspace->clip->collapsed = 0;
828 wDockShowIcons(workspace->clip);
831 if (workspace->clip->auto_raise_lower)
832 wDockRaise(workspace->clip);
834 scr->last_dock = workspace->clip;
836 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
837 if (!docking)
838 XMapWindow(dpy, scr->dock_shadow);
840 docking = 1;
841 } else if (docking) {
842 XUnmapWindow(dpy, scr->dock_shadow);
843 docking = 0;
846 break;
848 case ButtonPress:
849 break;
851 case ButtonRelease:
852 if (ev.xbutton.button != clickButton)
853 break;
854 XUngrabPointer(dpy, CurrentTime);
856 if (docking) {
857 Bool docked;
859 /* icon is trying to be docked */
860 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
861 XUnmapWindow(dpy, scr->dock_shadow);
862 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy, False);
863 if (scr->last_dock->auto_collapse)
864 collapsed = 0;
866 if (workspace->clip &&
867 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
868 wDockLower(workspace->clip);
870 if (!docked) {
871 /* If icon could not be docked, slide it back to the old
872 * position */
873 SlideWindow(icon->core->window, x, y, oldX, oldY);
875 } else {
876 if (movingSingle) {
877 /* move back to its place */
878 SlideWindow(icon->core->window, x, y, oldX, oldY);
879 wAppIconMove(aicon, oldX, oldY);
880 } else {
881 XMoveWindow(dpy, icon->core->window, x, y);
882 aicon->x_pos = x;
883 aicon->y_pos = y;
885 if (workspace->clip && workspace->clip->auto_raise_lower)
886 wDockLower(workspace->clip);
888 if (collapsed) {
889 scr->last_dock->collapsed = 1;
890 wDockHideIcons(scr->last_dock);
891 collapsed = 0;
893 if (superfluous) {
894 if (ghost != None)
895 XFreePixmap(dpy, ghost);
896 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
899 if (wPreferences.auto_arrange_icons)
900 wArrangeIcons(scr, True);
902 if (wPreferences.single_click && !hasMoved)
903 iconDblClick(desc, event);
905 done = 1;
906 break;
911 /* This function save the application icon and store the path in the Dictionary */
912 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
914 WMPropList *dict = WDWindowAttributes->dictionary;
915 WMPropList *adict, *key, *iconk;
916 WMPropList *val;
917 char *tmp;
919 tmp = get_name_for_instance_class(wm_instance, wm_class);
920 key = WMCreatePLString(tmp);
921 wfree(tmp);
923 adict = WMGetFromPLDictionary(dict, key);
924 iconk = WMCreatePLString("Icon");
926 if (adict) {
927 val = WMGetFromPLDictionary(adict, iconk);
928 } else {
929 /* no dictionary for app, so create one */
930 adict = WMCreatePLDictionary(NULL, NULL);
931 WMPutInPLDictionary(dict, key, adict);
932 WMReleasePropList(adict);
933 val = NULL;
936 if (!val) {
937 val = WMCreatePLString(iconPath);
938 WMPutInPLDictionary(adict, iconk, val);
939 WMReleasePropList(val);
940 } else {
941 val = NULL;
944 WMReleasePropList(key);
945 WMReleasePropList(iconk);
947 if (val && !wPreferences.flags.noupdates)
948 UpdateDomainFile(WDWindowAttributes);
951 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
953 WAppIcon *aicon = NULL;
955 aicon = wDockFindIconForWindow(dock, main_window);
956 if (!aicon) {
957 wDockTrackWindowLaunch(dock, main_window);
958 aicon = wDockFindIconForWindow(dock, main_window);
960 return aicon;
963 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
965 WScreen *scr = wwin->screen_ptr;
966 wapp->app_icon = NULL;
968 if (scr->last_dock)
969 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
971 /* check main dock if we did not find it in last dock */
972 if (!wapp->app_icon && scr->dock)
973 wapp->app_icon = findDockIconFor(scr->dock, main_window);
975 /* finally check clips */
976 if (!wapp->app_icon) {
977 int i;
978 for (i = 0; i < scr->workspace_count; i++) {
979 WDock *dock = scr->workspaces[i]->clip;
980 if (dock)
981 wapp->app_icon = findDockIconFor(dock, main_window);
982 if (wapp->app_icon)
983 break;
987 /* If created, then set some flags */
988 if (wapp->app_icon) {
989 WWindow *mainw = wapp->main_window_desc;
991 wapp->app_icon->running = 1;
992 wapp->app_icon->icon->owner = mainw;
993 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
994 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
996 /* Update the icon images */
997 wIconUpdate(wapp->app_icon->icon);
999 /* Paint it */
1000 wAppIconPaint(wapp->app_icon, False);
1001 save_appicon(wapp->app_icon, True);
1005 /* Add the appicon to the appiconlist */
1006 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
1008 appicon->prev = NULL;
1009 appicon->next = scr->app_icon_list;
1010 if (scr->app_icon_list)
1011 scr->app_icon_list->prev = appicon;
1013 scr->app_icon_list = appicon;
1016 /* Remove the appicon from the appiconlist */
1017 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
1019 if (appicon == scr->app_icon_list) {
1020 if (appicon->next)
1021 appicon->next->prev = NULL;
1022 scr->app_icon_list = appicon->next;
1023 } else {
1024 if (appicon->next)
1025 appicon->next->prev = appicon->prev;
1026 if (appicon->prev)
1027 appicon->prev->next = appicon->next;
1030 appicon->prev = NULL;
1031 appicon->next = NULL;
1034 /* Return the AppIcon associated with a given (Xlib) Window. */
1035 WAppIcon *wAppIconFor(Window window)
1037 WObjDescriptor *desc;
1039 if (window == None)
1040 return NULL;
1042 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
1043 return NULL;
1045 if (desc->parent_type == WCLASS_APPICON || desc->parent_type == WCLASS_DOCK_ICON)
1046 return desc->parent;
1048 return NULL;