00001
00023 #ifndef _MENU_H
00024 #define _MENU_H
00025
00026
00027 #include <GL/glut.h>
00028 #include <map>
00029 #include <KeyBoard.H>
00030 #include <ControlGroup.H>
00031 #include <UserInterface.H>
00032
00033 namespace xchen
00034 {
00035
00037 class UI :: Menu
00038 {
00039 static map<MenuID, Menu*, less<int> > menuID2menu;
00040
00041 static void nctrl_keyboard_cb(ConvertedKeyValue item_id, int x, int y);
00042 static void menu_cb(int item_id);
00043 public:
00044 enum { max_items = 1000 };
00045
00046 static Menu* GetMenu(MenuID menu_id) { return menuID2menu[menu_id]; }
00047 public:
00048 Menu(KeyBoard* kb,const string& nm="") : name(nm) { init_menu(kb); }
00049 virtual ~Menu() { glutDestroyMenu(menu_id); menuID2menu.erase(menu_id); }
00050
00051 KeyBoard& GetKeyBoard() { return *keyboard; }
00052
00053 ItemID AddItem(string str, KeyEventCB cb, KeyValue k=0);
00054 ItemID AddGlobalItem(string str, KeyEventCB cb, KeyValue k=0);
00055 ItemID AddItem(string str, KeyEventCB cb, KeyValue k, ConvertedKeyValue k2);
00057 void AddSubMenu(const string& doc_str, MenuID sub_menu_id);
00058 void AddSeparator(const string& str = " ") { AddItem(str, KeyBoard::null_cb); }
00059
00060
00061 MenuID GetMenuID() const { return menu_id; }
00062 void Attach(int button = GLUT_RIGHT_BUTTON);
00063 void Detach(int button = GLUT_RIGHT_BUTTON);
00064
00065 protected:
00066 const string name;
00067 MenuID menu_id;
00068 int cur_item_id;
00069
00070 vector<MenuID> submenus;
00071
00072 KeyValue item2key[max_items];
00073 KeyBoard* keyboard;
00074
00075 void init_menu(KeyBoard* kb);
00076 Menu(const Menu&) { }
00077 Menu& operator=(const Menu&) { return *this; }
00078
00079 };
00080
00081 inline UI :: ItemID UI :: Menu :: AddItem(string str, KeyEventCB cb, KeyValue k)
00082 {
00083 return AddItem(str, cb, k, cur_item_id);
00084 }
00085
00086 inline UI :: ItemID UI :: Menu :: AddGlobalItem(string str, KeyEventCB cb, KeyValue k)
00087 {
00088 return AddItem(str,cb,k,menu_id*max_items + cur_item_id);
00089 }
00090
00091
00092 }
00093 #endif