Fb minimal GUI

User projects written in or related to FreeBASIC.
Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Fb minimal GUI

Postby Luxan » Apr 12, 2019 5:05

This really is using a minimal of code to implement a rudimentary GUI , no external libraries used here .

About the only other code I require is for selecting and loading , saving , files .

This may also run faster than other GUI constructs and use less resources.
The while ... wend loop really requires a timer to occasionally check for mouse activity ; there's an example of this on the web for game construction .

The bmp icons for the buttons were constructed using a Bacon utility Tex to SVG/png generator ; quite useful .
The bmp icons are 100 x 40 , 16 bit colour ; with vertical gradients .

Can't figure out how to attach or insert to post from my computer the icons , might upload somewhere else first ; wonder if google drive is still available .



Code: Select all


'
'   (c) Copyright 2019
'
'
'    sciwiseg@gmail.com
'
'   Dependent upon various FreeBASIC examples , from the documentation .
'
'   Mouse  routine  2
'
'   Note Inkey$ is deprecated , use Inkey instead .
'
Const LEFTBUTTON   = 1
Const MIDDLEBUTTON = 4   ' UNUSED IN THIS DEMO
Const RIGHTBUTTON  = 2   ' UNUSED IN THIS DEMO
Const SHOWMOUSE    = 1
Const HIDEMOUSE    = 0
'
Dim CurrentX     As Integer
Dim CurrentY     As Integer
Dim MouseButtons As Integer
Dim CanExit      As Integer
'
' ----------------------------------------------------------
'
declare sub pxy1()
declare sub ms3()

declare sub plot2d()
'
' ---------------------------------------------------------
'
'' A function that creates an image buffer with the same
'' dimensions as a BMP image, and loads a file into it.

Const NULL As Any Ptr = 0

declare Function bmp_load( ByRef filename As Const String ) As Any Ptr
dim As Any Ptr img1

'
' ----------------------------------------------------------------------
'
'Screen 12,24

'ScreenRes 640, 480, 32 ' screen 12 dimensions .

screen 18,16' screen 12 dimensions .

'window (-1,-1)-(1,1)
'view(118,0)-(679,479)
'line(-1,-1)-(1,1),11,b

'line(118,0)-(679,479),rgb(12,120,120),b
SetMouse 1, 1, SHOWMOUSE
CanExit = 1



'pxy1
ms3

'window (-1,-1)-(1,1)
'view(118,0)-(679,479)
'line(-1,-1)-(1,1),11,b


sleep 100
'
'
end
'
' ======================================================================
'
'  Button location and colour .
'
'
bxd:
data 10,110
data 10,110
data 10,110
byd:
data 10,50
data 60,100
data 110,150
bcd:
data 2,2,1,1,4,4
'
' ----------------------------------------------------------------
'
sub pxy1
'
'   Draw boundaries for graph .
'
static as integer  w , h
static as integer depth
static as string  driver_name

'Screen 15, 32
' Obtain info about current mode
ScreenInfo w, h, depth,,,,driver_name
'Print Str(w) + "x" + Str(h) + "x" + Str(depth);
'Print " using " + driver_name + " driver"
'Sleep
' Quit graphics mode and obtain info about desktop
'Screen 0
'ScreenInfo w, h, depth
Print "Desktop running at " + Str(w) + "x" + Str(h) + "x" + Str(depth);

line (118,0)-(w-1,h-1),11,b
line (0,0)-(w-1,h-1),11,b'

'  draw graph ?


'sleep
'
'
end sub
'
' ----------------------------------------------------------------
'

'
' ----------------------------------------------------------------
'
sub ms3
'
'    Draw buttons using data and select using array values .
'
'
static as integer CurrentX , CurrentY , MouseButtons
'
static as single x1,x2,y1,y2
static as integer colour,colorxy
static as integer i,j,k,n

static As Any Ptr img1 , img2
'
'                            Read data .
'
n=5
dim as single bxyc(0 to n,0 to 2)
restore bxd
for i=0 to n
   read bxyc(i,0)
next i
restore byd
for i=0 to n
   read bxyc(i,1)
next i
restore bcd
for i=0 to n
   read bxyc(i,2)
next i
'
'
'cls
'

'
'  Dedicated routine for this .
'
'color 15,2
'locate 2,7
'print "Run"
'
img1 = bmp_load( "run.bmp" )

If img1 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 10), img1

    ImageDestroy( img1 )

End If

img2 = bmp_load( "draw.bmp" )

If img2 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 60), img2

    ImageDestroy( img2 )

End If
img2 = bmp_load( "exit.bmp" )

If img2 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 110), img2

    ImageDestroy( img2 )

End If

for i=0 to n step 2
    x1=bxyc(i,0)
    x2=bxyc(i+1,0)
    y1=bxyc(i,1)
    y2=bxyc(i+1,1)
    colour = bxyc(i,2)
    colour=rgb(colour,12,12)
'   
    'line (x1,y1)-(x2,y2),7,bf
   ' line (x1,y1)-(x2,y2),15,b
   ' line (x1+4,y1+4)-(x2-4,y2-4),colour,bf
'    line (x1+4,y1+4)-(x2-4,y2-4),rgb(150,150,150),b
    line (x1,y1)-(x2,y2),rgb(150,150,150),b
   
   
'
next i




'color 15,1
'locate 6,7
'print "Draw"

'color 15,4
'locate 9,7
'print "Exit"



'
'                          Select button .
'
MouseButtons = 0
    k=0
