]>
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 //----------------------------------------------------------------------------------------
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14 //----------------------------------------------------------------------------------------
18 //----------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------
23 //-- all #includes that every .cpp needs --- 19990807.mj10777 ----------------
24 //----------------------------------------------------------------------------------------
25 #include "std.h" // sorgsam Pflegen !
26 //----------------------------------------------------------------------------------------
27 BEGIN_EVENT_TABLE(DBGrid
, wxGrid
)
28 EVT_MOTION (DBGrid::OnMouseMove
)
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
)
51 //----------------------------------------------------------------------------------------
52 // wxListCtrl(parent, id, pos, size, style)
53 // wxGrid(parent,wxID_ANY,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
54 //----------------------------------------------------------------------------------------
56 //----------------------------------------------------------------------------------------
57 // DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size):
58 // wxGrid(parent, id, pos, size)
59 DBGrid::DBGrid(wxWindow
*parent
, const wxWindowID id
,const wxPoint
& pos
,const wxSize
& size
, long style
):
60 wxGrid(parent
, id
, pos
, size
, style
)
63 //---------------------------------------------------------------------------------------
64 popupMenu1
= new wxMenu
;
65 popupMenu1
->Append(GRID_EDIT
, _("Edit Modus"));
66 popupMenu2
= new wxMenu
;
67 popupMenu2
->Append(GRID_BROWSE
, _("Browse Modus"));
70 //----------------------------------------------------------------------------------------
77 //----------------------------------------------------------------------------------------
78 int DBGrid::OnTableView(wxString Table
)
81 //---------------------------------------------------------------------------------------
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
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
95 int ValidTable
= x
; // Save the Tablenumber
96 (db_Br
+i_Which
)->OnSelect(Table
,false); // Select * from "table"
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
101 int i
= (db_Br
+i_Which
)->i_Records
; // How many Records are there
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
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
);
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
116 Temp0
.Printf(_T("%06d"),z
+1); SetRowLabelValue(z
,Temp0
); // Set Row Lable Value
117 (db_Br
+i_Which
)->OnGetNext((ct_BrowserDB
->pTableInf
+ValidTable
)->numCols
,false);
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
);
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);
128 #endif // wxUSE_STATUSBAR
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());
133 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
134 #endif // wxUSE_STATUSBAR
135 // The Grid has been filled, now leave
137 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
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)
144 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
145 //---------------------------------------------------------------------------------------
147 EnableEditing(b_EditModus
); // Deactivate in-place Editing
149 //---------------------------------------------------------------------------------------
150 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
154 //----------------------------------------------------------------------------------------
155 void DBGrid::OnModusEdit(wxCommandEvent
& WXUNUSED(event
))
157 b_EditModus
= true; // Needed by PopupMenu
158 EnableEditing(b_EditModus
); // Activate in-place Editing
159 UpdateDimensions(); // Redraw the Grid
160 // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
163 //----------------------------------------------------------------------------------------
164 void DBGrid::OnModusBrowse(wxCommandEvent
& WXUNUSED(event
))
166 b_EditModus
= false; // Needed by PopupMenu
167 EnableEditing(b_EditModus
); // Deactivate in-place Editing
168 UpdateDimensions(); // Redraw the Grid
169 // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
172 //----------------------------------------------------------------------------------------
173 void DBGrid::OnEditorShown( wxGridEvent
& ev
)
175 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
179 //----------------------------------------------------------------------------------------
180 void DBGrid::OnEditorHidden( wxGridEvent
& ev
)
182 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
186 //----------------------------------------------------------------------------------------
187 void DBGrid::OnSelectCell( wxGridEvent
& ev
)
189 logBuf
= wxEmptyString
;
190 logBuf
<< _T("Selected cell at row ") << ev
.GetRow()
191 << _T(" col ") << ev
.GetCol();
192 wxLogMessage( _T("%s"), logBuf
.c_str() );
193 // you must call Skip() if you want the default processing
194 // to occur in wxGrid
198 //----------------------------------------------------------------------------------------
199 void DBGrid::OnMouseMove(wxMouseEvent
&event
)
201 MousePos
= event
.GetPosition();
204 //----------------------------------------------------------------------------------------
205 void DBGrid::OnLabelLeftClick( wxGridEvent
& ev
)
207 logBuf
= _T("DBGrid::OnLabelLeftClick : ");
208 if ( ev
.GetRow() != -1 )
210 logBuf
<< _T("row label ") << ev
.GetRow();
212 else if ( ev
.GetCol() != -1 )
214 logBuf
<< _T("col label ") << ev
.GetCol();
218 logBuf
<< _T("corner label");
220 if ( ev
.ShiftDown() )
221 logBuf
<< _T(" (shift down)");
223 // wxLogMessage( "%s", logBuf.c_str() );
225 wxLogMessage(logBuf
.c_str());
229 //----------------------------------------------------------------------------------------
230 void DBGrid::OnLabelRightClick( wxGridEvent
& ev
)
232 //-------------------------------------------------------
234 PopupMenu(popupMenu2
,MousePos
.x
,MousePos
.y
);
236 PopupMenu(popupMenu1
,MousePos
.x
,MousePos
.y
);
237 //-------------------
238 logBuf
= _T("DBGrid::OnLabelRightClick : ");
239 if ( ev
.GetRow() != -1 )
241 logBuf
<< _T("row label ") << ev
.GetRow();
243 else if ( ev
.GetCol() != -1 )
245 logBuf
<< _T("col label ") << ev
.GetCol();
249 logBuf
<< _T("corner label");
251 if ( ev
.ShiftDown() )
252 logBuf
<< _T(" (shift down)");
254 // wxLogMessage( "%s", logBuf.c_str() );
256 wxLogMessage(logBuf
.c_str());
260 //----------------------------------------------------------------------------------------
261 void DBGrid::OnLabelLeftDClick( wxGridEvent
& ev
)
263 logBuf
= _T("DBGrid::OnLabelLeftDClick : ");
264 if ( ev
.GetRow() != -1 )
266 logBuf
<< _T("row label ") << ev
.GetRow();
268 else if ( ev
.GetCol() != -1 )
270 logBuf
<< _T("col label ") << ev
.GetCol();
274 logBuf
<< _T("corner label");
276 if ( ev
.ShiftDown() )
277 logBuf
<< _T(" (shift down)");
279 // wxLogMessage( _T("%s"), logBuf.c_str() );
281 wxLogMessage(logBuf
.c_str());
285 //----------------------------------------------------------------------------------------
286 void DBGrid::OnLabelRightDClick( wxGridEvent
& ev
)
288 logBuf
= _T("DBGrid::OnLabelRightDClick : ");
289 if ( ev
.GetRow() != -1 )
291 logBuf
<< _T("row label ") << ev
.GetRow();
293 else if ( ev
.GetCol() != -1 )
295 logBuf
<< _T("col label ") << ev
.GetCol();
299 logBuf
<< _T("corner label");
301 if ( ev
.ShiftDown() )
302 logBuf
<< _T(" (shift down)");
303 // wxLogMessage( "%s", logBuf.c_str() );
305 wxLogMessage(logBuf
.c_str());
309 //----------------------------------------------------------------------------------------
310 void DBGrid::OnCellLeftClick( wxGridEvent
& ev
)
312 logBuf
= _T("DBGrid::OnCellLeftClick : ");
313 logBuf
<< _T("Cell at row ") << ev
.GetRow()
314 << _T(" col ") << ev
.GetCol();
315 // wxLogMessage( "%s", logBuf.c_str() );
316 // wxMessageBox(logBuf);
318 wxLogMessage(logBuf
.c_str());
319 // you must call event skip if you want default grid processing
320 // (cell highlighting etc.)
325 //----------------------------------------------------------------------------------------
326 void DBGrid::OnCellRightClick( wxGridEvent
& ev
)
328 logBuf
= _T("DBGrid::OnCellRightClick : ");
329 logBuf
<< _T("Cell at row ") << ev
.GetRow()
330 << _T(" col ") << ev
.GetCol();
331 // wxLogMessage( "%s", logBuf.c_str() );
332 // wxMessageBox(logBuf);
334 wxLogMessage(logBuf
.c_str());
335 // you must call event skip if you want default grid processing
336 // (cell highlighting etc.)
341 //----------------------------------------------------------------------------------------
342 void DBGrid::OnCellLeftDClick( wxGridEvent
& ev
)
344 logBuf
= _T("DBGrid::OnCellLeftDClick : ");
345 logBuf
<< _T("Cell at row ") << ev
.GetRow()
346 << _T(" col ") << ev
.GetCol();
347 // wxLogMessage( "%s", logBuf.c_str() );
348 // wxMessageBox(logBuf);
350 wxLogMessage(logBuf
.c_str());
351 // you must call event skip if you want default grid processing
352 // (cell highlighting etc.)
357 //----------------------------------------------------------------------------------------
358 void DBGrid::OnCellRightDClick( wxGridEvent
& ev
)
360 logBuf
= _T("DBGrid::OnCellRightDClick : ");
361 logBuf
<< _T("Cell at row ") << ev
.GetRow()
362 << _T(" col ") << ev
.GetCol();
363 // wxLogMessage( "%s", logBuf.c_str() );
364 // wxMessageBox(logBuf);
366 wxLogMessage(logBuf
.c_str());
367 // you must call event skip if you want default grid processing
368 // (cell highlighting etc.)
373 //----------------------------------------------------------------------------------------
374 void DBGrid::OnCellChange( wxGridEvent
& ev
)
376 logBuf
= _T("DBGrid::OnCellChange : ");
377 logBuf
<< _T("Cell at row ") << ev
.GetRow()
378 << _T(" col ") << ev
.GetCol();
379 // wxLogMessage( "%s", logBuf.c_str() );
380 // wxMessageBox(logBuf);
382 wxLogMessage(logBuf
.c_str());
383 // you must call event skip if you want default grid processing
384 // (cell highlighting etc.)
389 //----------------------------------------------------------------------------------------
390 void DBGrid::OnRowSize( wxGridSizeEvent
& ev
)
392 logBuf
= _T("DBGrid::OnRowSize : ");
393 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
394 // wxLogMessage( "%s", logBuf.c_str() );
396 wxLogMessage(logBuf
.c_str());
400 //----------------------------------------------------------------------------------------
401 void DBGrid::OnColSize( wxGridSizeEvent
& ev
)
403 logBuf
= _T("DBGrid::OnColSize : ");
404 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
405 // wxLogMessage( "%s", logBuf.c_str() );
407 wxLogMessage(logBuf
.c_str());
411 //----------------------------------------------------------------------------------------
412 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
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();
420 // wxLogMessage( "%s", logBuf.c_str() );
421 wxLogMessage(logBuf
.c_str());
424 //----------------------------------------------------------------------------------------