View previous topic :: View next topic |
Author |
Message |
GG
Joined: 05 Nov 2009 Posts: 18
|
Posted: Fri Dec 11, 2009 8:54 pm Post subject: Glib.Timeout and ProgressBar |
|
|
I try to use ProgressBar with Glib.Timeout in this sample code but I get a segmentation fault when enter in update_progress_bar.
I'm doing something wrong ?
Code: |
module sample;
import gtk.Window;
import gtk.Main;
import gtk.MainWindow;
import gtk.Widget;
import gtk.ProgressBar;
import glib.Timeout;
gboolean update_progress_bar(gpointer data)
{
//gtk_progress_bar_pulse(GTK_PROGRESS_BAR (data));
ProgressBar p = cast(ProgressBar)data;
p.setPulseStep(0.1);
p.pulse();
p.pulse();
p.pulse();
/* Return true so the function will be called again; returning false removes
* this timeout function.
*/
return false;
}
int main (string[] args)
{
Main.init(args);
Window w = new Window("test");
w.addOnHide(delegate void(Widget wid) {Main.exit(0);});
ProgressBar p = new ProgressBar();
p.setPulseStep(0.1);
Timeout.add(1000,cast(GSourceFunc)&update_progress_bar,p.getProgressBarStruct()); // or passing p : same problem
w.add(p);
w.showAll();
Main.run();
return 0;
}
|
Thanks ! |
|
Back to top |
|
|
Mike Wey
Joined: 07 May 2007 Posts: 428
|
Posted: Sat Dec 12, 2009 5:16 am Post subject: |
|
|
I'll test this later today,
but the update_progress_bar needs to be marked as extern(C). |
|
Back to top |
|
|
Mike Wey
Joined: 07 May 2007 Posts: 428
|
Posted: Sat Dec 12, 2009 7:13 am Post subject: |
|
|
This example is working for me:
Code: | module sample;
import gtk.Window;
import gtk.Main;
import gtk.MainWindow;
import gtk.Widget;
import gtk.ProgressBar;
import glib.Timeout;
extern(C) gboolean update_progress_bar(gpointer data)
{
//gtk_progress_bar_pulse(GTK_PROGRESS_BAR (data));
ProgressBar p = cast(ProgressBar)data;
p.setPulseStep(0.1);
p.pulse();
p.pulse();
p.pulse();
/* Return true so the function will be called again; returning false removes
* this timeout function.
*/
return true;
}
int main (string[] args)
{
Main.init(args);
Window w = new Window("test");
w.addOnHide(delegate void(Widget wid) {Main.exit(0);});
ProgressBar p = new ProgressBar();
p.setPulseStep(0.1);
Timeout.add(100,cast(GSourceFunc)&update_progress_bar,cast(void*)p);
w.add(p);
w.showAll();
Main.run();
return 0;
} |
Quote: | // or passing p : same problem |
cast it to void, or when passing the struct, use the constructor for ProgressBar to turn it back into the object. since there is already an object associated with the struct the constructor will not create a new opject but rather return the same associated object. |
|
Back to top |
|
|
GG
Joined: 05 Nov 2009 Posts: 18
|
Posted: Sat Dec 12, 2009 11:34 pm Post subject: |
|
|
I forgot extern C at the beginning of the fonction.
Do we have to use extern C because the fonction is not implemented in D ? If I want to implement in D, is there a solution ? Convert into a delegate fonction maybe?
Thanks Mike for your support, I learned a lot this week. |
|
Back to top |
|
|
Mike Wey
Joined: 07 May 2007 Posts: 428
|
Posted: Sun Dec 13, 2009 8:49 am Post subject: |
|
|
It has to be marked as extern(C) so that glib is able to call it. |
|
Back to top |
|
|
|