Added Text2PDF, a tool which converts text files
[AROS-Contrib.git] / bgui / request.c
blobf459a0877c102f9cf6ae552ae3fa6ec10f43018c
1 /*
2 * @(#) $Header$
4 * BGUI library
5 * request.c
7 * (C) Copyright 1998 Manuel Lemos.
8 * (C) Copyright 1996-1997 Ian J. Einman.
9 * (C) Copyright 1993-1996 Jaba Development.
10 * (C) Copyright 1993-1996 Jan van den Baard.
11 * All Rights Reserved.
13 * $Log$
14 * Revision 42.5 2004/06/16 20:16:48 verhaegs
15 * Use METHODPROTO, METHOD_END and REGFUNCPROTOn where needed.
17 * Revision 42.4 2003/01/18 19:10:00 chodorowski
18 * Instead of using the _AROS or __AROS preprocessor symbols, use __AROS__.
20 * Revision 42.3 2000/08/09 11:45:57 chodorowski
21 * Removed a lot of #ifdefs that disabled the AROS_LIB* macros when not building on AROS. This is now handled in contrib/bgui/include/bgui_compilerspecific.h.
23 * Revision 42.2 2000/05/15 19:27:02 stegerg
24 * another hundreds of REG() macro replacements in func headers/protos.
26 * Revision 42.1 2000/05/14 23:32:48 stegerg
27 * changed over 200 function headers which all use register
28 * parameters (oh boy ...), because the simple REG() macro
29 * doesn't work with AROS. And there are still hundreds
30 * of headers left to be fixed :(
32 * Many of these functions would also work with stack
33 * params, but since i have fixed every single one
34 * I encountered up to now, I guess will have to do
35 * the same for the rest.
37 * Revision 42.0 2000/05/09 22:10:03 mlemos
38 * Bumped to revision 42.0 before handing BGUI to AROS team
40 * Revision 41.11 2000/05/09 19:54:59 mlemos
41 * Merged with the branch Manuel_Lemos_fixes.
43 * Revision 41.10 1998/02/25 21:12:59 mlemos
44 * Bumping to 41.10
46 * Revision 1.1 1998/02/25 17:09:37 mlemos
47 * Ian sources
52 #include "include/classdefs.h"
55 * IDCMPHook data.
57 typedef struct {
58 struct bguiRequest *ihd_Request; /* Requester structure. */
59 Object *ihd_ReturnObj; /* Return key object. */
60 ULONG ihd_MaxID; /* Maximum gadget ID. */
61 } IHD;
64 * Parse a button label.
66 STATIC ASM UBYTE *GetButtonName( REG(a0) UBYTE *source, REG(a1) UBYTE *uc, REG(d0) ULONG uchar )
68 uc[0] = uc[1] = 0;
71 * Search for the end-of-string or
72 * the seperator character.
74 while (*source && *source != '|')
77 * Look up the character
78 * to underline.
80 if (*source == uchar)
82 uc[0] = tolower(source[1]);
84 source++;
88 * When we reached a seperator we
89 * truncate the gadget string and
90 * return a pointer to the next
91 * name.
93 if (*source == '|')
95 *source++ = 0;
96 return source;
100 * The end, finito, ende, einde....
102 return NULL;
106 * Create the button group.
108 STATIC Object *CreateButtonGroup( UBYTE *gstring, ULONG xen, UBYTE uchar, ULONG *maxid, Object **retobj )
110 Object *master, *button;
111 UBYTE *ptr, underscore[ 2 ];
112 UWORD id = 2, style = FS_NORMAL;
113 BOOL sing = FALSE, got_def = FALSE;
116 * Single gadget?
118 if (!strchr( gstring, '|'))
119 sing = TRUE;
122 * Create a horizontal master group.
124 if ((master = BGUI_NewObject(BGUI_GROUP_GADGET, GROUP_Spacing, GRSPACE_WIDE, GROUP_EqualWidth, TRUE, TAG_END)))
127 * Add a spacing object for single
128 * gadget requesters.
130 if (sing)
132 if (!AsmDoMethod(master, GRM_ADDSPACEMEMBER, DEFAULT_WEIGHT))
133 goto fail;
137 * Loop to get all the gadgets
138 * names.
140 while ((ptr = gstring))
143 * Get a pointer to the next
144 * gadget name.
146 gstring = GetButtonName( gstring, &underscore[ 0 ], uchar );
149 * Default button?
151 if ((*ptr == '*') && (!got_def))
153 ptr++;
154 got_def = TRUE;
155 style = FSF_BOLD;
159 * Create the button.
161 if ((button = BGUI_NewObject(BGUI_BUTTON_GADGET, LAB_Label, ptr, LAB_Style, style, LAB_Underscore, uchar,
162 GA_ID, id++, BT_Key, underscore, xen ? FRM_Type : TAG_IGNORE, FRTYPE_XEN_BUTTON, TAG_DONE)))
165 * Pick this button if it's the first
166 * created or the default button.
168 if ((id == 3) || (style == FSF_BOLD))
169 *retobj = button;
172 * Add it to the master group.
174 if ( ! AsmDoMethod( master, GRM_ADDMEMBER, button, LGO_FixMinHeight, TRUE, TAG_END ))
175 goto fail;
178 else
181 * Button creation failed.
183 fail:
184 DisposeObject( master );
185 return NULL;
188 * Reset style.
190 style = FS_NORMAL;
194 * Save the maximum gadget ID.
196 *maxid = id - 2;
199 * Add a spacing object for single
200 * gadget requesters.
202 if (sing)
204 if (!AsmDoMethod(master, GRM_ADDSPACEMEMBER, DEFAULT_WEIGHT))
205 goto fail;
209 * The last (right most) button get's
210 * 0 as it's ID to keep compatible with
211 * intuition's EasyRequest().
213 DoSetMethod(button, NULL, GA_ID, 1, TAG_END);
216 * Return the master group.
218 return master;
220 return NULL;
224 * Requester IDCMP hook.
226 //STATIC SAVEDS ASM VOID ReqHookFunc(REG(a0) struct Hook *hook, REG(a2) Object *window, REG(a1) struct IntuiMessage *msg)
227 STATIC SAVEDS ASM REGFUNC3(IPTR, ReqHookFunc,
228 REGPARAM(A0, struct Hook *, hook),
229 REGPARAM(A2, Object *, window),
230 REGPARAM(A1, struct IntuiMessage *, msg))
232 IHD *ihd = (IHD *)hook->h_Data;
233 int id = 0;
236 * Which key was pressed?
238 switch (msg->Code)
241 * Left-Amiga+V?
243 case 0x34:
244 if (msg->Qualifier & IEQUALIFIER_LCOMMAND)
245 id = (ihd->ihd_MaxID == 1) ? 1 : 2;
246 break;
249 * Left-Amiga+B?
251 case 0x35:
252 if (msg->Qualifier & IEQUALIFIER_LCOMMAND)
253 id = 1;
254 break;
257 * Return/Enter?
259 case 0x43:
260 case 0x44:
261 if (ihd->ihd_Request->br_Flags & BREQF_FAST_KEYS)
262 id = (ihd->ihd_MaxID == 1) ? 1 : GADGET(ihd->ihd_ReturnObj)->GadgetID;
263 break;
266 * Esc?
268 case 0x45:
269 if (ihd->ihd_Request->br_Flags & BREQF_FAST_KEYS)
270 id = 1;
271 break;
273 if (id) AsmDoMethod(window, WM_REPORT_ID, id, 0);
275 return 0;
277 REGFUNC_END
278 static struct Hook ReqHook = { { NULL, NULL }, (HOOKFUNC)ReqHookFunc, NULL, NULL };
281 * Put up a BGUI requester.
283 #ifdef __AROS__
284 makearosproto
285 AROS_LH3(ULONG, BGUI_RequestA,
286 AROS_LHA(struct Window *, win, A0),
287 AROS_LHA(struct bguiRequest *, es, A1),
288 AROS_LHA(IPTR *, args, A2),
289 struct Library *, BGUIBase, 7, BGUI)
290 #else
291 makeproto SAVEDS ASM ULONG BGUI_RequestA(REG(a0) struct Window *win, REG(a1) struct bguiRequest *es, REG(a2) IPTR *args)
292 #endif
294 AROS_LIBFUNC_INIT
296 Object *window;
297 struct Screen *screen;
298 IHD ihd;
299 struct Window *wptr;
300 UBYTE *gstr;
301 CONST_STRPTR wdt;
302 ULONG id, maxid = 0, rc = ~0;
303 IPTR signal;
304 APTR rlock = NULL;
305 BOOL cw = FALSE, running = TRUE;
308 * Center on the window?
310 if (( es->br_Flags & BREQF_CENTERWINDOW ) && win )
311 cw = TRUE;
314 * Get the screen.
316 screen = win ? win->WScreen : es->br_Screen;
319 * We need these strings.
321 if (es->br_TextFormat && es->br_GadgetFormat)
324 * Allocate private copy of the gadget string.
326 if ((gstr = (UBYTE *)BGUI_AllocPoolMem(strlen(es->br_GadgetFormat) + 1)))
329 * Copy the gadget string.
331 strcpy(gstr, es->br_GadgetFormat);
334 * Figure out requester title.
336 if (es->br_Title) wdt = es->br_Title;
337 else
339 if (win && win->Title) wdt = win->Title;
340 else
342 InitLocale();
343 wdt = LOCSTR(MSG_BGUI_REQUESTA_TITLE);
348 * Create a window object.
350 window = WindowObject,
351 WINDOW_Position, es->br_ReqPos,
352 cw ? WINDOW_PosRelBox : TAG_IGNORE, IBOX( &win->LeftEdge ),
353 WINDOW_Title, wdt,
354 WINDOW_CloseGadget, FALSE,
355 WINDOW_SizeGadget, FALSE,
356 WINDOW_RMBTrap, TRUE,
357 WINDOW_SmartRefresh, TRUE,
358 WINDOW_Font, es->br_TextAttr,
359 WINDOW_IDCMP, IDCMP_RAWKEY,
360 WINDOW_IDCMPHookBits, IDCMP_RAWKEY,
361 WINDOW_IDCMPHook, &ReqHook,
362 WINDOW_NoBufferRP, TRUE,
363 WINDOW_AutoAspect, es->br_Flags & BREQF_AUTO_ASPECT ? TRUE : FALSE,
364 WINDOW_AutoKeyLabel, TRUE,
366 screen ? WINDOW_Screen : TAG_IGNORE, screen,
368 WINDOW_MasterGroup,
369 VGroupObject, NormalHOffset, NormalVOffset, NormalSpacing,
370 es->br_Flags & BREQF_NO_PATTERN ? TAG_IGNORE : GROUP_BackFill, SHINE_RASTER,
371 StartMember,
372 InfoObject,
373 FRM_Type, FRTYPE_BUTTON,
374 FRM_Recessed, TRUE,
375 INFO_TextFormat, es->br_TextFormat,
376 INFO_FixTextWidth, TRUE,
377 INFO_FixTextHeight, TRUE,
378 INFO_Args, args,
379 INFO_HorizOffset, 10,
380 INFO_VertOffset, 6,
381 EndObject,
382 EndMember,
383 StartMember,
384 CreateButtonGroup(gstr, es->br_Flags & BREQF_XEN_BUTTONS, es->br_Underscore, &maxid, &ihd.ihd_ReturnObj),
385 LGO_FixMinHeight, TRUE,
386 EndMember,
387 EndObject,
388 EndObject;
390 if (window)
393 * Setup hook data.
395 ihd.ihd_Request = es;
396 ihd.ihd_MaxID = maxid;
397 ReqHook.h_Data = ( APTR )&ihd;
400 * Open the window and wait
401 * for a gadget click.
403 if ((wptr = (struct Window *)AsmDoMethod(window, WM_OPEN)))
406 * Lock parent window?
408 if ((es->br_Flags & BREQF_LOCKWINDOW) && win)
409 rlock = BGUI_LockWindow(win);
412 * Pick up signal bit.
414 Get_Attr(window, WINDOW_SigMask, &signal);
418 Wait(signal);
419 while((id = (ULONG)AsmDoMethod( window, WM_HANDLEIDCMP )) != WMHI_NOMORE)
422 * Gadget clicked?
424 if (id >= 1 && id <= maxid)
426 rc = id - 1;
427 running = FALSE;
430 } while (running);
431 AsmDoMethod(window, WM_CLOSE);
432 BGUI_UnlockWindow(rlock);
434 DisposeObject(window);
436 BGUI_FreePoolMem(gstr);
439 return rc;
441 AROS_LIBFUNC_EXIT