views:

303

answers:

1

Hello,
when I am programming with "gtkmm", there is a widget "Gtk::DrawingArea".
I can program that widget "by hand" (so write the code) or more elegant way is to use "glade" user interface designer, where I can do the same "graphically".

Now I am trying to connect OpenGL with gtkmm through "gtkglextmm" library. In that library, there is a widget "Gtk::GL::DrawingArea" - but this widget "IS NOT" available in glade.

So is there any way to program with "OpenGL + gtkglextmm" using "glade" (for the "graphical user interface" part)?

Thanks.

+3  A: 

First of all libglade is an old library. If you are writing new project start with gtk builder.

As you can see here gtkmm provide easy way to create your own widgets and see them (almost) in glade tool. You simply insert plain DrawinArea widget to window and then tell gtk-builder to put in this place yours derived class.

Here is all together:

Setting up gtk-builder:

refBuilder = Gtk::Builder::create_from_file(ui_file);

GlDrawingArea*glArea = NULL;
refBuilder->get_widget_derived("drawing_gl",glArea);

Opengl DrawingArea class:

 class GlDrawingArea : public Gtk::DrawingArea ,
                       public Gtk::GL::Widget<GlDrawingArea>
 {                   
 public:             
         GlDrawingArea(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder);
         virtual ~GlDrawingArea();
 protected:          
         void on_realize();
         bool on_expose_event(GdkEventExpose* event);
         bool on_configure_event(GdkEventConfigure* event);          
 private:
         Glib::RefPtr<Gtk::Builder> refBuilder;
 };

Constructing opengl drawingarea:

// GlDrawingArea:
GlDrawingArea::GlDrawingArea(BaseObjectType*cobject, const Glib::RefPtr<Gtk::Builder>& builder)
        : Gtk::DrawingArea(cobject),
          refBuilder(builder),
          screen_tex(0)
{                                                                                               
        //
        // Configure OpenGL-capable visual.
        //
        Glib::RefPtr<Gdk::GL::Config> glconfig;

        // Try double-buffered visual
        glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB    |
                                           Gdk::GL::MODE_DEPTH  |
                                           Gdk::GL::MODE_DOUBLE);
        if (!glconfig) {
                std::cerr << "*** Cannot find the double-buffered visual.\n"
                          << "*** Trying single-buffered visual.\n";

                // Try single-buffered visual
                glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB |Gdk::GL::MODE_DEPTH);
                if (!glconfig) {
                        std::cerr << "*** Cannot find any OpenGL-capable visual.\n";
                        std::exit(1);
                }
        }

        // print frame buffer attributes.
        GLConfigUtil::examine_gl_attrib(glconfig);

        //
        // Set OpenGL-capability to the widget.
        //
        set_gl_capability(glconfig);
}
qba
Thank you very much "qba".So the key was in using "derived" widgets.
Petike