Re: Re: [gtkmm] Can't compile tutorial example "Drawing Lines"



I just updated the example from 
http://www.gtkmm.org/gtkmm2/docs/tutorial/html/ch14s03.html

I started with Gtk/Gdk/Gtkmm yesterday, so please review the code. I plan to 
switch from Qt to Gtkmm, but I'm still quiet unsure, because I need to 
implement own widgets and it looks much more complicated than with Qt, but I 
hope it's worth the trouble...

Here's the code:
(take care of new-lines from email-client...)

#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/drawingarea.h>
#include <gdkmm/drawable.h>
#include <gtkmm/style.h>

//Custom drawing area with modified expose_event.
class cust_draw_area: public Gtk::DrawingArea
{
  int width, height;
  Glib::RefPtr<Gdk::GC> gc_; // not needed, but code gets clearer
  Gdk::Color blue_,red_; // needed for alloc_color(Gdk::Color &);
  Glib::RefPtr<Gdk::Window> window_; // needed for clear();
  
  public:
              cust_draw_area(int x_size = 0, int y_size = 0);
         bool on_expose_event(GdkEventExpose*); // has to return bool 
 virtual void on_realize();
};

//Constructor.
cust_draw_area::cust_draw_area(int x_size, int y_size)
: DrawingArea(), width(x_size), height(y_size)
{
  Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap ();

  blue_ = Gdk::Color("blue");
  red_ = Gdk::Color("red");

  colormap->alloc_color(blue_); // needed
  colormap->alloc_color(red_);
  
  set_size_request(width, height);
}

//Expose_event method.
bool cust_draw_area::on_expose_event(GdkEventExpose *event)
{
  window_->clear(); 
  get_window()->draw_line(this->get_style()->get_black_gc(), 5, 2, 5, 20);
  window_->draw_line(gc_, 5, 11, 10, 11); // much shorter ....
  get_window()->draw_line(this->get_style()->get_black_gc(), 10, 2, 10, 20);
  get_window()->draw_line(this->get_style()->get_black_gc(), 15, 2, 21, 2);
  get_window()->draw_line(this->get_style()->get_black_gc(), 18, 2, 18, 20);
  get_window()->draw_line(this->get_style()->get_black_gc(), 15, 20, 21, 20);
}

void cust_draw_area::on_realize()
{
  // we need to do the default realize, else we'll get a segmentation fault
  Gtk::DrawingArea::on_realize();

  // Now we can allocate any additional resources we need
  window_ = get_window();
  Glib::RefPtr<Gdk::Drawable> drawable = window_; 
  gc_ = Gdk::GC::create(drawable);
  
  window_->set_background(red_);
  window_->clear();
  
  this->get_style()->get_black_gc()->set_foreground(blue_); 
  gc_->set_foreground(blue_); // just another possiblity, not needed
}


class test_window : public Gtk::Window
{
  cust_draw_area some_draw_area;
  public:
    test_window();
};

test_window::test_window()
:  some_draw_area(50, 50)
{
  add(some_draw_area);
  show_all();
}

int main(int argc, char *argv[])
{
  Gtk::Main main_runner(argc, argv);
  test_window foo;
  main_runner.run();
  return(0);
}





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