Advertisement
bretjoseph

GtkSurfaces

Jan 4th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include <gtk/gtk.h>
  2.  
  3. GtkWidget *window, *child, *subchild;
  4.  
  5. #define EXAMPLE_FIXED_TYPE (example_fixed_get_type())
  6. G_DECLARE_FINAL_TYPE(ExampleFixed, example_fixed, Example, Fixed, GtkFixed)
  7.  
  8. #define EXAMPLE_BOX_TYPE (example_box_get_type())
  9. G_DECLARE_FINAL_TYPE(ExampleBox, example_box, Example, Box, GtkBox)
  10.  
  11. struct _ExampleFixed
  12. {
  13.     GtkFixed parent;
  14. };
  15.  
  16. struct _ExampleBox
  17. {
  18.     GtkBox parent;
  19. };
  20.  
  21. G_DEFINE_TYPE(ExampleFixed, example_fixed, GTK_TYPE_FIXED);
  22. G_DEFINE_TYPE(ExampleBox, example_box, GTK_TYPE_BOX);
  23.  
  24. static void example_fixed_init(ExampleFixed *fixed)
  25. {
  26.     gtk_widget_set_has_surface (GTK_WIDGET (fixed), TRUE);
  27. }
  28.  
  29. static void example_box_init(ExampleBox *box)
  30. {
  31.     gtk_widget_set_has_surface (GTK_WIDGET (box), TRUE);
  32. }
  33.  
  34. static void example_fixed_realize(GtkWidget *widget)
  35. {
  36.     const GtkAllocation allocation;
  37.     GdkSurface *surface, *fixedsurf;
  38.  
  39.     surface = gtk_widget_get_surface(window);
  40.     fixedsurf = gdk_surface_new_child(surface, &allocation);
  41.     gtk_widget_set_surface(widget, fixedsurf);
  42.     gtk_widget_register_surface(widget, fixedsurf);
  43.     GTK_WIDGET_CLASS(example_fixed_parent_class)->realize(widget);
  44. }
  45.  
  46. static void example_box_realize(GtkWidget *widget)
  47. {
  48.     const GtkAllocation allocation;
  49.     GdkSurface *surface, *boxsurf;
  50.  
  51.     surface = gtk_widget_get_surface(window);
  52.     boxsurf = gdk_surface_new_child(surface, &allocation);
  53.     gtk_widget_set_surface(widget, boxsurf);
  54.     gtk_widget_register_surface(widget, boxsurf);
  55.     GTK_WIDGET_CLASS(example_box_parent_class)->realize(widget);
  56. }
  57.  
  58.  
  59. static void example_fixed_class_init(ExampleFixedClass *klass)
  60. {
  61.     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
  62.     widget_class->realize = example_fixed_realize;
  63. }
  64.  
  65. static void example_box_class_init(ExampleBoxClass *klass)
  66. {
  67.     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
  68.     widget_class->realize = example_box_realize;
  69. }
  70.  
  71. GtkWidget *example_fixed_new()
  72. {
  73.     return g_object_new(EXAMPLE_FIXED_TYPE, NULL);
  74. }
  75.  
  76. GtkWidget *example_box_new()
  77. {
  78.     return g_object_new(EXAMPLE_BOX_TYPE, NULL);
  79. }
  80.  
  81. static void activate (GtkApplication* app, gpointer user_data)
  82. {
  83.   window = gtk_application_window_new (app);
  84.   gtk_window_set_title (GTK_WINDOW (window), "Window");
  85.   gtk_window_set_default_size (GTK_WINDOW (window), 1024, 768);
  86.   child = example_fixed_new();
  87.   subchild = example_box_new();
  88.   gtk_container_add(GTK_CONTAINER(child), subchild);
  89.   gtk_container_add(GTK_CONTAINER(window), child);
  90.   gtk_widget_show (window);
  91. }
  92.  
  93. int main (int argc, char **argv)
  94. {
  95.   GtkApplication *app;
  96.   int status;
  97.  
  98.   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  99.   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  100.   status = g_application_run (G_APPLICATION (app), argc, argv);
  101.   g_object_unref (app);
  102.  
  103.   return status;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement