]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbgrid.cpp
Change Fit() back to using SetSize, not SetClientSize.
[wxWidgets.git] / demos / dbbrowse / dbgrid.cpp
1 //----------------------------------------------------------------------------------------
2 // Name: DBGrid.cpp
3 // Purpose: wxGrid sample
4 // Author: Mark Johnson
5 // Modified by: 19990929.mj10777 a reuseable DBGrid
6 // Created: 19990929
7 // Copyright: (c)
8 // Licence: wxWindows license
9 // RCS-ID: $Id$
10 //----------------------------------------------------------------------------------------
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14 //----------------------------------------------------------------------------------------
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18 //----------------------------------------------------------------------------------------
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22 //----------------------------------------------------------------------------------------
23 //-- all #includes that every .cpp needs --- 19990807.mj10777 ----------------
24 //----------------------------------------------------------------------------------------
25 #include "std.h" // sorgsam Pflegen !
26 //----------------------------------------------------------------------------------------
27 BEGIN_EVENT_TABLE(DBGrid, wxGrid)
28 EVT_MOTION (DBGrid::OnMouseMove)
29 // DBGrid
30 // ------------
31 EVT_GRID_CELL_CHANGE( DBGrid::OnCellChange )
32 EVT_GRID_CELL_LEFT_CLICK( DBGrid::OnCellLeftClick )
33 EVT_GRID_CELL_LEFT_DCLICK( DBGrid::OnCellLeftDClick )
34 EVT_GRID_CELL_RIGHT_CLICK( DBGrid::OnCellRightClick )
35 EVT_GRID_CELL_RIGHT_DCLICK( DBGrid::OnCellRightDClick )
36 // EVT_GRID_COL_SIZE( DBGrid::OnColSize )
37 // EVT_GRID_ROW_SIZE( DBGrid::OnRowSize )
38 EVT_GRID_EDITOR_SHOWN( DBGrid::OnEditorShown )
39 EVT_GRID_EDITOR_HIDDEN( DBGrid::OnEditorHidden )
40 EVT_GRID_LABEL_LEFT_CLICK( DBGrid::OnLabelLeftClick )
41 EVT_GRID_LABEL_LEFT_DCLICK( DBGrid::OnLabelLeftDClick )
42 EVT_GRID_LABEL_RIGHT_CLICK( DBGrid::OnLabelRightClick )
43 EVT_GRID_LABEL_RIGHT_DCLICK( DBGrid::OnLabelRightDClick )
44 EVT_GRID_RANGE_SELECT( DBGrid::OnRangeSelected )
45 EVT_GRID_ROW_SIZE( DBGrid::OnRowSize )
46 EVT_GRID_SELECT_CELL( DBGrid::OnSelectCell )
47 EVT_MENU(GRID_EDIT,DBGrid::OnModusEdit)
48 EVT_MENU(GRID_BROWSE,DBGrid::OnModusBrowse)
49 END_EVENT_TABLE()
50
51 //----------------------------------------------------------------------------------------
52 // wxListCtrl(parent, id, pos, size, style)
53 // wxGrid(parent,wxID_ANY,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
54 //----------------------------------------------------------------------------------------
55 // DBGrid
56 //----------------------------------------------------------------------------------------
57 // DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size):
58 // wxGrid(parent, id, pos, size)
59 DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size, long style):
60 wxGrid(parent, id, pos, size, style)
61 {
62 b_EditModus = false;
63 //---------------------------------------------------------------------------------------
64 popupMenu1 = new wxMenu;
65 popupMenu1->Append(GRID_EDIT, _("Edit Modus"));
66 popupMenu2 = new wxMenu;
67 popupMenu2->Append(GRID_BROWSE, _("Browse Modus"));
68 }
69
70 //----------------------------------------------------------------------------------------
71 DBGrid::~DBGrid()
72 {
73 delete popupMenu1;
74 delete popupMenu2;
75 }
76
77 //----------------------------------------------------------------------------------------
78 int DBGrid::OnTableView(wxString Table)
79 {
80 wxStopWatch sw;
81 //---------------------------------------------------------------------------------------
82 int x,y,z;
83 wxString Temp0;
84 wxBeginBusyCursor();
85 SetDefaultCellFont(* pDoc->ft_Doc);
86 //---------------------------------------------------------------------------------------
87 ct_BrowserDB = (db_Br+i_Which)->ct_BrowserDB; // Get the DSN Pointer
88 //---------------------------------------------------------------------------------------
89 if (ct_BrowserDB) // Valid pointer (!= NULL) ?
90 { // Pointer is Valid, use the wxDatabase Information
91 for (x=0;x<ct_BrowserDB->numTables;x++) // go through the Tables
92 {
93 if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableName,Table)) // is this our Table ?
94 { // Yes, the Data of this Table shall be put into the Grid
95 int ValidTable = x; // Save the Tablenumber
96 (db_Br+i_Which)->OnSelect(Table,false); // Select * from "table"
97 // Set the local Pointer to the Column Information we are going to use
98 (db_Br+i_Which)->cl_BrowserDB = (ct_BrowserDB->pTableInf+x)->pColInf;
99 if ((ct_BrowserDB->pTableInf+x)->pColInf) // Valid pointer (!= NULL) ?
100 { // Pointer is Valid, Column Informationen sind Vorhanden
101 int i = (db_Br+i_Which)->i_Records; // How many Records are there
102 (db_Br+i_Which)->i_Which = ValidTable; // Still used ???? mj10777
103 if (i == 0) // If the Table is empty, then show one empty row
104 i++;
105 // wxLogMessage(_("\n-I-> DBGrid::OnTableView() : Vor CreateGrid"));
106 CreateGrid(i,(ct_BrowserDB->pTableInf+x)->numCols); // Records , Columns
107 for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++) // Loop through the Fields
108 { // The Field / Column name is used here as Row Titel
109 SetColLabelValue(y,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
110 SetColSize(y,95);
111 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
112 SetColSize(((ct_BrowserDB->pTableInf+x)->numCols-1),120); // Make the last Column Wider
113 // The Grid has been created, now fill it
114 for (z=0;z<(db_Br+i_Which)->i_Records;z++) // Loop through the Records
115 {
116 Temp0.Printf(_T("%06d"),z+1); SetRowLabelValue(z,Temp0); // Set Row Lable Value
117 (db_Br+i_Which)->OnGetNext((ct_BrowserDB->pTableInf+ValidTable)->numCols,false);
118 for (y=0;y<(ct_BrowserDB->pTableInf+ValidTable)->numCols;y++) // Loop through the Fields
119 { // BrowserDB::OnGetNext Formats the field Value into tablename
120 SetCellValue(z, y,((db_Br+i_Which)->cl_BrowserDB+y)->tableName);
121 }
122 #if wxUSE_STATUSBAR
123 if (z % 50 == 0)
124 {
125 Temp0.Printf(_("-I-> DBGrid::OnTableView(%s) - Record %6d (from %d) has been read."),Table.c_str(),z,(db_Br+i_Which)->i_Records);
126 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
127 }
128 #endif // wxUSE_STATUSBAR
129 } // for (z=0;z<(db_Br+i_Which)->i_Records;z++)
130 Temp0.Printf(_("-I-> DBGrid::OnTableView(%s) - %6d Records have been read. - Time needed : %ld ms"),Table.c_str(),z,sw.Time());
131 wxLogMessage(Temp0);
132 #if wxUSE_STATUSBAR
133 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
134 #endif // wxUSE_STATUSBAR
135 // The Grid has been filled, now leave
136 goto Weiter;
137 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
138 else
139 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid Column Pointer : Failed"));
140 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
141 } // for (x=0;x<ct_BrowserDB->numTables;x++)
142 } // if (ct_BrowserDB)
143 else
144 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
145 //---------------------------------------------------------------------------------------
146 Weiter:
147 EnableEditing(b_EditModus); // Deactivate in-place Editing
148 wxEndBusyCursor();
149 //---------------------------------------------------------------------------------------
150 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
151 return 0;
152 }
153
154 //----------------------------------------------------------------------------------------
155 void DBGrid::OnModusEdit(wxCommandEvent& WXUNUSED(event))
156 {
157 b_EditModus = true; // Needed by PopupMenu
158 EnableEditing(b_EditModus); // Activate in-place Editing
159 UpdateDimensions(); // Redraw the Grid
160 // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
161 }
162
163 //----------------------------------------------------------------------------------------
164 void DBGrid::OnModusBrowse(wxCommandEvent& WXUNUSED(event))
165 {
166 b_EditModus = false; // Needed by PopupMenu
167 EnableEditing(b_EditModus); // Deactivate in-place Editing
168 UpdateDimensions(); // Redraw the Grid
169 // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
170 }
171
172 //----------------------------------------------------------------------------------------
173 void DBGrid::OnEditorShown( wxGridEvent& ev )
174 {
175 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
176 ev.Skip();
177 }
178
179 //----------------------------------------------------------------------------------------
180 void DBGrid::OnEditorHidden( wxGridEvent& ev )
181 {
182 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
183 ev.Skip();
184 }
185
186 //----------------------------------------------------------------------------------------
187 void DBGrid::OnSelectCell( wxGridEvent& ev )
188 {
189 logBuf = wxEmptyString;
190 logBuf << _T("Selected cell at row ") << ev.GetRow()
191 << _T(" col ") << ev.GetCol();
192 wxLogMessage( _T("%s"), logBuf.c_str() );
193 // you must call Skip() if you want the default processing
194 // to occur in wxGrid
195 ev.Skip();
196 }
197
198 //----------------------------------------------------------------------------------------
199 void DBGrid::OnMouseMove(wxMouseEvent &event)
200 {
201 MousePos = event.GetPosition();
202 }
203
204 //----------------------------------------------------------------------------------------
205 void DBGrid::OnLabelLeftClick( wxGridEvent& ev )
206 {
207 logBuf = _T("DBGrid::OnLabelLeftClick : ");
208 if ( ev.GetRow() != -1 )
209 {
210 logBuf << _T("row label ") << ev.GetRow();
211 }
212 else if ( ev.GetCol() != -1 )
213 {
214 logBuf << _T("col label ") << ev.GetCol();
215 }
216 else
217 {
218 logBuf << _T("corner label");
219 }
220 if ( ev.ShiftDown() )
221 logBuf << _T(" (shift down)");
222
223 // wxLogMessage( "%s", logBuf.c_str() );
224 logBuf += _T("\n");
225 wxLogMessage(logBuf.c_str());
226 ev.Skip();
227 }
228
229 //----------------------------------------------------------------------------------------
230 void DBGrid::OnLabelRightClick( wxGridEvent& ev )
231 {
232 //-------------------------------------------------------
233 if (b_EditModus)
234 PopupMenu(popupMenu2,MousePos.x,MousePos.y);
235 else
236 PopupMenu(popupMenu1,MousePos.x,MousePos.y);
237 //-------------------
238 logBuf = _T("DBGrid::OnLabelRightClick : ");
239 if ( ev.GetRow() != -1 )
240 {
241 logBuf << _T("row label ") << ev.GetRow();
242 }
243 else if ( ev.GetCol() != -1 )
244 {
245 logBuf << _T("col label ") << ev.GetCol();
246 }
247 else
248 {
249 logBuf << _T("corner label");
250 }
251 if ( ev.ShiftDown() )
252 logBuf << _T(" (shift down)");
253
254 // wxLogMessage( "%s", logBuf.c_str() );
255 logBuf += _T("\n");
256 wxLogMessage(logBuf.c_str());
257 ev.Skip();
258 }
259
260 //----------------------------------------------------------------------------------------
261 void DBGrid::OnLabelLeftDClick( wxGridEvent& ev )
262 {
263 logBuf = _T("DBGrid::OnLabelLeftDClick : ");
264 if ( ev.GetRow() != -1 )
265 {
266 logBuf << _T("row label ") << ev.GetRow();
267 }
268 else if ( ev.GetCol() != -1 )
269 {
270 logBuf << _T("col label ") << ev.GetCol();
271 }
272 else
273 {
274 logBuf << _T("corner label");
275 }
276 if ( ev.ShiftDown() )
277 logBuf << _T(" (shift down)");
278
279 // wxLogMessage( _T("%s"), logBuf.c_str() );
280 logBuf += _T("\n");
281 wxLogMessage(logBuf.c_str());
282 ev.Skip();
283 }
284
285 //----------------------------------------------------------------------------------------
286 void DBGrid::OnLabelRightDClick( wxGridEvent& ev )
287 {
288 logBuf = _T("DBGrid::OnLabelRightDClick : ");
289 if ( ev.GetRow() != -1 )
290 {
291 logBuf << _T("row label ") << ev.GetRow();
292 }
293 else if ( ev.GetCol() != -1 )
294 {
295 logBuf << _T("col label ") << ev.GetCol();
296 }
297 else
298 {
299 logBuf << _T("corner label");
300 }
301 if ( ev.ShiftDown() )
302 logBuf << _T(" (shift down)");
303 // wxLogMessage( "%s", logBuf.c_str() );
304 logBuf += _T("\n");
305 wxLogMessage(logBuf.c_str());
306 ev.Skip();
307 }
308
309 //----------------------------------------------------------------------------------------
310 void DBGrid::OnCellLeftClick( wxGridEvent& ev )
311 {
312 logBuf = _T("DBGrid::OnCellLeftClick : ");
313 logBuf << _T("Cell at row ") << ev.GetRow()
314 << _T(" col ") << ev.GetCol();
315 // wxLogMessage( "%s", logBuf.c_str() );
316 // wxMessageBox(logBuf);
317 logBuf += _T("\n");
318 wxLogMessage(logBuf.c_str());
319 // you must call event skip if you want default grid processing
320 // (cell highlighting etc.)
321 //
322 ev.Skip();
323 }
324
325 //----------------------------------------------------------------------------------------
326 void DBGrid::OnCellRightClick( wxGridEvent& ev )
327 {
328 logBuf = _T("DBGrid::OnCellRightClick : ");
329 logBuf << _T("Cell at row ") << ev.GetRow()
330 << _T(" col ") << ev.GetCol();
331 // wxLogMessage( "%s", logBuf.c_str() );
332 // wxMessageBox(logBuf);
333 logBuf += _T("\n");
334 wxLogMessage(logBuf.c_str());
335 // you must call event skip if you want default grid processing
336 // (cell highlighting etc.)
337 //
338 ev.Skip();
339 }
340
341 //----------------------------------------------------------------------------------------
342 void DBGrid::OnCellLeftDClick( wxGridEvent& ev )
343 {
344 logBuf = _T("DBGrid::OnCellLeftDClick : ");
345 logBuf << _T("Cell at row ") << ev.GetRow()
346 << _T(" col ") << ev.GetCol();
347 // wxLogMessage( "%s", logBuf.c_str() );
348 // wxMessageBox(logBuf);
349 logBuf += _T("\n");
350 wxLogMessage(logBuf.c_str());
351 // you must call event skip if you want default grid processing
352 // (cell highlighting etc.)
353 //
354 ev.Skip();
355 }
356
357 //----------------------------------------------------------------------------------------
358 void DBGrid::OnCellRightDClick( wxGridEvent& ev )
359 {
360 logBuf = _T("DBGrid::OnCellRightDClick : ");
361 logBuf << _T("Cell at row ") << ev.GetRow()
362 << _T(" col ") << ev.GetCol();
363 // wxLogMessage( "%s", logBuf.c_str() );
364 // wxMessageBox(logBuf);
365 logBuf += _T("\n");
366 wxLogMessage(logBuf.c_str());
367 // you must call event skip if you want default grid processing
368 // (cell highlighting etc.)
369 //
370 ev.Skip();
371 }
372
373 //----------------------------------------------------------------------------------------
374 void DBGrid::OnCellChange( wxGridEvent& ev )
375 {
376 logBuf = _T("DBGrid::OnCellChange : ");
377 logBuf << _T("Cell at row ") << ev.GetRow()
378 << _T(" col ") << ev.GetCol();
379 // wxLogMessage( "%s", logBuf.c_str() );
380 // wxMessageBox(logBuf);
381 logBuf += _T("\n");
382 wxLogMessage(logBuf.c_str());
383 // you must call event skip if you want default grid processing
384 // (cell highlighting etc.)
385 //
386 ev.Skip();
387 }
388
389 //----------------------------------------------------------------------------------------
390 void DBGrid::OnRowSize( wxGridSizeEvent& ev )
391 {
392 logBuf = _T("DBGrid::OnRowSize : ");
393 logBuf << _T("Resized row ") << ev.GetRowOrCol();
394 // wxLogMessage( "%s", logBuf.c_str() );
395 logBuf += _T("\n");
396 wxLogMessage(logBuf.c_str());
397 ev.Skip();
398 }
399
400 //----------------------------------------------------------------------------------------
401 void DBGrid::OnColSize( wxGridSizeEvent& ev )
402 {
403 logBuf = _T("DBGrid::OnColSize : ");
404 logBuf << _T("Resized col ") << ev.GetRowOrCol();
405 // wxLogMessage( "%s", logBuf.c_str() );
406 logBuf += _T("\n");
407 wxLogMessage(logBuf.c_str());
408 ev.Skip();
409 }
410
411 //----------------------------------------------------------------------------------------
412 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent& ev )
413 {
414 logBuf = _T("DBGrid::OnRangeSelected : ");
415 logBuf << _T("Selected cells from row ") << ev.GetTopRow()
416 << _T(" col ") << ev.GetLeftCol()
417 << _T(" to row ") << ev.GetBottomRow()
418 << _T(" col ") << ev.GetRightCol();
419 logBuf += _T("\n");
420 // wxLogMessage( "%s", logBuf.c_str() );
421 wxLogMessage(logBuf.c_str());
422 ev.Skip();
423 }
424 //----------------------------------------------------------------------------------------