Re: same cairo graphics context in multiple methods

On 3/16/08, misho <misho88 gmail com> wrote:
>  Let's say I have a class which extends DrawingArea, and in the class, I have
>  declared:
>  Cairo::RefPtr<Cairo::Context> gfx;
>  and in a method of the class (I do this in: on_expose_event), I instantiate
>  gfx:
>  gfx=this->get_window()->create_cairo_context();
>  Now, I want to use that specific graphics context in other methods (all
>  within the same scope as gfx). This doesn't seem to be a problem (i.e. the
>  context collects the data I give it) until I call gfx->stroke(); at which
>  point my program crashes, with this error:
>  glibmm-ERROR **:
>  unhandled exception (type std::exception) in signal handler:
>  what: <unknown error status>

Did you check what the exception was?  I'm guessing that it's related
to the surface being 'finished', which means that you can't draw to it
anymore.  See below for more information

>  For example, the following method generates an error when it's run the
>  second time (the same method being run a second time might as well be a
>  different method - the result is the same, anyway):
>  bool Canvas::on_expose_event(GdkEventExpose * event)
>  {
>         //get dimensions
>         Gtk::Allocation allocation = get_allocation();
>         int width = allocation.get_width();
>         int height = allocation.get_height();
>         if(!gfx)
>         {
>         gfx = this->get_window()->create_cairo_context();
>         std::cout<<"creating new graphics context\n";
>         //white rectangle
>         gfx->set_source_rgb(1.0, 1.0, 1.0);
>         gfx->move_to(0.0,0.0);
>         gfx->line_to(0.0, height);
>         gfx->line_to(width, height);
>         gfx->line_to(width, 0.0);
>         gfx->close_path();
>         gfx->fill_preserve();
>         gfx->stroke();
>         }
>         gfx->stroke();
>  }
>  Is what I'm trying even possible or am I going to have to generate a new
>  graphics context and stroke() for every method?

As Carl explains in this message [1], the way GTK+ works requires that
you create a new cairo context for every expose event.  Perhaps the
create_cairo_context() documentation needs to be updated to make this
more clear as suggested in the post.



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