GtkIconView displaying ellipsized label



Hi all,
I try to used a GtkIconViewer for displaying icons with very long labels. So
I used a custom layout and set the "ellipsize" property to the text cell.
It works well as long as the items are not selected. When an item is
selected, the size request for its text label (and focus box) is get from
the "width-chars" property (see GtkCellRendererText::width-chars doc)  which
renders ugly.

Note I am using GTK+ 2.12.

Here is a sample code :

#include <gtk/gtk.h> /* GTK+ 2.12.12 */

enum
  {
    ICON_COLUMN =0,
    NAME_COLUMN,
    N_COLUMN
  };

static void  _fill_model(GtkListStore* list_model);
static GtkWidget* create_main_window();
static GtkWidget* _create_icon_view();


static void _fill_model(GtkListStore* list_model)
{
  GtkWidget* render_widget;
  GdkPixbuf* pixbuf;
  GtkTreeIter iter;
  const gchar* long_text = "a sometime very long icon name description";
  int i =0;

  render_widget = gtk_invisible_new();
  pixbuf = gtk_widget_render_icon (render_widget,
                   GTK_STOCK_DIRECTORY,
                   GTK_ICON_SIZE_DIALOG,
                   NULL);
  for( i = 0; i < 10; ++i)
    {
      gtk_list_store_append(list_model, &iter);
      gtk_list_store_set(list_model, &iter,
             ICON_COLUMN, pixbuf,
             NAME_COLUMN, long_text,
             -1);
    }
  gtk_object_sink(GTK_OBJECT(render_widget));
  g_object_unref(G_OBJECT(pixbuf));
}

static GtkWidget* create_main_window()
{
  GtkWidget* main_window;
  GtkWidget* scolled_window;
  GtkWidget* icon_view;


  main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  scolled_window = gtk_scrolled_window_new(NULL, NULL);
  icon_view = _create_icon_view();
  gtk_container_add(GTK_CONTAINER(main_window), scolled_window);
  gtk_container_add(GTK_CONTAINER(scolled_window), icon_view);
  g_signal_connect(G_OBJECT(main_window),"delete-event",
G_CALLBACK(gtk_main_quit), NULL);
  return main_window;

}

static GtkWidget* _create_icon_view()
{
  GtkWidget* icon_view;
  GtkCellRenderer* text_cell;
  GtkCellRenderer* pixbuf_cell;
  GtkListStore* list_model;
  list_model = gtk_list_store_new(N_COLUMN, GDK_TYPE_PIXBUF, G_TYPE_STRING);
  icon_view =     gtk_icon_view_new_with_model(GTK_TREE_MODEL(list_model));
  /* pixbuf*/
  pixbuf_cell = gtk_cell_renderer_pixbuf_new();
  g_object_set( G_OBJECT(pixbuf_cell),
        "follow-state", TRUE,
        NULL);
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), pixbuf_cell,
FALSE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (icon_view), pixbuf_cell,
"pixbuf", ICON_COLUMN);
  /* text : if too long we ellipsize */
  text_cell= gtk_cell_renderer_text_new ();
  g_object_set(G_OBJECT(text_cell),
           "ellipsize-set", TRUE, "ellipsize", PANGO_ELLIPSIZE_END,
          /* "width-chars", 20, */
           NULL);

  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), text_cell,
FALSE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (icon_view), text_cell,
"text", NAME_COLUMN);

  /* tooltip for long text */
  gtk_icon_view_set_tooltip_column(GTK_ICON_VIEW(icon_view),
                   NAME_COLUMN);
  /* fill model */
  _fill_model(list_model);
  return icon_view;
}

gint main(gint argc, gchar** argv)
{
  GtkWidget* main_window;
  gtk_init(&argc, &argv);
  main_window = create_main_window();
  gtk_widget_show_all(main_window);
  gtk_main();
  return 0;
}

Regards
Nicolas



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