HTMLify

gui.c
Views: 3 | Author: abh
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "image.h"
#include "filter.h"
#include "gui.h"

extern char *image_path_1;
extern char *image_path_2;
extern char *output_path;
extern char *filter_name;
extern Image *image1, *image2;
extern Filter filter;

Image *gui_filtered_image;

void activate(GtkApplication *, gpointer);

void on_filter_apply(GtkButton *, gpointer);

void on_save(GtkButton *, gpointer);

int gui() {
    GtkApplication *app = gtk_application_new("kk.sir.for.image-filter", 0);
    int status;
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), 0, NULL);
    g_object_unref(app);
    return status;
}

void activate(GtkApplication *app, gpointer null_value) {
    if (image2 == NULL)
        image2 = image_new(0, 0);

    GtkWidget
        *image1_window = gtk_application_window_new(app),
        *image2_window = gtk_application_window_new(app),
        *filter_window = gtk_application_window_new(app),
        *output_window = gtk_application_window_new(app),

        *filter_buttons_grid = gtk_grid_new(),
        *output_window_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2),

        *image1_preview = gtk_image_new_from_pixbuf(image_to_pixbuf(image1)),
        *image2_preview = gtk_image_new_from_pixbuf(image_to_pixbuf(image2)),
        *output_preview = gtk_image_new(),

        *save_button = gtk_button_new_with_label("Save"); 
    ;


    // image 1 window
    gtk_window_set_title(GTK_WINDOW(image1_window), "Image 1");
    gtk_image_set_from_pixbuf(GTK_IMAGE(image1_preview), image_to_pixbuf(image1));
    gtk_window_set_child(GTK_WINDOW(image1_window), image1_preview);
    gtk_window_set_default_size(GTK_WINDOW(image1_window), 200, 200);
    
    // image 2 window
    gtk_window_set_title(GTK_WINDOW(image2_window), "Image 2");
    gtk_image_set_from_pixbuf(GTK_IMAGE(image2_preview), image_to_pixbuf(image2));
    gtk_window_set_child(GTK_WINDOW(image2_window), image2_preview);
    gtk_window_set_default_size(GTK_WINDOW(image2_window), 200, 200);

    // filter window
    GtkWidget *filter_buttons[filter_count];
    for (int i=0; i<filter_count; i++) {
        GtkWidget *button = gtk_button_new_with_label(filters[i].name);
        gtk_widget_set_name(button, filters[i].name);
        g_signal_connect(button, "clicked", G_CALLBACK(on_filter_apply), output_preview);
        int row = i / 5;
        int col = i % 5;
        gtk_grid_attach(GTK_GRID(filter_buttons_grid), button, row, col, 1, 1);
    }
    gtk_window_set_child(GTK_WINDOW(filter_window), filter_buttons_grid);
    gtk_window_set_default_size(GTK_WINDOW(filter_window), 200, 200);

    // output window
    gtk_window_set_title(GTK_WINDOW(output_window), "Output Preview");
    gtk_widget_set_vexpand(output_preview, true);
    gtk_widget_set_hexpand(output_preview, true);
    g_signal_connect(save_button, "clicked", G_CALLBACK(on_save), output_preview);
    gtk_box_append(GTK_BOX(output_window_box), save_button);
    gtk_box_append(GTK_BOX(output_window_box), output_preview);
    gtk_window_set_child(GTK_WINDOW(output_window), output_window_box);
    gtk_window_set_default_size(GTK_WINDOW(output_window), 200, 200);

    gtk_window_present(GTK_WINDOW(image1_window));
    gtk_window_present(GTK_WINDOW(image2_window));
    gtk_window_present(GTK_WINDOW(filter_window));
    gtk_window_present(GTK_WINDOW(output_window));
}

void on_filter_apply(GtkButton *button, gpointer _output_preview) {
    GtkImage *output_preview = GTK_IMAGE(_output_preview);
    Filter *filter = filter_by_name(gtk_widget_get_name(GTK_WIDGET(button)));
    g_print("applying filter \n");
    if (filter->argc == 1) {
        gui_filtered_image = filter->arg_1(image1);
    } else {
        gui_filtered_image = filter->arg_2(image1, image2);
    }
    gtk_image_set_from_pixbuf(output_preview, image_to_pixbuf(gui_filtered_image));
}

void on_save(GtkButton *button, gpointer null) {
    bool saved = save_image(gui_filtered_image, output_path);
    if (saved) {
        g_print("Image saved\n");
    } else {
        g_print("Image not saved..\n");
    }
}

Comments