module TkBalloon

  @@show_balloons= true

  def parse_keys( keys=nil)
    return if keys.nil?
    if keys.key?('balloon')
      set_balloon(keys['balloon'])
      keys.delete('balloon')
    end

  end

  def set_balloon(text)
    @text_balloon= text
    @balloon_timer = TkAfter.new( Defaults::DEFS['balloon_timein'], 1, proc{show_balloon})
#    @timerout = TkAfter.new( Defaults::DEFS['balloon_timeout'], 1, proc{hide_balloon})
    self
  end

  def show_ev(type,ev)
    debug type+ ':'+ev.widget.to_s
  end


  def show_balloon
    return unless @@show_balloons
    return if @balloon_timer.nil?
    return if @text_balloon.nil?

    x = TkWinfo.pointerx(self)+10
    y = TkWinfo.pointery(self)+1

    if @w_balloon.nil?
      @w_balloon= TkToplevel.new {geometry "+#{x}+#{y}"
        overrideredirect true}

      opts= TkDefaults::GUI['tkballoon.label'].update( 'text'=> @text_balloon)
      TkLabel.new(@w_balloon, opts ).pack
    end
	@balloon_timer.stop
  end

  def hide_balloon
    if @w_balloon
      @w_balloon.destroy
	  @w_balloon= nil

    end
  end

  def bind_balloon
    return if @balloon_timer.nil?
    self.bind('Enter', proc { @balloon_timer.start})
    self.bind('Leave', proc { hide_balloon; @balloon_timer.stop})
    self.bind('Motion', proc { hide_balloon; @balloon_timer.restart})
  end

  def unbind_balloon
    return if @balloon_timer.nil?
    self.bind('Enter','')
    self.bind('Leave','')
    self.bind('Motion','')
    hide_balloon
  end

end

module TkBalloon_old

  @@show_balloons= true

  def parse_keys( keys=nil)
    return if keys.nil?
    if keys.key?('balloon')
      set_balloon(keys['balloon'])
      keys.delete('balloon')
    end

  end

  def set_balloon(text)
    @text_balloon= text
    @balloon_timer = TkAfter.new( Defaults::DEFS['balloon_timein'], 1, proc{show_balloon})
    @timerout = TkAfter.new( Defaults::DEFS['balloon_timeout'], 1, proc{hide_balloon})
    self
  end

  def show_ev(type,ev)
    debug type+ ':'+ev.widget.to_s
  end


  def show_balloon
    return unless @@show_balloons
    return if @balloon_timer.nil?
    return if @text_balloon.nil?

    x = TkWinfo.pointerx(self)+10
    y = TkWinfo.pointery(self)+1

    if @w_balloon.nil?
      @w_balloon= TkToplevel.new {geometry "+#{x}+#{y}"
        overrideredirect true}

      opts= TkDefaults::GUI['tkballoon.label'].update( 'text'=> @text_balloon)
      TkLabel.new(@w_balloon, opts ).pack
    else
      @w_balloon.geometry("+#{x}+#{y}")
    end
    @w_balloon.deiconify
    @timerout.start
  end

  def hide_balloon
    return if @balloon_timer.nil?
    if @w_balloon
      @balloon_timer.stop
      @w_balloon.withdraw
    end
  end

  def bind_balloon
    return if @balloon_timer.nil?

    self.bind('Enter', proc { @balloon_timer.start})
    self.bind('Leave', proc {hide_balloon})
    self.bind('Motion', proc {@balloon_timer.restart; @timerout.restart})
  end

  def unbind_balloon
    return if @balloon_timer.nil?
    self.bind('Enter','')
    self.bind('Leave','')
    self.bind('Motion','')
    hide_balloon
  end

end


class TkObject
  include TkBalloon

  alias old_configure configure

  def configure(slot, value=None)

    if  slot.kind_of? Hash
      if slot.key?('balloon')
        set_balloon(slot['balloon'])
        slot.delete('balloon')
        bind_balloon
      end
      if slot.key?('label') #and TkDefaults::MENU[slot['label']]
        debug slot['label']
       #slot['label']= TkDefaults::MENU[slot['label']]
      end

      old_configure(slot)
    elsif slot == 'balloon'
        set_balloon(value)
        bind_balloon
      else
        old_configure(slot, value)
      end
      self
    end

  end

#ruby 1.6 a enlever après
  module  TkcTagAccess
    alias old_configure configure
    def configure(slot, value=None)
      if  slot.kind_of? Hash
        if slot.key?('balloon')
          set_balloon(slot['balloon'])
          slot.delete('balloon')
          bind_balloon
        end
        if slot.key?('label') #and TkDefaults::MENU[slot['label']]
          debug slot['label']
       #slot['label']= TkDefaults::MENU[slot['label']]
      end

      old_configure(slot)
    elsif slot == 'balloon'
          set_balloon(value)
          bind_balloon
        else
          old_configure(slot, value)
        end
        self

      end
    end


    class TkWindow

      alias old_init initialize

      def initialize(parent=nil,keys=nil)
=begin
        puts caller
        puts "parent:"
        p parent
        puts "objet:"
        p self
        puts "clefs:"
        p keys
=end

        if parent.kind_of? Hash
          parse_keys(parent)
        elsif keys
          parse_keys(keys)
        end
        old_init(parent,keys)
        bind_balloon if  @text_balloon
        self
    end


   # if keys.key?('text')
   #  keys['text']='bidon'
   #end


  def TkWindow.show_balloons=(val)
    @@show_balloons= val
  end


end


# set_trace_func proc { |event, file, line, id, binding, classname|
#    printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname unless file == "/home/jef/lib/ruby/1.6/tk.rb"
#   }



#$menu_font="user-medium-r-normal-*-*-*-*-*-*-*-iso8859-15"

=begin
class TkMenu

def translate(keys=nil)
if keys and keys.key?('label')
if TkDefaults::MENU[keys['label']]
keys['label']=TkDefaults::MENU[keys['label']]
else
#	puts "MENU[\"#{keys['label']}\"]=\"\"" unless keys['label']== $user
end
end
end


alias old_add add
def add(type, keys=nil)
translate(keys)
old_add(type,keys)
end



alias old_insert insert
def insert(index, type, keys=nil)
translate(keys)
old_insert(index, type, keys)
end

end

=end

class TkcTagItem
  def delete
    @c.delete @id
    if CTagID_TBL[@path]
      CTagID_TBL[@path].delete(@id)
      CTagID_TBL.delete(@path)
    end
  end

end

#ruby 1.7
class TkcItem

  alias old_init initialize
  def initialize(parent, *args)
    if args[-1].kind_of? Hash
      parse_keys(args[-1])
    end
    old_init(parent,*args)
  end



# to be removed with ruby 1.7
  #alias old_delete delete
  def delete
    @c.delete @id
    if CItemID_TBL[@path]
      CItemID_TBL[@path].delete(@id)
      CItemID_TBL.delete(@path)
    end
  end
end


#bug correction to delete with next version
class  TkText

  def tag_bind(tag, seq, cmd=Proc.new, args=nil)
    _bind([@path, 'tag', 'bind', tag], seq, cmd, args)

  end

  def tag_bind_append(tag, seq, cmd=Proc.new, args=nil)
    _bind_append([@path, 'tag', 'bind', tag], seq, cmd, args)
  end

  def tag_bindinfo(tag, context=nil)
    _bindinfo([@path, 'tag', 'bind', tag], context)

  end

end


