File: vstest.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (611 lines) | stat: -rw-r--r-- 18,154 bytes parent folder | download | duplicates (10)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/////////////////////////////////////////////////////////////////////////////
// Name:        samples/vscroll/vstest.cpp
// Purpose:     VScroll wxWidgets sample
// Author:      Vadim Zeitlin
// Modified by: Brad Anderson
// Created:     04/01/98
// Copyright:   (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
    #include "wx/app.h"
    #include "wx/frame.h"
#endif

// we need to include the headers not included from wx/wx.h explicitly anyhow
#include "wx/vscroll.h"

// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------

// the application icon (under Windows and OS/2 it is in resources)
#ifndef wxHAS_IMAGES_IN_RESOURCES
    #include "../sample.xpm"
#endif

// ----------------------------------------------------------------------------
// definitions
// ----------------------------------------------------------------------------

#define MAX_LINES 10000

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class VarScrollApp : public wxApp
{
public:
    // create our main window
    virtual bool OnInit();
};

// Define a new frame type: this is going to be our main frame
class VarScrollFrame : public wxFrame
{
public:
    // ctor
    VarScrollFrame();

    // event handlers (these functions should _not_ be virtual)
    void OnQuit(wxCommandEvent& event);
    void OnModeVScroll(wxCommandEvent& event);
    void OnModeHScroll(wxCommandEvent& event);
    void OnModeHVScroll(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

    void OnSize(wxSizeEvent& event)
    {
        // show current size in the status bar
#if wxUSE_STATUSBAR
        if ( m_frameStatusBar )
        {
            wxSize sz = GetClientSize();
            SetStatusText(wxString::Format(wxT("%dx%d"), sz.x, sz.y), 1);
        }
#endif // wxUSE_STATUSBAR

        event.Skip();
    }

private:
    // either a wxVScrolledWindow or a wxHVScrolled window, depending on current mode
    wxPanel *m_scrollWindow;

    // any class wishing to process wxWidgets events must use this macro
    wxDECLARE_EVENT_TABLE();
};

class VScrollWindow : public wxVScrolledWindow
{
public:
    VScrollWindow(wxFrame *frame) : wxVScrolledWindow(frame, wxID_ANY)
    {
        m_frame = frame;

        SetRowCount(MAX_LINES);

        int i;
        for ( i = 0; i < MAX_LINES; ++i )
            m_heights[i] = rand()%25+16; // low: 16; high: 40

        m_changed = true;
    }

    void OnIdle(wxIdleEvent&)
    {
#if wxUSE_STATUSBAR
        m_frame->SetStatusText(wxString::Format
                               (
                                    wxT("Page size = %d, pos = %d, max = %d"),
                                    GetScrollThumb(wxVERTICAL),
                                    GetScrollPos(wxVERTICAL),
                                    GetScrollRange(wxVERTICAL)
                               ));
#endif // wxUSE_STATUSBAR
        m_changed = false;
    }

    void OnPaint(wxPaintEvent&)
    {
        wxPaintDC dc(this);

        dc.SetPen(*wxBLACK_PEN);

        const size_t lineFirst = GetVisibleBegin(),
                     lineLast = GetVisibleEnd();

        const wxCoord hText = dc.GetCharHeight();

        wxSize clientSize = GetClientSize();

        wxCoord y = 0;
        for ( size_t line = lineFirst; line < lineLast; line++ )
        {
            dc.DrawLine(0, y, clientSize.GetWidth(), y);

            wxCoord hLine = OnGetRowHeight(line);
            dc.DrawText(wxString::Format(wxT("Line %lu"), (unsigned long)line),
                        2, y + (hLine - hText) / 2);

            y += hLine;
            dc.DrawLine(0, y, 1000, y);
        }
    }

    void OnScroll(wxScrollWinEvent& event)
    {
        m_changed = true;

        event.Skip();
    }

    void OnMouse(wxMouseEvent& event)
    {
        if(event.LeftDown())
            CaptureMouse();
        else if(event.LeftUp())
            ReleaseMouse();
        event.Skip();
    }

    virtual wxCoord OnGetRowHeight(size_t n) const
    {
        wxASSERT( n < GetRowCount() );

        return m_heights[n];
    }

private:
    wxFrame *m_frame;

