A simple program: Sum of two numbers

New to FreeBASIC? Post your questions here.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: A simple program: Sum of two numbers

Post by TJF »

Lothar Schirm wrote:"Just use a good GUI Builder ...":

Code with FireFly - 6 lines to be written by the user:
Yes, it's that simple using a powerful tool chain with grafical GUI designer.

But FireFly is no solution, since the code neither is cross-platform nor does it support internationalization (I18N). And when you add a new field (or delete an existing), you've to re-arrange the complete GUI mask. And ..., and ..., and ...

I wouldn't waste my time by learning the usage of an insufficient GUI toolkit.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: A simple program: Sum of two numbers

Post by RockTheSchock »

TJF wrote:
Lothar Schirm wrote:"Just use a good GUI Builder ...":

Code with FireFly - 6 lines to be written by the user:
Yes, it's that simple using a powerful tool chain with grafical GUI designer.

But FireFly is no solution, ...
I wouldn't waste my time by learning the usage of an insufficient GUI toolkit.
Well with "a good GUI Builder" I meant something like Lazarus or for Java Netbeans GUI Builder / Eclipse WindowBuilder Pro. Theese are CrossPlattform and have support for internationalization (I18N).


FireFly as solution is as good as any other approach. The program is a simple calculator. There is no need for an internationalisation framework. The only thing you need is to handle decimal points by language/country.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: A simple program: Sum of two numbers

Post by TJF »

RockTheSchock wrote:FireFly as solution is as good as any other approach. The program is a simple calculator. There is no need for an internationalisation framework.
What does that mean? This time you don't need I18N. Next time you'll learn and use another GUI toolkit? And later on, you'll maintain those projects (and combine parts of them in a further)?
dodicat
Posts: 8244
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A simple program: Sum of two numbers

Post by dodicat »

FreeBasic has enough functions/keywords to make your own GUI as you go along.

About 60 lines for ADD two numbers.

Code: Select all

screenres 400,600,32
color ,rgb(236,233,216)
windowtitle "ADD UP"
dim as integer numboxes=3
dim shared as string t(1 to numboxes),j(1 to numboxes)
type box
    as integer x,y,w,h
    as uinteger col
    as zstring ptr s
end type

function inbox(x as integer,y as integer,b as box) as integer
    return x>b.x and x<(b.x+b.w) and y>b.y and y<(b.y+b.h)
end function

sub drawbox(b as box)
    line(b.x,b.y)-(b.x+b.w,b.y+b.h),b.col,bf
     line(b.x,b.y)-(b.x+b.w,b.y+b.h),b.col/2,b
    draw string(b.x+2,b.y+4),*b.s,rgb(0,0,0)
end sub

Sub _input(i as string,x As Integer,y As Integer,st As String,message As String,clr As Uinteger=Rgb(0,0,0),flag as integer=1)
    Static As Integer count,gap=20
    Static As String blink
    count=count+1 Mod 100
    If Left(i,1)=Chr(08) Then j(flag)=Mid(j(flag),1,Len(j(flag))-1)
    Select Case Left(i,1)
    Case Chr(0) To Chr(254)
        If Left(i,1)<>Chr(08) Then
            j(flag)=j(flag)+Left(i,1)
        End If
    End Select
    If count Mod gap=0 Then blink=" "
    If count Mod 2*gap=0 Then blink="_"
    If Left$(i,1)=Chr(27) Then j(flag)=""
        Draw String(x+2,y+4),st & i & blink,clr
        message=mid(j(flag),1,25)
End Sub

var Input1=type<box>(48,80,200,30,rgb(255,255,255),@" ")
var Input2=type<box>(48,280,200,30,rgb(255,255,255),@" ")
var answer=type<box>(48,480,200,30,rgb(236,233,216),@" ")

dim as integer mx,my,mb,bb,cc
dim as string i
do
    i=inkey
    if i=chr(256)+"k" then end
    getmouse mx,my,,mb
    screenlock
    cls
drawbox(Input1):drawbox(Input2):drawbox(answer)
if mb=1 then bb=inbox(mx,my,Input1) :cc=inbox(mx,my,Input2) 
if bb then _input(i,Input1.x,Input1.y,*Input1.s,t(1),,1):Input1.s=@t(1)[0]
if cc then _input(i,Input2.x,Input2.y,*Input2.s,t(2),,2):Input2.s=@t(2)[0]
var sumf=(val(*Input2.s)+val(*Input1.s))
draw string(answer.x,answer.y-16),"Answer:",rgb(0,0,0)
draw string(answer.x+2,answer.y+4),"" &sumf,rgb(0,0,0)
screenunlock
sleep 1,1
loop  
BasicCoder2
Posts: 3954
Joined: Jan 01, 2009 7:03
Location: Australia

