]> git.saurik.com Git - wxWidgets.git/blame - demos/dbbrowse/dbgrid.cpp
added missing unixfiles
[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 10//----------------------------------------------------------------------------------------
8907154c 11
b5ffecfc
GT
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
c92b0f9a 14//----------------------------------------------------------------------------------------
b5ffecfc
GT
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
c92b0f9a 18//----------------------------------------------------------------------------------------
b5ffecfc
GT
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
c92b0f9a
MJ
22//----------------------------------------------------------------------------------------
23//-- all #includes that every .cpp needs --- 19990807.mj10777 ----------------
24//----------------------------------------------------------------------------------------
b5ffecfc 25#include "std.h" // sorgsam Pflegen !
c92b0f9a 26//----------------------------------------------------------------------------------------
b5ffecfc 27BEGIN_EVENT_TABLE(DBGrid, wxGrid)
645889ad
GT
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)
c92b0f9a 49END_EVENT_TABLE()
645889ad 50
c92b0f9a
MJ
51//----------------------------------------------------------------------------------------
52// wxListCtrl(parent, id, pos, size, style)
5151c7af 53// wxGrid(parent,wxID_ANY,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
c92b0f9a
MJ
54//----------------------------------------------------------------------------------------
55// DBGrid
56//----------------------------------------------------------------------------------------
57// DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size):
58// wxGrid(parent, id, pos, size)
59DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size, long style):
645889ad 60wxGrid(parent, id, pos, size, style)
b5ffecfc 61{
5151c7af 62 b_EditModus = false;
645889ad 63 //---------------------------------------------------------------------------------------
5d2ac6b8 64 popupMenu1 = new wxMenu;
645889ad 65 popupMenu1->Append(GRID_EDIT, _("Edit Modus"));
5d2ac6b8 66 popupMenu2 = new wxMenu;
645889ad 67 popupMenu2->Append(GRID_BROWSE, _("Browse Modus"));
b5ffecfc 68}
645889ad 69
c92b0f9a 70//----------------------------------------------------------------------------------------
b5ffecfc
GT
71DBGrid::~DBGrid()
72{
645889ad
GT
73 delete popupMenu1;
74 delete popupMenu2;
b5ffecfc 75}
645889ad 76
c92b0f9a 77//----------------------------------------------------------------------------------------
b5ffecfc
GT
78int DBGrid::OnTableView(wxString Table)
79{
645889ad
GT
80 wxStopWatch sw;
81 //---------------------------------------------------------------------------------------
74de91cc 82 int x,y,z;
645889ad
GT
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
74de91cc 95 int ValidTable = x; // Save the Tablenumber
5151c7af 96 (db_Br+i_Which)->OnSelect(Table,false); // Select * from "table"
645889ad
GT
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
74de91cc 101 int i = (db_Br+i_Which)->i_Records; // How many Records are there
645889ad
GT
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 {
daf06bb8 116 Temp0.Printf(_T("%06d"),z+1); SetRowLabelValue(z,Temp0); // Set Row Lable Value
5151c7af 117 (db_Br+i_Which)->OnGetNext((ct_BrowserDB->pTableInf+ValidTable)->numCols,false);
645889ad
GT
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 }
67a99992 122#if wxUSE_STATUSBAR
645889ad
GT
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 }
67a99992 128#endif // wxUSE_STATUSBAR
645889ad
GT
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);
67a99992 132#if wxUSE_STATUSBAR
645889ad 133 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
67a99992 134#endif // wxUSE_STATUSBAR
645889ad
GT
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)
e8c6aabe 143 else
645889ad
GT
144 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
145 //---------------------------------------------------------------------------------------
146Weiter:
147 EnableEditing(b_EditModus); // Deactivate in-place Editing
148 wxEndBusyCursor();
149 //---------------------------------------------------------------------------------------
150 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
151 return 0;
b5ffecfc 152}
645889ad 153
c92b0f9a 154//----------------------------------------------------------------------------------------
74de91cc 155void DBGrid::OnModusEdit(wxCommandEvent& WXUNUSED(event))
b5ffecfc 156{
5151c7af 157 b_EditModus = true; // Needed by PopupMenu
645889ad
GT
158 EnableEditing(b_EditModus); // Activate in-place Editing
159 UpdateDimensions(); // Redraw the Grid
160 // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
b5ffecfc 161}
645889ad 162
c92b0f9a 163//----------------------------------------------------------------------------------------
74de91cc 164void DBGrid::OnModusBrowse(wxCommandEvent& WXUNUSED(event))
b5ffecfc 165{
5151c7af 166 b_EditModus = false; // Needed by PopupMenu
645889ad
GT
167 EnableEditing(b_EditModus); // Deactivate in-place Editing
168 UpdateDimensions(); // Redraw the Grid
169 // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
47677e9b 170}
645889ad 171
47677e9b
MJ
172//----------------------------------------------------------------------------------------
173void DBGrid::OnEditorShown( wxGridEvent& ev )
174{
645889ad
GT
175 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
176 ev.Skip();
47677e9b 177}
645889ad 178
47677e9b
MJ
179//----------------------------------------------------------------------------------------
180void DBGrid::OnEditorHidden( wxGridEvent& ev )
181{
645889ad
GT
182 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
183 ev.Skip();
47677e9b 184}
645889ad
GT
185
186//----------------------------------------------------------------------------------------
47677e9b
MJ
187void DBGrid::OnSelectCell( wxGridEvent& ev )
188{
5d2ac6b8 189 logBuf = wxEmptyString;
daf06bb8
JS
190 logBuf << _T("Selected cell at row ") << ev.GetRow()
191 << _T(" col ") << ev.GetCol();
192 wxLogMessage( _T("%s"), logBuf.c_str() );
645889ad
GT
193 // you must call Skip() if you want the default processing
194 // to occur in wxGrid
195 ev.Skip();
b5ffecfc 196}
645889ad 197
c92b0f9a 198//----------------------------------------------------------------------------------------
b5ffecfc
GT
199void DBGrid::OnMouseMove(wxMouseEvent &event)
200{
645889ad 201 MousePos = event.GetPosition();
b5ffecfc 202}
645889ad 203
c92b0f9a 204//----------------------------------------------------------------------------------------
b5ffecfc
GT
205void DBGrid::OnLabelLeftClick( wxGridEvent& ev )
206{
daf06bb8 207 logBuf = _T("DBGrid::OnLabelLeftClick : ");
645889ad
GT
208 if ( ev.GetRow() != -1 )
209 {
daf06bb8 210 logBuf << _T("row label ") << ev.GetRow();
645889ad
GT
211 }
212 else if ( ev.GetCol() != -1 )
213 {
daf06bb8 214 logBuf << _T("col label ") << ev.GetCol();
645889ad
GT
215 }
216 else
217 {
daf06bb8 218 logBuf << _T("corner label");
645889ad
GT
219 }
220 if ( ev.ShiftDown() )
daf06bb8 221 logBuf << _T(" (shift down)");
645889ad
GT
222
223 // wxLogMessage( "%s", logBuf.c_str() );
daf06bb8 224 logBuf += _T("\n");
645889ad
GT
225 wxLogMessage(logBuf.c_str());
226 ev.Skip();
b5ffecfc 227}
645889ad 228
c92b0f9a 229//----------------------------------------------------------------------------------------
b5ffecfc
GT
230void DBGrid::OnLabelRightClick( wxGridEvent& ev )
231{
645889ad
GT
232 //-------------------------------------------------------
233 if (b_EditModus)
234 PopupMenu(popupMenu2,MousePos.x,MousePos.y);
235 else
236 PopupMenu(popupMenu1,MousePos.x,MousePos.y);
237 //-------------------
daf06bb8 238 logBuf = _T("DBGrid::OnLabelRightClick : ");
645889ad
GT
239 if ( ev.GetRow() != -1 )
240 {
daf06bb8 241 logBuf << _T("row label ") << ev.GetRow();
645889ad
GT
242 }
243 else if ( ev.GetCol() != -1 )
244 {
daf06bb8 245 logBuf << _T("col label ") << ev.GetCol();
645889ad
GT
246 }
247 else
248 {
daf06bb8 249 logBuf << _T("corner label");
645889ad
GT
250 }
251 if ( ev.ShiftDown() )
daf06bb8 252 logBuf << _T(" (shift down)");
645889ad
GT
253
254 // wxLogMessage( "%s", logBuf.c_str() );
daf06bb8 255 logBuf += _T("\n");
645889ad
GT
256 wxLogMessage(logBuf.c_str());
257 ev.Skip();
b5ffecfc 258}
645889ad 259
c92b0f9a 260//----------------------------------------------------------------------------------------
b5ffecfc
GT
261void DBGrid::OnLabelLeftDClick( wxGridEvent& ev )
262{
daf06bb8 263 logBuf = _T("DBGrid::OnLabelLeftDClick : ");
645889ad
GT
264 if ( ev.GetRow() != -1 )
265 {
daf06bb8 266 logBuf << _T("row label ") << ev.GetRow();
645889ad
GT
267 }
268 else if ( ev.GetCol() != -1 )
269 {
daf06bb8 270 logBuf << _T("col label ") << ev.GetCol();
645889ad
GT
271 }
272 else
273 {
daf06bb8 274 logBuf << _T("corner label");
645889ad
GT
275 }
276 if ( ev.ShiftDown() )
daf06bb8 277 logBuf << _T(" (shift down)");
645889ad 278
daf06bb8
JS
279 // wxLogMessage( _T("%s"), logBuf.c_str() );
280 logBuf += _T("\n");
645889ad
GT
281 wxLogMessage(logBuf.c_str());
282 ev.Skip();
b5ffecfc 283}
645889ad 284
c92b0f9a 285//----------------------------------------------------------------------------------------
b5ffecfc
GT
286void DBGrid::OnLabelRightDClick( wxGridEvent& ev )
287{
daf06bb8 288 logBuf = _T("DBGrid::OnLabelRightDClick : ");
645889ad
GT
289 if ( ev.GetRow() != -1 )
290 {
daf06bb8 291 logBuf << _T("row label ") << ev.GetRow();
645889ad
GT
292 }
293 else if ( ev.GetCol() != -1 )
294 {
daf06bb8 295 logBuf << _T("col label ") << ev.GetCol();
645889ad
GT
296 }
297 else
298 {
daf06bb8 299 logBuf << _T("corner label");
645889ad
GT
300 }
301 if ( ev.ShiftDown() )
daf06bb8 302 logBuf << _T(" (shift down)");
645889ad 303 // wxLogMessage( "%s", logBuf.c_str() );
daf06bb8 304 logBuf += _T("\n");
645889ad
GT
305 wxLogMessage(logBuf.c_str());
306 ev.Skip();
b5ffecfc 307}
645889ad 308
c92b0f9a 309//----------------------------------------------------------------------------------------
b5ffecfc
GT
310void DBGrid::OnCellLeftClick( wxGridEvent& ev )
311{
daf06bb8
JS
312 logBuf = _T("DBGrid::OnCellLeftClick : ");
313 logBuf << _T("Cell at row ") << ev.GetRow()
314 << _T(" col ") << ev.GetCol();
645889ad
GT
315 // wxLogMessage( "%s", logBuf.c_str() );
316 // wxMessageBox(logBuf);
daf06bb8 317 logBuf += _T("\n");
645889ad
GT
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();
b5ffecfc 323}
645889ad 324
c92b0f9a 325//----------------------------------------------------------------------------------------
b5ffecfc
GT
326void DBGrid::OnCellRightClick( wxGridEvent& ev )
327{
daf06bb8
JS
328 logBuf = _T("DBGrid::OnCellRightClick : ");
329 logBuf << _T("Cell at row ") << ev.GetRow()
330 << _T(" col ") << ev.GetCol();
645889ad
GT
331 // wxLogMessage( "%s", logBuf.c_str() );
332 // wxMessageBox(logBuf);
daf06bb8 333 logBuf += _T("\n");
645889ad
GT
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();
b5ffecfc 339}
645889ad 340
c92b0f9a 341//----------------------------------------------------------------------------------------
b5ffecfc
GT
342void DBGrid::OnCellLeftDClick( wxGridEvent& ev )
343{
daf06bb8
JS
344 logBuf = _T("DBGrid::OnCellLeftDClick : ");
345 logBuf << _T("Cell at row ") << ev.GetRow()
346 << _T(" col ") << ev.GetCol();
645889ad
GT
347 // wxLogMessage( "%s", logBuf.c_str() );
348 // wxMessageBox(logBuf);
daf06bb8 349 logBuf += _T("\n");
645889ad
GT
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();
b5ffecfc 355}
645889ad 356
c92b0f9a 357//----------------------------------------------------------------------------------------
b5ffecfc
GT
358void DBGrid::OnCellRightDClick( wxGridEvent& ev )
359{
daf06bb8
JS
360 logBuf = _T("DBGrid::OnCellRightDClick : ");
361 logBuf << _T("Cell at row ") << ev.GetRow()
362 << _T(" col ") << ev.GetCol();
645889ad
GT
363 // wxLogMessage( "%s", logBuf.c_str() );
364 // wxMessageBox(logBuf);
daf06bb8 365 logBuf += _T("\n");
645889ad
GT
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();
b5ffecfc 371}
645889ad 372
c92b0f9a 373//----------------------------------------------------------------------------------------
b5ffecfc
GT
374void DBGrid::OnCellChange( wxGridEvent& ev )
375{
daf06bb8
JS
376 logBuf = _T("DBGrid::OnCellChange : ");
377 logBuf << _T("Cell at row ") << ev.GetRow()
378 << _T(" col ") << ev.GetCol();
645889ad
GT
379 // wxLogMessage( "%s", logBuf.c_str() );
380 // wxMessageBox(logBuf);
daf06bb8 381 logBuf += _T("\n");
645889ad
GT
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();
b5ffecfc 387}
645889ad 388
c92b0f9a 389//----------------------------------------------------------------------------------------
b5ffecfc
GT
390void DBGrid::OnRowSize( wxGridSizeEvent& ev )
391{
daf06bb8
JS
392 logBuf = _T("DBGrid::OnRowSize : ");
393 logBuf << _T("Resized row ") << ev.GetRowOrCol();
645889ad 394 // wxLogMessage( "%s", logBuf.c_str() );
daf06bb8 395 logBuf += _T("\n");
645889ad
GT
396 wxLogMessage(logBuf.c_str());
397 ev.Skip();
b5ffecfc 398}
645889ad 399
c92b0f9a 400//----------------------------------------------------------------------------------------
b5ffecfc
GT
401void DBGrid::OnColSize( wxGridSizeEvent& ev )
402{
daf06bb8
JS
403 logBuf = _T("DBGrid::OnColSize : ");
404 logBuf << _T("Resized col ") << ev.GetRowOrCol();
645889ad 405 // wxLogMessage( "%s", logBuf.c_str() );
daf06bb8 406 logBuf += _T("\n");
645889ad
GT
407 wxLogMessage(logBuf.c_str());
408 ev.Skip();
b5ffecfc 409}
645889ad 410
c92b0f9a 411//----------------------------------------------------------------------------------------
b5ffecfc
GT
412void DBGrid::OnRangeSelected( wxGridRangeSelectEvent& ev )
413{
daf06bb8
JS
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");
645889ad
GT
420 // wxLogMessage( "%s", logBuf.c_str() );
421 wxLogMessage(logBuf.c_str());
422 ev.Skip();
b5ffecfc 423}
c92b0f9a 424//----------------------------------------------------------------------------------------