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