Appendix B. Code Examples

B.1. scribblesimple.py

    1   #!/usr/bin/env python
    2   
    3   # example scribblesimple.py
    4   
    5    # GTK - The GIMP Toolkit
    6    # Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
    7    #
    8    # This library is free software; you can redistribute it and/or
    9    # modify it under the terms of the GNU Library General Public
   10    # License as published by the Free Software Foundation; either
   11    # version 2 of the License, or (at your option) any later version.
   12    #
   13    # This library is distributed in the hope that it will be useful,
   14    # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16    # Library General Public License for more details.
   17    #
   18    # You should have received a copy of the GNU Library General Public
   19    # License along with this library; if not, write to the
   20    # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   21    # Boston, MA 02111-1307, USA.
   22   
   23   
   24   import gtk
   25   import GDK
   26   
   27   # Backing pixmap for drawing area
   28   pixmap = None
   29   
   30   # Create a new backing pixmap of the appropriate size
   31   def configure_event(widget, event):
   32       global pixmap
   33   
   34       x, y, width, height = widget.get_allocation()
   35       pixmap = gtk.create_pixmap(widget.get_window(), width, height, -1)
   36       gtk.draw_rectangle(pixmap, widget.get_style().white_gc,
   37                          gtk.TRUE, 0, 0, width, height)
   38   
   39       return gtk.TRUE
   40   
   41   # Redraw the screen from the backing pixmap
   42   def expose_event(widget, event):
   43       x , y, width, height = event.area
   44       widget.draw_pixmap(widget.get_style().fg_gc[gtk.STATE_NORMAL],
   45                          pixmap, x, y, x, y, width, height)
   46       return gtk.FALSE
   47   
   48   # Draw a rectangle on the screen
   49   def draw_brush(widget, x, y):
   50       rect = (x - 5, y - 5, 10, 10)
   51       gtk.draw_rectangle(pixmap, widget.get_style().black_gc, gtk.TRUE,
   52                          rect[0], rect[1], rect[2], rect[3])
   53       widget.draw(rect)
   54   
   55   def button_press_event(widget, event):
   56       if event.button == 1 and pixmap != None:
   57           draw_brush(widget, event.x, event.y)
   58       return gtk.TRUE
   59   
   60   def motion_notify_event(widget, event):
   61       if event.is_hint:
   62           x, y, = event.window.pointer
   63           state = event.window.pointer_state
   64       else:
   65           x = event.x
   66           y = event.y
   67           state = event.state
   68       
   69       if state & GDK.BUTTON1_MASK and pixmap != None:
   70           draw_brush(widget, x, y)
   71     
   72       return gtk.TRUE
   73   
   74   def main():
   75       window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   76       window.set_name ("Test Input")
   77   
   78       vbox = gtk.GtkVBox(gtk.FALSE, 0)
   79       window.add(vbox)
   80       vbox.show()
   81   
   82       window.connect("destroy", gtk.mainquit)
   83   
   84       # Create the drawing area
   85       drawing_area = gtk.GtkDrawingArea()
   86       drawing_area.size(200, 200)
   87       vbox.pack_start(drawing_area, gtk.TRUE, gtk.TRUE, 0)
   88   
   89       drawing_area.show()
   90   
   91       # Signals used to handle backing pixmap
   92       drawing_area.connect("expose_event", expose_event)
   93       drawing_area.connect("configure_event", configure_event)
   94   
   95       # Event signals
   96       drawing_area.connect("motion_notify_event", motion_notify_event)
   97       drawing_area.connect("button_press_event", button_press_event)
   98   
   99       drawing_area.set_events(GDK.EXPOSURE_MASK
  100                               | GDK.LEAVE_NOTIFY_MASK
  101                               | GDK.BUTTON_PRESS_MASK
  102                               | GDK.POINTER_MOTION_MASK
  103                               | GDK.POINTER_MOTION_HINT_MASK)
  104   
  105       # .. And a quit button
  106       button = gtk.GtkButton("Quit")
  107       vbox.pack_start(button, gtk.FALSE, gtk.FALSE, 0)
  108   
  109       button.connect_object("clicked", window.destroy, window)
  110       button.show()
  111   
  112       window.show()
  113   
  114       gtk.mainloop()
  115   
  116       return 0
  117   
  118   if __name__ == "__main__":
  119       main()

