f3305d7c5505f8c20be5a5a61d4994557322330e
[debian/gnuradio] / grc / src / 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 from gnuradio.gr.pubsub import pubsub
25
26 default_gui_size = (200, 100)
27
28 class top_block_gui(gr.top_block, pubsub):
29         """gr top block with wx gui app and grid sizer."""
30
31         def __init__(self, title='', size=default_gui_size, icon=None):
32                 """
33                 Initialize the gr top block.
34                 Create the wx gui elements.
35                 @param title the main window title
36                 @param size the main window size tuple in pixels
37                 @param icon the file path to an icon or None
38                 """
39                 #initialize
40                 gr.top_block.__init__(self)
41                 pubsub.__init__(self)
42                 self._size = size
43                 #set the icon
44                 if icon and os.path.isfile(icon): self._icon = icon
45                 else: self._icon = None
46                 #create gui elements
47                 self._wx_app = wx.App()
48                 self._wx_frame = wx.Frame(None , -1, title)
49                 self._wx_grid = wx.GridBagSizer(5, 5)
50                 self._wx_vbox = wx.BoxSizer(wx.VERTICAL)
51
52         def GetWin(self):
53                 """
54                 Get the window for wx elements to fit within.
55                 @return the wx frame
56                 """
57                 return self._wx_frame
58
59         def Add(self, win):
60                 """
61                 Add a window to the wx vbox.
62                 @param win the wx window
63                 """
64                 self._wx_vbox.Add(win, 0, wx.EXPAND)
65
66         def GridAdd(self, win, row, col, row_span=1, col_span=1):
67                 """
68                 Add a window to the wx grid at the given position.
69                 @param win the wx window
70                 @param row the row specification (integer >= 0)
71                 @param col the column specification (integer >= 0)
72                 @param row_span the row span specification (integer >= 1)
73                 @param col_span the column span specification (integer >= 1)
74                 """
75                 self._wx_grid.Add(win, wx.GBPosition(row, col), wx.GBSpan(row_span, col_span), wx.EXPAND)
76
77         def Run(self):
78                 """
79                 Setup the wx gui elements.
80                 Start the gr top block.
81                 Block with the wx main loop.
82                 """
83                 #set wx app icon
84                 if self._icon: self._wx_frame.SetIcon(wx.Icon(self._icon, wx.BITMAP_TYPE_ANY))
85                 #set minimal window size
86                 self._wx_frame.SetSizeHints(*self._size)
87                 #create callback for quit
88                 def _quit(event):
89                         gr.top_block.stop(self)
90                         self._wx_frame.Destroy()
91                 #setup app
92                 self._wx_vbox.Add(self._wx_grid, 0, wx.EXPAND)
93                 self._wx_frame.Bind(wx.EVT_CLOSE, _quit)
94                 self._wx_frame.SetSizerAndFit(self._wx_vbox)
95                 self._wx_frame.Show()
96                 self._wx_app.SetTopWindow(self._wx_frame)
97                 #start flow graph
98                 gr.top_block.start(self)
99                 #blocking main loop
100                 self._wx_app.MainLoop()