Change to the linux kernel coding style
[wmaker-crm.git] / src / dockedapp.c
1 /* dockedapp.c- docked application settings panel
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
21 */
22
23 #include "wconfig.h"
24
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "WindowMaker.h"
31 #include "wcore.h"
32 #include "window.h"
33 #include "icon.h"
34 #include "appicon.h"
35 #include "dock.h"
36 #include "dialog.h"
37 #include "funcs.h"
38 #include "defaults.h"
39 #include "framewin.h"
40 #include "xinerama.h"
41
42 /**** Global variables ****/
43 extern WPreferences wPreferences;
44
45 typedef struct _AppSettingsPanel {
46 WMWindow *win;
47 WAppIcon *editedIcon;
48
49 WWindow *wwin;
50
51 WMLabel *iconLabel;
52 WMLabel *nameLabel;
53
54 WMFrame *commandFrame;
55 WMTextField *commandField;
56
57 WMFrame *dndCommandFrame;
58 WMTextField *dndCommandField;
59 WMLabel *dndCommandLabel;
60
61 WMFrame *pasteCommandFrame;
62 WMTextField *pasteCommandField;
63 WMLabel *pasteCommandLabel;
64
65 WMFrame *iconFrame;
66 WMTextField *iconField;
67 WMButton *browseBtn;
68
69 WMButton *autoLaunchBtn;
70 WMButton *lockBtn;
71
72 WMButton *okBtn;
73 WMButton *cancelBtn;
74
75 Window parent;
76
77 /* kluge */
78 unsigned int destroyed:1;
79 unsigned int choosingIcon:1;
80 } AppSettingsPanel;
81
82 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel);
83
84 static void updateCommand(WAppIcon * icon, char *command)
85 {
86 if (icon->command)
87 wfree(icon->command);
88 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
89 wfree(command);
90 command = NULL;
91 }
92 icon->command = command;
93
94 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
95 icon->forced_dock = 1;
96 }
97 }
98
99 static void updatePasteCommand(WAppIcon * icon, char *command)
100 {
101 if (icon->paste_command)
102 wfree(icon->paste_command);
103 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
104 wfree(command);
105 command = NULL;
106 }
107 icon->paste_command = command;
108 }
109
110 #ifdef XDND
111 static void updateDNDCommand(WAppIcon * icon, char *command)
112 {
113 if (icon->dnd_command)
114 wfree(icon->dnd_command);
115 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
116 wfree(command);
117 command = NULL;
118 }
119 icon->dnd_command = command;
120 }
121 #endif /* XDND */
122
123 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
124 {
125 char *file;
126
127 file = WMGetTextFieldText(panel->iconField);
128 if (!file)
129 WMSetLabelImage(panel->iconLabel, NULL);
130 else {
131 char *path;
132
133 path = FindImage(wPreferences.icon_path, file);
134 if (!path) {
135 wwarning(_("could not find icon %s, used in a docked application"), file);
136 wfree(file);
137 WMSetLabelImage(panel->iconLabel, NULL);
138 return;
139 } else {
140 WMPixmap *pixmap;
141 RColor color;
142
143 color.red = 0xae;
144 color.green = 0xaa;
145 color.blue = 0xae;
146 color.alpha = 0;
147 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
148 if (!pixmap) {
149 WMSetLabelImage(panel->iconLabel, NULL);
150 } else {
151 WMSetLabelImage(panel->iconLabel, pixmap);
152 WMReleasePixmap(pixmap);
153 }
154 }
155 wfree(file);
156 wfree(path);
157 }
158 }
159
160 static void chooseIconCallback(WMWidget * self, void *clientData)
161 {
162 char *file;
163 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
164 int result;
165
166 panel->choosingIcon = 1;
167
168 WMSetButtonEnabled(panel->browseBtn, False);
169
170 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
171 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
172
173 panel->choosingIcon = 0;
174 if (!panel->destroyed) {
175 if (result) {
176 WMSetTextFieldText(panel->iconField, file);
177 wfree(file);
178 updateSettingsPanelIcon(panel);
179 }
180
181 WMSetButtonEnabled(panel->browseBtn, True);
182 } else {
183 /* kluge for the case, the user asked to close the panel before
184 * the icon chooser */
185 DestroyDockAppSettingsPanel(panel);
186 }
187 }
188
189 static void panelBtnCallback(WMWidget * self, void *data)
190 {
191 WMButton *btn = self;
192 AppSettingsPanel *panel = (AppSettingsPanel *) data;
193 char *text;
194 int done;
195
196 done = 1;
197 if (panel->okBtn == btn) {
198 text = WMGetTextFieldText(panel->iconField);
199 if (text[0] == 0) {
200 wfree(text);
201 text = NULL;
202 }
203 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
204 char *buf;
205 int len = strlen(text) + 64;
206
207 buf = wmalloc(len);
208 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
209 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
210 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
211 if (text)
212 wfree(text);
213 wfree(buf);
214 return;
215 }
216 wfree(buf);
217 } else {
218 WAppIcon *aicon = panel->editedIcon;
219
220 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
221 wClipIconPaint(aicon);
222 else
223 wAppIconPaint(aicon);
224
225 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance, aicon->wm_class, text);
226 }
227 if (text)
228 wfree(text);
229
230 /* cannot free text from this, because it will be not be duplicated
231 * in updateCommand */
232 text = WMGetTextFieldText(panel->commandField);
233 if (text[0] == 0) {
234 wfree(text);
235 text = NULL;
236 }
237 updateCommand(panel->editedIcon, text);
238 #ifdef XDND
239 /* cannot free text from this, because it will be not be duplicated
240 * in updateDNDCommand */
241 text = WMGetTextFieldText(panel->dndCommandField);
242 updateDNDCommand(panel->editedIcon, text);
243 #endif
244 text = WMGetTextFieldText(panel->pasteCommandField);
245 updatePasteCommand(panel->editedIcon, text);
246
247 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
248
249 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
250 }
251
252 if (done)
253 DestroyDockAppSettingsPanel(panel);
254 }
255
256 #define PWIDTH 295
257 #define PHEIGHT 430
258
259 void ShowDockAppSettingsPanel(WAppIcon * aicon)
260 {
261 AppSettingsPanel *panel;
262 WScreen *scr = aicon->icon->core->screen_ptr;
263 Window parent;
264 WMFont *font;
265 int x, y;
266 WMBox *vbox;
267
268 panel = wmalloc(sizeof(AppSettingsPanel));
269 memset(panel, 0, sizeof(AppSettingsPanel));
270
271 panel->editedIcon = aicon;
272
273 aicon->panel = panel;
274 aicon->editing = 1;
275
276 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
277 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
278
279 panel->iconLabel = WMCreateLabel(panel->win);
280 WMResizeWidget(panel->iconLabel, 64, 64);
281 WMMoveWidget(panel->iconLabel, 10, 10);
282 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
283
284 panel->nameLabel = WMCreateLabel(panel->win);
285 WMResizeWidget(panel->nameLabel, 190, 18);
286 WMMoveWidget(panel->nameLabel, 80, 35);
287 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
288 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
289 WMSetLabelFont(panel->nameLabel, font);
290 WMReleaseFont(font);
291 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
292 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
293 else
294 WMSetLabelText(panel->nameLabel, aicon->wm_class);
295
296 vbox = WMCreateBox(panel->win);
297 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
298 WMMoveWidget(vbox, 10, 84);
299
300 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
301 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
302 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
303 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
304
305 panel->lockBtn = WMCreateSwitchButton(vbox);
306 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
307 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
308 WMSetButtonSelected(panel->lockBtn, aicon->lock);
309
310 panel->commandFrame = WMCreateFrame(vbox);
311 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
312 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
313
314 panel->commandField = WMCreateTextField(panel->commandFrame);
315 WMResizeWidget(panel->commandField, 256, 20);
316 WMMoveWidget(panel->commandField, 10, 20);
317 WMSetTextFieldText(panel->commandField, aicon->command);
318
319 WMMapSubwidgets(panel->commandFrame);
320
321 panel->pasteCommandFrame = WMCreateFrame(vbox);
322 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
323 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
324
325 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
326 WMResizeWidget(panel->pasteCommandField, 256, 20);
327 WMMoveWidget(panel->pasteCommandField, 10, 20);
328
329 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
330 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
331 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
332
333 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
334 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
335 WMMapSubwidgets(panel->pasteCommandFrame);
336
337 panel->dndCommandFrame = WMCreateFrame(vbox);
338 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
339 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
340
341 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
342 WMResizeWidget(panel->dndCommandField, 256, 20);
343 WMMoveWidget(panel->dndCommandField, 10, 20);
344
345 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
346 WMResizeWidget(panel->dndCommandLabel, 256, 18);
347 WMMoveWidget(panel->dndCommandLabel, 10, 45);
348 #ifdef XDND
349 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
350 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
351 #else
352 WMSetTextFieldEditable(panel->dndCommandField, False);
353 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
354 #endif
355 WMMapSubwidgets(panel->dndCommandFrame);
356
357 panel->iconFrame = WMCreateFrame(vbox);
358 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
359 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
360
361 panel->iconField = WMCreateTextField(panel->iconFrame);
362 WMResizeWidget(panel->iconField, 176, 20);
363 WMMoveWidget(panel->iconField, 10, 20);
364 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(scr, aicon->wm_instance, aicon->wm_class, True));
365
366 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
367 WMResizeWidget(panel->browseBtn, 70, 24);
368 WMMoveWidget(panel->browseBtn, 195, 18);
369 WMSetButtonText(panel->browseBtn, _("Browse..."));
370 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
371
372 {
373 WMBox *hbox;
374
375 hbox = WMCreateBox(vbox);
376 WMSetBoxHorizontal(hbox, True);
377 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
378
379 panel->okBtn = WMCreateCommandButton(hbox);
380 WMSetButtonText(panel->okBtn, _("OK"));
381 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
382 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
383
384 panel->cancelBtn = WMCreateCommandButton(hbox);
385 WMSetButtonText(panel->cancelBtn, _("Cancel"));
386 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
387 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
388
389 WMMapSubwidgets(hbox);
390 }
391
392 WMRealizeWidget(panel->win);
393 WMMapSubwidgets(panel->win);
394 WMMapSubwidgets(vbox);
395 WMMapSubwidgets(panel->iconFrame);
396
397 updateSettingsPanelIcon(panel);
398
399 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
400 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
401
402 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
403
404 /*
405 * make things relative to head
406 */
407 {
408 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
409
410 y = aicon->y_pos;
411 if (y < 0)
412 y = 0;
413 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
414 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
415
416 if (aicon->dock && aicon->dock->type == WM_DOCK) {
417 if (aicon->dock->on_right_side)
418 x = rect.pos.x + rect.size.width / 2;
419 else
420 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
421 } else {
422 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
423 }
424 }
425
426 panel->wwin = wManageInternalWindow(scr, parent, None,
427 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
428
429 panel->wwin->client_leader = WMWidgetXID(panel->win);
430
431 panel->parent = parent;
432
433 WMMapWidget(panel->win);
434
435 wWindowMap(panel->wwin);
436 }
437
438 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
439 {
440 if (!panel->destroyed) {
441 XUnmapWindow(dpy, panel->wwin->client_win);
442 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
443 wUnmanageWindow(panel->wwin, False, False);
444 }
445
446 panel->destroyed = 1;
447
448 /*
449 * kluge. If we destroy the panel before the icon chooser is closed,
450 * we will crash when it does close, trying to access something in the
451 * destroyed panel. Could use wretain()/wrelease() in the panel,
452 * but it is not working for some reason.
453 */
454 if (panel->choosingIcon)
455 return;
456
457 WMDestroyWidget(panel->win);
458
459 XDestroyWindow(dpy, panel->parent);
460
461 panel->editedIcon->panel = NULL;
462
463 panel->editedIcon->editing = 0;
464
465 wfree(panel);
466 }