#! /usr/bin/python
#
# expose a bug in font metrics handling.
# (c) 2013, jw@suse.de
#
# Requires: zypper in typelib-1_0-GooCanvas-2_0

from gi.repository import Gtk as gtk
from gi.repository import GooCanvas as goocanvas
from gi.repository import cairo

def scale_up(win, ev):
  s = canvas.get_scale()  
  if   chr(ev.keyval) == '+':  canvas.set_scale(s*1.2)
  elif chr(ev.keyval) == '-':  canvas.set_scale(s*.8)
  else: gtk.main_quit()
  print canvas.get_scale()

def button_press(win, ev):
  win.click_x = ev.x
  win.click_y = ev.y
  print win.click_x, win.click_y

def button_release(win, ev):
  win.click_x = None
  win.click_y = None

def motion_notify(win, ev):
  try:
    dx = (ev.x-win.click_x)/canvas.get_scale()
    dy = (ev.y-win.click_y)/canvas.get_scale()
    win.click_x = ev.x
    win.click_y = ev.y
    (x1,y1,x2,y2) = canvas.get_bounds()
    canvas.set_bounds(x1-dx,y1-dy,x2-dx,y2-dy)
  except:
    pass


win = gtk.Window()
win.connect("destroy", gtk.main_quit)
win.connect("key-press-event", scale_up)
win.connect("motion-notify-event", motion_notify)
win.connect("button-press-event", button_press)
win.connect("button-release-event", button_release)
        
canvas = goocanvas.Canvas(units='mm', scale=2)
canvas.set_size_request(1000, 400)
root = canvas.get_root_item()

goocanvas.CanvasText(parent=root, x=0, y=0,  text="TooT, World", font="6")
goocanvas.CanvasText(parent=root, x=0, y=8, text="TooT, World", font="sans 9")
goocanvas.CanvasText(parent=root, x=0, y=20, text="TooT, World", font="Arial 12")
goocanvas.CanvasText(parent=root, x=0, y=30, text="TooT, World", font="24")
                
win.add(canvas)
win.show_all()

gtk.main()
