home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / t / tcu_32.zip / TCU32_DB.ZIP / DBDEMO.C < prev    next >
C/C++ Source or Header  |  1991-08-01  |  9KB  |  280 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <usr\tcu.h>
  5.  
  6.  
  7. static TCU_DB            db;
  8.  
  9.  
  10. void main (void)
  11. {
  12.    int               rkey;
  13.    char             *keys[] = { "LName" , "FName" , "Date_Reg" , NULL };
  14.    TCU_FORM          form;
  15.    TCU_FORM_INFO     finfo;
  16.    TCU_WINDOW        win;
  17.  
  18.    int  far          button_handler (TCU_FORM *, int);
  19.    void far          help_handler (TCU_FORM *, int);
  20.  
  21. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  22.  
  23. /* Open the form object file */
  24. /* ------------------------- */
  25.  
  26.    if (tcu_load_form (&form, "dbform") != TCU_OK) {
  27.       printf ("DBDEMO: Error opening form 'dbform.cfo'\n");
  28.       exit (1);
  29.    }
  30.  
  31.  
  32. /* Set up the button and help handlers for the form */
  33. /* ------------------------------------------------ */
  34.  
  35.    tcu_set_button_fn (&form, button_handler);
  36.    tcu_set_form_help (&form, help_handler);
  37.  
  38.  
  39. /* Try to open existing database, else try to create new one */
  40. /* --------------------------------------------------------- */
  41.  
  42.    if (tcu_db_open (&db, &form, "demo_db", 2) == TCU_ERROR)
  43.       if (tcu_db_create (&db, &form, "demo_db", 2, keys) == TCU_ERROR) {
  44.          tcu_unload_form (&form);
  45.          printf ("DBDEMO: Cannot open or create database\n");
  46.          exit (1);
  47.       }
  48.  
  49.  
  50. /* Set the search options for the database */
  51. /* --------------------------------------- */
  52.  
  53.    tcu_db_set_search_mode (&db, TCU_DB_IGNORE_CASE);
  54.    tcu_db_set_search_indices (&db, 2);
  55.  
  56.  
  57. /* Open window as background */
  58. /* ------------------------- */
  59.  
  60.    tcu_open_window (&win, 1, 1, 80, 25, "",
  61.                     tcu_colour_attrib (LIGHTGREEN, BLUE),
  62.                     tcu_colour_attrib (0, BLUE),
  63.                     tcu_colour_attrib (0, BLUE),
  64.                     TCU_BOX_SINGLE);
  65.  
  66.  
  67. /* Get form info. and use it to display the form in the middle at the top */
  68. /* ---------------------------------------------------------------------- */
  69.  
  70.    tcu_get_form_info (&form, &finfo);
  71.    tcu_display_form (&form, (81-finfo.width)/2, 2);
  72.  
  73.  
  74. /* All the work is now done by the button handler function.  Edit the form */
  75. /* and quit only when selected.  Start at the first database record.       */
  76. /* ----------------------------------------------------------------------- */
  77.  
  78.    tcu_db_first (&db);
  79.    tcu_db_read (&db);
  80.  
  81.    tcu_set_form_mode (&form, TCU_FORM_NOESCS);
  82.    do
  83.       tcu_edit_form (&form, 1, &rkey);
  84.    while (rkey != TCU_FLD_BUTTONESC);
  85.    
  86.  
  87. /* Finished!  Can remove database and form structures and terminate */
  88. /* ---------------------------------------------------------------- */
  89.  
  90.    tcu_db_close (&db);
  91.    tcu_unload_form (&form);
  92.    tcu_close_window (&win);
  93.  
  94.    exit (0);
  95. }
  96.  
  97.  
  98.  
  99. int far button_handler (TCU_FORM *form, int button)
  100. {
  101.    int                ret_val,
  102.                       stat;
  103.    TCU_NOTICE         nt;
  104.    unsigned char      bcol,
  105.                       ncol;
  106.  
  107. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  108.  
  109. /* Save the environment so we can use notices relative to the whole screen */
  110. /* ----------------------------------------------------------------------- */
  111.  
  112.    tcu_save_environment ();
  113.    tcu_db_start_form_edit (&db);
  114.  
  115.  
  116. /* If QUIT is selected, check that the user is really sure */
  117. /* ------------------------------------------------------- */
  118.  
  119.    if (button == tcu_get_field_id (form, "B_Quit", NULL))
  120.       if (tcu_get_confirm (22, 12,
  121.                            tcu_colour_attrib (WHITE,MAGENTA),
  122.                            tcu_colour_attrib (WHITE,MAGENTA),
  123.                            "Are you sure you want to quit (Y/N)?"))
  124.          ret_val = 2;
  125.       else
  126.          ret_val = 3;
  127.  
  128.    bcol = tcu_colour_attrib (WHITE,BLUE);
  129.    ncol = tcu_colour_attrib (YELLOW,BLUE);
  130.  
  131.    if (button == tcu_get_field_id (form, "B_First", NULL) ||
  132.        button == tcu_get_field_id (form, "B_Last", NULL)) {
  133.       if (button == tcu_get_field_id (form, "B_First", NULL))
  134.          stat = tcu_db_first (&db);
  135.       else
  136.          stat = tcu_db_last (&db);
  137.       if (stat == TCU_ERROR) {
  138.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  139.                              TCU_BOX_SINGLE);
  140.          tcu_notice_text (&nt, "Database is empty!");
  141.          tcu_display_notice (&nt, 29, 22);
  142.          tcu_clear_notice (&nt);
  143.          ret_val = 3;
  144.       } else {
  145.          tcu_db_read (&db);
  146.          ret_val = 1;
  147.       }
  148.    }
  149.  
  150.    if (button == tcu_get_field_id (form, "B_Next", NULL))
  151.       if (tcu_db_next (&db) == TCU_ERROR) {
  152.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  153.                              TCU_BOX_SINGLE);
  154.          tcu_notice_text (&nt, "END OF DATABASE");
  155.          tcu_display_notice (&nt, 30, 22);
  156.          tcu_clear_notice (&nt);
  157.          tcu_db_last (&db);
  158.          ret_val = 3;
  159.       } else {
  160.          tcu_db_read (&db);
  161.          ret_val = 1;
  162.       }
  163.  
  164.    if (button == tcu_get_field_id (form, "B_Prev", NULL))
  165.       if (tcu_db_previous (&db) == TCU_ERROR) {
  166.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  167.                              TCU_BOX_SINGLE);
  168.          tcu_notice_text (&nt, "START OF DATABASE");
  169.          tcu_display_notice (&nt, 29, 22);
  170.          tcu_clear_notice (&nt);
  171.          tcu_db_first (&db);
  172.          ret_val = 3;
  173.       } else {
  174.          tcu_db_read (&db);
  175.          ret_val = 1;
  176.       }
  177.  
  178.    if (button == tcu_get_field_id (form, "B_Add", NULL))
  179.       if (tcu_db_write (&db) == TCU_ERROR) {
  180.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  181.                              TCU_BOX_SINGLE);
  182.          tcu_notice_text (&nt, "DUPLICATES EXISTING RECORD");
  183.          tcu_display_notice (&nt, 25, 22);
  184.          tcu_clear_notice (&nt);
  185.          ret_val = 3;
  186.       } else {
  187.          tcu_prepare_notice (&nt, "New Record", bcol, bcol, ncol,
  188.                              TCU_BOX_SINGLE);
  189.          tcu_notice_text (&nt, "RECORD ADDED, %d RECORDS",
  190.                           tcu_db_record_count (&db));
  191.          tcu_display_notice (&nt, 26, 22);
  192.          tcu_clear_notice (&nt);
  193.          ret_val = 1;
  194.       }
  195.  
  196.    if (button == tcu_get_field_id (form, "B_Delete", NULL))
  197.       if (tcu_db_delete (&db) == TCU_ERROR) {
  198.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  199.                              TCU_BOX_SINGLE);
  200.          tcu_notice_text (&nt, "NO RECORD TO DELETE");
  201.          tcu_display_notice (&nt, 28, 22);
  202.          tcu_clear_notice (&nt);
  203.          ret_val = 3;
  204.       } else {
  205.          if (tcu_db_at_eof (&db))
  206.             tcu_db_last (&db);
  207.          if (!tcu_db_record_count (&db))
  208.             tcu_clear_form_fields (form);
  209.          else
  210.             tcu_db_read (&db);
  211.          ret_val = 1;
  212.       }
  213.  
  214.    if (button == tcu_get_field_id (form, "B_Search", NULL)) {
  215.       if (tcu_db_search (&db, NULL) == TCU_ERROR) {
  216.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  217.                              TCU_BOX_SINGLE);
  218.          tcu_notice_text (&nt, "NO SUCH RECORD FOUND");
  219.          tcu_display_notice (&nt, 28, 22);
  220.          tcu_clear_notice (&nt);
  221.          ret_val = 3;
  222.       } else {
  223.          tcu_db_read (&db);
  224.          ret_val = 1;
  225.       }
  226.    }
  227.  
  228.    if (button == tcu_get_field_id (form, "B_Update", NULL))
  229.       if (tcu_db_rewrite (&db) == TCU_ERROR) {
  230.          tcu_prepare_notice (&nt, "Warning", bcol, bcol, ncol,
  231.                              TCU_BOX_SINGLE);
  232.          tcu_notice_text (&nt, "RECORD DOES NOT EXIST");
  233.          tcu_display_notice (&nt, 28, 22);
  234.          tcu_clear_notice (&nt);
  235.          ret_val = 3;
  236.       } else
  237.          ret_val = 1;
  238.  
  239.    tcu_db_end_form_edit (&db);
  240.    tcu_restore_environment ();
  241.    return (ret_val);
  242. }
  243.  
  244.  
  245.  
  246. void far help_handler (TCU_FORM *form, int field)
  247. {
  248.    TCU_NOTICE           nt;
  249.    long                 recs;
  250.  
  251. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  252.  
  253.    tcu_prepare_notice (&nt, "HELP!",
  254.                        tcu_colour_attrib (WHITE, BLUE),
  255.                        tcu_colour_attrib (WHITE, BLUE),
  256.                        tcu_colour_attrib (YELLOW, BLUE),
  257.                        TCU_BOX_SINGLE);
  258.    tcu_notice_text (&nt, "Use the usual form editing facilities to complete");
  259.    tcu_notice_text (&nt, "form entries. Use the mouse (or keyboard & =) to");
  260.    tcu_notice_text (&nt, "perform the appropriate function:");
  261.    tcu_notice_text (&nt, "");
  262.    tcu_notice_text (&nt, "    FIRST     : Move to first record");
  263.    tcu_notice_text (&nt, "    LAST      : Move to last record");
  264.    tcu_notice_text (&nt, "    NEXT      : Move to next record");
  265.    tcu_notice_text (&nt, "    PREVIOUS  : Move to previous record");
  266.    tcu_notice_text (&nt, "    SEARCH    : Search for matching name");
  267.    tcu_notice_text (&nt, "    ADD       : Add new record");
  268.    tcu_notice_text (&nt, "    DELETE    : Delete displayed record");
  269.    tcu_notice_text (&nt, "    UPDATE    : Change an existing record");
  270.    tcu_notice_text (&nt, "    QUIT      : Exit the demonstration");
  271.    tcu_notice_text (&nt, "");
  272.    recs = tcu_db_record_count (&db);
  273.    tcu_notice_text (&nt, "There %s currently %ld record%s in the database",
  274.                     (recs == 1)? "is" : "are", recs, (recs == 1)? "" : "s");
  275.    tcu_display_notice (&nt, 14, 8);
  276.    tcu_clear_notice (&nt);
  277. #pragma warn -par
  278. }
  279. #pragma warn .par
  280.