]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/samples/ide/activegrid/tool/PythonEditor.py
Add code to remove the selection (if any) in wxTextCtrl::WriteText for multi-line...
[wxWidgets.git] / wxPython / samples / ide / activegrid / tool / PythonEditor.py
index 876f42b6bf1fad05fdfb1629f0ab1a47c0e0196a..ea5f6b71d1ccb834073f229223c47746964aeb65 100644 (file)
@@ -25,6 +25,7 @@ import keyword # for GetAutoCompleteKeywordList
 import sys # for GetAutoCompleteKeywordList
 import MessageService # for OnCheckCode
 import OutlineService
+from UICommon import CaseInsensitiveCompare
 try:
     import checker # for pychecker
     _CHECKER_INSTALLED = True
@@ -49,6 +50,11 @@ class PythonDocument(CodeEditor.CodeDocument):
 class PythonView(CodeEditor.CodeView):
 
 
+    def GetCtrlClass(self):
+        """ Used in split window to instantiate new instances """
+        return PythonCtrl
+
+
     def ProcessUpdateUIEvent(self, event):
         if not self.GetCtrl():
             return False
@@ -62,15 +68,13 @@ class PythonView(CodeEditor.CodeView):
         return CodeEditor.CodeView.ProcessUpdateUIEvent(self, event)
 
 
-    def GetCtrlClass(self):
-        """ Used in split window to instantiate new instances """
-        return PythonCtrl
-
-
     def OnActivateView(self, activate, activeView, deactiveView):
         STCTextEditor.TextView.OnActivateView(self, activate, activeView, deactiveView)
-        if activate:
-            wx.CallAfter(self.LoadOutline)  # need CallAfter because document isn't loaded yet
+        if activate and self.GetCtrl():
+            if self.GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI:
+                self.LoadOutline()
+            else:
+                wx.CallAfter(self.LoadOutline)  # need CallAfter because document isn't loaded yet
         
 
     def OnClose(self, deleteWindow = True):
@@ -99,7 +103,7 @@ class PythonView(CodeEditor.CodeView):
             filterkw = filter(lambda item: item.lower().startswith(lowerHint), kw)  # remove variables and methods that don't match hint
             kw = filterkw
 
-        kw.sort(self.CaseInsensitiveCompare)
+        kw.sort(CaseInsensitiveCompare)
 
         if hint:
             replaceLen = len(hint)
@@ -119,6 +123,7 @@ class PythonView(CodeEditor.CodeView):
         # pychecker only works on files, doesn't take a stream or string input
         if self.GetDocument().IsModified():
             dlg = wx.MessageDialog(self.GetFrame(), _("'%s' has been modfied and must be saved first.  Save file and check code?") % filename, _("Check Code"))
+            dlg.CenterOnParent()
             val = dlg.ShowModal()
             dlg.Destroy()
             if val == wx.ID_OK:
@@ -167,7 +172,7 @@ class PythonView(CodeEditor.CodeView):
                 break
 
         if not foundView:
-            doc = wx.GetApp().GetDocumentManager().CreateDocument(filename, wx.lib.docview.DOC_SILENT)
+            doc = wx.GetApp().GetDocumentManager().CreateDocument(filename, wx.lib.docview.DOC_SILENT|wx.lib.docview.DOC_OPEN_ONCE)
             foundView = doc.GetFirstView()
 
         if foundView:
@@ -269,11 +274,29 @@ class PythonInterpreterView(wx.lib.docview.View):
         return True
 
 
+class PythonInterpreterDocument(wx.lib.docview.Document):
+    """ Generate Unique Doc Type """
+    pass
+    
+
 class PythonService(CodeEditor.CodeService):
 
 
     def __init__(self):
         CodeEditor.CodeService.__init__(self)
