GTK3 and Glade3 using Kotlin

Submitted by Dickens A S on Sat, 11/30/2019 - 14:13

GTK windows design using glade builder

This code explains how to use Kotlin to program GTK3 and Glade3 using Cygwin Glade designer

Install cygwin with below dependencies

mingw64-x86_64-gtk3    3.22.28-1
glade                                 3.20.3-1
xorg-server                       1.20.4-1
xinit                                   1.4.1-1

launch cygwin and export the PATH as

export PATH=$PATH:/usr/x86_64-w64-mingw32/sys-root/mingw/bin

The execute the gradle command to run the app, refer github for gradle command

Below is the Kotlin code

package plot

import kotlinx.cinterop.*
import gtk3.*


fun main() = memScoped {
    var argc = alloc<IntVar>().apply { this.value = 0 }
    gtk_init(argc.ptr, null)
    
    var builder = gtk_builder_new()
    gtk_builder_add_from_file (builder, "glade/window_main.glade", null)
    
    var window = gtk_builder_get_object(builder, "window_main")!!.reinterpret<GtkWidget>()
    
    g_signal_connect_data(
        window.reinterpret(), 
        "destroy", 
        staticCFunction {  
            -> gtk_main_quit()
        }.reinterpret(), 
        null, 
        null, 
        0u
    )
    
    g_object_unref(builder)

    gtk_widget_show(window)
    gtk_main()
}

Example window output

GT3

You can modify the glade file and add components accordingly

You can bind event as specified in the code

    g_signal_connect_data(
        window.reinterpret(), 
        "destroy", 
        staticCFunction {  
            -> gtk_main_quit()
        }.reinterpret(), 
        null, 
        null, 
        0u
    )

Source Code

End Of Article !

Add new comment