Show app name in Kill Application dialog
[wmaker-crm.git] / WPrefs.app / Menu.c
blobdd7c30f7d43bb704d6f26a70a4597448a5cc7ff4
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "WPrefs.h"
24 #include <assert.h>
25 #include <ctype.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 DirectoryInfo,
39 WSMenuInfo,
40 WWindowListInfo,
41 LastInfo
42 } InfoType;
44 #define MAX_SECTION_SIZE 4
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;
470 WEditMenu *smenu;
471 ItemData *data;
473 pad = makeFactoryMenu(panel->box, 150);
474 WMMoveWidget(pad, 10, 40);
476 data = putNewItem(panel, pad, ExecInfo, _("Run Program"));
477 data = putNewItem(panel, pad, CommandInfo, _("Internal Command"));
478 smenu = putNewSubmenu(pad, _("Submenu"));
479 data = putNewItem(panel, pad, ExternalInfo, _("External Submenu"));
480 data = putNewItem(panel, pad, PipeInfo, _("Generated Submenu"));
481 data = putNewItem(panel, pad, DirectoryInfo, _("Directory Contents"));
482 data = putNewItem(panel, pad, WSMenuInfo, _("Workspace Menu"));
483 data = putNewItem(panel, pad, WWindowListInfo, _("Window List Menu"));
485 panel->itemPad[0] = pad;
489 WEditMenu *pad;
490 ItemData *data;
491 WMScrollView *sview;
493 sview = WMCreateScrollView(panel->box);
494 WMResizeWidget(sview, 150, 180);
495 WMMoveWidget(sview, 10, 40);
496 WMSetScrollViewHasVerticalScroller(sview, True);
498 pad = makeFactoryMenu(panel->box, 130);
500 WMSetScrollViewContentView(sview, WMWidgetView(pad));
502 data = putNewItem(panel, pad, ExecInfo, _("XTerm"));
503 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white";
505 data = putNewItem(panel, pad, ExecInfo, _("rxvt"));
506 data->param.exec.command = "rxvt";
508 data = putNewItem(panel, pad, ExecInfo, _("ETerm"));
509 data->param.exec.command = "eterm";
511 data = putNewItem(panel, pad, ExecInfo, _("Run..."));
512 data->param.exec.command = _("%a(Run,Type command to run)");
514 data = putNewItem(panel, pad, ExecInfo, _("Netscape"));
515 data->param.exec.command = "netscape";
517 data = putNewItem(panel, pad, ExecInfo, _("gimp"));
518 data->param.exec.command = "gimp";
520 data = putNewItem(panel, pad, ExecInfo, _("epic"));
521 data->param.exec.command = "xterm -e epic";
523 data = putNewItem(panel, pad, ExecInfo, _("ee"));
524 data->param.exec.command = "ee";
526 data = putNewItem(panel, pad, ExecInfo, _("xv"));
527 data->param.exec.command = "xv";
529 data = putNewItem(panel, pad, ExecInfo, _("Acrobat Reader"));
530 data->param.exec.command = "acroread || /usr/local/Acrobat4/bin/acroread";
532 data = putNewItem(panel, pad, ExecInfo, _("ghostview"));
533 data->param.exec.command = "gv";
535 data = putNewItem(panel, pad, CommandInfo, _("Exit Window Maker"));
536 data->param.command.command = 3;
538 WMMapWidget(pad);
540 panel->itemPad[1] = sview;
544 WEditMenu *pad, *smenu;
545 ItemData *data;
546 WMScrollView *sview;
548 sview = WMCreateScrollView(panel->box);
549 WMResizeWidget(sview, 150, 180);
550 WMMoveWidget(sview, 10, 40);
551 WMSetScrollViewHasVerticalScroller(sview, True);
553 pad = makeFactoryMenu(panel->box, 130);
555 WMSetScrollViewContentView(sview, WMWidgetView(pad));
557 data = putNewItem(panel, pad, ExternalInfo, _("Debian Menu"));
558 data->param.pipe.command = "/etc/X11/WindowMaker/menu.hook";
560 data = putNewItem(panel, pad, PipeInfo, _("RedHat Menu"));
561 data->param.pipe.command = "wmconfig --output wmaker";
563 data = putNewItem(panel, pad, PipeInfo, _("Menu Conectiva"));
564 data->param.pipe.command = "wmconfig --output wmaker";
566 data = putNewItem(panel, pad, DirectoryInfo, _("Themes"));
567 data->param.directory.command = "setstyle";
568 data->param.directory.directory =
569 "/usr/share/WindowMaker/Themes /usr/local/share/WindowMaker/Themes $HOME/GNUstep/Library/WindowMaker/Themes";
570 data->param.directory.stripExt = 1;
572 data = putNewItem(panel, pad, DirectoryInfo, _("Bg Images (scale)"));
573 data->param.directory.command = "wmsetbg -u -s";
574 data->param.directory.directory =
575 "/opt/kde2/share/wallpapers /usr/share/WindowMaker/Backgrounds $HOME/GNUstep/Library/WindowMaker/Backgrounds";
576 data->param.directory.stripExt = 1;
578 data = putNewItem(panel, pad, DirectoryInfo, _("Bg Images (tile)"));
579 data->param.directory.command = "wmsetbg -u -t";
580 data->param.directory.directory =
581 "/opt/kde2/share/wallpapers /usr/share/WindowMaker/Backgrounds $HOME/GNUstep/Library/WindowMaker/Backgrounds";
582 data->param.directory.stripExt = 1;
584 smenu = putNewSubmenu(pad, _("Assorted XTerms"));
586 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Yellow on Blue"));
587 data->param.exec.command = "xterm -sb -sl 2000 -bg midnightblue -fg yellow";
589 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Black"));
590 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white";
592 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Black on White"));
593 data->param.exec.command = "xterm -sb -sl 2000 -bg white -fg black";
595 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Black on Beige"));
596 data->param.exec.command = "xterm -sb -sl 2000 -bg '#bbbb99' -fg black";
598 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Green"));
599 data->param.exec.command = "xterm -sb -sl 2000 -bg '#228822' -fg white";
601 data = putNewItem(panel, smenu, ExecInfo, _("XTerm White on Olive"));
602 data->param.exec.command = "xterm -sb -sl 2000 -bg '#335533' -fg white";
604 data = putNewItem(panel, smenu, ExecInfo, _("XTerm Blue on Blue"));
605 data->param.exec.command = "xterm -sb -sl 2000 -bg '#112244' -fg '#88aabb'";
607 data = putNewItem(panel, smenu, ExecInfo, _("XTerm BIG FONTS"));
608 data->param.exec.command = "xterm -sb -sl 2000 -bg black -fg white -fn 10x20";
610 WMMapWidget(pad);
612 panel->itemPad[2] = sview;
615 width = FRAME_WIDTH - 20 - 150 - 10 - 2;
617 panel->optionsF = WMCreateFrame(panel->box);
618 WMResizeWidget(panel->optionsF, width, FRAME_HEIGHT - 15);
619 WMMoveWidget(panel->optionsF, 10 + 150 + 10, 5);
621 width -= 20;
623 /* command */
625 panel->commandF = WMCreateFrame(panel->optionsF);
626 WMResizeWidget(panel->commandF, width, 50);
627 WMMoveWidget(panel->commandF, 10, 20);
628 WMSetFrameTitle(panel->commandF, _("Program to Run"));
629 WMSetFrameTitlePosition(panel->commandF, WTPAtTop);
631 panel->commandT = WMCreateTextField(panel->commandF);
632 WMResizeWidget(panel->commandT, width - 95, 20);
633 WMMoveWidget(panel->commandT, 10, 20);
635 panel->browseB = WMCreateCommandButton(panel->commandF);
636 WMResizeWidget(panel->browseB, 70, 24);
637 WMMoveWidget(panel->browseB, width - 80, 18);
638 WMSetButtonText(panel->browseB, _("Browse"));
639 WMSetButtonAction(panel->browseB, browseForFile, panel);
641 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->commandT);
643 #if 0
644 panel->xtermC = WMCreateSwitchButton(panel->commandF);
645 WMResizeWidget(panel->xtermC, width - 20, 20);
646 WMMoveWidget(panel->xtermC, 10, 50);
647 WMSetButtonText(panel->xtermC, _("Run the program inside a Xterm"));
648 #endif
649 WMMapSubwidgets(panel->commandF);
651 /* path */
653 panel->pathF = WMCreateFrame(panel->optionsF);
654 WMResizeWidget(panel->pathF, width, 150);
655 WMMoveWidget(panel->pathF, 10, 40);
656 WMSetFrameTitle(panel->pathF, _("Path for Menu"));
658 panel->pathT = WMCreateTextField(panel->pathF);
659 WMResizeWidget(panel->pathT, width - 20, 20);
660 WMMoveWidget(panel->pathT, 10, 20);
662 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->pathT);
664 label = WMCreateLabel(panel->pathF);
665 WMResizeWidget(label, width - 20, 80);
666 WMMoveWidget(label, 10, 50);
667 WMSetLabelText(label, _("Enter the path for a file containing a menu\n"
668 "or a list of directories with the programs you\n"
669 "want to have listed in the menu. Ex:\n"
670 "~/GNUstep/Library/WindowMaker/menu\n" "or\n" "/usr/X11R6/bin ~/xbin"));
672 WMMapSubwidgets(panel->pathF);
674 /* pipe */
676 panel->pipeF = WMCreateFrame(panel->optionsF);
677 WMResizeWidget(panel->pipeF, width, 155);
678 WMMoveWidget(panel->pipeF, 10, 30);
679 WMSetFrameTitle(panel->pipeF, _("Command"));
681 panel->pipeT = WMCreateTextField(panel->pipeF);
682 WMResizeWidget(panel->pipeT, width - 20, 20);
683 WMMoveWidget(panel->pipeT, 10, 20);
685 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->pipeT);
687 label = WMCreateLabel(panel->pipeF);
688 WMResizeWidget(label, width - 20, 40);
689 WMMoveWidget(label, 10, 50);
690 WMSetLabelText(label, _("Enter a command that outputs a menu\n" "definition to stdout when invoked."));
692 panel->pipeCacheB = WMCreateSwitchButton(panel->pipeF);
693 WMResizeWidget(panel->pipeCacheB, width - 20, 40);
694 WMMoveWidget(panel->pipeCacheB, 10, 110);
695 WMSetButtonText(panel->pipeCacheB, _("Cache menu contents after opening for\n" "the first time"));
697 WMMapSubwidgets(panel->pipeF);
699 /* directory menu */
701 panel->dcommandF = WMCreateFrame(panel->optionsF);
702 WMResizeWidget(panel->dcommandF, width, 90);
703 WMMoveWidget(panel->dcommandF, 10, 25);
704 WMSetFrameTitle(panel->dcommandF, _("Command to Open Files"));
706 panel->dcommandT = WMCreateTextField(panel->dcommandF);
707 WMResizeWidget(panel->dcommandT, width - 20, 20);
708 WMMoveWidget(panel->dcommandT, 10, 20);
710 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->dcommandT);
712 label = WMCreateLabel(panel->dcommandF);
713 WMResizeWidget(label, width - 20, 45);
714 WMMoveWidget(label, 10, 40);
715 WMSetLabelText(label, _("Enter the command you want to use to open the\n"
716 "files in the directories listed below."));
718 WMMapSubwidgets(panel->dcommandF);
720 panel->dpathF = WMCreateFrame(panel->optionsF);
721 WMResizeWidget(panel->dpathF, width, 80);
722 WMMoveWidget(panel->dpathF, 10, 125);
723 WMSetFrameTitle(panel->dpathF, _("Directories with Files"));
725 panel->dpathT = WMCreateTextField(panel->dpathF);
726 WMResizeWidget(panel->dpathT, width - 20, 20);
727 WMMoveWidget(panel->dpathT, 10, 20);
729 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->dpathT);
731 panel->dstripB = WMCreateSwitchButton(panel->dpathF);
732 WMResizeWidget(panel->dstripB, width - 20, 20);
733 WMMoveWidget(panel->dstripB, 10, 50);
734 WMSetButtonText(panel->dstripB, _("Strip extensions from file names"));
736 WMSetButtonAction(panel->dstripB, buttonClicked, panel);
738 WMMapSubwidgets(panel->dpathF);
740 /* shortcut */
742 panel->shortF = WMCreateFrame(panel->optionsF);
743 WMResizeWidget(panel->shortF, width, 50);
744 WMMoveWidget(panel->shortF, 10, 160);
745 WMSetFrameTitle(panel->shortF, _("Keyboard Shortcut"));
747 panel->shortT = WMCreateTextField(panel->shortF);
748 WMResizeWidget(panel->shortT, width - 20 - 150, 20);
749 WMMoveWidget(panel->shortT, 10, 20);
751 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->shortT);
753 panel->sgrabB = WMCreateCommandButton(panel->shortF);
754 WMResizeWidget(panel->sgrabB, 70, 24);
755 WMMoveWidget(panel->sgrabB, width - 80, 18);
756 WMSetButtonText(panel->sgrabB, _("Capture"));
757 WMSetButtonAction(panel->sgrabB, sgrabClicked, panel);
759 panel->sclearB = WMCreateCommandButton(panel->shortF);
760 WMResizeWidget(panel->sclearB, 70, 24);
761 WMMoveWidget(panel->sclearB, width - 155, 18);
762 WMSetButtonText(panel->sclearB, _("Clear"));
763 WMSetButtonAction(panel->sclearB, sgrabClicked, panel);
765 WMMapSubwidgets(panel->shortF);
767 /* internal command */
769 panel->icommandL = WMCreateList(panel->optionsF);
770 WMResizeWidget(panel->icommandL, width, 80);
771 WMMoveWidget(panel->icommandL, 10, 20);
773 WMSetListAction(panel->icommandL, icommandLClicked, panel);
775 WMAddNotificationObserver(dataChanged, panel, WMListSelectionDidChangeNotification, panel->icommandL);
777 WMInsertListItem(panel->icommandL, 0, _("Arrange Icons"));
778 WMInsertListItem(panel->icommandL, 1, _("Hide All Windows Except For The Focused One"));
779 WMInsertListItem(panel->icommandL, 2, _("Show All Windows"));
781 WMInsertListItem(panel->icommandL, 3, _("Exit Window Maker"));
782 WMInsertListItem(panel->icommandL, 4, _("Exit X Session"));
783 WMInsertListItem(panel->icommandL, 5, _("Restart Window Maker"));
784 WMInsertListItem(panel->icommandL, 6, _("Start Another Window Manager : ("));
786 WMInsertListItem(panel->icommandL, 7, _("Save Current Session"));
787 WMInsertListItem(panel->icommandL, 8, _("Clear Saved Session"));
788 WMInsertListItem(panel->icommandL, 9, _("Refresh Screen"));
789 WMInsertListItem(panel->icommandL, 10, _("Open Info Panel"));
790 WMInsertListItem(panel->icommandL, 11, _("Open Copyright Panel"));
792 panel->paramF = WMCreateFrame(panel->optionsF);
793 WMResizeWidget(panel->paramF, width, 50);
794 WMMoveWidget(panel->paramF, 10, 105);
795 WMSetFrameTitle(panel->paramF, _("Window Manager to Start"));
797 panel->paramT = WMCreateTextField(panel->paramF);
798 WMResizeWidget(panel->paramT, width - 20, 20);
799 WMMoveWidget(panel->paramT, 10, 20);
801 WMAddNotificationObserver(dataChanged, panel, WMTextDidChangeNotification, panel->paramT);
803 WMMapSubwidgets(panel->paramF);
805 panel->quickB = WMCreateSwitchButton(panel->optionsF);
806 WMResizeWidget(panel->quickB, width, 20);
807 WMMoveWidget(panel->quickB, 10, 120);
808 WMSetButtonText(panel->quickB, _("Do not confirm action."));
809 WMSetButtonAction(panel->quickB, buttonClicked, panel);
811 label = WMCreateLabel(panel->optionsF);
812 WMResizeWidget(label, width + 5, FRAME_HEIGHT - 50);
813 WMMoveWidget(label, 7, 20);
814 WMSetLabelText(label,
815 _("Instructions:\n\n"
816 " - drag items from the left to the menu to add new items\n"
817 " - drag items out of the menu to remove items\n"
818 " - drag items in menu to change their position\n"
819 " - drag items with Control pressed to copy them\n"
820 " - double click in a menu item to change the label\n"
821 " - click on a menu item to change related information"));
822 WMMapWidget(label);
824 WMRealizeWidget(panel->box);
825 WMMapSubwidgets(panel->box);
826 WMMapWidget(panel->box);
829 int i;
830 for (i = 0; i < 3; i++)
831 WMUnmapWidget(panel->itemPad[i]);
833 changedItemPad(panel->typeP, panel);
835 panel->sections[NoInfo][0] = label;
837 panel->sections[ExecInfo][0] = panel->commandF;
838 panel->sections[ExecInfo][1] = panel->shortF;
840 panel->sections[CommandInfo][0] = panel->icommandL;
841 panel->sections[CommandInfo][1] = panel->shortF;
843 panel->sections[ExternalInfo][0] = panel->pathF;
845 panel->sections[PipeInfo][0] = panel->pipeF;
847 panel->sections[DirectoryInfo][0] = panel->dpathF;
848 panel->sections[DirectoryInfo][1] = panel->dcommandF;
850 panel->currentType = NoInfo;
852 showData(panel);
855 WMPoint pos;
857 pos = WMGetViewScreenPosition(WMWidgetView(panel->box));
859 if (pos.x < 200) {
860 pos.x += FRAME_WIDTH + 20;
861 } else {
862 pos.x = 10;
865 pos.y = WMAX(pos.y - 100, 0);
867 if (panel->menu)
868 WEditMenuShowAt(panel->menu, pos.x, pos.y);
872 static void freeItemData(ItemData * data)
874 #define CFREE(d) if (d) wfree(d)
876 /* TODO */
877 switch (data->type) {
878 case CommandInfo:
879 CFREE(data->param.command.parameter);
880 CFREE(data->param.command.shortcut);
881 break;
883 case ExecInfo:
884 CFREE(data->param.exec.command);
885 CFREE(data->param.exec.shortcut);
886 break;
888 case PipeInfo:
889 CFREE(data->param.pipe.command);
890 break;
892 case ExternalInfo:
893 CFREE(data->param.external.path);
894 break;
896 case DirectoryInfo:
897 CFREE(data->param.directory.command);
898 CFREE(data->param.directory.directory);
899 break;
901 default:
902 break;
905 wfree(data);
906 #undef CFREE
909 static ItemData *parseCommand(WMPropList * item)
911 ItemData *data = NEW(ItemData);
912 WMPropList *p;
913 char *command = NULL;
914 char *parameter = NULL;
915 char *shortcut = NULL;
916 int i = 1;
918 p = WMGetFromPLArray(item, i++);
919 command = WMGetFromPLString(p);
920 if (strcmp(command, "SHORTCUT") == 0) {
921 p = WMGetFromPLArray(item, i++);
922 shortcut = WMGetFromPLString(p);
923 p = WMGetFromPLArray(item, i++);
924 command = WMGetFromPLString(p);
926 p = WMGetFromPLArray(item, i++);
927 if (p)
928 parameter = WMGetFromPLString(p);
930 if (strcmp(command, "EXEC") == 0 || strcmp(command, "SHEXEC") == 0) {
932 data->type = ExecInfo;
934 data->param.exec.command = wstrdup(parameter);
935 if (shortcut)
936 data->param.exec.shortcut = wstrdup(shortcut);
938 } else if (strcmp(command, "OPEN_MENU") == 0) {
939 char *p;
941 * dir menu, menu file
942 * dir WITH
943 * |pipe
945 p = parameter;
946 while (isspace(*p) && *p)
947 p++;
948 if (*p == '|') {
949 if (*(p + 1) == '|') {
950 p++;
951 data->param.pipe.cached = 0;
952 } else {
953 data->param.pipe.cached = 1;
955 data->type = PipeInfo;
956 data->param.pipe.command = wtrimspace(p + 1);
957 } else {
958 char *s;
960 p = wstrdup(p);
962 s = strstr(p, "WITH");
963 if (s) {
964 char **tokens;
965 char **ctokens;
966 int tokn;
967 int i, j;
969 data->type = DirectoryInfo;
971 *s = '\0';
972 s += 5;
973 while (*s && isspace(*s))
974 s++;
975 data->param.directory.command = wstrdup(s);
977 wtokensplit(p, &tokens, &tokn);
978 wfree(p);
980 ctokens = wmalloc(sizeof(char *) * tokn);
982 for (i = 0, j = 0; i < tokn; i++) {
983 if (strcmp(tokens[i], "-noext") == 0) {
984 data->param.directory.stripExt = 1;
985 } else {
986 ctokens[j++] = tokens[i];
989 data->param.directory.directory = wtokenjoin(ctokens, j);
990 wfree(ctokens);
992 wtokenfree(tokens, tokn);
993 } else {
994 data->type = ExternalInfo;
995 data->param.external.path = p;
998 } else if (strcmp(command, "WORKSPACE_MENU") == 0) {
999 data->type = WSMenuInfo;
1000 } else if (strcmp(command, "WINDOWS_MENU") == 0) {
1001 data->type = WWindowListInfo;
1002 } else {
1003 int cmd;
1005 if (strcmp(command, "ARRANGE_ICONS") == 0) {
1006 cmd = 0;
1007 } else if (strcmp(command, "HIDE_OTHERS") == 0) {
1008 cmd = 1;
1009 } else if (strcmp(command, "SHOW_ALL") == 0) {
1010 cmd = 2;
1011 } else if (strcmp(command, "EXIT") == 0) {
1012 cmd = 3;
1013 } else if (strcmp(command, "SHUTDOWN") == 0) {
1014 cmd = 4;
1015 } else if (strcmp(command, "RESTART") == 0) {
1016 if (parameter) {
1017 cmd = 6;
1018 } else {
1019 cmd = 5;
1021 } else if (strcmp(command, "SAVE_SESSION") == 0) {
1022 cmd = 7;
1023 } else if (strcmp(command, "CLEAR_SESSION") == 0) {
1024 cmd = 8;
1025 } else if (strcmp(command, "REFRESH") == 0) {
1026 cmd = 9;
1027 } else if (strcmp(command, "INFO_PANEL") == 0) {
1028 cmd = 10;
1029 } else if (strcmp(command, "LEGAL_PANEL") == 0) {
1030 cmd = 11;
1031 } else {
1032 wwarning(_("unknown command '%s' in menu"), command);
1033 goto error;
1036 data->type = CommandInfo;
1038 data->param.command.command = cmd;
1039 if (shortcut)
1040 data->param.command.shortcut = wstrdup(shortcut);
1041 if (parameter)
1042 data->param.command.parameter = wstrdup(parameter);
1045 return data;
1047 error:
1048 wfree(data);
1050 return NULL;
1053 static void updateFrameTitle(_Panel * panel, char *title, InfoType type)
1055 if (type != NoInfo) {
1056 char *tmp;
1058 switch (type) {
1059 case ExecInfo:
1060 tmp = wstrconcat(title, _(": Execute Program"));
1061 break;
1063 case CommandInfo:
1064 tmp = wstrconcat(title, _(": Perform Internal Command"));
1065 break;
1067 case ExternalInfo:
1068 tmp = wstrconcat(title, _(": Open a Submenu"));
1069 break;
1071 case PipeInfo:
1072 tmp = wstrconcat(title, _(": Program Generated Submenu"));
1073 break;
1075 case DirectoryInfo:
1076 tmp = wstrconcat(title, _(": Directory Contents Menu"));
1077 break;
1079 case WSMenuInfo:
1080 tmp = wstrconcat(title, _(": Open Workspaces Submenu"));
1081 break;
1083 case WWindowListInfo:
1084 tmp = wstrconcat(title, _(": Open Window List Submenu"));
1085 break;
1087 default:
1088 tmp = NULL;
1089 break;
1091 WMSetFrameTitle(panel->optionsF, tmp);
1092 wfree(tmp);
1093 } else {
1094 WMSetFrameTitle(panel->optionsF, NULL);
1098 static void changeInfoType(_Panel * panel, char *title, InfoType type)
1100 WMWidget **w;
1102 if (panel->currentType != type) {
1104 w = panel->sections[panel->currentType];
1106 while (*w) {
1107 WMUnmapWidget(*w);
1108 w++;
1110 WMUnmapWidget(panel->paramF);
1111 WMUnmapWidget(panel->quickB);
1113 w = panel->sections[type];
1115 while (*w) {
1116 WMMapWidget(*w);
1117 w++;
1121 updateFrameTitle(panel, title, type);
1123 panel->currentType = type;
1126 static void updateMenuItem(_Panel * panel, WEditMenuItem * item, WMWidget * changedWidget)
1128 ItemData *data = WGetEditMenuItemData(item);
1130 assert(data != NULL);
1132 #define REPLACE(v, d) if (v) wfree(v); v = d
1134 switch (data->type) {
1135 case ExecInfo:
1136 if (changedWidget == panel->commandT) {
1137 REPLACE(data->param.exec.command, WMGetTextFieldText(panel->commandT));
1139 if (changedWidget == panel->shortT) {
1140 REPLACE(data->param.exec.shortcut, WMGetTextFieldText(panel->shortT));
1142 break;
1144 case CommandInfo:
1145 if (changedWidget == panel->icommandL) {
1146 data->param.command.command = WMGetListSelectedItemRow(panel->icommandL);
1148 switch (data->param.command.command) {
1149 case 3:
1150 case 4:
1151 if (changedWidget == panel->quickB) {
1152 REPLACE(data->param.command.parameter, WMGetButtonSelected(panel->quickB)
1153 ? wstrdup("QUICK") : NULL);
1155 break;
1157 case 6:
1158 if (changedWidget == panel->paramT) {
1159 REPLACE(data->param.command.parameter, WMGetTextFieldText(panel->paramT));
1161 break;
1163 if (changedWidget == panel->shortT) {
1164 REPLACE(data->param.command.shortcut, WMGetTextFieldText(panel->shortT));
1167 break;
1169 case PipeInfo:
1170 if (changedWidget == panel->pipeT) {
1171 REPLACE(data->param.pipe.command, WMGetTextFieldText(panel->pipeT));
1173 if (changedWidget == panel->pipeCacheB) {
1174 data->param.pipe.cached = WMGetButtonSelected(panel->pipeCacheB);
1176 break;
1178 case ExternalInfo:
1179 if (changedWidget == panel->pathT) {
1180 REPLACE(data->param.external.path, WMGetTextFieldText(panel->pathT));
1182 break;
1184 case DirectoryInfo:
1185 if (changedWidget == panel->dpathT) {
1186 REPLACE(data->param.directory.directory, WMGetTextFieldText(panel->dpathT));
1188 if (changedWidget == panel->dcommandT) {
1189 REPLACE(data->param.directory.command, WMGetTextFieldText(panel->dcommandT));
1191 if (changedWidget == panel->dstripB) {
1192 data->param.directory.stripExt = WMGetButtonSelected(panel->dstripB);
1194 break;
1196 default:
1197 assert(0);
1198 break;
1201 #undef REPLACE
1204 static void
1205 menuItemCloned(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * origItem, WEditMenuItem * newItem)
1207 ItemData *data = WGetEditMenuItemData(origItem);
1208 ItemData *newData;
1210 if (!data)
1211 return;
1213 #define DUP(s) (s) ? wstrdup(s) : NULL
1215 newData = NEW(ItemData);
1217 newData->type = data->type;
1219 switch (data->type) {
1220 case ExecInfo:
1221 newData->param.exec.command = DUP(data->param.exec.command);
1222 newData->param.exec.shortcut = DUP(data->param.exec.shortcut);
1223 break;
1225 case CommandInfo:
1226 newData->param.command.command = data->param.command.command;
1227 newData->param.command.parameter = DUP(data->param.command.parameter);
1228 newData->param.command.shortcut = DUP(data->param.command.shortcut);
1229 break;
1231 case PipeInfo:
1232 newData->param.pipe.command = DUP(data->param.pipe.command);
1233 newData->param.pipe.cached = data->param.pipe.cached;
1234 break;
1236 case ExternalInfo:
1237 newData->param.external.path = DUP(data->param.external.path);
1238 break;
1240 case DirectoryInfo:
1241 newData->param.directory.directory = DUP(data->param.directory.directory);
1242 newData->param.directory.command = DUP(data->param.directory.command);
1243 newData->param.directory.stripExt = data->param.directory.stripExt;
1244 break;
1246 default:
1247 break;
1250 #undef DUP
1252 WSetEditMenuItemData(newItem, newData, (WMCallback *) freeItemData);
1255 static void menuItemEdited(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item)
1257 _Panel *panel = (_Panel *) delegate->data;
1258 WEditMenu *submenu;
1260 updateFrameTitle(panel, WGetEditMenuItemTitle(item), panel->currentType);
1262 submenu = WGetEditMenuSubmenu(menu, item);
1263 if (submenu) {
1264 WSetEditMenuTitle(submenu, WGetEditMenuItemTitle(item));
1268 static Bool shouldRemoveItem(struct WEditMenuDelegate *delegate, WEditMenu * menu, WEditMenuItem * item)
1270 _Panel *panel = (_Panel *) delegate->data;
1272 if (panel->dontAsk)
1273 return True;
1275 if (WGetEditMenuSubmenu(menu, item)) {
1276 int res;
1278 res = WMRunAlertPanel(WMWidgetScreen(menu), NULL,
1279 _("Remove Submenu"),
1280 _("Removing this item will destroy all items inside\n"
1281 "the submenu. Do you really want to do that?"),
1282 _("Yes"), _("No"), _("Yes, don't ask again."));
1283 switch (res) {
1284 case WAPRDefault:
1285 return True;
1286 case WAPRAlternate:
1287 return False;
1288 case WAPROther:
1289 panel->dontAsk = True;
1290 return True;
1293 return True;
1296 static void menuItemDeselected(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * item)
1298 _Panel *panel = (_Panel *) delegate->data;
1300 changeInfoType(panel, NULL, NoInfo);
1303 static void menuItemSelected(WEditMenuDelegate * delegate, WEditMenu * menu, WEditMenuItem * item)
1305 ItemData *data = WGetEditMenuItemData(item);
1306 _Panel *panel = (_Panel *) delegate->data;
1308 panel->currentItem = item;
1310 if (data) {
1311 changeInfoType(panel, WGetEditMenuItemTitle(item), data->type);
1313 switch (data->type) {
1314 case NoInfo:
1315 break;
1317 case ExecInfo:
1318 WMSetTextFieldText(panel->commandT, data->param.exec.command);
1319 WMSetTextFieldText(panel->shortT, data->param.exec.shortcut);
1320 break;
1322 case CommandInfo:
1323 WMSelectListItem(panel->icommandL, data->param.command.command);
1324 WMSetListPosition(panel->icommandL, data->param.command.command - 2);
1325 WMSetTextFieldText(panel->shortT, data->param.command.shortcut);
1327 switch (data->param.command.command) {
1328 case 3:
1329 case 4:
1330 WMSetButtonSelected(panel->quickB, data->param.command.parameter != NULL);
1331 break;
1332 case 6:
1333 WMSetTextFieldText(panel->paramT, data->param.command.parameter);
1334 break;
1337 icommandLClicked(panel->icommandL, panel);
1338 break;
1340 case PipeInfo:
1341 WMSetTextFieldText(panel->pipeT, data->param.pipe.command);
1342 WMSetButtonSelected(panel->pipeCacheB, data->param.pipe.cached);
1343 break;
1345 case ExternalInfo:
1346 WMSetTextFieldText(panel->pathT, data->param.external.path);
1347 break;
1349 case DirectoryInfo:
1350 WMSetTextFieldText(panel->dpathT, data->param.directory.directory);
1351 WMSetTextFieldText(panel->dcommandT, data->param.directory.command);
1352 WMSetButtonSelected(panel->dstripB, data->param.directory.stripExt);
1353 break;
1355 case WSMenuInfo:
1356 break;
1358 default:
1359 break;
1364 static WEditMenu *buildSubmenu(_Panel * panel, WMPropList * pl)
1366 WMScreen *scr = WMWidgetScreen(panel->parent);
1367 WEditMenu *menu;
1368 WEditMenuItem *item;
1369 char *title;
1370 WMPropList *tp, *bp;
1371 int i;
1373 tp = WMGetFromPLArray(pl, 0);
1374 title = WMGetFromPLString(tp);
1376 menu = WCreateEditMenu(scr, title);
1378 for (i = 1; i < WMGetPropListItemCount(pl); i++) {
1379 WMPropList *pi;
1381 pi = WMGetFromPLArray(pl, i);
1383 tp = WMGetFromPLArray(pi, 0);
1384 bp = WMGetFromPLArray(pi, 1);
1386 title = WMGetFromPLString(tp);
1388 if (!bp || WMIsPLArray(bp)) { /* it's a submenu */
1389 WEditMenu *submenu;
1391 submenu = buildSubmenu(panel, pi);
1393 item = WAddMenuItemWithTitle(menu, title);
1395 WSetEditMenuSubmenu(menu, item, submenu);
1396 } else {
1397 ItemData *data;
1399 item = WAddMenuItemWithTitle(menu, title);
1401 data = parseCommand(pi);
1403 if (panel->markerPix[data->type])
1404 WSetEditMenuItemImage(item, panel->markerPix[data->type]);
1405 WSetEditMenuItemData(item, data, (WMCallback *) freeItemData);
1409 WSetEditMenuAcceptsDrop(menu, True);
1410 WSetEditMenuDelegate(menu, &menuDelegate);
1412 WMRealizeWidget(menu);
1414 return menu;
1417 static void buildMenuFromPL(_Panel * panel, WMPropList * pl)
1419 panel->menu = buildSubmenu(panel, pl);
1422 static WMPropList *getDefaultMenu(_Panel * panel)
1424 WMPropList *menu;
1425 char *menuPath, *gspath;
1427 gspath = wusergnusteppath();
1429 menuPath = wmalloc(strlen(gspath) + 128);
1430 sprintf(menuPath, "%s/Library/WindowMaker/plmenu", gspath);
1432 menu = WMReadPropListFromFile(menuPath);
1434 if (!menu) {
1435 char *buffer, *msg;
1437 msg = _("Could not open default menu from '%s'");
1438 buffer = wmalloc(strlen(msg) + strlen(menuPath) + 10);
1439 sprintf(buffer, msg, menuPath);
1440 WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
1441 _("Error"), buffer, _("OK"), NULL, NULL);
1442 wfree(buffer);
1445 wfree(menuPath);
1447 return menu;
1450 static void showData(_Panel * panel)
1452 char *gspath;
1453 char *menuPath;
1454 WMPropList *pmenu;
1456 gspath = wusergnusteppath();
1458 menuPath = wmalloc(strlen(gspath) + 32);
1459 strcpy(menuPath, gspath);
1460 strcat(menuPath, "/Defaults/WMRootMenu");
1462 pmenu = WMReadPropListFromFile(menuPath);
1464 if (!pmenu || !WMIsPLArray(pmenu)) {
1465 int res;
1467 res = WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
1468 _("Warning"),
1469 _("The menu file format currently in use is not supported\n"
1470 "by this tool. Do you want to discard the current menu\n"
1471 "to use this tool?"),
1472 _("Yes, Discard and Update"), _("No, Keep Current Menu"), NULL);
1474 if (res == WAPRDefault) {
1475 pmenu = getDefaultMenu(panel);
1477 if (!pmenu) {
1478 pmenu = WMCreatePLArray(WMCreatePLString("Applications"), NULL);
1480 } else {
1481 panel->dontSave = True;
1482 return;
1486 panel->menuPath = menuPath;
1488 buildMenuFromPL(panel, pmenu);
1490 WMReleasePropList(pmenu);
1493 static Bool notblank(char *s)
1495 if (s) {
1496 while (*s++) {
1497 if (!isspace(*s))
1498 return True;
1501 return False;
1504 static WMPropList *processData(char *title, ItemData * data)
1506 WMPropList *item;
1507 char *s1;
1508 static WMPropList *pscut = NULL;
1509 static WMPropList *pomenu = NULL;
1510 int i;
1512 if (!pscut) {
1513 pscut = WMCreatePLString("SHORTCUT");
1514 pomenu = WMCreatePLString("OPEN_MENU");
1517 item = WMCreatePLArray(WMCreatePLString(title), NULL);
1519 switch (data->type) {
1520 case ExecInfo:
1521 if (data->param.exec.command == NULL)
1522 return NULL;
1523 #if 1
1524 if (strpbrk(data->param.exec.command, "&$*|><?`=;")) {
1525 s1 = "SHEXEC";
1526 } else {
1527 s1 = "EXEC";
1529 #else
1530 s1 = "SHEXEC";
1531 #endif
1533 if (notblank(data->param.exec.shortcut)) {
1534 WMAddToPLArray(item, pscut);
1535 WMAddToPLArray(item, WMCreatePLString(data->param.exec.shortcut));
1538 WMAddToPLArray(item, WMCreatePLString(s1));
1539 WMAddToPLArray(item, WMCreatePLString(data->param.exec.command));
1540 break;
1542 case CommandInfo:
1543 if (notblank(data->param.command.shortcut)) {
1544 WMAddToPLArray(item, pscut);
1545 WMAddToPLArray(item, WMCreatePLString(data->param.command.shortcut));
1548 i = data->param.command.command;
1550 WMAddToPLArray(item, WMCreatePLString(commandNames[i]));
1552 switch (i) {
1553 case 3:
1554 case 4:
1555 if (data->param.command.parameter) {
1556 WMAddToPLArray(item, WMCreatePLString(data->param.command.parameter));
1558 break;
1560 case 6: /* restart */
1561 if (data->param.command.parameter) {
1562 WMAddToPLArray(item, WMCreatePLString(data->param.command.parameter));
1564 break;
1567 break;
1569 case PipeInfo:
1570 if (!data->param.pipe.command)
1571 return NULL;
1572 WMAddToPLArray(item, pomenu);
1573 if (data->param.pipe.cached)
1574 s1 = wstrconcat("| ", data->param.pipe.command);
1575 else
1576 s1 = wstrconcat("|| ", data->param.pipe.command);
1577 WMAddToPLArray(item, WMCreatePLString(s1));
1578 wfree(s1);
1579 break;
1581 case ExternalInfo:
1582 if (!data->param.external.path)
1583 return NULL;
1584 WMAddToPLArray(item, pomenu);
1585 WMAddToPLArray(item, WMCreatePLString(data->param.external.path));
1586 break;
1588 case DirectoryInfo:
1589 if (!data->param.directory.directory || !data->param.directory.command)
1590 return NULL;
1592 int l;
1593 char *tmp;
1595 l = strlen(data->param.directory.directory);
1596 l += strlen(data->param.directory.command);
1597 l += 32;
1599 WMAddToPLArray(item, pomenu);
1601 tmp = wmalloc(l);
1602 sprintf(tmp, "%s%s WITH %s",
1603 data->param.directory.stripExt ? "-noext " : "",
1604 data->param.directory.directory, data->param.directory.command);
1606 WMAddToPLArray(item, WMCreatePLString(tmp));
1607 wfree(tmp);
1609 break;
1611 case WSMenuInfo:
1612 WMAddToPLArray(item, WMCreatePLString("WORKSPACE_MENU"));
1613 break;
1615 case WWindowListInfo:
1616 WMAddToPLArray(item, WMCreatePLString("WINDOWS_MENU"));
1617 break;
1619 default:
1620 assert(0);
1621 break;
1624 return item;
1627 static WMPropList *processSubmenu(WEditMenu * menu)
1629 WEditMenuItem *item;
1630 WMPropList *pmenu;
1631 WMPropList *pl;
1632 char *s;
1633 int i;
1635 s = WGetEditMenuTitle(menu);
1636 pl = WMCreatePLString(s);
1638 pmenu = WMCreatePLArray(pl, NULL);
1640 i = 0;
1641 while ((item = WGetEditMenuItem(menu, i++))) {
1642 WEditMenu *submenu;
1644 s = WGetEditMenuItemTitle(item);
1646 submenu = WGetEditMenuSubmenu(menu, item);
1647 if (submenu) {
1648 pl = processSubmenu(submenu);
1649 } else {
1650 pl = processData(s, WGetEditMenuItemData(item));
1653 if (!pl)
1654 continue;
1656 WMAddToPLArray(pmenu, pl);
1659 return pmenu;
1662 static WMPropList *buildPLFromMenu(_Panel * panel)
1664 WMPropList *menu;
1666 menu = processSubmenu(panel->menu);
1668 return menu;
1671 static void storeData(_Panel * panel)
1673 WMPropList *menu;
1675 if (panel->dontSave)
1676 return;
1678 menu = buildPLFromMenu(panel);
1680 WMWritePropListToFile(menu, panel->menuPath, True);
1682 WMReleasePropList(menu);
1685 static void showMenus(_Panel * panel)
1687 if (panel->menu)
1688 WEditMenuUnhide(panel->menu);
1691 static void hideMenus(_Panel * panel)
1693 if (panel->menu)
1694 WEditMenuHide(panel->menu);
1697 Panel *InitMenu(WMScreen * scr, WMWidget * parent)
1699 _Panel *panel;
1701 panel = wmalloc(sizeof(_Panel));
1702 memset(panel, 0, sizeof(_Panel));
1704 panel->sectionName = _("Applications Menu Definition");
1706 panel->description = _("Edit the menu for launching applications.");
1708 panel->parent = parent;
1710 panel->callbacks.createWidgets = createPanel;
1711 panel->callbacks.updateDomain = storeData;
1712 panel->callbacks.showPanel = showMenus;
1713 panel->callbacks.hidePanel = hideMenus;
1715 AddSection(panel, ICON_FILE);
1717 return panel;