class PanedText < TkcText

  def initialize(parent,x,y,opts)
    @parent= parent
    super(parent,x,y,opts)
  end

  def configure(args)
    super(args)
    @parent.reconfigure
    if @parent.kind_of? VTexturedPane
      pwidth= @parent.width
   #puts $ip._eval("#{@parent.path} cget -width")
      box= bbox
      w= box[2] - box[0]
      if pwidth > 0
        coords((pwidth-w)/2,box[1])
      elsif  @parent.fixed_w > 0
        coords((@parent.fixed_w - w)/2,box[1])
      else
        coords(20,box[1])
      end
    end

  end
end


class TexturedPane < TkCanvas

  attr_reader :width, :height, :fixed_w, :fixed_h
  def initialize (parent,img,padx,pady,dimx,dimy )

    @img=img
    @padx= padx
    @pady= pady
    super(parent)
    make_texture(dimx,dimy)
    @items= []
    @width=0
    @height=0
    @fixed_w = -1
    @fixed_h = -1
    cb = proc { |w,h| re_size(w.to_i,h.to_i) }

    TkCb.tk_register_cb(cb)
    $ip._eval(" bind #@path <Configure> {ruby {tk_callback( '#{cb.id}',%w,%h)}}")

  end

  def re_size(w,h)
    @width=w
    @height=h
  end




  def set_fixed_size(w,h)
    @fixed_w = w
    @fixed_h = h
  end


  def make_texture(dimx,dimy)

    w=  @img.width
    h=  @img.height


    for i in 0..dimx
      for j in 0..dimy
        $ip._eval("#@path create image #{i*w} #{j*h} -anchor nw -image #{@img.path}")
      end
    end
  end

  def add(widget)

    Tk.update
    item= TkcWindow.new(self,0,0,'anchor'=>'nw','window'=>widget,'tags'=>['item'])
    @items.push(item)
     # windows bug: ruby crashes
     #widget.bind('Configure',proc { reconfigure })
    item
  end

  def add_image (opts)
    item= TkcImage.new(self,0,0,opts.update('anchor'=>'nw','tags'=>['item']))
    Tk.update
    @items.push(item)
     #  item.bind('Configure',proc { reconfigure })
    item
  end

  def add_text (opts)

    item= PanedText.new(self,0,0,opts.update('anchor'=>'nw','tags'=>['item']))
    Tk.update
    @items.push(item)
      # item.bind('Configure',proc { reconfigure })
    item
  end

  def reconfigure
  end

end


class HTexturedPane < TexturedPane

  def reconfigure
#Tk.update

    offset = cget('borderwidth')
    posx=@padx
    maxh= 0
    h=0
    box= nil
    if @fixed_h <0

      @items.each { |item| 

        box= bbox(item)

        if box.empty?

          h = 32  # aheum
        else
          h= box[3] - box[1]
        end
        maxh = h if h > maxh
      }
      maxh += 2*@pady
    else 
      maxh = @fixed_h
    end

    @items.each { |item|

      box= bbox(item)

      if box.empty?

        h = 32  # aheum
      else
        h= box[3] - box[1]
      end

      item.coords(posx+offset,(maxh - h)/2+offset)

      posx += box[2] - box[0] + @padx
    }

    if @fixed_w >= 0
      posx = @fixed_w
    end
    configure('width'=> posx, 'height'=> maxh)

  end

end

class VTexturedPane < TexturedPane

  def reconfigure
#   Tk.update

    offset = cget('borderwidth')

    posy=@pady
    maxw= 0
    box=nil
    w=0

    if @fixed_w < 0
      @items.each { |item| 
        box= bbox(item)
        w= box[2] - box[0]
        maxw = w if w > maxw
      }
      maxw += 2*@padx
    else
      maxw =  @fixed_w 
    end

    @items.each { |item|
      box= bbox(item)
      w= box[2] - box[0]
      item.coords((maxw - w)/2 + offset,posy+offset)
  #	debug "#{item}: #{maxw} #{w} #{(maxw - w)/2}"
      posy += box[3] - box[1] + @pady
    }

    if @fixed_h > 0
      posy = @fixed_h
    end
    configure('width'=> maxw, 'height'=>posy  )
  end

end


class PTexturedPane < TexturedPane


  def reconfigure
#Tk.update
    box= bbox('item')
    maxw= box[2]
    maxh = box[3]


    configure('width'=> maxw, 'height'=>maxh  )
  end

  def place(target,sourcex,sourcey,mx=0,my=0)
    offset = cget('borderwidth')
    if sourcex == self
      posx = @padx +mx
    else
      box = bbox(sourcex)
      posx = box[2] + mx
    end
    if sourcey == self
      posy = @pady +my
    else
      box = bbox(sourcey)
      posy = box[3] + my
    end
    target.coords(posx+offset,posy+offset)
  end

end



