Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

glui_menu.C

Go to the documentation of this file.
00001 #include "glui_menu.H" 
00002 #include "glui/glui.h"
00003 
00004 ARRAY<MenuItem *> GLUIMoveMenu::_menu_items(10);
00005 
00006 GLUIMoveMenu::GLUIMoveMenu(Cstr_ptr &name, int main_window_id) :
00007    MoveMenu(name),
00008    _glui(0),
00009    _menu_created(false),
00010    _main_window_id(main_window_id),
00011    _id(-1),
00012    _item_ids(8)
00013 {
00014 }
00015 
00016 
00017 void
00018 GLUIMoveMenu::show()
00019 {
00020    if (!_glui) return;
00021 
00022    int old_id = glutGetWindow();
00023 
00024    glutSetWindow(_main_window_id);
00025    int root_x = glutGet(GLUT_WINDOW_X);
00026    int root_y = glutGet(GLUT_WINDOW_Y);
00027    int root_w = glutGet(GLUT_WINDOW_WIDTH);
00028    //  int root_h = glutGet(GLUT_WINDOW_HEIGHT);
00029 
00030    glutSetWindow(_id);
00031    glutPositionWindow(root_x + root_w + 10, root_y);
00032 
00033     glutSetWindow(old_id);
00034 
00035    if(!_is_shown) 
00036    {
00037       _glui->show();
00038       _is_shown = 1;
00039    }
00040 }
00041 
00042 //
00043 // Show menu - if recreate is true, recreate the menu
00044 //
00045 void
00046 GLUIMoveMenu::menu(int recreate)
00047 {
00048    if (recreate || !_menu_created) create_menu();
00049 }
00050 
00051 void
00052 GLUIMoveMenu::hide()
00053 {
00054    if (!_glui)
00055       return;
00056    
00057    if (_is_shown) {
00058       _glui->hide();
00059       _is_shown = 0;
00060    }
00061 }
00062 
00063 
00064 void
00065 GLUIMoveMenu::create_menu()
00066 {       
00067 
00068    // XXX -- Need to call _glui->close() to destroy the menu before
00069    // recreating it. I don't know of any other way to recreate the menu
00070    // without creating a new glui -- this is awkward!!
00071 
00072    if(_glui) {
00073       _glui->close();
00074       _glui = 0;
00075    }
00076 
00077    // If we're recreating the menu, must unmap previously mapped item ids
00078    for (int k=0; k < _item_ids.num(); k++) {
00079       unmap_menu_item(_item_ids[k]);
00080    }
00081    
00082    _item_ids.clear();
00083 
00084    // get position of main window for relative placement of menu
00085    glutSetWindow(_main_window_id);
00086    int root_x = glutGet(GLUT_WINDOW_X);
00087    int root_y = glutGet(GLUT_WINDOW_Y);
00088    int root_w = glutGet(GLUT_WINDOW_WIDTH);
00089 //   int root_h = glutGet(GLUT_WINDOW_HEIGHT);
00090    
00091    // create the glui window
00092    _glui = GLUI_Master.create_glui(*(*_name), 0, root_x + root_w + 10, root_y);
00093    assert(_glui);
00094    
00095    // tell glui which is the main window
00096    _glui->set_main_gfx_window(_main_window_id);
00097 
00098    _id = _glui->get_glut_window_id();
00099 
00100    // create a button for every menu item
00101    if(_item_list.num()>0) {
00102       MenuItem **items = _item_list.array();
00103       for (int i = 0; i < _item_list.num(); i++) {
00104          // Tell the menu item which menu it belongs to
00105          items[i]->menu(this);
00106          char *label = 0;
00107          // Make default label if one doesn't exist
00108          if (!items[i]->label()) {
00109             label ="----";
00110          } else label = **items[i]->label();
00111 
00112          // set the menu item in the global list used for callbacks
00113          int item_id = map_menu_item(items[i]);
00114          // record this menu's item id's
00115          _item_ids += item_id;
00116 
00117          // create the button
00118          _glui->add_button(label, item_id, GLUIMoveMenu::btn_callback);
00119       }
00120    }
00121    _menu_created = true;
00122    _is_shown = true;
00123 }
00124 
00125 
00126 void
00127 GLUIMoveMenu::btn_callback(int id)
00128 {
00129    using mlib::XYpt;
00130    assert(_menu_items.valid_index(id));
00131    assert(_menu_items[id]);
00132    XYpt dummy;
00133    _menu_items[id]->exec(dummy);
00134 }
00135 
00136 
00137 // Assigns a unique id to a menu item, which can be used to access the
00138 // item from a static array (_menu_item_map) in a static callback
00139 // functions.  Finds an empty slot in the _menu_item_map, growing the
00140 // array, if necessary, assigns the 'item' pointer to that slot, and
00141 // returns the slot's index, to be used as the item's unique id.
00142 
00143 int
00144 GLUIMoveMenu::map_menu_item(MenuItem *item)
00145 {
00146    assert(item);
00147 
00148    // See if there's an empty slot in the menu item array.
00149    for (int i=0; i<_menu_items.num(); i++) {
00150       if (_menu_items[i] == 0) {    // found an empty slot
00151          cerr << "map_menu_item(), empty slot " << i << endl;
00152          _menu_items[i] = item; // store item 
00153          return i;              // return the index as the item's id
00154       }
00155    }
00156 
00157    // we found no empty slot, so append the item to the array
00158    _menu_items += item;
00159 
00160    // return its index
00161    int ret_id = _menu_items.num()-1;
00162 
00163    return ret_id;
00164 }
00165 
00166 
00167 
00168 // Clears the entry for the menu item a the give index.
00169 void
00170 GLUIMoveMenu::unmap_menu_item(int item_index)
00171 {
00172    assert(_menu_items.valid_index(item_index));
00173    _menu_items[item_index] = 0;
00174 }

Generated on Mon Sep 18 11:39:31 2006 for jot by  doxygen 1.4.4