while ( Inkey = "" )

   GetMouse CurrentX, CurrentY, , MouseButtons
   
   If MouseButtons And LEFTBUTTON Then
      colour = Point(CurrentX,CurrentY)
   
    for i = 0 to n step 2
    '
          k = 0
         x1 = bxyc(i,0)
         x2 = bxyc(i+1,0)
         y1 = bxyc(i,1)
         y2 = bxyc(i+1,1)
     colorxy = bxyc(i,2)
'       If CurrentY > y1+4 and CurrentY < y2 -4 and CurrentX > x1+4 and CurrentX < x2 - 4 and colour = colorxy then
       If CurrentY > y1+4 and CurrentY < y2 -4 and CurrentX > x1+4 and CurrentX < x2 - 4  then
       
        'color colorxy
        color rgb(180,18,240)
        Locate 23,4
       
        j=int(i/2)
        select case j
              case 0:
                print " Run "
              case 1:
                print " Draw "
                window (-1,-1)-(1,1)
                view(118,0)-(679,479)
             
                plot2d
               
                sleep 120
               
               '   Return to native resolution .
       
                window
                view
 
              case 2:
                print " Exit "
                k=1
                exit for
              case else   
        end select
 
      End If
     '
    next i
    '
    if k=1 then exit while
      End If
wend   
'
if k=1 then
   color rgb(200,120,60)
   Locate 24,4
   print " Done  "
end if
'
sleep 400
'
end sub
'
' ---------------------------------------------------------------------
'
sub plot2d
'
'
'
static as single x,y,yp,dx
static as integer colour
'
colour = rgb(240,240,240)               
line(-1,-1)-(1,1),colour,b
line(0,-1)-(0,1),colour
line(-1,0)-(1,0),colour
 
colour = rgb(12,120,200)

dx =2/600

for x=-1 to 1 - dx step dx
    y=sin(x*6.28)
    yp =sin((x+dx)*6.28)
   line (x,y)-(x+dx,yp),colour
next x
'
colour = rgb(240,240,240)
line(-1,-1)-(1,1),colour,b
line(0,-1)-(0,1),colour
line(-1,0)-(1,0),colour
'
end sub
'
' -------------------------------------------------------------------------
'
Function bmp_load( ByRef filename As Const String ) As Any Ptr

    Dim As Long filenum, bmpwidth, bmpheight
    Dim As Any Ptr img

    '' open BMP file
    filenum = FreeFile()
    If Open( filename For Binary Access Read As #filenum ) <> 0 Then Return NULL

        '' retrieve BMP dimensions
        Get #filenum, 19, bmpwidth
        Get #filenum, 23, bmpheight

    Close #filenum

    '' create image with BMP dimensions
    img = ImageCreate( bmpwidth, Abs(bmpheight) )

    If img = NULL Then Return NULL

    '' load BMP file into image buffer
    If BLoad( filename, img ) <> 0 Then ImageDestroy( img ): Return NULL

    Return img

End Function
'
' -------------------------------------------------------------------------
'
.
paul doe
Posts: 916
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Fb minimal GUI

Postby paul doe » Apr 12, 2019 5:33

Luxan wrote:...Can't figure out how to attach or insert to post from my computer the icons , might upload somewhere else first ; wonder if google drive is still available .

Why don't you use GitHub? You can create a repo with all needed files there.
Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI

Postby Luxan » Apr 12, 2019 5:44

Haven't tried GitHub for anything .

These are shared links to the icons , upon my google drive ; all other material should be
private .

Seems this will work .

[img]

https://drive.google.com/open?id=1Q67PN ... xW7FGh9HWI
https://drive.google.com/open?id=1NX2vN ... BAuh1MUr19
https://drive.google.com/open?id=1n2DZ3 ... H3pjcJvyRX

[/img]
Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI

Postby Luxan » Apr 16, 2019 2:42

Just a quick note , one might construct buttons natively using something like the RGBA example .

In the meantime , here's a very minor update .

Code: Select all

'
'   (c) Copyright 2019
'
'
'    sciwiseg@gmail.com
'
'   Dependent upon various FreeBASIC examples , from the documentation .
'
'   Mouse  routine  2
'
'   Note Inkey$ is deprecated , use Inkey instead .
'
Const LEFTBUTTON   = 1
Const MIDDLEBUTTON = 4   ' UNUSED IN THIS DEMO
Const RIGHTBUTTON  = 2   ' UNUSED IN THIS DEMO
Const SHOWMOUSE    = 1
Const HIDEMOUSE    = 0
'
Dim CurrentX     As Integer
Dim CurrentY     As Integer
Dim MouseButtons As Integer
Dim CanExit      As Integer
'
' ----------------------------------------------------------
'
declare sub pxy1()
declare sub ms3()

declare sub plot2d()
'
' ---------------------------------------------------------
'
'' A function that creates an image buffer with the same
'' dimensions as a BMP image, and loads a file into it.

Const NULL As Any Ptr = 0

declare Function bmp_load( ByRef filename As Const String ) As Any Ptr
dim As Any Ptr img1

'
' ----------------------------------------------------------------------
'
'Screen 12,24

'ScreenRes 640, 480, 32 ' screen 12 dimensions .

screen 18,16' screen 12 dimensions .

'window (-1,-1)-(1,1)
'view(118,0)-(679,479)
'line(-1,-1)-(1,1),11,b

'line(118,0)-(679,479),rgb(12,120,120),b
SetMouse 1, 1, SHOWMOUSE
CanExit = 1



'pxy1
ms3

'window (-1,-1)-(1,1)
'view(118,0)-(679,479)
'line(-1,-1)-(1,1),11,b


sleep 100


 
'
'
end
'
' ======================================================================
'
'  Button location and colour .
'
'
bxd:
data 10,110
data 10,110
data 10,110
byd:
data 10,50
data 60,100
data 110,150
bcd:
data 2,2,1,1,4,4
'
' ----------------------------------------------------------------
'
sub pxy1
'
'   Draw boundaries for graph .
'
static as integer  w , h
static as integer depth
static as string  driver_name

'Screen 15, 32
' Obtain info about current mode
ScreenInfo w, h, depth,,,,driver_name
'Print Str(w) + "x" + Str(h) + "x" + Str(depth);
'Print " using " + driver_name + " driver"
'Sleep
' Quit graphics mode and obtain info about desktop
'Screen 0
'ScreenInfo w, h, depth
Print "Desktop running at " + Str(w) + "x" + Str(h) + "x" + Str(depth);

line (118,0)-(w-1,h-1),11,b
line (0,0)-(w-1,h-1),11,b'

'  draw graph ?


'sleep
'
'
end sub
'
' ----------------------------------------------------------------
'

'
' ----------------------------------------------------------------
'
sub ms3
'
'    Draw buttons using data and select using array values .
'
'
static as integer CurrentX , CurrentY , MouseButtons
'
static as single x1,x2,y1,y2
static as integer colour,colorxy
static as integer i,j,k,n

static As Any Ptr img1 , img2 , img3 , imgB

imgB= ImageCreate( 100, 50, RGB(0, 0, 0) )


'
'                            Read data .
'
n=5
dim as single bxyc(0 to n,0 to 2)
restore bxd
for i=0 to n
   read bxyc(i,0)
next i
restore byd
for i=0 to n
   read bxyc(i,1)
next i
restore bcd
for i=0 to n
   read bxyc(i,2)
next i
'
'
'cls
'

'
'  Dedicated routine for this .
'
'color 15,2
'locate 2,7
'print "Run"
'
img1 = bmp_load( "run.bmp" )

If img1 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 10), img1

 '   ImageDestroy( img1 )

