Add history to some dialog boxes
[wmaker-crm.git] / src / dockedapp.c
blob957f393491d3850c356450d01a9326f7202c3f43
1 /* dockedapp.c- docked application settings panel
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 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.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "WindowMaker.h"
31 #include "wcore.h"
32 #include "window.h"
33 #include "icon.h"
34 #include "appicon.h"
35 #include "dock.h"
36 #include "dialog.h"
37 #include "funcs.h"
38 #include "defaults.h"
39 #include "framewin.h"
40 #include "xinerama.h"
42 /**** Global variables ****/
43 extern WPreferences wPreferences;
45 typedef struct _AppSettingsPanel {
46 WMWindow *win;
47 WAppIcon *editedIcon;
49 WWindow *wwin;
51 WMLabel *iconLabel;
52 WMLabel *nameLabel;
54 WMFrame *commandFrame;
55 WMTextField *commandField;
57 WMFrame *dndCommandFrame;
58 WMTextField *dndCommandField;
59 WMLabel *dndCommandLabel;
61 WMFrame *pasteCommandFrame;
62 WMTextField *pasteCommandField;
63 WMLabel *pasteCommandLabel;
65 WMFrame *iconFrame;
66 WMTextField *iconField;
67 WMButton *browseBtn;
69 WMButton *autoLaunchBtn;
70 WMButton *lockBtn;
72 WMButton *okBtn;
73 WMButton *cancelBtn;
75 Window parent;
77 /* kluge */
78 unsigned int destroyed:1;
79 unsigned int choosingIcon:1;
80 } AppSettingsPanel;
82 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel);
84 static void updateCommand(WAppIcon * icon, char *command)
86 if (icon->command)
87 wfree(icon->command);
88 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
89 wfree(command);
90 command = NULL;
92 icon->command = command;
94 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
95 icon->forced_dock = 1;
99 static void updatePasteCommand(WAppIcon * icon, char *command)
101 if (icon->paste_command)
102 wfree(icon->paste_command);
103 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
104 wfree(command);
105 command = NULL;
107 icon->paste_command = command;
110 #ifdef XDND
111 static void updateDNDCommand(WAppIcon * icon, char *command)
113 if (icon->dnd_command)
114 wfree(icon->dnd_command);
115 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
116 wfree(command);
117 command = NULL;
119 icon->dnd_command = command;
121 #endif /* XDND */
123 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
125 char *file;
127 file = WMGetTextFieldText(panel->iconField);
128 if (!file)
129 WMSetLabelImage(panel->iconLabel, NULL);
130 else {
131 char *path;
133 path = FindImage(wPreferences.icon_path, file);
134 if (!path) {
135 wwarning(_("could not find icon %s, used in a docked application"), file);
136 wfree(file);
137 WMSetLabelImage(panel->iconLabel, NULL);
138 return;
139 } else {
140 WMPixmap *pixmap;
141 RColor color;
143 color.red = 0xae;
144 color.green = 0xaa;
145 color.blue = 0xae;
146 color.alpha = 0;
147 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
148 if (!pixmap) {
149 WMSetLabelImage(panel->iconLabel, NULL);
150 } else {
151 WMSetLabelImage(panel->iconLabel, pixmap);
152 WMReleasePixmap(pixmap);
155 wfree(file);
156 wfree(path);
160 static void chooseIconCallback(WMWidget * self, void *clientData)
162 char *file;
163 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
164 int result;
166 panel->choosingIcon = 1;
168 WMSetButtonEnabled(panel->browseBtn, False);
170 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
171 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
173 panel->choosingIcon = 0;
174 if (!panel->destroyed) {
175 if (result) {
176 WMSetTextFieldText(panel->iconField, file);
177 wfree(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);
189 static void 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 wfree(text);
201 text = NULL;
203 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
204 char *buf;
205 int len = strlen(text) + 64;
207 buf = wmalloc(len);
208 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
209 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
210 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
211 if (text)
212 wfree(text);
213 wfree(buf);
214 return;
216 wfree(buf);
217 } else {
218 WAppIcon *aicon = panel->editedIcon;
220 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
221 wClipIconPaint(aicon);
222 else
223 wAppIconPaint(aicon);
225 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance, aicon->wm_class, text);
227 if (text)
228 wfree(text);
230 /* cannot free text from this, because it will be not be duplicated
231 * in updateCommand */
232 text = WMGetTextFieldText(panel->commandField);
233 if (text[0] == 0) {
234 wfree(text);
235 text = NULL;
237 updateCommand(panel->editedIcon, text);
238 #ifdef XDND
239 /* cannot free text from this, because it will be not be duplicated
240 * in updateDNDCommand */
241 text = WMGetTextFieldText(panel->dndCommandField);
242 updateDNDCommand(panel->editedIcon, text);
243 #endif
244 text = WMGetTextFieldText(panel->pasteCommandField);
245 updatePasteCommand(panel->editedIcon, text);
247 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
249 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
252 if (done)
253 DestroyDockAppSettingsPanel(panel);
256 #define PWIDTH 295
257 #define PHEIGHT 430
259 void ShowDockAppSettingsPanel(WAppIcon * aicon)
261 AppSettingsPanel *panel;
262 WScreen *scr = aicon->icon->core->screen_ptr;
263 Window parent;
264 WMFont *font;
265 int x, y;
266 WMBox *vbox;
268 panel = wmalloc(sizeof(AppSettingsPanel));
269 memset(panel, 0, sizeof(AppSettingsPanel));
271 panel->editedIcon = aicon;
273 aicon->panel = panel;
274 aicon->editing = 1;
276 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
277 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
279 panel->iconLabel = WMCreateLabel(panel->win);
280 WMResizeWidget(panel->iconLabel, 64, 64);
281 WMMoveWidget(panel->iconLabel, 10, 10);
282 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
284 panel->nameLabel = WMCreateLabel(panel->win);
285 WMResizeWidget(panel->nameLabel, 190, 18);
286 WMMoveWidget(panel->nameLabel, 80, 35);
287 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
288 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
289 WMSetLabelFont(panel->nameLabel, font);
290 WMReleaseFont(font);
291 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
292 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
293 else
294 WMSetLabelText(panel->nameLabel, aicon->wm_class);
296 vbox = WMCreateBox(panel->win);
297 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
298 WMMoveWidget(vbox, 10, 84);
300 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
301 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
302 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
303 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
305 panel->lockBtn = WMCreateSwitchButton(vbox);
306 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
307 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
308 WMSetButtonSelected(panel->lockBtn, aicon->lock);
310 panel->commandFrame = WMCreateFrame(vbox);
311 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
312 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
314 panel->commandField = WMCreateTextField(panel->commandFrame);
315 WMResizeWidget(panel->commandField, 256, 20);
316 WMMoveWidget(panel->commandField, 10, 20);
317 WMSetTextFieldText(panel->commandField, aicon->command);
319 WMMapSubwidgets(panel->commandFrame);
321 panel->pasteCommandFrame = WMCreateFrame(vbox);
322 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
323 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
325 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
326 WMResizeWidget(panel->pasteCommandField, 256, 20);
327 WMMoveWidget(panel->pasteCommandField, 10, 20);
329 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
330 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
331 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
333 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
334 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
335 WMMapSubwidgets(panel->pasteCommandFrame);
337 panel->dndCommandFrame = WMCreateFrame(vbox);
338 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
339 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
341 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
342 WMResizeWidget(panel->dndCommandField, 256, 20);
343 WMMoveWidget(panel->dndCommandField, 10, 20);
345 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
346 WMResizeWidget(panel->dndCommandLabel, 256, 18);
347 WMMoveWidget(panel->dndCommandLabel, 10, 45);
348 #ifdef XDND
349 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
350 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
351 #else
352 WMSetTextFieldEditable(panel->dndCommandField, False);
353 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
354 #endif
355 WMMapSubwidgets(panel->dndCommandFrame);
357 panel->iconFrame = WMCreateFrame(vbox);
358 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
359 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
361 panel->iconField = WMCreateTextField(panel->iconFrame);
362 WMResizeWidget(panel->iconField, 176, 20);
363 WMMoveWidget(panel->iconField, 10, 20);
364 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(scr, aicon->wm_instance, aicon->wm_class, True));
366 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
367 WMResizeWidget(panel->browseBtn, 70, 24);
368 WMMoveWidget(panel->browseBtn, 195, 18);
369 WMSetButtonText(panel->browseBtn, _("Browse..."));
370 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
373 WMBox *hbox;
375 hbox = WMCreateBox(vbox);
376 WMSetBoxHorizontal(hbox, True);
377 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
379 panel->okBtn = WMCreateCommandButton(hbox);
380 WMSetButtonText(panel->okBtn, _("OK"));
381 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
382 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
384 panel->cancelBtn = WMCreateCommandButton(hbox);
385 WMSetButtonText(panel->cancelBtn, _("Cancel"));
386 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
387 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
389 WMMapSubwidgets(hbox);
392 WMRealizeWidget(panel->win);
393 WMMapSubwidgets(panel->win);
394 WMMapSubwidgets(vbox);
395 WMMapSubwidgets(panel->iconFrame);
397 updateSettingsPanelIcon(panel);
399 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
400 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
402 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
405 * make things relative to head
408 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
410 y = aicon->y_pos;
411 if (y < 0)
412 y = 0;
413 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
414 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
416 if (aicon->dock && aicon->dock->type == WM_DOCK) {
417 if (aicon->dock->on_right_side)
418 x = rect.pos.x + rect.size.width / 2;
419 else
420 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
421 } else {
422 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
426 panel->wwin = wManageInternalWindow(scr, parent, None,
427 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
429 panel->wwin->client_leader = WMWidgetXID(panel->win);
431 panel->parent = parent;
433 WMMapWidget(panel->win);
435 wWindowMap(panel->wwin);
438 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
440 if (!panel->destroyed) {
441 XUnmapWindow(dpy, panel->wwin->client_win);
442 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
443 wUnmanageWindow(panel->wwin, False, False);
446 panel->destroyed = 1;
449 * kluge. If we destroy the panel before the icon chooser is closed,
450 * we will crash when it does close, trying to access something in the
451 * destroyed panel. Could use wretain()/wrelease() in the panel,
452 * but it is not working for some reason.
454 if (panel->choosingIcon)
455 return;
457 WMDestroyWidget(panel->win);
459 XDestroyWindow(dpy, panel->parent);
461 panel->editedIcon->panel = NULL;
463 panel->editedIcon->editing = 0;
465 wfree(panel);