class TclTkInterpreter - tk-0.1.2 Documentation

# class TclTkInterpreter

class TclTkInterpreter: tcl/tk interpreter

### Public Class Methods

new() click to toggle source

initialize():

    # File lib/tcltk.rb, line 89
    def initialize()
      # generate interpreter object
      @ip = TclTkIp.new()

      # add ruby_fmt command to tcl interpreter
      # ruby_fmt command format arguments by `format' and call `ruby' command
      # (notice ruby command receives only one argument)
      if $DEBUG
        @ip._eval("proc ruby_fmt {fmt args} { puts \"ruby_fmt: $fmt $args\" ; set cmd [list ruby [format $fmt $args]] ; uplevel $cmd }")
      else
        @ip._eval("proc ruby_fmt {fmt args} { set cmd [list ruby [format $fmt $args]] ; uplevel $cmd }")
      end

      # @ip._get_eval_string(*args): generate string to evaluate in tcl interpreter
      #   *args: script which is going to be evaluated under tcl/tk
      def @ip._get_eval_string(*args)
        argstr = ""
        args.each{|arg|
          argstr += " " if argstr != ""
          # call to_eval if it is defined
          if (arg.respond_to?(:to_eval))
            argstr += arg.to_eval()
          else
            # call to_s unless defined
            argstr += arg.to_s()
          end
        }
        return argstr
      end

      # @ip._eval_args(*args): evaluate string under tcl/tk interpreter
      #     returns result string.
      #   *args: script which is going to be evaluated under tcl/tk
      def @ip._eval_args(*args)
        # calculate the string to eval in the interpreter
        argstr = _get_eval_string(*args)

        # evaluate under the interpreter
        print("_eval: \"", argstr, "\"") if $DEBUG
        res = _eval(argstr)
        if $DEBUG
          print(" -> \"", res, "\"\n")
        elsif  _return_value() != 0
          print(res, "\n")
        end
        fail(%Q/can't eval "#{argstr}"/) if _return_value() != 0 #'
        return res
      end

      # generate tcl/tk command object and register in the hash
      @commands = {}
      # for all commands registered in tcl/tk interpreter:
      @ip._eval("info command").split(/ /).each{|comname|
        if comname =~ /^[.]/
          # if command is a widget (path), generate TclTkWidget,
          # and register it in the hash
          @commands[comname] = TclTkWidget.new(@ip, comname)
        else
          # otherwise, generate TclTkCommand
          @commands[comname] = TclTkCommand.new(@ip, comname)
        end
      }
    end

### Public Instance Methods

_tcltkip() click to toggle source

_tcltkip(): returns @ip(TclTkIp)

    # File lib/tcltk.rb, line 164
    def _tcltkip()
      return @ip
    end

commands() click to toggle source

commands(): returns hash of the tcl/tk commands

    # File lib/tcltk.rb, line 154
    def commands()
      return @commands
    end

method_missing(id, *args) click to toggle source

#method_missing(id, *args): execute undefined method as tcl/tk command

    id: method symbol
    *args: method arguments

Calls superclass method Object#method_missing

    # File lib/tcltk.rb, line 171
    def method_missing(id, *args)
      # if command named by id registered, then execute it
      if @commands.key?(id.id2name)
        return @commands[id.id2name].e(*args)
      else
        # otherwise, exception
        super
      end
    end

rootwidget() click to toggle source

rootwidget(): returns root widget(TclTkWidget)

    # File lib/tcltk.rb, line 159
    def rootwidget()
      return @commands["."]
    end