    int m_heights[MAX_LINES];

    bool m_changed;

    wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(VScrollWindow, wxVScrolledWindow)
    EVT_IDLE(VScrollWindow::OnIdle)
    EVT_PAINT(VScrollWindow::OnPaint)
    EVT_SCROLLWIN(VScrollWindow::OnScroll)
    EVT_MOUSE_EVENTS(VScrollWindow::OnMouse)
wxEND_EVENT_TABLE()

class HScrollWindow : public wxHScrolledWindow
{
public:
    HScrollWindow(wxFrame *frame) : wxHScrolledWindow(frame, wxID_ANY)
    {
        m_frame = frame;

        SetColumnCount(MAX_LINES);

        int i;
        for ( i = 0; i < MAX_LINES; ++i )
            m_heights[i] = rand()%25+16; // low: 15; high: 40

        m_changed = true;
    }

    void OnIdle(wxIdleEvent&)
    {
#if wxUSE_STATUSBAR
        m_frame->SetStatusText(wxString::Format
                               (
                                    wxT("Page size = %d, pos = %d, max = %d"),
                                    GetScrollThumb(wxVERTICAL),
                                    GetScrollPos(wxVERTICAL),
                                    GetScrollRange(wxVERTICAL)
                               ));
#endif // wxUSE_STATUSBAR
        m_changed = false;
    }

    void OnPaint(wxPaintEvent&)
    {
        wxPaintDC dc(this);

        dc.SetPen(*wxBLACK_PEN);

        const size_t lineFirst = GetVisibleBegin(),
                     lineLast = GetVisibleEnd();

        const wxCoord hText = dc.GetCharHeight();

        wxSize clientSize = GetClientSize();

        wxCoord x = 0;
        for ( size_t line = lineFirst; line < lineLast; line++ )
        {
            dc.DrawLine(x, 0, x, clientSize.GetHeight());

            wxCoord wLine = OnGetColumnWidth(line);
            dc.DrawRotatedText(wxString::Format(wxT("Line %lu"), (unsigned long)line),
                               x + (wLine - hText) / 2, clientSize.GetHeight() - 5, 90);

            x += wLine;
            dc.DrawLine(x, 0, x, 1000);
        }
    }

    void OnScroll(wxScrollWinEvent& event)
    {
        m_changed = true;

        event.Skip();
    }

    void OnMouse(wxMouseEvent& event)
    {
        if(event.LeftDown())
            CaptureMouse();
        else if(event.LeftUp())
            ReleaseMouse();
        event.Skip();
    }

    virtual wxCoord OnGetColumnWidth(size_t n) const
    {
        wxASSERT( n < GetColumnCount() );

        return m_heights[n];
    }

private:
    wxFrame *m_frame;

    int m_heights[MAX_LINES];

    bool m_changed;

    wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(HScrollWindow, wxHScrolledWindow)
    EVT_IDLE(HScrollWindow::OnIdle)
    EVT_PAINT(HScrollWindow::OnPaint)
    EVT_SCROLLWIN(HScrollWindow::OnScroll)
    EVT_MOUSE_EVENTS(HScrollWindow::OnMouse)
wxEND_EVENT_TABLE()

class HVScrollWindow : public wxHVScrolledWindow
{
public:
    HVScrollWindow(wxFrame *frame) : wxHVScrolledWindow(frame, wxID_ANY)
    {
        m_frame = frame;

        SetRowColumnCount(MAX_LINES, MAX_LINES);

        int i;
        for ( i = 0; i < MAX_LINES; ++i )
        {
            m_heights[i] = rand()%30+31; // low: 30; high: 60
            m_widths[i] = rand()%30+61;  // low: 60; high: 90
        }

        m_changed = true;
    }

    void OnIdle(wxIdleEvent&)
    {
#if wxUSE_STATUSBAR
        m_frame->SetStatusText(wxString::Format
                               (
                                    wxT("Page size = %d rows %d columns; pos = row: %d, column: %d; max = %d rows, %d columns"),
                                    GetScrollThumb(wxVERTICAL),
                                    GetScrollThumb(wxHORIZONTAL),
                                    GetScrollPos(wxVERTICAL),
                                    GetScrollPos(wxHORIZONTAL),
                                    GetScrollRange(wxVERTICAL),
                                    GetScrollRange(wxHORIZONTAL)
                               ));
#endif // wxUSE_STATUSBAR
        m_changed = false;
    }