End If

img2 = bmp_load( "draw.bmp" )

If img2 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 60), img2
   ' Put (10, 440), img2
   ' ImageDestroy( img2 )

End If
img3 = bmp_load( "exit.bmp" )

If img3 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 110), img3

   ' ImageDestroy( img3 )

End If

for i=0 to n step 2
    x1=bxyc(i,0)
    x2=bxyc(i+1,0)
    y1=bxyc(i,1)
    y2=bxyc(i+1,1)
    colour = bxyc(i,2)
    colour=rgb(colour,12,12)
'   
    'line (x1,y1)-(x2,y2),7,bf
   ' line (x1,y1)-(x2,y2),15,b
   ' line (x1+4,y1+4)-(x2-4,y2-4),colour,bf
'    line (x1+4,y1+4)-(x2-4,y2-4),rgb(150,150,150),b
    line (x1,y1)-(x2,y2),rgb(150,150,150),b
   
   
'
next i
'
'
'Put (10, 250), img2
'



'color 15,1
'locate 6,7
'print "Draw"

'color 15,4
'locate 9,7
'print "Exit"



'
'                          Select button .
'
MouseButtons = 0
    k=0
while ( Inkey = "" )

   GetMouse CurrentX, CurrentY, , MouseButtons
   
   If MouseButtons And LEFTBUTTON Then
      colour = Point(CurrentX,CurrentY)
   
    for i = 0 to n step 2
    '
          k = 0
         x1 = bxyc(i,0)
         x2 = bxyc(i+1,0)
         y1 = bxyc(i,1)
         y2 = bxyc(i+1,1)
     colorxy = bxyc(i,2)
'       If CurrentY > y1+4 and CurrentY < y2 -4 and CurrentX > x1+4 and CurrentX < x2 - 4 and colour = colorxy then
       If CurrentY > y1+4 and CurrentY < y2 -4 and CurrentX > x1+4 and CurrentX < x2 - 4  then
       
        'color colorxy
        color rgb(180,18,240)
        Locate 23,4
       
        j=int(i/2)
        select case j
              case 0:
               Put (10, 440), img1

              '  print " Run "
                '
                ' plot 3d
                sleep 1200 ' mostly for animated image .
                Put (10, 440), imgB,Pset
               
               
              case 1:
                 Put (10, 440), img2
               ' print " Draw "
                window (-1,-1)-(1,1)
                view(118,0)-(679,479)
               
                plot2d
               
                sleep 1200 ' may not be necessary for this .
               
               '   Return to native resolution .
       
                window
                view
                 Put (10, 440), imgB,Pset
 
              case 2:
               Put (10, 440), img3
               
                print " Exit "
                k=1
                exit for
              case else   
        end select
 
      End If
     '
    next i
    '
    if k=1 then exit while
      End If
wend   
'
if k=1 then
   color rgb(200,120,60)
   Locate 24,4
   print " Done  "
end if
'
sleep 400
'
ImageDestroy( img1 )
 ImageDestroy( img2 )
 ImageDestroy( img3 )
'
end sub
'
' ---------------------------------------------------------------------
'
sub plot2d
'
'
'
static as single x,y,yp,dx
static as integer colour
'
colour = rgb(240,240,240)               
line(-1,-1)-(1,1),colour,b
line(0,-1)-(0,1),colour
line(-1,0)-(1,0),colour
 
colour = rgb(12,120,200)

dx =2/600

