#!/usr/bin/env ruby
# vim: ts=2 sw=2

# make this demo run without installing the library...
$: << "../src"

require 'evas'

FONTPATH = '/usr/share/fonts/truetype'
TEXT = 'abcdef ABCDEF 12345 The quick brown fox jumped over the lazy dogs.'

dpy = Display.new
$width, $height = dpy.width/2, dpy.height*3/4

$evas = Evas.new(
	:display => dpy,
	:width => $width, :height => $height,
	:title => 'Evas Demonstration',
	:fontpath => FONTPATH )

rect = $evas.new_rectangle()
rect.move(0, 0)
rect.resize($width, $height)
rect.set_color(255, 255, 255, 255)
rect.show()

$fontsize = 10
$textobjects = []

$cliprect = $evas.new_rectangle
$cliprect.move(0, 0)
$cliprect.resize($width - 50, $height)
$cliprect.set_color(0, 0, 0, 255)
$cliprect.show()

$highlight = $evas.new_rectangle
$highlight.set_color(200, 50, 50, 255)

rect.onevent(EvasObject::EVENT_MOUSE_DOWN, proc {|b, x, y|
	case b
	when 4
		$fontsize -= 1
	when 5
		$fontsize += 1
	end
	relayout()
})

def highlight(obj)
	$highlight.move(obj.x, obj.y)
	$highlight.resize(obj.width < $cliprect.width ? obj.width : $cliprect.width, obj.height)
	$highlight.show()
end
def unhighlight
	$highlight.hide()
end

def relayout
	$textobjects.each {|obj| obj.del }
	$textobjects = []

	Dir.chdir(FONTPATH)
	y = 0
	Dir["*.ttf"].each {|font|
		font.sub!(/.ttf$/, '');
		puts font
		text = $evas.new_text(font, $fontsize, " %s: %s" % [font, TEXT])
		text.move(5, y)
		text.set_layer(1)
		text.set_color(0, 0, 0, 255)
		text.set_clip $cliprect
		text.show()

		text.onevent(EvasObject::EVENT_MOUSE_IN, proc { highlight(text) })
		text.onevent(EvasObject::EVENT_MOUSE_OUT, proc { unhighlight })
		
		$textobjects << text
		y = y + text.height
		break if y > $height
	}
end

relayout
loop {
	$evas.handle_x_events
	$evas.render
}


