home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Archive Magazine 1996
/
ARCHIVE_96.iso
/
discs
/
mag_discs
/
volume_4
/
issue_08
/
hints
/
TXT_T
Wrap
Text File
|
1995-06-23
|
5KB
|
130 lines
Using the txt Library in 'C'
David Scott
This article was inspired by the article 'Introduction to 'C' ù Part 5,
in Archive Volume 3, Issue 6, March 1990. This gave a complete RISC-OS
application using the libraries supplied with Release 3 of Acorn C. In
particular it used the 'txt' library to provide a window to display text
generated by the sample program. This requires a minimum of effort by
the programmer since the library looks after most of the problems.
Although it works as described, it also has two major disadvantages. The
first is the slow speed during text generation. The second is the
operation of the window controls. In particular, the cursor control keys
cannot be used to move the text through the window, the close icon has
no effect and the vertical scroll bars can only be dragged. This article
sets out techniques which overcome these problems.
Improving text generation speed
This turns out to be a very simple modification since the cause of the
slow operation is the redrawing of the window for every item added to
the text buffer using, for example, the txt_insertstring function. Two
extra lines are required; the first turns off the display updates when
text generation starts, the second turns it back on when the operation
is complete. The lines shown below should be inserted immediately after
the visdelay_begin() statement and immediately before the visdelay_end()
statement in the original program function sysvars_to_text().
/* turn off display update */ txt_setcharoptions(t, txt_DISPLAY, FALSE);
/* turn on display update */ txt_setcharoptions(t, txt_DISPLAY, TRUE);
Improving text window control
This requires rather more code but again the principle is fairly
straightforward. Firstly an event handler has to be registered for the
text window following its successful creation by the txt_new() function
using the following statement:
/* register the text window event handler */ txt_eventhandler(t,
user_txevent, NULL);
This registers the function user_txevent which will be called to process
text window events.
The function itself has to process all the events which the user
requires. A sample function is given below which is commented to show
which events are being processed. The keyboard key macro definitions
given in 'akbd.h' are used for consistency but in addition the 'Home'
key must also be defined using a macro as this is omitted from 'akbd.h'.
The actual key values required are defined in the Programmers Reference
Manual, Volume III, page 1198 and the macro definitions are given in
file 'akbd.h'. Note, however, that the definitions given for both
akbd_PageUpK and akbd_PageDownK are wrong so I have not used these but
used their correct definition in the following code. The value
txt_EXTRACODE is added to the key value to represent the equivalent
window operation. A full list of these is given on page 325 of the ANSI
C Release 3.
#include "akbd.h"
#define HOME (30)
/**************************************** user_txevent text window
event handler
t text object
h event handle
****************************************/ void user_txevent(txt t, void
*h) {
int lines; /* number of lines in window */
h = h;
while (txt_queue(t) > 0)
{
/* find number of lines visible in window */
lines = txt_visiblelinecount(t);
/* process the next user event code */
switch (txt_get(t))
{
case txt_EXTRACODE + akbd_Fn + 127:
/* close window icon */
txt_hide(t);
break;
case akbd_UpK:
case txt_EXTRACODE + akbd_UpK:
case txt_EXTRACODE + akbd_Sh + akbd_Ctl + akbd_UpK:
/* scroll up one line */
txt_movevertical(t, -1, TRUE);
break;
case akbd_DownK:
case txt_EXTRACODE + akbd_DownK:
case txt_EXTRACODE + akbd_Sh + akbd_Ctl + akbd_DownK:
/* scroll down one line */
txt_movevertical(t, 1, TRUE);
break;
case akbd_Sh + akbd_UpK:
case txt_EXTRACODE + akbd_Sh + akbd_UpK:
/* scroll up one page */
txt_movevertical(t, -lines, FALSE);
break;
case akbd_Sh + akbd_DownK:
case txt_EXTRACODE + akbd_Sh + akbd_DownK:
/* scroll down one page */
txt_movevertical(t, lines, FALSE);
break;
case akbd_Ctl + akbd_UpK:
case HOME:
/* move to start of text */
txt_setdot(t, 0);
break;
case akbd_Ctl + akbd_DownK:
case akbd_Sh + akbd_CopyK:
/* move to end of text */
txt_setdot(t, txt_size(t));
break;
default:
break;
}
}
return; }