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