1#include <gtk/gtk.h>
2
3
4
5typedef struct _GtkTextureView GtkTextureView;
6typedef struct _GtkTextureViewClass GtkTextureViewClass;
7
8#define GTK_TYPE_TEXTURE_VIEW (gtk_texture_view_get_type ())
9#define GTK_TEXTURE_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj, GTK_TYPE_TEXTURE_VIEW, GtkTextureView))
10#define GTK_TEXTURE_VIEW_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST(cls, GTK_TYPE_TEXTURE_VIEW, GtkTextureViewClass))
11struct _GtkTextureView
12{
13 GtkWidget parent_instance;
14
15 GdkTexture *texture;
16};
17
18struct _GtkTextureViewClass
19{
20 GtkWidgetClass parent_class;
21};
22
23GType gtk_texture_view_get_type (void) G_GNUC_CONST;
24
25
26G_DEFINE_TYPE(GtkTextureView, gtk_texture_view, GTK_TYPE_WIDGET)
27
28static void
29gtk_texture_view_measure (GtkWidget *widget,
30 GtkOrientation orientation,
31 int for_size,
32 int *minimum,
33 int *natural,
34 int *minimum_baseline,
35 int *natural_baseline)
36{
37 GtkTextureView *self = GTK_TEXTURE_VIEW (widget);
38
39 if (self->texture == NULL)
40 return;
41
42 if (orientation == GTK_ORIENTATION_HORIZONTAL)
43 {
44 *minimum = 0;
45 *natural = gdk_texture_get_width (texture: self->texture);
46 }
47 else /* VERTICAL */
48 {
49 *minimum = 0;
50 *natural = gdk_texture_get_height (texture: self->texture);
51 }
52}
53
54static void
55gtk_texture_view_snapshot (GtkWidget *widget,
56 GtkSnapshot *snapshot)
57{
58 GtkTextureView *self = GTK_TEXTURE_VIEW (widget);
59 int width = gtk_widget_get_width (widget);
60 int height = gtk_widget_get_height (widget);
61
62 if (self->texture != NULL)
63 {
64 graphene_rect_t bounds;
65
66 bounds.origin.x = MAX (0, width / 2 - gdk_texture_get_width (self->texture));
67 bounds.origin.y = MAX (0, height / 2 - gdk_texture_get_height (self->texture));
68
69 bounds.size.width = MIN (width, gdk_texture_get_width (self->texture));
70 bounds.size.height = MIN (height, gdk_texture_get_height (self->texture));
71
72 gtk_snapshot_append_texture (snapshot, texture: self->texture, bounds: &bounds);
73 }
74}
75
76static void
77gtk_texture_view_finalize (GObject *object)
78{
79 GtkTextureView *self = GTK_TEXTURE_VIEW (object);
80
81 g_clear_object (&self->texture);
82
83 G_OBJECT_CLASS (gtk_texture_view_parent_class)->finalize (object);
84}
85
86static void
87gtk_texture_view_init (GtkTextureView *self)
88{
89}
90
91static void
92gtk_texture_view_class_init (GtkTextureViewClass *klass)
93{
94 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
96
97 object_class->finalize = gtk_texture_view_finalize;
98
99 widget_class->measure = gtk_texture_view_measure;
100 widget_class->snapshot = gtk_texture_view_snapshot;
101}
102
103static void
104quit_cb (GtkWidget *widget,
105 gpointer data)
106{
107 gboolean *done = data;
108
109 *done = TRUE;
110
111 g_main_context_wakeup (NULL);
112}
113
114int
115main (int argc, char **argv)
116{
117 GtkWidget *window;
118 GtkWidget *view;
119 GdkTexture *texture;
120 GFile *file;
121 GError *error = NULL;
122 gboolean done = FALSE;
123
124 gtk_init ();
125
126 if (argc != 2)
127 {
128 g_error ("No texture file path given.");
129 return -1;
130 }
131
132 file = g_file_new_for_path (path: argv[1]);
133 texture = gdk_texture_new_from_file (file, error: &error);
134
135 if (error != NULL)
136 {
137 g_error ("Error: %s", error->message);
138 return -1;
139 }
140
141 window = gtk_window_new ();
142 g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
143 view = g_object_new (GTK_TYPE_TEXTURE_VIEW, NULL);
144 ((GtkTextureView*)view)->texture = g_steal_pointer (&texture);
145
146 gtk_window_set_child (GTK_WINDOW (window), child: view);
147
148 gtk_widget_show (widget: window);
149
150 while (!done)
151 g_main_context_iteration (NULL, TRUE);
152
153 g_object_unref (object: file);
154
155 return 0;
156}
157

source code of gtk/tests/testtexture.c