# ブラウザみたいなもの

require "phi"
require "mozilla"

# 色々と生成
form = Phi::Form.new
panel = Phi::Panel.new(form)
backbtn = Phi::Button.new(panel)
fwdbtn = Phi::Button.new(panel)
homebtn = Phi::Button.new(panel)
urledit = Phi::ComboBox.new(panel)
gobtn = Phi::Button.new(panel)
browser = Phi::MozillaBrowser.new(form, :wb)

# レイアウト
#form.position = Phi::PO_DEFAULT
panel.align = Phi::AL_TOP
backbtn.caption = "戻"
backbtn.width = 40
backbtn.left = 8
backbtn.top = 8
backbtn.enabled = false
fwdbtn.caption = "進"
fwdbtn.width = 40
fwdbtn.left = backbtn.left + backbtn.width + 8
fwdbtn.top = 8
fwdbtn.enabled = false
homebtn.caption = "家"
homebtn.width = 40
homebtn.left = fwdbtn.left + fwdbtn.width + 8
homebtn.top = 8
gobtn.caption = "行"
gobtn.width = 40
gobtn.left = panel.width - gobtn.width - 8
gobtn.top = 8
gobtn.anchors = [Phi::AK_RIGHT]
urledit.left = homebtn.left + homebtn.width + 8
urledit.top = 8
urledit.width = gobtn.left - 8 - urledit.left
urledit.anchors = [Phi::AK_RIGHT, Phi::AK_LEFT]
browser.align = Phi::AL_CLIENT

# イベント
browser.on_command_state_change = proc do |sender, command, enable|
  case command
  when 1                        # CSC_NAVIGATEFORWARD
    fwdbtn.enabled = enable
  when 2                        # CSC_NAGIGATEBACK
    backbtn.enabled = enable
  end
end
browser.on_title_change = proc do |sender, text|
  form.caption = text
end
urledit.on_key_press = proc do |sender, key|
  if key == 0x0d
    browser.navigate2(urledit.text)
    key = 0
  end
end
backbtn.on_click = proc do |sender|
  browser.go_back
end
fwdbtn.on_click = proc do |sender|
  browser.go_forward
end
homebtn.on_click = proc do |sender|
  browser.go_home
end
gobtn.on_click = proc do |sender|
  browser.navigate2(urledit.text)
end

# メニュー
Phi.new_menu(form, :main_menu, [
	       Phi.new_item('ファイル(&F)', '', :mi_file).add(Phi.new_item('終了(&X)', '', :mi_file_exit)),
	       Phi.new_item('表示(&V)', '', :mi_view).add(Phi.new_item('再表示', 'Ctrl+R', :mi_view_reload)),
	       Phi.new_item('ヘルプ(&F)', '', :mi_help).add(Phi.new_item('バージョン情報(&A)', '', :mi_help_about))])

form.show
Phi.mainloop
