wmaker: remove parameter 'which' from local function 'restoreMenu'
[wmaker-crm.git] / src / dockedapp.c
blob44eaacda775e16386ae0ab22fc27441db3d42f0a
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;
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 wfree(text);
174 wfree(buf);
175 return;
177 wfree(buf);
178 } else {
179 WAppIcon *aicon = panel->editedIcon;
181 // Cf dock.c:dockIconPaint(WAppIcon *aicon)?
182 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
183 wClipIconPaint(aicon);
184 else if (wIsADrawer(aicon))
185 wDrawerIconPaint(aicon);
186 else
187 wAppIconPaint(aicon);
189 wDefaultChangeIcon(aicon->wm_instance, aicon->wm_class, text);
191 if (text)
192 wfree(text);
194 /* cannot free text from this, because it will be not be duplicated
195 * in updateCommand */
196 text = WMGetTextFieldText(panel->commandField);
197 if (text[0] == 0) {
198 wfree(text);
199 text = NULL;
201 updateCommand(panel->editedIcon, text);
202 #ifdef XDND
203 /* cannot free text from this, because it will be not be duplicated
204 * in updateDNDCommand */
205 text = WMGetTextFieldText(panel->dndCommandField);
206 updateDNDCommand(panel->editedIcon, text);
207 #endif
208 text = WMGetTextFieldText(panel->pasteCommandField);
209 updatePasteCommand(panel->editedIcon, text);
211 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
213 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
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 dragged and dropped files"));
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, _("XDnD support was not compiled in"));
317 WMSetFrameTitleColor(panel->dndCommandFrame, WMDarkGrayColor(scr->wmscreen));
318 WMSetLabelTextColor(panel->dndCommandLabel, WMDarkGrayColor(scr->wmscreen));
319 #endif
320 WMMapSubwidgets(panel->dndCommandFrame);
322 panel->iconFrame = WMCreateFrame(vbox);
323 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
324 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
326 panel->iconField = WMCreateTextField(panel->iconFrame);
327 WMResizeWidget(panel->iconField, 176, 20);
328 WMMoveWidget(panel->iconField, 10, 20);
329 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(aicon->wm_instance, aicon->wm_class, False));
331 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
332 WMResizeWidget(panel->browseBtn, 70, 24);
333 WMMoveWidget(panel->browseBtn, 195, 18);
334 WMSetButtonText(panel->browseBtn, _("Browse..."));
335 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
338 WMBox *hbox;
340 hbox = WMCreateBox(vbox);
341 WMSetBoxHorizontal(hbox, True);
342 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
344 panel->okBtn = WMCreateCommandButton(hbox);
345 WMSetButtonText(panel->okBtn, _("OK"));
346 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
347 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
349 panel->cancelBtn = WMCreateCommandButton(hbox);
350 WMSetButtonText(panel->cancelBtn, _("Cancel"));
351 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
352 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
354 WMMapSubwidgets(hbox);
357 WMRealizeWidget(panel->win);
358 WMMapSubwidgets(panel->win);
359 WMMapSubwidgets(vbox);
360 WMMapSubwidgets(panel->iconFrame);
362 updateSettingsPanelIcon(panel);
364 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
365 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
367 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
370 * make things relative to head
373 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
375 y = aicon->y_pos;
376 if (y < 0)
377 y = 0;
378 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
379 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
381 if (aicon->dock && aicon->dock->type == WM_DOCK) {
382 if (aicon->dock->on_right_side)
383 x = rect.pos.x + rect.size.width / 2;
384 else
385 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
386 } else {
387 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
391 panel->wwin = wManageInternalWindow(scr, parent, None,
392 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
394 panel->wwin->client_leader = WMWidgetXID(panel->win);
396 panel->parent = parent;
398 WMMapWidget(panel->win);
400 wWindowMap(panel->wwin);
403 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
405 if (!panel->destroyed) {
406 XUnmapWindow(dpy, panel->wwin->client_win);
407 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
408 wUnmanageWindow(panel->wwin, False, False);
411 panel->destroyed = 1;
414 * kluge. If we destroy the panel before the icon chooser is closed,
415 * we will crash when it does close, trying to access something in the
416 * destroyed panel. Could use wretain()/wrelease() in the panel,
417 * but it is not working for some reason.
419 if (panel->choosingIcon)
420 return;
422 WMDestroyWidget(panel->win);
424 XDestroyWindow(dpy, panel->parent);
426 panel->editedIcon->panel = NULL;
428 panel->editedIcon->editing = 0;
430 wfree(panel);