#!/usr/bin/env ruby
# -*- coding: euc-jp -*-
#
# rolodex --
# ΥץȤ Tom LaStrange  rolodex ΰǤ
#
# Copyright (C) 1998 by Takaaki Tateishi <ttate@jaist.ac.jp>
# Time-stamp: "04/04/09 00:32:12 nagai"
#

require "tk"
Tk.encoding = "euc-jp"

def show_help(topic,x=0,y=0)
  if( topic.is_a?(TkWindow) )
    w = TkWinfo.containing(x,y)
    if( w.is_a?(TkWindow) )
      if( TkWinfo.exist?(w) )
	topic = w
      end
    end
  end

  if( $helpTopics.include?(topic) )
    msg = $helpTopics[topic]
  else
    msg = "ΥȥԥåˤĤƤΥإפϤޤѤǤޤ"
  end
  TkDialog.new("title"=>"Rolodex Help",
	       "message"=>"#{topic}\n\n#{msg}",
	       "default_button"=>0,
	       "buttons"=>["OK"])
end

def fillCard
  clearAction
  $root.frame.entry[1].insert(0, "Ω ")
  $root.frame.entry[2].insert(0, "923-1292 ")
  $root.frame.entry[3].insert(0, "äĮ  1-1")
  $root.frame.entry[4].insert(0, "Φüʳصر")
  $root.frame.entry[5].insert(0,"private")
  $root.frame.entry[6].insert(0,"***-***-****")
  $root.frame.entry[7].insert(0,"***-***-****")
end

def addAction
  for i in 1..7
    STDERR.print format("%-12s %s\n",
			RolodexFrame::LABEL[i],
			$root.frame.entry[i].value)
  end
end

def clearAction
  for i in 1..7
    $root.frame.entry[i].delete(0,"end")
  end
end

def fileAction
  TkDialog.new("title"=>"File Selection",
	       "message"=>"ϥեΥߡǤ\n",
	       "default_button"=>0,
	       "buttons"=>["OK"])
  STDERR.print "dummy file name\n"
end

def deleteAction
  result = TkDialog.new("title"=>"Confirm Action",
			"message"=>"Ǥ",
			"default_button"=>0,
			"buttons"=>["󥻥"])
  if( result.value == 0 )
    clearAction
  end
end


class RolodexFrame < TkFrame
  attr_reader :entry, :label

  LABEL = ["","̾:","","","","():","():","Fax:"]

  def initialize(parent=nil,keys=nil)
    super(parent,keys)
    self["relief"] = "flat"
    @i = []
    @label = []
    @entry = []
    for i in 1..7
      @i[i] = TkFrame.new(self)
      @i[i].pack("side"=>"top",
		 "pady"=>2,
		 "anchor"=>"e")
      @label[i] = TkLabel.new(@i[i],
			      "text"=>LABEL[i],
			      "anchor"=>"e")
      @entry[i] = TkEntry.new(@i[i],
			      "width"=>30,
			      "relief"=>"sunken")
      @entry[i].pack("side"=>"right")
      @label[i].pack("side"=>"right")
    end
  end
end

class RolodexButtons < TkFrame
  attr_reader :clear, :add, :search, :delete

  def initialize(parent,keys=nil)
    super(parent,keys)
    @clear = TkButton.new(self,"text" => "ꥢ")
    @add = TkButton.new(self,  "text" => "ɲ")
    @search = TkButton.new(self, "text" => "")
    @delete = TkButton.new(self,  "text" => "õ")
    for w in [@clear,@add,@search,@delete]
      w.pack("side"=>"left", "padx"=>2)
    end
  end
end

class RolodexMenuFrame < TkFrame
  attr_reader :file_menu, :help_menu, :file, :help

  def initialize(parent,keys=nil)
    super(parent,keys)
    configure("relief"=>"raised",
	      "borderwidth"=>1)

    @file = TkMenubutton.new(self,
			     "text"=> "ե",
			     "underline"=>0)
    @file_menu = TkMenu.new(@file)
    @file_menu.add("command",
		   "label" => "ɤ߹ ...",
		   "command" => proc{fileAction},
		   "underline" => 0)
    @file_menu.add("command",
		   "label" => "λ",
		   "command" => proc{$root.destroy},
		   "underline" => 0)
    @file.menu(@file_menu)
    @file.pack("side"=>"left")

    @help = TkMenubutton.new(self,
			     "text"=> "إ",
			     "underline"=>0)
    @help_menu = TkMenu.new(@help)
    @help_menu.add("command",
		   "label"=> "ƥȤˤĤ",
		   "command"=>proc{show_help("ƥ")},
		   "underline"=>3)
    @help_menu.add("command",
		   "label"=> "إפˤĤ",
		   "command"=>proc{show_help("إ")},
		   "underline"=>3)
    @help_menu.add("command",
		   "label"=> "ɥˤĤ",
		   "command"=>proc{show_help("ɥ")},
		   "underline"=>3)
    @help_menu.add("command",
		   "label"=> "ˤĤ",
		   "command"=>proc{show_help("")},
		   "underline"=>3)
    @help_menu.add("command",
		   "label"=> "С",
		   "command"=>proc{show_help("С")},
		   "underline"=>3)
    @help.menu(@help_menu)
    @help.pack("side"=>"right")
  end
