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