133aabcf775a4c11b1b50c142167a5beba9ec435
[wmaker-crm.git] / src / appicon.c
blob133aabcf775a4c11b1b50c142167a5beba9ec435
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 wAppIconPaint(wapp->app_icon, True);
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, Bool update_icon)
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 if (update_icon)
384 wIconUpdate(aicon->icon);
386 wIconPaint(aicon->icon);
388 # ifdef WS_INDICATOR
389 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
390 updateDockNumbers(scr);
391 # endif
392 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
393 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
394 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
395 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
396 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
398 #ifdef HIDDENDOT
399 if (wapp && wapp->flags.hidden) {
400 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
401 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
402 XCopyArea(dpy, scr->dock_dots->image,
403 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
405 #endif /* HIDDENDOT */
407 if (aicon->omnipresent)
408 drawCorner(aicon->icon);
410 XSetClipMask(dpy, scr->copy_gc, None);
411 if (aicon->launching)
412 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
413 0, 0, wPreferences.icon_size, wPreferences.icon_size);
416 /* Save the application icon, if it's a dockapp then use it with dock = True */
417 void save_appicon(WAppIcon *aicon, Bool dock)
419 char *path;
421 if (!aicon)
422 return;
424 if (dock && (!aicon->docked || aicon->attracted))
425 return;
427 path = wIconStore(aicon->icon);
428 if (!path)
429 return;
431 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
432 wfree(path);
435 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
437 /* main_window may not have the full command line; try to find one which does */
438 static void relaunchApplication(WApplication *wapp)
440 WScreen *scr;
441 WWindow *wlist, *next;
443 scr = wapp->main_window_desc->screen_ptr;
444 wlist = scr->focused_window;
445 if (! wlist)
446 return;
448 while (wlist->prev)
449 wlist = wlist->prev;
451 while (wlist) {
452 next = wlist->next;
454 if (wlist->main_window == wapp->main_window) {
455 if (RelaunchWindow(wlist))
456 return;
459 wlist = next;
463 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
465 WApplication *wapp = (WApplication *) entry->clientdata;
467 relaunchApplication(wapp);
470 static void hideCallback(WMenu * menu, WMenuEntry * entry)
472 WApplication *wapp = (WApplication *) entry->clientdata;
474 if (wapp->flags.hidden) {
475 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
476 wUnhideApplication(wapp, False, False);
477 } else {
478 wHideApplication(wapp);
482 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
484 WApplication *wapp = (WApplication *) entry->clientdata;
486 wUnhideApplication(wapp, False, True);
489 static void setIconCallback(WMenu *menu, WMenuEntry *entry)
491 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
492 char *file = NULL;
493 WScreen *scr;
494 int result;
496 assert(icon != NULL);
498 if (icon->editing)
499 return;
501 icon->editing = 1;
502 scr = icon->icon->core->screen_ptr;
504 wretain(icon);
506 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
508 if (result && !icon->destroyed) {
509 if (file && *file == 0) {
510 wfree(file);
511 file = NULL;
513 if (!wIconChangeImageFile(icon->icon, file)) {
514 wMessageDialog(scr, _("Error"),
515 _("Could not open specified icon file"), _("OK"), NULL, NULL);
516 } else {
517 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
518 /* The image was updated previously at wIconChangeImageFile,
519 * so we don't need update it here again */
520 wAppIconPaint(icon, False);
522 if (file)
523 wfree(file);
525 icon->editing = 0;
526 wrelease(icon);
529 static void killCallback(WMenu * menu, WMenuEntry * entry)
531 WApplication *wapp = (WApplication *) entry->clientdata;
532 WFakeGroupLeader *fPtr;
533 char *buffer;
534 char *shortname;
535 char *basename(const char *shortname);
537 if (!WCHECK_STATE(WSTATE_NORMAL))
538 return;
540 WCHANGE_STATE(WSTATE_MODAL);
542 assert(entry->clientdata != NULL);
544 shortname = basename(wapp->app_icon->wm_instance);
546 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
547 _(" will be forcibly closed.\n"
548 "Any unsaved changes will be lost.\n" "Please confirm."));
550 fPtr = wapp->main_window_desc->fake_group;
552 wretain(wapp->main_window_desc);
553 if (wPreferences.dont_confirm_kill
554 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
555 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
556 if (fPtr != NULL) {
557 WWindow *wwin, *twin;
559 wwin = wapp->main_window_desc->screen_ptr->focused_window;
560 while (wwin) {
561 twin = wwin->prev;
562 if (wwin->fake_group == fPtr)
563 wClientKill(wwin);
564 wwin = twin;
566 } else if (!wapp->main_window_desc->flags.destroyed) {
567 wClientKill(wapp->main_window_desc);
570 wrelease(wapp->main_window_desc);
571 wfree(buffer);
572 WCHANGE_STATE(WSTATE_NORMAL);
575 static WMenu *createApplicationMenu(WScreen *scr)
577 WMenu *menu;
579 menu = wMenuCreate(scr, NULL, False);
580 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
581 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
582 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
583 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
584 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
586 return menu;
589 static void openApplicationMenu(WApplication * wapp, int x, int y)
591 WMenu *menu;
592 WScreen *scr = wapp->main_window_desc->screen_ptr;
593 int i;
595 if (!scr->icon_menu) {
596 scr->icon_menu = createApplicationMenu(scr);
597 wfree(scr->icon_menu->entries[1]->text);
600 menu = scr->icon_menu;
602 if (wapp->flags.hidden)
603 menu->entries[1]->text = _("Unhide");
604 else
605 menu->entries[1]->text = _("Hide");
607 menu->flags.realized = 0;
608 wMenuRealize(menu);
610 x -= menu->frame->core->width / 2;
611 if (x + menu->frame->core->width > scr->scr_width)
612 x = scr->scr_width - menu->frame->core->width;
614 if (x < 0)
615 x = 0;
617 /* set client data */
618 for (i = 0; i < menu->entry_no; i++)
619 menu->entries[i]->clientdata = wapp;
621 wMenuMapAt(menu, x, y, False);
624 /******************************************************************/
626 static void iconExpose(WObjDescriptor *desc, XEvent *event)
628 wAppIconPaint(desc->parent, False);
631 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
633 WAppIcon *aicon = desc->parent;
634 WApplication *wapp;
635 WScreen *scr = aicon->icon->core->screen_ptr;
636 int unhideHere;
638 assert(aicon->icon->owner != NULL);
640 wapp = wApplicationOf(aicon->icon->owner->main_window);
642 if (event->xbutton.state & ControlMask) {
643 relaunchApplication(wapp);
644 return;
647 unhideHere = (event->xbutton.state & ShiftMask);
648 /* go to the last workspace that the user worked on the app */
649 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
650 wWorkspaceChange(scr, wapp->last_workspace);
652 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
654 if (event->xbutton.state & MOD_MASK)
655 wHideOtherApplications(aicon->icon->owner);
658 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
660 WAppIcon *aicon = desc->parent;
661 WIcon *icon = aicon->icon;
662 XEvent ev;
663 int x = aicon->x_pos, y = aicon->y_pos;
664 int dx = event->xbutton.x, dy = event->xbutton.y;
665 int grabbed = 0;
666 int done = 0;
667 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
668 WScreen *scr = icon->core->screen_ptr;
669 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
670 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
671 int ix, iy;
672 int clickButton = event->xbutton.button;
673 Pixmap ghost = None;
674 Window wins[2];
675 Bool movingSingle = False;
676 int oldX = x;
677 int oldY = y;
678 Bool hasMoved = False;
680 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
681 return;
683 if (IsDoubleClick(scr, event)) {
684 /* Middle or right mouse actions were handled on first click */
685 if (event->xbutton.button == Button1)
686 iconDblClick(desc, event);
687 return;
690 if (event->xbutton.button == Button2) {
691 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
693 if (wapp)
694 relaunchApplication(wapp);
696 return;
699 if (event->xbutton.button == Button3) {
700 WObjDescriptor *desc;
701 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
703 if (!wapp)
704 return;
706 if (event->xbutton.send_event &&
707 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
708 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
709 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
710 wwarning("pointer grab failed for appicon menu");
711 return;
714 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
716 /* allow drag select of menu */
717 desc = &scr->icon_menu->menu->descriptor;
718 event->xbutton.send_event = True;
719 (*desc->handle_mousedown) (desc, event);
720 return;
723 if (event->xbutton.state & MOD_MASK)
724 wLowerFrame(icon->core);
725 else
726 wRaiseFrame(icon->core);
728 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
729 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
730 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
731 wwarning("pointer grab failed for appicon move");
733 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
734 dockable = 0;
735 else
736 dockable = canBeDocked(icon->owner);
738 wins[0] = icon->core->window;
739 wins[1] = scr->dock_shadow;
740 XRestackWindows(dpy, wins, 2);
741 if (superfluous) {
742 if (icon->pixmap != None)
743 ghost = MakeGhostIcon(scr, icon->pixmap);
744 else
745 ghost = MakeGhostIcon(scr, icon->core->window);
746 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
747 XClearWindow(dpy, scr->dock_shadow);
750 while (!done) {
751 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
752 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
753 switch (ev.type) {
754 case Expose:
755 WMHandleEvent(&ev);
756 break;
758 case EnterNotify:
759 /* It means the cursor moved so fast that it entered
760 * something else (if moving slowly, it would have
761 * stayed in the appIcon that is being moved. Ignore
762 * such "spurious" EnterNotifiy's */
763 break;
765 case MotionNotify:
766 hasMoved = True;
767 if (!grabbed) {
768 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
769 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
770 XChangeActivePointerGrab(dpy, ButtonMotionMask
771 | ButtonReleaseMask | ButtonPressMask,
772 wCursor[WCUR_MOVE], CurrentTime);
773 grabbed = 1;
774 } else {
775 break;
778 x = ev.xmotion.x_root - dx;
779 y = ev.xmotion.y_root - dy;
781 if (movingSingle)
782 XMoveWindow(dpy, icon->core->window, x, y);
783 else
784 wAppIconMove(aicon, x, y);
786 if (dockable) {
787 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
788 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
789 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
791 if (scr->last_dock != scr->dock && collapsed) {
792 scr->last_dock->collapsed = 1;
793 wDockHideIcons(scr->last_dock);
794 collapsed = 0;
796 if (!collapsed && (collapsed = scr->dock->collapsed)) {
797 scr->dock->collapsed = 0;
798 wDockShowIcons(scr->dock);
801 if (scr->dock->auto_raise_lower)
802 wDockRaise(scr->dock);
804 scr->last_dock = scr->dock;
806 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
807 if (!docking)
808 XMapWindow(dpy, scr->dock_shadow);
810 docking = 1;
811 } else if (workspace->clip &&
812 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
813 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
814 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
816 if (scr->last_dock != workspace->clip && collapsed) {
817 scr->last_dock->collapsed = 1;
818 wDockHideIcons(scr->last_dock);
819 collapsed = 0;
821 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
822 workspace->clip->collapsed = 0;
823 wDockShowIcons(workspace->clip);
826 if (workspace->clip->auto_raise_lower)
827 wDockRaise(workspace->clip);
829 scr->last_dock = workspace->clip;
831 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
832 if (!docking)
833 XMapWindow(dpy, scr->dock_shadow);
835 docking = 1;
836 } else if (docking) {
837 XUnmapWindow(dpy, scr->dock_shadow);
838 docking = 0;
841 break;
843 case ButtonPress:
844 break;
846 case ButtonRelease:
847 if (ev.xbutton.button != clickButton)
848 break;
849 XUngrabPointer(dpy, CurrentTime);
851 if (docking) {
852 Bool docked;
854 /* icon is trying to be docked */
855 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
856 XUnmapWindow(dpy, scr->dock_shadow);
857 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy, False);
858 if (scr->last_dock->auto_collapse)
859 collapsed = 0;
861 if (workspace->clip &&
862 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
863 wDockLower(workspace->clip);
865 if (!docked) {
866 /* If icon could not be docked, slide it back to the old
867 * position */
868 SlideWindow(icon->core->window, x, y, oldX, oldY);
870 } else {
871 if (movingSingle) {
872 /* move back to its place */
873 SlideWindow(icon->core->window, x, y, oldX, oldY);
874 wAppIconMove(aicon, oldX, oldY);
875 } else {
876 XMoveWindow(dpy, icon->core->window, x, y);
877 aicon->x_pos = x;
878 aicon->y_pos = y;
880 if (workspace->clip && workspace->clip->auto_raise_lower)
881 wDockLower(workspace->clip);
883 if (collapsed) {
884 scr->last_dock->collapsed = 1;
885 wDockHideIcons(scr->last_dock);
886 collapsed = 0;
888 if (superfluous) {
889 if (ghost != None)
890 XFreePixmap(dpy, ghost);
891 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
894 if (wPreferences.auto_arrange_icons)
895 wArrangeIcons(scr, True);
897 if (wPreferences.single_click && !hasMoved)
898 iconDblClick(desc, event);
900 done = 1;
901 break;
906 /* This function save the application icon and store the path in the Dictionary */
907 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
909 WMPropList *dict = WDWindowAttributes->dictionary;
910 WMPropList *adict, *key, *iconk;
911 WMPropList *val;
912 char *tmp;
914 tmp = get_name_for_instance_class(wm_instance, wm_class);
915 key = WMCreatePLString(tmp);
916 wfree(tmp);
918 adict = WMGetFromPLDictionary(dict, key);
919 iconk = WMCreatePLString("Icon");
921 if (adict) {
922 val = WMGetFromPLDictionary(adict, iconk);
923 } else {
924 /* no dictionary for app, so create one */
925 adict = WMCreatePLDictionary(NULL, NULL);
926 WMPutInPLDictionary(dict, key, adict);
927 WMReleasePropList(adict);
928 val = NULL;
931 if (!val) {
932 val = WMCreatePLString(iconPath);
933 WMPutInPLDictionary(adict, iconk, val);
934 WMReleasePropList(val);
935 } else {
936 val = NULL;
939 WMReleasePropList(key);
940 WMReleasePropList(iconk);
942 if (val && !wPreferences.flags.noupdates)
943 UpdateDomainFile(WDWindowAttributes);
946 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
948 WAppIcon *aicon = NULL;
950 aicon = wDockFindIconForWindow(dock, main_window);
951 if (!aicon) {
952 wDockTrackWindowLaunch(dock, main_window);
953 aicon = wDockFindIconForWindow(dock, main_window);
955 return aicon;
958 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
960 WScreen *scr = wwin->screen_ptr;
961 wapp->app_icon = NULL;
963 if (scr->last_dock)
964 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
966 /* check main dock if we did not find it in last dock */
967 if (!wapp->app_icon && scr->dock)
968 wapp->app_icon = findDockIconFor(scr->dock, main_window);
970 /* finally check clips */
971 if (!wapp->app_icon) {
972 int i;
973 for (i = 0; i < scr->workspace_count; i++) {
974 WDock *dock = scr->workspaces[i]->clip;
975 if (dock)
976 wapp->app_icon = findDockIconFor(dock, main_window);
977 if (wapp->app_icon)
978 break;
982 /* If created, then set some flags */
983 if (wapp->app_icon) {
984 WWindow *mainw = wapp->main_window_desc;
986 wapp->app_icon->running = 1;
987 wapp->app_icon->icon->owner = mainw;
988 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
989 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
991 wAppIconPaint(wapp->app_icon, True);
992 save_appicon(wapp->app_icon, True);
996 /* Add the appicon to the appiconlist */
997 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
999 appicon->prev = NULL;
1000 appicon->next = scr->app_icon_list;
1001 if (scr->app_icon_list)
1002 scr->app_icon_list->prev = appicon;
1004 scr->app_icon_list = appicon;
1007 /* Remove the appicon from the appiconlist */
1008 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
1010 if (appicon == scr->app_icon_list) {
1011 if (appicon->next)
1012 appicon->next->prev = NULL;
1013 scr->app_icon_list = appicon->next;
1014 } else {
1015 if (appicon->next)
1016 appicon->next->prev = appicon->prev;
1017 if (appicon->prev)
1018 appicon->prev->next = appicon->next;
1021 appicon->prev = NULL;
1022 appicon->next = NULL;
1025 /* Return the AppIcon associated with a given (Xlib) Window. */
1026 WAppIcon *wAppIconFor(Window window)
1028 WObjDescriptor *desc;
1030 if (window == None)
1031 return NULL;
1033 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
1034 return NULL;
1036 if (desc->parent_type == WCLASS_APPICON || desc->parent_type == WCLASS_DOCK_ICON)
1037 return desc->parent;
1039 return NULL;