Merging r11186:11273 from grc branch.
[debian/gnuradio] / grc / gui / DrawingArea.py
1 """
2 Copyright 2007, 2008, 2009 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
20 import pygtk
21 pygtk.require('2.0')
22 import gtk
23 from Constants import MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT, DND_TARGETS
24
25 class DrawingArea(gtk.DrawingArea):
26         """
27         DrawingArea is the gtk pixel map that graphical elements may draw themselves on.
28         The drawing area also responds to mouse and key events.
29         """
30
31         def __init__(self, flow_graph):
32                 """
33                 DrawingArea contructor.
34                 Connect event handlers.
35                 @param main_window the main_window containing all flow graphs
36                 """
37                 self.ctrl_mask = False
38                 self._flow_graph = flow_graph
39                 gtk.DrawingArea.__init__(self)
40                 self.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT)
41                 self.connect('realize', self._handle_window_realize)
42                 self.connect('configure-event', self._handle_window_configure)
43                 self.connect('expose-event', self._handle_window_expose)
44                 self.connect('motion-notify-event', self._handle_mouse_motion)
45                 self.connect('button-press-event', self._handle_mouse_button_press)
46                 self.connect('button-release-event', self._handle_mouse_button_release)
47                 self.add_events(
48                         gtk.gdk.BUTTON_PRESS_MASK | \
49                         gtk.gdk.POINTER_MOTION_MASK | \
50                         gtk.gdk.BUTTON_RELEASE_MASK | \
51                         gtk.gdk.LEAVE_NOTIFY_MASK | \
52                         gtk.gdk.ENTER_NOTIFY_MASK
53                 )
54                 #setup drag and drop
55                 self.drag_dest_set(gtk.DEST_DEFAULT_ALL, DND_TARGETS, gtk.gdk.ACTION_COPY)
56                 self.connect('drag-data-received', self._handle_drag_data_received)
57                 #setup the focus flag
58                 self._focus_flag = False
59                 self.get_focus_flag = lambda: self._focus_flag
60                 def _handle_focus_event(widget, event, focus_flag): self._focus_flag = focus_flag
61                 self.connect('leave-notify-event', _handle_focus_event, False)
62                 self.connect('enter-notify-event', _handle_focus_event, True)
63
64         def new_pixmap(self, width, height): return gtk.gdk.Pixmap(self.window, width, height, -1)
65
66         ##########################################################################
67         ## Handlers
68         ##########################################################################
69         def _handle_drag_data_received(self, widget, drag_context, x, y, selection_data, info, time):
70                 """
71                 Handle a drag and drop by adding a block at the given coordinate.
72                 """
73                 self._flow_graph.add_new_block(selection_data.data, (x, y))
74
75         def _handle_mouse_button_press(self, widget, event):
76                 """
77                 Forward button click information to the flow graph.
78                 """
79                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
80                 self._flow_graph.handle_mouse_button_press(
81                         left_click=(event.button == 1),
82                         double_click=(event.type == gtk.gdk._2BUTTON_PRESS),
83                         coordinate=(event.x, event.y),
84                 )
85
86         def _handle_mouse_button_release(self, widget, event):
87                 """
88                 Forward button release information to the flow graph.
89                 """
90                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
91                 self._flow_graph.handle_mouse_button_release(
92                         left_click=(event.button == 1),
93                         coordinate=(event.x, event.y),
94                 )
95
96         def _handle_mouse_motion(self, widget, event):
97                 """
98                 Forward mouse motion information to the flow graph.
99                 """
100                 self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK
101                 self._flow_graph.handle_mouse_motion(
102                         coordinate=(event.x, event.y),
103                 )
104
105         def _handle_window_realize(self, widget):
106                 """
107                 Called when the window is realized.
108                 Update the flowgraph, which calls new pixmap.
109                 """
110                 self._flow_graph.update()
111
112         def _handle_window_configure(self, widget, event):
113                 """
114                 Called when the window is resized.
115                 Create a new pixmap for background buffer.
116                 """
117                 self._pixmap = self.new_pixmap(*self.get_size_request())
118
119         def _handle_window_expose(self, widget, event):
120                 """
121                 Called when window is exposed, or queue_draw is called.
122                 Double buffering: draw to pixmap, then draw pixmap to window.
123                 """
124                 gc = self.window.new_gc()
125                 self._flow_graph.draw(gc, self._pixmap)
126                 self.window.draw_drawable(gc, self._pixmap, 0, 0, 0, 0, -1, -1)