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

Source Code for Module gui.relax_prompt

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