Skip to content

Instantly share code, notes, and snippets.

@chergert
Last active May 15, 2024 20:40
Show Gist options
  • Save chergert/7409632e6bcf2416d6d18cbb573bdfb2 to your computer and use it in GitHub Desktop.
Save chergert/7409632e6bcf2416d6d18cbb573bdfb2 to your computer and use it in GitHub Desktop.
simple widget example
#include "foo-widget.h"
struct _FooWidget
{
GtkBin parent_instance;
GtkLabel *label;
};
G_DEFINE_TYPE (FooWidget, foo_widget, GTK_TYPE_BIN)
static void
foo_widget_class_init (FooWidgetClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
gtk_widget_class_set_css_name (widget_class, "foowidget");
gtk_widget_class_set_template_from_resource (widget_class, "/foo/widget.ui");
gtk_widget_class_bind_template_child (widget_class, FooWidget, label);
}
static void
foo_widget_init (FooWidget *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
}
GtkWidget *
foo_widget_new (void)
{
return g_object_new (FOO_TYPE_WIDGET, NULL);
}
#ifndef FOO_WIDGET_H
#define FOO_WIDGET_H
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define FOO_TYPE_WIDGET (foo_widget_get_type())
G_DECLARE_FINAL_TYPE (FooWidget, foo_widget, FOO, WIDGET, GtkBin)
GtkWidget *foo_widget_new (void);
G_END_DECLS
#endif /* FOO_WIDGET_H */
<?xml version="1.0" encoding="utf-8"?>
<interface>
<template class="FooWidget" parent="GtkBin">
<child>
<object class="GtkLabel" id="label">
<property name="label">Hello, World!</property>
<property name="visible">true</property>
</object>
</child>
</template>
</interface>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment