Web lists-archives.com

Re: Catalog of GObject-specific design patterns?

On Fri, Aug 11, 2017 at 09:16:32AM +0700, Arnaud wrote:
> As for using the `g_object_set_data()` like you do, it's something I
> always tried to stay away from, because it seems to me that everything
> becomes possible with that, and there's probably plenty of mistakes to
> be made ;) On the other hand, there must be some situations where it's
> the only solution. I'd be curious to know what the old-timers think
> about it, when to use it and when to avoid it.

A bad example in Devhelp:

The function creates a new tab and appends it to the GtkNotebook. vbox
is the content of the tab, which contains web_view and info_bar. In
other DhWindow functions, we have access to the GtkNotebook, but from
the GtkNotebook we have access only to the vbox. So with the vbox we can
call g_object_get_data() to get the web_view and info_bar.

This is a typical example where it is better to create a new class:
called for example DhTab, a subclass of GtkBox or GtkGrid. The web_view
and info_bar are simply stored as instance variables in DhTab. This
would also have the nice benefit to have less code in DhWindow, because
DhWindow would delegate some of its work to DhTab.

For other use-cases, creating a subclass is a too heavy solution, or
sometimes creating a subclass is even not possible/desirable as I
explained in my blog post.

An example where creating a subclass would be a too heavy solution: to
implement a context/right-click menu, attaching some information to menu
items, so when the ::activate signal is called we have the required info
to implement the action. For instance for spell-checking: the context
menu to choose another word:

> At last, I take this opportunity to share my bookmark post on GObject. I
> collected all the interesting bits I found on the Net while learning
> from zero, plus added some personal thoughts, and in the end it's just a
> mess, but learners might find interesting bits there, who knows.
> https://github.com/elboulangero/goodvibes/wiki/GObject-Introduction

I've written also a small intro to GObject here:

But not how to subclass GObject, just how to *use* an existing GObject

gtk-devel-list mailing list