Re: [gtk-list] using a callback as a normal function



On Wed, Apr 07, 1999 at 08:04:02PM +0000, Aaron Walker wrote:
> Is it possible to use a callback also as a normal function?
> For example, I have a program that can either open files from a
> FileSelection dialog or from the command line.  Instead of having two
> separate functions for each, I want to have just one universal function:
> 
> void file_open(GtkWidget *widget, gint method)
> {
>         if(method == 1)
>             /* do this if it is called from the cmd line */
>         else if(method == 2)
>             /* do this if it is called from the FileSelection */
> }
> 
> Now say I want to use this function as a callback for a menu item:
> 
> static GtkItemFactoryEntry menu_items[] =
> {
>      {"/_File", NULL, NULL, 0, "<Branch>"},
>      {"/File/_New", "<control>N", NULL, 0, NULL},
>      {"/File/_Open", "<control>O", file_open, 0, NULL},
>      ....
> };
> 
> how do I pass arguments to this?

You could use wrapper functions, like this:

void file_open(gint method)
{
         if(method == 1)
             /* do this if it is called from the cmd line */
         else if(method == 2)
             /* do this if it is called from the FileSelection */
         else if(method == 3)
             /* do this if it is called from the menu */
}

void file_open_cmdline(void);
{
	/* commandline related preparatory stuff happens, and then... */
	file_open(1)
}

void file_open_fileselection(GtkWidget *widget) 
{
	/* FileSelection related preparatory stuff happens, and then... */
	file_open(2)
}

void file_open_menu(gpointer *data)
{
	/* menu related stuff preparatory stuff happens, and then... */
	file_open(3)
}



--
Chris Malek, Sys. Admin.           cmalek@etho.caltech.edu
Division of Biology, 216-76
California Institute of Technology
Pasadena, CA  91125



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