From 9c69e88cee6a857140d75494c22b623a80797b5f Mon Sep 17 00:00:00 2001 From: trustable-code Date: Tue, 10 Dec 2019 22:25:13 +0100 Subject: Add example for custom button --- examples/example_30_custom_button.nim | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 examples/example_30_custom_button.nim (limited to 'examples') 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() -- cgit v1.2.3