]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridDragAndDrop.py
   3 Example showing how to make a grid a drop target for files. 
   8 import  wx
.grid 
as gridlib
 
  10 #--------------------------------------------------------------------------- 
  11 # Set VIRTUAL to 1 to use a virtual grid 
  13 #--------------------------------------------------------------------------- 
  15 class GridFileDropTarget(wx
.FileDropTarget
): 
  16     def __init__(self
, grid
): 
  17         wx
.FileDropTarget
.__init
__(self
) 
  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
) 
  25         # now we need to get the row and column from the grid 
  26         # but we need to first remove the RowLabel and ColumnLabel 
  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
) 
  34         if row 
> -1 and col 
> -1: 
  35             self
.grid
.SetCellValue(row
, col
, filenames
[0]) 
  36             self
.grid
.AutoSizeColumn(col
) 
  41 class FooTable(gridlib
.PyGridTableBase
): 
  43         gridlib
.PyGridTableBase
.__init
__(self
) 
  44         self
.dropTargets 
= {(0,0):"Drag", 
  50     def GetNumberCols(self
): 
  52     def GetNumberRows(self
): 
  54     def GetValue(self
, row
, col
): 
  55         return self
.dropTargets
.get((row
, col
), "") 
  58 class SimpleGrid(gridlib
.Grid
): 
  59     def __init__(self
, parent
, log
): 
  60         gridlib
.Grid
.__init
__(self
, parent
, -1) 
  65             self
.table 
= FooTable() 
  66             self
.SetTable(self
.table
) 
  68             self
.CreateGrid(25, 25) 
  70         # set the drag and drop target 
  71         dropTarget 
= GridFileDropTarget(self
) 
  72         self
.SetDropTarget(dropTarget
) 
  73         self
.EnableDragRowSize() 
  74         self
.EnableDragColSize() 
  76     def SetCellValue(self
, row
, col
, value
): 
  78             self
.table
.dropTargets
[row
, col
] = value
 
  80             gridlib
.Grid
.SetCellValue(self
, row
, col
, value
) 
  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
) 
  90 #--------------------------------------------------------------------------- 
  92 if __name__ 
== '__main__': 
  94     app 
= wx
.PySimpleApp() 
  95     frame 
= TestFrame(None, sys
.stdout
) 
 100 #---------------------------------------------------------------------------