+        docManager = wx.GetApp().GetDocumentManager()
+        pythonInterpreterTemplate = wx.lib.docview.DocTemplate(docManager,
+                                          _("Python Interpreter"),
+                                          "*.Foobar",
+                                          "Foobar",
+                                          ".Foobar",
+                                          _("Python Interpreter Document"),
+                                          _("Python Interpreter View"),
+                                          PythonInterpreterDocument,
+                                          PythonInterpreterView,
+                                          flags = wx.lib.docview.TEMPLATE_INVISIBLE,
+                                          icon = getPythonIcon())
+        docManager.AssociateTemplate(pythonInterpreterTemplate)
 
 
     def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
@@ -308,7 +331,7 @@ class PythonService(CodeEditor.CodeService):
             docManager = wx.GetApp().GetDocumentManager()
             event.Check(False)
             for doc in docManager.GetDocuments():
-                if isinstance(doc.GetFirstView(), PythonInterpreterView):
+                if isinstance(doc, PythonInterpreterDocument):
                     event.Check(True)
                     break
             return True
@@ -318,35 +341,27 @@ class PythonService(CodeEditor.CodeService):
 
     def OnViewPythonInterpreter(self, event):
         for doc in wx.GetApp().GetDocumentManager().GetDocuments():
-            if isinstance(doc.GetFirstView(), PythonInterpreterView):
-                doc.GetFirstView().GetDocument().DeleteAllViews()
+            if isinstance(doc, PythonInterpreterDocument):
+                doc.DeleteAllViews()
                 return
                 
-        docManager = self.GetDocumentManager()
-        template = wx.lib.docview.DocTemplate(docManager,
-                                          _("Python Interpreter"),
-                                          "*.Foobar",
-                                          "Foobar",
-                                          ".Foobar",
-                                          _("Python Interpreter Document"),
-                                          _("Python Interpreter View"),
-                                          wx.lib.docview.Document,
-                                          PythonInterpreterView,
-                                          flags = wx.lib.docview.TEMPLATE_INVISIBLE)
-        newDoc = template.CreateDocument('', wx.lib.docview.DOC_SILENT)
-        if newDoc:
-            newDoc.SetDocumentName(template.GetDocumentName())
-            newDoc.SetDocumentTemplate(template)
-            newDoc.OnNewDocument()
-            newDoc.SetWriteable(False)
-            newDoc.GetFirstView().GetFrame().SetTitle(_("Python Interpreter"))
+        for template in self.GetDocumentManager().GetTemplates():
+            if template.GetDocumentType() == PythonInterpreterDocument:
+                newDoc = template.CreateDocument('', wx.lib.docview.DOC_SILENT|wx.lib.docview.DOC_OPEN_ONCE)
+                if newDoc:
+                    newDoc.SetDocumentName(template.GetDocumentName())
+                    newDoc.SetDocumentTemplate(template)
+                    newDoc.OnNewDocument()
+                    newDoc.SetWriteable(False)
+                    newDoc.GetFirstView().GetFrame().SetTitle(_("Python Interpreter"))
+                break
 
 
 class PythonCtrl(CodeEditor.CodeCtrl):
 
 
-    def __init__(self, parent, ID = -1, style = wx.NO_FULL_REPAINT_ON_RESIZE):
-        CodeEditor.CodeCtrl.__init__(self, parent, ID, style)
+    def __init__(self, parent, id=-1, style=wx.NO_FULL_REPAINT_ON_RESIZE):
+        CodeEditor.CodeCtrl.__init__(self, parent, id, style)
         self.SetProperty("tab.timmy.whinge.level", "1")
         self.SetProperty("fold.comment.python", "1")
         self.SetProperty("fold.quotes.python", "1")
@@ -355,7 +370,7 @@ class PythonCtrl(CodeEditor.CodeCtrl):
 
 
     def SetViewDefaults(self):
-        CodeEditor.CodeCtrl.SetViewDefaults(self, configPrefix = "Python", hasWordWrap = False, hasTabs = True)
+        CodeEditor.CodeCtrl.SetViewDefaults(self, configPrefix = "Python", hasWordWrap = True, hasTabs = True)
 
 
     def GetFontAndColorFromConfig(self):
