]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridDragAndDrop.py
1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
7 Example showing how to make a grid a drop target for files.
12 import wx
.grid
as gridlib
14 #---------------------------------------------------------------------------
15 # Set VIRTUAL to 1 to use a virtual grid
17 #---------------------------------------------------------------------------
19 class GridFileDropTarget(wx
.FileDropTarget
):
20 def __init__(self
, grid
):
21 wx
.FileDropTarget
.__init
__(self
)
24 def OnDropFiles(self
, x
, y
, filenames
):
25 # the x,y coordinates here are Unscrolled coordinates. They must be changed
26 # to scrolled coordinates.
27 x
, y
= self
.grid
.CalcUnscrolledPosition(x
, y
)
29 # now we need to get the row and column from the grid
30 # but we need to first remove the RowLabel and ColumnLabel
32 # Why this isn't done for us, I'll never know...
33 x
= x
- self
.grid
.GetGridRowLabelWindow().GetRect().width
34 y
= y
- self
.grid
.GetGridColLabelWindow().GetRect().height
35 col
= self
.grid
.XToCol(x
)
36 row
= self
.grid
.YToRow(y
)
38 if row
> -1 and col
> -1:
39 self
.grid
.SetCellValue(row
, col
, filenames
[0])
40 self
.grid
.AutoSizeColumn(col
)
45 class FooTable(gridlib
.PyGridTableBase
):
47 gridlib
.PyGridTableBase
.__init
__(self
)
48 self
.dropTargets
= {(0,0):"Drag",
54 def GetNumberCols(self
):
56 def GetNumberRows(self
):
58 def GetValue(self
, row
, col
):
59 return self
.dropTargets
.get((row
, col
), "")
62 class SimpleGrid(gridlib
.Grid
):
63 def __init__(self
, parent
, log
):
64 gridlib
.Grid
.__init
__(self
, parent
, -1)
69 self
.table
= FooTable()
70 self
.SetTable(self
.table
)
72 self
.CreateGrid(25, 25)
74 # set the drag and drop target
75 dropTarget
= GridFileDropTarget(self
)
76 self
.SetDropTarget(dropTarget
)
77 self
.EnableDragRowSize()
78 self
.EnableDragColSize()
80 def SetCellValue(self
, row
, col
, value
):
82 self
.table
.dropTargets
[row
, col
] = value
84 gridlib
.Grid
.SetCellValue(self
, row
, col
, value
)
87 class TestFrame(wx
.Frame
):
88 def __init__(self
, parent
, log
):
89 wx
.Frame
.__init
__(self
, parent
, -1, "DragAndDrop Grid", size
=(640,480))
90 grid
= SimpleGrid(self
, log
)
94 #---------------------------------------------------------------------------
96 if __name__
== '__main__':
98 app
= wx
.PySimpleApp()
99 frame
= TestFrame(None, sys
.stdout
)
104 #---------------------------------------------------------------------------