' ***********************************************************
' PROGRAM:      haiku-hug.bac
' PURPOSE:      Small HAIKU program, based on FORTH
'                       version by Kent Peterson
' AUTHOR:               Peter van Eerten, February 2011 (GPL)
' MODDED:               GUI version by vovchik (Puppy Linux forum)
' COMMENTS:     Haiku generated in the style of Matsuo Basho,
'                       a Japanese poet of the 17th century.
' DEPENDS:      bash, bacon, hug (0.31)
' PLATFORM:     Puppy Linux (actually, any *nix)
' DATE:         15-02-2011
' VERSION:     0.1a
' ***********************************************************
' ---- some minor adjustments in INFO message by PvE ----
' ***********************************************************

' **********************
' DECLARATIONS
' **********************

' Identify program version
CONST MyVersion$ = "Haiku Generator - v.0.1a"
' Define runtime check for size of array
DEF FN bound(x) = SIZEOF(x)/SIZEOF(STRING)
' Create array of adjectives
DECLARE adje$[] = { "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", "summer", "icy", \
        "delicate", "quiet", "white", "cool", "spring", "winter", "patient", "twilight", "dawn", "crimson", \
        "wispy", "weathered", "blue", "billowing", "broken", "cold", "damp", "falling", "frosty", "green", \
        "long", "late", "lingering", "bold", "little", "morning", "muddy", "old", "red", "rough", \
        "still", "small", "sparkling", "throbbing", "shy", "wandering", "withered", "wild", "black", "young", \
        "holy", "solitary", "fragrant", "aged", "snowy", "proud", "floral", "restless", "divine", "polished", \
        "ancient", "purple", "lively", "nameless" }
' Create array of nouns
DECLARE noun$[] = { "waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning", "snow", "lake", \
        "sunset", "pine", "shadow", "leaf", "dawn", "glitter", "forest", "hill", "cloud", "meadow", \
        "sun", "glade", "bird", "brook", "butterfly", "bush", "dew", "dust", "field", "fire", \
        "flower", "firefly", "feather", "grass", "haze", "mountain", "night", "pond", "darkness", "snowflake", \
        "silence", "sound", "sky", "shape", "surf", "thunder", "violet", "water", "wildflower", "wave", \
        "water", "resonance", "sun", "wood", "dream", "cherry", "tree", "fog", "frost", "voice", \
        "paper", "frog", "smoke", "star" }
' Create array of verbs
DECLARE verb$[] = { "shakes", "drifts", "has stopped", "struggles", "hears", "has passed", "sleeps", "creeps", "flutters", "fades", \
        "is falling", "trickles", "murmurs", "warms", "hides", "jumps", "is dreaming", "sleeps", "falls", "wanders", \
        "waits", "has risen", "stands", "dying", "is drawing", "singing", "rises", "paints", "capturing", "flying", \
        "lies", "picked up", "gathers in", "invites", "separates", "eats", "plants", "digs into", "has fallen", "weeping", \
        "facing", "mourns", "tastes", "breaking", "shaking", "walks", "builds", "reveals", "piercing", "craves", \
        "departing", "opens", "falling", "confronts", "keeps", "breaking", "is floating", "settles", "reaches", "illuminates", \
        "closes", "leaves", "explodes", "drawing" }
' Create array of prepositions
DECLARE prep$[] = { "on", "beside", "in", "beneath", "of", "above", "under", "by", "over", "against", "near" }
' Declare vars used for generating haiku text
DECLARE myline1$, myline2$, myline3$, mypoem$
' Define the info box text
a_line1$ = "The haiku generated is in the style of Matsuo Basho"
a_line2$ = "(æ<9d>¾å°¾ è<8a>­è<95><89>), a Japanese poet of the 17th century."
a_line3$ = "This program was originally written in pygmy"
a_line4$ = "FORTH by Kent Peterson and ported to Bacon by"
a_line5$ = "Peter van Eerten. Vovchik did the GUI version."
info$ = CONCAT$(a_line1$, NL$, a_line2$, NL$, NL$, a_line3$, NL$, a_line4$, NL$, a_line5$, NL$)

' **********************
' END DECLARATIONS
' **********************


