diff options
| author | trustable-code <krauter.simon@arcor.de> | 2019-12-10 22:25:13 +0100 |
|---|---|---|
| committer | trustable-code <krauter.simon@arcor.de> | 2019-12-10 22:25:13 +0100 |
| commit | 9c69e88cee6a857140d75494c22b623a80797b5f (patch) | |
| tree | 7352183c42e3e1008bf484948b8a1f6e3d4008d1 | |
| parent | 3841f1770ba68c7f1fea5960b5a812ac00f7e513 (diff) | |
| download | NiGui-9c69e88cee6a857140d75494c22b623a80797b5f.tar.gz NiGui-9c69e88cee6a857140d75494c22b623a80797b5f.zip | |
Add example for custom button
| -rwxr-xr-x | examples/example_30_custom_button.nim | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/example_30_custom_button.nim b/examples/example_30_custom_button.nim new file mode 100755 index 0000000..1a1bc9d --- /dev/null +++ b/examples/example_30_custom_button.nim @@ -0,0 +1,48 @@ +# This example shows how to make a custom button (subclassing).
+
+import nigui
+
+
+# Definition of a custom button
+type CustomButton = ref object of Button
+
+# Custom widgets need to draw themselves
+method handleDrawEvent*(control: CustomButton, event: DrawEvent) =
+ var button = cast[Button](event.control)
+ let canvas = event.control.canvas
+ canvas.areaColor = rgb(55, 55, 55)
+ canvas.textColor = rgb(255, 255, 255)
+ canvas.lineColor = rgb(255, 255, 255)
+ canvas.drawRectArea(0, 0, button.width, button.height)
+ canvas.drawTextCentered(button.text)
+ canvas.drawRectOutline(0, 0, button.width, button.height)
+
+# Override nigui.newButton (optional)
+proc newButton(text = ""): Button =
+ result = new CustomButton
+ result.init()
+ result.text = text
+
+
+# Main program
+
+app.init()
+
+var window = newWindow()
+
+var container = newLayoutContainer(Layout_Vertical)
+window.add(container)
+
+# Since newButton is overriden, this code is exactly the same as in a program using the native button
+var button = newButton("Button")
+container.add(button)
+
+var textArea = newTextArea()
+container.add(textArea)
+
+button.onClick = proc(event: ClickEvent) =
+ textArea.addLine("Button clicked")
+
+window.show()
+
+app.run()
|
