]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/lib/gridmovers.py
Treat Ctrl-Shift-Backspace the same as ESC in the shell.
[wxWidgets.git] / wxPython / wx / lib / gridmovers.py
index 018e80fc15c023437846bf45966d62a01be52d2e..9cd7269dfb6b23b10b73729d4d4b048b68ef1754 100644 (file)
@@ -9,27 +9,39 @@
 # RCS-ID:       $Id$
 # Licence:      wxWindows license
 #----------------------------------------------------------------------------
+# 12/07/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o 2.5 Compatability changes
+#
+# 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o wxGridColMoveEvent -> GridColMoveEvent
+# o wxGridRowMoveEvent -> GridRowMoveEvent
+# o wxGridColMover -> GridColMover
+# o wxGridRowMover -> GridRowMover
+#
 
-from wxPython.wx import *
-from wxPython.grid import wxGrid
-
-#----------------------------------------------------------------------------
-# event class and macors
 
+import  wx
+import  wx.grid
 
-wxEVT_COMMAND_GRID_COL_MOVE = wxNewEventType()
-wxEVT_COMMAND_GRID_ROW_MOVE = wxNewEventType()
+#----------------------------------------------------------------------------
+# event class and macros
+# 
+# New style 12/7/03
+#
 
-def EVT_GRID_COL_MOVE(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_GRID_COL_MOVE, func)
+wxEVT_COMMAND_GRID_COL_MOVE = wx.NewEventType()
+wxEVT_COMMAND_GRID_ROW_MOVE = wx.NewEventType()
 
-def EVT_GRID_ROW_MOVE(win,id,func):
-    win.Connect(id, -1, wxEVT_COMMAND_GRID_ROW_MOVE, func)
+EVT_GRID_COL_MOVE = wx.PyEventBinder(wxEVT_COMMAND_GRID_COL_MOVE, 1)
+EVT_GRID_ROW_MOVE = wx.PyEventBinder(wxEVT_COMMAND_GRID_ROW_MOVE, 1)
 
+#----------------------------------------------------------------------------
 
-class wxGridColMoveEvent(wxPyCommandEvent):
+class GridColMoveEvent(wx.PyCommandEvent):
     def __init__(self, id, dCol, bCol):
-        wxPyCommandEvent.__init__(self, id = id)
+        wx.PyCommandEvent.__init__(self, id = id)
         self.SetEventType(wxEVT_COMMAND_GRID_COL_MOVE)
         self.moveColumn = dCol
         self.beforeColumn = bCol
@@ -41,9 +53,9 @@ class wxGridColMoveEvent(wxPyCommandEvent):
         return self.beforeColumn
 
 
-class wxGridRowMoveEvent(wxPyCommandEvent):
+class GridRowMoveEvent(wx.PyCommandEvent):
     def __init__(self, id, dRow, bRow):
-        wxPyCommandEvent.__init__(self,id = id)
+        wx.PyCommandEvent.__init__(self,id = id)
         self.SetEventType(wxEVT_COMMAND_GRID_ROW_MOVE)
         self.moveRow = dRow
         self.beforeRow = bRow
@@ -62,43 +74,47 @@ def _ColToRect(self,col):
     if self.GetNumberRows() > 0:
         rect = self.CellToRect(0,col)
     else:
-        rect = wxRect()
+        rect = wx.Rect()
         rect.height = self.GetColLabelSize()
         rect.width = self.GetColSize(col)
+
         for cCol in range(0,col):
             rect.x += self.GetColSize(cCol)
+
     rect.y = self.GetGridColLabelWindow().GetPosition()[1]
     return rect
 
-wxGrid.ColToRect = _ColToRect
+wx.grid.Grid.ColToRect = _ColToRect
 
 
 def _RowToRect(self,row):
     if self.GetNumberCols() > 0:
         rect = self.CellToRect(row,0)
     else:
-        rect = wxRect()
+        rect = wx.Rect()
         rect.width = self.GetRowLabelSize()
         rect.height = self.GetRowSize(row)
+
         for cRow in range(0,row):
             rect.y += self.GetRowSize(cRow)
+
     rect.x = self.GetGridRowLabelWindow().GetPosition()[0]
     return rect
 
-wxGrid.RowToRect = _RowToRect
+wx.grid.Grid.RowToRect = _RowToRect
 
 
 #----------------------------------------------------------------------------
 
