making Calc.yab more useful.
#1
I added keyboard input and saving window location to Calc.yab and now I think it is better than the DeskCalc app included in Haiku.


Code:
#!yab
sleep .01
This_Dir$=attribute get$ "", ""
This_Dir$=This_Dir$+"/"
localize
window open 100,50 to 320,320, "Calc", "CalcView"
if (open(1,"/boot/home/config/settings/Calc.settings")) then
    line input #1 x$
    line input #1 y$
close #1    
WINDOW SET "Calc", "MoveTo", val(x$),val(y$)    
end if

draw set "bgcolor", 100, 150, 220, "Calc"
draw set "lowcolor", 100, 190, 100, "Calc"
draw set "highcolor", 255, 255, 255, "Calc"
window set "Calc", "minimumto", 220, 260
window set "Calc", "maximumto", 220, 260

layout "all", "Calc"

menu "File", "Reset", "R", "Calc"
menu "File", "--", "", "Calc"
menu "File", "Quit", "Q", "Calc"
menu "Help", "Help...", "H", "Calc"
menu "Help", "--", "", "Calc"
menu "Help", "About...", "", "Calc"

button 20,70 to 55,105,"b7", "7", "Calc"
button 65,70 to 100,105,"b8", "8", "Calc"
button 110,70 to 145,105,"b9", "9", "Calc"
button 155,70 to 190,105,"b/", "/", "Calc"
button 20,115 to 55,150,"b4", "4", "Calc"
button 65,115 to 100,150,"b5", "5", "Calc"
button 110,115 to 145,150,"b6", "6", "Calc"
button 155,115 to 190,150,"b*", "*", "Calc"
button 20,160 to 55,195,"b1", "1", "Calc"
button 65,160 to 100,195,"b2", "2", "Calc"
button 110,160 to 145,195,"b3", "3", "Calc"
button 155,160 to 190,195,"b-", "-", "Calc"
button 20,205 to 55,240,"b0", "0", "Calc"
button 65,205 to 100,240,"b.", ".", "Calc"
button 110,205 to 145,240,"bC", "C", "Calc"
button 155,205 to 190,240,"b+/=", "+=", "Calc"

draw text 20,50,"0","Calc"

display$ = "0"
result = 0
type$ = "+"

dim msgbuffer$(1)
inloop = true
while(inloop)
    msgnumber = split(message$, msgbuffer$(), "|")
    // keyboard input
    if msgnumber = 0 then
        option set "Calc", "Focus", true
        k$ = KEYBOARD MESSAGE$("Calc")
        // check for a key press
        if(k$<>"") then
            // clear the keyboard buffer
            a$= KEYBOARD MESSAGE$("Calc")
            while (a$<>"")
                a$= KEYBOARD MESSAGE$("Calc")
            wend
            // what key was pressed?
            switch(k$)
                case "."
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b."
                    msgnumber=1
                    break
                case "0"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b0"
                    msgnumber=1
                    break
                case "1"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b1"
                    msgnumber=1
                    break    
                case "2"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b2"
                    msgnumber=1
                    break
                case "3"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b3"
                    msgnumber=1
                    break
                case "4"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b4"
                    msgnumber=1
                    break
                case "5"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b5"
                    msgnumber=1
                    break
                case "6"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b6"
                    msgnumber=1
                    break
                case "7"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b7"
                    msgnumber=1
                    break
                case "8"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b8"
                    msgnumber=1
                    break
                case "9"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b9"
                    msgnumber=1
                    break
                case "-"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b-"
                    msgnumber=1
                    break
                case "7"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b7"
                    msgnumber=1
                    break
                case "+"
                case "="
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b+/="
                    msgnumber=1
                    break
                case "*"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b*"
                    msgnumber=1
                    break
                case "/"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="b/"
                    msgnumber=1
                    break
                case "c"
                case "C"
                    dim msgbuffer$(1)
                    msgbuffer$(1)="bC"
                    msgnumber=1
                    break
                default
                    // ignore the keypress as it doesn't mean anything to Calc
            end switch
        end if
    end if
    
    
    
    
    
    for i=0 to msgnumber
        switch msgbuffer$(i)
            case "b."
            case "b0"
            case "b1"
            case "b2"
            case "b3"
            case "b4"
            case "b5"
            case "b6"
            case "b7"
            case "b8"
            case "b9"
                if(len(display$)<10) display$ = display$ + right$(msgbuffer$(i),1)
                while(left$(display$,1)="0")
                    display$ = right$(display$,len(display$)-1)
                wend
                if(display$="") display$="0"
                draw rect 20,30 to 170,60, "Calc"
                draw flush "Calc"
                draw text 20,50, display$,"Calc"
                break
            case "b-"
                result = calculate(result, type$, val(display$))
                type$ = "-"
                draw rect 20,30 to 170,60, "Calc"
                draw flush "Calc"
                draw text 20,50, str$(result),"Calc"
                display$="0"
                break
            case "b+/="
                result = calculate(result, type$, val(display$))
                type$ = "+"
                draw rect 20,30 to 170,60, "Calc"
                draw flush "Calc"
                draw text 20,50, str$(result),"Calc"
                display$="0"
                break
            case "b*"
                result = calculate(result, type$, val(display$))
                type$ = "*"
                draw rect 20,30 to 170,60, "Calc"
                draw flush "Calc"
                draw text 20,50, str$(result),"Calc"
                display$="0"
                break
            case "b/"
                result = calculate(result, type$, val(display$))
                type$ = "/"
                draw rect 20,30 to 170,60, "Calc"
                draw flush "Calc"
                draw text 20,50, str$(result),"Calc"
                display$="0"
                break
            case "bC"
            case "Calc:File:Reset"
                result=0
                draw rect 20,30 to 170,60, "Calc"
                draw flush "Calc"
                draw text 20,50, str$(result),"Calc"
                display$="0"
                type$ = "+"
                break
            case "Calc:File:Quit"
            case "Calc:_QuitRequested"
                closewindow()
                
                break
            case "Calc:Help:Help..."
                alert "Calc demonstrates how to program\n a calculator in yab.\n\n", "Ok", "info"
                break
            case "Calc:Help:About..."
                window open 250,100 to 570,291, "About", "About"
                window set "About", "look", "bordered"
                window set "About", "feel", "modal-app"
                draw set "BGColor", 255,255,255, "About"
                window set "About", "minimumto", 320, 191
                window set "About", "maximumto", 320, 191
                err = draw image 0,0, This_Dir$+"img/image.png", "About"
                if(err>0) then
                    alert "Error loading image.png!"+str$(err), "Close", "warning"
                    window close "About"
                else
                    button 20,163 to 300,183,"b", "Close", "About"
                endif

                break
            case translate$("b")
            case translate$("About:_QuitRequested")
                window close "About"
                break
            default
                break
        end switch
    next i
    
    if(window count = 0) inloop = false
    
wend


sub closewindow()
x = WINDOW GET "Calc", "Position-X"
y = WINDOW GET "Calc", "Position-y"
x$=str$(x)
y$=str$(y)


open #1, "/boot/home/config/settings/Calc.settings","w"
if x>0 and y>0 then
print #1 x$
print #1 y$

endif

close #1
    window close "Calc"
    return
end sub


sub calculate(a,type$,b)
    ret = 0
    switch(type$)
        case "+"
            ret = a + b
            break
        case "-"
            ret = a - b
            break
        case "*"
            ret = a * b
            break
        case "/"
            ret = a / b
            break
    end switch
    return ret
end sub
Reply
#2
but do not forget that this is an example. to make learning easly. do no make them to complicated. will be better to add a second calc example like extended calc example

beginners need simple stuff to make first steps and a calculator is a good program for this
Reply
#3
(07-24-2015, 01:16 AM)lelldorin Wrote: but do not forget that this is an example. to make learning easly. do no make them to complicated. will be better to add a second calc example like extended calc example

beginners need simple stuff to make first steps and a calculator is a good program for this

I have no intention of ghanging the example, just thought I would share this modification here.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
Free Web Hosting