Leber
Leber

Reputation: 149

Resizing GTK Widgets into vbox in C

Well, I'm having a problem with a small interface that i'm doing in GTK.

I have some widgets into a box, exactly three widgets, and I put them into a vbox with gtk_box_pack_start. I resized these widgets with gtk_widget_set_size_request function, but seems don't take effect because widgets are expanded to the total width of the vbox, and don't respect the size that I fixed in a function.

The piece of code that make this is:

enter = gtk_button_new();
gtk_button_set_image(GTK_BUTTON(enter), gtk_image_new_from_file("iconos/submit.png"));
gtk_widget_set_size_request(enter, 195, 32);

//User,pass
login->user = gtk_entry_new();
login->passwd = gtk_entry_new();
gtk_widget_set_size_request(login->user, 195, 32);
gtk_widget_set_size_request(login->passwd, 195, 32);

gtk_box_pack_start(GTK_BOX(v_box), login->user, FALSE, FALSE, 10);
gtk_box_pack_start(GTK_BOX(v_box), login->passwd, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(v_box), enter, FALSE, FALSE, 10);

gtk_container_add(GTK_CONTAINER(window), v_box);

If I put these widgets into an alignment, I have no problem, the size is fixed right.

Why is this?

Upvotes: 4

Views: 4720

Answers (1)

ptomato
ptomato

Reputation: 57854

What do you want to achieve by setting the size? As the documentation for gtk_widget_set_size_request() says,

Note the inherent danger of setting any fixed size - themes, translations into other languages, different fonts, and user action can all change the appropriate size for a given widget. So, it's basically impossible to hardcode a size that will always be correct.

You can set the size request of a widget, but that's not the same as setting the size; we can't always get what we want, and so in certain circumstances the widget doesn't get the size it requests. This is one of those cases.

GTK is designed so that you shouldn't really have to set fixed sizes for widgets. (See the above quote from the documentation.) There's usually a better way to achieve what you want.

In this case, put your button in a GtkHButtonBox with gtk_container_add, and pack the button box into your vertical box, with the expand and fill parameters set to FALSE, as you were doing before.

Upvotes: 3

Related Questions