# Purpose: Examine and debug the FreeBASE databus
#
# $Id: file_browser.rb,v 1.5 2004/09/18 15:32:33 ljulliar Exp $
#
# Authors:  Laurent Julliard <laurent AT moldus DOT org>
# Contributors: Rich Kilmer <rich@infoether.com>
#
# This file is part of the FreeRIDE project
#
# This application is free software; you can redistribute it and/or
# modify it under the terms of the Ruby license defined in the
# COPYING file.
#
# Copyright (c) 2002 Rich Kilmer All rights reserved.
#

#require 'fox'

module FreeRIDE
  module FoxRenderer
    
    ##
    # This module defines the databus inspector
    #
    class FileBrowser
      include Fox
      extend FreeBASE::StandardPlugin
      
      def FileBrowser.start(plugin)
        # Handle icons
        plugin['/system/ui/icons/FileBrowser'].subscribe do |event, slot|
          if event == :notify_slot_add
            app = plugin['/system/ui/fox/FXApp'].data
            path = "#{plugin.plugin_configuration.full_base_path}/icons/#{slot.name}.png"
            if FileTest.exist?(path)
              slot.data = FXPNGIcon.new(app, File.open(path, "rb").read)
              slot.data.create
            end
          end
        end
        
        # Add command
        browser = FileBrowser.new(plugin)
        plugin["/system/ui/commands"].manager.add("App/View/FileBrowser","File &List") do |cmd_slot|
          browser.toggle
        end
        plugin["/system/ui/commands"].manager.command("App/View/FileBrowser").icon = "/system/ui/icons/FileBrowser/browse"
        
        plugin["/system/ui/keys"].manager.bind("App/View/FileBrowser", :ctrl, :L)
        
        plugin["/system/ui/current/ToolBar"].manager.add_command("View", "App/View/FileBrowser")
        # Insert the inspector in the Tools menu
        viewmenu = plugin["/system/ui/components/MenuPane/View_menu"].manager
        viewmenu.add_command("App/View/FileBrowser")
        viewmenu.uncheck("App/View/FileBrowser")
        plugin["/system/state/all_plugins_loaded"].subscribe do |event, slot|
          if slot.data == true
            if plugin.properties["Open"]
              browser.toggle
            end
          end
        end
        
        plugin.transition(FreeBASE::RUNNING)
      end
      
      def initialize(plugin)
        @docked = false
        @viewmenu = plugin["/system/ui/components/MenuPane/View_menu"].manager
        @plugin = plugin
        @cmd_mgr = plugin['/system/ui/commands'].manager
        @dockpane_slot = plugin['/system/ui/components/DockPane'].manager.add("File View")
        main_window = @plugin["/system/ui/fox/FXMainWindow"].data
        dirlist = FXDirList.new(main_window, 0, nil, 0, 
          (TREELIST_SHOWS_LINES|TREELIST_SHOWS_BOXES|FRAME_SUNKEN|FRAME_THICK|
          LAYOUT_FILL_X|LAYOUT_FILL_Y|DIRLIST_SHOWFILES), 0, 0, 0, 0)
        filetypes = @plugin.properties['FileTypes']
        unless filetypes
          filetypes = "*.rb,*.xml"
          @plugin.properties["FileTypes"] = filetypes
        end
        dirlist.setPattern(filetypes)
        dirlist.connect(SEL_DOUBLECLICKED) { |sender, sel, item|
          if dirlist.isItemFile(item)
            pathname = dirlist.getItemPathname(item)
            plugin.properties["LastFile"] = pathname #dirlist.directory
            @cmd_mgr.command("App/File/Load").invoke(pathname)
          else
            dirlist.expandTree(item, !item.expanded?)
          end
        }
        dirlist.hide
        dirlist.create
        lastFile = plugin.properties["LastFile"]
        if lastFile && lastFile!=""
          dirlist.currentFile = lastFile
        else
          dirlist.directory = Dir.pwd
        end
        
        @dockpane_slot.data = dirlist
      end
      
      def toggle
        unless @docked
          @dockpane_slot.manager.dock('west')
          @docked = true
          @viewmenu.check("App/View/FileBrowser")
          @checked = true
          @plugin.properties["Open"] = true
        else
          if @checked
            @dockpane_slot.manager.hide
            @viewmenu.uncheck("App/View/FileBrowser")
            @checked = false
            @plugin.properties["Open"] = false
          else
            @dockpane_slot.manager.show
            @checked = true
            @viewmenu.check("App/View/FileBrowser")
            @plugin.properties["Open"] = true
          end
        end
      end
      
    end
    
  end
end
