
{ "name": "hellogtkd", "targetPath": "bin", "targetType": "executable", "dependencies": { "gtk-d": "~>3.1.3" } }  import gtk.Main; import gtk.MainWindow; import gtk.Label; class HelloWorld : MainWindow { this() { super( "wintitle" ); setBorderWidth(20); //      add( new Label( "hello habr!" ) ); showAll(); } } void main( string[] args ) { Main.init( args ); new HelloWorld(); Main.run(); }  dub build && bin/hellogtkd 

 import std.string : format; import gtk.Main; import gtk.Builder; import gtk.Window; import gtk.Widget; import std.format; final class UI { string glade_file; Builder builder; //        this( string file ) { builder = new Builder; if( !builder.addFromFile( file ) ) except( "could no load glade object from file '%s'", file ); glade_file = file; prepare(); } void prepare() { prepareMainWindow(); } void prepareMainWindow() { auto w = obj!Window( "mwindow" ); //   Window   w.setTitle( "glade ui" ); w.addOnHide( (Widget aux){ Main.quit(); } ); //        w.showAll(); //    } //            auto obj(T)( string name ) { //  getObject      ObjectG,       auto ret = cast(T)builder.getObject( name ); //        if( ret is null ) except( "no '%s' element in file '%s'", name, glade_file ); return ret; } void except( string file=__FILE__, size_t line=__LINE__, Args...)( Args args ) { throw new Exception( format( args ), file, line ); } } void main( string[] args ) { Main.init( args ); new UI( "ui.glade" ); //     ,       ,      Main.run(); }  ... void prepare() { prepareMainWindow(); prepareButtonAction(); } ... void prepareButtonAction() { //    ,      Clicked obj!Button( "btn" ).addOnClicked( (Button aux) { obj!DrawingArea( "plot" ).queueDraw(); //    plot }); }  module draw; import std.stdio; import std.datetime : Clock; import gtk.Widget; import cairo.Context; class Figure { bool draw( Scoped!Context cr, Widget aux ) { writefln( "draw figure %012d", Clock.currAppTick().length ); //   true,       //  ,   false,   //       //     ,  bool delegate(...) return false; } }  ... void prepare() { prepareMainWindow(); prepareButtonAction(); prepareDrawing(); } ... Figure fig; void prepareDrawing() { fig = new Figure; //         ,    obj!DrawingArea( "plot" ).addOnDraw( &fig.draw ); }  ... draw figure 014855276247 draw figure 014872180248 draw figure 014889286316 ...  ... float angle = 0; bool draw( Scoped!Context cr, Widget aux ) { writefln( "draw figure %012d", Clock.currAppTick().length ); import std.math; auto w = aux.getAllocatedWidth(); auto h = aux.getAllocatedHeight(); auto xc = w / 2.0; auto yc = h / 2.0; auto radius = fmin(w,h) / 2.0; auto x1 = cos( angle ) * radius + xc; auto y1 = sin( angle ) * radius + yc; auto x2 = cos( angle + PI / 3 * 2 ) * radius + xc; auto y2 = sin( angle + PI / 3 * 2 ) * radius + yc; auto x3 = cos( angle + PI / 3 * 4 ) * radius + xc; auto y3 = sin( angle + PI / 3 * 4 ) * radius + yc; cr.setSourceRgb( 1.0, 0.0, 0.0 ); cr.moveTo( x1, y1 ); cr.lineTo( x2, y2 ); cr.lineTo( x3, y3 ); cr.closePath(); cr.fill(); //   true,       //  ,   false,   //       //     ,  bool delegate(...) return false; } ...  ... void prepareButtonAction() { obj!Button( "btn" ).addOnClicked( (Button aux) { fig.angle += 0.1; //    obj!DrawingArea( "plot" ).queueDraw(); }); } ... 
Source: https://habr.com/ru/post/259717/
All Articles