Re: Gtkmm draw line with mouse events



On Mon, 14 Jul 2014 21:27:50 +0530
Gurjot Singh <bhattigurjot gmail com> wrote:
On 14 July 2014 00:22, Chris Vine <chris cvine freeserve co uk> wrote:
If that is not possible only then I'd be forced to do everything
in a single on_draw().

You should draw in the on_draw() method or in a draw signal handler
so that the widget re-renders correctly when this is necessary by
virtue of other desktop activities.  Obviously your overriding
on_draw() method can call whatever other drawing functions it
wants.  You can force a draw when needed by calling
Gtk::Widget::queue_draw_area() and cognates.

Pardon me, but I didn't get you.
Can you explain with some example?
Are you saying that everything that is to be drawn must be written in
on_draw function?
Suppose I have two functions that are called to draw point and line,
something like this:
http://pastebin.com/Ancx87EF

This example looks fine.

But if this is it, it'd re-draws the whole drawing area, so I'd be
unable to retain any previously drawn entity.

I don't understand what you mean here.  Anyway, the cairo context comes
pre-clipped with the area of the dirty region which caused the draw
event.  See the documentation on the draw signal:

"The signal handler will get a cr with a clip region already set to the
widget's dirty region, i.e. to the area that needs repainting.
Complicated widgets that want to avoid redrawing themselves completely
can get the full extents of the clip region with
gdk_cairo_get_clip_rectangle(), or they can get a finer-grained
representation of the dirty region with
cairo_copy_clip_rectangle_list()."

However, fiddling about with the dirty region is highly unlikely to be
necessary for what you want to do.  When it comes to interfacing with
the X backend (or whatever backend you are using) GDK knows what region
needs to be reproduced.

And that is why I was asking if it could be done something like,

void DrawingArea :: on_line_cb()
{

cr->set_source_rgb(0.0, 0.26, 0.26);
cr->save();
cr->move_to(0,0);
cr->line_to(100,100);
cr->restore();
cr->stroke();

queue_draw();
std::cout<<"Line created"<<std::endl;
}

But this doesn't work because it says that 'cr' is not declared in the
scope or something related to this.

As a matter of cairo implementation you need a cairo context to work
with.  That is provided by the on_draw() method (pre-clipped, as I
said). As a matter of basic C++, the cairo context object needs to be
visible to the code addressing it, which means it must be in scope.

Chris


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