• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ui/button.cc

Go to the documentation of this file.
00001 // $Id: button.cc 3342 2010-09-17 18:32:00Z java $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
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     //refresh();
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     // Simulate a tab key and bail out
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 }

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1