Wireshark-dev: Re: [Wireshark-dev] New packet list - small fixes

From: Jakub Zawadzki <darkjames@xxxxxxxxxxxxxxxx>
Date: Sun, 12 Jul 2009 20:14:42 +0200
On Sun, Jul 12, 2009 at 11:08:59AM +0200, Anders Broman wrote:
> I haven't figured out where packet_list_get_value():s column
> value comes from yet. 

It's set by gtk_tree_view_column_add_attribute(..., "text", X) call

Doc: http://library.gnome.org/devel/gtk/stable/GtkTreeViewColumn.html#gtk-tree-view-column-add-attribute
"So for example if column 2 of the model contains strings, you could
have the "text" attribute of a GtkCellRendererText get its values from
column 2."

In attachment patch.
diff --git gtk/new_packet_list.c gtk/new_packet_list.c
index 73f7209..e43d789 100644
--- gtk/new_packet_list.c
+++ gtk/new_packet_list.c
@@ -132,8 +132,7 @@ create_view_and_model(void)
 	for(i = 0; i < cfile.cinfo.num_cols; i++) {
 		col = gtk_tree_view_column_new();
 		gtk_tree_view_column_pack_start(col, renderer, TRUE);
-		gtk_tree_view_column_add_attribute(col, renderer, "text",
-						   cfile.cinfo.col_fmt[i]);
+		gtk_tree_view_column_add_attribute(col, renderer, "text", i);
 		gtk_tree_view_column_set_title(col, cfile.cinfo.col_title[i]);
 		gtk_tree_view_column_set_sort_column_id(col, i);
 		gtk_tree_view_column_set_resizable(col, TRUE);
diff --git gtk/packet_list_store.c gtk/packet_list_store.c
index 1b1cc14..c772d21 100644
--- gtk/packet_list_store.c
+++ gtk/packet_list_store.c
@@ -205,11 +205,11 @@ packet_list_tree_model_init(GtkTreeModelIface *iface)
 static void
 packet_list_init(PacketList *packet_list)
 {
-	guint i;
+	gint i;
 #if 0
 	gint fmt;
 
-	for(i = 0; i < (guint)cfile.cinfo.num_cols; i++) {
+	for(i = 0; i < NUM_COL_FMTS && i < cfile.cinfo.num_cols; i++) {
 		/* Get the format of the column, see column_info.h */
 		fmt = get_column_format(i);
 		switch(fmt){
@@ -224,12 +224,12 @@ packet_list_init(PacketList *packet_list)
 	}
 #endif
 
-	for(i = 0; i < NUM_COL_FMTS; i++) { /* XXX - Temporary? */
+	for(i = 0; i < NUM_COL_FMTS && i < cfile.cinfo.num_cols; i++) { /* XXX - Temporary? */
 		packet_list->column_types[i] = G_TYPE_STRING;
 	}
 	
 
-	packet_list->n_columns = NUM_COL_FMTS;
+	packet_list->n_columns = i;	/* max(NUM_COL_FMTS, cfile.cinfo.num_cols) */
 	packet_list->num_rows = 0;
 	packet_list->rows = NULL;
 
@@ -517,12 +517,10 @@ packet_list_append_record(PacketList *packet_list, row_data_t *row_data)
 				    packet_list->num_rows);
 
 	newrecord = se_alloc0(sizeof(PacketListRecord));
-	newrecord->col_text = se_alloc0(sizeof(row_data->col_text)* NUM_COL_FMTS);
-
+	newrecord->col_text = se_alloc(sizeof(row_data->col_text) * packet_list->n_columns);
 
-	/* XXX newrecord->col_text still uses the fmt index */
-	for(i = 0; i < cfile.cinfo.num_cols; i++)
-		newrecord->col_text[row_data->col_fmt[i]] = row_data->col_text[i];
+	for(i = 0; i < packet_list->n_columns; i++)
+		newrecord->col_text[i] = (i < cfile.cinfo.num_cols) ? row_data->col_text[i] : NULL;
 
 	newrecord->fdata = row_data->fdata;
 
@@ -632,8 +630,8 @@ packet_list_compare_records(gint sort_id, PacketListRecord *a,
 	 * seen by the user, whereas the col_text array from a and b is a
 	 * column format number. This matches them to each other to get
 	 * the right column text. */
-	gchar *a_col_text = a->col_text[cfile.cinfo.col_fmt[sort_id]];
-	gchar *b_col_text = b->col_text[cfile.cinfo.col_fmt[sort_id]];
+	gchar *a_col_text = a->col_text[sort_id];
+	gchar *b_col_text = b->col_text[sort_id];
 
 	if((a_col_text) && (b_col_text))
 		return strcmp(a_col_text, b_col_text);