File: dbgrid.cpp

package info (click to toggle)
wxwidgets2.6 2.6.3.2.1.5%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 83,228 kB
  • ctags: 130,941
  • sloc: cpp: 820,043; ansic: 113,030; python: 107,485; makefile: 42,996; sh: 10,305; lex: 194; yacc: 128; xml: 95; pascal: 74
file content (430 lines) | stat: -rw-r--r-- 17,407 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//----------------------------------------------------------------------------------------
// Name:        DBGrid.cpp
// Purpose:     wxGrid sample
// Author:      Mark Johnson
// Modified by: 19990929.mj10777 a reuseable DBGrid
// Created:     19990929
// Copyright:   (c)
// Licence:     wxWindows license
// RCS-ID:      $Id: dbgrid.cpp,v 1.20 2005/01/31 18:09:45 ABX Exp $
//----------------------------------------------------------------------------------------
//-- all #ifdefs that the whole Project needs. -------------------------------------------
//----------------------------------------------------------------------------------------
#ifdef __GNUG__
#pragma implementation
#pragma interface
#endif
//----------------------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
//----------------------------------------------------------------------------------------
#ifdef __BORLANDC__
#pragma hdrstop
#endif
//----------------------------------------------------------------------------------------
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
//----------------------------------------------------------------------------------------
//-- all #includes that every .cpp needs             --- 19990807.mj10777 ----------------
//----------------------------------------------------------------------------------------
#include "std.h"    // sorgsam Pflegen !
//----------------------------------------------------------------------------------------
BEGIN_EVENT_TABLE(DBGrid, wxGrid)
    EVT_MOTION (DBGrid::OnMouseMove)
    // DBGrid
    // ------------
    EVT_GRID_CELL_CHANGE( DBGrid::OnCellChange )
    EVT_GRID_CELL_LEFT_CLICK( DBGrid::OnCellLeftClick )
    EVT_GRID_CELL_LEFT_DCLICK( DBGrid::OnCellLeftDClick )
    EVT_GRID_CELL_RIGHT_CLICK( DBGrid::OnCellRightClick )
    EVT_GRID_CELL_RIGHT_DCLICK( DBGrid::OnCellRightDClick )
    // EVT_GRID_COL_SIZE( DBGrid::OnColSize )
    // EVT_GRID_ROW_SIZE( DBGrid::OnRowSize )
    EVT_GRID_EDITOR_SHOWN( DBGrid::OnEditorShown )
    EVT_GRID_EDITOR_HIDDEN( DBGrid::OnEditorHidden )
    EVT_GRID_LABEL_LEFT_CLICK( DBGrid::OnLabelLeftClick )
    EVT_GRID_LABEL_LEFT_DCLICK( DBGrid::OnLabelLeftDClick )
    EVT_GRID_LABEL_RIGHT_CLICK( DBGrid::OnLabelRightClick )
    EVT_GRID_LABEL_RIGHT_DCLICK( DBGrid::OnLabelRightDClick )
    EVT_GRID_RANGE_SELECT( DBGrid::OnRangeSelected )
    EVT_GRID_ROW_SIZE( DBGrid::OnRowSize )
    EVT_GRID_SELECT_CELL( DBGrid::OnSelectCell )
    EVT_MENU(GRID_EDIT,DBGrid::OnModusEdit)
    EVT_MENU(GRID_BROWSE,DBGrid::OnModusBrowse)
END_EVENT_TABLE()

//----------------------------------------------------------------------------------------
//           wxListCtrl(parent, id, pos, size, style)
//           wxGrid(parent,wxID_ANY,wxPoint( 0, 0 ), wxSize( 400, 300 ) );
//----------------------------------------------------------------------------------------
// DBGrid
//----------------------------------------------------------------------------------------
// DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size):
//          wxGrid(parent, id, pos, size)
DBGrid::DBGrid(wxWindow *parent, const wxWindowID id,const wxPoint& pos,const wxSize& size, long style):
wxGrid(parent, id, pos, size, style)
{
    b_EditModus = false;
    //---------------------------------------------------------------------------------------
    popupMenu1 = new wxMenu;
    popupMenu1->Append(GRID_EDIT, _("Edit Modus"));
    popupMenu2 = new wxMenu;
    popupMenu2->Append(GRID_BROWSE, _("Browse Modus"));
}

