c++gtkmmdrawingarea

Get Gtk::DrawingArea size, before signaling draw


I want to draw some frames, one by one in a Gtk::DrawingArea. At the beginning of application, I need to get the size of DrawingArea, to allocate a RGB buffer, hewever, DrawingArea::get_width() and DrawingArea::get_height() return 1.

My code is based on https://developer.gnome.org/gtkmm-tutorial/stable/sec-drawing-clock-example.html.en with an extra line to get the size of DrawingArea:

#include "clock.h"
#include <gtkmm/application.h>
#include <gtkmm/window.h>

int main(int argc, char** argv)
{
   auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

   Gtk::Window win;
   win.set_title("Cairomm Clock");

   Clock c;
   win.add(c);
   c.show();

   int w = c.get_width();
   // w is 1;

   return app->run(win);
}

How can I get the size of DrawingArea before actually drawing into it?


Solution

  • I was able to trace size changes based on Scheff's comments:

    #include "clock.h"
    #include <gtkmm/application.h>
    #include <gtkmm/window.h>
    
    int width_ = 0;
    int height_ = 0;
    bool resized_ = false;
    
    void check_size()
    {
       if (resized_)
       {
          resized_ = false;
          // Apply size changes to resources using width_ and height_
       }
    }
    
    int main(int argc, char** argv)
    {
       auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
    
       Gtk::Window win;
       win.set_title("Cairomm Clock");
    
       Clock c;
       c.signal_configure_event().connect([&](GdkEventConfigure* event)
       {
          width_ = event->width;
          height_ = event->height;
          resized_ = true;
          return true;
       });
       win.add(c);
       c.show();
    
       return app->run(win);
    }
    

    Where to call check_size function is dependent on the design of the application. In my case I call it right before c.queue_draw().