Fixing search in project feature for Windows
authorlaurent
Mon, 10 Oct 2011 02:22:26 +0200
changeset 571 79af7b821233
parent 570 37ba389e5c01
child 572 1af3cc2b207c
Fixing search in project feature for Windows
PLCOpenEditor.py
SearchResultPanel.py
dialogs/SearchInProjectDialog.py
graphics/GraphicCommons.py
--- a/PLCOpenEditor.py	Sun Oct 09 23:31:50 2011 +0200
+++ b/PLCOpenEditor.py	Mon Oct 10 02:22:26 2011 +0200
@@ -2445,7 +2445,6 @@
 #-------------------------------------------------------------------------------
 
     def ShowHighlight(self, infos, start, end, highlight_type):
-        print infos, start, end, highlight_type
         self.SelectTypesTreeItem(infos[0])
         if infos[1] == "name":
             self.Highlights[infos[0]] = highlight_type
--- a/SearchResultPanel.py	Sun Oct 09 23:31:50 2011 +0200
+++ b/SearchResultPanel.py	Mon Oct 10 02:22:26 2011 +0200
@@ -89,7 +89,7 @@
 
         self.SearchResultsTree = CT.CustomTreeCtrl(id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE,
               name="SearchResultsTree", parent=self,
-              pos=wx.Point(0, 0), style=0)
+              pos=wx.Point(0, 0), style=CT.TR_HAS_BUTTONS|CT.TR_NO_LINES|CT.TR_HAS_VARIABLE_ROW_HEIGHT)
         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnSearchResultsTreeItemActivated,
               id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE)
         
@@ -138,6 +138,8 @@
 
     def SetSearchResults(self, criteria, search_results):
         self.Criteria = criteria
+        self.SearchResults = {}
+        self.ElementsOrder = []
         
         for infos, start, end, text in search_results:
             if infos[0] not in self.ElementsOrder:
@@ -155,9 +157,9 @@
         self.RefreshView()
     
     def RefreshView(self):
+        self.SearchResultsTree.DeleteAllItems()
         if self.Criteria is None:
             self.HeaderLabel.SetLabel(_("No search results available."))
-            self.SearchResultsTree.DeleteAllItems()
             self.ResetButton.Enable(False)
         else:
             matches_number = 0
@@ -259,37 +261,48 @@
             else:
                 self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]])
         
+        text = None
         if infos["text"] is not None:
-            text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
-                    value=infos["text"], style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
-            width, height = wx.PaintDC(text_ctrl).GetTextExtent(infos["text"])
-            text_ctrl.SetClientSize(wx.Size(width + 1, height))
-            text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour())
+            text = infos["text"]
+            start, end = infos["data"][1:3]
+            text_lines = infos["text"].splitlines()
+            start_idx = start[1]
+            end_idx = reduce(lambda x, y: x + y, map(lambda x: len(x) + 1, text_lines[:end[0] - start[0]]), end[1] + 1)
+            style = wx.TextAttr(wx.BLACK, wx.Colour(206, 204, 247))
+        elif infos["type"] is not None and infos["matches"] > 1:
+            text = _("(%d matches)") % infos["matches"]
+            start_idx, end_idx = 0, len(text)
+            style = wx.TextAttr(wx.Colour(0, 127, 174))
+        
+        if text is not None:
+            background_colour = self.SearchResultsTree.GetBackgroundColour()
+            if wx.Platform != '__WXMSW__':
+                text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
+                        value=text, style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
+                width, height = text_ctrl.GetTextExtent(text)
+                text_ctrl.SetClientSize(wx.Size(width + 1, height))
+                self.SearchResultsTree.SetItemWindow(root, text_ctrl)
+            else:
+                panel = wx.Panel(id=-1, parent=self.SearchResultsTree)
+                panel.SetBackgroundColour(background_colour)
+                sizer = wx.BoxSizer(wx.HORIZONTAL)
+                if len(text.splitlines()) > 1:
+                    text_ctrl = wx.TextCtrl(id=-1, parent=panel, pos=wx.Point(0, 0), 
+                          value=text, style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
+                else:
+                    text_ctrl = wx.TextCtrl(id=-1, parent=panel, pos=wx.Point(0, 0), 
+                           value=text, size=wx.Size(0, 13), style=wx.BORDER_NONE|wx.TE_READONLY|wx.TE_RICH2)
+                width, height = text_ctrl.GetTextExtent(text)
+                sizer.AddWindow(text_ctrl, 1, border=0, flags=wx.ALIGN_CENTER_VERTICAL)
+                panel.SetSizer(sizer)
+                panel.SetClientSize(wx.Size(width + 1, height + 5))
+                self.SearchResultsTree.SetItemWindow(root, panel)
+            
+            text_ctrl.SetBackgroundColour(background_colour)
             text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root))
             text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root))
