File: datepick.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 (341 lines) | stat: -rw-r--r-- 10,176 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
/////////////////////////////////////////////////////////////////////////////
// Program:     wxWidgets Widgets Sample
// Name:        datepick.cpp
// Purpose:     Part of the widgets sample showing date picker
// Author:      Dimitri Schoolwerth, Vadim Zeitlin
// Created:     27 Sep 2003
// Copyright:   (c) 2003 wxWindows team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

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

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

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

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if wxUSE_DATEPICKCTRL

// for all others, include the necessary headers
#ifndef WX_PRECOMP
    #include "wx/app.h"
    #include "wx/log.h"

    #include "wx/bitmap.h"
    #include "wx/button.h"
    #include "wx/checkbox.h"
    #include "wx/radiobox.h"
    #include "wx/statbox.h"
    #include "wx/textctrl.h"

    #include "wx/sizer.h"
#endif

#include "wx/datectrl.h"
#include "wx/dateevt.h"

#include "widgets.h"

#include "icons/datepick.xpm"

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

// control ids
enum
{
    DatePickerPage_Reset = wxID_HIGHEST,
    DatePickerPage_Set,
    DatePickerPage_SetRange,
    DatePickerPage_Picker
};

// ----------------------------------------------------------------------------
// CheckBoxWidgetsPage
// ----------------------------------------------------------------------------

class DatePickerWidgetsPage : public WidgetsPage
{
public:
    DatePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
    virtual ~DatePickerWidgetsPage(){};

    virtual wxControl *GetWidget() const { return m_datePicker; }
    virtual void RecreateWidget() { CreateDatePicker(); }

    // lazy creation of the content
    virtual void CreateContent();

protected:
    // event handlers
    void OnDateChanged(wxDateEvent& event);

    void OnButtonSet(wxCommandEvent& event);
    void OnButtonSetRange(wxCommandEvent& event);
    void OnButtonReset(wxCommandEvent& event);

    // reset the date picker parameters
    void Reset();

    // (re)create the date picker
    void CreateDatePicker();

    // the controls
    // ------------

    // the checkbox itself and the sizer it is in
    wxDatePickerCtrl *m_datePicker;
    wxSizer *m_sizerDatePicker;

    wxTextCtrl *m_textCur;
    wxTextCtrl *m_textMin;
    wxTextCtrl *m_textMax;

    wxRadioBox* m_radioKind;
    wxCheckBox* m_chkStyleCentury;
    wxCheckBox* m_chkStyleAllowNone;

    // the text entries for command parameters
    wxTextCtrl *m_textLabel;

private:
    wxDECLARE_EVENT_TABLE();
    DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage)
};

// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------

wxBEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
    EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
    EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
    EVT_BUTTON(DatePickerPage_SetRange, DatePickerWidgetsPage::OnButtonSetRange)

    EVT_DATE_CHANGED(wxID_ANY, DatePickerWidgetsPage::OnDateChanged)
wxEND_EVENT_TABLE()

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

#if defined(__WXMSW__)
    #define FAMILY_CTRLS NATIVE_CTRLS
#else
    #define FAMILY_CTRLS GENERIC_CTRLS
#endif

IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"),
                       FAMILY_CTRLS | PICKER_CTRLS
                       );

DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
                                         wxImageList *imaglist)
                      :WidgetsPage(book, imaglist, datepick_xpm)
{
}

void DatePickerWidgetsPage::CreateContent()
{
    wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);

    // left pane: style
    wxSizer* const sizerLeft = new wxBoxSizer(wxVERTICAL);

    static const wxString kinds[] = { "&Default", "&Spin", "Drop do&wn" };
    m_radioKind = new wxRadioBox(this, wxID_ANY, "&Kind",
                                 wxDefaultPosition, wxDefaultSize,
                                 WXSIZEOF(kinds), kinds,
                                 1, wxRA_SPECIFY_COLS);
    sizerLeft->Add(m_radioKind, wxSizerFlags().Expand().Border());

    wxSizer* const sizerStyle = new wxStaticBoxSizer(wxVERTICAL, this, "&Style");
    m_chkStyleCentury = CreateCheckBoxAndAddToSizer(sizerStyle, "Show &century");
    m_chkStyleAllowNone = CreateCheckBoxAndAddToSizer(sizerStyle, "Allow &no value");

    sizerLeft->Add(sizerStyle, wxSizerFlags().Expand().Border());

    sizerLeft->Add(new wxButton(this, DatePickerPage_Reset, "&Recreate"),
                   wxSizerFlags().Centre().Border());


    // middle pane: operations
    wxSizer* const sizerMiddle = new wxBoxSizer(wxVERTICAL);
    sizerMiddle->Add(CreateSizerWithTextAndButton
                     (
                        DatePickerPage_Set,
                        "&Set date",
                        wxID_ANY,
                        &m_textCur
                     ),
                     wxSizerFlags().Expand().Border());

    m_textCur->SetMinSize(wxSize(GetTextExtent("  9999-99-99  ").x, -1));

    sizerMiddle->AddSpacer(10);

    sizerMiddle->Add(CreateSizerWithTextAndLabel
                     (
                        "&Min date",
                        wxID_ANY,
                        &m_textMin
                     ),
                     wxSizerFlags().Expand().Border());
    sizerMiddle->Add(CreateSizerWithTextAndLabel
                     (
                        "Ma&x date",
                        wxID_ANY,
                        &m_textMax
                     ),
                     wxSizerFlags().Expand().Border());
    sizerMiddle->Add(new wxButton(this, DatePickerPage_SetRange, "Set &range"),
                     wxSizerFlags().Centre().Border());


    // right pane: control itself
    wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);

    m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);

    sizerRight->Add(0, 0, 1, wxCENTRE);
    sizerRight->Add(m_datePicker, 1, wxCENTRE);
    sizerRight->Add(0, 0, 1, wxCENTRE);
    m_sizerDatePicker = sizerRight; // save it to modify it later

    // the 3 panes panes compose the window
    sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
    sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
    sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);

    // final initializations
    m_chkStyleCentury->SetValue(true);
    Reset();

    SetSizer(sizerTop);
}

void DatePickerWidgetsPage::Reset()
{
    const wxDateTime today = wxDateTime::Today();

    m_datePicker->SetValue(today);
    m_textCur->SetValue(today.FormatISODate());
}

void DatePickerWidgetsPage::CreateDatePicker()
{
    const wxDateTime value = m_datePicker->GetValue();

    size_t count = m_sizerDatePicker->GetChildren().GetCount();
    for ( size_t n = 0; n < count; n++ )
    {
        m_sizerDatePicker->Remove(0);
    }

    delete m_datePicker;

    long style = 0;
    switch ( m_radioKind->GetSelection() )
    {
        case 0:
            style = wxDP_DEFAULT;
            break;

        case 1:
            style = wxDP_SPIN;
            break;

        case 2:
            style = wxDP_DROPDOWN;
            break;
    }

    if ( m_chkStyleCentury->GetValue() )
        style |= wxDP_SHOWCENTURY;
    if ( m_chkStyleAllowNone->GetValue() )
        style |= wxDP_ALLOWNONE;

    m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value,
                                        wxDefaultPosition, wxDefaultSize,
                                        style);

    m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
    m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE);
    m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
    m_sizerDatePicker->Layout();
}

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

void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
    Reset();

    CreateDatePicker();
}

static bool GetDateFromTextControl(wxDateTime& dt, const wxTextCtrl* text)
{
    const wxString& value = text->GetValue();
    if ( !value.empty() )
    {
        wxString::const_iterator end;
        if ( !dt.ParseDate(value, &end) || end != value.end() )
        {
            wxLogError("Invalid date \"%s\"");
            return false;
        }
    }

    return true;
}

void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
{
    wxDateTime dt;
    if ( GetDateFromTextControl(dt, m_textCur) )
        m_datePicker->SetValue(dt);
}

void DatePickerWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
{
    wxDateTime dt1, dt2;
    if ( !GetDateFromTextControl(dt1, m_textMin) ||
           !GetDateFromTextControl(dt2, m_textMax) )
        return;

    m_datePicker->SetRange(dt1, dt2);

    if ( !m_datePicker->GetRange(&dt1, &dt2) )
    {
        wxLogMessage("No range set");
    }
    else
    {
        m_textMin->SetValue(dt1.IsValid() ? dt1.FormatISODate() : wxString());
        m_textMax->SetValue(dt2.IsValid() ? dt2.FormatISODate() : wxString());

        wxLogMessage("Date picker range updated");
    }
}

void DatePickerWidgetsPage::OnDateChanged(wxDateEvent& event)
{
    wxLogMessage("Date changed, now is %s (control value is %s).",
                 event.GetDate().FormatISOCombined(),
                 m_datePicker->GetValue().FormatISOCombined());
}

#endif // wxUSE_DATEPICKCTRL