home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1996 / ARCHIVE_96.iso / discs / mag_discs / volume_4 / issue_08 / hints / TXT_T
Text File  |  1995-06-23  |  5KB  |  130 lines

  1. Using the txt Library in 'C'
  2.  
  3. David Scott
  4.  
  5. This article was inspired by the article 'Introduction to 'C' ù Part 5,
  6. in Archive Volume 3, Issue 6, March 1990. This gave a complete RISC-OS
  7. application using the libraries supplied with Release 3 of Acorn C. In
  8. particular it used the 'txt' library to provide a window to display text
  9. generated by the sample program. This requires a minimum of effort by
  10. the programmer since the library looks after most of the problems.
  11.  
  12. Although it works as described, it also has two major disadvantages. The
  13. first is the slow speed during text generation. The second is the
  14. operation of the window controls. In particular, the cursor control keys
  15. cannot be used to move the text through the window, the close icon has
  16. no effect and the vertical scroll bars can only be dragged. This article
  17. sets out techniques which overcome these problems.
  18.  
  19. Improving text generation speed
  20.  
  21. This turns out to be a very simple modification since the cause of the
  22. slow operation is the redrawing of the window for every item added to
  23. the text buffer using, for example, the txt_insertstring function. Two
  24. extra lines are required; the first turns off the display updates when
  25. text generation starts, the second turns it back on when the operation
  26. is complete. The lines shown below should be inserted immediately after
  27. the visdelay_begin() statement and immediately before the visdelay_end()
  28. statement in the original program function sysvars_to_text().
  29.  
  30. /* turn off display update */ txt_setcharoptions(t, txt_DISPLAY, FALSE);
  31.  
  32. /* turn on display update */ txt_setcharoptions(t, txt_DISPLAY, TRUE);
  33.  
  34. Improving text window control
  35.  
  36. This requires rather more code but again the principle is fairly
  37. straightforward. Firstly an event handler has to be registered for the
  38. text window following its successful creation by the txt_new() function
  39. using the following statement:
  40.  
  41. /* register the text window event handler */ txt_eventhandler(t,
  42. user_txevent, NULL);
  43.  
  44. This registers the function user_txevent which will be called to process
  45. text window events.
  46.  
  47. The function itself has to process all the events which the user
  48. requires. A sample function is given below which is commented to show
  49. which events are being processed. The keyboard key macro definitions
  50. given in 'akbd.h' are used for consistency but in addition the 'Home'
  51. key must also be defined using a macro as this is omitted from 'akbd.h'.
  52. The actual key values required are defined in the Programmers Reference
  53. Manual, Volume III, page 1198 and the macro definitions are given in
  54. file 'akbd.h'. Note, however, that the definitions given for both
  55. akbd_PageUpK and akbd_PageDownK are wrong so I have not used these but
  56. used their correct definition in the following code. The value
  57. txt_EXTRACODE is added to the key value to represent the equivalent
  58. window operation. A full list of these is given on page 325 of the ANSI
  59. C Release 3.
  60.  
  61. #include "akbd.h"
  62.  
  63. #define HOME        (30)
  64.  
  65. /**************************************** user_txevent    text window
  66. event handler
  67.         t               text object
  68.         h               event handle
  69. ****************************************/ void user_txevent(txt t, void
  70. *h) {
  71.   int lines; /* number of lines in window */
  72.  
  73.   h = h;
  74.   while (txt_queue(t) > 0)
  75.   {
  76.     /* find number of lines visible in window */
  77.     lines = txt_visiblelinecount(t);
  78.  
  79.     /* process the next user event code */
  80.     switch (txt_get(t))
  81.     {
  82.       case txt_EXTRACODE + akbd_Fn + 127:
  83.         /* close window icon */
  84.         txt_hide(t);
  85.         break;
  86.       
  87.       case akbd_UpK:
  88.       case txt_EXTRACODE + akbd_UpK:
  89.       case txt_EXTRACODE + akbd_Sh + akbd_Ctl + akbd_UpK:
  90.         /* scroll up one line */
  91.         txt_movevertical(t, -1, TRUE);
  92.         break;
  93.       
  94.       case akbd_DownK:
  95.       case txt_EXTRACODE + akbd_DownK:
  96.       case txt_EXTRACODE + akbd_Sh + akbd_Ctl + akbd_DownK:
  97.         /* scroll down one line */
  98.         txt_movevertical(t, 1, TRUE);
  99.         break;
  100.       
  101.       case akbd_Sh + akbd_UpK:
  102.       case txt_EXTRACODE + akbd_Sh + akbd_UpK:
  103.         /* scroll up one page */
  104.         txt_movevertical(t, -lines, FALSE);
  105.         break;
  106.       
  107.       case akbd_Sh + akbd_DownK:
  108.       case txt_EXTRACODE + akbd_Sh + akbd_DownK:
  109.         /* scroll down one page */
  110.         txt_movevertical(t, lines, FALSE);
  111.         break;
  112.       
  113.       case akbd_Ctl + akbd_UpK:
  114.       case HOME:
  115.         /* move to start of text */
  116.         txt_setdot(t, 0);
  117.         break;
  118.       
  119.       case akbd_Ctl + akbd_DownK:
  120.       case akbd_Sh + akbd_CopyK:
  121.         /* move to end of text */
  122.         txt_setdot(t, txt_size(t));
  123.         break;
  124.       
  125.       default:
  126.         break;
  127.     }
  128.   }
  129.   return; }
  130.