Re: A simple program: Sum of two numbers

Post by BasicCoder2 »

dodicat wrote:FreeBasic has enough functions/keywords to make your own GUI as you go along.
About 60 lines for ADD two numbers.
Rolling my own is what I always do with FreeBasic. Although this example is more than 60 lines it has always been my intention to create a file of GUI objects and methods using just FB code which could included at the beginning of a program which would allow easy use of such GUI objects. A kind of retro-GUI interface :) I have also done some experimental GUI design programs that generate a FB template to which you simply add code in the event loop to tell it what to do for any particular event.

Code: Select all

screenres 640,480,32
color rgb(0,0,0),rgb(255,255,255):cls

dim shared as integer mx,my,mb  'mouse variables
dim shared as integer event,ID 'event number and ID of GUI object

type TextButton
    x as integer    'text screen coordinates
    y as integer
    w as integer    'width
    h as integer    'height
    c1 as uinteger  'normal color
    c2 as uinteger  'selected color
    t as string     'actual text
    s as integer    'button selected flag if not -1
end type

sub drawButton(b as TextButton)
    locate (b.y+4)\8+1,(b.x+4)\8+1    
    if b.s = 1 then
        line (b.x,b.y)-(b.x+b.w,b.y+b.h),rgb(255,0,0),b
        color b.c2
    else
        line (b.x,b.y)-(b.x+b.w,b.y+b.h),rgb(255,0,255),b
        color b.c1
    end if
    draw string (b.x+4,b.y+4), b.t  'draw text inside button
    color rgb(255,255,255)
end sub

type TextBox
    as integer x
    as integer y
    as integer w
    as integer h
    as string  t
    as integer c  'cursor position in string
    as integer s  'starting position
    as integer a  'is text box active?
end type

sub drawTextBox(tb as TextBox)
    line (tb.x,tb.y)-(tb.x+tb.w,tb.y+tb.h),rgb(255,255,255),bf
    line (tb.x,tb.y)-(tb.x+tb.w,tb.y+tb.h),rgb(0,0,255),b
    draw string (tb.x+4,tb.y+4),mid(tb.t, tb.s +1,20),rgb(0,0,0)
    if tb.a = 1 then
        draw string (tb.x+4 + (tb.c- tb.s )*8,tb.y+4),"_",rgb(255,0,0)  'draw cursor
    end if
end sub

sub editTextBox(tb as TextBox)
    tb.a = 1
    dim as string  strKey
    dim as integer ascKey

    dim as string text1,text2
    text1 = tb.t
    screenlock()
    drawTextBox(tb)
    screenunlock()
    
    do

        strKey = inkey
        ascKey = asc(strKey)

        if strKey <> "" then
           ascKey = asc(strKey)
           if ascKey = 255 then
                
               ascKey = asc(right(strKey,1))
                If ascKey = 75 Then             'MOVE CURSOR LEFT
                    if len(text1)<>0 then
                        text2 = right(text1,1) + text2
                        text1 = left(text1,len(text1)-1)
                        tb.c = tb.c - 1
                    end if
                end if
      
                If ascKey = 77 Then
                    if len(text2)<>0 then         'MOVER CURSOR RIGHT
                        text1 = text1 + left(text2,1)
                        text2 = right(text2,len(text2)-1)
                        tb.c = tb.c + 1
                    end if
                end if
                
                if ascKey = 83 then  'delete key
                    if len(text2)<>0 then
                        text2 = right(text2,len(text2)-1)
                    end if
                end if

            else
                
                if ascKey = 8 then  'backspace
                    if len(text1)<> 0 then
                        text1 = left(text1,len(text1)-1)
                        tb.c = tb.c - 1
                    end if
                else
                    if ascKey<>13 and ascKey<>9 and ascKey<> 27 then
                        text1 = text1 + chr(ascKey)
                        tb.c = tb.c + 1
                    end if
                end if
            
            end if

            tb.t = text1 & text2
        
            if tb.c >  tb.s +19 then
                tb.s  =  tb.s  + 1
            end if
            if tb.c <=  tb.s  then
                if  tb.s <>0 then
                    tb.s  =  tb.s -1
                end if
            end if
            
            screenlock()
            drawTextBox(tb)
            screenunlock()
            
        end if
        getmouse mx,my,,mb
    loop until ascKey = 13 or ascKey=9 or ascKey= 27 or mb=1

    tb.t = text1 & text2
    tb.c = len(tb.t)
    tb.a = 0
    