' **********************
' INCLUDES
' **********************

 INCLUDE "hug.bac", INIT, WINDOW, FRAME, MARK, SHOW, TEXT, IMAGE, HSEPARATOR, MSGDIALOG, BUTTON, ATTACH, CALLBACK, DISPLAY, QUIT, HUGOPTIONS, HIDE

' **********************
' END INCLUDES
' **********************

INIT

' **********************
' FUNCTIONS & SUBS
' **********************

' ------------
SUB MakeHaiku()
' ------------
        ' Generate a random number in the range of 0 to 3 for style selection
        mystyle = RANDOM(4)
        ' Create random haiku on basis of style selection
        SELECT mystyle
                CASE 0
                        myline1$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
                        myline2$ = CONCAT$(noun$[RANDOM(bound(noun$))], " ", verb$[RANDOM(bound(verb$))], " ", prep$[RANDOM(bound(prep$))], " ", noun$[RANDOM(bound(noun$))])
                        myline3$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
                CASE 1
                        myline1$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))], " ", verb$[RANDOM(bound(verb$))])
                        myline2$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
                        myline3$ = CONCAT$(verb$[RANDOM(bound(verb$))], " ", adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
                CASE 2
                        myline1$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
                        myline2$ = CONCAT$(prep$[RANDOM(bound(prep$))], " ", adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
                        myline3$ = CONCAT$(noun$[RANDOM(bound(noun$))], " ", verb$[RANDOM(bound(verb$))])
                CASE 3
                        myline1$ = CONCAT$(noun$[RANDOM(bound(noun$))], " ", prep$[RANDOM(bound(prep$))], " ", noun$[RANDOM(bound(noun$))])
                        myline2$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))], " ", prep$[RANDOM(bound(prep$))], " ", noun$[RANDOM(bound(noun$))])
                        myline3$ = CONCAT$(adje$[RANDOM(bound(adje$))], " ", noun$[RANDOM(bound(noun$))])
        END SELECT
        ' Create the haiku text as a single string with line feeds
        mypoem$ = CONCAT$(myline1$, NL$, myline2$, NL$, myline3$)
END SUB

' ------------
FUNCTION ShowInfo()
' ------------
        ' Show info window
        SHOW(myhelp)
        RETURN TRUE
END FUNCTION

' ------------
FUNCTION RefreshGui()
' ------------
        ' Create a new haiku
        MakeHaiku
        ' Update the widget "myhaiku" that displays the text
        TEXT(myhaiku, mypoem$)
        RETURN TRUE
END FUNCTION

' **********************
' END FUNCTIONS AND SUBS
' **********************


' **********************
' MAIN
' **********************

' Create the initial random haiku text
MakeHaiku
' Comment the following line when using Puppy Linux
HUGOPTIONS("BASEXFTDPI 78")
' Define the GUI
mywin = WINDOW(MyVersion$, 250, 150)
myframe = FRAME(240, 140)
' Define fixed text widget, just to demostrate non-ASCII chars
mytext = MARK("With apologies to æ<9d>¾å°¾ è<8a>­è<95><89>", 235, 40)
' Define image that will be displayed
myzen = IMAGE("zen.png", 40, 40)
' Define widget that displayes the haiku text
myhaiku = MARK(mypoem$, 240, 100)
' Define a horizontal Separator
myhline = HSEPARATOR(230)
myhelp = MSGDIALOG(info$, 350, 175, 0, 2)
CALLBACK(myhelp, HIDE)
' Define more and quit buttons
myinfo = BUTTON("Info", 60, 20)
mymore = BUTTON("More", 60, 20)
myquit = BUTTON("Quit", 60, 20)
' Attach all widgets to main window
ATTACH(mywin, myzen, 105, 27)
ATTACH(mywin, myframe, 5, 5)
ATTACH(mywin, mytext, 7, 1)
ATTACH(mywin, myhaiku, 5, 35)
ATTACH(mywin, myhline, 10,115)
ATTACH(mywin, myinfo, 10, 120)
ATTACH(mywin, mymore, 95, 120)
ATTACH(mywin, myquit, 180, 120)
' Define the button handler to display an info window
CALLBACK(myinfo, ShowInfo)
' Define the button handler for generating the next haiku text
CALLBACK(mymore, RefreshGui)
' Define the button handler for quitting the program
CALLBACK(myquit, QUIT)
' Show the GUI until the user exits
DISPLAY

' **********************
' END MAIN
' **********************