end

class Rolodex < TkRoot
  attr_reader :frame, :buttons, :menu

  def initialize(*args)
    super(*args)
    @frame = RolodexFrame.new(self)
    @frame.pack("side"=>"top",
		"fill"=>"y",
		"anchor"=>"center")
    @buttons = RolodexButtons.new(self)
    @buttons.pack("side"=>"bottom",
		  "pady"=>2,
		  "anchor"=>"center")
    @menu = RolodexMenuFrame.new(self)
    @menu.pack("before"=>@frame,
	       "side"=>"top",
	       "fill"=>"x")
  end
end

$root = Rolodex.new

$root.buttons.delete.configure("command"=>proc{deleteAction})
$root.buttons.add.configure("command"=>proc{addAction})
$root.buttons.clear.configure("command"=>proc{clearAction})
$root.buttons.search.configure("command"=>proc{addAction; fillCard})

$root.buttons.clear.configure("text"=> "ꥢ   Ctrl+C")
$root.bind("Control-c",proc{clearAction})

$root.buttons.add.configure("text"=> "ɲ   Ctrl+A")
$root.bind("Control-a",proc{addAction})

$root.buttons.search.configure("text"=> "   Ctrl+S")
$root.bind("Control-s",proc{addAction; fillCard})

$root.buttons.delete.configure("text"=> "õ   Ctrl+D")
$root.bind("Control-d",proc{deleteAction})

$root.menu.file_menu.entryconfigure(1, "accel"=>"Ctrl+F")
$root.bind("Control-f",proc{fileAction})

$root.menu.file_menu.entryconfigure(2, "accel"=>"Ctrl+Q")
$root.bind("Control-q",proc{$root.destroy})

$root.frame.entry[1].focus

$root.bind("Any-F1",
	   proc{|event| show_help(event.widget, event.x_root, event.y_root)})
$root.bind("Any-Help",
	   proc{|event| show_help(event.widget, event.x_root, event.y_root)})


$helpTopics = {}

$helpTopics[$root.menu.file] = <<EOF
ϡ֥եץ˥塼Ǥɤ߹ߡפֽλפʤɤ
ԤʤȤǤޤ
EOF

$helpTopics[$root.menu.file_menu.index(0)] = <<EOF
եɤ߹ߤԤʤȤ˻Ȥޤ
EOF

$helpTopics[$root.menu.file_menu.index(1)] = <<EOF
ץꥱλȤ˻Ȥޤ
EOF

$helpTopics[$root.frame.entry[1]] = <<EOF
̾륨ȥǤ
EOF

$helpTopics[$root.frame.entry[2]] = <<EOF
륨ȥǤ
EOF

$helpTopics[$root.frame.entry[3]] = <<EOF
륨ȥǤ
EOF

$helpTopics[$root.frame.entry[4]] = <<EOF
륨ȥǤ
EOF

$helpTopics[$root.frame.entry[5]] = <<EOF
ֹ륨ȥǤ\
ʤȤ private ȵޤ
EOF

$helpTopics[$root.frame.entry[6]] = <<EOF
Ҥֹ륨ȥǤ
EOF

$helpTopics[$root.frame.entry[7]] = <<EOF
FAXֹ륨ȥǤ
EOF

$helpTopics["ƥ"] = <<EOF
Ruby/TkǤgrabεʤᤳΥץꥱǤ\
ƥȥإפϥݡȤƤޤ
Ʊ褦ʸ̤bindȥޥΰ֤WedgetΤ\
Ȥ뤳ȤǤޤ
EOF

$helpTopics["إ"] = <<EOF
ޥ򥦥ɥˤ碌F1򲡤Ȥˤä\
Υإפ򸫤뤳ȤǤޤ
EOF

$helpTopics["ɥ"] = <<EOF
ΥɥϥߡǤ
EOF

$helpTopics[""] = <<EOF
Ctrl+A:		ɲ
Ctrl+C:		ꥢ
Ctrl+D:		õ
Ctrl+F:		ե
Ctrl+Q:		λ
Ctrl+S:		
EOF

$helpTopics["С"] = <<EOF
С 1.0.1j Ǥ
EOF

Tk.mainloop
