Gtk::DrawingArea parameters



Hi,
I'm fairly new at gtkmm, and wish to clarify what I think is happening when a Gtk::DrawingArea is used.

The issue I've been prodding at is that the Cairo surface is obtained (as far as I can make out) by using a RefPtr to the window being drawn on.  When the Cairo surface is the only object in that window, that works fine; however, if there are any other objects created in the same class as the DrawingArea, they get drawn over.

For example, if I modify the clock example, adding a button to the display works fine if it is created and added in the main function.
  If I modify the Clock class constructor to put the additional widgets in there(see below for code), they seem to get drawn over when the on_expose_event method is called.

My guess is that the DrawingArea selected within the on_expose_event is the size of the widget the entire class is constructed in, as it appears to ignore the widget the Drawing Area is created within if there are other widgets in the class. I'm stumped as to whether it's determined within the Gtk::Allocation or on creation of the Cairo::Context, and can't find any examples around giving an alternative way of getting a context.  The only way I can see to workaround it is to ensure the Drawing Area is in a separate class of its own, as that seems to keep the window the size of the widget it _should_ be assigned to.

Glib::RefPtr<Gdk::Window> window = get_window();
    if(window){
        Gtk::Allocation allocation = get_allocation(); //this appears to only get dimensions after the window size has been passed.  Is this right?
       const int width = allocation.get_width();
       const int height = allocation.get_height();

        Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();// this appears to be where the drawing area obtains it's dimensions.  So the window size is determined where?


My questions:
Am I missing something obvious (yet again...)? 
Is there another way of creating a DrawingArea that allows more explicit defining of the surface? 
Am I right about where the surface size comes from?

modified section of clock.h:
#ifndef GTKMM_EXAMPLE_CLOCK_H
#define GTKMM_EXAMPLE_CLOCK_H

#include <gtkmm/drawingarea.h>
#include <gtkmm/window.h>
#include <cairomm/context.h>

/*added to draw additional widgets: */
#include <gtkmm/box.h>
#include <gtkmm/button.h>

class Clock : public Gtk::Window
{
public:
  Clock();
  virtual ~Clock();

protected:
  //Override default signal handler:
  virtual bool on_expose_event(GdkEventExpose* event);

  bool on_timeout();

  double m_radius;
  double m_line_width;
  Gtk::DrawingArea clocksquare;
  Gtk::HBox OuterBox, middlebox;
  Gtk::Button button;


};

and then within clock.cc:


#include <ctime>
#include <math.h>
#include "clock.h"

Clock::Clock()
: m_radius(0.42), m_line_width(0.05),
middlebox(),
clocksquare(),
button("Hello!")
{

   set_title("Cairomm Clock");
   set_size_request(450,150);
   middlebox.set_size_request(100, 150);

    add(OuterBox);
    OuterBox.pack_end(clocksquare, Gtk::PACK_SHRINK);
    OuterBox.pack_start(button);
    OuterBox.pack_start(middlebox, Gtk::PACK_SHRINK);
    show_all_children();

NB this example simply shows what happens when things don't work.


Need a Holiday? Win a $10,000 Holiday of your choice. Enter now..

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]