wmaker: Added 'const' attribute to local function 'getMaxStringWidth'
[wmaker-crm.git] / src / application.c
blob1bb0890166e4ef1ba1406c2178c44e23aa2034ab
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <string.h>
26 #include "WindowMaker.h"
27 #include "menu.h"
28 #include "window.h"
29 #ifdef USER_MENU
30 #include "usermenu.h"
31 #endif /* USER_MENU */
32 #include "appicon.h"
33 #include "application.h"
34 #include "appmenu.h"
35 #include "properties.h"
36 #include "workspace.h"
37 #include "dock.h"
38 #include "defaults.h"
40 /******** Global variables ********/
42 extern XContext wAppWinContext;
43 extern XContext wWinContext;
45 /******** Local variables ********/
47 static WWindow *makeMainWindow(WScreen * scr, Window window)
49 WWindow *wwin;
50 XWindowAttributes attr;
52 if (!XGetWindowAttributes(dpy, window, &attr))
53 return NULL;
55 wwin = wWindowCreate();
56 wwin->screen_ptr = scr;
57 wwin->client_win = window;
58 wwin->main_window = window;
59 wwin->wm_hints = XGetWMHints(dpy, window);
61 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
63 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
64 &wwin->defined_user_flags, True);
66 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask | StructureNotifyMask);
67 return wwin;
70 WApplication *wApplicationOf(Window window)
72 WApplication *wapp;
74 if (window == None)
75 return NULL;
76 if (XFindContext(dpy, window, wAppWinContext, (XPointer *) & wapp) != XCSUCCESS)
77 return NULL;
78 return wapp;
81 WApplication *wApplicationCreate(WWindow * wwin)
83 WScreen *scr = wwin->screen_ptr;
84 Window main_window = wwin->main_window;
85 WApplication *wapp;
86 WWindow *leader;
88 if (main_window == None || main_window == scr->root_win)
89 return NULL;
92 Window root;
93 int foo;
94 unsigned int bar;
95 /* check if the window is valid */
96 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar))
97 return NULL;
100 wapp = wApplicationOf(main_window);
101 if (wapp) {
102 wapp->refcount++;
103 if (wapp->app_icon && wapp->app_icon->docked &&
104 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group)
105 wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
107 return wapp;
110 wapp = wmalloc(sizeof(WApplication));
112 wapp->refcount = 1;
113 wapp->last_focused = NULL;
114 wapp->urgent_bounce_timer = NULL;
116 wapp->last_workspace = 0;
118 wapp->main_window = main_window;
119 wapp->main_window_desc = makeMainWindow(scr, main_window);
120 if (!wapp->main_window_desc) {
121 wfree(wapp);
122 return NULL;
125 wapp->main_window_desc->fake_group = wwin->fake_group;
126 wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
128 leader = wWindowFor(main_window);
129 if (leader)
130 leader->main_window = main_window;
132 wapp->menu = wAppMenuGet(scr, main_window);
133 #ifdef USER_MENU
134 if (!wapp->menu)
135 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
136 #endif
138 /* Set application wide attributes from the leader */
139 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
140 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
142 /* application descriptor */
143 XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
145 create_appicon_for_application(wapp, wwin);
147 return wapp;
150 void wApplicationDestroy(WApplication * wapp)
152 WWindow *wwin;
153 WScreen *scr;
155 if (!wapp)
156 return;
158 wapp->refcount--;
159 if (wapp->refcount > 0)
160 return;
162 if (wapp->urgent_bounce_timer) {
163 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
164 wapp->urgent_bounce_timer = NULL;
166 if (wapp->flags.bouncing) {
167 /* event.c:handleDestroyNotify forced this destroy
168 and thereby overlooked the bounce callback */
169 wapp->refcount = 1;
170 return;
173 scr = wapp->main_window_desc->screen_ptr;
175 if (wapp == scr->wapp_list) {
176 if (wapp->next)
177 wapp->next->prev = NULL;
178 scr->wapp_list = wapp->next;
179 } else {
180 if (wapp->next)
181 wapp->next->prev = wapp->prev;
182 if (wapp->prev)
183 wapp->prev->next = wapp->next;
186 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
187 wAppMenuDestroy(wapp->menu);
189 /* Remove application icon */
190 removeAppIconFor(wapp);
192 wwin = wWindowFor(wapp->main_window_desc->client_win);
194 wWindowDestroy(wapp->main_window_desc);
195 if (wwin) {
196 /* undelete client window context that was deleted in
197 * wWindowDestroy */
198 XSaveContext(dpy, wwin->client_win, wWinContext, (XPointer) & wwin->client_descriptor);
200 wfree(wapp);
203 void wApplicationActivate(WApplication *wapp)
205 if (wapp->app_icon) {
206 wIconSetHighlited(wapp->app_icon->icon, True);
207 wAppIconPaint(wapp->app_icon);
211 void wApplicationDeactivate(WApplication *wapp)
213 if (wapp->app_icon) {
214 wIconSetHighlited(wapp->app_icon->icon, False);
215 wAppIconPaint(wapp->app_icon);