for x=-1 to 1 - dx step dx
    y=sin(x*6.28)
    yp =sin((x+dx)*6.28)
   line (x,y)-(x+dx,yp),colour
next x
'
colour = rgb(240,240,240)
line(-1,-1)-(1,1),colour,b
line(0,-1)-(0,1),colour
line(-1,0)-(1,0),colour
'
end sub
'
' -------------------------------------------------------------------------
'
Function bmp_load( ByRef filename As Const String ) As Any Ptr

    Dim As Long filenum, bmpwidth, bmpheight
    Dim As Any Ptr img

    '' open BMP file
    filenum = FreeFile()
    If Open( filename For Binary Access Read As #filenum ) <> 0 Then Return NULL

        '' retrieve BMP dimensions
        Get #filenum, 19, bmpwidth
        Get #filenum, 23, bmpheight

    Close #filenum

    '' create image with BMP dimensions
    img = ImageCreate( bmpwidth, Abs(bmpheight) )

    If img = NULL Then Return NULL

    '' load BMP file into image buffer
    If BLoad( filename, img ) <> 0 Then ImageDestroy( img ): Return NULL

    Return img

End Function
'
' -------------------------------------------------------------------------
'

Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI

Postby Luxan » May 18, 2019 23:38

A bit of an update .

I've included an else sleep 25 , in the main loop and the while conditional is (k = 0) .

This means less CPU usage , previously using 100% of one of my cores ; I used
htop to examine this. There may still be scope for improving this.

There is now more code in the select case construct .

I'd prefer sourceforge over github if I was to start a significant project .

Code: Select all


'
'   (c) Copyright 2019
'
'
'    sciwiseg@gmail.com
'
'   Dependent upon various FreeBASIC examples , from the documentation .
'
'   Mouse  routine  2
'
'   Note Inkey$ is deprecated , use Inkey instead .
'
Const LEFTBUTTON   = 1
Const MIDDLEBUTTON = 4   ' UNUSED IN THIS DEMO
Const RIGHTBUTTON  = 2   ' UNUSED IN THIS DEMO
Const SHOWMOUSE    = 1
Const HIDEMOUSE    = 0
'
Dim CurrentX     As Integer
Dim CurrentY     As Integer
Dim MouseButtons As Integer
Dim CanExit      As Integer
'
' ----------------------------------------------------------
'
declare sub pxy1()
declare sub ms3()

declare sub plot2d()
'
' ---------------------------------------------------------
'
'' A function that creates an image buffer with the same
'' dimensions as a BMP image, and loads a file into it.

Const NULL As Any Ptr = 0

declare Function bmp_load( ByRef filename As Const String ) As Any Ptr
dim As Any Ptr img1

'
' ----------------------------------------------------------------------
'
'Screen 12,24

'ScreenRes 640, 480, 32 ' screen 12 dimensions .

screen 18,16' screen 12 dimensions .

'window (-1,-1)-(1,1)
'view(118,0)-(679,479)
'line(-1,-1)-(1,1),11,b

'line(118,0)-(679,479),rgb(12,120,120),b
SetMouse 1, 1, SHOWMOUSE
CanExit = 1



'pxy1
ms3

'window (-1,-1)-(1,1)
'view(118,0)-(679,479)
'line(-1,-1)-(1,1),11,b


sleep 100


 
'
'
end
'
' ======================================================================
'
'  Button location and colour .
'
'
bxd:
data 10,110
data 10,110
data 10,110
byd:
data 10,50
data 60,100
data 110,150
bcd:
data 2,2,1,1,4,4
'
' ----------------------------------------------------------------
'
sub pxy1
'
'   Draw boundaries for graph .
'
static as integer  w , h
static as integer depth
static as string  driver_name

'Screen 15, 32
' Obtain info about current mode
ScreenInfo w, h, depth,,,,driver_name
'Print Str(w) + "x" + Str(h) + "x" + Str(depth);
'Print " using " + driver_name + " driver"
'Sleep
' Quit graphics mode and obtain info about desktop
'Screen 0
'ScreenInfo w, h, depth
Print "Desktop running at " + Str(w) + "x" + Str(h) + "x" + Str(depth);

line (118,0)-(w-1,h-1),11,b
line (0,0)-(w-1,h-1),11,b'

'  draw graph ?


'sleep
'
'
end sub
'
' ----------------------------------------------------------------
'

'
' ----------------------------------------------------------------
'
sub ms3
'
'    Draw buttons using data and select using array values .
'
'
static as integer CurrentX , CurrentY , MouseButtons
'
static as single x1,x2,y1,y2,x,y,u,v , r,g,b
static as integer colour,colorxy
static as integer i,j,k,n

static As Any Ptr img1 , img2 , img3 , imgB

imgB= ImageCreate( 100, 50, RGB(0, 0, 0) )


'
'                            Read data .
'
n=5
dim as single bxyc(0 to n,0 to 2)
restore bxd
for i=0 to n
   read bxyc(i,0)
next i
restore byd
for i=0 to n
   read bxyc(i,1)
next i
restore bcd
for i=0 to n
   read bxyc(i,2)
next i
'
'
'cls
'

'
'  Dedicated routine for this .
'
'color 15,2
'locate 2,7
'print "Run"
'
img1 = bmp_load( "run.bmp" )

If img1 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 10), img1

 '   ImageDestroy( img1 )

End If

img2 = bmp_load( "draw.bmp" )

If img2 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 60), img2
   ' Put (10, 440), img2
   ' ImageDestroy( img2 )

End If
img3 = bmp_load( "exit.bmp" )

If img3 = NULL Then
    Print "bmp_load failed"

