title: Initializing private data in custom Gtk+ widget which depends on parent's members (C)
body: <p>I'm working on a pet project solely for the purpose of learning a few API's. It's not intended to have practical value, but rather to be relatively simple excercise to get me comfortable with libpcap, gtk+, and cairo before I use them for anything serious. This is a graphical program, implemented in C and using Gtk+ 2.x. It's eventually going to read frames with pcap (currently I just have a hardcoded test frame), then use cairo to generate pretty pictures using color values generated from the raw packet (at this stage, I'm just using cairo_show_text to print a text representation of the frame or packet). The pictures will then be drawn to a custom widget inheriting from GtkDrawingArea. </p>
  
  <p>My first step, of course, is to get a decent grasp of the Gtk+ runtime environment so I can implement my widget. I've already managed to render and draw text using cairo to my custom widget. Now I'm at the point where I think the widget really needs private storage for things like the cairo_t context pointer and a GdkRegion pointer (I had not planned to use Gdk directly, but my research indicates that it may be necessary in order to call gdk_window_invalidate_region() to force my DrawingArea to refresh once I've drawn a frame, not to mention gdk_cairo_create()). I've set up private storage as a global variable (the horror! Apparently this is conventional for Gtk+. I'm still not sure how this will even work if I have multiple instances of my widget, so maybe I'm not doing this part right. Or maybe the preprocessor macros and runtime environment are doing some magic to give each instance its own copy of this struct?):</p>
  
  <pre><code>/* private data */
  typedef struct _CandyDrawPanePrivate CandyDrawPanePrivate;
  struct _CandyDrawPanePrivate {
          cairo_t *cr;
          GdkRegion *region;
  };
  
  
  #define CANDY_DRAW_PANE_GET_PRIVATE(obj)\
          (G_TYPE_INSTANCE_GET_PRIVATE((obj), CANDY_DRAW_PANE_TYPE, CandyDrawPanePrivate))
  </code></pre>
  
  <p>Here's my question: Initializing the pointers in my private data struct depends on members inherited from the parent, GtkWidget:</p>
  
  <pre><code>/* instance initializer */
  static void candy_draw_pane_init(CandyDrawPane *pane) {
          GdkWindow *win = NULL;
          /*win = gtk_widget_get_window((GtkWidget *)pane);*/
          win = ((GtkWidget*)pane)-&gt;window;
          if (!win)
              return;
  
          /* TODO: I should probably also check this return value */
          CandyDrawPanePrivate *priv = CANDY_DRAW_PANE_GET_PRIVATE(((CandyDrawPane*)pane));
          priv-&gt;cr = gdk_cairo_create(win);
          priv-&gt;region = gdk_drawable_get_clip_region(win);
  
          candy_draw_pane_update(pane);
          g_timeout_add(1000, candy_draw_pane_update, pane);
  }
  </code></pre>
  
  <p>When I replaced my old code, which called gdk_cairo_create() and gdk_drawable_get_clip_region() during my event handlers, with this code, which calls them during candy_draw_pane_init(), the application would no longer draw. Stepping through with a debugger, I can see that pane->window and pane->parent are both NULL pointers while we are within candy_draw_pane_init(). The pointers are valid later, in the Gtk event processing loop. This leads me to believe that the inherited members have not yet been initialized when my derived class' "_init()" method is called. I'm sure this is just the nature of the Gtk+ runtime environment.</p>
  
  <p>So how is this sort of thing typically handled? I could add logic to my event handlers to check priv->cr and priv->region for NULL, and call gdk_cairo_create() and gdk_drawable_get_clip_region() if they are still NULL. Or I could add a "post-init" method to my CandyDrawPane widget and call it explicitly after I call candy_draw_pane_new(). I'm sure lots of other people have encountered this sort of scenario, so is there a clean and conventional way to handle it?</p>
  
  <p>This is my first real foray into object-oriented C, so please excuse me if I'm using any terminology incorrectly. I think one source of my confusion is that Gtk has separate concepts of instance and class initialization. C++ may do something similar "under the hood," but if so, it isn't as obvious to the coder.</p>
  
  <p>I have a feeling that if this was C++, most of the the code that's going into candy_draw_pane_init() would be in the class constructor, and any secondary initialization that depended on the constructor having completed would go into an "Init()" method (which of course is not a feature of the language, but just a commonly used convention). Is there an analogous convention for Gtk+? Or perhaps someone can give a good overview of the flow of control when these widgets are instantiated. I have not been very impressed with the quality of the official Gnome documentation. Much of it is either too high-level, contains errors and typos in code, or has broken links or missing examples. And of course the heavy use of macros makes it a little harder to follow even my own code (in this respect it reminds me of Win32 GUI development). In short, I'm sure I can struggle through this on my own and make it work, but I'd like to hear from someone experienced with Gtk+ and C what the "right" way to do this is.</p>
  
  <p>For completeness, here is the header where I set up my custom widget:</p>
  
  <pre><code>#ifndef __GTKCAIRO_H__
  #define __GTKCAIRO_H__ 1
  
  #include &lt;gtk/gtk.h&gt;
  
  /* Following tutorial; see gtkcairo.c */
  /* Not sure about naming convention; may need revisiting */
  G_BEGIN_DECLS
  #define CANDY_DRAW_PANE_TYPE    (candy_draw_pane_get_type())
  #define CANDY_DRAW_PANE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), CANDY_DRAW_PANE_TYPE, CandyDrawPane))
  #define CANDY_DRAW_PANE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass)CANDY_DRAW_PANE_TYPE, CandyDrawPaneClass))
  #define IS_CANDY_DRAW_PANE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CANDY_DRAW_PANE_TYPE))
  #define IS_CANDY_DRAW_PANE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CANDY_DRAW_PANE_TYPE))
  // official gtk tutorial, which seems to be of higher quality, does not use this.
  
  // #define CANDY_DRAW_PANE_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), CANDY_DRAW_PANE_TYPE, CandyDrawPaneClass))
  
  typedef struct {
          GtkDrawingArea parent;
  
          /* private */
  } CandyDrawPane;
  
  typedef struct {
          GtkDrawingAreaClass parent_class;
  } CandyDrawPaneClass;
  
  /* method prototypes */
  GtkWidget*      candy_draw_pane_new(void);
  GType   candy_draw_pane_get_type(void);
  void    candy_draw_pane_clear(CandyDrawPane *cdp);
  
  G_END_DECLS
  #endif 
  </code></pre>
  
  <p>Any insight is much appreciated. I do realize I could use a code-generating IDE and crank something out more quickly, and probably dodge having to deal with some of this stuff, but the whole point of this exercise is to get a good grasp of the Gtk runtime, so I'd prefer to write the boilerplate by hand.</p>