Search code examples
c++11ubuntugtk3gtkmm3

having issue getting widget size in gtkmm


I have gtk::applicationWindow with an openGL widget. I would like to query the size of the openGL widget on creation and on resize. This is done so that I can compensate for the aspect ratio. Here's a snippet of my code. The problem is that when I resize or spawn the window. The value of m_glAreaWidth and m_glAreaHeight are always 0. The widget does indeed spawn and reacts to resizing so I don't understand how get_allocation gives a size 0 rectangle.

mainWindow::mainWindow(BaseObjectType* cobject ,const Glib::RefPtr<Gtk::Builder>& builder):
        Gtk::ApplicationWindow(cobject),
        m_refGlade(builder),

{

        //add GLArea Widget 
        m_TopLayout->pack_start(m_GLArea);

        m_GLArea.set_auto_render(true);

        signal_size_allocate().connect(sigc::mem_fun(*this, &mainWindow::on_my_size_allocate),false);

        //Makes window fill the whole screen
        maximize();

        show_all();

}
    void mainWindow::on_my_size_allocate(Gtk::Allocation& allocation)
{
        Gtk::Allocation glAreaAlloc = m_GLArea.get_allocation();

        m_glAreaWidth = glAreaAlloc.get_x();
        m_glAreaHeight = glAreaAlloc.get_y();


        m_GLArea.queue_render();


}

Solution

  • I figured it out. I just replaced:

        Gtk::Allocation glAreaAlloc = m_GLArea.get_allocation();
    
        m_glAreaWidth = glAreaAlloc.get_x();
        m_glAreaHeight = glAreaAlloc.get_y();
    

    with

        m_glAreaWidth = m_GLArea.get_allocated_width();
        m_glAreaHeight = m_GLArea.get_allocated_height();
    

    still not sure why this worked and not the other one.