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

Menu.h

Go to the documentation of this file.
00001 //     $Id: Menu.h,v 1.2 2001/05/08 03:28:50 burnett Exp $
00002 //  Author: Toby Burnett
00003 //
00004 
00005 #ifndef MENU_H
00006 #define MENU_H
00007 
00008 #include "gui/Command.h"
00009 #include "gui/GUI.h"
00010 
00011 #include <vector>
00012 #include <map>
00013 
00014 namespace gui {
00015 
00016 class SubMenu;
00017 class MenuBase;
00018 
00019 class Menu {
00020 // This class manages a hierarchy of <<Command>> instances, each associated with a 
00021 // unique name. If there is an associated <<GUI>>, it will also enter each command into
00022 // the GUI's menu bar
00023 //
00024 // When started, it establishes the File menu, and adds an Exit command to it when
00025 // its run is called.
00026 //
00027 // The Menu also manages a table of keystroke commands
00028 public:
00029     friend class SubMenu;
00030 
00031     Menu(GUI&);
00032     // constuctor with a reference to the GUI
00033     
00034     ~Menu();
00035 
00036     SubMenu& subMenu(const std::string& label);
00037     // Return a SubMenu (pull-down on a GUI menu bar)
00038       
00039     SubMenu& file_menu();
00040     // Return reference to the file menu sub menu object;
00041 
00042     void add_button(const std::string& label, Command* cmd);
00043     // Add top-level button (not kosher in Motif)
00044 
00045     void register_key(char key, Command* cmd);
00046     // Register a short-cut key with a  command
00047 
00048     bool strike(char c);
00049     // Execute the command, if any, associated with the key c. 
00050     // Return true if it has been registered, false otherwise
00051 
00052     void query(const std::string& ask, int* value);
00053     // Ask the user for an int   
00054 
00055     void query(const std::string& ask, double* value, int count=1);
00056     // Ask the user for a double (or a list)
00057 
00058     void query(const std::string& ask, float* value);
00059     // Ask the user for a float
00060 
00061     void query(const std::string& ask, std::string* value);
00062     // Ask the user for a string
00063 
00064     void run(bool paused=true);
00065     // register the "quit" command after any user file/IO comamnds
00066     // start the GUI, and optionally the message loop
00067     
00068     void check_messages();
00069     // check for message in the GUI (needed for long events if single threaded
00070 
00071     void add(MenuBase* client);
00072     // client uses to schedule a call to its finishSetup method
00073     
00074     static Menu* instance(Menu* m=0); 
00075         // deprecated method
00076 private:
00077 
00078     class Node{
00079         //empty class used as tag
00080     };
00081 
00082     // following used by implementation
00083     void addCommand(const std::string& label, Command* command);
00084     GUI::Toggle* addToggle(const std::string& label, bool state, Command* set, Command* unset);
00085     Node* beginMenu(const std::string&, Node* subnode=0);
00086     void setMenu(Node* m);
00087     void endMenu();
00088     void addSeparator();
00089     void quit();
00090 
00091     static Menu* s_instance;
00092 
00093     GUI& m_gui; // reference to the GUI
00094     
00095     SubMenu*    m_fileMenu;
00096     // for adding file menu commands; set by the constructor
00097 
00098     typedef std::map<char, Command*, std::less<char> > KeyCommandMap;
00099     KeyCommandMap m_key_map;
00100     // list of keys and commands
00101     
00102     typedef    std::vector<MenuBase* > ClientList;
00103     ClientList  m_clients;
00104     // list of clients who want finishSetup to be called
00105     
00106     std::vector<SubMenu*> m_sub_menus;
00107     // list of sub menus to delete
00108 
00109     std::vector<Command*> m_commands;
00110     // list of commands to delete
00111 };
00112 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00113 template< class T >
00114 class SimpleDialogBox : public Command {
00115         // define a templated class that can be used as a command to query for a single
00116         // entity
00117 public:
00118     SimpleDialogBox(Menu* m, std::string& q, T* t)
00119         : m_menu(m),
00120         m_question(q),
00121         m_t(t){}
00122     void execute(){m_menu->query(m_question, m_t);}
00123 private:
00124     Menu* m_menu;
00125     std::string m_question;
00126     T* m_t;
00127 };
00128 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00129 class MenuBase {
00130 // Virtual base class for the templated menu client class
00131 public:
00132     MenuBase(){}
00133     virtual void finishSetup(){};
00134     virtual void quit(){};
00135 };
00136 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00137 template< class T >
00138 class MenuClient : public MenuBase {
00139     // Template class with the class of the client as the template parameter. 
00140     // If a client supports finishSetup  method, it can add
00141     // itself with the statement: "menu().add(new MenuClient<Client>(this));"
00142     // it will be called back just before starting if it implements finishSetup(),
00143     // and its quit() if the File/Exit button is pushed. 
00144 public:
00145     MenuClient(T* t):m_t(t){}
00146         // Constructor taking the client as argument
00147 
00148     void finishSetup(){m_t->finishSetup();}
00149     void quit(){m_t->quit();}
00150         // Send the finishSetup() or quit() to the client
00151     friend class Menu;
00152 private:
00153     T* m_t;
00154 };
00155 
00156 inline void Menu::add(MenuBase* client){ m_clients.push_back(client);}
00157 
00158 } //namespace gui
00159 #endif
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 

Generated at Mon Nov 26 18:18:11 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000