]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridDragAndDrop.py
Added SF Patch#
[wxWidgets.git] / wxPython / demo / GridDragAndDrop.py
1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 """
7 Example showing how to make a grid a drop target for files.
8
9 """
10
11 import wx
12 import wx.grid as gridlib
13
14 #---------------------------------------------------------------------------
15 # Set VIRTUAL to 1 to use a virtual grid
16 VIRTUAL = 1
17 #---------------------------------------------------------------------------
18
19 class GridFileDropTarget(wx.FileDropTarget):
20 def __init__(self, grid):
21 wx.FileDropTarget.__init__(self)
22 self.grid = grid
23
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)
28
29 # now we need to get the row and column from the grid
30 # but we need to first remove the RowLabel and ColumnLabel
31 # bounding boxes
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)
37
38 if row > -1 and col > -1:
39 self.grid.SetCellValue(row, col, filenames[0])
40 self.grid.AutoSizeColumn(col)
41 self.grid.Refresh()
42
43
44
45 class FooTable(gridlib.PyGridTableBase):
46 def __init__(self):
47 gridlib.PyGridTableBase.__init__(self)
48 self.dropTargets = {(0,0):"Drag",
49 (1,0):"A",
50 (2,0):"File",
51 (3,0):"To",
52 (4,0):"A",
53 (5,0):"Cell"}
54 def GetNumberCols(self):
55 return 100
56 def GetNumberRows(self):
57 return 100
58 def GetValue(self, row, col):
59 return self.dropTargets.get((row, col), "")
60
61
62 class SimpleGrid(gridlib.Grid):
63 def __init__(self, parent, log):
64 gridlib.Grid.__init__(self, parent, -1)
65 self.log = log
66 self.moveTo = None
67
68 if VIRTUAL:
69 self.table = FooTable()
70 self.SetTable(self.table)
71 else:
72 self.CreateGrid(25, 25)
73
74 # set the drag and drop target
75 dropTarget = GridFileDropTarget(self)
76 self.SetDropTarget(dropTarget)
77 self.EnableDragRowSize()
78 self.EnableDragColSize()
79
80 def SetCellValue(self, row, col, value):
81 if VIRTUAL:
82 self.table.dropTargets[row, col] = value
83 else:
84 gridlib.Grid.SetCellValue(self, row, col, value)
85
86
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)
91
92
93
94 #---------------------------------------------------------------------------
95
96 if __name__ == '__main__':
97 import sys
98 app = wx.PySimpleApp()
99 frame = TestFrame(None, sys.stdout)
100 frame.Show(True)
101 app.MainLoop()
102
103
104 #---------------------------------------------------------------------------
105
106
107
108