GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Apr 18, 2014 1:10 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: problem to open a new window
PostPosted: Thu Feb 15, 2007 9:42 pm 
Offline
Familiar Face

Joined: Thu Feb 15, 2007 3:49 pm
Posts: 6
Hello,
I'am new to GTK.
I would like to open a new window, when a button is clicked to show the progress to the user.
But the window apperas at the end of the whole if part. Why?
How can I show to window at the beginning of the if part?
Code:
   
if(error==0) {
  GladeXML *gxml_progress = NULL;
  gxml_progress = glade_xml_new (GLADE_FILE, "progress_window", NULL);
  GtkWidget *progress_window = glade_xml_get_widget(gxml2, "progress_window");
  do some work...
}


Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 12:33 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You just have to call gtk_widget_show_all() on the widget.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 10:30 pm 
Offline
Familiar Face

Joined: Thu Feb 15, 2007 3:49 pm
Posts: 6
Thank you for your answer, but the same effect with gtk_widget_show_all().
So I will post some more code to explain the problem.

Code:
GladeXML *gxml = NULL;  //Global var, so that I have access from the other functions to gxml

int main (int argc, char *argv[]) {
GtkWidget *app_window = NULL;
gtk_init (&argc, &argv);
gxml = glade_xml_new (GLADE_FILE, "app_window", NULL);
g_assert (gxml);
app_window = glade_xml_get_widget (gxml, "app_window");
glade_xml_signal_connect_data (gxml, "on_button_go_clicked",
                     G_CALLBACK (on_button_go_clicked), NULL);
g_signal_connect(G_OBJECT(app_window), "delete_event", G_CALLBACK(delete_event_cb), NULL);         
g_signal_connect(G_OBJECT(app_window), "destroy", G_CALLBACK(destroy_cb), NULL);
gtk_main ();
return 0;
}

void some_other_functions() {

}

void on_button_go_clicked() {
//check if all things are ok, etc.
...
if(error==0) { //start with the task, when no error occured
  GladeXML *gxml_progress = NULL;
  gxml_progress = glade_xml_new (GLADE_FILE, "progress_window", NULL);   //should show the window now
  GtkWidget *progress_window = glade_xml_get_widget(gxml_progress, "progress_window");
  //gtk_widget_show_all(progress_window); //no effect with show_all

  ..do some work...
  sprintf(...);
  system(...);
  g_print("");
  ...and a lot more...
}  //end from if part, the progress window now appears, but should appear at the beginning from the if block
} //end function


thank you


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 17, 2007 12:20 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Ok. So, I put together a short example that was this exact thing and it was displayed for me. What doesn't make sense is that the dialog is visible at all if you don't set it that way.

There is one thing that you could try. For your progress window, go to the properties window in Glade and go to the "Common" tab. Under there, set "Visible" to Yes. This should force it to be visible initially. Also, you could put this loop right after you call gtk_widget_show_all():

Code:
while (gtk_events_pending())
  gtk_main_iteration();


This will process all pending events after you show the window before continuing with the rest of the code. It is commonly used to make sure that your user interface continues to update while something processor-intensive is done.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 10:10 pm 
Offline
Familiar Face

Joined: Thu Feb 15, 2007 3:49 pm
Posts: 6
Thank you very much for your answer.
The window now appears, if I use
Code:
while (gtk_events_pending())
  gtk_main_iteration();

but not the other widgets (labels, progress bar). Even if I use gtk_widget_show(label) or show_all.

So I put together this example:

sample.glade
Code:
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="border_width">10</property>
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">False</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkButton" id="button1">
      <property name="width_request">150</property>
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="label" translatable="yes">Run</property>
      <property name="use_underline">True</property>
      <property name="relief">GTK_RELIEF_NORMAL</property>
      <property name="focus_on_click">True</property>
      <signal name="clicked" handler="on_button1_clicked" last_modification_time="Mon, 19 Feb 2007 20:34:21 GMT"/>
    </widget>
  </child>
</widget>

<widget class="GtkWindow" id="window2">
  <property name="border_width">10</property>
  <property name="visible">True</property>
  <property name="title" translatable="yes">window2</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">False</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
   <widget class="GtkLabel" id="label1">
     <property name="visible">True</property>
     <property name="label" translatable="yes">Progress window</property>
     <property name="use_underline">False</property>
     <property name="use_markup">False</property>
     <property name="justify">GTK_JUSTIFY_LEFT</property>
     <property name="wrap">False</property>
     <property name="selectable">False</property>
     <property name="xalign">0.5</property>
     <property name="yalign">0.5</property>
     <property name="xpad">0</property>
     <property name="ypad">0</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
     <property name="width_chars">-1</property>
     <property name="single_line_mode">False</property>
     <property name="angle">0</property>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>

      <child>
   <widget class="GtkProgressBar" id="progressbar1">
     <property name="visible">True</property>
     <property name="orientation">GTK_PROGRESS_LEFT_TO_RIGHT</property>
     <property name="fraction">0</property>
     <property name="pulse_step">0.10000000149</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>

      <child>
   <widget class="GtkLabel" id="label2">
     <property name="visible">True</property>
     <property name="label" translatable="yes">progress_description</property>
     <property name="use_underline">False</property>
     <property name="use_markup">False</property>
     <property name="justify">GTK_JUSTIFY_LEFT</property>
     <property name="wrap">False</property>
     <property name="selectable">False</property>
     <property name="xalign">0.5</property>
     <property name="yalign">0.5</property>
     <property name="xpad">0</property>
     <property name="ypad">0</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
     <property name="width_chars">-1</property>
     <property name="single_line_mode">False</property>
     <property name="angle">0</property>
   </widget>
   <packing>
     <property name="padding">2</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>


