# RubyPanel Toolbar (C) 2007 jim.foltz@gmail.com
#
# With special thanks to Chris Phillips (Sketchy Physics)
# for the Win32API code examples. 

# 2011-01-05 <jim.foltz@gmail.com>
#   * Changed Toolbar name from "Ruby COnsole" to "Ruby Toolbar"  (TT)
#     http://forums.sketchucation.com/viewtopic.php?f=323&t=1542&p=298587#p298587
#   * Wrapped in addition module RubyToolbar
#   * Use $suString.GetSting to get proper "Ruby Console" name string.
#   * Better check if TB was previously visible
#   * Use UI.start_timer to restore Toolbar


module JF
   module RubyToolbar16

      @mrufile = Sketchup.read_default(REG, "MRUFile")
      @mrudir = Sketchup.read_default(REG, "MRUDirectory")

      def safe_path(path)
         path = path.gsub(/\\/, '/')
      end

      def self.clear_console
         SKETCHUP_CONSOLE.clear
      end

      def self.toggle
         if !SKETCHUP_CONSOLE.visible?
            SKETCHUP_CONSOLE.show
         else
            SKETCHUP_CONSOLE.hide
         end
      end

      def self.rt_load(filename = "")
         @mrudir = Sketchup.find_support_file("Plugins") unless @mrudir
         @mrudir.gsub!("/", "\\\\")
         f = UI.openpanel(T["Load Script"], @mrudir, "Ruby File|*.rb||")
         return unless f
         @mrudir = File.dirname(f)
         begin
            Kernel.load f
            Sketchup.set_status_text "#{File.basename(f)} #{T['loaded.']} (#{Time.now.strftime('%H:%M:%S')})"
            @mrufile = f
            @mrufile.gsub!(/[\\]+/, '/')
            @mrudir.gsub!(/[\\]+/, '/')
            Sketchup.write_default(REG, "MRUFile", @mrufile)
            Sketchup.write_default(REG, "MRUDirectory", @mrudir)
         rescue => exc
            p exc
            UI.messagebox(T["Could not load"] + " #{File.basename(f)}:\n #{$!}")
         end
      end

      def self.rt_reload
         if @mrufile.nil?
            rt_load
         else
            begin
               Kernel.load @mrufile
               Sketchup.set_status_text "#{File.basename(@mrufile)} Reloaded. "+Time.now.strftime("%H:%M:%S")
            rescue => exc
               p e
            end
         end
      end

      plugins = File.dirname(File.expand_path(__FILE__))
      imgdir  = plugins + "/images"

      # create toolbar
      tb = UI::Toolbar.new("Ruby Toolbar 2016r1")

      # Toggle console cmd
      cmd = UI::Command.new("Show/Hide") { toggle }
      cmd.large_icon = cmd.small_icon = File.join(imgdir, "rubypanel.png")
      cmd.tooltip = T["Show/Hide Ruby Console"]
      tb.add_item cmd

      # Clear Console
      cmd = UI::Command.new("Clear") { clear_console }
      cmd.status_bar_text = cmd.tooltip = T["Clear Console"]
      cmd.large_icon = cmd.small_icon = File.join(imgdir, "Delete24.png")
      tb.add_item cmd

      cmd = UI::Command.new("LoadScript") { rt_load }
      cmd.large_icon = cmd.small_icon = File.join(imgdir, "doc_ruby.png")
      cmd.tooltip = cmd.status_bar_text = T["Load Script"]
      tb.add_item cmd

      cmd = UI::Command.new("Reload") { rt_reload }
      cmd.large_icon = cmd.small_icon = File.join(imgdir, "reload.png")
      cmd.status_bar_text = cmd.tooltip = T["Reload Script"]
      tb.add_item cmd

      # dev dir
      cmd = UI::Command.new("PluginsDir") {
         #UI.openURL("c:\\program files\\google\\google sketchup 6\\plugins")
         @mrudir = Sketchup.find_support_file("Plugins") unless @mrudir
         UI.openURL(@mrudir)
      }
      cmd.tooltip = cmd.status_bar_text = T["Browse Plugins Folder"]
      cmd.large_icon = cmd.small_icon = File.join(imgdir, "open_folder.png")
      tb.add_item cmd

      if tb.get_last_state == TB_VISIBLE
         UI.start_timer(0.1, false) { tb.restore }
      end

   end # RubyToolbar16
end # module JF
