1 module declgtk.backend; 2 3 import declgtk.components; 4 import declgtk.queue; 5 import declgtk.util; 6 import declui.backend; 7 import declui.components.window; 8 import gtk.Application : Application, GApplicationFlags; 9 import gtk.ApplicationWindow : ApplicationWindow; 10 import gio.Application : GioApplication = Application; 11 12 import std.exception; 13 14 private static bool _started = false; 15 16 /** 17 Determines whether or not GTK has already been started. 18 Returns: `true` if GTK was started, `false` if it has not yet been started. 19 */ 20 bool isGTKStarted() 21 { 22 return _started; 23 } 24 25 /** 26 Creates GTK components. 27 */ 28 class GtkBackend : ToolkitBackend, IApplicationProxy 29 { 30 private Application _application; 31 private GioApplication _gioApplication; 32 private void delegate()[] callbacks; 33 34 /// Queues a callback to be run on the GTK event loop. 35 void queue(void delegate() callback) 36 { 37 callbacks ~= callback; 38 } 39 40 override void run(string[] args, IWindow window) 41 { 42 version(linux) 43 { 44 version (DMD) 45 { 46 import etc.linux.memoryerror : registerMemoryErrorHandler; 47 registerMemoryErrorHandler(); 48 } 49 } 50 51 GtkWindow gtkWindow = cast(GtkWindow) window.getInternal(); 52 assert(gtkWindow !is null, "window is not a GtkWindow"); 53 54 _application = new Application("dlang.declui.app", GApplicationFlags.FLAGS_NONE); 55 _application.addOnActivate((gioApp) 56 { 57 _gioApplication = gioApp; 58 gtkWindow.setApplication(this); 59 gtkWindow.visible = true; 60 executeGtkQueue(); 61 }); 62 _application.run(args); 63 } 64 65 override GtkWindow window() 66 { 67 return new GtkWindow; 68 } 69 70 override GtkLabel label() 71 { 72 return new GtkLabel; 73 } 74 75 override GtkButton button() 76 { 77 return new GtkButton; 78 } 79 80 override GtkMenuBar menubar() 81 { 82 return new GtkMenuBar; 83 } 84 85 override GtkMenu menu() 86 { 87 return new GtkMenu; 88 } 89 90 override GtkMenuButton menubutton() 91 { 92 return new GtkMenuButton; 93 } 94 95 override Application application() 96 { 97 return _application; 98 } 99 100 override GioApplication gioApplication() 101 { 102 return _gioApplication; 103 } 104 } 105 106 shared static this() 107 { 108 registerToolkit("gtk", () => new GtkBackend); 109 }