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

Source Code for Module gui.relax_prompt

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