-class ColDragWindow(wxWindow):
+class ColDragWindow(wx.Window):
     def __init__(self,parent,image,dragCol):
-        wxWindow.__init__(self,parent,-1, style=wxSIMPLE_BORDER)
+        wx.Window.__init__(self,parent,-1, style=wx.SIMPLE_BORDER)
         self.image = image
         self.SetSize((self.image.GetWidth(),self.image.GetHeight()))
         self.ux = parent.GetScrollPixelsPerUnit()[0]
         self.moveColumn = dragCol
 
-        EVT_PAINT(self,self.OnPaint)
+        self.Bind(wx.EVT_PAINT, self.OnPaint)
 
     def DisplayAt(self,pos,y):
         x = self.GetPositionTuple()[0]
@@ -114,20 +130,24 @@ class ColDragWindow(wxWindow):
         parent = self.GetParent()
         sx = parent.GetViewStart()[0] * self.ux
         sx -= parent._rlSize
-        x = self.GetPositionTuple()[0]
-        w = self.GetSizeTuple()[0]
+        x = self.GetPosition()[0]
+        w = self.GetSize()[0]
         sCol = parent.XToCol(x + sx)
         eCol = parent.XToCol(x + w + sx)
         iPos = xPos = xCol = 99999
         centerPos = x + sx + (w / 2)
+
         for col in range(sCol,eCol + 1):
             cx = parent.ColToRect(col)[0]
+
             if abs(cx - centerPos) < iPos:
                 iPos = abs(cx - centerPos)
                 xCol = col
                 xPos = cx
+
         if xCol < 0 or xCol > parent.GetNumberCols():
             xCol = parent.GetNumberCols()
+
         return (xPos - sx - x,xCol)
 
     def GetInsertionColumn(self):
@@ -137,30 +157,30 @@ class ColDragWindow(wxWindow):
         return self._GetInsertionInfo()[0]
 
     def OnPaint(self,evt):
-        dc = wxPaintDC(self)
+        dc = wx.PaintDC(self)
         w,h = self.GetSize()
-        dc.DrawBitmap(self.image, (0,0))
-        dc.SetPen(wxPen(wxBLACK,1,wxSOLID))
-        dc.SetBrush(wxTRANSPARENT_BRUSH)
-        dc.DrawRectangle((0,0), (w,h))
+        dc.DrawBitmap(self.image, 0,0)
+        dc.SetPen(wx.Pen(wx.BLACK,1,wx.SOLID))
+        dc.SetBrush(wx.TRANSPARENT_BRUSH)
+        dc.DrawRectangle(0,0, w,h)
         iPos = self.GetInsertionPos()
-        dc.DrawLine((iPos,h - 10), (iPos,h))
+        dc.DrawLine(iPos,h - 10, iPos,h)
 
 
 
 
-class RowDragWindow(wxWindow):
+class RowDragWindow(wx.Window):
     def __init__(self,parent,image,dragRow):
-        wxWindow.__init__(self,parent,-1, style=wxSIMPLE_BORDER)
+        wx.Window.__init__(self,parent,-1, style=wx.SIMPLE_BORDER)
         self.image = image
         self.SetSize((self.image.GetWidth(),self.image.GetHeight()))
         self.uy = parent.GetScrollPixelsPerUnit()[1]
         self.moveRow = dragRow
 
-        EVT_PAINT(self,self.OnPaint)
+        self.Bind(wx.EVT_PAINT, self.OnPaint)
 
     def DisplayAt(self,x,pos):
-        y = self.GetPositionTuple()[1]
+        y = self.GetPosition()[1]
         if y == pos:
             self.Refresh()              # Need to display insertion point
         else:
@@ -173,20 +193,24 @@ class RowDragWindow(wxWindow):
         parent = self.GetParent()
         sy = parent.GetViewStart()[1] * self.uy
         sy -= parent._clSize
-        y = self.GetPositionTuple()[1]
-        h = self.GetSizeTuple()[1]
+        y = self.GetPosition()[1]
+        h = self.GetSize()[1]
         sRow = parent.YToRow(y + sy)
         eRow = parent.YToRow(y + h + sy)
         iPos = yPos = yRow = 99999
         centerPos = y + sy + (h / 2)
+
         for row in range(sRow,eRow + 1):
             cy = parent.RowToRect(row)[1]
