############################################################################
#
#	File:     control.icn
#
#	Subject:  Procedures related to interface control in charpatt
#
#	Author:   Ralph E. Griswold
#
#	Date:     September 6, 1998
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#  This file contains the VIB section, callbacks, and shortcut handling.
#
############################################################################
#
#  Requires:  Version 9 graphics.
#
############################################################################

$include "defines.icn"

#  Handle File menu.

procedure file_cb(vidget, value)

   case value[1] of {
      "open grammar  @O"  :  open_file(load_grammar)
      "load pattern  @C"  :  open_file(load_pattern)
      "load values   @V"  :  open_file(load_values)
      "enter pattern @E"  :  enter_pattern()
      "save grammar  @S"  :  save_grammar()
      "undo          @U"  :  undo()
      "redo          @T"  :  redo()
      "clear saved   @D"  :  clear_saved()
      "revert        @A"  :  revert()
      "quit          @Q"  :  exit_app()
      }

   return

end

#  Handle Information menu.

procedure grammar_cb(vidget, value)

   case value[1] of {
      "symbols     @Y"  :  symbol_information()
      "add comment @K"  :  comment()
      }

   return

end

#  Handle Encode menu.

procedure options_cb(vidget, value)

   case value[1] of {
      "selections @X"  :  selections()
      }

   return

end

#  Handle callbacks for pattern text-list vidget.

procedure plist_cb(vidget, value)
   local position

   if /value then return		# deselection

   if &meta then {
      if TextDialog("Segment", , value, LineWidth) == "Cancel" then fail
      }
   else fail

   find_string := dialog_value[1]

   #  Note that the following code determines the range for section from the
   #  first occurrence of find_string -- the precise location is not known.

   value ? {					# get range for section()
      if tab(upto(find_string)) then {		# could fail if faked value
         if position := &pos + LineWidth * (VGetState(plist)[2] - 1) then
            secrange := position || ":" || position + *find_string
         }
      else {
         return FailNotice("Selection not found.")
         }
      }
         
   minn := maxn := minl := maxl := *find_string

   VSetState(vidget, value)		# deselect

   return

end

#  Handle Search menu.

procedure search_cb(vidget, value)

   case value[1] of {
      "constant   @F"  :  search_constant("string")
      "repetition @R"  :  search_repetition()
      "run        @H"  :  search_run()
      "palindroid @P"  :  search_palindroid()
      "reverse    @V"  :  search_constant("reverse")
      "n-grams    @N"  :  search_ngrams()
      "section    @C"  :  section()
      "locate     @L"  :  locate()
      }

   return

end

#  Handle callbacks for definitions text-list vidget.

procedure slist_cb(vidget, value)
   local rsym, rdef, sym

   if /value then return			# deselection

   if &meta then {				# remove current symbol
      save_state(undo_list)
      if &shift then collapse() | fail
         else {
         rsym := value[1]
         if rsym == symbol then {
            Notice("Removing the current variable is not allowed.")
            fail
            }
         if rsym == goal then {
            Notice("Removing the goal symbol is not allowed.")
            fail
            }
         if AskDialog("Remove symbol " || rsym || "?") == "No" then fail
         rdef := symbol_tbl[rsym]
         delete(symbol_tbl, rsym)
         every sym := !keylist(symbol_tbl) do
            symbol_tbl[sym] := replace(symbol_tbl[sym], rsym, rdef)
         workspace_string := symbol_tbl[symbol]
         push(symbols_list, rsym)		# make removed symbol available
         }
      }

   else {					# make new symbol current
      symbol_tbl[symbol] := workspace_string	# save current string
      value ? {
         symbol := move(1)
         ="->"
         workspace_string := tab(0)
         }
      VSetState(vidget, value)
      }

   refresh(1)

   return

end

procedure tokens_cb(vidget, value)

   case value[1] of {
      "edit   @1"  :  edit_tokens()
      "write  @2"  :  write_tokens()
      }

   return

end

#  Handle Configuration menu.

