Fixed crashing bug related to omnipresent icons in clip (fixes bug #1167)
[wmaker-crm.git] / src / dockedapp.c
blob5e247ad4d8b4f412706b25c9510fb8a633287379
1 /* dockedapp.c- docked application settings panel
3 * Window Maker window manager
5 * Copyright (c) 1998 Alfredo K. Kojima
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * 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 "dock.h"
37 #include "dialog.h"
38 #include "funcs.h"
39 #include "defaults.h"
40 #include "framewin.h"
43 /**** Global variables ****/
44 extern WPreferences wPreferences;
48 typedef struct _AppSettingsPanel {
49 WMWindow *win;
50 WAppIcon *editedIcon;
52 WWindow *wwin;
54 WMLabel *iconLabel;
55 WMLabel *nameLabel;
57 WMFrame *commandFrame;
58 WMTextField *commandField;
60 WMFrame *dndCommandFrame;
61 WMTextField *dndCommandField;
62 WMLabel *dndCommandLabel;
64 WMFrame *iconFrame;
65 WMTextField *iconField;
66 WMButton *browseBtn;
68 WMButton *autoLaunchBtn;
70 WMButton *okBtn;
71 WMButton *cancelBtn;
73 Window parent;
75 /* kluge */
76 unsigned int destroyed:1;
77 unsigned int choosingIcon:1;
78 } AppSettingsPanel;
81 void DestroyDockAppSettingsPanel(AppSettingsPanel *panel);
84 static void
85 updateCommand(WAppIcon *icon, char *command)
87 if (icon->command)
88 free(icon->command);
89 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
90 free(command);
91 command = NULL;
93 icon->command = command;
95 if (!icon->wm_class && !icon->wm_instance && icon->command
96 && strlen(icon->command)>0) {
97 icon->forced_dock = 1;
102 #ifdef OFFIX_DND
103 static void
104 updateDNDCommand(WAppIcon *icon, char *command)
106 if (icon->dnd_command)
107 free(icon->dnd_command);
108 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
109 free(command);
110 command = NULL;
112 icon->dnd_command = command;
114 #endif /* OFFIX_DND */
117 static void
118 updateSettingsPanelIcon(AppSettingsPanel *panel)
120 char *file;
122 file = WMGetTextFieldText(panel->iconField);
123 if (!file)
124 WMSetLabelImage(panel->iconLabel, NULL);
125 else {
126 char *path;
128 path = FindImage(wPreferences.icon_path, file);
129 if (!path) {
130 wwarning(_("could not find icon %s, used in a docked application"),
131 file);
132 free(file);
133 WMSetLabelImage(panel->iconLabel, NULL);
134 return;
135 } else {
136 WMPixmap *pixmap;
137 RColor color;
139 color.red = 0xae;
140 color.green = 0xaa;
141 color.blue = 0xae;
142 color.alpha = 0;
143 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win),
144 path, &color);
145 if (!pixmap) {
146 WMSetLabelImage(panel->iconLabel, NULL);
147 } else {
148 WMSetLabelImage(panel->iconLabel, pixmap);
149 WMReleasePixmap(pixmap);
152 free(file);
153 free(path);
158 static void
159 chooseIconCallback(WMWidget *self, void *clientData)
161 char *file;
162 AppSettingsPanel *panel = (AppSettingsPanel*)clientData;
163 int result;
165 panel->choosingIcon = 1;
167 WMSetButtonEnabled(panel->browseBtn, False);
169 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
170 panel->editedIcon->wm_instance,
171 panel->editedIcon->wm_class);
173 panel->choosingIcon = 0;
174 if (!panel->destroyed) {
175 if (result) {
176 WMSetTextFieldText(panel->iconField, file);
177 free(file);
178 updateSettingsPanelIcon(panel);
181 WMSetButtonEnabled(panel->browseBtn, True);
182 } else {
183 /* kluge for the case, the user asked to close the panel before
184 * the icon chooser */
185 DestroyDockAppSettingsPanel(panel);
190 static void
191 panelBtnCallback(WMWidget *self, void *data)
193 WMButton *btn = self;
194 AppSettingsPanel *panel = (AppSettingsPanel*)data;
195 char *text;
196 int done;
198 done = 1;
199 if (panel->okBtn == btn) {
200 text = WMGetTextFieldText(panel->iconField);
201 if (text[0]==0) {
202 free(text);
203 text = NULL;
205 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
206 char *buf;
208 buf = wmalloc(strlen(text) + 64);
209 sprintf(buf, _("Could not open specified icon file: %s"), text);
210 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
211 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
212 if (text)
213 free(text);
214 free(buf);
215 return;
217 free(buf);
218 } else {
219 WAppIcon *aicon = panel->editedIcon;
221 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
222 wClipIconPaint(aicon);
223 else
224 wAppIconPaint(aicon);
226 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance,
227 aicon->wm_class, text);
229 if (text)
230 free(text);
232 /* cannot free text from this, because it will be not be duplicated
233 * in updateCommand */
234 text = WMGetTextFieldText(panel->commandField);
235 if (text[0]==0) {
236 free(text);
237 text = NULL;
239 updateCommand(panel->editedIcon, text);
240 #ifdef OFFIX_DND
241 /* cannot free text from this, because it will be not be duplicated
242 * in updateDNDCommand */
243 text = WMGetTextFieldText(panel->dndCommandField);
244 updateDNDCommand(panel->editedIcon, text);
245 #endif
247 panel->editedIcon->auto_launch =
248 WMGetButtonSelected(panel->autoLaunchBtn);
251 if (done)
252 DestroyDockAppSettingsPanel(panel);
256 #define PWIDTH 295
257 #define PHEIGHT 345
260 void
261 ShowDockAppSettingsPanel(WAppIcon *aicon)
263 AppSettingsPanel *panel;
264 WScreen *scr = aicon->icon->core->screen_ptr;
265 Window parent;
266 WMFont *font;
267 int x, y;
269 panel = wmalloc(sizeof(AppSettingsPanel));
270 memset(panel, 0, sizeof(AppSettingsPanel));
272 panel->editedIcon = aicon;
274 aicon->panel = panel;
275 aicon->editing = 1;
277 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
278 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
280 panel->iconLabel = WMCreateLabel(panel->win);
281 WMResizeWidget(panel->iconLabel, 64, 64);
282 WMMoveWidget(panel->iconLabel, 10, 10);
283 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
285 panel->nameLabel = WMCreateLabel(panel->win);
286 WMResizeWidget(panel->nameLabel, 190, 18);
287 WMMoveWidget(panel->nameLabel, 80, 35);
288 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
289 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
290 WMSetLabelFont(panel->nameLabel, font);
291 WMReleaseFont(font);
292 WMSetLabelText(panel->nameLabel, aicon->wm_class);
294 panel->autoLaunchBtn = WMCreateSwitchButton(panel->win);
295 WMResizeWidget(panel->autoLaunchBtn, PWIDTH-30, 20);
296 WMMoveWidget(panel->autoLaunchBtn, 15, 80);
297 WMSetButtonText(panel->autoLaunchBtn,
298 _("Start when WindowMaker is started"));
299 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
301 panel->commandFrame = WMCreateFrame(panel->win);
302 WMResizeWidget(panel->commandFrame, 275, 50);
303 WMMoveWidget(panel->commandFrame, 10, 105);
304 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
306 panel->commandField = WMCreateTextField(panel->commandFrame);
307 WMResizeWidget(panel->commandField, 256, 20);
308 WMMoveWidget(panel->commandField, 10, 20);
309 WMSetTextFieldText(panel->commandField, aicon->command);
311 panel->dndCommandFrame = WMCreateFrame(panel->win);
312 WMResizeWidget(panel->dndCommandFrame, 275, 70);
313 WMMoveWidget(panel->dndCommandFrame, 10, 165);
314 WMSetFrameTitle(panel->dndCommandFrame,
315 _("Command for files dropped with DND"));
317 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
318 WMResizeWidget(panel->dndCommandField, 256, 20);
319 WMMoveWidget(panel->dndCommandField, 10, 20);
321 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
322 WMResizeWidget(panel->dndCommandLabel, 256, 18);
323 WMMoveWidget(panel->dndCommandLabel, 10, 45);
324 #ifdef OFFIX_DND
325 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
326 WMSetLabelText(panel->dndCommandLabel,
327 _("%d will be replaced with the file name"));
328 #else
329 WMSetTextFieldEditable(panel->dndCommandField, False);
330 WMSetLabelText(panel->dndCommandLabel,
331 _("DND support was not compiled in"));
332 #endif
334 panel->iconFrame = WMCreateFrame(panel->win);
335 WMResizeWidget(panel->iconFrame, 275, 50);
336 WMMoveWidget(panel->iconFrame, 10, 245);
337 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
339 panel->iconField = WMCreateTextField(panel->iconFrame);
340 WMResizeWidget(panel->iconField, 176, 20);
341 WMMoveWidget(panel->iconField, 10, 20);
342 WMSetTextFieldText(panel->iconField,
343 wDefaultGetIconFile(scr, aicon->wm_instance,
344 aicon->wm_class, True));
346 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
347 WMResizeWidget(panel->browseBtn, 70, 24);
348 WMMoveWidget(panel->browseBtn, 195, 18);
349 WMSetButtonText(panel->browseBtn, _("Browse..."));
350 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
353 panel->okBtn = WMCreateCommandButton(panel->win);
354 WMResizeWidget(panel->okBtn, 80, 26);
355 WMMoveWidget(panel->okBtn, 200, 308);
356 WMSetButtonText(panel->okBtn, _("OK"));
357 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
359 panel->cancelBtn = WMCreateCommandButton(panel->win);
360 WMResizeWidget(panel->cancelBtn, 80, 26);
361 WMMoveWidget(panel->cancelBtn, 110, 308);
362 WMSetButtonText(panel->cancelBtn, _("Cancel"));
363 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
365 WMRealizeWidget(panel->win);
366 WMMapSubwidgets(panel->win);
367 WMMapSubwidgets(panel->commandFrame);
368 WMMapSubwidgets(panel->dndCommandFrame);
369 WMMapSubwidgets(panel->iconFrame);
371 updateSettingsPanelIcon(panel);
373 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT,
374 0, 0, 0);
375 XSelectInput(dpy, parent, KeyPressMask|KeyReleaseMask);
377 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
379 y = aicon->y_pos;
380 if (y < 0)
381 y = 0;
382 else if (y + PWIDTH > scr->scr_height)
383 y = scr->scr_height - PHEIGHT - 30;
385 if (aicon->dock && aicon->dock->type == WM_DOCK) {
386 if (aicon->dock->on_right_side)
387 x = scr->scr_width/2;
388 else
389 x = scr->scr_width/2 - PWIDTH;
390 } else {
391 x = (scr->scr_width - PWIDTH)/2;
393 panel->wwin = wManageInternalWindow(scr, parent, None,
394 _("Docked Application Settings"),
395 x, y, PWIDTH, PHEIGHT);
397 panel->wwin->client_leader = WMWidgetXID(panel->win);
399 panel->parent = parent;
401 WMMapWidget(panel->win);
403 wWindowMap(panel->wwin);
407 void
408 DestroyDockAppSettingsPanel(AppSettingsPanel *panel)
410 if (!panel->destroyed) {
411 XUnmapWindow(dpy, panel->wwin->client_win);
412 XReparentWindow(dpy, panel->wwin->client_win,
413 panel->wwin->screen_ptr->root_win, 0, 0);
414 wUnmanageWindow(panel->wwin, False, False);
417 panel->destroyed = 1;
420 * kluge. If we destroy the panel before the icon chooser is closed,
421 * we will crash when it does close, trying to access something in the
422 * destroyed panel. Could use wretain()/wrelease() in the panel,
423 * but it is not working for some reason.
425 if (panel->choosingIcon)
426 return;
428 WMDestroyWidget(panel->win);
430 XDestroyWindow(dpy, panel->parent);
432 panel->editedIcon->panel = NULL;
434 panel->editedIcon->editing = 0;
436 free(panel);