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