Re: Newbie -> About gtk_clist_find_row_from_dat...



On Mon, Oct 29, 2001 at 02:32:03AM +0100 Antonio Martínez Álvarez wrote:

Hello.

I have a gtkclist list with 2 columns. When I add lines to my list, I
would like to make sure that there is not another similar line.
Well, I want the first column of my list not to have a repeated token.
I have wrote this code (I have used gtk_clist_find_row_from_data, but it
doesn't work).

Note: I'm testing the output of the gtk_clist_find_row_from_data
function by writting the returned value. I always obtain -1...

What's wrong?

Sorry for my english. Greetings from Spain.

[My code]

void boton1(GtkButton *widget, gpointer data)
{

      gchar *cadenas[2];
     
      cadenas[0] = (gchar *) malloc(255);
      cadenas[1] = (gchar *) malloc(255);

You dont need malloc here, just delete the two lines.

      cadenas[0] = gtk_entry_get_text((GtkEntry *) entry1);
      cadenas[1] = gtk_entry_get_text((GtkEntry *) entry2);

This gives you the pointer to the entry strings (as long as you
dont destroy the entry widget).

      if ((strcmp(cadenas[0],"")==0)||((strcmp(cadenas[1],"")==0)))
        printf("Cadenas vacía\n");
      else
        {                                                
          printf("[%i]", (gtk_clist_find_row_from_data((GtkCList *)
clist1,(gpointer)cadenas[0])));  
          gtk_clist_append((GtkCList *) clist1, cadenas);
        }
}

The problem is here:
gtk_clist_find_row_from_data() does not compare the given string with
any text the clist displays, it only compares the _pointer_
with the pointer you have attached to the clist lines earlier (in your
case you didnt attach any data to the lines).

You have to go through the clist lines and use:
gtk_clist_get_text(clist, row, column, text);

A better method would be:
Separate view and functionality, make a data structure for your
inserted items:

  GList* clist_entries = NULL;
  char* temp_string;
  int row;

  // inserting a new item:
  cadenas[0] = gtk_entry_get_text((GtkEntry *) entry1);
  cadenas[1] = gtk_entry_get_text((GtkEntry *) entry2);

  if (search_in_clist_entries_for(cadenas[0])) // you have to implement this
                                               // search function
    return;   // do nothing if already in list
  
  // now append data to the entries:
  temp_string = g_strdup(cadensa[0]);
  clist_entries = g_list_prepend(clist_entries, temp_string);
  // then make it visible and attach data. 
  row = gtk_clist_append(clist, cadenas);
  gtk_clist_set_row_data_full(clist, row, temp_string, 
                              (GtkDestroyNotify*)destroy_clist_data);
  // you have to implement the destroy_clist_data(), that frees the attached
  // data and removes the data from the clist_entries with:
  // g_free(attached_data_pointer);
  // clist_entries = g_list_remove(clist_entries, attached_data_pointer);

This may look like an overkill, but it is (in my opinion) a cleaner style of
programming and at least avoidable if you'd like to attach more complex data to the
clist rows.

Markus.



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