Re: How to maintain a reference to Cairo::Context or Cairo::Surface?



Hey Diego,

The thing with this is that I don't think it makes any sense.

Just keeping an old Cairo::Context arouind is not really useful, Context creation is very cheap
and I think there might be implications regarding Widget double-buffering for keeping a Context around,
(but someone else would have to make a definitive statement about that, I'm not 100% sure right now).

As for keeping another surface around: to show something on the Widget's window
you need to draw on that, and not on another surface. If you do in fact draw on another surface then, yes, you will
have to copy that over the actual Widget's surface every time in expose, which can be indeed useful if your
drawings are very complex and expensive to redraw, but is generally not recommended if you can instead keep a state
in your app so that in expose, the drawing can be redrawn based on the state.

The initial code sent here by BC, again, doesn't make really any sense at all (no offense). It just added an extra step
of getting the surface associated with the Widget's window, and then getting a context for that surface again, which is
equivalent to just getting the context with window->create_cairo_context();


M.

On Wed, Dec 3, 2008 at 12:49 PM, Diego A. Fons <diegofons apexar com> wrote:
Milosz Derezynski wrote:
It does not seem neccessary to create 2 separate Cairo Contexts:

class MyArea : public Gtk::DrawingArea
{
public:
   virtual ~MyArea();
protected:
//Override default signal handler:
   virtual bool on_expose_event(

   GdkEventExpose* event);
   };
   MyArea::MyArea()
   {
   }
   MyArea::~MyArea()
   {
   }
   bool MyArea::on_expose_event(GdkEventExpose* event)
   {
   // This is where we draw on the window
       Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
       cr->set_line_width(10.0);
       cr->move_to(0, 0);
       cr->line_to(100, 100);
       cr->stroke();          return true;
   }


This will work equally well. Is there any specific reason why you want to keep the surface member in the class and use it the way you wrote it in the preceding mail?

M.


2008/12/3 BC Zhu <zhubicen gmail com <mailto:zhubicen gmail com>>


   Now  i am using Gtkmm. I want to maintain a reference to
   Cairo::Context or Cairo::Surface, so i can draw something on it
   anywhere.
   I am trying to do this , but failed.
   With following code , i am trying to keep a reference to
   Cairo:::Surface,but when on_expose_event was called secondly , it
   will fail.
   And i have tried to maintain a reference to Cairo::Context too,
   but failed either.
   Could some body give me an advice?
   Thanks in advance!


   class MyArea : public Gtk::DrawingArea
   {
   public:
       virtual ~MyArea();
   protected:
   //Override default signal handler:
       virtual bool on_expose_event(GdkEventExpose* event);
   private:
       Cairo::RefPtr<Cairo::Surface> surf_;
   };
   MyArea::MyArea()
   {
   }
   MyArea::~MyArea()
   {
   }
   bool MyArea::on_expose_event(GdkEventExpose* event)
   {
   // This is where we draw on the window
       if(!surf_){
           Glib::RefPtr<Gdk::Window> window = get_window();
           Cairo::RefPtr<Cairo::Context> cr =
   window->create_cairo_context();
           surf_ = cr->get_target();
       }
       Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surf_);
       cr->set_line_width(10.0);
       cr->move_to(0, 0);
       cr->line_to(100, 100);
       cr->stroke();          return true;
   }


   --    Best Regards
   Bicen.Zhu

   _______________________________________________
   gtkmm-list mailing list
   gtkmm-list gnome org <mailto:gtkmm-list gnome org>

   http://mail.gnome.org/mailman/listinfo/gtkmm-list




--
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]
------------------------------------------------------------------------


_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list
 
Hi,

I also have this problem, my reason to keep the context (or surface) is that i want to draw and i don't want to loose the previous drawings i made in the context.

The last code works fine but when you call

 Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
in the on_expose_event(), you are creating a new context, an empty context.

My solution was drawing in a context i create and then copy this context over the one i get from the component, the problem is that this method is _very_ inefficient.

Regards,
Diego A. Fons.



--
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]


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