libyui-ncurses  2.47.6
NCInputTextBase.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: NCInputText.cc
21 
22  Author: Angelo Naselli <anaselli@linux.it>
23 
24 /-*/
25 #include <climits>
26 
27 
28 #define YUILogComponent "ncurses"
29 #include <yui/YUILog.h>
30 #include "NCurses.h"
31 #include "NCInputTextBase.h"
32 
33 #include <wctype.h> // iswalnum()
34 
35 
36 NCInputTextBase::NCInputTextBase ( YWidget * parent,
37  bool passwordMode,
38  unsigned maxInput,
39  unsigned maxFld )
40  : NCWidget ( parent )
41  , passwd ( passwordMode )
42  , lwin ( 0 )
43  , twin ( 0 )
44  , maxFldLength ( maxFld )
45  , maxInputLength ( maxInput )
46  , fldstart ( 0 )
47  , fldlength ( 0 )
48  , curpos ( 0 )
49  , returnOnReturn_b ( false )
50 {
51  yuiDebug() << std::endl;
52 
53  if ( maxInputLength &&
54  ( !maxFldLength || maxFldLength > maxInputLength ) )
55  {
56  maxFldLength = maxInputLength;
57  }
58 
59  hotlabel = &_label;
60 }
61 
62 
63 
64 NCInputTextBase::~NCInputTextBase()
65 {
66  delete lwin;
67  delete twin;
68  yuiDebug() << std::endl;
69 }
70 
71 
72 
73 int NCInputTextBase::preferredWidth()
74 {
75  return wGetDefsze().W;
76 }
77 
78 
79 
80 int NCInputTextBase::preferredHeight()
81 {
82  return wGetDefsze().H;
83 }
84 
85 
86 
87 void NCInputTextBase::setEnabled ( bool do_bv )
88 {
89  NCWidget::setEnabled ( do_bv );
90 }
91 
92 
93 
94 void NCInputTextBase::setSize ( int newwidth, int newheight )
95 {
96  wRelocate ( wpos ( 0 ), wsze ( newheight, newwidth ) );
97 }
98 
99 
100 
101 void NCInputTextBase::setDefsze()
102 {
103  unsigned defwidth = maxFldLength ? maxFldLength : 5;
104 
105  if ( _label.width() > defwidth )
106  defwidth = _label.width();
107 
108  defsze = wsze ( _label.height() + 1, defwidth );
109 }
110 
111 
112 
113 void NCInputTextBase::wCreate ( const wrect & newrect )
114 {
115  NCWidget::wCreate ( newrect );
116 
117  if ( !win )
118  return;
119 
120  wrect lrect ( 0, wsze::min ( newrect.Sze,
121  wsze ( _label.height(), newrect.Sze.W ) ) );
122 
123  if ( lrect.Sze.H == newrect.Sze.H )
124  lrect.Sze.H -= 1;
125 
126  wrect trect ( 0, wsze ( 1, newrect.Sze.W ) );
127 
128  trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
129 
130  lwin = new NCursesWindow ( *win,
131  lrect.Sze.H, lrect.Sze.W,
132  lrect.Pos.L, lrect.Pos.C,
133  'r' );
134 
135  twin = new NCursesWindow ( *win,
136  trect.Sze.H, trect.Sze.W,
137  trect.Pos.L, trect.Pos.C,
138  'r' );
139 
140  if ( maxFldLength && maxFldLength < ( unsigned ) newrect.Sze.W )
141  trect.Sze.W = maxFldLength;
142 
143  fldlength = trect.Sze.W;
144 }
145 
146 
147 
148 void NCInputTextBase::wDelete()
149 {
150  delete lwin;
151  delete twin;
152  lwin = 0;
153  twin = 0;
154  NCWidget::wDelete();
155 }
156 
157 
158 void NCInputTextBase::wRedraw()
159 {
160  if ( !win )
161  return;
162 
163  // label
164  const NCstyle::StWidget & style ( widgetStyle ( true ) );
165 
166  lwin->bkgd ( style.plain );
167 
168  lwin->clear();
169 
170  _label.drawAt ( *lwin, style );
171 
172  tUpdate();
173 }
174 
175 
176 
177 bool NCInputTextBase::bufferFull() const
178 {
179  return ( maxInputLength && buffer.length() == maxInputLength );
180 }
181 
182 
183 
184 unsigned NCInputTextBase::maxCursor() const
185 {
186  return ( bufferFull() ? buffer.length() - 1 : buffer.length() );
187 }
188 
189 
190 
191 void NCInputTextBase::tUpdate()
192 {
193  if ( !win )
194  return;
195 
196  unsigned maxc = maxCursor();
197 
198  // adjust cursor
199  if ( curpos > maxc )
200  {
201  curpos = maxc;
202  }
203 
204  // adjust fldstart that cursor is visible
205  if ( maxc < fldlength )
206  {
207  fldstart = 0;
208  }
209  else
210  {
211  if ( curpos <= fldstart )
212  {
213  fldstart = curpos ? curpos - 1 : 0;
214  }
215 
216  if ( curpos >= fldstart + fldlength - 1 )
217  {
218  fldstart = curpos + ( curpos == maxc ? 1 : 2 ) - fldlength;
219  }
220  }
221 
222  const NCstyle::StWidget & style ( widgetStyle() );
223 
224  twin->bkgd ( widgetStyle ( true ).plain );
225 
226  twin->move ( 0, 0 );
227 
228  unsigned i = 0;
229 
230  unsigned end = fldlength;
231 
232  const wchar_t * cp = buffer.data() + fldstart;
233 
234  // draw left scrollhint if
235  if ( *cp && fldstart )
236  {
237  twin->bkgdset ( style.scrl );
238  twin->addch ( ACS_LARROW );
239  ++i;
240  ++cp;
241  }
242 
243  // check for right scrollhint
244  if ( fldstart + fldlength <= maxc )
245  {
246  --end;
247  }
248 
249  // draw field
250  twin->bkgdset ( style.data );
251 
252  for ( /*adjusted i*/; *cp && i < end; ++i )
253  {
254  if ( passwd )
255  {
256  twin->addwstr ( L"*" );
257  }
258  else
259  {
260  twin->addwstr ( cp, 1 );
261  }
262 
263  ++cp;
264  }
265 
266  twin->bkgdset ( style.plain );
267 
268  for ( /*adjusted i*/; i < end; ++i )
269  {
270  twin->addch ( ACS_CKBOARD );
271  }
272 
273  // draw right scrollhint if
274  if ( end < fldlength )
275  {
276  twin->bkgdset ( style.scrl );
277  twin->addch ( ACS_RARROW );
278  }
279 
280  // reverse curpos
281  if ( GetState() == NC::WSactive )
282  {
283  twin->move ( 0, curpos - fldstart );
284  twin->bkgdset ( wStyle().cursor );
285 
286  if ( curpos < buffer.length() )
287  twin->add_attr_char( );
288  else
289  twin->addch ( ACS_CKBOARD );
290  }
291 
292  Update();
293 }
294 
295 
296 
C++ class for windows.
Definition: ncursesw.h:904
int clear()
Clear the window.
Definition: ncursesw.h:1522
int bkgd(const chtype ch)
Set the background property and apply it to the window.
Definition: ncursesw.h:1443
void bkgdset(chtype ch)
Set the background property.
Definition: ncursesw.h:1448
Definition: position.h:109
int addwstr(const wchar_t *str, int n=-1)
Write the wchar_t str to the window, stop writing if the terminating NUL or the limit n is reached...
Definition: ncursesw.cc:123
int add_attr_char(int y, int x)
Put attributed character from given position to the window.
Definition: ncursesw.cc:166
int addch(const char ch)
Put attributed character to the window.
Definition: ncursesw.h:1228
int move(int y, int x)
Move cursor the this position.
Definition: ncursesw.h:1155
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
Definition: position.h:154
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.