This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomWidgetBindingExample.kt
100 lines (83 loc) · 2.92 KB
/
CustomWidgetBindingExample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package customwidgetbinding
import bindings.gtk.*
import bindings.gtk.usertypes.WidgetClass
import bindings.gtk.usertypes.WidgetCompanion
import kotlinx.cinterop.CPointer
import native.gobject.G_BINDING_SYNC_CREATE
import native.gobject.G_PARAM_READWRITE
import native.gtk.GtkAlign.GTK_ALIGN_CENTER
import native.gtk.GtkOrientation.GTK_ORIENTATION_HORIZONTAL
import native.gtk.GtkOrientation.GTK_ORIENTATION_VERTICAL
import usertypes.intProperty
/**
* An example showing how to create a Custom Widget with actions and properties
* and bind the properties to other widgets.
*/
fun main() {
val app = Application("io.quantus.gtk-kotlin-native.example.customwidgetbinding")
app.onActivate {
val window = ApplicationWindow(app)
window.title = "Custom Widget Example"
window.defaultSize = Pair(600, 400)
buildUI(window)
window.show()
}
app.run()
app.unref()
}
private fun buildUI(window: Window) {
// create the widgets
val counterWidget = CounterWidget()
val label = Label()
// bind the "label" property of label to the "value" property of counterWidget
counterWidget.bindProperty("value", label, "label", G_BINDING_SYNC_CREATE)
// add the widgets to the layout
window.child = Box(GTK_ORIENTATION_HORIZONTAL, 20).apply {
append(counterWidget)
append(label)
homogeneous = true
}
}
/**
* A counter widget that has a [value] property and two buttons for incrementing and decrementing
* the value.
*/
private class CounterWidget : Box {
var value: Int by VALUE_PROPERTY
private val incrementButton = Button("Increment").apply { actionName = "counter.increment" }
private val decrementButton = Button("Decrement").apply { actionName = "counter.decrement" }
constructor(pointer: CPointer<*>) : super(pointer)
constructor() : this(newInstancePointer()) {
// configure the box
orientation = GTK_ORIENTATION_VERTICAL
spacing = 20
valign = GTK_ALIGN_CENTER
append(incrementButton)
append(decrementButton)
}
private fun increment() {
value++
}
private fun decrement() {
value--
}
companion object : WidgetCompanion<CounterWidget>() {
override val typeName = "CounterWidget"
override val parentType = Box.Type
private val VALUE_PROPERTY = intProperty(
CounterWidget::value,
name = "value",
nick = "value",
blurb = "The value of this counter widget",
minimum = 0,
maximum = Int.MAX_VALUE,
defaultValue = 0,
flags = G_PARAM_READWRITE
)
override fun classInit(klass: WidgetClass<CounterWidget>) {
klass.installAction("counter.increment", CounterWidget::increment)
klass.installAction("counter.decrement", CounterWidget::decrement)
klass.installProperty(VALUE_PROPERTY)
}
}
}