@@ -516,6 +531,7 @@ class PythonCtrl(CodeEditor.CodeCtrl):
                 if doExtraIndent or len(textNoTrailingSpaces) and textNoTrailingSpaces[-1] == ':':
                     spaces = spaces + ' ' * self.GetIndent()
             self.AddText('\n' + spaces)
+        self.EnsureCaretVisible()
 
 
     # Callback for tokenizer in self.DoIndent
@@ -543,40 +559,56 @@ class PythonOptionsPanel(wx.Panel):
         choosePathButton = wx.Button(self, -1, _("Browse..."))
         pathSizer = wx.BoxSizer(wx.HORIZONTAL)
         HALF_SPACE = 5
-        pathSizer.Add(pathLabel, 0, wx.ALIGN_LEFT | wx.LEFT | wx.RIGHT | wx.TOP, HALF_SPACE)
-        pathSizer.Add(self._pathTextCtrl, 0, wx.ALIGN_LEFT | wx.EXPAND | wx.RIGHT, HALF_SPACE)
-        pathSizer.Add(choosePathButton, 0, wx.ALIGN_RIGHT | wx.LEFT, HALF_SPACE)
+        SPACE = 10
+        pathSizer.Add(pathLabel, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.TOP, HALF_SPACE)
+        pathSizer.Add(self._pathTextCtrl, 1, wx.EXPAND|wx.LEFT|wx.TOP, HALF_SPACE)
+        pathSizer.Add(choosePathButton, 0, wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT|wx.TOP, HALF_SPACE)
         wx.EVT_BUTTON(self, choosePathButton.GetId(), self.OnChoosePath)
         mainSizer = wx.BoxSizer(wx.VERTICAL)                
-        mainSizer.Add(pathSizer, 0, wx.LEFT | wx.RIGHT | wx.TOP, 10)
+        mainSizer.Add(pathSizer, 0, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, SPACE)
 
-        self._otherOptions = STCTextEditor.TextOptionsPanel(self, -1, configPrefix = "Python", label = "Python", hasWordWrap = False, hasTabs = True, addPage=False)
-        mainSizer.Add(self._otherOptions)
+        self._otherOptions = STCTextEditor.TextOptionsPanel(self, -1, configPrefix = "Python", label = "Python", hasWordWrap = True, hasTabs = True, addPage=False)
+        mainSizer.Add(self._otherOptions, 0, wx.EXPAND|wx.BOTTOM, SPACE)
         self.SetSizer(mainSizer)
         parent.AddPage(self, _("Python"))
         
+
     def OnChoosePath(self, event):
+        defaultDir = os.path.dirname(self._pathTextCtrl.GetValue().strip())
+        defaultFile = os.path.basename(self._pathTextCtrl.GetValue().strip())
         if _WINDOWS:
-            wildcard = _("*.exe")
+            wildcard = _("Executable (*.exe)|*.exe|All (*.*)|*.*")
+            if not defaultFile:
+                defaultFile = "python.exe"
         else:
             wildcard = _("*")
-        path = wx.FileSelector(_("Select a File"),
-                               _(""),
-                               _(""),
-                               wildcard = wildcard ,
-                               flags = wx.HIDE_READONLY,
-                               parent = wx.GetApp().GetTopWindow())
-        if path:  
-            self._pathTextCtrl.SetValue(path)
-            self._pathTextCtrl.SetToolTipString(self._pathTextCtrl.GetValue())
-            self._pathTextCtrl.SetInsertionPointEnd()
+        dlg = wx.FileDialog(wx.GetApp().GetTopWindow(),
+                               _("Select a File"),
+                               defaultDir=defaultDir,
+                               defaultFile=defaultFile,
+                               wildcard=wildcard,
+                               style=wx.OPEN|wx.FILE_MUST_EXIST|wx.HIDE_READONLY)
+        # dlg.CenterOnParent()  # wxBug: caused crash with wx.FileDialog
+        if dlg.ShowModal() == wx.ID_OK:
+            path = dlg.GetPath()
+            if path:  
+                self._pathTextCtrl.SetValue(path)
+                self._pathTextCtrl.SetToolTipString(self._pathTextCtrl.GetValue())
+                self._pathTextCtrl.SetInsertionPointEnd()
+        dlg.Destroy()            
+
 
     def OnOK(self, optionsDialog):
