]>
Commit | Line | Data |
---|---|---|
1 | """ | |
2 | Example showing how to make a grid a drop target for files. | |
3 | ||
4 | """ | |
5 | ||
6 | from wxPython.wx import * | |
7 | from wxPython.grid import * | |
8 | ||
9 | #--------------------------------------------------------------------------- | |
10 | # Set VIRTUAL to 1 to use a virtual grid | |
11 | ||
12 | VIRTUAL = 1 | |
13 | ||
14 | class GridFileDropTarget(wxFileDropTarget): | |
15 | def __init__(self, grid): | |
16 | wxFileDropTarget.__init__(self) | |
17 | self.grid = grid | |
18 | ||
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) | |
23 | ||
24 | # now we need to get the row and column from the grid | |
25 | # but we need to first remove the RowLabel and ColumnLabel | |
26 | # bounding boxes | |
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) | |
32 | ||
33 | if row > -1 and col > -1: | |
34 | self.grid.SetCellValue(row, col, filenames[0]) | |
35 | self.grid.AutoSizeColumn(col) | |
36 | self.grid.Refresh() | |
37 | ||
38 | ||
39 | ||
40 | class FooTable(wxPyGridTableBase): | |
41 | def __init__(self): | |
42 | wxPyGridTableBase.__init__(self) | |
43 | self.dropTargets = {(0,0):"Drag", | |
44 | (1,0):"A", | |
45 | (2,0):"File", | |
46 | (3,0):"To", | |
47 | (4,0):"A", | |
48 | (5,0):"Cell"} | |
49 | def GetNumberCols(self): | |
50 | return 100 | |
51 | def GetNumberRows(self): | |
52 | return 100 | |
53 | def GetValue(self, row, col): | |
54 | return self.dropTargets.get((row, col), "") | |
55 | ||
56 | ||
57 | ||
58 | class SimpleGrid(wxGrid): | |
59 | def __init__(self, parent, log): | |
60 | wxGrid.__init__(self, parent, -1) | |
61 | self.log = log | |
62 | self.moveTo = None | |
63 | if VIRTUAL: | |
64 | self.table = FooTable() | |
65 | self.SetTable(self.table) | |
66 | else: | |
67 | self.CreateGrid(25, 25) | |
68 | ||
69 | # set the drag and drop target | |
70 | dropTarget = GridFileDropTarget(self) | |
71 | self.SetDropTarget(dropTarget) | |
72 | self.EnableDragRowSize() | |
73 | self.EnableDragColSize() | |
74 | ||
75 | def SetCellValue(self, row, col, value): | |
76 | if VIRTUAL: | |
77 | self.table.dropTargets[row, col] = value | |
78 | else: | |
79 | wxGrid.SetCellValue(self, row, col, value) | |
80 | ||
81 | ||
82 | ||
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) | |
87 | ||
88 | ||
89 | ||
90 | #--------------------------------------------------------------------------- | |
91 | ||
92 | if __name__ == '__main__': | |
93 | import sys | |
94 | app = wxPySimpleApp() | |
95 | frame = TestFrame(None, sys.stdout) | |
96 | frame.Show(True) | |
97 | app.MainLoop() | |
98 | ||
99 | ||
100 | #--------------------------------------------------------------------------- | |
101 | ||
102 | ||
103 | ||
104 |