NCBI C++ ToolKit
flexibledate_panel.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: flexibledate_panel.cpp 47479 2023-05-02 13:24:02Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Roman Katargin
27  */
28 
29 
30 #include <ncbi_pch.hpp>
31 #include <objects/general/Date.hpp>
35 
37 
38 ////@begin includes
39 ////@end includes
40 ////@begin includes
41 ////@end includes
42 
44 
45 #include <wx/sizer.h>
46 #include <wx/bitmap.h>
47 #include <wx/icon.h>
48 #include <wx/stattext.h>
49 #include <wx/textctrl.h>
50 #include <wx/choice.h>
51 
54 
55 ////@begin XPM images
56 ////@end XPM images
57 
58 
59 /*
60  * CFlexibleDatePanel type definition
61  */
62 
63 IMPLEMENT_DYNAMIC_CLASS( CFlexibleDatePanel, wxPanel )
64 
65 
66 /*
67  * CFlexibleDatePanel event table definition
68  */
69 
70 BEGIN_EVENT_TABLE( CFlexibleDatePanel, wxPanel )
71 
72 ////@begin CFlexibleDatePanel event table entries
73 ////@end CFlexibleDatePanel event table entries
74 
76 
77 
78 /*
79  * CFlexibleDatePanel constructors
80  */
81 
83 {
84  Init();
85 }
86 
87 CFlexibleDatePanel::CFlexibleDatePanel( wxWindow* parent, CRef<CDate> the_date, long min_year, long range,
88  wxWindowID id, const wxPoint& pos, const wxSize& size, long style ) :
89  m_Date(the_date), m_MinYear(min_year), m_YearRange(range)
90 {
91  Init();
92  Create(parent, id, pos, size, style);
93 }
94 
95 
96 
97 /*
98  * CFlexibleDatePanel creator
99  */
100 
101 bool CFlexibleDatePanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
102 {
103 ////@begin CFlexibleDatePanel creation
104  SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
105  wxPanel::Create( parent, id, pos, size, style );
106 
107  CreateControls();
108  if (GetSizer())
109  {
110  GetSizer()->SetSizeHints(this);
111  }
112  Centre();
113 ////@end CFlexibleDatePanel creation
114  return true;
115 }
116 
117 
118 /*
119  * CFlexibleDatePanel destructor
120  */
121 
123 {
124 ////@begin CFlexibleDatePanel destruction
125 ////@end CFlexibleDatePanel destruction
126 }
127 
128 
129 /*
130  * Member initialisation
131  */
132 
134 {
135 ////@begin CFlexibleDatePanel member initialisation
136  m_YearCtrl = NULL;
137  m_MonthCtrl = NULL;
138  m_DayCtrl = NULL;
139 ////@end CFlexibleDatePanel member initialisation
140 }
141 
142 
143 /*
144  * Control creation for CFlexibleDatePanel
145  */
146 
148 {
149 ////@begin CFlexibleDatePanel content construction
150  CFlexibleDatePanel* itemPanel1 = this;
151 
152  wxFlexGridSizer* itemFlexGridSizer2 = new wxFlexGridSizer(0, 3, 0, 0);
153  itemPanel1->SetSizer(itemFlexGridSizer2);
154 
155  wxStaticText* itemStaticText3 = new wxStaticText( itemPanel1, wxID_STATIC, _("Year"), wxDefaultPosition, wxDefaultSize, 0 );
156  itemFlexGridSizer2->Add(itemStaticText3, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
157 
158  wxStaticText* itemStaticText4 = new wxStaticText( itemPanel1, wxID_STATIC, _("Month"), wxDefaultPosition, wxDefaultSize, 0 );
159  itemFlexGridSizer2->Add(itemStaticText4, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
160 
161  wxStaticText* itemStaticText5 = new wxStaticText( itemPanel1, wxID_STATIC, _("Day"), wxDefaultPosition, wxDefaultSize, 0 );
162  itemFlexGridSizer2->Add(itemStaticText5, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
163 
164  m_YearCtrl = new wxTextCtrl( itemPanel1, ID_FLEXIBLEDATE_YEAR, wxEmptyString, wxDefaultPosition, wxSize(60, -1), 0 );
165  itemFlexGridSizer2->Add(m_YearCtrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
166 
167  wxArrayString m_MonthCtrlStrings;
168  m_MonthCtrlStrings.Add(wxEmptyString);
169  m_MonthCtrlStrings.Add(_("Jan"));
170  m_MonthCtrlStrings.Add(_("Feb"));
171  m_MonthCtrlStrings.Add(_("Mar"));
172  m_MonthCtrlStrings.Add(_("Apr"));
173  m_MonthCtrlStrings.Add(_("May"));
174  m_MonthCtrlStrings.Add(_("Jun"));
175  m_MonthCtrlStrings.Add(_("Jul"));
176  m_MonthCtrlStrings.Add(_("Aug"));
177  m_MonthCtrlStrings.Add(_("Sep"));
178  m_MonthCtrlStrings.Add(_("Oct"));
179  m_MonthCtrlStrings.Add(_("Nov"));
180  m_MonthCtrlStrings.Add(_("Dec"));
181  m_MonthCtrl = new wxChoice( itemPanel1, ID_FLEXIBLEDATE_MONTH, wxDefaultPosition, wxDefaultSize, m_MonthCtrlStrings, 0 );
182  itemFlexGridSizer2->Add(m_MonthCtrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
183 
184  m_DayCtrl = new wxTextCtrl( itemPanel1, ID_FLEXIBLEDATE_DAY, wxEmptyString, wxDefaultPosition, wxSize(40, -1), 0 );
185  itemFlexGridSizer2->Add(m_DayCtrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
186 
187 ////@end CFlexibleDatePanel content construction
188 
189  m_Year = 0;
190  m_Day = 0;
191  m_Month = 0;
192  if (m_Date->SetStd().IsSetYear()) {
193  m_Year = m_Date->GetStd().GetYear();
194  }
195  if (m_Date->SetStd().IsSetMonth()) {
196  m_Month = m_Date->GetStd().GetMonth();
197  if (m_Month > 12 || m_Month < 0) {
198  m_Month = 0;
199  }
200  }
201  if (m_Date->SetStd().IsSetDay()) {
202  m_Day = m_Date->GetStd().GetDay();
203  }
204  m_MonthCtrl->SetSelection(m_Month);
205  m_DayCtrl->SetValidator(CNumberOrBlankValidator(&m_Day));
207 }
208 
209 void CFlexibleDatePanel::SetToolTip(const wxString &tipString)
210 {
211  m_YearCtrl->SetToolTip(tipString);
212  m_MonthCtrl->SetToolTip(tipString);
213  m_DayCtrl->SetToolTip(tipString);
214  wxPanel::SetToolTip(tipString);
215 }
216 
218 {
219  m_Year = 0;
220  m_Day = 0;
221  m_Month = 0;
222  if (m_Date->SetStd().IsSetYear()) {
223  m_Year = m_Date->GetStd().GetYear();
224  }
225  if (m_Date->SetStd().IsSetMonth()) {
226  m_Month = m_Date->GetStd().GetMonth();
227  if (m_Month > 12 || m_Month < 0) {
228  m_Month = 0;
229  }
230  }
231  if (m_Date->SetStd().IsSetDay()) {
232  m_Day = m_Date->GetStd().GetDay();
233  }
234  if (!wxPanel::TransferDataToWindow())
235  return false;
236 
237  m_MonthCtrl->SetSelection(m_Month);
238  return true;
239 }
240 
241 
243 {
244  if (!wxPanel::TransferDataFromWindow())
245  return false;
246 
247  if (m_Year < m_MinYear) {
248  m_Date->SetStr("?");
249  return true;
250  }
251 
252  m_Month = m_MonthCtrl->GetSelection();
253 
254  m_Date->SetStd().SetYear(static_cast<CDate_std::TYear>(m_Year));
255  if (m_Month > 0) {
256  m_Date->SetStd().SetMonth(static_cast<CDate_std::TMonth>(m_Month));
257  if (m_Day > 0) {
258  m_Date->SetStd().SetDay(static_cast<CDate_std::TDay>(m_Day));
259  } else {
260  m_Date->SetStd().ResetDay();
261  }
262  } else {
263  m_Date->SetStd().ResetMonth();
264  m_Date->SetStd().ResetDay();
265  }
266  return true;
267 }
268 
269 
270 static bool s_IsDateEmpty (CRef<CDate> a_date)
271 {
272  if (!a_date || !a_date->IsStd() || !a_date->GetStd().IsSetYear() || !a_date->GetStd().IsSetMonth() || !a_date->GetStd().IsSetDay()) {
273  return true;
274  } else {
275  return false;
276  }
277 }
278 
279 
281 {
282  if (s_IsDateEmpty (m_Date)) {
284  return no_date;
285  }
286 
287  CRef<CDate> a_date(new CDate());
288  a_date->Assign(*m_Date);
289 
290  return a_date;
291 }
292 
293 void CFlexibleDatePanel::SetDate( const CDate& a_date )
294 {
295  m_Date->Assign(a_date);
296 
298 }
299 
300 
302 {
303  m_Date->Reset();
305 }
306 
307 
308 /*
309  * Should we show tooltips?
310  */
311 
313 {
314  return true;
315 }
316 
317 /*
318  * Get bitmap resources
319  */
320 
321 wxBitmap CFlexibleDatePanel::GetBitmapResource( const wxString& name )
322 {
323  // Bitmap retrieval
324 ////@begin CFlexibleDatePanel bitmap retrieval
325  wxUnusedVar(name);
326  return wxNullBitmap;
327 ////@end CFlexibleDatePanel bitmap retrieval
328 }
329 
330 /*
331  * Get icon resources
332  */
333 
334 wxIcon CFlexibleDatePanel::GetIconResource( const wxString& name )
335 {
336  // Icon retrieval
337 ////@begin CFlexibleDatePanel icon retrieval
338  wxUnusedVar(name);
339  return wxNullIcon;
340 ////@end CFlexibleDatePanel icon retrieval
341 }
342 
343 
344 void CFlexibleDatePanel::SetDate(const string &date)
345 {
346  vector<string> dmy;
347  NStr::Split(date, "-", dmy);
348  m_Year = 0;
349  m_Day = 0;
350  m_Month = 0;
351  if (dmy.size() == 1)
352  {
353  m_Year = NStr::StringToInt(dmy[0]);
354  }
355  else if (dmy.size() == 2)
356  {
357  m_Month = m_MonthCtrl->FindString(wxString(dmy[0]), false);
358  m_Year = NStr::StringToInt(dmy[1]);
359  }
360  else if (dmy.size() == 3)
361  {
362  m_Day = NStr::StringToInt(dmy[0]);
363  m_Month = m_MonthCtrl->FindString(wxString(dmy[1]), false);
364  m_Year = NStr::StringToInt(dmy[2]);
365  }
366  wxPanel::TransferDataToWindow();
367  m_MonthCtrl->SetSelection(m_Month);
368  TransferDataFromWindow(); // This populates m_Date
369 }
370 
bool no_date(Parser::EFormat format, const TSeqdescList &descrs)
Definition: add.cpp:190
Definition: Date.hpp:53
void SetToolTip(const wxString &tipString)
CRef< objects::CDate > GetDate() const
CFlexibleDatePanel()
Constructors.
wxIcon GetIconResource(const wxString &name)
Retrieves icon resources.
static bool ShowToolTips()
Should we show tooltips?
void Init()
Initialises member variables.
void SetDate(const objects::CDate &a_date)
CRef< objects::CDate > m_Date
void CreateControls()
Creates the controls and sizers.
bool Create(wxWindow *parent, wxWindowID id=10042, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(400, 300), long style=wxTAB_TRAVERSAL)
Creation.
wxBitmap GetBitmapResource(const wxString &name)
Retrieves bitmap resources.
~CFlexibleDatePanel()
Destructor.
#define _(proto)
Definition: ct_nlmzip_i.h:78
USING_SCOPE(objects)
static bool s_IsDateEmpty(CRef< CDate > a_date)
#define ID_FLEXIBLEDATE_DAY
#define ID_FLEXIBLEDATE_MONTH
#define ID_FLEXIBLEDATE_YEAR
static void Init(void)
Definition: cursor6.c:76
#define NULL
Definition: ncbistd.hpp:225
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
bool IsSetYear(void) const
full year (including 1900) Check if a value has been assigned to Year data member.
Definition: Date_std_.hpp:407
bool IsStd(void) const
Check if variant Std is selected.
Definition: Date_.hpp:320
bool IsSetDay(void) const
day of month (1-31) Check if a value has been assigned to Day data member.
Definition: Date_std_.hpp:501
bool IsSetMonth(void) const
month (1-12) Check if a value has been assigned to Month data member.
Definition: Date_std_.hpp:454
const TStd & GetStd(void) const
Get the variant data.
Definition: Date_.cpp:109
END_EVENT_TABLE()
range(_Ty, _Ty) -> range< _Ty >
const struct ncbi::grid::netcache::search::fields::SIZE size
Modified on Fri Jun 14 16:51:42 2024 by modify_doxy.py rev. 669887