libyui-ncurses  2.48.3
NCTimeField.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: NCTimeField.cc
21 
22  Author: Angelo Naselli <anaselli@linux.it>
23 
24 /-*/
25 #include <climits>
26 #include <iostream>
27 #include <sstream>
28 
29 #define YUILogComponent "ncurses"
30 #include <yui/YUILog.h>
31 #include "NCurses.h"
32 #include "NCTimeField.h"
33 #include "NCInputTextBase.h"
34 
35 #include <wctype.h> // iswalnum()
36 
37 
38 const unsigned NCTimeField::fieldLength = 8;
39 
40 NCTimeField::NCTimeField ( YWidget * parent,
41  const std::string & nlabel )
42  : YTimeField ( parent, nlabel )
43  , NCInputTextBase ( parent, false, fieldLength, fieldLength )
44 {
45  yuiDebug() << std::endl;
46 
47  setLabel ( nlabel );
48 
49  setValue ( "00:00:00" );
50 }
51 
52 
53 
54 NCTimeField::~NCTimeField()
55 {
56  yuiDebug() << std::endl;
57 }
58 
59 
60 
61 int NCTimeField::preferredWidth()
62 {
63  return NCInputTextBase::preferredWidth();
64 }
65 
66 
67 
68 int NCTimeField::preferredHeight()
69 {
70  return NCInputTextBase::preferredHeight();
71 }
72 
73 
74 
75 void NCTimeField::setSize ( int newwidth, int newheight )
76 {
77  NCInputTextBase::setSize ( newwidth, newheight );
78 }
79 
80 
81 void NCTimeField::setEnabled ( bool do_bv )
82 {
83  NCWidget::setEnabled ( do_bv );
84  YWidget::setEnabled ( do_bv );
85 }
86 
87 void NCTimeField::setLabel ( const std::string & nlabel )
88 {
89  _label = NCstring ( nlabel );
90  _label.stripHotkey();
91  YTimeField::setLabel ( nlabel );
92  setDefsze();
93  Redraw();
94 }
95 
96 
97 bool NCTimeField::validTime(const std::string& input_time)
98 {
99  tm tm1;
100  std::stringstream ss;
101  ss << input_time;
102  char c;
103 
104  if (!(ss >> tm1.tm_hour))
105  return false;
106  ss >> c;
107 
108  if (!(ss >> tm1.tm_min))
109  return false;
110  ss >> c;
111 
112  if (!(ss >> tm1.tm_sec))
113  return false;
114 
115  return (tm1.tm_hour<=23 && tm1.tm_min <= 59 && tm1.tm_sec <= 59);
116 }
117 
118 
119 void NCTimeField::setValue ( const std::string & ntext )
120 {
121  if (validTime(ntext))
122  {
123  buffer = NCstring ( ntext ).str();
124 
125  if ( buffer.length() > maxFldLength )
126  {
127  buffer = buffer.erase ( maxFldLength );
128  }
129 
130  fldstart = 0;
131 
132  tUpdate();
133  }
134 }
135 
136 
137 
138 std::string NCTimeField::value( )
139 {
140  NCstring text ( buffer );
141 
142  return text.Str();
143 }
144 
145 
146 NCursesEvent NCTimeField::wHandleInput ( wint_t key )
147 {
148  NCursesEvent ret = NCursesEvent::none;
149  bool beep = false;
150  bool update = true;
151 
152  switch ( key )
153  {
154  case '\b': //ctrl-h
155  case 0x7f: //del
156  case KEY_BACKSPACE:
157 
158  buffer.erase ( curpos, 1 );
159  buffer.insert ( std::wstring::size_type ( curpos ), 1, '0' );
160  if ( curpos == 3 || curpos == 6 )
161  curpos -= 2;
162  else
163  if ( curpos )
164  curpos--;
165 
166  break;
167 
168  case KEY_DC:
169 
170  if ( curpos < buffer.length() )
171  {
172  buffer.erase ( curpos, 1 );
173  buffer.insert ( std::wstring::size_type ( curpos ), 1, '0' );
174  }
175  else
176  {
177  update = false;
178  beep = true;
179  }
180 
181  break;
182 
183  case KEY_HOME:
184 
185  if ( curpos )
186  {
187  curpos = 0;
188  }
189  else
190  {
191  update = false;
192  beep = true;
193  }
194 
195  break;
196 
197  case KEY_END:
198 
199  if ( curpos < maxCursor() )
200  {
201  curpos = maxCursor();
202  }
203  else
204  {
205  update = false;
206  beep = true;
207  }
208 
209  break;
210 
211  case KEY_LEFT:
212 
213  if ( curpos == 3 || curpos == 6 )
214  curpos -= 2;
215  else
216  if ( curpos )
217  {
218  --curpos;
219  }
220  else
221  {
222  update = false;
223  beep = true;
224  }
225 
226  break;
227 
228  case KEY_RIGHT:
229 
230  if ( curpos == 1 || curpos == 4 )
231  curpos += 2;
232  else
233  if ( curpos < maxCursor() )
234  {
235  ++curpos;
236  }
237  else
238  {
239  update = false;
240  beep = true;
241  }
242 
243  break;
244 
245  case KEY_RETURN:
246  update = false;
247 
248  if ( notify() || returnOnReturn_b )
249  ret = NCursesEvent::Activated;
250 
251  break;
252 
253  case KEY_HOTKEY:
254  update = false;
255 
256  break;
257 
258  default:
259  bool is_special = false;
260 
261  if ( key > 0xFFFF )
262  {
263  is_special = true;
264  key -= 0xFFFF;
265  }
266 
267  if ( ( !is_special && KEY_MIN < key && KEY_MAX > key )
268  ||
269  !iswprint ( key ) )
270  {
271  update = false;
272  beep = true;
273  }
274  else
275  {
276  {
277  switch ( key )
278  {
279  case L'0':
280  case L'1':
281  case L'2':
282  case L'3':
283  case L'4':
284  case L'5':
285  case L'6':
286  case L'7':
287  case L'8':
288  case L'9':
289  {
290  std::string buf = NCstring(buffer).Str();
291  buffer.erase ( curpos, 1 );
292  buffer.insert ( std::wstring::size_type ( curpos ), 1, key );
293 
294  if (validTime(NCstring(buffer).Str()))
295  {
296  if ( curpos == 1 || curpos == 4 )
297  curpos += 2;
298  else if ( curpos < maxCursor() )
299  ++curpos;
300  }
301  else
302  {
303  update = false;
304  setValue(buf);
305  beep = true;
306  }
307  }
308  break;
309 
310  default:
311  update = false;
312  beep = true;
313  break;
314  }
315  }
316 
317  }
318 
319  break;
320  }
321 
322  if ( update )
323  {
324  tUpdate();
325 
326  if ( notify() )
327  ret = NCursesEvent::ValueChanged;
328  }
329 
330  if ( beep )
331  ::beep();
332 
333  return ret;
334 
335 }
336 
337 
NCInputTextBase
Definition: NCInputTextBase.h:33
NCstring
Definition: NCstring.h:32
NCWidget::setEnabled
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
NCTimeField::setEnabled
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCTimeField.cc:81
NCursesEvent
Definition: NCurses.h:72