Update local copy of GPLv2 and FSF address in copyrights
[wmaker-crm.git] / src / dockedapp.c
blob944e025afe2f44ac0f00bd415fde3a5c83665555
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 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.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "WindowMaker.h"
30 #include "window.h"
31 #include "icon.h"
32 #include "appicon.h"
33 #include "dock.h"
34 #include "dialog.h"
35 #include "funcs.h"
36 #include "defaults.h"
37 #include "framewin.h"
38 #include "xinerama.h"
40 /**** Global variables ****/
41 extern WPreferences wPreferences;
43 typedef struct _AppSettingsPanel {
44 WMWindow *win;
45 WAppIcon *editedIcon;
47 WWindow *wwin;
49 WMLabel *iconLabel;
50 WMLabel *nameLabel;
52 WMFrame *commandFrame;
53 WMTextField *commandField;
55 WMFrame *dndCommandFrame;
56 WMTextField *dndCommandField;
57 WMLabel *dndCommandLabel;
59 WMFrame *pasteCommandFrame;
60 WMTextField *pasteCommandField;
61 WMLabel *pasteCommandLabel;
63 WMFrame *iconFrame;
64 WMTextField *iconField;
65 WMButton *browseBtn;
67 WMButton *autoLaunchBtn;
68 WMButton *lockBtn;
70 WMButton *okBtn;
71 WMButton *cancelBtn;
73 Window parent;
75 /* kluge */
76 unsigned int destroyed:1;
77 unsigned int choosingIcon:1;
78 } AppSettingsPanel;
80 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel);
82 static void updateCommand(WAppIcon * icon, char *command)
84 if (icon->command)
85 wfree(icon->command);
86 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
87 wfree(command);
88 command = NULL;
90 icon->command = command;
92 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
93 icon->forced_dock = 1;
97 static void updatePasteCommand(WAppIcon * icon, char *command)
99 if (icon->paste_command)
100 wfree(icon->paste_command);
101 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
102 wfree(command);
103 command = NULL;
105 icon->paste_command = command;
108 #ifdef XDND
109 static void updateDNDCommand(WAppIcon * icon, char *command)
111 if (icon->dnd_command)
112 wfree(icon->dnd_command);
113 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
114 wfree(command);
115 command = NULL;
117 icon->dnd_command = command;
119 #endif /* XDND */
121 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
123 char *file;
125 file = WMGetTextFieldText(panel->iconField);
126 if (!file)
127 WMSetLabelImage(panel->iconLabel, NULL);
128 else {
129 char *path;
131 path = FindImage(wPreferences.icon_path, file);
132 if (!path) {
133 wwarning(_("could not find icon %s, used in a docked application"), file);
134 wfree(file);
135 WMSetLabelImage(panel->iconLabel, NULL);
136 return;
137 } else {
138 WMPixmap *pixmap;
139 RColor color;
141 color.red = 0xae;
142 color.green = 0xaa;
143 color.blue = 0xae;
144 color.alpha = 0;
145 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
146 if (!pixmap) {
147 WMSetLabelImage(panel->iconLabel, NULL);
148 } else {
149 WMSetLabelImage(panel->iconLabel, pixmap);
150 WMReleasePixmap(pixmap);
153 wfree(file);
154 wfree(path);
158 static void chooseIconCallback(WMWidget * self, void *clientData)
160 char *file;
161 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
162 int result;
164 panel->choosingIcon = 1;
166 WMSetButtonEnabled(panel->browseBtn, False);
168 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
169 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
171 panel->choosingIcon = 0;
172 if (!panel->destroyed) {
173 if (result) {
174 WMSetTextFieldText(panel->iconField, file);
175 wfree(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);
187 static void 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 wfree(text);
199 text = NULL;
201 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
202 char *buf;
203 int len = strlen(text) + 64;
205 buf = wmalloc(len);
206 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
207 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
208 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
209 if (text)
210 wfree(text);
211 wfree(buf);
212 return;
214 wfree(buf);
215 } else {
216 WAppIcon *aicon = panel->editedIcon;
218 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
219 wClipIconPaint(aicon);
220 else
221 wAppIconPaint(aicon);
223 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance, aicon->wm_class, text);
225 if (text)
226 wfree(text);
228 /* cannot free text from this, because it will be not be duplicated
229 * in updateCommand */
230 text = WMGetTextFieldText(panel->commandField);
231 if (text[0] == 0) {
232 wfree(text);
233 text = NULL;
235 updateCommand(panel->editedIcon, text);
236 #ifdef XDND
237 /* cannot free text from this, because it will be not be duplicated
238 * in updateDNDCommand */
239 text = WMGetTextFieldText(panel->dndCommandField);
240 updateDNDCommand(panel->editedIcon, text);
241 #endif
242 text = WMGetTextFieldText(panel->pasteCommandField);
243 updatePasteCommand(panel->editedIcon, text);
245 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
247 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
250 if (done)
251 DestroyDockAppSettingsPanel(panel);
254 #define PWIDTH 295
255 #define PHEIGHT 430
257 void ShowDockAppSettingsPanel(WAppIcon * aicon)
259 AppSettingsPanel *panel;
260 WScreen *scr = aicon->icon->core->screen_ptr;
261 Window parent;
262 WMFont *font;
263 int x, y;
264 WMBox *vbox;
266 panel = wmalloc(sizeof(AppSettingsPanel));
267 memset(panel, 0, sizeof(AppSettingsPanel));
269 panel->editedIcon = aicon;
271 aicon->panel = panel;
272 aicon->editing = 1;
274 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
275 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
277 panel->iconLabel = WMCreateLabel(panel->win);
278 WMResizeWidget(panel->iconLabel, 64, 64);
279 WMMoveWidget(panel->iconLabel, 10, 10);
280 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
282 panel->nameLabel = WMCreateLabel(panel->win);
283 WMResizeWidget(panel->nameLabel, 190, 18);
284 WMMoveWidget(panel->nameLabel, 80, 35);
285 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
286 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
287 WMSetLabelFont(panel->nameLabel, font);
288 WMReleaseFont(font);
289 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
290 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
291 else
292 WMSetLabelText(panel->nameLabel, aicon->wm_class);
294 vbox = WMCreateBox(panel->win);
295 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
296 WMMoveWidget(vbox, 10, 84);
298 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
299 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
300 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
301 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
303 panel->lockBtn = WMCreateSwitchButton(vbox);
304 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
305 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
306 WMSetButtonSelected(panel->lockBtn, aicon->lock);
308 panel->commandFrame = WMCreateFrame(vbox);
309 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
310 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
312 panel->commandField = WMCreateTextField(panel->commandFrame);
313 WMResizeWidget(panel->commandField, 256, 20);
314 WMMoveWidget(panel->commandField, 10, 20);
315 WMSetTextFieldText(panel->commandField, aicon->command);
317 WMMapSubwidgets(panel->commandFrame);
319 panel->pasteCommandFrame = WMCreateFrame(vbox);
320 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
321 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
323 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
324 WMResizeWidget(panel->pasteCommandField, 256, 20);
325 WMMoveWidget(panel->pasteCommandField, 10, 20);
327 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
328 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
329 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
331 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
332 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
333 WMMapSubwidgets(panel->pasteCommandFrame);
335 panel->dndCommandFrame = WMCreateFrame(vbox);
336 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
337 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
339 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
340 WMResizeWidget(panel->dndCommandField, 256, 20);
341 WMMoveWidget(panel->dndCommandField, 10, 20);
343 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
344 WMResizeWidget(panel->dndCommandLabel, 256, 18);
345 WMMoveWidget(panel->dndCommandLabel, 10, 45);
346 #ifdef XDND
347 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
348 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
349 #else
350 WMSetTextFieldEditable(panel->dndCommandField, False);
351 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
352 #endif
353 WMMapSubwidgets(panel->dndCommandFrame);
355 panel->iconFrame = WMCreateFrame(vbox);
356 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
357 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
359 panel->iconField = WMCreateTextField(panel->iconFrame);
360 WMResizeWidget(panel->iconField, 176, 20);
361 WMMoveWidget(panel->iconField, 10, 20);
362 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(scr, aicon->wm_instance, aicon->wm_class, True));
364 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
365 WMResizeWidget(panel->browseBtn, 70, 24);
366 WMMoveWidget(panel->browseBtn, 195, 18);
367 WMSetButtonText(panel->browseBtn, _("Browse..."));
368 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
371 WMBox *hbox;
373 hbox = WMCreateBox(vbox);
374 WMSetBoxHorizontal(hbox, True);
375 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
377 panel->okBtn = WMCreateCommandButton(hbox);
378 WMSetButtonText(panel->okBtn, _("OK"));
379 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
380 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
382 panel->cancelBtn = WMCreateCommandButton(hbox);
383 WMSetButtonText(panel->cancelBtn, _("Cancel"));
384 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
385 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
387 WMMapSubwidgets(hbox);
390 WMRealizeWidget(panel->win);
391 WMMapSubwidgets(panel->win);
392 WMMapSubwidgets(vbox);
393 WMMapSubwidgets(panel->iconFrame);
395 updateSettingsPanelIcon(panel);
397 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
398 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
400 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
403 * make things relative to head
406 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
408 y = aicon->y_pos;
409 if (y < 0)
410 y = 0;
411 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
412 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
414 if (aicon->dock && aicon->dock->type == WM_DOCK) {
415 if (aicon->dock->on_right_side)
416 x = rect.pos.x + rect.size.width / 2;
417 else
418 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
419 } else {
420 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
424 panel->wwin = wManageInternalWindow(scr, parent, None,
425 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
427 panel->wwin->client_leader = WMWidgetXID(panel->win);
429 panel->parent = parent;
431 WMMapWidget(panel->win);
433 wWindowMap(panel->wwin);
436 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
438 if (!panel->destroyed) {
439 XUnmapWindow(dpy, panel->wwin->client_win);
440 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
441 wUnmanageWindow(panel->wwin, False, False);
444 panel->destroyed = 1;
447 * kluge. If we destroy the panel before the icon chooser is closed,
448 * we will crash when it does close, trying to access something in the
449 * destroyed panel. Could use wretain()/wrelease() in the panel,
450 * but it is not working for some reason.
452 if (panel->choosingIcon)
453 return;
455 WMDestroyWidget(panel->win);
457 XDestroyWindow(dpy, panel->parent);
459 panel->editedIcon->panel = NULL;
461 panel->editedIcon->editing = 0;
463 wfree(panel);