Re: Why can't I pass a char* in gtk_signal_connect!



sreekant kodela wrote:

Hi folks

I am trying to make a small toolbar app for my
purposes. I suppose I can always use someone elses
work but I thought I will give it a try. The program
is supposed to start, find the user home dir, parse
the config file, and create buttons with icons as a
button bar. As part of that it links the button to the
callback run_event which accepts the widget and data.
I used the command string eg: xterm , gimp what ever
in the data section of params for callback. But if you
try and compile the bummer it seem to get the string
completely mixed up. Some times it sends the same
string or null or partial string. If I use a string in
"" directly it accepts but otherwise if I use char*
parsed from config file, it gets the strings wrong as
above.
[snip]

You can't rely on strings returned by strtok() to be around later. You need to pass a heap-allocated copy to g_signal_connect(). I'd recommend using g_strdup() for this purpose. Note that you'll need to free this string when you're done using it, otherwise you'll end up with a memory leak. I'd suggest you read the man page for strtok() while you're at it.

   -brian

Full source code below. [ Not very big ]
=================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>


#define SZ_LINES 512
#define SZ_PARSE 128
#define SZ_WIDTH 128

void delete_event (GtkWidget *widget, gpointer data)
{
 gtk_main_quit ();
}

//whether data is char* or gpointer it still doesnot
work.
void run_event(GtkWidget *widget,gpointer data)
{
 printf("%s",data);
 //   pid_t id;
 /*  id=fork();
 if (id==0)
 {system(data); exit(0);}*/
}

int main (int argc, char *argv[])
{
 uid_t userid;
 struct passwd *user;
 FILE* fin;
 char *buf,*cnfname;
 char *title,*cmd,*pixmap;
 int cnt=0;
 size_t len=0;
 ssize_t read;
 GtkWidget* win;
 GtkWidget * btnbox;
 GtkWidget * iconw[SZ_WIDTH];
 GtkWidget* button[SZ_WIDTH];
 GtkWidget* xpmbox[SZ_WIDTH];
 GtkWidget *cbtn;
 GtkTooltips *tooltips;

 //Start UI
 gtk_init (&argc, &argv);
 tooltips=gtk_tooltips_new();
 win = gtk_window_new (GTK_WINDOW_POPUP);
 gtk_window_set_decorated (GTK_WINDOW (win), FALSE);
 gtk_window_set_skip_pager_hint (GTK_WINDOW (win),
TRUE);
 g_signal_connect (G_OBJECT (win),
"delete_event",G_CALLBACK (delete_event), NULL);
 gtk_widget_realize (win);
 btnbox=gtk_hbox_new(FALSE,0);
 gtk_container_add (GTK_CONTAINER (win), btnbox);
 //Create the first button, for closing the app
 cbtn=gtk_button_new_from_stock("gtk-stop");

g_signal_connect(G_OBJECT(cbtn),"clicked",G_CALLBACK(delete_event),NULL);

gtk_box_pack_start(GTK_BOX(btnbox),cbtn,FALSE,FALSE,0);
 gtk_widget_show(cbtn);
 //Findout home dir and read conf
 //TODO: error checking in conf reading
 //TODO: Deal better with empty new lines etc.
 userid=getuid();
 user=getpwuid(userid);
 cnfname=(char *)malloc(SZ_LINES);
 strcpy(cnfname,user->pw_dir);
 strcat(cnfname,"/.mybar/config");
 //  fin=fopen(cnfname,"r"); //dbg
 fin=fopen(argv[1],"r");
 if (fin==NULL) exit(1);
 //Get some memory
 buf=(char *)calloc(SZ_LINES,sizeof(char));
 title=(char *)calloc(SZ_PARSE,sizeof(char));
 cmd=(char *)calloc(SZ_PARSE,sizeof(char));
 pixmap=(char *)calloc(SZ_PARSE,sizeof(char));
 //TODO : check memory has been alloced.

 //Now create the buttons
 while ((read=getline(&buf,&len,fin))!= -1)
   {
     // parse lines
     title=strtok(buf,",");
     cmd=strtok(NULL,",");
     pixmap=strtok(NULL,",");
     pixmap[strlen(pixmap)-1]='\0';
     button[cnt]=gtk_button_new();
     iconw[cnt] = gtk_image_new_from_file (pixmap);
gtk_container_add(GTK_CONTAINER(button[cnt]),iconw[cnt]);
     //The one below works but the one further down
doesnot. Why!!!
// g_signal_connect(G_OBJECT(button[cnt]),"clicked",G_CALLBACK(run_event),(gpointer)"hi
there");
g_signal_connect(G_OBJECT(button[cnt]),"clicked",G_CALLBACK(run_event),(gpointer)cmd);
     gtk_tooltips_set_tip (tooltips,
button[cnt],title, NULL);
gtk_box_pack_start(GTK_BOX(btnbox),button[cnt],FALSE,FALSE,0);
     gtk_widget_show(button[cnt]);
     gtk_widget_show(iconw[cnt]);
     memset(buf,sizeof(buf),'\0');
     memset(title,sizeof(title),'\0');
     memset(pixmap,sizeof(pixmap),'\0');
     memset(cmd,SZ_PARSE,'\0');
     ++cnt;
   }
 gtk_widget_show (btnbox);
 gtk_widget_show (win);

 gtk_main ();
 return 0;
}


// CONF FILE BELOW
/*
Terminal,xterm,kterm.xpm
Gimp,gimp,xpaint.xpm
Gimp,gimp,xpaint.xpm
Terminal,xterm,kterm.xpm
*/
//END CONF FILE



                
__________________________________ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ _______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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