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