Redefine cursor switching procedure in graphic viewers
authorlaurent
Thu, 23 Jul 2009 18:07:14 +0200
changeset 381 98890d848701
parent 380 9ca678ee827f
child 382 42a9b03bba82
Redefine cursor switching procedure in graphic viewers
Viewer.py
graphics/GraphicCommons.py
graphics/LD_Objects.py
graphics/SFC_Objects.py
--- a/Viewer.py	Thu Jul 23 17:53:54 2009 +0200
+++ b/Viewer.py	Thu Jul 23 18:07:14 2009 +0200
@@ -41,6 +41,18 @@
 WINDOW_BORDER = 10
 SCROLL_ZONE = 10
 
+CURSORS = None
+
+def ResetCursors():
+    global CURSORS
+    if CURSORS == None:
+        CURSORS = [wx.NullCursor, 
+                   wx.StockCursor(wx.CURSOR_HAND),
+                   wx.StockCursor(wx.CURSOR_SIZENWSE),
+                   wx.StockCursor(wx.CURSOR_SIZENESW),
+                   wx.StockCursor(wx.CURSOR_SIZEWE),
+                   wx.StockCursor(wx.CURSOR_SIZENS)]
+
 def AppendMenu(parent, help, id, kind, text):
     if wx.VERSION >= (2, 6, 0):
         parent.Append(help=help, id=id, kind=kind, text=text)
@@ -364,6 +376,10 @@
         self.StartMousePos = None
         self.StartScreenPos = None
         
+        # Initialize Cursors
+        ResetCursors()
+        self.CurrentCursor = 0
+        
         # Initialize Block, Wire and Comment numbers
         self.block_id = self.wire_id = self.comment_id = 0
         
@@ -414,6 +430,12 @@
         self.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheelWindow)
         self.Bind(wx.EVT_SIZE, self.OnMoveWindow)
     
+    def SetCurrentCursor(self, cursor):
+        global CURSORS
+        if self.CurrentCursor != cursor:
+            self.CurrentCursor = cursor
+            self.SetCursor(CURSORS[cursor])
+    
     def GetScrolledRect(self, rect):
         rect.x, rect.y = self.CalcScrolledPosition(int(rect.x * self.ViewScale[0]), 
                                                    int(rect.y * self.ViewScale[1]))
@@ -1492,7 +1514,7 @@
                     Graphic_Element.OnLeftUp(self.SelectedElement, event, dc, self.Scaling)
                 else:
                     self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
-                wx.CallAfter(self.SetCursor, wx.NullCursor)
+                wx.CallAfter(self.SetCurrentCursor, 0)
         if self.Mode != MODE_SELECTION and not self.SavedMode:
             wx.CallAfter(self.ParentWindow.ResetCurrentMode)
         event.Skip()
@@ -1531,7 +1553,7 @@
                 Graphic_Element.OnRightUp(self.SelectedElement, event, dc, self.Scaling)
             else:
                 self.SelectedElement.OnRightUp(event, dc, self.Scaling)
-            wx.CallAfter(self.SetCursor, wx.NullCursor)
+            wx.CallAfter(self.SetCurrentCursor, 0)
         elif not self.Debug:
             self.PopupDefaultMenu(False)
         event.Skip()
@@ -1683,7 +1705,7 @@
             self.SelectedElement = None
             self.RefreshBuffer()
             self.RefreshScrollBars()
-            self.SetCursor(wx.NullCursor)
+            wx.CallAfter(self.SetCurrentCursor, 0)
             self.RefreshRect(self.GetScrolledRect(rect), False)
         elif not self.Debug and keycode == wx.WXK_RETURN and self.SelectedElement is not None:
             self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
--- a/graphics/GraphicCommons.py	Thu Jul 23 17:53:54 2009 +0200
+++ b/graphics/GraphicCommons.py	Thu Jul 23 18:07:14 2009 +0200
@@ -92,18 +92,6 @@
 
 # Color for Highlighting
 HIGHLIGHTCOLOR = wx.CYAN
