I'm starting to experiment with NiGui. I'm trying to create "widgets" that
maintain some state and that can communicate events between the child widgets
they contain. I also want to modularise the code into separate functions and
objects. Here's my first hack on your example 10 (see also a question in the
code & notes after the code):
{.experimental: "codeReordering".}
import nigui
type
MainWindow = object
window: Window
statusLabel: Label
CustomControl = object
control: Control
proc main(): void =
app.init()
var mainWindow = newMainWindow("Test #2")
mainWindow.statusLabel.text = "Click..."
mainWindow.window.show()
app.run()
proc newMainWindow(title: string): MainWindow =
result.window = newWindow(title)
result.window.width = 500
result.window.height = 500
var vbox = newLayoutContainer(LayoutVertical)
result.window.add(vbox)
result.statusLabel = newLabel()
vbox.add(result.statusLabel)
# pass in the label so that the new control can "talk: to it: is there
a better way?
var control = newCustomControl(result.statusLabel)
control.control.width = 400
control.control.height = 400
vbox.add(control.control)
control.control.widthMode = WidthMode_Fill
control.control.heightMode = HeightMode_Fill
proc newCustomControl(label: Label): CustomControl =
result.control = newControl()
result.control.onDraw = proc (event: DrawEvent) =
let canvas = event.control.canvas
canvas.areaColor = rgb(30, 30, 30) # dark grey
canvas.fill()
canvas.setPixel(0, 0, rgb(255, 0, 0))
canvas.areaColor = rgb(255, 0, 0) # red
canvas.drawRectArea(10, 10, 30, 30)
canvas.lineColor = rgb(255, 0, 0) # red
canvas.drawLine(60, 10, 110, 40)
let text = "Hello World!"
canvas.textColor = rgb(0, 255, 0) # lime
canvas.fontSize = 20
canvas.fontFamily = "Arial"
canvas.drawText(text, 10, 70)
canvas.drawRectOutline(10, 70, canvas.getTextWidth(text),
canvas.getTextLineHeight())
result.control.onMouseButtonDown = proc (event: MouseEvent) =
label.text = $event.button & " (" & $event.x & ", " & $event.y & ")"
# Shows where the mouse is clicked in control-relative coordinates
main()
Run
You'll notice that I had to drop the images since I couldn't get them to work
(even though they work fine in your original example). Oh, and this runs fine
on Windows & Linux:-)
PS I use 4 spaces for indents because I read years ago in _Code Complete_ that
comparative studies had shown that a minimum of 3 is needed for clarity, and
I'm used to 4 from Python;-}