end sub

dim shared as TextButton btnExit

'intialize button 1
btnExit.x = 10
btnExit.y = 10
btnExit.t = " EXIT "
btnExit.w = len(btnExit.t)*8+4
btnExit.h = 14
btnExit.s = 0   'button not selected
btnExit.c1 = rgb(0,0,0)
btnExit.c2 = rgb(255,0,255)

dim shared as TextBox tb1,tb2,tb3
tb1.x = 100
tb1.y = 100
tb1.w = 8*21
tb1.h = 16
tb1.c = 0
tb1.s = 0
tb1.a = 0  'a = 1 if activated for editing

tb2.x = 100
tb2.y = 200
tb2.w = 8*21
tb2.h = 16
tb2.c = 0
tb2.s = 0
tb2.a = 0   '

tb3.x = 100
tb3.y = 300
tb3.w = 8*21
tb3.h = 16
tb3.c = 0
tb3.s = 0
tb3.a = 0   '

sub updateScreen()
    screenlock()
    cls
    drawButton(btnExit)
    drawTextBox(tb1)
    drawTextBox(tb2)
    drawTextBox(tb3)
    screenunlock()
end sub

sub getEvent()
    getmouse mx,my,,mb
    ID = -1
    event  = -1   'assume no event
    
    'test if button down over a GUI ID
    if mb = 1 then
        
        'is it down over exit button?
        if mx>btnExit.x and mx<btnExit.x + btnExit.w then
            if my>btnExit.y and my < btnExit.y + btnExit.h then
                event  = 1  'button press event
                ID = 0  'id of ID
            end if
        end if
        
        'is it over tb1
        if mx>tb1.x and mx<tb1.x + tb1.w then
            if my>tb1.y and my < tb1.y + tb1.h then
                event  = 2  'textbox press event
                ID = 0  'id of ID
            end if
        end if
        
        'is it over tb2
        if mx>tb2.x and mx<tb2.x + tb2.w then
            if my>tb2.y and my < tb2.y + tb2.h then
                event  = 2  'textbox press event
                ID = 1  'id of ID
            end if
        end if
        
        while mb = 1
            getmouse mx,my,,mb
        wend
        
    end if

end sub

dim as integer exitFlag
exitFlag = 0

dim as double num1,num2

while exitFlag = 0
    
    getEvent()
    
    'use event number and ID ID to select code to execute
    if event = 1 then   'button event
        if ID = 0 then 'exit program
            exitFlag = 1
        end if
    end if
    
    if event = 2 then
        
        if ID = 0 then
            editTextBox(tb1)
        end if
        
        if ID = 1 then
            editTextBox(tb2)
        end if
        
        num1 = val(tb1.t)
        num2 = val(tb2.t)
        
        tb3.t = str(num1 + num2)

    end if
    
    updateScreen() 
    
wend
Last edited by BasicCoder2 on Jan 08, 2015 3:01, edited 1 time in total.
dodicat
Posts: 8244
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A simple program: Sum of two numbers

Post by dodicat »

BasicCoder2
Works well.
You must be using a really old fb version.
object is now a reserved keyword, has been for quite a while!
BasicCoder2
Posts: 3954
Joined: Jan 01, 2009 7:03
Location: Australia

Re: A simple program: Sum of two numbers

Post by BasicCoder2 »

@dodicat,
The FB version would be nearly four years old. My Windows8 laptop has gone all silly. When I double click the FBIDE icon it pops up a window saying it isn't a .png file!! I don't know if a virus or malware has taken hold so at the moment I am using my older and for me nicer to use windows7 laptop. I haven't updated FB on that computer yet.

Thanks for the info I have changed "object" to "ID".
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: A simple program: Sum of two numbers

Post by TJF »

dodicat wrote:About 60 lines for ADD two numbers.
Yes, we can do a lot with FB. And we can loose our focus and run in to a never ending project. A modern GUI is more than showing characters on screen.

Clipboard? Tooltips? Font selecting (ie. big font for eye handicaped users)? Screen reader for blind users? ...
BasicCoder2
Posts: 3954
Joined: Jan 01, 2009 7:03
Location: Australia

Re: A simple program: Sum of two numbers

Post by BasicCoder2 »

@TJF
So how many people actually use FreeBasic for a serious commercial project? How many advanced programmers actually use it for practical applications like big fonts for the handicapped or writing a screen reader for the blind? I have the impression not too many? They certainly don't talk about it here.

I use FreeBasic because it easy for an old QBASIC hobbyist to use but if I had to learn all its advanced features it would cease to be easy to use instead I would be better off using VB or C# or some stable well supported language that supports the all singing all dancing GUI's but without the high learning curve and expert knowledge that would be required to duplicate all that in FB.

If my little project needs some buttons, a combo box, a picture box, sliders, radio buttons, text box, edit box and so on then it is easy enough to whip them up without a GUI library instead a bit of cut and paste from old code does the job including big fancy fonts.
ike
Posts: 387
Joined: Jan 17, 2011 18:59

Re: A simple program: Sum of two numbers

Post by ike »

Code: Select all


#include once "fltk-c.bi"

enum
  Me, Input1, Input2, Input3, Btn7,  LastItem = Btn7
end enum

dim shared as any ptr ptr gList(LastItem)

sub ButtonCB cdecl (byval self as Fl_Widget ptr, byval index as long)
    dim as double v1=val(*Fl_Input_GetValue (gList(Input1))), v2 =val(*Fl_Input_GetValue (gList(Input2)))
    Fl_Input_SetValue gList(Input3), str(v1+v2)
end sub

' main
Fl_SetScheme "gleam"
gList(Me) = Fl_Double_WindowNew (200,200,"a+b=c")
gList(Input1) = Fl_InputNew(30,30,100,20, "a=")
gList(Input2) = Fl_InputNew(30,60,100,20, "b=")
gList(Input3) = Fl_InputNew(30,90,100,20, "c=")

gList(Btn7) = Fl_ButtonNew(30,120,100,30,"Calc c")
Fl_WidgetSetCallback1Arg gList(Btn7), @ButtonCB, Btn7

Fl_WindowShow gList(Me)
Fl_Run

Code: Select all

#include once "fltk-c.bi"

enum
  Me, Input1, Input2, Input3, Btn7,  LastItem = Btn7
end enum

dim shared as any ptr ptr gList(LastItem)

function InputBoxGetValue(in as Integer) as double
 dim as double v = val(*Fl_Input_GetValue (gList(in)))
 return v
end function

sub InputBoxSetText(in as Integer, s as string)
 Fl_Input_SetValue gList(in), s
end sub

sub ButtonCB cdecl (byval self as Fl_Widget ptr, byval index as long)
    dim as double v1 = InputBoxGetValue(Input1)
    dim as double v2 = InputBoxGetValue(Input2)
    InputBoxSetText (Input3, str(v1+v2))
end sub

' main
Fl_SetScheme(@"plastic")
gList(Me) = Fl_Double_WindowNew (200,200,"a+b=c")
gList(Input1) = Fl_InputNew(30,30,100,20, "a=")
gList(Input2) = Fl_InputNew(30,60,100,20, "b=")
gList(Input3) = Fl_InputNew(30,90,100,20, "c=")

gList(Btn7) = Fl_ButtonNew(30,120,100,30,"Calc c")
Fl_WidgetSetCallback1Arg gList(Btn7), @ButtonCB, Btn7

Fl_WindowShow gList(Me)
Fl_Run

TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: A simple program: Sum of two numbers

Post by TJF »

BasicCoder2 wrote:... writing a screen reader for the blind?

I use FreeBasic because it easy for an old QBASIC hobbyist to use but if I had to learn all its advanced features it would cease to be easy to use instead I would be better off using VB or C# or some stable well supported language that supports the all singing all dancing GUI's but without the high learning curve and expert knowledge that would be required to duplicate all that in FB.
Sorry, I don't understand what you're talking about.
  • Why writing a (further) screen reader?
  • Why vb or c#, when all features are available in FB?
  • Why duplicate anything in FB, when it's already available (in libraries)?
If you like to understand what I mean by low and high level GUI, just compare the output of examples/GUI/GTK+/FB_Calc by this example (and read the srvaldez post below).