Else

    Put (10, 110), img3

   ' ImageDestroy( img3 )

End If

for i=0 to n step 2
    x1=bxyc(i,0)
    x2=bxyc(i+1,0)
    y1=bxyc(i,1)
    y2=bxyc(i+1,1)
    colour = bxyc(i,2)
    colour=rgb(colour,12,12)
'   
    'line (x1,y1)-(x2,y2),7,bf
   ' line (x1,y1)-(x2,y2),15,b
   ' line (x1+4,y1+4)-(x2-4,y2-4),colour,bf
'    line (x1+4,y1+4)-(x2-4,y2-4),rgb(150,150,150),b
    line (x1,y1)-(x2,y2),rgb(150,150,150),b
   
   
'
next i
'
'
'Put (10, 250), img2
'



'color 15,1
'locate 6,7
'print "Draw"

'color 15,4
'locate 9,7
'print "Exit"



'
'                          Select button .
'
MouseButtons = 0
    k=0
while ( k = 0 )

   GetMouse CurrentX, CurrentY, , MouseButtons
   
   If MouseButtons And LEFTBUTTON Then   '  if0
      colour = Point(CurrentX,CurrentY)
   
    for i = 0 to n step 2
    '
          k = 0
         x1 = bxyc(i,0)
         x2 = bxyc(i+1,0)
         y1 = bxyc(i,1)
         y2 = bxyc(i+1,1)
     colorxy = bxyc(i,2)
'       If CurrentY > y1+4 and CurrentY < y2 -4 and CurrentX > x1+4 and CurrentX < x2 - 4 and colour = colorxy then
       If CurrentY > y1+4 and CurrentY < y2 -4 and CurrentX > x1+4 and CurrentX < x2 - 4  then
       
        'color colorxy
        color rgb(180,18,240)
        Locate 23,4
       
        j=int(i/2)
        select case j
              case 0:
                 Put (10, 440), img1

               window   (-1.01,-1.01)-(1.01,1.01)

                view(118,0)-(679,479)
                line(-1,-1)-(1,1),rgb(0,0,0),bf
                line(-1,-1)-(1,1),rgb(11,145,187),b

'
'
'
for k=0 to 200
     x=-1+2*rnd
     y=-1+2*rnd
     u=-1+2*rnd
     v=-1+2*rnd
     r=cint(255*rnd)
     g=cint(255*rnd)
     b=cint(255*rnd)
  line(x,y)-(u,v),rgb(r,g,b)   
next k
k=0

               window
                view

              '  print " Run "
                '
                ' plot 3d
                'sleep 120 ' mostly for animated image .
                Put (10, 440), imgB,Pset
             
               
              case 1:
                   Put (10, 440), img2
               ' print " Draw "
                window (-1.01,-1.01)-(1.01,1.01)
                view(118,0)-(679,479)
                line(-1,-1)-(1,1),rgb(0,0,0),bf
                line(-1,-1)-(1,1),rgb(11,145,187),b
 
                plot2d
               
          '      sleep 1200 ' may not be necessary for this .
               
               '   Return to native resolution .
       
                window
                view
                 Put (10, 440), imgB,Pset           
             
 
              case 2:
               Put (10, 440), img3
               
                print " Exit "
                k=1
                exit for
              case else   
        end select
 
      End If
     '
    next i
    '
 '   if k=1 then exit while
   
    else
   
      sleep 25
   
      End If  ' if0
wend   
'
if k=1 then
   color rgb(200,120,60)
   Locate 24,4
   print " Done  "
end if
'
sleep 400
'
ImageDestroy( img1 )
 ImageDestroy( img2 )
 ImageDestroy( img3 )
'
end sub
'
' ---------------------------------------------------------------------
'
sub plot2d
'
'
'
static as single x,y,yp,dx
static as integer colour
'
colour = rgb(240,240,240)               
line(-1,-1)-(1,1),colour,b
line(0,-1)-(0,1),colour
line(-1,0)-(1,0),colour
 
colour = rgb(12,120,200)

dx =2/600

for x=-1 to 1 - dx step dx
    y=sin(x*6.28)
    yp =sin((x+dx)*6.28)
   line (x,y)-(x+dx,yp),colour
