Consultor Eletrônico



Kbase 195: SAMPLE PROGRAM to SCROLL up down left right thru text
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/05/1998
SAMPLE PROGRAM to SCROLL up down left right thru text

920401-elp01
INTRODUCTION:
=============
This Product Services Technical Support Knowledgebase entry
contains a program that illustrates how to have more than 78
characters of information to display on a terminal that does
not support more width than that. This procedure provides a
tool to display any ASCII file that is in full-line quoted
format (such as that provided by the quoter utility) to a
terminal. The display can be scrolled up, down, left, and
right.

PROCEDURAL APPROACH:
====================
Generate the information you need to display, such as through
a report procedure, to a disk file. Each line must be
a quoted string. Lines may be longer than 78 characters.

From the calling procedure, invoke the procedure below with
a RUN statement. For example:

RUN seekpage.p (INPUT "choose.doc").

The user can scroll throughout the file and can exit
at any time.

SAMPLE PROCEDURE:
=================

/****************************** START ********************************/
/* seekpage.p - */
/* Allows a user to page up/down/left/right in a physical file. */

DEF INPUT PARAMETER psc-file AS CHARACTER NO-UNDO. /* quoter'd file */
DEF VAR psc-ahead AS CHARACTER NO-UNDO. /* scrap char */
DEF VAR psc-back AS INTEGER NO-UNDO. /* seek back */
DEF VAR psc-down AS INTEGER NO-UNDO. /* down */
DEF VAR psc-hpos AS INTEGER INITIAL 1 NO-UNDO. /* horz pos */
DEF VAR psc-index AS INTEGER NO-UNDO. /* scrap int */
DEF VAR psc-line AS INTEGER NO-UNDO. /* scrn line */
DEF VAR psc-msg AS LOGICAL INITIAL FALSE NO-UNDO. /* shown msg */
DEF VAR psc-page AS INTEGER INITIAL 1 NO-UNDO. /* this page */
DEF VAR psc-scrap AS INTEGER NO-UNDO. /* scrap int */
DEF VAR psc-seek AS INTEGER EXTENT 1024 NO-UNDO. /* seek pos's */
DEF VAR psc-text AS CHARACTER NO-UNDO. /* text */
DEF VAR psc-width AS INTEGER INITIAL 1 NO-UNDO. /* width */
DEF STREAM file-stream.

psc-down = SCREEN-LINES - 3.

FORMAT
psc-text FORMAT "x(78)"
WITH FRAME box-text NO-ATTR-SPACE NO-LABELS OVERLAY
COLUMNS 1 ROW 2 psc-down DOWN.

/* psc-seek is an array which holds */
/* the seek positions for the first */
/* character of each page. This allows */
/* the user to page backwards. When the */
/* last page is reached, psc-seek[] */
/* for that page is set to negative of */
/* the number of lines on that page. */
ASSIGN
psc-seek = ?
psc-seek[1] = 0.

/* set up the screen... */
PAUSE 0.
DISPLAY "" @ psc-text WITH FRAME box-text.
STATUS DEFAULT.
MESSAGE "Press [" + KBLABEL("CURSOR-UP") + "] and ["
+ KBLABEL("CURSOR-DOWN") + "] to navigate, ["
+ KBLABEL("END-ERROR") + "] when done.".

INPUT STREAM file-stream FROM VALUE(psc-file) NO-ECHO.
DO WHILE TRUE:
/* go to the start of the current page */
SEEK STREAM file-stream TO psc-seek[psc-page].
UP FRAME-LINE(box-text) - 1 WITH FRAME box-text.

/* this is how many lines to read in */
psc-scrap = (IF psc-seek[psc-page + 1] < 0
THEN - psc-seek[psc-page + 1] ELSE psc-down).

/* Now read in lines. Special handling is done for eof. */
DO psc-line = 1 TO psc-scrap ON ERROR UNDO,
LEAVE ON ENDKEY UNDO,LEAVE:
psc-back = SEEK(file-stream).
IMPORT STREAM file-stream psc-text.

/* delete away cr's. */
psc-index = INDEX(psc-text,CHR(13)).
IF psc-index > 0 THEN
DO WHILE psc-index > 0:
ASSIGN
SUBSTRING(psc-text,psc-index,1) = ""
psc-index = INDEX(psc-text,CHR(13)).
END.
/* Elegantly handle ff's. */
IF psc-text BEGINS CHR(12) THEN
DO:
IF psc-line > 1 THEN UNDO,LEAVE.
psc-text = SUBSTRING(psc-text,2).
END.

