Demo entry 6770309

Basic GTK 3 app using Kotlin Native

   

Submitted by anonymous on Nov 13, 2018 at 03:05
Language: Kotlin. Code size: 1.9 kB.

package org.example.gtktest

import gtk3.*
import kotlinx.cinterop.CFunction
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.staticCFunction

private fun <F : CFunction<*>> connectGtkSignal(
    obj: CPointer<*>,
    actionName: String,
    action: CPointer<F>,
    data: gpointer? = null,
    connectFlags: GConnectFlags = 0u
) {
    g_signal_connect_data(
        instance = obj.reinterpret(),
        detailed_signal = actionName,
        c_handler = action.reinterpret(),
        data = data,
        destroy_data = null,
        connect_flags = connectFlags
    )
}


private fun btnClicked(@Suppress("UNUSED_PARAMETER") widget: CPointer<GtkWidget>, data: gpointer) {
    g_print("Hello World! :)\n", null)
    g_print("Exiting...\n", null)
    gtk_widget_destroy(data.reinterpret())
}

private fun activate(app: CPointer<GtkApplication>, @Suppress("UNUSED_PARAMETER") userData: gpointer) {
    val win = gtk_application_window_new(app)
    val btnBox = gtk_button_box_new(GtkOrientation.GTK_ORIENTATION_HORIZONTAL)
    val btn = gtk_button_new_with_label("Hello World")

    if (btn != null) {
        connectGtkSignal(obj = btn, actionName = "clicked", action = staticCFunction(::btnClicked), data = win)
    }
    gtk_container_add(btnBox?.reinterpret(), btn)
    gtk_container_add(win?.reinterpret(), btnBox)
    gtk_window_set_title(win?.reinterpret(), "GTK Test")
    gtk_window_set_default_size(window = win?.reinterpret(), width = 200, height = 200)
    gtk_widget_show_all(win)
}

fun main() {
    val app = gtk_application_new("org.example.gtktest", G_APPLICATION_FLAGS_NONE)
    if (app != null) connectGtkSignal(obj = app, actionName = "activate", action = staticCFunction(::activate))
    val status = g_application_run(app?.reinterpret(), 0, null)
    g_object_unref(app)
    println("Application Status: $status")
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).