//----------------------------------------------------------------------------------------
DBGrid::~DBGrid()
{
    delete popupMenu1;
    delete popupMenu2;
}

//----------------------------------------------------------------------------------------
int  DBGrid::OnTableView(wxString Table)
{
    wxStopWatch sw;
    //---------------------------------------------------------------------------------------
    int  x,y,z;
    wxString Temp0;
    wxBeginBusyCursor();
    SetDefaultCellFont(* pDoc->ft_Doc);
    //---------------------------------------------------------------------------------------
    ct_BrowserDB = (db_Br+i_Which)->ct_BrowserDB;                       // Get the DSN Pointer
    //---------------------------------------------------------------------------------------
    if (ct_BrowserDB)                                                   // Valid pointer (!= NULL) ?
    {      // Pointer is Valid, use the wxDatabase Information
        for (x=0;x<ct_BrowserDB->numTables;x++)                            // go through the Tables
        {
            if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableName,Table))      // is this our Table ?
            {    // Yes, the Data of this Table shall be put into the Grid
                int ValidTable = x;                                              // Save the Tablenumber
                (db_Br+i_Which)->OnSelect(Table,false);                          // Select * from "table"
                // Set the local Pointer to the Column Information we are going to use
                (db_Br+i_Which)->cl_BrowserDB = (ct_BrowserDB->pTableInf+x)->pColInf;
                if ((ct_BrowserDB->pTableInf+x)->pColInf)                        // Valid pointer (!= NULL) ?
                {   // Pointer is Valid, Column Informationen sind Vorhanden
                    int i = (db_Br+i_Which)->i_Records;                             // How many Records are there
                    (db_Br+i_Which)->i_Which = ValidTable;                          // Still used ???? mj10777
                    if (i == 0)     // If the Table is empty, then show one empty row
                        i++;
                    // wxLogMessage(_("\n-I-> DBGrid::OnTableView() : Vor CreateGrid"));
                    CreateGrid(i,(ct_BrowserDB->pTableInf+x)->numCols);             // Records , Columns
                    for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)            // Loop through the Fields
                    {  // The Field / Column name is used here as Row Titel
                        SetColLabelValue(y,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
                        SetColSize(y,95);
                    }  // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
                    SetColSize(((ct_BrowserDB->pTableInf+x)->numCols-1),120);       // Make the last Column Wider
                    // The Grid has been created, now fill it
                    for (z=0;z<(db_Br+i_Which)->i_Records;z++)                      // Loop through the Records
                    {
                        Temp0.Printf(_T("%06d"),z+1);  SetRowLabelValue(z,Temp0);          // Set Row Lable Value
                        (db_Br+i_Which)->OnGetNext((ct_BrowserDB->pTableInf+ValidTable)->numCols,false);
                        for (y=0;y<(ct_BrowserDB->pTableInf+ValidTable)->numCols;y++) // Loop through the Fields
                        { // BrowserDB::OnGetNext Formats the field Value into tablename
                            SetCellValue(z, y,((db_Br+i_Which)->cl_BrowserDB+y)->tableName);
                        }
#if wxUSE_STATUSBAR
                        if (z % 50 == 0)
                        {
                            Temp0.Printf(_("-I-> DBGrid::OnTableView(%s) - Record %6d (from %d) has been read."),Table.c_str(),z,(db_Br+i_Which)->i_Records);
                            pDoc->p_MainFrame->SetStatusText(Temp0, 0);
                        }
#endif // wxUSE_STATUSBAR
                    }  // for (z=0;z<(db_Br+i_Which)->i_Records;z++)
                    Temp0.Printf(_("-I-> DBGrid::OnTableView(%s) - %6d Records have been read. - Time needed : %ld ms"),Table.c_str(),z,sw.Time());
                    wxLogMessage(Temp0);
#if wxUSE_STATUSBAR
                    pDoc->p_MainFrame->SetStatusText(Temp0, 0);
#endif // wxUSE_STATUSBAR
                    // The Grid has been filled, now leave
                    goto Weiter;
                }   // if ((ct_BrowserDB->pTableInf+x)->pColInf)
                else
                    wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid Column Pointer : Failed"));
            }    // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
        }     // for (x=0;x<ct_BrowserDB->numTables;x++)
    }      // if (ct_BrowserDB)
    else
        wxLogMessage(_("\n-E-> DBGrid::OnTableView() : Invalid DSN Pointer : Failed"));
    //---------------------------------------------------------------------------------------
