[GTK] [C] Attach to grid dynamically

Hello all,

This is probably a basic question about GTK containers.

I typed out this hierarchy to demonstrate my current interface layout. Upon initial execution of the program, there is only a single textView field within grid. However, as the user inputs a string and presses enter, I would like to populate a new textView right under the previous one. I would like this to occur as many times as the user is willing to input a string and press enter.

layout:

[window]
[Vbox]
[Menubar][/Menubar]
[Scrolled_window]
[grid]
[textView1][/textView1]
[textView2][/textView2]
.
.
.
[textViewN][/textViewN]
[/grid]
[/scrolled_window]
[/Vbox]
[/window]

My question is this possible? It seems like I can only do it upon the initial creation of the window, but not after. Do I have to destroy and recreate the window each time to create the effect I’m looking for?

for example, I have this callback for when a user presses enter on a textview:

gboolean on_key_press (GtkWidget *widget, GdkEventKey* pKey, gpointer userdata){
    if (pKey->type == GDK_KEY_PRESS){
        g_print("%i\n", pKey->keyval);

        switch (pKey->keyval){
            case GDK_KEY_Return:
                interpretRequest(&widget);
                addUserPromptEntry(linecount); //this function will attach a new textview to the grid
                break;
            case 65362: cycleBuffer(&widget); //up arrow

            break;
        }
    }
    return FALSE;
}
void addUserPromptEntry(int x){
    linecount++;
    GtkWidget *text_view;
    text_view = gtk_text_view_new();
    g_signal_connect(text_view,"key_press_event", G_CALLBACK(on_key_press), NULL);
    gtk_grid_attach(GTK_GRID(master_grid),text_view,0,x,10,1);

}

Yes, I think it should work. Maybe for first tests, you should use a widget that really occupies area, like a GtkLabel. For an empty GtkTextView, in a GtkScrolledWindow, I am not sure how the visual efect would look. Personally, I would start with a tiny example, maybe a button, which for each click adds a new GtkLabel to a new GtkGrid cell.

I figured it out. What I was missing was the call to gtk_show_all(master_grid) to refresh the updates

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.