wrlib: add explicit type definition in API to allow compiler Type Checks (3/3)
[wmaker-crm.git] / src / dockedapp.c
blob6a84afc6a232ea41a45e3f5ec4115db766ca091d
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 = WMCreateScaledBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color, 64, 64);
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 updateSettingsPanelIcon(panel);
142 WMSetButtonEnabled(panel->browseBtn, True);
143 } else {
144 /* kluge for the case, the user asked to close the panel before
145 * the icon chooser */
146 DestroyDockAppSettingsPanel(panel);
148 if (result)
149 wfree(file);
152 static void panelBtnCallback(WMWidget * self, void *data)
154 WMButton *btn = self;
155 AppSettingsPanel *panel = (AppSettingsPanel *) data;
156 char *text;
157 int done = 1;
159 if (panel->okBtn == btn) {
160 text = WMGetTextFieldText(panel->iconField);
161 if (text[0] == 0) {
162 wfree(text);
163 text = NULL;
166 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
167 char *buf;
168 int len = strlen(text) + 64;
170 buf = wmalloc(len);
171 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
172 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
173 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
174 if (text)
175 wfree(text);
176 wfree(buf);
177 return;
179 wfree(buf);
180 } else {
181 WAppIcon *aicon = panel->editedIcon;
183 // Cf dock.c:dockIconPaint(WAppIcon *aicon)?
184 if (aicon == w_global.clip.icon)
185 wClipIconPaint();
186 else if (wIsADrawer(aicon))
187 wDrawerIconPaint(aicon);
188 else
189 wAppIconPaint(aicon);
191 wDefaultChangeIcon(aicon->wm_instance, aicon->wm_class, text);
193 if (text)
194 wfree(text);
196 /* cannot free text from this, because it will be not be duplicated
197 * in updateCommand */
198 text = WMGetTextFieldText(panel->commandField);
199 if (text[0] == 0) {
200 wfree(text);
201 text = NULL;
203 updateCommand(panel->editedIcon, text);
204 #ifdef XDND
205 /* cannot free text from this, because it will be not be duplicated
206 * in updateDNDCommand */
207 text = WMGetTextFieldText(panel->dndCommandField);
208 updateDNDCommand(panel->editedIcon, text);
209 #endif
210 text = WMGetTextFieldText(panel->pasteCommandField);
211 updatePasteCommand(panel->editedIcon, text);
213 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
215 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
218 if (done)
219 DestroyDockAppSettingsPanel(panel);
222 #define PWIDTH 295
223 #define PHEIGHT 430
225 void ShowDockAppSettingsPanel(WAppIcon * aicon)
227 AppSettingsPanel *panel;
228 WScreen *scr = aicon->icon->core->screen_ptr;
229 Window parent;
230 WMFont *font;
231 int x, y;
232 WMBox *vbox;
234 panel = wmalloc(sizeof(AppSettingsPanel));
236 panel->editedIcon = aicon;
238 aicon->panel = panel;
239 aicon->editing = 1;
241 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
242 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
244 panel->iconLabel = WMCreateLabel(panel->win);
245 WMResizeWidget(panel->iconLabel, 64, 64);
246 WMMoveWidget(panel->iconLabel, 10, 10);
247 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
249 panel->nameLabel = WMCreateLabel(panel->win);
250 WMResizeWidget(panel->nameLabel, 190, 18);
251 WMMoveWidget(panel->nameLabel, 80, 35);
252 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
253 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
254 WMSetLabelFont(panel->nameLabel, font);
255 WMReleaseFont(font);
256 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
257 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
258 else
259 WMSetLabelText(panel->nameLabel, aicon->wm_class);
261 vbox = WMCreateBox(panel->win);
262 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
263 WMMoveWidget(vbox, 10, 84);
265 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
266 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
267 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
268 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
270 panel->lockBtn = WMCreateSwitchButton(vbox);
271 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
272 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
273 WMSetButtonSelected(panel->lockBtn, aicon->lock);
275 panel->commandFrame = WMCreateFrame(vbox);
276 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
277 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
279 panel->commandField = WMCreateTextField(panel->commandFrame);
280 WMResizeWidget(panel->commandField, 256, 20);
281 WMMoveWidget(panel->commandField, 10, 20);
282 WMSetTextFieldText(panel->commandField, aicon->command);
284 WMMapSubwidgets(panel->commandFrame);
286 panel->pasteCommandFrame = WMCreateFrame(vbox);
287 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
288 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
290 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
291 WMResizeWidget(panel->pasteCommandField, 256, 20);
292 WMMoveWidget(panel->pasteCommandField, 10, 20);
294 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
295 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
296 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
298 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
299 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
300 WMMapSubwidgets(panel->pasteCommandFrame);
302 panel->dndCommandFrame = WMCreateFrame(vbox);
303 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
304 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
306 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
307 WMResizeWidget(panel->dndCommandField, 256, 20);
308 WMMoveWidget(panel->dndCommandField, 10, 20);
310 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
311 WMResizeWidget(panel->dndCommandLabel, 256, 18);
312 WMMoveWidget(panel->dndCommandLabel, 10, 45);
313 #ifdef XDND
314 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
315 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
316 #else
317 WMSetTextFieldEditable(panel->dndCommandField, False);
318 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
320 WMSetFrameTitleColor(panel->dndCommandFrame, WMDarkGrayColor(scr->wmscreen));
321 WMSetLabelTextColor(panel->dndCommandLabel, WMDarkGrayColor(scr->wmscreen));
322 #endif
323 WMMapSubwidgets(panel->dndCommandFrame);
325 panel->iconFrame = WMCreateFrame(vbox);
326 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
327 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
329 panel->iconField = WMCreateTextField(panel->iconFrame);
330 WMResizeWidget(panel->iconField, 176, 20);
331 WMMoveWidget(panel->iconField, 10, 20);
332 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(aicon->wm_instance, aicon->wm_class, False));
334 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
335 WMResizeWidget(panel->browseBtn, 70, 24);
336 WMMoveWidget(panel->browseBtn, 195, 18);
337 WMSetButtonText(panel->browseBtn, _("Browse..."));
338 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
341 WMBox *hbox;
343 hbox = WMCreateBox(vbox);
344 WMSetBoxHorizontal(hbox, True);
345 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
347 panel->okBtn = WMCreateCommandButton(hbox);
348 WMSetButtonText(panel->okBtn, _("OK"));
349 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
350 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
352 panel->cancelBtn = WMCreateCommandButton(hbox);
353 WMSetButtonText(panel->cancelBtn, _("Cancel"));
354 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
355 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
357 WMMapSubwidgets(hbox);
360 WMRealizeWidget(panel->win);
361 WMMapSubwidgets(panel->win);
362 WMMapSubwidgets(vbox);
363 WMMapSubwidgets(panel->iconFrame);
365 updateSettingsPanelIcon(panel);
367 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
368 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
370 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
373 * make things relative to head
376 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
378 y = aicon->y_pos;
379 if (y < 0)
380 y = 0;
381 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
382 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
384 if (aicon->dock && aicon->dock->type == WM_DOCK) {
385 if (aicon->dock->on_right_side)
386 x = rect.pos.x + rect.size.width / 2;
387 else
388 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
389 } else {
390 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
394 panel->wwin = wManageInternalWindow(scr, parent, None,
395 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
397 panel->wwin->client_leader = WMWidgetXID(panel->win);
399 panel->parent = parent;
401 WMMapWidget(panel->win);
403 wWindowMap(panel->wwin);
406 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
408 if (!panel->destroyed) {
409 XUnmapWindow(dpy, panel->wwin->client_win);
410 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
411 wUnmanageWindow(panel->wwin, False, False);
414 panel->destroyed = 1;
417 * kluge. If we destroy the panel before the icon chooser is closed,
418 * we will crash when it does close, trying to access something in the
419 * destroyed panel. Could use wretain()/wrelease() in the panel,
420 * but it is not working for some reason.
422 if (panel->choosingIcon)
423 return;
425 WMDestroyWidget(panel->win);
427 XDestroyWindow(dpy, panel->parent);
429 panel->editedIcon->panel = NULL;
431 panel->editedIcon->editing = 0;
433 wfree(panel);