home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 152 / sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-21  |  8.5 KB  |  243 lines

  1. /***************************************************************************/
  2. /*  C-manship, Listing 1,ST-Log #13,Megamax C.(refined for Mark Williams C)*/
  3. /***************************************************************************/
  4. #include <OSBIND.H>
  5. #include "SAMPLE.H"
  6.  
  7. #define FMD_START  0
  8. #define FMD_GROW   1
  9. #define FMD_SHRINK 2
  10. #define FMD_FINISH 3
  11. #define R_TREE     0
  12. #define FINGER     3
  13.  
  14. /* The usual required GEM global arrays */
  15. int work_in[11], 
  16.     work_out[57], 
  17.     pxyarray[10],
  18.     contrl[12], 
  19.     intin[128], 
  20.     ptsin[128], 
  21.     intout[128], 
  22.     ptsout[128];
  23.  
  24. /* Global variables */
  25. int handle, dum;
  26.  
  27. int dial_x,  /* Dialog x coordinate.    */
  28.     dial_y,  /* Dialog y coordinate.    */
  29.     dial_w,  /* Dilaog width.           */
  30.     dial_h,  /* Dialog height.          */
  31.     num,     /* Value of number option. */
  32.     n_x,     /* NUMBERS object x coord. */
  33.     n_y;     /* NUMBERS object y coord. */
  34.  
  35. char number_str[13] = "    0000    ";  /* NUMBERS string. */
  36.  
  37. char *find_str();  /* Function declaration. */
  38.  
  39.    /* Structure to hold an object's description. */
  40. typedef struct object
  41. {
  42.    int           ob_next;  /* Next sibling of object.              */
  43.    int           ob_head;  /* Head of object's children.           */
  44.    int           ob_tail;  /* Tail of object's children.           */
  45.    unsigned int  ob_type;  /* Type of object.                      */
  46.    unsigned int  ob_flags; /* Flags.                               */
  47.    unsigned int  ob_state; /* State of object.                     */
  48.    char          *ob_spec; /* Miscellaneous information.           */
  49.    int           ob_x;     /* x coord of object upper left corner. */
  50.    int           ob_y;     /* y coord of object upper left corner. */
  51.    int           ob_w;     /* Width of object.                     */
  52.    int           ob_h;     /* Height of object.                    */
  53. } OBJECT;
  54.  
  55. OBJECT *tree_addr;  /* Pointer to our object structure. */
  56.  
  57.    /* Structure to hold object text information. */
  58. typedef struct text_edinfo
  59. {
  60.    char *te_ptext;    /* Pointer to text.             */
  61.    char *te_ptmplt;   /* Pointer to template.         */
  62.    char *te_pvalid;   /* Pointer to validation chars. */
  63.    int  te_font;      /* Font.                        */
  64.    int  te_junk1;     /* Unused.                      */
  65.    int  te_just;      /* Justification.               */
  66.    int  te_color;     /* Color information.           */
  67.    int  te_junk2;     /* Unused.                      */
  68.    int  te_thickness; /* Border thickness.            */
  69.    int  te_txtlen;    /* length of text string.       */
  70.    int  te_tmplen;    /* length of template string.   */
  71. } TEDINFO;
  72.  
  73. main ()
  74. {
  75.    appl_init ();              /* Initialize application.        */
  76.    open_vwork ();             /* Set up workstation.            */
  77.    do_dialog();               /* Go do the dialog box.          */
  78.    print_results(tree_addr);  /* Print user's choices.          */
  79.    button_wait();             /* Wait for mouse button.         */
  80.    v_clsvwk (handle);         /* Close virtual workstation.     */
  81.    appl_exit ();              /* Back to the desktop.           */
  82. }
  83.  
  84. open_vwork ()
  85. {
  86.    int i;
  87.  
  88.    /* Get graphics handle, initialize the GEM arrays and open  */
  89.    /* a virtual workstation.                                   */
  90.  
  91.    handle = graf_handle (&dum,&dum,&dum,&dum);
  92.    for (i=0; i<10; work_in[i++] = 1);
  93.    work_in[10] = 2;
  94.    v_opnvwk (work_in, &handle, work_out);
  95. }
  96.  
  97. do_dialog ()
  98. {
  99.    int choice; /* Button choice from dialog. */
  100.  
  101.    /* Here we load the resource file.  If the file is missing,  */
  102.    /* we warn the user with an alert box then terminate the     */
  103.    /* program by skipping the code following the else.          */
  104.  
  105.    if (! rsrc_load ("\SAMPLE.RSC"))
  106.       form_alert (1, "[1][SAMPLE.RSC missing!][I'll do better!]");
  107.  
  108.    /* If the resource file loads OK, we get the address of the  */
  109.    /* tree, get the coords for centering the dialog, save the   */
  110.    /* portion of the screen that'll be covered by the dialog,   */
  111.    /* and draw the dialog.  The mouse pointer is changed to     */
  112.    /* pointing finger.                                          */
  113.  
  114.    else {
  115.       rsrc_gaddr (R_TREE, SAMPLE, &tree_addr);
  116.       form_center (tree_addr, &dial_x, &dial_y, &dial_w, &dial_h);
  117.       objc_offset (tree_addr, NUMBERS, &n_x, &n_y);
  118.       form_dial (FMD_START, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  119.       form_dial (FMD_GROW, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  120.       objc_draw (tree_addr, 0, 2, dial_x, dial_y, dial_w, dial_h);
  121.       graf_mouse (FINGER,&dum);
  122.  
  123.    /* Here we allow the user to interact with the dialog then,  */
  124.    /* based on the chosen button, perform the necessary action. */
  125.    /* The form_do function is repeated until the user chooses   */
  126.    /* either the OK button or the CANCEL button.                */
  127.  
  128.       num = 0;
  129.       do {
  130.          choice = form_do (tree_addr, NAME);
  131.          if (choice == RADIO1) v_gtext (handle,160,20,"Radio 1   ");
  132.          if (choice == RADIO2) v_gtext (handle,160,20,"Radio 2   ");
  133.          if (choice == RADIO3) v_gtext (handle,160,20,"Radio 3   ");
  134.          if (choice == RADIO4) v_gtext (handle,160,20,"Radio 4   ");
  135.          if (choice == RADIO5) v_gtext (handle,160,20,"Radio 5   ");
  136.          if (choice == RADIO6) v_gtext (handle,160,20,"Radio 6   ");
  137.          if (choice == OPTION1) v_gtext (handle,160,20,"Option 1  ");
  138.          if (choice == OPTION2) v_gtext (handle,160,20,"Option 2  ");
  139.          if (choice == UPARROW) do_up();
  140.          if (choice == DWNARROW) do_down();
  141.       }
  142.       while (choice != CANCEL && choice != OK);
  143.  
  144.    /* Once the CANCEL or OK buttons have been pressed, we clean  */
  145.    /* up after ourselves by performing the "shrinking box" and   */
  146.    /* then redrawing the screen.                                 */
  147.  
  148.       form_dial (FMD_SHRINK, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  149.       form_dial (FMD_FINISH, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  150.       print_results(tree_addr);         /* Print user's choices. */
  151.  
  152.   }
  153. }
  154.  
  155. do_up ()
  156. {
  157.  
  158.    /* First we increment our value and make sure it stays in    */
  159.    /* range.  If the value has become larger than 9999, we must */
  160.    /* also reinitialize display string for the object NUMBERS.  */
  161.    /* We then call our function to update the NUMBERS object.   */
  162.  
  163.    num += 1;
  164.    if (num > 9999) {
  165.       num = 0;
  166.       strcpy (number_str,"    0000    ");
  167.    }
  168.    edit_object ();
  169. }
  170.  
  171. do_down ()
  172. {
  173.  
  174.    /* Here we decrement the value and check for its range, after */
  175.    /* which we update the NUMBERS object.                        */
  176.  
  177.    num -= 1;
  178.    if (num < 0) num = 9999;
  179.    edit_object ();
  180. }
  181.  
  182. edit_object ()
  183. {
  184.    TEDINFO *ob_tedinfo;
  185.    char temp_str[10];
  186.  
  187.    /* Here we edit the string we're using for the text display  */
  188.    /* in the object NUMBERS so that it reflects the new value.  */
  189.  
  190.    sprintf (temp_str,"%d",num);
  191.    strcpy (&number_str[8 - strlen (temp_str)],temp_str);
  192.    strcpy (&number_str[8],"    ");
  193.  
  194.    /* Then we find the object NUMBERS' TEDINFO and point the    */
  195.    /* te_ptext member to our updated string, after which we     */
  196.    /* redraw the object NUMBERS.                                */
  197.  
  198.    ob_tedinfo = (TEDINFO *) tree_addr[NUMBERS].ob_spec;
  199.    ob_tedinfo -> te_ptext = number_str;
  200.    objc_draw (tree_addr, NUMBERS, 1, n_x, n_y, 96, 16);
  201. }
  202.  
  203. print_results (tree_addr)
  204. OBJECT tree_addr[];
  205. {
  206.    char *string;
  207.  
  208.    /* Here we call the function that locates the string, then  */
  209.    /* print the user's input to the screen.                    */
  210.  
  211.    string = find_str (NAME, string);
  212.    v_gtext (handle, 160, 20, "Your name is ");
  213.    v_gtext (handle,264,20,string);
  214.    string = find_str (AGE, string);
  215.    v_gtext (handle, 160, 28, "Your age is ");
  216.    v_gtext (handle, 264, 28, string);
  217.    string = find_str (NUMBERS, string);
  218.    v_gtext (handle, 160, 36, "Final number value: ");
  219.    v_gtext (handle,320,36,&string[4]);
  220. }
  221.  
  222. char *find_str (object, string)
  223. int object;
  224. char *string;
  225. {
  226.    TEDINFO *ob_tedinfo;
  227.  
  228.    /* In this function, we locate the object's TEDINFO structure */
  229.    /* then set our string pointer to the pointer found in the    */
  230.    /* te_ptext member.                                           */
  231.  
  232.    ob_tedinfo = (TEDINFO *) tree_addr[object].ob_spec;
  233.    string = ob_tedinfo -> te_ptext;
  234.    return (string);
  235. }
  236.    
  237. /* Waits for left button to be pressed and released. */
  238. button_wait()
  239. {
  240.     evnt_button (1,1,1,&dum,&dum,&dum,&dum);
  241.     evnt_button (1,1,0,&dum,&dum,&dum,&dum);
  242. }
  243.