Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef HAVE_CONFIG_H
00022 # include <config.h>
00023 #endif
00024
00025 #ifdef HAVE_ASSERT_H
00026 # include <assert.h>
00027 #endif
00028
00029 #include "../intl.h"
00030 #include "checkboxgroup.h"
00031 #include "colors.h"
00032
00033 #include "basewindow.h"
00034
00035 using namespace YAPET::UI;
00036
00037 inline int
00038 CheckBoxGroup::ItemsUsableWidth() const {
00039 return width - BASE_WIDTH;
00040 }
00041
00042 inline int
00043 CheckBoxGroup::ItemsUsableHeight() const {
00044 return height - BASE_HEIGHT;
00045 }
00046
00047 inline int
00048 CheckBoxGroup::lastItemPos() const {
00049 int max_pos = items.size() > (std::vector<std::string>::size_type) ItemsUsableHeight() ?
00050 ItemsUsableHeight() : items.size();
00051 return max_pos > MAX_ITEMS ? MAX_ITEMS : max_pos;
00052 }
00053
00054 void
00055 CheckBoxGroup::addCheckBox (int pos) throw (UIException) {
00056 assert ( (pos > -1) && (pos <= lastItemPos() ) );
00057 int retval = mymvwaddstr (window,
00058 1 + pos,
00059 2,
00060 "[ ]");
00061
00062 if (retval == ERR)
00063 throw UIException (_ ("Error printing check box") );
00064 }
00065
00066 void
00067 CheckBoxGroup::setCheckMark (int pos, bool mark) throw (UIException) {
00068 assert ( (pos > -1) && (pos <= lastItemPos() ) );
00069 int retval = mymvwaddstr (window,
00070 1 + pos,
00071 3,
00072 (mark ? "X" : " ") );
00073
00074 if (retval == ERR)
00075 throw UIException (_ ("Error printing check box") );
00076 }
00077
00078 void
00079 CheckBoxGroup::setCheckMark (int pos) throw (UIException) {
00080 assert ( (pos > -1) && (pos <= lastItemPos() ) );
00081 setCheckMark (pos, ( options & (1 << pos) ? true : false) );
00082 }
00083
00084 void
00085 CheckBoxGroup::setCursor (int pos) throw (UIException) {
00086 assert ( (pos > -1) && (pos <= lastItemPos() ) );
00087 int retval = wmove (window, 1 + pos, 3);
00088
00089 if (retval == ERR)
00090 throw UIException (_ ("Error moving cursor for widget") );
00091 }
00092
00093 void
00094 CheckBoxGroup::displayItems() throw (UIException) {
00095 for (int i = 0; i < lastItemPos(); i++) {
00096 addCheckBox (i);
00097 setCheckMark (i);
00098 int retval = mymvwaddnstr (window,
00099 1 + i,
00100 6,
00101 items[i].c_str(),
00102 ItemsUsableWidth() );
00103
00104 if (retval == ERR)
00105 throw UIException (_ ("Error printing check box item") );
00106 }
00107 }
00108
00109 void
00110 CheckBoxGroup::createWindow() throw (UIException) {
00111 window = newwin (height, width, start_y, start_x);
00112
00113 if (window == NULL)
00114 throw UIException (_ ("Error creating checkbox window") );
00115
00116 Colors::setcolor (window, CHECKBOXGROUP);
00117 int retval = keypad (window, true);
00118
00119 if (retval == ERR)
00120 throw UIException (_ ("Error enabling keypad") );
00121 }
00122
00123 CheckBoxGroup::CheckBoxGroup (std::string t,
00124 const std::vector<std::string>& it,
00125 uint16_t o,
00126 int x,
00127 int y,
00128 int w,
00129 int h) throw (UIException) : title (t),
00130 items (it),
00131 options (o),
00132 start_x (x),
00133 start_y (y),
00134 width (w),
00135 height (h) {
00136
00137 if ( x == -1 ||
00138 y == -1 ||
00139 width == -1 ||
00140 height == -1 )
00141 throw UIException (_ ("No idea of the dimension of the widget") );
00142
00143 createWindow();
00144 }
00145
00146 CheckBoxGroup::~CheckBoxGroup() {
00147 assert (window != NULL);
00148 delwin (window);
00149 }
00150
00151 void
00152 CheckBoxGroup::refresh() throw (UIException) {
00153 Colors::setcolor (window, CHECKBOXGROUP);
00154 int retval = werase (window);
00155
00156 if (retval == ERR)
00157 throw UIException (_ ("Error erasing window") );
00158
00159 retval = box (window, 0, 0);
00160
00161 if (retval == ERR)
00162 throw UIException (_ ("Error setting border") );
00163
00164 Colors::setcolor (window, CHECKBOXGROUP_TITLE);
00165 retval = mymvwaddstr (window,
00166 0,
00167 2,
00168 title.c_str() );
00169
00170 if (retval == ERR)
00171 throw UIException (_ ("Error printing title") );
00172
00173 Colors::setcolor (window, MESSAGEBOX);
00174 displayItems();
00175 retval = wrefresh (window);
00176
00177 if (retval == ERR)
00178 throw UIException (_ ("Error refreshing message box") );
00179 }
00180
00191 int
00192 CheckBoxGroup::focus() throw (UIException) {
00193 refresh();
00194 int cur_pos = 0;
00195 visibleCursor (true);
00196 int ch = 0;
00197 int retval = 0;
00198
00199 while (1) {
00200 retval = wmove (window, 1 + cur_pos, 3);
00201
00202 if (retval == ERR)
00203 throw UIException (_ ("Error moving cursor for widget") );
00204
00205 ch = wgetch (window);
00206
00207 switch (ch) {
00208
00209 #ifdef HAVE_WRESIZE
00210 case KEY_RESIZE:
00211 #endif // HAVE_WRESIZE
00212 case KEY_ESC:
00213 case KEY_TAB:
00214 goto BAILOUT;
00215 case KEY_UP:
00216 case 'k':
00217
00218 if ( cur_pos == 0 ) {
00219
00220 cur_pos = lastItemPos();
00221 } else {
00222 cur_pos --;
00223 }
00224
00225 break;
00226 case KEY_DOWN:
00227 case 'j':
00228
00229 if ( cur_pos < lastItemPos() - 1 ) {
00230
00231 cur_pos ++;
00232 } else {
00233 cur_pos = 0;
00234 }
00235
00236 break;
00237 case KEY_ENTER:
00238 case KEY_SPACE:
00239 case '\n':
00240 options ^= (1 << cur_pos);
00241 setCheckMark (cur_pos);
00242 break;
00243 case KEY_REFRESH:
00244 BaseWindow::refreshAll();
00245 break;
00246 default:
00247 break;
00248 }
00249 }
00250
00251 BAILOUT:
00252 visibleCursor (false);
00253 return ch;
00254 }
00255
00256 void
00257 CheckBoxGroup::resize (int sx, int sy, int w, int h) throw (UIException) {
00258 int retval = delwin (window);
00259
00260 if (retval == ERR)
00261 throw UIException (_ ("Error deleting message box") );
00262
00263 start_x = sx;
00264 start_y = sy;
00265 width = w;
00266 height = h;
00267 createWindow();
00268 }