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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#include <gtk-3.0/gtk/gtk.h>
#define TIMEOUT 20
#define ZOOM_FACTOR 0.1
GtkWidget *window;
GtkWidget *image;
GdkPixbuf *pixbuf;
GdkPixbuf *curr_pixbuf;
int pixbuf_width, pixbuf_height;
double aspect_ratio;
double curr_zoom = 1.0;
void zoom(int type)
{
if (type == 0) {
if (curr_zoom == 1.0)
return;
curr_zoom = (double)1.0;
} else if (type < 0) {
if (curr_zoom < 0.2)
return;
curr_zoom -= (double)ZOOM_FACTOR;
} else if (type > 0) {
if (curr_zoom > 2)
return;
curr_zoom += (double)ZOOM_FACTOR;
}
if (curr_pixbuf != NULL)
g_object_unref(curr_pixbuf);
curr_pixbuf = gdk_pixbuf_scale_simple(
GDK_PIXBUF(pixbuf), pixbuf_width * curr_zoom,
pixbuf_height * curr_zoom, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf));
}
static gboolean key_press(GtkWindow *window, GdkEvent *event, gpointer data)
{
switch (event->key.keyval) {
case GDK_KEY_q:
g_application_quit(G_APPLICATION(
gtk_window_get_application(GTK_WINDOW(window))));
return FALSE;
case GDK_KEY_plus:
case GDK_KEY_ScrollUp:
zoom(1);
return FALSE;
case GDK_KEY_minus:
case GDK_KEY_ScrollDown:
zoom(-1);
return FALSE;
case GDK_KEY_equal:
case GDK_KEY_ScrollClick:
zoom(0);
return FALSE;
default:
return TRUE;
}
}
gboolean scroll_callback(GtkWindow *window, GdkEvent *event, gpointer data)
{
GdkModifierType state;
gdk_event_get_state(event, &state);
if (state & GDK_CONTROL_MASK &&
event->scroll.direction == GDK_SCROLL_UP) {
zoom(1);
return FALSE;
} else if (state & GDK_CONTROL_MASK &&
event->scroll.direction == GDK_SCROLL_DOWN) {
zoom(-1);
return FALSE;
}
return TRUE;
}
gboolean resize_done(gpointer data)
{
guint *id = data;
*id = 0;
gint win_width, win_height;
gint new_pixbuf_width, new_pixbuf_height;
gtk_window_get_size(GTK_WINDOW(window), &win_width, &win_height);
if (win_width < pixbuf_width && win_height > pixbuf_height) {
new_pixbuf_width = win_width;
new_pixbuf_height = (double)new_pixbuf_width / aspect_ratio;
} else if (win_width > pixbuf_width && win_height < pixbuf_height) {
new_pixbuf_height = win_height;
new_pixbuf_width = (double)new_pixbuf_height * aspect_ratio;
} else if (win_width < pixbuf_width && win_height < pixbuf_height) {
if (((double)win_width / (double)win_height) > aspect_ratio) {
new_pixbuf_height = win_height;
new_pixbuf_width =
((double)new_pixbuf_height * aspect_ratio);
} else {
new_pixbuf_width = win_width;
new_pixbuf_height =
(double)new_pixbuf_width / aspect_ratio;
}
} else {
new_pixbuf_width = pixbuf_width;
new_pixbuf_height = pixbuf_height;
}
if (curr_pixbuf != NULL)
g_object_unref(curr_pixbuf);
curr_pixbuf =
gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), new_pixbuf_width,
new_pixbuf_height, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf));
return FALSE;
}
gboolean configure_callback(GtkWindow *window, GdkEvent *event, gpointer data)
{
static guint id = 0;
if (id)
g_source_remove(id);
id = g_timeout_add(TIMEOUT, resize_done, &id);
return FALSE;
}
static void activate(GtkApplication *app, gpointer user_data)
{
GError *error = NULL;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "qwe");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
/* g_signal_connect(G_OBJECT(window), "configure-event", */
/* G_CALLBACK(configure_callback), NULL); */
g_signal_connect(G_OBJECT(window), "key-press-event",
G_CALLBACK(key_press), NULL);
gtk_widget_add_events(GTK_WIDGET(window), GDK_SCROLL_MASK);
g_signal_connect(G_OBJECT(window), "scroll-event",
G_CALLBACK(scroll_callback), NULL);
pixbuf = gdk_pixbuf_new_from_file(
"/home/nirav/Downloads/Saved Pictures/bash help shortcuts.png",
&error);
if (error) {
g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n",
error->message);
g_clear_error(&error);
return;
}
image = gtk_image_new_from_pixbuf(GDK_PIXBUF(pixbuf));
pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf));
pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf));
aspect_ratio = (double)pixbuf_width / (double)pixbuf_height;
GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(image));
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(scrolled_window));
gtk_widget_show_all(GTK_WIDGET(window));
}
int main(int argc, char *argv[])
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.qwe", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
|