OpenCPN Partial API docs
Loading...
Searching...
No Matches
time_textbox.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2019 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#ifndef time_textbox_h
25#define time_textbox_h
26
27#pragma once
28
29#include <wx/dateevt.h>
30#include <wx/datetime.h>
31#include <wx/msgdlg.h>
32#include <wx/textctrl.h>
33
34#define NO_TIME "00:00"
35#define TIME_FORMAT "%H:%M"
36
37class TimeCtrl : public wxTextCtrl {
38public:
39 TimeCtrl(wxWindow *parent, wxWindowID id,
40 const wxDateTime &value = wxDefaultDateTime,
41 const wxPoint &pos = wxDefaultPosition,
42 const wxSize &size = wxDefaultSize, long style = 0,
43 const wxValidator &validator = wxDefaultValidator,
44 const wxString &name = wxTextCtrlNameStr)
45 : wxTextCtrl(parent, id,
46 value.IsValid() ? value.Format(TIME_FORMAT) : NO_TIME, pos,
47 size, style, validator, name) {
48 Bind(wxEVT_KEY_UP, &TimeCtrl::OnChar, this);
49 Bind(wxEVT_KILL_FOCUS, &TimeCtrl::OnKillFocus, this);
50 };
51
52 void SetValue(const wxDateTime val) {
53 if (val.IsValid()) {
54 wxTextCtrl::SetValue(val.Format(TIME_FORMAT));
55 } else {
56 wxTextCtrl::SetValue(NO_TIME);
57 }
58 };
59
60 wxDateTime GetValue() {
61 wxDateTime dt;
62 wxString str = wxTextCtrl::GetValue();
63 wxString::const_iterator end;
64 if (!dt.ParseTime(str, &end)) {
65 return wxInvalidDateTime;
66 } else if (end == str.end()) {
67 return dt;
68 } else {
69 return dt;
70 }
71 };
72
73 void OnChar(wxKeyEvent &event) {
74 if (GetValue().IsValid()) {
75 wxDateEvent evt(this, GetValue(), wxEVT_TIME_CHANGED);
76 HandleWindowEvent(evt);
77 }
78 };
79
80 void OnKillFocus(wxFocusEvent &event) {
81 wxTextCtrl::SetValue(GetValue().Format(TIME_FORMAT));
82 };
83
84 bool GetTime(int *hour, int *min, int *sec) {
85 const wxDateTime::Tm tm = GetValue().GetTm();
86 *hour = tm.hour;
87 *min = tm.min;
88 *sec = tm.sec;
89
90 return true;
91 }
92};
93
94#endif /* time_textbox_h */