home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 480.lha / SmartField / Programs / requester / r.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-06  |  12.1 KB  |  444 lines

  1. /****************************************
  2. *  SMART FIELDS REQUESTER PROGRAM v1.00
  3. *  © Copyright 1988 Timm Martin
  4. *  All Rights Reserved
  5. *****************************************/
  6.  
  7. #include <exec/io.h>
  8. #include <exec/memory.h>
  9. #include <exec/ports.h>
  10. #include <exec/types.h>
  11. #include <functions.h>
  12. #include <graphics/gfxbase.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/intuitionbase.h>
  15. #include <console/console.h>
  16. #include <console/fields.h>
  17. #include <console/functions.h>
  18. #include <toolkit/toolkit.h>
  19.  
  20. /************************
  21. *  INTUITION STRUCTURES
  22. *************************/
  23.  
  24. struct IntuitionBase *IntuitionBase = NULL;
  25. struct GfxBase       *GfxBase = NULL;
  26. struct Window        *win = NULL;
  27. struct RastPort      *rp;
  28.  
  29. /********************
  30. *  FIELD STRUCTURES
  31. *********************/
  32.  
  33. /*** TITLES ***/
  34.  
  35. struct IntuiText field1_text = {
  36.   1, 0, JAM1, -3, 11, NULL, (STRPTR)"Field 1", NULL
  37. };
  38. struct IntuiText field2_text = {
  39.   1, 0, JAM1, -3, 11, NULL, (STRPTR)"Field 2", NULL
  40. };
  41.  
  42. /*** BORDERS ***/
  43.  
  44. SHORT field_pairs2[] = { 2,11, 274,11, 274, 1, 275, 1, 275,11 };
  45. SHORT field_pairs1[] = { 0, 0, 273, 0, 273,10,   0,10,   0, 0 };
  46.  
  47. struct Border field_border2 = { 
  48.   -3, -2, 2, 0, JAM1, 5, field_pairs2, NULL
  49. };
  50. struct Border field_border1 = {
  51.   -3, -2, 1, 0, JAM1, 5, field_pairs1, &field_border2
  52. };
  53.  
  54. /*** BUFFERS ***/
  55.  
  56. #define FIELD_SIZE 31
  57.  
  58. UBYTE field1_input[FIELD_SIZE];
  59. UBYTE field2_input[FIELD_SIZE];
  60.  
  61. UBYTE field1_undo[FIELD_SIZE];
  62. UBYTE field2_undo[FIELD_SIZE];
  63.  
  64. /*** FIELDS ***/
  65.  
  66. #define FIELD1_FIELD 1
  67. #define FIELD2_FIELD 2
  68.  
  69. #define LEFT_EDGE 78
  70. #define FIRST_FIELD field1_field
  71. struct Field field1_field = {
  72.   NULL, LATER, field1_input, field1_undo, NULL, 1, 0, CON_PLAIN,
  73.   FIELD_ENABLED, NULL, NULL, LEFT_EDGE, 28, 0, 0, FIELD_SIZE, 0,
  74.   0, 0, 0, &field1_text, &field_border1, NULL, FIELD1_FIELD, NULL, NULL, NULL
  75. };
  76. struct Field field2_field = {
  77.   &field1_field, NULL, field2_input, field2_undo, NULL, 1, 0, CON_PLAIN,
  78.   FIELD_ENABLED, NULL, NULL, LEFT_EDGE, 54, 0, 0, FIELD_SIZE, 0,
  79.   0, 0, 0, &field2_text, &field_border1, NULL, FIELD2_FIELD, NULL, NULL, NULL
  80. };
  81. #define FINAL_FIELD field2_field
  82.  
  83. struct  FieldHeader field_header = { INIT_FIELD_HEADER };
  84. #define CURRENT_FIELD field_header.CurrentField
  85. UBYTE   con_buffer[CONSOLE_BUFFER_SIZE];
  86.  
  87. /***************
  88. *  REQUESTER 1
  89. ****************/
  90.  
  91. struct IntuiText req1_text9 = {
  92.   1, 2, JAM1, 20, 10, NULL, (STRPTR)"With SmartFields, you can display simple", NULL
  93. };
  94. struct IntuiText req1_text8 = {
  95.   1, 2, JAM1, 36, 21, NULL, (STRPTR)"message requesters that the user can", &req1_text9
  96. };
  97. struct IntuiText req1_text7 = {
  98.   1, 2, JAM1, 28, 32, NULL, (STRPTR)"answer with the mouse or the keyboard.", &req1_text8
  99. };
  100. struct IntuiText req1_text6 = {
  101.   1, 2, JAM1, 28, 43, NULL, (STRPTR)"You can decide which keys the user can", &req1_text7
  102. };
  103. struct IntuiText req1_text5 = {
  104.   1, 2, JAM1, 32, 54, NULL, (STRPTR)"press to make this requester go away.", &req1_text6
  105. };
  106. struct IntuiText req1_text4 = {
  107.   1, 2, JAM1, 32, 65, NULL, (STRPTR)"In this case, pressing either the 'O'", &req1_text5
  108. };
  109. struct IntuiText req1_text3 = {
  110.   1, 2, JAM1, 28, 76, NULL, (STRPTR)"key, <RETURN> key, or <ESCape> key, or", &req1_text4
  111. };
  112. struct IntuiText req1_text2 = {
  113.   1, 2, JAM1, 40, 87, NULL, (STRPTR)"clicking on the OK gadget will make", &req1_text3
  114. };
  115. struct IntuiText req1_text1 = {
  116.   1, 2, JAM1, 80, 98, NULL, (STRPTR)"this requester disappear.", &req1_text2
  117. };
  118. struct IntuiText ok_text = {
  119.   1, 2, JAM1, 18, 2, NULL, (STRPTR)"OK", NULL
  120. };
  121.  
  122. SHORT req1_pairs[] = {
  123.   2,1, 357,1, 357,135, 2,135, 2,1, 356,1, 356,135, 3,135, 3,1
  124. };
  125. SHORT ok_pairs2[] = { 2,13, 52,13, 52,1, 53,1, 53,13 };
  126. SHORT ok_pairs1[] = { 0,0, 51,0, 51,12, 0,12, 0,0 };
  127.  
  128. struct Border req1_border = {
  129.   0, 0, 1, 0, JAM1, 9, req1_pairs, NULL
  130. };
  131. struct Border ok_border2 = {
  132.   -1, -1, 1, 0, JAM1, 5, ok_pairs2, NULL
  133. };
  134. struct Border ok_border1 = {
  135.   -1, -1, 1, 0, JAM1, 5, ok_pairs1, &ok_border2
  136. };
  137.  
  138. struct Gadget ok_gadget = {
  139.   NULL, 154, 115, 50, 11, GADGHCOMP, RELVERIFY, BOOLGADGET | REQGADGET,
  140.   (APTR)&ok_border1, NULL, &ok_text, NULL, NULL, 1, NULL
  141. };
  142.  
  143. struct Requester req1_requester = {
  144.   NULL, 35, 32, 360, 137, 0, 0, &ok_gadget, &req1_border,
  145.   &req1_text1, NOISYREQ, 2, NULL, NULL, NULL, NULL, NULL
  146. };
  147.  
  148. /***************
  149. *  REQUESTER 2
  150. ****************/
  151.  
  152. struct IntuiText req2_text7 = {
  153.   2, 3, JAM1, 32, 76, NULL, (STRPTR)"press either the 'Y' or 'N' key.", NULL
  154. };
  155. struct IntuiText req2_text6 = {
  156.   2, 3, JAM1, 40, 65, NULL, (STRPTR)"make this requester disappear,", &req2_text7
  157. };
  158. struct IntuiText req2_text5 = {
  159.   2, 3, JAM1, 28, 54, NULL, (STRPTR)"letter of the desired gadget.  To", &req2_text6
  160. };
  161. struct IntuiText req2_text4 = {
  162.   2, 3, JAM1, 36, 43, NULL, (STRPTR)"gadget or by pressing the first", &req2_text5
  163. };
  164. struct IntuiText req2_text3 = {
  165.   2, 3, JAM1, 32, 32, NULL, (STRPTR)"either by clicking on the proper", &req2_text4
  166. };
  167. struct IntuiText req2_text2 = {
  168.   2, 3, JAM1, 20, 21, NULL, (STRPTR)"requesters that the user can answer", &req2_text3
  169. };
  170. struct IntuiText req2_text1 = {
  171.   2, 3, JAM1, 20, 10, NULL, (STRPTR)"You can also display multi-response", &req2_text2
  172. };
  173. struct IntuiText yes_text = {
  174.   2, 3, JAM1, 14, 2, NULL, (STRPTR)"YES", NULL
  175. };
  176. struct IntuiText no_text = {
  177.   2, 3, JAM1, 18, 2, NULL, (STRPTR)"NO", NULL
  178. };
  179.  
  180. SHORT req2_pairs[] = {
  181.   2,1, 317,1, 317,113, 2,113, 2,1, 316,1, 316,113, 3,113, 3,1
  182. };
  183. struct Border yes_border2 = {
  184.   -1, -1, 2, 0, JAM1, 5, ok_pairs2, NULL
  185. };
  186. struct Border yes_border1 = {
  187.   -1, -1, 2, 0, JAM1, 5, ok_pairs1, &yes_border2
  188. };
  189.  
  190. struct Border req2_border = {
  191.   0, 0, 2, 0, JAM1, 9, req2_pairs, NULL
  192. };
  193.  
  194. #define YES_GADGET 1
  195. #define NO_GADGET  2
  196. struct Gadget yes_gadget = {
  197.   NULL, 22, 93, 50, 11, GADGHCOMP, RELVERIFY, BOOLGADGET | REQGADGET,
  198.   (APTR)&yes_border1, NULL, &yes_text, NULL, NULL, YES_GADGET, NULL
  199. };
  200. struct Gadget no_gadget = {
  201.   &yes_gadget, 248, 93, 50, 11, GADGHCOMP, RELVERIFY, BOOLGADGET | REQGADGET,
  202.   (APTR)&yes_border1, NULL, &no_text, NULL, NULL, NO_GADGET, NULL
  203. };
  204.  
  205. struct Requester req2_requester = {
  206.   NULL, 55, 37, 320, 115, 0, 0, &no_gadget, &req2_border,
  207.   &req2_text1, NOISYREQ, 3, NULL, NULL, NULL, NULL, NULL
  208. };
  209.  
  210. /***************
  211. *  WINDOW TEXT
  212. ****************/
  213.  
  214. struct IntuiText win5_text = {
  215.   1, 0, JAM2, 139, 120, NULL, (STRPTR)"manage requesters.", NULL
  216. };
  217. struct IntuiText win4_text = {
  218.   1, 0, JAM2, 91, 110, NULL, (STRPTR)"use SmartFields to display and", &win5_text
  219. };
  220. struct IntuiText win3_text = {
  221.   1, 0, JAM2, 99, 100, NULL, (STRPTR)"demonstration on how you can", &win4_text
  222. };
  223. struct IntuiText win2_text = {
  224.   1, 0, JAM2, 99, 90, NULL, (STRPTR)"and press the HELP key for a", &win3_text
  225. };
  226. struct IntuiText win1_text = {
  227.   1, 0, JAM2, 95, 80, NULL, (STRPTR)"Type anything into the fields", &win2_text
  228. };
  229.  
  230. struct IntuiText yup_text = {
  231.   3, 0, JAM2, 103, 160, NULL, (STRPTR)"You selected the YES gadget!", NULL
  232. };
  233. struct IntuiText nop_text = {
  234.   3, 0, JAM2, 103, 160, NULL, (STRPTR)"You selected the NO gadget! ", NULL
  235. };
  236.  
  237. /************************
  238. *  NEW WINDOW STRUCTURE
  239. *************************/
  240.  
  241. struct NewWindow new_window = {
  242.   0, 0, 430, 200, 0, 1, CLOSEWINDOW | GADGETUP | MOUSEBUTTONS | REFRESHWINDOW,
  243.   ACTIVATE | SMART_REFRESH | WINDOWCLOSE | WINDOWDEPTH | WINDOWDRAG |
  244.   WINDOWSIZING, NULL, NULL, (STRPTR)"SmartFields Requester Program v1.00",
  245.   NULL, NULL, 50, 25, 430, 200, WBENCHSCREEN
  246. };
  247.  
  248. /********************
  249. *  GLOBAL VARIABLES
  250. *********************/
  251.  
  252. #define WAIT_FOR_INPUT Wait(1L<<field_header.ReadPort->mp_SigBit|1L<<win->UserPort->mp_SigBit)
  253. #define CONSOLE_INPUT  message=(struct Message *)GetMsg(field_header.ReadPort)
  254. #define WINDOW_INPUT   imessage=(struct IntuiMessage *)GetMsg(win->UserPort)
  255.  
  256. /*************
  257. *  FUNCTIONS
  258. **************/
  259.  
  260. void   draw_screen();    /* draws window & fields */
  261. void   end_program();    /* terminates program */
  262. void   get_inputs();     /* monitors user input */
  263. USHORT help();           /* display help requesters */
  264. void   open_all();       /* opens libraries, windows, devices */
  265. void   next_field();     /* moves cursor to next field */
  266.  
  267. /**************************
  268. *  M A I N  P R O G R A M
  269. ***************************/
  270.  
  271. void main()
  272. {
  273.   open_all();
  274.   draw_screen();
  275.   get_inputs();
  276. }
  277.  
  278. /***************
  279. *  DRAW SCREEN
  280. ****************/
  281.  
  282. void draw_screen()
  283. {
  284.   PrintIText( rp, &win1_text, 0L, 0L );
  285.   field_refresh( &field_header, &FIRST_FIELD, -1, CURRENT_FIELD );
  286. }
  287.  
  288. /***************
  289. *  END PROGRAM
  290. ****************/
  291.  
  292. void end_program( return_code )
  293.   int return_code;
  294. {
  295.   field_close( &field_header );
  296.  
  297.   if (win)           { ClearMenuStrip( win ); CloseWindow( win ); }
  298.   if (GfxBase)         CloseLibrary( GfxBase );
  299.   if (IntuitionBase)   CloseLibrary( IntuitionBase );
  300.  
  301.   exit( return_code );
  302. }
  303.  
  304. /**************
  305. *  GET INPUTS
  306. ***************/
  307.  
  308. void get_inputs()
  309. {
  310.   struct IntuiMessage *imessage;
  311.   USHORT gadget;
  312.   int    key;
  313.   struct Message *message;
  314.   struct Field *where;
  315.  
  316.   FOREVER {
  317.     WAIT_FOR_INPUT;
  318.  
  319.     if (CONSOLE_INPUT) {
  320.       key = field_input( &field_header );
  321.       switch (key) {
  322.         case FIELD_SWALLOW:  break;
  323.         case FIELD_RETURN:
  324.         case FIELD_NEXT:
  325.         case FIELD_PREVIOUS: next_field(); break;
  326.         case FIELD_FIRST:    field_goto( &field_header, &FIRST_FIELD ); break;
  327.         case FIELD_FINAL:    field_goto( &field_header, &FINAL_FIELD ); break;
  328.         case FIELD_HELP:
  329.           if ((gadget = help()) == YES_GADGET)
  330.             PrintIText( rp, &yup_text, 0L, 0L );
  331.           else if (gadget == NO_GADGET)
  332.             PrintIText( rp, &nop_text, 0L, 0L );
  333.           break;
  334.       } /* switch key */
  335.     }   /* if keyboard input */
  336.  
  337.     while (WINDOW_INPUT) {
  338.       switch (imessage->Class) {
  339.         case MOUSEBUTTONS:
  340.           if (imessage->Code == LEFT_MOUSE_BUTTON)
  341.             if (where = field_click( &field_header,
  342.                         imessage->MouseX, imessage->MouseY )) {
  343.               where->BufferPos = field_header.BufferPos;
  344.               field_goto( &field_header, where );
  345.             }
  346.           break;
  347.         case REFRESHWINDOW:
  348.           draw_screen();
  349.           BeginRefresh( win );
  350.           EndRefresh( win, TRUE );
  351.           break;
  352.         case CLOSEWINDOW:
  353.           end_program( 0 );
  354.           break;
  355.       } /* switch */
  356.       ReplyMsg( imessage );
  357.     } /* while window messages */
  358.   }   /* forever */
  359. }
  360.  
  361. /********
  362. *  HELP
  363. *********/
  364.  
  365. USHORT help()
  366. {
  367.   USHORT finished = 0;
  368.   struct IntuiMessage *imessage;
  369.   int    key;
  370.   struct Message *message;
  371.  
  372.   if (Request( &req1_requester, win )) {
  373.     while (!finished) {
  374.       WAIT_FOR_INPUT;
  375.  
  376.       if (CONSOLE_INPUT) {
  377.         key = console_input( &field_header );
  378.         finished = key == 'o' || key == 'O' || key == CON_ESCAPE || key == CON_RETURN;
  379.       }
  380.       while (WINDOW_INPUT) {
  381.         finished = imessage->Class == GADGETUP && imessage->IAddress == &ok_gadget;
  382.         ReplyMsg( imessage );
  383.       }
  384.     } /* while not finished */
  385.     EndRequest( &req1_requester, win );
  386.   } /* if opened requester ok */
  387.  
  388.   if (Request( &req2_requester, win )) {
  389.     finished = 0;
  390.     while (!finished) {
  391.       WAIT_FOR_INPUT;
  392.  
  393.       if (CONSOLE_INPUT) {
  394.         key = console_input( &field_header );
  395.         if (key == 'y' || key == 'Y')
  396.           finished = YES_GADGET;
  397.         else if (key == 'n' || key == 'N')
  398.           finished = NO_GADGET;
  399.       }
  400.       while (WINDOW_INPUT) {
  401.         if (imessage->Class == GADGETUP)
  402.           finished = ((struct Gadget *)imessage->IAddress)->GadgetID;
  403.         ReplyMsg( imessage );
  404.       }
  405.     } /* while not finished */
  406.     EndRequest( &req2_requester, win );
  407.   } /* if opened requester ok */
  408.   return (finished);
  409. }
  410.  
  411. /************
  412. *  OPEN ALL
  413. *************/
  414.  
  415. void open_all()
  416. {
  417.   int error;
  418.  
  419.   if (!(IntuitionBase = (struct IntuitionBase *)
  420.         OpenLibrary( "intuition.library", LIBRARY_VERSION )))
  421.     end_program( 0x0100 );
  422.   if (!(GfxBase = (struct GfxBase *) OpenLibrary( "graphics.library", 0L )))
  423.     end_program( 0x0101 );
  424.  
  425.   if (!(win = OpenWindow( &new_window )))
  426.     end_program( 0x0102 );
  427.   rp = win->RPort;
  428.  
  429.   if (error = field_open( win, &field_header, &FIRST_FIELD, &FINAL_FIELD, con_buffer ))
  430.     end_program( error );
  431. }
  432.  
  433. /**************
  434. *  NEXT FIELD
  435. ***************/
  436.  
  437. void next_field()
  438. {
  439.   switch (CURRENT_FIELD->FieldID) {
  440.     case FIELD1_FIELD: field_goto( &field_header, &field2_field ); break;
  441.     case FIELD2_FIELD: field_goto( &field_header, &field1_field ); break;
  442.   } /* switch current field */
  443. }
  444.