Re: [gtk-list] Creating widget from scratch



On Sun, 1 Mar 1998, Mike Frisch wrote:

> 
> I am a novice Gtk programmer and have taken on the creation of a new
> widget (a ledger-type widget for use in an accounting package) as a
> learning exercise.  I have followed the "Writing Your Own Widgets" section
> in the GTK Tutorial as well as looked at the source code to other widgets
> and am still having a small problem: Whenever I create my widget
> (GtkLedger), I get a "** WARNING **: invalid cast from "(unknown)" to
> "GtkWidget" message when the application is started.
> 
> To me, it would appear that my new widget type has not been registered,
> but I have stepped through the code of "gtk_ledger_get_type()" (copied
> from the GTK Tutorial and modified for my widget) to ensure it returns a
> proper return code.  And this code is being called from
> "gtk_ledger_new()", so I know it's being executed.
> 
> Does anybody have any idea as to what I may have overlooked?

i figured (on #gimp) that the most common error that novice widget writers
make, is to
struct _MyWidget
{
  GtkButton *button;
  [...]
};
this needs to be

struct _MyWidget
{
  GtkButton button; /* <- GtkButton is parent type! */
  [...]
};

since you can't derive from a pointer, but from a button structure ;))
other places that are important to watch is

struct _MyWidgetClass
{
  GtkButtonClass parent_class; /* <- GtkButton is parent type,
                                *    hence GtkButtonClass! */
};

the class has to be of the type that you derived from above.

also in the gtk_my_widget_get_type() routine, it is important to have the
same derivation type:

 my_widget_type = gtk_type_unique (gtk_button_get_type (), &my_widget_info);
				   /*  ^^^^^^ our parent type again */

also the static parent_class variable needs to be taken care about:

static GtkButtonClass *parent_class = NULL;
       /* ^^^^^^ we are derived from a button! */

and in gtk_my_widget_class_init:

  parent_class = gtk_type_class (gtk_button_get_type ());
                                 /*  ^^^^^^ and we need the class of
                                  * a button! */

hope this helps...

> 
> Much thanks.
> 
> Mike.
> 

---
ciaoTJ



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