I want to subclass "window" and call the initialiser. This is in python but the idea is clear,
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
print("Hello World")
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
So i need
# nim c button.nim
import gintro/[gtk, glib, gobject, gio]
proc buttonClicked (button: Button) =
button.label = utf8Strreverse(button.label, -1)
proc appActivate (app: Application) =
let window = newApplicationWindow(app)
window.title = "GNOME Button"
window.defaultSize = (250, 50)
let button = newButton("Click Me")
window.add(button)
button.connect("clicked", buttonClicked)
window.showAll
proc main =
let app = newApplication("org.gtk.example")
connect(app, "activate", appActivate)
discard app.run
main()
But in object oriented way, not in functional waySubclassing is explained in
http://ssalewski.de/gintroreadme.html#_extending_or_sub_classing_widgets
I know that this topic is missing from the GTK4 book still, but I generally assume that people read the README first.
Hmm, indeed. What I currently try and does not work :
import gintro/[gtk, gobject, gio]
when defined(gcDestructors):
proc `=destroy`(x: var typeof(CountButton()[])) =
gtk.`=destroy`(typeof(Button()[])(x))
type
CountButton = ref object of Button
counter: int
proc buttonClicked (button: CountButton; decrement: int) =
dec(button.counter, decrement)
button.label = "Counter: " & $button.counter
echo "Counter is now: ", button.counter
type
MyApplication = ref object of Application
method appActivate (this: Application) =
this.title = "Count Button"
#initButton(button, "Counting down from 100 by 5") # deprecated
let button = newButton(CountButton, "Counting down from 100 by 5")
button.counter = 100
this.add(button)
button.connect("clicked", buttonClicked, 5)
this.showAll
proc main =
let app = newApplication(MyApplication)
discard app.run
main()
It looks good to me but not for the compilerIt looks good to me but not for the compiler
Looks very wrong for me. Please follow the provided examples.
We generally do not use methods, we do not need the destructor with recent Nim, and you have to call "let window = newApplicationWindow(app)".
Is started going to fix your example, but then I discovered that it is unfixable.