Let's play with Dinfio!
This basic calculator is an example of object-oriented and GUI programming implementation. This example shows you how your class can be inherited from a GUI object.
' A Basic Calculator Example
import math, gui
start
var main: main_window
var about: about_window
var about_height = platform(196, 160, 160)
main_window = main()
stop
class main: gui_window
field hasil, angka, buffer, operator, ada_titik, state_hasil
field gui_textbox: display
field gui_button: tambah, kurang, kali, bagi, titik, tombol_angka
field gui_button: sama_dengan, plus_minus, clear, clear_all
field gui_button: info
field gui_label: label
function construct()
extend(this, gui_window("Calculator", 212, platform_mac(250, 278)))
this.hasil = ""; this.angka = ""; this.buffer = ""; this.operator = ""
this.ada_titik = false; this.state_hasil = false
this.create_gui()
stop
function create_gui()
this.tombol_angka = array(10)
this.display = gui_textbox("0", this, 8, 8, 194, 32)
this.tombol_angka[7] = gui_button("7", this, 8, 48, 34, 32)
this.tombol_angka[8] = gui_button("8", this, 48, 48, 34, 32)
this.tombol_angka[9] = gui_button("9", this, 88, 48, 34, 32)
this.bagi = gui_button("/", this, 128, 48, 34, 32)
this.clear = gui_button("C", this, 168, 48, 34, 32)
this.tombol_angka[4] = gui_button("4", this, 8, 86, 34, 32)
this.tombol_angka[5] = gui_button("5", this, 48, 86, 34, 32)
this.tombol_angka[6] = gui_button("6", this, 88, 86, 34, 32)
this.kali = gui_button("*", this, 128, 86, 34, 32)
this.clear_all = gui_button("CE", this, 168, 86, 34, 32)
this.tombol_angka[1] = gui_button("1", this, 8, 124, 34, 32)
this.tombol_angka[2] = gui_button("2", this, 48, 124, 34, 32)
this.tombol_angka[3] = gui_button("3", this, 88, 124, 34, 32)
this.kurang = gui_button("-", this, 128, 124, 34, 32)
this.tambah = gui_button("+", this, 168, 124, 34, platform_mac(32, 70))
this.tombol_angka[0] = gui_button("0", this, 8, 162, 34, 32)
this.plus_minus = gui_button("+/-", this, 48, 162, 34, 32)
this.titik = gui_button(".", this, 88, 162, 34, 32)
this.sama_dengan = gui_button("=", this, 128, 162, 34, 32)
this.label = gui_label("Dinfio Example - Calculator", this, 14, platform_mac(202, 218), 200, 32)
this.info = gui_button("?", this, 168, platform_mac(162, 208), 34, 32)
this.display.setlocked(true)
this.display.setfontsize(18)
this.label.setfontsize(platform(9, 11, 11))
this.info.settooltip("About...")
for i, 0, 9
this.tombol_angka[i].addevent(event.click, events::set_angka(i & ""))
endfor
this.titik.addevent(event.click, events::set_titik())
this.plus_minus.addevent(event.click, events::set_plusminus())
this.tambah.addevent(event.click, events::set_operator("+"))
this.kurang.addevent(event.click, events::set_operator("-"))
this.kali.addevent(event.click, events::set_operator("*"))
this.bagi.addevent(event.click, events::set_operator("/"))
this.sama_dengan.addevent(event.click, events::set_samadengan())
this.clear.addevent(event.click, events::set_clear())
this.clear_all.addevent(event.click, events::set_clearall())
this.info.addevent(event.click, events::show_about())
this.show()
stop
function update_display()
if this.operator != ""
this.display.settext(this.angka & "")
else
this.display.settext(this.hasil & "")
endif
if this.display.gettext() == ""; this.display.settext("0"); endif
if this.display.getfontsize() == 11; this.display.setfontsize(18); endif
stop
function set_angka(i)
if this.operator != ""
if (i == "0") && (this.angka&"" != "")
this.angka &= i
elseif (i != "0") && (i != ".")
this.angka &= i
elseif (i == ".") && (this.angka&"" == "")
this.angka = "0."
elseif (i == ".") && (this.angka&"" != "")
this.angka &= i
endif
else
if this.state_hasil
this.hasil = ""
endif
if (i == "0") && (this.hasil&"" != "")
this.hasil &= i
elseif (i != "0") && (i != ".")
this.hasil &= i
elseif (i == ".") && (this.hasil&"" == "")
this.hasil = "0."
elseif (i == ".") && (this.hasil&"" != "")
this.hasil &= i
endif
this.state_hasil = false
endif
this.update_display()
stop
function set_titik()
if !this.ada_titik
this.set_angka(".")
this.ada_titik = true
endif
stop
function set_plusminus()
if this.operator != ""
if this.angka&"" == ""; return; endif
this.angka = -getnumber(this.angka)
else
if this.hasil&"" == ""; return; endif
this.hasil = -getnumber(this.hasil)
endif
this.update_display()
stop
function set_operator(operator)
if this.hasil&"" == ""
return
endif
if this.operator != ""
this.set_samadengan()
endif
this.operator = operator
this.angka = ""
this.ada_titik = false
this.state_hasil = false
stop
function set_samadengan()
if this.operator == "" || this.hasil&"" == ""
this.operator = ""
return
endif
if this.angka&"" == ""
this.angka = getnumber(this.hasil)
endif
if this.operator == "+"
this.hasil = getnumber(this.hasil) + getnumber(this.angka)
elseif this.operator == "-"
this.hasil = getnumber(this.hasil) - getnumber(this.angka)
elseif this.operator == "*"
this.hasil = getnumber(this.hasil) * getnumber(this.angka)
elseif this.operator == "/"
if getnumber(this.angka) == 0
this.display.settext("Division by zero")
this.display.setfontsize(11)
this.operator = ""
this.hasil = ""
this.ada_titik = false
return
else
this.hasil = getnumber(this.hasil) / getnumber(this.angka)
endif
endif
this.state_hasil = true
this.operator = ""
this.ada_titik = false
this.update_display()
stop
function set_clear()
if this.operator != ""
this.angka = ""
else
this.hasil = ""
endif
this.ada_titik = false
this.update_display()
stop
function set_clearall()
this.hasil = ""
this.operator = ""
this.angka = ""
this.ada_titik = false
this.state_hasil = false
this.update_display()
stop
function show_about()
about_window = about()
about_window.show()
stop
endclass
class about: gui_window
field gui_label: label_top, label_bottom
field gui_button: ok
function construct()
extend(this, gui_window("About", 260, about_height))
this.label_top = gui_label("Simple Calculator v1.1", this, 0, 20, this.getinnerwidth(), 30, align.centre)
this.label_bottom = gui_label("This is an example of GUI programming and OOP Inheritance in Dinfio.", this, 20, 50, this.getinnerwidth() - 20, 60, align.centre)
this.ok = gui_button("OK", this)
this.label_top.setfontbold(true)
this.label_top.centrehorizontal()
this.label_bottom.setfontsize(12)
this.label_bottom.centrehorizontal()
this.ok.centrehorizontal()
this.ok.bottom(20)
this.setx(main_window.getx() + main_window.getinnerwidth() / 2 - this.getwidth() / 2)
this.sety(main_window.gety() + main_window.getinnerheight() / 2 - this.getheight() / 2)
this.ok.addevent(event.click, events::close_about())
stop
endclass
class events
function set_angka(i)
main_window.set_angka(i)
stop
function set_titik()
main_window.set_titik()
stop
function set_plusminus()
main_window.set_plusminus()
stop
function set_operator(o)
main_window.set_operator(o)
stop
function set_samadengan()
main_window.set_samadengan()
stop
function set_clear()
main_window.set_clear()
stop
function set_clearall()
main_window.set_clearall()
stop
function show_about()
main_window.show_about()
stop
function close_about()
about_window.close()
stop
endclass
Screenshots: