]>
Commit | Line | Data |
---|---|---|
8fa876ca | 1 | |
1fded56b RD |
2 | """ |
3 | Example showing how to make a grid a drop target for files. | |
4 | ||
5 | """ | |
6 | ||
8fa876ca RD |
7 | import wx |
8 | import wx.grid as gridlib | |
1fded56b RD |
9 | |
10 | #--------------------------------------------------------------------------- | |
11 | # Set VIRTUAL to 1 to use a virtual grid | |
1fded56b | 12 | VIRTUAL = 1 |
8fa876ca | 13 | #--------------------------------------------------------------------------- |
1fded56b | 14 | |
8fa876ca | 15 | class GridFileDropTarget(wx.FileDropTarget): |
1fded56b | 16 | def __init__(self, grid): |
8fa876ca | 17 | wx.FileDropTarget.__init__(self) |
1fded56b RD |
18 | self.grid = grid |
19 | ||
20 | def OnDropFiles(self, x, y, filenames): | |
21 | # the x,y coordinates here are Unscrolled coordinates. They must be changed | |
22 | # to scrolled coordinates. | |
23 | x, y = self.grid.CalcUnscrolledPosition(x, y) | |
24 | ||
25 | # now we need to get the row and column from the grid | |
26 | # but we need to first remove the RowLabel and ColumnLabel | |
27 | # bounding boxes | |
28 | # Why this isn't done for us, I'll never know... | |
29 | x = x - self.grid.GetGridRowLabelWindow().GetRect().width | |
30 | y = y - self.grid.GetGridColLabelWindow().GetRect().height | |
31 | col = self.grid.XToCol(x) | |
32 | row = self.grid.YToRow(y) | |
33 | ||
34 | if row > -1 and col > -1: | |
35 | self.grid.SetCellValue(row, col, filenames[0]) | |
36 | self.grid.AutoSizeColumn(col) | |
37 | self.grid.Refresh() | |
38 | ||
39 | ||
40 | ||
8fa876ca | 41 | class FooTable(gridlib.PyGridTableBase): |
1fded56b | 42 | def __init__(self): |
8fa876ca | 43 | gridlib.PyGridTableBase.__init__(self) |
1fded56b RD |
44 | self.dropTargets = {(0,0):"Drag", |
45 | (1,0):"A", | |
46 | (2,0):"File", | |
47 | (3,0):"To", | |
48 | (4,0):"A", | |
49 | (5,0):"Cell"} | |
50 | def GetNumberCols(self): | |
51 | return 100 | |
52 | def GetNumberRows(self): | |
53 | return 100 | |
54 | def GetValue(self, row, col): | |
55 | return self.dropTargets.get((row, col), "") | |
56 | ||
57 | ||
8fa876ca | 58 | class SimpleGrid(gridlib.Grid): |
1fded56b | 59 | def __init__(self, parent, log): |
8fa876ca | 60 | gridlib.Grid.__init__(self, parent, -1) |
1fded56b RD |
61 | self.log = log |
62 | self.moveTo = None | |
8fa876ca | 63 | |
1fded56b RD |
64 | if VIRTUAL: |
65 | self.table = FooTable() | |
66 | self.SetTable(self.table) | |
67 | else: | |
68 | self.CreateGrid(25, 25) | |
69 | ||
70 | # set the drag and drop target | |
71 | dropTarget = GridFileDropTarget(self) | |
72 | self.SetDropTarget(dropTarget) | |
73 | self.EnableDragRowSize() | |
74 | self.EnableDragColSize() | |
75 | ||
76 | def SetCellValue(self, row, col, value): | |
77 | if VIRTUAL: | |
78 | self.table.dropTargets[row, col] = value | |
79 | else: | |
8fa876ca | 80 | gridlib.Grid.SetCellValue(self, row, col, value) |
1fded56b RD |
81 | |
82 | ||
8fa876ca | 83 | class TestFrame(wx.Frame): |
1fded56b | 84 | def __init__(self, parent, log): |
8fa876ca | 85 | wx.Frame.__init__(self, parent, -1, "DragAndDrop Grid", size=(640,480)) |
1fded56b RD |
86 | grid = SimpleGrid(self, log) |
87 | ||
88 | ||
89 | ||
90 | #--------------------------------------------------------------------------- | |
91 | ||
92 | if __name__ == '__main__': | |
93 | import sys | |
8fa876ca | 94 | app = wx.PySimpleApp() |
1fded56b RD |
95 | frame = TestFrame(None, sys.stdout) |
96 | frame.Show(True) | |
97 | app.MainLoop() | |
98 | ||
99 | ||
100 | #--------------------------------------------------------------------------- | |
101 | ||
102 | ||
103 | ||
104 |