next x
'
colour = rgb(240,240,240)
line(-1,-1)-(1,1),colour,b
line(0,-1)-(0,1),colour
line(-1,0)-(1,0),colour
'
end sub
'
' -------------------------------------------------------------------------
'
Function bmp_load( ByRef filename As Const String ) As Any Ptr

    Dim As Long filenum, bmpwidth, bmpheight
    Dim As Any Ptr img

    '' open BMP file
    filenum = FreeFile()
    If Open( filename For Binary Access Read As #filenum ) <> 0 Then Return NULL

        '' retrieve BMP dimensions
        Get #filenum, 19, bmpwidth
        Get #filenum, 23, bmpheight

    Close #filenum

    '' create image with BMP dimensions
    img = ImageCreate( bmpwidth, Abs(bmpheight) )

    If img = NULL Then Return NULL

    '' load BMP file into image buffer
    If BLoad( filename, img ) <> 0 Then ImageDestroy( img ): Return NULL

    Return img

End Function
'
' -------------------------------------------------------------------------
'




Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI and gtk

Postby Luxan » May 26, 2019 11:06

I'm presently attempting to construct a glade template
that Is reusable .

Part of this necessitates that I not only be able to save files ,
I also require a message dialog that asks for an overwrite or not .

Thus far , this is the code that I've constructed , using fragments
from various FreeBASIC GTK applications .
With this I am able to save a file and selectively overwrite a file .

However a warning message is generated when I save a file :

(Unsaved 5:12594): Gtk-CRITICAL **: IA__gtk_entry_set_text:
assertion 'GTK_IS_ENTRY (entry)' failed

.

You will require all of these files and an installation of gtk 2.xx to examine this code .


Any suggestions .


Unsaved 5.glade


Code: Select all



<?xml version="1.0" encoding="UTF-8"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.24 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <widget class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button3">
            <property name="label" translatable="yes">save</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="clicked" handler="on_button3_clicked" />
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button2">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>





Unsaved 5.decl


Code: Select all


DECLARE SUB on_button3_clicked CDECL ALIAS "on_button3_clicked" (BYVAL objct AS GtkObject PTR, BYVAL user_data as gpointer)






Unsaved 5.bas


Code: Select all


'* ---------------------------------------------------------------------------
'* Program shell generated by utility 'glade2bas'
'* provided by Klaus Siebke, http://www.siebke.com
'* Dieser Programmrahmen wurde generiert durch das Werkzeug 'glade2bas'
'*
'*
'* Generated at / Generierung am 21/05/2019 18:43
'* ---------------------------------------------------------------------------

'******************************************************************************
'* Program name: Unsaved 5
'*
'* Version:      x.x
'*
'* Author:       xxx
'*
'* Description :
'* --------------------------
'*
'* xxx
'*
'* License :
'* ----------------
'*
'* Please prefer the GNU GENERAL PUBLIC LICENSE to support free software
'* For more information please visit: http://www.fsf.org
'*
'*
'*
'******************************************************************************
'*
'*
'*  H I N T 
'*
'*  If the final application shall only show the GUI and not the command window,
'*  compile the application with "fbc -s gui <progname>.bas"
'*
'*
'*
'******************************************************************************
'* Include to be compatible with some VB commands
'******************************************************************************
#include "vbcompat.bi"

'******************************************************************************
'* Includes for Gtk
'******************************************************************************
#include "gtk/gtk.bi"
#include "glade-xml.bi"
'#define NULL 0

'******************************************************************************
'* Subroutines (callbacks) or 'events' in Visual Basic notation
'*
'******************************************************************************
'1) corresponding to the signals in the glade xml file
'   
#include "Unsaved 5.decl"

'2) own routines
''declare sub xxx()

'******************************************************************************
'* Data definitions
'******************************************************************************
dim shared xml           as GladeXML ptr
dim shared slash         as string
dim shared window1   as GtkWidget ptr

'dim shared xml                   as GladeXML  ptr
dim shared toplevel              as GtkWidget ptr
'dim shared messagedialog         as GtkWidget ptr
dim shared parent                as GtkWindow ptr
' shared lblMessage            as GtkWidget ptr
' shared msgbuffer             as string
dim shared retcd                 as integer
'dim shared userid                as string
'dim shared answer                as integer
dim shared pathnm                as string
dim shared filenm                as string
'dim shared slash                 as string
dim shared appldir               as string
'dim shared l                     as integer
'dim shared filnm_glade           as string
'dim shared filnm_decl            as string
'dim shared filnm_bas             as string
'dim shared filnm_net             as string
'dim shared cnt_signal            as integer
'dim shared cnt_window            as integer
'redim shared callbacks(1 to 500) as string
'redim shared code(1 to 1000)     as string
'redim shared winids(1 to 100)    as string

'******************************************************************************
'*
'*  =========================
'*  START OF THE MAIN PROGRAM
'* 
'*  =========================
'*
'******************************************************************************

'******************************************************************************
'*  Choose correct slash for the OS
'******************************************************************************
    slash = "/"
    #ifdef __FB_WIN32__
    '...instructions only for Win32...
       slash = "\"
    #endif

'******************************************************************************
'*  Initialize Gtk / GUI
'******************************************************************************
    gtk_init( NULL, NULL )

'******************************************************************************
'*  Specify the windows to be shown
'******************************************************************************
    xml = glade_xml_new( "Unsaved 5.glade", NULL, NULL )

'******************************************************************************
'*  Reference some widgets
'******************************************************************************
    window1 = glade_xml_get_widget( xml, "window1" )


'******************************************************************************
'*  Display main window(s) / Hauptfenster anzeigen
'******************************************************************************

    gtk_widget_show_all( window1)

    glade_xml_signal_autoconnect( xml )

'******************************************************************************
'*  The main GUI loop starts / Hier beginnt die eigentliche GUI-Verarbeitung
'*  ------------------------------------------------------------------------
'*
'*  I M P O R T A N T  :
'*
'*  gtk_main can only be terminated by function gtk_main_quit(), so remember to
'*  define a signal to quit the application and call gtk_main_quit() in the
'*  associated SUB - e.g. by signal on_xxx_delete_event (xxx = window name)
'*
'*
'******************************************************************************
    gtk_main( )

'******************************************************************************
'*  Wrap up
'******************************************************************************
'   a) unattach xml reference / xml Referenz abbauen
    g_object_unref( xml )

'   b) Finish program with return code 0 / Programm mit Returncode 0 beenden
    end 0


'******************************************************************************
'*
'*  =======================
'*  END OF THE MAIN PROGRAM
'* 
'*  =======================
'*
'******************************************************************************

'------------------------------------------------------------------------------
'  now the SUB programs start ...  (SUB's) ...
'------------------------------------------------------------------------------

