Remove NEWAPPICON #ifdefs
[wmaker-crm.git] / src / appicon.c
blobbc50607272622562553e158d04d5d659f8e68a35
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 "wcore.h"
33 #include "window.h"
34 #include "icon.h"
35 #include "appicon.h"
36 #include "actions.h"
37 #include "stacking.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "defaults.h"
41 #include "workspace.h"
42 #include "superfluous.h"
43 #include "menu.h"
44 #include "framewin.h"
45 #include "dialog.h"
46 #include "client.h"
47 #ifdef XDND
48 #include "xdnd.h"
49 #endif
52 * icon_file for the dock is got from the preferences file by
53 * using the classname/instancename
56 /**** Global variables ****/
57 extern Cursor wCursor[WCUR_LAST];
58 extern WPreferences wPreferences;
60 #define MOD_MASK wPreferences.modifier_mask
62 void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
63 static void iconDblClick(WObjDescriptor * desc, XEvent * event);
64 static void iconExpose(WObjDescriptor * desc, XEvent * event);
66 WAppIcon *wAppIconCreateForDock(WScreen * scr, char *command, char *wm_instance, char *wm_class, int tile)
68 WAppIcon *dicon;
69 char *path;
71 dicon = wmalloc(sizeof(WAppIcon));
72 wretain(dicon);
73 memset(dicon, 0, sizeof(WAppIcon));
74 dicon->yindex = -1;
75 dicon->xindex = -1;
77 dicon->prev = NULL;
78 dicon->next = scr->app_icon_list;
79 if (scr->app_icon_list) {
80 scr->app_icon_list->prev = dicon;
82 scr->app_icon_list = dicon;
84 if (command) {
85 dicon->command = wstrdup(command);
87 if (wm_class)
88 dicon->wm_class = wstrdup(wm_class);
89 if (wm_instance)
90 dicon->wm_instance = wstrdup(wm_instance);
92 path = wDefaultGetIconFile(scr, wm_instance, wm_class, True);
93 if (!path && command) {
94 wApplicationExtractDirPackIcon(scr, command, wm_instance, wm_class);
96 path = wDefaultGetIconFile(scr, wm_instance, wm_class, False);
99 if (path)
100 path = FindImage(wPreferences.icon_path, path);
102 dicon->icon = wIconCreateWithIconFile(scr, path, tile);
103 if (path)
104 wfree(path);
105 #ifdef XDND
106 wXDNDMakeAwareness(dicon->icon->core->window);
107 #endif
109 /* will be overriden by dock */
110 dicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
111 dicon->icon->core->descriptor.handle_expose = iconExpose;
112 dicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
113 dicon->icon->core->descriptor.parent = dicon;
114 AddToStackList(dicon->icon->core);
116 return dicon;
119 WAppIcon *wAppIconCreate(WWindow * leader_win)
121 WAppIcon *aicon;
122 WScreen *scr = leader_win->screen_ptr;
124 aicon = wmalloc(sizeof(WAppIcon));
125 wretain(aicon);
126 memset(aicon, 0, sizeof(WAppIcon));
128 aicon->yindex = -1;
129 aicon->xindex = -1;
131 aicon->prev = NULL;
132 aicon->next = scr->app_icon_list;
133 if (scr->app_icon_list) {
134 scr->app_icon_list->prev = aicon;
136 scr->app_icon_list = aicon;
138 if (leader_win->wm_class)
139 aicon->wm_class = wstrdup(leader_win->wm_class);
140 if (leader_win->wm_instance)
141 aicon->wm_instance = wstrdup(leader_win->wm_instance);
143 aicon->icon = wIconCreate(leader_win);
144 #ifdef XDND
145 wXDNDMakeAwareness(aicon->icon->core->window);
146 #endif
148 /* will be overriden if docked */
149 aicon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
150 aicon->icon->core->descriptor.handle_expose = iconExpose;
151 aicon->icon->core->descriptor.parent_type = WCLASS_APPICON;
152 aicon->icon->core->descriptor.parent = aicon;
153 AddToStackList(aicon->icon->core);
154 aicon->icon->show_title = 0;
155 wIconUpdate(aicon->icon);
157 return aicon;
160 void wAppIconDestroy(WAppIcon * aicon)
162 WScreen *scr = aicon->icon->core->screen_ptr;
164 RemoveFromStackList(aicon->icon->core);
165 wIconDestroy(aicon->icon);
166 if (aicon->command)
167 wfree(aicon->command);
168 #ifdef XDND
169 if (aicon->dnd_command)
170 wfree(aicon->dnd_command);
171 #endif
172 if (aicon->wm_instance)
173 wfree(aicon->wm_instance);
174 if (aicon->wm_class)
175 wfree(aicon->wm_class);
177 if (aicon == scr->app_icon_list) {
178 if (aicon->next)
179 aicon->next->prev = NULL;
180 scr->app_icon_list = aicon->next;
181 } else {
182 if (aicon->next)
183 aicon->next->prev = aicon->prev;
184 if (aicon->prev)
185 aicon->prev->next = aicon->next;
188 aicon->destroyed = 1;
189 wrelease(aicon);
192 static void drawCorner(WIcon * icon)
194 WScreen *scr = icon->core->screen_ptr;
195 XPoint points[3];
197 points[0].x = 1;
198 points[0].y = 1;
199 points[1].x = 12;
200 points[1].y = 1;
201 points[2].x = 1;
202 points[2].y = 12;
203 XFillPolygon(dpy, icon->core->window, scr->icon_title_texture->normal_gc,
204 points, 3, Convex, CoordModeOrigin);
205 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 0, 12);
206 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 0, 0, 12, 0);
207 /* drawing the second line gives a weird concave look. -Dan */
208 #if 0
209 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 1, 1, 1, 11);
210 XDrawLine(dpy, icon->core->window, scr->icon_title_texture->light_gc, 1, 1, 11, 1);
211 #endif
214 void wAppIconMove(WAppIcon * aicon, int x, int y)
216 XMoveWindow(dpy, aicon->icon->core->window, x, y);
217 aicon->x_pos = x;
218 aicon->y_pos = y;
221 #ifdef WS_INDICATOR
222 static void updateDockNumbers(WScreen * scr)
224 int length;
225 char *ws_numbers;
226 WAppIcon *dicon = scr->dock->icon_array[0];
228 ws_numbers = wmalloc(20);
229 snprintf(ws_numbers, 20, "%i [ %i ]", scr->current_workspace + 1, ((scr->current_workspace / 10) + 1));
230 length = strlen(ws_numbers);
232 XClearArea(dpy, dicon->icon->core->window, 2, 2, 50, WMFontHeight(scr->icon_title_font) + 1, False);
234 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->black,
235 scr->icon_title_font, 4, 3, ws_numbers, length);
237 WMDrawString(scr->wmscreen, dicon->icon->core->window, scr->white,
238 scr->icon_title_font, 3, 2, ws_numbers, length);
240 wfree(ws_numbers);
242 #endif /* WS_INDICATOR */
244 void wAppIconPaint(WAppIcon * aicon)
246 WApplication *wapp;
247 WScreen *scr = aicon->icon->core->screen_ptr;
249 if (aicon->icon->owner)
250 wapp = wApplicationOf(aicon->icon->owner->main_window);
251 else
252 wapp = NULL;
254 wIconPaint(aicon->icon);
256 # ifdef WS_INDICATOR
257 if (aicon->docked && scr->dock && scr->dock == aicon->dock && aicon->yindex == 0)
258 updateDockNumbers(scr);
259 # endif
260 if (scr->dock_dots && aicon->docked && !aicon->running && aicon->command != NULL) {
261 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
262 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
263 XCopyArea(dpy, scr->dock_dots->image, aicon->icon->core->window,
264 scr->copy_gc, 0, 0, scr->dock_dots->width, scr->dock_dots->height, 0, 0);
266 #ifdef HIDDENDOT
267 if (wapp && wapp->flags.hidden) {
268 XSetClipMask(dpy, scr->copy_gc, scr->dock_dots->mask);
269 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
270 XCopyArea(dpy, scr->dock_dots->image,
271 aicon->icon->core->window, scr->copy_gc, 0, 0, 7, scr->dock_dots->height, 0, 0);
273 #endif /* HIDDENDOT */
275 if (aicon->omnipresent)
276 drawCorner(aicon->icon);
278 XSetClipMask(dpy, scr->copy_gc, None);
279 if (aicon->launching) {
280 XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
281 0, 0, wPreferences.icon_size, wPreferences.icon_size);
285 #define canBeDocked(wwin) ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
287 static void hideCallback(WMenu * menu, WMenuEntry * entry)
289 WApplication *wapp = (WApplication *) entry->clientdata;
291 if (wapp->flags.hidden) {
292 wWorkspaceChange(menu->menu->screen_ptr, wapp->last_workspace);
293 wUnhideApplication(wapp, False, False);
294 } else {
295 wHideApplication(wapp);
299 static void unhideHereCallback(WMenu * menu, WMenuEntry * entry)
301 WApplication *wapp = (WApplication *) entry->clientdata;
303 wUnhideApplication(wapp, False, True);
306 static void setIconCallback(WMenu * menu, WMenuEntry * entry)
308 WAppIcon *icon = ((WApplication *) entry->clientdata)->app_icon;
309 char *file = NULL;
310 WScreen *scr;
311 int result;
313 assert(icon != NULL);
315 if (icon->editing)
316 return;
317 icon->editing = 1;
318 scr = icon->icon->core->screen_ptr;
320 wretain(icon);
322 result = wIconChooserDialog(scr, &file, icon->wm_instance, icon->wm_class);
324 if (result && !icon->destroyed) {
325 if (file && *file == 0) {
326 wfree(file);
327 file = NULL;
329 if (!wIconChangeImageFile(icon->icon, file)) {
330 wMessageDialog(scr, _("Error"),
331 _("Could not open specified icon file"), _("OK"), NULL, NULL);
332 } else {
333 wDefaultChangeIcon(scr, icon->wm_instance, icon->wm_class, file);
334 wAppIconPaint(icon);
336 if (file)
337 wfree(file);
339 icon->editing = 0;
340 wrelease(icon);
343 static void killCallback(WMenu * menu, WMenuEntry * entry)
345 WApplication *wapp = (WApplication *) entry->clientdata;
346 WFakeGroupLeader *fPtr;
347 char *buffer;
348 char *shortname;
349 char *basename(const char *shortname);
351 if (!WCHECK_STATE(WSTATE_NORMAL))
352 return;
354 WCHANGE_STATE(WSTATE_MODAL);
356 assert(entry->clientdata != NULL);
358 shortname = basename(wapp->app_icon->wm_instance);
360 buffer = wstrconcat(wapp->app_icon ? shortname : NULL,
361 _(" will be forcibly closed.\n"
362 "Any unsaved changes will be lost.\n" "Please confirm."));
364 fPtr = wapp->main_window_desc->fake_group;
366 wretain(wapp->main_window_desc);
367 if (wPreferences.dont_confirm_kill
368 || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
369 buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
370 if (fPtr != NULL) {
371 WWindow *wwin, *twin;
373 wwin = wapp->main_window_desc->screen_ptr->focused_window;
374 while (wwin) {
375 twin = wwin->prev;
376 if (wwin->fake_group == fPtr) {
377 wClientKill(wwin);
379 wwin = twin;
381 } else if (!wapp->main_window_desc->flags.destroyed) {
382 wClientKill(wapp->main_window_desc);
385 wrelease(wapp->main_window_desc);
387 wfree(buffer);
389 WCHANGE_STATE(WSTATE_NORMAL);
392 static WMenu *createApplicationMenu(WScreen * scr)
394 WMenu *menu;
396 menu = wMenuCreate(scr, NULL, False);
397 wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
398 wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
399 wMenuAddCallback(menu, _("Set Icon..."), setIconCallback, NULL);
400 wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
402 return menu;
405 static void openApplicationMenu(WApplication * wapp, int x, int y)
407 WMenu *menu;
408 WScreen *scr = wapp->main_window_desc->screen_ptr;
409 int i;
411 if (!scr->icon_menu) {
412 scr->icon_menu = createApplicationMenu(scr);
413 wfree(scr->icon_menu->entries[1]->text);
416 menu = scr->icon_menu;
418 if (wapp->flags.hidden) {
419 menu->entries[1]->text = _("Unhide");
420 } else {
421 menu->entries[1]->text = _("Hide");
424 menu->flags.realized = 0;
425 wMenuRealize(menu);
427 x -= menu->frame->core->width / 2;
428 if (x + menu->frame->core->width > scr->scr_width)
429 x = scr->scr_width - menu->frame->core->width;
430 if (x < 0)
431 x = 0;
433 /* set client data */
434 for (i = 0; i < menu->entry_no; i++) {
435 menu->entries[i]->clientdata = wapp;
437 wMenuMapAt(menu, x, y, False);
440 /******************************************************************/
442 static void iconExpose(WObjDescriptor * desc, XEvent * event)
444 wAppIconPaint(desc->parent);
447 static void iconDblClick(WObjDescriptor * desc, XEvent * event)
449 WAppIcon *aicon = desc->parent;
450 WApplication *wapp;
451 WScreen *scr = aicon->icon->core->screen_ptr;
452 int unhideHere;
454 assert(aicon->icon->owner != NULL);
456 wapp = wApplicationOf(aicon->icon->owner->main_window);
457 #ifdef DEBUG0
458 if (!wapp) {
459 wwarning("could not find application descriptor for app icon!!");
460 return;
462 #endif
464 unhideHere = (event->xbutton.state & ShiftMask);
466 /* go to the last workspace that the user worked on the app */
467 if (!unhideHere && wapp->last_workspace != scr->current_workspace)
468 wWorkspaceChange(scr, wapp->last_workspace);
470 wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
472 if (event->xbutton.state & MOD_MASK) {
473 wHideOtherApplications(aicon->icon->owner);
477 void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
479 WAppIcon *aicon = desc->parent;
480 WIcon *icon = aicon->icon;
481 XEvent ev;
482 int x = aicon->x_pos, y = aicon->y_pos;
483 int dx = event->xbutton.x, dy = event->xbutton.y;
484 int grabbed = 0;
485 int done = 0;
486 int superfluous = wPreferences.superfluous; /* we catch it to avoid problems */
487 WScreen *scr = icon->core->screen_ptr;
488 WWorkspace *workspace = scr->workspaces[scr->current_workspace];
489 int shad_x = 0, shad_y = 0, docking = 0, dockable, collapsed = 0;
490 int ix, iy;
491 int clickButton = event->xbutton.button;
492 Pixmap ghost = None;
493 Window wins[2];
494 Bool movingSingle = False;
495 int oldX = x;
496 int oldY = y;
497 Bool hasMoved = False;
499 if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
500 return;
502 if (IsDoubleClick(scr, event)) {
503 iconDblClick(desc, event);
504 return;
507 if (event->xbutton.button == Button3) {
508 WObjDescriptor *desc;
509 WApplication *wapp = wApplicationOf(aicon->icon->owner->main_window);
511 if (!wapp)
512 return;
514 if (event->xbutton.send_event &&
515 XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
516 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
517 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
518 wwarning("pointer grab failed for appicon menu");
519 return;
522 openApplicationMenu(wapp, event->xbutton.x_root, event->xbutton.y_root);
524 /* allow drag select of menu */
525 desc = &scr->icon_menu->menu->descriptor;
526 event->xbutton.send_event = True;
527 (*desc->handle_mousedown) (desc, event);
528 return;
530 #ifdef DEBUG
531 puts("Moving icon");
532 #endif
533 if (event->xbutton.state & MOD_MASK)
534 wLowerFrame(icon->core);
535 else
536 wRaiseFrame(icon->core);
538 if (XGrabPointer(dpy, icon->core->window, True, ButtonMotionMask
539 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
540 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
541 wwarning("pointer grab failed for appicon move");
544 if (wPreferences.flags.nodock && wPreferences.flags.noclip)
545 dockable = 0;
546 else
547 dockable = canBeDocked(icon->owner);
549 wins[0] = icon->core->window;
550 wins[1] = scr->dock_shadow;
551 XRestackWindows(dpy, wins, 2);
552 if (superfluous) {
553 if (icon->pixmap != None)
554 ghost = MakeGhostIcon(scr, icon->pixmap);
555 else
556 ghost = MakeGhostIcon(scr, icon->core->window);
557 XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
558 XClearWindow(dpy, scr->dock_shadow);
561 while (!done) {
562 WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
563 | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
564 switch (ev.type) {
565 case Expose:
566 WMHandleEvent(&ev);
567 break;
569 case EnterNotify:
570 /* It means the cursor moved so fast that it entered
571 * something else (if moving slowly, it would have
572 * stayed in the appIcon that is being moved. Ignore
573 * such "spurious" EnterNotifiy's */
574 break;
576 case MotionNotify:
577 hasMoved = True;
578 if (!grabbed) {
579 if (abs(dx - ev.xmotion.x) >= MOVE_THRESHOLD
580 || abs(dy - ev.xmotion.y) >= MOVE_THRESHOLD) {
581 XChangeActivePointerGrab(dpy, ButtonMotionMask
582 | ButtonReleaseMask | ButtonPressMask,
583 wCursor[WCUR_MOVE], CurrentTime);
584 grabbed = 1;
585 } else {
586 break;
589 x = ev.xmotion.x_root - dx;
590 y = ev.xmotion.y_root - dy;
592 if (movingSingle) {
593 XMoveWindow(dpy, icon->core->window, x, y);
594 } else {
595 wAppIconMove(aicon, x, y);
598 if (dockable) {
599 if (scr->dock && wDockSnapIcon(scr->dock, aicon, x, y, &ix, &iy, False)) {
600 shad_x = scr->dock->x_pos + ix * wPreferences.icon_size;
601 shad_y = scr->dock->y_pos + iy * wPreferences.icon_size;
603 if (scr->last_dock != scr->dock && collapsed) {
604 scr->last_dock->collapsed = 1;
605 wDockHideIcons(scr->last_dock);
606 collapsed = 0;
608 if (!collapsed && (collapsed = scr->dock->collapsed)) {
609 scr->dock->collapsed = 0;
610 wDockShowIcons(scr->dock);
613 if (scr->dock->auto_raise_lower)
614 wDockRaise(scr->dock);
616 scr->last_dock = scr->dock;
618 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
619 if (!docking) {
620 XMapWindow(dpy, scr->dock_shadow);
622 docking = 1;
623 } else if (workspace->clip &&
624 wDockSnapIcon(workspace->clip, aicon, x, y, &ix, &iy, False)) {
625 shad_x = workspace->clip->x_pos + ix * wPreferences.icon_size;
626 shad_y = workspace->clip->y_pos + iy * wPreferences.icon_size;
628 if (scr->last_dock != workspace->clip && collapsed) {
629 scr->last_dock->collapsed = 1;
630 wDockHideIcons(scr->last_dock);
631 collapsed = 0;
633 if (!collapsed && (collapsed = workspace->clip->collapsed)) {
634 workspace->clip->collapsed = 0;
635 wDockShowIcons(workspace->clip);
638 if (workspace->clip->auto_raise_lower)
639 wDockRaise(workspace->clip);
641 scr->last_dock = workspace->clip;
643 XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
644 if (!docking) {
645 XMapWindow(dpy, scr->dock_shadow);
647 docking = 1;
648 } else if (docking) {
649 XUnmapWindow(dpy, scr->dock_shadow);
650 docking = 0;
654 break;
656 case ButtonPress:
657 break;
659 case ButtonRelease:
660 if (ev.xbutton.button != clickButton)
661 break;
662 XUngrabPointer(dpy, CurrentTime);
664 if (docking) {
665 Bool docked;
667 /* icon is trying to be docked */
668 SlideWindow(icon->core->window, x, y, shad_x, shad_y);
669 XUnmapWindow(dpy, scr->dock_shadow);
670 docked = wDockAttachIcon(scr->last_dock, aicon, ix, iy);
671 if (scr->last_dock->auto_collapse) {
672 collapsed = 0;
674 if (workspace->clip &&
675 workspace->clip != scr->last_dock && workspace->clip->auto_raise_lower)
676 wDockLower(workspace->clip);
678 if (!docked) {
679 /* If icon could not be docked, slide it back to the old
680 * position */
681 SlideWindow(icon->core->window, x, y, oldX, oldY);
683 } else {
684 if (movingSingle) {
685 /* move back to its place */
686 SlideWindow(icon->core->window, x, y, oldX, oldY);
687 wAppIconMove(aicon, oldX, oldY);
688 } else {
689 XMoveWindow(dpy, icon->core->window, x, y);
690 aicon->x_pos = x;
691 aicon->y_pos = y;
693 if (workspace->clip && workspace->clip->auto_raise_lower)
694 wDockLower(workspace->clip);
696 if (collapsed) {
697 scr->last_dock->collapsed = 1;
698 wDockHideIcons(scr->last_dock);
699 collapsed = 0;
701 if (superfluous) {
702 if (ghost != None)
703 XFreePixmap(dpy, ghost);
704 XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
707 if (wPreferences.auto_arrange_icons)
708 wArrangeIcons(scr, True);
710 if (wPreferences.single_click && !hasMoved)
711 iconDblClick(desc, event);
713 done = 1;
714 break;
717 #ifdef DEBUG
718 puts("End icon move");
719 #endif