Update for 0.51.0
[wmaker-crm.git] / src / dockedapp.c
blob105916e93d89d70145b2e8db0a0a351929bd8bc7
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 && icon->command
94 && 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,
168 panel->editedIcon->wm_instance,
169 panel->editedIcon->wm_class);
171 panel->choosingIcon = 0;
172 if (!panel->destroyed) {
173 if (result) {
174 WMSetTextFieldText(panel->iconField, file);
175 free(file);
176 updateSettingsPanelIcon(panel);
179 WMSetButtonEnabled(panel->browseBtn, True);
180 } else {
181 /* kluge for the case, the user asked to close the panel before
182 * the icon chooser */
183 DestroyDockAppSettingsPanel(panel);
188 static void
189 panelBtnCallback(WMWidget *self, void *data)
191 WMButton *btn = self;
192 AppSettingsPanel *panel = (AppSettingsPanel*)data;
193 char *text;
194 int done;
196 done = 1;
197 if (panel->okBtn == btn) {
198 text = WMGetTextFieldText(panel->iconField);
199 if (text[0]==0) {
200 free(text);
201 text = NULL;
203 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
204 char *buf;
206 buf = wmalloc(strlen(text) + 64);
207 sprintf(buf, _("Could not open specified icon file:%s"), text);
208 wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf, _("OK"),
209 NULL, NULL);
210 free(buf);
211 done = 0;
212 return;
213 } else {
214 WAppIcon *aicon = panel->editedIcon;
216 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
217 wClipIconPaint(aicon);
218 else
219 wAppIconPaint(aicon);
221 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance,
222 aicon->wm_class, text);
224 if (text)
225 free(text);
227 /* cannot free text from this, because it will be not be duplicated
228 * in updateCommand */
229 text = WMGetTextFieldText(panel->commandField);
230 if (text[0]==0) {
231 free(text);
232 text = NULL;
234 updateCommand(panel->editedIcon, text);
235 #ifdef OFFIX_DND
236 /* cannot free text from this, because it will be not be duplicated
237 * in updateDNDCommand */
238 text = WMGetTextFieldText(panel->dndCommandField);
239 updateDNDCommand(panel->editedIcon, text);
240 #endif
242 panel->editedIcon->auto_launch =
243 WMGetButtonSelected(panel->autoLaunchBtn);
246 if (done)
247 DestroyDockAppSettingsPanel(panel);
251 #define PWIDTH 295
252 #define PHEIGHT 345
255 void
256 ShowDockAppSettingsPanel(WAppIcon *aicon)
258 AppSettingsPanel *panel;
259 WScreen *scr = aicon->icon->core->screen_ptr;
260 Window parent;
261 WMFont *font;
262 int x, y;
264 panel = wmalloc(sizeof(AppSettingsPanel));
265 memset(panel, 0, sizeof(AppSettingsPanel));
267 panel->editedIcon = aicon;
269 aicon->panel = panel;
270 aicon->editing = 1;
272 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
273 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
275 panel->iconLabel = WMCreateLabel(panel->win);
276 WMResizeWidget(panel->iconLabel, 64, 64);
277 WMMoveWidget(panel->iconLabel, 10, 10);
278 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
280 panel->nameLabel = WMCreateLabel(panel->win);
281 WMResizeWidget(panel->nameLabel, 190, 18);
282 WMMoveWidget(panel->nameLabel, 80, 35);
283 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
284 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
285 WMSetLabelFont(panel->nameLabel, font);
286 WMReleaseFont(font);
287 WMSetLabelText(panel->nameLabel, aicon->wm_class);
289 panel->autoLaunchBtn = WMCreateSwitchButton(panel->win);
290 WMResizeWidget(panel->autoLaunchBtn, PWIDTH-30, 20);
291 WMMoveWidget(panel->autoLaunchBtn, 15, 80);
292 WMSetButtonText(panel->autoLaunchBtn,
293 _("Start when WindowMaker is started"));
294 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
297 panel->commandFrame = WMCreateFrame(panel->win);
298 WMResizeWidget(panel->commandFrame, 275, 50);
299 WMMoveWidget(panel->commandFrame, 10, 105);
300 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
302 panel->commandField = WMCreateTextField(panel->commandFrame);
303 WMResizeWidget(panel->commandField, 256, 20);
304 WMMoveWidget(panel->commandField, 10, 20);
305 WMSetTextFieldText(panel->commandField, aicon->command);
307 panel->dndCommandFrame = WMCreateFrame(panel->win);
308 WMResizeWidget(panel->dndCommandFrame, 275, 70);
309 WMMoveWidget(panel->dndCommandFrame, 10, 165);
310 WMSetFrameTitle(panel->dndCommandFrame,
311 _("Command for files dropped with DND"));
313 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
314 WMResizeWidget(panel->dndCommandField, 256, 20);
315 WMMoveWidget(panel->dndCommandField, 10, 20);
317 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
318 WMResizeWidget(panel->dndCommandLabel, 256, 18);
319 WMMoveWidget(panel->dndCommandLabel, 10, 45);
320 #ifdef OFFIX_DND
321 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
322 WMSetLabelText(panel->dndCommandLabel,
323 _("%d will be replaced with the file name"));
324 #else
325 WMSetTextFieldEnabled(panel->dndCommandField, False);
326 WMSetLabelText(panel->dndCommandLabel,
327 _("DND support was not compiled in"));
328 #endif
330 panel->iconFrame = WMCreateFrame(panel->win);
331 WMResizeWidget(panel->iconFrame, 275, 50);
332 WMMoveWidget(panel->iconFrame, 10, 245);
333 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
335 panel->iconField = WMCreateTextField(panel->iconFrame);
336 WMResizeWidget(panel->iconField, 176, 20);
337 WMMoveWidget(panel->iconField, 10, 20);
338 WMSetTextFieldText(panel->iconField,
339 wDefaultGetIconFile(scr, aicon->wm_instance,
340 aicon->wm_class, True));
342 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
343 WMResizeWidget(panel->browseBtn, 70, 24);
344 WMMoveWidget(panel->browseBtn, 195, 18);
345 WMSetButtonText(panel->browseBtn, _("Browse..."));
346 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
349 panel->okBtn = WMCreateCommandButton(panel->win);
350 WMResizeWidget(panel->okBtn, 80, 26);
351 WMMoveWidget(panel->okBtn, 200, 308);
352 WMSetButtonText(panel->okBtn, _("OK"));
353 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
355 panel->cancelBtn = WMCreateCommandButton(panel->win);
356 WMResizeWidget(panel->cancelBtn, 80, 26);
357 WMMoveWidget(panel->cancelBtn, 110, 308);
358 WMSetButtonText(panel->cancelBtn, _("Cancel"));
359 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
361 WMRealizeWidget(panel->win);
362 WMMapSubwidgets(panel->win);
363 WMMapSubwidgets(panel->commandFrame);
364 WMMapSubwidgets(panel->dndCommandFrame);
365 WMMapSubwidgets(panel->iconFrame);
367 updateSettingsPanelIcon(panel);
369 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT,
370 0, 0, 0);
371 XSelectInput(dpy, parent, KeyPressMask|KeyReleaseMask);
373 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
375 y = aicon->y_pos;
376 if (y < 0)
377 y = 0;
378 else if (y + PWIDTH > scr->scr_height)
379 y = scr->scr_height - PHEIGHT - 30;
381 if (aicon->dock && aicon->dock->type == WM_DOCK) {
382 if (aicon->dock->on_right_side)
383 x = scr->scr_width/2;
384 else
385 x = scr->scr_width/2 - PWIDTH;
386 } else {
387 x = (scr->scr_width - PWIDTH)/2;
389 panel->wwin = wManageInternalWindow(scr, parent, None,
390 _("Docked Application Settings"),
391 x, y, PWIDTH, PHEIGHT);
393 panel->wwin->client_leader = WMWidgetXID(panel->win);
395 WMMapWidget(panel->win);
397 wWindowMap(panel->wwin);
401 void
402 DestroyDockAppSettingsPanel(AppSettingsPanel *panel)
404 if (!panel->destroyed) {
405 XUnmapWindow(dpy, panel->wwin->client_win);
406 XReparentWindow(dpy, panel->wwin->client_win,
407 panel->wwin->screen_ptr->root_win, 0, 0);
408 wUnmanageWindow(panel->wwin, False, False);
411 panel->destroyed = 1;
414 * kluge. If we destroy the panel before the icon chooser is closed,
415 * we will crash when it does close, trying to access something in the
416 * destroyed panel. Could use wretain()/wrelease() in the panel,
417 * but it is not working for some reason.
419 if (panel->choosingIcon)
420 return;
422 WMDestroyWidget(panel->win);
424 XDestroyWindow(dpy, panel->wwin->client_win);
426 panel->editedIcon->panel = NULL;
428 panel->editedIcon->editing = 0;
430 free(panel);