On 01/16/12 23:42, Edzard Egberts wrote:
> Greg Ercolano schrieb:
>>      AFAIK, FLTK doesn't use RTTI;
> 
> But most of the modern compilers use RTTI as default (and even the older 
> ones can activate it) and so it works, when regarding classes, derived 
> from FLTK.

        Neat, thanks Edzard!

        So here's a simple little example that defines different widgets
        so when you push the button, it walks the window's child() array,
        printing the different types of classes:


// DEMONSTRATE USING RTTI IN FLTK - erco 01/17/12
// As suggested by Edzard on fltk.general
//
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Input.H>

// SIMPLE DERIVED WIDGET
class MyClass : public Fl_Widget {
public:
    MyClass(int X, int Y, int W, int H, const char*L=0) : Fl_Widget(X,Y,W,H,L) 
{ }
    void draw() { fl_color(FL_BLACK); fl_rectf(x(),y(),w(),h()); }
};

// CALLBACK INVOKED BY "Show" BUTTON
void ShowWidgets_CB(Fl_Widget *w, void*) {
    Fl_Window *win = w->window();               // get button's parent window
    for ( int i=0; i<win->children(); i++ ) {   // walk all window's children
        const char *classname = "?";
        Fl_Widget *w = win->child(i);
        // Determine the class name of each widget
             if ( dynamic_cast< Fl_Button* >(w) ) { classname = "Fl_Button"; }
        else if ( dynamic_cast< Fl_Box*    >(w) ) { classname = "Fl_Box"; }
        else if ( dynamic_cast< Fl_Input*  >(w) ) { classname = "Fl_Input"; }
        else if ( dynamic_cast< MyClass*   >(w) ) { classname = "MyClass"; }
        printf("child #%d: %s\n", i, classname);
    }
}

int main() {
    Fl_Double_Window win(200,200,"RTTI Test");
    // Make a bunch of children
    Fl_Button but(10,10,140,25,"Show"); but.callback(ShowWidgets_CB);
    Fl_Input  inp(10,45,140,25);
    Fl_Box    box(10,80,10,10); box.color(45); box.box(FL_BORDER_BOX);
    MyClass   my1(10,100,10,10);
    MyClass   my2(10,120,10,10);
    win.end();
    win.show();
    return(Fl::run());
}
_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to