home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AmigActive 13
/
AACD13.ISO
/
AACD
/
Resources
/
System
/
BoingBag1
/
Contributions
/
Workbench
/
RexxArpLib3p6
/
src
/
sreq
/
simpreq.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-06-17
|
9KB
|
414 lines
/** simpreq.c
*
*
* SimpleRequest(1.0) ARP Programmers Manual SimpleRequest(1.0)
*
*
*
* NAME
* SimpleRequest -- Get string and/or yes/no answer from user
*
* SYNOPSIS
* result = SimpleRequest( SimpleRequester )
* D0 A0
*
* FUNCTION
* This function displays a set of prompts and allows the user to
* give an answer in an Intuition style requester, and returns it
* to you. The requester may contain a string gadget and/or one or
* two boolean gadgets, the latter in the standard "Okay" and
* "Cancel" positions, but with selectable text strings. The
* requester is auto-sizing: it determines its size based on the
* number of non-NULL prompts and on the presence or absence of the
* various gadgets and their sizes. The requester always comes with
* a standard intuition "close" button, who's meaning is equivalent
* to "Cancel".
*
* INPUTS
* SimpleRequester -- A pointer to an initialized SimpleRequester
* structure.
*
* RESULT
* result -- 0 if "Cancel" or "Close" was selected or if
* an error was detected. In that case the error value may be
* obtained from the SimpleRequester structure.
* 1 if "Okay" was selected or, in case neither "Okay"
* nor "Cancel" gadget is present, when a <cr> is typed by the
* user while in the string gadget.
*
* ADDITIONAL CONSIDERATIONS
* The resulting string will overwrite the one given in the
* SimpleRequester structure, unless the user clicks on "Cancel"
* or "Close".
*
* BUGS
* None reported sofar.
*
* AUTHOR
* W.G.J. Langeveld (WGL)
*
*
**/
#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuitionbase.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <functions.h>
#include <simpreq.h>
#include "simpdefs.h"
static struct ReqPens defaultpens =
{
DEFWINBPEN, DEFWINDPEN, DEFBACKPEN,
DEFPROMPEN, DEFBOXPEN , DEFSHADPEN,
DEFOKAYPEN, DEFCANCPEN, NULL
};
extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;
long SimpleRequest( struct SimpleRequester *s )
{
UWORD code;
BOOL quit_flag;
WORD vsize,
i,
hsize,
temp,
stry,
booly,
olen,
clen,
boolheight;
ULONG class,
idcmpflags,
flags;
long result = NULL;
struct Gadget *SREQString,
*SREQOkay,
*SREQCancel,
*firstgadget,
*object;
struct Window *w = NULL;
struct IntuiMessage *message;
struct ReqPens *reqp;
ULONG ec = 0;
SREQString = SREQOkay = SREQCancel = firstgadget = object = NULL;
/*
* Return if no request.
*/
if (s == NULL)
goto cleanup;
s->sr_ErrorCode = SR_SUCCESS;
/*
* Set up PenList
*/
reqp = s->sr_PenList;
if (reqp == NULL)
reqp = &defaultpens;
/*
* Get the dimensions of the window.
* vsize is the height, hsize the width of the window.
*
* First, make hsize the length of the longest prompt line, and increment
* vsize along the way.
*/
hsize = MINWINWIDTH;
for (i = 0, vsize = MINWINHEIGHT; s->sr_Prompt[i] && i < SR_MAXPROMPTS;
i++, vsize += LINEHEIGHT)
{
if ((temp = CHARWIDTH * strlen(s->sr_Prompt[i])) > hsize)
hsize = temp;
}
/*
* Add spacing for string gadget if it exists, and save the vertical
* position of it in stry. Then add height of string gadget.
*/
if (s->sr_String)
{
vsize += VSKIP;
stry = vsize;
vsize += STRHEIGHT;
}
/*
* Add spacing for Okay or Cancel if any exists, and save the vertical
* position of them in booly.
*/
if (s->sr_Okay || s->sr_Cancel)
{
vsize += SMALLVSKIP;
booly = vsize;
}
/*
* Make the boolean gadgets
*/
if (s->sr_Okay)
{
SREQOkay = MakeSBoolGad( s->sr_Okay,
reqp->reqp_BoxPen,
reqp->reqp_ShadowPen,
reqp->reqp_OkayPen,
VBORDER,
booly,
3,
2,
2,
&ec); /* booly OK */
}
if (s->sr_Cancel)
{
SREQCancel = MakeSBoolGad( s->sr_Cancel,
reqp->reqp_BoxPen,
reqp->reqp_ShadowPen,
reqp->reqp_CancelPen,
0,
booly,
3,
2,
3,
&ec); /* booly OK */
}
/*
* olen, and clen are the widths of the string gadget's default
* string and the Okay and Cancel gadget's text.
*/
olen = clen = 0;
if (s->sr_Okay)
olen = SREQOkay->Width;
if (s->sr_Cancel)
clen = SREQCancel->Width;
/*
* Adjust the width if necessary to fit the two boolean gadgets next to
* one another. The ize of the default string in the string gadget
* is not taken into account, since if it is too long it can cause the
* window to not open, and the size is unpredictable.
*/
temp = olen + clen;
if (olen && clen)
temp += BOOLMINSPACE;
if (temp > hsize)
hsize = temp;
/*
* Make the string gadget
*/
if (s->sr_String)
{
temp = hsize;
if ((reqp->reqp_BackPen == 0) &&
(IntuitionBase->LibNode.lib_Version >= 36)) temp = -hsize;
SREQString = MakeSStrGad( s->sr_String,
reqp->reqp_BoxPen,
reqp->reqp_ShadowPen,
VBORDER,
stry, /* stry OK */
temp,
1,
&ec);
}
/*
* Add space for the vertical borders of the requester.
*/
hsize += 2 * VBORDER;
/*
* Now adjust the Cancel gadget's position
*/
if (SREQCancel)
{
SREQCancel->LeftEdge = hsize - VBORDER - SREQCancel->Width;
}
/*
* Add vertical space for the boolean gadgets.
*/
temp = 0;
boolheight = 0;
if (s->sr_Okay)
temp = SREQOkay->Height;
if (s->sr_Cancel)
boolheight = SREQCancel->Height;
if (temp > boolheight)
boolheight = temp;
vsize += boolheight;
/*
* Add a bit more space, and we have the window height in vsize.
*/
vsize += VSKIP;
/*
* Open the window
*/
idcmpflags = GADGETUP + GADGETDOWN + CLOSEWINDOW + VANILLAKEY;
flags = WINDOWDRAG + WINDOWCLOSE + ACTIVATE + WINDOWDEPTH;
w = OpenSReqWindow(s, hsize, vsize, idcmpflags, flags);
if (w == NULL)
goto cleanup;
/*
* Add the prompts
*/
UpdateMsgWindow(w, s);
/*
* Add string gadget. 'firstgadget' will contain a pointer to the first
* gadget.
*/
if (s->sr_String)
{
if (SREQString == NULL)
{
s->sr_ErrorCode = ec;
goto cleanup;
}
AddGadget(w, SREQString, -1L);
firstgadget = SREQString;
}
/*
* Add Okay gadget
*/
if (s->sr_Okay)
{
if (SREQOkay == NULL)
{
s->sr_ErrorCode = ec;
goto cleanup;
}
AddGadget(w, SREQOkay, -1L);
if (firstgadget == NULL)
firstgadget = SREQOkay;
}
/*
* Add Cancel gadget
*/
if (s->sr_Cancel)
{
if (SREQCancel == NULL)
{
s->sr_ErrorCode = ec;
goto cleanup;
}
AddGadget(w, SREQCancel, -1L);
if (firstgadget == NULL)
firstgadget = SREQCancel;
}
/*
* Refresh the gadgets
*/
if (firstgadget)
{
RefreshGadgets(firstgadget, w, NULL);
if (SREQString)
ActivateGadget(SREQString, w, NULL);
}
/*
* Wait for gadget activity
*/
quit_flag = FALSE;
do
{
WaitPort(w->UserPort);
while( (message = (struct IntuiMessage *)
GetMsg(w->UserPort) ) != NULL)
{
code = message->Code;
object = (struct Gadget *) message->IAddress;
class = message->Class;
ReplyMsg((struct Message *) message);
if ( class == CLOSEWINDOW )
quit_flag = TRUE;
if ( (class == GADGETUP) || (class == GADGETDOWN) )
{
switch (object->GadgetID)
{
case 1:
if (s->sr_Okay == NULL)
{
quit_flag = TRUE;
result = 1;
}
break;
case 2:
result = 1;
quit_flag = TRUE;
break;
case 3:
result = 0;
quit_flag = TRUE;
break;
default:
break;
}
}
if ( class == VANILLAKEY )
{
if (code == 'v' || code == 'V')
{
if (s->sr_Okay)
{
result = 1;
quit_flag = TRUE;
}
}
else
if (code == 'b' || code == 'B')
{
if (s->sr_Cancel)
{
result = 0;
quit_flag = TRUE;
}
}
}
}
}
while (quit_flag == FALSE);
/*
* Do we have a new string?
*/
if (result && SREQString)
{
strcpy(s->sr_String,
((struct StringInfo *) (SREQString->SpecialInfo))->Buffer);
}
/*
* Clean up the mess.
*/
cleanup:
if (w)
{
if (SREQString)
RemoveGadget(w, SREQString);
if (SREQOkay)
RemoveGadget(w, SREQOkay);
if (SREQCancel)
RemoveGadget(w, SREQCancel);
}
if (SREQString)
FreeSimpGad(SREQString);
if (SREQOkay)
FreeSimpGad(SREQOkay);
if (SREQCancel)
FreeSimpGad(SREQCancel);
if (w)
{
while( message = (struct IntuiMessage *) GetMsg(w->UserPort) )
ReplyMsg((struct Message *) message);
CloseWindow(w);
}
return(result);
}