03e006c745d3f40daac52b2716d55dc085eb61cd
[wmaker-crm.git] / src / appicon.c
blob03e006c745d3f40daac52b2716d55dc085eb61cd
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 = get_default_icon_filename(scr, wm_instance, wm_class, command, False);
133 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
134 if (path)
135 wfree(path);
136 #ifdef XDND
137 wXDNDMakeAwareness(dicon->icon->core->window);
138 #endif
140 /* will be overriden by dock */
141 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
142 dicon->icon->core->descriptor.handle_expose = iconExpose;
143 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
144 dicon->icon->core->descriptor.parent = dicon;
145 AddToStackList(dicon->icon->core);
147 return dicon;
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);
239 aicon->yindex = -1;
240 aicon->xindex = -1;
242 /* When no_appicon is set we want to avoid having it on the list
243 * because otherwise there will be a hole when the icons are
244 * arranged with wArrangeIcons() */
245 if (!WFLAGP(leader_win, no_appicon))
246 add_to_appicon_list(scr, aicon);
248 if (leader_win->wm_class)
249 aicon->wm_class = wstrdup(leader_win->wm_class);
251 if (leader_win->wm_instance)
252 aicon->wm_instance = wstrdup(leader_win->wm_instance);
254 aicon->icon = wIconCreate(leader_win);
255 #ifdef XDND
256 wXDNDMakeAwareness(aicon->icon->core->window);
257 #endif
259 /* will be overriden if docked */
260 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
261 aicon->icon->core->descriptor.handle_expose = iconExpose;
262 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
263 aicon->icon->core->descriptor.parent = aicon;
264 AddToStackList(aicon->icon->core);
265 aicon->icon->show_title = 0;
267 return aicon;
270 void wAppIconDestroy(WAppIcon * aicon)
272 WScreen *scr = aicon->icon->core->screen_ptr;
274 RemoveFromStackList(aicon->icon->core);
275 wIconDestroy(aicon->icon);
276 if (aicon->command)
277 wfree(aicon->command);
278 #ifdef XDND
279 if (aicon->dnd_command)
280 wfree(aicon->dnd_command);
281 #endif
282 if (aicon->wm_instance)
283 wfree(aicon->wm_instance);
285 if (aicon->wm_class)
286 wfree(aicon->wm_class);
288 remove_from_appicon_list(scr, aicon);
290 aicon->destroyed = 1;
291 wrelease(aicon);
294 static void drawCorner(WIcon * icon)
296 WScreen *scr = icon->core->screen_ptr;
297 XPoint points[3];
299 points[0].x = 1;
300 points[0].y = 1;
301 points[1].x = 12;
302 points[1].y = 1;
303 points[2].x = 1;
304 points[2].y = 12;
305 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
306 points, 3, Convex, CoordModeOrigin);
307 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
308 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
311 void wAppIconMove(WAppIcon * aicon, int x, int y)
313 XMoveWindow(dpy, aicon->icon->core->window, x, y);
314 aicon->x_pos = x;
315 aicon->y_pos = y;
318 #ifdef WS_INDICATOR
319 static void updateDockNumbers(WScreen * scr)
321 int length;
322 char *ws_numbers;
323 WAppIcon *dicon = scr->dock->icon_array[0];
325 ws_numbers = wmalloc(20);
326 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
327 length = strlen(ws_numbers);
329 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
331 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
332 scr->icon_title_font, 4, 3, ws_numbers, length);
334 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
335 scr->icon_title_font, 3, 2, ws_numbers, length);
337 wfree(ws_numbers);
339 #endif /* WS_INDICATOR */
341 void wAppIconPaint(WAppIcon * aicon)
343 WApplication *wapp;
344 WScreen *scr = aicon->icon->core->screen_ptr;
346 if (aicon->icon->owner)
347 wapp = wApplicationOf(aicon->icon->owner->main_window);
348 else
349 wapp = NULL;
351 wIconPaint(aicon->icon);
353 # ifdef WS_INDICATOR
354 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
355 updateDockNumbers(scr);
356 # endif
357 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
358 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
359 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
360 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
361 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
363 #ifdef HIDDENDOT
364 if (wapp && wapp->flags.hidden) {
365 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
366 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
367 XCopyArea(dpy, scr->dock_dots->image,
368 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
370 #endif /* HIDDENDOT */
372 if (aicon->omnipresent)
373 drawCorner(aicon->icon);
375 XSetClipMask(dpy, scr->copy_gc, None);
376 if (aicon->launching)
377 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
378 0, 0, wPreferences.icon_size, wPreferences.icon_size);
381 /* Save the application icon, if it's a dockapp then use it with dock = True */
382 void save_appicon(WAppIcon *aicon, Bool dock)
384 char *path;
386 if (!aicon)
387 return;
389 if (dock && (!aicon->docked || aicon->attracted))
390 return;
392 path = wIconStore(aicon->icon);
393 if (!path)
394 return;
396 wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
397 wfree(path);
400 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
402 /* main_window may not have the full command line; try to find one which does */
403 static void relaunchApplication(WApplication *wapp)
405 WScreen *scr;
406 WWindow *wlist, *next;
408 scr = wapp->main_window_desc->screen_ptr;
409 wlist = scr->focused_window;
410 if (! wlist)
411 return;
413 while (wlist->prev)
414 wlist = wlist->prev;
416 while (wlist) {
417 next = wlist->next;
419 if (wlist->main_window == wapp->main_window) {
420 if (RelaunchWindow(wlist))
421 return;
424 wlist = next;
428 static void relaunchCallback(WMenu * menu, WMenuEntry * entry)
430 WApplication *wapp = (WApplication *) entry->clientdata;
432 relaunchApplication(wapp);
435 static void hideCallback(WMenu * menu, WMenuEntry * entry)
437 WApplication *wapp = (WApplication *) entry->clientdata;
439 if (wapp->flags.hidden) {
440 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
441 wUnhideApplication(wapp, False, False);
442 } else {
443 wHideApplication(wapp);
447 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
449 WApplication *wapp = (WApplication *) entry->clientdata;
451 wUnhideApplication(wapp, False, True);
454 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
456 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
457 char *file = NULL;
458 WScreen *scr;
459 int result;
461 assert(icon != NULL);
463 if (icon->editing)
464 return;
466 icon->editing = 1;
467 scr = icon->icon->core->screen_ptr;
469 wretain(icon);
471 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
473 if (result && !icon->destroyed) {
474 if (file && *file == 0) {
475 wfree(file);
476 file = NULL;
478 if (!wIconChangeImageFile(icon->icon, file)) {
479 wMessageDialog(scr, _("Error"),
480 _("Could not open specified icon file"), _("OK"), NULL, NULL);
481 } else {
482 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
483 wAppIconPaint(icon);
485 if (file)
486 wfree(file);
488 icon->editing = 0;
489 wrelease(icon);
492 static void killCallback(WMenu * menu, WMenuEntry * entry)
494 WApplication *wapp = (WApplication *) entry->clientdata;
495 WFakeGroupLeader *fPtr;
496 char *buffer;
497 char *shortname;
498 char *basename(const char *shortname);
500 if (!WCHECK_STATE(WSTATE_NORMAL))
501 return;
503 WCHANGE_STATE(WSTATE_MODAL);
505 assert(entry->clientdata != NULL);
507 shortname = basename(wapp->app_icon->wm_instance);
509 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
510 _(" will be forcibly closed.\n"
511 "Any unsaved changes will be lost.\n" "Please confirm."));
513 fPtr = wapp->main_window_desc->fake_group;
515 wretain(wapp->main_window_desc);
516 if (wPreferences.dont_confirm_kill
517 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
518 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
519 if (fPtr != NULL) {
520 WWindow *wwin, *twin;
522 wwin = wapp->main_window_desc->screen_ptr->focused_window;
523 while (wwin) {
524 twin = wwin->prev;
525 if (wwin->fake_group == fPtr)
526 wClientKill(wwin);
527 wwin = twin;
529 } else if (!wapp->main_window_desc->flags.destroyed) {
530 wClientKill(wapp->main_window_desc);
533 wrelease(wapp->main_window_desc);
534 wfree(buffer);
535 WCHANGE_STATE(WSTATE_NORMAL);
538 static WMenu *createApplicationMenu(WScreen * scr)
540 WMenu *menu;
542 menu = wMenuCreate(scr, NULL, False);
543 wMenuAddCallback(menu, _("Launch"), relaunchCallback, NULL);
544 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
545 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
546 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
547 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
549 return menu;
552 static void openApplicationMenu(WApplication * wapp, int x, int y)
554 WMenu *menu;
555 WScreen *scr = wapp->main_window_desc->screen_ptr;
556 int i;
558 if (!scr->icon_menu) {
559 scr->icon_menu = createApplicationMenu(scr);
560 wfree(scr->icon_menu->entries[1]->text);
563 menu = scr->icon_menu;
565 if (wapp->flags.hidden)
566 menu->entries[1]->text = _("Unhide");
567 else
568 menu->entries[1]->text = _("Hide");
570 menu->flags.realized = 0;
571 wMenuRealize(menu);
573 x -= menu->frame->core->width / 2;
574 if (x + menu->frame->core->width > scr->scr_width)
575 x = scr->scr_width - menu->frame->core->width;
577 if (x < 0)
578 x = 0;
580 /* set client data */
581 for (i = 0; i < menu->entry_no; i++)
582 menu->entries[i]->clientdata = wapp;
584 wMenuMapAt(menu, x, y, False);
587 /******************************************************************/
589 static void iconExpose(WObjDescriptor * desc, XEvent * event)
591 wAppIconPaint(desc->parent);
594 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
596 WAppIcon *aicon = desc->parent;
597 WApplication *wapp;
598 WScreen *scr = aicon->icon->core->screen_ptr;
599 int unhideHere;
601 assert(aicon->icon->owner != NULL);
603 wapp = wApplicationOf(aicon->icon->owner->main_window);
605 if (event->xbutton.state & ControlMask) {
606 relaunchApplication(wapp);
607 return;
610 unhideHere = (event->xbutton.state & ShiftMask);
611 /* go to the last workspace that the user worked on the app */
612 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
613 wWorkspaceChange(scr, wapp->last_workspace);
615 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
617 if (event->xbutton.state & MOD_MASK)
618 wHideOtherApplications(aicon->icon->owner);
621 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
623 WAppIcon *aicon = desc->parent;
624 WIcon *icon = aicon->icon;
625 XEvent ev;
626 int x = aicon->x_pos, y = aicon->y_pos;
627 int dx = event->xbutton.x, dy = event->xbutton.y;
628 int grabbed = 0;
629 int done = 0;
630 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
631 WScreen *scr = icon->core->screen_ptr;
632 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
633 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
634 int ix, iy;
635 int clickButton = event->xbutton.button;
636 Pixmap ghost = None;
637 Window wins[2];
638 Bool movingSingle = False;
639 int oldX = x;
640 int oldY = y;
641 Bool hasMoved = False;
643 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
644 return;
646 if (IsDoubleClick(scr, event)) {
647 /* Middle or right mouse actions were handled on first click */
648 if (event->xbutton.button == Button1)
649 iconDblClick(desc, event);
650 return;
653 if (event->xbutton.button == Button2) {
654 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
656 if (wapp)
657 relaunchApplication(wapp);
659 return;
662 if (event->xbutton.button == Button3) {
663 WObjDescriptor *desc;
664 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
666 if (!wapp)
667 return;
669 if (event->xbutton.send_event &&
670 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
671 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
672 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
673 wwarning("pointer grab failed for appicon menu");
674 return;
677 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
679 /* allow drag select of menu */
680 desc = &scr->icon_menu->menu->descriptor;
681 event->xbutton.send_event = True;
682 (*desc->handle_mousedown) (desc, event);
683 return;
686 if (event->xbutton.state & MOD_MASK)
687 wLowerFrame(icon->core);
688 else
689 wRaiseFrame(icon->core);
691 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
692 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
693 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
694 wwarning("pointer grab failed for appicon move");
696 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
697 dockable = 0;
698 else
699 dockable = canBeDocked(icon->owner);
701 wins[0] = icon->core->window;
702 wins[1] = scr->dock_shadow;
703 XRestackWindows(dpy, wins, 2);
704 if (superfluous) {
705 if (icon->pixmap != None)
706 ghost = MakeGhostIcon(scr, icon->pixmap);
707 else
708 ghost = MakeGhostIcon(scr, icon->core->window);
709 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
710 XClearWindow(dpy, scr->dock_shadow);
713 while (!done) {
714 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
715 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
716 switch (ev.type) {
717 case Expose:
718 WMHandleEvent(&ev);
719 break;
721 case EnterNotify:
722 /* It means the cursor moved so fast that it entered
723 * something else (if moving slowly, it would have
724 * stayed in the appIcon that is being moved. Ignore
725 * such "spurious" EnterNotifiy's */
726 break;
728 case MotionNotify:
729 hasMoved = True;
730 if (!grabbed) {
731 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
732 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
733 XChangeActivePointerGrab(dpy, ButtonMotionMask
734 | ButtonReleaseMask | ButtonPressMask,
735 wCursor[WCUR_MOVE], CurrentTime);
736 grabbed = 1;
737 } else {
738 break;
741 x = ev.xmotion.x_root - dx;
742 y = ev.xmotion.y_root - dy;
744 if (movingSingle)
745 XMoveWindow(dpy, icon->core->window, x, y);
746 else
747 wAppIconMove(aicon, x, y);
749 if (dockable) {
750 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
751 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
752 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
754 if (scr->last_dock != scr->dock && collapsed) {
755 scr->last_dock->collapsed = 1;
756 wDockHideIcons(scr->last_dock);
757 collapsed = 0;
759 if (!collapsed && (collapsed = scr->dock->collapsed)) {
760 scr->dock->collapsed = 0;
761 wDockShowIcons(scr->dock);
764 if (scr->dock->auto_raise_lower)
765 wDockRaise(scr->dock);
767 scr->last_dock = scr->dock;
769 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
770 if (!docking)
771 XMapWindow(dpy, scr->dock_shadow);
773 docking = 1;
774 } else if (workspace->clip &&
775 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
776 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
777 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
779 if (scr->last_dock != workspace->clip && collapsed) {
780 scr->last_dock->collapsed = 1;
781 wDockHideIcons(scr->last_dock);
782 collapsed = 0;
784 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
785 workspace->clip->collapsed = 0;
786 wDockShowIcons(workspace->clip);
789 if (workspace->clip->auto_raise_lower)
790 wDockRaise(workspace->clip);
792 scr->last_dock = workspace->clip;
794 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
795 if (!docking)
796 XMapWindow(dpy, scr->dock_shadow);
798 docking = 1;
799 } else if (docking) {
800 XUnmapWindow(dpy, scr->dock_shadow);
801 docking = 0;
804 break;
806 case ButtonPress:
807 break;
809 case ButtonRelease:
810 if (ev.xbutton.button != clickButton)
811 break;
812 XUngrabPointer(dpy, CurrentTime);
814 if (docking) {
815 Bool docked;
817 /* icon is trying to be docked */
818 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
819 XUnmapWindow(dpy, scr->dock_shadow);
820 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
821 if (scr->last_dock->auto_collapse)
822 collapsed = 0;
824 if (workspace->clip &&
825 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
826 wDockLower(workspace->clip);
828 if (!docked) {
829 /* If icon could not be docked, slide it back to the old
830 * position */
831 SlideWindow(icon->core->window, x, y, oldX, oldY);
833 } else {
834 if (movingSingle) {
835 /* move back to its place */
836 SlideWindow(icon->core->window, x, y, oldX, oldY);
837 wAppIconMove(aicon, oldX, oldY);
838 } else {
839 XMoveWindow(dpy, icon->core->window, x, y);
840 aicon->x_pos = x;
841 aicon->y_pos = y;
843 if (workspace->clip && workspace->clip->auto_raise_lower)
844 wDockLower(workspace->clip);
846 if (collapsed) {
847 scr->last_dock->collapsed = 1;
848 wDockHideIcons(scr->last_dock);
849 collapsed = 0;
851 if (superfluous) {
852 if (ghost != None)
853 XFreePixmap(dpy, ghost);
854 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
857 if (wPreferences.auto_arrange_icons)
858 wArrangeIcons(scr, True);
860 if (wPreferences.single_click && !hasMoved)
861 iconDblClick(desc, event);
863 done = 1;
864 break;
869 /* This function save the application icon and store the path in the Dictionary */
870 static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
872 WMPropList *dict = WDWindowAttributes->dictionary;
873 WMPropList *adict, *key, *iconk;
874 WMPropList *val;
875 char *tmp;
877 tmp = get_name_for_instance_class(wm_instance, wm_class);
878 key = WMCreatePLString(tmp);
879 wfree(tmp);
881 adict = WMGetFromPLDictionary(dict, key);
882 iconk = WMCreatePLString("Icon");
884 if (adict) {
885 val = WMGetFromPLDictionary(adict, iconk);
886 } else {
887 /* no dictionary for app, so create one */
888 adict = WMCreatePLDictionary(NULL, NULL);
889 WMPutInPLDictionary(dict, key, adict);
890 WMReleasePropList(adict);
891 val = NULL;
894 if (!val) {
895 val = WMCreatePLString(iconPath);
896 WMPutInPLDictionary(adict, iconk, val);
897 WMReleasePropList(val);
898 } else {
899 val = NULL;
902 WMReleasePropList(key);
903 WMReleasePropList(iconk);
905 if (val && !wPreferences.flags.noupdates)
906 UpdateDomainFile(WDWindowAttributes);
909 static WAppIcon *findDockIconFor(WDock *dock, Window main_window)
911 WAppIcon *aicon = NULL;
913 aicon = wDockFindIconForWindow(dock, main_window);
914 if (!aicon) {
915 wDockTrackWindowLaunch(dock, main_window);
916 aicon = wDockFindIconForWindow(dock, main_window);
918 return aicon;
921 void create_appicon_from_dock(WWindow *wwin, WApplication *wapp, Window main_window)
923 WScreen *scr = wwin->screen_ptr;
924 wapp->app_icon = NULL;
926 if (scr->last_dock)
927 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
929 /* check main dock if we did not find it in last dock */
930 if (!wapp->app_icon && scr->dock)
931 wapp->app_icon = findDockIconFor(scr->dock, main_window);
933 /* finally check clips */
934 if (!wapp->app_icon) {
935 int i;
936 for (i = 0; i < scr->workspace_count; i++) {
937 WDock *dock = scr->workspaces[i]->clip;
938 if (dock)
939 wapp->app_icon = findDockIconFor(dock, main_window);
940 if (wapp->app_icon)
941 break;
945 /* If created, then set some flags */
946 if (wapp->app_icon) {
947 WWindow *mainw = wapp->main_window_desc;
949 wapp->app_icon->running = 1;
950 wapp->app_icon->icon->force_paint = 1;
951 wapp->app_icon->icon->owner = mainw;
952 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
953 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
955 wAppIconPaint(wapp->app_icon);
956 save_appicon(wapp->app_icon, True);
960 /* Add the appicon to the appiconlist */
961 static void add_to_appicon_list(WScreen *scr, WAppIcon *appicon)
963 appicon->prev = NULL;
964 appicon->next = scr->app_icon_list;
965 if (scr->app_icon_list)
966 scr->app_icon_list->prev = appicon;
968 scr->app_icon_list = appicon;
971 /* Remove the appicon from the appiconlist */
972 static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon)
974 if (appicon == scr->app_icon_list) {
975 if (appicon->next)
976 appicon->next->prev = NULL;
977 scr->app_icon_list = appicon->next;
978 } else {
979 if (appicon->next)
980 appicon->next->prev = appicon->prev;
981 if (appicon->prev)
982 appicon->prev->next = appicon->next;