added include <cstdio> statements in several files to make it compatible with g+...
[debian/gnuradio] / grc / src / gui / NotebookPage.py
1 """
2 Copyright 2008 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 from Actions import FLOW_GRAPH_CLOSE
21 import pygtk
22 pygtk.require('2.0')
23 import gtk
24 from .. utils import ParseXML
25 from StateCache import StateCache
26 from .. platforms.base.Constants import FLOW_GRAPH_DTD
27 from Constants import MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT
28 from DrawingArea import DrawingArea
29 import os
30
31 ############################################################
32 ## Notebook Page
33 ############################################################
34
35 class NotebookPage(gtk.HBox):
36         """A page in the notebook."""
37
38         def __init__(self, main_window, flow_graph, file_path=''):
39                 """
40                 Page constructor.
41                 @param main_window main window
42                 @param file_path path to a flow graph file
43                 """
44                 self._flow_graph = flow_graph
45                 self.set_pid(None)
46                 #import the file
47                 self.main_window = main_window
48                 self.set_file_path(file_path)
49                 file_path = file_path or flow_graph.get_parent().get_default_flow_graph()
50                 open(file_path, 'r') #test open
51                 ############################################################
52                 from .. utils import converter
53                 converter.convert(file_path, flow_graph.get_parent())
54                 ############################################################
55                 ParseXML.validate_dtd(file_path, FLOW_GRAPH_DTD)
56                 initial_state = ParseXML.from_file(file_path)
57                 self.state_cache = StateCache(initial_state)
58                 self.set_saved(True)
59                 #import the data to the flow graph
60                 self.get_flow_graph().import_data(initial_state)
61                 #initialize page gui
62                 gtk.HBox.__init__(self, False, 0)
63                 self.show()
64                 #tab box to hold label and close button
65                 self.tab = gtk.HBox(False, 0)
66                 #setup tab label
67                 self.label = gtk.Label()
68                 self.tab.pack_start(self.label, False)
69                 #setup button image
70                 image = gtk.Image()
71                 image.set_from_stock('gtk-close', gtk.ICON_SIZE_MENU)
72                 #setup image box
73                 image_box = gtk.HBox(False, 0)
74                 image_box.pack_start(image, True, False, 0)
75                 #setup the button
76                 button = gtk.Button()
77                 button.connect("clicked", self._handle_button)
78                 button.set_relief(gtk.RELIEF_NONE)
79                 button.add(image_box)
80                 #button size
81                 w, h = gtk.icon_size_lookup_for_settings(button.get_settings(), gtk.ICON_SIZE_MENU)
82                 button.set_size_request(w+6, h+6)
83                 self.tab.pack_start(button, False)
84                 self.tab.show_all()
85                 #setup scroll window and drawing area
86                 self.scrolled_window = gtk.ScrolledWindow()
87                 self.scrolled_window.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT)
88                 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
89                 self.drawing_area = DrawingArea(self.get_flow_graph())
90                 self.scrolled_window.add_with_viewport(self.get_drawing_area())
91                 self.pack_start(self.scrolled_window)
92                 #inject drawing area and handle states into flow graph
93                 self.get_flow_graph().drawing_area = self.get_drawing_area()
94                 self.get_flow_graph().handle_states = main_window.handle_states
95                 self.show_all()
96
97         def get_drawing_area(self): return self.drawing_area
98
99         def get_generator(self):
100                 """
101                 Get the generator object for this flow graph.
102                 @return generator
103                 """
104                 return self.get_flow_graph().get_parent().get_generator()(
105                         self.get_flow_graph(),
106                         self.get_file_path(),
107                 )
108
109         def _handle_button(self, button):
110                 """
111                 The button was clicked.
112                 Make the current page selected, then close.
113                 @param the button
114                 """
115                 self.main_window.page_to_be_closed = self
116                 self.main_window.handle_states(FLOW_GRAPH_CLOSE)
117
118         def set_text(self, text):
119                 """
120                 Set the text in this label.
121                 @param text the new text
122                 """
123                 self.label.set_text(text)
124
125         def get_tab(self):
126                 """
127                 Get the gtk widget for this page's tab.
128                 @return gtk widget
129                 """
130                 return self.tab
131
132         def get_pid(self):
133                 """
134                 Get the pid for the flow graph.
135                 @return the pid number
136                 """
137                 return self.pid
138
139         def set_pid(self, pid):
140                 """
141                 Set the pid number.
142                 @param pid the new pid number
143                 """
144                 self.pid = pid
145
146         def get_flow_graph(self):
147                 """
148                 Get the flow graph.
149                 @return the flow graph
150                 """
151                 return self._flow_graph
152
153         def get_read_only(self):
154                 """
155                 Get the read-only state of the file.
156                 Always false for empty path.
157                 @return true for read-only
158                 """
159                 if not self.get_file_path(): return False
160                 return os.path.exists(self.get_file_path()) and \
161                 not os.access(self.get_file_path(), os.W_OK)
162
163         def get_file_path(self):
164                 """
165                 Get the file path for the flow graph.
166                 @return the file path or ''
167                 """
168                 return self.file_path
169
170         def set_file_path(self, file_path=''):
171                 """
172                 Set the file path, '' for no file path.
173                 @param file_path file path string
174                 """
175                 if file_path: self.file_path = os.path.abspath(file_path)
176                 else: self.file_path = ''
177
178         def get_saved(self):
179                 """
180                 Get the saved status for the flow graph.
181                 @return true if saved
182                 """
183                 return self.saved
184
185         def set_saved(self, saved=True):
186                 """
187                 Set the saved status.
188                 @param saved boolean status
189                 """
190                 self.saved = saved
191
192         def get_state_cache(self):
193                 """
194                 Get the state cache for the flow graph.
195                 @return the state cache
196                 """
197                 return self.state_cache