Re: Changing a Label Presentation



Hi Ruben,

The problem is that you may never have two process, or threads
that share the same X-connection. And this is usually not 
necessary either. What you want to do is the following:

         +----+       +-------------------+       +------+
         |  X |   <=> | Your gtk program  |  <=>  |  tar |
	 +----+       +-------------------+       +------+

I.e. all the display from the tar program has to pass through
your gtk program. Now the problem is how you can make one 
program talk to two processes at once. Especially since you
are supposed to leave control to gtk by calling gtk_main().
The answer is to use a call to gdk_input_add(). By doing the
following commands:

    FILE *TAR;

    TAR = popen("tar -zvcf whatever", "r");
    gdk_input_add(fileno(TAR),
                  GTK_INPUT_READ,
		  cb_read_on_tar_handle,
		  my_applic);

    :
    gint cb_read_on_tar_handle(gpointer data, 
                               gint source, 
			       GdkInputCondition condition)
    {
       MyApp *app = (MyApp*)data;
       FILE *TAR = fdopen(source, "r");

       num_bytes_read = fread(buf, 1,sizeof(buf),TAR);

       gtk_text_append(app->text,
                       buf, ...);
       fclose(TAR);
    }

or something similar. Obviously the code above dosent' compile. The 
key is to start thinking in terms of event drive programming. The 
program doesn't have control. It only sets up event handlers that
are evoked whenever something happens.

Hope this helps.

Regards,
Dov

