libyui-ncurses  2.50.4
NCDateField.cc
1 /*
2  Copyright (C) 2014 Angelo Naselli
3 
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 
18 /*-/
19 
20  File: NCDateField.cc
21 
22  Author: Angelo Naselli <anaselli@linux.it>
23 
24 /-*/
25 #include <climits>
26 #include <boost/date_time/posix_time/posix_time.hpp>
27 #include <iostream>
28 #include <sstream>
29 
30 
31 #define YUILogComponent "ncurses"
32 #include <yui/YUILog.h>
33 #include "NCurses.h"
34 #include "NCDateField.h"
35 #include "NCInputTextBase.h"
36 
37 using namespace boost::gregorian;
38 
39 const unsigned NCDateField::fieldLength = 10;
40 
41 NCDateField::NCDateField ( YWidget * parent,
42  const std::string & nlabel )
43  : YDateField ( parent, nlabel )
44  , NCInputTextBase ( parent, false, fieldLength, fieldLength )
45 {
46  yuiDebug() << std::endl;
47 
48  setLabel ( nlabel );
49 
50  // Same value as QT default
51  setValue ( "2000-01-01" );
52 }
53 
54 
55 NCDateField::~NCDateField()
56 {
57  yuiDebug() << std::endl;
58 }
59 
60 int NCDateField::preferredHeight()
61 {
62  return NCInputTextBase::preferredHeight();
63 }
64 
65 int NCDateField::preferredWidth()
66 {
67  return NCInputTextBase::preferredWidth();
68 }
69 
70 void NCDateField::setSize ( int newWidth, int newHeight )
71 {
72  NCInputTextBase::setSize ( newWidth, newHeight );
73 }
74 
75 
76 void NCDateField::setEnabled ( bool do_bv )
77 {
79  YWidget::setEnabled ( do_bv );
80 }
81 
82 void NCDateField::setLabel ( const std::string & nlabel )
83 {
84  _label = NCstring ( nlabel );
85  _label.stripHotkey();
86  YDateField::setLabel ( nlabel );
87  setDefsze();
88  Redraw();
89 }
90 
91 bool NCDateField::validDate(const std::string& input_date)
92 {
93  std::wstringstream ss;
94  wdate_input_facet * fac = new wdate_input_facet(L"%Y-%m-%d");
95  ss.imbue(std::locale(std::locale::classic(), fac));
96 
97  date d;
98  ss << input_date.c_str();
99  ss >> d;
100 
101  return d != date();
102 }
103 
104 
105 void NCDateField::setValue ( const std::string & ntext )
106 {
107  if ( validDate(ntext) )
108  {
109  buffer = NCstring ( ntext ).str();
110 
111  if ( buffer.length() > maxFldLength )
112  {
113  buffer = buffer.erase ( maxFldLength );
114  }
115 
116  fldstart = 0;
117 
118  curpos = buffer.length();
119  tUpdate();
120  }
121 }
122 
123 
124 std::string NCDateField::value( )
125 {
126  NCstring text ( buffer );
127 
128  return text.Str();
129 }
130 
131 
132 NCursesEvent NCDateField::wHandleInput ( wint_t key )
133 {
134  NCursesEvent ret = NCursesEvent::none;
135  bool beep = false;
136  bool update = true;
137 
138  switch ( key )
139  {
140  case '\b': //ctrl-h
141  case 0x7f: //del
142  case KEY_BACKSPACE:
143 
144  buffer.erase ( curpos, 1 );
145  buffer.insert ( std::wstring::size_type ( curpos ), 1, '0' );
146  if ( curpos == 5 || curpos == 8 )
147  curpos -= 2;
148  else
149  if ( curpos )
150  curpos--;
151 
152  break;
153 
154  case KEY_DC:
155 
156  if ( curpos < buffer.length() )
157  {
158  buffer.erase ( curpos, 1 );
159  buffer.insert ( std::wstring::size_type ( curpos ), 1, '0' );
160  }
161  else
162  {
163  update = false;
164  beep = true;
165  }
166 
167  break;
168 
169  case KEY_HOME:
170 
171  if ( curpos )
172  {
173  curpos = 0;
174  }
175  else
176  {
177  update = false;
178  beep = true;
179  }
180 
181  break;
182 
183  case KEY_END:
184 
185  if ( curpos < maxCursor() )
186  {
187  curpos = maxCursor();
188  }
189  else
190  {
191  update = false;
192  beep = true;
193  }
194 
195  break;
196 
197  case KEY_LEFT:
198 
199  if ( curpos == 5 || curpos == 8 )
200  curpos -= 2;
201  else
202  if ( curpos )
203  {
204  --curpos;
205  }
206  else
207  {
208  update = false;
209  beep = true;
210  }
211 
212  break;
213 
214  case KEY_RIGHT:
215 
216  if ( curpos == 3 || curpos == 6 )
217  curpos += 2;
218  else
219  if ( curpos < maxCursor() )
220  {
221  ++curpos;
222  }
223  else
224  {
225  update = false;
226  beep = true;
227  }
228 
229  break;
230 
231  case KEY_RETURN:
232  update = false;
233 
234  if ( notify() || returnOnReturn_b )
235  ret = NCursesEvent::Activated;
236 
237  break;
238 
239  case KEY_HOTKEY:
240  update = false;
241 
242  break;
243 
244  default:
245  bool is_special = false;
246 
247  if ( key > 0xFFFF )
248  {
249  is_special = true;
250  key -= 0xFFFF;
251  }
252 
253  if ( ( !is_special && KEY_MIN < key && KEY_MAX > key )
254  ||
255  !iswprint ( key ) )
256  {
257  update = false;
258  beep = true;
259  }
260  else
261  {
262  {
263  switch ( key )
264  {
265  case L'0':
266  case L'1':
267  case L'2':
268  case L'3':
269  case L'4':
270  case L'5':
271  case L'6':
272  case L'7':
273  case L'8':
274  case L'9':
275  {
276  std::string buf = NCstring(buffer).Str();
277  buffer.erase ( curpos, 1 );
278  buffer.insert ( std::wstring::size_type ( curpos ), 1, key );
279  if (validDate(NCstring(buffer).Str()))
280  {
281  if ( curpos == 3 || curpos == 6 )
282  curpos += 2;
283  else
284  if ( curpos < maxCursor() )
285  ++curpos;
286  }
287  else
288  {
289  update = false;
290  setValue(buf);
291  beep = true;
292  }
293  }
294  break;
295 
296  default:
297  update = false;
298  beep = true;
299  break;
300  }
301  }
302 
303  }
304 
305  break;
306  }
307 
308  if ( update )
309  {
310  tUpdate();
311 
312  if ( notify() )
313  ret = NCursesEvent::ValueChanged;
314  }
315 
316  if ( beep )
317  ::beep();
318 
319  return ret;
320 
321 }
322 
323 
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCDateField.cc:76
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.