Fixed a few improper macro usages
[wmaker-crm.git] / src / dockedapp.c
blob19d55bbddadae2145206961aa206d7c8198a65ab
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>
28 #include <stdio.h>
30 #include "WindowMaker.h"
31 #include "window.h"
32 #include "icon.h"
33 #include "appicon.h"
34 #include "dock.h"
35 #include "dockedapp.h"
36 #include "dialog.h"
37 #include "misc.h"
38 #include "defaults.h"
39 #include "framewin.h"
40 #include "xinerama.h"
43 static void updateCommand(WAppIcon * icon, char *command)
45 if (icon->command)
46 wfree(icon->command);
47 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
48 wfree(command);
49 command = NULL;
51 icon->command = command;
53 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
54 icon->forced_dock = 1;
58 static void updatePasteCommand(WAppIcon * icon, char *command)
60 if (icon->paste_command)
61 wfree(icon->paste_command);
62 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
63 wfree(command);
64 command = NULL;
66 icon->paste_command = command;
69 #ifdef XDND
70 static void updateDNDCommand(WAppIcon * icon, char *command)
72 if (icon->dnd_command)
73 wfree(icon->dnd_command);
74 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
75 wfree(command);
76 command = NULL;
78 icon->dnd_command = command;
80 #endif /* XDND */
82 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
84 char *file;
86 file = WMGetTextFieldText(panel->iconField);
87 if (!file)
88 WMSetLabelImage(panel->iconLabel, NULL);
89 else {
90 char *path;
92 path = FindImage(wPreferences.icon_path, file);
93 if (!path) {
94 wwarning(_("could not find icon %s, used in a docked application"), file);
95 wfree(file);
96 WMSetLabelImage(panel->iconLabel, NULL);
97 return;
98 } else {
99 WMPixmap *pixmap;
100 RColor color;
102 color.red = 0xae;
103 color.green = 0xaa;
104 color.blue = 0xae;
105 color.alpha = 0;
106 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
107 if (!pixmap) {
108 WMSetLabelImage(panel->iconLabel, NULL);
109 } else {
110 WMSetLabelImage(panel->iconLabel, pixmap);
111 WMReleasePixmap(pixmap);
114 wfree(file);
115 wfree(path);
119 static void chooseIconCallback(WMWidget * self, void *clientData)
121 char *file;
122 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
123 int result;
125 /* Parameter not used, but tell the compiler that it is ok */
126 (void) self;
128 panel->choosingIcon = 1;
130 WMSetButtonEnabled(panel->browseBtn, False);
132 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
133 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
135 panel->choosingIcon = 0;
136 if (!panel->destroyed) {
137 if (result) {
138 WMSetTextFieldText(panel->iconField, file);
139 wfree(file);
140 updateSettingsPanelIcon(panel);
143 WMSetButtonEnabled(panel->browseBtn, True);
144 } else {
145 /* kluge for the case, the user asked to close the panel before
146 * the icon chooser */
147 DestroyDockAppSettingsPanel(panel);
151 static void panelBtnCallback(WMWidget * self, void *data)
153 WMButton *btn = self;
154 AppSettingsPanel *panel = (AppSettingsPanel *) data;
155 char *text;
156 int done = 1;
158 if (panel->okBtn == btn) {
159 text = WMGetTextFieldText(panel->iconField);
160 if (text[0] == 0) {
161 wfree(text);
162 text = NULL;
165 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
166 char *buf;
167 int len = strlen(text) + 64;
169 buf = wmalloc(len);
170 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
171 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
172 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
173 if (text)
174 wfree(text);
175 wfree(buf);
176 return;
178 wfree(buf);
179 } else {
180 WAppIcon *aicon = panel->editedIcon;
182 // Cf dock.c:dockIconPaint(WAppIcon *aicon)?
183 if (aicon == w_global.clip.icon)
184 wClipIconPaint();
185 else if (wIsADrawer(aicon))
186 wDrawerIconPaint(aicon);
187 else
188 wAppIconPaint(aicon);
190 wDefaultChangeIcon(aicon->wm_instance, aicon->wm_class, text);
192 if (text)
193 wfree(text);
195 /* cannot free text from this, because it will be not be duplicated
196 * in updateCommand */
197 text = WMGetTextFieldText(panel->commandField);
198 if (text[0] == 0) {
199 wfree(text);
200 text = NULL;
202 updateCommand(panel->editedIcon, text);
203 #ifdef XDND
204 /* cannot free text from this, because it will be not be duplicated
205 * in updateDNDCommand */
206 text = WMGetTextFieldText(panel->dndCommandField);
207 updateDNDCommand(panel->editedIcon, text);
208 #endif
209 text = WMGetTextFieldText(panel->pasteCommandField);
210 updatePasteCommand(panel->editedIcon, text);
212 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
214 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
217 if (done)
218 DestroyDockAppSettingsPanel(panel);
221 #define PWIDTH 295
222 #define PHEIGHT 430
224 void ShowDockAppSettingsPanel(WAppIcon * aicon)
226 AppSettingsPanel *panel;
227 WScreen *scr = aicon->icon->core->screen_ptr;
228 Window parent;
229 WMFont *font;
230 int x, y;
231 WMBox *vbox;
233 panel = wmalloc(sizeof(AppSettingsPanel));
235 panel->editedIcon = aicon;
237 aicon->panel = panel;
238 aicon->editing = 1;
240 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
241 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
243 panel->iconLabel = WMCreateLabel(panel->win);
244 WMResizeWidget(panel->iconLabel, 64, 64);
245 WMMoveWidget(panel->iconLabel, 10, 10);
246 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
248 panel->nameLabel = WMCreateLabel(panel->win);
249 WMResizeWidget(panel->nameLabel, 190, 18);
250 WMMoveWidget(panel->nameLabel, 80, 35);
251 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
252 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
253 WMSetLabelFont(panel->nameLabel, font);
254 WMReleaseFont(font);
255 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
256 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
257 else
258 WMSetLabelText(panel->nameLabel, aicon->wm_class);
260 vbox = WMCreateBox(panel->win);
261 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
262 WMMoveWidget(vbox, 10, 84);
264 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
265 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
266 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
267 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
269 panel->lockBtn = WMCreateSwitchButton(vbox);
270 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
271 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
272 WMSetButtonSelected(panel->lockBtn, aicon->lock);
274 panel->commandFrame = WMCreateFrame(vbox);
275 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
276 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
278 panel->commandField = WMCreateTextField(panel->commandFrame);
279 WMResizeWidget(panel->commandField, 256, 20);
280 WMMoveWidget(panel->commandField, 10, 20);
281 WMSetTextFieldText(panel->commandField, aicon->command);
283 WMMapSubwidgets(panel->commandFrame);
285 panel->pasteCommandFrame = WMCreateFrame(vbox);
286 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
287 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
289 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
290 WMResizeWidget(panel->pasteCommandField, 256, 20);
291 WMMoveWidget(panel->pasteCommandField, 10, 20);
293 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
294 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
295 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
297 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
298 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
299 WMMapSubwidgets(panel->pasteCommandFrame);
301 panel->dndCommandFrame = WMCreateFrame(vbox);
302 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
303 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
305 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
306 WMResizeWidget(panel->dndCommandField, 256, 20);
307 WMMoveWidget(panel->dndCommandField, 10, 20);
309 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
310 WMResizeWidget(panel->dndCommandLabel, 256, 18);
311 WMMoveWidget(panel->dndCommandLabel, 10, 45);
312 #ifdef XDND
313 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
314 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
315 #else
316 WMSetTextFieldEditable(panel->dndCommandField, False);
317 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
318 #endif
319 WMMapSubwidgets(panel->dndCommandFrame);
321 panel->iconFrame = WMCreateFrame(vbox);
322 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
323 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
325 panel->iconField = WMCreateTextField(panel->iconFrame);
326 WMResizeWidget(panel->iconField, 176, 20);
327 WMMoveWidget(panel->iconField, 10, 20);
328 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(aicon->wm_instance, aicon->wm_class, False));
330 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
331 WMResizeWidget(panel->browseBtn, 70, 24);
332 WMMoveWidget(panel->browseBtn, 195, 18);
333 WMSetButtonText(panel->browseBtn, _("Browse..."));
334 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
337 WMBox *hbox;
339 hbox = WMCreateBox(vbox);
340 WMSetBoxHorizontal(hbox, True);
341 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
343 panel->okBtn = WMCreateCommandButton(hbox);
344 WMSetButtonText(panel->okBtn, _("OK"));
345 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
346 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
348 panel->cancelBtn = WMCreateCommandButton(hbox);
349 WMSetButtonText(panel->cancelBtn, _("Cancel"));
350 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
351 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
353 WMMapSubwidgets(hbox);
356 WMRealizeWidget(panel->win);
357 WMMapSubwidgets(panel->win);
358 WMMapSubwidgets(vbox);
359 WMMapSubwidgets(panel->iconFrame);
361 updateSettingsPanelIcon(panel);
363 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
364 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
366 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
369 * make things relative to head
372 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
374 y = aicon->y_pos;
375 if (y < 0)
376 y = 0;
377 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
378 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
380 if (aicon->dock && aicon->dock->type == WM_DOCK) {
381 if (aicon->dock->on_right_side)
382 x = rect.pos.x + rect.size.width / 2;
383 else
384 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
385 } else {
386 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
390 panel->wwin = wManageInternalWindow(scr, parent, None,
391 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
393 panel->wwin->client_leader = WMWidgetXID(panel->win);
395 panel->parent = parent;
397 WMMapWidget(panel->win);
399 wWindowMap(panel->wwin);
402 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
404 if (!panel->destroyed) {
405 XUnmapWindow(dpy, panel->wwin->client_win);
406 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
407 wUnmanageWindow(panel->wwin, False, False);
410 panel->destroyed = 1;
413 * kluge. If we destroy the panel before the icon chooser is closed,
414 * we will crash when it does close, trying to access something in the
415 * destroyed panel. Could use wretain()/wrelease() in the panel,
416 * but it is not working for some reason.
418 if (panel->choosingIcon)
419 return;
421 WMDestroyWidget(panel->win);
423 XDestroyWindow(dpy, panel->parent);
425 panel->editedIcon->panel = NULL;
427 panel->editedIcon->editing = 0;
429 wfree(panel);