Updating to version 0.20.2
[wmaker-crm.git] / src / dockedapp.c
blob293ae99e5ca6c8e628fb89c72727ae421d058fb5
1 /* dockedapp.c- docked application settings panel
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1998 Alfredo K. Kojima
6 *
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 /* kluge */
74 unsigned int destroyed:1;
75 unsigned int choosingIcon:1;
76 } AppSettingsPanel;
79 void DestroyDockAppSettingsPanel(AppSettingsPanel *panel);
82 static void
83 updateCommand(WAppIcon *icon, char *command)
85 if (icon->command)
86 free(icon->command);
87 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
88 free(command);
89 command = NULL;
91 icon->command = command;
93 if (!icon->wm_class && !icon->wm_instance &&
94 icon->command && strlen(icon->command)>0) {
95 icon->forced_dock = 1;
100 #ifdef OFFIX_DND
101 static void
102 updateDNDCommand(WAppIcon *icon, char *command)
104 if (icon->dnd_command)
105 free(icon->dnd_command);
106 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
107 free(command);
108 command = NULL;
110 icon->dnd_command = command;
112 #endif /* OFFIX_DND */
115 static void
116 updateSettingsPanelIcon(AppSettingsPanel *panel)
118 char *file;
120 file = WMGetTextFieldText(panel->iconField);
121 if (!file)
122 WMSetLabelImage(panel->iconLabel, NULL);
123 else {
124 char *path;
126 path = FindImage(wPreferences.icon_path, file);
127 if (!path) {
128 wwarning(_("could not find icon %s, used in a docked application"),
129 file);
130 free(file);
131 WMSetLabelImage(panel->iconLabel, NULL);
132 return;
133 } else {
134 WMPixmap *pixmap;
135 RColor color;
137 color.red = 0xae;
138 color.green = 0xaa;
139 color.blue = 0xae;
140 color.alpha = 0;
141 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win),
142 path, &color);
143 if (!pixmap) {
144 WMSetLabelImage(panel->iconLabel, NULL);
145 } else {
146 WMSetLabelImage(panel->iconLabel, pixmap);
147 WMReleasePixmap(pixmap);
150 free(file);
151 free(path);
156 static void
157 chooseIconCallback(WMWidget *self, void *clientData)
159 char *file;
160 AppSettingsPanel *panel = (AppSettingsPanel*)clientData;
161 int result;
163 panel->choosingIcon = 1;
165 WMSetButtonEnabled(panel->browseBtn, False);
167 result = wIconChooserDialog(panel->wwin->screen_ptr, &file);
169 panel->choosingIcon = 0;
170 if (!panel->destroyed) {
171 if (result) {
172 WMSetTextFieldText(panel->iconField, file);
173 free(file);
174 updateSettingsPanelIcon(panel);
177 WMSetButtonEnabled(panel->browseBtn, True);
178 } else {
179 /* kluge for the case, the user asked to close the panel before
180 * the icon chooser */
181 DestroyDockAppSettingsPanel(panel);
186 static void
187 panelBtnCallback(WMWidget *self, void *data)
189 WMButton *btn = self;
190 AppSettingsPanel *panel = (AppSettingsPanel*)data;
191 char *text;
192 int done;
194 done = 1;
195 if (panel->okBtn == btn) {
196 text = WMGetTextFieldText(panel->iconField);
197 if (text[0]==0) {
198 free(text);
199 text = NULL;
201 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
202 char *buf;
204 buf = wmalloc(strlen(text) + 64);
205 sprintf(buf, _("Could not open specified icon file:%s"), text);
206 wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf, _("OK"),
207 NULL, NULL);
208 free(buf);
209 done = 0;
210 return;
211 } else {
212 WAppIcon *aicon = panel->editedIcon;
214 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
215 wClipIconPaint(aicon);
216 else
217 wAppIconPaint(aicon);
219 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance,
220 aicon->wm_class, text);
222 if (text)
223 free(text);
225 /* cannot free text from this, because it will be not be duplicated
226 * in updateCommand */
227 text = WMGetTextFieldText(panel->commandField);
228 if (text[0]==0) {
229 free(text);
230 text = NULL;
232 updateCommand(panel->editedIcon, text);
233 #ifdef OFFIX_DND
234 /* cannot free text from this, because it will be not be duplicated
235 * in updateDNDCommand */
236 text = WMGetTextFieldText(panel->dndCommandField);
237 updateDNDCommand(panel->editedIcon, text);
238 #endif
240 panel->editedIcon->auto_launch =
241 WMGetButtonSelected(panel->autoLaunchBtn);
244 if (done)
245 DestroyDockAppSettingsPanel(panel);
249 #define PWIDTH 295
250 #define PHEIGHT 345
253 void
254 ShowDockAppSettingsPanel(WAppIcon *aicon)
256 AppSettingsPanel *panel;
257 WScreen *scr = aicon->icon->core->screen_ptr;
258 Window parent;
259 WMFont *font;
260 int x, y;
262 panel = wmalloc(sizeof(AppSettingsPanel));
263 memset(panel, 0, sizeof(AppSettingsPanel));
265 panel->editedIcon = aicon;
267 aicon->panel = panel;
268 aicon->editing = 1;
270 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
271 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
273 panel->iconLabel = WMCreateLabel(panel->win);
274 WMResizeWidget(panel->iconLabel, 64, 64);
275 WMMoveWidget(panel->iconLabel, 10, 10);
276 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
278 panel->nameLabel = WMCreateLabel(panel->win);
279 WMResizeWidget(panel->nameLabel, 190, 18);
280 WMMoveWidget(panel->nameLabel, 80, 35);
281 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
282 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
283 WMSetLabelFont(panel->nameLabel, font);
284 WMReleaseFont(font);
285 WMSetLabelText(panel->nameLabel, aicon->wm_class);
287 panel->autoLaunchBtn = WMCreateSwitchButton(panel->win);
288 WMResizeWidget(panel->autoLaunchBtn, PWIDTH-30, 20);
289 WMMoveWidget(panel->autoLaunchBtn, 15, 80);
290 WMSetButtonText(panel->autoLaunchBtn,
291 _("Start when WindowMaker is started"));
292 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
295 panel->commandFrame = WMCreateFrame(panel->win);
296 WMResizeWidget(panel->commandFrame, 275, 50);
297 WMMoveWidget(panel->commandFrame, 10, 105);
298 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
300 panel->commandField = WMCreateTextField(panel->commandFrame);
301 WMResizeWidget(panel->commandField, 256, 20);
302 WMMoveWidget(panel->commandField, 10, 20);
303 WMSetTextFieldText(panel->commandField, aicon->command);
305 panel->dndCommandFrame = WMCreateFrame(panel->win);
306 WMResizeWidget(panel->dndCommandFrame, 275, 70);
307 WMMoveWidget(panel->dndCommandFrame, 10, 165);
308 WMSetFrameTitle(panel->dndCommandFrame,
309 _("Command for files dropped with DND"));
311 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
312 WMResizeWidget(panel->dndCommandField, 256, 20);
313 WMMoveWidget(panel->dndCommandField, 10, 20);
315 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
316 WMResizeWidget(panel->dndCommandLabel, 256, 18);
317 WMMoveWidget(panel->dndCommandLabel, 10, 45);
318 #ifdef OFFIX_DND
319 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
320 WMSetLabelText(panel->dndCommandLabel,
321 _("%d will be replaced with the file name"));
322 #else
323 WMSetTextFieldEnabled(panel->dndCommandField, False);
324 WMSetLabelText(panel->dndCommandLabel,
325 _("DND support was not compiled in"));
326 #endif
328 panel->iconFrame = WMCreateFrame(panel->win);
329 WMResizeWidget(panel->iconFrame, 275, 50);
330 WMMoveWidget(panel->iconFrame, 10, 245);
331 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
333 panel->iconField = WMCreateTextField(panel->iconFrame);
334 WMResizeWidget(panel->iconField, 176, 20);
335 WMMoveWidget(panel->iconField, 10, 20);
336 WMSetTextFieldText(panel->iconField,
337 wDefaultGetIconFile(scr, aicon->wm_instance,
338 aicon->wm_class, True));
340 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
341 WMResizeWidget(panel->browseBtn, 70, 24);
342 WMMoveWidget(panel->browseBtn, 195, 18);
343 WMSetButtonText(panel->browseBtn, _("Browse..."));
344 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
347 panel->okBtn = WMCreateCommandButton(panel->win);
348 WMResizeWidget(panel->okBtn, 80, 26);
349 WMMoveWidget(panel->okBtn, 200, 308);
350 WMSetButtonText(panel->okBtn, _("OK"));
351 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
353 panel->cancelBtn = WMCreateCommandButton(panel->win);
354 WMResizeWidget(panel->cancelBtn, 80, 26);
355 WMMoveWidget(panel->cancelBtn, 110, 308);
356 WMSetButtonText(panel->cancelBtn, _("Cancel"));
357 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
359 WMRealizeWidget(panel->win);
360 WMMapSubwidgets(panel->win);
361 WMMapSubwidgets(panel->commandFrame);
362 WMMapSubwidgets(panel->dndCommandFrame);
363 WMMapSubwidgets(panel->iconFrame);
365 updateSettingsPanelIcon(panel);
367 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT,
368 0, 0, 0);
369 XSelectInput(dpy, parent, KeyPressMask|KeyReleaseMask);
371 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
373 y = aicon->y_pos;
374 if (y < 0)
375 y = 0;
376 else if (y + PWIDTH > scr->scr_height)
377 y = scr->scr_height - PHEIGHT - 30;
379 if (aicon->dock && aicon->dock->type == WM_DOCK) {
380 if (aicon->dock->on_right_side)
381 x = scr->scr_width/2;
382 else
383 x = scr->scr_width/2 - PWIDTH;
384 } else {
385 x = (scr->scr_width - PWIDTH)/2;
387 panel->wwin = wManageInternalWindow(scr, parent, None,
388 _("Docked Application Settings"),
389 x, y, PWIDTH, PHEIGHT);
391 panel->wwin->client_leader = WMWidgetXID(panel->win);
393 WMMapWidget(panel->win);
395 wWindowMap(panel->wwin);
399 void
400 DestroyDockAppSettingsPanel(AppSettingsPanel *panel)
402 if (!panel->destroyed) {
403 XUnmapWindow(dpy, panel->wwin->client_win);
404 XReparentWindow(dpy, panel->wwin->client_win,
405 panel->wwin->screen_ptr->root_win, 0, 0);
406 wUnmanageWindow(panel->wwin, False);
409 panel->destroyed = 1;
412 * kluge. If we destroy the panel before the icon chooser is closed,
413 * we will crash when it does close, trying to access something in the
414 * destroyed panel. Could use wretain()/wrelease() in the panel,
415 * but it is not working for some reason.
417 if (panel->choosingIcon)
418 return;
420 WMDestroyWidget(panel->win);
422 XDestroyWindow(dpy, panel->wwin->client_win);
424 panel->editedIcon->panel = NULL;
426 panel->editedIcon->editing = 0;
428 free(panel);