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

Source Code for Module gui.relax_prompt

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2010-2014 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 add_shell(self, sizer):
70 """Add the relax prompt to the sizer. 71 72 @param sizer: The sizer element to pack the relax prompt into. 73 @type sizer: wx.Sizer instance 74 """ 75 76 # The shell. 77 self.prompt = wx.py.shell.Shell(self, InterpClass=InterpClass) 78 79 # Colours. 80 self.prompt.StyleSetBackground(style=stc.STC_STYLE_DEFAULT, back='white') 81 self.prompt.SetCaretForeground(fore="black") 82 self.prompt.StyleClearAll() 83 self.prompt.StyleSetSpec(stc.STC_STYLE_DEFAULT, "fore:black") 84 self.prompt.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#0000ff") 85 self.prompt.StyleSetSpec(stc.STC_P_NUMBER, "fore:#125a0a") 86 self.prompt.StyleSetSpec(stc.STC_P_STRING, "fore:#ff00ff") 87 self.prompt.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#ff00ff") 88 self.prompt.StyleSetSpec(stc.STC_P_WORD, "fore:#a52a2a") 89 self.prompt.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#008b8b") 90 self.prompt.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#008b8b") 91 92 # Set the focus. 93 self.prompt.setFocus() 94 95 # Override the exiting commands. 96 for name in ['exit', 'bye', 'quit', 'q']: 97 self.prompt.interp.locals[name] = _Exit(fn=self.gui.exit_gui) 98 99 # Add the shell to the sizer. 100 sizer.Add(self.prompt, 1, wx.EXPAND|wx.ALL, self.border)
101 102
103 - def enable(self):
104 """Enable and disable the prompt with the execution lock.""" 105 106 # Flag for enabling or disabling the prompt. 107 enable = False 108 if not status.exec_lock.locked(): 109 enable = True 110 111 # Enable/disable. 112 wx.CallAfter(self.prompt.Enable, enable)
113 114
115 - def handler_close(self, event):
116 """Event handler for the close window action. 117 118 @param event: The wx event. 119 @type event: wx event 120 """ 121 122 # Close the window. 123 self.Hide()
124 125
126 - def setup_frame(self):
127 """Set up the relax controller frame. 128 129 @return: The sizer object. 130 @rtype: wx.Sizer instance 131 """ 132 133 # Set the frame title. 134 self.SetTitle("The relax prompt") 135 136 # Use a box sizer for packing the shell. 137 sizer = wx.BoxSizer(wx.VERTICAL) 138 self.SetSizer(sizer) 139 140 # Close the window cleanly (hide so it can be reopened). 141 self.Bind(wx.EVT_CLOSE, self.handler_close) 142 143 # Set the default size of the controller. 144 self.SetSize((self.size_x, self.size_y)) 145 146 # Return the sizer. 147 return sizer
148 149
150 -class _Exit:
151 - def __init__(self, fn=None):
152 """Store the exiting function. 153 154 @keyword fn: The exiting function. 155 @type fn: func 156 """ 157 158 # Store. 159 self.fn = fn
160 161
162 - def __repr__(self):
163 """Exit the program.""" 164 165 # Execute the exiting function. 166 self.fn() 167 168 # Return nothing. 169 return ''
170 171
172 -class InterpClass(wx.py.interpreter.Interpreter):
173 - def __init__(self, locals=None, rawin=None, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, showInterpIntro=True):
174 """Redefine the interpreter.""" 175 176 # Execute the base class __init__() method. 177 wx.py.interpreter.Interpreter.__init__(self, locals=locals, rawin=rawin, stdin=stdin, stdout=stdout, stderr=stderr, showInterpIntro=showInterpIntro) 178 179 # The introductory text. 180 info = Info_box() 181 self.introText = info.intro_text() 182 183 # The relax interpreter. 184 interp = interpreter.Interpreter(show_script=False, raise_relax_error=True) 185 186 # The locals. 187 self.locals = interp._locals
188