class MainWindow(ApplicationWindow): """ The main application window. """ ########################################################################### # 'object' interface. ########################################################################### def __init__(self, **traits): """ Creates a new application window. """ # Base class constructor. super(MainWindow, self).__init__(**traits) # Add a menu bar. self.menu_bar_manager = MenuBarManager( MenuManager(Action(name='&Open...', on_perform=self._open_file), Action(name='&Save', on_perform=self._save_file), Action(name='E&xit', on_perform=self.close), name='&File')) return ########################################################################### # Protected 'IApplication' interface. ########################################################################### def _create_contents(self, parent): """ Create the editor. """ self._editor = PythonEditor(parent) return self._editor.control ########################################################################### # Private interface. ########################################################################### def _open_file(self): """ Open a new file. """ if self.control: dlg = FileDialog(parent=self.control, wildcard="*.py") if dlg.open() == OK: self._editor.path = dlg.path def _save_file(self): """ Save the file. """ if self.control: try: self._editor.save() except IOError, e: # If you are trying to save to a file that doesn't exist, # open up a FileDialog with a 'save as' action. dlg = FileDialog(parent=self.control, action='save as', wildcard="*.py") if dlg.open() == OK: self._editor.save(dlg.path)
class MainWindow(ApplicationWindow): """ The main application window. """ ########################################################################### # 'object' interface. ########################################################################### def __init__(self, **traits): """ Creates a new application window. """ # Base class constructor. super(MainWindow, self).__init__(**traits) # Add a menu bar. self.menu_bar_manager = MenuBarManager(MenuManager( Action(name='&Open...', on_perform=self._open_file), Action(name='&Save', on_perform=self._save_file), Action(name='E&xit', on_perform=self.close), name='&File')) return ########################################################################### # Protected 'IApplication' interface. ########################################################################### def _create_contents(self, parent): """ Create the editor. """ self._editor = PythonEditor(parent) return self._editor.control ########################################################################### # Private interface. ########################################################################### def _open_file(self): """ Open a new file. """ if self.control: dlg = FileDialog(parent=self.control, wildcard="*.py") if dlg.open() == OK: self._editor.path = dlg.path def _save_file(self): """ Save the file. """ if self.control: try: self._editor.save() except IOError, e: # If you are trying to save to a file that doesn't exist, # open up a FileDialog with a 'save as' action. dlg = FileDialog(parent=self.control, action='save as', wildcard="*.py") if dlg.open() == OK: self._editor.save(dlg.path)
class PythonEditorPane(TaskPane): id = Str name = Str editor = Instance(PythonEditor) def create(self, parent): self.editor = PythonEditor(parent) self.control = self.editor.control def destroy(self): self.editor.destroy() self.control = self.editor = None
class PythonEditorPane(TaskPane): id = 'example.python_editor_pane' name = 'Python Editor' editor = Instance(PythonEditor) def create(self, parent): self.editor = PythonEditor(parent) self.control = self.editor.control def destroy(self): self.editor.destroy() self.control = self.editor = None
def create_control(self, parent): """ Creates the toolkit-specific control that represents the editor. 'parent' is the toolkit-specific control that is the editor's parent. """ ed = PythonEditor(parent, show_line_numbers=False) # FIXME: Implement toolkit specific Python editor subclass import wx styles = [ wx.stc.STC_STYLE_DEFAULT, wx.stc.STC_STYLE_CONTROLCHAR, wx.stc.STC_STYLE_BRACELIGHT, wx.stc.STC_STYLE_BRACEBAD, wx.stc.STC_P_DEFAULT, wx.stc.STC_P_COMMENTLINE, wx.stc.STC_P_NUMBER, wx.stc.STC_P_STRING, wx.stc.STC_P_CHARACTER, wx.stc.STC_P_WORD, wx.stc.STC_P_TRIPLE, wx.stc.STC_P_TRIPLEDOUBLE, wx.stc.STC_P_CLASSNAME, wx.stc.STC_P_DEFNAME, wx.stc.STC_P_OPERATOR, wx.stc.STC_P_IDENTIFIER, wx.stc.STC_P_COMMENTBLOCK, wx.stc.STC_P_STRINGEOL ] for style in styles: ed.control.StyleSetFaceName(style, "monospace") ed.control.StyleSetSize(style, 10) path = self.obj.path if exists(path): ed.path = path ed.load() return ed.control
class PythonEditorPane(TaskPane): """ A wrapper around the Pyface Python editor. """ #### TaskPane interface ################################################### id = 'example.python_editor_pane' name = 'Python Editor' #### PythonEditorPane interface ########################################### editor = Instance(PythonEditor) ########################################################################### # 'ITaskPane' interface. ########################################################################### def create(self, parent): self.editor = PythonEditor(parent) self.control = self.editor.control def destroy(self): self.editor.destroy() self.control = self.editor = None
class PythonEditorPane(TaskPane): """ A wrapper around the Pyface Python editor. """ # TaskPane interface --------------------------------------------------- id = "example.python_editor_pane" name = "Python Editor" # PythonEditorPane interface ------------------------------------------- editor = Instance(PythonEditor) # ------------------------------------------------------------------------ # 'ITaskPane' interface. # ------------------------------------------------------------------------ def create(self, parent): self.editor = PythonEditor(parent) self.control = self.editor.control def destroy(self): self.editor.destroy() self.control = self.editor = None
def create(self, parent): self.editor = PythonEditor(parent) self.control = self.editor.control
def _create_contents(self, parent): """ Create the editor. """ self._editor = PythonEditor(parent) return self._editor.control
class MainWindow(ApplicationWindow): """ The main application window. """ ########################################################################### # 'object' interface. ########################################################################### def __init__(self, **traits): """ Creates a new application window. """ # Base class constructor. super(MainWindow, self).__init__(**traits) # Add a menu bar. self.menu_bar_manager = MenuBarManager( MenuManager(Group( Action(name='&Open...', accelerator='Ctrl+O', on_perform=self.on_open_file), Action(name='&Save', accelerator='Ctrl+S', on_perform=self.on_save_file), id='document_group', ), Action(name='&Close', accelerator='Ctrl+W', on_perform=self.close), name='&File')) # Add a tool bar if we are using qt4 - wx has layout issues if toolkit_object.toolkit == 'qt4': from pygments.styles import STYLE_MAP styles = list(STYLE_MAP) self.tool_bar_manager = ToolBarManager( Group( Action(name='Open...', on_perform=self.on_open_file), Action(name='Save', on_perform=self.on_save_file), Action(name='Close', on_perform=self.close), id='document_group', ), Group( Action( name="Lines", style='toggle', on_perform=self.on_show_line_numbers, checked=True, ), FieldAction( name='Style', field_type=ComboField, field_defaults={ 'values': styles, 'value': 'default', 'tooltip': 'Style', }, on_perform=self.on_style_changed, ), )) ########################################################################### # Protected 'IApplication' interface. ########################################################################### def _create_contents(self, parent): """ Create the editor. """ self._editor = PythonEditor(parent) return self._editor.control ########################################################################### # Private interface. ########################################################################### def on_open_file(self): """ Open a new file. """ if self.control: dlg = FileDialog(parent=self.control, wildcard='*.py') if dlg.open() == OK: self._editor.path = dlg.path def on_save_file(self): """ Save the file. """ if self.control: try: self._editor.save() except IOError: # If you are trying to save to a file that doesn't exist, # open up a FileDialog with a 'save as' action. dlg = FileDialog(parent=self.control, action='save as', wildcard="*.py") if dlg.open() == OK: self._editor.save(dlg.path) def on_show_line_numbers(self): self._editor.show_line_numbers = not self._editor.show_line_numbers def on_style_changed(self, value): from pygments.styles import get_style_by_name # XXX surface this to a proper API on the editor widget # XXX Qt backend only highlighter = self._editor.control.code.highlighter highlighter._style = get_style_by_name(value) highlighter._brushes = {} highlighter._formats = {} highlighter.rehighlight()
def init(self, parent): """ Finishes initializing the editor by creating the underlying toolkit widget. """ factory = self.factory self._editor = editor = PythonEditor( parent, show_line_numbers=factory.show_line_numbers) self.control = control = editor.control # There are a number of events which aren't well documented that look # to be useful in future implmentations, below are a subset of the # events that look interesting: # EVT_STC_AUTOCOMP_SELECTION # EVT_STC_HOTSPOT_CLICK # EVT_STC_HOTSPOT_DCLICK # EVT_STC_DOUBLECLICK # EVT_STC_MARGINCLICK control.SetSize(wx.Size(300, 124)) # Clear out the goofy hotkeys for zooming text control.CmdKeyClear(ord('B'), stc.STC_SCMOD_CTRL) control.CmdKeyClear(ord('N'), stc.STC_SCMOD_CTRL) # Set up the events wx.EVT_KILL_FOCUS(control, self.wx_update_object) stc.EVT_STC_CALLTIP_CLICK(control, control.GetId(), self._calltip_clicked) if factory.auto_scroll and (factory.selected_line != ''): wx.EVT_SIZE(control, self._update_selected_line) if factory.auto_set: editor.on_trait_change(self.update_object, 'changed', dispatch='ui') if factory.key_bindings is not None: editor.on_trait_change(self.key_pressed, 'key_pressed', dispatch='ui') if self.readonly: control.SetReadOnly(True) # Set up the lexer control.SetLexer(stc.STC_LEX_CONTAINER) control.Bind(stc.EVT_STC_STYLENEEDED, self._style_needed) try: self.lexer = getattr(stc, 'STC_LEX_' + self.factory.lexer.upper()) except AttributeError: self.lexer = stc.STC_LEX_NULL # Define the markers we use: control.MarkerDefine(MARK_MARKER, stc.STC_MARK_BACKGROUND, background=factory.mark_color_) control.MarkerDefine(SEARCH_MARKER, stc.STC_MARK_BACKGROUND, background=factory.search_color_) control.MarkerDefine(SELECTED_MARKER, stc.STC_MARK_BACKGROUND, background=factory.selected_color_) # Make sure the editor has been initialized: self.update_editor() # Set up any event listeners: self.sync_value(factory.mark_lines, 'mark_lines', 'from', is_list=True) self.sync_value(factory.selected_line, 'selected_line', 'from') self.sync_value(factory.selected_text, 'selected_text', 'to') self.sync_value(factory.line, 'line') self.sync_value(factory.column, 'column') self.sync_value(factory.calltip_clicked, 'calltip_clicked') self.sync_value(factory.dim_lines, 'dim_lines', 'from', is_list=True) if self.factory.dim_color == '': self.dim_color = 'dark grey' else: self.sync_value(factory.dim_color, 'dim_color', 'from') self.sync_value(factory.squiggle_lines, 'squiggle_lines', 'from', is_list=True) if factory.squiggle_color == '': self.squiggle_color = 'red' else: self.sync_value(factory.squiggle_color, 'squiggle_color', 'from') # Check if we need to monitor the line or column position being # changed: if (factory.line != '') or (factory.column != '') or \ (factory.selected_text != ''): stc.EVT_STC_UPDATEUI(control, control.GetId(), self._position_changed) self.set_tooltip()
def init(self, parent): """ Finishes initializing the editor by creating the underlying toolkit widget. """ factory = self.factory self._editor = editor = PythonEditor( parent, show_line_numbers=factory.show_line_numbers) self.control = control = editor.control # There are a number of events which aren't well documented that look # to be useful in future implmentations, below are a subset of the # events that look interesting: # EVT_STC_AUTOCOMP_SELECTION # EVT_STC_HOTSPOT_CLICK # EVT_STC_HOTSPOT_DCLICK # EVT_STC_DOUBLECLICK # EVT_STC_MARGINCLICK control.SetSize(wx.Size(300, 124)) # Clear out the goofy hotkeys for zooming text control.CmdKeyClear(ord("B"), stc.STC_SCMOD_CTRL) control.CmdKeyClear(ord("N"), stc.STC_SCMOD_CTRL) # Set up the events control.Bind(wx.EVT_KILL_FOCUS, self.wx_update_object) control.Bind(stc.EVT_STC_CALLTIP_CLICK, self._calltip_clicked) if factory.auto_scroll and (factory.selected_line != ""): control.Bind(wx.EVT_SIZE, self._update_selected_line) if factory.auto_set: editor.on_trait_change(self.update_object, "changed", dispatch="ui") if factory.key_bindings is not None: editor.on_trait_change(self.key_pressed, "key_pressed", dispatch="ui") if self.readonly: control.SetReadOnly(True) # Set up the lexer control.SetLexer(stc.STC_LEX_CONTAINER) control.Bind(stc.EVT_STC_STYLENEEDED, self._style_needed) try: self.lexer = getattr(stc, "STC_LEX_" + self.factory.lexer.upper()) except AttributeError: self.lexer = stc.STC_LEX_NULL # Define the markers we use: control.MarkerDefine( MARK_MARKER, stc.STC_MARK_BACKGROUND, background=wx.Colour(factory.mark_color_), ) control.MarkerDefine( SEARCH_MARKER, stc.STC_MARK_BACKGROUND, background=wx.Colour(factory.search_color_), ) control.MarkerDefine( SELECTED_MARKER, stc.STC_MARK_BACKGROUND, background=wx.Colour(factory.selected_color_), ) # Make sure the editor has been initialized: self.update_editor() # Set up any event listeners: self.sync_value(factory.mark_lines, "mark_lines", "from", is_list=True) self.sync_value(factory.selected_line, "selected_line", "from") self.sync_value(factory.selected_text, "selected_text", "to") self.sync_value(factory.line, "line") self.sync_value(factory.column, "column") self.sync_value(factory.calltip_clicked, "calltip_clicked") self.sync_value(factory.dim_lines, "dim_lines", "from", is_list=True) if self.factory.dim_color == "": self.dim_color = "dark grey" else: self.sync_value(factory.dim_color, "dim_color", "from") self.sync_value(factory.squiggle_lines, "squiggle_lines", "from", is_list=True) if factory.squiggle_color == "": self.squiggle_color = "red" else: self.sync_value(factory.squiggle_color, "squiggle_color", "from") # Check if we need to monitor the line or column position being # changed: if ((factory.line != "") or (factory.column != "") or (factory.selected_text != "")): control.Bind(stc.EVT_STC_UPDATEUI, self._position_changed) self.set_tooltip()
def _create_contents(self, parent): """ Create the editor. """ self._editor = PythonEditor(parent) return self._editor.control