WINGs: Add W_KeycodeToKeysym to replace XKeycodeToKeysym/XkbKeycodeToKeysym calls
[wmaker-crm.git] / WPrefs.app / Menu.c
blob87c7a34d3cd3a2335ab6d624efa59cf64da815db
1 /* Menu.c- menu definition
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 2000-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 "WPrefs.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <unistd.h>
27 #include <X11/keysym.h>
28 #include <X11/cursorfont.h>
30 #include "editmenu.h"
32 typedef enum {
33 NoInfo,
34 ExecInfo,
35 CommandInfo,
36 ExternalInfo,
37 PipeInfo,
38 PLPipeInfo,
39 DirectoryInfo,
40 WSMenuInfo,
41 WWindowListInfo,
42 LastInfo
43 } InfoType;
45 #define MAX_SECTION_SIZE 4
47 typedef struct _Panel {
48 WMBox *box;
49 char *sectionName;
51 char *description;
53 CallbackRec callbacks;
54 WMWidget *parent;
56 WMFont *boldFont;
57 WMFont *normalFont;
58 WMColor *white;
59 WMColor *gray;
60 WMColor *black;
62 WMPixmap *markerPix[LastInfo];
64 WMPopUpButton *typeP;
66 WMWidget *itemPad[3];
67 int currentPad;
69 WEditMenu *menu;
70 char *menuPath;
72 WMFrame *optionsF;
74 WMFrame *commandF;
75 WMTextField *commandT; /* command to run */
76 WMButton *browseB;
77 WMButton *xtermC; /* inside xterm? */
79 WMFrame *pathF;
80 WMTextField *pathT;
82 WMFrame *pipeF;
83 WMTextField *pipeT;
84 WMButton *pipeCacheB;
86 WMFrame *plpipeF;
87 WMTextField *plpipeT;
88 WMButton *plpipeCacheB;
90 WMFrame *dpathF;
91 WMTextField *dpathT;
93 WMFrame *dcommandF;
94 WMTextField *dcommandT;
96 WMButton *dstripB;
98 WMFrame *shortF;
99 WMTextField *shortT;
100 WMButton *sgrabB;
101 WMButton *sclearB;
103 WMList *icommandL;
105 WMFrame *paramF;
106 WMTextField *paramT;
108 WMButton *quickB;
110 Bool dontAsk; /* whether to comfirm submenu remove */
111 Bool dontSave;
113 Bool capturing;
115 /* about the currently selected item */
116 WEditMenuItem *currentItem;
117 InfoType currentType;
118 WMWidget *sections[LastInfo][MAX_SECTION_SIZE];
119 } _Panel;
121 typedef struct {
122 InfoType type;
123 union {
124 struct {
125 int command;
126 char *parameter;
127 char *shortcut;
128 } command;
129 struct {
130 char *command;
131 char *shortcut;
132 } exec;
133 struct {
134 char *path;
135 } external;
136 struct {
137 char *command;
138 unsigned cached:1;
139 } pipe;
140 struct {
141 char *directory;
142 char *command;
143 unsigned stripExt:1;
144 } directory;
145 } param;
146 } ItemData;
148 static char *commandNames[] = {
149 "ARRANGE_ICONS",
150 "HIDE_OTHERS",
151 "SHOW_ALL",
152 "EXIT",
153 "SHUTDOWN",
154 "RESTART",
155 "RESTART",
156 "SAVE_SESSION",
157 "CLEAR_SESSION",
158 "REFRESH",
159 "INFO_PANEL",
160 "LEGAL_PANEL"
163 #define ICON_FILE "menus"
165 static void showData(_Panel * panel);
167 static void updateMenuItem(_Panel * panel, WEditMenuItem * item, WMWidget * changedWidget);
169 static void menuItemSelected(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
171 static void menuItemDeselected(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
173 static void menuItemCloned(struct WEditMenuDelegate *delegate, WEditMenu * menu,
174 WEditMenuItem * origItem, WEditMenuItem * newItem);
176 static void menuItemEdited(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
178 static Bool shouldRemoveItem(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
180 static void freeItemData(ItemData * data);
183 static WEditMenuDelegate menuDelegate = {
184 NULL,
185 menuItemCloned,
186 menuItemEdited,
187 menuItemSelected,
188 menuItemDeselected,
189 shouldRemoveItem
192 static void dataChanged(void *self, WMNotification * notif)
194 _Panel *panel = (_Panel *) self;
195 WEditMenuItem *item = panel->currentItem;
196 WMWidget *w = (WMWidget *) WMGetNotificationObject(notif);
198 updateMenuItem(panel, item, w);
201 static void buttonClicked(WMWidget * w, void *data)
203 _Panel *panel = (_Panel *) data;
204 WEditMenuItem *item = panel->currentItem;
206 updateMenuItem(panel, item, w);
209 static void icommandLClicked(WMWidget * w, void *data)
211 _Panel *panel = (_Panel *) data;
212 int cmd;
214 cmd = WMGetListSelectedItemRow(w);
215 if (cmd == 3 || cmd == 4) {
216 WMMapWidget(panel->quickB);
217 } else {
218 WMUnmapWidget(panel->quickB);
220 if (cmd == 6) {
221 WMMapWidget(panel->paramF);
222 } else {
223 WMUnmapWidget(panel->paramF);
227 static void browseForFile(WMWidget * self, void *clientData)
229 _Panel *panel = (_Panel *) clientData;
230 WMFilePanel *filePanel;
231 char *text, *oldprog, *newprog;
233 filePanel = WMGetOpenPanel(WMWidgetScreen(self));
234 text = WMGetTextFieldText(panel->commandT);
236 oldprog = wtrimspace(text);
237 wfree(text);
239 if (oldprog[0] == 0 || oldprog[0] != '/') {
240 wfree(oldprog);
241 oldprog = wstrdup("/");
242 } else {
243 char *ptr = oldprog;
244 while (*ptr && !isspace(*ptr))
245 ptr++;
246 *ptr = 0;
249 WMSetFilePanelCanChooseDirectories(filePanel, False);
251 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, oldprog, _("Select Program"), NULL) == True) {
252 newprog = WMGetFilePanelFileName(filePanel);
253 WMSetTextFieldText(panel->commandT, newprog);
254 updateMenuItem(panel, panel->currentItem, panel->commandT);
255 wfree(newprog);
258 wfree(oldprog);
261 static void sgrabClicked(WMWidget * w, void *data)
263 _Panel *panel = (_Panel *) data;
264 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
265 char *shortcut;
267 if (w == panel->sclearB) {
268 WMSetTextFieldText(panel->shortT, "");
269 updateMenuItem(panel, panel->currentItem, panel->shortT);
270 return;
273 if (!panel->capturing) {
274 panel->capturing = 1;
275 WMSetButtonText(w, _("Cancel"));
276 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync, GrabModeAsync, CurrentTime);
277 shortcut = capture_shortcut(dpy, &panel->capturing, 0);
278 if (shortcut) {
279 WMSetTextFieldText(panel->shortT, shortcut);
280 updateMenuItem(panel, panel->currentItem, panel->shortT);
281 wfree(shortcut);
284 panel->capturing = 0;
285 WMSetButtonText(w, _("Capture"));
286 XUngrabKeyboard(dpy, CurrentTime);
289 static void changedItemPad(WMWidget * w, void *data)
291 _Panel *panel = (_Panel *) data;
292 int padn = WMGetPopUpButtonSelectedItem(w);
294 WMUnmapWidget(panel->itemPad[panel->currentPad]);
295 WMMapWidget(panel->itemPad[padn]);
297 panel->currentPad = padn;
300 static WEditMenu *putNewSubmenu(WEditMenu * menu, const char *title)
302 WEditMenu *tmp;
303 WEditMenuItem *item;
305 item = WAddMenuItemWithTitle(menu, title);
307 tmp = WCreateEditMenu(WMWidgetScreen(menu), title);
308 WSetEditMenuAcceptsDrop(tmp, True);
309 WSetEditMenuDelegate(tmp, &menuDelegate);
310 WSetEditMenuSubmenu(menu, item, tmp);
312 return tmp;
315 static ItemData *putNewItem(_Panel * panel, WEditMenu * menu, int type, const char *title)
317 WEditMenuItem *item;
318 ItemData *data;
320 item = WAddMenuItemWithTitle(menu, title);
322 data = wmalloc(sizeof(ItemData));
323 data->type = type;
324 WSetEditMenuItemData(item, data, (WMCallback *) freeItemData);
325 WSetEditMenuItemImage(item, panel->markerPix[type]);
327 return data;
330 static WEditMenu *makeFactoryMenu(WMWidget * parent, int width)
332 WEditMenu *pad;
334 pad = WCreateEditMenuPad(parent);
335 WMResizeWidget(pad, width, 10);
336 WSetEditMenuMinSize(pad, wmksize(width, 0));
337 WSetEditMenuMaxSize(pad, wmksize(width, 0));
338 WSetEditMenuSelectable(pad, False);
339 WSetEditMenuEditable(pad, False);
340 WSetEditMenuIsFactory(pad, True);
341 WSetEditMenuDelegate(pad, &menuDelegate);
343 return pad;
346 static void createPanel(_Panel * p)
348 _Panel *panel = (_Panel *) p;
349 WMScreen *scr = WMWidgetScreen(panel->parent);
350 WMColor *black = WMBlackColor(scr);
351 WMColor *white = WMWhiteColor(scr);
352 WMColor *gray = WMGrayColor(scr);
353 WMFont *bold = WMBoldSystemFontOfSize(scr, 12);
354 WMFont *font = WMSystemFontOfSize(scr, 12);
355 WMLabel *label;
356 int width;
358 menuDelegate.data = panel;
360 panel->boldFont = bold;
361 panel->normalFont = font;
363 panel->black = black;
364 panel->white = white;
365 panel->gray = gray;
368 Pixmap pix;
369 Display *dpy = WMScreenDisplay(scr);
370 GC gc;
371 WMPixmap *pixm;
373 pixm = WMCreatePixmap(scr, 7, 7, WMScreenDepth(scr), True);
375 pix = WMGetPixmapXID(pixm);
377 XDrawLine(dpy, pix, WMColorGC(black), 0, 3, 6, 3);
378 XDrawLine(dpy, pix, WMColorGC(black), 3, 0, 3, 6);
380 XDrawLine(dpy, pix, WMColorGC(black), 1, 0, 3, 3);
381 XDrawLine(dpy, pix, WMColorGC(black), 1, 6, 3, 3);
382 XDrawLine(dpy, pix, WMColorGC(black), 0, 0, 0, 6);
385 pix = WMGetPixmapMaskXID(pixm);
387 gc = XCreateGC(dpy, pix, 0, NULL);
389 XSetForeground(dpy, gc, 0);
390 XFillRectangle(dpy, pix, gc, 0, 0, 7, 7);
392 XSetForeground(dpy, gc, 1);
393 XDrawLine(dpy, pix, gc, 0, 3, 6, 3);
394 XDrawLine(dpy, pix, gc, 3, 0, 3, 6);
396 panel->markerPix[ExternalInfo] = pixm;
397 panel->markerPix[PipeInfo] = pixm;
398 panel->markerPix[PLPipeInfo] = pixm;
399 panel->markerPix[DirectoryInfo] = pixm;
400 panel->markerPix[WSMenuInfo] = pixm;
401 panel->markerPix[WWindowListInfo] = pixm;
403 XFreeGC(dpy, gc);
406 panel->box = WMCreateBox(panel->parent);
407 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
409 panel->typeP = WMCreatePopUpButton(panel->box);
410 WMResizeWidget(panel->typeP, 150, 20);
411 WMMoveWidget(panel->typeP, 10, 10);
413 WMAddPopUpButtonItem(panel->typeP, _("New Items"));
414 WMAddPopUpButtonItem(panel->typeP, _("Sample Commands"));
415 WMAddPopUpButtonItem(panel->typeP, _("Sample Submenus"));
417 WMSetPopUpButtonAction(panel->typeP, changedItemPad, panel);
419 WMSetPopUpButtonSelectedItem(panel->typeP, 0);
422 WEditMenu *pad;
424 pad = makeFactoryMenu(panel->box, 150);
425 WMMoveWidget(pad, 10, 40);
427 putNewItem(panel, pad, ExecInfo, _("Run Program"));
428 putNewItem(panel, pad, CommandInfo, _("Internal Command"));
429 putNewSubmenu(pad, _("Submenu"));
430 putNewItem(panel, pad, ExternalInfo, _("External Submenu"));
431 putNewItem(panel, pad, PipeInfo, _("Generated Submenu"));
432 putNewItem(panel, pad, PLPipeInfo, _("Generated PL Menu"));
433 putNewItem(panel, pad, DirectoryInfo, _("Directory Contents"));
434 putNewItem(panel, pad, WSMenuInfo, _("Workspace Menu"));
435 putNewItem(panel, pad, WWindowListInfo, _("Window List Menu"));
437 panel->itemPad[0] = pad;
441 WEditMenu *pad;
442 ItemData *data;
443 WMScrollView *sview;
445 sview = WMCreateScrollView(panel->box);
446 WMResizeWidget(sview, 150, 180);
447 WMMoveWidget(sview, 10, 40);
448 WMSetScrollViewHasVerticalScroller(sview, True);
450 pad = makeFactoryMenu(panel->box, 130);
452 WMSetScrollViewContentView(sview, WMWidgetView(pad));
454 data = putNewItem(panel, pad, ExecInfo, _("XTerm"));
455 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white";
457 data = putNewItem(panel, pad, ExecInfo, _("rxvt"));
458 data->param.exec.command = "rxvt";
460 data = putNewItem(panel, pad, ExecInfo, _("ETerm"));
461 data->param.exec.command = "eterm";
463 data = putNewItem(panel, pad, ExecInfo, _("Run..."));
464 data->param.exec.command = _("%A(Run,Type command to run)");
466 data = putNewItem(panel, pad, ExecInfo, _("Firefox"));
467 data->param.exec.command = "firefox";
469 data = putNewItem(panel, pad, ExecInfo, _("gimp"));
470 data->param.exec.command = "gimp";
472 data = putNewItem(panel, pad, ExecInfo, _("epic"));
473 data->param.exec.command = "xterm -e epic";
475 data = putNewItem(panel, pad, ExecInfo, _("ee"));
476 data->param.exec.command = "ee";
478 data = putNewItem(panel, pad, ExecInfo, _("xv"));
479 data->param.exec.command = "xv";
481 data = putNewItem(panel, pad, ExecInfo, _("Evince"));
482 data->param.exec.command = "evince";
484 data = putNewItem(panel, pad, ExecInfo, _("ghostview"));
485 data->param.exec.command = "gv";
487 data = putNewItem(panel, pad, CommandInfo, _("Exit Window Maker"));
488 data->param.command.command = 3;
490 WMMapWidget(pad);
492 panel->itemPad[1] = sview;
496 WEditMenu *pad, *smenu;
497 ItemData *data;
498 WMScrollView *sview;
500 sview = WMCreateScrollView(panel->box);
501 WMResizeWidget(sview, 150, 180);
502 WMMoveWidget(sview, 10, 40);
503 WMSetScrollViewHasVerticalScroller(sview, True);
505 pad = makeFactoryMenu(panel->box, 130);
507 WMSetScrollViewContentView(sview, WMWidgetView(pad));
509 data = putNewItem(panel, pad, ExternalInfo, _("Debian Menu"));
510 data->param.pipe.command = "/etc/" GSUSER_SUBDIR "/" DEFAULTS_SUBDIR "/menu.hook";
512 data = putNewItem(panel, pad, PipeInfo, _("RedHat Menu"));
513 data->param.pipe.command = "wmconfig --output wmaker";
515 data = putNewItem(panel, pad, PipeInfo, _("Menu Conectiva"));
516 data->param.pipe.command = "wmconfig --output wmaker";
518 data = putNewItem(panel, pad, DirectoryInfo, _("Themes"));
519 data->param.directory.command = "setstyle";
520 data->param.directory.directory =
521 "/usr/share/" PACKAGE_TARNAME "/Themes"
522 " /usr/local/share/" PACKAGE_TARNAME "/Themes"
523 " $HOME/" GSUSER_SUBDIR "/" USERDATA_SUBDIR "/" PACKAGE_TARNAME "/Themes";
524 data->param.directory.stripExt = 1;
526 data = putNewItem(panel, pad, DirectoryInfo, _("Bg Images (scale)"));
527 data->param.directory.command = "wmsetbg -u -s";
528 data->param.directory.directory =
529 "/opt/kde2/share/wallpapers"
530 " /usr/share/" PACKAGE_TARNAME "/Backgrounds"
531 " $HOME/" GSUSER_SUBDIR "/" USERDATA_SUBDIR "/" PACKAGE_TARNAME "/Backgrounds";
532 data->param.directory.stripExt = 1;
534 data = putNewItem(panel, pad, DirectoryInfo, _("Bg Images (tile)"));
535 data->param.directory.command = "wmsetbg -u -t";
536 data->param.directory.directory =
537 "/opt/kde2/share/wallpapers"
538 " /usr/share/" PACKAGE_TARNAME "/Backgrounds"
539 " $HOME/" GSUSER_SUBDIR "/" USERDATA_SUBDIR "/" PACKAGE_TARNAME "/Backgrounds";
540 data->param.directory.stripExt = 1;
542 smenu = putNewSubmenu(pad, _("Assorted XTerms"));
544 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Yellow on Blue"));
545 data->param.exec.command = "xterm -sb -sl 2000 -bg midnightblue -fg yellow";
547 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Black"));
548 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white";
550 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Black on White"));
551 data->param.exec.command = "xterm -sb -sl 2000 -bg white -fg black";
553 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Black on Beige"));
554 data->param.exec.command = "xterm -sb -sl 2000 -bg '#bbbb99' -fg black";
556 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Green"));
557 data->param.exec.command = "xterm -sb -sl 2000 -bg '#228822' -fg white";
559 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Olive"));
560 data->param.exec.command = "xterm -sb -sl 2000 -bg '#335533' -fg white";
562 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Blue on Blue"));
563 data->param.exec.command = "xterm -sb -sl 2000 -bg '#112244' -fg '#88aabb'";
565 data = putNewItem(panel, smenu, ExecInfo, _("XTerm BIG FONTS"));
566 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white -fn 10x20";
568 WMMapWidget(pad);
570 panel->itemPad[2] = sview;
573 width = FRAME_WIDTH - 20 - 150 - 10 - 2;
575 panel->optionsF = WMCreateFrame(panel->box);
576 WMResizeWidget(panel->optionsF, width, FRAME_HEIGHT - 15);
577 WMMoveWidget(panel->optionsF, 10 + 150 + 10, 5);
579 width -= 20;
581 /* command */
583 panel->commandF = WMCreateFrame(panel->optionsF);
584 WMResizeWidget(panel->commandF, width, 50);
585 WMMoveWidget(panel->commandF, 10, 20);
586 WMSetFrameTitle(panel->commandF, _("Program to Run"));
587 WMSetFrameTitlePosition(panel->commandF, WTPAtTop);
589 panel->commandT = WMCreateTextField(panel->commandF);
590 WMResizeWidget(panel->commandT, width - 95, 20);
591 WMMoveWidget(panel->commandT, 10, 20);
593 panel->browseB = WMCreateCommandButton(panel->commandF);
594 WMResizeWidget(panel->browseB, 70, 24);
595 WMMoveWidget(panel->browseB, width - 80, 18);
596 WMSetButtonText(panel->browseB, _("Browse"));
597 WMSetButtonAction(panel->browseB, browseForFile, panel);
599 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->commandT);
601 #if 0
602 panel->xtermC = WMCreateSwitchButton(panel->commandF);
603 WMResizeWidget(panel->xtermC, width - 20, 20);
604 WMMoveWidget(panel->xtermC, 10, 50);
605 WMSetButtonText(panel->xtermC, _("Run the program inside a Xterm"));
606 #endif
607 WMMapSubwidgets(panel->commandF);
609 /* path */
611 panel->pathF = WMCreateFrame(panel->optionsF);
612 WMResizeWidget(panel->pathF, width, 150);
613 WMMoveWidget(panel->pathF, 10, 40);
614 WMSetFrameTitle(panel->pathF, _("Path for Menu"));
616 panel->pathT = WMCreateTextField(panel->pathF);
617 WMResizeWidget(panel->pathT, width - 20, 20);
618 WMMoveWidget(panel->pathT, 10, 20);
620 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->pathT);
622 label = WMCreateLabel(panel->pathF);
623 WMResizeWidget(label, width - 20, 90);
624 WMMoveWidget(label, 10, 50);
625 WMSetLabelText(label, _("Enter the path for a file containing a menu\n"
626 "or a list of directories with the programs you\n"
627 "want to have listed in the menu. Ex:\n"
628 "~/" GSUSER_SUBDIR "/" USERDATA_SUBDIR "/" PACKAGE_TARNAME "/menu\n"
629 "or\n" "/usr/bin ~/xbin"));
631 WMMapSubwidgets(panel->pathF);
633 /* pipe */
635 panel->pipeF = WMCreateFrame(panel->optionsF);
636 WMResizeWidget(panel->pipeF, width, 155);
637 WMMoveWidget(panel->pipeF, 10, 30);
638 WMSetFrameTitle(panel->pipeF, _("Command"));
640 panel->pipeT = WMCreateTextField(panel->pipeF);
641 WMResizeWidget(panel->pipeT, width - 20, 20);
642 WMMoveWidget(panel->pipeT, 10, 20);
644 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->pipeT);
646 label = WMCreateLabel(panel->pipeF);
647 WMResizeWidget(label, width - 20, 40);
648 WMMoveWidget(label, 10, 50);
649 WMSetLabelText(label, _("Enter a command that outputs a menu\n" "definition to stdout when invoked."));
651 panel->pipeCacheB = WMCreateSwitchButton(panel->pipeF);
652 WMResizeWidget(panel->pipeCacheB, width - 20, 40);
653 WMMoveWidget(panel->pipeCacheB, 10, 110);
654 WMSetButtonText(panel->pipeCacheB, _("Cache menu contents after opening for\n" "the first time"));
656 WMMapSubwidgets(panel->pipeF);
658 /* proplist pipe */
660 panel->plpipeF = WMCreateFrame(panel->optionsF);
661 WMResizeWidget(panel->plpipeF, width, 155);
662 WMMoveWidget(panel->plpipeF, 10, 30);
663 WMSetFrameTitle(panel->plpipeF, _("Command"));
665 panel->plpipeT = WMCreateTextField(panel->plpipeF);
666 WMResizeWidget(panel->plpipeT, width - 20, 20);
667 WMMoveWidget(panel->plpipeT, 10, 20);
669 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->plpipeT);
671 label = WMCreateLabel(panel->plpipeF);
672 WMResizeWidget(label, width - 20, 40);
673 WMMoveWidget(label, 10, 50);
674 WMSetLabelText(label, _("Enter a command that outputs a proplist menu\n" "definition to stdout when invoked."));
676 panel->plpipeCacheB = WMCreateSwitchButton(panel->plpipeF);
677 WMResizeWidget(panel->plpipeCacheB, width - 20, 40);
678 WMMoveWidget(panel->plpipeCacheB, 10, 110);
679 WMSetButtonText(panel->plpipeCacheB, _("Cache menu contents after opening for\n" "the first time"));
681 WMMapSubwidgets(panel->plpipeF);
683 /* directory menu */
685 panel->dcommandF = WMCreateFrame(panel->optionsF);
686 WMResizeWidget(panel->dcommandF, width, 90);
687 WMMoveWidget(panel->dcommandF, 10, 25);
688 WMSetFrameTitle(panel->dcommandF, _("Command to Open Files"));
690 panel->dcommandT = WMCreateTextField(panel->dcommandF);
691 WMResizeWidget(panel->dcommandT, width - 20, 20);
692 WMMoveWidget(panel->dcommandT, 10, 20);
694 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->dcommandT);
696 label = WMCreateLabel(panel->dcommandF);
697 WMResizeWidget(label, width - 20, 45);
698 WMMoveWidget(label, 10, 40);
699 WMSetLabelText(label, _("Enter the command you want to use to open the\n"
700 "files in the directories listed below."));
702 WMMapSubwidgets(panel->dcommandF);
704 panel->dpathF = WMCreateFrame(panel->optionsF);
705 WMResizeWidget(panel->dpathF, width, 80);
706 WMMoveWidget(panel->dpathF, 10, 125);
707 WMSetFrameTitle(panel->dpathF, _("Directories with Files"));
709 panel->dpathT = WMCreateTextField(panel->dpathF);
710 WMResizeWidget(panel->dpathT, width - 20, 20);
711 WMMoveWidget(panel->dpathT, 10, 20);
713 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->dpathT);
715 panel->dstripB = WMCreateSwitchButton(panel->dpathF);
716 WMResizeWidget(panel->dstripB, width - 20, 20);
717 WMMoveWidget(panel->dstripB, 10, 50);
718 WMSetButtonText(panel->dstripB, _("Strip extensions from file names"));
720 WMSetButtonAction(panel->dstripB, buttonClicked, panel);
722 WMMapSubwidgets(panel->dpathF);
724 /* shortcut */
726 panel->shortF = WMCreateFrame(panel->optionsF);
727 WMResizeWidget(panel->shortF, width, 50);
728 WMMoveWidget(panel->shortF, 10, 160);
729 WMSetFrameTitle(panel->shortF, _("Keyboard Shortcut"));
731 panel->shortT = WMCreateTextField(panel->shortF);
732 WMResizeWidget(panel->shortT, width - 20 - 150, 20);
733 WMMoveWidget(panel->shortT, 10, 20);
735 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->shortT);
737 panel->sgrabB = WMCreateCommandButton(panel->shortF);
738 WMResizeWidget(panel->sgrabB, 70, 24);
739 WMMoveWidget(panel->sgrabB, width - 80, 18);
740 WMSetButtonText(panel->sgrabB, _("Capture"));
741 WMSetButtonAction(panel->sgrabB, sgrabClicked, panel);
743 panel->sclearB = WMCreateCommandButton(panel->shortF);
744 WMResizeWidget(panel->sclearB, 70, 24);
745 WMMoveWidget(panel->sclearB, width - 155, 18);
746 WMSetButtonText(panel->sclearB, _("Clear"));
747 WMSetButtonAction(panel->sclearB, sgrabClicked, panel);
749 WMMapSubwidgets(panel->shortF);
751 /* internal command */
753 panel->icommandL = WMCreateList(panel->optionsF);
754 WMResizeWidget(panel->icommandL, width, 80);
755 WMMoveWidget(panel->icommandL, 10, 20);
757 WMSetListAction(panel->icommandL, icommandLClicked, panel);
759 WMAddNotificationObserver(dataChanged, panel, WMListSelectionDidChangeNotification, panel->icommandL);
761 WMInsertListItem(panel->icommandL, 0, _("Arrange Icons"));
762 WMInsertListItem(panel->icommandL, 1, _("Hide All Windows Except For The Focused One"));
763 WMInsertListItem(panel->icommandL, 2, _("Show All Windows"));
765 WMInsertListItem(panel->icommandL, 3, _("Exit Window Maker"));
766 WMInsertListItem(panel->icommandL, 4, _("Exit X Session"));
767 WMInsertListItem(panel->icommandL, 5, _("Restart Window Maker"));
768 WMInsertListItem(panel->icommandL, 6, _("Start Another Window Manager : ("));
770 WMInsertListItem(panel->icommandL, 7, _("Save Current Session"));
771 WMInsertListItem(panel->icommandL, 8, _("Clear Saved Session"));
772 WMInsertListItem(panel->icommandL, 9, _("Refresh Screen"));
773 WMInsertListItem(panel->icommandL, 10, _("Open Info Panel"));
774 WMInsertListItem(panel->icommandL, 11, _("Open Copyright Panel"));
776 panel->paramF = WMCreateFrame(panel->optionsF);
777 WMResizeWidget(panel->paramF, width, 50);
778 WMMoveWidget(panel->paramF, 10, 105);
779 WMSetFrameTitle(panel->paramF, _("Window Manager to Start"));
781 panel->paramT = WMCreateTextField(panel->paramF);
782 WMResizeWidget(panel->paramT, width - 20, 20);
783 WMMoveWidget(panel->paramT, 10, 20);
785 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->paramT);
787 WMMapSubwidgets(panel->paramF);
789 panel->quickB = WMCreateSwitchButton(panel->optionsF);
790 WMResizeWidget(panel->quickB, width, 20);
791 WMMoveWidget(panel->quickB, 10, 120);
792 WMSetButtonText(panel->quickB, _("Do not confirm action."));
793 WMSetButtonAction(panel->quickB, buttonClicked, panel);
795 label = WMCreateLabel(panel->optionsF);
796 WMResizeWidget(label, width + 5, FRAME_HEIGHT - 50);
797 WMMoveWidget(label, 7, 20);
798 WMSetLabelText(label,
799 _("Instructions:\n\n"
800 " - drag items from the left to the menu to add new items\n"
801 " - drag items out of the menu to remove items\n"
802 " - drag items in menu to change their position\n"
803 " - drag items with Control pressed to copy them\n"
804 " - double click in a menu item to change the label\n"
805 " - click on a menu item to change related information"));
806 WMMapWidget(label);
808 WMRealizeWidget(panel->box);
809 WMMapSubwidgets(panel->box);
810 WMMapWidget(panel->box);
813 int i;
814 for (i = 0; i < wlengthof(panel->itemPad); i++)
815 WMUnmapWidget(panel->itemPad[i]);
817 changedItemPad(panel->typeP, panel);
819 panel->sections[NoInfo][0] = label;
821 panel->sections[ExecInfo][0] = panel->commandF;
822 panel->sections[ExecInfo][1] = panel->shortF;
824 panel->sections[CommandInfo][0] = panel->icommandL;
825 panel->sections[CommandInfo][1] = panel->shortF;
827 panel->sections[ExternalInfo][0] = panel->pathF;
829 panel->sections[PipeInfo][0] = panel->pipeF;
831 panel->sections[PLPipeInfo][0] = panel->plpipeF;
833 panel->sections[DirectoryInfo][0] = panel->dpathF;
834 panel->sections[DirectoryInfo][1] = panel->dcommandF;
836 panel->currentType = NoInfo;
838 showData(panel);
841 WMPoint pos;
843 pos = WMGetViewScreenPosition(WMWidgetView(panel->box));
845 if (pos.x < 200) {
846 pos.x += FRAME_WIDTH + 20;
847 } else {
848 pos.x = 10;
851 pos.y = WMAX(pos.y - 100, 0);
853 if (panel->menu)
854 WEditMenuShowAt(panel->menu, pos.x, pos.y);
858 static void freeItemData(ItemData * data)
860 #define CFREE(d) if (d) wfree(d)
862 /* TODO */
863 switch (data->type) {
864 case CommandInfo:
865 CFREE(data->param.command.parameter);
866 CFREE(data->param.command.shortcut);
867 break;
869 case ExecInfo:
870 CFREE(data->param.exec.command);
871 CFREE(data->param.exec.shortcut);
872 break;
874 case PipeInfo:
875 CFREE(data->param.pipe.command);
876 break;
878 case PLPipeInfo:
879 CFREE(data->param.pipe.command);
880 break;
882 case ExternalInfo:
883 CFREE(data->param.external.path);
884 break;
886 case DirectoryInfo:
887 CFREE(data->param.directory.command);
888 CFREE(data->param.directory.directory);
889 break;
891 default:
892 break;
895 wfree(data);
896 #undef CFREE
899 static ItemData *parseCommand(WMPropList * item)
901 ItemData *data = wmalloc(sizeof(ItemData));
902 WMPropList *p;
903 char *command = NULL;
904 char *parameter = NULL;
905 char *shortcut = NULL;
906 int i = 1;
908 p = WMGetFromPLArray(item, i++);
909 command = WMGetFromPLString(p);
910 if (strcmp(command, "SHORTCUT") == 0) {
911 p = WMGetFromPLArray(item, i++);
912 shortcut = WMGetFromPLString(p);
913 p = WMGetFromPLArray(item, i++);
914 command = WMGetFromPLString(p);
916 p = WMGetFromPLArray(item, i++);
917 if (p)
918 parameter = WMGetFromPLString(p);
920 if (strcmp(command, "EXEC") == 0 || strcmp(command, "SHEXEC") == 0) {
922 data->type = ExecInfo;
924 if (parameter == NULL) {
925 wfree(data);
926 return NULL;
929 data->param.exec.command = wstrdup(parameter);
930 if (shortcut)
931 data->param.exec.shortcut = wstrdup(shortcut);
933 } else if (strcmp(command, "OPEN_MENU") == 0) {
934 char *p;
936 * dir menu, menu file
937 * dir WITH
938 * |pipe
940 p = parameter;
942 if (p == NULL) {
943 wfree(data);
944 return NULL;
947 while (isspace(*p) && *p)
948 p++;
949 if (*p == '|') {
950 if (*(p + 1) == '|') {
951 p++;
952 data->param.pipe.cached = 0;
953 } else {
954 data->param.pipe.cached = 1;
956 data->type = PipeInfo;
957 data->param.pipe.command = wtrimspace(p + 1);
958 } else {
959 char *s;
961 p = wstrdup(p);
963 s = strstr(p, "WITH");
964 if (s) {
965 char **tokens;
966 char **ctokens;
967 int tokn;
968 int i, j;
970 data->type = DirectoryInfo;
972 *s = '\0';
973 s += 5;
974 while (*s && isspace(*s))
975 s++;
976 data->param.directory.command = wstrdup(s);
978 wtokensplit(p, &tokens, &tokn);
979 wfree(p);
981 ctokens = wmalloc(sizeof(char *) * tokn);
983 for (i = 0, j = 0; i < tokn; i++) {
984 if (strcmp(tokens[i], "-noext") == 0) {
985 data->param.directory.stripExt = 1;
986 } else {
987 ctokens[j++] = tokens[i];
990 data->param.directory.directory = wtokenjoin(ctokens, j);
991 wfree(ctokens);
993 wtokenfree(tokens, tokn);
994 } else {
995 data->type = ExternalInfo;
996 data->param.external.path = p;
999 } else if (strcmp(command, "OPEN_PLMENU") == 0) {
1000 char *p;
1002 p = parameter;
1003 while (isspace(*p) && *p)
1004 p++;
1005 if (*p == '|') {
1006 if (*(p + 1) == '|') {
1007 p++;
1008 data->param.pipe.cached = 0;
1009 } else {
1010 data->param.pipe.cached = 1;
1012 data->type = PLPipeInfo;
1013 data->param.pipe.command = wtrimspace(p + 1);
1015 } else if (strcmp(command, "WORKSPACE_MENU") == 0) {
1016 data->type = WSMenuInfo;
1017 } else if (strcmp(command, "WINDOWS_MENU") == 0) {
1018 data->type = WWindowListInfo;
1019 } else {
1020 int cmd;
1022 if (strcmp(command, "ARRANGE_ICONS") == 0) {
1023 cmd = 0;
1024 } else if (strcmp(command, "HIDE_OTHERS") == 0) {
1025 cmd = 1;
1026 } else if (strcmp(command, "SHOW_ALL") == 0) {
1027 cmd = 2;
1028 } else if (strcmp(command, "EXIT") == 0) {
1029 cmd = 3;
1030 } else if (strcmp(command, "SHUTDOWN") == 0) {
1031 cmd = 4;
1032 } else if (strcmp(command, "RESTART") == 0) {
1033 if (parameter) {
1034 cmd = 6;
1035 } else {
1036 cmd = 5;
1038 } else if (strcmp(command, "SAVE_SESSION") == 0) {
1039 cmd = 7;
1040 } else if (strcmp(command, "CLEAR_SESSION") == 0) {
1041 cmd = 8;
1042 } else if (strcmp(command, "REFRESH") == 0) {
1043 cmd = 9;
1044 } else if (strcmp(command, "INFO_PANEL") == 0) {
1045 cmd = 10;
1046 } else if (strcmp(command, "LEGAL_PANEL") == 0) {
1047 cmd = 11;
1048 } else {
1049 wwarning(_("unknown command '%s' in menu"), command);
1050 wfree(data);
1051 return NULL;
1054 data->type = CommandInfo;
1056 data->param.command.command = cmd;
1057 if (shortcut)
1058 data->param.command.shortcut = wstrdup(shortcut);
1059 if (parameter)
1060 data->param.command.parameter = wstrdup(parameter);
1063 return data;
1066 static void updateFrameTitle(_Panel * panel, const char *title, InfoType type)
1068 if (type != NoInfo) {
1069 char *tmp;
1071 switch (type) {
1072 case ExecInfo:
1073 tmp = wstrconcat(title, _(": Execute Program"));
1074 break;
1076 case CommandInfo:
1077 tmp = wstrconcat(title, _(": Perform Internal Command"));
1078 break;
1080 case ExternalInfo:
1081 tmp = wstrconcat(title, _(": Open a Submenu"));
1082 break;
1084 case PipeInfo:
1085 tmp = wstrconcat(title, _(": Program Generated Submenu"));
1086 break;
1088 case PLPipeInfo:
1089 tmp = wstrconcat(title, _(": Program Generated Proplist Submenu"));
1090 break;
1092 case DirectoryInfo:
1093 tmp = wstrconcat(title, _(": Directory Contents Menu"));
1094 break;
1096 case WSMenuInfo:
1097 tmp = wstrconcat(title, _(": Open Workspaces Submenu"));
1098 break;
1100 case WWindowListInfo:
1101 tmp = wstrconcat(title, _(": Open Window List Submenu"));
1102 break;
1104 default:
1105 tmp = NULL;
1106 break;
1108 WMSetFrameTitle(panel->optionsF, tmp);
1109 wfree(tmp);
1110 } else {
1111 WMSetFrameTitle(panel->optionsF, NULL);
1115 static void changeInfoType(_Panel * panel, const char *title, InfoType type)
1117 WMWidget **w;
1119 if (panel->currentType != type) {
1121 w = panel->sections[panel->currentType];
1123 while (*w) {
1124 WMUnmapWidget(*w);
1125 w++;
1127 WMUnmapWidget(panel->paramF);
1128 WMUnmapWidget(panel->quickB);
1130 w = panel->sections[type];
1132 while (*w) {
1133 WMMapWidget(*w);
1134 w++;
1138 updateFrameTitle(panel, title, type);
1140 panel->currentType = type;
1143 static void updateMenuItem(_Panel * panel, WEditMenuItem * item, WMWidget * changedWidget)
1145 ItemData *data = WGetEditMenuItemData(item);
1147 assert(data != NULL);
1149 #define REPLACE(v, d) if (v) wfree(v); v = d
1151 switch (data->type) {
1152 case ExecInfo:
1153 if (changedWidget == panel->commandT) {
1154 REPLACE(data->param.exec.command, WMGetTextFieldText(panel->commandT));
1156 if (changedWidget == panel->shortT) {
1157 REPLACE(data->param.exec.shortcut, WMGetTextFieldText(panel->shortT));
1159 break;
1161 case CommandInfo:
1162 if (changedWidget == panel->icommandL) {
1163 data->param.command.command = WMGetListSelectedItemRow(panel->icommandL);
1165 switch (data->param.command.command) {
1166 case 3:
1167 case 4:
1168 if (changedWidget == panel->quickB) {
1169 REPLACE(data->param.command.parameter, WMGetButtonSelected(panel->quickB)
1170 ? wstrdup("QUICK") : NULL);
1172 break;
1174 case 6:
1175 if (changedWidget == panel->paramT) {
1176 REPLACE(data->param.command.parameter, WMGetTextFieldText(panel->paramT));
1178 break;
1180 if (changedWidget == panel->shortT) {
1181 REPLACE(data->param.command.shortcut, WMGetTextFieldText(panel->shortT));
1184 break;
1186 case PipeInfo:
1187 if (changedWidget == panel->pipeT) {
1188 REPLACE(data->param.pipe.command, WMGetTextFieldText(panel->pipeT));
1190 if (changedWidget == panel->pipeCacheB) {
1191 data->param.pipe.cached = WMGetButtonSelected(panel->pipeCacheB);
1193 break;
1195 case PLPipeInfo:
1196 if (changedWidget == panel->plpipeT) {
1197 REPLACE(data->param.pipe.command, WMGetTextFieldText(panel->plpipeT));
1199 if (changedWidget == panel->plpipeCacheB) {
1200 data->param.pipe.cached = WMGetButtonSelected(panel->plpipeCacheB);
1202 break;
1204 case ExternalInfo:
1205 if (changedWidget == panel->pathT) {
1206 REPLACE(data->param.external.path, WMGetTextFieldText(panel->pathT));
1208 break;
1210 case DirectoryInfo:
1211 if (changedWidget == panel->dpathT) {
1212 REPLACE(data->param.directory.directory, WMGetTextFieldText(panel->dpathT));
1214 if (changedWidget == panel->dcommandT) {
1215 REPLACE(data->param.directory.command, WMGetTextFieldText(panel->dcommandT));
1217 if (changedWidget == panel->dstripB) {
1218 data->param.directory.stripExt = WMGetButtonSelected(panel->dstripB);
1220 break;
1222 default:
1223 assert(0);
1224 break;
1227 #undef REPLACE
1230 static void
1231 menuItemCloned(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * origItem, WEditMenuItem * newItem)
1233 ItemData *data = WGetEditMenuItemData(origItem);
1234 ItemData *newData;
1236 /* Parameter not used, but tell the compiler that it is ok */
1237 (void) delegate;
1238 (void) menu;
1240 if (!data)
1241 return;
1243 #define DUP(s) (s) ? wstrdup(s) : NULL
1245 newData = wmalloc(sizeof(ItemData));
1247 newData->type = data->type;
1249 switch (data->type) {
1250 case ExecInfo:
1251 newData->param.exec.command = DUP(data->param.exec.command);
1252 newData->param.exec.shortcut = DUP(data->param.exec.shortcut);
1253 break;
1255 case CommandInfo:
1256 newData->param.command.command = data->param.command.command;
1257 newData->param.command.parameter = DUP(data->param.command.parameter);
1258 newData->param.command.shortcut = DUP(data->param.command.shortcut);
1259 break;
1261 case PipeInfo:
1262 newData->param.pipe.command = DUP(data->param.pipe.command);
1263 newData->param.pipe.cached = data->param.pipe.cached;
1264 break;
1266 case PLPipeInfo:
1267 newData->param.pipe.command = DUP(data->param.pipe.command);
1268 newData->param.pipe.cached = data->param.pipe.cached;
1269 break;
1271 case ExternalInfo:
1272 newData->param.external.path = DUP(data->param.external.path);
1273 break;
1275 case DirectoryInfo:
1276 newData->param.directory.directory = DUP(data->param.directory.directory);
1277 newData->param.directory.command = DUP(data->param.directory.command);
1278 newData->param.directory.stripExt = data->param.directory.stripExt;
1279 break;
1281 default:
1282 break;
1285 #undef DUP
1287 WSetEditMenuItemData(newItem, newData, (WMCallback *) freeItemData);
1290 static void menuItemEdited(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item)
1292 _Panel *panel = (_Panel *) delegate->data;
1293 WEditMenu *submenu;
1295 /* Parameter not used, but tell the compiler it is ok */
1296 (void) menu;
1298 updateFrameTitle(panel, WGetEditMenuItemTitle(item), panel->currentType);
1300 submenu = WGetEditMenuSubmenu(item);
1301 if (submenu) {
1302 WSetEditMenuTitle(submenu, WGetEditMenuItemTitle(item));
1306 static Bool shouldRemoveItem(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item)
1308 _Panel *panel = (_Panel *) delegate->data;
1310 if (panel->dontAsk)
1311 return True;
1313 if (WGetEditMenuSubmenu(item)) {
1314 int res;
1316 res = WMRunAlertPanel(WMWidgetScreen(menu), NULL,
1317 _("Remove Submenu"),
1318 _("Removing this item will destroy all items inside\n"
1319 "the submenu. Do you really want to do that?"),
1320 _("Yes"), _("No"), _("Yes, don't ask again"));
1321 switch (res) {
1322 case WAPRDefault:
1323 return True;
1324 case WAPRAlternate:
1325 return False;
1326 case WAPROther:
1327 panel->dontAsk = True;
1328 return True;
1331 return True;
1334 static void menuItemDeselected(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * item)
1336 _Panel *panel = (_Panel *) delegate->data;
1338 /* Parameter not used, but tell the compiler that it is ok */
1339 (void) menu;
1340 (void) item;
1342 changeInfoType(panel, NULL, NoInfo);
1345 static void menuItemSelected(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * item)
1347 ItemData *data = WGetEditMenuItemData(item);
1348 _Panel *panel = (_Panel *) delegate->data;
1350 /* Parameter not used, but tell the compiler that it is ok */
1351 (void) menu;
1353 panel->currentItem = item;
1355 if (data) {
1356 changeInfoType(panel, WGetEditMenuItemTitle(item), data->type);
1358 switch (data->type) {
1359 case NoInfo:
1360 break;
1362 case ExecInfo:
1363 WMSetTextFieldText(panel->commandT, data->param.exec.command);
1364 WMSetTextFieldText(panel->shortT, data->param.exec.shortcut);
1365 break;
1367 case CommandInfo:
1368 WMSelectListItem(panel->icommandL, data->param.command.command);
1369 WMSetListPosition(panel->icommandL, data->param.command.command - 2);
1370 WMSetTextFieldText(panel->shortT, data->param.command.shortcut);
1372 switch (data->param.command.command) {
1373 case 3:
1374 case 4:
1375 WMSetButtonSelected(panel->quickB, data->param.command.parameter != NULL);
1376 break;
1377 case 6:
1378 WMSetTextFieldText(panel->paramT, data->param.command.parameter);
1379 break;
1382 icommandLClicked(panel->icommandL, panel);
1383 break;
1385 case PipeInfo:
1386 WMSetTextFieldText(panel->pipeT, data->param.pipe.command);
1387 WMSetButtonSelected(panel->pipeCacheB, data->param.pipe.cached);
1388 break;
1390 case PLPipeInfo:
1391 WMSetTextFieldText(panel->plpipeT, data->param.pipe.command);
1392 WMSetButtonSelected(panel->plpipeCacheB, data->param.pipe.cached);
1393 break;
1395 case ExternalInfo:
1396 WMSetTextFieldText(panel->pathT, data->param.external.path);
1397 break;
1399 case DirectoryInfo:
1400 WMSetTextFieldText(panel->dpathT, data->param.directory.directory);
1401 WMSetTextFieldText(panel->dcommandT, data->param.directory.command);
1402 WMSetButtonSelected(panel->dstripB, data->param.directory.stripExt);
1403 break;
1405 case WSMenuInfo:
1406 break;
1408 default:
1409 break;
1414 static WEditMenu *buildSubmenu(_Panel * panel, WMPropList * pl)
1416 WMScreen *scr = WMWidgetScreen(panel->parent);
1417 WEditMenu *menu;
1418 WEditMenuItem *item;
1419 char *title;
1420 WMPropList *tp, *bp;
1421 int i;
1423 tp = WMGetFromPLArray(pl, 0);
1424 title = WMGetFromPLString(tp);
1426 menu = WCreateEditMenu(scr, title);
1428 for (i = 1; i < WMGetPropListItemCount(pl); i++) {
1429 WMPropList *pi;
1431 pi = WMGetFromPLArray(pl, i);
1433 tp = WMGetFromPLArray(pi, 0);
1434 bp = WMGetFromPLArray(pi, 1);
1436 title = WMGetFromPLString(tp);
1438 if (!bp || WMIsPLArray(bp)) { /* it's a submenu */
1439 WEditMenu *submenu;
1441 submenu = buildSubmenu(panel, pi);
1443 item = WAddMenuItemWithTitle(menu, title);
1445 WSetEditMenuSubmenu(menu, item, submenu);
1446 } else {
1447 ItemData *data;
1449 data = parseCommand(pi);
1451 if (data != NULL) {
1452 item = WAddMenuItemWithTitle(menu, title);
1453 if (panel->markerPix[data->type])
1454 WSetEditMenuItemImage(item, panel->markerPix[data->type]);
1455 WSetEditMenuItemData(item, data, (WMCallback *) freeItemData);
1456 } else {
1457 char buf[256];
1459 snprintf(buf, sizeof(buf), _("Invalid menu command \"%s\" with label \"%s\" cleared"),
1460 WMGetFromPLString(WMGetFromPLArray(pi, 1)),
1461 WMGetFromPLString(WMGetFromPLArray(pi, 0)));
1462 WMRunAlertPanel(scr, panel->parent, _("Warning"), buf, _("OK"), NULL, NULL);
1468 WSetEditMenuAcceptsDrop(menu, True);
1469 WSetEditMenuDelegate(menu, &menuDelegate);
1471 WMRealizeWidget(menu);
1473 return menu;
1476 static void buildMenuFromPL(_Panel * panel, WMPropList * pl)
1478 panel->menu = buildSubmenu(panel, pl);
1481 static WMPropList *getDefaultMenu(_Panel * panel)
1483 WMPropList *menu;
1484 static const char menuPath[] = WMAKER_RESOURCE_PATH "/plmenu";
1486 menu = WMReadPropListFromFile(menuPath);
1487 if (!menu) {
1488 char *buffer, *msg;
1490 msg = _("Could not open default menu from '%s'");
1491 buffer = wmalloc(strlen(msg) + strlen(menuPath) + 10);
1492 sprintf(buffer, msg, menuPath);
1493 WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
1494 _("Error"), buffer, _("OK"), NULL, NULL);
1495 wfree(buffer);
1498 return menu;
1501 static void showData(_Panel * panel)
1503 char *menuPath, *labelText;
1504 char buf[1024];
1505 WMPropList *pmenu;
1507 menuPath = wdefaultspathfordomain("WMRootMenu");
1509 pmenu = WMReadPropListFromFile(menuPath);
1511 /* check if WMRootMenu references another file, and if so,
1512 if that file is in proplist format */
1513 while (WMIsPLString(pmenu)) {
1514 char *path = NULL;
1516 path = wexpandpath(WMGetFromPLString(pmenu));
1518 if (access(path, F_OK) < 0) {
1519 char *old_path = path;
1521 path = wfindfile(DEF_CONFIG_PATHS, path);
1522 wfree(old_path);
1525 /* TODO: if needed, concatenate locale suffix to path.
1526 See getLocalizedMenuFile() in src/rootmenu.c. */
1528 if (!path)
1529 break;
1531 if (access(path, W_OK) < 0) {
1532 snprintf(buf, sizeof(buf),
1533 _("The menu file \"%s\" referenced by "
1534 "WMRootMenu is read-only.\n"
1535 "You cannot use WPrefs to modify it."),
1536 path);
1537 WMRunAlertPanel(WMWidgetScreen(panel->parent),
1538 panel->parent,
1539 _("Warning"), buf,
1540 _("OK"), NULL, NULL);
1541 panel->dontSave = True;
1542 wfree(path);
1543 return;
1546 pmenu = WMReadPropListFromFile(path);
1547 menuPath = path;
1550 if (!pmenu || !WMIsPLArray(pmenu)) {
1551 int res;
1553 res = WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
1554 _("Warning"),
1555 _("The menu file format currently in use is not supported\n"
1556 "by this tool. Do you want to discard the current menu\n"
1557 "to use this tool?"),
1558 _("Yes, Discard and Update"), _("No, Keep Current Menu"), NULL);
1560 if (res == WAPRDefault) {
1561 pmenu = getDefaultMenu(panel);
1563 if (!pmenu) {
1564 pmenu = WMCreatePLArray(WMCreatePLString("Applications"), NULL);
1566 } else {
1567 panel->dontSave = True;
1568 return;
1572 panel->menuPath = menuPath;
1574 snprintf(buf, sizeof(buf),
1575 _("\n\nWhen saved, the menu will be written to the file\n\"%s\"."),
1576 menuPath);
1577 labelText = WMGetLabelText(panel->sections[NoInfo][0]);
1578 labelText = wstrconcat(labelText, buf);
1579 WMSetLabelText(panel->sections[NoInfo][0], labelText);
1580 wfree(labelText);
1582 buildMenuFromPL(panel, pmenu);
1584 WMReleasePropList(pmenu);
1587 static Bool notblank(const char *s)
1589 if (s) {
1590 while (*s++) {
1591 if (!isspace(*s))
1592 return True;
1595 return False;
1598 static WMPropList *processData(const char *title, ItemData * data)
1600 WMPropList *item;
1601 char *s1;
1602 static WMPropList *pscut = NULL;
1603 static WMPropList *pomenu = NULL;
1604 static WMPropList *poplmenu = NULL;
1605 int i;
1607 if (data == NULL)
1608 return NULL;
1610 if (!pscut) {
1611 pscut = WMCreatePLString("SHORTCUT");
1612 pomenu = WMCreatePLString("OPEN_MENU");
1613 poplmenu = WMCreatePLString("OPEN_PLMENU");
1616 item = WMCreatePLArray(WMCreatePLString(title), NULL);
1618 switch (data->type) {
1619 case ExecInfo:
1620 if (data->param.exec.command == NULL)
1621 goto return_null;
1623 if (strpbrk(data->param.exec.command, "&$*|><?`=;")) {
1624 s1 = "SHEXEC";
1625 } else {
1626 s1 = "EXEC";
1629 if (notblank(data->param.exec.shortcut)) {
1630 WMAddToPLArray(item, pscut);
1631 WMAddToPLArray(item, WMCreatePLString(data->param.exec.shortcut));
1634 WMAddToPLArray(item, WMCreatePLString(s1));
1635 WMAddToPLArray(item, WMCreatePLString(data->param.exec.command));
1636 break;
1638 case CommandInfo:
1639 if (notblank(data->param.command.shortcut)) {
1640 WMAddToPLArray(item, pscut);
1641 WMAddToPLArray(item, WMCreatePLString(data->param.command.shortcut));
1644 i = data->param.command.command;
1646 WMAddToPLArray(item, WMCreatePLString(commandNames[i]));
1648 switch (i) {
1649 case 3:
1650 case 4:
1651 if (data->param.command.parameter) {
1652 WMAddToPLArray(item, WMCreatePLString(data->param.command.parameter));
1654 break;
1656 case 6: /* restart */
1657 if (data->param.command.parameter) {
1658 WMAddToPLArray(item, WMCreatePLString(data->param.command.parameter));
1660 break;
1663 break;
1665 case PipeInfo:
1666 case PLPipeInfo:
1667 if (!data->param.pipe.command)
1668 goto return_null;
1669 if (data->type == PLPipeInfo)
1670 WMAddToPLArray(item, poplmenu);
1671 else
1672 WMAddToPLArray(item, pomenu);
1674 if (data->param.pipe.cached)
1675 s1 = wstrconcat("| ", data->param.pipe.command);
1676 else
1677 s1 = wstrconcat("|| ", data->param.pipe.command);
1678 WMAddToPLArray(item, WMCreatePLString(s1));
1679 wfree(s1);
1680 break;
1682 case ExternalInfo:
1683 if (!data->param.external.path)
1684 goto return_null;
1685 WMAddToPLArray(item, pomenu);
1686 WMAddToPLArray(item, WMCreatePLString(data->param.external.path));
1687 break;
1689 case DirectoryInfo:
1691 int l;
1692 char *tmp;
1694 if (!data->param.directory.directory || !data->param.directory.command)
1695 goto return_null;
1697 l = strlen(data->param.directory.directory);
1698 l += strlen(data->param.directory.command);
1699 l += 32;
1701 WMAddToPLArray(item, pomenu);
1703 tmp = wmalloc(l);
1704 sprintf(tmp, "%s%s WITH %s",
1705 data->param.directory.stripExt ? "-noext " : "",
1706 data->param.directory.directory, data->param.directory.command);
1708 WMAddToPLArray(item, WMCreatePLString(tmp));
1709 wfree(tmp);
1711 break;
1713 case WSMenuInfo:
1714 WMAddToPLArray(item, WMCreatePLString("WORKSPACE_MENU"));
1715 break;
1717 case WWindowListInfo:
1718 WMAddToPLArray(item, WMCreatePLString("WINDOWS_MENU"));
1719 break;
1721 default:
1722 assert(0);
1723 break;
1726 return item;
1728 return_null:
1729 WMReleasePropList(item);
1730 return NULL;
1733 static WMPropList *processSubmenu(WEditMenu * menu)
1735 WEditMenuItem *item;
1736 WMPropList *pmenu;
1737 WMPropList *pl;
1738 char *s;
1739 int i;
1741 s = WGetEditMenuTitle(menu);
1742 pl = WMCreatePLString(s);
1744 pmenu = WMCreatePLArray(pl, NULL);
1746 i = 0;
1747 while ((item = WGetEditMenuItem(menu, i++))) {
1748 WEditMenu *submenu;
1750 s = WGetEditMenuItemTitle(item);
1752 submenu = WGetEditMenuSubmenu(item);
1753 if (submenu) {
1754 pl = processSubmenu(submenu);
1755 } else {
1756 pl = processData(s, WGetEditMenuItemData(item));
1759 if (!pl)
1760 continue;
1762 WMAddToPLArray(pmenu, pl);
1765 return pmenu;
1768 static WMPropList *buildPLFromMenu(_Panel * panel)
1770 WMPropList *menu;
1772 menu = processSubmenu(panel->menu);
1774 return menu;
1777 static void storeData(_Panel * panel)
1779 WMPropList *menu;
1781 if (panel->dontSave)
1782 return;
1784 menu = buildPLFromMenu(panel);
1786 WMWritePropListToFile(menu, panel->menuPath);
1788 WMReleasePropList(menu);
1791 static void showMenus(_Panel * panel)
1793 if (panel->menu)
1794 WEditMenuUnhide(panel->menu);
1797 static void hideMenus(_Panel * panel)
1799 if (panel->menu)
1800 WEditMenuHide(panel->menu);
1803 Panel *InitMenu(WMWidget *parent)
1805 _Panel *panel;
1807 panel = wmalloc(sizeof(_Panel));
1809 panel->sectionName = _("Applications Menu Definition");
1811 panel->description = _("Edit the menu for launching applications.");
1813 panel->parent = parent;
1815 panel->callbacks.createWidgets = createPanel;
1816 panel->callbacks.updateDomain = storeData;
1817 panel->callbacks.showPanel = showMenus;
1818 panel->callbacks.hidePanel = hideMenus;
1820 AddSection(panel, ICON_FILE);
1822 return panel;