How to refresh, redraw a window (widget) in gtk?

Ravindra Wagh picture Ravindra Wagh · Aug 31, 2015 · Viewed 8.3k times · Source

I am using Opensuse 13.1 Linux Os. I am new to gtk2 and c. I am trying to create a application that can place a button on a table which is attached as per the values typed by the user. My program code is as following

#include <stdlib.h>
#include <gtk/gtk.h>

typedef struct {
    GtkWidget *value1, *value2, *value3, *value4;
} entrygrouped;

guint ival1, ival2, ival3, ival4;

void button_clicked(entrygrouped *widget)
{
    const gchar *value1, *value2, *value3, *value4;

    value1 = gtk_entry_get_text (GTK_ENTRY(widget->value1));
    value2 = gtk_entry_get_text (GTK_ENTRY(widget->value2));
    value3 = gtk_entry_get_text (GTK_ENTRY(widget->value3));
    value4 = gtk_entry_get_text (GTK_ENTRY(widget->value4));

    ival1 = (guint)atoi(value1);
    ival2 = (guint)atoi(value2);
    ival3 = (guint)atoi(value3);
    ival4 = (guint)atoi(value4);

    g_print("ENTRY VALUES = %s %s %s %s\n", value1, value2, value3, value4);
    g_print("ENTRY NUMS = %d %d %d %d\n", ival1, ival2, ival3, ival4);
}

int main (int argc, char *args[])
{
    GtkWidget *window, *vbox, *uptable, *downtable, *label;
    GtkWidget  *button, *button2;
    gtk_init(&argc, &args);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_get_resizable(GTK_WINDOW(window));

    entrygrouped *eg;
    eg = g_slice_new(entrygrouped);

    vbox = gtk_vbox_new(FALSE, 2);
    uptable = gtk_table_new (3, 4, FALSE);
    downtable = gtk_table_new (3, 3, TRUE);

    label = gtk_label_new (" Enter the values to position the widget ");
    eg->value1 = gtk_entry_new();
    eg->value2 = gtk_entry_new();
    eg->value3 = gtk_entry_new();
    eg->value4 = gtk_entry_new();
    button = gtk_button_new_with_label ("Submit");
    button2 = gtk_button_new_with_label("BUTTON");

    gtk_table_attach_defaults (GTK_TABLE(uptable), label, 0, 3, 0, 1);
    gtk_table_attach_defaults (GTK_TABLE(uptable), eg->value1, 0, 1, 1, 2);
    gtk_table_attach_defaults (GTK_TABLE(uptable), eg->value2, 1, 2, 1, 2);
    gtk_table_attach_defaults (GTK_TABLE(uptable), eg->value3, 0, 1, 2, 3);
    gtk_table_attach_defaults (GTK_TABLE(uptable), eg->value4, 1, 2, 2, 3);
    gtk_table_attach_defaults (GTK_TABLE(uptable), button, 1, 2, 3, 4);

    gtk_widget_queue_draw(GTK_WIDGET(window));

    gtk_table_attach_defaults (GTK_TABLE(downtable), button2, ival1, ival2,
                                                                ival3, ival4);

    gtk_box_pack_start(GTK_BOX(vbox), uptable, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), downtable, 0, 0, 0);


    g_signal_connect_swapped(G_OBJECT(button), "clicked",
                                G_CALLBACK(button_clicked), eg);

    g_signal_connect_swapped(G_OBJECT(window), "destroy",
                                G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add (GTK_CONTAINER(window), vbox);
    gtk_widget_show_all(window);

    gtk_main();
    return 0;
}

Now whenever i open my application from terminal and enter some values and hit submit button, values are printed on terminal, but the problem is window did not draw the button2 with newly assigned values. I dont know any function which can refresh or redraw the whole window, though i tried the

while (gtk_events_pending())
  gtk_main_iteration();

above the button2 table attachment still i window do nothing. Do i need another callback function to resize window? Please help me with this problem.

EDIT : If my question is unclear i want to show it graphical way as follows

 ----------------------------
| a.out             _ [ ] x  |
 ----------------------------               before entering the 
| -------------------------- |              values 
||            |             ||
| -------------------------- |
||            |             ||
| -------------------------- |
|                   ________ |
|                  | Submit ||
|                   -------- |
 ----------------------------

 ----------------------------
| a.out             _ [ ] x  |
 ----------------------------               after entering the 
| -------------------------- |              values 
||           0 |          1 ||               
| -------------------------- |              button2 widget should be 
||           0 |          1 ||              redrawn according to the
| -------------------------- |              values entered and window
|                   ________ |              widget should be updated 
|                  | Submit ||
|                   -------- | 
|   _________                |
|  |  Button |               |
|   ---------                |
 ----------------------------

Answer

Basile Starynkevitch picture Basile Starynkevitch · Aug 31, 2015

You never changed the label of the button2.

You should have some callback calling gtk_button_set_label on button2 ; if you want to resize the widget you may need to send the size-request signal and/or size-allocate and/or the check-resize signal on the container. See also this thread.

BTW, you really should use GTK3 in new code (since GTK3 improved a lot on widget layout w.r.t. GTK2). And you'll better first code something simpler, to understand the event loop of GTK. You'll need to use the gdb debugger (and valgrind should be useful too).

Don't forget to enable all warnings & debug info when compiling (with e.g. gcc -Wall -Wextra -Wstring-prototypes -g)

As you guessed, you need much more callbacks (and signals and slots) functions. Understanding what continuations are and what CPS is should be useful (because callbacks are related to continuations).