]>
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,wxID_ANY,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(_T(""));
71 popupMenu1
->Append(GRID_EDIT
, _("Edit Modus"));
72 popupMenu2
= new wxMenu(_T(""));
73 popupMenu2
->Append(GRID_BROWSE
, _("Browse Modus"));
76 //----------------------------------------------------------------------------------------
83 //----------------------------------------------------------------------------------------
84 int DBGrid::OnTableView(wxString Table
)
87 //---------------------------------------------------------------------------------------
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 int 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 int 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(_T("%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
);
131 Temp0
.Printf(_("-I-> DBGrid::OnTableView(%s) - Record %6d (from %d) has been read."),Table
.c_str(),z
,(db_Br
+i_Which
)->i_Records
);
132 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
134 #endif // wxUSE_STATUSBAR
135 } // for (z=0;z<(db_Br+i_Which)->i_Records;z++)
136 Temp0
.Printf(_("-I-> DBGrid::OnTableView(%s) - %6d Records have been read. - Time needed : %ld ms"),Table
.c_str(),z
,sw
.Time());
139 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
140 #endif // wxUSE_STATUSBAR
141 // The Grid has been filled, now leave
143 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
145 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid Column Pointer : Failed"));
146 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
147 } // for (x=0;x<ct_BrowserDB->numTables;x++)
148 } // if (ct_BrowserDB)
150 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
151 //---------------------------------------------------------------------------------------
153 EnableEditing(b_EditModus
); // Deactivate in-place Editing
155 //---------------------------------------------------------------------------------------
156 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
160 //----------------------------------------------------------------------------------------
161 void DBGrid::OnModusEdit(wxCommandEvent
& WXUNUSED(event
))
163 b_EditModus
= true; // Needed by PopupMenu
164 EnableEditing(b_EditModus
); // Activate in-place Editing
165 UpdateDimensions(); // Redraw the Grid
166 // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
169 //----------------------------------------------------------------------------------------
170 void DBGrid::OnModusBrowse(wxCommandEvent
& WXUNUSED(event
))
172 b_EditModus
= false; // Needed by PopupMenu
173 EnableEditing(b_EditModus
); // Deactivate in-place Editing
174 UpdateDimensions(); // Redraw the Grid
175 // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
178 //----------------------------------------------------------------------------------------
179 void DBGrid::OnEditorShown( wxGridEvent
& ev
)
181 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
185 //----------------------------------------------------------------------------------------
186 void DBGrid::OnEditorHidden( wxGridEvent
& ev
)
188 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
192 //----------------------------------------------------------------------------------------
193 void DBGrid::OnSelectCell( wxGridEvent
& ev
)
196 logBuf
<< _T("Selected cell at row ") << ev
.GetRow()
197 << _T(" col ") << ev
.GetCol();
198 wxLogMessage( _T("%s"), logBuf
.c_str() );
199 // you must call Skip() if you want the default processing
200 // to occur in wxGrid
204 //----------------------------------------------------------------------------------------
205 void DBGrid::OnMouseMove(wxMouseEvent
&event
)
207 MousePos
= event
.GetPosition();
210 //----------------------------------------------------------------------------------------
211 void DBGrid::OnLabelLeftClick( wxGridEvent
& ev
)
213 logBuf
= _T("DBGrid::OnLabelLeftClick : ");
214 if ( ev
.GetRow() != -1 )
216 logBuf
<< _T("row label ") << ev
.GetRow();
218 else if ( ev
.GetCol() != -1 )
220 logBuf
<< _T("col label ") << ev
.GetCol();
224 logBuf
<< _T("corner label");
226 if ( ev
.ShiftDown() )
227 logBuf
<< _T(" (shift down)");
229 // wxLogMessage( "%s", logBuf.c_str() );
231 wxLogMessage(logBuf
.c_str());
235 //----------------------------------------------------------------------------------------
236 void DBGrid::OnLabelRightClick( wxGridEvent
& ev
)
238 //-------------------------------------------------------
240 PopupMenu(popupMenu2
,MousePos
.x
,MousePos
.y
);
242 PopupMenu(popupMenu1
,MousePos
.x
,MousePos
.y
);
243 //-------------------
244 logBuf
= _T("DBGrid::OnLabelRightClick : ");
245 if ( ev
.GetRow() != -1 )
247 logBuf
<< _T("row label ") << ev
.GetRow();
249 else if ( ev
.GetCol() != -1 )
251 logBuf
<< _T("col label ") << ev
.GetCol();
255 logBuf
<< _T("corner label");
257 if ( ev
.ShiftDown() )
258 logBuf
<< _T(" (shift down)");
260 // wxLogMessage( "%s", logBuf.c_str() );
262 wxLogMessage(logBuf
.c_str());
266 //----------------------------------------------------------------------------------------
267 void DBGrid::OnLabelLeftDClick( wxGridEvent
& ev
)
269 logBuf
= _T("DBGrid::OnLabelLeftDClick : ");
270 if ( ev
.GetRow() != -1 )
272 logBuf
<< _T("row label ") << ev
.GetRow();
274 else if ( ev
.GetCol() != -1 )
276 logBuf
<< _T("col label ") << ev
.GetCol();
280 logBuf
<< _T("corner label");
282 if ( ev
.ShiftDown() )
283 logBuf
<< _T(" (shift down)");
285 // wxLogMessage( _T("%s"), logBuf.c_str() );
287 wxLogMessage(logBuf
.c_str());
291 //----------------------------------------------------------------------------------------
292 void DBGrid::OnLabelRightDClick( wxGridEvent
& ev
)
294 logBuf
= _T("DBGrid::OnLabelRightDClick : ");
295 if ( ev
.GetRow() != -1 )
297 logBuf
<< _T("row label ") << ev
.GetRow();
299 else if ( ev
.GetCol() != -1 )
301 logBuf
<< _T("col label ") << ev
.GetCol();
305 logBuf
<< _T("corner label");
307 if ( ev
.ShiftDown() )
308 logBuf
<< _T(" (shift down)");
309 // wxLogMessage( "%s", logBuf.c_str() );
311 wxLogMessage(logBuf
.c_str());
315 //----------------------------------------------------------------------------------------
316 void DBGrid::OnCellLeftClick( wxGridEvent
& ev
)
318 logBuf
= _T("DBGrid::OnCellLeftClick : ");
319 logBuf
<< _T("Cell at row ") << ev
.GetRow()
320 << _T(" col ") << ev
.GetCol();
321 // wxLogMessage( "%s", logBuf.c_str() );
322 // wxMessageBox(logBuf);
324 wxLogMessage(logBuf
.c_str());
325 // you must call event skip if you want default grid processing
326 // (cell highlighting etc.)
331 //----------------------------------------------------------------------------------------
332 void DBGrid::OnCellRightClick( wxGridEvent
& ev
)
334 logBuf
= _T("DBGrid::OnCellRightClick : ");
335 logBuf
<< _T("Cell at row ") << ev
.GetRow()
336 << _T(" col ") << ev
.GetCol();
337 // wxLogMessage( "%s", logBuf.c_str() );
338 // wxMessageBox(logBuf);
340 wxLogMessage(logBuf
.c_str());
341 // you must call event skip if you want default grid processing
342 // (cell highlighting etc.)
347 //----------------------------------------------------------------------------------------
348 void DBGrid::OnCellLeftDClick( wxGridEvent
& ev
)
350 logBuf
= _T("DBGrid::OnCellLeftDClick : ");
351 logBuf
<< _T("Cell at row ") << ev
.GetRow()
352 << _T(" col ") << ev
.GetCol();
353 // wxLogMessage( "%s", logBuf.c_str() );
354 // wxMessageBox(logBuf);
356 wxLogMessage(logBuf
.c_str());
357 // you must call event skip if you want default grid processing
358 // (cell highlighting etc.)
363 //----------------------------------------------------------------------------------------
364 void DBGrid::OnCellRightDClick( wxGridEvent
& ev
)
366 logBuf
= _T("DBGrid::OnCellRightDClick : ");
367 logBuf
<< _T("Cell at row ") << ev
.GetRow()
368 << _T(" col ") << ev
.GetCol();
369 // wxLogMessage( "%s", logBuf.c_str() );
370 // wxMessageBox(logBuf);
372 wxLogMessage(logBuf
.c_str());
373 // you must call event skip if you want default grid processing
374 // (cell highlighting etc.)
379 //----------------------------------------------------------------------------------------
380 void DBGrid::OnCellChange( wxGridEvent
& ev
)
382 logBuf
= _T("DBGrid::OnCellChange : ");
383 logBuf
<< _T("Cell at row ") << ev
.GetRow()
384 << _T(" col ") << ev
.GetCol();
385 // wxLogMessage( "%s", logBuf.c_str() );
386 // wxMessageBox(logBuf);
388 wxLogMessage(logBuf
.c_str());
389 // you must call event skip if you want default grid processing
390 // (cell highlighting etc.)
395 //----------------------------------------------------------------------------------------
396 void DBGrid::OnRowSize( wxGridSizeEvent
& ev
)
398 logBuf
= _T("DBGrid::OnRowSize : ");
399 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
400 // wxLogMessage( "%s", logBuf.c_str() );
402 wxLogMessage(logBuf
.c_str());
406 //----------------------------------------------------------------------------------------
407 void DBGrid::OnColSize( wxGridSizeEvent
& ev
)
409 logBuf
= _T("DBGrid::OnColSize : ");
410 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
411 // wxLogMessage( "%s", logBuf.c_str() );
413 wxLogMessage(logBuf
.c_str());
417 //----------------------------------------------------------------------------------------
418 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
420 logBuf
= _T("DBGrid::OnRangeSelected : ");
421 logBuf
<< _T("Selected cells from row ") << ev
.GetTopRow()
422 << _T(" col ") << ev
.GetLeftCol()
423 << _T(" to row ") << ev
.GetBottomRow()
424 << _T(" col ") << ev
.GetRightCol();
426 // wxLogMessage( "%s", logBuf.c_str() );
427 wxLogMessage(logBuf
.c_str());
430 //----------------------------------------------------------------------------------------