LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gtk_grid_attach_next_to issue [solved] (https://www.linuxquestions.org/questions/programming-9/gtk_grid_attach_next_to-issue-%5Bsolved%5D-4175690538/)

billc3012 02-15-2021 03:39 AM

gtk_grid_attach_next_to issue [solved]
 
I setup a grid with 4 buttons.
Clicking a button should add a message to the grid below the button.
If I click any button, the window resizes to reflect the size of the message.
However buttons 1-3 show no message.
If I click the 4th button the message is displayed correctly.
If I only have 1 button then everything works correctly.

This is fixed by calling gtk_grid_insert_next_to prior to gtk_grid_attach_next_to

Code:

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

#define GWD GtkWidget

typedef struct GWD3{
        GWD *grid;
        GWD *entry;
}GWD3;

GWD *win;


static GWD3 *bldEntry(char *prompt){
        GWD *grid,*lbl,*entry;
        grid=gtk_grid_new();
        lbl=gtk_label_new(prompt);
        entry=gtk_entry_new();
        gtk_entry_set_text(GTK_ENTRY(entry),"1.00 mm");
        gtk_entry_set_alignment(GTK_ENTRY(entry),2);
        gtk_entry_set_max_length(GTK_ENTRY(entry),11);
        gtk_grid_attach(GTK_GRID(grid),lbl,1,0,1,1);
        gtk_grid_attach(GTK_GRID(grid),entry,2,0,1,1);
        GWD3 *ret=g_malloc(sizeof(GWD2));
        ret->grid=grid; ret->entry=entry;
        return ret; }

static gboolean addData(GWD *w, gpointer data){
        GWD *grid=(GWD *)data;
        GWD3 *pair=bldEntry("ENTER Length  ");
        gtk_grid_attach_next_to(GTK_GRID(grid),pair->grid,w,GTK_POS_BOTTOM,2,1);
        gtk_widget_grab_focus(pair->entry);
        gtk_widget_show_all(win);
        return false;}

static GWD *addGrid(){
        GWD *grid=gtk_grid_new();
        GWD *lbl=gtk_label_new("Test Grid");
        gtk_grid_attach(GTK_GRID(grid),lbl,1,0,1,1);
        GWD *btn=gtk_button_new_with_label("Button 2");
        g_signal_connect(btn,"clicked",G_CALLBACK(addData),(gpointer)grid);
        gtk_grid_attach(GTK_GRID(grid),btn,1,1,1,1);
        btn=gtk_button_new_with_label("Button 3");
        g_signal_connect(btn,"clicked",G_CALLBACK(addData),(gpointer)grid);
        gtk_grid_attach(GTK_GRID(grid),btn,1,2,1,1);
        btn=gtk_button_new_with_label("Button 4");
        g_signal_connect(btn,"clicked",G_CALLBACK(addData),(gpointer)grid);
        gtk_grid_attach(GTK_GRID(grid),btn,1,3,1,1);
        btn=gtk_button_new_with_label("Button 5");
        g_signal_connect(btn,"clicked",G_CALLBACK(addData),(gpointer)grid);
        gtk_grid_attach(GTK_GRID(grid),btn,1,4,1,1);
        return grid;}

void btn_clicked(GtkWidget *w, gpointer data){
                g_print("Button clicked\n");}

int main(int argc,char *argv[]){
                gtk_init(&argc,&argv);
                win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
                g_signal_connect(win,"delete_event",gtk_main_quit,NULL);
                GWD *grid=addGrid();
                gtk_container_add(GTK_CONTAINER(win),grid);
                gtk_widget_show_all(win);
                gtk_main();
                return 1;}

Any suggestions welcome.

Thanks in advance
Bill

astrogeek 02-15-2021 12:34 PM

Welcome to LQ and the Programming forum!

Your post is not clear to me and you seem to ask no specific question. From your description it seems that everything is working correctly. Could you please edit your post to be specific about the problem you are having, if any, and how the observed behavior differs from what you expect. And please tell us what you have done to try to identify the cause.

You may find it helpful to review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!

billc3012 02-15-2021 05:16 PM

It is necessary to call gtk_grid_insert_next_to before calling gtk_grid attach_next_to. If this is not done and the following row has content then gtk documentation says the results are undefined. So gtk works as per documentation.


All times are GMT -5. The time now is 11:26 PM.