home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / menu / tcu_32a / demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-26  |  13.2 KB  |  360 lines

  1. /*==========================================================================*
  2.  *                                                                          *
  3.  *   DEMO.C  by Karl Keyte                                                  *
  4.  *                                                                          *
  5.  *   Demonstrates the use of the TCU library (by same author)               *
  6.  *                                                                          *
  7.  *==========================================================================*/
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <sys\types.h>
  16. #include <usr\tcu.h>            /* Utilities include file */
  17.  
  18. static TCU_NOTICE  my_notice;
  19. static TCU_FORM    form;        /* Form object */
  20. static     int     time_field;
  21. static     char   *record;
  22.  
  23. extern TCU_FORM  SAMPLE;        /* Form object to be linked in.  Note that */
  24.                                 /* the name is CASE SENSITIVE */
  25. void main (void)
  26. {
  27.    int               name_id,       /* To hold field ID of name form field */
  28.                      pos_x, pos_y,  /* Position for the form */
  29.                      rkey,          /* Key used to exit form */
  30.                      select;        /* Menu selction */
  31.    char              buf[83];       /* Prompt input buffer */
  32.    TCU_MENU          mymenu;        /* Menu object */
  33.    TCU_FIELD_VALUE   fval;          /* Form field value object */
  34.    TCU_FORM_INFO     f_info;        /* Form information structure */
  35.  
  36.    void far      help (TCU_FORM *, int);     /* Form help function */
  37.    int far       idler (unsigned long);
  38.    int far       keyboard_handler (unsigned short *);
  39.    int far       test_age (TCU_FORM *, int, TCU_FIELD_VALUE *);/* Age verify */
  40.    int far       fn_keys (TCU_FORM *, int, int);               /* Fn. keys */
  41.  
  42.    char *option_list[] = { "AAdd Customer           =>",   /* Menu options */
  43.                            "DDelete Customer        =>",
  44.                            "QQuery Customer         =>",
  45.                            "PPrint Customer Details",
  46.                            "bAbout this Demo",
  47.                            "GGo back to DOS",
  48.                            NULL};
  49.  
  50. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  51.  
  52. /* Define the menu */
  53.  
  54.    select = tcu_define_menu (&mymenu,
  55.                          "Customer Menu",
  56.                          tcu_colour_attrib (YELLOW, CYAN),       /* Title */
  57.                          tcu_colour_attrib (BLUE, CYAN),         /* Box */
  58.                          tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
  59.                          tcu_colour_attrib (WHITE, BLUE),        /* Selected */
  60.                          tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
  61.                          TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP |
  62.                                        TCU_ESC_FUNC,
  63.                          TCU_BOX_DOUBLE,
  64.                          option_list,
  65.              tcu_colour_attrib (YELLOW, MAGENTA));
  66.  
  67.    if (select != TCU_OK) {
  68.       printf ("Error in defining menu!\n");
  69.       exit (1);
  70.    }
  71.  
  72.    tcu_load_image_form (&form, &SAMPLE);     /* Load form from image file */
  73.  
  74.    record = malloc (tcu_form_record_size (&form));  /* Grab a buffer for a record */
  75.  
  76.    tcu_get_field_id (&form, "name", &name_id);   /* Get name field ID */
  77.    tcu_get_field_id (&form, "time", &time_field); /* Get time field ID */
  78.  
  79.    tcu_get_form_info (&form, &f_info);         /* Get form information */
  80.    pos_x = 81-f_info.width;                    /* Calculate where to put */
  81.    pos_y = 26-f_info.height;                   /* the form. */
  82.  
  83.    tcu_set_form_fnkey_fn (&form, fn_keys);     /* Establish fn. key handler */
  84.    tcu_set_form_help (&form, help);            /* Set form help function */
  85.    tcu_set_field_verify (&form, test_age);     /* To verify age field */
  86.  
  87.    tcu_set_user_key_handler (keyboard_handler);
  88.  
  89.    tcu_set_menu_option (&mymenu, 4, 0);        /* Disable menu option 4 */
  90.  
  91.    select = tcu_display_menu (&mymenu, 5, 5);  /* Display the menu */
  92.  
  93.    buf[2] = '\0';       /* Empty starting string for name prompt */
  94.  
  95. /* Now get the user's menu selction. Keep getting it until an escape key */
  96. /* is pressed or the 'Go back to DOS' option is selcted (select = 6).    */
  97.  
  98.    while ((select = tcu_read_menu_selection (&mymenu))> 0 && select < 6) {
  99.  
  100.       if (select == 5) {   /* ...about this demo... */
  101.  
  102.          tcu_prepare_notice (&my_notice,
  103.                              "ABOUT THIS DEMO...",
  104.                              tcu_colour_attrib (BLUE, LIGHTGRAY),
  105.                          tcu_colour_attrib (LIGHTBLUE, LIGHTGRAY),
  106.                              tcu_colour_attrib (BLUE, LIGHTGRAY),
  107.                              TCU_BOX_DOUBLE);
  108.  
  109.          tcu_notice_text (&my_notice, "");
  110.          tcu_notice_text (&my_notice,
  111.                   "This is a small demonstration to show the use of the");
  112.          tcu_notice_text (&my_notice,
  113.                   "TCU facilities including menus, notices, prompts and");
  114.          tcu_notice_text (&my_notice,
  115.                   "forms.  Full descriptions of the functions are in the");
  116.          tcu_notice_text (&my_notice,
  117.                   "documentation files provided.  For additional support");
  118.          tcu_notice_text (&my_notice,
  119.                   "& queries, contact:");
  120.          tcu_notice_text (&my_notice, "");
  121.          tcu_notice_text (&my_notice, "      Karl Keyte");
  122.          tcu_notice_text (&my_notice, "      KKEYTE@ESOC.BITNET");
  123.          tcu_notice_text (&my_notice, "");
  124.  
  125.          tcu_display_notice (&my_notice, 20, 14);  /* Put the notice up */
  126.  
  127.          tcu_clear_notice (&my_notice);            /* Get rid of notice definition */
  128.  
  129.       } else {   /* ...otherwise get ready for user's input */
  130.  
  131.          tcu_prepare_notice (&my_notice,
  132.                              "",
  133.                              0,
  134.                          tcu_colour_attrib (BLACK, GREEN),
  135.                              tcu_colour_attrib (WHITE, GREEN),
  136.                              TCU_BOX_SINGLE);
  137.  
  138.          tcu_notice_text (&my_notice,
  139.                   "Customer Name :");   /* Prompt bit for input */
  140.  
  141.          buf[0] = 30;    /* Maximum length of input string */
  142.          tcu_prompt_input (&my_notice,
  143.                            17, 1, buf, tcu_colour_attrib (YELLOW, RED));
  144.  
  145.          tcu_display_notice (&my_notice,
  146.                          20, 6+select);   /* Put up prompt & get string */
  147.  
  148.          tcu_clear_notice (&my_notice);       /* Get rid of prompt definition */
  149.  
  150.          if ((signed char) buf[1] != -1) {
  151.  
  152.             fval.v_string = buf+2;             /* Load form name field with */
  153.             tcu_put_field (&form, name_id, &fval); /* the string just obtained */
  154.  
  155.             tcu_set_field_mode (&form, name_id, TCU_FORM_NOEDIT); /* No changing name! */
  156.             if (select == 3)
  157.                tcu_set_form_mode (&form, TCU_FORM_NOEDIT);
  158.             else
  159.                tcu_set_form_mode (&form, TCU_FORM_EDIT);
  160.  
  161.             fval.v_int = 0;
  162.             tcu_put_field (&form, time_field, &fval);
  163.  
  164.             tcu_set_idle_loop (idler);
  165.  
  166.             tcu_display_form (&form, pos_x, pos_y); /* Display the form and allow */
  167.             tcu_edit_form (&form, 1, &rkey);        /* interactive edits */
  168.  
  169.             tcu_set_idle_loop (NULL);
  170.  
  171.             tcu_remove_form (&form);                /* Remove the form from screen */
  172.  
  173.          }
  174.       }
  175.    }
  176.  
  177.  
  178.    tcu_unload_form (&form);       /* Unload the form definition from memory */
  179.    tcu_remove_menu (&mymenu);     /* Remove the menu from the screen */
  180.  
  181.  
  182. /* This next section is just to demonstrate the way escape keys work from */
  183. /* menus.  It shows what key was used to leave the menu */
  184.  
  185.  
  186.    if (select == 0)
  187.       printf ("Error in displaying menu!\n");
  188.    else
  189.       if (select < 0) {
  190.          select = -select;
  191.          switch (select) {
  192.             case TCU_ESC_ESC:    printf ("User cancelled with ESC key\n");
  193.                                  break;
  194.             case TCU_ESC_PGUP:   printf ("User escaped with PgUp key\n");
  195.                                  break;
  196.             case TCU_ESC_PGDN:   printf ("User escaped with PgDn key\n");
  197.                                  break;
  198.             case TCU_ESC_CLEFT:  printf ("User escaped with <--- key\n");
  199.                                  break;
  200.             case TCU_ESC_CRIGHT: printf ("User escaped with ---> key\n");
  201.                                  break;
  202.             case TCU_ESC_CNTL_C: printf ("User escaped with CNTL/C\n");
  203.                                  break;
  204.             case TCU_ESC_FUNC:   printf ("User escaped with F-%d key\n",
  205.                                          tcu_escape_fkey ());
  206.                                  break;
  207.             case TCU_ESC_USERKEY:printf ("User defined escape key\n");
  208.                                  break;
  209.             default:             printf ("??? Unknown escape key!\n");
  210.                                  break;
  211.          }
  212.       }
  213. }
  214.  
  215.  
  216. int far test_age (TCU_FORM *frm, int field, TCU_FIELD_VALUE *val)
  217. {
  218.    int                  age_fld,
  219.                         mstatus_id;
  220.    TCU_FIELD_VALUE      local_field;
  221.  
  222.    tcu_get_field_id (frm, "age", &age_fld);
  223.    if (field != age_fld)
  224.       return (1);
  225.  
  226.    if (val->v_int == 50) {
  227.  
  228.       tcu_prepare_notice (&my_notice,
  229.                           "",
  230.                           0,
  231.                           tcu_colour_attrib (YELLOW, RED),
  232.                           tcu_colour_attrib (YELLOW, RED),
  233.                           TCU_BOX_SINGLE);
  234.  
  235.       tcu_notice_text (&my_notice, "The AGE must be a number from 18 to");
  236.       tcu_notice_text (&my_notice, "120, excluding 50 which is a boring");
  237.       tcu_notice_text (&my_notice, "sort of age!");
  238.  
  239.       tcu_display_notice (&my_notice, 15, 9);
  240.       tcu_clear_notice (&my_notice);
  241.  
  242.       return (0);    /* Failed verification */
  243.    }
  244.  
  245. /* As a little demo., check if the age is less than 16, and if so set the */
  246. /* married status field to NO and disable changes in that field.  If the */
  247. /* age is 16 or over, ensure that the field is editable. */
  248.  
  249.    tcu_get_field_id (frm, "MSTATUS", &mstatus_id);
  250.    local_field.v_logical = 0;
  251.  
  252.    if (val->v_int < 16) {
  253.       tcu_put_field (frm, mstatus_id, &local_field);
  254.       tcu_set_field_mode (frm, mstatus_id, TCU_FORM_NOEDIT);
  255.    } else
  256.       tcu_set_field_mode (frm, mstatus_id, TCU_FORM_EDIT);
  257.  
  258.    return (1);       /* Passed verification */
  259. #pragma warn -par
  260. }
  261. #pragma warn .par
  262.  
  263.  
  264. void far help (TCU_FORM *form, int field)
  265. {
  266.    tcu_prepare_notice (&my_notice,
  267.                        "HELP",
  268.                        tcu_colour_attrib (BLUE, GREEN),
  269.                        tcu_colour_attrib (WHITE, GREEN),
  270.                        tcu_colour_attrib (WHITE, GREEN),
  271.                        TCU_BOX_DOUBLE);
  272.  
  273.    tcu_notice_text (&my_notice, "Field : %03d", field);
  274.    tcu_notice_text (&my_notice, "");
  275.    tcu_notice_text (&my_notice, "Please complete the form displayed and");
  276.    tcu_notice_text (&my_notice, "finish your entry with:");
  277.    tcu_notice_text (&my_notice, "");
  278.    tcu_notice_text (&my_notice, "    ESC   : To abort the entry");
  279.    tcu_notice_text (&my_notice, " or PgUp  : To complete the entry");
  280.    tcu_notice_text (&my_notice, "");
  281.    tcu_notice_text (&my_notice, "      PRESS RETURN TO CONTINUE");
  282.  
  283.    tcu_display_notice (&my_notice, 19, 6);
  284.    tcu_clear_notice (&my_notice);
  285. #pragma warn -par
  286. }
  287. #pragma warn .par
  288.  
  289.  
  290. /* Function key handler */
  291.  
  292. int far fn_keys (TCU_FORM *f, int field, int key)
  293. {
  294.    if (key == 11) {
  295.       FILE *of;
  296.       of = fopen ("demo.dat", "wb");
  297.       tcu_write_formrec (f, record);
  298.       fwrite (record, 155, 1, of);
  299.       fclose (of);
  300.       return (3);
  301.    }
  302.  
  303.    if (key == 12) {
  304.       FILE *inf;
  305.       inf = fopen ("demo.dat", "rb");
  306.       fread (record, 155, 1, inf);
  307.       fclose (inf);
  308.       tcu_read_formrec (f, record);
  309.       return (3);
  310.    }
  311.  
  312.    tcu_prepare_notice (&my_notice,
  313.                        "",
  314.                        0,
  315.                        tcu_colour_attrib (WHITE, GREEN),   /* Reject all function */
  316.                        tcu_colour_attrib (WHITE, GREEN),   /* keys. */
  317.                        TCU_BOX_DOUBLE);
  318.  
  319.    tcu_notice_text (&my_notice,
  320.                 "You just pressed F-%d on field %d which is undefined!",
  321.                 key, field);
  322.    tcu_display_notice (&my_notice, 15, 12);
  323.    tcu_clear_notice (&my_notice);
  324.  
  325.    return (0);          /* 0 means continue as if no key was pressed */
  326. #pragma warn -par
  327. }
  328. #pragma warn +par
  329.  
  330.  
  331. int far idler (unsigned long ticks)
  332. {
  333.    time_t               timer;
  334.    char                 ch_time[6];
  335.    TCU_FIELD_VALUE      val;
  336.  
  337.    if (!(ticks % 100)) {
  338.       tcu_save_environment ();
  339.       time (&timer);
  340.       strncpy (ch_time, ctime (&timer) + 11, 5);
  341.       ch_time[5] = '\0';
  342.       val.v_string = ch_time;
  343.       tcu_put_field (_TCU_UPDATE_form, time_field, &val);
  344.       tcu_restore_environment ();
  345.    }
  346.    return (0);      
  347. }
  348.  
  349.  
  350. /* Change ALT = into accept character and '~' into '?' */
  351.  
  352. int far keyboard_handler (unsigned short *scancode)
  353. {
  354.    if (*scancode == 0x8300)
  355.       return (1);
  356.    if ((*scancode & 0x00FF) == '~')
  357.       *scancode = '?';
  358.    return (0);
  359. }
  360.