Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Menu.C

00001 /* $Id: Menu.C,v 1.1 2001/01/01 00:25:56 rpav Exp $
00002  *
00003  * SFW - Studio FrameWork
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Menu.C
00024  *
00025  * Menu widget implementation
00026  *
00027  */ 
00028 
00029 #include <SFW/GC.H>
00030 #include <SFW/Curses/Menu.H>
00031 
00032 TYPE_INIT(SFW::Curses::Menu);
00033 TYPE_INIT(SFW::Curses::Menu::Method);
00034 
00035 using namespace SFW;
00036 using namespace SFW::Curses;
00037 
00038 Menu::Menu(Window *_w, int _r, int _c, int _width, int _height,
00039            char **_items)
00040     : binding(this) {
00041     w      = _w;
00042     r      = _r;
00043     c      = _c;
00044     width  = _width;
00045     height = _height;
00046 
00047     curs   = 0;
00048     offs   = 0;
00049 
00050     if(_items)
00051         setItems(_items);
00052 
00053     binding.bind(Key::Up,    new Method(&Menu::select_prev));
00054     binding.bind(Key::Down,  new Method(&Menu::select_next));
00055     binding.bind(Key::Enter, new Method(&Menu::select_item));
00056     binding.bind(10,         new Method(&Menu::select_item));
00057     binding.bind(13,         new Method(&Menu::select_item));
00058     binding.bind(7,          new Method(&Menu::abort_select));
00059 }
00060 
00061 Menu::~Menu() {
00062     delItems();
00063 }
00064 
00065 void Menu::delItems() {
00066     for(Vector::iterator i = items.begin(); i != items.end(); i++)
00067         GC.done(*i);
00068 
00069     items.clear();
00070 }
00071 
00072 void Menu::setItems(char **_items) {
00073     delItems();
00074     
00075     for(int i = 0; _items[i] != 0; i++) {
00076         var *entry = new var(string(_items[i]));
00077         items.push_back(entry);
00078     }
00079 }
00080 
00081 int Menu::getIndex() {
00082     return curs + offs;
00083 }
00084 
00085 string Menu::getItem() {
00086     if(items.size() == 0)
00087         return string("");
00088 
00089     string ret = *(var*)items[curs + offs];
00090 
00091     return ret;
00092 }
00093 
00094 void Menu::draw(bool noRefresh) {
00095     for(int i = 0; i < height && (i + offs) < items.size(); i++) {
00096         string entry = *(var*)items[i + offs];
00097         
00098         if(i == curs)
00099             w->attrset(Attr::Reverse);
00100         else
00101             w->attrset(Attr::Normal);
00102 
00103         w->addstr(i + r, c, entry, width);
00104         w->addstr(string(width - entry.size(), ' '));
00105     }
00106 
00107     w->attrset(Attr::Normal);
00108 
00109     if(!noRefresh)
00110         w->refresh();
00111 }
00112 
00113 void Menu::activate() {
00114     finished = false;
00115     aborted  = false;
00116 
00117     while(!finished) {
00118         draw();
00119         binding.listen();
00120     }
00121 }
00122 
00123 Object* Menu::select_next(Object*) {
00124     curs++;
00125 
00126     if(curs + offs >= items.size())
00127         curs = (height < items.size() ? height : items.size()) - 1;
00128     else if(curs >= height) {
00129         curs = height - 1;
00130         offs++;
00131     }
00132     
00133     return NULL;
00134 }
00135 
00136 Object* Menu::select_prev(Object*) {
00137     curs--;
00138 
00139     if(curs < 0) {
00140         curs = 0;
00141 
00142         if(offs)
00143             offs--;
00144     }
00145 
00146     return NULL;
00147 }
00148 
00149 Object* Menu::select_item(Object*) {
00150     finished = true;
00151 
00152     return NULL;
00153 }
00154 
00155 Object* Menu::abort_select(Object*) {
00156     finished = true;
00157     aborted  = true;
00158 
00159     return NULL;
00160 }

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000