Improve dockapp recognition
[wmaker-crm.git] / src / dockedapp.c
blob9c90387d751f6692532c3805830e34e6948074bc
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
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.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "WindowMaker.h"
31 #include "window.h"
32 #include "icon.h"
33 #include "appicon.h"
34 #include "dock.h"
35 #include "dialog.h"
36 #include "funcs.h"
37 #include "defaults.h"
38 #include "framewin.h"
39 #include "xinerama.h"
41 /**** Global variables ****/
42 extern WPreferences wPreferences;
44 typedef struct _AppSettingsPanel {
45 WMWindow *win;
46 WAppIcon *editedIcon;
48 WWindow *wwin;
50 WMLabel *iconLabel;
51 WMLabel *nameLabel;
53 WMFrame *commandFrame;
54 WMTextField *commandField;
56 WMFrame *dndCommandFrame;
57 WMTextField *dndCommandField;
58 WMLabel *dndCommandLabel;
60 WMFrame *pasteCommandFrame;
61 WMTextField *pasteCommandField;
62 WMLabel *pasteCommandLabel;
64 WMFrame *iconFrame;
65 WMTextField *iconField;
66 WMButton *browseBtn;
68 WMButton *autoLaunchBtn;
69 WMButton *lockBtn;
71 WMButton *okBtn;
72 WMButton *cancelBtn;
74 Window parent;
76 /* kluge */
77 unsigned int destroyed:1;
78 unsigned int choosingIcon:1;
79 } AppSettingsPanel;
81 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel);
83 static void updateCommand(WAppIcon * icon, char *command)
85 if (icon->command)
86 wfree(icon->command);
87 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
88 wfree(command);
89 command = NULL;
91 icon->command = command;
93 if (!icon->wm_class && !icon->wm_instance && icon->command && strlen(icon->command) > 0) {
94 icon->forced_dock = 1;
98 static void updatePasteCommand(WAppIcon * icon, char *command)
100 if (icon->paste_command)
101 wfree(icon->paste_command);
102 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
103 wfree(command);
104 command = NULL;
106 icon->paste_command = command;
109 #ifdef XDND
110 static void updateDNDCommand(WAppIcon * icon, char *command)
112 if (icon->dnd_command)
113 wfree(icon->dnd_command);
114 if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
115 wfree(command);
116 command = NULL;
118 icon->dnd_command = command;
120 #endif /* XDND */
122 static void updateSettingsPanelIcon(AppSettingsPanel * panel)
124 char *file;
126 file = WMGetTextFieldText(panel->iconField);
127 if (!file)
128 WMSetLabelImage(panel->iconLabel, NULL);
129 else {
130 char *path;
132 path = FindImage(wPreferences.icon_path, file);
133 if (!path) {
134 wwarning(_("could not find icon %s, used in a docked application"), file);
135 wfree(file);
136 WMSetLabelImage(panel->iconLabel, NULL);
137 return;
138 } else {
139 WMPixmap *pixmap;
140 RColor color;
142 color.red = 0xae;
143 color.green = 0xaa;
144 color.blue = 0xae;
145 color.alpha = 0;
146 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
147 if (!pixmap) {
148 WMSetLabelImage(panel->iconLabel, NULL);
149 } else {
150 WMSetLabelImage(panel->iconLabel, pixmap);
151 WMReleasePixmap(pixmap);
154 wfree(file);
155 wfree(path);
159 static void chooseIconCallback(WMWidget * self, void *clientData)
161 char *file;
162 AppSettingsPanel *panel = (AppSettingsPanel *) clientData;
163 int result;
165 panel->choosingIcon = 1;
167 WMSetButtonEnabled(panel->browseBtn, False);
169 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
170 panel->editedIcon->wm_instance, panel->editedIcon->wm_class);
172 panel->choosingIcon = 0;
173 if (!panel->destroyed) {
174 if (result) {
175 WMSetTextFieldText(panel->iconField, file);
176 wfree(file);
177 updateSettingsPanelIcon(panel);
180 WMSetButtonEnabled(panel->browseBtn, True);
181 } else {
182 /* kluge for the case, the user asked to close the panel before
183 * the icon chooser */
184 DestroyDockAppSettingsPanel(panel);
188 static void panelBtnCallback(WMWidget * self, void *data)
190 WMButton *btn = self;
191 AppSettingsPanel *panel = (AppSettingsPanel *) data;
192 char *text;
193 int done;
195 done = 1;
196 if (panel->okBtn == btn) {
197 text = WMGetTextFieldText(panel->iconField);
198 if (text[0] == 0) {
199 wfree(text);
200 text = NULL;
202 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
203 char *buf;
204 int len = strlen(text) + 64;
206 buf = wmalloc(len);
207 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
208 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
209 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
210 if (text)
211 wfree(text);
212 wfree(buf);
213 return;
215 wfree(buf);
216 } else {
217 WAppIcon *aicon = panel->editedIcon;
219 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
220 wClipIconPaint(aicon);
221 else
222 wAppIconPaint(aicon);
224 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance, aicon->wm_class, text);
226 if (text)
227 wfree(text);
229 /* cannot free text from this, because it will be not be duplicated
230 * in updateCommand */
231 text = WMGetTextFieldText(panel->commandField);
232 if (text[0] == 0) {
233 wfree(text);
234 text = NULL;
236 updateCommand(panel->editedIcon, text);
237 #ifdef XDND
238 /* cannot free text from this, because it will be not be duplicated
239 * in updateDNDCommand */
240 text = WMGetTextFieldText(panel->dndCommandField);
241 updateDNDCommand(panel->editedIcon, text);
242 #endif
243 text = WMGetTextFieldText(panel->pasteCommandField);
244 updatePasteCommand(panel->editedIcon, text);
246 panel->editedIcon->auto_launch = WMGetButtonSelected(panel->autoLaunchBtn);
248 panel->editedIcon->lock = WMGetButtonSelected(panel->lockBtn);
251 if (done)
252 DestroyDockAppSettingsPanel(panel);
255 #define PWIDTH 295
256 #define PHEIGHT 430
258 void ShowDockAppSettingsPanel(WAppIcon * aicon)
260 AppSettingsPanel *panel;
261 WScreen *scr = aicon->icon->core->screen_ptr;
262 Window parent;
263 WMFont *font;
264 int x, y;
265 WMBox *vbox;
267 panel = wmalloc(sizeof(AppSettingsPanel));
268 memset(panel, 0, sizeof(AppSettingsPanel));
270 panel->editedIcon = aicon;
272 aicon->panel = panel;
273 aicon->editing = 1;
275 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
276 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
278 panel->iconLabel = WMCreateLabel(panel->win);
279 WMResizeWidget(panel->iconLabel, 64, 64);
280 WMMoveWidget(panel->iconLabel, 10, 10);
281 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
283 panel->nameLabel = WMCreateLabel(panel->win);
284 WMResizeWidget(panel->nameLabel, 190, 18);
285 WMMoveWidget(panel->nameLabel, 80, 35);
286 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
287 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
288 WMSetLabelFont(panel->nameLabel, font);
289 WMReleaseFont(font);
290 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp") == 0)
291 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
292 else
293 WMSetLabelText(panel->nameLabel, aicon->wm_class);
295 vbox = WMCreateBox(panel->win);
296 WMResizeWidget(vbox, PWIDTH - 20, PHEIGHT - 84 - 10);
297 WMMoveWidget(vbox, 10, 84);
299 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
300 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True, 20, 20, 2);
301 WMSetButtonText(panel->autoLaunchBtn, _("Start when Window Maker is started"));
302 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
304 panel->lockBtn = WMCreateSwitchButton(vbox);
305 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True, 20, 20, 5);
306 WMSetButtonText(panel->lockBtn, _("Lock (prevent accidental removal)"));
307 WMSetButtonSelected(panel->lockBtn, aicon->lock);
309 panel->commandFrame = WMCreateFrame(vbox);
310 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
311 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True, 50, 50, 5);
313 panel->commandField = WMCreateTextField(panel->commandFrame);
314 WMResizeWidget(panel->commandField, 256, 20);
315 WMMoveWidget(panel->commandField, 10, 20);
316 WMSetTextFieldText(panel->commandField, aicon->command);
318 WMMapSubwidgets(panel->commandFrame);
320 panel->pasteCommandFrame = WMCreateFrame(vbox);
321 WMSetFrameTitle(panel->pasteCommandFrame, _("Command for middle-click launch"));
322 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True, 70, 70, 5);
324 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
325 WMResizeWidget(panel->pasteCommandField, 256, 20);
326 WMMoveWidget(panel->pasteCommandField, 10, 20);
328 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
329 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
330 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
332 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
333 WMSetLabelText(panel->pasteCommandLabel, _("%s will be replaced with current selection"));
334 WMMapSubwidgets(panel->pasteCommandFrame);
336 panel->dndCommandFrame = WMCreateFrame(vbox);
337 WMSetFrameTitle(panel->dndCommandFrame, _("Command for files dropped with DND"));
338 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True, 70, 70, 5);
340 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
341 WMResizeWidget(panel->dndCommandField, 256, 20);
342 WMMoveWidget(panel->dndCommandField, 10, 20);
344 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
345 WMResizeWidget(panel->dndCommandLabel, 256, 18);
346 WMMoveWidget(panel->dndCommandLabel, 10, 45);
347 #ifdef XDND
348 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
349 WMSetLabelText(panel->dndCommandLabel, _("%d will be replaced with the file name"));
350 #else
351 WMSetTextFieldEditable(panel->dndCommandField, False);
352 WMSetLabelText(panel->dndCommandLabel, _("DND support was not compiled in"));
353 #endif
354 WMMapSubwidgets(panel->dndCommandFrame);
356 panel->iconFrame = WMCreateFrame(vbox);
357 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
358 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True, 50, 50, 10);
360 panel->iconField = WMCreateTextField(panel->iconFrame);
361 WMResizeWidget(panel->iconField, 176, 20);
362 WMMoveWidget(panel->iconField, 10, 20);
363 WMSetTextFieldText(panel->iconField, wDefaultGetIconFile(scr, aicon->wm_instance, aicon->wm_class, True));
365 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
366 WMResizeWidget(panel->browseBtn, 70, 24);
367 WMMoveWidget(panel->browseBtn, 195, 18);
368 WMSetButtonText(panel->browseBtn, _("Browse..."));
369 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
372 WMBox *hbox;
374 hbox = WMCreateBox(vbox);
375 WMSetBoxHorizontal(hbox, True);
376 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
378 panel->okBtn = WMCreateCommandButton(hbox);
379 WMSetButtonText(panel->okBtn, _("OK"));
380 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
381 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
383 panel->cancelBtn = WMCreateCommandButton(hbox);
384 WMSetButtonText(panel->cancelBtn, _("Cancel"));
385 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
386 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
388 WMMapSubwidgets(hbox);
391 WMRealizeWidget(panel->win);
392 WMMapSubwidgets(panel->win);
393 WMMapSubwidgets(vbox);
394 WMMapSubwidgets(panel->iconFrame);
396 updateSettingsPanelIcon(panel);
398 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT, 0, 0, 0);
399 XSelectInput(dpy, parent, KeyPressMask | KeyReleaseMask);
401 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
404 * make things relative to head
407 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
409 y = aicon->y_pos;
410 if (y < 0)
411 y = 0;
412 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
413 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
415 if (aicon->dock && aicon->dock->type == WM_DOCK) {
416 if (aicon->dock->on_right_side)
417 x = rect.pos.x + rect.size.width / 2;
418 else
419 x = rect.pos.x + rect.size.width / 2 - PWIDTH - 2;
420 } else {
421 x = rect.pos.x + (rect.size.width - PWIDTH) / 2;
425 panel->wwin = wManageInternalWindow(scr, parent, None,
426 _("Docked Application Settings"), x, y, PWIDTH, PHEIGHT);
428 panel->wwin->client_leader = WMWidgetXID(panel->win);
430 panel->parent = parent;
432 WMMapWidget(panel->win);
434 wWindowMap(panel->wwin);
437 void DestroyDockAppSettingsPanel(AppSettingsPanel * panel)
439 if (!panel->destroyed) {
440 XUnmapWindow(dpy, panel->wwin->client_win);
441 XReparentWindow(dpy, panel->wwin->client_win, panel->wwin->screen_ptr->root_win, 0, 0);
442 wUnmanageWindow(panel->wwin, False, False);
445 panel->destroyed = 1;
448 * kluge. If we destroy the panel before the icon chooser is closed,
449 * we will crash when it does close, trying to access something in the
450 * destroyed panel. Could use wretain()/wrelease() in the panel,
451 * but it is not working for some reason.
453 if (panel->choosingIcon)
454 return;
456 WMDestroyWidget(panel->win);
458 XDestroyWindow(dpy, panel->parent);
460 panel->editedIcon->panel = NULL;
462 panel->editedIcon->editing = 0;
464 wfree(panel);