home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / him / wndemo1.c < prev   
Encoding:
C/C++ Source or Header  |  1988-12-01  |  16.8 KB  |  493 lines

  1. /***************** Program Source Module ***********************/
  2. #ifdef AUTOPDOC
  3.  
  4. MODULE:     wndemo1.c
  5.  
  6. DESCRIPTION:
  7.  
  8. Demo and test program for the HIM Window Manager wninput() function.
  9.  
  10. Will create 2 windows.  One for explaining what is going on, and the other to
  11. input ALL contortions possible of the various input types the wninput function
  12. can handle.  This is a tester program as well as a demo.
  13.  
  14. #endif
  15. /*
  16.  *
  17.  *   Allsoft (tm)
  18.  *   100 Calle Playa Del Sol NE
  19.  *   Albuquerque, NM  87109
  20.  *
  21.  *
  22.  *   Use any or all of this code in your applications.
  23.  *
  24.  */
  25. /********** Filled by Polytron Version Control System **********
  26.  
  27. $Author:   james borders  $
  28.  
  29. $Date:   01 Dec 1988 12:07:46  $
  30.  
  31. $Revision:   1.0  $
  32.  
  33. $Log:   D:/C/FMLIB/WN/VCS/WNDEMO1.C  $
  34.  * 
  35.  *    Rev 1.0   01 Dec 1988 12:07:46   james borders
  36.  * Initial revision.
  37.  
  38. ****************************************************************/
  39.  
  40. /*** Include Files ***/
  41.  
  42. #include    "stdio.h"
  43. #include    "ctype.h"
  44. #ifdef MSC
  45. #include    "malloc.h"
  46. #endif
  47. #ifdef TURBOC
  48. #include    "alloc.h"
  49. #endif
  50. #include    "stdarg.h"
  51. #include    "string.h"
  52. #include    "lm.h"
  53. #include    "km.h"
  54. #include    "wn.h"
  55.  
  56.  
  57. /*** Global Vars ***/
  58. /*
  59.     We need to give the wninput function somewhere to read and write the data
  60.     so we'll point the fielddef_s dptr to one of these global variables.
  61.  
  62.     In a normal application you would probably want to create a structure of
  63.     data items for each data entry screen (name, address, city, state...) and
  64.     set the dptr field in your fielddef_s structure to point to the appropriate
  65.     field in your application structure.
  66. */
  67.  
  68. char                gchar;
  69. char                gcharstr[500];
  70. int                 gint;
  71. unsigned int        guint;
  72. long int            glint;
  73. unsigned long int   gulint;
  74. float               gfloat;
  75. double              gdouble;
  76.  
  77. /*** Typedefs ***/
  78.  
  79. /*
  80.     This structure will be used by the wnprocessscr() function found in this
  81.     file.  Create 1 array of this structure for each screen in your application
  82.     and you've got all of your data entry done!
  83.  
  84.     If you need even more powerful data validation or display capabilities,
  85.     just use the HIM Forms Manager.
  86. */
  87.  
  88. struct  fielddef_s  {       /* complete data prompt and input field spec */
  89.     char    *desc;          /* what to print in status window */
  90.     char    *dptr;          /* where does the data live */
  91.     char    *prompt;        /* what to ask */
  92.     int     row,col;        /* where to ask it */
  93.     int     dtype;          /* complete data type of input */
  94.     int     len;            /* mix some too small, default, and too big */
  95.     int     precision;      /* if float or double, make sure to mix.  */
  96.     char    *charset;       /* do some unsigned inputs with dtype == WNINT */
  97.     int     (*action)();    /* do our own action for some fields */
  98.     };
  99.  
  100. /*
  101.     NOTE: the *desc field above is only used by the wnprocessscr() function
  102.           to display the data type that is being input.  Modify the
  103.           wnprocessscr() function to NOT display this info if you use it in
  104.           your programs.
  105. */
  106.  
  107. /*** Constants ***/
  108.  
  109. int     inaction1();
  110. int     inaction2();
  111.  
  112. int     swnum;  /* screen window number */
  113. int     dwnum;  /* description window number */
  114.  
  115. struct fielddef_s fdefs1[] = {
  116.     /*
  117.         All the Default inputs...
  118.     */
  119.     {   "Default WNCHAR..", &gchar,
  120.         "Char : ", 1,2, WNCHAR, 0, 0, "", WNACTIONNULL
  121.     },
  122.     {   "Default WNCHARSTR, w/WNCRCHOP..", gcharstr,
  123.         "Charstr : ", 2,2, WNCHARSTR | WNCRCHOP, 0, 0, "", WNACTIONNULL
  124.     },
  125.     {   "Default WNINT, w/WNWIPE..", (char *)&gint,
  126.         "Integer : ", 3,2, WNINT | WNWIPE, 0, 0, "", WNACTIONNULL
  127.     },
  128.     {   "Default WNUINT..", (char *)&guint,
  129.         "Unsigned Integer : ", 4, 2, WNUINT, 0, 0, "", WNACTIONNULL
  130.     },
  131.     {   "Default WNLINT..", (char *)&glint,
  132.         "Long Integer : ", 5,2, WNLINT, 0, 0, "", WNACTIONNULL
  133.     },
  134.     {   "Default WNULINT..", (char *)&gulint,
  135.         "Unsigned Long Integer : ", 6, 2, WNULINT, 0, 0, "", WNACTIONNULL
  136.     },
  137.     {   "Default WNFLOAT.., 0 precision", (char *)&gfloat,
  138.         "Float : ", 7, 2, WNFLOAT, 0, 0, "", WNACTIONNULL
  139.     },
  140.     {   "Default WNDOUBLE.., 1 precision", (char *)&gdouble,
  141.         "Double : ", 8, 2, WNDOUBLE, 0, 1, "", WNACTIONNULL
  142.     }
  143. };
  144.  
  145. struct fielddef_s fdefs2[] = {
  146.  
  147.     /*
  148.         Now do it again with short and extra input field lengths, charsets
  149.         and our own action routine.
  150.     */
  151.     {   "WNCHAR with input = 5..", &gchar,
  152.         "Char : ", 1,2, WNCHAR, 5, 0, "", WNACTIONNULL
  153.     },
  154.     {   "WNCHARSTR with input = 10, charset == ABC xyz..", gcharstr,
  155.         "Charstr : ", 2,2, WNCHARSTR, 10, 0, "ABC xyz", inaction1
  156.     },
  157.     {   "WNINT with input == 15..", (char *)&gint,
  158.         "Integer : ", 3,2, WNINT, 15, 0, "", WNACTIONNULL
  159.     },
  160.     {   "WNUINT with input == 3..", (char *)&guint,
  161.         "Unsigned Integer : ", 4, 2, WNUINT, 3, 0, NULL, inaction1
  162.     },
  163.     {   "WNLINT with input == 27, digits 0123789..", (char *)&glint,
  164.         "Long Integer : ", 5,2, WNLINT, 27, 0, "0123789", inaction1
  165.     },
  166.     {   "WNULINT with input == default, digits 01..", (char *)&gulint,
  167.         "Unsigned Long Integer : ", 6, 2, WNULINT, 0, 0, "01", WNACTIONNULL
  168.     },
  169.     {   "WNFLOAT with input == 6, precision == 2.., no minus sign", (char *)&gfloat,
  170.         "Float : ", 7, 2, WNFLOAT, 6, 2, "0123456789+.eE ", WNACTIONNULL
  171.     },
  172.     {   "WNDOUBLE with input == default, precision == 5, no plus/minus..", (char *)&gdouble,
  173.         "Double : ", 8, 2, WNDOUBLE, 0, 5, "0123456789. e E", WNACTIONNULL
  174.     }
  175.   
  176. };
  177.  
  178. struct fielddef_s fdefs3[] = {
  179.  
  180.     /*
  181.         Use prefilled default data...
  182.         The global data holders will have to be filled in before fdefs3
  183.         is processed.
  184.     */
  185.     {   "WNCHAR with default == Y, charset == 'ynYN'", &gchar,
  186.         "Char : ", 1,2, WNCHAR | WNDPTRDISPLAY, 0, 0, "ynYN", inaction2
  187.     },
  188.     {   "WNCHARSTR with input = 11, default == hello world, wipe active", gcharstr,
  189.         "Charstr : ", 2,2, WNCHARSTR | WNDPTRDISPLAY | WNWIPE, 11, 0, "", inaction2
  190.     },
  191.     {   "WNINT with default == -12345", (char *)&gint,
  192.         "Integer : ", 3,2, WNINT | WNDPTRDISPLAY, 0, 0, "", inaction2
  193.     },
  194.     {   "WNUINT with default == 65432", (char *)&guint,
  195.         "Unsigned Integer : ", 4, 2, WNUINT | WNDPTRDISPLAY, 0, 0, NULL, inaction2
  196.     },
  197.     {   "WNLINT with default == 9873210, charset == 0123789", (char *)&glint,
  198.         "Long Integer : ", 5,2, WNLINT | WNDPTRDISPLAY, 0, 0, "0123789", inaction2
  199.     },
  200.     {   "WNULINT with default == 4294967295", (char *)&gulint,
  201.         "Unsigned Long Integer : ", 6, 2, WNULINT | WNDPTRDISPLAY, 0, 0, NULL, inaction2
  202.     },
  203.     {   "WNFLOAT with default == -123456.12 , precision == 2", (char *)&gfloat,
  204.         "Float : ", 7, 2, WNFLOAT | WNDPTRDISPLAY, 0, 2, NULL, inaction2
  205.     },
  206.     {   "WNDOUBLE with default == -9999999999.98765, len == 20, precision == 5", (char *)&gdouble,
  207.         "Double : ", 8, 2, WNDOUBLE | WNDPTRDISPLAY, 20, 5, "", inaction2
  208.     }
  209. };
  210.  
  211.  
  212. #define     numdefs(a)     ( sizeof(a) / sizeof(struct fielddef_s) )
  213.  
  214. /*** Macros ***/
  215.  
  216. main()
  217. /****/
  218. {
  219. int kbhit(), getch();
  220.  
  221. lminit((char *(*)())malloc,free,0);     /* init list manager with debug off */
  222. wninit(0,0,NULL,0,(char *(*)())malloc,free); /* window manager saves to memory */
  223. kminit(kbhit,getch);                         /* keyboard manager */
  224. wninputtest();
  225. };
  226.  
  227.  
  228. wninputtest()
  229. /***********/
  230. /*
  231.     assumes that the list, keyboard, and window manager have been initialized.
  232. */
  233. {
  234. if ((swnum = wncreate(0,0,80,13,WNBLUE,WNCYAN,WNBLUE,WNCYAN)) < 0){
  235.     printf("\nCan't create window..");
  236.     return(-1);
  237.     }
  238. wnswscroll(swnum,0);    /* so last line printing won't scroll window */
  239.  
  240. if ((dwnum = wncreate(13,0,80,12,WNBLACK,WNCYAN,WNBLACK,WNCYAN)) < 0){
  241.     printf("\nCan't create window..");
  242.     return(-1);
  243.     }
  244. wnttitle(dwnum,"[ WNINDEMO V1.0 ]");
  245. explaintest();          /* explain this program */
  246.  
  247. wnprintf(dwnum,"\nTesting input fields with default len, precision, charset, & action");
  248. wnprocessscr(swnum,fdefs1,numdefs(fdefs1));
  249. checkkey("Hit a key for next screen, ESC to quit...");
  250.  
  251. wncls(dwnum);
  252. wnprintf(dwnum,"\nTesting input fields with different display lengths and options..");
  253. wnprocessscr(swnum,fdefs2,numdefs(fdefs2));
  254. checkkey("Hit a key for next screen, ESC to quit...");
  255.  
  256. wncls(dwnum);
  257. wnprintf(dwnum,"\nTesting input fields with different display lengths, options, ");
  258. wnprintf(dwnum,"\nand default data.");
  259. wnprintf(dwnum,"\n\nThe action routine supplied for these fields will interpret");
  260. wnprintf(dwnum,"\nUp/Down arrows as \"bad characters\" and cause processing to move");
  261. wnprintf(dwnum,"\nbetween fields..");
  262. fillglobalsdef3();
  263. wnprocessscr(swnum,fdefs3,numdefs(fdefs3));
  264. checkkey("Hit a key to end demo/test of wninput()...");
  265. wndestroy(swnum);
  266. wndestroy(dwnum);
  267. return(0);
  268. };
  269.  
  270. explaintest()
  271. /************/
  272. {
  273. wnprintf(dwnum,"\nThe HIM Window Manager wninput() function is a powerful, easy to use routine");
  274. wnprintf(dwnum,"\nfor obtaining data from a user of your application.  The code contained in");
  275. wnprintf(dwnum,"\nthis demo is sufficient to allow your application to input almost any type");
  276. wnprintf(dwnum,"\nof data.  Feel free to modify any part of it for your own use.");
  277. checkkey("Press a key to see wninput synopsis, ESC to quit..");
  278. wncls(dwnum);
  279. wnprintf(dwnum,"\nint     wninput(wnum,dtype,dptr,len,precision,charset,badinaction);\n");
  280.  
  281. wnprintf(dwnum,"\nint     wnum;               window number to input from");
  282. wnprintf(dwnum,"\nint     dtype;              data type to input");
  283. wnprintf(dwnum,"\nchar    *dptr;              pointer to data");
  284. wnprintf(dwnum,"\nint     len;                size of data if char, else input buffer size");
  285. wnprintf(dwnum,"\nint     precision;          decimal precision if float or double");
  286. wnprintf(dwnum,"\nchar    *charset;           allowable characters, NULL for default");
  287. wnprintf(dwnum,"\nint     (*badinaction)();   routine to call if bad input attempted");
  288. checkkey("Press a key to continue, ESC to quit..");
  289. wncls(dwnum);
  290.  
  291. wnprintf(dwnum,"\nAll of the C data types can be input as well as precision if getting");
  292. wnprintf(dwnum,"\nfloating point data.  The len parameter gives you control over the size");
  293. wnprintf(dwnum,"\nof the input area.  The charset string you supply determines what");
  294. wnprintf(dwnum,"\ncharacters are valid for input.");
  295. wnprintf(dwnum,"\n\nBefore we test the wninput() function remember, if you can't do");
  296. wnprintf(dwnum,"\nthe EXACT validation you would like using the code in this demo,");
  297. wnprintf(dwnum,"\ncheck out the HIM Forms Manager.  It WILL handle any data entry");
  298. wnprintf(dwnum,"\njob you have in mind.");
  299. checkkey("Press a key to continue, ESC to quit..");
  300. wncls(dwnum);
  301.  
  302. wnprintf(dwnum,"\nSome of the inputs have action routines which will");
  303. wnprintf(dwnum,"\ntell you the current type, code, and current buffer.");
  304. wnprintf(dwnum,"\n\nYou must press ESC when done reading the action routine");
  305. wnprintf(dwnum,"\ntext.");
  306. checkkey("Press a key to continue, ESC to quit..");
  307. wncls(dwnum);
  308. return(0);
  309. };
  310.  
  311. checkkey(msg)
  312. /***********/
  313. char *msg;
  314. {
  315. char buf[80];
  316. sprintf(buf,"[ %s ]",msg);
  317. wnbtitle(dwnum,buf);
  318. if (kmgetch() == KESC){
  319.     wndestroy(swnum);
  320.     wndestroy(dwnum);
  321.     exit(0);
  322.     };
  323. wnbtitle(dwnum,"");
  324. return(0);
  325. };
  326.  
  327.  
  328. wnprocessscr(wnum,fdefs,ndefs)
  329. /******************************/
  330. int wnum, ndefs;
  331. struct fielddef_s *fdefs;
  332. {
  333. int i, code;
  334.  
  335.  
  336. wncls(wnum);
  337. for (i = 0; i < ndefs; i++){                    /* display the prompts */
  338.     switch(fdefs[i].dtype & WNDTYPEMASK){
  339.         case WNCHAR:  case WNCHARSTR: case WNINT:   case WNUINT:
  340.         case WNLINT:  case WNULINT:   case WNFLOAT: case WNDOUBLE:
  341.             break;
  342.         default:
  343.             wnprintf(wnum,"\nIllegal dtype in fielddef structure.  Press a key...");
  344.             kmgetch();
  345.             return(0);
  346.         }
  347.     wnlputs(wnum, fdefs[i].row, fdefs[i].col, fdefs[i].prompt);
  348.     code = wninput(wnum, fdefs[i].dtype | WNDISPLAYONLY, fdefs[i].dptr,
  349.                          fdefs[i].len, fdefs[i].precision, fdefs[i].charset,
  350.                          fdefs[i].action);
  351.     }
  352.  
  353. i = 0;
  354. while (i < ndefs){
  355.     /*
  356.         Take out this wnprintf() line for your application...
  357.     */
  358.     wnprintf(dwnum,"\n%s",fdefs[i].desc);
  359.  
  360.     wnlputs(wnum,fdefs[i].row,fdefs[i].col,fdefs[i].prompt); /* to place cursor */
  361.     code = wninput(wnum, fdefs[i].dtype, fdefs[i].dptr, fdefs[i].len,
  362.                    fdefs[i].precision, fdefs[i].charset, fdefs[i].action);
  363.     /*
  364.         take out this switch if using wnprocessscr() in your code
  365.     */
  366.     switch(fdefs[i].dtype & WNDTYPEMASK){
  367.         case    WNCHAR:     wnprintf(dwnum,"\nRcode is [%d], Data is [%c]",code,*fdefs[i].dptr); break;
  368.         case    WNCHARSTR:  wnprintf(dwnum,"\nRcode is [%d], Data is [%s]",code,fdefs[i].dptr); break;
  369.         case    WNINT:      wnprintf(dwnum,"\nRcode is [%d], Data is [%d]",code,*(int *)fdefs[i].dptr); break;
  370.         case    WNUINT:     wnprintf(dwnum,"\nRcode is [%d], Data is [%u]",code,*(unsigned int *)fdefs[i].dptr); break;
  371.         case    WNLINT:     wnprintf(dwnum,"\nRcode is [%d], Data is [%ld]",code,*(long int *)fdefs[i].dptr); break;
  372.         case    WNULINT:    wnprintf(dwnum,"\nRcode is [%d], Data is [%lu]",code,*(unsigned long int *)fdefs[i].dptr); break;
  373.         case    WNFLOAT:    wnprintf(dwnum,"\nRcode is [%d], Data is [%f]",code,*(float *)fdefs[i].dptr); break;
  374.         case    WNDOUBLE:   wnprintf(dwnum,"\nRcode is [%d], Data is [%lf]",code,*(double *)fdefs[i].dptr); break;
  375.         }
  376.     /*
  377.         now check the return code to see what should be done next..
  378.     */
  379.     switch(code){
  380.         case    WNOK:
  381.         case    WNCR:                   /* user entered CR with data */
  382.             i++;
  383.             if (i == ndefs)
  384.                 return(0);
  385.             break;
  386.         case    KUP:                    /* up arrow */
  387.             i = (i > 0)?i-1:i;
  388.             break;
  389.         case    KDOWN:                  /* down arrow */
  390.             i = (i < (ndefs -1))?i+1:i;
  391.             break;
  392.         case    KESC:                   /* returned by action routine on ESC */
  393.         case    WNESC:                  /* returned by default action on ESC */
  394.             return(0);
  395.         }
  396.     };
  397. return(0);
  398. };
  399.  
  400.  
  401. fillglobalsdef3()
  402. /***************/
  403. /*
  404.     This routine is called before the processing of field screen 3 in order
  405.     to demonstrate that the fields can be filled with data prior to display
  406.     and input.
  407. */
  408. {
  409. gchar = 'Y';
  410. strcpy(gcharstr,"hello world");
  411. gint = -12345;
  412. guint = 65432;
  413. glint = 9873210L;
  414. gulint = 1431655765L * 3L; /* msc problem, constant too big. */
  415. gfloat = -123456.12;
  416. gdouble = -9999999999.98765;
  417. return(0);
  418. };
  419.  
  420.  
  421. inaction1(wnum,dtype,code,key,curbuf)
  422. /*******************************/
  423. /*
  424.     One of the action routines that will get called when bad input is seen
  425.     by the wninput function.
  426. */
  427. int  wnum, dtype, code, key;
  428. char *curbuf;
  429. {
  430. char buf[80];
  431. char *chartype;
  432. char *codetype;
  433.  
  434. switch(dtype & WNDTYPEMASK){
  435.     case    WNCHAR:     chartype = "WNCHAR    "; break;
  436.     case    WNCHARSTR:  chartype = "WNCHARSTR "; break;
  437.     case    WNINT:      chartype = "WNINT     "; break;
  438.     case    WNUINT:     chartype = "WNUINT    "; break;
  439.     case    WNLINT:     chartype = "WNLINT    "; break;
  440.     case    WNULINT:    chartype = "WNULINT   "; break;
  441.     case    WNFLOAT:    chartype = "WNFLOAT   "; break;
  442.     case    WNDOUBLE:   chartype = "WNDOUBLE  "; break;
  443.     }
  444. switch(code){
  445.     case    WNCR:       codetype = "WNCR "; break;
  446.     case    WNESC:      codetype = "WNESC "; break;
  447.     case    WNBADCHAR:  codetype = "WNBADCHAR "; break;
  448.     }
  449. /*
  450.     Display the parameters to the user and wait for a key.  ESC will cause
  451.     editing to resume, anything else will abort the field.
  452. */
  453. sprintf(buf,"[ %s %s %s ]",chartype,codetype,curbuf);
  454. wnbtitle(wnum,buf);
  455. code = kmgetch();
  456. wnbtitle(wnum,"");
  457. if (code == KESC)
  458.     return(-1);
  459. else
  460.     return(code);
  461. };
  462.  
  463. inaction2(wnum,dtype,code,key,curbuf)
  464. /*******************************/
  465. /*
  466.     This action routine is used by the 3rd field screen group.  Since we want
  467.     the wnprocessscr() routine to be able to see the KUP and KDOWN keys we
  468.     need to supply this action routine.  Otherwise the default action would
  469.     be to beep at the user and continue editing.
  470. */
  471. int  wnum, dtype, code, key;
  472. char *curbuf;
  473. {
  474. switch(code){
  475.     case    WNCR:
  476.         wnprintf(wnum,"\007");
  477.         return(-1);
  478.     case    WNESC:
  479.         return(KESC);
  480.     case    WNBADCHAR:
  481.         if ((key == KUP) || (key == KDOWN) || (key == KESC))
  482.             return(key);
  483.         else{
  484.             wnprintf(wnum,"\007");
  485.             return(-1);
  486.             }
  487.     }
  488. return(-1);
  489. };
  490.  
  491.  
  492.  
  493.