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