GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem to open a new window

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
thunar
Familiar Face


Joined: 15 Feb 2007
Posts: 6

PostPosted: Thu Feb 15, 2007 9:42 pm    Post subject: problem to open a new window Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
   
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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Fri Feb 16, 2007 12:33 am    Post subject: Reply with quote

You just have to call gtk_widget_show_all() on the widget.
Back to top
thunar
Familiar Face


Joined: 15 Feb 2007
Posts: 6

PostPosted: Fri Feb 16, 2007 10:30 pm    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Sat Feb 17, 2007 12:20 am    Post subject: Reply with quote

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: (Plaintext)
1
2
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.
Back to top
thunar
Familiar Face


Joined: 15 Feb 2007
Posts: 6

PostPosted: Mon Feb 19, 2007 10:10 pm    Post subject: Reply with quote

Thank you very much for your answer.
The window now appears, if I use
Code: (Plaintext)
1
2
3
4

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

<?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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#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: (Plaintext)
1
2
3

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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Mon Feb 19, 2007 11:12 pm    Post subject: Reply with quote

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.
Back to top
thunar
Familiar Face


Joined: 15 Feb 2007
Posts: 6

PostPosted: Tue Feb 20, 2007 12:56 pm    Post subject: Reply with quote

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.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Tue Feb 20, 2007 1:12 pm    Post subject: Reply with quote

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/glib/glib-Spawning-Processes.html
Back to top
thunar
Familiar Face


Joined: 15 Feb 2007
Posts: 6

PostPosted: Tue Feb 20, 2007 4:15 pm    Post subject: Reply with quote

I replaced all my system() calls with g_spawn_command_line_sync(), and now it works :P :D

Thank you very much.
Back to top
thunar
Familiar Face


Joined: 15 Feb 2007
Posts: 6

PostPosted: Fri Feb 23, 2007 2:53 pm    Post subject: Reply with quote

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP