1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "image.h"
30
31 #include "container.h"
32 #include "menu.h"
33 #include "../common/context.h"
34 #include "../common/object.h"
35
36
37 UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
38 UiObject *current = uic_current_obj(obj);
39
40 GtkWidget *scrolledwindow =
SCROLLEDWINDOW_NEW();
41 #if GTK_CHECK_VERSION(
4,
0,
0)
42 GtkWidget *image = gtk_picture_new();
43 #else
44 GtkWidget *image = gtk_image_new();
45 #endif
46
47 ui_set_name_and_style(image, args.name, args.style_class);
48
49 #if GTK_MAJOR_VERSION <
4
50 GtkWidget *eventbox = gtk_event_box_new();
51 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, eventbox);
52 gtk_container_add(
GTK_CONTAINER(eventbox), image);
53 #else
54 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, image);
55 GtkWidget *eventbox = image;
56 #endif
57
58 UI_APPLY_LAYOUT1(current, args);
59 current->container->add(current->container, scrolledwindow,
TRUE);
60
61 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname,
UI_VAR_GENERIC);
62 if(var) {
63 UiGeneric *value = var->value;
64 value->get = ui_imageviewer_get;
65 value->get_type = ui_imageviewer_get_type;
66 value->set = ui_imageviewer_set;
67 value->obj = image;
68 if(value->value && value->type && !strcmp(value->type,
UI_IMAGE_OBJECT_TYPE)) {
69 GdkPixbuf *pixbuf = value->value;
70 value->value =
NULL;
71 ui_imageviewer_set(value, pixbuf,
UI_IMAGE_OBJECT_TYPE);
72 }
73 }
74
75 if(args.contextmenu) {
76 UIMENU menu = ui_contextmenu_create(args.contextmenu, obj, eventbox);
77 ui_widget_set_contextmenu(eventbox, menu);
78 }
79
80 return scrolledwindow;
81 }
82
83 void* ui_imageviewer_get(UiGeneric *g) {
84 return g->value;
85 }
86
87 const char* ui_imageviewer_get_type(UiGeneric *g) {
88
89 }
90
91 int ui_imageviewer_set(UiGeneric *g,
void *value,
const char *type) {
92 if(!type || strcmp(type,
UI_IMAGE_OBJECT_TYPE)) {
93 return 1;
94 }
95
96
97
98 g->value = value;
99 g->type = type;
100 GdkPixbuf *pixbuf = value;
101
102 if(pixbuf) {
103 int width = gdk_pixbuf_get_width(pixbuf);
104 int height = gdk_pixbuf_get_height(pixbuf);
105
106 #if GTK_CHECK_VERSION(
4,
0,
0)
107 GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
108 gtk_picture_set_paintable(
GTK_PICTURE(g->obj),
GDK_PAINTABLE(texture));
109 #else
110 gtk_image_set_from_pixbuf(
GTK_IMAGE(g->obj), pixbuf);
111 #endif
112 gtk_widget_set_size_request(g->obj, width, height);
113 }
114
115
116 return 0;
117 }
118
119
120
121 int ui_image_load_file(UiGeneric *obj,
const char *path) {
122 GError *error =
NULL;
123 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
124 if(!pixbuf) {
125 return 1;
126 }
127
128 if(obj->set) {
129 obj->set(obj, pixbuf,
UI_IMAGE_OBJECT_TYPE);
130 }
else {
131 obj->value = pixbuf;
132 }
133
134 return 0;
135 }
136