GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon May 20, 2013 8:50 pm

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Gtk show label during a callback.
PostPosted: Thu Mar 31, 2011 1:21 pm 
Offline
Familiar Face

Joined: Thu Mar 31, 2011 1:10 pm
Posts: 11
Hi all, I'm newbie to gtk. I'm writing mine first app that will be an installer program. I have setted an input text callback correctly :

GtkWidget *path_install;
path_install = gtk_entry_new();
gtk_box_pack_start (GTK_BOX (box1), path_install, TRUE, TRUE, 0);
gtk_widget_show (path_install);
g_signal_connect(GTK_OBJECT(path_install), "activate", GTK_SIGNAL_FUNC(avviaInstall), NULL);

static void avviaInstall(GtkWidget *widget, gpointer callback_data){

gtk_widget_show(install1); /* show : installing A.... */
do_install(A);
gtk_widget_show(install2); /* show : installing B.... */
do_install(B);
}

Well the problem is that during callback I want to show first one label, and after the second that are both hidden on main() (installing A, installing B, etc...) but all the labels are shown on same moment ONLY when callback terminate.

Is any work around to avoid this ?

Regards
Nunzio


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Thu Mar 31, 2011 2:24 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 324
Location: Sydney, Australia
Yes there is but its complicated.
The reason they're shown at the same time is that interaction with your GUI happens with gtk_main(); and so nothing will change visually until your code reaches it.
The workaround is to use multithreading. Beyond this point I can't help you much as multithreading is on my yet to learn list.


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Thu Mar 31, 2011 3:45 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

As Paul already told you, one way of doing things would be to separate GUI from core functionality of your application and use separate threads/processes for different tasks.

But for such simple example, you could just "flush" the events to the main loop and force it to update stuff. Code fragment for this is:
Code:
while (gtk_events_pending ())
  gtk_main_iteration ();

Just add this code to the location in source where changes to the GUI should be made.

Tadej


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Thu Mar 31, 2011 5:43 pm 
Offline
Familiar Face

Joined: Thu Mar 31, 2011 1:10 pm
Posts: 11
Quote:
Code:
while (gtk_events_pending ())
  gtk_main_iteration ();

Just add this code to the location in source where changes to the GUI should be made.

Tadej


Hi, I've tried your solution but no works with labels. This should be a simple thing I suppose, but seems to be not.


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sat Apr 02, 2011 2:49 pm 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
I did not understood the details of your question. You should perhaps give a complete code example, suitably tagged witn the [ code ] tag.

But I don't understand why you don't use dialogs. In my code (for instance, IaCa, which does not do anything very interesting yet), I have something like (in function popup_final_dialog from file module/first.c)
Code:
  GtkWidget *dial = 0;
  GtkWidget *lab = 0;
  dial = gtk_dialog_new_with_buttons ("Finally dump state",
                  win,
                  GTK_DIALOG_MODAL |
                  GTK_DIALOG_DESTROY_WITH_PARENT,
                  GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
                  GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                  NULL);
  lab = gtk_label_new (NULL);
  markup = /* some markup */;
  gtk_label_set_markup (GTK_LABEL (lab), markup);
  gtk_container_add (GTK_CONTAINER
           (gtk_dialog_get_content_area (GTK_DIALOG (dial))), lab);
  gtk_widget_show_all (dial);
  res = gtk_dialog_run (GTK_DIALOG (dial));
  if (res == GTK_RESPONSE_ACCEPT)
   { /* do shomething useful */ };


Why don't you want to use (modal) GtkDialog-s?

_________________
Basile Starynkevitch (France)
http://starynkevitch.net/Basile/


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sat Apr 02, 2011 9:28 pm 
Offline
Familiar Face

Joined: Thu Mar 31, 2011 1:10 pm
Posts: 11
[quote="Basile S."]I did not understood the details of your question. You should perhaps give a complete code example, suitably tagged witn the [ code ] tag.

I would like to do an installer. This installer should print on every action (check mysql, java, and copy files to dir) to a new label. When a single function end this label should print on screen, example :

Code:

void checkJava() {
.... check jvm
if (ok)
    gtk_widget_show (install1);
}

void checkMysql() {
.... check jvm
if (ok)
     gtk_widget_show (install2);
}

void avviaInstall(){
checkJava();
checkMysql();

....do rest of installation....

}

int main(){
GtkWidget *install1, *install2;
....

install1 = gtk_label_new("[*] Verifica Mysql Server             [ OK ]");
install2 = gtk_label_new("[*] Verifica Java Sun Jvm             [ OK ]");

/* install1 and install2 are not showed */
g_signal_connect(GTK_OBJECT(rmi_input), "activate", GTK_SIGNAL_FUNC(avviaInstall), NULL);
....
}


Instead all labels are printed when functions end.
Installer must show to user in realtime what happen on installation.


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 7:37 am 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
I still think that GtkDialog are much better suited for the goals you have in mind.

Otherwise, you could use gtk_widget_set_sensitive to "gray out" some widgets.

And I don't understand your
Code:
install1 = gtk_label_new("[*] Verifica Mysql Server             [ OK ]");

this seems really something which should be a dialog...

Otherwise, if you really want just text, use a GtkTextBuffer and a GtkTextView like explained in the Text Widget Overview. You can have various textual appearance and behavior thru GtkTextTag-s.

If you really want to hide a widget, use gtk_widget_hide

My guess is that you have not yet a clear view of Gtk basic concepts. Maybe you should try to re-read the Gtk tutorial (but some details might have changed in Gtk 3) ?

