removed the need for RTTI/runtime type checking/dynamic_cast in glui. It is unnecessary to overhaul the build systems just for this feature. Replaced by upcasting virtual methods

if ( !dynamic_cast<GLUI_Rollout*>(this) &&
becomes
if ( !this->dynamicCastGLUI_Rollout() &&
etc.
This commit is contained in:
ejcoumans
2007-10-21 03:02:11 +00:00
parent fb1a4bd37e
commit 11a0589732
8 changed files with 158 additions and 47 deletions

View File

@@ -315,6 +315,13 @@ private:
/************************************************************/
class GLUI_Control;
class GLUI_Column;
class GLUI_Panel;
class GLUI_FileBrowser;
class GLUI_Scrollbar;
class GLUI_Listbox;
class GLUI_List;
/**
GLUI_Node is a node in a sort of tree of GLUI controls.
@@ -354,6 +361,56 @@ public:
void dump( FILE *out, const char *name );
virtual GLUI_Panel* dynamicCastGLUI_Panel()
{
return 0;
}
virtual GLUI_Column* dynamicCastGLUI_Column()
{
return 0;
}
virtual GLUI_EditText* dynamicCastGLUI_EditText()
{
return 0;
}
virtual GLUI_Rollout* dynamicCastGLUI_Rollout()
{
return 0;
}
virtual GLUI_Tree* dynamicCastGLUI_Tree()
{
return 0;
}
virtual GLUI_List* dynamicCastGLUI_List()
{
return 0;
}
virtual GLUI_FileBrowser* dynamicCastGLUI_FileBrowser()
{
return 0;
}
virtual GLUI_Scrollbar* dynamicCastGLUI_Scrollbar()
{
return 0;
}
virtual GLUI_Listbox* dynamicCastGLUI_Listbox()
{
return 0;
}
virtual GLUI_TreePanel* dynamicCastGLUI_TreePanel()
{
return 0;
}
protected:
static void add_child_to_control(GLUI_Node *parent,GLUI_Control *child);
GLUI_Node *parent_node;
@@ -745,6 +802,9 @@ public:
/* */
/************************************************************/
//get rid of the dynamic_cast/RTTI requirements, just do a virtual function
/**
All the GUI objects inherit from GLUI_Control: buttons,
checkboxes, labels, edit boxes, scrollbars, etc.
@@ -843,6 +903,7 @@ public:
void hide_internal( int recurse );
void unhide_internal( int recurse );
/** Return true if it currently makes sense to draw this class. */
int can_draw( void ) { return (glui != NULL && hidden == false); }
@@ -1073,6 +1134,10 @@ public:
GLUI_Column( GLUI_Node *parent, int draw_bar = true );
GLUI_Column( void ) { common_init(); }
virtual GLUI_Column* dynamicCastGLUI_Column()
{
return this;
}
protected:
void common_init() {
w = 0;
@@ -1115,6 +1180,11 @@ public:
void update_size( void );
virtual GLUI_Panel* dynamicCastGLUI_Panel()
{
return this;
}
protected:
void common_init( void ) {
w = 300;
@@ -1167,6 +1237,11 @@ public:
const char* get_file() { return file.c_str(); }
void set_allow_change_dir(int c) { allow_change_dir = c; }
virtual GLUI_FileBrowser* dynamicCastGLUI_FileBrowser()
{
return this;
}
protected:
void common_init()
{
@@ -1235,6 +1310,11 @@ public:
void update_size( void );
virtual GLUI_Rollout* dynamicCastGLUI_Rollout()
{
return 0;
}
protected:
void common_init() {
currently_inside = false;
@@ -1316,6 +1396,11 @@ public:
green = g;
blue = b;
}
virtual GLUI_Tree* dynamicCastGLUI_Tree()
{
return this;
}
protected:
void common_init()
{
@@ -1403,6 +1488,10 @@ public:
void update_all( void );
void initNode(GLUI_Tree *temp);
void formatNode(GLUI_Tree *temp);
virtual GLUI_TreePanel* dynamicCastGLUI_TreePanel()
{
return this;
}
protected:
int uniqueID( void ) { next_id++; return next_id - 1; }
@@ -1660,6 +1749,12 @@ public:
// Deprecated constructor, only called internally
GLUI_EditText( void ) { common_init(); }
virtual GLUI_EditText* dynamicCastGLUI_EditText()
{
return this;
}
protected:
void common_init( void ) {
h = GLUI_EDITTEXT_HEIGHT;
@@ -2161,6 +2256,11 @@ public:
void set_object_callback(GLUI_CB cb=GLUI_CB(), GLUI_Control*obj=NULL)
{ obj_cb=cb; associated_object=obj; }
virtual GLUI_List* dynamicCastGLUI_List()
{
return this;
}
protected:
void common_init()
{
@@ -2288,6 +2388,11 @@ public:
void set_object_callback(GLUI_CB cb=GLUI_CB(), GLUI_Control*obj=NULL)
{ object_cb=cb; associated_object=obj; }
virtual GLUI_Scrollbar* dynamicCastGLUI_Scrollbar()
{
return this;
}
protected:
void common_init ( void );
void common_construct(
@@ -2358,6 +2463,11 @@ public:
int id=-1, GLUI_CB callback=GLUI_CB() );
GLUI_Listbox( void ) { common_init(); }
virtual GLUI_Listbox* dynamicCastGLUI_Listbox()
{
return this;
}
protected:
/** Change w and return true if we need to be widened to fit the current item. */
bool recalculate_item_width( void );

View File

@@ -932,7 +932,7 @@ GLUI_Control *GLUI_Main::find_control( int x, int y )
node = main_panel;
while( node != NULL ) {
if ( !dynamic_cast<GLUI_Column*>(node) AND
if ( !node->dynamicCastGLUI_Column() AND
PT_IN_BOX( x, y,
node->x_abs, node->x_abs + node->w,
node->y_abs, node->y_abs + node->h )
@@ -944,7 +944,7 @@ GLUI_Control *GLUI_Main::find_control( int x, int y )
/*** SPECIAL CASE: for edittext boxes, we make sure click is
in box, and not on name string. This should be generalized
for all controls later... ***/
if ( dynamic_cast<GLUI_EditText*>(node) ) {
if ( node->dynamicCastGLUI_EditText() ) {
if ( x < node->x_abs + ((GLUI_EditText*)node)->text_x_offset )
return (GLUI_Control*) node->parent();
}

View File

@@ -220,7 +220,7 @@ void GLUI_Control::draw_recursive( int x, int y )
}
else
{
if ( dynamic_cast<GLUI_Column*>(this) ) {
if ( this->dynamicCastGLUI_Column() ) {
/* printf( "%s w/h: %d/%d\n", (char*) name, w, h ); */
/*w = 2; */
}
@@ -387,7 +387,7 @@ void GLUI_Control::align()
get_this_column_dims(&col_x, &col_y, &col_w, &col_h,
&col_x_off, &col_y_off);
if ( dynamic_cast<GLUI_Column*>(this) ) {
if ( this->dynamicCastGLUI_Column() ) {
/* if ( this->prev() != NULL ) {
((GLUI_Control*)prev())->get_this_column_dims(&col_x, &col_y, &col_w, &col_h,
&col_x_off, &col_y_off);
@@ -419,7 +419,7 @@ void GLUI_Control::align()
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
if ( node->dynamicCastGLUI_Column() ) {
node->x_abs += delta;
}
@@ -461,11 +461,11 @@ void GLUI_Control::pack_old(int x, int y)
/*** Iterate over children, packing them first ***/
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible) {
if ( node->dynamicCastGLUI_Panel() && !node->collapsible) {
/* Pad some space above fixed size panels */
curr_y += GLUI_ITEMSPACING;
}
else if ( dynamic_cast<GLUI_Column*>(node)) {
else if ( node->dynamicCastGLUI_Column()) {
curr_column = (GLUI_Column*) node;
if ( 1 ) {
column_x += max_w + 2 * x_margin;
@@ -487,7 +487,7 @@ void GLUI_Control::pack_old(int x, int y)
continue;
}
node->pack( curr_x, curr_y );
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible)
if ( node->dynamicCastGLUI_Panel() && !node->collapsible)
/* Pad some space below fixed size panels */
curr_y += GLUI_ITEMSPACING;
curr_y += node->h;
@@ -506,7 +506,7 @@ void GLUI_Control::pack_old(int x, int y)
if ( this->is_container ) {
max_y += y_margin_bot; /*** Add bottom border inside box */
if ( this->first_child() ) {
if ( dynamic_cast<GLUI_Rollout*>(this) ) {
if ( this->dynamicCastGLUI_Rollout() ) {
/** We don't want the rollout to shrink in width when it's
closed **/
this->w = MAX(this->w, column_x + max_w + 2 * x_margin );
@@ -544,7 +544,7 @@ void GLUI_Control::get_this_column_dims( int *col_x, int *col_y,
parent_h = parent_ptr->h;
parent_y_abs = parent_ptr->y_abs;
if ( dynamic_cast<GLUI_Panel*>(parent_ptr) AND
if ( parent_ptr->dynamicCastGLUI_Panel() AND
parent_ptr->int_val == GLUI_PANEL_EMBOSSED AND
parent_ptr->name != "" ) {
parent_h -= GLUI_PANEL_EMBOSS_TOP;
@@ -556,12 +556,12 @@ void GLUI_Control::get_this_column_dims( int *col_x, int *col_y,
/** Look for first control in this column **/
first = this;
while (first->prev() AND !dynamic_cast<GLUI_Column*>(first->prev()) )
while (first->prev() AND !(first->prev())->dynamicCastGLUI_Column() )
first = first->prev();
/** Look for last control in this column **/
last = this;
while ( last->next() AND !dynamic_cast<GLUI_Column*>(first->next()) )
while ( last->next() AND !(first->next())->dynamicCastGLUI_Column() )
last = last->next();
curr = first;
@@ -596,7 +596,7 @@ void GLUI_Control::get_this_column_dims( int *col_x, int *col_y,
/*** Look for preceding column ***/
node = (GLUI_Control*) this->prev();
while( node ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
if ( node->dynamicCastGLUI_Column() ) {
*col_x = node->x_abs;
*col_y = parent_y_abs;
*col_w = node->w;
@@ -613,7 +613,7 @@ void GLUI_Control::get_this_column_dims( int *col_x, int *col_y,
/*** Nope, Look for next column ***/
node = (GLUI_Control*) this->next();
while( node ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
if ( node->dynamicCastGLUI_Column() ) {
*col_x = parent_ptr->x_abs;
*col_y = parent_y_abs;
*col_w = node->x_abs - parent_ptr->x_abs;
@@ -671,11 +671,11 @@ void GLUI_Control::pack( int x, int y )
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible) {
if ( node->dynamicCastGLUI_Panel() && !node->collapsible) {
/* Pad some space above fixed-size panels */
curr_y += GLUI_ITEMSPACING;
}
else if ( dynamic_cast<GLUI_Column*>(node) ) {
else if ( node->dynamicCastGLUI_Column() ) {
curr_column = (GLUI_Column*) node;
curr_x += max_w + 1 * x_margin;
column_x = curr_x;
@@ -695,7 +695,7 @@ void GLUI_Control::pack( int x, int y )
node->pack( curr_x, curr_y );
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible)
if ( node->dynamicCastGLUI_Panel() && !node->collapsible)
/* Pad some space below fixed-size panels */
curr_y += GLUI_ITEMSPACING;
@@ -729,8 +729,8 @@ void GLUI_Control::pack( int x, int y )
this->h = (max_y - y_in);
}
else { /* An empty container, so just assign default w & h */
if ( !dynamic_cast<GLUI_Rollout*>(this) &&
!dynamic_cast<GLUI_Tree*>(this) ) {
if ( !this->dynamicCastGLUI_Rollout() &&
!this->dynamicCastGLUI_Tree() ) {
this->w = GLUI_DEFAULT_CONTROL_WIDTH;
this->h = GLUI_DEFAULT_CONTROL_HEIGHT;
}
@@ -744,7 +744,7 @@ void GLUI_Control::pack( int x, int y )
/*** Now we step through the GLUI_Columns, setting the 'h' ***/
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
if ( node->dynamicCastGLUI_Column() ) {
node->h = this->h - y_margin_bot - y_margin_top;
}

View File

@@ -57,7 +57,7 @@ void GLUI_FileBrowser::dir_list_callback(GLUI_Control *glui_object) {
GLUI_List *list = dynamic_cast<GLUI_List*>(glui_object);
if (!list)
return;
GLUI_FileBrowser* me = dynamic_cast<GLUI_FileBrowser*>(list->associated_object);
GLUI_FileBrowser* me = list->associated_object->dynamicCastGLUI_FileBrowser();
if (!me)
return;
int this_item;

View File

@@ -527,7 +527,7 @@ int GLUI_List::mouse_over( int state, int x, int y )
}
void GLUI_List::scrollbar_callback(GLUI_Control *my_scrollbar) {
GLUI_Scrollbar *sb = dynamic_cast<GLUI_Scrollbar*>(my_scrollbar);
GLUI_Scrollbar *sb = my_scrollbar->dynamicCastGLUI_Scrollbar();
if (!sb) return;
GLUI_List* me = (GLUI_List*) sb->associated_object;
if (me->scrollbar == NULL)

View File

@@ -283,7 +283,7 @@ static void listbox_callback( int i )
int old_val;
if ( NOT GLUI_Master.curr_left_button_glut_menu OR
!dynamic_cast<GLUI_Listbox*>(GLUI_Master.curr_left_button_glut_menu) )
!GLUI_Master.curr_left_button_glut_menu->dynamicCastGLUI_Listbox() )
return;
old_val = ((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->int_val;

View File

@@ -1092,7 +1092,7 @@ int GLUI_TextBox::mouse_over( int state, int x, int y )
}
void GLUI_TextBox::scrollbar_callback(GLUI_Control *my_scrollbar) {
GLUI_Scrollbar *sb = dynamic_cast<GLUI_Scrollbar*>(my_scrollbar);
GLUI_Scrollbar *sb = my_scrollbar->dynamicCastGLUI_Scrollbar();
if (!sb) return;
GLUI_TextBox* me = (GLUI_TextBox*) sb->associated_object;
if (me->scrollbar == NULL)

View File

@@ -58,7 +58,7 @@ GLUI_Tree *GLUI_TreePanel::ab(const char *name, GLUI_Tree *root)
curr_root = temp;
curr_branch = NULL; /* Currently at leaf */
if (dynamic_cast<GLUI_Tree*>(temp))
if (temp->dynamicCastGLUI_Tree())
((GLUI_Tree *)temp)->set_current(true);
//refresh();
// glui->deactivate_current_control();
@@ -86,7 +86,7 @@ void GLUI_TreePanel::fb(GLUI_Tree *branch)
if (branch != NULL) {
if ( dynamic_cast<GLUI_Tree*>(branch) )
if ( branch->dynamicCastGLUI_Tree() )
((GLUI_Tree *)branch)->set_current(false);
curr_branch = (GLUI_Tree *)branch->next();
@@ -95,13 +95,14 @@ void GLUI_TreePanel::fb(GLUI_Tree *branch)
if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
if ( dynamic_cast<GLUI_Tree*>(curr_root) )
if ( curr_root->dynamicCastGLUI_Tree() )
((GLUI_Tree *)curr_root)->set_current(true);
} else {
if (curr_root != NULL) { /* up one parent */
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree *)curr_root)->set_current(false);
curr_branch = (GLUI_Tree *) curr_root->next();
@@ -110,7 +111,7 @@ void GLUI_TreePanel::fb(GLUI_Tree *branch)
if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree *)curr_root)->set_current(true);
}
@@ -139,15 +140,15 @@ void GLUI_TreePanel::initNode(GLUI_Tree *temp)
int level = temp->get_level();
int child_number = 1;
GLUI_Tree *ptree = dynamic_cast<GLUI_Tree*>(temp->parent());
GLUI_Tree *ptree = temp->parent()->dynamicCastGLUI_Tree();
if (ptree) {
level = ptree->get_level() + 1;
GLUI_Tree *prevTree = dynamic_cast<GLUI_Tree*>(temp->prev());
GLUI_Tree *prevTree = temp->prev()->dynamicCastGLUI_Tree();
if (prevTree) {
child_number = prevTree->get_child_number() + 1;
}
} else if (dynamic_cast<GLUI_Tree*>(temp) &&
dynamic_cast<GLUI_TreePanel*>(temp->parent())) {
} else if (temp->dynamicCastGLUI_Tree() &&
temp->parent()->dynamicCastGLUI_TreePanel()) {
child_number = ++root_children;
}
temp->set_id(uniqueID()); // -1 if unset
@@ -173,7 +174,7 @@ void GLUI_TreePanel::formatNode(GLUI_Tree *temp)
glui_format_str(level_name, "%d", level);
}
if (format & GLUI_TREEPANEL_HIERARCHY_NUMERICDOT) {
if ( dynamic_cast<GLUI_Tree*>(temp->parent()) )
if ( temp->parent()->dynamicCastGLUI_Tree() )
glui_format_str(level_name, "%s.%d",
((GLUI_Tree *)(temp->parent()))->level_name.c_str(),
child_number);
@@ -206,12 +207,12 @@ void GLUI_TreePanel::formatNode(GLUI_Tree *temp)
} else {
if (format & GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) {
temp->disable_bar();
if ( dynamic_cast<GLUI_Tree*>(curr_root) )
if ( curr_root->dynamicCastGLUI_Tree() )
((GLUI_Tree *)curr_root)->enable_bar();
} else
if (format & GLUI_TREEPANEL_CONNECT_CHILDREN_ONLY) {
temp->disable_bar();
if (temp->prev() && dynamic_cast<GLUI_Tree*>(temp->prev()) )
if (temp->prev() && temp->prev()->dynamicCastGLUI_Tree() )
{
((GLUI_Tree *)temp->prev())->enable_bar();
}
@@ -229,11 +230,11 @@ void GLUI_TreePanel::update_all()
GLUI_Tree *saved_branch = curr_branch;
root_children = 0;
resetToRoot(this);
if (curr_branch && dynamic_cast<GLUI_Tree*>(curr_branch))
if (curr_branch && curr_branch->dynamicCastGLUI_Tree())
formatNode((GLUI_Tree *)curr_branch);
next();
while (curr_root && curr_branch != this->first_child()) {
if (curr_branch && dynamic_cast<GLUI_Tree*>(curr_branch)) {
if (curr_branch && curr_branch->dynamicCastGLUI_Tree()) {
formatNode((GLUI_Tree *)curr_branch);
}
next();
@@ -250,11 +251,11 @@ void GLUI_TreePanel::expand_all()
GLUI_Tree *saved_branch = curr_branch;
resetToRoot(this);
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree*)curr_root)->open();
next();
while (curr_root != NULL && curr_branch != this->first_child()) {
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree*)curr_root)->open();
next();
}
@@ -273,7 +274,7 @@ void GLUI_TreePanel::collapse_all()
resetToRoot(this);
next();
while (curr_root != NULL && curr_branch != this->first_child()) {
if (dynamic_cast<GLUI_Tree*>(curr_root) &&
if (curr_root->dynamicCastGLUI_Tree() &&
curr_branch == NULL) { /* we want to close everything leaf-first */
((GLUI_Tree*)curr_root)->close();
/* Rather than simply next(), we need to manually move the
@@ -318,11 +319,11 @@ void GLUI_TreePanel::db(GLUI_Tree *root)
delete curr_root;
curr_branch = (GLUI_Tree *) temp_branch;
curr_root = (GLUI_Panel *) temp_root;
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree *)curr_root)->open();
if ((format & GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) == GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) {
if (dynamic_cast<GLUI_Tree*>(curr_root) && ((GLUI_Tree *)curr_root->next()) == NULL)
if (curr_root->dynamicCastGLUI_Tree() && ((GLUI_Tree *)curr_root->next()) == NULL)
((GLUI_Tree *)curr_root)->disable_bar();
}
//refresh();
@@ -337,7 +338,7 @@ void GLUI_TreePanel::descendBranch(GLUI_Panel *root) {
else
resetToRoot(curr_root);
if (curr_branch != NULL && curr_branch != ((GLUI_Panel *)this)) {
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree *)curr_root)->set_current(false);
descendBranch(curr_branch);
}
@@ -355,7 +356,7 @@ void GLUI_TreePanel::next()
if (curr_branch != NULL && curr_branch != ((GLUI_Panel *)this)) { /* Descend into branch */
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree *)curr_root)->set_current(false);
resetToRoot(curr_branch);
} else if (curr_branch == NULL) {
@@ -372,7 +373,7 @@ void GLUI_TreePanel::resetToRoot(GLUI_Panel *new_root)
if (new_root != NULL)
root = new_root;
curr_root = root;
if (dynamic_cast<GLUI_Tree*>(curr_root))
if (curr_root->dynamicCastGLUI_Tree())
((GLUI_Tree *)curr_root)->set_current(true);
curr_branch = (GLUI_Tree *)root->first_child();
@@ -381,7 +382,7 @@ void GLUI_TreePanel::resetToRoot(GLUI_Panel *new_root)
if (curr_branch == NULL && (root->collapsed_node).first_child() != NULL) {
curr_branch = (GLUI_Tree *)(root->collapsed_node).first_child();
}
while (curr_branch && dynamic_cast<GLUI_Tree*>(curr_branch)) {
while (curr_branch && curr_branch->dynamicCastGLUI_Tree()) {
curr_branch=(GLUI_Tree *)curr_branch->next();
}
}