noDefault changed to default_icon
[wmaker-crm.git] / src / appicon.c
blobe3672ff343cd8aac8169d0a76bba88bc8aaa2e7d
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;
64 #define MOD_MASK wPreferences.modifier_mask
66 void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
67 static void iconDblClick(WObjDescriptor * desc, XEvent * event);
68 static void iconExpose(WObjDescriptor * desc, XEvent * event);
69 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class);
70 static WAppIcon *wAppIconCreate(WWindow * leader_win);
71 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon);
72 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon);
74 /* This function is used if the application is a .app. It checks if it has an icon in it
75 * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
77 void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
79 char *iconPath = NULL;
80 char *tmp = NULL;
82 if (strstr(path, ".app")) {
83 tmp = wmalloc(strlen(path) + 16);
85 if (scr->flags.supports_tiff) {
86 strcpy(tmp, path);
87 strcat(tmp, ".tiff");
88 if (access(tmp, R_OK) == 0)
89 iconPath = tmp;
92 if (!iconPath) {
93 strcpy(tmp, path);
94 strcat(tmp, ".xpm");
95 if (access(tmp, R_OK) == 0)
96 iconPath = tmp;
99 if (!iconPath)
100 wfree(tmp);
102 if (iconPath) {
103 wApplicationSaveIconPathFor(iconPath, wm_instance, wm_class);
104 wfree(iconPath);
109 WAppIcon *wAppIconCreateForDock(WScreen * scr, char *command, char *wm_instance, char *wm_class, int tile)
111 WAppIcon *dicon;
112 char *path;
114 dicon = wmalloc(sizeof(WAppIcon));
115 wretain(dicon);
116 dicon->yindex = -1;
117 dicon->xindex = -1;
119 add_to_appicon_list(scr, dicon);
121 if (command)
122 dicon->command = wstrdup(command);
124 if (wm_class)
125 dicon->wm_class = wstrdup(wm_class);
127 if (wm_instance)
128 dicon->wm_instance = wstrdup(wm_instance);
130 /* Search the icon using instance and class, without default icon */
131 path = wDefaultGetIconFile(wm_instance, wm_class, False);
132 if (!path && command) {
133 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
134 /* Search again, now with default icon */
135 path = wDefaultGetIconFile(wm_instance, wm_class, True);
138 if (path)
139 path = FindImage(wPreferences.icon_path, path);
141 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
142 if (path)
143 wfree(path);
144 #ifdef XDND
145 wXDNDMakeAwareness(dicon->icon->core->window);
146 #endif
148 /* will be overriden by dock */
149 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
150 dicon->icon->core->descriptor.handle_expose = iconExpose;
151 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
152 dicon->icon->core->descriptor.parent = dicon;
153 AddToStackList(dicon->icon->core);
155 return dicon;
158 void makeAppIconFor(WApplication * wapp)
160 /* If app_icon, work is done, return */
161 if (wapp->app_icon)
162 return;
164 /* Create the icon */
165 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
167 /* Now, paint the icon */
168 if (!WFLAGP(wapp->main_window_desc, no_appicon))
169 paint_app_icon(wapp);
172 void paint_app_icon(WApplication *wapp)
174 WIcon *icon;
175 WScreen *scr = wapp->main_window_desc->screen_ptr;
176 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
177 int x = 0, y = 0;
179 if (!wapp || !wapp->app_icon)
180 return;
182 icon = wapp->app_icon->icon;
183 wapp->app_icon->main_window = wapp->main_window;
185 /* If the icon is docked, don't continue */
186 if (wapp->app_icon->docked)
187 return;
189 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
190 wapp->app_icon->attracted = 1;
191 if (!icon->shadowed) {
192 icon->shadowed = 1;
193 icon->force_paint = 1;
195 wDockAttachIcon(clip, wapp->app_icon, x, y);
196 } else {
197 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
198 wAppIconMove(wapp->app_icon, x, y);
199 wLowerFrame(icon->core);
202 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
203 XMapWindow(dpy, icon->core->window);
205 if (wPreferences.auto_arrange_icons && !wapp->app_icon->attracted)
206 wArrangeIcons(scr, True);
209 void removeAppIconFor(WApplication * wapp)
211 if (!wapp->app_icon)
212 return;
214 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
215 wapp->app_icon->running = 0;
216 /* since we keep it, we don't care if it was attracted or not */
217 wapp->app_icon->attracted = 0;
218 wapp->app_icon->icon->shadowed = 0;
219 wapp->app_icon->main_window = None;
220 wapp->app_icon->pid = 0;
221 wapp->app_icon->icon->owner = NULL;
222 wapp->app_icon->icon->icon_win = None;
223 wapp->app_icon->icon->force_paint = 1;
224 wAppIconPaint(wapp->app_icon);
225 } else if (wapp->app_icon->docked) {
226 wapp->app_icon->running = 0;
227 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
228 } else {
229 wAppIconDestroy(wapp->app_icon);
232 wapp->app_icon = NULL;
234 if (wPreferences.auto_arrange_icons)
235 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
238 static WAppIcon *wAppIconCreate(WWindow * leader_win)
240 WAppIcon *aicon;
241 WScreen *scr = leader_win->screen_ptr;
243 aicon = wmalloc(sizeof(WAppIcon));
244 wretain(aicon);
246 aicon->yindex = -1;
247 aicon->xindex = -1;
249 /* When no_appicon is set we want to avoid having it on the list
250 * because otherwise there will be a hole when the icons are
251 * arranged with wArrangeIcons() */
252 if (!WFLAGP(leader_win, no_appicon))
253 add_to_appicon_list(scr, aicon);
255 if (leader_win->wm_class)
256 aicon->wm_class = wstrdup(leader_win->wm_class);
258 if (leader_win->wm_instance)
259 aicon->wm_instance = wstrdup(leader_win->wm_instance);
261 aicon->icon = wIconCreate(leader_win);
262 #ifdef XDND
263 wXDNDMakeAwareness(aicon->icon->core->window);
264 #endif
266 /* will be overriden if docked */
267 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
268 aicon->icon->core->descriptor.handle_expose = iconExpose;
269 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
270 aicon->icon->core->descriptor.parent = aicon;
271 AddToStackList(aicon->icon->core);
272 aicon->icon->show_title = 0;
273 wIconUpdate(aicon->icon);
275 return aicon;
278 void wAppIconDestroy(WAppIcon * aicon)
280 WScreen *scr = aicon->icon->core->screen_ptr;
282 RemoveFromStackList(aicon->icon->core);
283 wIconDestroy(aicon->icon);
284 if (aicon->command)
285 wfree(aicon->command);
286 #ifdef XDND
287 if (aicon->dnd_command)
288 wfree(aicon->dnd_command);
289 #endif
290 if (aicon->wm_instance)
291 wfree(aicon->wm_instance);
293 if (aicon->wm_class)
294 wfree(aicon->wm_class);
296 remove_from_appicon_list(scr, aicon);
298 aicon->destroyed = 1;
299 wrelease(aicon);
302 static void drawCorner(WIcon * icon)
304 WScreen *scr = icon->core->screen_ptr;
305 XPoint points[3];
307 points[0].x = 1;
308 points[0].y = 1;
309 points[1].x = 12;
310 points[1].y = 1;
311 points[2].x = 1;
312 points[2].y = 12;
313 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
314 points, 3, Convex, CoordModeOrigin);
315 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
316 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
319 void wAppIconMove(WAppIcon * aicon, int x, int y)
321 XMoveWindow(dpy, aicon->icon->core->window, x, y);
322 aicon->x_pos = x;
323 aicon->y_pos = y;
326 #ifdef WS_INDICATOR
327 static void updateDockNumbers(WScreen * scr)
329 int length;
330 char *ws_numbers;
331 WAppIcon *dicon = scr->dock->icon_array[0];
333 ws_numbers = wmalloc(20);
334 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
335 length = strlen(ws_numbers);
337 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
339 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
340 scr->icon_title_font, 4, 3, ws_numbers, length);
342 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
343 scr->icon_title_font, 3, 2, ws_numbers, length);
345 wfree(ws_numbers);
347 #endif /* WS_INDICATOR */
349 void wAppIconPaint(WAppIcon * aicon)
351 WApplication *wapp;
352 WScreen *scr = aicon->icon->core->screen_ptr;
354 if (aicon->icon->owner)
355 wapp = wApplicationOf(aicon->icon->owner->main_window);
356 else
357 wapp = NULL;
359 wIconPaint(aicon->icon);
361 # ifdef WS_INDICATOR
362 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
363 updateDockNumbers(scr);
364 # endif
365 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
366 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
367 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
368 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
369 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
371 #ifdef HIDDENDOT
372 if (wapp && wapp->flags.hidden) {
373 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
374 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
375 XCopyArea(dpy, scr->dock_dots->image,
376 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
378 #endif /* HIDDENDOT */
380 if (aicon->omnipresent)
381 drawCorner(aicon->icon);
383 XSetClipMask(dpy, scr->copy_gc, None);
384 if (aicon->launching)
385 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
386 0, 0, wPreferences.icon_size, wPreferences.icon_size);
389 /* Save the application icon, if it's a dockapp then use it with dock = True */
390 void save_appicon(WAppIcon *aicon, Bool dock)
392 char *path;
394 if (!aicon)
395 return;
397 if (dock && (!aicon->docked || aicon->attracted))
398 return;
400 path = wIconStore(aicon->icon);
401 if (!path)
402 return;
404 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
405 wfree(path);
408 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
410 /* main_window may not have the full command line; try to find one which does */
411 static void relaunchApplication(WApplication *wapp)
413 WScreen *scr;
414 WWindow *wlist, *next;
416 scr = wapp->main_window_desc->screen_ptr;
417 wlist = scr->focused_window;
418 if (! wlist)
419 return;
421 while (wlist->prev)
422 wlist = wlist->prev;
424 while (wlist) {
425 next = wlist->next;
427 if (wlist->main_window == wapp->main_window) {
428 if (RelaunchWindow(wlist))
429 return;
432 wlist = next;
436 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
438 WApplication *wapp = (WApplication *) entry->clientdata;
440 relaunchApplication(wapp);
443 static void hideCallback(WMenu * menu, WMenuEntry * entry)
445 WApplication *wapp = (WApplication *) entry->clientdata;
447 if (wapp->flags.hidden) {
448 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
449 wUnhideApplication(wapp, False, False);
450 } else {
451 wHideApplication(wapp);
455 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
457 WApplication *wapp = (WApplication *) entry->clientdata;
459 wUnhideApplication(wapp, False, True);
462 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
464 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
465 char *file = NULL;
466 WScreen *scr;
467 int result;
469 assert(icon != NULL);
471 if (icon->editing)
472 return;
474 icon->editing = 1;
475 scr = icon->icon->core->screen_ptr;
477 wretain(icon);
479 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
481 if (result && !icon->destroyed) {
482 if (file && *file == 0) {
483 wfree(file);
484 file = NULL;
486 if (!wIconChangeImageFile(icon->icon, file)) {
487 wMessageDialog(scr, _("Error"),
488 _("Could not open specified icon file"), _("OK"), NULL, NULL);
489 } else {
490 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
491 wAppIconPaint(icon);
493 if (file)
494 wfree(file);
496 icon->editing = 0;
497 wrelease(icon);
500 static void killCallback(WMenu * menu, WMenuEntry * entry)
502 WApplication *wapp = (WApplication *) entry->clientdata;
503 WFakeGroupLeader *fPtr;
504 char *buffer;
505 char *shortname;
506 char *basename(const char *shortname);
508 if (!WCHECK_STATE(WSTATE_NORMAL))
509 return;
511 WCHANGE_STATE(WSTATE_MODAL);
513 assert(entry->clientdata != NULL);
515 shortname = basename(wapp->app_icon->wm_instance);
517 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
518 _(" will be forcibly closed.\n"
519 "Any unsaved changes will be lost.\n" "Please confirm."));
521 fPtr = wapp->main_window_desc->fake_group;
523 wretain(wapp->main_window_desc);
524 if (wPreferences.dont_confirm_kill
525 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
526 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
527 if (fPtr != NULL) {
528 WWindow *wwin, *twin;
530 wwin = wapp->main_window_desc->screen_ptr->focused_window;
531 while (wwin) {
532 twin = wwin->prev;
533 if (wwin->fake_group == fPtr)
534 wClientKill(wwin);
535 wwin = twin;
537 } else if (!wapp->main_window_desc->flags.destroyed) {
538 wClientKill(wapp->main_window_desc);
541 wrelease(wapp->main_window_desc);
542 wfree(buffer);
543 WCHANGE_STATE(WSTATE_NORMAL);
546 static WMenu *createApplicationMenu(WScreen * scr)
548 WMenu *menu;
550 menu = wMenuCreate(scr, NULL, False);
551 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
552 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
553 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
554 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
555 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
557 return menu;
560 static void openApplicationMenu(WApplication * wapp, int x, int y)
562 WMenu *menu;
563 WScreen *scr = wapp->main_window_desc->screen_ptr;
564 int i;
566 if (!scr->icon_menu) {
567 scr->icon_menu = createApplicationMenu(scr);
568 wfree(scr->icon_menu->entries[1]->text);
571 menu = scr->icon_menu;
573 if (wapp->flags.hidden)
574 menu->entries[1]->text = _("Unhide");
575 else
576 menu->entries[1]->text = _("Hide");
578 menu->flags.realized = 0;
579 wMenuRealize(menu);
581 x -= menu->frame->core->width / 2;
582 if (x + menu->frame->core->width > scr->scr_width)
583 x = scr->scr_width - menu->frame->core->width;
585 if (x < 0)
586 x = 0;
588 /* set client data */
589 for (i = 0; i < menu->entry_no; i++)
590 menu->entries[i]->clientdata = wapp;
592 wMenuMapAt(menu, x, y, False);
595 /******************************************************************/
597 static void iconExpose(WObjDescriptor * desc, XEvent * event)
599 wAppIconPaint(desc->parent);
602 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
604 WAppIcon *aicon = desc->parent;
605 WApplication *wapp;
606 WScreen *scr = aicon->icon->core->screen_ptr;
607 int unhideHere;
609 assert(aicon->icon->owner != NULL);
611 wapp = wApplicationOf(aicon->icon->owner->main_window);
613 if (event->xbutton.state & ControlMask) {
614 relaunchApplication(wapp);
615 return;
618 unhideHere = (event->xbutton.state & ShiftMask);
619 /* go to the last workspace that the user worked on the app */
620 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
621 wWorkspaceChange(scr, wapp->last_workspace);
623 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
625 if (event->xbutton.state & MOD_MASK)
626 wHideOtherApplications(aicon->icon->owner);
629 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
631 WAppIcon *aicon = desc->parent;
632 WIcon *icon = aicon->icon;
633 XEvent ev;
634 int x = aicon->x_pos, y = aicon->y_pos;
635 int dx = event->xbutton.x, dy = event->xbutton.y;
636 int grabbed = 0;
637 int done = 0;
638 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
639 WScreen *scr = icon->core->screen_ptr;
640 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
641 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
642 int ix, iy;
643 int clickButton = event->xbutton.button;
644 Pixmap ghost = None;
645 Window wins[2];
646 Bool movingSingle = False;
647 int oldX = x;
648 int oldY = y;
649 Bool hasMoved = False;
651 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
652 return;
654 if (IsDoubleClick(scr, event)) {
655 /* Middle or right mouse actions were handled on first click */
656 if (event->xbutton.button == Button1)
657 iconDblClick(desc, event);
658 return;
661 if (event->xbutton.button == Button2) {
662 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
664 if (wapp)
665 relaunchApplication(wapp);
667 return;
670 if (event->xbutton.button == Button3) {
671 WObjDescriptor *desc;
672 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
674 if (!wapp)
675 return;
677 if (event->xbutton.send_event &&
678 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
679 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
680 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
681 wwarning("pointer grab failed for appicon menu");
682 return;
685 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
687 /* allow drag select of menu */
688 desc = &scr->icon_menu->menu->descriptor;
689 event->xbutton.send_event = True;
690 (*desc->handle_mousedown) (desc, event);
691 return;
694 if (event->xbutton.state & MOD_MASK)
695 wLowerFrame(icon->core);
696 else
697 wRaiseFrame(icon->core);
699 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
700 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
701 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
702 wwarning("pointer grab failed for appicon move");
704 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
705 dockable = 0;
706 else
707 dockable = canBeDocked(icon->owner);
709 wins[0] = icon->core->window;
710 wins[1] = scr->dock_shadow;
711 XRestackWindows(dpy, wins, 2);
712 if (superfluous) {
713 if (icon->pixmap != None)
714 ghost = MakeGhostIcon(scr, icon->pixmap);
715 else
716 ghost = MakeGhostIcon(scr, icon->core->window);
717 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
718 XClearWindow(dpy, scr->dock_shadow);
721 while (!done) {
722 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
723 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
724 switch (ev.type) {
725 case Expose:
726 WMHandleEvent(&ev);
727 break;
729 case EnterNotify:
730 /* It means the cursor moved so fast that it entered
731 * something else (if moving slowly, it would have
732 * stayed in the appIcon that is being moved. Ignore
733 * such "spurious" EnterNotifiy's */
734 break;
736 case MotionNotify:
737 hasMoved = True;
738 if (!grabbed) {
739 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
740 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
741 XChangeActivePointerGrab(dpy, ButtonMotionMask
742 | ButtonReleaseMask | ButtonPressMask,
743 wCursor[WCUR_MOVE], CurrentTime);
744 grabbed = 1;
745 } else {
746 break;
749 x = ev.xmotion.x_root - dx;
750 y = ev.xmotion.y_root - dy;
752 if (movingSingle)
753 XMoveWindow(dpy, icon->core->window, x, y);
754 else
755 wAppIconMove(aicon, x, y);
757 if (dockable) {
758 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
759 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
760 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
762 if (scr->last_dock != scr->dock && collapsed) {
763 scr->last_dock->collapsed = 1;
764 wDockHideIcons(scr->last_dock);
765 collapsed = 0;
767 if (!collapsed && (collapsed = scr->dock->collapsed)) {
768 scr->dock->collapsed = 0;
769 wDockShowIcons(scr->dock);
772 if (scr->dock->auto_raise_lower)
773 wDockRaise(scr->dock);
775 scr->last_dock = scr->dock;
777 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
778 if (!docking)
779 XMapWindow(dpy, scr->dock_shadow);
781 docking = 1;
782 } else if (workspace->clip &&
783 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
784 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
785 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
787 if (scr->last_dock != workspace->clip && collapsed) {
788 scr->last_dock->collapsed = 1;
789 wDockHideIcons(scr->last_dock);
790 collapsed = 0;
792 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
793 workspace->clip->collapsed = 0;
794 wDockShowIcons(workspace->clip);
797 if (workspace->clip->auto_raise_lower)
798 wDockRaise(workspace->clip);
800 scr->last_dock = workspace->clip;
802 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
803 if (!docking)
804 XMapWindow(dpy, scr->dock_shadow);
806 docking = 1;
807 } else if (docking) {
808 XUnmapWindow(dpy, scr->dock_shadow);
809 docking = 0;
812 break;
814 case ButtonPress:
815 break;
817 case ButtonRelease:
818 if (ev.xbutton.button != clickButton)
819 break;
820 XUngrabPointer(dpy, CurrentTime);
822 if (docking) {
823 Bool docked;
825 /* icon is trying to be docked */
826 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
827 XUnmapWindow(dpy, scr->dock_shadow);
828 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
829 if (scr->last_dock->auto_collapse)
830 collapsed = 0;
832 if (workspace->clip &&
833 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
834 wDockLower(workspace->clip);
836 if (!docked) {
837 /* If icon could not be docked, slide it back to the old
838 * position */
839 SlideWindow(icon->core->window, x, y, oldX, oldY);
841 } else {
842 if (movingSingle) {
843 /* move back to its place */
844 SlideWindow(icon->core->window, x, y, oldX, oldY);
845 wAppIconMove(aicon, oldX, oldY);
846 } else {
847 XMoveWindow(dpy, icon->core->window, x, y);
848 aicon->x_pos = x;
849 aicon->y_pos = y;
851 if (workspace->clip && workspace->clip->auto_raise_lower)
852 wDockLower(workspace->clip);
854 if (collapsed) {
855 scr->last_dock->collapsed = 1;
856 wDockHideIcons(scr->last_dock);
857 collapsed = 0;
859 if (superfluous) {
860 if (ghost != None)
861 XFreePixmap(dpy, ghost);
862 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
865 if (wPreferences.auto_arrange_icons)
866 wArrangeIcons(scr, True);
868 if (wPreferences.single_click && !hasMoved)
869 iconDblClick(desc, event);
871 done = 1;
872 break;
877 /* This function save the application icon and store the path in the Dictionary */
878 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
880 WMPropList *dict = WDWindowAttributes->dictionary;
881 WMPropList *adict, *key, *iconk;
882 WMPropList *val;
883 char *tmp;
885 tmp = get_name_for_instance_class(wm_instance, wm_class);
886 key = WMCreatePLString(tmp);
887 wfree(tmp);
889 adict = WMGetFromPLDictionary(dict, key);
890 iconk = WMCreatePLString("Icon");
892 if (adict) {
893 val = WMGetFromPLDictionary(adict, iconk);
894 } else {
895 /* no dictionary for app, so create one */
896 adict = WMCreatePLDictionary(NULL, NULL);
897 WMPutInPLDictionary(dict, key, adict);
898 WMReleasePropList(adict);
899 val = NULL;
902 if (!val) {
903 val = WMCreatePLString(iconPath);
904 WMPutInPLDictionary(adict, iconk, val);
905 WMReleasePropList(val);
906 } else {
907 val = NULL;
910 WMReleasePropList(key);
911 WMReleasePropList(iconk);
913 if (val && !wPreferences.flags.noupdates)
914 UpdateDomainFile(WDWindowAttributes);
917 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
919 WAppIcon *aicon = NULL;
921 aicon = wDockFindIconForWindow(dock, main_window);
922 if (!aicon) {
923 wDockTrackWindowLaunch(dock, main_window);
924 aicon = wDockFindIconForWindow(dock, main_window);
926 return aicon;
929 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
931 WScreen *scr = wwin->screen_ptr;
932 wapp->app_icon = NULL;
934 if (scr->last_dock)
935 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
937 /* check main dock if we did not find it in last dock */
938 if (!wapp->app_icon && scr->dock)
939 wapp->app_icon = findDockIconFor(scr->dock, main_window);
941 /* finally check clips */
942 if (!wapp->app_icon) {
943 int i;
944 for (i = 0; i < scr->workspace_count; i++) {
945 WDock *dock = scr->workspaces[i]->clip;
946 if (dock)
947 wapp->app_icon = findDockIconFor(dock, main_window);
948 if (wapp->app_icon)
949 break;
953 /* If created, then set some flags */
954 if (wapp->app_icon) {
955 WWindow *mainw = wapp->main_window_desc;
957 wapp->app_icon->running = 1;
958 wapp->app_icon->icon->force_paint = 1;
959 wapp->app_icon->icon->owner = mainw;
960 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
961 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
963 wAppIconPaint(wapp->app_icon);
964 save_appicon(wapp->app_icon, True);
968 /* Add the appicon to the appiconlist */
969 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
971 appicon->prev = NULL;
972 appicon->next = scr->app_icon_list;
973 if (scr->app_icon_list)
974 scr->app_icon_list->prev = appicon;
976 scr->app_icon_list = appicon;
979 /* Remove the appicon from the appiconlist */
980 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
982 if (appicon == scr->app_icon_list) {
983 if (appicon->next)
984 appicon->next->prev = NULL;
985 scr->app_icon_list = appicon->next;
986 } else {
987 if (appicon->next)
988 appicon->next->prev = appicon->prev;
989 if (appicon->prev)
990 appicon->prev->next = appicon->next;