00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "../intl.h"
00022 #include "basewindow.h"
00023 #include "inputwidget.h"
00024 #include "colors.h"
00025
00026 using namespace YAPET::UI;
00027
00028 void
00029 InputWidget::moveBackward() throw (UIException) {
00030 pos--;
00031
00032 if (pos < 0) {
00033 pos = 0;
00034 start_pos--;
00035 }
00036
00037 if (start_pos < 0)
00038 start_pos = 0;
00039
00040 refresh();
00041 }
00042
00043 void
00044 InputWidget::moveForward() throw (UIException) {
00045 if ( ( (secstring::size_type) (start_pos + pos + 1) ) > buffer.length() ) {
00046 refresh();
00047 return;
00048 }
00049
00050 if (pos + 1 > width)
00051 start_pos++;
00052 else
00053 pos++;
00054
00055 refresh();
00056 }
00057
00058 void
00059 InputWidget::moveHome() throw (UIException) {
00060 pos = 0;
00061 start_pos = 0;
00062 refresh();
00063 }
00064
00065 void
00066 InputWidget::moveEnd() throw (UIException) {
00067 if (buffer.length() < ( (secstring::size_type) width) ) {
00068 start_pos = 0;
00069 pos = buffer.length();
00070 } else {
00071 start_pos = buffer.length() - width + 1;
00072 pos = width - 1;
00073 }
00074
00075 refresh();
00076 }
00077
00078 void
00079 InputWidget::processInput (int ch) throw (UIException) {
00080 if (buffer.length() + 1 > ( (secstring::size_type) max_length) ) return;
00081 if (readonly) return;
00082
00083 if ( ( (secstring::size_type) start_pos + pos) > buffer.length() )
00084 buffer.append ("" + ch);
00085 else
00086 buffer.insert (start_pos + pos, 1, ch);
00087
00088 moveForward();
00089
00090 text_changed = true;
00091 }
00092
00093 void
00094 InputWidget::processBackspace() throw (UIException) {
00095 if (pos + start_pos == 0) return;
00096 if (readonly) return;
00097
00098 moveBackward();
00099 processDelete();
00100 }
00101
00102 void
00103 InputWidget::processDelete() throw (UIException) {
00104 if ( ( (secstring::size_type) pos + start_pos) == buffer.length() ) return;
00105 if (readonly) return;
00106
00107 buffer.erase (pos + start_pos, 1);
00108
00109 if ( ( (secstring::size_type) pos + start_pos) > buffer.length() ) {
00110 if (pos > 0)
00111 pos--;
00112 else
00113 start_pos--;
00114 }
00115
00116 refresh();
00117
00118 text_changed = true;
00119 }
00120
00121
00122 void
00123 InputWidget::createWindow (int sx, int sy, int w) throw (UIException) {
00124 if (window != NULL)
00125 throw UIException (_ ("May you consider deleting the window first before reallocating it") );
00126
00127 window = newwin (1, w, sy, sx);
00128
00129 if (window == NULL)
00130 throw UIException (_ ("Error creating the input window") );
00131
00132 Colors::setcolor (window, INPUTWIDGET_NOFOCUS);
00133 int retval = wclear (window);
00134
00135 if (retval == ERR)
00136 throw UIException (_ ("Error clearing input widget") );
00137
00138 retval = keypad (window, TRUE);
00139
00140 if (retval == ERR)
00141 throw UIException (_ ("Error setting keypad on input widget") );
00142
00143
00144 }
00145
00146 InputWidget::InputWidget (int sx, int sy, int w, int ml, bool ro)
00147 throw (UIException) : window (NULL),
00148 max_length (ml),
00149 start_pos (0),
00150 pos (0),
00151 width (w),
00152 text_changed (false),
00153 readonly (ro) {
00154 createWindow (sx, sy, w);
00155 }
00156
00157 InputWidget::InputWidget (int sx, int sy, int w, bool ro)
00158 throw (UIException) : window (NULL),
00159 max_length (DEFAULT_TEXT_LEN),
00160 start_pos (0),
00161 pos (0),
00162 width (w),
00163 text_changed (false),
00164 readonly (ro) {
00165 createWindow (sx, sy, w);
00166 }
00167
00168 InputWidget::~InputWidget() {
00169 clearText();
00170
00171
00172
00173
00174
00175 wrefresh (window);
00176 delwin (window);
00177 }
00178
00179 int
00180 InputWidget::focus() throw (UIException) {
00181 Colors::setcolor (window, INPUTWIDGET_FOCUS);
00182 int retval = wrefresh (window);
00183
00184 if (retval == ERR)
00185 throw UIException (_ ("Error refreshing the widget") );
00186
00187 retval = wmove (window, 0, pos);
00188
00189 if (retval == ERR)
00190 throw UIException (_ ("Error moving cursor for widget") );
00191
00192 visibleCursor (true);
00193 int ch;
00194
00195 while (true) {
00196 ch = wgetch (window);
00197
00198 switch (ch) {
00199
00200 #ifdef HAVE_WRESIZE
00201 case KEY_RESIZE:
00202 #endif // HAVE_WRESIZE
00203 case '\n':
00204 case KEY_TAB:
00205 case KEY_ESC:
00206 case KEY_CTRL_E:
00207 goto BAILOUT;
00208
00209 case KEY_UP:
00210 case KEY_LEFT:
00211 moveBackward();
00212 break;
00213 case KEY_DOWN:
00214 case KEY_RIGHT:
00215 moveForward();
00216 break;
00217 case KEY_END:
00218 case KEY_A1:
00219 moveEnd();
00220 break;
00221 case KEY_HOME:
00222 case KEY_C1:
00223 moveHome();
00224 break;
00225 case KEY_ENTER:
00226 ungetch ('\n');
00227 break;
00228 case KEY_DC:
00229 processDelete();
00230 break;
00231 case KEY_BACKSPACE:
00232 case 127:
00233 processBackspace();
00234 break;
00235 case KEY_REFRESH:
00236 BaseWindow::refreshAll();
00237 break;
00238 default:
00239 processInput (ch);
00240 break;
00241 }
00242 }
00243
00244 BAILOUT:
00245 visibleCursor (false);
00246 Colors::setcolor (window, INPUTWIDGET_NOFOCUS);
00247 retval = wrefresh (window);
00248
00249 if (retval == ERR)
00250 throw UIException (_ ("Error refreshing the widget") );
00251
00252 return ch;
00253 }
00254
00255 void
00256 InputWidget::refresh() throw (UIException) {
00257 int retval = werase (window);
00258
00259 if (retval == ERR)
00260 throw UIException (_ ("Error clearing input widget") );
00261
00262 if (buffer.length() > 0) {
00263 secstring sub = buffer.substr (start_pos, width);
00264 retval = mymvwaddnstr (window,
00265 0,
00266 0,
00267 sub.c_str(),
00268 width - 1);
00269
00270 if (retval == ERR)
00271 throw UIException (_ ("Error adding text to window") );
00272
00273 if (pos >= width - 1)
00274 retval = wmove (window, 0, width - 1);
00275 else
00276 retval = wmove (window, 0, pos);
00277
00278 if (retval == ERR)
00279 throw UIException (_ ("Error moving cursor") );
00280 }
00281
00282 retval = wrefresh (window);
00283
00284 if (retval == ERR)
00285 throw UIException (_ ("Error refreshing input widget") );
00286 }
00287
00288 void
00289 InputWidget::resize (int sx, int sy, int w) throw (UIException) {
00290 int retval = wclear (window);
00291
00292 if (retval == ERR)
00293 throw UIException (_ ("Error clearing input widget") );
00294
00295 retval = wrefresh (window);
00296
00297 if (retval == ERR)
00298 throw UIException (_ ("Error refreshing input widget") );
00299
00300 retval = delwin (window);
00301
00302 if (retval == ERR)
00303 throw UIException (_ ("Error deleting input widget") );
00304
00305 window = NULL;
00306 createWindow (sx, sy, w);
00307 }
00308
00309 void
00310 InputWidget::setText (secstring t) throw (UIException) {
00311 clearText();
00312 buffer = t;
00313 start_pos = 0;
00314 pos = 0;
00315 text_changed = false;
00316 refresh();
00317 }
00318
00319 void
00320 InputWidget::clearText() {
00321 for (secstring::size_type i = 0; i < buffer.length(); i++)
00322 buffer[i] = 0;
00323
00324 buffer.clear();
00325 wclear (window);
00326 }