23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
24 |
25 import wx |
25 import wx |
26 import wx.grid |
26 import wx.grid |
27 |
27 |
|
28 |
28 class CustomGrid(wx.grid.Grid): |
29 class CustomGrid(wx.grid.Grid): |
29 |
30 |
30 def __init__(self, *args, **kwargs): |
31 def __init__(self, *args, **kwargs): |
31 wx.grid.Grid.__init__(self, *args, **kwargs) |
32 wx.grid.Grid.__init__(self, *args, **kwargs) |
32 |
33 |
33 self.Editable = True |
34 self.Editable = True |
34 |
35 |
35 self.AddButton = None |
36 self.AddButton = None |
36 self.DeleteButton = None |
37 self.DeleteButton = None |
37 self.UpButton = None |
38 self.UpButton = None |
38 self.DownButton = None |
39 self.DownButton = None |
39 |
40 |
40 self.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
41 self.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
41 self.SetLabelFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
42 self.SetLabelFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
42 self.SetSelectionBackground(wx.WHITE) |
43 self.SetSelectionBackground(wx.WHITE) |
43 self.SetSelectionForeground(wx.BLACK) |
44 self.SetSelectionForeground(wx.BLACK) |
44 self.DisableDragRowSize() |
45 self.DisableDragRowSize() |
45 |
46 |
46 self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell) |
47 self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell) |
47 self.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden) |
48 self.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden) |
48 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
49 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
49 |
50 |
50 def SetFocus(self): |
51 def SetFocus(self): |
51 if self: |
52 if self: |
52 wx.grid.Grid.SetFocus(self) |
53 wx.grid.Grid.SetFocus(self) |
53 |
54 |
54 def SetDefaultValue(self, default_value): |
55 def SetDefaultValue(self, default_value): |
55 self.DefaultValue = default_value |
56 self.DefaultValue = default_value |
56 |
57 |
57 def SetEditable(self, editable=True): |
58 def SetEditable(self, editable=True): |
58 self.Editable = editable |
59 self.Editable = editable |
59 self.RefreshButtons() |
60 self.RefreshButtons() |
60 |
61 |
61 def SetButtons(self, buttons): |
62 def SetButtons(self, buttons): |
62 for name in ["Add", "Delete", "Up", "Down"]: |
63 for name in ["Add", "Delete", "Up", "Down"]: |
63 button = buttons.get(name, None) |
64 button = buttons.get(name, None) |
64 setattr(self, "%sButton" % name, button) |
65 setattr(self, "%sButton" % name, button) |
65 if button is not None: |
66 if button is not None: |
66 button.Bind(wx.EVT_BUTTON, getattr(self, "On%sButton" % name)) |
67 button.Bind(wx.EVT_BUTTON, getattr(self, "On%sButton" % name)) |
67 |
68 |
68 def RefreshButtons(self): |
69 def RefreshButtons(self): |
69 if self: |
70 if self: |
70 rows = self.Table.GetNumberRows() |
71 rows = self.Table.GetNumberRows() |
71 row = self.GetGridCursorRow() |
72 row = self.GetGridCursorRow() |
72 if self.AddButton is not None: |
73 if self.AddButton is not None: |
75 self.DeleteButton.Enable(self.Editable and rows > 0) |
76 self.DeleteButton.Enable(self.Editable and rows > 0) |
76 if self.UpButton is not None: |
77 if self.UpButton is not None: |
77 self.UpButton.Enable(self.Editable and row > 0) |
78 self.UpButton.Enable(self.Editable and row > 0) |
78 if self.DownButton is not None: |
79 if self.DownButton is not None: |
79 self.DownButton.Enable(self.Editable and 0 <= row < rows - 1) |
80 self.DownButton.Enable(self.Editable and 0 <= row < rows - 1) |
80 |
81 |
81 def CloseEditControl(self): |
82 def CloseEditControl(self): |
82 row, col = self.GetGridCursorRow(), self.GetGridCursorCol() |
83 row, col = self.GetGridCursorRow(), self.GetGridCursorCol() |
83 if row != -1 and col != -1: |
84 if row != -1 and col != -1: |
84 self.SetGridCursor(row, col) |
85 self.SetGridCursor(row, col) |
85 |
86 |
117 new_row = self.Table.MoveRow(row, move) |
118 new_row = self.Table.MoveRow(row, move) |
118 if new_row != row: |
119 if new_row != row: |
119 self.Table.ResetView(self) |
120 self.Table.ResetView(self) |
120 if new_row != row: |
121 if new_row != row: |
121 self.SetSelectedCell(new_row, col) |
122 self.SetSelectedCell(new_row, col) |
122 |
123 |
123 def SetSelectedCell(self, row, col): |
124 def SetSelectedCell(self, row, col): |
124 self.SetGridCursor(row, col) |
125 self.SetGridCursor(row, col) |
125 self.MakeCellVisible(row, col) |
126 self.MakeCellVisible(row, col) |
126 self.RefreshButtons() |
127 self.RefreshButtons() |
127 |
128 |
128 def OnAddButton(self, event): |
129 def OnAddButton(self, event): |
129 self.AddRow() |
130 self.AddRow() |
130 self.SetFocus() |
131 self.SetFocus() |
131 event.Skip() |
132 event.Skip() |
132 |
133 |
133 def OnDeleteButton(self, event): |
134 def OnDeleteButton(self, event): |
134 self.DeleteRow() |
135 self.DeleteRow() |
135 self.SetFocus() |
136 self.SetFocus() |
136 event.Skip() |
137 event.Skip() |
137 |
138 |
138 def OnUpButton(self, event): |
139 def OnUpButton(self, event): |
139 self.MoveRow(self.GetGridCursorRow(), -1) |
140 self.MoveRow(self.GetGridCursorRow(), -1) |
140 self.SetFocus() |
141 self.SetFocus() |
141 event.Skip() |
142 event.Skip() |
142 |
143 |
143 def OnDownButton(self, event): |
144 def OnDownButton(self, event): |
144 self.MoveRow(self.GetGridCursorRow(), 1) |
145 self.MoveRow(self.GetGridCursorRow(), 1) |
145 self.SetFocus() |
146 self.SetFocus() |
146 event.Skip() |
147 event.Skip() |
147 |
148 |