home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / chint / useful21.hnt < prev    next >
Encoding:
Text File  |  1993-10-29  |  3.7 KB  |  95 lines

  1. This hint shows how to add a user hook into the "list_n_choose()" function
  2. from DB_LIST.C.
  3.  
  4. PART I - How to insert the hook into the code of DB_LIST.C (DB_LIST.H)
  5. ----------------------------------------------------------------------
  6.  
  7. First introduce a "procedureal" type to DB_LIST.H, and an associated variable
  8. of this new type :--
  9.  
  10.   typedef void (*listuserhook)(int fno, int kno, long rno, char thekey);
  11.  
  12.   extern listuserhook _dblisthook;
  13.  
  14.  
  15. Also add the variable declaration to DB_LIST.C (which it where the "extern"al
  16. refers to) :--
  17.  
  18.   listuserhook _dblisthook;
  19.  
  20.  
  21. The idea is that the "_dblisthook" is to be called if any of the extended
  22. keyboard keys are pressed, (the function keys, <Ctrl>+Key etcetera).  The
  23. exceptions being those keys that have already got a purpose, (EG <Alt>+"Z"
  24. is already used for "Zoom").
  25.  
  26. Add in code to the "default" part of the switch statement that processes the
  27. "action" inside the "list_n_choose()" function :--
  28.  
  29.       default:
  30.         slen = strlen(search);
  31.         if (!_xkey) {
  32.           ...
  33.           refilltab();
  34.         }
  35.         else {                                                       /*MOD*/
  36.           if (_dblisthook != NULL) {                                 /*MOD*/
  37.                         fval = labs((*rectab)[((pno+midlist-2)%listlen)+1].ref); /*MOD*/
  38.             _dblisthook(fno,kno,fval,action);                        /*MOD*/
  39.             refilltab();                                             /*MOD*/
  40.           }                                                          /*MOD*/
  41.         }                                                            /*MOD*/
  42.       break;
  43.  
  44. Finally we need to initialize the user hook to "NULL" as, by default, there
  45. is no function defined to be called!!  In the unit initialization add :--
  46.  
  47.   _dblisthook = NULL;
  48.  
  49.  
  50. PART II  -  An example of how the hook might be used.
  51. -----------------------------------------------------
  52. Assume that you have a database, TEST, defined and in the fields for file 1
  53. there is a Display field called TAGFIELD with input picture "U".  This field
  54. is NOT part of any key.
  55.  
  56. Copy DBC.SKL to TEST.SKL and make the following modifications :--
  57.  
  58. Near the end of the code, just before the main program block add in the
  59. following function :--
  60.  
  61. void tagDuringList(int fno, int kno, long rno, uchar thekey)
  62. {
  63.   ptr savbuf;
  64.  
  65.   if ((fno == 1) && (thekey == F9)) {                /*use F9 to toggle TAG*/
  66.     savbuf = db_malloc(TEST1.datasize);     /*allocate a save buffer memory*/
  67.     memmove(savbuf,&TEST1,TEST1.datasize);    /*save existing record buffer*/
  68.     getrec(datf[fno],rno,&TEST1);/*Get the record highlighted in the "List"*/
  69.     if (TEST1.TAGFIELD[0] == '√')              /*if its a tick blank it out*/
  70.       TEST1.TAGFIELD[0] = ' ';
  71.     else
  72.       TEST1.TAGFIELD[0] = '√';                   /*otherwise make it a tick*/
  73.     putrec(datf[fno],rno,&TEST1);                     /*save change to disk*/
  74.     memmove(&TEST1,savbuf,TEST1.datasize);/*restore the saved record buffer*/
  75.     db_free(savbuf);                              /*free save buffer memory*/
  76.   }
  77. }
  78.  
  79.  
  80. Initialize the user hook into DB_LIST so it will point somewhere meaningful,
  81. that is initialize the procedureal pointer "_dblisthook" to the adddress of
  82. this function we've just implemented, "tagDuringList()".
  83.  
  84. Begin
  85.   ...
  86.     _keyexpr = keyexpr;
  87.     _getarec = getarec;
  88.     _horizrec = horizrec;
  89.   _dblisthook = tagDuringList;                                        /*MOD*/
  90.   ...
  91.  
  92.  
  93. The result of all this is that when we use the "Find" option for file 1 in
  94. the "TEST.EXE" application we will be able to toggle the "TAGFIELD" field
  95. between a tick, (√), and a space by pressing the <F9> function key.