Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* dumbhex-test.c */
- #include <gtk/gtk.h>
- #define is_displayable(c) (((c) >= 0x20) && ((c) < 0x7f))
- #define DUMBHEX_TYPE_BYTE dumbhex_byte_get_type()
- G_DECLARE_FINAL_TYPE (DumbhexByte, dumbhex_byte, DUMBHEX, BYTE, GObject)
- struct _DumbhexByte
- {
- GObject parent_instance;
- char byte;
- guint offset;
- };
- G_DEFINE_TYPE (DumbhexByte, dumbhex_byte, G_TYPE_OBJECT)
- static void
- dumbhex_byte_init (DumbhexByte *self)
- {
- }
- static void
- dumbhex_byte_class_init (DumbhexByteClass *klass)
- {
- }
- static DumbhexByte *
- dumbhex_byte_new (char byte, gint64 offset)
- {
- DumbhexByte *self = g_object_new (DUMBHEX_TYPE_BYTE, NULL);
- self->byte = byte;
- self->offset = offset;
- return self;
- }
- static char
- dumbhex_byte_get_byte (DumbhexByte *self)
- {
- return self->byte;
- }
- static gint64
- dumbhex_byte_get_offset (DumbhexByte *self)
- {
- return self->offset;
- }
- #define DUMBHEX_TYPE_WIDGET dumbhex_widget_get_type()
- G_DECLARE_FINAL_TYPE (DumbhexWidget, dumbhex_widget, DUMBHEX, WIDGET, GtkWidget)
- struct _DumbhexWidget
- {
- GtkWidget parent_instance;
- GtkWidget *listview;
- };
- G_DEFINE_TYPE (DumbhexWidget, dumbhex_widget, GTK_TYPE_WIDGET)
- static void
- factory_bind_cb (DumbhexWidget *self,
- GtkListItem *list_item,
- GtkSignalListItemFactory *factory)
- {
- DumbhexByte *byte = gtk_list_item_get_item (list_item);
- GtkWidget *label = gtk_list_item_get_child (list_item);
- char c = dumbhex_byte_get_byte (byte);
- g_autofree char *str;
- if (is_displayable (c))
- str = g_strdup_printf ("%c", dumbhex_byte_get_byte (byte));
- else
- str = g_strdup (".");
- gtk_label_set_text (GTK_LABEL(label), str);
- }
- static void
- factory_setup_cb (DumbhexWidget *self,
- GtkListItem *list_item,
- GtkSignalListItemFactory *factory)
- {
- GtkWidget *label = gtk_label_new (NULL);
- gtk_list_item_set_child (list_item, label);
- }
- static void
- do_factory (DumbhexWidget *self)
- {
- GtkListItemFactory *factory;
- factory = gtk_signal_list_item_factory_new ();
- g_signal_connect_swapped (factory, "setup", G_CALLBACK(factory_setup_cb), self);
- g_signal_connect_swapped (factory, "bind", G_CALLBACK(factory_bind_cb), self);
- gtk_list_view_set_factory (GTK_LIST_VIEW(self->listview), factory);
- }
- static void
- do_selection_model (DumbhexWidget *self, GListModel *model)
- {
- GtkMultiSelection *selection;
- selection = gtk_multi_selection_new (model);
- gtk_list_view_set_model (GTK_LIST_VIEW(self->listview), GTK_SELECTION_MODEL(selection));
- }
- static void
- do_store (DumbhexWidget *self)
- {
- GListStore *store;
- GRand *rand = g_rand_new ();
- store = g_list_store_new (DUMBHEX_TYPE_BYTE);
- for (guint i = 0; i < 10000; ++i)
- {
- char c = g_rand_int_range (rand, 0, 127);
- DumbhexByte *byte = dumbhex_byte_new (c, i);
- g_list_store_append (store, byte);
- }
- do_selection_model (self, G_LIST_MODEL(store));
- }
- static void
- dumbhex_widget_init (DumbhexWidget *self)
- {
- self->listview = gtk_list_view_new (NULL, NULL);
- gtk_scrollable_set_hscroll_policy (GTK_SCROLLABLE(self->listview), GTK_SCROLL_NATURAL);
- gtk_scrollable_set_vscroll_policy (GTK_SCROLLABLE(self->listview), GTK_SCROLL_NATURAL);
- gtk_widget_set_parent (self->listview, GTK_WIDGET(self));
- do_factory (self);
- do_store (self);
- }
- static void
- dumbhex_widget_class_init (DumbhexWidgetClass *klass)
- {
- GObjectClass *object_class = G_OBJECT_CLASS(klass);
- gtk_widget_class_set_layout_manager_type (GTK_WIDGET_CLASS(klass), GTK_TYPE_BIN_LAYOUT);
- }
- GtkWidget *
- dumbhex_widget_new (void)
- {
- return g_object_new (DUMBHEX_TYPE_WIDGET, NULL);
- }
- static void
- activate_cb (GtkApplication *app)
- {
- GtkWidget *window = gtk_application_window_new (app);
- GtkWidget *sw = gtk_scrolled_window_new ();
- GtkWidget *dumb = dumbhex_widget_new ();
- gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW(sw), dumb);
- gtk_window_set_child (GTK_WINDOW(window), sw);
- gtk_window_set_default_size (GTK_WINDOW(window), 300, 480);
- gtk_window_present (GTK_WINDOW(window));
- }
- int main
- (int argc, char *argv[])
- {
- GtkApplication *app = gtk_application_new ("com.gitlab.LARathbone.DumbApp", G_APPLICATION_FLAGS_NONE);
- g_signal_connect (app, "activate", G_CALLBACK(activate_cb), NULL);
- return g_application_run (G_APPLICATION(app), argc, argv);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement