WINGs: Remove proplist-compat.h
[wmaker-crm.git] / WPrefs.app / Menu.c
blob21a9fe139e4670c638c1d7f13b5645c641b1be56
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>
26 #include <X11/keysym.h>
27 #include <X11/cursorfont.h>
29 #include "editmenu.h"
31 typedef enum {
32 NoInfo,
33 ExecInfo,
34 CommandInfo,
35 ExternalInfo,
36 PipeInfo,
37 DirectoryInfo,
38 WSMenuInfo,
39 WWindowListInfo,
40 LastInfo
41 } InfoType;
43 #define MAX_SECTION_SIZE 4
44 #define PATH_LEN 256
46 typedef struct _Panel {
47 WMBox *box;
48 char *sectionName;
50 char *description;
52 CallbackRec callbacks;
53 WMWidget *parent;
55 WMFont *boldFont;
56 WMFont *normalFont;
57 WMColor *white;
58 WMColor *gray;
59 WMColor *black;
61 WMPixmap *markerPix[LastInfo];
63 WMPopUpButton *typeP;
65 WMWidget *itemPad[3];
66 int currentPad;
68 WEditMenu *menu;
69 char *menuPath;
71 WMFrame *optionsF;
73 WMFrame *commandF;
74 WMTextField *commandT; /* command to run */
75 WMButton *browseB;
76 WMButton *xtermC; /* inside xterm? */
78 WMFrame *pathF;
79 WMTextField *pathT;
81 WMFrame *pipeF;
82 WMTextField *pipeT;
83 WMButton *pipeCacheB;
85 WMFrame *dpathF;
86 WMTextField *dpathT;
88 WMFrame *dcommandF;
89 WMTextField *dcommandT;
91 WMButton *dstripB;
93 WMFrame *shortF;
94 WMTextField *shortT;
95 WMButton *sgrabB;
96 WMButton *sclearB;
98 WMList *icommandL;
100 WMFrame *paramF;
101 WMTextField *paramT;
103 WMButton *quickB;
105 Bool dontAsk; /* whether to comfirm submenu remove */
106 Bool dontSave;
108 Bool capturing;
110 /* about the currently selected item */
111 WEditMenuItem *currentItem;
112 InfoType currentType;
113 WMWidget *sections[LastInfo][MAX_SECTION_SIZE];
114 } _Panel;
116 typedef struct {
117 InfoType type;
118 union {
119 struct {
120 int command;
121 char *parameter;
122 char *shortcut;
123 } command;
124 struct {
125 char *command;
126 char *shortcut;
127 } exec;
128 struct {
129 char *path;
130 } external;
131 struct {
132 char *command;
133 unsigned cached:1;
134 } pipe;
135 struct {
136 char *directory;
137 char *command;
138 unsigned stripExt:1;
139 } directory;
140 } param;
141 } ItemData;
143 static char *commandNames[] = {
144 "ARRANGE_ICONS",
145 "HIDE_OTHERS",
146 "SHOW_ALL",
147 "EXIT",
148 "SHUTDOWN",
149 "RESTART",
150 "RESTART",
151 "SAVE_SESSION",
152 "CLEAR_SESSION",
153 "REFRESH",
154 "INFO_PANEL",
155 "LEGAL_PANEL"
158 #define NEW(type) memset(wmalloc(sizeof(type)), 0, sizeof(type))
160 #define ICON_FILE "menus"
162 static void showData(_Panel * panel);
164 static void updateMenuItem(_Panel * panel, WEditMenuItem * item, WMWidget * changedWidget);
166 static void menuItemSelected(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
168 static void menuItemDeselected(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
170 static void menuItemCloned(struct WEditMenuDelegate *delegate, WEditMenu * menu,
171 WEditMenuItem * origItem, WEditMenuItem * newItem);
173 static void menuItemEdited(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
175 static Bool shouldRemoveItem(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item);
177 static void freeItemData(ItemData * data);
179 static WEditMenuDelegate menuDelegate = {
180 NULL,
181 menuItemCloned,
182 menuItemEdited,
183 menuItemSelected,
184 menuItemDeselected,
185 shouldRemoveItem
188 static void dataChanged(void *self, WMNotification * notif)
190 _Panel *panel = (_Panel *) self;
191 WEditMenuItem *item = panel->currentItem;
192 WMWidget *w = (WMWidget *) WMGetNotificationObject(notif);
194 updateMenuItem(panel, item, w);
197 static void buttonClicked(WMWidget * w, void *data)
199 _Panel *panel = (_Panel *) data;
200 WEditMenuItem *item = panel->currentItem;
202 updateMenuItem(panel, item, w);
205 static void icommandLClicked(WMWidget * w, void *data)
207 _Panel *panel = (_Panel *) data;
208 int cmd;
210 cmd = WMGetListSelectedItemRow(w);
211 if (cmd == 3 || cmd == 4) {
212 WMMapWidget(panel->quickB);
213 } else {
214 WMUnmapWidget(panel->quickB);
216 if (cmd == 6) {
217 WMMapWidget(panel->paramF);
218 } else {
219 WMUnmapWidget(panel->paramF);
223 static void browseForFile(WMWidget * self, void *clientData)
225 _Panel *panel = (_Panel *) clientData;
226 WMFilePanel *filePanel;
227 char *text, *oldprog, *newprog;
229 filePanel = WMGetOpenPanel(WMWidgetScreen(self));
230 text = WMGetTextFieldText(panel->commandT);
232 oldprog = wtrimspace(text);
233 wfree(text);
235 if (oldprog[0] == 0 || oldprog[0] != '/') {
236 wfree(oldprog);
237 oldprog = wstrdup("/");
238 } else {
239 char *ptr = oldprog;
240 while (*ptr && !isspace(*ptr))
241 ptr++;
242 *ptr = 0;
245 WMSetFilePanelCanChooseDirectories(filePanel, False);
247 if (WMRunModalFilePanelForDirectory(filePanel, panel->parent, oldprog, _("Select Program"), NULL) == True) {
248 newprog = WMGetFilePanelFileName(filePanel);
249 WMSetTextFieldText(panel->commandT, newprog);
250 updateMenuItem(panel, panel->currentItem, panel->commandT);
251 wfree(newprog);
254 wfree(oldprog);
257 static char *captureShortcut(Display * dpy, _Panel * panel)
259 XEvent ev;
260 KeySym ksym;
261 char buffer[64];
262 char *key = NULL;
264 while (panel->capturing) {
265 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
266 WMNextEvent(dpy, &ev);
267 if (ev.type == KeyPress && ev.xkey.keycode != 0) {
268 ksym = XKeycodeToKeysym(dpy, ev.xkey.keycode, 0);
269 if (!IsModifierKey(ksym)) {
270 key = XKeysymToString(ksym);
271 panel->capturing = 0;
272 break;
275 WMHandleEvent(&ev);
278 if (!key)
279 return NULL;
281 buffer[0] = 0;
283 if (ev.xkey.state & ControlMask) {
284 strcat(buffer, "Control+");
286 if (ev.xkey.state & ShiftMask) {
287 strcat(buffer, "Shift+");
289 if (ev.xkey.state & Mod1Mask) {
290 strcat(buffer, "Mod1+");
292 if (ev.xkey.state & Mod2Mask) {
293 strcat(buffer, "Mod2+");
295 if (ev.xkey.state & Mod3Mask) {
296 strcat(buffer, "Mod3+");
298 if (ev.xkey.state & Mod4Mask) {
299 strcat(buffer, "Mod4+");
301 if (ev.xkey.state & Mod5Mask) {
302 strcat(buffer, "Mod5+");
304 strcat(buffer, key);
306 return wstrdup(buffer);
309 static void sgrabClicked(WMWidget * w, void *data)
311 _Panel *panel = (_Panel *) data;
312 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
313 char *shortcut;
315 if (w == panel->sclearB) {
316 WMSetTextFieldText(panel->shortT, "");
317 updateMenuItem(panel, panel->currentItem, panel->shortT);
318 return;
321 if (!panel->capturing) {
322 panel->capturing = 1;
323 WMSetButtonText(w, _("Cancel"));
324 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync, GrabModeAsync, CurrentTime);
325 shortcut = captureShortcut(dpy, panel);
326 if (shortcut) {
327 WMSetTextFieldText(panel->shortT, shortcut);
328 updateMenuItem(panel, panel->currentItem, panel->shortT);
329 wfree(shortcut);
332 panel->capturing = 0;
333 WMSetButtonText(w, _("Capture"));
334 XUngrabKeyboard(dpy, CurrentTime);
337 static void changedItemPad(WMWidget * w, void *data)
339 _Panel *panel = (_Panel *) data;
340 int padn = WMGetPopUpButtonSelectedItem(w);
342 WMUnmapWidget(panel->itemPad[panel->currentPad]);
343 WMMapWidget(panel->itemPad[padn]);
345 panel->currentPad = padn;
348 static WEditMenu *putNewSubmenu(WEditMenu * menu, char *title)
350 WEditMenu *tmp;
351 WEditMenuItem *item;
353 item = WAddMenuItemWithTitle(menu, title);
355 tmp = WCreateEditMenu(WMWidgetScreen(menu), title);
356 WSetEditMenuAcceptsDrop(tmp, True);
357 WSetEditMenuDelegate(tmp, &menuDelegate);
358 WSetEditMenuSubmenu(menu, item, tmp);
360 return tmp;
363 static ItemData *putNewItem(_Panel * panel, WEditMenu * menu, int type, char *title)
365 WEditMenuItem *item;
366 ItemData *data;
368 item = WAddMenuItemWithTitle(menu, title);
370 data = NEW(ItemData);
371 data->type = type;
372 WSetEditMenuItemData(item, data, (WMCallback *) freeItemData);
373 WSetEditMenuItemImage(item, panel->markerPix[type]);
375 return data;
378 static WEditMenu *makeFactoryMenu(WMWidget * parent, int width)
380 WEditMenu *pad;
382 pad = WCreateEditMenuPad(parent);
383 WMResizeWidget(pad, width, 10);
384 WSetEditMenuMinSize(pad, wmksize(width, 0));
385 WSetEditMenuMaxSize(pad, wmksize(width, 0));
386 WSetEditMenuSelectable(pad, False);
387 WSetEditMenuEditable(pad, False);
388 WSetEditMenuIsFactory(pad, True);
389 WSetEditMenuDelegate(pad, &menuDelegate);
391 return pad;
394 static void createPanel(_Panel * p)
396 _Panel *panel = (_Panel *) p;
397 WMScreen *scr = WMWidgetScreen(panel->parent);
398 WMColor *black = WMBlackColor(scr);
399 WMColor *white = WMWhiteColor(scr);
400 WMColor *gray = WMGrayColor(scr);
401 WMFont *bold = WMBoldSystemFontOfSize(scr, 12);
402 WMFont *font = WMSystemFontOfSize(scr, 12);
403 WMLabel *label;
404 int width;
406 menuDelegate.data = panel;
408 panel->boldFont = bold;
409 panel->normalFont = font;
411 panel->black = black;
412 panel->white = white;
413 panel->gray = gray;
416 Pixmap pix;
417 Display *dpy = WMScreenDisplay(scr);
418 GC gc;
419 WMPixmap *pixm;
421 pixm = WMCreatePixmap(scr, 7, 7, WMScreenDepth(scr), True);
423 pix = WMGetPixmapXID(pixm);
425 XDrawLine(dpy, pix, WMColorGC(black), 0, 3, 6, 3);
426 XDrawLine(dpy, pix, WMColorGC(black), 3, 0, 3, 6);
428 XDrawLine(dpy, pix, WMColorGC(black), 1, 0, 3, 3);
429 XDrawLine(dpy, pix, WMColorGC(black), 1, 6, 3, 3);
430 XDrawLine(dpy, pix, WMColorGC(black), 0, 0, 0, 6);
433 pix = WMGetPixmapMaskXID(pixm);
435 gc = XCreateGC(dpy, pix, 0, NULL);
437 XSetForeground(dpy, gc, 0);
438 XFillRectangle(dpy, pix, gc, 0, 0, 7, 7);
440 XSetForeground(dpy, gc, 1);
441 XDrawLine(dpy, pix, gc, 0, 3, 6, 3);
442 XDrawLine(dpy, pix, gc, 3, 0, 3, 6);
444 panel->markerPix[ExternalInfo] = pixm;
445 panel->markerPix[PipeInfo] = pixm;
446 panel->markerPix[DirectoryInfo] = pixm;
447 panel->markerPix[WSMenuInfo] = pixm;
448 panel->markerPix[WWindowListInfo] = pixm;
450 XFreeGC(dpy, gc);
453 panel->box = WMCreateBox(panel->parent);
454 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
456 panel->typeP = WMCreatePopUpButton(panel->box);
457 WMResizeWidget(panel->typeP, 150, 20);
458 WMMoveWidget(panel->typeP, 10, 10);
460 WMAddPopUpButtonItem(panel->typeP, _("New Items"));
461 WMAddPopUpButtonItem(panel->typeP, _("Sample Commands"));
462 WMAddPopUpButtonItem(panel->typeP, _("Sample Submenus"));
464 WMSetPopUpButtonAction(panel->typeP, changedItemPad, panel);
466 WMSetPopUpButtonSelectedItem(panel->typeP, 0);
469 WEditMenu *pad;
471 pad = makeFactoryMenu(panel->box, 150);
472 WMMoveWidget(pad, 10, 40);
474 putNewItem(panel, pad, ExecInfo, _("Run Program"));
475 putNewItem(panel, pad, CommandInfo, _("Internal Command"));
476 putNewSubmenu(pad, _("Submenu"));
477 putNewItem(panel, pad, ExternalInfo, _("External Submenu"));
478 putNewItem(panel, pad, PipeInfo, _("Generated Submenu"));
479 putNewItem(panel, pad, DirectoryInfo, _("Directory Contents"));
480 putNewItem(panel, pad, WSMenuInfo, _("Workspace Menu"));
481 putNewItem(panel, pad, WWindowListInfo, _("Window List Menu"));
483 panel->itemPad[0] = pad;
487 WEditMenu *pad;
488 ItemData *data;
489 WMScrollView *sview;
491 sview = WMCreateScrollView(panel->box);
492 WMResizeWidget(sview, 150, 180);
493 WMMoveWidget(sview, 10, 40);
494 WMSetScrollViewHasVerticalScroller(sview, True);
496 pad = makeFactoryMenu(panel->box, 130);
498 WMSetScrollViewContentView(sview, WMWidgetView(pad));
500 data = putNewItem(panel, pad, ExecInfo, _("XTerm"));
501 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white";
503 data = putNewItem(panel, pad, ExecInfo, _("rxvt"));
504 data->param.exec.command = "rxvt";
506 data = putNewItem(panel, pad, ExecInfo, _("ETerm"));
507 data->param.exec.command = "eterm";
509 data = putNewItem(panel, pad, ExecInfo, _("Run..."));
510 data->param.exec.command = _("%A(Run,Type command to run)");
512 data = putNewItem(panel, pad, ExecInfo, _("Firefox"));
513 data->param.exec.command = "firefox";
515 data = putNewItem(panel, pad, ExecInfo, _("gimp"));
516 data->param.exec.command = "gimp";
518 data = putNewItem(panel, pad, ExecInfo, _("epic"));
519 data->param.exec.command = "xterm -e epic";
521 data = putNewItem(panel, pad, ExecInfo, _("ee"));
522 data->param.exec.command = "ee";
524 data = putNewItem(panel, pad, ExecInfo, _("xv"));
525 data->param.exec.command = "xv";
527 data = putNewItem(panel, pad, ExecInfo, _("Evince"));
528 data->param.exec.command = "evince";
530 data = putNewItem(panel, pad, ExecInfo, _("ghostview"));
531 data->param.exec.command = "gv";
533 data = putNewItem(panel, pad, CommandInfo, _("Exit Window Maker"));
534 data->param.command.command = 3;
536 WMMapWidget(pad);
538 panel->itemPad[1] = sview;
542 WEditMenu *pad, *smenu;
543 ItemData *data;
544 WMScrollView *sview;
546 sview = WMCreateScrollView(panel->box);
547 WMResizeWidget(sview, 150, 180);
548 WMMoveWidget(sview, 10, 40);
549 WMSetScrollViewHasVerticalScroller(sview, True);
551 pad = makeFactoryMenu(panel->box, 130);
553 WMSetScrollViewContentView(sview, WMWidgetView(pad));
555 data = putNewItem(panel, pad, ExternalInfo, _("Debian Menu"));
556 data->param.pipe.command = "/etc/X11/WindowMaker/menu.hook";
558 data = putNewItem(panel, pad, PipeInfo, _("RedHat Menu"));
559 data->param.pipe.command = "wmconfig --output wmaker";
561 data = putNewItem(panel, pad, PipeInfo, _("Menu Conectiva"));
562 data->param.pipe.command = "wmconfig --output wmaker";
564 data = putNewItem(panel, pad, DirectoryInfo, _("Themes"));
565 data->param.directory.command = "setstyle";
566 data->param.directory.directory =
567 "/usr/share/WindowMaker/Themes /usr/local/share/WindowMaker/Themes $HOME/GNUstep/Library/WindowMaker/Themes";
568 data->param.directory.stripExt = 1;
570 data = putNewItem(panel, pad, DirectoryInfo, _("Bg Images (scale)"));
571 data->param.directory.command = "wmsetbg -u -s";
572 data->param.directory.directory =
573 "/opt/kde2/share/wallpapers /usr/share/WindowMaker/Backgrounds $HOME/GNUstep/Library/WindowMaker/Backgrounds";
574 data->param.directory.stripExt = 1;
576 data = putNewItem(panel, pad, DirectoryInfo, _("Bg Images (tile)"));
577 data->param.directory.command = "wmsetbg -u -t";
578 data->param.directory.directory =
579 "/opt/kde2/share/wallpapers /usr/share/WindowMaker/Backgrounds $HOME/GNUstep/Library/WindowMaker/Backgrounds";
580 data->param.directory.stripExt = 1;
582 smenu = putNewSubmenu(pad, _("Assorted XTerms"));
584 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Yellow on Blue"));
585 data->param.exec.command = "xterm -sb -sl 2000 -bg midnightblue -fg yellow";
587 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Black"));
588 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white";
590 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Black on White"));
591 data->param.exec.command = "xterm -sb -sl 2000 -bg white -fg black";
593 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Black on Beige"));
594 data->param.exec.command = "xterm -sb -sl 2000 -bg '#bbbb99' -fg black";
596 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Green"));
597 data->param.exec.command = "xterm -sb -sl 2000 -bg '#228822' -fg white";
599 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Olive"));
600 data->param.exec.command = "xterm -sb -sl 2000 -bg '#335533' -fg white";
602 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Blue on Blue"));
603 data->param.exec.command = "xterm -sb -sl 2000 -bg '#112244' -fg '#88aabb'";
605 data = putNewItem(panel, smenu, ExecInfo, _("XTerm BIG FONTS"));
606 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white -fn 10x20";
608 WMMapWidget(pad);
610 panel->itemPad[2] = sview;
613 width = FRAME_WIDTH - 20 - 150 - 10 - 2;
615 panel->optionsF = WMCreateFrame(panel->box);
616 WMResizeWidget(panel->optionsF, width, FRAME_HEIGHT - 15);
617 WMMoveWidget(panel->optionsF, 10 + 150 + 10, 5);
619 width -= 20;
621 /* command */
623 panel->commandF = WMCreateFrame(panel->optionsF);
624 WMResizeWidget(panel->commandF, width, 50);
625 WMMoveWidget(panel->commandF, 10, 20);
626 WMSetFrameTitle(panel->commandF, _("Program to Run"));
627 WMSetFrameTitlePosition(panel->commandF, WTPAtTop);
629 panel->commandT = WMCreateTextField(panel->commandF);
630 WMResizeWidget(panel->commandT, width - 95, 20);
631 WMMoveWidget(panel->commandT, 10, 20);
633 panel->browseB = WMCreateCommandButton(panel->commandF);
634 WMResizeWidget(panel->browseB, 70, 24);
635 WMMoveWidget(panel->browseB, width - 80, 18);
636 WMSetButtonText(panel->browseB, _("Browse"));
637 WMSetButtonAction(panel->browseB, browseForFile, panel);
639 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->commandT);
641 #if 0
642 panel->xtermC = WMCreateSwitchButton(panel->commandF);
643 WMResizeWidget(panel->xtermC, width - 20, 20);
644 WMMoveWidget(panel->xtermC, 10, 50);
645 WMSetButtonText(panel->xtermC, _("Run the program inside a Xterm"));
646 #endif
647 WMMapSubwidgets(panel->commandF);
649 /* path */
651 panel->pathF = WMCreateFrame(panel->optionsF);
652 WMResizeWidget(panel->pathF, width, 150);
653 WMMoveWidget(panel->pathF, 10, 40);
654 WMSetFrameTitle(panel->pathF, _("Path for Menu"));
656 panel->pathT = WMCreateTextField(panel->pathF);
657 WMResizeWidget(panel->pathT, width - 20, 20);
658 WMMoveWidget(panel->pathT, 10, 20);
660 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->pathT);
662 label = WMCreateLabel(panel->pathF);
663 WMResizeWidget(label, width - 20, 80);
664 WMMoveWidget(label, 10, 50);
665 WMSetLabelText(label, _("Enter the path for a file containing a menu\n"
666 "or a list of directories with the programs you\n"
667 "want to have listed in the menu. Ex:\n"
668 "~/GNUstep/Library/WindowMaker/menu\n" "or\n" "/usr/X11R6/bin ~/xbin"));
670 WMMapSubwidgets(panel->pathF);
672 /* pipe */
674 panel->pipeF = WMCreateFrame(panel->optionsF);
675 WMResizeWidget(panel->pipeF, width, 155);
676 WMMoveWidget(panel->pipeF, 10, 30);
677 WMSetFrameTitle(panel->pipeF, _("Command"));
679 panel->pipeT = WMCreateTextField(panel->pipeF);
680 WMResizeWidget(panel->pipeT, width - 20, 20);
681 WMMoveWidget(panel->pipeT, 10, 20);
683 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->pipeT);
685 label = WMCreateLabel(panel->pipeF);
686 WMResizeWidget(label, width - 20, 40);
687 WMMoveWidget(label, 10, 50);
688 WMSetLabelText(label, _("Enter a command that outputs a menu\n" "definition to stdout when invoked."));
690 panel->pipeCacheB = WMCreateSwitchButton(panel->pipeF);
691 WMResizeWidget(panel->pipeCacheB, width - 20, 40);
692 WMMoveWidget(panel->pipeCacheB, 10, 110);
693 WMSetButtonText(panel->pipeCacheB, _("Cache menu contents after opening for\n" "the first time"));
695 WMMapSubwidgets(panel->pipeF);
697 /* directory menu */
699 panel->dcommandF = WMCreateFrame(panel->optionsF);
700 WMResizeWidget(panel->dcommandF, width, 90);
701 WMMoveWidget(panel->dcommandF, 10, 25);
702 WMSetFrameTitle(panel->dcommandF, _("Command to Open Files"));
704 panel->dcommandT = WMCreateTextField(panel->dcommandF);
705 WMResizeWidget(panel->dcommandT, width - 20, 20);
706 WMMoveWidget(panel->dcommandT, 10, 20);
708 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->dcommandT);
710 label = WMCreateLabel(panel->dcommandF);
711 WMResizeWidget(label, width - 20, 45);
712 WMMoveWidget(label, 10, 40);
713 WMSetLabelText(label, _("Enter the command you want to use to open the\n"
714 "files in the directories listed below."));
716 WMMapSubwidgets(panel->dcommandF);
718 panel->dpathF = WMCreateFrame(panel->optionsF);
719 WMResizeWidget(panel->dpathF, width, 80);
720 WMMoveWidget(panel->dpathF, 10, 125);
721 WMSetFrameTitle(panel->dpathF, _("Directories with Files"));
723 panel->dpathT = WMCreateTextField(panel->dpathF);
724 WMResizeWidget(panel->dpathT, width - 20, 20);
725 WMMoveWidget(panel->dpathT, 10, 20);
727 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->dpathT);
729 panel->dstripB = WMCreateSwitchButton(panel->dpathF);
730 WMResizeWidget(panel->dstripB, width - 20, 20);
731 WMMoveWidget(panel->dstripB, 10, 50);
732 WMSetButtonText(panel->dstripB, _("Strip extensions from file names"));
734 WMSetButtonAction(panel->dstripB, buttonClicked, panel);
736 WMMapSubwidgets(panel->dpathF);
738 /* shortcut */
740 panel->shortF = WMCreateFrame(panel->optionsF);
741 WMResizeWidget(panel->shortF, width, 50);
742 WMMoveWidget(panel->shortF, 10, 160);
743 WMSetFrameTitle(panel->shortF, _("Keyboard Shortcut"));
745 panel->shortT = WMCreateTextField(panel->shortF);
746 WMResizeWidget(panel->shortT, width - 20 - 150, 20);
747 WMMoveWidget(panel->shortT, 10, 20);
749 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->shortT);
751 panel->sgrabB = WMCreateCommandButton(panel->shortF);
752 WMResizeWidget(panel->sgrabB, 70, 24);
753 WMMoveWidget(panel->sgrabB, width - 80, 18);
754 WMSetButtonText(panel->sgrabB, _("Capture"));
755 WMSetButtonAction(panel->sgrabB, sgrabClicked, panel);
757 panel->sclearB = WMCreateCommandButton(panel->shortF);
758 WMResizeWidget(panel->sclearB, 70, 24);
759 WMMoveWidget(panel->sclearB, width - 155, 18);
760 WMSetButtonText(panel->sclearB, _("Clear"));
761 WMSetButtonAction(panel->sclearB, sgrabClicked, panel);
763 WMMapSubwidgets(panel->shortF);
765 /* internal command */
767 panel->icommandL = WMCreateList(panel->optionsF);
768 WMResizeWidget(panel->icommandL, width, 80);
769 WMMoveWidget(panel->icommandL, 10, 20);
771 WMSetListAction(panel->icommandL, icommandLClicked, panel);
773 WMAddNotificationObserver(dataChanged, panel, WMListSelectionDidChangeNotification, panel->icommandL);
775 WMInsertListItem(panel->icommandL, 0, _("Arrange Icons"));
776 WMInsertListItem(panel->icommandL, 1, _("Hide All Windows Except For The Focused One"));
777 WMInsertListItem(panel->icommandL, 2, _("Show All Windows"));
779 WMInsertListItem(panel->icommandL, 3, _("Exit Window Maker"));
780 WMInsertListItem(panel->icommandL, 4, _("Exit X Session"));
781 WMInsertListItem(panel->icommandL, 5, _("Restart Window Maker"));
782 WMInsertListItem(panel->icommandL, 6, _("Start Another Window Manager : ("));
784 WMInsertListItem(panel->icommandL, 7, _("Save Current Session"));
785 WMInsertListItem(panel->icommandL, 8, _("Clear Saved Session"));
786 WMInsertListItem(panel->icommandL, 9, _("Refresh Screen"));
787 WMInsertListItem(panel->icommandL, 10, _("Open Info Panel"));
788 WMInsertListItem(panel->icommandL, 11, _("Open Copyright Panel"));
790 panel->paramF = WMCreateFrame(panel->optionsF);
791 WMResizeWidget(panel->paramF, width, 50);
792 WMMoveWidget(panel->paramF, 10, 105);
793 WMSetFrameTitle(panel->paramF, _("Window Manager to Start"));
795 panel->paramT = WMCreateTextField(panel->paramF);
796 WMResizeWidget(panel->paramT, width - 20, 20);
797 WMMoveWidget(panel->paramT, 10, 20);
799 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->paramT);
801 WMMapSubwidgets(panel->paramF);
803 panel->quickB = WMCreateSwitchButton(panel->optionsF);
804 WMResizeWidget(panel->quickB, width, 20);
805 WMMoveWidget(panel->quickB, 10, 120);
806 WMSetButtonText(panel->quickB, _("Do not confirm action."));
807 WMSetButtonAction(panel->quickB, buttonClicked, panel);
809 label = WMCreateLabel(panel->optionsF);
810 WMResizeWidget(label, width + 5, FRAME_HEIGHT - 50);
811 WMMoveWidget(label, 7, 20);
812 WMSetLabelText(label,
813 _("Instructions:\n\n"
814 " - drag items from the left to the menu to add new items\n"
815 " - drag items out of the menu to remove items\n"
816 " - drag items in menu to change their position\n"
817 " - drag items with Control pressed to copy them\n"
818 " - double click in a menu item to change the label\n"
819 " - click on a menu item to change related information"));
820 WMMapWidget(label);
822 WMRealizeWidget(panel->box);
823 WMMapSubwidgets(panel->box);
824 WMMapWidget(panel->box);
827 int i;
828 for (i = 0; i < 3; i++)
829 WMUnmapWidget(panel->itemPad[i]);
831 changedItemPad(panel->typeP, panel);
833 panel->sections[NoInfo][0] = label;
835 panel->sections[ExecInfo][0] = panel->commandF;
836 panel->sections[ExecInfo][1] = panel->shortF;
838 panel->sections[CommandInfo][0] = panel->icommandL;
839 panel->sections[CommandInfo][1] = panel->shortF;
841 panel->sections[ExternalInfo][0] = panel->pathF;
843 panel->sections[PipeInfo][0] = panel->pipeF;
845 panel->sections[DirectoryInfo][0] = panel->dpathF;
846 panel->sections[DirectoryInfo][1] = panel->dcommandF;
848 panel->currentType = NoInfo;
850 showData(panel);
853 WMPoint pos;
855 pos = WMGetViewScreenPosition(WMWidgetView(panel->box));
857 if (pos.x < 200) {
858 pos.x += FRAME_WIDTH + 20;
859 } else {
860 pos.x = 10;
863 pos.y = WMAX(pos.y - 100, 0);
865 if (panel->menu)
866 WEditMenuShowAt(panel->menu, pos.x, pos.y);
870 static void freeItemData(ItemData * data)
872 #define CFREE(d) if (d) wfree(d)
874 /* TODO */
875 switch (data->type) {
876 case CommandInfo:
877 CFREE(data->param.command.parameter);
878 CFREE(data->param.command.shortcut);
879 break;
881 case ExecInfo:
882 CFREE(data->param.exec.command);
883 CFREE(data->param.exec.shortcut);
884 break;
886 case PipeInfo:
887 CFREE(data->param.pipe.command);
888 break;
890 case ExternalInfo:
891 CFREE(data->param.external.path);
892 break;
894 case DirectoryInfo:
895 CFREE(data->param.directory.command);
896 CFREE(data->param.directory.directory);
897 break;
899 default:
900 break;
903 wfree(data);
904 #undef CFREE
907 static ItemData *parseCommand(WMPropList * item)
909 ItemData *data = NEW(ItemData);
910 WMPropList *p;
911 char *command = NULL;
912 char *parameter = NULL;
913 char *shortcut = NULL;
914 int i = 1;
916 p = WMGetFromPLArray(item, i++);
917 command = WMGetFromPLString(p);
918 if (strcmp(command, "SHORTCUT") == 0) {
919 p = WMGetFromPLArray(item, i++);
920 shortcut = WMGetFromPLString(p);
921 p = WMGetFromPLArray(item, i++);
922 command = WMGetFromPLString(p);
924 p = WMGetFromPLArray(item, i++);
925 if (p)
926 parameter = WMGetFromPLString(p);
928 if (strcmp(command, "EXEC") == 0 || strcmp(command, "SHEXEC") == 0) {
930 data->type = ExecInfo;
932 data->param.exec.command = wstrdup(parameter);
933 if (shortcut)
934 data->param.exec.shortcut = wstrdup(shortcut);
936 } else if (strcmp(command, "OPEN_MENU") == 0) {
937 char *p;
939 * dir menu, menu file
940 * dir WITH
941 * |pipe
943 p = parameter;
944 while (isspace(*p) && *p)
945 p++;
946 if (*p == '|') {
947 if (*(p + 1) == '|') {
948 p++;
949 data->param.pipe.cached = 0;
950 } else {
951 data->param.pipe.cached = 1;
953 data->type = PipeInfo;
954 data->param.pipe.command = wtrimspace(p + 1);
955 } else {
956 char *s;
958 p = wstrdup(p);
960 s = strstr(p, "WITH");
961 if (s) {
962 char **tokens;
963 char **ctokens;
964 int tokn;
965 int i, j;
967 data->type = DirectoryInfo;
969 *s = '\0';
970 s += 5;
971 while (*s && isspace(*s))
972 s++;
973 data->param.directory.command = wstrdup(s);
975 wtokensplit(p, &tokens, &tokn);
976 wfree(p);
978 ctokens = wmalloc(sizeof(char *) * tokn);
980 for (i = 0, j = 0; i < tokn; i++) {
981 if (strcmp(tokens[i], "-noext") == 0) {
982 data->param.directory.stripExt = 1;
983 } else {
984 ctokens[j++] = tokens[i];
987 data->param.directory.directory = wtokenjoin(ctokens, j);
988 wfree(ctokens);
990 wtokenfree(tokens, tokn);
991 } else {
992 data->type = ExternalInfo;
993 data->param.external.path = p;
996 } else if (strcmp(command, "WORKSPACE_MENU") == 0) {
997 data->type = WSMenuInfo;
998 } else if (strcmp(command, "WINDOWS_MENU") == 0) {
999 data->type = WWindowListInfo;
1000 } else {
1001 int cmd;
1003 if (strcmp(command, "ARRANGE_ICONS") == 0) {
1004 cmd = 0;
1005 } else if (strcmp(command, "HIDE_OTHERS") == 0) {
1006 cmd = 1;
1007 } else if (strcmp(command, "SHOW_ALL") == 0) {
1008 cmd = 2;
1009 } else if (strcmp(command, "EXIT") == 0) {
1010 cmd = 3;
1011 } else if (strcmp(command, "SHUTDOWN") == 0) {
1012 cmd = 4;
1013 } else if (strcmp(command, "RESTART") == 0) {
1014 if (parameter) {
1015 cmd = 6;
1016 } else {
1017 cmd = 5;
1019 } else if (strcmp(command, "SAVE_SESSION") == 0) {
1020 cmd = 7;
1021 } else if (strcmp(command, "CLEAR_SESSION") == 0) {
1022 cmd = 8;
1023 } else if (strcmp(command, "REFRESH") == 0) {
1024 cmd = 9;
1025 } else if (strcmp(command, "INFO_PANEL") == 0) {
1026 cmd = 10;
1027 } else if (strcmp(command, "LEGAL_PANEL") == 0) {
1028 cmd = 11;
1029 } else {
1030 wwarning(_("unknown command '%s' in menu"), command);
1031 wfree(data);
1032 return NULL;
1035 data->type = CommandInfo;
1037 data->param.command.command = cmd;
1038 if (shortcut)
1039 data->param.command.shortcut = wstrdup(shortcut);
1040 if (parameter)
1041 data->param.command.parameter = wstrdup(parameter);
1044 return data;
1047 static void updateFrameTitle(_Panel * panel, char *title, InfoType type)
1049 if (type != NoInfo) {
1050 char *tmp;
1052 switch (type) {
1053 case ExecInfo:
1054 tmp = wstrconcat(title, _(": Execute Program"));
1055 break;
1057 case CommandInfo:
1058 tmp = wstrconcat(title, _(": Perform Internal Command"));
1059 break;
1061 case ExternalInfo:
1062 tmp = wstrconcat(title, _(": Open a Submenu"));
1063 break;
1065 case PipeInfo:
1066 tmp = wstrconcat(title, _(": Program Generated Submenu"));
1067 break;
1069 case DirectoryInfo:
1070 tmp = wstrconcat(title, _(": Directory Contents Menu"));
1071 break;
1073 case WSMenuInfo:
1074 tmp = wstrconcat(title, _(": Open Workspaces Submenu"));
1075 break;
1077 case WWindowListInfo:
1078 tmp = wstrconcat(title, _(": Open Window List Submenu"));
1079 break;
1081 default:
1082 tmp = NULL;
1083 break;
1085 WMSetFrameTitle(panel->optionsF, tmp);
1086 wfree(tmp);
1087 } else {
1088 WMSetFrameTitle(panel->optionsF, NULL);
1092 static void changeInfoType(_Panel * panel, char *title, InfoType type)
1094 WMWidget **w;
1096 if (panel->currentType != type) {
1098 w = panel->sections[panel->currentType];
1100 while (*w) {
1101 WMUnmapWidget(*w);
1102 w++;
1104 WMUnmapWidget(panel->paramF);
1105 WMUnmapWidget(panel->quickB);
1107 w = panel->sections[type];
1109 while (*w) {
1110 WMMapWidget(*w);
1111 w++;
1115 updateFrameTitle(panel, title, type);
1117 panel->currentType = type;
1120 static void updateMenuItem(_Panel * panel, WEditMenuItem * item, WMWidget * changedWidget)
1122 ItemData *data = WGetEditMenuItemData(item);
1124 assert(data != NULL);
1126 #define REPLACE(v, d) if (v) wfree(v); v = d
1128 switch (data->type) {
1129 case ExecInfo:
1130 if (changedWidget == panel->commandT) {
1131 REPLACE(data->param.exec.command, WMGetTextFieldText(panel->commandT));
1133 if (changedWidget == panel->shortT) {
1134 REPLACE(data->param.exec.shortcut, WMGetTextFieldText(panel->shortT));
1136 break;
1138 case CommandInfo:
1139 if (changedWidget == panel->icommandL) {
1140 data->param.command.command = WMGetListSelectedItemRow(panel->icommandL);
1142 switch (data->param.command.command) {
1143 case 3:
1144 case 4:
1145 if (changedWidget == panel->quickB) {
1146 REPLACE(data->param.command.parameter, WMGetButtonSelected(panel->quickB)
1147 ? wstrdup("QUICK") : NULL);
1149 break;
1151 case 6:
1152 if (changedWidget == panel->paramT) {
1153 REPLACE(data->param.command.parameter, WMGetTextFieldText(panel->paramT));
1155 break;
1157 if (changedWidget == panel->shortT) {
1158 REPLACE(data->param.command.shortcut, WMGetTextFieldText(panel->shortT));
1161 break;
1163 case PipeInfo:
1164 if (changedWidget == panel->pipeT) {
1165 REPLACE(data->param.pipe.command, WMGetTextFieldText(panel->pipeT));
1167 if (changedWidget == panel->pipeCacheB) {
1168 data->param.pipe.cached = WMGetButtonSelected(panel->pipeCacheB);
1170 break;
1172 case ExternalInfo:
1173 if (changedWidget == panel->pathT) {
1174 REPLACE(data->param.external.path, WMGetTextFieldText(panel->pathT));
1176 break;
1178 case DirectoryInfo:
1179 if (changedWidget == panel->dpathT) {
1180 REPLACE(data->param.directory.directory, WMGetTextFieldText(panel->dpathT));
1182 if (changedWidget == panel->dcommandT) {
1183 REPLACE(data->param.directory.command, WMGetTextFieldText(panel->dcommandT));
1185 if (changedWidget == panel->dstripB) {
1186 data->param.directory.stripExt = WMGetButtonSelected(panel->dstripB);
1188 break;
1190 default:
1191 assert(0);
1192 break;
1195 #undef REPLACE
1198 static void
1199 menuItemCloned(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * origItem, WEditMenuItem * newItem)
1201 ItemData *data = WGetEditMenuItemData(origItem);
1202 ItemData *newData;
1204 if (!data)
1205 return;
1207 #define DUP(s) (s) ? wstrdup(s) : NULL
1209 newData = NEW(ItemData);
1211 newData->type = data->type;
1213 switch (data->type) {
1214 case ExecInfo:
1215 newData->param.exec.command = DUP(data->param.exec.command);
1216 newData->param.exec.shortcut = DUP(data->param.exec.shortcut);
1217 break;
1219 case CommandInfo:
1220 newData->param.command.command = data->param.command.command;
1221 newData->param.command.parameter = DUP(data->param.command.parameter);
1222 newData->param.command.shortcut = DUP(data->param.command.shortcut);
1223 break;
1225 case PipeInfo:
1226 newData->param.pipe.command = DUP(data->param.pipe.command);
1227 newData->param.pipe.cached = data->param.pipe.cached;
1228 break;
1230 case ExternalInfo:
1231 newData->param.external.path = DUP(data->param.external.path);
1232 break;
1234 case DirectoryInfo:
1235 newData->param.directory.directory = DUP(data->param.directory.directory);
1236 newData->param.directory.command = DUP(data->param.directory.command);
1237 newData->param.directory.stripExt = data->param.directory.stripExt;
1238 break;
1240 default:
1241 break;
1244 #undef DUP
1246 WSetEditMenuItemData(newItem, newData, (WMCallback *) freeItemData);
1249 static void menuItemEdited(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item)
1251 _Panel *panel = (_Panel *) delegate->data;
1252 WEditMenu *submenu;
1254 updateFrameTitle(panel, WGetEditMenuItemTitle(item), panel->currentType);
1256 submenu = WGetEditMenuSubmenu(menu, item);
1257 if (submenu) {
1258 WSetEditMenuTitle(submenu, WGetEditMenuItemTitle(item));
1262 static Bool shouldRemoveItem(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item)
1264 _Panel *panel = (_Panel *) delegate->data;
1266 if (panel->dontAsk)
1267 return True;
1269 if (WGetEditMenuSubmenu(menu, item)) {
1270 int res;
1272 res = WMRunAlertPanel(WMWidgetScreen(menu), NULL,
1273 _("Remove Submenu"),
1274 _("Removing this item will destroy all items inside\n"
1275 "the submenu. Do you really want to do that?"),
1276 _("Yes"), _("No"), _("Yes, don't ask again."));
1277 switch (res) {
1278 case WAPRDefault:
1279 return True;
1280 case WAPRAlternate:
1281 return False;
1282 case WAPROther:
1283 panel->dontAsk = True;
1284 return True;
1287 return True;
1290 static void menuItemDeselected(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * item)
1292 _Panel *panel = (_Panel *) delegate->data;
1294 changeInfoType(panel, NULL, NoInfo);
1297 static void menuItemSelected(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * item)
1299 ItemData *data = WGetEditMenuItemData(item);
1300 _Panel *panel = (_Panel *) delegate->data;
1302 panel->currentItem = item;
1304 if (data) {
1305 changeInfoType(panel, WGetEditMenuItemTitle(item), data->type);
1307 switch (data->type) {
1308 case NoInfo:
1309 break;
1311 case ExecInfo:
1312 WMSetTextFieldText(panel->commandT, data->param.exec.command);
1313 WMSetTextFieldText(panel->shortT, data->param.exec.shortcut);
1314 break;
1316 case CommandInfo:
1317 WMSelectListItem(panel->icommandL, data->param.command.command);
1318 WMSetListPosition(panel->icommandL, data->param.command.command - 2);
1319 WMSetTextFieldText(panel->shortT, data->param.command.shortcut);
1321 switch (data->param.command.command) {
1322 case 3:
1323 case 4:
1324 WMSetButtonSelected(panel->quickB, data->param.command.parameter != NULL);
1325 break;
1326 case 6:
1327 WMSetTextFieldText(panel->paramT, data->param.command.parameter);
1328 break;
1331 icommandLClicked(panel->icommandL, panel);
1332 break;
1334 case PipeInfo:
1335 WMSetTextFieldText(panel->pipeT, data->param.pipe.command);
1336 WMSetButtonSelected(panel->pipeCacheB, data->param.pipe.cached);
1337 break;
1339 case ExternalInfo:
1340 WMSetTextFieldText(panel->pathT, data->param.external.path);
1341 break;
1343 case DirectoryInfo:
1344 WMSetTextFieldText(panel->dpathT, data->param.directory.directory);
1345 WMSetTextFieldText(panel->dcommandT, data->param.directory.command);
1346 WMSetButtonSelected(panel->dstripB, data->param.directory.stripExt);
1347 break;
1349 case WSMenuInfo:
1350 break;
1352 default:
1353 break;
1358 static WEditMenu *buildSubmenu(_Panel * panel, WMPropList * pl)
1360 WMScreen *scr = WMWidgetScreen(panel->parent);
1361 WEditMenu *menu;
1362 WEditMenuItem *item;
1363 char *title;
1364 WMPropList *tp, *bp;
1365 int i;
1367 tp = WMGetFromPLArray(pl, 0);
1368 title = WMGetFromPLString(tp);
1370 menu = WCreateEditMenu(scr, title);
1372 for (i = 1; i < WMGetPropListItemCount(pl); i++) {
1373 WMPropList *pi;
1375 pi = WMGetFromPLArray(pl, i);
1377 tp = WMGetFromPLArray(pi, 0);
1378 bp = WMGetFromPLArray(pi, 1);
1380 title = WMGetFromPLString(tp);
1382 if (!bp || WMIsPLArray(bp)) { /* it's a submenu */
1383 WEditMenu *submenu;
1385 submenu = buildSubmenu(panel, pi);
1387 item = WAddMenuItemWithTitle(menu, title);
1389 WSetEditMenuSubmenu(menu, item, submenu);
1390 } else {
1391 ItemData *data;
1393 data = parseCommand(pi);
1395 if (data != NULL) {
1396 item = WAddMenuItemWithTitle(menu, title);
1397 if (panel->markerPix[data->type])
1398 WSetEditMenuItemImage(item, panel->markerPix[data->type]);
1399 WSetEditMenuItemData(item, data, (WMCallback *) freeItemData);
1400 } else {
1401 char *buf = wmalloc(1024);
1402 snprintf(buf, 1024, _("Invalid menu command \"%s\" with label \"%s\" cleared"),
1403 WMGetFromPLString(WMGetFromPLArray(pi, 1)),
1404 WMGetFromPLString(WMGetFromPLArray(pi, 0)));
1405 WMRunAlertPanel(scr, panel->parent, _("Warning"), buf, _("OK"), NULL, NULL);
1406 wfree(buf);
1412 WSetEditMenuAcceptsDrop(menu, True);
1413 WSetEditMenuDelegate(menu, &menuDelegate);
1415 WMRealizeWidget(menu);
1417 return menu;
1420 static void buildMenuFromPL(_Panel * panel, WMPropList * pl)
1422 panel->menu = buildSubmenu(panel, pl);
1425 static WMPropList *getDefaultMenu(_Panel * panel)
1427 WMPropList *menu;
1428 char *menuPath, *gspath;
1430 gspath = wstrdup(WMAKER_RESOURCE_PATH);
1432 menuPath = wmalloc(strlen(gspath) + 128);
1433 sprintf(menuPath, "%s/plmenu", gspath);
1435 menu = WMReadPropListFromFile(menuPath);
1437 if (!menu) {
1438 char *buffer, *msg;
1440 msg = _("Could not open default menu from '%s'");
1441 buffer = wmalloc(strlen(msg) + strlen(menuPath) + 10);
1442 sprintf(buffer, msg, menuPath);
1443 WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
1444 _("Error"), buffer, _("OK"), NULL, NULL);
1445 wfree(buffer);
1448 wfree(gspath);
1449 wfree(menuPath);
1451 return menu;
1454 static void showData(_Panel * panel)
1456 char *gspath;
1457 char *menuPath;
1458 WMPropList *pmenu;
1460 gspath = wusergnusteppath();
1462 menuPath = wmalloc(strlen(gspath) + 32);
1463 strcpy(menuPath, gspath);
1464 strcat(menuPath, "/Defaults/WMRootMenu");
1466 pmenu = WMReadPropListFromFile(menuPath);
1468 if (!pmenu || !WMIsPLArray(pmenu)) {
1469 int res;
1471 res = WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
1472 _("Warning"),
1473 _("The menu file format currently in use is not supported\n"
1474 "by this tool. Do you want to discard the current menu\n"
1475 "to use this tool?"),
1476 _("Yes, Discard and Update"), _("No, Keep Current Menu"), NULL);
1478 if (res == WAPRDefault) {
1479 pmenu = getDefaultMenu(panel);
1481 if (!pmenu) {
1482 pmenu = WMCreatePLArray(WMCreatePLString("Applications"), NULL);
1484 } else {
1485 panel->dontSave = True;
1486 return;
1490 panel->menuPath = menuPath;
1492 buildMenuFromPL(panel, pmenu);
1494 WMReleasePropList(pmenu);
1497 static Bool notblank(char *s)
1499 if (s) {
1500 while (*s++) {
1501 if (!isspace(*s))
1502 return True;
1505 return False;
1508 static WMPropList *processData(char *title, ItemData * data)
1510 WMPropList *item;
1511 char *s1;
1512 static WMPropList *pscut = NULL;
1513 static WMPropList *pomenu = NULL;
1514 int i;
1516 if (data == NULL)
1517 return NULL;
1519 if (!pscut) {
1520 pscut = WMCreatePLString("SHORTCUT");
1521 pomenu = WMCreatePLString("OPEN_MENU");
1524 item = WMCreatePLArray(WMCreatePLString(title), NULL);
1526 switch (data->type) {
1527 case ExecInfo:
1528 if (data->param.exec.command == NULL)
1529 return NULL;
1530 #if 1
1531 if (strpbrk(data->param.exec.command, "&$*|><?`=;")) {
1532 s1 = "SHEXEC";
1533 } else {
1534 s1 = "EXEC";
1536 #else
1537 s1 = "SHEXEC";
1538 #endif
1540 if (notblank(data->param.exec.shortcut)) {
1541 WMAddToPLArray(item, pscut);
1542 WMAddToPLArray(item, WMCreatePLString(data->param.exec.shortcut));
1545 WMAddToPLArray(item, WMCreatePLString(s1));
1546 WMAddToPLArray(item, WMCreatePLString(data->param.exec.command));
1547 break;
1549 case CommandInfo:
1550 if (notblank(data->param.command.shortcut)) {
1551 WMAddToPLArray(item, pscut);
1552 WMAddToPLArray(item, WMCreatePLString(data->param.command.shortcut));
1555 i = data->param.command.command;
1557 WMAddToPLArray(item, WMCreatePLString(commandNames[i]));
1559 switch (i) {
1560 case 3:
1561 case 4:
1562 if (data->param.command.parameter) {
1563 WMAddToPLArray(item, WMCreatePLString(data->param.command.parameter));
1565 break;
1567 case 6: /* restart */
1568 if (data->param.command.parameter) {
1569 WMAddToPLArray(item, WMCreatePLString(data->param.command.parameter));
1571 break;
1574 break;
1576 case PipeInfo:
1577 if (!data->param.pipe.command)
1578 return NULL;
1579 WMAddToPLArray(item, pomenu);
1580 if (data->param.pipe.cached)
1581 s1 = wstrconcat("| ", data->param.pipe.command);
1582 else
1583 s1 = wstrconcat("|| ", data->param.pipe.command);
1584 WMAddToPLArray(item, WMCreatePLString(s1));
1585 wfree(s1);
1586 break;
1588 case ExternalInfo:
1589 if (!data->param.external.path)
1590 return NULL;
1591 WMAddToPLArray(item, pomenu);
1592 WMAddToPLArray(item, WMCreatePLString(data->param.external.path));
1593 break;
1595 case DirectoryInfo:
1596 if (!data->param.directory.directory || !data->param.directory.command)
1597 return NULL;
1599 int l;
1600 char *tmp;
1602 l = strlen(data->param.directory.directory);
1603 l += strlen(data->param.directory.command);
1604 l += 32;
1606 WMAddToPLArray(item, pomenu);
1608 tmp = wmalloc(l);
1609 sprintf(tmp, "%s%s WITH %s",
1610 data->param.directory.stripExt ? "-noext " : "",
1611 data->param.directory.directory, data->param.directory.command);
1613 WMAddToPLArray(item, WMCreatePLString(tmp));
1614 wfree(tmp);
1616 break;
1618 case WSMenuInfo:
1619 WMAddToPLArray(item, WMCreatePLString("WORKSPACE_MENU"));
1620 break;
1622 case WWindowListInfo:
1623 WMAddToPLArray(item, WMCreatePLString("WINDOWS_MENU"));
1624 break;
1626 default:
1627 assert(0);
1628 break;
1631 return item;
1634 static WMPropList *processSubmenu(WEditMenu * menu)
1636 WEditMenuItem *item;
1637 WMPropList *pmenu;
1638 WMPropList *pl;
1639 char *s;
1640 int i;
1642 s = WGetEditMenuTitle(menu);
1643 pl = WMCreatePLString(s);
1645 pmenu = WMCreatePLArray(pl, NULL);
1647 i = 0;
1648 while ((item = WGetEditMenuItem(menu, i++))) {
1649 WEditMenu *submenu;
1651 s = WGetEditMenuItemTitle(item);
1653 submenu = WGetEditMenuSubmenu(menu, item);
1654 if (submenu) {
1655 pl = processSubmenu(submenu);
1656 } else {
1657 pl = processData(s, WGetEditMenuItemData(item));
1660 if (!pl)
1661 continue;
1663 WMAddToPLArray(pmenu, pl);
1666 return pmenu;
1669 static WMPropList *buildPLFromMenu(_Panel * panel)
1671 WMPropList *menu;
1673 menu = processSubmenu(panel->menu);
1675 return menu;
1678 static void storeData(_Panel * panel)
1680 WMPropList *menu;
1682 if (panel->dontSave)
1683 return;
1685 menu = buildPLFromMenu(panel);
1687 WMWritePropListToFile(menu, panel->menuPath);
1689 WMReleasePropList(menu);
1692 static void showMenus(_Panel * panel)
1694 if (panel->menu)
1695 WEditMenuUnhide(panel->menu);
1698 static void hideMenus(_Panel * panel)
1700 if (panel->menu)
1701 WEditMenuHide(panel->menu);
1704 Panel *InitMenu(WMScreen * scr, WMWidget * parent)
1706 _Panel *panel;
1708 panel = wmalloc(sizeof(_Panel));
1709 memset(panel, 0, sizeof(_Panel));
1711 panel->sectionName = _("Applications Menu Definition");
1713 panel->description = _("Edit the menu for launching applications.");
1715 panel->parent = parent;
1717 panel->callbacks.createWidgets = createPanel;
1718 panel->callbacks.updateDomain = storeData;
1719 panel->callbacks.showPanel = showMenus;
1720 panel->callbacks.hidePanel = hideMenus;
1722 AddSection(panel, ICON_FILE);
1724 return panel;