]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbgrid.cpp
browsedb.cpp browsedb.h dbbrowse.cpp dbbrowse.h dbbrowse.rc
[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 EVT_GRID_LABEL_LEFT_CLICK( DBGrid::OnLabelLeftClick )
37 EVT_GRID_LABEL_RIGHT_CLICK( DBGrid::OnLabelRightClick )
38 EVT_GRID_LABEL_LEFT_DCLICK( DBGrid::OnLabelLeftDClick )
39 EVT_GRID_LABEL_RIGHT_DCLICK( DBGrid::OnLabelRightDClick )
40 EVT_GRID_CELL_LEFT_CLICK( DBGrid::OnCellLeftClick )
41 EVT_GRID_CELL_RIGHT_CLICK( DBGrid::OnCellRightClick )
42 EVT_GRID_CELL_LEFT_DCLICK( DBGrid::OnCellLeftDClick )
43 EVT_GRID_CELL_RIGHT_DCLICK( DBGrid::OnCellRightDClick )
44 EVT_GRID_ROW_SIZE( DBGrid::OnRowSize )
45 // EVT_GRID_COL_SIZE( DBGrid::OnColSize )
46 EVT_GRID_RANGE_SELECT( DBGrid::OnRangeSelected )
47 EVT_GRID_CELL_CHANGE( DBGrid::OnCellChange )
48 EVT_MENU(GRID_EDIT,DBGrid::OnModusEdit)
49 EVT_MENU(GRID_BROWSE,DBGrid::OnModusBrowse)
50 END_EVENT_TABLE()
51 //----------------------------------------------------------------------------------------
52 // wxListCtrl(parent, id, pos, size, style)
53 // wxGrid(parent,-1,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
54 //----------------------------------------------------------------------------------------
55 // DBGrid
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)
61 {
62 b_EditModus = TRUE;
63 //---------------------------------------------------------------------------------------
64 popupMenu1 = new wxMenu("");
65 popupMenu1->Append(GRID_EDIT, _("Edit Modus"));
66 popupMenu2 = new wxMenu("");
67 popupMenu2->Append(GRID_BROWSE, _("Browse Modus"));
68 }
69 //----------------------------------------------------------------------------------------
70 DBGrid::~DBGrid()
71 {
72 }
73 //----------------------------------------------------------------------------------------
74 int DBGrid::OnTableView(wxString Table)
75 {
76 //---------------------------------------------------------------------------------------
77 int i=0,x,y,z, ValidTable=0;
78 wxString Temp0;
79 //SetLabelFont(* f_Temp);
80 wxBeginBusyCursor();
81 SetFont(* pDoc->ft_Doc);
82 //---------------------------------------------------------------------------------------
83 ct_BrowserDB = (db_Br+i_Which)->ct_BrowserDB; // Get the DSN Pointer
84 //---------------------------------------------------------------------------------------
85 if (ct_BrowserDB) // Valid pointer (!= NULL) ?
86 { // Pointer is Valid, use the wxDatabase Information
87 for (x=0;x<ct_BrowserDB->numTables;x++) // go through the Tables
88 {
89 if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableName,Table)) // is this our Table ?
90 { // Yes, the Data of this Table shall be put into the Grid
91 ValidTable = x; // Save the Tablenumber
92 (db_Br+i_Which)->OnSelect(Table,FALSE); // Select * from "table"
93 // Set the local Pointer to the Column Information we are going to use
94 (db_Br+i_Which)->cl_BrowserDB = (ct_BrowserDB->pTableInf+x)->pColInf;
95 if ((ct_BrowserDB->pTableInf+x)->pColInf) // Valid pointer (!= NULL) ?
96 { // Pointer is Valid, Column Informationen sind Vorhanden
97 i = (db_Br+i_Which)->i_Records; // How many Records are there
98 (db_Br+i_Which)->i_Which = ValidTable; // Still used ???? mj10777
99 if (i == 0) // If the Table is empty, then show one empty row
100 i++;
101 // wxLogMessage(_("\n-I-> DBGrid::OnTableView():: Vor CreateGrid"));
102 CreateGrid(i,(ct_BrowserDB->pTableInf+x)->numCols); // Records , Columns
103 for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++) // Loop through the Fields
104 { // The Field / Column name is used here as Row Titel
105 SetColLabelValue(y,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
106 SetColSize(y,95);
107 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
108 SetColSize(((ct_BrowserDB->pTableInf+x)->numCols-1),120); // Make the last Column Wider
109 // The Grid has been created, now fill it
110 for (z=0;z<(db_Br+i_Which)->i_Records;z++) // Loop through the Records
111 {
112 Temp0.Printf("%06d",z+1); SetRowLabelValue(z,Temp0); // Set Row Lable Value
113 (db_Br+i_Which)->OnGetNext((ct_BrowserDB->pTableInf+ValidTable)->numCols,FALSE);
114 for (y=0;y<(ct_BrowserDB->pTableInf+ValidTable)->numCols;y++) // Loop through the Fields
115 { // BrowserDB::OnGetNext Formats the field Value into tablename
116 SetCellValue(z, y,((db_Br+i_Which)->cl_BrowserDB+y)->tableName);
117 }
118 if (z % 50 == 0)
119 {
120 Temp0.Printf(_("-I-> DBGrid::OnTableView(%s) - Record %6d has been read."),Table.c_str(),z);
121 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
122 }
123 } // for (z=0;z<(db_Br+i_Which)->i_Records;z++)
124 Temp0.Printf(_("-I-> DBGrid::OnTableView(%s) - %6d Records have been read."),Table.c_str(),z);
125 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
126 // The Grid has been filled, now leave
127 goto Weiter;
128 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
129 else
130 wxLogMessage(_("\n-E-> DBGrid::OnTableView():: Invalid Column Pointer : Failed"));
131 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
132 } // for (x=0;x<ct_BrowserDB->numTables;x++)
133 } // if (ct_BrowserDB)
134 else
135 wxLogMessage(_("\n-E-> DBGrid::OnTableView():: Invalid DSN Pointer : Failed"));
136 //---------------------------------------------------------------------------------------
137 Weiter:
138 SetEditInPlace(b_EditModus); // Activate in-place Editing (FALSE)
139 wxEndBusyCursor();
140 //---------------------------------------------------------------------------------------
141 wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
142 return 0;
143 }
144 //----------------------------------------------------------------------------------------
145 void DBGrid::OnModusEdit(wxMenu& menu, wxCommandEvent& event)
146 {
147 b_EditModus = TRUE; // Needed by PopupMenu
148 SetEditable(FALSE); // Do not Edit with Text Edit Control
149 SetEditInPlace(b_EditModus); // Deactivate in-place Editing
150 UpdateDimensions(); // Redraw the Grid
151 }
152 //----------------------------------------------------------------------------------------
153 void DBGrid::OnModusBrowse(wxMenu& menu, wxCommandEvent& event)
154 {
155 b_EditModus = FALSE; // Needed by PopupMenu
156 SetEditInPlace(b_EditModus); // Deactivate in-place Editing
157 UpdateDimensions(); // Redraw the Grid
158 }
159 //----------------------------------------------------------------------------------------
160 void DBGrid::OnMouseMove(wxMouseEvent &event)
161 {
162 MousePos = event.GetPosition();
163 }
164 //----------------------------------------------------------------------------------------
165 void DBGrid::OnLabelLeftClick( wxGridEvent& ev )
166 {
167 logBuf = "DBGrid::OnLabelLeftClick : ";
168 if ( ev.GetRow() != -1 )
169 {
170 logBuf << "row label " << ev.GetRow();
171 }
172 else if ( ev.GetCol() != -1 )
173 {
174 logBuf << "col label " << ev.GetCol();
175 }
176 else
177 {
178 logBuf << "corner label";
179 }
180 if ( ev.ShiftDown() )
181 logBuf << " (shift down)";
182 // wxLogMessage( "%s", logBuf.c_str() );
183 logBuf += "\n";
184 wxLogMessage(logBuf.c_str());
185 ev.Skip();
186 }
187 //----------------------------------------------------------------------------------------
188 void DBGrid::OnLabelRightClick( wxGridEvent& ev )
189 {
190 //-------------------
191 if (b_EditModus)
192 PopupMenu(popupMenu2,MousePos.x,MousePos.y);
193 else
194 PopupMenu(popupMenu1,MousePos.x,MousePos.y);
195 //-------------------
196 logBuf = "DBGrid::OnLabelRightClick : ";
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::OnLabelLeftDClick( wxGridEvent& ev )
218 {
219 logBuf = "DBGrid::OnLabelLeftDClick : ";
220 if ( ev.GetRow() != -1 )
221 {
222 logBuf << "row label " << ev.GetRow();
223 }
224 else if ( ev.GetCol() != -1 )
225 {
226 logBuf << "col label " << ev.GetCol();
227 }
228 else
229 {
230 logBuf << "corner label";
231 }
232 if ( ev.ShiftDown() )
233 logBuf << " (shift down)";
234 // wxLogMessage( "%s", logBuf.c_str() );
235 logBuf += "\n";
236 wxLogMessage(logBuf.c_str());
237 ev.Skip();
238 }
239 //----------------------------------------------------------------------------------------
240 void DBGrid::OnLabelRightDClick( wxGridEvent& ev )
241 {
242 logBuf = "DBGrid::OnLabelRightDClick : ";
243 if ( ev.GetRow() != -1 )
244 {
245 logBuf << "row label " << ev.GetRow();
246 }
247 else if ( ev.GetCol() != -1 )
248 {
249 logBuf << "col label " << ev.GetCol();
250 }
251 else
252 {
253 logBuf << "corner label";
254 }
255 if ( ev.ShiftDown() )
256 logBuf << " (shift down)";
257 // wxLogMessage( "%s", logBuf.c_str() );
258 logBuf += "\n";
259 wxLogMessage(logBuf.c_str());
260 ev.Skip();
261 }
262 //----------------------------------------------------------------------------------------
263 void DBGrid::OnCellLeftClick( wxGridEvent& ev )
264 {
265 logBuf = "DBGrid::OnCellLeftClick : ";
266 logBuf << "Cell at row " << ev.GetRow()
267 << " col " << ev.GetCol();
268 // wxLogMessage( "%s", logBuf.c_str() );
269 // wxMessageBox(logBuf);
270 logBuf += "\n";
271 wxLogMessage(logBuf.c_str());
272 // you must call event skip if you want default grid processing
273 // (cell highlighting etc.)
274 //
275 ev.Skip();
276 }
277 //----------------------------------------------------------------------------------------
278 void DBGrid::OnCellRightClick( wxGridEvent& ev )
279 {
280 logBuf = "DBGrid::OnCellRightClick : ";
281 logBuf << "Cell at row " << ev.GetRow()
282 << " col " << ev.GetCol();
283 // wxLogMessage( "%s", logBuf.c_str() );
284 // wxMessageBox(logBuf);
285 logBuf += "\n";
286 wxLogMessage(logBuf.c_str());
287 // you must call event skip if you want default grid processing
288 // (cell highlighting etc.)
289 //
290 ev.Skip();
291 }
292 //----------------------------------------------------------------------------------------
293 void DBGrid::OnCellLeftDClick( wxGridEvent& ev )
294 {
295 logBuf = "DBGrid::OnCellLeftDClick : ";
296 logBuf << "Cell at row " << ev.GetRow()
297 << " col " << ev.GetCol();
298 // wxLogMessage( "%s", logBuf.c_str() );
299 // wxMessageBox(logBuf);
300 logBuf += "\n";
301 wxLogMessage(logBuf.c_str());
302 // you must call event skip if you want default grid processing
303 // (cell highlighting etc.)
304 //
305 ev.Skip();
306 }
307 //----------------------------------------------------------------------------------------
308 void DBGrid::OnCellRightDClick( wxGridEvent& ev )
309 {
310 logBuf = "DBGrid::OnCellRightDClick : ";
311 logBuf << "Cell at row " << ev.GetRow()
312 << " col " << ev.GetCol();
313 // wxLogMessage( "%s", logBuf.c_str() );
314 // wxMessageBox(logBuf);
315 logBuf += "\n";
316 wxLogMessage(logBuf.c_str());
317 // you must call event skip if you want default grid processing
318 // (cell highlighting etc.)
319 //
320 ev.Skip();
321 }
322 //----------------------------------------------------------------------------------------
323 void DBGrid::OnCellChange( wxGridEvent& ev )
324 {
325 logBuf = "DBGrid::OnCellChange : ";
326 logBuf << "Cell at row " << ev.GetRow()
327 << " col " << ev.GetCol();
328 // wxLogMessage( "%s", logBuf.c_str() );
329 // wxMessageBox(logBuf);
330 logBuf += "\n";
331 wxLogMessage(logBuf.c_str());
332 // you must call event skip if you want default grid processing
333 // (cell highlighting etc.)
334 //
335 ev.Skip();
336 }
337 //----------------------------------------------------------------------------------------
338 void DBGrid::OnRowSize( wxGridSizeEvent& ev )
339 {
340 logBuf = "DBGrid::OnRowSize : ";
341 logBuf << "Resized row " << ev.GetRowOrCol();
342 // wxLogMessage( "%s", logBuf.c_str() );
343 logBuf += "\n";
344 wxLogMessage(logBuf.c_str());
345 ev.Skip();
346 }
347 //----------------------------------------------------------------------------------------
348 void DBGrid::OnColSize( wxGridSizeEvent& ev )
349 {
350 logBuf = "DBGrid::OnColSize : ";
351 logBuf << "Resized col " << ev.GetRowOrCol();
352 // wxLogMessage( "%s", logBuf.c_str() );
353 logBuf += "\n";
354 wxLogMessage(logBuf.c_str());
355 ev.Skip();
356 }
357 //----------------------------------------------------------------------------------------
358 void DBGrid::OnRangeSelected( wxGridRangeSelectEvent& ev )
359 {
360 logBuf = "DBGrid::OnRangeSelected : ";
361 logBuf << "Selected cells from row " << ev.GetTopRow()
362 << " col " << ev.GetLeftCol()
363 << " to row " << ev.GetBottomRow()
364 << " col " << ev.GetRightCol();
365 logBuf += "\n";
366 // wxLogMessage( "%s", logBuf.c_str() );
367 wxLogMessage(logBuf.c_str());
368 ev.Skip();
369 }
370 //----------------------------------------------------------------------------------------