summaryrefslogtreecommitdiff
path: root/examples/example_30_custom_button.nim
blob: 1a1bc9d5813c0bcee07dd357c24743a00b567a62 (plain)
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
# 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()