+
             if abs(cy - centerPos) < iPos:
                 iPos = abs(cy - centerPos)
                 yRow = row
                 yPos = cy
+
         if yRow < 0 or yRow > parent.GetNumberRows():
             yRow = parent.GetNumberRows()
+
         return (yPos - sy - y,yRow)
 
     def GetInsertionRow(self):
@@ -196,20 +220,20 @@ class RowDragWindow(wxWindow):
         return self._GetInsertionInfo()[0]
 
     def OnPaint(self,evt):
-        dc = wxPaintDC(self)
+        dc = wx.PaintDC(self)
         w,h = self.GetSize()
-        dc.DrawBitmap(self.image, (0,0))
-        dc.SetPen(wxPen(wxBLACK,1,wxSOLID))
-        dc.SetBrush(wxTRANSPARENT_BRUSH)
-        dc.DrawRectangle((0,0), (w,h))
+        dc.DrawBitmap(self.image, 0,0)
+        dc.SetPen(wx.Pen(wx.BLACK,1,wx.SOLID))
+        dc.SetBrush(wx.TRANSPARENT_BRUSH)
+        dc.DrawRectangle(0,0, w,h)
         iPos = self.GetInsertionPos()
-        dc.DrawLine((w - 10,iPos), (w,iPos))
+        dc.DrawLine(w - 10,iPos, w,iPos)
 
 #----------------------------------------------------------------------------
 
-class wxGridColMover(wxEvtHandler):
+class GridColMover(wx.EvtHandler):
     def __init__(self,grid):
-        wxEvtHandler.__init__(self)
+        wx.EvtHandler.__init__(self)
 
         self.grid = grid
         self.grid._rlSize = self.grid.GetRowLabelSize()
@@ -222,46 +246,59 @@ class wxGridColMover(wxEvtHandler):
         self.didMove = False
         self.isDragging = False
 
-        EVT_MOTION(self,self.OnMouseMove)
-        EVT_LEFT_DOWN(self,self.OnPress)
-        EVT_LEFT_UP(self,self.OnRelease)
+        self.Bind(wx.EVT_MOTION, self.OnMouseMove)
+        self.Bind(wx.EVT_LEFT_DOWN, self.OnPress)
+        self.Bind(wx.EVT_LEFT_UP, self.OnRelease)
 
     def OnMouseMove(self,evt):
         if self.isDragging:
-            if abs(self.startX - evt.m_x) >= 3:
+            if abs(self.startX - evt.m_x) >= 3 \
+                   and abs(evt.m_x - self.lastX) >= 3:
+                self.lastX = evt.m_x 
                 self.didMove = True
                 sx,y = self.grid.GetViewStart()
-                w,h = self.lwin.GetClientSizeTuple()
+                w,h = self.lwin.GetClientSize()
                 x = sx * self.ux
+
                 if (evt.m_x + x) < x:
                     x = evt.m_x + x
                 elif evt.m_x > w:
                     x += evt.m_x - w
+
                 if x < 1: x = 0
                 else: x /= self.ux
+
                 if x != sx:
-                    if wxPlatform == '__WXMSW__':
+                    if wx.Platform == '__WXMSW__':
                         self.colWin.Show(False)
+
                     self.grid.Scroll(x,y)
+
                 x,y = self.lwin.ClientToScreenXY(evt.m_x,0)
                 x,y = self.grid.ScreenToClientXY(x,y)
+
                 if not self.colWin.IsShown():
                     self.colWin.Show(True)
+
                 px = x - self.cellX
+
                 if px < 0 + self.grid._rlSize: px = 0 + self.grid._rlSize
-                if px > w - self.colWin.GetSizeTuple()[0] + self.grid._rlSize:
-                    px = w - self.colWin.GetSizeTuple()[0] + self.grid._rlSize
+
+                if px > w - self.colWin.GetSize()[0] + self.grid._rlSize:
+                    px = w - self.colWin.GetSize()[0] + self.grid._rlSize
+
                 self.colWin.DisplayAt(px,y)
                 return
-        evt.Skip()
+
 
     def OnPress(self,evt):
-        self.startX = evt.m_x
+        self.startX = self.lastX = evt.m_x
         sx = self.grid.GetViewStart()[0] * self.ux
         sx -= self.grid._rlSize
         px,py = self.lwin.ClientToScreenXY(evt.m_x,evt.m_y)
         px,py = self.grid.ScreenToClientXY(px,py)
