]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridDragAndDrop.py
reSWIGged
[wxWidgets.git] / wxPython / demo / GridDragAndDrop.py
CommitLineData
8fa876ca
RD
1# 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for wx namespace
4#
5
1fded56b
RD
6"""
7Example showing how to make a grid a drop target for files.
8
9"""
10
8fa876ca
RD
11import wx
12import wx.grid as gridlib
1fded56b
RD
13
14#---------------------------------------------------------------------------
15# Set VIRTUAL to 1 to use a virtual grid
1fded56b 16VIRTUAL = 1
8fa876ca 17#---------------------------------------------------------------------------
1fded56b 18
8fa876ca 19class GridFileDropTarget(wx.FileDropTarget):
1fded56b 20 def __init__(self, grid):
8fa876ca 21 wx.FileDropTarget.__init__(self)
1fded56b
RD
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
8fa876ca 45class FooTable(gridlib.PyGridTableBase):
1fded56b 46 def __init__(self):
8fa876ca 47 gridlib.PyGridTableBase.__init__(self)
1fded56b
RD
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
8fa876ca 62class SimpleGrid(gridlib.Grid):
1fded56b 63 def __init__(self, parent, log):
8fa876ca 64 gridlib.Grid.__init__(self, parent, -1)
1fded56b
RD
65 self.log = log
66 self.moveTo = None
8fa876ca 67
1fded56b
RD
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:
8fa876ca 84 gridlib.Grid.SetCellValue(self, row, col, value)
1fded56b
RD
85
86
8fa876ca 87class TestFrame(wx.Frame):
1fded56b 88 def __init__(self, parent, log):
8fa876ca 89 wx.Frame.__init__(self, parent, -1, "DragAndDrop Grid", size=(640,480))
1fded56b
RD
90 grid = SimpleGrid(self, log)
91
92
93
94#---------------------------------------------------------------------------
95
96if __name__ == '__main__':
97 import sys
8fa876ca 98 app = wx.PySimpleApp()
1fded56b
RD
99 frame = TestFrame(None, sys.stdout)
100 frame.Show(True)
101 app.MainLoop()
102
103
104#---------------------------------------------------------------------------
105
106
107
108