Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / dockedapp.c
blobcc592e0df90b57a782eac116196a8cf749e4e4cb
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 "dockedapp.h"
35 #include "dialog.h"
36 #include "misc.h"
37 #include "defaults.h"
38 #include "framewin.h"
39 #include "xinerama.h"
41 /**** Global variables ****/
42 extern WPreferences wPreferences;
44 static void updateCommand(WAppIcon * icon, char *command)
46 if (icon->command)
47 wfree(icon->command);
48 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
49 wfree(command);
50 command = NULL;
52 icon->command = command;
54 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
55 icon->forced_dock = 1;
59 static void updatePasteCommand(WAppIcon * icon, char *command)
61 if (icon->paste_command)
62 wfree(icon->paste_command);
63 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
64 wfree(command);
65 command = NULL;
67 icon->paste_command = command;
70 #ifdef XDND
71 static void updateDNDCommand(WAppIcon * icon, char *command)
73 if (icon->dnd_command)
74 wfree(icon->dnd_command);
75 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
76 wfree(command);
77 command = NULL;
79 icon->dnd_command = command;
81 #endif /* XDND */
83 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
85 char *file;
87 file = WMGetTextFieldText(panel->iconField);
88 if (!file)
89 WMSetLabelImage(panel->iconLabel, NULL);
90 else {
91 char *path;
93 path = FindImage(wPreferences.icon_path, file);
94 if (!path) {
95 wwarning(_("could not find icon %s, used in a docked application"), file);
96 wfree(file);
97 WMSetLabelImage(panel->iconLabel, NULL);
98 return;
99 } else {
100 WMPixmap *pixmap;
101 RColor color;
103 color.red = 0xae;
104 color.green = 0xaa;
105 color.blue = 0xae;
106 color.alpha = 0;
107 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
108 if (!pixmap) {
109 WMSetLabelImage(panel->iconLabel, NULL);
110 } else {
111 WMSetLabelImage(panel->iconLabel, pixmap);
112 WMReleasePixmap(pixmap);
115 wfree(file);
116 wfree(path);
120 static void chooseIconCallback(WMWidget * self, void *clientData)
122 char *file;
123 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
124 int result;
126 panel->choosingIcon = 1;
128 WMSetButtonEnabled(panel->browseBtn, False);
130 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
131 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
133 panel->choosingIcon = 0;
134 if (!panel->destroyed) {
135 if (result) {
136 WMSetTextFieldText(panel->iconField, file);
137 wfree(file);
138 updateSettingsPanelIcon(panel);
141 WMSetButtonEnabled(panel->browseBtn, True);
142 } else {
143 /* kluge for the case, the user asked to close the panel before
144 * the icon chooser */
145 DestroyDockAppSettingsPanel(panel);
149 static void panelBtnCallback(WMWidget * self, void *data)
151 WMButton *btn = self;
152 AppSettingsPanel *panel = (AppSettingsPanel *) data;
153 char *text;
154 int done = 1;
156 if (panel->okBtn == btn) {
157 text = WMGetTextFieldText(panel->iconField);
158 if (text[0] == 0) {
159 wfree(text);
160 text = NULL;
163 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
164 char *buf;
165 int len = strlen(text) + 64;
167 buf = wmalloc(len);
168 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
169 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
170 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
171 if (text)
172 wfree(text);
173 wfree(buf);
174 return;
176 wfree(buf);
177 } else {
178 WAppIcon *aicon = panel->editedIcon;
180 // Cf dock.c:dockIconPaint(WAppIcon *aicon)?
181 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
182 wClipIconPaint(aicon);
183 else if (wIsADrawer(aicon->icon->core->screen_ptr, aicon))
184 wDrawerIconPaint(aicon);
185 else
186 wAppIconPaint(aicon);
188 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance, aicon->wm_class, text);
190 if (text)
191 wfree(text);
193 /* cannot free text from this, because it will be not be duplicated
194 * in updateCommand */
195 text = WMGetTextFieldText(panel->commandField);
196 if (text[0] == 0) {
197 wfree(text);
198 text = NULL;
200 updateCommand(panel->editedIcon, text);
201 #ifdef XDND
202 /* cannot free text from this, because it will be not be duplicated
203 * in updateDNDCommand */
204 text = WMGetTextFieldText(panel->dndCommandField);
205 updateDNDCommand(panel->editedIcon, text);
206 #endif
207 text = WMGetTextFieldText(panel->pasteCommandField);
208 updatePasteCommand(panel->editedIcon, text);
210 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
212 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
215 if (done)
216 DestroyDockAppSettingsPanel(panel);
219 #define PWIDTH 295
220 #define PHEIGHT 430
222 void ShowDockAppSettingsPanel(WAppIcon * aicon)
224 AppSettingsPanel *panel;
225 WScreen *scr = aicon->icon->core->screen_ptr;
226 Window parent;
227 WMFont *font;
228 int x, y;
229 WMBox *vbox;
231 panel = wmalloc(sizeof(AppSettingsPanel));
233 panel->editedIcon = aicon;
235 aicon->panel = panel;
236 aicon->editing = 1;
238 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
239 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
241 panel->iconLabel = WMCreateLabel(panel->win);
242 WMResizeWidget(panel->iconLabel, 64, 64);
243 WMMoveWidget(panel->iconLabel, 10, 10);
244 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
246 panel->nameLabel = WMCreateLabel(panel->win);
247 WMResizeWidget(panel->nameLabel, 190, 18);
248 WMMoveWidget(panel->nameLabel, 80, 35);
249 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
250 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
251 WMSetLabelFont(panel->nameLabel, font);
252 WMReleaseFont(font);
253 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
254 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
255 else
256 WMSetLabelText(panel->nameLabel, aicon->wm_class);
258 vbox = WMCreateBox(panel->win);
259 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
260 WMMoveWidget(vbox, 10, 84);
262 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
263 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
264 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
265 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
267 panel->lockBtn = WMCreateSwitchButton(vbox);
268 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
269 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
270 WMSetButtonSelected(panel->lockBtn, aicon->lock);
272 panel->commandFrame = WMCreateFrame(vbox);
273 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
274 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
276 panel->commandField = WMCreateTextField(panel->commandFrame);
277 WMResizeWidget(panel->commandField, 256, 20);
278 WMMoveWidget(panel->commandField, 10, 20);
279 WMSetTextFieldText(panel->commandField, aicon->command);
281 WMMapSubwidgets(panel->commandFrame);
283 panel->pasteCommandFrame = WMCreateFrame(vbox);
284 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
285 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
287 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
288 WMResizeWidget(panel->pasteCommandField, 256, 20);
289 WMMoveWidget(panel->pasteCommandField, 10, 20);
291 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
292 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
293 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
295 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
296 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
297 WMMapSubwidgets(panel->pasteCommandFrame);
299 panel->dndCommandFrame = WMCreateFrame(vbox);
300 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
301 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
303 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
304 WMResizeWidget(panel->dndCommandField, 256, 20);
305 WMMoveWidget(panel->dndCommandField, 10, 20);
307 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
308 WMResizeWidget(panel->dndCommandLabel, 256, 18);
309 WMMoveWidget(panel->dndCommandLabel, 10, 45);
310 #ifdef XDND
311 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
312 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
313 #else
314 WMSetTextFieldEditable(panel->dndCommandField, False);
315 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
316 #endif
317 WMMapSubwidgets(panel->dndCommandFrame);
319 panel->iconFrame = WMCreateFrame(vbox);
320 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
321 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
323 panel->iconField = WMCreateTextField(panel->iconFrame);
324 WMResizeWidget(panel->iconField, 176, 20);
325 WMMoveWidget(panel->iconField, 10, 20);
326 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(aicon->wm_instance, aicon->wm_class, False));
328 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
329 WMResizeWidget(panel->browseBtn, 70, 24);
330 WMMoveWidget(panel->browseBtn, 195, 18);
331 WMSetButtonText(panel->browseBtn, _("Browse..."));
332 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
335 WMBox *hbox;
337 hbox = WMCreateBox(vbox);
338 WMSetBoxHorizontal(hbox, True);
339 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
341 panel->okBtn = WMCreateCommandButton(hbox);
342 WMSetButtonText(panel->okBtn, _("OK"));
343 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
344 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
346 panel->cancelBtn = WMCreateCommandButton(hbox);
347 WMSetButtonText(panel->cancelBtn, _("Cancel"));
348 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
349 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
351 WMMapSubwidgets(hbox);
354 WMRealizeWidget(panel->win);
355 WMMapSubwidgets(panel->win);
356 WMMapSubwidgets(vbox);
357 WMMapSubwidgets(panel->iconFrame);
359 updateSettingsPanelIcon(panel);
361 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
362 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
364 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
367 * make things relative to head
370 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
372 y = aicon->y_pos;
373 if (y < 0)
374 y = 0;
375 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
376 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
378 if (aicon->dock && aicon->dock->type == WM_DOCK) {
379 if (aicon->dock->on_right_side)
380 x = rect.pos.x + rect.size.width / 2;
381 else
382 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
383 } else {
384 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
388 panel->wwin = wManageInternalWindow(scr, parent, None,
389 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
391 panel->wwin->client_leader = WMWidgetXID(panel->win);
393 panel->parent = parent;
395 WMMapWidget(panel->win);
397 wWindowMap(panel->wwin);
400 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
402 if (!panel->destroyed) {
403 XUnmapWindow(dpy, panel->wwin->client_win);
404 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
405 wUnmanageWindow(panel->wwin, False, False);
408 panel->destroyed = 1;
411 * kluge. If we destroy the panel before the icon chooser is closed,
412 * we will crash when it does close, trying to access something in the
413 * destroyed panel. Could use wretain()/wrelease() in the panel,
414 * but it is not working for some reason.
416 if (panel->choosingIcon)
417 return;
419 WMDestroyWidget(panel->win);
421 XDestroyWindow(dpy, panel->parent);
423 panel->editedIcon->panel = NULL;
425 panel->editedIcon->editing = 0;
427 wfree(panel);