home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / prog / c / rqtls21b.lha / ReqTools / demo / demo.c < prev    next >
C/C++ Source or Header  |  1992-07-24  |  14KB  |  445 lines

  1. /*********************************
  2. *                                *
  3. *  reqtools.library (V38)        *
  4. *                                *
  5. *  Release 2.0                   *
  6. *                                *
  7. *  (c) 1991/1992 Nico François   *
  8. *                                *
  9. *  demo.c                        *
  10. *                                *
  11. *  This source is public domain  *
  12. *  in all respects.              *
  13. *                                *
  14. *********************************/
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/execbase.h>
  19. #include <libraries/dos.h>
  20. #include <intuition/intuition.h>
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23.  
  24. #include <stdio.h>
  25.  
  26. #include <libraries/reqtools.h>
  27. #include <proto/reqtools.h>
  28.  
  29. extern struct ExecBase *SysBase;
  30. struct ReqToolsBase *ReqToolsBase;
  31.  
  32. #define REG register
  33.  
  34. /* Callback functions
  35.     Define DOHOOKS when you compile to activate these filter functions */
  36.  
  37. BOOL __asm __saveds file_filterfunc (
  38.     REG __a0 struct Hook *, REG __a2 struct rtFileRequester *,
  39.     REG __a1 struct FileInfoBlock *
  40.     );
  41. BOOL __asm __saveds font_filterfunc (
  42.     REG __a0 struct Hook *, REG __a2 struct rtFontRequester *,
  43.     REG __a1 struct TextAttr *
  44.     );
  45. BOOL __asm __saveds vol_filterfunc (
  46.     REG __a0 struct Hook *, REG __a2 struct rtFileRequester *,
  47.     REG __a1 struct rtVolumeEntry *
  48.     );
  49.  
  50. ULONG undertag[] = { RT_Underscore, '_', TAG_END };
  51.  
  52. void myputs (char *str)
  53. {
  54.     Write (Output(), str, strlen(str));
  55. }
  56.  
  57. _main()
  58. {
  59.     struct rtFileRequester *filereq;
  60.     struct rtFontRequester *fontreq;
  61.     struct rtScreenModeRequester *scrmodereq;
  62.     struct rtFileList *flist, *tempflist;
  63.     struct Hook filterhook, font_filterhook, vol_filterhook;
  64.     char buffer[128], filename[34];
  65.     long longnum, ret, color;
  66.  
  67.     if (!(ReqToolsBase = (struct ReqToolsBase *)
  68.                                           OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))) {
  69.         myputs ("You need reqtools.library V38 or higher!\n"
  70.                   "Please install it in your Libs: drirectory.\n");
  71.         exit (0);
  72.         }
  73.  
  74.     rtEZRequest ("ReqTools 2.0 Demo\n"
  75.                  "~~~~~~~~~~~~~~~~~\n"
  76.                      "'reqtools.library' offers several\n"
  77.                      "different types of requesters:",
  78.                      "Let's see them", NULL, NULL);
  79.  
  80.     rtEZRequest ("NUMBER 1:\nThe larch :-)", "Be serious!", NULL, NULL);
  81.  
  82.     rtEZRequest ("NUMBER 1:\nString requester\nfunction: rtGetString()",
  83.                      "Show me", NULL, NULL);
  84.  
  85.     strcpy (buffer, "A bit of text");
  86.     if (!rtGetString (buffer, 127, "Enter anything:", NULL, TAG_END))
  87.         rtEZRequest ("You entered nothing :-(", "I'm sorry", NULL, NULL);
  88.     else
  89.         rtEZRequest ("You entered this string:\n'%s'.",
  90.                          "So I did", NULL, NULL, buffer);
  91.  
  92.     ret = rtGetString (buffer, 127, "Enter anything:", NULL,
  93.                              RTGS_GadFmt, " _Ok |New _2.0 feature!|_Cancel",
  94.                              RTGS_TextFmt,
  95.                                  "These are two new features of ReqTools 2.0:\n"
  96.                                   "Text above the entry gadget and more than\n"
  97.                                   "one response gadget.",
  98.                      TAG_MORE, undertag);
  99.     if (ret == 2)
  100.         rtEZRequest ("Yep, this is a new\nReqTools 2.0 feature!",
  101.                          "Oh boy!", NULL, NULL);
  102.  
  103.     ret = rtGetString (buffer, 127, "Enter anything:", NULL,
  104.                              RTGS_GadFmt, " _Ok |_Abort|_Cancel",
  105.                              RTGS_TextFmt,
  106.                                  "New is also the ability to switch off the\n"
  107.                                   "backfill pattern.  You can also center the\n"
  108.                                   "text above the entry gadget.\n"
  109.                                   "These new features are also available in\n"
  110.                                   "the rtGetLong() requester.",
  111.                              RTGS_BackFill, FALSE,
  112.                              RTGS_Flags, GSREQF_CENTERTEXT|GSREQF_HIGHLIGHTTEXT,
  113.                              TAG_MORE, undertag);
  114.     if (ret == 2)
  115.         rtEZRequest ("What!! You pressed abort!?!\nYou must be joking :-)",
  116.                          "Ok, Continue", NULL, NULL);
  117.                          
  118.  
  119.     rtEZRequest ("NUMBER 2:\nNumber requester\nfunction: rtGetLong()",
  120.                      "Show me", NULL, NULL);
  121.  
  122.     if (!rtGetLong (&longnum, "Enter a number:", NULL,
  123.                          RTGL_ShowDefault, FALSE,
  124.                          RTGL_Min, 0, RTGL_Max, 666,TAG_END))
  125.         rtEZRequest ("You entered nothing :-(",
  126.                          "I'm sorry", NULL, NULL);
  127.     else
  128.         rtEZRequest ("The number you entered was:\n%ld%s",
  129.                          "So it was", NULL, NULL, longnum,
  130.                             (longnum == 666) ? " (you devil! :)" : "");
  131.  
  132.     rtEZRequest ("NUMBER 3:\nMessage requester, the requester\n"
  133.                      "you've been using all the time!\nfunction: rtEZRequest()",
  134.                      "Show me more", NULL, NULL);
  135.  
  136.     rtEZRequest ("Simplest usage: some body text and\na single centered gadget.",
  137.                      "Got it", NULL, NULL);
  138.  
  139.     while (!rtEZRequest ("You can also use two gadgets to\n"
  140.                                 "ask the user something.\n"
  141.                                 "Do you understand?", "Of course|Not really",
  142.                                 NULL, NULL))
  143.         rtEZRequest ("You are not one of the brightest are you?\n"
  144.                          "We'll try again...",
  145.                          "Ok", NULL, NULL);
  146.  
  147.     rtEZRequest ("Great, we'll continue then.", "Fine", NULL, NULL);
  148.  
  149.     switch (rtEZRequest ("You can also put up a requester with\n"
  150.                                 "three choices.\n"
  151.                                 "How do you like the demo so far ?",
  152.                                 "Great|So so|Rubbish", NULL, NULL)) {
  153.         case FALSE:
  154.             rtEZRequest ("Too bad, I really hoped you\nwould like it better.",
  155.                              "So what", NULL, NULL);
  156.             break;
  157.         case TRUE:
  158.             rtEZRequest ("I'm glad you like it so much.", "Fine", NULL, NULL);
  159.             break;
  160.         case 2:
  161.             rtEZRequest ("Maybe if you run the demo again\n"
  162.                              "you'll REALLY like it.",
  163.                              "Perhaps", NULL, NULL);
  164.             break;
  165.         }
  166.  
  167.     ret = rtEZRequestTags ("The number of responses is not limited to three\n"
  168.                                   "as you can see.  The gadgets are labeled with\n"    
  169.                                   "the return code from rtEZRequest().\n"
  170.                                   "Pressing Return will choose 4, note that\n"
  171.                                   "4's button text is printed in boldface.",
  172.                                   "1|2|3|4|5|0", NULL, NULL,
  173.                                   RTEZ_DefaultResponse, 4, TAG_END);
  174.     rtEZRequest ("You picked '%ld'.", "How true", NULL, NULL, ret);
  175.  
  176.     rtEZRequestTags ("New for Release 2.0 of ReqTools (V38) is\n"
  177.                           "the possibility to define characters in the\n"
  178.                           "buttons as keyboard shortcuts.\n"
  179.                           "As you can see these characters are underlined.\n"
  180.                           "Pressing shift while still holding down the key\n"
  181.                           "will cancel the shortcut.\n"
  182.                           "Note that in other requesters a string gadget may\n"
  183.                           "be active.  To use the keyboard shortcuts there\n"
  184.                           "you have to keep the Right Amiga key pressed down.",
  185.                           "_Great|_Fantastic|_Swell|Oh _Boy",
  186.                           NULL, NULL,
  187.                           RT_Underscore, '_', TAG_END);
  188.  
  189.     rtEZRequest (
  190.         "You may also use C-style formatting codes in the body text.\n"
  191.         "Like this:\n\n"
  192.         "'The number %%ld is written %%s.' will give:\n\n"
  193.         "The number %ld is written %s.\n\n"
  194.         "if you also pass '5' and '\"five\"' to rtEZRequest().",
  195.         "_Proceed", NULL, (struct TagItem *)&undertag, 5, "five");
  196.  
  197.     if (rtEZRequestTags ("It is also possible to pass extra IDCMP flags\n"
  198.                                "that will satisfy rtEZRequest(). This requester\n"
  199.                                "has had DISKINSERTED passed to it.\n"
  200.                                "(Try inserting a disk).",
  201.                                "_Continue", NULL, NULL,
  202.                                RT_IDCMPFlags, DISKINSERTED,
  203.                                 RT_Underscore, '_', TAG_END)
  204.                                & DISKINSERTED)
  205.         rtEZRequest ("You inserted a disk.", "I did", NULL, NULL);
  206.     else
  207.         rtEZRequest ("You used the 'Continue' gadget\n"
  208.                          "to satisfy the requester.", "I did", NULL, NULL);
  209.  
  210.     rtEZRequestTags ("Finally, it is possible to specify the position\n"
  211.                           "of the requester.\n"
  212.                           "E.g. at the top left of the screen, like this.\n"
  213.                           "This works for all requesters, not just rtEZRequest()!",
  214.                           "_Amazing", NULL, NULL,
  215.                           RT_ReqPos, REQPOS_TOPLEFTSCR,
  216.                           RT_Underscore, '_', TAG_END);
  217.  
  218.     rtEZRequestTags ("Alternatively, you can center the\n"
  219.                           "requester on the screen.\n"
  220.                           "Check out 'reqtools.doc' for all the possibilities.",
  221.                           "I'll do that", NULL, NULL,
  222.                           RT_ReqPos, REQPOS_CENTERSCR, TAG_END);
  223.  
  224.     rtEZRequestTags ("NUMBER 4:\nFile requester\n"
  225.                           "function: rtFileRequest()",
  226.                           "_Demonstrate", NULL, NULL, RT_Underscore, '_', TAG_END);
  227.  
  228.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  229.  
  230.         filterhook.h_Entry = (ULONG (*)())file_filterfunc;
  231.  
  232.         filename[0] = 0;
  233.         if (rtFileRequest (filereq, filename, "Pick a file",
  234. #ifdef DOHOOKS
  235.                                  RTFI_FilterFunc, &filterhook,
  236. #endif
  237.                                  TAG_END))
  238.             rtEZRequest ("You picked the file:\n'%s'\nin directory:\n'%s'",
  239.                              "Right", NULL, NULL, filename, filereq->Dir);
  240.         else
  241.             rtEZRequest ("You didn't pick a file.", "No", NULL, NULL);
  242.  
  243.         rtEZRequestTags ("The file requester has the ability\n"
  244.                               "to allow you to pick more than one\n"
  245.                               "file (use Shift to extend-select).\n"
  246.                               "Note the extra gadgets you get.",
  247.                               "_Interesting", NULL, NULL, RT_Underscore, '_', TAG_END);
  248.  
  249.         flist = rtFileRequest (filereq, filename, "Pick some files",
  250.                                       RTFI_Flags, FREQF_MULTISELECT, TAG_END);
  251.         if (flist) {
  252.             rtEZRequest ("You selected some files, this is\n"
  253.                              "the first one:\n"
  254.                              "'%s'\n"
  255.                              "All the files are returned as a linked\n"
  256.                              "list (see demo.c and reqtools.h).",
  257.                              "Aha", NULL, NULL, flist->Name);
  258.             /* Traverse all selected files */
  259.             /*
  260.             tempflist = flist;
  261.             while (tempflist) {
  262.                 DoSomething (tempflist->Name, tempflist->StrLen);
  263.                 tempflist = tempflist->Next;
  264.                 }
  265.             */
  266.             /* Free filelist when no longer needed! */
  267.             rtFreeFileList (flist);
  268.             }
  269.  
  270.         rtFreeRequest (filereq);
  271.         }
  272.     else
  273.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  274.  
  275.  
  276.     rtEZRequestTags ("The file requester can be used\n"
  277.                           "as a directory requester as well.",
  278.                           "Let's _see that", NULL, NULL, RT_Underscore, '_', TAG_END);
  279.  
  280.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  281.         if (rtFileRequest (filereq, filename, "Pick a directory",
  282.                                  RTFI_Flags, FREQF_NOFILES, TAG_END))
  283.             rtEZRequest ("You picked the directory:\n'%s'",
  284.                              "Right", NULL, NULL, filereq->Dir);
  285.         else
  286.             rtEZRequest ("You didn't pick a directory.", "No", NULL, NULL);
  287.  
  288.         rtFreeRequest (filereq);
  289.         }
  290.     else
  291.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  292.  
  293.     rtEZRequest ("NUMBER 5:\nFont requester\n"
  294.                      "function: rtFontRequest()",
  295.                      "Show", NULL, NULL);
  296.  
  297.     if (fontreq = rtAllocRequestA (RT_FONTREQ, NULL)) {
  298.  
  299.         fontreq->Flags = FREQF_STYLE|FREQF_COLORFONTS;
  300.         font_filterhook.h_Entry = (ULONG (*)())font_filterfunc;
  301.  
  302.         if (rtFontRequest (fontreq, "Pick a font",
  303. #ifdef DOHOOKS
  304.                                  RTFO_FilterFunc, &font_filterhook,
  305. #endif
  306.                                  TAG_END))
  307.             rtEZRequest ("You picked the font:\n'%s'\nwith size:\n'%ld'",
  308.                              "Right", NULL, NULL,
  309.                              fontreq->Attr.ta_Name, fontreq->Attr.ta_YSize);
  310.         else
  311.             rtEZRequestTags ("You canceled.\nWas there no font you liked ?",
  312.                                   "_Nope", NULL, NULL, RT_Underscore, '_', TAG_END);
  313.  
  314.         rtFreeRequest (fontreq);
  315.         }
  316.     else
  317.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  318.  
  319.     rtEZRequestTags ("NUMBER 6:\nPalette requester\n"
  320.                           "function: rtPaletteRequest()",
  321.                           "_Proceed", NULL, NULL, RT_Underscore, '_', TAG_END);
  322.  
  323.     color = rtPaletteRequest ("Change palette", NULL, TAG_END);
  324.     if (color == -1)
  325.         rtEZRequest ("You canceled.\nNo nice colors to be picked ?",
  326.                          "Nah", NULL, NULL);
  327.     else
  328.         rtEZRequest ("You picked color number %ld.", "Sure did",
  329.                          NULL, NULL, color);
  330.  
  331.     rtEZRequestTags ("NUMBER 7: (ReqTools 2.0)\n"
  332.                           "Volume requester\n"
  333.                           "function: rtFileRequest() with\n"
  334.                           "          RTFI_VolumeRequest tag.",
  335.                           "_Show me", NULL, NULL, RT_Underscore, '_', TAG_END);
  336.  
  337.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  338.  
  339.         vol_filterhook.h_Entry = (ULONG (*)())vol_filterfunc;
  340.  
  341.         if (rtFileRequest (filereq, NULL, "Pick a volume",
  342. #ifdef DOHOOKS
  343.                                  RTFI_FilterFunc, &vol_filterhook,
  344. #endif
  345.                                  RTFI_VolumeRequest, 0, TAG_END))
  346.             rtEZRequest ("You picked the volume:\n'%s'",
  347.                              "Right", NULL, NULL, filereq->Dir);
  348.         else
  349.             rtEZRequest ("You didn't pick a volume.", "I did not", NULL, NULL);
  350.  
  351.         rtFreeRequest (filereq);
  352.         }
  353.     else
  354.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  355.  
  356.     rtEZRequestTags ("NUMBER 8: (ReqTools 2.0)\n"
  357.                           "Screen mode requester\n"
  358.                           "function: rtScreenModeRequest()\n"
  359.                           "Only available on Kickstart 2.0!",
  360.                           "_Proceed", NULL, NULL, RT_Underscore, '_', TAG_END);
  361.  
  362.     if (SysBase->LibNode.lib_Version < 37)
  363.         rtEZRequestTags ("Your Amiga doesn't seem to have\n"
  364.                               "Kickstart 2.0 in ROM so I am not\n"
  365.                               "able to show you the Screen mode\n"
  366.                               "requester.\n"
  367.                               "So upgrade to 2.0 *now* :-)",
  368.                               "_Allright", NULL, NULL, RT_Underscore, '_', TAG_END);
  369.     else {
  370.         if (scrmodereq = rtAllocRequestA (RT_SCREENMODEREQ, NULL)) {
  371.  
  372.             if (rtScreenModeRequest (scrmodereq, "Pick a screen mode:",
  373.                                              RTSC_Flags, SCREQF_DEPTHGAD|SCREQF_SIZEGADS|
  374.                                                  SCREQF_AUTOSCROLLGAD|SCREQF_OVERSCANGAD,
  375.                                               TAG_END))
  376.                 rtEZRequest ("You picked this mode:\n"
  377.                                  "ModeID  : 0x%lx\n"
  378.                                  "Size    : %ld x %ld\n"
  379.                                  "Depth   : %ld\n"
  380.                                  "Overscan: %ld\n"
  381.                                  "AutoScroll %s",
  382.                                  "Right", NULL, NULL,
  383.                                  scrmodereq->DisplayID,
  384.                                  scrmodereq->DisplayWidth,
  385.                                  scrmodereq->DisplayHeight,
  386.                                  scrmodereq->DisplayDepth,
  387.                                  scrmodereq->OverscanType,
  388.                                  scrmodereq->AutoScroll ? "On" : "Off");
  389.             else
  390.                 rtEZRequest ("You didn't pick a screen mode.", "Nope", NULL, NULL);
  391.  
  392.             rtFreeRequest (scrmodereq);
  393.             }
  394.         else
  395.             rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  396.         }
  397.  
  398.     rtEZRequestTags ("That's it!\n"
  399.                           "Hope you enjoyed the demo", "_Sure did", NULL, NULL,
  400.                           RT_Underscore, '_', TAG_END);
  401.  
  402.     /* free all resources */
  403.     CloseLibrary ((struct Library *)ReqToolsBase);
  404.  
  405.     exit (0);
  406. }
  407.  
  408. /********
  409. * HOOKS *
  410. ********/
  411.  
  412. BOOL __asm __saveds file_filterfunc (
  413.     REG __a0 struct Hook *hook,
  414.     REG __a2 struct rtFileRequester *filereq,
  415.     REG __a1 struct FileInfoBlock *fib
  416.     )
  417. {
  418.     myputs (fib->fib_FileName);
  419.     myputs ("\n");
  420.     return (TRUE);
  421. }
  422.  
  423. BOOL __asm __saveds font_filterfunc (
  424.     REG __a0 struct Hook *hook,
  425.     REG __a2 struct rtFontRequester *fontreq,
  426.     REG __a1 struct TextAttr *textattr
  427.     )
  428. {
  429.     myputs (textattr->ta_Name);
  430.     myputs ("\n");
  431.     return (TRUE);
  432. }
  433.  
  434. BOOL __asm __saveds vol_filterfunc (
  435.     REG __a0 struct Hook *hook,
  436.     REG __a2 struct rtFileRequester *filereq,
  437.     REG __a1 struct rtVolumeEntry *volentry
  438.     )
  439. {
  440.     myputs (volentry->Type == DLT_DEVICE ? "(Volume) " : "(Assign) ");
  441.     myputs (volentry->Name);
  442.     myputs ("\n");
  443.     return (TRUE);
  444. }
  445.