def __init__(self): window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Drawing Area Example") window.connect("destroy", lambda w: gtk.main_quit()) self.area = gtk.DrawingArea() self.area.set_size_request(400, 300) self.pangolayout = self.area.create_pango_layout("") self.sw = gtk.ScrolledWindow() self.sw.add_with_viewport(self.area) self.table = gtk.Table(2,2) self.hruler = gtk.HRuler() self.vruler = gtk.VRuler() self.hruler.set_range(0, 400, 0, 400) self.vruler.set_range(0, 300, 0, 300) #self.table.attach(self.hruler, 1, 2, 0, 1, yoptions=0) #self.table.attach(self.vruler, 0, 1, 1, 2, xoptions=0) self.table.attach(self.sw, 1, 2, 1, 2) window.add(self.table) self.area.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK ) self.area.connect("expose-event", self.area_expose_cb) def motion_notify(ruler, event): return ruler.emit("motion_notify_event", event) self.area.connect_object("motion_notify_event", motion_notify, self.hruler) self.area.connect_object("motion_notify_event", motion_notify, self.vruler) self.hadj = self.sw.get_hadjustment() self.vadj = self.sw.get_vadjustment() def val_cb(adj, ruler, horiz): if horiz: span = self.sw.get_allocation()[3] else: span = self.sw.get_allocation()[2] l,u,p,m = ruler.get_range() v = adj.value ruler.set_range(v, v+span, p, m) while gtk.events_pending(): gtk.main_iteration() self.hadj.connect('value-changed', val_cb, self.hruler, True) self.vadj.connect('value-changed', val_cb, self.vruler, False) def size_allocate_cb(wid, allocation): x, y, w, h = allocation l,u,p,m = self.hruler.get_range() m = max(m, w) self.hruler.set_range(l, l+w, p, m) l,u,p,m = self.vruler.get_range() m = max(m, h) self.vruler.set_range(l, l+h, p, m) #self.sw.connect('size-allocate', size_allocate_cb) ''''self.area.show() self.hruler.show() self.vruler.show() self.sw.show() self.table.show()''' window.show_all()
def __init__(self): window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.connect("delete_event", self.close_application) window.set_border_width(10) # Create a table for placing the ruler and the drawing area table = gtk.Table(3, 2, False) window.add(table) area = gtk.DrawingArea() area.set_size_request(self.XSIZE, self.YSIZE) table.attach(area, 1, 2, 1, 2, gtk.EXPAND | gtk.FILL, gtk.FILL, 0, 0) area.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK) # The horizontal ruler goes on top. As the mouse moves across the # drawing area, a motion_notify_event is passed to the # appropriate event handler for the ruler. hrule = gtk.HRuler() hrule.set_metric(gtk.PIXELS) hrule.set_range(7, 13, 0, 20) def motion_notify(ruler, event): return ruler.emit("motion_notify_event", event) area.connect_object("motion_notify_event", motion_notify, hrule) table.attach(hrule, 1, 2, 0, 1, gtk.EXPAND | gtk.SHRINK | gtk.FILL, gtk.FILL, 0, 0) # The vertical ruler goes on the left. As the mouse moves across # the drawing area, a motion_notify_event is passed to the # appropriate event handler for the ruler. vrule = gtk.VRuler() vrule.set_metric(gtk.PIXELS) vrule.set_range(0, self.YSIZE, 10, self.YSIZE) area.connect_object("motion_notify_event", motion_notify, vrule) table.attach(vrule, 0, 1, 1, 2, gtk.FILL, gtk.EXPAND | gtk.SHRINK | gtk.FILL, 0, 0) # Now show everything area.show() hrule.show() vrule.show() table.show() window.show()
def __set_draw(self): #vb=gtk.ScrolledWindow() tg = gtk.Table(2, 2, False) hrule = gtk.HRuler() hrule.set_metric(gtk.PIXELS) hrule.set_range(7, 13, 0, 20) tg.attach(hrule, 1, 2, 0, 1, gtk.EXPAND | gtk.SHRINK | gtk.FILL, gtk.FILL, 0, 0) vrule = gtk.VRuler() vrule.set_metric(gtk.PIXELS) vrule.set_range(7, 13, 0, 20) tg.attach(vrule, 0, 1, 1, 2, gtk.FILL, gtk.EXPAND | gtk.SHRINK | gtk.FILL, 0, 0) area = gtk.DrawingArea() tg.attach(area, 1, 2, 1, 2) #vb.add(tg) #return vb return tg
def initui(self): """ Create the window and all of the UI widgets, and make them visible. """ ## Creating the objects # Create a window with custom accelerators accels = gtk.AccelGroup() self.window = gtk.Window() self.window.set_title('DVI Display: %s' % self.path) self.window.set_icon_name('display') self.window.connect('destroy', self.destroy) self.window.add_accel_group(accels) # Use a table for the main layout. # In it are rulers, a scrolling window for the image, and a toolbar. self.table = gtk.Table(rows=3, columns=2) self.hruler = gtk.HRuler() self.vruler = gtk.VRuler() self.scrollwindow = gtk.ScrolledWindow() self.scrollwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.scrollwindow.get_hadjustment().connect('value-changed', self.update_rulers) self.scrollwindow.get_vadjustment().connect('value-changed', self.update_rulers) self.scrollwindow.connect('size-allocate', self.update_rulers) # gtk.Image cannot scroll by itself, so we wrap it in a gtk.Viewport. self.imageviewport = gtk.Viewport() # Request mouse events self.imageviewport.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK) self.imageviewport.connect('motion-notify-event', self.image_mouse_move) self.imageviewport.connect( 'button-press-event', lambda w, event: self.image_mouse_button(event.button, True)) self.imageviewport.connect( 'button-release-event', lambda w, event: self.image_mouse_button(event.button, False)) self.imagedisplay = gtk.Image() self.imagedisplay.set_alignment(0, 0) # The toolbar is a simple H-box self.toolbar = gtk.HBox() # A button to display the mouse coordinates and copy them to clipboard self.coordinates = gtk.Button('') self.coordinates.get_child().set_markup( '<span face="mono"> %4s %4s </span>' % ('', '')) self.coordinates.connect('clicked', self.coordinates_clicked) # Other toolbar widgets: previous, next, save, save all, progress self.prevbutton = gtk.Button(stock=gtk.STOCK_MEDIA_PREVIOUS) self.prevbutton.connect('clicked', lambda b: self.display_image(self.index - 1)) self.nextbutton = gtk.Button(stock=gtk.STOCK_MEDIA_NEXT) self.nextbutton.connect('clicked', lambda b: self.display_image(self.index + 1)) self.savebutton = gtk.Button(stock=gtk.STOCK_SAVE) self.savebutton.connect('clicked', lambda b: self.save_image(False)) self.saveallbutton = gtk.Button("Save _All") self.saveallbutton.set_image( gtk.image_new_from_stock(gtk.STOCK_SAVE, gtk.ICON_SIZE_BUTTON)) self.saveallbutton.connect('clicked', lambda b: self.save_image(True)) self.progress = gtk.ProgressBar() # Update the toolbar, setting sensitivities and text self.update_bar() ## Defining extra accelerators self.nextbutton.add_accelerator('clicked', accels, ord('n'), 0, 0) self.nextbutton.add_accelerator('clicked', accels, ord('j'), 0, 0) self.nextbutton.add_accelerator('clicked', accels, ord('l'), 0, 0) self.nextbutton.add_accelerator('clicked', accels, ord('.'), 0, 0) self.nextbutton.add_accelerator('clicked', accels, ord('>'), 0, 0) self.prevbutton.add_accelerator('clicked', accels, ord('p'), 0, 0) self.prevbutton.add_accelerator('clicked', accels, ord('k'), 0, 0) self.prevbutton.add_accelerator('clicked', accels, ord('h'), 0, 0) self.prevbutton.add_accelerator('clicked', accels, ord(','), 0, 0) self.prevbutton.add_accelerator('clicked', accels, ord('<'), 0, 0) self.savebutton.add_accelerator('clicked', accels, ord('s'), gtk.gdk.CONTROL_MASK, 0) self.coordinates.add_accelerator('clicked', accels, ord('c'), gtk.gdk.CONTROL_MASK, 0) ## Making the widget hierarchy self.imageviewport.add(self.imagedisplay) self.scrollwindow.add(self.imageviewport) self.table.attach(self.hruler, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL) self.table.attach(self.vruler, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL) self.table.attach(self.scrollwindow, 1, 2, 1, 2) self.toolbar.pack_start(self.prevbutton, expand=False) self.toolbar.pack_start(self.coordinates, expand=False) self.toolbar.pack_start(self.progress) self.toolbar.pack_start(self.saveallbutton, expand=False) self.toolbar.pack_start(self.savebutton, expand=False) self.toolbar.pack_start(self.nextbutton, expand=False) self.table.attach(self.toolbar, 0, 2, 2, 3, yoptions=gtk.FILL) self.window.add(self.table) ## Show everything self.window.show_all()
def __init__(self, winMain): self.winMain = winMain gtk.Table.__init__(self, 3, 3, False) # Ruler horizontal/vertical self.hruler = gtk.HRuler() self.hruler.set_metric(gtk.PIXELS) self.hruler.set_range(0, 11111, 0, 11111) self.vruler = gtk.VRuler() self.vruler.set_metric(gtk.PIXELS) self.vruler.set_range(0, 11111, 0, 11111) # Scrolled Window containing drawing Area self.area = gtk.DrawingArea() self.area.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK) self.area.set_size_request(11111, 11111) self.area.connect("expose-event", self.area_expose_cb) self.sw = gtk.ScrolledWindow() self.sw.add_with_viewport(self.area) self.attach(self.hruler, 1, 2, 0, 1, gtk.EXPAND | gtk.SHRINK | gtk.FILL, gtk.FILL, 0, 0) self.attach(self.vruler, 0, 1, 1, 2, gtk.FILL, gtk.EXPAND | gtk.SHRINK | gtk.FILL, 0, 0) self.attach(self.sw, 1, 2, 1, 2, gtk.FILL, gtk.FILL, 0, 0) # Tell mouse motion to rulers for updating the mouse position marker. def motion_notify(ruler, event): return ruler.emit("motion_notify_event", event) self.sw.connect_object("motion_notify_event", motion_notify, self.hruler) self.sw.connect_object("motion_notify_event", motion_notify, self.vruler) # Set ruler scaling regarding to the visible area def size_allocate_cb(wid, allocation): x, y, w, h = allocation #print("x: %d y: %d w:%d h:%d" % (x,y,w,h)) lower, upper, pos, maxSize = self.hruler.get_range() maxSize = max(maxSize, w) self.hruler.set_range(lower, lower + w, pos, maxSize) lower, upper, pos, maxSize = self.vruler.get_range() maxSize = max(maxSize, h) self.vruler.set_range(lower, lower + h, pos, maxSize) self.sw.connect('size-allocate', size_allocate_cb) # Recalculate ruler ranges with respect to scroll window and mouse position def val_cb(adj, ruler, horiz): #print("lower: %d value: %d upper-page_size:%d upper:%d, page_size:%d" % (adj.lower, adj.value, adj.upper-adj.page_size,adj.upper,adj.page_size)) #position of the scrollbar between 'lower' and 'upper -page_size'. #lower is 0, upper is the maximum value of the scroll region (self.are.set_size_request()), page_size is the scroll knob length. v = adj.value if horiz: span = self.sw.get_allocation().width else: span = self.sw.get_allocation().height lower, upper, pos, maxSize = ruler.get_range() ruler.set_range(v, v + span, pos - v, maxSize) while gtk.events_pending(): gtk.main_iteration() self.hadj = self.sw.get_hadjustment() self.hadj.connect('value-changed', val_cb, self.hruler, True) self.vadj = self.sw.get_vadjustment() self.vadj.connect('value-changed', val_cb, self.vruler, False) self.hruler.show() self.vruler.show() self.sw.show() self.area.show() self.show()