aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--main.c99
-rw-r--r--makefile34
3 files changed, 136 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..992e5e8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.clang_complete
+qwe
+*.o
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..211de8b
--- /dev/null
+++ b/main.c
@@ -0,0 +1,99 @@
+#include <gtk-3.0/gtk/gtk.h>
+
+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;
+}
+
+void configure_callback(GtkWindow *window, GdkEvent *event, gpointer data)
+{
+ 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;
+ }
+
+ gtk_container_remove(GTK_CONTAINER(window), GTK_WIDGET(image));
+ 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);
+ image = gtk_image_new_from_pixbuf(GDK_PIXBUF(curr_pixbuf));
+ gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(image));
+ gtk_widget_show_all(GTK_WIDGET(window));
+}
+
+static void activate(GtkApplication *app, gpointer user_data)
+{
+ GError *error = NULL;
+
+ window = gtk_application_window_new(app);
+ 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_window_set_title(GTK_WINDOW(window), "qwe");
+ gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
+ 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;
+
+ gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(image));
+ 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;
+}
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..f50f036
--- /dev/null
+++ b/makefile
@@ -0,0 +1,34 @@
+RM=rm -f
+INSTALL=install
+
+CFLAGS=-o2 -pipe -Wall -std=c11 $(shell pkg-config --cflags gtk+-3.0)
+LDFLAGS=$(shell pkg-config --libs gtk+-3.0)
+# GLIB_COMPILE_RESOURCES=$(shell pkg-config --variable=glib_compile_resources gio-2.0)
+
+PREFIX=/usr
+BINDIR=$(PREFIX)/bin
+OBJECTS=main.o
+# RESOURCES=resources.c
+
+all: qwe
+
+qwe: $(OBJECTS)
+ $(CC) -o qwe $(OBJECTS) $(LDFLAGS)
+
+.c.o:
+ $(CC) -c $(CFLAGS) -o $@ $<
+
+# resources.c: qwe.gresource.xml window.ui
+# $(GLIB_COMPILE_RESOURCES) qwe.gresource.xml --target=$(RESOURCES) --sourcedir=. --generate-source
+
+clean:
+ $(RM) $(OBJECTS) $(RESOURCES) qwe
+
+install: qwe
+ $(INSTALL) -m0755 qwe $(BINDIR)
+
+uninstall:
+ $(RM) $(BINDIR)/qwe
+
+run: qwe
+ ./qwe