ca5ca4a76fc1f0f8573719bb7d684bb3a7f4a872
[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 #@author Josh Blum
22
23 import pygtk
24 pygtk.require('2.0')
25 import gtk
26 from grc.Constants import *
27
28 class DrawingArea(gtk.DrawingArea):
29         """
30         DrawingArea is the gtk pixel map that graphical elements may draw themselves on.
31         The drawing area also responds to mouse and key events.
32         """
33
34         def __init__(self, main_window):
35                 """!
36                 DrawingArea contructor.
37                 Connect event handlers.
38                 @param main_window the main_window containing all flow graphs
39                 """
40                 self.ctrl_mask = False
41                 self._main_window = main_window
42                 #inject drawing area into main_window
43                 self._main_window.drawing_area = self
44                 gtk.DrawingArea.__init__(self)
45                 self.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT)
46                 self.connect('expose-event', self._handle_window_expose)
47                 self.connect('motion-notify-event', self._handle_mouse_motion)
48                 self.connect('button-press-event', self._handle_mouse_button_press)
49                 self.connect('button-release-event', self._handle_mouse_button_release)
50                 self.set_events(
51                         gtk.gdk.BUTTON_PRESS_MASK | \
52                         gtk.gdk.POINTER_MOTION_MASK | \
53                         gtk.gdk.BUTTON_RELEASE_MASK | \
54                         gtk.gdk.LEAVE_NOTIFY_MASK | \
55                         gtk.gdk.ENTER_NOTIFY_MASK
56                 )
57                 #setup the focus flag
58                 self._focus_flag = False
59                 self.get_focus_flag = lambda: self._focus_flag
60                 self.connect("leave-notify-event", self._handle_focus_event, False)
61                 self.connect("enter-notify-event", self._handle_focus_event, True)
62                 #pixmap for drawing
63                 self.pixmap = None
64                 self.gc = None
65
66         def draw(self):
67                 """!
68                 Draw the pixmap onto this drawing area.
69                 """
70                 self.window.draw_drawable(self.gc, self.pixmap, 0, 0, 0, 0, -1, -1)
71
72         ##########################################################################
73         ## Handlers
74         ##########################################################################
75         def _handle_focus_event(self, widget, event, focus_flag):
76                 """Record the focus state of the flow graph window."""
77                 self._focus_flag = focus_flag
78
79         def _handle_mouse_button_press(self, widget, event):
80                 """!
81                 Forward button click information to the flow graph.
82                 """
83                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
84                 self._main_window.get_flow_graph().handle_mouse_button_press(
85                         left_click=(event.button == 1),
86                         double_click=(event.type == gtk.gdk._2BUTTON_PRESS),
87                         coordinate=(event.x, event.y),
88                 )
89                 return True
90
91         def _handle_mouse_button_release(self, widget, event):
92                 """!
93                 Forward button release information to the flow graph.
94                 """
95                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
96                 self._main_window.get_flow_graph().handle_mouse_button_release(
97                         left_click=(event.button == 1),
98                         coordinate=(event.x, event.y),
99                 )
100                 return True
101
102         def _handle_mouse_motion(self, widget, event):
103                 """!
104                 Forward mouse motion information to the flow graph.
105                 """
106                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
107                 self._main_window.get_flow_graph().handle_mouse_motion(
108                         coordinate=(event.x, event.y),
109                 )
110                 return True
111
112         def _handle_window_expose(self, widget, event):
113                 """!
114                 Called when the window initially appears or is resized: create a new pixmap, draw the flow graph.
115                 """
116                 self.gc = self.window.new_gc()
117                 width, height = self.get_size_request()
118                 if not self.pixmap or (width, height) != self.pixmap.get_size():
119                         self.pixmap = gtk.gdk.Pixmap(self.window, width, height, -1)
120                 self._main_window.get_flow_graph().draw()
121                 return True