Drawing any rectangle to a GTK+ DrawingArea fills the whole DrawingArea
I have a GTK+ DrawingArea that should display a rectangle in the top left corner. When I draw the rectangle using Cairo, the whole drawing area is filled with the color of the rectangle. How can I prevent that? Why does Cairo do that? What am I doing wrong?
#include <gtkmm.h>
class Window : public Gtk::Window
{
private:
Gtk::DrawingArea area;
bool on_area_expose(GdkEventExpose* event)
{
Gtk::Allocation allocation = area.get_allocation();
Cairo::RefPtr<Cairo::Context> context =
area.get_window()->create_cairo_context();
int width = allocation.get_width();
int height = allocation.get_height();
context->set_source_rgba(0, 0, 0, 1);
开发者_运维技巧 context->rectangle(0, 0, double(width)/10, double(height)/10);
context->paint();
return true;
}
public:
Window() : Gtk::Window()
{
area.signal_expose_event().connect(
sigc::mem_fun(*this, &Window::on_area_expose));
add(area);
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
Window window;
window.show_all();
Gtk::Main::run(window);
return 0;
}
I compiled the code using
g++ gtktest.cpp `pkg-config --libs --cflags gtkmm-2.4` -o gtktest
context->paint()
paints the current source everywhere within the current clip region. The proper method to call is Gtk::Context::fill
.
精彩评论