9100476a469fd9128e1bfdff9f899ef4d8d10a9a
[debian/gnuradio] / grc / src / grc / gui / DrawingArea.py
1 """
2 Copyright 2007 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19 ##@package grc.gui.DrawingArea
20 #Drawing area for graphical elements.
21
22 import pygtk
23 pygtk.require('2.0')
24 import gtk
25 from grc.Constants import *
26
27 class DrawingArea(gtk.DrawingArea):
28         """
29         DrawingArea is the gtk pixel map that graphical elements may draw themselves on.
30         The drawing area also responds to mouse and key events.
31         """
32
33         def __init__(self, main_window):
34                 """!
35                 DrawingArea contructor.
36                 Connect event handlers.
37                 @param main_window the main_window containing all flow graphs
38                 """
39                 self.ctrl_mask = False
40                 self._main_window = main_window
41                 #inject drawing area into main_window
42                 self._main_window.drawing_area = self
43                 gtk.DrawingArea.__init__(self)
44                 self.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT)
45                 self.connect('expose-event', self._handle_window_expose)
46                 self.connect('motion-notify-event', self._handle_mouse_motion)
47                 self.connect('button-press-event', self._handle_mouse_button_press)
48                 self.connect('button-release-event', self._handle_mouse_button_release)
49                 self.set_events(
50                         gtk.gdk.BUTTON_PRESS_MASK | \
51                         gtk.gdk.POINTER_MOTION_MASK | \
52                         gtk.gdk.BUTTON_RELEASE_MASK | \
53                         gtk.gdk.LEAVE_NOTIFY_MASK | \
54                         gtk.gdk.ENTER_NOTIFY_MASK
55                 )
56                 #setup the focus flag
57                 self._focus_flag = False
58                 self.get_focus_flag = lambda: self._focus_flag
59                 self.connect("leave-notify-event", self._handle_focus_event, False)
60                 self.connect("enter-notify-event", self._handle_focus_event, True)
61                 #pixmap for drawing
62                 self.pixmap = None
63                 self.gc = None
64
65         def draw(self):
66                 """!
67                 Draw the pixmap onto this drawing area.
68                 """
69                 self.window.draw_drawable(self.gc, self.pixmap, 0, 0, 0, 0, -1, -1)
70
71         ##########################################################################
72         ## Handlers
73         ##########################################################################
74         def _handle_focus_event(self, widget, event, focus_flag):
75                 """Record the focus state of the flow graph window."""
76                 self._focus_flag = focus_flag
77
78         def _handle_mouse_button_press(self, widget, event):
79                 """!
80                 Forward button click information to the flow graph.
81                 """
82                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
83                 self._main_window.get_flow_graph().handle_mouse_button_press(
84                         left_click=(event.button == 1),
85                         double_click=(event.type == gtk.gdk._2BUTTON_PRESS),
86                         coordinate=(event.x, event.y),
87                 )
88                 return True
89
90         def _handle_mouse_button_release(self, widget, event):
91                 """!
92                 Forward button release information to the flow graph.
93                 """
94                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
95                 self._main_window.get_flow_graph().handle_mouse_button_release(
96                         left_click=(event.button == 1),
97                         coordinate=(event.x, event.y),
98                 )
99                 return True
100
101         def _handle_mouse_motion(self, widget, event):
102                 """!
103                 Forward mouse motion information to the flow graph.
104                 """
105                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
106                 self._main_window.get_flow_graph().handle_mouse_motion(
107                         coordinate=(event.x, event.y),
108                 )
109                 return True
110
111         def _handle_window_expose(self, widget, event):
112                 """!
113                 Called when the window initially appears or is resized: create a new pixmap, draw the flow graph.
114                 """
115                 self.gc = self.window.new_gc()
116                 width, height = self.get_size_request()
117                 if not self.pixmap or (width, height) != self.pixmap.get_size():
118                         self.pixmap = gtk.gdk.Pixmap(self.window, width, height, -1)
119                 self._main_window.get_flow_graph().draw()
120                 return True