#!/usr/bin/env python # example scribblexinput.py # GTK - The GIMP Toolkit # Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Library General Public License for more details. # # You should have received a copy of the GNU Library General Public # License along with this library; if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. import gtk import GDK inputd = None # Backing pixmap for drawing area pixmap = None # Create a new backing pixmap of the appropriate size def configure_event(widget, event): global pixmap x, y, width, height = widget.get_allocation() pixmap = gtk.create_pixmap(widget.get_window(), width, height, -1) gtk.draw_rectangle(pixmap, widget.get_style().white_gc, gtk.TRUE, 0, 0, width, height) return gtk.TRUE # Redraw the screen from the backing pixmap def expose_event(widget, event): x , y, width, height = event.area widget.draw_pixmap(widget.get_style().fg_gc[gtk.STATE_NORMAL], pixmap, x, y, x, y, width, height) return gtk.FALSE # Draw a rectangle on the screen, size depending on pressure, # and color on the type of device def draw_brush(widget, source, x, y, pressure): if source == GDK.SOURCE_MOUSE: gc = widget.get_style().dark_gc[gtk.STATE_NORMAL] elif source == GDK.SOURCE_PEN: gc = widget.get_style().black_gc elif source == GDK.SOURCE_ERASER: gc = widget.get_style().white_gc else: gc = widget.get_style().light_gc[gtk.STATE_NORMAL] rect = (x-10*pressure, y-10*pressure, 20*pressure, 20*pressure) gtk.draw_rectangle (pixmap, gc, gtk.TRUE, rect[0], rect[1], rect[2], rect[3]) widget.draw(rect) def print_button_press(deviceid): print "Button press on device '%s'" % deviceid return def button_press_event(widget, event): print_button_press(event.deviceid) if event.button == 1 and pixmap != None: draw_brush (widget, event.source, event.x, event.y, event.pressure) return gtk.TRUE def motion_notify_event(widget, event): if event.is_hint: x,y,pressure,d,d,state = event.window.input_get_pointer(event.deviceid) else: x = event.x y = event.y pressure = event.pressure state = event.state if state & GDK.BUTTON1_MASK and pixmap != None: draw_brush(widget, event.source, x, y, pressure) return gtk.TRUE def input_dialog_destroy(w): global inputd inputd = None def create_input_dialog(widget): global inputd if not inputd: inputd = gtk.GtkInputDialog() inputd.connect("destroy", input_dialog_destroy) inputd.close_button.connect_object("clicked", inputd.hide, inputd) inputd.save_button.hide() inputd.show() else: if not (inputd.flags() & gtk.MAPPED): inputd.show() else: inputd.get_window()._raise() def main(): window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL) window.set_name("Test Input") vbox = gtk.GtkVBox(gtk.FALSE, 0) window.add(vbox) vbox.show() window.connect("destroy", gtk.mainquit) # Create the drawing area drawing_area = gtk.GtkDrawingArea() drawing_area.size(200, 200) vbox.pack_start(drawing_area, gtk.TRUE, gtk.TRUE, 0) drawing_area.show() # Signals used to handle backing pixmap drawing_area.connect("expose_event", expose_event) drawing_area.connect("configure_event", configure_event) # Event signals drawing_area.connect("motion_notify_event", motion_notify_event) drawing_area.connect("button_press_event", button_press_event) drawing_area.set_events(GDK.EXPOSURE_MASK | GDK.LEAVE_NOTIFY_MASK | GDK.BUTTON_PRESS_MASK | GDK.POINTER_MOTION_MASK | GDK.POINTER_MOTION_HINT_MASK) # The following call enables tracking and processing of extension # events for the drawing area drawing_area.set_extension_events(GDK.EXTENSION_EVENTS_CURSOR) # .. And some buttons button = gtk.GtkButton("Input Dialog") vbox.pack_start(button, gtk.FALSE, gtk.FALSE, 0) button.connect("clicked", create_input_dialog) button.show() button = gtk.GtkButton("Quit") vbox.pack_start(button, gtk.FALSE, gtk.FALSE, 0) button.connect("clicked", window.destroy) button.show() window.show() gtk.mainloop() return 0 if __name__ == "__main__": main()