0

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) failed GTK_CRITICAL**: IA__gtk_container_add : assertion GTK_IS_WIDGET(widget) failed

What am I doing wrong? Or even I what in the world is this error about?

2
  • Try compiling with all warnings i.e. -Wall option in gcc ... You are bound to get a warning at if (!pro_note_tab_get_type) statement as pointed out by @Lews Therin, it should be if(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 to static Commented Sep 23, 2011 at 12:04
  • 1
    You might want to check out G_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. Commented Sep 24, 2011 at 19:56

3 Answers 3

1

According to the docs, gtk_type_unique is "deprecated and should not be used in newly-written code".

Use g_type_register_static instead. More so if you are trying to master GObject, not old Gtk+.

Anyway, I'd say that your error is due to some of the NULL function pointers you are setting, some are probably not optional, but this is poorly documented.

Sign up to request clarification or add additional context in comments.

Comments

0

For one, the pro_note_tab_type variable in pro_note_tab_get_type() really looks like it should be static.

Comments

0

That must be the problem

 if (!pro_note_tab_get_type)
{

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.