On Sun, Jun 09, 2002 at 10:00:19AM -0400, Ruben I Safir wrote:
> 
> 
> Dov
> 
> Thanks for the reply.  I've finally had time to get back to this problem. 
> 
> It seems I'm going through some growing pains with GTK at this junction.
> 
> I took your suggestion and I tried to fork the taring process and capture
> the output in a text object, in a new toplevel window.  This resulted in 
> a common problem, with an error mesage from X related to threading which
> I didn't really intend to use:
> 
> See ref(http://groups.google.com/groups?q=Xlib:+unexpected+async+reply+GTK&hl=en&lr=&selm=abei4b%246bp%244%40news1.kornet.net&rnum=5)
> 
> Xlib: unexpected async reply 
> 
> 
> I wanted to fork the tar process off by itself have it show in it's own window.
> 
> In fact, I've been trying to seperate the programming logic from the 
> display segments of the program, and I'm stumbling.  I assume there 
> is a way to handle this common problem with forking without having to rely
> on threading.  Popup windows with forked processes happen all the time.  The
> code looks like this:
> 
> gint createbackup(){
> 	gint ret; 
> //	gint pid;
> 	gchar buffer[512];
> 	FILE *tarcommand;
> 	GtkWidget *window_proc;
> 	GtkWidget *text;
> 	GtkWidget *box;
> 	GtkWidget *vscroll;
> 	const char *command = "/bin/tar -cvpPf /tmp/apache.tar /usr/local/apache/htdocs/nylinux/ 2>/dev/null";
> 	const char *command2 = "/bin/tar -tf /tmp/apache.tar 1> /home/ruben/backuplist";
> 	GdkColormap *cmap;
> 	GdkColor color;
> 
> /*	pid = fork(); 
> 	if(pid != 0){  
> 	    perror("We are in the child");   */	
> 	    gtk_window_new(GTK_WINDOW_TOPLEVEL);
>             window_proc = gtk_window_new(GTK_WINDOW_TOPLEVEL);
> 	    gtk_widget_set_usize(window_proc, 600, 400);
>             gtk_signal_connect(GTK_OBJECT(window_proc), "delete_event", 
> 			    GTK_SIGNAL_FUNC(delete_win),NULL);
> 	    
>             text = gtk_text_new(NULL,NULL);
> 	    gtk_text_set_editable(GTK_TEXT(text), FALSE);
> 	    box = gtk_hbox_new(FALSE,0);
> 	    vscroll=gtk_vscrollbar_new(GTK_TEXT(text)->vadj);
> 	    gtk_container_add(GTK_CONTAINER(window_proc), box);
> 	    gtk_box_pack_start(GTK_BOX(box), text, TRUE, TRUE,0);
> 	    gtk_box_pack_start(GTK_BOX(box), vscroll, FALSE, FALSE,0);
> 	    gtk_widget_show(vscroll);
> 	    gtk_widget_show(text);
> 	    gtk_widget_show(box);
> 	    gtk_widget_show(window_proc);
> 	    cmap = gdk_colormap_get_system();
> 	    color.red = 0xffff;
> 	    color.green = 0;
> 	    color.blue = 0;
> 	    if (!gdk_color_alloc(cmap, &color)) {
> 	         g_error("couldn't allocate color");
> 	   }
> 
> 		     
> 
> 			    
> 			    
> 	   tarcommand  = popen(command, "r");
> 	   while(fgets(buffer, 512, tarcommand)){
> //		   gtk_text_freeze(GTK_TEXT(text));
> 		   gtk_text_insert(GTK_TEXT(text),NULL,NULL,NULL, buffer, strlen(buffer));
> //		   gtk_text_thaw(GTK_TEXT(text));
> 		   }
> 		   gtk_text_insert(GTK_TEXT(text),NULL,&color,NULL,
> 				   "DONE", -1);
> 		   
> 	   
> 	   ret = pclose(tarcommand);
> 	   ret = system(command2);
> 
> 	   
> 	    if( ret  == 127 ){
> 			 g_print("Can not open shell\n");
> 			 exit(0);
> 			 
> 	     }
> 	    return ret;
> 	    
> /*	    
> 	}else{ */
> 	 /*ret = fgetc(tar);*/
> /*	    ret = 0;	
> 	    return(ret);
> 	} */
> }
> 
> 
> I tried system() and popen.
> 
> Also, in the other segment I'm writing, I'm trying to display a list of
> all the files which have been tarred up, for untarring.  This can be a LONG
> list, and I tried to use the GtkList object to try to display this for user
> input.  When I tried to create a Glib GList and add it to the GtkList widget,
> no matter how I tried to run  gtk_widget_show(item); it wouldn't show and I 
> got empty windows.  I had to resort to entering them item by item like this:
> 
> GList  *createlist(void){
> 	GtkWidget *item;
> 	GList *list = NULL;
> 	GtkWidget *listbox;
> 	GtkWidget * window_select;
> 	FILE *fp;
> 	char file[512];
> 	fp = fopen("/home/ruben/backuplist", "r");
> 	window_select = gtk_window_new(GTK_WINDOW_TOPLEVEL);
> 	gtk_container_border_width(GTK_CONTAINER(window_select), 10);
> 	gtk_window_set_title(GTK_WINDOW(window_select), "Files to Restore");
> 	listbox = gtk_list_new();
> 	gtk_list_set_selection_mode(GTK_LIST(listbox), GTK_SELECTION_BROWSE);
> 	gtk_container_add(GTK_CONTAINER(window_select), listbox);
> 	while(fgets(file, 512, fp)){
> 		g_print("%s",file);
> 		item = gtk_list_item_new_with_label(file);
> 		gtk_container_add(GTK_CONTAINER(listbox), item);
> 		gtk_widget_show(item);
> 	}
> 	fclose(fp);
> 	gtk_widget_show(listbox);
> 	gtk_widget_show(window_select);
> 
> 	return(list);
> }
> 
> When I really wanted to append the whole list at once.  Further, when I looked up the GktList
> object on the GTK docs, I see it's being phased out.  It didn't work anyway.  Too many records need
> to be displayed and it used up all my memory.  It might do better with a scroll bar, which is the next 
> thing to try.  But is GtkList is being phased out, and appending a Glist isn't working ANYWAY, what's 
> the proper widget to use?
> 
> 
> 
> Ruben
>  
> On 2002.05.19 10:00 Dov Grobgeld wrote:
> > The reason that the label isn't drawn is that the graphics context
> > isn't flushed. This may be done simply by calling gtk_flush(). But
> > there is a more fundemental problem with your program, and it has 
> > to do with the concept of event driven programming. In your callback
> > you are doing an operation which is taking a very long time. This
> > is basically a no-no, as you are taking away the control from the
> > gtk_main() loop. What you should do in such a case is to fork off
> > a child that runs the backup process in the background. (If you want
> > to you can read stdout from the standard output and put it e.g. in
> > a text view widget). When the child process is terminated it sends
> > to its parent a SIG_CHILD unix signal. By catching this unix signal
> > you can figure out that your child process is finished and change
> > the value of your label.
> > 
> > I hope this helps.
> > 
> > Regards,
> > Dov
> > 
> > On Sun, May 19, 2002 at 02:14:20AM -0400, Ruben I Safir wrote:
> > > Hello
> > > 
> > > I'm trying to write a program which changes a label from a callback and it's not working.
> > > 
> > > rmbackup.c
> > > 
> > > #include <stdio.h>
> > > #include "tarlib.h"
> > > #include "backprog.h"
> > > 
> > > int main(int argc, char **argv){
> > > 	struct s1 window1;
> > > 	gtk_init(&argc, &argv);
> > > 	window1 = screen1();
> > > 	gtk_main();
> > > 	exit(0);
> > > }
> > > 
> > > backprog.c
> > > 
> > > struct s1 maketoggles(){
> > > 	static struct s1 handle;
> > > 	GtkWidget *button;
> > > 	GtkWidget *vbox;
> > > 	GtkWidget *label;
> > > 	
> > > 	vbox = gtk_vbox_new(TRUE, 0);
> > > 	
> > > 	label = gtk_label_new("Choose an Option");
> > > 	
> > > 	gtk_widget_show(label);
> > > 	
> > > 	gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE,3);
> > > 	
> > > 	button = gtk_toggle_button_new_with_label("Create Backup");
> > > 	gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, FALSE, 0);
> > > 	gtk_widget_show(button);
> > > 	gtk_signal_connect(GTK_OBJECT(button), "clicked",
> > > 			GTK_SIGNAL_FUNC(startbackup), 
> > > 			label );
> > > 
> > > ........
> > > 
> > > void startbackup( GtkWidget *widget, GtkWidget *lab){
> > > 	gint ret;
> > > 	gtk_label_set_text(GTK_LABEL(lab), "CREATING BACKUP");  <-----Not changing
> > > 	ret = createbackup();
> > > 	g_print("%d",ret);
> > > 	g_print("\n");
> > > 	gtk_label_set_text(GTK_LABEL(lab), "Choose an Option");
> > > 	
> > > }
> > > 
> > > 
> > > -- 
> > > __________________________
> > > 
> > > Brooklyn Linux Solutions
> > > __________________________
> > > http://www.mrbrklyn.com - Consulting
> > > http://www.brooklynonline.com - For the love of Brooklyn
> > > http://www.nylxs.com - Leadership Development in Free Software
> > > http://www.nyfairuse.org - The foundation of Democracy
> > > http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from around the net
> > > http://www2.mrbrklyn.com/mp3/dr.mp3 - Imagine my surprise when I saw you...
> > > http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn....
> > > 
> > > 1-718-382-5752
> > > 
> > > 
> > > 
> > > _______________________________________________
> > > gtk-list mailing list
> > > gtk-list gnome org
> > > http://mail.gnome.org/mailman/listinfo/gtk-list
> > 
> > -- 
> >                                                         ___   ___
> >                                                       /  o  \   o \
> > Dov Grobgeld                                         ( o  o  ) o   |
> > The Weizmann Institute of Science, Israel             \  o  /o  o /
> > "Where the tree of wisdom carries oranges"              | |   | |
> >                                                        _| |_ _| |_
> >  
> > 
> -- 
> __________________________
> 
> Brooklyn Linux Solutions
> __________________________
> http://www.mrbrklyn.com - Consulting
> http://www.brooklynonline.com - For the love of Brooklyn
> http://www.nylxs.com - Leadership Development in Free Software
> http://www.nyfairuse.org - The foundation of Democracy
> http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from around the net
> http://www2.mrbrklyn.com/mp3/dr.mp3 - Imagine my surprise when I saw you...
> http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn....
> 
> 1-718-382-5752
> 
> 
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list



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