Added handling for the command line argument ARGUMENTS.
[AROS.git] / workbench / c / R / gui.c
blobe1c48e444445e51b603a28f77aa3831b6c59f255
1 /*
2 Copyright © 2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
7 #include <aros/debug.h>
9 #include <libraries/mui.h>
10 #include <libraries/asl.h>
12 #include <proto/muimaster.h>
13 #include <proto/intuition.h>
14 #include <proto/exec.h>
15 #include <proto/alib.h>
16 #include <proto/asl.h>
17 #include <proto/dos.h>
19 #include <stdio.h>
21 #include "r.h"
22 #include "locale.h"
24 Object *application, *window;
25 struct Hook execute_btn_hook, pop_single_btn_hook, pop_multi_btn_hook;
26 struct FileRequester *request;
28 AROS_UFH3S(void, execute_btn_func,
29 AROS_UFHA(struct Hook *, h, A0),
30 AROS_UFHA(Object *, object, A2),
31 AROS_UFHA(struct Req **, msg, A1))
33 AROS_USERFUNC_INIT
35 D(bug("[R/execute_btn_func] msg %p *msg %p\n", msg, *msg));
36 (*msg)->do_execute = TRUE;
37 DoMethod(application, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
39 AROS_USERFUNC_EXIT
43 AROS_UFH3S(void, pop_single_btn_func,
44 AROS_UFHA(struct Hook *, h, A0),
45 AROS_UFHA(Object *, object, A2),
46 AROS_UFHA(Object **, str_object, A1))
48 AROS_USERFUNC_INIT
50 D(bug("[R/pop_single_btn_func] msg %p *msg %p\n", str_object, *str_object));
54 AslRequestTags
56 request,
57 ASLFR_DoMultiSelect, FALSE,
58 ASLFR_InitialFile, "",
59 ASLFR_InitialDrawer, "",
60 TAG_DONE
64 ULONG buflen = strlen(request->fr_Drawer) + strlen(request->fr_File) + 10;
65 TEXT *buffer = AllocPooled(poolmem, buflen);
66 if (buffer)
68 strcpy(buffer, request->fr_Drawer);
69 AddPart(buffer, request->fr_File, buflen);
70 SET(*str_object, MUIA_String_Contents, buffer);
71 FreePooled(poolmem, buffer, buflen);
75 AROS_USERFUNC_EXIT
79 AROS_UFH3S(void, pop_multi_btn_func,
80 AROS_UFHA(struct Hook *, h, A0),
81 AROS_UFHA(Object *, object, A2),
82 AROS_UFHA(Object **, str_object, A1))
84 AROS_USERFUNC_INIT
86 D(bug("[R/pop_multi_btn_func] msg %p *msg %p\n", str_object, *str_object));
90 AslRequestTags
92 request,
93 ASLFR_DoMultiSelect, TRUE,
94 ASLFR_InitialFile, "",
95 ASLFR_InitialDrawer, "",
96 TAG_DONE
100 LONG i;
101 ULONG buflen = 10;
102 for (i = 0; i < request->fr_NumArgs; i++)
104 buflen += strlen(request->fr_Drawer) + strlen(request->fr_ArgList[i].wa_Name) + 5;
107 TEXT *buffer = AllocPooled(poolmem, buflen);
108 if (buffer)
110 TEXT *ptr = buffer;
111 *ptr = '\0';
112 for (i = 0; i < request->fr_NumArgs; i++)
114 *ptr++ = '\"';
115 strcpy(ptr, request->fr_Drawer);
116 AddPart(ptr, request->fr_ArgList[i].wa_Name, buffer + buflen - ptr - 2);
117 ptr = buffer + strlen(buffer);
118 strcpy(ptr, "\" ");
119 ptr += 2;
121 SET(*str_object, MUIA_String_Contents, buffer);
122 FreePooled(poolmem, buffer, buflen);
126 AROS_USERFUNC_EXIT
130 BOOL create_gui(struct Req *req)
132 Object *ok_btn, *cancel_btn, *str_group, *chk_group;
133 struct TagItem str_group_tags[50];
134 struct TagItem chk_group_tags[50];
135 TEXT filename[256];
136 LONG i, str_cnt, chk_cnt;
138 request = AllocAslRequest(ASL_FileRequest, NULL);
139 if (request == NULL)
140 return FALSE;
142 execute_btn_hook.h_Entry = (HOOKFUNC)execute_btn_func;
143 pop_single_btn_hook.h_Entry = (HOOKFUNC)pop_single_btn_func;
144 pop_multi_btn_hook.h_Entry = (HOOKFUNC)pop_multi_btn_func;
148 bug(" i Name A F K M N S T\n");
149 for (i = 0; i < req->arg_cnt; i++)
153 "%2d %25s %c %c %c %c %c %c\n",
154 i, req->cargs[i].argname,
155 req->cargs[i].a_flag ? 'X' : '-',
156 req->cargs[i].f_flag ? 'X' : '-',
157 req->cargs[i].k_flag ? 'X' : '-',
158 req->cargs[i].m_flag ? 'X' : '-',
159 req->cargs[i].n_flag ? 'X' : '-',
160 req->cargs[i].s_flag ? 'X' : '-',
161 req->cargs[i].t_flag ? 'X' : '-'
168 i = 0, str_cnt = 0, chk_cnt = 0;
169 i < req->arg_cnt && str_cnt < 40 && chk_cnt < 40;
173 Object *new_obj = NULL;
174 Object *new_arg_obj = NULL; // the object with the argument's value
176 if (req->cargs[i].s_flag || req->cargs[i].t_flag) // Switch
178 new_obj = Label1(req->cargs[i].argname);
179 if (new_obj)
181 chk_group_tags[chk_cnt].ti_Tag = Child;
182 chk_group_tags[chk_cnt].ti_Data = (IPTR)new_obj;
183 chk_cnt++;
185 new_arg_obj = MUI_MakeObject(MUIO_Checkmark, NULL);
186 if (new_arg_obj)
188 chk_group_tags[chk_cnt].ti_Tag = Child;
189 chk_group_tags[chk_cnt].ti_Data = (IPTR)new_arg_obj;
190 req->cargs[i].object = new_arg_obj;
191 chk_cnt++;
194 else if (req->cargs[i].n_flag) // Numeric
196 new_obj = Label2(req->cargs[i].argname);
197 if (new_obj)
199 str_group_tags[str_cnt].ti_Tag = Child;
200 str_group_tags[str_cnt].ti_Data = (IPTR)new_obj;
201 str_cnt++;
203 new_arg_obj = StringObject,
204 StringFrame,
205 MUIA_CycleChain, 1,
206 MUIA_String_Accept , "+-0123456879",
207 End;
208 if (new_arg_obj)
210 str_group_tags[str_cnt].ti_Tag = Child;
211 str_group_tags[str_cnt].ti_Data = (IPTR)new_arg_obj;
212 req->cargs[i].object = new_arg_obj;
213 str_cnt++;
216 else if (req->cargs[i].m_flag) // Multiple
218 Object *pop_btn;
219 new_obj = Label2(req->cargs[i].argname);
220 if (new_obj)
222 str_group_tags[str_cnt].ti_Tag = Child;
223 str_group_tags[str_cnt].ti_Data = (IPTR)new_obj;
224 str_cnt++;
226 new_obj = HGroup,
227 Child, new_arg_obj = StringObject,
228 StringFrame,
229 MUIA_CycleChain, 1,
230 End,
231 Child, pop_btn = PopButton(MUII_PopFile),
232 End;
233 if (new_obj)
235 D(bug("[R] pop %p string %p\n", pop_btn, new_arg_obj));
236 DoMethod
238 pop_btn, MUIM_Notify, MUIA_Pressed, FALSE,
239 (IPTR)pop_btn, 3, MUIM_CallHook, &pop_multi_btn_hook, new_arg_obj
241 str_group_tags[str_cnt].ti_Tag = Child;
242 str_group_tags[str_cnt].ti_Data = (IPTR)new_obj;
243 req->cargs[i].object = new_arg_obj;
244 str_cnt++;
247 else
249 Object *pop_btn;
250 new_obj = Label2(req->cargs[i].argname);
251 if (new_obj)
253 str_group_tags[str_cnt].ti_Tag = Child;
254 str_group_tags[str_cnt].ti_Data = (IPTR)new_obj;
255 str_cnt++;
257 new_obj = HGroup,
258 Child, new_arg_obj = StringObject,
259 StringFrame,
260 MUIA_CycleChain, 1,
261 End,
262 Child, pop_btn = PopButton(MUII_PopFile),
263 End;
264 if (new_obj)
266 D(bug("[R] pop %p string %p\n", pop_btn, new_arg_obj));
267 DoMethod
269 pop_btn, MUIM_Notify, MUIA_Pressed, FALSE,
270 (IPTR)pop_btn, 3, MUIM_CallHook, &pop_single_btn_hook, new_arg_obj
272 str_group_tags[str_cnt].ti_Tag = Child;
273 str_group_tags[str_cnt].ti_Data = (IPTR)new_obj;
274 req->cargs[i].object = new_arg_obj;
275 str_cnt++;
280 // create string group
281 str_group_tags[str_cnt].ti_Tag = MUIA_Group_Columns;
282 str_group_tags[str_cnt].ti_Data = 2;
283 str_cnt++;
284 str_group_tags[str_cnt].ti_Tag = TAG_DONE;
285 str_group = MUI_NewObjectA(MUIC_Group, str_group_tags);
287 // fill empty check slots
288 for (i = 0; i < chk_cnt % 8; i++)
290 chk_group_tags[chk_cnt].ti_Tag = Child;
291 chk_group_tags[chk_cnt].ti_Data = (IPTR)HVSpace;
292 chk_cnt++;
295 // create check group
296 chk_group_tags[chk_cnt].ti_Tag = MUIA_Group_Columns;
297 chk_group_tags[chk_cnt].ti_Data = 8;
298 chk_cnt++;
299 chk_group_tags[chk_cnt].ti_Tag = TAG_DONE;
300 chk_group = MUI_NewObjectA(MUIC_Group, chk_group_tags);
302 sprintf(filename, _(MSG_WIN_TITLE), req->filename);
303 application = (Object *)ApplicationObject,
304 MUIA_Application_Title, __(MSG_APP_TITLE),
305 MUIA_Application_Version, (IPTR) VERSION,
306 MUIA_Application_Description, __(MSG_APP_DESCRIPTION),
307 MUIA_Application_Base, (IPTR) "REQUEST",
308 MUIA_Application_UseCommodities, FALSE,
309 SubWindow, (IPTR)(window = (Object *)WindowObject,
310 MUIA_Window_Title, (IPTR) filename,
311 MUIA_Window_ID, MAKE_ID('R','E','Q','U'),
312 WindowContents, VGroup,
313 Child, VGroup,
314 GroupFrame,
315 Child, str_group,
316 Child, HVSpace,
317 Child, chk_group,
318 End,
319 Child, (IPTR) (RectangleObject,
320 MUIA_Rectangle_HBar, TRUE,
321 MUIA_FixHeight, 2,
322 End),
323 Child, HGroup,
324 MUIA_Group_SameWidth, TRUE,
325 Child, ok_btn = SimpleButton(_(MSG_OK)),
326 Child, HVSpace,
327 Child, cancel_btn = SimpleButton(_(MSG_CANCEL)),
328 End,
329 End,
330 End),
331 End;
333 if (application)
335 DoMethod
337 window,
338 MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
339 (IPTR)application, 2,
340 MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
343 DoMethod
345 cancel_btn,
346 MUIM_Notify, MUIA_Pressed, FALSE,
347 (IPTR)application, 2,
348 MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
351 DoMethod
353 ok_btn,
354 MUIM_Notify, MUIA_Pressed, FALSE,
355 (IPTR)application, 3,
356 MUIM_CallHook, &execute_btn_hook, req
359 SET(window, MUIA_Window_Open, TRUE);
361 return TRUE;
363 return FALSE;
367 BOOL handle_gui(struct Req *req)
369 DoMethod(application, MUIM_Application_Execute);
370 SET(window, MUIA_Window_Open, FALSE);
371 return req->do_execute;
374 // combine string array to a single string with quoted entries
375 static STRPTR build_quoted_string(STRPTR *strings)
377 LONG i;
378 LONG size = 1;
379 STRPTR str;
381 for (i = 0; strings[i]; i++)
383 size += strlen(strings[i]) + 3;
385 str = AllocPooled(poolmem, size);
386 if (str == NULL)
388 return NULL;
391 for (i = 0; strings[i]; i++)
393 strcat(str, "\"");
394 strcat(str, strings[i]);
395 strcat(str, "\" ");
397 // string will be '\0' terminated because AllocPooled has MEMF_CLEAR
398 return str;
402 // fill the gadgets with the values which were given by the
403 // command line argument ARGUMENTS
404 BOOL set_defaults(struct Req *req)
406 struct RDArgs *rda;
407 IPTR *array;
408 LONG i;
409 BOOL success = FALSE;
411 if (req->arguments == NULL || req->arguments[0] == '\0')
413 // we're done if shell parameter 'argument' wasn't given
414 return TRUE;
417 array = AllocPooled(poolmem, sizeof (IPTR) * req->arg_cnt);
418 if (array)
420 rda = AllocDosObject(DOS_RDARGS, NULL);
421 if (rda)
423 rda->RDA_Source.CS_Buffer = req->arguments;
424 rda->RDA_Source.CS_Length = strlen(req->arguments);
425 rda->RDA_Source.CS_CurChr = 0;
426 if (ReadArgs(req->cmd_template, array, rda))
428 for (i = 0; i < req->arg_cnt; i++)
430 if (req->cargs[i].s_flag || req->cargs[i].t_flag)
432 SET(req->cargs[i].object, MUIA_Selected, array[i] ? TRUE : FALSE);
434 else if (req->cargs[i].n_flag && array[i])
436 SET(req->cargs[i].object, MUIA_String_Integer, *(LONG *)array[i]);
438 else if (req->cargs[i].m_flag && array[i])
440 SET(req->cargs[i].object, MUIA_String_Contents, build_quoted_string((STRPTR *)array[i]));
442 else if (array[i])
444 SET(req->cargs[i].object, MUIA_String_Contents, array[i]);
447 success = TRUE;
448 FreeArgs(rda);
450 FreeDosObject(DOS_RDARGS, rda);
452 FreePooled(poolmem, array, sizeof (IPTR) * req->arg_cnt);
454 return success;
458 BOOL get_gui_bool(struct CArg *carg)
460 return XGET(carg->object, MUIA_Selected);
464 CONST_STRPTR get_gui_string(struct CArg *carg)
466 return (CONST_STRPTR)XGET(carg->object, MUIA_String_Contents);
470 void cleanup_gui(void)
472 if (application) MUI_DisposeObject(application);
473 if (request) FreeAslRequest(request);