wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / src / dockedapp.c
blobb24f1277cb615c125622d36ad3b1da5ffe3811aa
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 panel->choosingIcon = 1;
127 WMSetButtonEnabled(panel->browseBtn, False);
129 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
130 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
132 panel->choosingIcon = 0;
133 if (!panel->destroyed) {
134 if (result) {
135 WMSetTextFieldText(panel->iconField, file);
136 wfree(file);
137 updateSettingsPanelIcon(panel);
140 WMSetButtonEnabled(panel->browseBtn, True);
141 } else {
142 /* kluge for the case, the user asked to close the panel before
143 * the icon chooser */
144 DestroyDockAppSettingsPanel(panel);
148 static void panelBtnCallback(WMWidget * self, void *data)
150 WMButton *btn = self;
151 AppSettingsPanel *panel = (AppSettingsPanel *) data;
152 char *text;
153 int done = 1;
155 if (panel->okBtn == btn) {
156 text = WMGetTextFieldText(panel->iconField);
157 if (text[0] == 0) {
158 wfree(text);
159 text = NULL;
162 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
163 char *buf;
164 int len = strlen(text) + 64;
166 buf = wmalloc(len);
167 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
168 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
169 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
170 if (text)
171 wfree(text);
172 wfree(buf);
173 return;
175 wfree(buf);
176 } else {
177 WAppIcon *aicon = panel->editedIcon;
179 // Cf dock.c:dockIconPaint(WAppIcon *aicon)?
180 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
181 wClipIconPaint(aicon);
182 else if (wIsADrawer(aicon->icon->core->screen_ptr, aicon))
183 wDrawerIconPaint(aicon);
184 else
185 wAppIconPaint(aicon);
187 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance, aicon->wm_class, text);
189 if (text)
190 wfree(text);
192 /* cannot free text from this, because it will be not be duplicated
193 * in updateCommand */
194 text = WMGetTextFieldText(panel->commandField);
195 if (text[0] == 0) {
196 wfree(text);
197 text = NULL;
199 updateCommand(panel->editedIcon, text);
200 #ifdef XDND
201 /* cannot free text from this, because it will be not be duplicated
202 * in updateDNDCommand */
203 text = WMGetTextFieldText(panel->dndCommandField);
204 updateDNDCommand(panel->editedIcon, text);
205 #endif
206 text = WMGetTextFieldText(panel->pasteCommandField);
207 updatePasteCommand(panel->editedIcon, text);
209 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
211 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
214 if (done)
215 DestroyDockAppSettingsPanel(panel);
218 #define PWIDTH 295
219 #define PHEIGHT 430
221 void ShowDockAppSettingsPanel(WAppIcon * aicon)
223 AppSettingsPanel *panel;
224 WScreen *scr = aicon->icon->core->screen_ptr;
225 Window parent;
226 WMFont *font;
227 int x, y;
228 WMBox *vbox;
230 panel = wmalloc(sizeof(AppSettingsPanel));
232 panel->editedIcon = aicon;
234 aicon->panel = panel;
235 aicon->editing = 1;
237 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
238 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
240 panel->iconLabel = WMCreateLabel(panel->win);
241 WMResizeWidget(panel->iconLabel, 64, 64);
242 WMMoveWidget(panel->iconLabel, 10, 10);
243 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
245 panel->nameLabel = WMCreateLabel(panel->win);
246 WMResizeWidget(panel->nameLabel, 190, 18);
247 WMMoveWidget(panel->nameLabel, 80, 35);
248 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
249 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
250 WMSetLabelFont(panel->nameLabel, font);
251 WMReleaseFont(font);
252 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
253 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
254 else
255 WMSetLabelText(panel->nameLabel, aicon->wm_class);
257 vbox = WMCreateBox(panel->win);
258 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
259 WMMoveWidget(vbox, 10, 84);
261 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
262 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
263 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
264 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
266 panel->lockBtn = WMCreateSwitchButton(vbox);
267 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
268 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
269 WMSetButtonSelected(panel->lockBtn, aicon->lock);
271 panel->commandFrame = WMCreateFrame(vbox);
272 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
273 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
275 panel->commandField = WMCreateTextField(panel->commandFrame);
276 WMResizeWidget(panel->commandField, 256, 20);
277 WMMoveWidget(panel->commandField, 10, 20);
278 WMSetTextFieldText(panel->commandField, aicon->command);
280 WMMapSubwidgets(panel->commandFrame);
282 panel->pasteCommandFrame = WMCreateFrame(vbox);
283 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
284 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
286 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
287 WMResizeWidget(panel->pasteCommandField, 256, 20);
288 WMMoveWidget(panel->pasteCommandField, 10, 20);
290 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
291 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
292 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
294 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
295 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
296 WMMapSubwidgets(panel->pasteCommandFrame);
298 panel->dndCommandFrame = WMCreateFrame(vbox);
299 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
300 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
302 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
303 WMResizeWidget(panel->dndCommandField, 256, 20);
304 WMMoveWidget(panel->dndCommandField, 10, 20);
306 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
307 WMResizeWidget(panel->dndCommandLabel, 256, 18);
308 WMMoveWidget(panel->dndCommandLabel, 10, 45);
309 #ifdef XDND
310 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
311 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
312 #else
313 WMSetTextFieldEditable(panel->dndCommandField, False);
314 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
315 #endif
316 WMMapSubwidgets(panel->dndCommandFrame);
318 panel->iconFrame = WMCreateFrame(vbox);
319 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
320 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
322 panel->iconField = WMCreateTextField(panel->iconFrame);
323 WMResizeWidget(panel->iconField, 176, 20);
324 WMMoveWidget(panel->iconField, 10, 20);
325 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(aicon->wm_instance, aicon->wm_class, False));
327 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
328 WMResizeWidget(panel->browseBtn, 70, 24);
329 WMMoveWidget(panel->browseBtn, 195, 18);
330 WMSetButtonText(panel->browseBtn, _("Browse..."));
331 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
334 WMBox *hbox;
336 hbox = WMCreateBox(vbox);
337 WMSetBoxHorizontal(hbox, True);
338 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
340 panel->okBtn = WMCreateCommandButton(hbox);
341 WMSetButtonText(panel->okBtn, _("OK"));
342 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
343 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
345 panel->cancelBtn = WMCreateCommandButton(hbox);
346 WMSetButtonText(panel->cancelBtn, _("Cancel"));
347 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
348 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
350 WMMapSubwidgets(hbox);
353 WMRealizeWidget(panel->win);
354 WMMapSubwidgets(panel->win);
355 WMMapSubwidgets(vbox);
356 WMMapSubwidgets(panel->iconFrame);
358 updateSettingsPanelIcon(panel);
360 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
361 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
363 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
366 * make things relative to head
369 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
371 y = aicon->y_pos;
372 if (y < 0)
373 y = 0;
374 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
375 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
377 if (aicon->dock && aicon->dock->type == WM_DOCK) {
378 if (aicon->dock->on_right_side)
379 x = rect.pos.x + rect.size.width / 2;
380 else
381 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
382 } else {
383 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
387 panel->wwin = wManageInternalWindow(scr, parent, None,
388 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
390 panel->wwin->client_leader = WMWidgetXID(panel->win);
392 panel->parent = parent;
394 WMMapWidget(panel->win);
396 wWindowMap(panel->wwin);
399 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
401 if (!panel->destroyed) {
402 XUnmapWindow(dpy, panel->wwin->client_win);
403 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
404 wUnmanageWindow(panel->wwin, False, False);
407 panel->destroyed = 1;
410 * kluge. If we destroy the panel before the icon chooser is closed,
411 * we will crash when it does close, trying to access something in the
412 * destroyed panel. Could use wretain()/wrelease() in the panel,
413 * but it is not working for some reason.
415 if (panel->choosingIcon)
416 return;
418 WMDestroyWidget(panel->win);
420 XDestroyWindow(dpy, panel->parent);
422 panel->editedIcon->panel = NULL;
424 panel->editedIcon->editing = 0;
426 wfree(panel);