SUB on_button3_clicked CDECL ALIAS "on_button3_clicked" (BYVAL objct AS GtkObject PTR, BYVAL user_data as gpointer) EXPORT
'******************************************************************************
'* put your code below for signal on_button3_clicked
'******************************************************************************
'
'  Save data to a particular file , with user selected filename .
'
'
'
  Dim declfile    as Integer
'  Dim signal      as string
  Dim sub_decl    as string
  Dim retcd       as integer
'  Dim winid       as string
'
  dim as integer one
  dim as GtkWidget ptr dialog
  dim as gtkfilefilter ptr filter
  dim as zstring ptr filename
  dim as zstring ptr pathname

     appldir = curdir
     
     one = 1
'
' .............................................................................
'
  gtk_init( 0, NULL )
 

  dialog = gtk_file_chooser_dialog_new( "Save File", _
                                 NULL, _
                                 GTK_FILE_CHOOSER_ACTION_SAVE, _
                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, _
                                 GTK_STOCK_SAVE,GTK_RESPONSE_ACCEPT, _
                                 NULL )
                            
  gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), appldir)

  filter = gtk_file_filter_new()
  gtk_file_filter_set_name(filter, ("glade files(*.glade)"))
  gtk_file_filter_add_pattern(filter, "*.glade")
  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter)
 'filter = gtk_file_filter_new()
 'gtk_file_filter_set_name(filter, ("All files(*.*)"))
 'gtk_file_filter_add_pattern(filter, "*")
 'gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter)   
 
  gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), one)
 
  parent = GTK_WINDOW (toplevel)
  gtk_window_set_transient_for (GTK_WINDOW(dialog), parent)                               
'
'
  if (gtk_dialog_run( GTK_DIALOG( dialog ) ) = GTK_RESPONSE_ACCEPT ) then
      filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) )
      filenm = *filename
     pathname = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER( dialog ) )
     pathnm = *pathname
     gtk_entry_set_text (GTK_ENTRY (glade_xml_get_widget( xml, "entFilnm" )), *filename)
     g_free( filename )
  end if
'
     gtk_widget_destroy( GTK_WIDGET(dialog) )
'
' .............................................................................
'
'  This might be any type of data .
'

'    sub_decl="1234567890qwertyuuiop[]asdfghjkl;'\<zxcvbnm,./"

     sub_decl=" another item "
     
     declfile  = FreeFile
     retcd = Open (filenm for output as #declfile)
     
        print #declfile, sub_decl
   
     Close #declfile
'
'
END SUB





glade-xml.bi



Code: Select all


''
''
'' glade-xml -- header translated with help of SWIG FB wrapper
''
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
''         be included in other distributions without authorization.
''
''
#ifndef __glade_xml_bi__
#define __glade_xml_bi__

extern "c" lib "glade-2.0"

#include once "glib.bi"
#include once "gtk/gtk.bi"

#define GLADE_TYPE_XML (glade_xml_get_type())
#define GLADE_XML(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_XML, GladeXML))
#define GLADE_XML_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_XML, GladeXMLClass))
#define GLADE_IS_XML(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_XML))
#define GLADE_IS_XML_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), GLADE_TYPE_XML))
#define GLADE_XML_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GLADE_TYPE_XML, GladeXMLClass))

type GladeXML as _GladeXML
type GladeXMLClass as _GladeXMLClass
type GladeXMLPrivate as _GladeXMLPrivate

type _GladeXML
   parent as GObject
   filename as zstring ptr
   priv as GladeXMLPrivate ptr
end type

type _GladeXMLClass
   parent_class as GObjectClass
   lookup_type as function cdecl(byval as GladeXML ptr, byval as zstring ptr) as GType
end type

declare function glade_xml_get_type () as GType
declare function glade_xml_new (byval fname as zstring ptr, byval root as zstring ptr, byval domain as zstring ptr) as GladeXML ptr
declare function glade_xml_new_from_buffer (byval buffer as zstring ptr, byval size as integer, byval root as zstring ptr, byval domain as zstring ptr) as GladeXML ptr
declare function glade_xml_construct (byval self as GladeXML ptr, byval fname as zstring ptr, byval root as zstring ptr, byval domain as zstring ptr) as gboolean
declare sub glade_xml_signal_connect (byval self as GladeXML ptr, byval handlername as zstring ptr, byval func as GCallback)
declare sub glade_xml_signal_connect_data (byval self as GladeXML ptr, byval handlername as zstring ptr, byval func as GCallback, byval user_data as gpointer)
declare sub glade_xml_signal_autoconnect (byval self as GladeXML ptr)

type GladeXMLConnectFunc as sub cdecl(byval as zstring ptr, byval as GObject ptr, byval as zstring ptr, byval as zstring ptr, byval as GObject ptr, byval as gboolean, byval as gpointer)

declare sub glade_xml_signal_connect_full (byval self as GladeXML ptr, byval handler_name as zstring ptr, byval func as GladeXMLConnectFunc, byval user_data as gpointer)
declare sub glade_xml_signal_autoconnect_full (byval self as GladeXML ptr, byval func as GladeXMLConnectFunc, byval user_data as gpointer)
declare function glade_xml_get_widget (byval self as GladeXML ptr, byval name as zstring ptr) as GtkWidget ptr
declare function glade_xml_get_widget_prefix (byval self as GladeXML ptr, byval name as zstring ptr) as GList ptr
declare function glade_xml_relative_file (byval self as GladeXML ptr, byval filename as zstring ptr) as zstring ptr
declare function glade_get_widget_name (byval widget as GtkWidget ptr) as zstring ptr
declare function glade_get_widget_tree (byval widget as GtkWidget ptr) as GladeXML ptr

type GladeXMLCustomWidgetHandler as function cdecl(byval as GladeXML ptr, byval as zstring ptr, byval as zstring ptr, byval as zstring ptr, byval as zstring ptr, byval as gint, byval as gint, byval as gpointer) as GtkWidget

declare sub glade_set_custom_handler (byval handler as GladeXMLCustomWidgetHandler, byval user_data as gpointer)

#define glade_xml_new_with_domain glade_xml_new
#define glade_xml_new_from_memory glade_xml_new_from_buffer

end extern

#endif





jj2007
Posts: 1210
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Fb minimal GUI

Postby jj2007 » May 26, 2019 11:20

Luxan wrote: I've included an else sleep 25 , in the main loop and the while conditional is (k = 0) .

This means less CPU usage , previously using 100% of one of my cores ; I used
htop to examine this. There may still be scope for improving this.
If you see high cpu use in a Windows application, there is something fundamentally wrong. Under normal circumstances, cpu use should be 0%. That is what the message loop is designed for. Can't speak for Linux, though.
Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI

Postby Luxan » May 27, 2019 0:26

I've heard about some supposedly professional software suffering from the inappropriate
use of loop structures for handling tasks like mouse or keyboard entries .

For a moment I thought that this might be the instance with gtk and the code that I'm
constructing with that ; this isn't the instance and the CPU usage tends to be less than 1% .

For the minimal code , written entirely in FreeBASIC with the sleep 24 instruction , CPU
usage is even less .
Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI

Postby Luxan » May 27, 2019 1:04

I believe I've sorted out the cause of the error message from my gtk example .

The original code that I copied this from has a text box , to the left of the button , where
the filename is displayed ; I'm not using a text box like that at the moment , therefore I
shouldn't attempt to set any text .


Code: Select all



'
  if (gtk_dialog_run( GTK_DIALOG( dialog ) ) = GTK_RESPONSE_ACCEPT ) then
      filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) )
      filenm = *filename
     pathname = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER( dialog ) )
     pathnm = *pathname

  '   gtk_entry_set_text (GTK_ENTRY (glade_xml_get_widget( xml, "entFilnm" )), *filename) ' ' this is for the text box , to display filename .

     g_free( filename )
  end if
