I am trying to master the GObject Library. So I tried to make a simple Gtk+ Custom Widget by inheriting from GtkHBox. I can't figure out what the problem is or even where the problem is so I'll have to paste the entire code. Here is the code:
notetab.h
#ifndef NOTETAB_H
#define NOTETAB_H
G_BEGIN_DECLS
#define PRO_NOTE_TAB(obj) GTK_CHECK_CAST(obj, pro_note_tab_get_type (), ProNoteTab)
#define GTK_CPU_CLASS(klass) GTK_CHECK_CLASS_CAST(klass, pro_note_tab_get_type(), ProNoteTabClass)
#define GTK_IS_CPU(obj) GTK_CHECK_TYPE(obj, pro_note_tab_get_type())
typedef struct _ProNoteTab ProNoteTab;
typedef struct _ProNoteTabClass ProNoteTabClass;
struct _ProNoteTab
{
GtkWidget hbox;
GtkObject parent_instance;
GtkLabel label;
GtkButton cbtn;
};
struct _ProNoteTabClass
{
GtkHBoxClass parent_class;
};
GtkType pro_note_tab_get_type(void);
GtkWidget* pro_note_tab_new(void);
G_END_DECLS
#endif
notetab.c
#include "common.h"
#include "notetab.h"
GtkType pro_note_tab_get_type()
{
GtkType pro_note_tab_type = 0;
if (!pro_note_tab_get_type)
{
static const GtkTypeInfo pro_note_tab_info =
{
"ProNoteTab",
sizeof(ProNoteTab),
sizeof(ProNoteTabClass),
(GtkClassInitFunc) NULL,
(GtkObjectInitFunc) NULL,
NULL,
NULL,
(GtkClassInitFunc) NULL
};
pro_note_tab_type = gtk_type_unique(GTK_TYPE_WIDGET, &pro_note_tab_info);
}
return pro_note_tab_type;
}
GtkWidget* pro_note_tab_new(void)
{
return GTK_WIDGET(gtk_type_new(pro_note_tab_get_type()));
}
Now the program compiles perfectly fine. But the error I get at runtime is:
GTK_CRITICAL**: IA__gtk_type_new : assertion
GTK_TYPE_IS_OBJECT(type) failedGTK_CRITICAL**: IA__gtk_container_add : assertionGTK_IS_WIDGET(widget) failed
What am I doing wrong? Or even I what in the world is this error about?
-Walloption ingcc... You are bound to get a warning atif (!pro_note_tab_get_type)statement as pointed out by @Lews Therin, it should beif(pro_note_tab_type)without get in the variable name which might be cause of the problem. Also consider the suggestion provided by @unwind related tostaticG_DEFINE_TYPE(), that will expand to most of the above code for you, with the added bonus that it will be much harder for errors to sneak in there.