GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Apr 19, 2014 7:36 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Passing data through callback functions
PostPosted: Thu Dec 27, 2012 12:02 am 
Offline
Familiar Face

Joined: Tue Dec 25, 2012 7:14 pm
Posts: 5
Hello everyone.

This is a GTK 2.0 and C programming question. I am using Ubuntu.

I used glade to make a GTK interface. It has several buttons and each one has its own callback function.

I'd like to know what is the best way to pass data through these functions.

The following code happens when I press an "OK" button in a File Chooser Dialog.

Code:
void OK_FCD_clicked ()
{
    int n;
    GSList * FileList;
   
    GtkWidget *filechooserdialog = GTK_WIDGET(gtk_builder_get_object(builderG, "filechooserdialog1")); // gets the widget from the builder object
   
    FileList = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(filechooserdialog)); //puts the selected files path in the GSlist FileList
   
    guint nFiles = g_slist_length(FileList); //number of files selected
   
    PathList paths[nFiles]; //struct I created to organize the data
   
    for (n=0; n<nFiles;n++) //cycle to put the GSlist data into my struct
    {
     paths[n].number=n; 
     paths[n].filename=g_slist_nth_data(FileList,n);
     paths[n].total=nFiles;
    }   
   
   for (n=0; n<nFiles;n++) printf("%d - %s\n",paths[n].number,paths[n].filename); //this cycle just prints out the information I just organized

}


The PathList struct is the following:

Code:
typedef struct {int number; char * filename; int total;} PathList;


Now I need to use that information in other callback functions, but I am not sure how to pass the data through all these functions.

Im new to GTK so please be kind :P

Best regards


Top
 Profile  
 
 Post subject: Re: Passing data through callback functions
PostPosted: Thu Dec 27, 2012 10:36 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
oo... "best" is always a loaded term and open to opinion.
You can pass structures to callbacks but not back from callbacks.

The solution; however, is very simple. Use a global variable. Any data declared outside of a function will be available for all the functions following it. Using global variables is generally more efficient than local variables, they just have a habit of making your code a bit messy and may lead to bugs if not used correctly; which is why people prefer to do things local.

Now the trouble with your code is that you don't know how big to allocate this structure until the callback finds out how many files are selected and thus can't create your Pathlist as an array except in making it really big and just hope it doesn't overflow. This is the problem with static memory. Fortunately you have at your finger tips a number of freely expandable data types that use dynamic memory (you are already using one, the gslist). In fact all you have to do is declare FileList at the top of your code and you can use it anywhere.

As an alternative example, let's say you want to transfer your data to the more compact GArray (at least I think its more compact). Declare your GArray as a global variable, then append each PathList element into it. In fact because of the nature of the data type you will only need the filename (it is ordered and keeps track of its length - as does gslist). Here's an example (I've rejigged the iterator to be a bit more efficient):
Code:
GArray* gb_pla;
void cb(...)
{
  gchar* str;
  GSList* list;
  ...
  list=gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(filechooserdialog));
  while (list)
  {
    str=g_str_dup((gchar*) (list->data));
    g_array_append_val(gb_pla, str);
    g_free(str);
    list=list->next;
  }
  ...
}
...
int main(...)
{
  gb_pla=g_array_sized_new(FALSE, FALSE,sizeof(gchar*),0);
  ...
  gtk_main();
  g_array_free(gb_pla);
}

Keep in mind that with memory management you should free up your data when done with it (I think I should be including steps to free list as I go in the code above, but I'm in a lazy mood today).


Top
 Profile  
 
 Post subject: Re: Passing data through callback functions
PostPosted: Thu Dec 27, 2012 10:02 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2012 7:14 pm
Posts: 5
Thank you for the answer!

I will try to take advantadge of the GTK data types, and then I'll give some feedback !


Top
 Profile  
 
 Post subject: Re: Passing data through callback functions
PostPosted: Fri Dec 28, 2012 9:00 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2012 7:14 pm
Posts: 5
It worked, thanks :)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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