user11107069
user11107069

Reputation:

Creating and storing images with gtk2?

I am having some trouble drawing images with gtk2. I have tried this code:

#include <gtk/gtk.h>

static gboolean button_press_callback (GtkWidget *event_box, GdkEventButton *event, gpointer data)
{
  g_print ("Event box clicked at coordinates %f,%f\n",
           event->x, event->y);

  /* Returning TRUE means we handled the event, so the signal
   * emission should be stopped (don't call any further
   * callbacks that may be connected). Return FALSE
   * to continue invoking callbacks.
   */
  return TRUE;
}

static GtkWidget*
create_image (void)
{
  GtkWidget *image;
  GtkWidget *event_box;

  image = gtk_image_new_from_file ("image.png");
}

int main(int argc, char const *argv[])
{
  create_image();
  return 0;
}

It will not draw any images onscreen, infact I don`t see any window at all. Also, what is the best way to store an image in a variable for future use?

Upvotes: 0

Views: 302

Answers (1)

user1531591
user1531591

Reputation:

I suggest you to look at the gtk tutorial https://developer.gnome.org/gtk-tutorial/stable/, a lot of things are missing for your code to display here a sample on how to display a simple picture in a window :

#include <gtk/gtk.h>

GtkWidget* create_gui()
{
    GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL); // create the application window
    GtkWidget *img = gtk_image_new_from_file("image.png"); // image shall be in the same dir 
    gtk_container_add(GTK_CONTAINER(win), img); // add the image to the window
    g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(gtk_main_quit), NULL); // end the application if user close the window
    return win;
}
int main(int argc, char** argv) {
    GtkWidget* win;

    gtk_init(&argc, &argv);


    win = create_gui();
    gtk_widget_show_all(win); // display the window
    gtk_main(); // start the event loop
    return 0;
}

BTW, gtk 2 is no longer being maintained, I suggest you start with gtk3 if you can

Upvotes: 1

Related Questions