Package gui :: Module relax_prompt
[hide private]
[frames] | no frames]

Source Code for Module gui.relax_prompt

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2010-2011 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """The relax prompt GUI element.""" 
 25   
 26  # Python module imports. 
 27  import sys 
 28  import wx 
 29  import wx.py 
 30  import wx.stc as stc 
 31   
 32  # relax module imports 
 33  from info import Info_box 
 34  from prompt import interpreter 
 35  from status import Status; status = Status() 
 36   
 37  # relax GUI module imports 
 38  from gui.icons import relax_icons 
 39   
 40   
41 -class Prompt(wx.Frame):
42 """The relax prompt window object.""" 43
44 - def __init__(self, *args, **kwds):
45 """Set up the relax prompt.""" 46 47 # Store the parent object. 48 self.gui = kwds.pop('parent') 49 50 # Create GUI elements 51 kwds["style"] = wx.DEFAULT_FRAME_STYLE 52 wx.Frame.__init__(self, *args, **kwds) 53 54 # Set up the window icon. 55 self.SetIcons(relax_icons) 56 57 # Some default values. 58 self.size_x = 1000 59 self.size_y = 500 60 self.border = 0 61 62 # Set up the frame. 63 sizer = self.setup_frame() 64 65 # The shell. 66 self.add_shell(sizer) 67 68 # Register functions with the observer objects. 69 status.observers.exec_lock.register('GUI prompt', self.enable)
70 71
72 - def add_shell(self, sizer):
73 """Add the relax prompt to the sizer. 74 75 @param sizer: The sizer element to pack the relax prompt into. 76 @type sizer: wx.Sizer instance 77 """ 78 79 # The shell. 80 self.prompt = wx.py.shell.Shell(self, InterpClass=InterpClass) 81 82 # Colours. 83 self.prompt.StyleSetBackground(style=stc.STC_STYLE_DEFAULT, back='white') 84 self.prompt.SetCaretForeground(fore="black") 85 self.prompt.StyleClearAll() 86 self.prompt.StyleSetSpec(stc.STC_STYLE_DEFAULT, "fore:black") 87 self.prompt.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#0000ff") 88 self.prompt.StyleSetSpec(stc.STC_P_NUMBER, "fore:#125a0a") 89 self.prompt.StyleSetSpec(stc.STC_P_STRING, "fore:#ff00ff") 90 self.prompt.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#ff00ff") 91 self.prompt.StyleSetSpec(stc.STC_P_WORD, "fore:#a52a2a") 92 self.prompt.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#008b8b") 93 self.prompt.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#008b8b") 94 95 # Set the focus. 96 self.prompt.setFocus() 97 98 # Override the exiting commands. 99 for name in ['exit', 'bye', 'quit', 'q']: 100 self.prompt.interp.locals[name] = _Exit(fn=self.gui.exit_gui) 101 102 # Add the shell to the sizer. 103 sizer.Add(self.prompt, 1, wx.EXPAND|wx.ALL, self.border)
104 105
106 - def enable(self):
107 """Enable and disable the prompt with the execution lock.""" 108 109 # Flag for enabling or disabling the prompt. 110 enable = False 111 if not status.exec_lock.locked(): 112 enable = True 113 114 # Enable/disable. 115 wx.CallAfter(self.prompt.Enable, enable)
116 117
118 - def handler_close(self, event):
119 """Event handler for the close window action. 120 121 @param event: The wx event. 122 @type event: wx event 123 """ 124 125 # Close the window. 126 self.Hide()
127 128
129 - def setup_frame(self):
130 """Set up the relax controller frame. 131 132 @return: The sizer object. 133 @rtype: wx.Sizer instance 134 """ 135 136 # Set the frame title. 137 self.SetTitle("The relax prompt") 138 139 # Use a box sizer for packing the shell. 140 sizer = wx.BoxSizer(wx.VERTICAL) 141 self.SetSizer(sizer) 142 143 # Close the window cleanly (hide so it can be reopened). 144 self.Bind(wx.EVT_CLOSE, self.handler_close) 145 146 # Set the default size of the controller. 147 self.SetSize((self.size_x, self.size_y)) 148 149 # Return the sizer. 150 return sizer
151 152
153 -class _Exit:
154 - def __init__(self, fn=None):
155 """Store the exiting function. 156 157 @keyword fn: The exiting function. 158 @type fn: func 159 """ 160 161 # Store. 162 self.fn = fn
163 164
165 - def __repr__(self):
166 """Exit the program.""" 167 168 # Execute the exiting function. 169 self.fn() 170 171 # Return nothing. 172 return ''
173 174
175 -class InterpClass(wx.py.interpreter.Interpreter):
176 - def __init__(self, locals=None, rawin=None, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, showInterpIntro=True):
177 """Redefine the interpreter.""" 178 179 # Execute the base class __init__() method. 180 wx.py.interpreter.Interpreter.__init__(self, locals=locals, rawin=rawin, stdin=stdin, stdout=stdout, stderr=stderr, showInterpIntro=showInterpIntro) 181 182 # The introductory text. 183 info = Info_box() 184 self.introText = info.intro_text() 185 186 # The relax interpreter. 187 interp = interpreter.Interpreter(show_script=False, quit=False, raise_relax_error=True) 188 189 # The locals. 190 self.locals = interp._locals
191