open and draw image with path
This commit is contained in:
45
main.c
45
main.c
@@ -4,8 +4,49 @@
|
||||
typedef struct{
|
||||
GtkApplication *app;
|
||||
GtkWidget *window;
|
||||
GtkWidget *box;
|
||||
GtkWidget *input;
|
||||
char dir[256];
|
||||
} Appdata;
|
||||
|
||||
static void load_image(Appdata *data){
|
||||
GError *error = NULL;
|
||||
GdkPixbuf *pix = gdk_pixbuf_new_from_file (("%s", data->dir), &error);
|
||||
if (pix == NULL) {
|
||||
g_printerr ("Error loading file: #%d %s\n", error->code, error->message);
|
||||
g_error_free (error);
|
||||
exit (1);
|
||||
}
|
||||
GtkWidget *widget = gtk_image_new_from_pixbuf (pix);
|
||||
gtk_box_append(GTK_BOX(data->box),widget);
|
||||
}
|
||||
|
||||
// activates after enter dir in input
|
||||
void on_entry_activate(GtkEntry *entry, Appdata *data){
|
||||
const gchar *user_text;
|
||||
user_text = gtk_editable_get_text(GTK_EDITABLE(entry));
|
||||
g_print("input: %s\n", user_text);
|
||||
// check empty input
|
||||
if (strcmp(user_text, "") == 0 || strcmp(user_text, " ") == 0){
|
||||
snprintf(data->dir, sizeof(data->dir), "/");
|
||||
gtk_editable_set_text(GTK_EDITABLE(data->input), "/");
|
||||
}
|
||||
else{
|
||||
snprintf(data->dir, sizeof(data->dir), "%s", user_text);
|
||||
gtk_editable_set_text(GTK_EDITABLE(data->input), ("%s", data->dir));
|
||||
}
|
||||
load_image(data);
|
||||
}
|
||||
|
||||
// make input line
|
||||
static void input_dir(Appdata *data){
|
||||
data->input = gtk_entry_new();
|
||||
gtk_editable_set_text(GTK_EDITABLE(data->input), ("%s", data->dir));
|
||||
g_signal_connect(data->input, "activate", G_CALLBACK(on_entry_activate), data);
|
||||
g_print("Set input line\n");
|
||||
gtk_box_append (GTK_BOX (data->box), data->input);
|
||||
}
|
||||
|
||||
static void app_activate (GtkApplication *app, gpointer *user_data){
|
||||
Appdata *data = (Appdata *)user_data;
|
||||
data->window = gtk_application_window_new (GTK_APPLICATION(app));
|
||||
@@ -13,6 +54,10 @@ static void app_activate (GtkApplication *app, gpointer *user_data){
|
||||
gtk_window_set_default_size (GTK_WINDOW (data->window), 600, 400);
|
||||
gtk_window_set_application(GTK_WINDOW(data->window),app);
|
||||
gtk_window_present(GTK_WINDOW(data->window));
|
||||
data->box = gtk_box_new(GTK_ORIENTATION_VERTICAL,10);
|
||||
gtk_widget_set_valign(data->box,GTK_ALIGN_START);
|
||||
gtk_window_set_child(GTK_WINDOW(data->window), data->box);
|
||||
input_dir(data);
|
||||
printf("Present window %d\n", data->window);
|
||||
g_print("Started\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user