-        if self.grid.XToEdgeOfCol(px + sx) != wxNOT_FOUND:
+
+        if self.grid.XToEdgeOfCol(px + sx) != wx.NOT_FOUND:
             evt.Skip()
             return
 
@@ -270,7 +307,7 @@ class wxGridColMover(wxEvtHandler):
         col = self.grid.XToCol(px + sx)
         rect = self.grid.ColToRect(col)
         self.cellX = px + sx - rect.x
-        size = self.lwin.GetSizeTuple()
+        size = self.lwin.GetSize()
         rect.y = 0
         rect.x -= sx + self.grid._rlSize
         rect.height = size[1]
@@ -278,43 +315,47 @@ class wxGridColMover(wxEvtHandler):
         self.colWin = ColDragWindow(self.grid,colImg,col)
         self.colWin.Show(False)
         self.lwin.CaptureMouse()
+        evt.Skip()
 
     def OnRelease(self,evt):
         if self.isDragging:
             self.lwin.ReleaseMouse()
             self.colWin.Show(False)
             self.isDragging = False
+
             if not self.didMove:
                 px = self.lwin.ClientToScreenXY(self.startX,0)[0]
                 px = self.grid.ScreenToClientXY(px,0)[0]
                 sx = self.grid.GetViewStart()[0] * self.ux
                 sx -= self.grid._rlSize
                 col = self.grid.XToCol(px+sx)
-                if col != wxNOT_FOUND:
+
+                if col != wx.NOT_FOUND:
                     self.grid.SelectCol(col,evt.m_controlDown)
+
                 return
             else:
                 bCol = self.colWin.GetInsertionColumn()
                 dCol = self.colWin.GetMoveColumn()
-                wxPostEvent(self,wxGridColMoveEvent(self.grid.GetId(),
-                                                    dCol,bCol))
+                wx.PostEvent(self,
+                             GridColMoveEvent(self.grid.GetId(), dCol, bCol))
+
             self.colWin.Destroy()
         evt.Skip()
 
     def _CaptureImage(self,rect):
-        bmp = wxEmptyBitmap(rect.width,rect.height)
-        memdc = wxMemoryDC()
+        bmp = wx.EmptyBitmap(rect.width,rect.height)
+        memdc = wx.MemoryDC()
         memdc.SelectObject(bmp)
-        dc = wxWindowDC(self.lwin)
-        memdc.Blit((0,0), rect.GetSize(), dc, rect.GetPosition())
-        memdc.SelectObject(wxNullBitmap)
+        dc = wx.WindowDC(self.lwin)
+        memdc.Blit(0,0, rect.width, rect.height, dc, rect.x, rect.y)
+        memdc.SelectObject(wx.NullBitmap)
         return bmp
 
 
-
-class wxGridRowMover(wxEvtHandler):
+class GridRowMover(wx.EvtHandler):
     def __init__(self,grid):
-        wxEvtHandler.__init__(self)
+        wx.EvtHandler.__init__(self)
 
         self.grid = grid
         self.grid._clSize = self.grid.GetColLabelSize()
@@ -327,46 +368,62 @@ class wxGridRowMover(wxEvtHandler):
         self.didMove = False
         self.isDragging = False
 
-        EVT_MOTION(self,self.OnMouseMove)
-        EVT_LEFT_DOWN(self,self.OnPress)
-        EVT_LEFT_UP(self,self.OnRelease)
+        self.Bind(wx.EVT_MOTION, self.OnMouseMove)
+        self.Bind(wx.EVT_LEFT_DOWN, self.OnPress)
+        self.Bind(wx.EVT_LEFT_UP, self.OnRelease)
 
     def OnMouseMove(self,evt):
         if self.isDragging:
-            if abs(self.startY - evt.m_y) >= 3:
+            if abs(self.startY - evt.m_y) >= 3 \
+                   and abs(evt.m_y - self.lastY) >= 3:
+                self.lastY = evt.m_y
                 self.didMove = True
                 x,sy = self.grid.GetViewStart()
                 w,h = self.lwin.GetClientSizeTuple()
                 y = sy * self.uy
+
                 if (evt.m_y + y) < y:
                     y = evt.m_y + y
                 elif evt.m_y > h:
                     y += evt.m_y - h
