Imported Upstream version 3.2.2
[debian/gnuradio] / gr-wxgui / src / python / gui.py
1 #
2 # Copyright 2009 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 import wx
23 from gnuradio import gr
24
25 #
26 # Top-level display panel with vertical box sizer.  User does not create or
27 # subclass this class; rather, the user supplies his own class constructor
28 # that gets invoked with needed parameters.
29 #
30 class top_panel(wx.Panel):
31     def __init__(self, frame, top_block, gui, options, args):
32         wx.Panel.__init__(self, frame, -1)
33         vbox = wx.BoxSizer(wx.VERTICAL)
34
35         # Create the user's GUI class
36         if gui is not None:
37             self.gui = gui(frame,          # Top-level window frame
38                            self,           # Parent class for user created windows
39                            vbox,           # Sizer for user to add windows to
40                            top_block,      # GUI-unaware flowgraph to manipulate
41                            options,        # Command-line options
42                            args)           # Command-line arguments
43
44         else:
45             # User hasn't made their own GUI, create our default
46             # We don't have a default GUI yet either :)
47             p = wx.Panel(self)
48             p.SetSize((640,480))
49             vbox.Add(p, 1, wx.EXPAND)
50             
51         self.SetSizer(vbox)
52         self.SetAutoLayout(True)
53         vbox.Fit(self)
54
55     def shutdown(self):
56         try:
57             self.gui.shutdown()
58         except AttributeError:
59             pass
60
61 #
62 # Top-level window frame with menu and status bars.
63 #
64 class top_frame(wx.Frame):
65     def __init__ (self, top_block, gui, options, args, 
66                   title, nstatus, start, realtime):
67
68         wx.Frame.__init__(self, None, -1, title)
69         self.top_block = top_block
70
71         self.CreateStatusBar(nstatus)
72         mainmenu = wx.MenuBar()
73         self.SetMenuBar(mainmenu)
74
75         menu = wx.Menu()
76
77         item = menu.Append(200, 'E&xit', 'Exit Application') # FIXME magic ID
78         self.Bind(wx.EVT_MENU, self.OnCloseWindow, item)
79         mainmenu.Append(menu, "&File")
80         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
81
82         # Create main panel, creates user GUI class with supplied parameters
83         self.panel = top_panel(self, top_block, gui, options, args)
84
85         vbox = wx.BoxSizer(wx.VERTICAL)
86         vbox.Add(self.panel, 1, wx.EXPAND)
87         self.SetSizer(vbox)
88         self.SetAutoLayout(True)
89         vbox.Fit(self)
90
91         if realtime:
92             if gr.enable_realtime_scheduling() != gr.RT_OK:
93                 self.SetStatusText("Failed to enable realtime scheduling")
94
95         if start and self.top_block is not None:
96             self.top_block.start()
97
98     def OnCloseWindow(self, event):
99         # Give user API a chance to do something
100         self.panel.shutdown()
101
102         # Stop flowgraph as a convenience
103         self.SetStatusText("Ensuring flowgraph has completed before exiting...")
104         if self.top_block is not None:
105             self.top_block.stop()
106             self.top_block.wait()
107
108         self.Destroy()
109
110
111 #
112 # Top-level wxPython application object.  User creates or subclasses this 
113 # in their GUI script.
114 #
115 class app(wx.App):
116     def __init__ (self, top_block=None, gui=None, options=None, args=None, 
117                   title="GNU Radio", nstatus=1, start=False, realtime=False):
118         self.top_block = top_block
119         self.gui = gui
120         self.options = options
121         self.args = args
122         self.title = title
123         self.nstatus = nstatus
124         self.start = start
125         self.realtime = realtime
126
127         wx.App.__init__ (self, redirect=False)
128
129     def OnInit(self):
130         # Pass user parameters to top window frame
131         frame = top_frame(self.top_block, self.gui, self.options, self.args,
132                           self.title, self.nstatus, self.start, self.realtime)
133         frame.Show(True)
134         self.SetTopWindow(frame)
135         return True