2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
5 Desc: RequestChoice CLI command
9 /*****************************************************************************
17 TITLE/A,BODY/A,GADGETS/A/M,PUBSCREEN/K
25 Allows AmigaDOS scripts to have access to the EasyRequest() function
30 TITLE - The text to display in the title bar of the requester.
32 BODY - The text to display in the body of the requester.
34 GADGETS - The text for each of the buttons.
36 PUBSCREEN - The name of the public screen to open the requester
41 Standard DOS return codes.
45 To place a newline into the body of the requester use *n or *N.
47 To place a quotation mark in the body of the requester use *".
49 The CLI template gives the GADGETS option as ALWAYS given; this
50 is different from the original program. This way, we do not have
51 to check to see if the gadgets have been given.
55 RequestChoice "This is a title" "This is*Na body" Okay|Cancel
57 This is self-explanitory, except for the "*N". This is the
58 equivalent of using a '\n' in C to get a newline in the body
59 of the requester. This requester will open on the Workbench
62 RequestChoice Title="This is a title" Body="This is*Na body"
63 Gadgets=Okay|Cancel PubScreen=DOPUS.1
65 This will do exactly the same as before except that it will
66 open on the Directory Opus public screen.
72 intuition.library/EasyRequestArgs()
78 ******************************************************************************/
80 #include <proto/dos.h>
81 #include <proto/exec.h>
82 #include <proto/intuition.h>
85 #include <dos/rdargs.h>
86 #include <exec/libraries.h>
87 #include <exec/memory.h>
88 #include <exec/types.h>
89 #include <intuition/intuition.h>
90 #include <intuition/screens.h>
94 #define ARG_TEMPLATE "TITLE/A,BODY/A,GADGETS/A/M,PUBSCREEN/K"
98 #define ARG_PUBSCREEN 3
101 const TEXT version
[] = "$VER: RequestChoice 41.3 (3.4.2014)\n";
103 static char ERROR_HEADER
[] = "RequestChoice";
105 static int Do_RequestChoice(STRPTR
, STRPTR
, STRPTR
*, STRPTR
);
106 static STRPTR
FilterBodyText(STRPTR Body
);
107 static STRPTR
ComposeGadgetText(STRPTR
* Gadgets
);
114 IPTR
* args
[TOTAL_ARGS
] = { NULL
, NULL
, NULL
, NULL
};
117 Return_Value
= RETURN_OK
;
119 rda
= ReadArgs(ARG_TEMPLATE
, (IPTR
*)args
, NULL
);
122 Return_Value
= Do_RequestChoice((STRPTR
)args
[ARG_TITLE
],
123 (STRPTR
)args
[ARG_BODY
],
124 (STRPTR
*)args
[ARG_GADGETS
],
125 (STRPTR
)args
[ARG_PUBSCREEN
]);
130 PrintFault(IoErr(), ERROR_HEADER
);
131 Return_Value
= RETURN_FAIL
;
134 return (Return_Value
);
138 STRPTR
ComposeGadgetText(STRPTR
*);
140 int Do_RequestChoice(STRPTR Title
,
146 struct EasyStruct ChoiceES
;
153 Return_Value
= RETURN_OK
;
156 GadgetText
= ComposeGadgetText(Gadgets
);
160 BodyText
= FilterBodyText(Body
);
164 /* Make sure we can open the requester on the specified screen.
166 * If the PubScreen argument is not specified it will contain
167 * NULL, and hence open on the Workbench screen.
169 Scr
= (struct Screen
*)LockPubScreen((UBYTE
*)PubScreen
);
172 ChoiceES
.es_StructSize
= sizeof(struct EasyStruct
);
173 ChoiceES
.es_Flags
= 0L;
174 ChoiceES
.es_Title
= Title
;
175 ChoiceES
.es_TextFormat
= BodyText
;
176 ChoiceES
.es_GadgetFormat
= GadgetText
;
178 /* Open the requester.
180 Result
= EasyRequestArgs(Scr
->FirstWindow
, &ChoiceES
, NULL
, NULL
);
183 args
[0] = (IPTR
)Result
;
185 VPrintf("%ld\n", args
);
189 Return_Value
= RETURN_FAIL
;
190 PrintFault(ERROR_NO_FREE_STORE
, ERROR_HEADER
);
192 UnlockPubScreen(NULL
, Scr
);
196 Return_Value
= RETURN_FAIL
;
197 PrintFault(ERROR_NO_FREE_STORE
, ERROR_HEADER
);
205 } /* Do_RequestChoice */
208 // Combine gadget strings and separate them by "|".
209 // Replace all "%" by "%%" because EasyRequest
210 // uses printf-like formatting.
211 static STRPTR
ComposeGadgetText(STRPTR
* Gadgets
)
213 STRPTR GadgetText
, BufferPos
;
214 int GadgetLength
= 0;
220 for (CurrentGadget
= 0; Gadgets
[CurrentGadget
]; CurrentGadget
++)
222 StrLen
= strlen(Gadgets
[CurrentGadget
]);
223 GadgetLength
+= StrLen
+ 1;
226 for (i
= 0; i
< StrLen
; i
++)
228 if (Gadgets
[CurrentGadget
][i
] == '%')
235 GadgetLength
+= PercentCnt
;
237 GadgetText
= AllocVec(GadgetLength
, MEMF_ANY
);
240 PrintFault(ERROR_NO_FREE_STORE
, ERROR_HEADER
);
244 BufferPos
= GadgetText
;
245 for (CurrentGadget
= 0; Gadgets
[CurrentGadget
]; CurrentGadget
++)
247 int LabelLength
= strlen(Gadgets
[CurrentGadget
]);
249 for (i
= 0; i
< LabelLength
; i
++)
251 if (Gadgets
[CurrentGadget
][i
] == '%')
260 *BufferPos
= Gadgets
[CurrentGadget
][i
];
264 if (Gadgets
[CurrentGadget
+ 1])
275 } /* ComposeGadgetText */
278 // Replace % by %% because EasyRequest uses
279 // printf-like formatting characters.
280 // Result string must be freed with FreeVec().
281 static STRPTR
FilterBodyText(STRPTR Body
)
284 int GadgetLength
= 0;
292 // Count % characters
293 StrLen
= strlen(Body
);
294 for (i
= 0; i
< StrLen
; i
++)
302 GadgetLength
= StrLen
+ PercentCnt
+ 1;
304 GadgetText
= AllocVec(GadgetLength
, MEMF_ANY
);
307 PrintFault(IoErr(), ERROR_HEADER
);
311 for (i
= 0, j
= 0; i
< StrLen
; i
++)
315 GadgetText
[j
++] = '%';
316 GadgetText
[j
++] = '%';
320 GadgetText
[j
++] = Body
[i
];
324 GadgetText
[j
] = '\0';
328 } /* FilterBodyText */