changed indentation to use spaces only
[wmaker-crm.git] / src / dockedapp.c
blob85fbba269c0d0de005e9977082ac2bcea0803a9b
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.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "wcore.h"
33 #include "window.h"
34 #include "icon.h"
35 #include "appicon.h"
36 #include "dock.h"
37 #include "dialog.h"
38 #include "funcs.h"
39 #include "defaults.h"
40 #include "framewin.h"
41 #include "xinerama.h"
44 /**** Global variables ****/
45 extern WPreferences wPreferences;
49 typedef struct _AppSettingsPanel {
50 WMWindow *win;
51 WAppIcon *editedIcon;
53 WWindow *wwin;
55 WMLabel *iconLabel;
56 WMLabel *nameLabel;
58 WMFrame *commandFrame;
59 WMTextField *commandField;
61 WMFrame *dndCommandFrame;
62 WMTextField *dndCommandField;
63 WMLabel *dndCommandLabel;
65 WMFrame *pasteCommandFrame;
66 WMTextField *pasteCommandField;
67 WMLabel *pasteCommandLabel;
69 WMFrame *iconFrame;
70 WMTextField *iconField;
71 WMButton *browseBtn;
73 WMButton *autoLaunchBtn;
74 WMButton *lockBtn;
76 WMButton *okBtn;
77 WMButton *cancelBtn;
79 Window parent;
81 /* kluge */
82 unsigned int destroyed:1;
83 unsigned int choosingIcon:1;
84 } AppSettingsPanel;
87 void DestroyDockAppSettingsPanel(AppSettingsPanel *panel);
90 static void
91 updateCommand(WAppIcon *icon, char *command)
93 if (icon->command)
94 wfree(icon->command);
95 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
96 wfree(command);
97 command = NULL;
99 icon->command = command;
101 if (!icon->wm_class && !icon->wm_instance && icon->command
102 && strlen(icon->command)>0) {
103 icon->forced_dock = 1;
108 static void
109 updatePasteCommand(WAppIcon *icon, char *command)
111 if (icon->paste_command)
112 wfree(icon->paste_command);
113 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
114 wfree(command);
115 command = NULL;
117 icon->paste_command = command;
122 #ifdef OFFIX_DND
123 static void
124 updateDNDCommand(WAppIcon *icon, char *command)
126 if (icon->dnd_command)
127 wfree(icon->dnd_command);
128 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
129 wfree(command);
130 command = NULL;
132 icon->dnd_command = command;
134 #endif /* OFFIX_DND */
137 static void
138 updateSettingsPanelIcon(AppSettingsPanel *panel)
140 char *file;
142 file = WMGetTextFieldText(panel->iconField);
143 if (!file)
144 WMSetLabelImage(panel->iconLabel, NULL);
145 else {
146 char *path;
148 path = FindImage(wPreferences.icon_path, file);
149 if (!path) {
150 wwarning(_("could not find icon %s, used in a docked application"),
151 file);
152 wfree(file);
153 WMSetLabelImage(panel->iconLabel, NULL);
154 return;
155 } else {
156 WMPixmap *pixmap;
157 RColor color;
159 color.red = 0xae;
160 color.green = 0xaa;
161 color.blue = 0xae;
162 color.alpha = 0;
163 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win),
164 path, &color);
165 if (!pixmap) {
166 WMSetLabelImage(panel->iconLabel, NULL);
167 } else {
168 WMSetLabelImage(panel->iconLabel, pixmap);
169 WMReleasePixmap(pixmap);
172 wfree(file);
173 wfree(path);
178 static void
179 chooseIconCallback(WMWidget *self, void *clientData)
181 char *file;
182 AppSettingsPanel *panel = (AppSettingsPanel*)clientData;
183 int result;
185 panel->choosingIcon = 1;
187 WMSetButtonEnabled(panel->browseBtn, False);
189 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
190 panel->editedIcon->wm_instance,
191 panel->editedIcon->wm_class);
193 panel->choosingIcon = 0;
194 if (!panel->destroyed) {
195 if (result) {
196 WMSetTextFieldText(panel->iconField, file);
197 wfree(file);
198 updateSettingsPanelIcon(panel);
201 WMSetButtonEnabled(panel->browseBtn, True);
202 } else {
203 /* kluge for the case, the user asked to close the panel before
204 * the icon chooser */
205 DestroyDockAppSettingsPanel(panel);
210 static void
211 panelBtnCallback(WMWidget *self, void *data)
213 WMButton *btn = self;
214 AppSettingsPanel *panel = (AppSettingsPanel*)data;
215 char *text;
216 int done;
218 done = 1;
219 if (panel->okBtn == btn) {
220 text = WMGetTextFieldText(panel->iconField);
221 if (text[0]==0) {
222 wfree(text);
223 text = NULL;
225 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
226 char *buf;
227 int len = strlen(text) + 64;
229 buf = wmalloc(len);
230 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
231 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
232 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
233 if (text)
234 wfree(text);
235 wfree(buf);
236 return;
238 wfree(buf);
239 } else {
240 WAppIcon *aicon = panel->editedIcon;
242 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
243 wClipIconPaint(aicon);
244 else
245 wAppIconPaint(aicon);
247 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance,
248 aicon->wm_class, text);
250 if (text)
251 wfree(text);
253 /* cannot free text from this, because it will be not be duplicated
254 * in updateCommand */
255 text = WMGetTextFieldText(panel->commandField);
256 if (text[0]==0) {
257 wfree(text);
258 text = NULL;
260 updateCommand(panel->editedIcon, text);
261 #ifdef OFFIX_DND
262 /* cannot free text from this, because it will be not be duplicated
263 * in updateDNDCommand */
264 text = WMGetTextFieldText(panel->dndCommandField);
265 updateDNDCommand(panel->editedIcon, text);
266 #endif
267 text = WMGetTextFieldText(panel->pasteCommandField);
268 updatePasteCommand(panel->editedIcon, text);
271 panel->editedIcon->auto_launch =
272 WMGetButtonSelected(panel->autoLaunchBtn);
274 panel->editedIcon->lock =
275 WMGetButtonSelected(panel->lockBtn);
278 if (done)
279 DestroyDockAppSettingsPanel(panel);
283 #define PWIDTH 295
284 #define PHEIGHT 430
287 void
288 ShowDockAppSettingsPanel(WAppIcon *aicon)
290 AppSettingsPanel *panel;
291 WScreen *scr = aicon->icon->core->screen_ptr;
292 Window parent;
293 WMFont *font;
294 int x, y;
295 WMBox *vbox;
297 panel = wmalloc(sizeof(AppSettingsPanel));
298 memset(panel, 0, sizeof(AppSettingsPanel));
300 panel->editedIcon = aicon;
302 aicon->panel = panel;
303 aicon->editing = 1;
305 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
306 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
308 panel->iconLabel = WMCreateLabel(panel->win);
309 WMResizeWidget(panel->iconLabel, 64, 64);
310 WMMoveWidget(panel->iconLabel, 10, 10);
311 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
313 panel->nameLabel = WMCreateLabel(panel->win);
314 WMResizeWidget(panel->nameLabel, 190, 18);
315 WMMoveWidget(panel->nameLabel, 80, 35);
316 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
317 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
318 WMSetLabelFont(panel->nameLabel, font);
319 WMReleaseFont(font);
320 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp")==0)
321 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
322 else
323 WMSetLabelText(panel->nameLabel, aicon->wm_class);
326 vbox = WMCreateBox(panel->win);
327 WMResizeWidget(vbox, PWIDTH-20, PHEIGHT-84-10);
328 WMMoveWidget(vbox, 10, 84);
330 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
331 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True,
332 20, 20, 2);
333 WMSetButtonText(panel->autoLaunchBtn,
334 _("Start when Window Maker is started"));
335 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
337 panel->lockBtn = WMCreateSwitchButton(vbox);
338 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True,
339 20, 20, 5);
340 WMSetButtonText(panel->lockBtn,
341 _("Lock (prevent accidental removal)"));
342 WMSetButtonSelected(panel->lockBtn, aicon->lock);
344 panel->commandFrame = WMCreateFrame(vbox);
345 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
346 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True,
347 50, 50, 5);
349 panel->commandField = WMCreateTextField(panel->commandFrame);
350 WMResizeWidget(panel->commandField, 256, 20);
351 WMMoveWidget(panel->commandField, 10, 20);
352 WMSetTextFieldText(panel->commandField, aicon->command);
354 WMMapSubwidgets(panel->commandFrame);
356 panel->pasteCommandFrame = WMCreateFrame(vbox);
357 WMSetFrameTitle(panel->pasteCommandFrame,
358 _("Command for middle-click launch"));
359 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True,
360 70, 70, 5);
362 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
363 WMResizeWidget(panel->pasteCommandField, 256, 20);
364 WMMoveWidget(panel->pasteCommandField, 10, 20);
366 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
367 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
368 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
370 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
371 WMSetLabelText(panel->pasteCommandLabel,
372 _("%s will be replaced with current selection"));
373 WMMapSubwidgets(panel->pasteCommandFrame);
375 panel->dndCommandFrame = WMCreateFrame(vbox);
376 WMSetFrameTitle(panel->dndCommandFrame,
377 _("Command for files dropped with DND"));
378 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True,
379 70, 70, 5);
381 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
382 WMResizeWidget(panel->dndCommandField, 256, 20);
383 WMMoveWidget(panel->dndCommandField, 10, 20);
385 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
386 WMResizeWidget(panel->dndCommandLabel, 256, 18);
387 WMMoveWidget(panel->dndCommandLabel, 10, 45);
388 #ifdef OFFIX_DND
389 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
390 WMSetLabelText(panel->dndCommandLabel,
391 _("%d will be replaced with the file name"));
392 #else
393 WMSetTextFieldEditable(panel->dndCommandField, False);
394 WMSetLabelText(panel->dndCommandLabel,
395 _("DND support was not compiled in"));
396 #endif
397 WMMapSubwidgets(panel->dndCommandFrame);
399 panel->iconFrame = WMCreateFrame(vbox);
400 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
401 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True,
402 50, 50, 10);
404 panel->iconField = WMCreateTextField(panel->iconFrame);
405 WMResizeWidget(panel->iconField, 176, 20);
406 WMMoveWidget(panel->iconField, 10, 20);
407 WMSetTextFieldText(panel->iconField,
408 wDefaultGetIconFile(scr, aicon->wm_instance,
409 aicon->wm_class, True));
411 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
412 WMResizeWidget(panel->browseBtn, 70, 24);
413 WMMoveWidget(panel->browseBtn, 195, 18);
414 WMSetButtonText(panel->browseBtn, _("Browse..."));
415 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
419 WMBox *hbox;
421 hbox = WMCreateBox(vbox);
422 WMSetBoxHorizontal(hbox, True);
423 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
426 panel->okBtn = WMCreateCommandButton(hbox);
427 WMSetButtonText(panel->okBtn, _("OK"));
428 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
429 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
431 panel->cancelBtn = WMCreateCommandButton(hbox);
432 WMSetButtonText(panel->cancelBtn, _("Cancel"));
433 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
434 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
436 WMMapSubwidgets(hbox);
439 WMRealizeWidget(panel->win);
440 WMMapSubwidgets(panel->win);
441 WMMapSubwidgets(vbox);
442 WMMapSubwidgets(panel->iconFrame);
444 updateSettingsPanelIcon(panel);
446 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT,
447 0, 0, 0);
448 XSelectInput(dpy, parent, KeyPressMask|KeyReleaseMask);
450 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
453 * make things relative to head
456 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
458 y = aicon->y_pos;
459 if (y < 0)
460 y = 0;
461 else if (y + PHEIGHT > rect.pos.y + rect.size.height)
462 y = rect.pos.y + rect.size.height - PHEIGHT - 30;
464 if (aicon->dock && aicon->dock->type == WM_DOCK) {
465 if (aicon->dock->on_right_side)
466 x = rect.pos.x + rect.size.width/2;
467 else
468 x = rect.pos.x + rect.size.width/2 - PWIDTH - 2;
469 } else {
470 x = rect.pos.x + (rect.size.width - PWIDTH)/2;
474 panel->wwin = wManageInternalWindow(scr, parent, None,
475 _("Docked Application Settings"),
476 x, y, PWIDTH, PHEIGHT);
478 panel->wwin->client_leader = WMWidgetXID(panel->win);
480 panel->parent = parent;
482 WMMapWidget(panel->win);
484 wWindowMap(panel->wwin);
488 void
489 DestroyDockAppSettingsPanel(AppSettingsPanel *panel)
491 if (!panel->destroyed) {
492 XUnmapWindow(dpy, panel->wwin->client_win);
493 XReparentWindow(dpy, panel->wwin->client_win,
494 panel->wwin->screen_ptr->root_win, 0, 0);
495 wUnmanageWindow(panel->wwin, False, False);
498 panel->destroyed = 1;
501 * kluge. If we destroy the panel before the icon chooser is closed,
502 * we will crash when it does close, trying to access something in the
503 * destroyed panel. Could use wretain()/wrelease() in the panel,
504 * but it is not working for some reason.
506 if (panel->choosingIcon)
507 return;
509 WMDestroyWidget(panel->win);
511 XDestroyWindow(dpy, panel->parent);
513 panel->editedIcon->panel = NULL;
515 panel->editedIcon->editing = 0;
517 wfree(panel);