| 1 | |
| 2 | """ |
| 3 | Example showing how to make a grid a drop target for files. |
| 4 | |
| 5 | """ |
| 6 | |
| 7 | import wx |
| 8 | import wx.grid as gridlib |
| 9 | |
| 10 | #--------------------------------------------------------------------------- |
| 11 | # Set VIRTUAL to 1 to use a virtual grid |
| 12 | VIRTUAL = 1 |
| 13 | #--------------------------------------------------------------------------- |
| 14 | |
| 15 | class GridFileDropTarget(wx.FileDropTarget): |
| 16 | def __init__(self, grid): |
| 17 | wx.FileDropTarget.__init__(self) |
| 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 | |
| 41 | class FooTable(gridlib.PyGridTableBase): |
| 42 | def __init__(self): |
| 43 | gridlib.PyGridTableBase.__init__(self) |
| 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 | |
| 58 | class SimpleGrid(gridlib.Grid): |
| 59 | def __init__(self, parent, log): |
| 60 | gridlib.Grid.__init__(self, parent, -1) |
| 61 | self.log = log |
| 62 | self.moveTo = None |
| 63 | |
| 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: |
| 80 | gridlib.Grid.SetCellValue(self, row, col, value) |
| 81 | |
| 82 | |
| 83 | class TestFrame(wx.Frame): |
| 84 | def __init__(self, parent, log): |
| 85 | wx.Frame.__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 = wx.PySimpleApp() |
| 95 | frame = TestFrame(None, sys.stdout) |
| 96 | frame.Show(True) |
| 97 | app.MainLoop() |
| 98 | |
| 99 | |
| 100 | #--------------------------------------------------------------------------- |
| 101 | |
| 102 | |
| 103 | |
| 104 | |