-            start, end = infos["data"][1:3]
-            text_lines = infos["text"].splitlines()
-            end_idx = reduce(lambda x, y: x + y, map(lambda x: len(x) + 1, text_lines[:end[0] - start[0]]), end[1] + 1)
             text_ctrl.SetInsertionPoint(0)
-            text_ctrl.SetStyle(start[1], end_idx, wx.TextAttr(wx.BLACK, wx.Colour(206, 204, 247)))
-            
-            self.SearchResultsTree.SetItemWindow(root, text_ctrl)
-        
-        elif infos["type"] is not None and infos["matches"] > 1:
-            text = _("(%d matches)") % infos["matches"]
-            text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
-                    value=text, style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
-            width, height = wx.PaintDC(text_ctrl).GetTextExtent(text)
-            text_ctrl.SetClientSize(wx.Size(width + 1, height))
-            text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour())
-            text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root))
-            text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root))
-            
-            text_ctrl.SetInsertionPoint(0)
-            text_ctrl.SetStyle(0, len(text), wx.TextAttr(wx.Colour(0, 127, 174)))
-            
-            self.SearchResultsTree.SetItemWindow(root, text_ctrl)
-            
+            text_ctrl.SetStyle(start_idx, end_idx, style)
             
         if wx.VERSION >= (2, 6, 0):
             item, root_cookie = self.SearchResultsTree.GetFirstChild(root)
@@ -298,15 +311,9 @@
         for child in infos["children"]:
             if item is None:
                 item = self.SearchResultsTree.AppendItem(root, "")
-                if wx.Platform != '__WXMSW__':
-                    item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
+                item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
             self.GenerateSearchResultsTreeBranch(item, child)
             item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
-        while item is not None:
-            to_delete.append(item)
-            item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
-        for item in to_delete:
-            self.SearchResultsTree.Delete(item)
     
     def ShowSearchResults(self, item):
         data = self.SearchResultsTree.GetPyData(item)
--- a/dialogs/SearchInProjectDialog.py	Sun Oct 09 23:31:50 2011 +0200
+++ b/dialogs/SearchInProjectDialog.py	Mon Oct 10 02:22:26 2011 +0200
@@ -163,11 +163,11 @@
             self.ElementsList.Append(_(label))
 
     def GetCriteria(self):
-        raw_pattern = self.Pattern.GetValue()
+        raw_pattern = pattern = self.Pattern.GetValue()
         if not self.CaseSensitive.GetValue():
-            pattern = raw_pattern.upper()
+            pattern = pattern.upper()
         if not self.RegularExpression.GetValue():
-            pattern = EscapeText(raw_pattern)
+            pattern = EscapeText(pattern)
         criteria = {
             "raw_pattern": raw_pattern, 
             "pattern": re.compile(pattern),
--- a/graphics/GraphicCommons.py	Sun Oct 09 23:31:50 2011 +0200
+++ b/graphics/GraphicCommons.py	Mon Oct 10 02:22:26 2011 +0200
@@ -95,8 +95,8 @@
 HIGHLIGHTCOLOR = wx.CYAN
 
 # Define highlight types
-ERROR_HIGHLIGHT = (wx.NamedColour("yellow"), wx.RED)
-SEARCH_RESULT_HIGHLIGHT = (wx.NamedColour("orange"), wx.WHITE)
+ERROR_HIGHLIGHT = (wx.Colour(255, 255, 0), wx.RED)
+SEARCH_RESULT_HIGHLIGHT = (wx.Colour(255, 165, 0), wx.WHITE)
 
 # Define highlight refresh inhibition period in second
 REFRESH_HIGHLIGHT_PERIOD = 0.1