Sign up

Application / ApplicationWindow

The Hello World example at http://www.dsource.org/projects/gtkd/wiki/CodeExamples is for the old style of working with Gtk: Main.init…Main.run. New style is to use Application and ApplicationWindow. Are there any examples of use that are known to work?

(I am having difficulty extrapolating from the source any sane code that compiles…)

Re: Application / ApplicationWindow

OK, so given the working C++ code:

#include <gtkmm/application.h>
#include <gtkmm/applicationwindow.h>
#include <gtkmm/label.h>

class HelloWorldWindow: public Gtk::ApplicationWindow {
 public:
  HelloWorldWindow()
    : hello_world("Hello World") {
    set_title("Hello World");
    add(hello_world);
    show_all_children();
  }
 private:
  Gtk::Label hello_world;
};

int main(int argc, char * argv[]) {
  auto application = Gtk::Application::create(argc, argv);
  HelloWorldWindow helloWorld;
  return application->run(helloWorld);
}

by a process of trial and (much) error, I came up with the D code:

import gtk.Application: Application;
import gtk.ApplicationWindow: ApplicationWindow;
import gtk.Label: Label;
import gtkc.giotypes: GApplicationFlags;

class HelloWorld: ApplicationWindow {
  this(Application application) {
    super(application);
    setTitle("Hello World.");
    add(new Label("Hello World"));
    showAll();
  }
}

int main(string[] args) {
  auto application = new Application("uk.org.winder.gtkd.helloworld", GApplicationFlags.NONE);
  auto helloWorld = new HelloWorld(application);
  return application.run(args);
}

which compiles with dmd 2.065 and gdc 4.9.1, but highlights a deep problem with ldc2 :-( However when the dmd compiled version is executed, I get a segmentation fault. Using gdb I get the stack trace:

#0  0x00007ffff6a80220 in ?? () from /usr/lib/x86_64-linux-gnu/libgtk-3.so.0
#1  0x00007ffff6a7de2b in ?? () from /usr/lib/x86_64-linux-gnu/libgtk-3.so.0
#2  0x00007ffff3cf56eb in g_cclosure_marshal_VOID__OBJECTv ()
   from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#3  0x00007ffff3cf2644 in ?? () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#4  0x00007ffff3d0cb07 in g_signal_emit_valist () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#5  0x00007ffff3d0d46f in g_signal_emit () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#6  0x00007ffff6cc5f52 in gtk_window_set_application () from /usr/lib/x86_64-linux-gnu/libgtk-3.so.0
#7  0x00007ffff3cf7cfd in ?? () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#8  0x00007ffff3cf9ab5 in g_object_new_valist () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#9  0x00007ffff3cf9df1 in g_object_new () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#10 0x00000000006b1ff9 in gtk.ApplicationWindow.ApplicationWindow.__ctor() ()
#11 0x0000000000696cb9 in helloWorld_d_gtkd.HelloWorld.__ctor() ()
#12 0x0000000000696d95 in D main ()
#13 0x00000000008575e8 in rt.dmain2._d_run_main() ()
#14 0x0000000000857542 in rt.dmain2._d_run_main() ()
#15 0x00000000008575a8 in rt.dmain2._d_run_main() ()
#16 0x0000000000857542 in rt.dmain2._d_run_main() ()
#17 0x00000000008574c3 in _d_run_main ()
#18 0x0000000000696dc7 in main ()

Hopefully I am doing something trivially silly. (Though segmentation fault isn't a good result of just being silly.)

Re: Application / ApplicationWindow

On 08/26/2014 08:50 PM, Russel Winder wrote:

Hopefully I am doing something trivially silly. (Though segmentation fault isn't a good result of just being silly.)

I'll have to do some research on this one.

Re: Application / ApplicationWindow

[…]

which compiles with dmd 2.065 and gdc 4.9.1, but highlights a deep problem with ldc2 :-( However […]

For the record, ldc2 has been fixed and now the code compiles with it.

Re: Application / ApplicationWindow

On Tue, 26 Aug 2014 23:21:06 +0200, Mike Wey wrote:
[…]

I'll have to do some research on this one.

Hopefully you have enough data to move on this. If not, let me know what experimentation I need to do to be able to provide data as needed.

Re: Application / ApplicationWindow

On 08/28/2014 06:50 PM, Russel Winder wrote:> On Tue, 26 Aug 2014 23:21:06 +0200, Mike Wey wrote:

[…]

I'll have to do some research on this one.

Hopefully you have enough data to move on this. If not, let me know what experimentation I need to do to be able to provide data as needed.

It appears an ApplicationWindow can only be added to an Application after the activate signal has been emitted.
Otherwise it will crash because the Application hasn't been registered yet.

The following code works:

import gtk.Application: Application;
import gio.Application: GioApplication = Application;
import gtk.ApplicationWindow: ApplicationWindow;
import gtk.Label: Label;
import gtkc.giotypes: GApplicationFlags;

class HelloWorld: ApplicationWindow {
  this(Application application) {
    super(application);
    setTitle("Hello World.");
    add(new Label("Hello World"));
    showAll();
  }
}

int main(string[] args) {
  auto application = new Application("uk.org.winder.gtkd.helloworld", GApplicationFlags.NONE);
  application.addOnActivate(delegate void(GioApplication app){ new HelloWorld(application); });
  return application.run(args);
}

We could use the same overload of run the gtkmm provides, so this whould be taken care of for you.

Re: Application / ApplicationWindow

On Thu, 28 Aug 2014 20:59:27 GMT, Mike Wey wrote:
[…]

It appears an ApplicationWindow can only be added to an Application after the activate signal has been emitted.
Otherwise it will crash because the Application hasn't been registered yet.

[…]

We could use the same overload of run the gtkmm provides, so this would be taken care of for you.

Thanks for sorting out my problem :-)

If gtkmm gets round it, then I see no reason why this shouldn't be the default behaviour for GtkD.