Format string bug in WMenuParserError
[wmaker-crm.git] / src / appicon.c
blob795b020d8fcd778f080b6ffa60e891dd891be1a7
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 path = wDefaultGetIconFile(wm_instance, wm_class, True);
131 if (!path && command) {
132 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
133 path = wDefaultGetIconFile(wm_instance, wm_class, False);
136 if (path)
137 path = FindImage(wPreferences.icon_path, path);
139 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
140 if (path)
141 wfree(path);
142 #ifdef XDND
143 wXDNDMakeAwareness(dicon->icon->core->window);
144 #endif
146 /* will be overriden by dock */
147 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
148 dicon->icon->core->descriptor.handle_expose = iconExpose;
149 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
150 dicon->icon->core->descriptor.parent = dicon;
151 AddToStackList(dicon->icon->core);
153 return dicon;
156 void makeAppIconFor(WApplication * wapp)
158 /* If app_icon, work is done, return */
159 if (wapp->app_icon)
160 return;
162 /* Create the icon */
163 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
165 /* Now, paint the icon */
166 if (!WFLAGP(wapp->main_window_desc, no_appicon))
167 paint_app_icon(wapp);
170 void paint_app_icon(WApplication *wapp)
172 WIcon *icon;
173 WScreen *scr = wapp->main_window_desc->screen_ptr;
174 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
175 int x = 0, y = 0;
177 if (!wapp || !wapp->app_icon)
178 return;
180 icon = wapp->app_icon->icon;
181 wapp->app_icon->main_window = wapp->main_window;
183 /* If the icon is docked, don't continue */
184 if (wapp->app_icon->docked)
185 return;
187 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
188 wapp->app_icon->attracted = 1;
189 if (!icon->shadowed) {
190 icon->shadowed = 1;
191 icon->force_paint = 1;
193 wDockAttachIcon(clip, wapp->app_icon, x, y);
194 } else {
195 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
196 wAppIconMove(wapp->app_icon, x, y);
197 wLowerFrame(icon->core);
200 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
201 XMapWindow(dpy, icon->core->window);
203 if (wPreferences.auto_arrange_icons && !wapp->app_icon->attracted)
204 wArrangeIcons(scr, True);
207 void removeAppIconFor(WApplication * wapp)
209 if (!wapp->app_icon)
210 return;
212 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
213 wapp->app_icon->running = 0;
214 /* since we keep it, we don't care if it was attracted or not */
215 wapp->app_icon->attracted = 0;
216 wapp->app_icon->icon->shadowed = 0;
217 wapp->app_icon->main_window = None;
218 wapp->app_icon->pid = 0;
219 wapp->app_icon->icon->owner = NULL;
220 wapp->app_icon->icon->icon_win = None;
221 wapp->app_icon->icon->force_paint = 1;
222 wAppIconPaint(wapp->app_icon);
223 } else if (wapp->app_icon->docked) {
224 wapp->app_icon->running = 0;
225 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
226 } else {
227 wAppIconDestroy(wapp->app_icon);
230 wapp->app_icon = NULL;
232 if (wPreferences.auto_arrange_icons)
233 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
236 static WAppIcon *wAppIconCreate(WWindow * leader_win)
238 WAppIcon *aicon;
239 WScreen *scr = leader_win->screen_ptr;
241 aicon = wmalloc(sizeof(WAppIcon));
242 wretain(aicon);
244 aicon->yindex = -1;
245 aicon->xindex = -1;
247 /* When no_appicon is set we want to avoid having it on the list
248 * because otherwise there will be a hole when the icons are
249 * arranged with wArrangeIcons() */
250 if (!WFLAGP(leader_win, no_appicon))
251 add_to_appicon_list(scr, aicon);
253 if (leader_win->wm_class)
254 aicon->wm_class = wstrdup(leader_win->wm_class);
256 if (leader_win->wm_instance)
257 aicon->wm_instance = wstrdup(leader_win->wm_instance);
259 aicon->icon = wIconCreate(leader_win);
260 #ifdef XDND
261 wXDNDMakeAwareness(aicon->icon->core->window);
262 #endif
264 /* will be overriden if docked */
265 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
266 aicon->icon->core->descriptor.handle_expose = iconExpose;
267 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
268 aicon->icon->core->descriptor.parent = aicon;
269 AddToStackList(aicon->icon->core);
270 aicon->icon->show_title = 0;
271 wIconUpdate(aicon->icon);
273 return aicon;
276 void wAppIconDestroy(WAppIcon * aicon)
278 WScreen *scr = aicon->icon->core->screen_ptr;
280 RemoveFromStackList(aicon->icon->core);
281 wIconDestroy(aicon->icon);
282 if (aicon->command)
283 wfree(aicon->command);
284 #ifdef XDND
285 if (aicon->dnd_command)
286 wfree(aicon->dnd_command);
287 #endif
288 if (aicon->wm_instance)
289 wfree(aicon->wm_instance);
291 if (aicon->wm_class)
292 wfree(aicon->wm_class);
294 remove_from_appicon_list(scr, aicon);
296 aicon->destroyed = 1;
297 wrelease(aicon);
300 static void drawCorner(WIcon * icon)
302 WScreen *scr = icon->core->screen_ptr;
303 XPoint points[3];
305 points[0].x = 1;
306 points[0].y = 1;
307 points[1].x = 12;
308 points[1].y = 1;
309 points[2].x = 1;
310 points[2].y = 12;
311 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
312 points, 3, Convex, CoordModeOrigin);
313 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
314 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
317 void wAppIconMove(WAppIcon * aicon, int x, int y)
319 XMoveWindow(dpy, aicon->icon->core->window, x, y);
320 aicon->x_pos = x;
321 aicon->y_pos = y;
324 #ifdef WS_INDICATOR
325 static void updateDockNumbers(WScreen * scr)
327 int length;
328 char *ws_numbers;
329 WAppIcon *dicon = scr->dock->icon_array[0];
331 ws_numbers = wmalloc(20);
332 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
333 length = strlen(ws_numbers);
335 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
337 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
338 scr->icon_title_font, 4, 3, ws_numbers, length);
340 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
341 scr->icon_title_font, 3, 2, ws_numbers, length);
343 wfree(ws_numbers);
345 #endif /* WS_INDICATOR */
347 void wAppIconPaint(WAppIcon * aicon)
349 WApplication *wapp;
350 WScreen *scr = aicon->icon->core->screen_ptr;
352 if (aicon->icon->owner)
353 wapp = wApplicationOf(aicon->icon->owner->main_window);
354 else
355 wapp = NULL;
357 wIconPaint(aicon->icon);
359 # ifdef WS_INDICATOR
360 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
361 updateDockNumbers(scr);
362 # endif
363 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
364 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
365 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
366 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
367 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
369 #ifdef HIDDENDOT
370 if (wapp && wapp->flags.hidden) {
371 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
372 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
373 XCopyArea(dpy, scr->dock_dots->image,
374 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
376 #endif /* HIDDENDOT */
378 if (aicon->omnipresent)
379 drawCorner(aicon->icon);
381 XSetClipMask(dpy, scr->copy_gc, None);
382 if (aicon->launching)
383 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
384 0, 0, wPreferences.icon_size, wPreferences.icon_size);
387 /* Save the application icon, if it's a dockapp then use it with dock = True */
388 void save_appicon(WAppIcon *aicon, Bool dock)
390 char *path;
392 if (!aicon)
393 return;
395 if (dock && (!aicon->docked || aicon->attracted))
396 return;
398 path = wIconStore(aicon->icon);
399 if (!path)
400 return;
402 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
403 wfree(path);
406 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
408 /* main_window may not have the full command line; try to find one which does */
409 static void relaunchApplication(WApplication *wapp)
411 WScreen *scr;
412 WWindow *wlist, *next;
414 scr = wapp->main_window_desc->screen_ptr;
415 wlist = scr->focused_window;
416 if (! wlist)
417 return;
419 while (wlist->prev)
420 wlist = wlist->prev;
422 while (wlist) {
423 next = wlist->next;
425 if (wlist->main_window == wapp->main_window) {
426 if (RelaunchWindow(wlist))
427 return;
430 wlist = next;
434 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
436 WApplication *wapp = (WApplication *) entry->clientdata;
438 relaunchApplication(wapp);
441 static void hideCallback(WMenu * menu, WMenuEntry * entry)
443 WApplication *wapp = (WApplication *) entry->clientdata;
445 if (wapp->flags.hidden) {
446 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
447 wUnhideApplication(wapp, False, False);
448 } else {
449 wHideApplication(wapp);
453 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
455 WApplication *wapp = (WApplication *) entry->clientdata;
457 wUnhideApplication(wapp, False, True);
460 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
462 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
463 char *file = NULL;
464 WScreen *scr;
465 int result;
467 assert(icon != NULL);
469 if (icon->editing)
470 return;
472 icon->editing = 1;
473 scr = icon->icon->core->screen_ptr;
475 wretain(icon);
477 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
479 if (result && !icon->destroyed) {
480 if (file && *file == 0) {
481 wfree(file);
482 file = NULL;
484 if (!wIconChangeImageFile(icon->icon, file)) {
485 wMessageDialog(scr, _("Error"),
486 _("Could not open specified icon file"), _("OK"), NULL, NULL);
487 } else {
488 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
489 wAppIconPaint(icon);
491 if (file)
492 wfree(file);
494 icon->editing = 0;
495 wrelease(icon);
498 static void killCallback(WMenu * menu, WMenuEntry * entry)
500 WApplication *wapp = (WApplication *) entry->clientdata;
501 WFakeGroupLeader *fPtr;
502 char *buffer;
503 char *shortname;
504 char *basename(const char *shortname);
506 if (!WCHECK_STATE(WSTATE_NORMAL))
507 return;
509 WCHANGE_STATE(WSTATE_MODAL);
511 assert(entry->clientdata != NULL);
513 shortname = basename(wapp->app_icon->wm_instance);
515 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
516 _(" will be forcibly closed.\n"
517 "Any unsaved changes will be lost.\n" "Please confirm."));
519 fPtr = wapp->main_window_desc->fake_group;
521 wretain(wapp->main_window_desc);
522 if (wPreferences.dont_confirm_kill
523 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
524 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
525 if (fPtr != NULL) {
526 WWindow *wwin, *twin;
528 wwin = wapp->main_window_desc->screen_ptr->focused_window;
529 while (wwin) {
530 twin = wwin->prev;
531 if (wwin->fake_group == fPtr)
532 wClientKill(wwin);
533 wwin = twin;
535 } else if (!wapp->main_window_desc->flags.destroyed) {
536 wClientKill(wapp->main_window_desc);
539 wrelease(wapp->main_window_desc);
540 wfree(buffer);
541 WCHANGE_STATE(WSTATE_NORMAL);
544 static WMenu *createApplicationMenu(WScreen * scr)
546 WMenu *menu;
548 menu = wMenuCreate(scr, NULL, False);
549 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
550 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
551 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
552 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
553 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
555 return menu;
558 static void openApplicationMenu(WApplication * wapp, int x, int y)
560 WMenu *menu;
561 WScreen *scr = wapp->main_window_desc->screen_ptr;
562 int i;
564 if (!scr->icon_menu) {
565 scr->icon_menu = createApplicationMenu(scr);
566 wfree(scr->icon_menu->entries[1]->text);
569 menu = scr->icon_menu;
571 if (wapp->flags.hidden)
572 menu->entries[1]->text = _("Unhide");
573 else
574 menu->entries[1]->text = _("Hide");
576 menu->flags.realized = 0;
577 wMenuRealize(menu);
579 x -= menu->frame->core->width / 2;
580 if (x + menu->frame->core->width > scr->scr_width)
581 x = scr->scr_width - menu->frame->core->width;
583 if (x < 0)
584 x = 0;
586 /* set client data */
587 for (i = 0; i < menu->entry_no; i++)
588 menu->entries[i]->clientdata = wapp;
590 wMenuMapAt(menu, x, y, False);
593 /******************************************************************/
595 static void iconExpose(WObjDescriptor * desc, XEvent * event)
597 wAppIconPaint(desc->parent);
600 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
602 WAppIcon *aicon = desc->parent;
603 WApplication *wapp;
604 WScreen *scr = aicon->icon->core->screen_ptr;
605 int unhideHere;
607 assert(aicon->icon->owner != NULL);
609 wapp = wApplicationOf(aicon->icon->owner->main_window);
611 if (event->xbutton.state & ControlMask) {
612 relaunchApplication(wapp);
613 return;
616 unhideHere = (event->xbutton.state & ShiftMask);
617 /* go to the last workspace that the user worked on the app */
618 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
619 wWorkspaceChange(scr, wapp->last_workspace);
621 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
623 if (event->xbutton.state & MOD_MASK)
624 wHideOtherApplications(aicon->icon->owner);
627 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
629 WAppIcon *aicon = desc->parent;
630 WIcon *icon = aicon->icon;
631 XEvent ev;
632 int x = aicon->x_pos, y = aicon->y_pos;
633 int dx = event->xbutton.x, dy = event->xbutton.y;
634 int grabbed = 0;
635 int done = 0;
636 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
637 WScreen *scr = icon->core->screen_ptr;
638 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
639 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
640 int ix, iy;
641 int clickButton = event->xbutton.button;
642 Pixmap ghost = None;
643 Window wins[2];
644 Bool movingSingle = False;
645 int oldX = x;
646 int oldY = y;
647 Bool hasMoved = False;
649 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
650 return;
652 if (IsDoubleClick(scr, event)) {
653 /* Middle or right mouse actions were handled on first click */
654 if (event->xbutton.button == Button1)
655 iconDblClick(desc, event);
656 return;
659 if (event->xbutton.button == Button2) {
660 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
662 if (wapp)
663 relaunchApplication(wapp);
665 return;
668 if (event->xbutton.button == Button3) {
669 WObjDescriptor *desc;
670 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
672 if (!wapp)
673 return;
675 if (event->xbutton.send_event &&
676 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
677 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
678 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
679 wwarning("pointer grab failed for appicon menu");
680 return;
683 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
685 /* allow drag select of menu */
686 desc = &scr->icon_menu->menu->descriptor;
687 event->xbutton.send_event = True;
688 (*desc->handle_mousedown) (desc, event);
689 return;
692 if (event->xbutton.state & MOD_MASK)
693 wLowerFrame(icon->core);
694 else
695 wRaiseFrame(icon->core);
697 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
698 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
699 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
700 wwarning("pointer grab failed for appicon move");
702 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
703 dockable = 0;
704 else
705 dockable = canBeDocked(icon->owner);
707 wins[0] = icon->core->window;
708 wins[1] = scr->dock_shadow;
709 XRestackWindows(dpy, wins, 2);
710 if (superfluous) {
711 if (icon->pixmap != None)
712 ghost = MakeGhostIcon(scr, icon->pixmap);
713 else
714 ghost = MakeGhostIcon(scr, icon->core->window);
715 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
716 XClearWindow(dpy, scr->dock_shadow);
719 while (!done) {
720 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
721 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
722 switch (ev.type) {
723 case Expose:
724 WMHandleEvent(&ev);
725 break;
727 case EnterNotify:
728 /* It means the cursor moved so fast that it entered
729 * something else (if moving slowly, it would have
730 * stayed in the appIcon that is being moved. Ignore
731 * such "spurious" EnterNotifiy's */
732 break;
734 case MotionNotify:
735 hasMoved = True;
736 if (!grabbed) {
737 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
738 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
739 XChangeActivePointerGrab(dpy, ButtonMotionMask
740 | ButtonReleaseMask | ButtonPressMask,
741 wCursor[WCUR_MOVE], CurrentTime);
742 grabbed = 1;
743 } else {
744 break;
747 x = ev.xmotion.x_root - dx;
748 y = ev.xmotion.y_root - dy;
750 if (movingSingle)
751 XMoveWindow(dpy, icon->core->window, x, y);
752 else
753 wAppIconMove(aicon, x, y);
755 if (dockable) {
756 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
757 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
758 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
760 if (scr->last_dock != scr->dock && collapsed) {
761 scr->last_dock->collapsed = 1;
762 wDockHideIcons(scr->last_dock);
763 collapsed = 0;
765 if (!collapsed && (collapsed = scr->dock->collapsed)) {
766 scr->dock->collapsed = 0;
767 wDockShowIcons(scr->dock);
770 if (scr->dock->auto_raise_lower)
771 wDockRaise(scr->dock);
773 scr->last_dock = scr->dock;
775 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
776 if (!docking)
777 XMapWindow(dpy, scr->dock_shadow);
779 docking = 1;
780 } else if (workspace->clip &&
781 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
782 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
783 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
785 if (scr->last_dock != workspace->clip && collapsed) {
786 scr->last_dock->collapsed = 1;
787 wDockHideIcons(scr->last_dock);
788 collapsed = 0;
790 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
791 workspace->clip->collapsed = 0;
792 wDockShowIcons(workspace->clip);
795 if (workspace->clip->auto_raise_lower)
796 wDockRaise(workspace->clip);
798 scr->last_dock = workspace->clip;
800 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
801 if (!docking)
802 XMapWindow(dpy, scr->dock_shadow);
804 docking = 1;
805 } else if (docking) {
806 XUnmapWindow(dpy, scr->dock_shadow);
807 docking = 0;
810 break;
812 case ButtonPress:
813 break;
815 case ButtonRelease:
816 if (ev.xbutton.button != clickButton)
817 break;
818 XUngrabPointer(dpy, CurrentTime);
820 if (docking) {
821 Bool docked;
823 /* icon is trying to be docked */
824 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
825 XUnmapWindow(dpy, scr->dock_shadow);
826 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
827 if (scr->last_dock->auto_collapse)
828 collapsed = 0;
830 if (workspace->clip &&
831 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
832 wDockLower(workspace->clip);
834 if (!docked) {
835 /* If icon could not be docked, slide it back to the old
836 * position */
837 SlideWindow(icon->core->window, x, y, oldX, oldY);
839 } else {
840 if (movingSingle) {
841 /* move back to its place */
842 SlideWindow(icon->core->window, x, y, oldX, oldY);
843 wAppIconMove(aicon, oldX, oldY);
844 } else {
845 XMoveWindow(dpy, icon->core->window, x, y);
846 aicon->x_pos = x;
847 aicon->y_pos = y;
849 if (workspace->clip && workspace->clip->auto_raise_lower)
850 wDockLower(workspace->clip);
852 if (collapsed) {
853 scr->last_dock->collapsed = 1;
854 wDockHideIcons(scr->last_dock);
855 collapsed = 0;
857 if (superfluous) {
858 if (ghost != None)
859 XFreePixmap(dpy, ghost);
860 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
863 if (wPreferences.auto_arrange_icons)
864 wArrangeIcons(scr, True);
866 if (wPreferences.single_click && !hasMoved)
867 iconDblClick(desc, event);
869 done = 1;
870 break;
875 /* This function save the application icon and store the path in the Dictionary */
876 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
878 WMPropList *dict = WDWindowAttributes->dictionary;
879 WMPropList *adict, *key, *iconk;
880 WMPropList *val;
881 char *tmp;
883 tmp = get_name_for_instance_class(wm_instance, wm_class);
884 key = WMCreatePLString(tmp);
885 wfree(tmp);
887 adict = WMGetFromPLDictionary(dict, key);
888 iconk = WMCreatePLString("Icon");
890 if (adict) {
891 val = WMGetFromPLDictionary(adict, iconk);
892 } else {
893 /* no dictionary for app, so create one */
894 adict = WMCreatePLDictionary(NULL, NULL);
895 WMPutInPLDictionary(dict, key, adict);
896 WMReleasePropList(adict);
897 val = NULL;
900 if (!val) {
901 val = WMCreatePLString(iconPath);
902 WMPutInPLDictionary(adict, iconk, val);
903 WMReleasePropList(val);
904 } else {
905 val = NULL;
908 WMReleasePropList(key);
909 WMReleasePropList(iconk);
911 if (val && !wPreferences.flags.noupdates)
912 UpdateDomainFile(WDWindowAttributes);
915 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
917 WAppIcon *aicon = NULL;
919 aicon = wDockFindIconForWindow(dock, main_window);
920 if (!aicon) {
921 wDockTrackWindowLaunch(dock, main_window);
922 aicon = wDockFindIconForWindow(dock, main_window);
924 return aicon;
927 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
929 WScreen *scr = wwin->screen_ptr;
930 wapp->app_icon = NULL;
932 if (scr->last_dock)
933 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
935 /* check main dock if we did not find it in last dock */
936 if (!wapp->app_icon && scr->dock)
937 wapp->app_icon = findDockIconFor(scr->dock, main_window);
939 /* finally check clips */
940 if (!wapp->app_icon) {
941 int i;
942 for (i = 0; i < scr->workspace_count; i++) {
943 WDock *dock = scr->workspaces[i]->clip;
944 if (dock)
945 wapp->app_icon = findDockIconFor(dock, main_window);
946 if (wapp->app_icon)
947 break;
951 /* If created, then set some flags */
952 if (wapp->app_icon) {
953 WWindow *mainw = wapp->main_window_desc;
955 wapp->app_icon->running = 1;
956 wapp->app_icon->icon->force_paint = 1;
957 wapp->app_icon->icon->owner = mainw;
958 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
959 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
961 wAppIconPaint(wapp->app_icon);
962 save_appicon(wapp->app_icon, True);
966 /* Add the appicon to the appiconlist */
967 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
969 appicon->prev = NULL;
970 appicon->next = scr->app_icon_list;
971 if (scr->app_icon_list)
972 scr->app_icon_list->prev = appicon;
974 scr->app_icon_list = appicon;
977 /* Remove the appicon from the appiconlist */
978 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
980 if (appicon == scr->app_icon_list) {
981 if (appicon->next)
982 appicon->next->prev = NULL;
983 scr->app_icon_list = appicon->next;
984 } else {
985 if (appicon->next)
986 appicon->next->prev = appicon->prev;
987 if (appicon->prev)
988 appicon->prev->next = appicon->next;