-        if len(self._pathTextCtrl.GetValue()) > 0:
-            config = wx.ConfigBase_Get()
-            config.Write("ActiveGridPythonLocation", self._pathTextCtrl.GetValue())
+        config = wx.ConfigBase_Get()
+        config.Write("ActiveGridPythonLocation", self._pathTextCtrl.GetValue().strip())
 
         self._otherOptions.OnOK(optionsDialog)
+        
+
+    def GetIcon(self):
+        return getPythonIcon()
+        
+
 #----------------------------------------------------------------------------
 # Icon Bitmaps - generated by encode_bitmaps.py
 #----------------------------------------------------------------------------
@@ -586,18 +618,28 @@ import cStringIO
 
 def getPythonData():
     return \
-"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
+'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
 \x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
-\x00\x00\xd5IDAT8\x8d\x8d\x93Y\x0e\xc3 \x0cD\x9fM\xcf\xddNr2.\x96\xb8\x1f\
-\x05\n\x84.#Y\x10\xa3\x19o\xb1\x99'*\xe2<\x82\x0e\xe6\xc9\xf8\x01\xef?\xa4\
-\xf7)]\x05\x970O\xcdr\xce!\x119\xe7\x00\x02\x88\xfe}i\xb5\x848\x8f\xa8\x19\
-\xcc\x19}+\xc5\xcc\xd3\x92<CZ\x0b\x99\xc4\xb2N\x01<\x80\xad\xdc?\x88\xf8\x1c\
-X\x8f7\xe1\x1f\xdc*\xa9a+\xe1\xa3\xdc\xe7\xb4\xf6\xd1\xe5\xb6'\xc3@\xc5\xa0#\
-\xab\x94\xd1\x0bL\xf0\xe6\x17\xa8v\xc3\x8aS\xa0.\x8be\x13\xe3\x15\x8f\xe1\
-\xa5D\xee\xc9\xdb~%\xc7y\x84\xbb'sO\xd6\xd4\x17\xe4~\xc4\xf5\xef\xac\xa7\r\
-\xbbp?b&\x0f\x89i\x14\x93\xca\x14z\xc5oh\x02E\xc4<\xd92\x03\xe0:B^\xc4K#\xe7\
-\xe5\x00\x02\xfd\xb9H\x9ex\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xcdE\xaf\xdeqlW\xaeVL\xaf`~\
+\xd9\x03@W\xd3\x00\xc4\x13\x0b\xc4\x92A\xcf\xd0\xf9\xe8:\x89\xebW\x01(|\xfd\
+\xe1\xbe-~F\xbas\xff\x91\xf75\x82n\x9d\x1c\xf0}\xfciw\xdd\xe7A<\xd1\x1b\xa8j\
+c\x9f\xb2\xd1F\x92\xe4\x80O\x12\xc0\xc6\xb3\x14\xf6Ta\xe0)g\x81\xba\x9a\xf6\
+\x9b(\x07\x14I@\x84lq\xb8?\xe6\xa3\xeb\x00\xdc\xba\x9d\xf4+\x10*~\xfem\xf3\
+\xf8\xe1\x06\xc7\xa7\xdb\xe8j\x9a\xf8\xdc\xa4\xb7\x1f[\\\xe5\xd2\x851/\xff\
+\x07\xac\x9b\xd1e\x12\x96\x0f\xfd\x00\x00\x00\x00IEND\xaeB`\x82' 
 
 
 def getPythonBitmap():