]>
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
)
56 //----------------------------------------------------------------------------------------
57 // wxListCtrl(parent, id, pos, size, style)
58 // wxGrid(parent,-1,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
59 //----------------------------------------------------------------------------------------
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
)
68 //---------------------------------------------------------------------------------------
69 popupMenu1
= new wxMenu("");
70 popupMenu1
->Append(GRID_EDIT
, _("Edit Modus"));
71 popupMenu2
= new wxMenu("");
72 popupMenu2
->Append(GRID_BROWSE
, _("Browse Modus"));
74 //----------------------------------------------------------------------------------------
78 //----------------------------------------------------------------------------------------
79 int DBGrid::OnTableView(wxString Table
)
82 //---------------------------------------------------------------------------------------
83 int i
=0,x
,y
,z
, ValidTable
=0;
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
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
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
);
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
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
);
125 Temp0
.Printf(_("-I-> DBGrid::OnTableView(%s) - Record %6d has been read."),Table
.c_str(),z
);
126 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
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());
131 pDoc
->p_MainFrame
->SetStatusText(Temp0
, 0);
132 // The Grid has been filled, now leave
134 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
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)
141 wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
142 //---------------------------------------------------------------------------------------
144 EnableEditing(b_EditModus
); // Deactivate in-place Editing
146 //---------------------------------------------------------------------------------------
147 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
150 //----------------------------------------------------------------------------------------
151 void DBGrid::OnModusEdit(wxCommandEvent
& event
)
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"));
158 //----------------------------------------------------------------------------------------
159 void DBGrid::OnModusBrowse(wxCommandEvent
& event
)
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"));
166 //----------------------------------------------------------------------------------------
167 void DBGrid::OnEditorShown( wxGridEvent
& ev
)
169 // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
172 //----------------------------------------------------------------------------------------
173 void DBGrid::OnEditorHidden( wxGridEvent
& ev
)
175 // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
178 void DBGrid::OnSelectCell( wxGridEvent
& ev
)
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
188 //----------------------------------------------------------------------------------------
189 void DBGrid::OnMouseMove(wxMouseEvent
&event
)
191 MousePos
= event
.GetPosition();
193 //----------------------------------------------------------------------------------------
194 void DBGrid::OnLabelLeftClick( wxGridEvent
& ev
)
196 logBuf
= "DBGrid::OnLabelLeftClick : ";
197 if ( ev
.GetRow() != -1 )
199 logBuf
<< "row label " << ev
.GetRow();
201 else if ( ev
.GetCol() != -1 )
203 logBuf
<< "col label " << ev
.GetCol();
207 logBuf
<< "corner label";
209 if ( ev
.ShiftDown() )
210 logBuf
<< " (shift down)";
211 // wxLogMessage( "%s", logBuf.c_str() );
213 wxLogMessage(logBuf
.c_str());
216 //----------------------------------------------------------------------------------------
217 void DBGrid::OnLabelRightClick( wxGridEvent
& ev
)
219 //-------------------------------------------------------
221 PopupMenu(popupMenu2
,MousePos
.x
,MousePos
.y
);
223 PopupMenu(popupMenu1
,MousePos
.x
,MousePos
.y
);
224 //-------------------
225 logBuf
= "DBGrid::OnLabelRightClick : ";
226 if ( ev
.GetRow() != -1 )
228 logBuf
<< "row label " << ev
.GetRow();
230 else if ( ev
.GetCol() != -1 )
232 logBuf
<< "col label " << ev
.GetCol();
236 logBuf
<< "corner label";
238 if ( ev
.ShiftDown() )
239 logBuf
<< " (shift down)";
240 // wxLogMessage( "%s", logBuf.c_str() );
242 wxLogMessage(logBuf
.c_str());
245 //----------------------------------------------------------------------------------------
246 void DBGrid::OnLabelLeftDClick( wxGridEvent
& ev
)
248 logBuf
= "DBGrid::OnLabelLeftDClick : ";
249 if ( ev
.GetRow() != -1 )
251 logBuf
<< "row label " << ev
.GetRow();
253 else if ( ev
.GetCol() != -1 )
255 logBuf
<< "col label " << ev
.GetCol();
259 logBuf
<< "corner label";
261 if ( ev
.ShiftDown() )
262 logBuf
<< " (shift down)";
263 // wxLogMessage( "%s", logBuf.c_str() );
265 wxLogMessage(logBuf
.c_str());
268 //----------------------------------------------------------------------------------------
269 void DBGrid::OnLabelRightDClick( wxGridEvent
& ev
)
271 logBuf
= "DBGrid::OnLabelRightDClick : ";
272 if ( ev
.GetRow() != -1 )
274 logBuf
<< "row label " << ev
.GetRow();
276 else if ( ev
.GetCol() != -1 )
278 logBuf
<< "col label " << ev
.GetCol();
282 logBuf
<< "corner label";
284 if ( ev
.ShiftDown() )
285 logBuf
<< " (shift down)";
286 // wxLogMessage( "%s", logBuf.c_str() );
288 wxLogMessage(logBuf
.c_str());
291 //----------------------------------------------------------------------------------------
292 void DBGrid::OnCellLeftClick( wxGridEvent
& ev
)
294 logBuf
= "DBGrid::OnCellLeftClick : ";
295 logBuf
<< "Cell at row " << ev
.GetRow()
296 << " col " << ev
.GetCol();
297 // wxLogMessage( "%s", logBuf.c_str() );
298 // wxMessageBox(logBuf);
300 wxLogMessage(logBuf
.c_str());
301 // you must call event skip if you want default grid processing
302 // (cell highlighting etc.)
306 //----------------------------------------------------------------------------------------
307 void DBGrid::OnCellRightClick( wxGridEvent
& ev
)
309 logBuf
= "DBGrid::OnCellRightClick : ";
310 logBuf
<< "Cell at row " << ev
.GetRow()
311 << " col " << ev
.GetCol();
312 // wxLogMessage( "%s", logBuf.c_str() );
313 // wxMessageBox(logBuf);
315 wxLogMessage(logBuf
.c_str());
316 // you must call event skip if you want default grid processing
317 // (cell highlighting etc.)
321 //----------------------------------------------------------------------------------------
322 void DBGrid::OnCellLeftDClick( wxGridEvent
& ev
)
324 logBuf
= "DBGrid::OnCellLeftDClick : ";
325 logBuf
<< "Cell at row " << ev
.GetRow()
326 << " col " << ev
.GetCol();
327 // wxLogMessage( "%s", logBuf.c_str() );
328 // wxMessageBox(logBuf);
330 wxLogMessage(logBuf
.c_str());
331 // you must call event skip if you want default grid processing
332 // (cell highlighting etc.)
336 //----------------------------------------------------------------------------------------
337 void DBGrid::OnCellRightDClick( wxGridEvent
& ev
)
339 logBuf
= "DBGrid::OnCellRightDClick : ";
340 logBuf
<< "Cell at row " << ev
.GetRow()
341 << " col " << ev
.GetCol();
342 // wxLogMessage( "%s", logBuf.c_str() );
343 // wxMessageBox(logBuf);
345 wxLogMessage(logBuf
.c_str());
346 // you must call event skip if you want default grid processing
347 // (cell highlighting etc.)
351 //----------------------------------------------------------------------------------------
352 void DBGrid::OnCellChange( wxGridEvent
& ev
)
354 logBuf
= "DBGrid::OnCellChange : ";
355 logBuf
<< "Cell at row " << ev
.GetRow()
356 << " col " << ev
.GetCol();
357 // wxLogMessage( "%s", logBuf.c_str() );
358 // wxMessageBox(logBuf);
360 wxLogMessage(logBuf
.c_str());
361 // you must call event skip if you want default grid processing
362 // (cell highlighting etc.)
366 //----------------------------------------------------------------------------------------
367 void DBGrid::OnRowSize( wxGridSizeEvent
& ev
)
369 logBuf
= "DBGrid::OnRowSize : ";
370 logBuf
<< "Resized row " << ev
.GetRowOrCol();
371 // wxLogMessage( "%s", logBuf.c_str() );
373 wxLogMessage(logBuf
.c_str());
376 //----------------------------------------------------------------------------------------
377 void DBGrid::OnColSize( wxGridSizeEvent
& ev
)
379 logBuf
= "DBGrid::OnColSize : ";
380 logBuf
<< "Resized col " << ev
.GetRowOrCol();
381 // wxLogMessage( "%s", logBuf.c_str() );
383 wxLogMessage(logBuf
.c_str());
386 //----------------------------------------------------------------------------------------
387 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
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();
395 // wxLogMessage( "%s", logBuf.c_str() );
396 wxLogMessage(logBuf
.c_str());
399 //----------------------------------------------------------------------------------------