-
-CURSORS = None
-
-def ResetCursors():
-    global CURSORS
-    if CURSORS == None:
-        CURSORS = [wx.NullCursor, 
-                   wx.StockCursor(wx.CURSOR_HAND),
-                   wx.StockCursor(wx.CURSOR_SIZENWSE),
-                   wx.StockCursor(wx.CURSOR_SIZENESW),
-                   wx.StockCursor(wx.CURSOR_SIZEWE),
-                   wx.StockCursor(wx.CURSOR_SIZENS)]
            
 HANDLE_CURSORS = {
     (1, 1) : 2,
@@ -470,8 +458,6 @@
         self.Size = wx.Size(0, 0)
         self.BoundingBox = wx.Rect(0, 0, 0, 0)
         self.Visible = False
-        self.CurrentCursor = 0
-        ResetCursors()
     
     def TestVisible(self, screen):
         self.Visible = self.GetRedrawRect().Intersects(screen)
@@ -630,9 +616,7 @@
         # Find which type of handle have been clicked,
         # Save a resize event and change the cursor
         cursor = HANDLE_CURSORS.get(handle, 1)
-        if cursor != self.CurrentCursor:
-            self.Parent.SetCursor(CURSORS[cursor])
-            self.CurrentCursor = cursor
+        wx.CallAfter(self.Parent.SetCurrentCursor, cursor)
         if cursor > 1:
             self.Handle = (HANDLE_RESIZE, handle)
         else:
@@ -649,9 +633,7 @@
         if self.Dragging and self.oldPos:
             self.RefreshModel()
             self.Parent.RefreshBuffer()
-        if self.CurrentCursor != 0:
-            self.Parent.SetCursor(CURSORS[0])
-            self.CurrentCursor = 0
+        wx.CallAfter(self.Parent.SetCurrentCursor, 0)
         self.SetSelected(True)
         self.oldPos = None
 
@@ -664,9 +646,7 @@
         if self.Dragging and self.oldPos:
             self.RefreshModel()
             self.Parent.RefreshBuffer()
-        if self.CurrentCursor != 0:
-            self.Parent.SetCursor(CURSORS[0])
-            self.CurrentCursor = 0
+        wx.CallAfter(self.Parent.SetCurrentCursor, 0)
         self.SetSelected(True)
         self.oldPos = None
 
@@ -703,9 +683,7 @@
             # Find which type of handle have been clicked,
             # Save a resize event and change the cursor
             cursor = HANDLE_CURSORS.get(handle, 0)
-            if cursor != self.CurrentCursor:
-                self.Parent.SetCursor(CURSORS[cursor])
-                self.CurrentCursor = cursor
+            wx.CallAfter(self.Parent.SetCurrentCursor, cursor)
             return 0, 0
 
     # Moves the element
@@ -2374,15 +2352,15 @@
         #result = self.TestPoint(pos)
         #if result != None:
         #    self.Handle = (HANDLE_POINT, result)
-        #    self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
+        #    wx.CallAfter(self.Parent.SetCurrentCursor, 1)
         #else:
         # Test if a segment have been handled
         result = self.TestSegment(pos)
         if result != None:
             if result[1] in (NORTH, SOUTH):
-                self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZEWE))
+                wx.CallAfter(self.Parent.SetCurrentCursor, 4)
             elif result[1] in (EAST, WEST):
-                self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZENS))
+                wx.CallAfter(self.Parent.SetCurrentCursor, 5)
             self.Handle = (HANDLE_SEGMENT, result)
         # Execute the default method for a graphic element
         else:
@@ -2461,13 +2439,9 @@
             result = self.TestSegment(pos)
             if result:
                 if result[1] in (NORTH, SOUTH):
-                    if self.CurrentCursor != 4:
-                        self.CurrentCursor = 4
-                        wx.CallAfter(self.Parent.SetCursor, CURSORS[4])
+                    wx.CallAfter(self.Parent.SetCurrentCursor, 4)
                 elif result[1] in (EAST, WEST):
-                    if self.CurrentCursor != 5:
-                        self.CurrentCursor = 5
-                        wx.CallAfter(self.Parent.SetCursor, CURSORS[5])
+                    wx.CallAfter(self.Parent.SetCurrentCursor, 5)
                 return 0, 0
             else:
                 # Test if a point has been handled
--- a/graphics/LD_Objects.py	Thu Jul 23 17:53:54 2009 +0200
+++ b/graphics/LD_Objects.py	Thu Jul 23 18:07:14 2009 +0200
@@ -297,7 +297,7 @@
         connector = self.TestConnector(pos, exclude=False)
         if connector:
             self.Handle = (HANDLE_CONNECTOR, connector)
-            self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
+            wx.CallAfter(self.Parent.SetCurrentCursor, 1)
             self.Selected = False
             # Initializes the last position
             self.oldPos = GetScaledEventPosition(event, dc, scaling)
--- a/graphics/SFC_Objects.py	Thu Jul 23 17:53:54 2009 +0200
+++ b/graphics/SFC_Objects.py	Thu Jul 23 18:07:14 2009 +0200
@@ -1332,7 +1332,7 @@
         connector = self.TestConnector(pos, exclude=False)
         if connector:
             self.Handle = (HANDLE_CONNECTOR, connector)
-            self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
+            wx.CallAfter(self.Parent.SetCurrentCursor, 1)
             self.Selected = False
             # Initializes the last position
             self.oldPos = GetScaledEventPosition(event, dc, scaling)