Merging r11186:11273 from grc branch.
[debian/gnuradio] / grc / grc_gnuradio / wxgui / top_block_gui.py
1 # Copyright 2008 Free Software Foundation, Inc.
2 #
3 # This file is part of GNU Radio
4 #
5 # GNU Radio is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3, or (at your option)
8 # any later version.
9 #
10 # GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street,
18 # Boston, MA 02110-1301, USA.
19 #
20
21 import wx
22 import sys, os
23 from gnuradio import gr
24
25 default_gui_size = (200, 100)
26
27 class top_block_gui(gr.top_block):
28         """gr top block with wx gui app and grid sizer."""
29
30         def __init__(self, title='', size=default_gui_size, icon=None):
31                 """
32                 Initialize the gr top block.
33                 Create the wx gui elements.
34                 @param title the main window title
35                 @param size the main window size tuple in pixels
36                 @param icon the file path to an icon or None
37                 """
38                 #initialize
39                 gr.top_block.__init__(self)
40                 self._size = size
41                 #set the icon
42                 if icon and os.path.isfile(icon): self._icon = icon
43                 else: self._icon = None
44                 #create gui elements
45                 self._wx_app = wx.App()
46                 self._wx_frame = wx.Frame(None , -1, title)
47                 self._wx_grid = wx.GridBagSizer(5, 5)
48                 self._wx_vbox = wx.BoxSizer(wx.VERTICAL)
49
50         def GetWin(self):
51                 """
52                 Get the window for wx elements to fit within.
53                 @return the wx frame
54                 """
55                 return self._wx_frame
56
57         def Add(self, win):
58                 """
59                 Add a window to the wx vbox.
60                 @param win the wx window
61                 """
62                 self._wx_vbox.Add(win, 0, wx.EXPAND)
63
64         def GridAdd(self, win, row, col, row_span=1, col_span=1):
65                 """
66                 Add a window to the wx grid at the given position.
67                 @param win the wx window
68                 @param row the row specification (integer >= 0)
69                 @param col the column specification (integer >= 0)
70                 @param row_span the row span specification (integer >= 1)
71                 @param col_span the column span specification (integer >= 1)
72                 """
73                 self._wx_grid.Add(win, wx.GBPosition(row, col), wx.GBSpan(row_span, col_span), wx.EXPAND)
74
75         def start(self, start=True):
76                 if start:
77                         gr.top_block.start(self)
78                 else:
79                         gr.top_block.stop(self)
80                         gr.top_block.wait(self)
81
82         def Run(self, autostart=True):
83                 """
84                 Setup the wx gui elements.
85                 Start the gr top block.
86                 Block with the wx main loop.
87                 """
88                 #set wx app icon
89                 if self._icon: self._wx_frame.SetIcon(wx.Icon(self._icon, wx.BITMAP_TYPE_ANY))
90                 #set minimal window size
91                 self._wx_frame.SetSizeHints(*self._size)
92                 #create callback for quit
93                 def _quit(event):
94                         gr.top_block.stop(self)
95                         self._wx_frame.Destroy()
96                 #setup app
97                 self._wx_vbox.Add(self._wx_grid, 0, wx.EXPAND)
98                 self._wx_frame.Bind(wx.EVT_CLOSE, _quit)
99                 self._wx_frame.SetSizerAndFit(self._wx_vbox)
100                 self._wx_frame.Show()
101                 self._wx_app.SetTopWindow(self._wx_frame)
102                 #start flow graph
103                 self.start(autostart)
104                 #blocking main loop
105                 self._wx_app.MainLoop()