-                if y < 1: y = 0
-                else: y /= self.uy
+
+                if y < 1: 
+                    y = 0
+                else: 
+                    y /= self.uy
+
                 if y != sy:
-                    if wxPlatform == '__WXMSW__':
+                    if wx.Platform == '__WXMSW__':
                         self.rowWin.Show(False)
+
                     self.grid.Scroll(x,y)
+
                 x,y = self.lwin.ClientToScreenXY(0,evt.m_y)
                 x,y = self.grid.ScreenToClientXY(x,y)
+
                 if not self.rowWin.IsShown():
                     self.rowWin.Show(True)
+
                 py = y - self.cellY
-                if py < 0 + self.grid._clSize: py = 0 + self.grid._clSize
-                if py > h - self.rowWin.GetSizeTuple()[1] + self.grid._clSize:
-                    py = h - self.rowWin.GetSizeTuple()[1] + self.grid._clSize
+
+                if py < 0 + self.grid._clSize: 
+                    py = 0 + self.grid._clSize
+
+                if py > h - self.rowWin.GetSize()[1] + self.grid._clSize:
+                    py = h - self.rowWin.GetSize()[1] + self.grid._clSize
+
                 self.rowWin.DisplayAt(x,py)
                 return
-        evt.Skip()
+
 
     def OnPress(self,evt):
-        self.startY = evt.m_y
+        self.startY = self.lastY = evt.m_y
         sy = self.grid.GetViewStart()[1] * self.uy
         sy -= self.grid._clSize
         px,py = self.lwin.ClientToScreenXY(evt.m_x,evt.m_y)
         px,py = self.grid.ScreenToClientXY(px,py)
-        if self.grid.YToEdgeOfRow(py + sy) != wxNOT_FOUND:
+
+        if self.grid.YToEdgeOfRow(py + sy) != wx.NOT_FOUND:
             evt.Skip()
             return
 
@@ -375,7 +432,7 @@ class wxGridRowMover(wxEvtHandler):
         row = self.grid.YToRow(py + sy)
         rect = self.grid.RowToRect(row)
         self.cellY = py + sy - rect.y
-        size = self.lwin.GetSizeTuple()
+        size = self.lwin.GetSize()
         rect.x = 0
         rect.y -= sy + self.grid._clSize
         rect.width = size[0]
@@ -383,36 +440,41 @@ class wxGridRowMover(wxEvtHandler):
         self.rowWin = RowDragWindow(self.grid,rowImg,row)
         self.rowWin.Show(False)
         self.lwin.CaptureMouse()
+        evt.Skip()
 
     def OnRelease(self,evt):
         if self.isDragging:
             self.lwin.ReleaseMouse()
             self.rowWin.Show(False)
             self.isDragging = False
+
             if not self.didMove:
                 py = self.lwin.ClientToScreenXY(0,self.startY)[1]
                 py = self.grid.ScreenToClientXY(0,py)[1]
                 sy = self.grid.GetViewStart()[1] * self.uy
                 sy -= self.grid._clSize
                 row = self.grid.YToRow(py + sy)
-                if row != wxNOT_FOUND:
+
+                if row != wx.NOT_FOUND:
                     self.grid.SelectRow(row,evt.m_controlDown)
                 return
             else:
                 bRow = self.rowWin.GetInsertionRow()
                 dRow = self.rowWin.GetMoveRow()
-                wxPostEvent(self,wxGridRowMoveEvent(self.grid.GetId(),
-                                                    dRow,bRow))
+
+                wx.PostEvent(self,
+                             GridRowMoveEvent(self.grid.GetId(), dRow, bRow))
+
             self.rowWin.Destroy()
         evt.Skip()
 
     def _CaptureImage(self,rect):
-        bmp = wxEmptyBitmap(rect.width,rect.height)
-        memdc = wxMemoryDC()
+        bmp = wx.EmptyBitmap(rect.width,rect.height)
+        memdc = wx.MemoryDC()
         memdc.SelectObject(bmp)
-        dc = wxWindowDC(self.lwin)
-        memdc.Blit((0,0), rect.GetSize(), dc, rect.GetPosition())
-        memdc.SelectObject(wxNullBitmap)
+        dc = wx.WindowDC(self.lwin)
+        memdc.Blit(0,0, rect.width, rect.height, dc, rect.x, rect.y)
+        memdc.SelectObject(wx.NullBitmap)
         return bmp