0ef180f1e23a94261b8465ec5926107cbb7bd78f
[wmaker-crm.git] / src / dockedapp.c
blob0ef180f1e23a94261b8465ec5926107cbb7bd78f
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"
42 /**** Global variables ****/
43 extern WPreferences wPreferences;
45 static void updateCommand(WAppIcon * icon, char *command)
47 if (icon->command)
48 wfree(icon->command);
49 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
50 wfree(command);
51 command = NULL;
53 icon->command = command;
55 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
56 icon->forced_dock = 1;
60 static void updatePasteCommand(WAppIcon * icon, char *command)
62 if (icon->paste_command)
63 wfree(icon->paste_command);
64 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
65 wfree(command);
66 command = NULL;
68 icon->paste_command = command;
71 #ifdef XDND
72 static void updateDNDCommand(WAppIcon * icon, char *command)
74 if (icon->dnd_command)
75 wfree(icon->dnd_command);
76 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
77 wfree(command);
78 command = NULL;
80 icon->dnd_command = command;
82 #endif /* XDND */
84 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
86 char *file;
88 file = WMGetTextFieldText(panel->iconField);
89 if (!file)
90 WMSetLabelImage(panel->iconLabel, NULL);
91 else {
92 char *path;
94 path = FindImage(wPreferences.icon_path, file);
95 if (!path) {
96 wwarning(_("could not find icon %s, used in a docked application"), file);
97 wfree(file);
98 WMSetLabelImage(panel->iconLabel, NULL);
99 return;
100 } else {
101 WMPixmap *pixmap;
102 RColor color;
104 color.red = 0xae;
105 color.green = 0xaa;
106 color.blue = 0xae;
107 color.alpha = 0;
108 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
109 if (!pixmap) {
110 WMSetLabelImage(panel->iconLabel, NULL);
111 } else {
112 WMSetLabelImage(panel->iconLabel, pixmap);
113 WMReleasePixmap(pixmap);
116 wfree(file);
117 wfree(path);
121 static void chooseIconCallback(WMWidget * self, void *clientData)
123 char *file;
124 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
125 int result;
127 panel->choosingIcon = 1;
129 WMSetButtonEnabled(panel->browseBtn, False);
131 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
132 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
134 panel->choosingIcon = 0;
135 if (!panel->destroyed) {
136 if (result) {
137 WMSetTextFieldText(panel->iconField, file);
138 wfree(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);
150 static void panelBtnCallback(WMWidget * self, void *data)
152 WMButton *btn = self;
153 AppSettingsPanel *panel = (AppSettingsPanel *) data;
154 char *text;
155 int done = 1;
157 if (panel->okBtn == btn) {
158 text = WMGetTextFieldText(panel->iconField);
159 if (text[0] == 0) {
160 wfree(text);
161 text = NULL;
164 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
165 char *buf;
166 int len = strlen(text) + 64;
168 buf = wmalloc(len);
169 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
170 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
171 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
172 if (text)
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->icon->core->screen_ptr, aicon))
185 wDrawerIconPaint(aicon);
186 else
187 wAppIconPaint(aicon);
189 wDefaultChangeIcon(panel->wwin->screen_ptr, 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 if (done)
217 DestroyDockAppSettingsPanel(panel);
220 #define PWIDTH 295
221 #define PHEIGHT 430
223 void ShowDockAppSettingsPanel(WAppIcon * aicon)
225 AppSettingsPanel *panel;
226 WScreen *scr = aicon->icon->core->screen_ptr;
227 Window parent;
228 WMFont *font;
229 int x, y;
230 WMBox *vbox;
232 panel = wmalloc(sizeof(AppSettingsPanel));
234 panel->editedIcon = aicon;
236 aicon->panel = panel;
237 aicon->editing = 1;
239 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
240 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
242 panel->iconLabel = WMCreateLabel(panel->win);
243 WMResizeWidget(panel->iconLabel, 64, 64);
244 WMMoveWidget(panel->iconLabel, 10, 10);
245 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
247 panel->nameLabel = WMCreateLabel(panel->win);
248 WMResizeWidget(panel->nameLabel, 190, 18);
249 WMMoveWidget(panel->nameLabel, 80, 35);
250 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
251 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
252 WMSetLabelFont(panel->nameLabel, font);
253 WMReleaseFont(font);
254 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
255 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
256 else
257 WMSetLabelText(panel->nameLabel, aicon->wm_class);
259 vbox = WMCreateBox(panel->win);
260 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
261 WMMoveWidget(vbox, 10, 84);
263 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
264 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
265 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
266 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
268 panel->lockBtn = WMCreateSwitchButton(vbox);
269 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
270 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
271 WMSetButtonSelected(panel->lockBtn, aicon->lock);
273 panel->commandFrame = WMCreateFrame(vbox);
274 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
275 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
277 panel->commandField = WMCreateTextField(panel->commandFrame);
278 WMResizeWidget(panel->commandField, 256, 20);
279 WMMoveWidget(panel->commandField, 10, 20);
280 WMSetTextFieldText(panel->commandField, aicon->command);
282 WMMapSubwidgets(panel->commandFrame);
284 panel->pasteCommandFrame = WMCreateFrame(vbox);
285 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
286 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
288 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
289 WMResizeWidget(panel->pasteCommandField, 256, 20);
290 WMMoveWidget(panel->pasteCommandField, 10, 20);
292 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
293 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
294 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
296 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
297 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
298 WMMapSubwidgets(panel->pasteCommandFrame);
300 panel->dndCommandFrame = WMCreateFrame(vbox);
301 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
302 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
304 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
305 WMResizeWidget(panel->dndCommandField, 256, 20);
306 WMMoveWidget(panel->dndCommandField, 10, 20);
308 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
309 WMResizeWidget(panel->dndCommandLabel, 256, 18);
310 WMMoveWidget(panel->dndCommandLabel, 10, 45);
311 #ifdef XDND
312 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
313 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
314 #else
315 WMSetTextFieldEditable(panel->dndCommandField, False);
316 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
317 #endif
318 WMMapSubwidgets(panel->dndCommandFrame);
320 panel->iconFrame = WMCreateFrame(vbox);
321 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
322 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
324 panel->iconField = WMCreateTextField(panel->iconFrame);
325 WMResizeWidget(panel->iconField, 176, 20);
326 WMMoveWidget(panel->iconField, 10, 20);
327 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(aicon->wm_instance, aicon->wm_class, False));
329 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
330 WMResizeWidget(panel->browseBtn, 70, 24);
331 WMMoveWidget(panel->browseBtn, 195, 18);
332 WMSetButtonText(panel->browseBtn, _("Browse..."));
333 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
336 WMBox *hbox;
338 hbox = WMCreateBox(vbox);
339 WMSetBoxHorizontal(hbox, True);
340 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
342 panel->okBtn = WMCreateCommandButton(hbox);
343 WMSetButtonText(panel->okBtn, _("OK"));
344 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
345 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
347 panel->cancelBtn = WMCreateCommandButton(hbox);
348 WMSetButtonText(panel->cancelBtn, _("Cancel"));
349 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
350 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
352 WMMapSubwidgets(hbox);
355 WMRealizeWidget(panel->win);
356 WMMapSubwidgets(panel->win);
357 WMMapSubwidgets(vbox);
358 WMMapSubwidgets(panel->iconFrame);
360 updateSettingsPanelIcon(panel);
362 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
363 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
365 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
368 * make things relative to head
371 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
373 y = aicon->y_pos;
374 if (y < 0)
375 y = 0;
376 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
377 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
379 if (aicon->dock && aicon->dock->type == WM_DOCK) {
380 if (aicon->dock->on_right_side)
381 x = rect.pos.x + rect.size.width / 2;
382 else
383 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
384 } else {
385 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
389 panel->wwin = wManageInternalWindow(scr, parent, None,
390 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
392 panel->wwin->client_leader = WMWidgetXID(panel->win);
394 panel->parent = parent;
396 WMMapWidget(panel->win);
398 wWindowMap(panel->wwin);
401 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
403 if (!panel->destroyed) {
404 XUnmapWindow(dpy, panel->wwin->client_win);
405 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
406 wUnmanageWindow(panel->wwin, False, False);
409 panel->destroyed = 1;
412 * kluge. If we destroy the panel before the icon chooser is closed,
413 * we will crash when it does close, trying to access something in the
414 * destroyed panel. Could use wretain()/wrelease() in the panel,
415 * but it is not working for some reason.
417 if (panel->choosingIcon)
418 return;
420 WMDestroyWidget(panel->win);
422 XDestroyWindow(dpy, panel->parent);
424 panel->editedIcon->panel = NULL;
426 panel->editedIcon->editing = 0;
428 wfree(panel);