DISPLAY SUBSTRING(psc-text,psc-hpos,78) @ psc-text
WITH FRAME box-text.

/* Track widest line read in, for scroll left/right message. */
psc-width = MAXIMUM(psc-width,LENGTH(psc-text)).

IF psc-line < psc-down THEN DOWN WITH FRAME box-text.
END.

/* This block handles checking to see if the next page is empty - */
/* meaning a ff ends the last page. This prevents an extra blank */
/* screen from appearing at the end of the report. */
IF SEEK(file-stream) <> ? THEN
DO:
ASSIGN
psc-scrap = SEEK(file-stream)
psc-ahead = "".
REPEAT WHILE psc-ahead = "":
psc-ahead = ?.
IMPORT STREAM file-stream psc-ahead.
IF psc-ahead BEGINS CHR(12) THEN psc-ahead =
SUBSTRING(psc-ahead,2).
END.
IF psc-ahead = ? THEN
psc-back = 1 - psc-line.
ELSE
SEEK STREAM file-stream TO psc-scrap.
END.

/* Now figure out where that page actually ended. */
IF psc-seek[psc-page + 1] = ? THEN
psc-seek[psc-page + 1] = (IF psc-line > psc-down THEN
SEEK(file-stream)
ELSE IF psc-text BEGINS CHR(12) THEN
psc-back
ELSE
1 - psc-line
).

/* This gets around PROGRESS' closing */
/* streams automatically when the eof is hit. */
IF SEEK(file-stream) = ? THEN
DO:
INPUT STREAM file-stream CLOSE.
INPUT STREAM file-stream FROM VALUE(psc-file) NO-ECHO.
END.

/* Display the page number. */
PUT SCREEN ROW SCREEN-LINES COLUMNS 70 "Page".
PUT SCREEN ROW SCREEN-LINES COLUMNS 75 STRING(psc-page,"ZZZZ").

/* If we have read in a line wider than 78 characters, let */
/* the user know that left/right scrolling is now enabled. */
IF psc-width > 78 AND NOT psc-msg THEN
DO:
psc-msg = TRUE.
HIDE MESSAGE NO-PAUSE.
MESSAGE "Press [" + KBLABEL("CURSOR-UP") + "] and ["
+ KBLABEL("CURSOR-DOWN") + "] to navigate,
[" + KBLABEL("END-ERROR") + "] when done.".
MESSAGE "Use [" + KBLABEL("CURSOR-LEFT") + "] and ["
+ KBLABEL("CURSOR-RIGHT") + "] to scroll report left/right".
END.

/* If text doesn't fill screen, clear empty */
/* lines below last displayed line. */
DO WHILE psc-line <= psc-down:
CLEAR FRAME box-text NO-PAUSE.
IF psc-line < psc-down THEN DOWN WITH FRAME box-text.
psc-line = psc-line + 1.
END.

/* Actions initiated here. */
READKEY.
IF (KEYFUNCTION(LASTKEY) MATCHES "*-DOWN"
OR KEYFUNCTION(LASTKEY) = "RETURN")
AND psc-seek[psc-page + 1] >= 0 AND psc-page < 1023 THEN
psc-page = psc-page + 1.
ELSE
IF KEYFUNCTION(LASTKEY) MATCHES "*-UP" AND psc-page > 1 THEN
psc-page = psc-page - 1.
ELSE
IF KEYFUNCTION(LASTKEY) = "HELP" THEN RUN applhelp.p.
ELSE
IF KEYFUNCTION(LASTKEY) = "HOME" THEN
ASSIGN
psc-page = 1
psc-hpos = 1.
ELSE
IF KEYFUNCTION(LASTKEY) MATCHES "*-LEFT" AND psc-hpos > 1 THEN
psc-hpos = psc-hpos - 20.
ELSE
IF KEYFUNCTION(LASTKEY) MATCHES "*-RIGHT"
AND psc-hpos < psc-width - 77 THEN psc-hpos = psc-hpos + 20.
ELSE
IF CAN-DO("GO,END-ERROR",KEYFUNCTION(LASTKEY)) THEN LEAVE.
END.


/* Close up shop. */
INPUT STREAM file-stream CLOSE.
HIDE FRAME box-text NO-PAUSE.
HIDE MESSAGE NO-PAUSE.


/* Go home. */
RETURN.

/******************************* END ********************************/

REFERENCES TO WRITTEN DOCUMENTATION:
====================================
Language Reference and Programming Handbook.

Progress Software Technical Support Note # 195