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 #include "../intl.h"
00022 #include "button.h"
00023 #include "colors.h"
00024 #include "basewindow.h"
00025
00026 using namespace YAPET::UI;
00027
00028 void
00029 Button::createWindow() throw (UIException) {
00030 window = newwin (1, BASE_SIZE + label.length(), start_y, start_x);
00031
00032 if (window == NULL)
00033 throw UIException (_ ("Error creating button") );
00034
00035
00036 }
00037
00038 Button::Button (std::string l, int x, int y, bool ro) : window (NULL),
00039 label (l),
00040 start_x (x),
00041 start_y (y),
00042 readonly (ro) {
00043 createWindow();
00044 }
00045
00046 Button::~Button() {
00047 wclear (window);
00048 delwin (window);
00049 }
00050
00051
00052 void
00053 Button::setLabel (std::string l) throw (UIException) {
00054 label = l;
00055 int retval = wclear (window);
00056
00057 if (retval == ERR)
00058 throw UIException (_ ("Error clearing button") );
00059
00060 retval = wrefresh (window);
00061
00062 if (retval == ERR)
00063 throw UIException (_ ("Error refreshing button") );
00064
00065 retval = delwin (window);
00066
00067 if (retval == ERR)
00068 throw UIException (_ ("Error deleting button") );
00069 }
00070
00071 void
00072 Button::refresh() throw (UIException) {
00073 Colors::setcolor (window, BUTTON_NOFOCUS);
00074 int retval = werase (window);
00075
00076 if (retval == ERR)
00077 throw UIException (_ ("Error erasing button") );
00078
00079 mvwprintw (window, 0, 0, "[ %s ]", label.c_str() );
00080 retval = wrefresh (window);
00081
00082 if (retval == ERR)
00083 throw UIException (_ ("Error refreshing button") );
00084 }
00085
00086 int
00087 Button::focus() throw (UIException) {
00088 Colors::setcolor (window, BUTTON_FOCUS);
00089 mvwprintw (window, 0, 0, "[ %s ]", label.c_str() );
00090 int retval = touchwin (window);
00091
00092 if (retval == ERR)
00093 throw UIException (_ ("Error touching window") );
00094
00095 retval = wrefresh (window);
00096
00097 if (retval == ERR)
00098 throw UIException (_ ("Error refreshing button") );
00099
00100 retval = keypad (window, TRUE);
00101
00102 if (retval == ERR)
00103 throw UIException (_ ("Error setting keypad") );
00104
00105 int ch;
00106 if (readonly) {
00107
00108 ch = '\t';
00109 goto BAILOUT;
00110 }
00111
00112 while (true) {
00113 ch = wgetch (window);
00114
00115 switch (ch) {
00116 case '\n':
00117 case KEY_ENTER:
00118 ch = '\n';
00119 onClick();
00120 goto BAILOUT;
00121 case KEY_TAB:
00122 case KEY_LEFT:
00123 case KEY_RIGHT:
00124 case KEY_UP:
00125 case KEY_DOWN:
00126 ch = '\t';
00127 goto BAILOUT;
00128 case KEY_ESC:
00129 goto BAILOUT;
00130 case KEY_REFRESH:
00131 BaseWindow::refreshAll();
00132 break;
00133 #ifdef HAVE_WRESIZE
00134 case KEY_RESIZE:
00135 goto BAILOUT;
00136 #endif // HAVE_WRESIZE
00137 }
00138 }
00139
00140 BAILOUT:
00141 Colors::setcolor (window, BUTTON_NOFOCUS);
00142 mvwprintw (window, 0, 0, "[ %s ]", label.c_str() );
00143 retval = touchwin (window);
00144
00145 if (retval == ERR)
00146 throw UIException (_ ("Error touching window") );
00147
00148 retval = wrefresh (window);
00149
00150 if (retval == ERR)
00151 throw UIException (_ ("Error refreshing button") );
00152
00153 return ch;
00154 }