--
--	menutest.occ -- menu test for ttyutil library
--	Copyright (C) 2009 Fred Barnes <frmb@kent.ac.uk>
--
--	This program is free software; you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
--

#INCLUDE "course.module"
#INCLUDE "useful.module"
#INCLUDE "ttyutil.module"

PROC menuitem.handler (VAL INT id, TTYUTIL.MENUITEM? link) --{{{
  MOBILE []BYTE str:
  INT value:
  SEQ

    value := 0

    format.sisi ("item ", id, " = ", value, str)

    INITIAL BOOL running IS TRUE:
    WHILE running
      INITIAL BOOL update IS FALSE:
      INITIAL BOOL reset IS FALSE:
      SEQ
        link[in] ? CASE
          get.text
            update := TRUE
          key.left
            SEQ
              value := value MINUS 1000
              reset := TRUE
          key.right
            SEQ
              value := value PLUS 1000
              reset := TRUE
          INT k:
          key.other; k
            SKIP
          quit
            running := FALSE

        IF
          reset
            SEQ
              format.sisi ("item ", id, " = ", value, str)
              update := TRUE
          TRUE
            SKIP
        IF
          update
            link[out] ! text; CLONE str
          TRUE
            SKIP
:
--}}}
PROC newitem.handler (VAL INT id, TTYUTIL.MENUITEM? link, SHARED CHAN TTYUTIL.MENU to.menu!) --{{{
  MOBILE []BYTE str:
  SEQ
    format.si ("delete item ", id, str)

    INITIAL BOOL running IS TRUE:
    WHILE running
      INITIAL BOOL update IS FALSE:
      SEQ
        link[in] ? CASE
          get.text
            update := TRUE
          key.left
            SKIP
          key.right
            SKIP
          INT k:
          key.other; k
            IF
              k = (INT '*n')
                --{{{  delete item 'id'
                CLAIM to.menu!
                  to.menu ! del.item; id
                --}}}
              TRUE
                SKIP
          quit
            running := FALSE
        IF
          update
            link[out] ! text; CLONE str
          TRUE
            SKIP
:
--}}}
PROC newitems.handler (TTYUTIL.MENUITEM? link, SHARED CHAN TTYUTIL.MENU to.menu!) --{{{
  MOBILE []BYTE str:
  INITIAL INT icount IS 0:
  SEQ

    str := "add new item"

    INITIAL BOOL running IS TRUE:
    WHILE running
      INITIAL BOOL update IS FALSE:
      SEQ
        link[in] ? CASE
          get.text
            update := TRUE
          key.left
            SKIP
          key.right
            SKIP
          INT k:
          key.other; k
            IF
              k = (INT '*n')
                --{{{  add new item!
                TTYUTIL.MENUITEM! t.cli:
                TTYUTIL.MENUITEM? t.svr:
                SEQ
                  t.cli, t.svr := MOBILE TTYUTIL.MENUITEM
                  FORK newitem.handler (icount, t.svr?, to.menu!)
                  icount := icount + 1
                  CLAIM to.menu!
                    to.menu ! insert.item; icount; t.cli
                --}}}
              TRUE
                SKIP
          quit
            running := FALSE
        IF
          update
            link[out] ! text; CLONE str
          TRUE
            SKIP
:
--}}}

PROC menutest (CHAN BYTE kyb?, scr!)
  TTYUTIL.MENUITEM? n.svr:
  TTYUTIL.MENUITEM! n.cli:
  SEQ
    n.svr, n.cli := MOBILE TTYUTIL.MENUITEM

    SHARED! CHAN TTYUTIL.DPY dpy:
    CHAN INT keys, keys.kill:
    SHARED! CHAN TTYUTIL.MENU menu:
    PAR
      SEQ
        cursor.invisible (scr!)
        ttyutil.display (dpy?, scr!)
      ttyutil.keyhandler (kyb?, keys.kill?, keys!)

      SEQ
        -- clear screen and run menu process
        CLAIM dpy!
          dpy ! clear.screen

        ttyutil.menu (10, 2, 30, 10, TRUE, menu?, dpy!)

      -- option that allows new items to be added
      newitems.handler (n.svr?, menu!)

      SEQ
        CLAIM menu!
          menu ! add.item; n.cli

        SEQ i = 0 FOR 20
          -- create menu items
          TTYUTIL.MENUITEM? i.svr:
          TTYUTIL.MENUITEM! i.cli:
          SEQ
            i.svr, i.cli := MOBILE TTYUTIL.MENUITEM

            FORK menuitem.handler (i, i.svr)
            CLAIM menu!
              menu ! add.item; i.cli

        -- filter keystrokes to menu
        WHILE TRUE
          INT key:
          SEQ
            keys ? key
            CASE key
              KEY.UP, KEY.DOWN, KEY.LEFT, KEY.RIGHT, '*n'
                CLAIM menu!
                  menu ! key.in; key
              ELSE
                SKIP
:

