Improve dockapp recognition
[wmaker-crm.git] / src / appicon.c
blob75fc044bd4021ff728b2655fd4755a03741e009a
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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "window.h"
33 #include "icon.h"
34 #include "appicon.h"
35 #include "actions.h"
36 #include "stacking.h"
37 #include "dock.h"
38 #include "funcs.h"
39 #include "defaults.h"
40 #include "workspace.h"
41 #include "superfluous.h"
42 #include "menu.h"
43 #include "framewin.h"
44 #include "dialog.h"
45 #include "client.h"
46 #ifdef XDND
47 #include "xdnd.h"
48 #endif
51 * icon_file for the dock is got from the preferences file by
52 * using the classname/instancename
55 /**** Global variables ****/
56 extern Cursor wCursor[WCUR_LAST];
57 extern WPreferences wPreferences;
59 #define MOD_MASK wPreferences.modifier_mask
61 void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
62 static void iconDblClick(WObjDescriptor * desc, XEvent * event);
63 static void iconExpose(WObjDescriptor * desc, XEvent * event);
65 WAppIcon *wAppIconCreateForDock(WScreen * scr, char *command, char *wm_instance, char *wm_class, int tile)
67 WAppIcon *dicon;
68 char *path;
70 dicon = wmalloc(sizeof(WAppIcon));
71 wretain(dicon);
72 memset(dicon, 0, sizeof(WAppIcon));
73 dicon->yindex = -1;
74 dicon->xindex = -1;
76 dicon->prev = NULL;
77 dicon->next = scr->app_icon_list;
78 if (scr->app_icon_list) {
79 scr->app_icon_list->prev = dicon;
81 scr->app_icon_list = dicon;
83 if (command) {
84 dicon->command = wstrdup(command);
86 if (wm_class)
87 dicon->wm_class = wstrdup(wm_class);
88 if (wm_instance)
89 dicon->wm_instance = wstrdup(wm_instance);
91 path = wDefaultGetIconFile(scr, wm_instance, wm_class, True);
92 if (!path && command) {
93 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
95 path = wDefaultGetIconFile(scr, wm_instance, wm_class, False);
98 if (path)
99 path = FindImage(wPreferences.icon_path, path);
101 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
102 if (path)
103 wfree(path);
104 #ifdef XDND
105 wXDNDMakeAwareness(dicon->icon->core->window);
106 #endif
108 /* will be overriden by dock */
109 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
110 dicon->icon->core->descriptor.handle_expose = iconExpose;
111 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
112 dicon->icon->core->descriptor.parent = dicon;
113 AddToStackList(dicon->icon->core);
115 return dicon;
118 WAppIcon *wAppIconCreate(WWindow * leader_win)
120 WAppIcon *aicon;
121 WScreen *scr = leader_win->screen_ptr;
123 aicon = wmalloc(sizeof(WAppIcon));
124 wretain(aicon);
125 memset(aicon, 0, sizeof(WAppIcon));
127 aicon->yindex = -1;
128 aicon->xindex = -1;
130 aicon->prev = NULL;
131 aicon->next = scr->app_icon_list;
132 if (scr->app_icon_list) {
133 scr->app_icon_list->prev = aicon;
135 scr->app_icon_list = aicon;
137 if (leader_win->wm_class)
138 aicon->wm_class = wstrdup(leader_win->wm_class);
139 if (leader_win->wm_instance)
140 aicon->wm_instance = wstrdup(leader_win->wm_instance);
142 aicon->icon = wIconCreate(leader_win);
143 #ifdef XDND
144 wXDNDMakeAwareness(aicon->icon->core->window);
145 #endif
147 /* will be overriden if docked */
148 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
149 aicon->icon->core->descriptor.handle_expose = iconExpose;
150 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
151 aicon->icon->core->descriptor.parent = aicon;
152 AddToStackList(aicon->icon->core);
153 aicon->icon->show_title = 0;
154 wIconUpdate(aicon->icon);
156 return aicon;
159 void wAppIconDestroy(WAppIcon * aicon)
161 WScreen *scr = aicon->icon->core->screen_ptr;
163 RemoveFromStackList(aicon->icon->core);
164 wIconDestroy(aicon->icon);
165 if (aicon->command)
166 wfree(aicon->command);
167 #ifdef XDND
168 if (aicon->dnd_command)
169 wfree(aicon->dnd_command);
170 #endif
171 if (aicon->wm_instance)
172 wfree(aicon->wm_instance);
173 if (aicon->wm_class)
174 wfree(aicon->wm_class);
176 if (aicon == scr->app_icon_list) {
177 if (aicon->next)
178 aicon->next->prev = NULL;
179 scr->app_icon_list = aicon->next;
180 } else {
181 if (aicon->next)
182 aicon->next->prev = aicon->prev;
183 if (aicon->prev)
184 aicon->prev->next = aicon->next;
187 aicon->destroyed = 1;
188 wrelease(aicon);
191 static void drawCorner(WIcon * icon)
193 WScreen *scr = icon->core->screen_ptr;
194 XPoint points[3];
196 points[0].x = 1;
197 points[0].y = 1;
198 points[1].x = 12;
199 points[1].y = 1;
200 points[2].x = 1;
201 points[2].y = 12;
202 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
203 points, 3, Convex, CoordModeOrigin);
204 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
205 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
206 /* drawing the second line gives a weird concave look. -Dan */
207 #if 0
208 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 1, 1, 1, 11);
209 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 1, 1, 11, 1);
210 #endif
213 void wAppIconMove(WAppIcon * aicon, int x, int y)
215 XMoveWindow(dpy, aicon->icon->core->window, x, y);
216 aicon->x_pos = x;
217 aicon->y_pos = y;
220 #ifdef WS_INDICATOR
221 static void updateDockNumbers(WScreen * scr)
223 int length;
224 char *ws_numbers;
225 WAppIcon *dicon = scr->dock->icon_array[0];
227 ws_numbers = wmalloc(20);
228 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
229 length = strlen(ws_numbers);
231 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
233 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
234 scr->icon_title_font, 4, 3, ws_numbers, length);
236 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
237 scr->icon_title_font, 3, 2, ws_numbers, length);
239 wfree(ws_numbers);
241 #endif /* WS_INDICATOR */
243 void wAppIconPaint(WAppIcon * aicon)
245 WApplication *wapp;
246 WScreen *scr = aicon->icon->core->screen_ptr;
248 if (aicon->icon->owner)
249 wapp = wApplicationOf(aicon->icon->owner->main_window);
250 else
251 wapp = NULL;
253 wIconPaint(aicon->icon);
255 # ifdef WS_INDICATOR
256 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
257 updateDockNumbers(scr);
258 # endif
259 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
260 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
261 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
262 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
263 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
265 #ifdef HIDDENDOT
266 if (wapp && wapp->flags.hidden) {
267 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
268 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
269 XCopyArea(dpy, scr->dock_dots->image,
270 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
272 #endif /* HIDDENDOT */
274 if (aicon->omnipresent)
275 drawCorner(aicon->icon);
277 XSetClipMask(dpy, scr->copy_gc, None);
278 if (aicon->launching) {
279 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
280 0, 0, wPreferences.icon_size, wPreferences.icon_size);
284 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
286 static void hideCallback(WMenu * menu, WMenuEntry * entry)
288 WApplication *wapp = (WApplication *) entry->clientdata;
290 if (wapp->flags.hidden) {
291 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
292 wUnhideApplication(wapp, False, False);
293 } else {
294 wHideApplication(wapp);
298 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
300 WApplication *wapp = (WApplication *) entry->clientdata;
302 wUnhideApplication(wapp, False, True);
305 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
307 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
308 char *file = NULL;
309 WScreen *scr;
310 int result;
312 assert(icon != NULL);
314 if (icon->editing)
315 return;
316 icon->editing = 1;
317 scr = icon->icon->core->screen_ptr;
319 wretain(icon);
321 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
323 if (result && !icon->destroyed) {
324 if (file && *file == 0) {
325 wfree(file);
326 file = NULL;
328 if (!wIconChangeImageFile(icon->icon, file)) {
329 wMessageDialog(scr, _("Error"),
330 _("Could not open specified icon file"), _("OK"), NULL, NULL);
331 } else {
332 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
333 wAppIconPaint(icon);
335 if (file)
336 wfree(file);
338 icon->editing = 0;
339 wrelease(icon);
342 static void killCallback(WMenu * menu, WMenuEntry * entry)
344 WApplication *wapp = (WApplication *) entry->clientdata;
345 WFakeGroupLeader *fPtr;
346 char *buffer;
347 char *shortname;
348 char *basename(const char *shortname);
350 if (!WCHECK_STATE(WSTATE_NORMAL))
351 return;
353 WCHANGE_STATE(WSTATE_MODAL);
355 assert(entry->clientdata != NULL);
357 shortname = basename(wapp->app_icon->wm_instance);
359 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
360 _(" will be forcibly closed.\n"
361 "Any unsaved changes will be lost.\n" "Please confirm."));
363 fPtr = wapp->main_window_desc->fake_group;
365 wretain(wapp->main_window_desc);
366 if (wPreferences.dont_confirm_kill
367 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
368 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
369 if (fPtr != NULL) {
370 WWindow *wwin, *twin;
372 wwin = wapp->main_window_desc->screen_ptr->focused_window;
373 while (wwin) {
374 twin = wwin->prev;
375 if (wwin->fake_group == fPtr) {
376 wClientKill(wwin);
378 wwin = twin;
380 } else if (!wapp->main_window_desc->flags.destroyed) {
381 wClientKill(wapp->main_window_desc);
384 wrelease(wapp->main_window_desc);
386 wfree(buffer);
388 WCHANGE_STATE(WSTATE_NORMAL);
391 static WMenu *createApplicationMenu(WScreen * scr)
393 WMenu *menu;
395 menu = wMenuCreate(scr, NULL, False);
396 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
397 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
398 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
399 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
401 return menu;
404 static void openApplicationMenu(WApplication * wapp, int x, int y)
406 WMenu *menu;
407 WScreen *scr = wapp->main_window_desc->screen_ptr;
408 int i;
410 if (!scr->icon_menu) {
411 scr->icon_menu = createApplicationMenu(scr);
412 wfree(scr->icon_menu->entries[1]->text);
415 menu = scr->icon_menu;
417 if (wapp->flags.hidden) {
418 menu->entries[1]->text = _("Unhide");
419 } else {
420 menu->entries[1]->text = _("Hide");
423 menu->flags.realized = 0;
424 wMenuRealize(menu);
426 x -= menu->frame->core->width / 2;
427 if (x + menu->frame->core->width > scr->scr_width)
428 x = scr->scr_width - menu->frame->core->width;
429 if (x < 0)
430 x = 0;
432 /* set client data */
433 for (i = 0; i < menu->entry_no; i++) {
434 menu->entries[i]->clientdata = wapp;
436 wMenuMapAt(menu, x, y, False);
439 /******************************************************************/
441 static void iconExpose(WObjDescriptor * desc, XEvent * event)
443 wAppIconPaint(desc->parent);
446 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
448 WAppIcon *aicon = desc->parent;
449 WApplication *wapp;
450 WScreen *scr = aicon->icon->core->screen_ptr;
451 int unhideHere;
453 assert(aicon->icon->owner != NULL);
455 wapp = wApplicationOf(aicon->icon->owner->main_window);
456 unhideHere = (event->xbutton.state & ShiftMask);
458 /* go to the last workspace that the user worked on the app */
459 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
460 wWorkspaceChange(scr, wapp->last_workspace);
462 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
464 if (event->xbutton.state & MOD_MASK) {
465 wHideOtherApplications(aicon->icon->owner);
469 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
471 WAppIcon *aicon = desc->parent;
472 WIcon *icon = aicon->icon;
473 XEvent ev;
474 int x = aicon->x_pos, y = aicon->y_pos;
475 int dx = event->xbutton.x, dy = event->xbutton.y;
476 int grabbed = 0;
477 int done = 0;
478 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
479 WScreen *scr = icon->core->screen_ptr;
480 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
481 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
482 int ix, iy;
483 int clickButton = event->xbutton.button;
484 Pixmap ghost = None;
485 Window wins[2];
486 Bool movingSingle = False;
487 int oldX = x;
488 int oldY = y;
489 Bool hasMoved = False;
491 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
492 return;
494 if (IsDoubleClick(scr, event)) {
495 iconDblClick(desc, event);
496 return;
499 if (event->xbutton.button == Button3) {
500 WObjDescriptor *desc;
501 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
503 if (!wapp)
504 return;
506 if (event->xbutton.send_event &&
507 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
508 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
509 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
510 wwarning("pointer grab failed for appicon menu");
511 return;
514 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
516 /* allow drag select of menu */
517 desc = &scr->icon_menu->menu->descriptor;
518 event->xbutton.send_event = True;
519 (*desc->handle_mousedown) (desc, event);
520 return;
523 if (event->xbutton.state & MOD_MASK)
524 wLowerFrame(icon->core);
525 else
526 wRaiseFrame(icon->core);
528 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
529 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
530 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
531 wwarning("pointer grab failed for appicon move");
534 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
535 dockable = 0;
536 else
537 dockable = canBeDocked(icon->owner);
539 wins[0] = icon->core->window;
540 wins[1] = scr->dock_shadow;
541 XRestackWindows(dpy, wins, 2);
542 if (superfluous) {
543 if (icon->pixmap != None)
544 ghost = MakeGhostIcon(scr, icon->pixmap);
545 else
546 ghost = MakeGhostIcon(scr, icon->core->window);
547 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
548 XClearWindow(dpy, scr->dock_shadow);
551 while (!done) {
552 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
553 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
554 switch (ev.type) {
555 case Expose:
556 WMHandleEvent(&ev);
557 break;
559 case EnterNotify:
560 /* It means the cursor moved so fast that it entered
561 * something else (if moving slowly, it would have
562 * stayed in the appIcon that is being moved. Ignore
563 * such "spurious" EnterNotifiy's */
564 break;
566 case MotionNotify:
567 hasMoved = True;
568 if (!grabbed) {
569 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
570 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
571 XChangeActivePointerGrab(dpy, ButtonMotionMask
572 | ButtonReleaseMask | ButtonPressMask,
573 wCursor[WCUR_MOVE], CurrentTime);
574 grabbed = 1;
575 } else {
576 break;
579 x = ev.xmotion.x_root - dx;
580 y = ev.xmotion.y_root - dy;
582 if (movingSingle) {
583 XMoveWindow(dpy, icon->core->window, x, y);
584 } else {
585 wAppIconMove(aicon, x, y);
588 if (dockable) {
589 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
590 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
591 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
593 if (scr->last_dock != scr->dock && collapsed) {
594 scr->last_dock->collapsed = 1;
595 wDockHideIcons(scr->last_dock);
596 collapsed = 0;
598 if (!collapsed && (collapsed = scr->dock->collapsed)) {
599 scr->dock->collapsed = 0;
600 wDockShowIcons(scr->dock);
603 if (scr->dock->auto_raise_lower)
604 wDockRaise(scr->dock);
606 scr->last_dock = scr->dock;
608 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
609 if (!docking) {
610 XMapWindow(dpy, scr->dock_shadow);
612 docking = 1;
613 } else if (workspace->clip &&
614 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
615 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
616 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
618 if (scr->last_dock != workspace->clip && collapsed) {
619 scr->last_dock->collapsed = 1;
620 wDockHideIcons(scr->last_dock);
621 collapsed = 0;
623 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
624 workspace->clip->collapsed = 0;
625 wDockShowIcons(workspace->clip);
628 if (workspace->clip->auto_raise_lower)
629 wDockRaise(workspace->clip);
631 scr->last_dock = workspace->clip;
633 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
634 if (!docking) {
635 XMapWindow(dpy, scr->dock_shadow);
637 docking = 1;
638 } else if (docking) {
639 XUnmapWindow(dpy, scr->dock_shadow);
640 docking = 0;
644 break;
646 case ButtonPress:
647 break;
649 case ButtonRelease:
650 if (ev.xbutton.button != clickButton)
651 break;
652 XUngrabPointer(dpy, CurrentTime);
654 if (docking) {
655 Bool docked;
657 /* icon is trying to be docked */
658 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
659 XUnmapWindow(dpy, scr->dock_shadow);
660 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
661 if (scr->last_dock->auto_collapse) {
662 collapsed = 0;
664 if (workspace->clip &&
665 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
666 wDockLower(workspace->clip);
668 if (!docked) {
669 /* If icon could not be docked, slide it back to the old
670 * position */
671 SlideWindow(icon->core->window, x, y, oldX, oldY);
673 } else {
674 if (movingSingle) {
675 /* move back to its place */
676 SlideWindow(icon->core->window, x, y, oldX, oldY);
677 wAppIconMove(aicon, oldX, oldY);
678 } else {
679 XMoveWindow(dpy, icon->core->window, x, y);
680 aicon->x_pos = x;
681 aicon->y_pos = y;
683 if (workspace->clip && workspace->clip->auto_raise_lower)
684 wDockLower(workspace->clip);
686 if (collapsed) {
687 scr->last_dock->collapsed = 1;
688 wDockHideIcons(scr->last_dock);
689 collapsed = 0;
691 if (superfluous) {
692 if (ghost != None)
693 XFreePixmap(dpy, ghost);
694 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
697 if (wPreferences.auto_arrange_icons)
698 wArrangeIcons(scr, True);
700 if (wPreferences.single_click && !hasMoved)
701 iconDblClick(desc, event);
703 done = 1;
704 break;