Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
windowcontroller.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #ifndef __WINDOWCONTROLLER_H__
23 #define __WINDOWCONTROLLER_H__
24 
25 #include "window.h"
26 #include "control.h"
27 
28 //This class is meant to run, or control, a Window. It probably creates
29 //the Window and its Controls, loads the controls with lists if necessary, etc.
30 //By default, this object deletes itself when the window closes.
31 //The Window passes command events to the controller, which manages any
32 //interdependencies between the controls.
33 //The controller basically manages the state changes in the application that
34 //the Window (the UI) requests.
36 {
37 public:
38 //Set up the window and get everything ready.
39 //This is separate from run() so the creator gets a chance to get involved
40 //with the window settings.
41  virtual void init( void ) = 0;
42 
43 //Make everything happen.
44  virtual void run( void );
45 
46  virtual void draw( void );
47 
48 //The window we are controlling.
49  virtual Window * window( void )
50  {
51  return m_window;
52  }
53  virtual void setWindow( Window *w )
54  {
55  m_window = w;
56  }
57 
58 //Process a command event from the window.
59  virtual bool processWindowCommand( const EventCommandId &command, Control *control );
60 
61 //CONSTRUCTION
63  virtual ~WindowController( void );
64 
65 protected:
66 
67 //VARIABLES
68  Window *m_window; //The window we control.
69  bool m_deleteOnWindowClose; //True = Delete this object when the window closes.
70 };
71 
72 template < class T >
74 {
75  typedef bool (T::*Handler)( const EventCommandId &command, Control *control );
76 
78  std::string controlId;
79  Handler function;
80  WindowControllerTableEntry( const EventCommandId &cmd, const std::string &cid, const Handler &func ) :
81  command( cmd )
82  , controlId( cid )
83  , function( func ) {}
84 };
85 
86 template < class Subclass >
87 class WctlBase : public WindowController
88 {
89 public:
91 
92  virtual bool processWindowCommand( const EventCommandId &command, Control *control )
93  {
94  //Iterate through the dispatch table.
95  for (const WctlTableEntry *p = &WctlCommandTable[0]; p->function; p++)
96  if (p->command == command)
97  if ( p->controlId.size() == 0 || p->controlId == control->id() )
98  //Found a handler for the command.
99  return ( ( static_cast< Subclass* > (this) )->*(p->function) )( command, control );
100  //Let the base class have a try at the command first.
101  if ( WindowController::processWindowCommand( command, control ) )
102  return true;
103  //Didn't find a handler.
104  return false;
105  }
106 protected:
107 //Dispatch table declarations.
108 //This is a member table so the handler functions don't need to be public.
110 };
111 
112 #endif //__WINDOWCONTROLLER_H__
113