ui/gtk/image.c

4 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 29 Mar 2025 18:17:01 +0100 (4 days ago)
changeset 529
0a4a6b0d1c82
parent 390
b130f80ec7f9
child 530
7992a44fe719
permissions
-rw-r--r--

add new imageviewer implementation (GTK)

339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "image.h"
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "container.h"
389
d15eca5fd8b3 implement image viewer context menus (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 381
diff changeset
32 #include "menu.h"
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "../common/context.h"
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../common/object.h"
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
36 static void imageviewer_destroy(UiImageViewer *iv) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
37 if(iv->pixbuf) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
38 g_object_unref(iv->pixbuf);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
39 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
40 free(iv);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
41 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
42
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
43 #if GTK_MAJOR_VERSION >= 4
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
44
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
45 static void imageviewer_draw(
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
46 GtkDrawingArea *drawingarea,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
47 cairo_t *cr,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
48 int width,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
49 int height,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
50 gpointer userdata)
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
51 {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
52 ui_cairo_draw_image(userdata, cr, width, height);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
53 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
54
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
55 #else
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
56
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
57 static gboolean imageviewer_draw(GtkWidget *widget, cairo_t *cr, gpointer userdata) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
58 int width = gtk_widget_get_allocated_width(widget);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
59 int height = gtk_widget_get_allocated_height(widget);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
60 ui_cairo_draw_image(userdata, cr, width, height);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
61 return FALSE;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
62 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
63
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
64 #endif
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 UiObject *current = uic_current_obj(obj);
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
69 GtkWidget *drawingarea = gtk_drawing_area_new();
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
70 GtkWidget *toplevel;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
71 GtkWidget *widget = drawingarea;
340
2dd42bd4fe5d add imageviewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 339
diff changeset
72
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
73 gtk_widget_set_size_request(drawingarea, 100, 100);
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 #if GTK_MAJOR_VERSION < 4
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 GtkWidget *eventbox = gtk_event_box_new();
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
77 gtk_container_add(GTK_CONTAINER(eventbox), drawingarea);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
78 widget = eventbox;
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 #endif
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
81 if(args.scrollarea) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
82 toplevel = SCROLLEDWINDOW_NEW();
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
83 SCROLLEDWINDOW_SET_CHILD(toplevel, widget);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
84 args.adjustsize = TRUE;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
85 } else {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
86 toplevel = widget;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
87 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
88
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
89 UiImageViewer *imgviewer = malloc(sizeof(UiImageViewer));
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
90 memset(imgviewer, 0, sizeof(UiImageViewer));
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
91 if(args.image_padding > 0) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
92 imgviewer->padding_left = args.image_padding;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
93 imgviewer->padding_right = args.image_padding;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
94 imgviewer->padding_top = args.image_padding;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
95 imgviewer->padding_bottom = args.image_padding;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
96 } else {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
97 imgviewer->padding_left = args.image_padding_left;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
98 imgviewer->padding_right = args.image_padding_right;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
99 imgviewer->padding_top = args.image_padding_top;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
100 imgviewer->padding_bottom = args.image_padding_bottom;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
101 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
102 imgviewer->adjustsize = args.adjustsize;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
103 imgviewer->autoscale = args.autoscale;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
104 imgviewer->useradjustable = args.useradjustable;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
105
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
106 g_object_set_data_full(G_OBJECT(drawingarea), "uiimageviewer", imgviewer, (GDestroyNotify)imageviewer_destroy);
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
109 imgviewer->var = var;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
110 imgviewer->widget = drawingarea;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
111
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 if(var) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 UiGeneric *value = var->value;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 value->get = ui_imageviewer_get;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 value->get_type = ui_imageviewer_get_type;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 value->set = ui_imageviewer_set;
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
117 value->obj = imgviewer;
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 if(value->value && value->type && !strcmp(value->type, UI_IMAGE_OBJECT_TYPE)) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 GdkPixbuf *pixbuf = value->value;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 value->value = NULL;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 ui_imageviewer_set(value, pixbuf, UI_IMAGE_OBJECT_TYPE);
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
125 #if GTK_MAJOR_VERSION >= 4
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
126 gtk_drawing_area_set_draw_func(
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
127 GTK_DRAWING_AREA(drawingarea),
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
128 imageviewer_draw,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
129 imgviewer,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
130 NULL);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
131 #elif GTK_MAJOR_VERSION == 3
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
132 g_signal_connect(
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
133 drawingarea,
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
134 "draw",
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
135 G_CALLBACK(imageviewer_draw),
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
136 imgviewer);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
137
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
138 #endif
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
139
389
d15eca5fd8b3 implement image viewer context menus (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 381
diff changeset
140 if(args.contextmenu) {
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
141 UIMENU menu = ui_contextmenu_create(args.contextmenu, obj, widget);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
142 ui_widget_set_contextmenu(widget, menu);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
143 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
144
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
145 UI_APPLY_LAYOUT1(current, args);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
146 current->container->add(current->container, toplevel, TRUE);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
147
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
148 return toplevel;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
149 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
150
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
151
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
152 void ui_cairo_draw_image(UiImageViewer *imgviewer, cairo_t *cr, int width, int height) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
153 if(!imgviewer->pixbuf) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
154 return;
389
d15eca5fd8b3 implement image viewer context menus (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 381
diff changeset
155 }
d15eca5fd8b3 implement image viewer context menus (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 381
diff changeset
156
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
157 GdkPixbuf *pixbuf = imgviewer->pixbuf;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
158 double dpixwidth = (double)gdk_pixbuf_get_width(pixbuf);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
159 double dpixheight = (double)gdk_pixbuf_get_height(pixbuf);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
160
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
161 double dwidth = width;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
162 double dheight = height;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
163 if(imgviewer->autoscale) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
164 double scale = dwidth / dpixwidth;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
165 if(dpixheight * scale > dheight) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
166 scale = dheight / dpixheight;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
167 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
168
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
169 dpixwidth *= scale;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
170 dpixheight *= scale;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
171 double x = (dwidth - dpixwidth) / 2;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
172 double y = (dheight - dpixheight) / 2;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
173 cairo_translate(cr, x, y);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
174 cairo_scale(cr, scale, scale);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
175 } else {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
176 double x = (dwidth - dpixwidth) / 2;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
177 double y = (dheight - dpixheight) / 2;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
178 if(x < 0) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
179 x = 0;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
180 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
181 if(y < 0) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
182 y = 0;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
183 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
184 cairo_translate(cr, (int)x, (int)y);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
185 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
186
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
187 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
188 cairo_paint(cr);
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 void* ui_imageviewer_get(UiGeneric *g) {
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
192 UiImageViewer *imgviewer = g->obj;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
193 g->value = imgviewer->pixbuf;
340
2dd42bd4fe5d add imageviewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 339
diff changeset
194 return g->value;
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 const char* ui_imageviewer_get_type(UiGeneric *g) {
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
198 return UI_IMAGE_OBJECT_TYPE;
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 return 1;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 GdkPixbuf *pixbuf = value;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
208 UiImageViewer *imgviewer = g->obj;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
209 g->value = pixbuf;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
210
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
211 if(imgviewer->pixbuf) {
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
212 g_object_unref(imgviewer->pixbuf);
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
213 }
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
214 imgviewer->pixbuf = pixbuf;
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
215
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
216 if(imgviewer->adjustsize) {
381
b47bda6666ce fix gtk3 image loading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 340
diff changeset
217 int width = gdk_pixbuf_get_width(pixbuf);
b47bda6666ce fix gtk3 image loading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 340
diff changeset
218 int height = gdk_pixbuf_get_height(pixbuf);
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
219 gtk_widget_set_size_request(imgviewer->widget, width, height);
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 }
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
221 gtk_widget_queue_draw(imgviewer->widget);
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 return 0;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 int ui_image_load_file(UiGeneric *obj, const char *path) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 GError *error = NULL;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 if(!pixbuf) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 return 1;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 if(obj->set) {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE);
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 } else {
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 obj->value = pixbuf;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
529
0a4a6b0d1c82 add new imageviewer implementation (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 390
diff changeset
240
339
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 return 0;
b68b5f984074 add minimally working image viewer (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }

mercurial