Re: Newbie -> About gtk_clist_find_row_from_dat



On Mon, 2001-10-29 at 12:47, 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);

Hola Antonio,

The problem with the assignments below is that they will leak the memory
you just allocated above.

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

        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])));  


Did you ever associate the pointer with the row? In other words,
gtk_clist_append returns the new row and then you do a 
gtk_clist_set_row_data?

Regardless, the address of the string is that of internal memory used by
the GtkEntry and so it seems like your intention was to compare the
actual text and not the pointers so this will not work.

            gtk_clist_append((GtkCList *) clist1, cadenas);
          }
}


A potential solution may be to create a hash table to contain the
cadenas[0] strings. You could do a lookup in the table to see if it is
already there. If so you don't append. If it is not in the hash table
you add it to the hash table and append to the clist.

See the glib g_hash_table functions for working with hash tables. Since
you'll be comparing strings, you could pass the g_str_equal() function
to the g_hash_table_new() function.

--
Antonio Martínez Álvarez, <antonioma eresmas net>







_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


-- 
regards,

Luciano Chavez

lnx1138 us ibm com          
http://sf.net/projects/evms




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