'===========================================================================
' Subject: ADJUSTABLE WORD WRAPPER            Date: 06-02-97 (22:31)       
'  Author: Brian Bacon                        Code: QB, QBasic, PDS        
'  Origin: kyberteknik@geocities.com        Packet: TEXT.ABC
'===========================================================================
'WordWrap by KyberTeknik
'yeah, I know this is a really slow demo, but I didn't want to take
'time make it fast.
'
' I got the idea from a word wrapping routine I found somewhere that
' was about 150 lines long. It was fancier, however, but this is a
' smaller version I wrote from scratch the works just as good.
'
' Permission is granted to use this code in your programs if you
' check out my web page... (if you can.. otherwise just use it
' anyways) --->  www.geocities.com/SiliconValley/Lakes/2213
'                kyberteknik@geocities.com
'
DECLARE SUB WordWrap (text$, x1, x2, y1)
a$ = "hello this is some sample text. hahaha"
a$ = a$ + " I made a wordwraper! really, I dont mean any of "
a$ = a$ + "this, this is just sample text...  but this does seem "
a$ = a$ + "to be a fast and SMALL way to word wrap text..."
a$ = a$ + "but dont take the text down to small... or it will screw up."
a$ = a$ + " Press 6 to increase the line length, 4 to decrease it. 'q'"
a$ = a$ + " to end"

I = 30
DO
  CLS
  WordWrap a$, 1, I, 1
  DO: b$ = INKEY$: LOOP UNTIL b$ <> ""
  IF b$ = "6" THEN
     I = I + 1
     IF I > 80 THEN I = 80
  END IF
  IF b$ = "4" THEN
     I = I - 1
     IF I < 25 THEN I = 25
  END IF
  IF b$ = "q" OR b$ = "Q" THEN END
LOOP

SUB WordWrap (text$, x1, x2, y1)
    savetext$ = text$
    s = x2 - x1
    y = y1
    LOCATE y, x1
    IF LEN(text$) > s THEN
       DO
         IF LEN(text$) = 0 THEN EXIT DO
         text$ = LTRIM$(text$)
         IF LEN(text$) >= s THEN
            tmp$ = LEFT$(text$, s)
         ELSE
            tmp$ = text$
         END IF
         stmp$ = tmp$
         GOSUB ReverseTmp
         IF MID$(text$, s + 1, 1) = " " THEN
            PRINT stmp$
            text$ = MID$(text$, s + 1)
         ELSE
         a = INSTR(tmp$, " ")
         IF a = 0 THEN
            PRINT stmp$
            text$ = MID$(text$, s + 1)
         ELSE
            PRINT LEFT$(text$, s - a)
            text$ = MID$(text$, s - a + 1)
         END IF
         END IF
         y = y + 1
         LOCATE y, x1
       LOOP
    ELSE
       PRINT s
    END IF
    text$ = savetext$
    EXIT SUB

ReverseTmp:
    t$ = ""
    FOR I = LEN(tmp$) TO 1 STEP -1
        t$ = t$ + MID$(tmp$, I, 1)
    NEXT I
    tmp$ = t$
    RETURN
END SUB

