GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 22, 2014 9:38 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Problems implementing word-wrap in a treeview column.
PostPosted: Mon Dec 03, 2012 11:32 am 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
I've been trying to implement text word-wrapping of text within a list using a code sample from Krause. For some reason unclear to me, the options that I've tried do not appear to work. Has anyone got it to work?

This is the test code that I've been working with.

WJG

Code:
#include <gtk/gtk.h>

enum
{
  BUY_IT = 0,
  QUANTITY,
  PRODUCT,
  COLUMNS
};

typedef struct
{
  gboolean buy;
  gint quantity;
  gchar *product;
} GroceryItem;

const GroceryItem list[] =
{
  { TRUE, 1, "Paper Towels" },
  { TRUE, 2, "Bread" },
  { FALSE, 1, "Butter" },
  { TRUE, 1, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." },
  { FALSE, 3, "Chips" },
  { TRUE, 4, "Soda" },
  { FALSE, 0, NULL }
};

static void setup_tree_view (GtkWidget*);

int main (int argc, char *argv[])
{
  GtkWidget *window, *treeview, *scrolled_win;
  GtkListStore *store;
  GtkTreeIter iter;
  guint i = 0;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Grocery List");
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  gtk_widget_set_size_request (window, 250, 175);

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

  treeview = gtk_tree_view_new ();
  setup_tree_view (treeview);

  /* Create a new tree model with three columns, as string, gint and guint. */
  store = gtk_list_store_new (COLUMNS, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_STRING);

  /* Add all of the products to the GtkListStore. */
  while (list[i].product != NULL)
  {
    gtk_list_store_append (store, &iter);
    gtk_list_store_set (store, &iter, BUY_IT, list[i].buy, QUANTITY, list[i].quantity, PRODUCT, list[i].product, -1);
    i++;
  }

  /* Add the tree model to the tree view and unreference it so that the model will
   * be destroyed along with the tree view. */
  gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
  g_object_unref (store);

  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  gtk_container_add (GTK_CONTAINER (scrolled_win), treeview);
  gtk_container_add (GTK_CONTAINER (window), scrolled_win);
  gtk_widget_show_all (window);

  gtk_main ();
  return 0;
}

/* Add three columns to the GtkTreeView. All three of the columns will be
* displayed as text, although one is a gboolean value and another is
* an integer. */
static void setup_tree_view (GtkWidget *treeview)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;

  /* Create a new GtkCellRendererText, add it to the tree view column and
   * append the column to the tree view. */
   
  // 1
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Buy", renderer, "text", BUY_IT, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

  // 2
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Count", renderer, "text", QUANTITY, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

  // 3
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Product", renderer, "text", PRODUCT, NULL); 
 
/**********************************
   * why do neither of these work?
   * **********************************/
  //column = gtk_tree_view_column_new_with_attributes ("wrap-mode", PANGO_WRAP_WORD, "wrap-width", 400,"Product", renderer, "text", PRODUCT, NULL);
  //gtk_tree_view_column_set_attributes ( column, renderer,"wrap-mode", PANGO_WRAP_WORD, "wrap-width", 400,NULL);
  /********************************************************************/
 
  gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
 Post subject: Re: Problems implementing word-wrap in a treeview column.
PostPosted: Mon Dec 03, 2012 10:36 pm 
Offline
Familiar Face

Joined: Mon Jul 11, 2011 10:24 am
Posts: 15
You try to manipulate the settings of the column, but you need to set wrap-mode and wrap-width for the cell renderer.
Do it like this:

Code:
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Product", renderer, "text", PRODUCT, NULL); 
 
g_object_set (renderer, "wrap-width", 400, "wrap-mode", PANGO_WRAP_WORD, NULL);


It worked for me.


Top
 Profile  
 
 Post subject: Re: Problems implementing word-wrap in a treeview column.
PostPosted: Tue Dec 04, 2012 10:13 am 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
Thanks, it worked a treat.

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], newguy and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group