'




With the entry commented out there's no longer an error or warning message .

Eventually I shall tidy up all of the gtk code and re post .
TJF
Posts: 3474
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Fb minimal GUI

Postby TJF » May 29, 2019 19:55

Hi Luxan!

Why don't you use GladeToBac in order to auto-generate the main source in a more structured way? And why do you use the external and outdated libglade? Today, all features (and some more) are integrated in GtkBuilder.

In order to update your code, you can simply start Glade3 and load your file Unsaved 5.glade. Then save it as Unsaved 5.ui for a modern GtkBuilder equivalent. Afterwards load that file in GladeToBac and create the modern main source code. GladeToBac can also manage the callback functions for you.

Note: your file Unsaved 5.decl contains a forward declaration. So it's a header file and should get the suffix .bi.

Regards

PS:
Luxan wrote:However a warning message is generated when I save a file :

(Unsaved 5:12594): Gtk-CRITICAL **: IA__gtk_entry_set_text:
assertion 'GTK_IS_ENTRY (entry)' failed
There's no widget called entFilnm in your Glade file, so the function glade_xml_get_widget() returns a NULL pointer.
Luxan
Posts: 75
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: Fb minimal GUI

Postby Luxan » Jun 08, 2019 15:02

I'm having some difficulty getting glade2bac to work ; I've tried many approaches , including compiling from source . The inc/TJF directory was absent from the download.

I mostly just wanted to get started with gtk for a template that I might re use , even the
ability to save a file via a menu was impossible with glade 3.08 , is this different for gtk builder ?
I'm having to assume that libglade is valid on newer distributions ; I'm using Ubuntu 14.04
as the development environment as newer versions don't have a glade that utilizes libglade.
TJF
Posts: 3474
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Fb minimal GUI

Postby TJF » Jun 10, 2019 12:15

Hi Luxan!
Luxan wrote:I'm having to assume that libglade is valid on newer distributions ; I'm using Ubuntu 14.04
as the development environment as newer versions don't have a glade that utilizes libglade.
libglade is obsolete. There's now support on newer systems. Glade3 doesn't support it any more. Today, it requires GtkBuilder and Gtk-3.08 minimum.
Luxan wrote:I'm having some difficulty getting glade2bac to work ; I've tried many approaches , including compiling from source . The inc/TJF directory was absent from the download.
I checked this. The folder inc is obsolete. It was used for custom bindings in a time when FreeBASIC doesn't ship with Gtk-3 headers.

But there was some 32 bit code in the source, which I adapted for 64 bit system. I also fixed some issues from my ToDo list. The project doesn't contain binaries any more, and is now hosted at GitHub. You should be able to compile that source on your system. Here I used XUbuntu-16.04, fbc-1.07, Gtk-3.18.9, Glade-3.18.3.
Luxan wrote:I mostly just wanted to get started with gtk for a template that I might re use , even the
ability to save a file via a menu was impossible with glade 3.08 , is this different for gtk builder ?
GtkBuilder is a little more typing since you need more instance casts. On the other hand side you can shift more things in to the UI file, ie. defining filters without any FB code.

The above mentioned repository includes a folder Demo with example code on how to use the GtkFileChooser widget. The resulting application creates or removes files on the local harddisc.

Regards

[Edit] Typo [/Edit]

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 18 guests