Set entry size in a grid



How do you keep the entries in a grid a set size? In GTK3.10 I could set the alignment property to center and the hexpand to false. Then the entry wouldn't expand in the grid. The same doesn't work in GTK3.18. I thought maybe it has something to do with CSS overriding properties but I don't know. Tried tweaking those also. The theme that I have is the Ubuntu default ambiance.

In the following test code I get all four entries expanded to the same size in the grid. Has anyone else run into this and have a solution?

Thanks,
Eric

/*
    gcc -Wall entry_grid1.c -o entry_grid1 `pkg-config gtk+-3.0 --cflags --libs`
    Tested on Ubuntu16.04, GTK3.18.
*/

#include<gtk/gtk.h>

int main(int argc, char *argv[])
{
  gtk_init(&argc, &argv);

  GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Entries in Grid");
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);

  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  GtkWidget *entry1=gtk_entry_new();
  gtk_widget_set_hexpand(entry1, FALSE);
  gtk_widget_set_vexpand(entry1, FALSE);
  gtk_widget_set_halign(entry1, GTK_ALIGN_CENTER);
  gtk_entry_set_width_chars(GTK_ENTRY(entry1), 3);

  GtkWidget *entry2=gtk_entry_new();
  gtk_widget_set_hexpand(entry2, FALSE);
  gtk_widget_set_vexpand(entry2, FALSE);
  gtk_entry_set_width_chars(GTK_ENTRY(entry2), 3);

  GtkWidget *entry3=gtk_entry_new();
  gtk_widget_set_hexpand(entry3, TRUE);
  gtk_widget_set_vexpand(entry3, TRUE);

  GtkWidget *entry4=gtk_entry_new();
  gtk_widget_set_hexpand(entry4, TRUE);
  gtk_widget_set_vexpand(entry4, TRUE);
 
  GtkWidget *grid=gtk_grid_new();
  gtk_grid_set_row_homogeneous(GTK_GRID(grid), FALSE);
  gtk_grid_set_column_homogeneous(GTK_GRID(grid), FALSE);
  gtk_grid_attach(GTK_GRID(grid), entry1, 0, 0, 1, 1);
  gtk_grid_attach(GTK_GRID(grid), entry2, 0, 1, 1, 1);
  gtk_grid_attach(GTK_GRID(grid), entry3, 1, 0, 1, 1);
  gtk_grid_attach(GTK_GRID(grid), entry4, 1, 1, 1, 1);

  //Try to change setting with CSS.
  GError *css_error=NULL;
  gint minor_version=gtk_get_minor_version();
  gchar *css_string=NULL;
  //GTK CSS changed after 3.20. This has only been tested on GTK3.18.
  if(minor_version>20) css_string=g_strdup("grid, entry{padding-right: 0; margin-right: 0; padding-left: 0; margin-left: 0}");
  else css_string=g_strdup("GtkGrid GtkEntry{padding-right: 0; margin-right: 0; padding-left: 0; margin-left: 0}");
  GtkCssProvider *provider=gtk_css_provider_new();
  GdkDisplay *display=gdk_display_get_default();
  GdkScreen *screen=gdk_display_get_default_screen(display);
  gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  gtk_css_provider_load_from_data(provider, css_string, -1, &css_error);
  if(css_error!=NULL)
    {
      g_print("CSS loader error %s\n", css_error->message);
      g_error_free(css_error);
    }
  if(css_string!=NULL) g_free(css_string);
  g_object_unref(provider);
 
  gtk_container_add(GTK_CONTAINER(window), grid);
  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]