sample.c
Code:
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <stdlib.h>

#define GLADE_FILE "sample.glade"

// functions
static gint delete_event_cb(GtkWidget* w, GdkEventAny* e, gpointer data);
static gint destroy_cb(GtkWidget* w, GdkEventAny* e, gpointer data);
static void on_button1_clicked(GtkButton *button, gpointer data);

int main (int argc, char *argv[])
{
        GladeXML *gxml;

        gtk_init (&argc, &argv);

   //Create interface
        gxml = glade_xml_new (GLADE_FILE, "window1", NULL);
       
   GtkWidget *window1 = glade_xml_get_widget(gxml, "window1");     

   //connect signals
   glade_xml_signal_connect_data (gxml, "on_button1_clicked",
           G_CALLBACK (on_button1_clicked), NULL);
         
        g_signal_connect(G_OBJECT(window1), "delete_event",
                G_CALLBACK(delete_event_cb), NULL);
               
        g_signal_connect(G_OBJECT(window1), "destroy",
                G_CALLBACK(destroy_cb), NULL);
       
   //show the window
        //gtk_widget_show (window1);

   //beginn loop
        gtk_main ();
       
        return 0;
}

static gint delete_event_cb(GtkWidget* w, GdkEventAny* e, gpointer data)
{   
        return 0;
}

static gint destroy_cb(GtkWidget* w, GdkEventAny* e, gpointer data)
{
        gtk_main_quit();
        return 0;
}

void
on_button1_clicked(GtkButton *button, gpointer data)
{
        /* the button was clicked */
   //Print out to console
   g_print("Beginn break\n");

   //Create the new "progress" window
       GladeXML        *gxml_progress = NULL;
   gxml_progress = glade_xml_new (GLADE_FILE, "window2", NULL);

   //show the window
   GtkWidget *window2 = glade_xml_get_widget(gxml_progress, "window2");
   gtk_widget_show_all(window2);

   //show the label
   //GtkWidget *label = glade_xml_get_widget(gxml_progress, "label1");
   //gtk_widget_show(label);

   while (gtk_events_pending())
     gtk_main_iteration();   

   //Make 5 sec. break
   system("sleep 5");
   g_print("End break\n");
}


I compile it with:
Code:
gcc -Wall -export-dynamic -g `pkg-config --cflags --libs gtk+-2.0` `pkg-config --cflags --libs libglade-2.0` -o sample sample.c


If I run the programm without gtk_events_pending... Then the window2 appears at the end from the 5 seconds. If I use it, then it appears at the beginning, but without the labels etc.
Or should I use something diffrent to open a new window?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 11:12 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You should use g_usleep(5000); to sleep for 5 seconds. The system() call interupts the application, but g_usleep() won't go until everything else in the application is processed.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 12:56 pm 
Offline
Familiar Face

Joined: Thu Feb 15, 2007 3:49 pm
Posts: 6
I quickly didn't found anything to make a break. But system("sleep 5") was more a placeholder, for the work I do in the apllication. There I make some system("pdftk ...) etc. But the problem I want to show with this example above, is when the new window appears and when not. So I needed the break to see the difference when I use gtk_events_pending or something else.
But it doesn't work as I expectet.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 1:12 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Well, my advice would be to stay away from calls to system() and stay with GLib functions. They tend to block the main loop haphazardly. If you need to run an application on the user's system, use the g_spawn_*() family functions found at:

http://developer.gnome.org/doc/API/2.0/ ... esses.html

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 4:15 pm 
Offline
Familiar Face

Joined: Thu Feb 15, 2007 3:49 pm
Posts: 6
I replaced all my system() calls with g_spawn_command_line_sync(), and now it works :P :D

Thank you very much.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 2:53 pm 
Offline
Familiar Face

Joined: Thu Feb 15, 2007 3:49 pm
Posts: 6
I thought it works, but now I realized, it doesn't work. :( It worked on a Intel Pentium D 3.4 Ghz machine at work, but not on a Pentium 4 2.6Ghz machine.
So I took the example above and replaced the system(sleep 5) with g_usleep(5000000), and tried it again but the window always appears at the end of the break but it should appear at the beginning. What should I do to open the window properly?
With gtk_events_pending... it shows only the border from the window but not the labels etc.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group