Show dialog after hide another

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Show dialog after hide another

Rúben Rodrigues
Hi guys,


I have i problem when trying to hide a dialog and show another at the
same moment.

I have a dialog that have an OK button that calls this function:

void on_button_Util_Firmware_Update_clicked(GtkButton *button, gpointer
user_data)
{
     GtkLabel    *gLabel_Result;
     FILE        *fp;
     gchar        pcTmp[BUFSIZ];

     // Fecha a janela.
gtk_widget_hide(GTK_WIDGET(gtk_widget_get_toplevel(GTK_WIDGET(button))));

     // Mostra a janela de espera.
     gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder,
"Setup_Wait_dialog")));
     sleep(10);

  //**Running something inside

// Esconde a janela de espera.
     gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder,
"Setup_Wait_dialog")));

}

As you can see this is a Waiting dialog to show the status of the
process, but the problem is  that don't show the Setup_Wait_dialog and
don't hides the first dialog. What's wrong?

Thanks


---
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Show dialog after hide another

Nicola Fontana-3
Il Wed, 6 Sep 2017 16:08:07 +0000 Rúben Rodrigues <[hidden email]> scrisse:

> ...
> void on_button_Util_Firmware_Update_clicked(GtkButton *button, gpointer
> user_data)
> {
>      GtkLabel    *gLabel_Result;
>      FILE        *fp;
>      gchar        pcTmp[BUFSIZ];
>
>      // Fecha a janela.
> gtk_widget_hide(GTK_WIDGET(gtk_widget_get_toplevel(GTK_WIDGET(button))));
>
>      // Mostra a janela de espera.
>      gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder,
> "Setup_Wait_dialog")));
>      sleep(10);
> ...

Hi Rúben,

this is the normal behavior in a single-threaded application: the
user interface is updated only when you release the CPU so that
the execution returns to the main loop:

https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#glib-The-Main-Event-Loop.description

The rule of thumb is: never block, i.e. never put a `sleep(10)` or
something equivalent in your code otherwise you'll get what you
requested, in that case an unresponsive interface for 10 seconds.

If you have a heavy process, fork to a working thread or decompose
it in a loop that can be run incrementally with an idle callback.

Ciao.
--
Nicola
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Show dialog after hide another

Rúben Rodrigues
In reply to this post by Rúben Rodrigues
Please!!

Help..


Às 17:08 de 06/09/2017, Rúben Rodrigues escreveu:

> Hi guys,
>
>
> I have i problem when trying to hide a dialog and show another at the
> same moment.
>
> I have a dialog that have an OK button that calls this function:
>
> void on_button_Util_Firmware_Update_clicked(GtkButton *button, gpointer
> user_data)
> {
>       GtkLabel    *gLabel_Result;
>       FILE        *fp;
>       gchar        pcTmp[BUFSIZ];
>
>       // Fecha a janela.
> gtk_widget_hide(GTK_WIDGET(gtk_widget_get_toplevel(GTK_WIDGET(button))));
>
>       // Mostra a janela de espera.
>       gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder,
> "Setup_Wait_dialog")));
>       sleep(10);
>
>    //**Running something inside
>
> // Esconde a janela de espera.
>       gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder,
> "Setup_Wait_dialog")));
>
> }
>
> As you can see this is a Waiting dialog to show the status of the
> process, but the problem is  that don't show the Setup_Wait_dialog and
> don't hides the first dialog. What's wrong?
>
> Thanks
>
>
> ---
> Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
> https://www.avast.com/antivirus
>
> _______________________________________________
> gtk-app-devel-list mailing list
> [hidden email]
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Show dialog after hide another

Stefan Salewski-2
On Fri, 2017-09-08 at 11:11 +0000, Rúben Rodrigues wrote:
> Please!!
>
> Help..

Well, I guess what you want is displaying a message dialog without
prior user actions.

Maybe you can emit a signal to do that? I don't know, have never needed
that.

Maybe you can just modify your first dialog, display another text?

Or, maybe what you really want is something like the GtkAssistant, I
think that one is something that guides a user through a longer
sequence of actions.

Finally, you misses to tell us if you are using mainline GTK3 or a
different version, which OS, and what your great FOSS application on
which you are working is.


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Show dialog after hide another

Gtk+ - Apps Dev mailing list
In reply to this post by Rúben Rodrigues

Hi Ruben,

You can give the following a try and see if it helps. Basically you don't want to bind up the "main" thread with a long running function and freeze your UI.

Eric

/*
  gcc -Wall firmware1.c -o firmware1 `pkg-config --cflags --libs gtk+-3.0`
  Tested on Ubuntu16.04 and GTK3.18
*/

#include<gtk/gtk.h>

/*
  Set some numbers for work to be done in the worker thread. Might need to change
  these for a faster cpu but be careful of guint overflow.
*/
#define SET1 1000000000
#define SET2 100000000

static guint timeout_id=0;
static gint thread_active=0;
static gint load_progress=0;
static GThread *thread=NULL;

static void start_thread(GtkWidget *widget, GtkWidget **widgets);
static gpointer thread_get_firmware(GtkWidget **widgets);
static gboolean thread_join(GtkWidget **widgets);
static gboolean check_progress(GtkWidget *progress);
static void show_dialog(GtkWidget *widget, GtkWidget *window);

int main(int argc, char *argv[])
  {
    gtk_init(&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Show Progress");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 100);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *progress=gtk_progress_bar_new();
    gtk_widget_set_hexpand(progress, TRUE);
    gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(progress), TRUE);

    GtkWidget *button1=gtk_button_new_with_label("Show Firmware Progress");
    gtk_widget_set_hexpand(button1, TRUE);
    //Some widgets to pass through the thread function to g_idle_add().
    GtkWidget *widgets[]={progress, button1};
    g_signal_connect(button1, "clicked", G_CALLBACK(start_thread), widgets);

    GtkWidget *button2=gtk_button_new_with_label("Show a Dialog");
    gtk_widget_set_hexpand(button2, TRUE);
    g_signal_connect(button2, "clicked", G_CALLBACK(show_dialog), window);

    GtkWidget *grid=gtk_grid_new();
    gtk_grid_attach(GTK_GRID(grid), button1, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), progress, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button2, 0, 2, 1, 1);
    gtk_container_add(GTK_CONTAINER(window), grid);
   
    gtk_widget_show_all(window);
   
    gtk_main();
    return 0;  
  }
static void start_thread(GtkWidget *widget, GtkWidget **widgets)
  {  
    gtk_widget_set_sensitive(widget, FALSE);
    g_atomic_int_set(&thread_active, 1);
   
    thread=g_thread_new("TestThread", (GThreadFunc)thread_get_firmware, widgets);  
    timeout_id=g_timeout_add(500, (GSourceFunc)check_progress, widgets[0]);    
  }
static gpointer thread_get_firmware(GtkWidget **widgets)
  {
    //No GTK functions called in thread_get_firmware().
    guint32 i=0;
    gdouble percent=0;    
   
    for(i=0;i<SET1;i++)
      {
        if(i%SET2==0)
          {
            percent=(gdouble)(i+1)/SET1;
            g_atomic_int_set(&load_progress, (gint)(percent*100));
          }
      }
   
    g_atomic_int_set(&thread_active, 0);
   
    g_idle_add((GSourceFunc)thread_join, widgets);
    return NULL;    
  }
static gboolean thread_join(GtkWidget **widgets)
  {
    if(thread!=NULL)
      {
         g_print("Thread Join\n");
         g_thread_join(thread);
         thread=NULL;
       }

    //Update widgets.
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets[0]), 0);
    gchar *string=g_strdup_printf("progress %i%%", 0);
    gtk_progress_bar_set_text(GTK_PROGRESS_BAR(widgets[0]), string);
    g_free(string);
    gtk_widget_set_sensitive(widgets[1], TRUE);
    g_atomic_int_set(&load_progress, 0);

    return FALSE;
  }
static gboolean check_progress(GtkWidget *progress)
  {
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), (gdouble)(g_atomic_int_get(&load_progress)/100.0));
    gchar *string=g_strdup_printf("progress %i%%", g_atomic_int_get(&load_progress));
    gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), string);
    g_free(string);
 
    if(g_atomic_int_get(&thread_active)==1)
      {
        return TRUE;
      }
    else
      {
        timeout_id=0;
        return FALSE;
      }
  }
static void show_dialog(GtkWidget *widget, GtkWidget *window)
  {
    GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Message Dialog");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
  }
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list