B.2. scribblexinput.py

    1   #!/usr/bin/env python
    2   
    3   # example scribblexinput.py
    4   
    5   # GTK - The GIMP Toolkit
    6   # Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
    7   #
    8   # This library is free software; you can redistribute it and/or
    9   # modify it under the terms of the GNU Library General Public
   10   # License as published by the Free Software Foundation; either
   11   # version 2 of the License, or (at your option) any later version.
   12   #
   13   # This library is distributed in the hope that it will be useful,
   14   # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15   # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16   # Library General Public License for more details.
   17   #
   18   # You should have received a copy of the GNU Library General Public
   19   # License along with this library; if not, write to the
   20   # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   21   # Boston, MA 02111-1307, USA.
   22   
   23   
   24   import gtk
   25   import GDK
   26   
   27   inputd = None
   28   
   29   # Backing pixmap for drawing area
   30   pixmap = None
   31   
   32   # Create a new backing pixmap of the appropriate size
   33   def configure_event(widget, event):
   34       global pixmap
   35   
   36       x, y, width, height = widget.get_allocation()
   37       pixmap = gtk.create_pixmap(widget.get_window(), width, height, -1)
   38       gtk.draw_rectangle(pixmap, widget.get_style().white_gc,
   39                          gtk.TRUE, 0, 0, width, height)
   40   
   41       return gtk.TRUE
   42   
   43   # Redraw the screen from the backing pixmap
   44   def expose_event(widget, event):
   45       x , y, width, height = event.area
   46       widget.draw_pixmap(widget.get_style().fg_gc[gtk.STATE_NORMAL],
   47                          pixmap, x, y, x, y, width, height)
   48       return gtk.FALSE
   49   
   50   # Draw a rectangle on the screen, size depending on pressure,
   51   # and color on the type of device
   52   def draw_brush(widget, source, x, y, pressure):
   53       if source == GDK.SOURCE_MOUSE:
   54           gc = widget.get_style().dark_gc[gtk.STATE_NORMAL]
   55       elif source == GDK.SOURCE_PEN:
   56           gc = widget.get_style().black_gc
   57       elif source == GDK.SOURCE_ERASER:
   58           gc = widget.get_style().white_gc
   59       else:
   60           gc = widget.get_style().light_gc[gtk.STATE_NORMAL]
   61   
   62       rect = (x-10*pressure, y-10*pressure, 20*pressure, 20*pressure)
   63       gtk.draw_rectangle (pixmap, gc, gtk.TRUE,
   64                           rect[0], rect[1], rect[2], rect[3])
   65       widget.draw(rect)
   66   
   67   def print_button_press(deviceid):
   68       print "Button press on device '%s'" % deviceid
   69       return
   70   
   71   def button_press_event(widget, event):
   72       print_button_press(event.deviceid)
   73     
   74       if event.button == 1 and pixmap != None:
   75           draw_brush (widget, event.source, event.x, event.y, event.pressure)
   76       return gtk.TRUE
   77   
   78   def motion_notify_event(widget, event):
   79       if event.is_hint:
   80           x,y,pressure,d,d,state = event.window.input_get_pointer(event.deviceid)
   81       else:
   82           x = event.x
   83           y = event.y
   84           pressure = event.pressure
   85           state = event.state
   86       
   87       if state & GDK.BUTTON1_MASK and pixmap != None:
   88           draw_brush(widget, event.source, x, y, pressure)
   89     
   90       return gtk.TRUE
   91   
   92   def input_dialog_destroy(w):
   93       global inputd
   94       inputd = None
   95   
   96   def create_input_dialog(widget):
   97       global inputd
   98   
   99       if not inputd:
  100           inputd = gtk.GtkInputDialog()
  101           inputd.connect("destroy", input_dialog_destroy)
  102           inputd.close_button.connect_object("clicked", inputd.hide, inputd)
  103           inputd.save_button.hide()
  104           inputd.show()
  105       else:
  106           if not (inputd.flags() & gtk.MAPPED):
  107               inputd.show()
  108           else:
  109               inputd.get_window()._raise()
  110   
  111   def main():
  112       window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
  113       window.set_name("Test Input")
  114   
  115       vbox = gtk.GtkVBox(gtk.FALSE, 0)
  116       window.add(vbox)
  117       vbox.show()
  118   
  119       window.connect("destroy", gtk.mainquit)
  120   
  121       # Create the drawing area
  122       drawing_area = gtk.GtkDrawingArea()
  123       drawing_area.size(200, 200)
  124       vbox.pack_start(drawing_area, gtk.TRUE, gtk.TRUE, 0)
  125       drawing_area.show()
  126   
  127       # Signals used to handle backing pixmap
  128       drawing_area.connect("expose_event", expose_event)
  129       drawing_area.connect("configure_event", configure_event)
  130   
  131       # Event signals
  132       drawing_area.connect("motion_notify_event", motion_notify_event)
  133       drawing_area.connect("button_press_event", button_press_event)
  134   
  135       drawing_area.set_events(GDK.EXPOSURE_MASK
  136                               | GDK.LEAVE_NOTIFY_MASK
  137                               | GDK.BUTTON_PRESS_MASK
  138                               | GDK.POINTER_MOTION_MASK
  139                               | GDK.POINTER_MOTION_HINT_MASK)
  140   
  141       # The following call enables tracking and processing of extension
  142       # events for the drawing area
  143       drawing_area.set_extension_events(GDK.EXTENSION_EVENTS_CURSOR)
  144   
  145       # .. And some buttons
  146       button = gtk.GtkButton("Input Dialog")
  147       vbox.pack_start(button, gtk.FALSE, gtk.FALSE, 0)
  148       button.connect("clicked", create_input_dialog)
  149       button.show()
  150   
  151       button = gtk.GtkButton("Quit")
  152       vbox.pack_start(button, gtk.FALSE, gtk.FALSE, 0)
  153       button.connect("clicked", window.destroy)
  154       button.show()
  155       window.show()
  156   
  157       gtk.mainloop()
  158       return 0
  159   
  160   if __name__ == "__main__":
  161       main()