Advertisement
Guest User

dumbhex-test.c

a guest
Feb 21st, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. /* dumbhex-test.c */
  2.  
  3. #include <gtk/gtk.h>
  4.  
  5. #define is_displayable(c) (((c) >= 0x20) && ((c) < 0x7f))
  6.  
  7. #define DUMBHEX_TYPE_BYTE dumbhex_byte_get_type()
  8. G_DECLARE_FINAL_TYPE (DumbhexByte, dumbhex_byte, DUMBHEX, BYTE, GObject)
  9.  
  10. struct _DumbhexByte
  11. {
  12.     GObject parent_instance;
  13.  
  14.     char byte;
  15.     guint offset;
  16. };
  17.  
  18. G_DEFINE_TYPE (DumbhexByte, dumbhex_byte, G_TYPE_OBJECT)
  19.  
  20. static void
  21. dumbhex_byte_init (DumbhexByte *self)
  22. {
  23. }
  24.  
  25. static void
  26. dumbhex_byte_class_init (DumbhexByteClass *klass)
  27. {
  28. }
  29.  
  30. static DumbhexByte *
  31. dumbhex_byte_new (char byte, gint64 offset)
  32. {
  33.     DumbhexByte *self = g_object_new (DUMBHEX_TYPE_BYTE, NULL);
  34.     self->byte = byte;
  35.     self->offset = offset;
  36.  
  37.     return self;
  38. }
  39.  
  40. static char
  41. dumbhex_byte_get_byte (DumbhexByte *self)
  42. {
  43.     return self->byte;
  44. }
  45.  
  46. static gint64
  47. dumbhex_byte_get_offset (DumbhexByte *self)
  48. {
  49.     return self->offset;
  50. }
  51.  
  52. #define DUMBHEX_TYPE_WIDGET dumbhex_widget_get_type()
  53. G_DECLARE_FINAL_TYPE (DumbhexWidget, dumbhex_widget, DUMBHEX, WIDGET, GtkWidget)
  54.  
  55. struct _DumbhexWidget
  56. {
  57.     GtkWidget parent_instance;
  58.  
  59.     GtkWidget *listview;
  60. };
  61.  
  62. G_DEFINE_TYPE (DumbhexWidget, dumbhex_widget, GTK_TYPE_WIDGET)
  63.  
  64. static void
  65. factory_bind_cb (DumbhexWidget *self,
  66.         GtkListItem *list_item,
  67.         GtkSignalListItemFactory *factory)
  68. {
  69.     DumbhexByte *byte = gtk_list_item_get_item (list_item);
  70.     GtkWidget *label = gtk_list_item_get_child (list_item);
  71.     char c = dumbhex_byte_get_byte (byte);
  72.     g_autofree char *str;
  73.  
  74.     if (is_displayable (c))
  75.         str = g_strdup_printf ("%c", dumbhex_byte_get_byte (byte));
  76.     else
  77.         str = g_strdup (".");
  78.  
  79.     gtk_label_set_text (GTK_LABEL(label), str);
  80. }
  81.  
  82. static void
  83. factory_setup_cb (DumbhexWidget *self,
  84.         GtkListItem *list_item,
  85.         GtkSignalListItemFactory *factory)
  86. {
  87.     GtkWidget *label = gtk_label_new (NULL);
  88.     gtk_list_item_set_child (list_item, label);
  89. }
  90.  
  91. static void
  92. do_factory (DumbhexWidget *self)
  93. {
  94.     GtkListItemFactory *factory;
  95.  
  96.     factory = gtk_signal_list_item_factory_new ();
  97.     g_signal_connect_swapped (factory, "setup", G_CALLBACK(factory_setup_cb), self);
  98.     g_signal_connect_swapped (factory, "bind", G_CALLBACK(factory_bind_cb), self);
  99.     gtk_list_view_set_factory (GTK_LIST_VIEW(self->listview), factory);
  100. }
  101.  
  102. static void
  103. do_selection_model (DumbhexWidget *self, GListModel *model)
  104. {
  105.     GtkMultiSelection *selection;
  106.  
  107.     selection = gtk_multi_selection_new (model);
  108.     gtk_list_view_set_model (GTK_LIST_VIEW(self->listview), GTK_SELECTION_MODEL(selection));
  109. }
  110.  
  111. static void
  112. do_store (DumbhexWidget *self)
  113. {
  114.     GListStore *store;
  115.     GRand *rand = g_rand_new ();
  116.  
  117.     store = g_list_store_new (DUMBHEX_TYPE_BYTE);
  118.  
  119.     for (guint i = 0; i < 10000; ++i)
  120.     {
  121.         char c = g_rand_int_range (rand, 0, 127);
  122.         DumbhexByte *byte = dumbhex_byte_new (c, i);
  123.         g_list_store_append (store, byte);
  124.     }
  125.  
  126.     do_selection_model (self, G_LIST_MODEL(store));
  127. }
  128.  
  129. static void
  130. dumbhex_widget_init (DumbhexWidget *self)
  131. {
  132.     self->listview = gtk_list_view_new (NULL, NULL);
  133.     gtk_scrollable_set_hscroll_policy (GTK_SCROLLABLE(self->listview), GTK_SCROLL_NATURAL);
  134.     gtk_scrollable_set_vscroll_policy (GTK_SCROLLABLE(self->listview), GTK_SCROLL_NATURAL);
  135.     gtk_widget_set_parent (self->listview, GTK_WIDGET(self));
  136.  
  137.     do_factory (self);
  138.     do_store (self);
  139. }
  140.  
  141. static void
  142. dumbhex_widget_class_init (DumbhexWidgetClass *klass)
  143. {
  144.     GObjectClass *object_class = G_OBJECT_CLASS(klass);
  145.  
  146.     gtk_widget_class_set_layout_manager_type (GTK_WIDGET_CLASS(klass), GTK_TYPE_BIN_LAYOUT);
  147. }
  148.  
  149. GtkWidget *
  150. dumbhex_widget_new (void)
  151. {
  152.     return g_object_new (DUMBHEX_TYPE_WIDGET, NULL);
  153. }
  154.  
  155. static void
  156. activate_cb (GtkApplication *app)
  157. {
  158.     GtkWidget *window = gtk_application_window_new (app);
  159.     GtkWidget *sw = gtk_scrolled_window_new ();
  160.     GtkWidget *dumb = dumbhex_widget_new ();
  161.  
  162.     gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW(sw), dumb);
  163.     gtk_window_set_child (GTK_WINDOW(window), sw);
  164.     gtk_window_set_default_size (GTK_WINDOW(window), 300, 480);
  165.     gtk_window_present (GTK_WINDOW(window));
  166. }
  167.  
  168. int main
  169. (int argc, char *argv[])
  170. {
  171.     GtkApplication *app = gtk_application_new ("com.gitlab.LARathbone.DumbApp", G_APPLICATION_FLAGS_NONE);
  172.     g_signal_connect (app, "activate", G_CALLBACK(activate_cb), NULL);
  173.     return g_application_run (G_APPLICATION(app), argc, argv);
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement