// dynmenu.cpp -- manage dynamic menus // // Written by Frederic Bouvier, started February 2004. // // Copyright (C) 2004 Frederic Bouvier - fredb@users.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: dynmenu.cpp,v 1.1 2004/02/14 14:17:38 fredb2 Exp $ #include "dynmenu.hpp" #include FGSD_DynamicSubMenu::FGSD_DynamicSubMenu() : _menu( 0 ) , _first( 0 ) , _last( 0 ) , _next( 0 ) { } void FGSD_DynamicSubMenu::init( Fl_Menu_ *menu, const char *parentLabel, int position, FGSD_DynamicMenu *next ) { _menu = menu; _parentLabel = parentLabel; _first = position + 1; _last = position + 2; _next = next; } void FGSD_DynamicSubMenu::clear() { while ( _last > _first ) { _menu->remove( _first ); _last -= 1; if ( _next ) _next->decrement(); } } int FGSD_DynamicSubMenu::add( const char *label, int shortcut, Fl_Callback *callback, void *user_data, int flags ) { std::ostringstream str; str << _parentLabel; std::string text = label; size_t pos; while ( ( pos = text.find( '/' ) ) != std::string::npos ) { str << text.substr( 0, pos ) << "\\/"; text.erase( 0, pos + 1 ); } if ( text.size() ) str << text; int ret = _menu->add( str.str().c_str(), shortcut, callback, user_data, flags ); _last += 1; if ( _next ) _next->increment(); return ret; } int FGSD_DynamicSubMenu::addNone() { std::string text = _parentLabel; text += "None"; int ret = _menu->add( text.c_str(), 0, 0, 0, FL_MENU_INACTIVE ); _last += 1; if ( _next ) _next->increment(); return ret; } void FGSD_DynamicSubMenu::increment() { _first += 1; _last += 1; if ( _next ) _next->increment(); } void FGSD_DynamicSubMenu::decrement() { if ( _next ) _next->decrement(); _first -= 1; _last -= 1; } FGSD_DynamicMenuItem::FGSD_DynamicMenuItem() : _menu( 0 ) , _pos( 0 ) , _next( 0 ) { } void FGSD_DynamicMenuItem::init( Fl_Menu_ *menu, const char *parentLabel, int position, FGSD_DynamicMenu *next ) { _menu = menu; _parentLabel = parentLabel; _pos = position; _next = next; } void FGSD_DynamicMenuItem::increment() { _pos += 1; if ( _next ) _next->increment(); } void FGSD_DynamicMenuItem::decrement() { if ( _next ) _next->decrement(); _pos -= 1; } void FGSD_DynamicMenuItem::label( const char *t ) { _label = t; _menu->replace( _pos, _label.c_str() ); } void FGSD_DynamicMenuItem::activate() { _menu->mode( _pos, _menu->mode( _pos ) & ~FL_MENU_INACTIVE ); } void FGSD_DynamicMenuItem::deactivate() { _menu->mode( _pos, _menu->mode( _pos ) | FL_MENU_INACTIVE ); }