home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / tui160 / tui_tut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-02  |  14.7 KB  |  426 lines

  1. /****************************************************************************/
  2. /* TUI_TUT.C                                                                */
  3. /*                                                                          */
  4. /* This file is the InTUItion version of the UltraWin tutorial UW_TUT10.C.  */
  5. /* Here we show how much easier it is to use InTUItion for all the user     */
  6. /* interface work, especially for the data entry!                           */
  7. /****************************************************************************/
  8. /* #define SOURCE_TRACE 1 */                 /* uncomment for SOURCE_TRACE  */
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <io.h>
  12. #ifndef __TURBOC__
  13. #include <sys\types.h>
  14. #endif
  15. #include <sys\stat.h>
  16. #include <time.h>
  17. #include <ctype.h>
  18. #include "t.h"                               /* include the main TUI header */
  19. #include "tui_tut.def"                       /* include the TUI definitions */
  20.  
  21. #define MAX_CUST 50
  22. #define TO_TUI   1
  23. #define FROM_TUI 2
  24.  
  25. typedef struct cust
  26. {
  27.   int  status;
  28.   int  cust_no;
  29.   char business[34];
  30.   char name[34];
  31.   char addr[34];
  32.   char city[34];
  33.   char state[4];
  34.   char zip[10];
  35.   char phone[16];
  36.   char fax[16];
  37.   char date[10];
  38.   char memo[34];
  39.   char unused[26];                                /* round out to 256 bytes */
  40. } CUST;
  41.  
  42. /*----------------------------- global vars --------------------------------*/
  43. PRINT Print;
  44. CUST  Customers[MAX_CUST], *Cp;
  45. char  Path[80], Filename[13] = "CUST.DAT", Fullname[80];
  46. int   Print_stat, End_flag, Curr_cust;
  47. TUI   Main_tui;
  48.  
  49. /*----------------------------- prototypes ---------------------------------*/
  50. int  main(void);
  51. int  disp_time(void);
  52. void do_cust_dialog( TUI *tuip );
  53. void do_main_menu( TUI *tuip );
  54. void copy_cust( CUST *cp, TUI *tuip, int dir );
  55. int  file_load(CUST *customers);
  56. int  file_save(CUST *customers);
  57. void print_cust(CUST *cp, PRINT *p);
  58.  
  59.  
  60. /*********/
  61. /* ~main */
  62. /*       ********************************************************************/
  63. /* The main function.  Replace the call to load_tui with a call to the      */
  64. /* appropriate register function generated by the TUICP to have your TUI    */
  65. /* linked in.                                                               */
  66. /****************************************************************************/
  67. int main( void )
  68. {
  69.   CUST *cp;
  70.  
  71.   if ( !load_tui(&Main_tui, "TUI_TUT.TUI") )
  72.   {
  73.     printf("Sorry, could not load the TUI file!\n");
  74.     return(0);
  75.   }
  76.   init_tui(&Main_tui);
  77.   init_clock(0x3333);
  78.  
  79.   /*------------------------ initialize the printer -------------------------*/
  80.   if( init_printer("temp.dat", NULL, 2048L, 2048L, &Print) )
  81.     Print_stat = 1;
  82.  
  83.   /*-------------- initialize first customer as EnQue Software --------------*/
  84.   cp = &Customers[0];
  85.   strcpy(cp->business, "EnQue Software"); 
  86.   strcpy(cp->name, "Kevin Huck & Boyd Gafford");  
  87.   strcpy(cp->addr, "Rt. 1 Box 116C"); 
  88.   strcpy(cp->city, "Pleasant Hill");  
  89.   strcpy(cp->state, "MO");  
  90.   strcpy(cp->zip, "64080"); 
  91.   strcpy(cp->phone, "(816)987-2515"); 
  92.   strcpy(cp->fax, "(816)987-2515");      
  93.   strcpy(cp->date, "09/11/92");      
  94.   strcpy(cp->memo, "BBS 816-353-0991"); 
  95.   copy_cust(cp, &Main_tui, TO_TUI);
  96.  
  97.   set_idle_func(disp_time);               /* set background clock function  */
  98.  
  99.   do_cust_dialog(&Main_tui);
  100.  
  101.   end_clock();
  102.   end_tui(&Main_tui);
  103.   free_tui(&Main_tui);
  104.   return(1);
  105. }
  106. /*** end of main ***/
  107.  
  108. /*******************/
  109. /* ~do_cust_dialog */
  110. /*                 **********************************************************/
  111. /* Shell code for typical dialog interaction.  A pointer to the TUI and the */
  112. /* define for the dialog are passed.                                        */
  113. /*                                                                          */
  114. /* Use DO_INPUT_ADV for forms, DO_NORMAL for normal processing, and 'or' in */
  115. /* DO_RETURN_EVENT to have all unprocessed events returned to you!          */
  116. /****************************************************************************/
  117. void do_cust_dialog( TUI *tuip )
  118. {
  119.   int old_cust = -1;
  120.   int dlg_inx = CUSTOMER_DIALOG, ret_inx, end_flag = OFF;
  121.   char *search;
  122.   WINDOW *wnp;
  123.  
  124.   set_slider(tuip, SLIDER, (long) Curr_cust, 10L, 49L);
  125.   draw_dialog(tuip, dlg_inx);
  126.   wnp = get_dlg_wnp(tuip, CUSTOMER_DIALOG);
  127.   Cp = &Customers[Curr_cust];
  128.   while (!end_flag && !End_flag)
  129.   {
  130.     set_slider(tuip, SLIDER, (long) Curr_cust, 10L, 50L);
  131.     if( Curr_cust != old_cust )
  132.       copy_cust(Cp, tuip, FROM_TUI);
  133.     Cp = &Customers[Curr_cust];
  134.     if( Curr_cust != old_cust )
  135.     {
  136.       copy_cust(Cp, tuip, TO_TUI);                /* DONT CALL DRAW DIALOG  */
  137.       refresh_dialog(tuip, dlg_inx);              /* force redraw of fields */
  138.       old_cust = Curr_cust;
  139.     }
  140.     mv_cs(19, 2, wnp);                            /* display cust number    */
  141.     wn_printf(wnp, "%3d", Curr_cust+1);
  142.  
  143.     ret_inx = do_dialog(tuip, dlg_inx, DO_RETURN_EVENT | DO_RETURN_CMD);
  144.     switch( ret_inx )
  145.     {
  146.       /*----------------------- ESC pressed, or click off! -----------------*/
  147.       case NO_SELECTION:
  148.         do_main_menu(tuip);
  149.         break;
  150.  
  151.       case T_MENU:
  152.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  153.         do_main_menu(tuip);
  154.         break;
  155.         
  156.       /*----------------------- an event not processed ---------------------*/
  157.       case EVENT_RETURNED:
  158.         if (Event.is_mouse)
  159.            do_main_menu(tuip);
  160.         else 
  161.           switch( Event.key )
  162.         {
  163.           /*--------------------- process cursor keys ----------------------*/
  164.           case KEY_HOME:
  165.             Curr_cust = 0;           
  166.             break;
  167.           case KEY_END:  
  168.             Curr_cust = MAX_CUST-1;  
  169.             break;
  170.           case KEY_DN:
  171.             if( Curr_cust < MAX_CUST-1 )
  172.               Curr_cust++;
  173.             break;
  174.           case KEY_UP:
  175.             if( Curr_cust > 0 )
  176.               Curr_cust--;
  177.             break;
  178.           case KEY_PGUP:
  179.             if( Curr_cust >= 10 )
  180.               Curr_cust -= 10;
  181.             else
  182.               Curr_cust = 0;
  183.             break;
  184.           case KEY_PGDN:
  185.             if( Curr_cust < MAX_CUST-11 )
  186.               Curr_cust += 10;
  187.             else
  188.               Curr_cust = MAX_CUST-1;
  189.             break;
  190.         }
  191.         break;
  192.  
  193.       case SLIDER:
  194.         Curr_cust = (int) get_sldr_pos(tuip,SLIDER);
  195.         break;
  196.  
  197.       case T_HELP:
  198.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  199.       default:
  200.          search = "";
  201.         switch(ret_inx)
  202.         {
  203.           case LABEL_BUSINESS:  search = "///Business"; break;
  204.           case LABEL_NAME:      search = "///Name";     break;
  205.           case LABEL_ADDRESS:   search = "///Address";  break;
  206.           case LABEL_CITY:      search = "///City";     break;
  207.           case LABEL_STATE:     search = "///State";    break;
  208.           case LABEL_ZIP:       search = "///Zip";      break;
  209.           case LABEL_PHONE:     search = "///Phone";    break;
  210.           case LABEL_FAX:       search = "///Fax";      break;
  211.           case LABEL_DATE:      search = "///Date";     break;
  212.           case LABEL_MEMO:      search = "///Memo";     break;
  213.          }
  214.          tui_help(2, 3, 78, 21, "tutor.hlp", search, 0);
  215.         break;
  216.     }
  217.   }
  218.   erase_dialog(tuip, dlg_inx);
  219. }
  220. /*** end of do_cust_dialog ***/
  221.  
  222. /*****************/
  223. /* ~do_main_menu */
  224. /*               ************************************************************/
  225. /* This is the function that allows the user to interact with the main top  */
  226. /* menu.                                                                    */
  227. /****************************************************************************/
  228. void do_main_menu( TUI *tuip )
  229. {
  230.   int i, dlg_inx = MAIN_MENU, ret_inx, end_flag = OFF;
  231.  
  232.   draw_dialog(tuip, dlg_inx);
  233.   while (!end_flag)
  234.   {
  235.     ret_inx = do_dialog(tuip, dlg_inx, DO_NORMAL | DO_RETURN_EVENT);
  236.     end_flag = ON;
  237.     switch( ret_inx )
  238.     {
  239.       /*-------------------- process file menu items -----------------------*/
  240.       case LOAD_F:
  241.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  242.         file_load(Customers);
  243.         copy_cust(Cp, tuip, TO_TUI);
  244.         refresh_dialog(tuip, CUSTOMER_DIALOG);
  245.         break;
  246.  
  247.       case SAVE_F:
  248.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  249.         file_save(Customers);
  250.         break;
  251.  
  252.       case QUIT:
  253.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  254.         End_flag = 1;
  255.         break;
  256.  
  257.      /*--------------------- process edit menu items -----------------------*/
  258.       case CLEAR_CURRENT:
  259.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  260.         setmem(Cp, sizeof(CUST), 0);
  261.         copy_cust(Cp, tuip, TO_TUI);
  262.         refresh_dialog(tuip, CUSTOMER_DIALOG);
  263.         break;
  264.       case CLEAR_ALL:
  265.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  266.         setmem(Customers, sizeof(Customers), 0);
  267.         copy_cust(Cp, tuip, TO_TUI);
  268.         refresh_dialog(tuip, CUSTOMER_DIALOG);
  269.         break;
  270.  
  271.       /*---------------------- process print menu items --------------------*/
  272.       case PRINT_CURRENT:
  273.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  274.         if( Print_stat )
  275.           print_cust(Cp, &Print);
  276.         break;
  277.       case PRINT_ALL:
  278.         deselect(tuip, ret_inx), draw_item(tuip, ret_inx);
  279.         if( Print_stat )
  280.           for( i = 0; i < MAX_CUST; i++ )
  281.             if( strlen(Customers[i].name) )                 /* not empty?   */
  282.               print_cust(&Customers[i], &Print);
  283.         break;
  284.  
  285.       default:
  286.         if( Event.is_mouse )              /* prevent dialog from redrawing  */
  287.           end_flag = OFF;                 /* rapidly                        */
  288.         break;
  289.     }
  290.   }
  291.   erase_dialog(tuip, dlg_inx);
  292. }
  293. /*** end of do_main_menu ***/
  294.  
  295. /**************/
  296. /* ~copy_cust */
  297. /*            ***************************************************************/
  298. /* copies customer info to/from customer record to tui...                   */
  299. /****************************************************************************/
  300. void copy_cust( CUST *cp, TUI *tuip, int dir )
  301. {
  302.   if( dir == FROM_TUI )
  303.   {
  304.     strcpy(cp->business, get_inp_text(tuip,BUSINESS));
  305.     strcpy(cp->name,     get_inp_text(tuip,NAME));
  306.     strcpy(cp->addr,     get_inp_text(tuip,ADDRESS));
  307.     strcpy(cp->city,     get_inp_text(tuip,CITY));
  308.     strcpy(cp->state,    get_inp_text(tuip,STATE));
  309.     strcpy(cp->zip,      get_inp_text(tuip,ZIP));
  310.     strcpy(cp->phone,    get_inp_text(tuip,PHONE));
  311.     strcpy(cp->fax,      get_inp_text(tuip,FAX));
  312.     strcpy(cp->date,     get_inp_text(tuip,DATE));
  313.     strcpy(cp->memo,     get_inp_text(tuip,MEMO));
  314.   }else{
  315.     strcpy(get_inp_text(tuip,BUSINESS), cp->business);
  316.     strcpy(get_inp_text(tuip,NAME    ), cp->name);
  317.     strcpy(get_inp_text(tuip,ADDRESS ), cp->addr);
  318.     strcpy(get_inp_text(tuip,CITY    ), cp->city);
  319.     strcpy(get_inp_text(tuip,STATE   ), cp->state);
  320.     strcpy(get_inp_text(tuip,ZIP     ), cp->zip);
  321.     strcpy(get_inp_text(tuip,PHONE   ), cp->phone);
  322.     strcpy(get_inp_text(tuip,FAX     ), cp->fax);
  323.     strcpy(get_inp_text(tuip,DATE    ), cp->date);
  324.     strcpy(get_inp_text(tuip,MEMO    ), cp->memo);
  325.   }
  326. }
  327. /*** end of copy_cust ***/
  328.  
  329.  
  330. /**************/
  331. /* ~disp_time */
  332. /*            ***************************************************************/
  333. /*  This routine is called in the background by wait_event and will display */
  334. /*  the time once per second.  Notice the use of the global variables       */
  335. /*  Uw_timers.  There is an array of four "countdown" timers that are user  */
  336. /*  accessible.  Each "timer tic" will decrement the counts by one, until   */
  337. /*  0 is reached.  By "reloading" the timer with "Tics_per_sec", we only    */
  338. /*  display the time of day once per second.  "Tics_per_sec" is set         */
  339. /*  by init_clock.                                                          */
  340. /****************************************************************************/
  341. int disp_time(void)
  342. {
  343.   time_t t;
  344.   WINDOW *wnp = Main_tui.back_wnp;
  345.  
  346.   print_in_bkgrnd();                            /* call this to print       */
  347.   if( !Uw_timers[0] )                           /* has one second passed?   */
  348.   {
  349.     Uw_timers[0] = Tics_per_sec;                /* if so, reload timer      */
  350.     t = time(NULL);                             /* get time                 */
  351.     mv_cs(55, 24, wnp);                         /* move window cursor       */
  352.     wn_st_qty(ctime(&t), 24, wnp);              /* output 24 characters     */
  353.     return(1);
  354.   }
  355.   return(0);
  356. }
  357. /*** end of disp_time ***/
  358.  
  359. /**************/
  360. /* ~file_load */
  361. /*            ***************************************************************/
  362. /*  This routine loads a file from disk into the customer array...          */
  363. /****************************************************************************/
  364. int file_load(CUST *customers)
  365. {
  366.   FILE *fp;
  367.  
  368.   if ( file_selected("*.DAT", Path, Filename, Fullname) )
  369.   {
  370.     if( (fp = fopen(Fullname, "rb")) != NULL )
  371.     {
  372.       fread(customers, sizeof(CUST), MAX_CUST, fp);
  373.       fclose(fp);
  374.       tone(1024,10);
  375.       return(1);
  376.     }
  377.   }
  378.   return(0);
  379. }
  380. /*** end of file_load ***/
  381.  
  382. /**************/
  383. /* ~file_save */
  384. /*            ***************************************************************/
  385. /*  This routine saves a file to disk from the customer array...            */
  386. /****************************************************************************/
  387. int file_save(CUST *customers)
  388. {
  389.   FILE *fp;
  390.  
  391.   if ( file_selected("*.DAT", Path, Filename, Fullname) )
  392.   {
  393.     if( (fp = fopen(Fullname, "wb")) != NULL )
  394.     {
  395.       fwrite(customers, sizeof(CUST), MAX_CUST, fp);
  396.       fclose(fp);
  397.       tone(1024,10);
  398.       return(1);
  399.     }
  400.   }
  401.   return(0);
  402. }
  403. /*** end of file_save ***/
  404.  
  405. /***************/
  406. /* ~print_cust */
  407. /*             **************************************************************/
  408. /*  This routine prints a customer in the desired window...                 */
  409. /****************************************************************************/
  410. void print_cust(CUST *cp, PRINT *p)
  411. {
  412.   print_printf( p, "Business: %-32s\r\n", cp->business);
  413.   print_printf( p, "Name    : %-32s\r\n", cp->name );
  414.   print_printf( p, "Address : %-32s\r\n", cp->addr );
  415.   print_printf( p, "City    : %-32s State: %2s  Zip: %5s\r\n",
  416.     cp->city, cp->state, cp->zip );
  417.   print_printf( p, "Phone   : %-16s\r\n", cp->phone );
  418.   print_printf( p, "Fax     : %-16s\r\n", cp->fax );
  419.   print_printf( p, "Date    : %-10s\r\n", cp->date );
  420.   print_printf( p, "Memo    : %-32s\r\n", cp->memo );
  421.   print_char(12,p);                                     /* print form feed  */
  422. }
  423. /*** end of print_cust ***/
  424.  
  425. /*** END OF FILE ***/
  426.