option.cc
Go to the documentation of this file.
1 
2 #include "stage.hh"
3 #include "worldfile.hh"
4 #include "option.hh"
5 #include "canvas.hh"
6 using namespace Stg;
7 
8 
9 Option::Option( const std::string& n,
10  const std::string& tok,
11  const std::string& key,
12  bool v,
13  World* world ) :
14  optName( n ),
15  value( v ),
16  wf_token( tok ),
17  shortcut( key ),
18  menu( NULL ),
19  menuCb( NULL ),
20  _world( world ),
21  htname( n )
22 {
23  /* do nothing */
24 }
25 
26 Fl_Menu_Item* getMenuItem( Fl_Menu_* menu, int i )
27 {
28  const Fl_Menu_Item* mArr = menu->menu();
29  return const_cast<Fl_Menu_Item*>( &mArr[ i ] );
30 }
31 
32 
33 void Option::Load( Worldfile* wf, int section )
34 {
35  //printf( "loading wf key %s\n", wf_token.c_str() );
36  set( (bool)wf->ReadInt( section, wf_token.c_str(), value ));
37 }
38 
39 void Option::Save( Worldfile* wf, int section )
40 {
41  wf->WriteInt(section, wf_token.c_str(), value );
42 }
43 
44 void Option::toggleCb( Fl_Widget* w, void* p )
45 {
46  //Fl_Menu_* menu = static_cast<Fl_Menu_*>( w );
47  Option* opt = static_cast<Option*>( p );
48  opt->invert();
49  if ( opt->menuCb )
50  opt->menuCb( opt->menuCbWidget, opt );
51 }
52 
53 void Option::menuCallback( Fl_Callback* cb, Fl_Widget* w ) {
54  menuCb = cb;
55  menuCbWidget = w;
56 }
57 
58 void Option::createMenuItem( Fl_Menu_Bar* m, std::string path )
59 {
60  menu = m;
61  path = path + "/" + optName;
62  // create a menu item and save its index
63  menuIndex = menu->add( path.c_str(), shortcut.c_str(),
64  toggleCb, this,
65  FL_MENU_TOGGLE | (value ? FL_MENU_VALUE : 0 ) );
66 }
67 
68 void Option::set( bool val )
69 {
70  value = val;
71 
72  if( menu ) {
73  Fl_Menu_Item* item = getMenuItem( menu, menuIndex );
74  value ? item->set() : item->clear();
75  }
76 
77  if( _world ) {
78  WorldGui* wg = dynamic_cast< WorldGui* >( _world );
79  if( wg == NULL ) return;
80  Canvas* canvas = wg->GetCanvas();
81  canvas->invalidate();
82  canvas->redraw();
83  }
84 }
bool val() const
Definition: option.hh:43
Canvas * GetCanvas(void) const
Definition: stage.hh:1592
World class
Definition: stage.hh:814
The Stage library uses its own namespace.
Definition: canvas.hh:8
static void toggleCb(Fl_Widget *w, void *p)
Definition: option.cc:44
Fl_Callback * menuCb
Definition: option.hh:30
void invert()
Definition: option.hh:51
std::string wf_token
Definition: option.hh:26
Fl_Widget * menuCbWidget
Definition: option.hh:31
void Save(Worldfile *wf, int section)
Definition: option.cc:39
void WriteInt(int entity, const char *name, int value)
Definition: worldfile.cc:1409
void set(bool val)
Definition: option.cc:68
void createMenuItem(Fl_Menu_Bar *menu, std::string path)
Definition: option.cc:58
World * _world
Definition: option.hh:32
void menuCallback(Fl_Callback *cb, Fl_Widget *w)
Definition: option.cc:53
Option(const std::string &n, const std::string &tok, const std::string &key, bool v, World *world)
Definition: option.cc:9
std::string optName
Definition: option.hh:23
bool value
Definition: option.hh:24
void Load(Worldfile *wf, int section)
Definition: option.cc:33
int ReadInt(int entity, const char *name, int value)
Definition: worldfile.cc:1398
std::string shortcut
Definition: option.hh:27
int menuIndex
Definition: option.hh:29
Fl_Menu_Item * getMenuItem(Fl_Menu_ *menu, int i)
Definition: option.cc:26
Fl_Menu_ * menu
Definition: option.hh:28


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09