wIcon* functions renamed
[wmaker-crm.git] / src / appicon.c
blob45911b829bf964dfca6a5b84dfb62a1fc1c411f5
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 *aicon;
112 char *path;
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 /* Search the icon using instance and class, without default icon */
131 path = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
133 aicon->icon = icon_create_for_dock(scr, path, tile);
134 if (path)
135 wfree(path);
136 #ifdef XDND
137 wXDNDMakeAwareness(aicon->icon->core->window);
138 #endif
140 /* will be overriden by dock */
141 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
142 aicon->icon->core->descriptor.handle_expose = iconExpose;
143 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
144 aicon->icon->core->descriptor.parent = aicon;
145 AddToStackList(aicon->icon->core);
147 return aicon;
150 void makeAppIconFor(WApplication *wapp)
152 /* If app_icon, work is done, return */
153 if (wapp->app_icon)
154 return;
156 /* Create the icon */
157 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
158 wIconUpdate(wapp->app_icon->icon);
160 /* Now, paint the icon */
161 if (!WFLAGP(wapp->main_window_desc, no_appicon))
162 paint_app_icon(wapp);
165 void paint_app_icon(WApplication *wapp)
167 WIcon *icon;
168 WScreen *scr = wapp->main_window_desc->screen_ptr;
169 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
170 int x = 0, y = 0;
172 if (!wapp || !wapp->app_icon)
173 return;
175 icon = wapp->app_icon->icon;
176 wapp->app_icon->main_window = wapp->main_window;
178 /* If the icon is docked, don't continue */
179 if (wapp->app_icon->docked)
180 return;
182 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
183 wapp->app_icon->attracted = 1;
184 if (!icon->shadowed) {
185 icon->shadowed = 1;
186 icon->force_paint = 1;
188 wDockAttachIcon(clip, wapp->app_icon, x, y);
189 } else {
190 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
191 wAppIconMove(wapp->app_icon, x, y);
192 wLowerFrame(icon->core);
195 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
196 XMapWindow(dpy, icon->core->window);
198 if (wPreferences.auto_arrange_icons && !wapp->app_icon->attracted)
199 wArrangeIcons(scr, True);
202 void removeAppIconFor(WApplication * wapp)
204 if (!wapp->app_icon)
205 return;
207 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
208 wapp->app_icon->running = 0;
209 /* since we keep it, we don't care if it was attracted or not */
210 wapp->app_icon->attracted = 0;
211 wapp->app_icon->icon->shadowed = 0;
212 wapp->app_icon->main_window = None;
213 wapp->app_icon->pid = 0;
214 wapp->app_icon->icon->owner = NULL;
215 wapp->app_icon->icon->icon_win = None;
216 wapp->app_icon->icon->force_paint = 1;
217 wAppIconPaint(wapp->app_icon);
218 } else if (wapp->app_icon->docked) {
219 wapp->app_icon->running = 0;
220 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
221 } else {
222 wAppIconDestroy(wapp->app_icon);
225 wapp->app_icon = NULL;
227 if (wPreferences.auto_arrange_icons)
228 wArrangeIcons(wapp->main_window_desc->screen_ptr, True);
231 static WAppIcon *wAppIconCreate(WWindow *leader_win)
233 WAppIcon *aicon;
234 WScreen *scr = leader_win->screen_ptr;
236 aicon = wmalloc(sizeof(WAppIcon));
237 wretain(aicon);
238 aicon->yindex = -1;
239 aicon->xindex = -1;
241 /* When no_appicon is set we want to avoid having it on the list
242 * because otherwise there will be a hole when the icons are
243 * arranged with wArrangeIcons() */
244 if (!WFLAGP(leader_win, no_appicon))
245 add_to_appicon_list(scr, aicon);
247 if (leader_win->wm_class)
248 aicon->wm_class = wstrdup(leader_win->wm_class);
250 if (leader_win->wm_instance)
251 aicon->wm_instance = wstrdup(leader_win->wm_instance);
253 aicon->icon = icon_create_for_wwindow(leader_win);
254 #ifdef XDND
255 wXDNDMakeAwareness(aicon->icon->core->window);
256 #endif
258 /* will be overriden if docked */
259 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
260 aicon->icon->core->descriptor.handle_expose = iconExpose;
261 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
262 aicon->icon->core->descriptor.parent = aicon;
263 AddToStackList(aicon->icon->core);
264 aicon->icon->show_title = 0;
266 return aicon;
269 void wAppIconDestroy(WAppIcon * aicon)
271 WScreen *scr = aicon->icon->core->screen_ptr;
273 RemoveFromStackList(aicon->icon->core);
274 wIconDestroy(aicon->icon);
275 if (aicon->command)
276 wfree(aicon->command);
277 #ifdef XDND
278 if (aicon->dnd_command)
279 wfree(aicon->dnd_command);
280 #endif
281 if (aicon->wm_instance)
282 wfree(aicon->wm_instance);
284 if (aicon->wm_class)
285 wfree(aicon->wm_class);
287 remove_from_appicon_list(scr, aicon);
289 aicon->destroyed = 1;
290 wrelease(aicon);
293 static void drawCorner(WIcon * icon)
295 WScreen *scr = icon->core->screen_ptr;
296 XPoint points[3];
298 points[0].x = 1;
299 points[0].y = 1;
300 points[1].x = 12;
301 points[1].y = 1;
302 points[2].x = 1;
303 points[2].y = 12;
304 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
305 points, 3, Convex, CoordModeOrigin);
306 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
307 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
310 void wAppIconMove(WAppIcon * aicon, int x, int y)
312 XMoveWindow(dpy, aicon->icon->core->window, x, y);
313 aicon->x_pos = x;
314 aicon->y_pos = y;
317 #ifdef WS_INDICATOR
318 static void updateDockNumbers(WScreen * scr)
320 int length;
321 char *ws_numbers;
322 WAppIcon *dicon = scr->dock->icon_array[0];
324 ws_numbers = wmalloc(20);
325 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
326 length = strlen(ws_numbers);
328 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
330 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
331 scr->icon_title_font, 4, 3, ws_numbers, length);
333 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
334 scr->icon_title_font, 3, 2, ws_numbers, length);
336 wfree(ws_numbers);
338 #endif /* WS_INDICATOR */
340 void wAppIconPaint(WAppIcon * aicon)
342 WApplication *wapp;
343 WScreen *scr = aicon->icon->core->screen_ptr;
345 if (aicon->icon->owner)
346 wapp = wApplicationOf(aicon->icon->owner->main_window);
347 else
348 wapp = NULL;
350 wIconPaint(aicon->icon);
352 # ifdef WS_INDICATOR
353 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
354 updateDockNumbers(scr);
355 # endif
356 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
357 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
358 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
359 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
360 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
362 #ifdef HIDDENDOT
363 if (wapp && wapp->flags.hidden) {
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,
367 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
369 #endif /* HIDDENDOT */
371 if (aicon->omnipresent)
372 drawCorner(aicon->icon);
374 XSetClipMask(dpy, scr->copy_gc, None);
375 if (aicon->launching)
376 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
377 0, 0, wPreferences.icon_size, wPreferences.icon_size);
380 /* Save the application icon, if it's a dockapp then use it with dock = True */
381 void save_appicon(WAppIcon *aicon, Bool dock)
383 char *path;
385 if (!aicon)
386 return;
388 if (dock && (!aicon->docked || aicon->attracted))
389 return;
391 path = wIconStore(aicon->icon);
392 if (!path)
393 return;
395 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
396 wfree(path);
399 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
401 /* main_window may not have the full command line; try to find one which does */
402 static void relaunchApplication(WApplication *wapp)
404 WScreen *scr;
405 WWindow *wlist, *next;
407 scr = wapp->main_window_desc->screen_ptr;
408 wlist = scr->focused_window;
409 if (! wlist)
410 return;
412 while (wlist->prev)
413 wlist = wlist->prev;
415 while (wlist) {
416 next = wlist->next;
418 if (wlist->main_window == wapp->main_window) {
419 if (RelaunchWindow(wlist))
420 return;
423 wlist = next;
427 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
429 WApplication *wapp = (WApplication *) entry->clientdata;
431 relaunchApplication(wapp);
434 static void hideCallback(WMenu * menu, WMenuEntry * entry)
436 WApplication *wapp = (WApplication *) entry->clientdata;
438 if (wapp->flags.hidden) {
439 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
440 wUnhideApplication(wapp, False, False);
441 } else {
442 wHideApplication(wapp);
446 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
448 WApplication *wapp = (WApplication *) entry->clientdata;
450 wUnhideApplication(wapp, False, True);
453 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
455 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
456 char *file = NULL;
457 WScreen *scr;
458 int result;
460 assert(icon != NULL);
462 if (icon->editing)
463 return;
465 icon->editing = 1;
466 scr = icon->icon->core->screen_ptr;
468 wretain(icon);
470 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
472 if (result && !icon->destroyed) {
473 if (file && *file == 0) {
474 wfree(file);
475 file = NULL;
477 if (!wIconChangeImageFile(icon->icon, file)) {
478 wMessageDialog(scr, _("Error"),
479 _("Could not open specified icon file"), _("OK"), NULL, NULL);
480 } else {
481 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
482 wAppIconPaint(icon);
484 if (file)
485 wfree(file);
487 icon->editing = 0;
488 wrelease(icon);
491 static void killCallback(WMenu * menu, WMenuEntry * entry)
493 WApplication *wapp = (WApplication *) entry->clientdata;
494 WFakeGroupLeader *fPtr;
495 char *buffer;
496 char *shortname;
497 char *basename(const char *shortname);
499 if (!WCHECK_STATE(WSTATE_NORMAL))
500 return;
502 WCHANGE_STATE(WSTATE_MODAL);
504 assert(entry->clientdata != NULL);
506 shortname = basename(wapp->app_icon->wm_instance);
508 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
509 _(" will be forcibly closed.\n"
510 "Any unsaved changes will be lost.\n" "Please confirm."));
512 fPtr = wapp->main_window_desc->fake_group;
514 wretain(wapp->main_window_desc);
515 if (wPreferences.dont_confirm_kill
516 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
517 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
518 if (fPtr != NULL) {
519 WWindow *wwin, *twin;
521 wwin = wapp->main_window_desc->screen_ptr->focused_window;
522 while (wwin) {
523 twin = wwin->prev;
524 if (wwin->fake_group == fPtr)
525 wClientKill(wwin);
526 wwin = twin;
528 } else if (!wapp->main_window_desc->flags.destroyed) {
529 wClientKill(wapp->main_window_desc);
532 wrelease(wapp->main_window_desc);
533 wfree(buffer);
534 WCHANGE_STATE(WSTATE_NORMAL);
537 static WMenu *createApplicationMenu(WScreen * scr)
539 WMenu *menu;
541 menu = wMenuCreate(scr, NULL, False);
542 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
543 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
544 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
545 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
546 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
548 return menu;
551 static void openApplicationMenu(WApplication * wapp, int x, int y)
553 WMenu *menu;
554 WScreen *scr = wapp->main_window_desc->screen_ptr;
555 int i;
557 if (!scr->icon_menu) {
558 scr->icon_menu = createApplicationMenu(scr);
559 wfree(scr->icon_menu->entries[1]->text);
562 menu = scr->icon_menu;
564 if (wapp->flags.hidden)
565 menu->entries[1]->text = _("Unhide");
566 else
567 menu->entries[1]->text = _("Hide");
569 menu->flags.realized = 0;
570 wMenuRealize(menu);
572 x -= menu->frame->core->width / 2;
573 if (x + menu->frame->core->width > scr->scr_width)
574 x = scr->scr_width - menu->frame->core->width;
576 if (x < 0)
577 x = 0;
579 /* set client data */
580 for (i = 0; i < menu->entry_no; i++)
581 menu->entries[i]->clientdata = wapp;
583 wMenuMapAt(menu, x, y, False);
586 /******************************************************************/
588 static void iconExpose(WObjDescriptor * desc, XEvent * event)
590 wAppIconPaint(desc->parent);
593 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
595 WAppIcon *aicon = desc->parent;
596 WApplication *wapp;
597 WScreen *scr = aicon->icon->core->screen_ptr;
598 int unhideHere;
600 assert(aicon->icon->owner != NULL);
602 wapp = wApplicationOf(aicon->icon->owner->main_window);
604 if (event->xbutton.state & ControlMask) {
605 relaunchApplication(wapp);
606 return;
609 unhideHere = (event->xbutton.state & ShiftMask);
610 /* go to the last workspace that the user worked on the app */
611 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
612 wWorkspaceChange(scr, wapp->last_workspace);
614 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
616 if (event->xbutton.state & MOD_MASK)
617 wHideOtherApplications(aicon->icon->owner);
620 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
622 WAppIcon *aicon = desc->parent;
623 WIcon *icon = aicon->icon;
624 XEvent ev;
625 int x = aicon->x_pos, y = aicon->y_pos;
626 int dx = event->xbutton.x, dy = event->xbutton.y;
627 int grabbed = 0;
628 int done = 0;
629 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
630 WScreen *scr = icon->core->screen_ptr;
631 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
632 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
633 int ix, iy;
634 int clickButton = event->xbutton.button;
635 Pixmap ghost = None;
636 Window wins[2];
637 Bool movingSingle = False;
638 int oldX = x;
639 int oldY = y;
640 Bool hasMoved = False;
642 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
643 return;
645 if (IsDoubleClick(scr, event)) {
646 /* Middle or right mouse actions were handled on first click */
647 if (event->xbutton.button == Button1)
648 iconDblClick(desc, event);
649 return;
652 if (event->xbutton.button == Button2) {
653 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
655 if (wapp)
656 relaunchApplication(wapp);
658 return;
661 if (event->xbutton.button == Button3) {
662 WObjDescriptor *desc;
663 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
665 if (!wapp)
666 return;
668 if (event->xbutton.send_event &&
669 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
670 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
671 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
672 wwarning("pointer grab failed for appicon menu");
673 return;
676 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
678 /* allow drag select of menu */
679 desc = &scr->icon_menu->menu->descriptor;
680 event->xbutton.send_event = True;
681 (*desc->handle_mousedown) (desc, event);
682 return;
685 if (event->xbutton.state & MOD_MASK)
686 wLowerFrame(icon->core);
687 else
688 wRaiseFrame(icon->core);
690 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
691 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
692 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
693 wwarning("pointer grab failed for appicon move");
695 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
696 dockable = 0;
697 else
698 dockable = canBeDocked(icon->owner);
700 wins[0] = icon->core->window;
701 wins[1] = scr->dock_shadow;
702 XRestackWindows(dpy, wins, 2);
703 if (superfluous) {
704 if (icon->pixmap != None)
705 ghost = MakeGhostIcon(scr, icon->pixmap);
706 else
707 ghost = MakeGhostIcon(scr, icon->core->window);
708 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
709 XClearWindow(dpy, scr->dock_shadow);
712 while (!done) {
713 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
714 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
715 switch (ev.type) {
716 case Expose:
717 WMHandleEvent(&ev);
718 break;
720 case EnterNotify:
721 /* It means the cursor moved so fast that it entered
722 * something else (if moving slowly, it would have
723 * stayed in the appIcon that is being moved. Ignore
724 * such "spurious" EnterNotifiy's */
725 break;
727 case MotionNotify:
728 hasMoved = True;
729 if (!grabbed) {
730 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
731 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
732 XChangeActivePointerGrab(dpy, ButtonMotionMask
733 | ButtonReleaseMask | ButtonPressMask,
734 wCursor[WCUR_MOVE], CurrentTime);
735 grabbed = 1;
736 } else {
737 break;
740 x = ev.xmotion.x_root - dx;
741 y = ev.xmotion.y_root - dy;
743 if (movingSingle)
744 XMoveWindow(dpy, icon->core->window, x, y);
745 else
746 wAppIconMove(aicon, x, y);
748 if (dockable) {
749 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
750 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
751 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
753 if (scr->last_dock != scr->dock && collapsed) {
754 scr->last_dock->collapsed = 1;
755 wDockHideIcons(scr->last_dock);
756 collapsed = 0;
758 if (!collapsed && (collapsed = scr->dock->collapsed)) {
759 scr->dock->collapsed = 0;
760 wDockShowIcons(scr->dock);
763 if (scr->dock->auto_raise_lower)
764 wDockRaise(scr->dock);
766 scr->last_dock = scr->dock;
768 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
769 if (!docking)
770 XMapWindow(dpy, scr->dock_shadow);
772 docking = 1;
773 } else if (workspace->clip &&
774 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
775 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
776 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
778 if (scr->last_dock != workspace->clip && collapsed) {
779 scr->last_dock->collapsed = 1;
780 wDockHideIcons(scr->last_dock);
781 collapsed = 0;
783 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
784 workspace->clip->collapsed = 0;
785 wDockShowIcons(workspace->clip);
788 if (workspace->clip->auto_raise_lower)
789 wDockRaise(workspace->clip);
791 scr->last_dock = workspace->clip;
793 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
794 if (!docking)
795 XMapWindow(dpy, scr->dock_shadow);
797 docking = 1;
798 } else if (docking) {
799 XUnmapWindow(dpy, scr->dock_shadow);
800 docking = 0;
803 break;
805 case ButtonPress:
806 break;
808 case ButtonRelease:
809 if (ev.xbutton.button != clickButton)
810 break;
811 XUngrabPointer(dpy, CurrentTime);
813 if (docking) {
814 Bool docked;
816 /* icon is trying to be docked */
817 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
818 XUnmapWindow(dpy, scr->dock_shadow);
819 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
820 if (scr->last_dock->auto_collapse)
821 collapsed = 0;
823 if (workspace->clip &&
824 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
825 wDockLower(workspace->clip);
827 if (!docked) {
828 /* If icon could not be docked, slide it back to the old
829 * position */
830 SlideWindow(icon->core->window, x, y, oldX, oldY);
832 } else {
833 if (movingSingle) {
834 /* move back to its place */
835 SlideWindow(icon->core->window, x, y, oldX, oldY);
836 wAppIconMove(aicon, oldX, oldY);
837 } else {
838 XMoveWindow(dpy, icon->core->window, x, y);
839 aicon->x_pos = x;
840 aicon->y_pos = y;
842 if (workspace->clip && workspace->clip->auto_raise_lower)
843 wDockLower(workspace->clip);
845 if (collapsed) {
846 scr->last_dock->collapsed = 1;
847 wDockHideIcons(scr->last_dock);
848 collapsed = 0;
850 if (superfluous) {
851 if (ghost != None)
852 XFreePixmap(dpy, ghost);
853 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
856 if (wPreferences.auto_arrange_icons)
857 wArrangeIcons(scr, True);
859 if (wPreferences.single_click && !hasMoved)
860 iconDblClick(desc, event);
862 done = 1;
863 break;
868 /* This function save the application icon and store the path in the Dictionary */
869 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
871 WMPropList *dict = WDWindowAttributes->dictionary;
872 WMPropList *adict, *key, *iconk;
873 WMPropList *val;
874 char *tmp;
876 tmp = get_name_for_instance_class(wm_instance, wm_class);
877 key = WMCreatePLString(tmp);
878 wfree(tmp);
880 adict = WMGetFromPLDictionary(dict, key);
881 iconk = WMCreatePLString("Icon");
883 if (adict) {
884 val = WMGetFromPLDictionary(adict, iconk);
885 } else {
886 /* no dictionary for app, so create one */
887 adict = WMCreatePLDictionary(NULL, NULL);
888 WMPutInPLDictionary(dict, key, adict);
889 WMReleasePropList(adict);
890 val = NULL;
893 if (!val) {
894 val = WMCreatePLString(iconPath);
895 WMPutInPLDictionary(adict, iconk, val);
896 WMReleasePropList(val);
897 } else {
898 val = NULL;
901 WMReleasePropList(key);
902 WMReleasePropList(iconk);
904 if (val && !wPreferences.flags.noupdates)
905 UpdateDomainFile(WDWindowAttributes);
908 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
910 WAppIcon *aicon = NULL;
912 aicon = wDockFindIconForWindow(dock, main_window);
913 if (!aicon) {
914 wDockTrackWindowLaunch(dock, main_window);
915 aicon = wDockFindIconForWindow(dock, main_window);
917 return aicon;
920 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
922 WScreen *scr = wwin->screen_ptr;
923 wapp->app_icon = NULL;
925 if (scr->last_dock)
926 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
928 /* check main dock if we did not find it in last dock */
929 if (!wapp->app_icon && scr->dock)
930 wapp->app_icon = findDockIconFor(scr->dock, main_window);
932 /* finally check clips */
933 if (!wapp->app_icon) {
934 int i;
935 for (i = 0; i < scr->workspace_count; i++) {
936 WDock *dock = scr->workspaces[i]->clip;
937 if (dock)
938 wapp->app_icon = findDockIconFor(dock, main_window);
939 if (wapp->app_icon)
940 break;
944 /* If created, then set some flags */
945 if (wapp->app_icon) {
946 WWindow *mainw = wapp->main_window_desc;
948 wapp->app_icon->running = 1;
949 wapp->app_icon->icon->force_paint = 1;
950 wapp->app_icon->icon->owner = mainw;
951 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
952 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
954 wAppIconPaint(wapp->app_icon);
955 save_appicon(wapp->app_icon, True);
959 /* Add the appicon to the appiconlist */
960 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
962 appicon->prev = NULL;
963 appicon->next = scr->app_icon_list;
964 if (scr->app_icon_list)
965 scr->app_icon_list->prev = appicon;
967 scr->app_icon_list = appicon;
970 /* Remove the appicon from the appiconlist */
971 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
973 if (appicon == scr->app_icon_list) {
974 if (appicon->next)
975 appicon->next->prev = NULL;
976 scr->app_icon_list = appicon->next;
977 } else {
978 if (appicon->next)
979 appicon->next->prev = appicon->prev;
980 if (appicon->prev)
981 appicon->prev->next = appicon->next;