My advice: when you spend time to learn a GUI toolkit, do it once.
BasicCoder2 wrote:So how many people actually use FreeBasic for a serious commercial project? How many advanced programmers actually use it for practical applications like big fonts for the handicapped ...
At least I do. If you wont learn from my experience, just give it a bone.
BasicCoder2
Posts: 3954
Joined: Jan 01, 2009 7:03
Location: Australia

Re: A simple program: Sum of two numbers

Post by BasicCoder2 »

TJF wrote:Sorry, I don't understand what you're talking about.
Possibly I don't know what you are talking about and that is the problem here?
Why vb or c#, when all features are available in FB?
But not equally easy to make use of. I can whip up a GUI quickly in VB without learning how to use a GUI library.

FreeBasic is a powerful low level language like C and is becoming more like C++ and in the right hands can do anything but some things are for the non-expert easier in another language.

I googled FreeBasic and GTK out of curiosity and found this as a starter,

http://zetcode.com/gui/fbgtk/

Sorry not as simple to learn or as readable as using the javax.swing package.

Before taking on FreeBasic I was getting my head around the SDL package and programming in C++. However FreeBasic made it so easy to use bitmaps I decided to use that instead of C++. There is no easy FreeBasic GUI to replace GTK package so you might as well stay with C++ ??
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: A simple program: Sum of two numbers

Post by TJF »

BasicCoder2 wrote:Possibly I don't know what you are talking about and that is the problem here?
Yes, may be.
BasicCoder2 wrote:I googled FreeBasic and GTK out of curiosity and found this as a starter,

http://zetcode.com/gui/fbgtk/
That's a good tutorial, but outdated (GTK+-2.8). It doesn't explain the use of the grafical designer Glade3 (GTK+-3.10 is latest).

A better start would be the tutorial of haegartheroot, but it uses libglade instead of the current GtkBuilder.
BasicCoder2 wrote:FreeBasic is a powerful low level language like C and is becoming more like C++ ...
C (or C++) has less than 50 keywords. FB has more than 400. I don't see them at the same level.
BasicCoder2 wrote:... but some things are for the non-expert easier in another language.
Not only for non-experts. That's why I use FB instead of those classic languages. And that's why I adapt so many tools like gettext, gtk-doc or Doxygen to use them with FB code.
BasicCoder2 wrote:But not equally easy to make use of. I can whip up a GUI quickly in VB without learning how to use a GUI library.
That's an important point. It looks easy at the beginning. But sooner or later you'll come to the point where you need a new feature that isn't implemented. Then you'll have to inherit an existing class and customize its features, which is impossible in vb.

The contrary extreme is QT library. Looks easy at the beginning. And you can customize a widget. But you've to dig deeply in to cryptic C++ code. IMHO made for bit-mutants, but unusable for a solution-orientated programmer.

I can whip up a cross-platform GUI quickly with the grafical designer Glade3 and GTK+. And I know I can maintain the project painless for years.
BasicCoder2
Posts: 3954
Joined: Jan 01, 2009 7:03
Location: Australia

Re: A simple program: Sum of two numbers

Post by BasicCoder2 »

TJF wrote:C (or C++) has less than 50 keywords. FB has more than 400. I don't see them at the same level.
That is not what I mean by same level. RobotBASIC has well over 800 commands and functions but FreeBasic is a more powerful language. It is not the number of keywords it is their power. All computer languages need to same core set of keywords which reflect the core set of assembler instructions. All the other keywords are simply the embodiment of useful subroutines which could just as well existed in include libraries.

I can whip up a cross-platform GUI quickly with the graphical designer Glade3 and GTK+. And I know I can maintain the project painless for years.
Well I may well have a look at Glade3 and GTK+ I just haven't up to now needed it for any of my projects.
dodicat
Posts: 8244
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A simple program: Sum of two numbers

Post by dodicat »

To be perfectly honest here, I would prefer to be proficient with the Wn Api for GUI's in Windows.

Learning the Win Api is learning the Core of Windows and DOS, and not simply using pre-written other party DLL's and the like.

Spending time grasping the ins and outs of some wrapper or the like, for me wouldn't do.

But I am only a hobby coder.

The only exception so far, for myself, is GMP.

But I spent some time building the trig functions and others from GMP, and I found this exercise was fun.
But, as I say, only in the FreeBasic world, not the real one.
Post Reply