procedure workspace_cb(vidget, value)

   case value[1] of {
      "write        @W"  :  save_string()
      "edit         @J"  :  edit_workspace()
      "make grammar @G"  :  new_grammar()
      "information  @I"  :  metrics()
      }

   return

end

#  Handle keyboard shortcuts.

procedure shortcuts(e)

   if &meta then case map(e) of {
      "0"  :  ()
      "1"  :  edit_tokens()
      "2"  :  write_tokens()
      "3"  :  ()
      "4"  :  write(nav_file, " ", *symbols_list)
      "5"  :  ()
      "6"  :  ()
      "7"  :  ()
      "8"  :  ()
      "9"  :  ()
      "a"  :  revert()
      "b"  :  search_constant("reverse")
      "c"  :  open_file(load_pattern)
      "d"  :  clear_saved()
      "e"  :  enter_pattern()
      "f"  :  search_constant("string")
      "g"  :  new_grammar()
      "h"  :  search_run()
      "i"  :  metrics()
      "j"  :  edit_workspace()
      "k"  :  section()
      "l"  :  locate()
      "m"  :  comment()
      "n"  :  search_ngrams()
      "o"  :  open_file(load_grammar)
      "p"  :  search_palindroid()
      "q"  :  exit_app()
      "r"  :  search_repetition()
      "s"  :  save_grammar()
      "t"  :  redo()
      "u"  :  undo()
      "v"  :  open_file(load_values)
      "w"  :  save_string()
      "x"  :  selections()
      "y"  :  symbol_information()
      "z"  :  ()
      }

   return

end

#===<<vib:begin>>===	modify using vib; do not remove this marker line
procedure ui_atts()
   return ["size=899,451", "bg=pale gray", "label=Character Patterns"]
end

procedure ui(win, cbk)
return vsetup(win, cbk,
   [":Sizer:::0,0,899,451:Character Patterns",],
   ["definitions:Label:::541,30,91,13:definitions: ",],
   ["depth:Label:::806,30,49,13:depth: ",],
   ["file:Menu:pull::2,3,36,21:File",file_cb,
      ["open grammar  @O","load pattern  @C","load values   @V","enter pattern @E","save grammar  @S",
      "revert        @A","undo          @U","redo          @T","clear saved   @D","quit          @Q"]],
   ["filename:Label:::6,30,77,13:file name: ",],
   ["gram:Label:::668,67,49,13:grammar",],
   ["grammar:Menu:pull::160,2,57,21:Grammar",grammar_cb,
      ["change name @H","symbols     @Y","add comment @M"]],
   ["left:Label:::669,30,98,13:symbols left: ",],
   ["line1:Line:::268,46,268,25:",],
   ["line2:Line:::532,46,532,25:",],
   ["line4:Line:::440,46,440,25:",],
   ["line5:Line:::0,48,900,48:",],
   ["line6:Line:::665,46,665,25:",],
   ["line7:Line:::800,46,800,25:",],
   ["menubar:Line:::0,24,900,24:",],
   ["options:Menu:pull::267,2,57,21:Options",options_cb,
      ["selections @X"]],
   ["plist:List:w::7,90,460,345:",plist_cb],
   ["search:Menu:pull::38,2,50,21:Search",search_cb,
      ["constant   @F","repetition @R","run        @H","reverse    @B","palindroid @P",
      "n-grams    @N","section    @K","locate     @L"]],
   ["size:Label:::272,30,98,13:grammar size: ",],
   ["slist:List:w::481,90,405,347:",slist_cb],
   ["tokens:Menu:pull::217,3,50,21:Tokens",tokens_cb,
      ["edit   @1","write  @2"]],
   ["variable:Label:::446,30,70,13:variable: ",],
   ["worksp:Label:::195,67,63,13:workspace",],
   ["workspace:Menu:pull::89,2,71,21:Workspace",workspace_cb,
      ["write        @W","edit         @J","make grammar @G","information  @I"]],
   )
end
#===<<vib:end>>===	end of section maintained by vib