- Fixed text in info panel for multibyte (Seiichi SATO <ssato@sh.rim.or.jp>)
[wmaker-crm.git] / src / dockedapp.c
blob7f327840d907ad7f590037de0da1cce38bedbefd
1 /* dockedapp.c- docked application settings panel
3 * Window Maker window manager
5 * Copyright (c) 1998-2002 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"
43 /**** Global variables ****/
44 extern WPreferences wPreferences;
48 typedef struct _AppSettingsPanel {
49 WMWindow *win;
50 WAppIcon *editedIcon;
52 WWindow *wwin;
54 WMLabel *iconLabel;
55 WMLabel *nameLabel;
57 WMFrame *commandFrame;
58 WMTextField *commandField;
60 WMFrame *dndCommandFrame;
61 WMTextField *dndCommandField;
62 WMLabel *dndCommandLabel;
64 WMFrame *pasteCommandFrame;
65 WMTextField *pasteCommandField;
66 WMLabel *pasteCommandLabel;
68 WMFrame *iconFrame;
69 WMTextField *iconField;
70 WMButton *browseBtn;
72 WMButton *autoLaunchBtn;
73 WMButton *lockBtn;
75 WMButton *okBtn;
76 WMButton *cancelBtn;
78 Window parent;
80 /* kluge */
81 unsigned int destroyed:1;
82 unsigned int choosingIcon:1;
83 } AppSettingsPanel;
86 void DestroyDockAppSettingsPanel(AppSettingsPanel *panel);
89 static void
90 updateCommand(WAppIcon *icon, char *command)
92 if (icon->command)
93 wfree(icon->command);
94 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
95 wfree(command);
96 command = NULL;
98 icon->command = command;
100 if (!icon->wm_class && !icon->wm_instance && icon->command
101 && strlen(icon->command)>0) {
102 icon->forced_dock = 1;
107 static void
108 updatePasteCommand(WAppIcon *icon, char *command)
110 if (icon->paste_command)
111 wfree(icon->paste_command);
112 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
113 wfree(command);
114 command = NULL;
116 icon->paste_command = command;
121 #ifdef OFFIX_DND
122 static void
123 updateDNDCommand(WAppIcon *icon, char *command)
125 if (icon->dnd_command)
126 wfree(icon->dnd_command);
127 if (command && (command[0]==0 || (command[0]=='-' && command[1]==0))) {
128 wfree(command);
129 command = NULL;
131 icon->dnd_command = command;
133 #endif /* OFFIX_DND */
136 static void
137 updateSettingsPanelIcon(AppSettingsPanel *panel)
139 char *file;
141 file = WMGetTextFieldText(panel->iconField);
142 if (!file)
143 WMSetLabelImage(panel->iconLabel, NULL);
144 else {
145 char *path;
147 path = FindImage(wPreferences.icon_path, file);
148 if (!path) {
149 wwarning(_("could not find icon %s, used in a docked application"),
150 file);
151 wfree(file);
152 WMSetLabelImage(panel->iconLabel, NULL);
153 return;
154 } else {
155 WMPixmap *pixmap;
156 RColor color;
158 color.red = 0xae;
159 color.green = 0xaa;
160 color.blue = 0xae;
161 color.alpha = 0;
162 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win),
163 path, &color);
164 if (!pixmap) {
165 WMSetLabelImage(panel->iconLabel, NULL);
166 } else {
167 WMSetLabelImage(panel->iconLabel, pixmap);
168 WMReleasePixmap(pixmap);
171 wfree(file);
172 wfree(path);
177 static void
178 chooseIconCallback(WMWidget *self, void *clientData)
180 char *file;
181 AppSettingsPanel *panel = (AppSettingsPanel*)clientData;
182 int result;
184 panel->choosingIcon = 1;
186 WMSetButtonEnabled(panel->browseBtn, False);
188 result = wIconChooserDialog(panel->wwin->screen_ptr, &file,
189 panel->editedIcon->wm_instance,
190 panel->editedIcon->wm_class);
192 panel->choosingIcon = 0;
193 if (!panel->destroyed) {
194 if (result) {
195 WMSetTextFieldText(panel->iconField, file);
196 wfree(file);
197 updateSettingsPanelIcon(panel);
200 WMSetButtonEnabled(panel->browseBtn, True);
201 } else {
202 /* kluge for the case, the user asked to close the panel before
203 * the icon chooser */
204 DestroyDockAppSettingsPanel(panel);
209 static void
210 panelBtnCallback(WMWidget *self, void *data)
212 WMButton *btn = self;
213 AppSettingsPanel *panel = (AppSettingsPanel*)data;
214 char *text;
215 int done;
217 done = 1;
218 if (panel->okBtn == btn) {
219 text = WMGetTextFieldText(panel->iconField);
220 if (text[0]==0) {
221 wfree(text);
222 text = NULL;
224 if (!wIconChangeImageFile(panel->editedIcon->icon, text)) {
225 char *buf;
226 int len = strlen(text) + 64;
228 buf = wmalloc(len);
229 snprintf(buf, len, _("Could not open specified icon file: %s"), text);
230 if (wMessageDialog(panel->wwin->screen_ptr, _("Error"), buf,
231 _("OK"), _("Ignore"), NULL) == WAPRDefault) {
232 if (text)
233 wfree(text);
234 wfree(buf);
235 return;
237 wfree(buf);
238 } else {
239 WAppIcon *aicon = panel->editedIcon;
241 if (aicon == aicon->icon->core->screen_ptr->clip_icon)
242 wClipIconPaint(aicon);
243 else
244 wAppIconPaint(aicon);
246 wDefaultChangeIcon(panel->wwin->screen_ptr, aicon->wm_instance,
247 aicon->wm_class, text);
249 if (text)
250 wfree(text);
252 /* cannot free text from this, because it will be not be duplicated
253 * in updateCommand */
254 text = WMGetTextFieldText(panel->commandField);
255 if (text[0]==0) {
256 wfree(text);
257 text = NULL;
259 updateCommand(panel->editedIcon, text);
260 #ifdef OFFIX_DND
261 /* cannot free text from this, because it will be not be duplicated
262 * in updateDNDCommand */
263 text = WMGetTextFieldText(panel->dndCommandField);
264 updateDNDCommand(panel->editedIcon, text);
265 #endif
266 text = WMGetTextFieldText(panel->pasteCommandField);
267 updatePasteCommand(panel->editedIcon, text);
270 panel->editedIcon->auto_launch =
271 WMGetButtonSelected(panel->autoLaunchBtn);
273 panel->editedIcon->lock =
274 WMGetButtonSelected(panel->lockBtn);
277 if (done)
278 DestroyDockAppSettingsPanel(panel);
282 #define PWIDTH 295
283 #define PHEIGHT 430
286 void
287 ShowDockAppSettingsPanel(WAppIcon *aicon)
289 AppSettingsPanel *panel;
290 WScreen *scr = aicon->icon->core->screen_ptr;
291 Window parent;
292 WMFont *font;
293 int x, y;
294 WMBox *vbox;
296 panel = wmalloc(sizeof(AppSettingsPanel));
297 memset(panel, 0, sizeof(AppSettingsPanel));
299 panel->editedIcon = aicon;
301 aicon->panel = panel;
302 aicon->editing = 1;
304 panel->win = WMCreateWindow(scr->wmscreen, "applicationSettings");
305 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
307 panel->iconLabel = WMCreateLabel(panel->win);
308 WMResizeWidget(panel->iconLabel, 64, 64);
309 WMMoveWidget(panel->iconLabel, 10, 10);
310 WMSetLabelImagePosition(panel->iconLabel, WIPImageOnly);
312 panel->nameLabel = WMCreateLabel(panel->win);
313 WMResizeWidget(panel->nameLabel, 190, 18);
314 WMMoveWidget(panel->nameLabel, 80, 35);
315 WMSetLabelTextAlignment(panel->nameLabel, WALeft);
316 font = WMBoldSystemFontOfSize(scr->wmscreen, 14);
317 WMSetLabelFont(panel->nameLabel, font);
318 WMReleaseFont(font);
319 if (aicon->wm_class && strcmp(aicon->wm_class, "DockApp")==0)
320 WMSetLabelText(panel->nameLabel, aicon->wm_instance);
321 else
322 WMSetLabelText(panel->nameLabel, aicon->wm_class);
325 vbox = WMCreateBox(panel->win);
326 WMResizeWidget(vbox, PWIDTH-20, PHEIGHT-84-10);
327 WMMoveWidget(vbox, 10, 84);
329 panel->autoLaunchBtn = WMCreateSwitchButton(vbox);
330 WMAddBoxSubview(vbox, WMWidgetView(panel->autoLaunchBtn), False, True,
331 20, 20, 2);
332 WMSetButtonText(panel->autoLaunchBtn,
333 _("Start when Window Maker is started"));
334 WMSetButtonSelected(panel->autoLaunchBtn, aicon->auto_launch);
336 panel->lockBtn = WMCreateSwitchButton(vbox);
337 WMAddBoxSubview(vbox, WMWidgetView(panel->lockBtn), False, True,
338 20, 20, 5);
339 WMSetButtonText(panel->lockBtn,
340 _("Lock (prevent accidental removal)"));
341 WMSetButtonSelected(panel->lockBtn, aicon->lock);
343 panel->commandFrame = WMCreateFrame(vbox);
344 WMSetFrameTitle(panel->commandFrame, _("Application path and arguments"));
345 WMAddBoxSubview(vbox, WMWidgetView(panel->commandFrame), False, True,
346 50, 50, 5);
348 panel->commandField = WMCreateTextField(panel->commandFrame);
349 WMResizeWidget(panel->commandField, 256, 20);
350 WMMoveWidget(panel->commandField, 10, 20);
351 WMSetTextFieldText(panel->commandField, aicon->command);
353 WMMapSubwidgets(panel->commandFrame);
355 panel->pasteCommandFrame = WMCreateFrame(vbox);
356 WMSetFrameTitle(panel->pasteCommandFrame,
357 _("Command for middle-click launch"));
358 WMAddBoxSubview(vbox, WMWidgetView(panel->pasteCommandFrame), False, True,
359 70, 70, 5);
361 panel->pasteCommandField = WMCreateTextField(panel->pasteCommandFrame);
362 WMResizeWidget(panel->pasteCommandField, 256, 20);
363 WMMoveWidget(panel->pasteCommandField, 10, 20);
365 panel->pasteCommandLabel = WMCreateLabel(panel->pasteCommandFrame);
366 WMResizeWidget(panel->pasteCommandLabel, 256, 18);
367 WMMoveWidget(panel->pasteCommandLabel, 10, 45);
369 WMSetTextFieldText(panel->pasteCommandField, aicon->paste_command);
370 WMSetLabelText(panel->pasteCommandLabel,
371 _("%s will be replaced with current selection"));
372 WMMapSubwidgets(panel->pasteCommandFrame);
374 panel->dndCommandFrame = WMCreateFrame(vbox);
375 WMSetFrameTitle(panel->dndCommandFrame,
376 _("Command for files dropped with DND"));
377 WMAddBoxSubview(vbox, WMWidgetView(panel->dndCommandFrame), False, True,
378 70, 70, 5);
380 panel->dndCommandField = WMCreateTextField(panel->dndCommandFrame);
381 WMResizeWidget(panel->dndCommandField, 256, 20);
382 WMMoveWidget(panel->dndCommandField, 10, 20);
384 panel->dndCommandLabel = WMCreateLabel(panel->dndCommandFrame);
385 WMResizeWidget(panel->dndCommandLabel, 256, 18);
386 WMMoveWidget(panel->dndCommandLabel, 10, 45);
387 #ifdef OFFIX_DND
388 WMSetTextFieldText(panel->dndCommandField, aicon->dnd_command);
389 WMSetLabelText(panel->dndCommandLabel,
390 _("%d will be replaced with the file name"));
391 #else
392 WMSetTextFieldEditable(panel->dndCommandField, False);
393 WMSetLabelText(panel->dndCommandLabel,
394 _("DND support was not compiled in"));
395 #endif
396 WMMapSubwidgets(panel->dndCommandFrame);
398 panel->iconFrame = WMCreateFrame(vbox);
399 WMSetFrameTitle(panel->iconFrame, _("Icon Image"));
400 WMAddBoxSubview(vbox, WMWidgetView(panel->iconFrame), False, True,
401 50, 50, 10);
403 panel->iconField = WMCreateTextField(panel->iconFrame);
404 WMResizeWidget(panel->iconField, 176, 20);
405 WMMoveWidget(panel->iconField, 10, 20);
406 WMSetTextFieldText(panel->iconField,
407 wDefaultGetIconFile(scr, aicon->wm_instance,
408 aicon->wm_class, True));
410 panel->browseBtn = WMCreateCommandButton(panel->iconFrame);
411 WMResizeWidget(panel->browseBtn, 70, 24);
412 WMMoveWidget(panel->browseBtn, 195, 18);
413 WMSetButtonText(panel->browseBtn, _("Browse..."));
414 WMSetButtonAction(panel->browseBtn, chooseIconCallback, panel);
418 WMBox *hbox;
420 hbox = WMCreateBox(vbox);
421 WMSetBoxHorizontal(hbox, True);
422 WMAddBoxSubview(vbox, WMWidgetView(hbox), False, True, 24, 24, 0);
425 panel->okBtn = WMCreateCommandButton(hbox);
426 WMSetButtonText(panel->okBtn, _("OK"));
427 WMSetButtonAction(panel->okBtn, panelBtnCallback, panel);
428 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->okBtn), False, True, 80, 80, 0);
430 panel->cancelBtn = WMCreateCommandButton(hbox);
431 WMSetButtonText(panel->cancelBtn, _("Cancel"));
432 WMSetButtonAction(panel->cancelBtn, panelBtnCallback, panel);
433 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->cancelBtn), False, True, 80, 80, 5);
435 WMMapSubwidgets(hbox);
438 WMRealizeWidget(panel->win);
439 WMMapSubwidgets(panel->win);
440 WMMapSubwidgets(vbox);
441 WMMapSubwidgets(panel->iconFrame);
443 updateSettingsPanelIcon(panel);
445 parent = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, PWIDTH, PHEIGHT,
446 0, 0, 0);
447 XSelectInput(dpy, parent, KeyPressMask|KeyReleaseMask);
449 XReparentWindow(dpy, WMWidgetXID(panel->win), parent, 0, 0);
451 y = aicon->y_pos;
452 if (y < 0)
453 y = 0;
454 else if (y + PHEIGHT > scr->scr_height)
455 y = scr->scr_height - PHEIGHT - 30;
457 if (aicon->dock && aicon->dock->type == WM_DOCK) {
458 if (aicon->dock->on_right_side)
459 x = scr->scr_width/2;
460 else
461 x = scr->scr_width/2 - PWIDTH - 2;
462 } else {
463 x = (scr->scr_width - PWIDTH)/2;
465 panel->wwin = wManageInternalWindow(scr, parent, None,
466 _("Docked Application Settings"),
467 x, y, PWIDTH, PHEIGHT);
469 panel->wwin->client_leader = WMWidgetXID(panel->win);
471 panel->parent = parent;
473 WMMapWidget(panel->win);
475 wWindowMap(panel->wwin);
479 void
480 DestroyDockAppSettingsPanel(AppSettingsPanel *panel)
482 if (!panel->destroyed) {
483 XUnmapWindow(dpy, panel->wwin->client_win);
484 XReparentWindow(dpy, panel->wwin->client_win,
485 panel->wwin->screen_ptr->root_win, 0, 0);
486 wUnmanageWindow(panel->wwin, False, False);
489 panel->destroyed = 1;
492 * kluge. If we destroy the panel before the icon chooser is closed,
493 * we will crash when it does close, trying to access something in the
494 * destroyed panel. Could use wretain()/wrelease() in the panel,
495 * but it is not working for some reason.
497 if (panel->choosingIcon)
498 return;
500 WMDestroyWidget(panel->win);
502 XDestroyWindow(dpy, panel->parent);
504 panel->editedIcon->panel = NULL;
506 panel->editedIcon->editing = 0;
508 wfree(panel);