#!/usr/bin/env ruby

require 'fox'
require 'scintilla_wrapper'

include Fox

ABOUT_MSG = <<EOM
The FOX GUI toolkit is developed by Jeroen van der Zijp.
The Scintilla source code editing component is developed by Neil Hodgson.
The FXScintilla widget is developed by Gilles Filippini.
and FXRuby is developed by Lyle Johnson.
EOM

class FXScintilla
  include Scintilla
end

class ScintillaTest  < FXMainWindow

  def initialize(app)
    # Invoke base class initialize method first
    super(app, "Scintilla Test", nil, nil, DECOR_ALL, 0, 0, 800, 600)

    # Menubar
    menubar = FXMenubar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)
  
    # Status bar
    FXStatusbar.new(self,
      LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER)

    # Scintilla widget takes up the rest of the space
    sunkenFrame = FXHorizontalFrame.new(self,
      FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y)
    @scintilla = FXScintilla.new(sunkenFrame, nil, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y)
    @scintilla.connect(SEL_COMMAND) do |sender, sel, scn|
      puts "received SCNotification:"
      puts "  scn.nmhdr.idFrom     = #{scn.nmhdr.idFrom}"
      puts "  scn.nmhdr.code       = #{scn.nmhdr.code}"
      puts "  scn.position         = #{scn.position}"
      puts "  scn.ch               = #{scn.ch}"
      puts "  scn.modifiers        = #{scn.modifiers}"
      puts "  scn.modificationType = #{scn.modificationType}"
      puts "  scn.text             = #{scn.text}"
      puts "  scn.length           = #{scn.length}"
      puts "  scn.linesAdded       = #{scn.linesAdded}"
      puts "  scn.message          = #{scn.message}"
      puts "  scn.wParam           = #{scn.wParam}"
      puts "  scn.lParam           = #{scn.lParam}"
      puts "  scn.line             = #{scn.line}"
      puts "  scn.foldLevelNow     = #{scn.foldLevelNow}"
      puts "  scn.foldLevelPrev    = #{scn.foldLevelPrev}"
      puts "  scn.margin           = #{scn.margin}"
      puts "  scn.listType         = #{scn.listType}"
      puts "  scn.x                = #{scn.x}"
      puts "  scn.y                = #{scn.y}"
    end
  
    # File menu
    filemenu = FXMenuPane.new(self)
    FXMenuCommand.new(filemenu, "&Open\tCtl-O\tOpen...").connect(SEL_COMMAND) {
      openDialog = FXFileDialog.new(self, "Open Document")
      openDialog.selectMode = SELECTFILE_EXISTING
      openDialog.patternList = ["All Files (*.*)", "Ruby Files (*.rb)"]
      if openDialog.execute != 0
        loadFile(openDialog.filename)
      end
    }
    FXMenuCommand.new(filemenu, "&Quit\tCtl-Q\tQuit application.", nil,
      getApp(), FXApp::ID_QUIT, 0)
    FXMenuTitle.new(menubar, "&File", nil, filemenu)
      
    # Help menu
    helpmenu = FXMenuPane.new(self)
    FXMenuCommand.new(helpmenu, "&About FXRuby...").connect(SEL_COMMAND) {
      FXMessageBox.information(self, MBOX_OK, "About FXRuby", ABOUT_MSG)
    }
    FXMenuTitle.new(menubar, "&Help", nil, helpmenu, LAYOUT_RIGHT)
  end

  def loadFile(filename)
    begin
      getApp().beginWaitCursor()
      text = File.open(filename, "r").read
      @scintilla.setText(text)
    ensure
      getApp().endWaitCursor()
    end
  end

  # Start
  def create
    super
    show(PLACEMENT_SCREEN)
  end
end

if __FILE__ == $0
  # Make application
  application = FXApp.new("ScintillaTest", "FoxTest")
  
  # Open display
  application.init(ARGV)
  
  # Make window
  ScintillaTest.new(application)
  
  # Create app
  application.create
  
  # Run
  application.run
end