    void OnPaint(wxPaintEvent&)
    {
        wxPaintDC dc(this);

        dc.SetPen(*wxBLACK_PEN);

        const size_t rowFirst = GetVisibleRowsBegin(),
                     rowLast = GetVisibleRowsEnd();
        const size_t columnFirst = GetVisibleColumnsBegin(),
                     columnLast = GetVisibleColumnsEnd();

        const wxCoord hText = dc.GetCharHeight();

        wxSize clientSize = GetClientSize();

        wxCoord y = 0;
        wxCoord x = 0;
        for ( size_t row = rowFirst; row < rowLast; row++ )
        {
            wxCoord rowHeight = OnGetRowHeight(row);
            dc.DrawLine(0, y, clientSize.GetWidth(), y);

            x = 0;
            for ( size_t col = columnFirst; col < columnLast; col++ )
            {
                wxCoord colWidth = OnGetColumnWidth(col);

                if ( row == rowFirst )
                    dc.DrawLine(x, 0, x, clientSize.GetHeight());

                dc.DrawText(wxString::Format(wxT("Row %lu"), (unsigned long)row),
                            x + 2, y + rowHeight / 2 - hText);
                dc.DrawText(wxString::Format(wxT("Col %lu"), (unsigned long)col),
                            x + 2, y + rowHeight / 2);

                x += colWidth;
                if ( row == rowFirst)
                    dc.DrawLine(x, 0, x, clientSize.GetHeight());
            }

            y += rowHeight;
            dc.DrawLine(0, y, clientSize.GetWidth(), y);
        }
    }

    void OnScroll(wxScrollWinEvent& event)
    {
        m_changed = true;

        event.Skip();
    }

    void OnMouse(wxMouseEvent& event)
    {
        if(event.LeftDown())
            CaptureMouse();
        else if(event.LeftUp())
            ReleaseMouse();
        event.Skip();
    }

    virtual wxCoord OnGetRowHeight(size_t n) const
    {
        wxASSERT( n < GetRowCount() );

        return m_heights[n];
    }

    virtual wxCoord OnGetColumnWidth(size_t n) const
    {
        wxASSERT( n < GetColumnCount() );

        return m_widths[n];
    }

private:
    wxFrame *m_frame;

    int m_heights[MAX_LINES];
    int m_widths[MAX_LINES];

    bool m_changed;

    wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(HVScrollWindow, wxHVScrolledWindow)
    EVT_IDLE(HVScrollWindow::OnIdle)
    EVT_PAINT(HVScrollWindow::OnPaint)
    EVT_SCROLLWIN(HVScrollWindow::OnScroll)
    EVT_MOUSE_EVENTS(HVScrollWindow::OnMouse)
wxEND_EVENT_TABLE()

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// IDs for the controls and the menu commands
enum
{
    // menu items
    VScroll_Quit = wxID_EXIT,

    // it is important for the id corresponding to the "About" command to have
    // this standard value as otherwise it won't be handled properly under Mac
    // (where it is special and put into the "Apple" menu)
    VScroll_About = wxID_ABOUT,

    VScroll_VScrollMode = wxID_HIGHEST + 1,
    VScroll_HScrollMode,
    VScroll_HVScrollMode
};

// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------

// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
wxBEGIN_EVENT_TABLE(VarScrollFrame, wxFrame)
    EVT_MENU(VScroll_Quit,  VarScrollFrame::OnQuit)
    EVT_MENU(VScroll_VScrollMode, VarScrollFrame::OnModeVScroll)
    EVT_MENU(VScroll_HScrollMode, VarScrollFrame::OnModeHScroll)
    EVT_MENU(VScroll_HVScrollMode, VarScrollFrame::OnModeHVScroll)
    EVT_MENU(VScroll_About, VarScrollFrame::OnAbout)
    EVT_SIZE(VarScrollFrame::OnSize)
wxEND_EVENT_TABLE()

// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. VarScrollApp and
// not wxApp)
IMPLEMENT_APP(VarScrollApp)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------

// 'Main program' equivalent: the program execution "starts" here
bool VarScrollApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    // create the main application window
    VarScrollFrame *frame = new VarScrollFrame;

