home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432a.lha / ReqLibrary_v2.2 / showoffreq.c < prev    next >
C/C++ Source or Header  |  1990-11-10  |  14KB  |  462 lines

  1.  
  2. #include    <intuition/intuition.h>
  3. #include    <exec/libraries.h>
  4. #include    <libraries/dosextens.h>        /* For the process structure definition. */
  5.  
  6.  
  7. #include    <libraries/dos.h>
  8.  
  9. #include    <libraries/reqbase.h>
  10.  
  11. #ifdef    LATTICE
  12. #include    <proto/all.h>
  13. #else
  14. struct Library *        OpenLibrary();
  15. struct Screen *            OpenScreen();
  16. struct Window *            OpenWindow();
  17. struct Task *            FindTask();
  18. struct Message *        GetMsg();
  19. #endif
  20.  
  21.  
  22. #define    WIDTH  640
  23. #define    HEIGHT 200
  24. #define    DEPTH  3
  25.  
  26. #define    GADGETSTARTX    20
  27. #define    GADGETSTARTY    60
  28.  
  29. #define    IDCMP    (GADGETUP)
  30. #define    FLAGS    (BACKDROP|SMART_REFRESH|ACTIVATE|BORDERLESS)
  31.  
  32. struct IntuitionBase    *IntuitionBase;
  33. struct GfxBase            *GfxBase;
  34. struct ReqBase            *ReqBase;
  35. struct Window            *window;
  36. struct Screen            *screen;
  37.  
  38. struct NewScreen ns =
  39.     {
  40.     0, 0,                    /* LeftEdge, TopEdge */
  41.     WIDTH, HEIGHT, DEPTH,    /* Width, Height, Depth */
  42.     0, 1,                    /* DetailPen, BlockPen */
  43.     HIRES,                    /* ViewModes */
  44.     CUSTOMSCREEN,            /* Type */
  45.     NULL,                    /* Font */
  46.     (UBYTE *)"Show off requester library.",    /* Title */
  47.     NULL,                    /* Gadgets */
  48.     NULL                    /* Bitmap */
  49.     };
  50.  
  51. struct NewWindow nw =
  52.     {
  53.     0,0,                    /* LeftEdge, TopEdge */
  54.     WIDTH,HEIGHT,            /* Width, Height */
  55.     -1,-1,                    /* DetailPen, BlockPen */
  56.     IDCMP,                    /* IDCMPFlags */
  57.     FLAGS,                    /* Flags */
  58.     NULL,                    /* FirstGadget */
  59.     NULL,                    /* CheckMark */
  60.     NULL,                    /* Title */
  61.     NULL,                    /* Screen */
  62.     NULL,                    /* BitMap */
  63.     79, 30,                    /* MinWidth, MinHeight */
  64.     640, 200,                /* MaxWidth, MaxHeight */
  65.     CUSTOMSCREEN            /* Type (of screen) */
  66.     };
  67.  
  68.  
  69. struct Process    *myprocess;
  70. APTR    olderrorwindow;
  71.  
  72.  
  73.  
  74.     /* I declared this variable as global, rather than just declaring it as */
  75.     /* a regular local variable because I need to make sure that all of the */
  76.     /* fields are initialized to zero. */
  77.     /* Also, the quit_cleanup routine needs to be able to access it to purge */
  78.     /* the directory buffers which I have chosen to use (by setting the */
  79.     /* FRQCACHINM flag. */
  80.  
  81. struct FileRequester    MyFileReqStruct;
  82. char filename[FCHARS];
  83. char directoryname[DSIZE];
  84.  
  85. void ShowFileRequester()
  86.     {
  87.     char    answerarray[DSIZE+FCHARS];
  88.  
  89.     answerarray[0] = 0;
  90.  
  91.         /* Initialize the 'PathName' field so that the file requester will */
  92.         /* construct a complete path name for me.  It will also put the two */
  93.         /* parts of the answer (directory and file name) into the directory */
  94.         /* file name which I also decided to supply it with.  Since the */
  95.         /* directory and file name arrays are present, it will use their */
  96.         /* initial contents as the initial file and directory.  If they aren't */
  97.         /* present it will leave both blank to start. */
  98.     MyFileReqStruct.PathName = answerarray;
  99.     MyFileReqStruct.Dir = directoryname;
  100.     MyFileReqStruct.File = filename;
  101.  
  102.         /* The directory caching of this file requester is one of its nice */
  103.         /* features, so I decided to show it off.  It is completely optional */
  104.         /* though, so if you don't want it, don't set this flag.  If you do */
  105.         /* want it, don't forget to call PurgeFiles() when you are done. */
  106.     MyFileReqStruct.Flags = FRQCACHINGM;
  107.  
  108.         /* Initialize a few colour fields.  Not strictly necessary, but */
  109.         /* personally, I like having my files a different colour from my */
  110.         /* directories. */
  111.     MyFileReqStruct.dirnamescolor = 2;
  112.     MyFileReqStruct.devicenamescolor = 2;
  113.         /* I could also make it larger, pass it a file and/or directory */
  114.         /* name, set the window title, set various flags and customize */
  115.         /* in many other ways, but I wanted to show that it can be easily */
  116.         /* used without having to fill in a lot of fields. */
  117.     if (FileRequester(&MyFileReqStruct))
  118.         SimpleRequest("You selected the file '%s'.", answerarray);
  119.     else
  120.         SimpleRequest("You didn't select a file.");
  121.     }
  122.  
  123.  
  124.  
  125. void ShowColorRequester()
  126.     {
  127.     ColorRequester(1L);        /* Pass it the color you want to start out being highlit. */
  128.                             /* It actually returns the colour that the user chose, but */
  129.                             /* I'm not interested in that value. */
  130.  
  131.         /* If you want to give more parameters to the colour requester, you can */
  132.         /* call ExtendedColorRequester with the address of an ExtendedColorRequest */
  133.         /* structure.  Currently the only additional parameter this lets you pass */
  134.         /* is the address of the window the requester should appear in.  This can */
  135.         /* normally be done more easily by specifying it in the pr_WindowPtr field */
  136.         /* of your task structure. */
  137.     }
  138.  
  139.  
  140.  
  141. void ShowSimpleText()
  142.     {
  143.     SimpleRequest(
  144. "     SimpleRequest()  is  a  tiny bit of\n"
  145. "glue  code  which  passes  a single text\n"
  146. "string  (with  optional  printf()  style\n"
  147. "formatting) to the TextRequest() routine\n"
  148. "in  the  library.   The  SimpleRequest()\n"
  149. "routine  can  be  easily modified to fit\n"
  150. "your own peculiar purposes.");
  151.     }
  152.  
  153.  
  154.  
  155. char *yesno[] =
  156.     {
  157.     "no",
  158.     "yes",
  159.     };
  160.  
  161. void ShowTwoGadText()
  162.     {
  163.     short    result;
  164.  
  165.     result = TwoGadRequest("Just testing the two gadget requester.");
  166.     SimpleRequest("You responded with a '%s' to this requester.", yesno[result]);
  167.     }
  168.  
  169.  
  170.  
  171. char *response[] =
  172.     {
  173.     "You really should use it.",
  174.     "Excellent choice.  You have good taste.",
  175.     "Oh come one, make up your mind.\nYou won't regret choosing 'yes'.",
  176.     };
  177.  
  178. void ShowThreeGadText()
  179.     {
  180.     struct TRStructure    MyTextStruct;
  181.     short    result;
  182.  
  183.     MyTextStruct.Text = "     Would you use the requester library\n"
  184.                         "in your programs?";
  185.         /* The Controls field doesn't actually have to be initialized, unless you */
  186.         /* are using printf() style formatting of the Text string. */
  187.     MyTextStruct.Window = 0;    /* This must be zero or a valid window pointer. */
  188.     MyTextStruct.MiddleText = "Perhaps...";
  189.     MyTextStruct.PositiveText = "Oh yeah, for sure!";
  190.     MyTextStruct.NegativeText = "Methinks not.";
  191.     MyTextStruct.Title = "Show off text requester.";
  192.     MyTextStruct.KeyMask = AMIGAKEYS;        /* Allow only keys together with the amiga */
  193.                                             /*  qualifiers to be used for keyboard shortcuts. */
  194.     MyTextStruct.textcolor = 1;
  195.     MyTextStruct.detailcolor = 0;
  196.     MyTextStruct.blockcolor = 1;
  197.     MyTextStruct.versionnumber = 0;
  198.     MyTextStruct.rfu1 = 0;
  199.     MyTextStruct.rfu2 = 0;
  200.     result = TextRequest(&MyTextStruct);
  201.     SimpleRequest(response[result]);
  202.     }
  203.  
  204.  
  205.  
  206.     /* I declared this variable globally, rather than just declaring it as */
  207.     /* a regular local variable because I need to make sure that all of the */
  208.     /* fields are initialized to zero. */
  209.     /* Also, the quit_cleanup routine needs to be able to access it to purge */
  210.     /* the directory buffers which I have chosen to use (by setting the */
  211.     /* FRQCACHINM flag. */
  212.     /* I could have used the same structure I used for the file requester, */
  213.     /* but I decided not to, so that the two requesters could have separate */
  214.     /* directory caches. */
  215.  
  216. struct FileRequester    MyFontReqStruct;
  217.  
  218. void ShowFontRequester()
  219.     {
  220.     char    fontname[FCHARS];
  221.     char    dirname[DSIZE];
  222.  
  223.         /* You do have to tell the font requester what directory to look */
  224.         /* in, usually fonts:. */
  225.  
  226.     strcpy(dirname, "fonts:");
  227.     fontname[0] = 0;
  228.  
  229.         /* Tell the file requester to be a font requester. */
  230.         /* The directory caching of this file requester is one of its nice */
  231.         /* features, so I decided to show it off.  It is completely optional */
  232.         /* though, so if you don't want it, don't set the caching flag.  If you do */
  233.         /* want it, don't forget to call PurgeFiles() when you are done. */
  234.     MyFontReqStruct.Flags |= FRQGETFONTSM | FRQCACHINGM;
  235.  
  236.     MyFontReqStruct.Dir = dirname;
  237.     MyFontReqStruct.File = fontname;
  238.         /* Initialize a colour field.  Not strictly necessary, but */
  239.         /* personally, I like having my fonts a different colour */
  240.         /* from the font size list. */
  241.     MyFontReqStruct.fontnamescolor = 2;
  242.     if (FileRequester(&MyFontReqStruct))
  243.         SimpleRequest("You selected the font '%s',\n"
  244.                       "size %d, style %d.", fontname, MyFontReqStruct.FontYSize, MyFontReqStruct.FontStyle);
  245.     else
  246.         SimpleRequest("You didn't select a font.");
  247.     }
  248.  
  249.  
  250.  
  251. #define    TEXTLENGTH    75
  252.  
  253. void ShowGetText()
  254.     {
  255.     char    mybuffer[TEXTLENGTH];
  256.  
  257.         /* Note that the buffer you pass to GetString must be initialized.  */
  258.         /* If you don't want any text to appear by default, just put a zero */
  259.         /* at the beginning of the array. */
  260.  
  261.     strcpy(mybuffer, "The default text.");
  262.     if (GetString(mybuffer, "Type anything, then hit return.", (struct Window *)0L, 50L, (long)TEXTLENGTH))
  263.         SimpleRequest("I'll bet you typed:\n"
  264.                       "'%s'.", mybuffer);
  265.     else
  266.         SimpleRequest("You didn't enter anything!");
  267.     }
  268.  
  269.  
  270.  
  271. void ShowGetLong()
  272.     {
  273.     struct GetLongStruct    mygetlongstruct;
  274.  
  275.     mygetlongstruct.titlebar = "Enter a number.";
  276.     mygetlongstruct.defaultval = 1234;
  277.     mygetlongstruct.minlimit = -100000;
  278.     mygetlongstruct.maxlimit = 100000;
  279.     mygetlongstruct.window = 0;            /* Must be zeroed or req.lib will use it as a window pointer. */
  280.     mygetlongstruct.versionnumber = 0;
  281.     mygetlongstruct.flags = 0;
  282.     mygetlongstruct.rfu2 = 0;
  283.     if (GetLong(&mygetlongstruct))
  284.         SimpleRequest("You entered the number '%ld'.", mygetlongstruct.result);
  285.     else
  286.         SimpleRequest("You didn't enter a number.");
  287.     }
  288.  
  289.  
  290.  
  291. void quit_cleanup(message)
  292. char *message;
  293.     {
  294.     if (window)
  295.         CloseWindow(window);
  296.     if (screen)
  297.         CloseScreen(screen);
  298.     if (ReqBase)
  299.         {
  300.         PurgeFiles(&MyFileReqStruct);    /* Only necessary if the FRQCACHINGM flag */
  301.         PurgeFiles(&MyFontReqStruct);    /* is set in the file requester structure. */
  302.         CloseLibrary(ReqBase);
  303.         }
  304.     if (GfxBase)
  305.         CloseLibrary(GfxBase);
  306.     if (IntuitionBase)
  307.         CloseLibrary(IntuitionBase);
  308.     if (message)
  309.         puts(message);
  310.     exit(0);
  311.     }
  312.  
  313.  
  314.  
  315. void _abort()
  316.     {
  317.     quit_cleanup("User abort requested.");
  318.     }
  319.  
  320.  
  321.  
  322. void bye()
  323.     {
  324.     myprocess->pr_WindowPtr = olderrorwindow;
  325.     quit_cleanup((char *)0);
  326.     }
  327.  
  328.  
  329.  
  330. struct gadgetandfunction
  331.     {
  332.     char    *gadgettext;
  333.     void    (*functionpointer)();
  334.     };
  335.  
  336. struct gadgetandfunction gadgetlist[] =
  337.     {
  338.         {
  339.         "Show the file requester.",
  340.         ShowFileRequester,
  341.         },
  342.         {
  343.         "Show the color requester.",
  344.         ShowColorRequester,
  345.         },
  346.         {
  347.         "Show a simple text requester.",
  348.         ShowSimpleText,
  349.         },
  350.         {
  351.         "Show a two gadget requester.",
  352.         ShowTwoGadText,
  353.         },
  354.         {
  355.         "Show a three gadget requester.",
  356.         ShowThreeGadText,
  357.         },
  358.         {
  359.         "Show the font requester.",
  360.         ShowFontRequester,
  361.         },
  362.         {
  363.         "Show the 'get text' requester.",
  364.         ShowGetText,
  365.         },
  366.         {
  367.         "Show the 'get number' requester.",
  368.         ShowGetLong,
  369.         },
  370.  
  371.         {
  372.         "Exit the demo.",
  373.         bye,
  374.         },
  375.     };
  376.  
  377. #define    NUMGADGETS    (sizeof(gadgetlist) / sizeof(struct gadgetandfunction))
  378.  
  379. struct GadgetBlock    gadgetblocks[NUMGADGETS];
  380.  
  381. void main()
  382.     {
  383.     short    x, y, gadgetnum;
  384.     struct IntuiMessage    messagecopy, *message;
  385.     struct Gadget        *gadgetpointer;
  386.  
  387.         /* First we do the usual opening up of libraries, pretty standard stuff. */
  388.  
  389.     if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 33L)))
  390.         quit_cleanup("Requires 1.2 operating system.");
  391.  
  392.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L)))
  393.         quit_cleanup("Couldn't open graphics.");
  394.  
  395.     if (!(ReqBase = (struct ReqBase *)OpenLibrary("req.library", 0L)))
  396.         quit_cleanup("Couldn't open the req library.");
  397.  
  398.         /* Then we open up a nice 640 by 200, three bitplane screen.  This demo */
  399.         /* can be easily modified to work on the workbench screen.  Simply */
  400.         /* take out the open screen command, and change the NewWindow Type field */
  401.         /* to WBENCHSCREEN.  All of the requesters will then open on the */
  402.         /* workbench screen. */
  403.  
  404.     if (!(screen = (struct Screen *)OpenScreen(&ns)))
  405.         quit_cleanup("Couldn't open screen.");
  406.  
  407.     for (gadgetnum = 0; gadgetnum < NUMGADGETS; gadgetnum++)
  408.         {
  409.         x = GADGETSTARTX + (gadgetnum & 1) * (WIDTH / 2);
  410.         y = GADGETSTARTY + (gadgetnum / 2) * 20;
  411.         LinkGadget(&gadgetblocks[gadgetnum], gadgetlist[gadgetnum].gadgettext, &nw, (long)x, (long)y);
  412.         gadgetblocks[gadgetnum].Gadget.GadgetID = gadgetnum;
  413.         }
  414.  
  415.         /* Find the address of the last gadget added, the 'goodbye' gadget */
  416.         /* and adjust it so that it appears in the middle at the top. */
  417.     gadgetpointer = &gadgetblocks[NUMGADGETS-1].Gadget;
  418.     gadgetpointer->TopEdge = 20;
  419.     gadgetpointer->LeftEdge = (WIDTH - gadgetpointer->Width) / 2;
  420.  
  421.     nw.Screen = screen;
  422.     if (!(window = (struct Window *)OpenWindow(&nw)))
  423.         quit_cleanup("Couldn't open window.");
  424.  
  425.         /* Now we set the pr_WindowPtr field in our process structure to */
  426.         /* point at our window so that DOS requesters and requester library */
  427.         /* requestes will open up on our custom screen, instead of on the */
  428.         /* workbench screen. NOTE!!!!!  It is VERY important that you restore */
  429.         /* this variable before the program exits.  This is why I make a copy */
  430.         /* of the previous value before I change it.  IF YOU DON'T RESTORE IT */
  431.         /* when you exit, NASTY THINGS WILL HAPPEN!! (but only sometimes).  */
  432.         /* If you execute a program WITHOUT using the 'run' command and it */
  433.         /* sets the pr_WindowPtr to point at it's window, and then exits */
  434.         /* without restoring it, the next DOS requester for that process will */
  435.         /* try to come up on a screen that NO LONGER EXISTS - which will probably */
  436.         /* BLOW UP YOUR MACHINE!  The pr_WindowPtr is restored in the bye() */
  437.         /* routine. */
  438.  
  439.     myprocess = (struct Process *)FindTask((char *)0);
  440.     olderrorwindow = myprocess->pr_WindowPtr;
  441.     myprocess->pr_WindowPtr = (APTR)window;
  442.  
  443.         /* Now we enter our main processing loop.  All we do is wait for */
  444.         /* GADGETUP messages (the only kind the IDCMP port is set to */
  445.         /* receive) and call the function associated with each gadget */
  446.         /* that the user presses.  One of those gadgets is an exit */
  447.         /* gadget which calls the 'bye' routine, which frees up all */
  448.         /* resources and exits.  This is how this loop, which seems to */
  449.         /* never terminate, terminates. */
  450.  
  451.     while (TRUE)
  452.         {
  453.         WaitPort(window->UserPort);
  454.         message = (struct IntuiMessage *)GetMsg(window->UserPort);
  455.         messagecopy = *message;
  456.         ReplyMsg((struct Message *)message);
  457.         gadgetnum = ((struct Gadget *)(messagecopy.IAddress))->GadgetID;
  458.  
  459.         gadgetlist[gadgetnum].functionpointer();
  460.         }
  461.     }
  462.