]>
git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbgrid.cpp
1 //----------------------------------------------------------------------------------------
3 // Purpose: wxGrid sample
4 // Author: Mark Johnson
5 // Modified by: 19990929.mj10777 a reuseable DBGrid
8 // Licence: wxWindows license
10 //----------------------------------------------------------------------------------------
11 //-- all #ifdefs that the whole Project needs. -------------------------------------------
12 //----------------------------------------------------------------------------------------
14 #pragma implementation
17 //----------------------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20 //----------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------
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
)
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
)
57 //----------------------------------------------------------------------------------------
58 // wxListCtrl(parent, id, pos, size, style)
59 // wxGrid(parent,-1,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
60 //----------------------------------------------------------------------------------------
62 //----------------------------------------------------------------------------------------
63 // DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size):
64 // wxGrid(parent, id, pos, size)
65 DBGrid::DBGrid(wxWindow
*parent
, const wxWindowID id
,const wxPoint
& pos
,const wxSize
& size
, long style
):
66 wxGrid(parent
, id
, pos
, size
, style
)
69 //---------------------------------------------------------------------------------------
70 popupMenu1
= new wxMenu("");
71 popupMenu1
->Append(GRID_EDIT
, _("Edit Modus"));
72 popupMenu2
= new wxMenu("");
73 popupMenu2
->Append(GRID_BROWSE
, _("Browse Modus"));
76 //----------------------------------------------------------------------------------------
83 //----------------------------------------------------------------------------------------
84 int DBGrid::OnTableView(wxString Table
)
87 //---------------------------------------------------------------------------------------
88 int i
=0,x
,y
,z
, ValidTable
=0;
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
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
101 ValidTable
= x
; // Save the Tablenumber
102 (db_Br
+i_Which
)->OnSelect(Table
,FALSE
); // Select * from "table"
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
107 i
= (db_Br
+i_Which
)->i_Records
; // How many Records are there
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
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
);
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
122 Temp0
.Printf("%06d",z
+1); SetRowLabelValue(z
,Temp0
); // Set Row Lable Value
123 (db_Br
+i_Which
)->OnGetNext((ct_BrowserDB
->pTableInf
+ValidTable
)->numCols
,FALSE
);
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
);
130 Temp0
.Printf(_("-I-> DBGrid::OnTableView(%s) - Record %6d (from %d) has been read."),Table
.c_str(),z
,(db_Br
+i_Which
)->i_Records
);
131 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
133 } // for (z=0;z<(db_Br+i_Which)->i_Records;z++)
134 Temp0
.Printf(_("-I-> DBGrid::OnTableView(%s) - %6d Records have been read. - Time needed : %ld ms"),Table
.c_str(),z
,sw
.Time());
136 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
137 // The Grid has been filled, now leave
139 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
141 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid Column Pointer : Failed"));
142 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
143 } // for (x=0;x<ct_BrowserDB->numTables;x++)
144 } // if (ct_BrowserDB)
146 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
147 //---------------------------------------------------------------------------------------
149 EnableEditing(b_EditModus
); // Deactivate in-place Editing
151 //---------------------------------------------------------------------------------------
152 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
156 //----------------------------------------------------------------------------------------
157 void DBGrid::OnModusEdit(wxCommandEvent
& event
)
159 b_EditModus
= TRUE
; // Needed by PopupMenu
160 EnableEditing(b_EditModus
); // Activate in-place Editing
161 UpdateDimensions(); // Redraw the Grid
162 // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
165 //----------------------------------------------------------------------------------------
166 void DBGrid::OnModusBrowse(wxCommandEvent
& event
)
168 b_EditModus
= FALSE
; // Needed by PopupMenu
169 EnableEditing(b_EditModus
); // Deactivate in-place Editing
170 UpdateDimensions(); // Redraw the Grid
171 // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
174 //----------------------------------------------------------------------------------------
175 void DBGrid::OnEditorShown( wxGridEvent
& ev
)
177 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
181 //----------------------------------------------------------------------------------------
182 void DBGrid::OnEditorHidden( wxGridEvent
& ev
)
184 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
188 //----------------------------------------------------------------------------------------
189 void DBGrid::OnSelectCell( wxGridEvent
& ev
)
192 logBuf
<< "Selected cell at row " << ev
.GetRow()
193 << " col " << ev
.GetCol();
194 wxLogMessage( "%s", logBuf
.c_str() );
195 // you must call Skip() if you want the default processing
196 // to occur in wxGrid
200 //----------------------------------------------------------------------------------------
201 void DBGrid::OnMouseMove(wxMouseEvent
&event
)
203 MousePos
= event
.GetPosition();
206 //----------------------------------------------------------------------------------------
207 void DBGrid::OnLabelLeftClick( wxGridEvent
& ev
)
209 logBuf
= "DBGrid::OnLabelLeftClick : ";
210 if ( ev
.GetRow() != -1 )
212 logBuf
<< "row label " << ev
.GetRow();
214 else if ( ev
.GetCol() != -1 )
216 logBuf
<< "col label " << ev
.GetCol();
220 logBuf
<< "corner label";
222 if ( ev
.ShiftDown() )
223 logBuf
<< " (shift down)";
225 // wxLogMessage( "%s", logBuf.c_str() );
227 wxLogMessage(logBuf
.c_str());
231 //----------------------------------------------------------------------------------------
232 void DBGrid::OnLabelRightClick( wxGridEvent
& ev
)
234 //-------------------------------------------------------
236 PopupMenu(popupMenu2
,MousePos
.x
,MousePos
.y
);
238 PopupMenu(popupMenu1
,MousePos
.x
,MousePos
.y
);
239 //-------------------
240 logBuf
= "DBGrid::OnLabelRightClick : ";
241 if ( ev
.GetRow() != -1 )
243 logBuf
<< "row label " << ev
.GetRow();
245 else if ( ev
.GetCol() != -1 )
247 logBuf
<< "col label " << ev
.GetCol();
251 logBuf
<< "corner label";
253 if ( ev
.ShiftDown() )
254 logBuf
<< " (shift down)";
256 // wxLogMessage( "%s", logBuf.c_str() );
258 wxLogMessage(logBuf
.c_str());
262 //----------------------------------------------------------------------------------------
263 void DBGrid::OnLabelLeftDClick( wxGridEvent
& ev
)
265 logBuf
= "DBGrid::OnLabelLeftDClick : ";
266 if ( ev
.GetRow() != -1 )
268 logBuf
<< "row label " << ev
.GetRow();
270 else if ( ev
.GetCol() != -1 )
272 logBuf
<< "col label " << ev
.GetCol();
276 logBuf
<< "corner label";
278 if ( ev
.ShiftDown() )
279 logBuf
<< " (shift down)";
281 // wxLogMessage( "%s", logBuf.c_str() );
283 wxLogMessage(logBuf
.c_str());
287 //----------------------------------------------------------------------------------------
288 void DBGrid::OnLabelRightDClick( wxGridEvent
& ev
)
290 logBuf
= "DBGrid::OnLabelRightDClick : ";
291 if ( ev
.GetRow() != -1 )
293 logBuf
<< "row label " << ev
.GetRow();
295 else if ( ev
.GetCol() != -1 )
297 logBuf
<< "col label " << ev
.GetCol();
301 logBuf
<< "corner label";
303 if ( ev
.ShiftDown() )
304 logBuf
<< " (shift down)";
305 // wxLogMessage( "%s", logBuf.c_str() );
307 wxLogMessage(logBuf
.c_str());
311 //----------------------------------------------------------------------------------------
312 void DBGrid::OnCellLeftClick( wxGridEvent
& ev
)
314 logBuf
= "DBGrid::OnCellLeftClick : ";
315 logBuf
<< "Cell at row " << ev
.GetRow()
316 << " col " << ev
.GetCol();
317 // wxLogMessage( "%s", logBuf.c_str() );
318 // wxMessageBox(logBuf);
320 wxLogMessage(logBuf
.c_str());
321 // you must call event skip if you want default grid processing
322 // (cell highlighting etc.)
327 //----------------------------------------------------------------------------------------
328 void DBGrid::OnCellRightClick( wxGridEvent
& ev
)
330 logBuf
= "DBGrid::OnCellRightClick : ";
331 logBuf
<< "Cell at row " << ev
.GetRow()
332 << " col " << ev
.GetCol();
333 // wxLogMessage( "%s", logBuf.c_str() );
334 // wxMessageBox(logBuf);
336 wxLogMessage(logBuf
.c_str());
337 // you must call event skip if you want default grid processing
338 // (cell highlighting etc.)
343 //----------------------------------------------------------------------------------------
344 void DBGrid::OnCellLeftDClick( wxGridEvent
& ev
)
346 logBuf
= "DBGrid::OnCellLeftDClick : ";
347 logBuf
<< "Cell at row " << ev
.GetRow()
348 << " col " << ev
.GetCol();
349 // wxLogMessage( "%s", logBuf.c_str() );
350 // wxMessageBox(logBuf);
352 wxLogMessage(logBuf
.c_str());
353 // you must call event skip if you want default grid processing
354 // (cell highlighting etc.)
359 //----------------------------------------------------------------------------------------
360 void DBGrid::OnCellRightDClick( wxGridEvent
& ev
)
362 logBuf
= "DBGrid::OnCellRightDClick : ";
363 logBuf
<< "Cell at row " << ev
.GetRow()
364 << " col " << ev
.GetCol();
365 // wxLogMessage( "%s", logBuf.c_str() );
366 // wxMessageBox(logBuf);
368 wxLogMessage(logBuf
.c_str());
369 // you must call event skip if you want default grid processing
370 // (cell highlighting etc.)
375 //----------------------------------------------------------------------------------------
376 void DBGrid::OnCellChange( wxGridEvent
& ev
)
378 logBuf
= "DBGrid::OnCellChange : ";
379 logBuf
<< "Cell at row " << ev
.GetRow()
380 << " col " << ev
.GetCol();
381 // wxLogMessage( "%s", logBuf.c_str() );
382 // wxMessageBox(logBuf);
384 wxLogMessage(logBuf
.c_str());
385 // you must call event skip if you want default grid processing
386 // (cell highlighting etc.)
391 //----------------------------------------------------------------------------------------
392 void DBGrid::OnRowSize( wxGridSizeEvent
& ev
)
394 logBuf
= "DBGrid::OnRowSize : ";
395 logBuf
<< "Resized row " << ev
.GetRowOrCol();
396 // wxLogMessage( "%s", logBuf.c_str() );
398 wxLogMessage(logBuf
.c_str());
402 //----------------------------------------------------------------------------------------
403 void DBGrid::OnColSize( wxGridSizeEvent
& ev
)
405 logBuf
= "DBGrid::OnColSize : ";
406 logBuf
<< "Resized col " << ev
.GetRowOrCol();
407 // wxLogMessage( "%s", logBuf.c_str() );
409 wxLogMessage(logBuf
.c_str());
413 //----------------------------------------------------------------------------------------
414 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
416 logBuf
= "DBGrid::OnRangeSelected : ";
417 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
418 << " col " << ev
.GetLeftCol()
419 << " to row " << ev
.GetBottomRow()
420 << " col " << ev
.GetRightCol();
422 // wxLogMessage( "%s", logBuf.c_str() );
423 wxLogMessage(logBuf
.c_str());
426 //----------------------------------------------------------------------------------------