3
class PageDrawer (gtk.DrawingArea):
5
def __init__(self, page_width=None, page_height=None,
6
sub_areas=[],xalign=0.5,yalign=0.5
8
"""Draw a page based on page areas given to us.
10
The areas can be given in any scale they like.
12
sub_areas are each (X1,Y1,WIDTH,HEIGHT) where the point defines
13
the upper-left corner of the rectangle.
18
gtk.DrawingArea.__init__(self)
19
self.gc = None # initialized in realize-event handler
20
self.width = 0 # updated in size-allocate handler
21
self.height = 0 # idem
22
if page_width and page_height:
23
self.set_page_area(page_width,page_height,sub_areas)
24
self.connect('size-allocate', self.on_size_allocate)
25
self.connect('expose-event', self.on_expose_event)
26
self.connect('realize', self.on_realize)
28
def set_page_area (self, page_width, page_height, sub_areas=[]):
29
self.xy_ratio = page_width/page_height
31
for x1,y1,w,h in sub_areas:
32
width = float(w)/page_width
33
height = float(h)/page_height
34
x = float(x1)/page_width
35
y = float(y1)/page_height
40
def on_realize(self, widget):
41
self.gc = widget.window.new_gc()
42
#self.gc.set_line_attributes(3, gtk.gdk.LINE_ON_OFF_DASH,
43
# gtk.gdk.CAP_ROUND, gtk.gdk.JOIN_ROUND)
45
def on_size_allocate(self, widget, allocation):
46
self.width = allocation.width
47
self.height = allocation.height
49
def on_expose_event(self, widget, event):
50
if not hasattr(self,'xy_ratio'): return
51
# This is where the drawing takes place
52
if self.xy_ratio * self.height > self.width:
53
width = int(self.width * 0.9)
54
height = int((self.width / self.xy_ratio) * 0.9)
56
width = int(self.xy_ratio*self.height*0.9)
57
height = int(self.height*0.9)
58
xpadding = int((self.width - width)*self.xalign)
59
ypadding = int((self.height - height)*self.yalign)
60
self.gc.set_line_attributes(3,
64
widget.window.draw_rectangle(self.gc, False,
65
xpadding, ypadding, width, height)
66
self.gc.set_line_attributes(1,
67
gtk.gdk.LINE_ON_OFF_DASH,
68
gtk.gdk.CAP_BUTT,gtk.gdk.JOIN_MITER)
69
for sub_area in self.areas:
71
self.window.draw_rectangle(
73
int(xpadding+(x*width)),int(ypadding+(y*height)),int(w*width),int(h*height)
75
#widget.window.draw_line(self.gc,
76
# 0, 0, self.width - 1, self.height - 1)
77
#widget.window.draw_line(self.gc,
78
# self.width - 1, 0, 0, self.height - 1)
81
if __name__ == '__main__':
83
w.add(PageDrawer(8.5,11,[(1,1,3,9.5),
87
w.connect('delete-event',lambda *args: gtk.main_quit())