]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridDragAndDrop.py
2 Example showing how to make a grid a drop target for files.
6 from wxPython
.wx
import *
7 from wxPython
.grid
import *
9 #---------------------------------------------------------------------------
10 # Set VIRTUAL to 1 to use a virtual grid
14 class GridFileDropTarget(wxFileDropTarget
):
15 def __init__(self
, grid
):
16 wxFileDropTarget
.__init
__(self
)
19 def OnDropFiles(self
, x
, y
, filenames
):
20 # the x,y coordinates here are Unscrolled coordinates. They must be changed
21 # to scrolled coordinates.
22 x
, y
= self
.grid
.CalcUnscrolledPosition(x
, y
)
24 # now we need to get the row and column from the grid
25 # but we need to first remove the RowLabel and ColumnLabel
27 # Why this isn't done for us, I'll never know...
28 x
= x
- self
.grid
.GetGridRowLabelWindow().GetRect().width
29 y
= y
- self
.grid
.GetGridColLabelWindow().GetRect().height
30 col
= self
.grid
.XToCol(x
)
31 row
= self
.grid
.YToRow(y
)
33 if row
> -1 and col
> -1:
34 self
.grid
.SetCellValue(row
, col
, filenames
[0])
35 self
.grid
.AutoSizeColumn(col
)
40 class FooTable(wxPyGridTableBase
):
42 wxPyGridTableBase
.__init
__(self
)
43 self
.dropTargets
= {(0,0):"Drag",
49 def GetNumberCols(self
):
51 def GetNumberRows(self
):
53 def GetValue(self
, row
, col
):
54 return self
.dropTargets
.get((row
, col
), "")
58 class SimpleGrid(wxGrid
):
59 def __init__(self
, parent
, log
):
60 wxGrid
.__init
__(self
, parent
, -1)
64 self
.table
= FooTable()
65 self
.SetTable(self
.table
)
67 self
.CreateGrid(25, 25)
69 # set the drag and drop target
70 dropTarget
= GridFileDropTarget(self
)
71 self
.SetDropTarget(dropTarget
)
72 self
.EnableDragRowSize()
73 self
.EnableDragColSize()
75 def SetCellValue(self
, row
, col
, value
):
77 self
.table
.dropTargets
[row
, col
] = value
79 wxGrid
.SetCellValue(self
, row
, col
, value
)
83 class TestFrame(wxFrame
):
84 def __init__(self
, parent
, log
):
85 wxFrame
.__init
__(self
, parent
, -1, "DragAndDrop Grid", size
=(640,480))
86 grid
= SimpleGrid(self
, log
)
90 #---------------------------------------------------------------------------
92 if __name__
== '__main__':
95 frame
= TestFrame(None, sys
.stdout
)
100 #---------------------------------------------------------------------------