Are you fully aware that a GUI program is event driven, so basically built around a loop (i.e. gtk_main etc... in Gtk)? So pedantically, your programming style becomes continuation passing style?

In other words, your program should be made of small steps, each implemented by a function which is a Gtk signal handler. If you really want to show a widget from a callback (i.e. a Gtk signal handler), that callback should return immediately after calling gtk_widget_show and have set something so that other callbacks -or timeout routines (thru g_timeout_add) or idle routines (thru g_idle_add), etc... could do the further steps. Some important things (notably actually showing widgets) happen only inside the main loop executed by gtk_main, not inside your callbacks.

But I still don't understand what you want exactly to achieve, and why you don't use Gtk dialogs or text views.

Quote:
I would like to do an installer. This installer should print on every action (check mysql, java, and copy files to dir) to a new label.

Perhaps each of your elementary action or step should be a callback (which might set an idle function doing the next step, etc.).


Perhaps GtkAssistant-s could be useful to you, since they offer a multi-user step machinery.

P.S. I did take care to add web links to many references. I'm sure they will help you! So take time to follow the links!

_________________
Basile Starynkevitch (France)
http://starynkevitch.net/Basile/


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 9:37 am 
Offline
Familiar Face

Joined: Thu Mar 31, 2011 1:10 pm
Posts: 11
Hi Basile S. thank you for your detailed explain. Yes I'm newbie to gtk, and as you said I would like after a click button perfom all entire installation that in real time write text label or something similar on various operations.

Image an installer program that after a single click on button " INSTALL " perform all operations and write to user all operations performed. With the events on gtk I have all text writed ONLY when ALL operation are finished, this is my issue. If you want I can put mine source code via PM to exaplain much better, thanks.


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 10:04 am 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
The point is that you should break down your program in small (and quick) steps, each in its own function.

Perhaps your avivaInstall() function could be something like
Code:
static gboolean avivastepone(gpointer);
static gboolean avivasteptwo(gpointer);

static void avivainstall(void) {
  g_idle_add(avivastepone, NULL);
}

static gboolean avivastepone(gpointer data) {
   if (checkvm()) {
      gtk_widget_show (install1);
   }
   g_idle_add(avivasteptwo, NULL);
   return FALSE; /* to remove this idle handler */
}


However, I am not at all convinced that your user interface idea is a good one. Why don'y you simply use modal GtkDialog or GtkAssistant-s?

Cheers

_________________
Basile Starynkevitch (France)
http://starynkevitch.net/Basile/


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 10:17 am 
Offline
Familiar Face

Joined: Thu Mar 31, 2011 1:10 pm
Posts: 11
Quote:
However, I am not at all convinced that your user interface idea is a good one. Why don'y you simply use modal GtkDialog or GtkAssistant-s?

Cheers


I have done : splitted with 3 functions all performing operations, but when they go on main() labels are showed.
Actually I have solved with a dirty workaround : few threads with global var wait until previous thread finish :

Code:
void *Thread2{
...
while (phase1_end); /* passive wait */
....
}


Btw I have never used gtkAssistant-s, I will try to use GtkDialog as you suggested.

Regards


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 10:24 am 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
Code:
while (phase1_end); /* passive wait */


This is not at all a passive wait, but a busy & time-consuming spinlock. It is bad programming (if you have 4 such threads, they make your processor hot without reason ; all the CPU cores are uselessly running an endless loop.). At the very least, you should have a waiting or synchronizing call inside the loop body.

I believe you should redesign your program, and read more.

If this is a student assignment and if I was your teacher, I won't give you a good grade!

_________________
Basile Starynkevitch (France)
http://starynkevitch.net/Basile/


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 10:29 am 
Offline
Familiar Face

Joined: Thu Mar 31, 2011 1:10 pm
Posts: 11
Don't worry it's not a student assignment, it's only mine curiosity for using gtk2 :). I was thinking that put some label from function was not a hard way, on other visual languages this is a simply thing, on gtk not :( .


Top
 Profile  
 
 Post subject: Re: Gtk show label during a callback.
PostPosted: Sun Apr 03, 2011 4:53 pm 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
Quote:
I was thinking that put some label from function was not a hard way, on other visual languages this is a simply thing, on Gtk not :(


I am not sure it is GTK specific. All Linux graphical toolkits are running with a X11 protocol and a connection to the Xorg server. And to be sure that all requests have indeed been sent to Xorg (and handled by it), you need to be in the main loop. So basically, you cannot be sure that any graphical function call (be it to a Gtk or to a Qt function) has an effect before reaching the main loop. So any GTK request called inside any GTk callback could take effect only when control gets back to gtk_main, outside and after your callback.

Linux graphical programming is in that respect somehow similar to Web programming. Whatever you do in a web program running in the HTTP server (your web program is a bit like a Gtk application, so things inside HTTP server are similar to things inside Xorg clients!), it has effect in the client's browser only when the HTTP reply has been sent (and recieved and processed by the browser). So the web browser is a bit like the Xorg server, and the web application (CGI, PHP, FastCGI, talking to the HTTP server) is a bit like the GTK application. Roles are reversed because for X11 protocol the server is near of the user, and for HTTP protocol the client (i.e. the browser) is near of the user.

NB. For performance reasons, X11 applications rarely call XFlush outside of their main loop.

_________________
Basile Starynkevitch (France)
http://starynkevitch.net/Basile/


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 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