    // and show it (the frames, unlike simple controls, are not shown when
    // created initially)
    frame->Show(true);

    // ok
    return true;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
VarScrollFrame::VarScrollFrame()
               : wxFrame(NULL,
                         wxID_ANY,
                         wxT("VScroll wxWidgets Sample"),
                         wxDefaultPosition,
                         wxSize(400, 350)),
                 m_scrollWindow(NULL)
{
    // set the frame icon
    SetIcon(wxICON(sample));

#if wxUSE_MENUS
    // create a menu bar
    wxMenu *menuFile = new wxMenu;

    wxMenu *menuMode = new wxMenu;

    // the "About" item should be in the help menu
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(VScroll_About, wxT("&About\tF1"), wxT("Show about dialog"));

#ifdef wxHAS_RADIO_MENU_ITEMS
    menuMode->AppendRadioItem(VScroll_VScrollMode, wxT("&Vertical\tAlt-V"),
                              wxT("Vertical scrolling only"));
    menuMode->AppendRadioItem(VScroll_HScrollMode, wxT("&Horizontal\tAlt-H"),
                              wxT("Horizontal scrolling only"));
    menuMode->AppendRadioItem(VScroll_HVScrollMode,
                              wxT("Hori&zontal/Vertical\tAlt-Z"),
                              wxT("Horizontal and vertical scrolling"));
    menuMode->Check(VScroll_VScrollMode, true);
#else
    menuMode->Append(VScroll_VScrollMode, wxT("&Vertical\tAlt-V"),
                     wxT("Vertical scrolling only"));
    menuMode->Append(VScroll_HScrollMode, wxT("&Horizontal\tAlt-H"),
                     wxT("Horizontal scrolling only"));
    menuMode->Append(VScroll_HVScrollMode, wxT("Hori&zontal/Vertical\tAlt-Z"),
                     wxT("Horizontal and vertical scrolling"));
#endif

    menuFile->Append(VScroll_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));

    // now append the freshly created menu to the menu bar...
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, wxT("&File"));
    menuBar->Append(menuMode, wxT("&Mode"));
    menuBar->Append(menuHelp, wxT("&Help"));

    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
    // create a status bar just for fun (by default with 1 pane only)
    CreateStatusBar(2);
    SetStatusText(wxT("Welcome to wxWidgets!"));
    int widths[2];
    widths[0] = -1;
    widths[1] = 100;
    SetStatusWidths(2, widths);
#endif // wxUSE_STATUSBAR

    // create our one and only child -- it will take our entire client area
    if ( menuMode->IsChecked(VScroll_VScrollMode) )
        m_scrollWindow = new VScrollWindow(this);
    else if ( menuMode->IsChecked(VScroll_HScrollMode) )
        m_scrollWindow = new HScrollWindow(this);
    else
        m_scrollWindow = new HVScrollWindow(this);
}

// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------

void VarScrollFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // true is to force the frame to close
    Close(true);
}

void VarScrollFrame::OnModeVScroll(wxCommandEvent& WXUNUSED(event))
{
    if ( m_scrollWindow )
        m_scrollWindow->Destroy();

    m_scrollWindow = new VScrollWindow(this);
    SendSizeEvent();
}

void VarScrollFrame::OnModeHScroll(wxCommandEvent& WXUNUSED(event))
{
    if ( m_scrollWindow )
        m_scrollWindow->Destroy();

    m_scrollWindow = new HScrollWindow(this);
    SendSizeEvent();
}

void VarScrollFrame::OnModeHVScroll(wxCommandEvent& WXUNUSED(event))
{
    if ( m_scrollWindow )
        m_scrollWindow->Destroy();

    m_scrollWindow = new HVScrollWindow(this);
    SendSizeEvent();
}

void VarScrollFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox(wxT("VScroll shows how to implement scrolling with\n")
                 wxT("variable line widths and heights.\n")
                 wxT("(c) 2003 Vadim Zeitlin"),
                 wxT("About VScroll"),
                 wxOK | wxICON_INFORMATION,
                 this);
}