[gtkmm] Drawing primitives on top of an image.



Hello,
I'm trying to write a program that will draw primitives (circles,
rectangles and lines), on top of an image that I'll read from a
file. Stealing code from the "examples" section and the gtkmm
tutorial, I was able to write the following program, which does not
work as expected (it only displays the image).

What am I missing ? Any hint or pointer is highly appreciated.

Thank you,
Giannis

--------------------------------------------------

#include <gdkmm.h>
#include <gtkmm/image.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>

#include <gdkmm/color.h>
#include <gdkmm/gc.h>
#include <gdkmm/types.h>

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

protected:
  virtual bool on_button_press_event(GdkEventButton* event);
};


Test::Test()
:
  Gtk::Window(Gtk::WINDOW_TOPLEVEL)
{
  set_resizable(false);
  set_decorated(false);
  set_position(Gtk::WIN_POS_CENTER);

  realize(); // the widget must be realized to create the GDK window
  Glib::RefPtr<Gdk::Drawable> drawable = get_window();
  const Gdk::Color transparent = Gtk::Widget::get_default_style()->get_bg(Gtk::STATE_NORMAL);
  Gtk::Image *const image = new Gtk::Image("test.jpg");
  add(*Gtk::manage(image));

  Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(drawable);
  gc->set_foreground(Gdk::Color("red"));

  drawable->draw_arc(gc, true, 30, 100, 50, 50, 2880, 17280);

	//Draw pellets.
	drawable->draw_rectangle(this->get_style()->get_white_gc(), true, 80, 120, 15, 10);
	drawable->draw_rectangle(this->get_style()->get_white_gc(), true, 110, 120, 15, 10);
	drawable->draw_rectangle(this->get_style()->get_white_gc(), true, 140, 120, 15, 10);

	//Draw some lines.
	drawable->draw_line(this->get_style()->get_black_gc(), 5, 2, 5, 20);
	drawable->draw_line(this->get_style()->get_black_gc(), 5, 11, 10, 11);
	drawable->draw_line(this->get_style()->get_black_gc(), 10, 2, 10, 20);
	drawable->draw_line(this->get_style()->get_black_gc(), 15, 2, 21, 2);
	drawable->draw_line(this->get_style()->get_black_gc(), 18, 2, 18, 20);
	drawable->draw_line(this->get_style()->get_black_gc(), 15, 20, 21, 20);

  image->show();

  add_events(Gdk::BUTTON_PRESS_MASK);
}

Test::~Test()
{}

bool Test::on_button_press_event(GdkEventButton* event)
{
  switch(event->button)
  {
    case 1: begin_move_drag(event->button, int(event->x_root), int(event->y_root), event->time); break;
    case 3: hide(); break;
  }

  return false;
}

int main(int argc, char** argv)
{
  Gtk::Main myapp (&argc, &argv);

  Test test;
  Gtk::Main::run(test);

  return 0;
}



-- 
question = ( to ) ? be : ! be;
                -- Wm. Shakespeare




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