]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbgrid.cpp
Added a Motif-only extra call to UpdateInfoText() just after frame creation
[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 //-- all #ifdefs that the whole Project needs. -------------------------------------------
12 //----------------------------------------------------------------------------------------
13 #ifdef __GNUG__
14 #pragma implementation
15 #pragma interface
16 #endif
17 //----------------------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20 //----------------------------------------------------------------------------------------
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24 //----------------------------------------------------------------------------------------
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28 //----------------------------------------------------------------------------------------
29 //-- all #includes that every .cpp needs --- 19990807.mj10777 ----------------
30 //----------------------------------------------------------------------------------------
31 #include "std.h" // sorgsam Pflegen !
32 //----------------------------------------------------------------------------------------
33 BEGIN_EVENT_TABLE(DBGrid, wxGrid)
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)
55 END_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)
64 DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size, long style):
65 wxGrid(parent, id, pos, size, style)
66 {
67 b_EditModus = FALSE;
68 //---------------------------------------------------------------------------------------
69 popupMenu1 = new wxMenu("");
70 popupMenu1->Append(GRID_EDIT, _("Edit Modus"));
71 popupMenu2 = new wxMenu("");
72 popupMenu2->Append(GRID_BROWSE, _("Browse Modus"));
73 }
74 //----------------------------------------------------------------------------------------
75 DBGrid::~DBGrid()
76 {
77 }
78 //----------------------------------------------------------------------------------------
79 int DBGrid::OnTableView(wxString Table)
80 {
81 wxStopWatch sw;
82 //---------------------------------------------------------------------------------------
83 int i=0,x,y,z, ValidTable=0;
84 wxString Temp0;
85 wxBeginBusyCursor();
86 SetDefaultCellFont(* pDoc->ft_Doc);
87 //---------------------------------------------------------------------------------------
88 ct_BrowserDB = (db_Br+i_Which)->ct_BrowserDB; // Get the DSN Pointer
89 //---------------------------------------------------------------------------------------
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++;
106 // wxLogMessage(_("\n-I-> DBGrid::OnTableView() : Vor CreateGrid"));
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++)
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);
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
136 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid Column Pointer : Failed"));
137 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
138 } // for (x=0;x<ct_BrowserDB->numTables;x++)
139 } // if (ct_BrowserDB)
140 else
141 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
142 //---------------------------------------------------------------------------------------
143 Weiter:
144 EnableEditing(b_EditModus); // Deactivate in-place Editing
145 wxEndBusyCursor();
146 //---------------------------------------------------------------------------------------
147 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
148 return 0;
149 }
150 //----------------------------------------------------------------------------------------
151 void DBGrid::OnModusEdit(wxCommandEvent& event)
152 {
153 b_EditModus = TRUE; // Needed by PopupMenu
154 EnableEditing(b_EditModus); // Activate in-place Editing
155 UpdateDimensions(); // Redraw the Grid
156 // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
157 }
158 //----------------------------------------------------------------------------------------
159 void DBGrid::OnModusBrowse(wxCommandEvent& event)
160 {
161 b_EditModus = FALSE; // Needed by PopupMenu
162 EnableEditing(b_EditModus); // Deactivate in-place Editing
163 UpdateDimensions(); // Redraw the Grid
164 // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
165 }
166 //----------------------------------------------------------------------------------------
167 void DBGrid::OnEditorShown( wxGridEvent& ev )
168 {
169 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
170 ev.Skip();
171 }
172 //----------------------------------------------------------------------------------------
173 void DBGrid::OnEditorHidden( wxGridEvent& ev )
174 {
175 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
176 ev.Skip();
177 }
178 void 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();
187 }
188 //----------------------------------------------------------------------------------------
189 void DBGrid::OnMouseMove(wxMouseEvent &event)
190 {
191 MousePos = event.GetPosition();
192 }
193 //----------------------------------------------------------------------------------------
194 void DBGrid::OnLabelLeftClick( wxGridEvent& ev )
195 {
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();
215 }
216 //----------------------------------------------------------------------------------------
217 void DBGrid::OnLabelRightClick( wxGridEvent& ev )
218 {
219 //-------------------------------------------------------
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();
244 }
245 //----------------------------------------------------------------------------------------
246 void DBGrid::OnLabelLeftDClick( wxGridEvent& ev )
247 {
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();
267 }
268 //----------------------------------------------------------------------------------------
269 void DBGrid::OnLabelRightDClick( wxGridEvent& ev )
270 {
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();
290 }
291 //----------------------------------------------------------------------------------------
292 void DBGrid::OnCellLeftClick( wxGridEvent& ev )
293 {
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();
305 }
306 //----------------------------------------------------------------------------------------
307 void DBGrid::OnCellRightClick( wxGridEvent& ev )
308 {
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();
320 }
321 //----------------------------------------------------------------------------------------
322 void DBGrid::OnCellLeftDClick( wxGridEvent& ev )
323 {
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();
335 }
336 //----------------------------------------------------------------------------------------
337 void DBGrid::OnCellRightDClick( wxGridEvent& ev )
338 {
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();
350 }
351 //----------------------------------------------------------------------------------------
352 void DBGrid::OnCellChange( wxGridEvent& ev )
353 {
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();
365 }
366 //----------------------------------------------------------------------------------------
367 void DBGrid::OnRowSize( wxGridSizeEvent& ev )
368 {
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();
375 }
376 //----------------------------------------------------------------------------------------
377 void DBGrid::OnColSize( wxGridSizeEvent& ev )
378 {
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();
385 }
386 //----------------------------------------------------------------------------------------
387 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent& ev )
388 {
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();
398 }
399 //----------------------------------------------------------------------------------------