aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 915d843d01a29af28b364c1f8ed5c307fca50931 (plain)
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
#include <gtk-3.0/gtk/gtk.h>
#define TIMEOUT 20

GtkWidget *window;
GtkWidget *image;
GdkPixbuf *pixbuf;
GdkPixbuf *curr_pixbuf;
int pixbuf_width, pixbuf_height;
double aspect_ratio;

static gboolean key_press(GtkWindow *window, gpointer data)
{
	GdkEventKey *event = (GdkEventKey *)data;
	if (event->keyval == GDK_KEY_q) {
		g_application_quit(G_APPLICATION(
		    gtk_window_get_application(GTK_WINDOW(window))));
		return FALSE;
	} else
		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);

	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;
}