Weiter:
    EnableEditing(b_EditModus);     // Deactivate in-place Editing
    wxEndBusyCursor();
    //---------------------------------------------------------------------------------------
    wxLogMessage(_("-I-> DBGrid::OnTableView() - End"));
    return 0;
}

//----------------------------------------------------------------------------------------
void DBGrid::OnModusEdit(wxCommandEvent& WXUNUSED(event))
{
    b_EditModus = true;             // Needed by PopupMenu
    EnableEditing(b_EditModus);     // Activate in-place Editing
    UpdateDimensions();             // Redraw the Grid
    // wxLogMessage(_("-I-> DBGrid::OnModusEdit() - End"));
}

//----------------------------------------------------------------------------------------
void DBGrid::OnModusBrowse(wxCommandEvent& WXUNUSED(event))
{
    b_EditModus = false;            // Needed by PopupMenu
    EnableEditing(b_EditModus);     // Deactivate in-place Editing
    UpdateDimensions();             // Redraw the Grid
    // wxLogMessage(_("-I-> DBGrid::OnModusBrowse() - End"));
}

//----------------------------------------------------------------------------------------
void DBGrid::OnEditorShown( wxGridEvent& ev )
{
    // wxLogMessage(_("-I-> DBGrid::OnEditorShown() - End"));
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnEditorHidden( wxGridEvent& ev )
{
    // wxLogMessage(_("-I-> DBGrid::OnEditorHidden() - End"));
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnSelectCell( wxGridEvent& ev )
{
    logBuf = wxEmptyString;
    logBuf << _T("Selected cell at row ") << ev.GetRow()
        << _T(" col ") << ev.GetCol();
    wxLogMessage( _T("%s"), logBuf.c_str() );
    // you must call Skip() if you want the default processing
    // to occur in wxGrid
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnMouseMove(wxMouseEvent &event)
{
    MousePos = event.GetPosition();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnLabelLeftClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnLabelLeftClick : ");
    if ( ev.GetRow() != -1 )
    {
        logBuf << _T("row label ") << ev.GetRow();
    }
    else if ( ev.GetCol() != -1 )
    {
        logBuf << _T("col label ") << ev.GetCol();
    }
    else
    {
        logBuf << _T("corner label");
    }
    if ( ev.ShiftDown() )
        logBuf << _T(" (shift down)");

    // wxLogMessage( "%s", logBuf.c_str() );
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnLabelRightClick( wxGridEvent& ev )
{
    //-------------------------------------------------------
    if (b_EditModus)
        PopupMenu(popupMenu2,MousePos.x,MousePos.y);
    else
        PopupMenu(popupMenu1,MousePos.x,MousePos.y);
    //-------------------
    logBuf = _T("DBGrid::OnLabelRightClick : ");
    if ( ev.GetRow() != -1 )
    {
        logBuf << _T("row label ") << ev.GetRow();
    }
    else if ( ev.GetCol() != -1 )
    {
        logBuf << _T("col label ") << ev.GetCol();
    }
    else
    {
        logBuf << _T("corner label");
    }
    if ( ev.ShiftDown() )
        logBuf << _T(" (shift down)");

    // wxLogMessage( "%s", logBuf.c_str() );
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnLabelLeftDClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnLabelLeftDClick : ");
    if ( ev.GetRow() != -1 )
    {
        logBuf << _T("row label ") << ev.GetRow();
    }
    else if ( ev.GetCol() != -1 )
    {
        logBuf << _T("col label ") << ev.GetCol();
    }
    else
    {
        logBuf << _T("corner label");
    }
    if ( ev.ShiftDown() )
        logBuf << _T(" (shift down)");

    // wxLogMessage( _T("%s"), logBuf.c_str() );
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnLabelRightDClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnLabelRightDClick : ");
    if ( ev.GetRow() != -1 )
    {
        logBuf << _T("row label ") << ev.GetRow();
    }
    else if ( ev.GetCol() != -1 )
    {
        logBuf << _T("col label ") << ev.GetCol();
    }
    else
    {
        logBuf << _T("corner label");
    }
    if ( ev.ShiftDown() )
        logBuf << _T(" (shift down)");
    // wxLogMessage( "%s", logBuf.c_str() );
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnCellLeftClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnCellLeftClick : ");
    logBuf << _T("Cell at row ") << ev.GetRow()
        << _T(" col ") << ev.GetCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    // wxMessageBox(logBuf);
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    // you must call event skip if you want default grid processing
    // (cell highlighting etc.)
    //
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnCellRightClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnCellRightClick : ");
    logBuf << _T("Cell at row ") << ev.GetRow()
        << _T(" col ") << ev.GetCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    // wxMessageBox(logBuf);
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    // you must call event skip if you want default grid processing
    // (cell highlighting etc.)
    //
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnCellLeftDClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnCellLeftDClick : ");
    logBuf << _T("Cell at row ") << ev.GetRow()
        << _T(" col ") << ev.GetCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    // wxMessageBox(logBuf);
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    // you must call event skip if you want default grid processing
    // (cell highlighting etc.)
    //
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnCellRightDClick( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnCellRightDClick : ");
    logBuf << _T("Cell at row ") << ev.GetRow()
        << _T(" col ") << ev.GetCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    // wxMessageBox(logBuf);
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    // you must call event skip if you want default grid processing
    // (cell highlighting etc.)
    //
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnCellChange( wxGridEvent& ev )
{
    logBuf = _T("DBGrid::OnCellChange : ");
    logBuf << _T("Cell at row ") << ev.GetRow()
        << _T(" col ") << ev.GetCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    // wxMessageBox(logBuf);
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    // you must call event skip if you want default grid processing
    // (cell highlighting etc.)
    //
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnRowSize( wxGridSizeEvent& ev )
{
    logBuf = _T("DBGrid::OnRowSize : ");
    logBuf << _T("Resized row ") << ev.GetRowOrCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnColSize( wxGridSizeEvent& ev )
{
    logBuf = _T("DBGrid::OnColSize : ");
    logBuf << _T("Resized col ") << ev.GetRowOrCol();
    // wxLogMessage( "%s", logBuf.c_str() );
    logBuf += _T("\n");
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}

//----------------------------------------------------------------------------------------
void DBGrid::OnRangeSelected( wxGridRangeSelectEvent& ev )
{
    logBuf = _T("DBGrid::OnRangeSelected : ");
    logBuf  << _T("Selected cells from row ") << ev.GetTopRow()
        << _T(" col ") << ev.GetLeftCol()
        << _T(" to row ") << ev.GetBottomRow()
        << _T(" col ") << ev.GetRightCol();
    logBuf += _T("\n");
    // wxLogMessage( "%s", logBuf.c_str() );
    wxLogMessage(logBuf.c_str());
    ev.Skip();
}
//----------------------------------------------------------------------------------------