home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / utilsa / cword / CWORDSRC.TXT < prev    next >
Text File  |  1994-07-17  |  3KB  |  137 lines

  1. Comments on source code for CWORD.DYL
  2. -------------------------------------
  3.  
  4. The .CAT file just defines one class which in turn only has one
  5. method:
  6.  
  7.   LIBRARY cword
  8.   
  9.   EXTERNAL olib
  10.   
  11.   INCLUDE olib.g
  12.   
  13.   CLASS cword root
  14.       {
  15.       ADD cword_count
  16.       }
  17.  
  18. The working of the "hook" command (via the ws_launch_dyl method of
  19. the WSERV class) is that the named DYL is loaded, then the first
  20. class in that DYL is instantiated, and then the first method of that
  21. object is invoked.
  22.  
  23. The basic supplied code
  24. -----------------------
  25.  
  26. /* CWORDC.C
  27.  
  28.   Copyright (C) Psion PLC 1994
  29.   Started by : DavidW
  30.   Started on:  17/7/94
  31. */
  32.  
  33. #include <p_std.h>
  34. #include <epoc.h>
  35. #include <edit.g>
  36. #include <scrimg.g>
  37. #include <edwin.g>
  38. #include <cword.g>
  39.  
  40. GLREF_D PR_EDWIN *DatApp1;
  41.  
  42. LOCAL_C INT IsWordProcess(VOID)
  43.     {
  44.     TEXT pName[E_MAX_NAME+2];
  45.  
  46.     p_pname(p_getpid(),&pName[0]);
  47.     return(pName[0]=='W' && pName[1]=='O' && pName[2]=='R'
  48.                          && pName[3]=='D' && pName[4]=='.');
  49.     }
  50.  
  51. LOCAL_C UINT CountWordsInSelectBlock(VOID)
  52.     {
  53.     UWORD selLen;
  54.     UWORD pos;
  55.     UWORD selEnd;
  56.     UWORD count;
  57.     VOID *doc;
  58.  
  59.     selLen=p_send3(DatApp1->edwin.scrimg,O_SI_GET_SELECT,&pos);
  60.     if (!selLen)
  61.         return(0);
  62.     selEnd=pos+selLen;
  63.     doc=DatApp1->edwin.doc;
  64.     count=1;
  65.     FOREVER
  66.         {
  67.         p_send4(doc,O_EP_SCAN_WORD,&pos,EP_SCAN_TO_BEGIN|EP_SCAN_JOIN_DELIM);
  68.         if (pos>selEnd)
  69.             break;
  70.         count++;
  71.         }
  72.     return(count);
  73.     }
  74.  
  75. LOCAL_C VOID TellUser(UINT count)
  76.     {
  77.     TEXT buf[8];
  78.  
  79.     buf[p_itob(&buf[0],count)]=0;
  80.     wInfoMsg(&buf[0]);
  81.     }
  82.  
  83. #pragma METHOD_CALL
  84.  
  85. METHOD INT cword_cword_count(PR_CWORD *self,HANDLE cathand)
  86.     {
  87.     if (IsWordProcess())
  88.         TellUser(CountWordsInSelectBlock());
  89.     return(TRUE);     /* system will tidy everything up */
  90.     }
  91.  
  92. How this works
  93. --------------
  94. The only knowledge this code has about the Word Processor is that
  95.   1. The main Word Processor editing window is a subclass of EDWIN
  96.   2. The handle of this object is written to DatApp1
  97.  
  98. Before looking at DatApp1, however, the code checks that it has been
  99. invoked from within an instance of the Word Processor.  (The macro
  100. launching this DYL could include a line "xpWORD" to much the same
  101. effect.)
  102.  
  103. On establishing that the process is a Word Processor, the code finds
  104. the two ends of the current selected region of text (if any), and
  105. then uses EP_SCAN_WORD to walk along this region, word at a time.
  106.  
  107. Telling the user the result
  108. ---------------------------
  109. The above version of the routine TellUser results in a barely
  110. noticeable InfoPrint.  Better alternatives would include
  111.  
  112.   * building up a longer text string to display
  113.   * using p_notify to obtain a dialog with the result
  114.   * use a little more knowledge about the Word Processor
  115.  
  116. The third option is what is adopted in the actual shipped version of
  117. CWORD.DYL, in which the code for TellUser is as follows:
  118.  
  119.   LOCAL_C VOID TellUser(UINT count)
  120.       {
  121.       hErrorDialog(0,105,count);
  122.       }
  123.  
  124. which also requires the additional header declaration
  125.  
  126.   #include <hwim.h>
  127.  
  128. This relies on the facts that
  129.  
  130. 1. hErrorDialog(0,...) functions like a kind of dialog version of puts
  131.  
  132. 2. Resource 105 for the word processor is (in English) the format
  133. string "%d Words"
  134.  
  135. (strictly speaking an f_leave should be done on the result of
  136. hErrorDialog).
  137.