Bugs fixes and add KEY_CONTROL_WINDOW_WEIGHT to key move/resize window.
[wmaker-crm.git] / WPrefs.app / MenuGuru.c
blobc34d1e9df4fb9c897cf4caa6accc1dcf4eeab09c
1 /* MenuGuru.c- OPEN_MENU definition "guru" assistant
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "WPrefs.h"
25 #include <assert.h>
26 #include <ctype.h>
29 typedef struct _MenuGuru {
30 WMWindow *win;
32 WMButton *nextB;
33 WMButton *backB;
34 WMButton *cancelB;
36 WMLabel *typetopL;
37 WMButton *typefB;
38 WMButton *typepB;
39 WMButton *typedB;
41 WMLabel *pathtopL;
42 WMTextField *pathT;
43 WMButton *pathB;
44 WMLabel *pathbotL;
46 WMLabel *pipetopL;
47 WMTextField *pipeT;
48 WMLabel *pipebotL;
50 WMLabel *dirtopL;
51 WMTextField *dirT;
52 WMLabel *dirbotL;
54 WMLabel *progtopL;
55 WMTextField *progT;
56 WMLabel *progbotL;
59 char ok;
60 char end;
61 int section;
62 } MenuGuru;
65 enum {
66 GSelectType,
67 GSelectFile,
68 GSelectPaths,
69 GSelectPipe,
70 GSelectProgram,
71 GDone
76 static char*
77 trimstr(char *str)
79 char *p = str;
80 int i;
82 while (isspace(*p)) p++;
83 p = wstrdup(p);
84 i = strlen(p);
85 while (isspace(p[i]) && i>0) {
86 p[i] = 0;
87 i--;
90 return p;
94 static void
95 showPart(MenuGuru *panel, int part)
97 WMUnmapSubwidgets(panel->win);
98 WMMapWidget(panel->nextB);
99 WMMapWidget(panel->backB);
100 WMMapWidget(panel->cancelB);
102 WMSetButtonEnabled(panel->backB, part!=GSelectType);
104 switch (part) {
105 case GSelectType:
106 WMSetWindowTitle(panel->win, _("Menu Guru - Select Type"));
107 WMMapWidget(panel->typetopL);
108 WMMapWidget(panel->typedB);
109 WMMapWidget(panel->typepB);
110 WMMapWidget(panel->typefB);
111 WMSetButtonText(panel->nextB, _("Next"));
112 break;
113 case GSelectFile:
114 WMSetWindowTitle(panel->win, _("Menu Guru - Select Menu File"));
115 WMMapWidget(panel->pathtopL);
116 WMMapWidget(panel->pathT);
117 /* WMMapWidget(panel->pathB);*/
118 WMMapWidget(panel->pathbotL);
119 WMSetButtonText(panel->nextB, _("OK"));
120 break;
121 case GSelectPipe:
122 WMSetWindowTitle(panel->win, _("Menu Guru - Select Pipe Command"));
123 WMMapWidget(panel->pipetopL);
124 WMMapWidget(panel->pipeT);
125 WMMapWidget(panel->pipebotL);
126 WMSetButtonText(panel->nextB, _("OK"));
127 break;
128 case GSelectPaths:
129 WMSetWindowTitle(panel->win, _("Menu Guru - Select Directories"));
130 WMMapWidget(panel->dirtopL);
131 WMMapWidget(panel->dirT);
132 WMMapWidget(panel->dirbotL);
133 WMSetButtonText(panel->nextB, _("Next"));
134 break;
135 case GSelectProgram:
136 WMSetWindowTitle(panel->win, _("Menu Guru - Select Command"));
137 WMMapWidget(panel->progtopL);
138 WMMapWidget(panel->progT);
139 WMMapWidget(panel->progbotL);
140 WMSetButtonText(panel->nextB, _("OK"));
141 break;
143 panel->section = part;
147 static void
148 clickNext(WMWidget *w, void *data)
150 MenuGuru *panel = (MenuGuru*)data;
151 char *tmp, *p;
153 switch (panel->section) {
154 case GSelectType:
155 if (WMGetButtonSelected(panel->typefB)) {
156 showPart(panel, GSelectFile);
157 } else if (WMGetButtonSelected(panel->typepB)) {
158 showPart(panel, GSelectPipe);
159 } else {
160 showPart(panel, GSelectPaths);
162 break;
163 case GSelectFile:
164 tmp = WMGetTextFieldText(panel->pathT);
165 p = trimstr(tmp); free(tmp);
166 if (strlen(p)==0) {
167 free(p);
168 return;
170 free(p);
171 panel->ok = 1;
172 panel->end = 1;
173 break;
174 case GSelectPaths:
175 tmp = WMGetTextFieldText(panel->dirT);
176 p = trimstr(tmp); free(tmp);
177 if (strlen(p)==0) {
178 free(p);
179 return;
181 free(p);
182 showPart(panel, GSelectProgram);
183 break;
184 case GSelectPipe:
185 tmp = WMGetTextFieldText(panel->pipeT);
186 p = trimstr(tmp); free(tmp);
187 if (strlen(p)==0) {
188 free(p);
189 return;
191 free(p);
192 panel->ok = 1;
193 panel->end = 1;
194 break;
195 case GSelectProgram:
196 panel->ok = 1;
197 panel->end = 1;
198 break;
199 default:
200 panel->end = 1;
206 static void
207 clickBack(WMWidget *w, void *data)
209 MenuGuru *panel = (MenuGuru*)data;
210 int newSection;
212 switch (panel->section) {
213 case GSelectFile:
214 newSection = GSelectType;
215 break;
216 case GSelectPipe:
217 newSection = GSelectType;
218 break;
219 case GSelectPaths:
220 newSection = GSelectType;
221 break;
222 case GSelectProgram:
223 newSection = GSelectPaths;
224 break;
225 default:
226 newSection = panel->section;
228 showPart(panel, newSection);
233 static void
234 closeWindow(WMWidget *w, void *data)
236 MenuGuru *panel = (MenuGuru*)data;
238 panel->end = 1;
242 static void
243 createPanel(WMWindow *mainWindow, MenuGuru *panel)
245 panel->win = WMCreatePanelForWindow(mainWindow, "menuGuru");
246 WMResizeWidget(panel->win, 370, 220);
248 panel->nextB = WMCreateCommandButton(panel->win);
249 WMResizeWidget(panel->nextB, 80, 24);
250 WMMoveWidget(panel->nextB, 280, 185);
251 WMSetButtonText(panel->nextB, _("Next"));
252 WMSetButtonAction(panel->nextB, clickNext, panel);
254 panel->backB = WMCreateCommandButton(panel->win);
255 WMResizeWidget(panel->backB, 80, 24);
256 WMMoveWidget(panel->backB, 195, 185);
257 WMSetButtonText(panel->backB, _("Back"));
258 WMSetButtonAction(panel->backB, clickBack, panel);
260 panel->cancelB = WMCreateCommandButton(panel->win);
261 WMResizeWidget(panel->cancelB, 80, 24);
262 WMMoveWidget(panel->cancelB, 110, 185);
263 WMSetButtonText(panel->cancelB, _("Cancel"));
264 WMSetButtonAction(panel->cancelB, closeWindow, panel);
266 /**/
268 panel->typetopL = WMCreateLabel(panel->win);
269 WMResizeWidget(panel->typetopL, 350, 45);
270 WMMoveWidget(panel->typetopL, 10, 10);
271 WMSetLabelText(panel->typetopL, _("This process will help you create a "
272 "submenu which definition is located in another file "
273 "or is created dynamically.\nWhat do you want to use as the "
274 "contents of the submenu?"));
276 panel->typefB = WMCreateRadioButton(panel->win);
277 WMResizeWidget(panel->typefB, 330, 35);
278 WMMoveWidget(panel->typefB, 20, 65);
279 WMSetButtonText(panel->typefB, _("A file containing the menu definition "
280 "in the plain text (non-property list) menu format."));
282 panel->typepB = WMCreateRadioButton(panel->win);
283 WMResizeWidget(panel->typepB, 330, 35);
284 WMMoveWidget(panel->typepB, 20, 105);
285 WMSetButtonText(panel->typepB, _("The menu definition generated by a "
286 "script/program read through a pipe."));
288 panel->typedB = WMCreateRadioButton(panel->win);
289 WMResizeWidget(panel->typedB, 330, 35);
290 WMMoveWidget(panel->typedB, 20, 140);
291 WMSetButtonText(panel->typedB, _("The files in one or more directories."));
293 WMGroupButtons(panel->typefB, panel->typepB);
294 WMGroupButtons(panel->typefB, panel->typedB);
296 WMPerformButtonClick(panel->typefB);
298 /**/
300 panel->pathtopL = WMCreateLabel(panel->win);
301 WMResizeWidget(panel->pathtopL, 330, 20);
302 WMMoveWidget(panel->pathtopL, 20, 25);
303 WMSetLabelText(panel->pathtopL, _("Type the path for the menu file:"));
305 panel->pathT = WMCreateTextField(panel->win);
306 WMResizeWidget(panel->pathT, 330, 20);
307 WMMoveWidget(panel->pathT, 20, 50);
309 panel->pathB = WMCreateCommandButton(panel->win);
310 WMResizeWidget(panel->pathB, 70, 24);
311 WMMoveWidget(panel->pathB, 275, 75);
312 WMSetButtonText(panel->pathB, _("Browse"));
315 panel->pathbotL = WMCreateLabel(panel->win);
316 WMResizeWidget(panel->pathbotL, 330, 80);
317 WMMoveWidget(panel->pathbotL, 20, 100);
318 WMSetLabelText(panel->pathbotL, _("The menu file must contain a menu "
319 "in the plain text menu file format. This format is "
320 "described in the menu files included with WindowMaker, "
321 "probably at ~/GNUstep/Library/WindowMaker/menu"));
323 /**/
325 panel->pipetopL = WMCreateLabel(panel->win);
326 WMResizeWidget(panel->pipetopL, 330, 32);
327 WMMoveWidget(panel->pipetopL, 20, 20);
328 WMSetLabelText(panel->pipetopL, _("Type the command that will generate "
329 "the menu definition:"));
331 panel->pipeT = WMCreateTextField(panel->win);
332 WMResizeWidget(panel->pipeT, 330, 20);
333 WMMoveWidget(panel->pipeT, 20, 55);
335 panel->pipebotL = WMCreateLabel(panel->win);
336 WMResizeWidget(panel->pipebotL, 330, 80);
337 WMMoveWidget(panel->pipebotL, 20, 85);
338 WMSetLabelText(panel->pipebotL, _("The command supplied must generate and "
339 "output a valid menu definition to stdout. This definition "
340 "should be in the plain text menu file format, described "
341 "in the menu files included with WindowMaker, usually "
342 "at ~/GNUstep/Library/WindowMaker/menu"));
345 /**/
347 panel->dirtopL = WMCreateLabel(panel->win);
348 WMResizeWidget(panel->dirtopL, 330, 32);
349 WMMoveWidget(panel->dirtopL, 20, 20);
350 WMSetLabelText(panel->dirtopL, _("Type the path for the directory. You "
351 "can type more than one path by separating them with "
352 "spaces."));
354 panel->dirT = WMCreateTextField(panel->win);
355 WMResizeWidget(panel->dirT, 330, 20);
356 WMMoveWidget(panel->dirT, 20, 55);
358 panel->dirbotL = WMCreateLabel(panel->win);
359 WMResizeWidget(panel->dirbotL, 330, 80);
360 WMMoveWidget(panel->dirbotL, 20, 85);
361 WMSetLabelText(panel->dirbotL, _("The menu generated will have an item "
362 "for each file in the directory. The directories can "
363 "contain program executables or data files (such as "
364 "jpeg images)."));
367 /**/
369 panel->dirtopL = WMCreateLabel(panel->win);
370 WMResizeWidget(panel->dirtopL, 330, 32);
371 WMMoveWidget(panel->dirtopL, 20, 20);
372 WMSetLabelText(panel->dirtopL, _("Type the path for the directory. You "
373 "can type more than one path by separating them with "
374 "spaces."));
376 panel->dirT = WMCreateTextField(panel->win);
377 WMResizeWidget(panel->dirT, 330, 20);
378 WMMoveWidget(panel->dirT, 20, 55);
380 panel->dirbotL = WMCreateLabel(panel->win);
381 WMResizeWidget(panel->dirbotL, 330, 80);
382 WMMoveWidget(panel->dirbotL, 20, 85);
383 WMSetLabelText(panel->dirbotL, _("The menu generated will have an item "
384 "for each file in the directory. The directories can "
385 "contain program executables or data files (such as "
386 "jpeg images)."));
389 /**/
391 panel->dirtopL = WMCreateLabel(panel->win);
392 WMResizeWidget(panel->dirtopL, 330, 32);
393 WMMoveWidget(panel->dirtopL, 20, 20);
394 WMSetLabelText(panel->dirtopL, _("Type the path for the directory. You "
395 "can type more than one path by separating them with "
396 "spaces."));
398 panel->dirT = WMCreateTextField(panel->win);
399 WMResizeWidget(panel->dirT, 330, 20);
400 WMMoveWidget(panel->dirT, 20, 60);
402 panel->dirbotL = WMCreateLabel(panel->win);
403 WMResizeWidget(panel->dirbotL, 330, 80);
404 WMMoveWidget(panel->dirbotL, 20, 85);
405 WMSetLabelText(panel->dirbotL, _("The menu generated will have an item "
406 "for each file in the directory. The directories can "
407 "contain program executables or data files (such as "
408 "jpeg images)."));
410 /**/
412 panel->progtopL = WMCreateLabel(panel->win);
413 WMResizeWidget(panel->progtopL, 330, 48);
414 WMMoveWidget(panel->progtopL, 20, 10);
415 WMSetLabelText(panel->progtopL, _("If the directory contain data files, "
416 "type the command used to open these files. Otherwise, "
417 "leave it in blank."));
419 panel->progT = WMCreateTextField(panel->win);
420 WMResizeWidget(panel->progT, 330, 20);
421 WMMoveWidget(panel->progT, 20, 60);
423 panel->progbotL = WMCreateLabel(panel->win);
424 WMResizeWidget(panel->progbotL, 330, 72);
425 WMMoveWidget(panel->progbotL, 20, 90);
426 WMSetLabelText(panel->progbotL, _("Each file in the directory will have "
427 "an item and they will be opened with the supplied command."
428 "For example, if the directory contains image files and "
429 "the command is \"xv -root\", each file in the directory "
430 "will have a menu item like \"xv -root imagefile\"."));
432 WMRealizeWidget(panel->win);
437 char*
438 OpenMenuGuru(WMWindow *mainWindow)
440 WMScreen *scr = WMWidgetScreen(mainWindow);
441 MenuGuru panel;
442 char *text, *p, *dirs;
444 createPanel(mainWindow, &panel);
445 WMSetWindowCloseAction(panel.win, closeWindow, &panel);
447 showPart(&panel, GSelectType);
449 WMMapWidget(panel.win);
451 panel.ok = 0;
452 panel.end = 0;
453 while (!panel.end) {
454 XEvent ev;
455 WMNextEvent(WMScreenDisplay(scr), &ev);
456 WMHandleEvent(&ev);
459 text = NULL;
460 if (panel.ok) {
461 switch (panel.section) {
462 case GSelectFile:
463 text = WMGetTextFieldText(panel.pathT);
464 break;
465 case GSelectPipe:
466 text = WMGetTextFieldText(panel.pipeT);
467 p = trimstr(text); free(text);
468 if (p[0]!='|') {
469 text = wmalloc(strlen(p)+4);
470 strcpy(text, "| ");
471 strcat(text, p);
472 free(p);
473 } else {
474 text = p;
476 break;
477 case GSelectProgram:
478 dirs = WMGetTextFieldText(panel.dirT);
479 text = WMGetTextFieldText(panel.progT);
480 p = trimstr(text); free(text);
481 if (strlen(p)==0) {
482 free(p);
483 text = dirs;
484 } else {
485 text = wmalloc(strlen(dirs)+16+strlen(p));
486 sprintf(text, "%s WITH %s", dirs, p);
487 free(dirs);
488 free(p);
490 break;
494 WMDestroyWidget(panel.win);
496 return text;