Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:15:16+00:00 2026-06-17T09:15:16+00:00

I have been able to do the following: for (int i = 0; i

  • 0

I have been able to do the following:

  for (int i = 0; i < NUM_LEDS; ++i) {
    ledoff = gtk_image_new_from_file("./ledoff.png");
      leds[i].pos=ledpos[i];
      gtk_layout_put(GTK_LAYOUT(layout), ledoff, leds[i].pos.x, leds[i].pos.y);
      leds[i].status=OFF;
  }

Basically, this loads a bunch of "ledoff" images onto some window.

What I need is to change the image ledoff to ledon every time I click on leds[i].pos.x, leds[i].pos.y. At the beginning I thought it was just a matter of loading a new image and replacing the previous one, but then since this will be done thousands of times, I thought I was "malloc’ing" one new file everytime I do gtk_image_new_from_file! Is this true? Or am I just replacing the file and not adding a new one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:15:17+00:00Added an answer on June 17, 2026 at 9:15 am

    Here is a working example that creates a 50×50 “LED” array in a window and allows you to toggle their status by clicking on them. It’s not really that efficient, and as I pointed out in comments you’re better of drawing the images yourself right onto the GtkLayout, but this will at least serve as a proof of concept.

    Edit: I’ve updated the sample code to take into account liberforce’s suggestions, which make things cleaner and more memory efficient.

    #include <gtk/gtk.h>
    
    #define ICON_WIDTH 16
    #define ICON_HEIGHT 16
    #define NUM_LEDS 2500
    
    typedef enum {
        ON,
        OFF
    } led_status;
    
    typedef struct {
        GtkWidget *img;
        struct {
            gint x;
            gint y;
        } pos;
        led_status status;
    } led;
    
    static led leds[NUM_LEDS];
    static GdkPixbuf *led_on;
    static GdkPixbuf *led_off;
    
    static gboolean click_handler(GtkWidget *widget,
                                  GdkEvent *event,
                                  gpointer user_data)
    {
        led *info = user_data;
    
        if (info->status == ON) {
            gtk_image_set_from_pixbuf(GTK_IMAGE(info->img), led_off);
            info->status = OFF;
        } else {
            gtk_image_set_from_pixbuf(GTK_IMAGE(info->img), led_on);
            info->status = ON;
        }
    
        return TRUE;
    }
    
    int main(int argc, char** argv)
    {
        GtkWidget *window, *layout;
        int i = 0, x, y;
    
        gtk_init(&argc, &argv);
    
        /* Load our images (ignoring errors - as any good sample code would) */
        led_on  = gdk_pixbuf_new_from_file("led-on.png", NULL);
        led_off = gdk_pixbuf_new_from_file("led-off.png", NULL);
    
        /* Initialize our array */
        for (x = 0; x < 50; x++) {
            for (y = 0; y < 50; y++) {
                leds[i].img = gtk_image_new();
                leds[i].pos.x = x * ICON_WIDTH;
                leds[i].pos.y = y * ICON_HEIGHT;
                leds[i].status = OFF;
    
                /* Initialize our image from the pixbuf we've already loaded */
                gtk_image_set_from_pixbuf(GTK_IMAGE(leds[i].img), led_off);
                i++;
            }
        }
    
        /* Create a window */
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(window), "LEDs");
        gtk_signal_connect(GTK_OBJECT(window),
                           "destroy",
                           G_CALLBACK(gtk_main_quit),
                           NULL);
    
        /* Create the widget */
        layout = gtk_layout_new(NULL, NULL);
    
        for (i = 0; i < NUM_LEDS; i++) {
            /*
             * A GtkImage doesn't have a window, so we need to put it inside
             * a GtkEventBox so we can capture events.
             */
            GtkWidget *eb = gtk_event_box_new();
            g_signal_connect(G_OBJECT(eb),
                             "button_press_event",
                             G_CALLBACK(click_handler),
                             &leds[i]);
            gtk_container_add(GTK_CONTAINER(eb), leds[i].img);
            gtk_layout_put(GTK_LAYOUT(layout), eb, leds[i].pos.x, leds[i].pos.y);
        }
    
        gtk_container_add(GTK_CONTAINER(window), layout);
        gtk_widget_show_all(window);
    
        gtk_main();
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following the standard instruction for using AES algorithm, I have not been able to
I have been able to run my C# .net 3.5 app just fine on
I have been able to make my photos large for single photo posts on
I have been able to find lots of examples of adding a Dropdown list
I have been able to design my layout so that it has 2 columns
I have been able to set up OBDC in Filemaker, and added table ,
I have been trying different things and still have been able to get exactly
Hi I have been able to extract a VARCHAR to a date using string_to_date
What I have been able to grasp from reading the source and documentation from
I have this excel file which I have been able to write the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.