FLTK C for FreeBASIC Jan 09, 2017

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby D.J.Peters » May 01, 2017 6:35

owen wrote:how do i get mouse x,y from image or the box containing the image?
You are killing me I get all my mouse coords from eBay :-)

There are more than ten solution to get such things like mouse coords in FLTK.
It depends from on which point in your application do you need it.

first take a look at this small code:

Code: Select all

#include once "fltk-c.bi"
var win = Fl_WindowNew(320,240)
var box = Fl_BoxNew2(BoxType(FL_GLEAM_DOWN_BOX),50,50,Fl_WidgetGetW(win)-100,Fl_WidgetGetH(win)-100)
Fl_WindowShow(win)
Fl_Run()
Aftert the call of Fl_Run() your GUI application is running and I can't see the point where you get any mouse coords.

Ok so far ?

As an FLTK beginners the only chance are to interact with your GUI application are you don't use Fl_Run() instead you can use Fl_Wait().

Fl_Wait() waits for any event and returns 0 if no more Windows are open.

Here are how this beginner friendly solution works.

Code: Select all

#include once "fltk-c.bi"
var win = Fl_WindowNew(320, 240)
var box = Fl_BoxNew2(BoxType(FL_GLEAM_DOWN_BOX),50,50,Fl_WidgetGetW(win)-100,Fl_WidgetGetH(win)-100)
Fl_WindowShow(win)

while Fl_Wait()
  var event=Fl_EventNumber()
  ' is it a mouse move and inside the box
  if event = FL_EVENT_MOVE and Fl_EventInside(box) then
    ' print the mouse coords relative to the box X,Y origin
    print event,Fl_EventX()-Fl_WidgetGetX(box),Fl_EventY()-Fl_WidgetGetY(box)
  end if 
wend
Before I show you other solutions play with some kinds of events here are some functions you can try

Fl_EventNumber, Fl_EventButton,Fl_EventButton1, Fl_EventButton2, Fl_EventButton3
Fl_EventButtons, Fl_GetEventClicks, Fl_GetEventIsClick, Fl_EventKey
Fl_EventAlt , Fl_EventCtrl, Fl_EventShift, Fl_EventCommand, Fl_EventText
Fl_EventX, Fl_EventY, Fl_EventXRoot, Fl_EventYRoot, Fl_EventDX, Fl_EventDY
Fl_EventClipboardData, Fl_EventClipboardType

But remember, if you book me as your personal FLTK coach I'm not cheap. :lol:

Joshy
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby D.J.Peters » May 01, 2017 7:58

Ok let's started may be other FLTK beginners are reading here also and will learn the primary idea of FLTK.

Again take a look at this small code the result are the same as before but I made one tiny change.

Code: Select all

#include once "fltk-c.bi"
var win = Fl_WindowNew(320,240)
var box = Fl_BoxExNew(50,50,Fl_WidgetGetW(win)-100,Fl_WidgetGetH(win)-100)
Fl_WidgetSetBox(box,BoxType(FL_GLEAM_DOWN_BOX))

Fl_WindowShow(win)

while Fl_Wait()
  var event=Fl_EventNumber()
  ' is it a mouse move and inside the box
  if event = FL_EVENT_MOVE and Fl_EventInside(box) then
    ' print the mouse coords coords inside the box
    print event,Fl_EventX()-Fl_WidgetGetX(box),Fl_EventY()-Fl_WidgetGetY(box)
  end if 
wend
Do you found the difference ?

If not compare the line where I create the Box.

May be you ask your self "What to hell is a BoxEx widget"

While I wrote the FLTK wrapper I added for all(*) FLTK Widget a second version.
(*) all means really all widgets exists in two versions.

The "normal" version of the FLTK widgets are this with only one defaut callback.

For example a FLTK button has only one callback and this callback will be trigger if anyone press the button.
Thats all what the "normal" button widget of the C++ wrapper can do for you.

Another example the only callback of an "normal" slider widget will be trigger if a value of the slider are changed.
Not more not less :-)

The only callback of a Window for example are called if the user close it via mouse or by the "hide" command.
Inside this default callback you can ignore it and an ESCAPE key won't close your running application anymore.

Back to the Ex not your ex girlfriend this is another bad story. :-)

The secret of the ...Ex widgets are:
you can overwrite all "normaly hidden" function and methods of the normal version of the same widget.

Let us begin simple we overwrite the hidden event handler callback with our own version.

Code: Select all

#include once "fltk-c.bi"

function MyBoxHandleCB cdecl(byval me as any ptr, byval event as FL_EVENT) as long
  ' ! in this simple case we only handle the mouse move event !
  if event = FL_EVENT_MOVE then
    print Fl_EventX()-Fl_WidgetGetX(me),Fl_EventY()-Fl_WidgetGetY(me)
    return 1
  end if
  ' all events our "BoxEx Widget class" we don't use e.g. resize, redraw and so one ...
  ' should be deligated to the "normal BoxWidget" it's the base class of our BoxExWidget class.
  return Fl_BoxExHandleBase(me,event)
end function
'
' main
'
var win = Fl_WindowNew(320,240)
var box = Fl_BoxExNew(50,50,Fl_WidgetGetW(win)-100,Fl_WidgetGetH(win)-100)
Fl_WidgetSetBox(box,BoxType(FL_GLEAM_DOWN_BOX))
Fl_BoxExSetHandleCB(box,@MyBoxHandleCB)
Fl_WindowShow(win)
Fl_Run()
May be this is the most perfect answer of your last question.
"how do i get mouse x,y from image or the box containing the image ?"

This was the first part of "FLTK-C For Dummies" :-)

It's time for me to go sleeping it's 9:30 at morning here in germany and I waked up two days before. :-)

But "I will be back" and terminate you all FLTK haters.

Joshy
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 02, 2017 0:34

draw lines and circles
zoom in and out with the mouse wheel

Code: Select all

#Include once "fltk-c.bi"

Declare Sub Create_Window_Main ()
Declare Sub draw_lines Cdecl (widget As FL_Widget Ptr)
Declare Sub draw_circles Cdecl (widget As FL_Widget Ptr)
Declare Sub myfbgfxtofltkimg Cdecl (byval UserData as any ptr)
Declare Function calcd(cd1 as Double,cd2 as Double,cd3 as Double,cd4 as Double,cd5 as Double,cd6 as Double) as double

Declare Sub mymain()
Declare Sub drawlines
Declare Sub drawcircles
Declare Sub zoomin()
Declare Sub zoomout()
Declare Sub zoominpan()
Declare Sub zoomoutpan()
Declare Sub escapeme()

'Windows and widgets:
Dim Shared As Fl_Window Ptr win
Dim Shared As Fl_Box Ptr box
Dim Shared As Fl_Button Ptr btn_draw_line
Dim Shared As Fl_Button Ptr btn_draw_circle
Dim Shared As Fl_Image Ptr img
'Dim Shared As Fl_EventNumber Ptr en


Dim Shared mywidth As Integer
Dim Shared myheight As Integer
Dim Shared As Integer myfltkimage_mouse_x, myfltkimage_mouse_y
Dim Shared As BOOLEAN mbp,mousebuttondown, mousebuttonup,myfltkimage_mouse_scroll_up,myfltkimage_mouse_scroll_down
Dim Shared As Integer mswp
Dim Shared As BOOLEAN drawingline=FALSE, drawingcircle=FALSE
Dim Shared As Integer linec,circlec
Dim Shared As Double lines(100,4), circles(100,3)
Dim Shared As String drawing_entity
drawing_entity="line"
Dim Shared As Integer mouse_clicks
Dim Shared As Integer wx1,wy1,wx2,wy2,wzoom,wzoomt,maxzoomin,maxzoomout

mywidth=580
myheight=580
'ScreenRes mywidth,myheight,,,fb.GFX_NULL
wx1=0
wy1=0
wx2=mywidth-1
wy2=myheight-1
wzoomt=10
maxzoomin=10
maxzoomout=10000



Dim Shared As Integer w,h,pitch_src
Dim Shared As Integer bytes_img=3 ' RGB
ScreenRes mywidth,myheight,,,-1
ScreenInfo w,h,,,pitch_src
Dim Shared As Integer pitch_img
pitch_img=w*bytes_img
Dim Shared As UByte Ptr rgb_img
rgb_img=allocate(h*pitch_img)

window (wx1,wy1)-(wx2,wy2)


Create_Window_Main ()
Fl_WidgetSetCallback0 btn_draw_line, @draw_lines
Fl_WidgetSetCallback0 btn_draw_circle, @draw_circles

Fl_AddIdle @mymain, box

Fl_WindowShow(win)
Fl_Run
DeAllocate rgb_img

End


Sub draw_lines Cdecl (widget As FL_Widget Ptr)
   drawing_entity="line"
End Sub
Sub draw_circles Cdecl (widget As FL_Widget Ptr)
   drawing_entity="circle"
End Sub


Sub Create_Window_Main ()
   win = Fl_WindowNew (800, 600, "my_fbgfx_to_fltk_image_test-5")
   box = Fl_BoxNew(120, 0, 580, 580)
   btn_draw_line = Fl_ButtonNew (10, 10, 100, 20, "Draw lines")
   btn_draw_circle = Fl_ButtonNew (10, 40, 100, 20, "Draw circles")

End Sub




Sub getfltkevents()
   
'   Print "Fl_EventNumber";Fl_EventNumber;" "
'   Print "Fl_EventButtons (non-zero)";Fl_EventButtons;"          "
'   Print "Fl_EventButton (mouse button (1-3))";Fl_EventButton;"          "
'   Print "Fl_EventX & Fl_EventY (mouse pointer x,y)";Fl_EventX();Fl_EventY();" "
'   Print "Fl_EventDY (mouse wheel)";Fl_EventDY();" "
'   Print "Fl_EventKey asc";Fl_EventKey();" "
   Select Case Fl_EventX()
      Case Fl_WidgetGetX(box) To Fl_WidgetGetX(box)+mywidth
         Select Case Fl_EventY()
            Case Fl_WidgetGetY(box) To Fl_WidgetGetY(box)+myheight
               Select Case Fl_EventNumber
                  Case 0 ' mouse button
                     If Fl_EventButtons<>0 Then
                        If mbp=FALSE Then
                           mbp=TRUE
                           Select Case Fl_EventButton
                              Case 1
                                 mousebuttondown=TRUE
                              Case 2
                                 'middle button
                              Case 3
                                 escapeme
                           End Select
                        EndIf
                     Else
                        mbp=FALSE
                     EndIf
                  Case 11 ' mouse move
                     myfltkimage_mouse_x=Fl_EventX()-Fl_WidgetGetX(box)
                     myfltkimage_mouse_y=Fl_EventY()'-Fl_WidgetGetY(box)
                     'change the mousey to increase from bottom up
                     myfltkimage_mouse_y=579-myfltkimage_mouse_y
                     myfltkimage_mouse_x=wx1+(wx2-wx1)*(myfltkimage_mouse_x/(580))
                     myfltkimage_mouse_y=wy1+(wy2-wy1)*(myfltkimage_mouse_y/(580))
                  Case 19 ' mouse scroll wheel
                     If mswp<>Fl_EventDY Then
                        mswp=Fl_EventDY
                        Select Case Fl_EventDY()
                           Case Is > 0
                              'zoom out
                              myfltkimage_mouse_scroll_up=TRUE
                           Case Is < 0
                              'zoom in
                              myfltkimage_mouse_scroll_down=TRUE
                        End Select
                     EndIf
               End Select
         End Select
   End Select
End Sub





Function calcd(cd1 as Double,cd2 As Double,cd3 as Double,cd4 as Double,cd5 as Double,cd6 as Double) as double
   calcd = sqr((cd1-cd4)^2 + (cd2-cd5)^2 + (cd3-cd6)^2)
end function



Sub mymain()
   getfltkevents
   Sleep 50
   ScreenLock
   Cls
   
   If mousebuttondown=TRUE Then
      mousebuttondown=FALSE
      mouse_clicks+=1
   EndIf
   Select Case mouse_clicks
      Case 1
         Select Case drawing_entity
            Case "line"
               If drawingline=FALSE Then
                  drawingline=TRUE
                  linec=linec+1
                  lines(linec,1)=myfltkimage_mouse_x
                  lines(linec,2)=myfltkimage_mouse_y
               EndIf
            Case "circle"
               If drawingcircle=FALSE Then
                  drawingcircle=TRUE
                  circlec=circlec+1
                  circles(circlec,1)=myfltkimage_mouse_x
                  circles(circlec,2)=myfltkimage_mouse_y
               EndIf            
         End Select
      Case 2
         Select Case drawing_entity
            Case "line"
               If drawingline=TRUE Then
                  drawingline=FALSE
                  mouse_clicks=0
                  lines(linec,3)=myfltkimage_mouse_x
                  lines(linec,4)=myfltkimage_mouse_y
               EndIf         
            Case "circle"
               If drawingcircle=TRUE Then
                  drawingcircle=FALSE
                  mouse_clicks=0
                  Dim r As Double
                  r=calcd( circles(circlec,1) , circles(circlec,2) ,0, CDbl(myfltkimage_mouse_x), CDbl(myfltkimage_mouse_y),0)
                  circles(circlec,3)=r
               EndIf         
         End Select
   End Select

   If drawingline=TRUE Then Line(lines(linec,1),lines(linec,2))-(myfltkimage_mouse_x,myfltkimage_mouse_y)
   If drawingcircle=TRUE Then
         Dim r As Double
         r=calcd( circles(circlec,1) , circles(circlec,2) ,0, CDbl(myfltkimage_mouse_x), CDbl(myfltkimage_mouse_y),0)
         Circle( circles(circlec,1),circles(circlec,2) ),r
   EndIf
   
   If myfltkimage_mouse_scroll_up=TRUE Then
      myfltkimage_mouse_scroll_up=FALSE
      '"Scroll up - zoom out"
      zoomoutpan
      window (wx1,wy1)-(wx2,wy2)

   EndIf
   If myfltkimage_mouse_scroll_down=TRUE Then
      myfltkimage_mouse_scroll_down=FALSE
      '"Scroll down - zoom in"
      zoominpan
      window (wx1,wy1)-(wx2,wy2)
   EndIf

   drawlines
   drawcircles
   myfbgfxtofltkimg(box)
   ScreenUnLock

   
   
End Sub

Sub escapeme()
   mouse_clicks=0
   If drawingline=TRUE Then
      drawingline=FALSE
      linec=linec-1
   EndIf         
   If drawingcircle=TRUE Then
      drawingcircle=FALSE
      circlec-=1
   EndIf         
End Sub

Sub zoomin()
   If wx2-wx1<maxzoomin Then Exit Sub
   wzoom=(wx2-wx1)/wzoomt
   wx1=wx1+wzoom
   wy1=wy1+wzoom
   wx2=wx2-wzoom
   wy2=wy2-wzoom

End Sub
Sub zoomout()
   wzoom=(wx2-wx1)/wzoomt
   wx1=wx1-wzoom
   wy1=wy1-wzoom
   wx2=wx2+wzoom
   wy2=wy2+wzoom
End Sub
Sub zoominpan()
   If wx2-wx1<maxzoomin Then Exit Sub
   Dim As Double panxf,panyf,panx,pany
   panxf=(myfltkimage_mouse_x-wx1)/(wx2-wx1)
   panyf=(myfltkimage_mouse_y-wy1)/(wy2-wy1)
   wx1=wx1+wzoom
   wy1=wy1+wzoom
   wx2=wx2-wzoom
   wy2=wy2-wzoom
   panx=wx1+int((wx2-wx1)*panxf)
   pany=wy1+Int((wy2-wy1)*panyf)
   wx1=wx1+(myfltkimage_mouse_x-panx)
   wx2=wx2+(myfltkimage_mouse_x-panx)
   wy1=wy1+(myfltkimage_mouse_y-pany)
   wy2=wy2+(myfltkimage_mouse_y-pany)
   wzoom=(wx2-wx1)/wzoomt
   If wzoom<1 Then wzoom=1
End Sub
Sub zoomoutpan()
   Dim As Double panxf,panyf,panx,pany
   panxf=(myfltkimage_mouse_x-wx1)/(wx2-wx1)
   panyf=(myfltkimage_mouse_y-wy1)/(wy2-wy1)
   wx1=wx1-wzoom
   wy1=wy1-wzoom
   wx2=wx2+wzoom
   wy2=wy2+wzoom
   panx=wx1+((wx2-wx1)*panxf)
   pany=wy1+((wy2-wy1)*panyf)
   wx1=wx1+(myfltkimage_mouse_x-panx)
   wx2=wx2+(myfltkimage_mouse_x-panx)
   wy1=wy1+(myfltkimage_mouse_y-pany)
   wy2=wy2+(myfltkimage_mouse_y-pany)
   wzoom=(wx2-wx1)/wzoomt
End Sub


Sub drawlines
   Dim As Integer i
   For i = 1 To linec
      If i=linec Then
         If drawingline=TRUE Then Exit For
      EndIf
      Line(lines(i,1),lines(i,2))-(lines(i,3),lines(i,4))
   Next
End Sub

Sub drawcircles
   Dim As Integer i
   For i = 1 To circlec
      If i=circlec Then
         If drawingcircle=TRUE Then Exit For
      EndIf
      circle(circles(i,1),circles(i,2)),circles(i,3)
   Next
End Sub



Sub button_draw_line()
   escapeme
   drawing_entity="line"
End Sub

Sub button_draw_circle()
   escapeme
   drawing_entity="circle"
End Sub



Sub myfbgfxtofltkimg Cdecl (byval UserData as any ptr)
   Dim As Integer i,x,y
   Dim as ubyte ptr rs=screenptr()  ' row source
   Dim as ubyte ptr ri=rgb_img      ' row image
   Dim as ulong     r,g,b
   For y = 0 to h-1
      i=0
      For x = 0 to w-1
         Palette get rs[x],r,g,b
         ri[i+0]=r: ri[i+1]=g : ri[i+2]=b : i+=3
      Next
      rs+=pitch_src
      ri+=pitch_img
   Next
   Fl_ImageDelete img
   img = Fl_RGB_ImageNew(rgb_img,w,h,bytes_img,pitch_img)
   Fl_WidgetSetImage box,img
   Fl_WidgetRedraw box
End Sub

owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 03, 2017 23:45

modification to:
draw lines and circles
zoom in and out with the mouse wheel

Using Fl_BoxEX instead of FL_Box
Fl_BoxExSetHandleCB box,@myboxHandleCB
Notice Function myboxHandleCB return 1 not return 0

Code: Select all

#Include once "fltk-c.bi"

'Declare Function Box_Event cdecl (me as any ptr, event as FL_EVENT) as Integer
Declare Sub Create_Window_Main ()
Declare Sub draw_lines Cdecl (widget As FL_Widget Ptr)
Declare Sub draw_circles Cdecl (widget As FL_Widget Ptr)
Declare Sub myfbgfxtofltkimg Cdecl (byval UserData as any ptr)
Declare Function calcd(cd1 as Double,cd2 as Double,cd3 as Double,cd4 as Double,cd5 as Double,cd6 as Double) as double
Declare Function myboxHandleCB cdecl (self as any ptr,event as Fl_Event) as integer
Declare Sub mymain()
Declare Sub drawlines
Declare Sub drawcircles
Declare Sub zoomin()
Declare Sub zoomout()
Declare Sub zoominpan()
Declare Sub zoomoutpan()
Declare Sub escapeme()

'Windows and widgets:
Dim Shared As Fl_Window Ptr win
Dim Shared As Fl_BoxEX Ptr box
Dim Shared As Fl_Button Ptr btn_draw_line
Dim Shared As Fl_Button Ptr btn_draw_circle
Dim Shared As Fl_Image Ptr img
'Dim Shared As Fl_EventNumber Ptr en


Dim Shared mywidth As Integer
Dim Shared myheight As Integer
Dim Shared As Integer myfltkimage_mouse_x, myfltkimage_mouse_y
Dim Shared As BOOLEAN mbp,mousebuttondown, mousebuttonup,myfltkimage_mouse_scroll_up,myfltkimage_mouse_scroll_down
Dim Shared As Integer mswp
Dim Shared As BOOLEAN drawingline=FALSE, drawingcircle=FALSE
Dim Shared As Integer linec,circlec
Dim Shared As Double lines(100,4), circles(100,3)
Dim Shared As String drawing_entity
drawing_entity="line"
Dim Shared As Integer mouse_clicks
Dim Shared As Integer wx1,wy1,wx2,wy2,wzoom,wzoomt,maxzoomin,maxzoomout

mywidth=580
myheight=580
'ScreenRes mywidth,myheight,,,fb.GFX_NULL
wx1=0
wy1=0
wx2=mywidth-1
wy2=myheight-1
wzoomt=10
maxzoomin=10
maxzoomout=10000



Dim Shared As Integer w,h,pitch_src
Dim Shared As Integer bytes_img=3 ' RGB
ScreenRes mywidth,myheight,,,-1
ScreenInfo w,h,,,pitch_src
Dim Shared As Integer pitch_img
pitch_img=w*bytes_img
Dim Shared As UByte Ptr rgb_img
rgb_img=allocate(h*pitch_img)

window (wx1,wy1)-(wx2,wy2)


Create_Window_Main ()
Fl_WidgetSetCallback0 btn_draw_line, @draw_lines
Fl_WidgetSetCallback0 btn_draw_circle, @draw_circles
Fl_BoxExSetHandleCB box,@myboxHandleCB

Fl_AddIdle @mymain, box

Fl_WindowShow(win)
Fl_Run
DeAllocate rgb_img

End


Sub draw_lines Cdecl (widget As FL_Widget Ptr)
   drawing_entity="line"
End Sub
Sub draw_circles Cdecl (widget As FL_Widget Ptr)
   drawing_entity="circle"
End Sub


Sub Create_Window_Main ()
   win = Fl_WindowNew (800, 600, "my_fbgfx_to_fltk_image_test-5")
   box = Fl_BoxExNew(120, 0, 580, 580)
   btn_draw_line = Fl_ButtonNew (10, 10, 100, 20, "Draw lines")
   btn_draw_circle = Fl_ButtonNew (10, 40, 100, 20, "Draw circles")

End Sub


function myboxHandleCB cdecl (self as any ptr,event as Fl_Event) as integer
   Select Case event
      Case 1 'mouse button pressed
         'Print "push"
         Select Case Fl_EventButton
            Case 1
               mousebuttondown=TRUE
            Case 2
               'middle button
            Case 3
               escapeme
         End Select
      Case 11 'mouse moved
         'Print "move"
         'Print Fl_EventX();" ";Fl_EventY()
         myfltkimage_mouse_x=Fl_EventX()-Fl_WidgetGetX(box)
         myfltkimage_mouse_y=Fl_EventY()'-Fl_WidgetGetY(box)
         'change the mousey to increase from bottom up
         myfltkimage_mouse_y=579-myfltkimage_mouse_y
         myfltkimage_mouse_x=wx1+(wx2-wx1)*(myfltkimage_mouse_x/(580))
         myfltkimage_mouse_y=wy1+(wy2-wy1)*(myfltkimage_mouse_y/(580))
      Case 19 'mouse scrollwheel
         'Print "mousewheel"
         Select Case Fl_EventDY()
            Case Is > 0
               'zoom out
               myfltkimage_mouse_scroll_up=TRUE
            Case Is < 0
               'zoom in
               myfltkimage_mouse_scroll_down=TRUE
         End Select
   End Select
   Return 1
End Function

Function calcd(cd1 as Double,cd2 As Double,cd3 as Double,cd4 as Double,cd5 as Double,cd6 as Double) as double
   calcd = sqr((cd1-cd4)^2 + (cd2-cd5)^2 + (cd3-cd6)^2)
end function



Sub mymain()
   Sleep 50
   ScreenLock
   Cls
   
   If mousebuttondown=TRUE Then
      mousebuttondown=FALSE
      mouse_clicks+=1
   EndIf
   Select Case mouse_clicks
      Case 1
         Select Case drawing_entity
            Case "line"
               If drawingline=FALSE Then
                  drawingline=TRUE
                  linec=linec+1
                  lines(linec,1)=myfltkimage_mouse_x
                  lines(linec,2)=myfltkimage_mouse_y
               EndIf
            Case "circle"
               If drawingcircle=FALSE Then
                  drawingcircle=TRUE
                  circlec=circlec+1
                  circles(circlec,1)=myfltkimage_mouse_x
                  circles(circlec,2)=myfltkimage_mouse_y
               EndIf            
         End Select
      Case 2
         Select Case drawing_entity
            Case "line"
               If drawingline=TRUE Then
                  drawingline=FALSE
                  mouse_clicks=0
                  lines(linec,3)=myfltkimage_mouse_x
                  lines(linec,4)=myfltkimage_mouse_y
               EndIf         
            Case "circle"
               If drawingcircle=TRUE Then
                  drawingcircle=FALSE
                  mouse_clicks=0
                  Dim r As Double
                  r=calcd( circles(circlec,1) , circles(circlec,2) ,0, CDbl(myfltkimage_mouse_x), CDbl(myfltkimage_mouse_y),0)
                  circles(circlec,3)=r
               EndIf         
         End Select
   End Select

   If drawingline=TRUE Then Line(lines(linec,1),lines(linec,2))-(myfltkimage_mouse_x,myfltkimage_mouse_y)
   If drawingcircle=TRUE Then
         Dim r As Double
         r=calcd( circles(circlec,1) , circles(circlec,2) ,0, CDbl(myfltkimage_mouse_x), CDbl(myfltkimage_mouse_y),0)
         Circle( circles(circlec,1),circles(circlec,2) ),r
   EndIf
   
   If myfltkimage_mouse_scroll_up=TRUE Then
      myfltkimage_mouse_scroll_up=FALSE
      '"Scroll up - zoom out"
      zoomoutpan
      window (wx1,wy1)-(wx2,wy2)

   EndIf
   If myfltkimage_mouse_scroll_down=TRUE Then
      myfltkimage_mouse_scroll_down=FALSE
      '"Scroll down - zoom in"
      zoominpan
      window (wx1,wy1)-(wx2,wy2)
   EndIf

   drawlines
   drawcircles
   myfbgfxtofltkimg(box)
   ScreenUnLock

   
   
End Sub

Sub escapeme()
   mouse_clicks=0
   If drawingline=TRUE Then
      drawingline=FALSE
      linec=linec-1
   EndIf         
   If drawingcircle=TRUE Then
      drawingcircle=FALSE
      circlec-=1
   EndIf         
End Sub

Sub zoomin()
   If wx2-wx1<maxzoomin Then Exit Sub
   wzoom=(wx2-wx1)/wzoomt
   wx1=wx1+wzoom
   wy1=wy1+wzoom
   wx2=wx2-wzoom
   wy2=wy2-wzoom

End Sub
Sub zoomout()
   wzoom=(wx2-wx1)/wzoomt
   wx1=wx1-wzoom
   wy1=wy1-wzoom
   wx2=wx2+wzoom
   wy2=wy2+wzoom
End Sub
Sub zoominpan()
   If wx2-wx1<maxzoomin Then Exit Sub
   Dim As Double panxf,panyf,panx,pany
   panxf=(myfltkimage_mouse_x-wx1)/(wx2-wx1)
   panyf=(myfltkimage_mouse_y-wy1)/(wy2-wy1)
   wx1=wx1+wzoom
   wy1=wy1+wzoom
   wx2=wx2-wzoom
   wy2=wy2-wzoom
   panx=wx1+int((wx2-wx1)*panxf)
   pany=wy1+Int((wy2-wy1)*panyf)
   wx1=wx1+(myfltkimage_mouse_x-panx)
   wx2=wx2+(myfltkimage_mouse_x-panx)
   wy1=wy1+(myfltkimage_mouse_y-pany)
   wy2=wy2+(myfltkimage_mouse_y-pany)
   wzoom=(wx2-wx1)/wzoomt
   If wzoom<1 Then wzoom=1
End Sub
Sub zoomoutpan()
   Dim As Double panxf,panyf,panx,pany
   panxf=(myfltkimage_mouse_x-wx1)/(wx2-wx1)
   panyf=(myfltkimage_mouse_y-wy1)/(wy2-wy1)
   wx1=wx1-wzoom
   wy1=wy1-wzoom
   wx2=wx2+wzoom
   wy2=wy2+wzoom
   panx=wx1+((wx2-wx1)*panxf)
   pany=wy1+((wy2-wy1)*panyf)
   wx1=wx1+(myfltkimage_mouse_x-panx)
   wx2=wx2+(myfltkimage_mouse_x-panx)
   wy1=wy1+(myfltkimage_mouse_y-pany)
   wy2=wy2+(myfltkimage_mouse_y-pany)
   wzoom=(wx2-wx1)/wzoomt
End Sub


Sub drawlines
   Dim As Integer i
   For i = 1 To linec
      If i=linec Then
         If drawingline=TRUE Then Exit For
      EndIf
      Line(lines(i,1),lines(i,2))-(lines(i,3),lines(i,4))
   Next
End Sub

Sub drawcircles
   Dim As Integer i
   For i = 1 To circlec
      If i=circlec Then
         If drawingcircle=TRUE Then Exit For
      EndIf
      circle(circles(i,1),circles(i,2)),circles(i,3)
   Next
End Sub



Sub button_draw_line()
   escapeme
   drawing_entity="line"
End Sub

Sub button_draw_circle()
   escapeme
   drawing_entity="circle"
End Sub



Sub myfbgfxtofltkimg Cdecl (byval UserData as any ptr)
   Dim As Integer i,x,y
   Dim as ubyte ptr rs=screenptr()  ' row source
   Dim as ubyte ptr ri=rgb_img      ' row image
   Dim as ulong     r,g,b
   For y = 0 to h-1
      i=0
      For x = 0 to w-1
         Palette get rs[x],r,g,b
         ri[i+0]=r: ri[i+1]=g : ri[i+2]=b : i+=3
      Next
      rs+=pitch_src
      ri+=pitch_img
   Next
   Fl_ImageDelete img
   img = Fl_RGB_ImageNew(rgb_img,w,h,bytes_img,pitch_img)
   Fl_WidgetSetImage box,img
   Fl_WidgetRedraw box
End Sub

owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 05, 2017 14:02

is there a way to set vertical scroll bar only for FL_Scroll
for example a browser you can use
Fl_Browser_SetHasScrollbar brwsr, FL_SCROLL_VERTICAL
i would think it would be something like
Fl_Scroll_SetHasScrollbar scr,FL_SCROLL_VERTICAL
jdebord
Posts: 508
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby jdebord » May 06, 2017 7:50

Try this:

Code: Select all

Fl_WidgetSetType scr, FL_SCROLL_VERTICAL

See the other FL_SCROLL_... constants in fltk-main.bi
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 07, 2017 1:10

thanks jdebord that works
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 09, 2017 13:45

in an effort to use fltk for the fbcad project and to give credit where credit is due. ie. both gtk and fltk are great contributions to us all so i give cudos to the programmers of both gui libraries and to joshy for helping bind fltk for freebasic.

today i am going to focus on building an expander for fltk similar to gtk's expander. the fbcad / gtk project uses multiple expanders wrapped in a scroll area.

in order to achieve this goal i wanted to first ask the freebasic.net community how they think this might be done in fltk.

naturally i have the idea that an expander would be made up using two fl_box. 1st box contains an alternating image (expand / collapse). 2nd box contains by buttons (icons). When 1st box (collapse) is clicked, 2nd box is hidden. When 1st box (expand) is clicked, 2nd box is shown.

since there are multiple expanders wrapped in a scroll area, the scroll area will need to be adjusted accordingly (as each expander is collapsed / expanded)

just trying to understand the layout / positioning concept of fltk
last night i experimented with this:

Code: Select all

#include once "fltk-c.bi"
Dim As Fl_Window Ptr win
win = Fl_WindowNew (800, 600, "layout test")
Dim as Fl_Pack ptr pack1 = Fl_PackNew(30,30,270,400)
Dim Shared As Fl_Button Ptr btn1(4,44)
Dim Shared As Fl_Button Ptr btn2(4,44)

Fl_GroupBegin pack1
var scr1 = Fl_ScrollNew(0,0,200,400)
Fl_WidgetSetType scr1, FL_SCROLL_VERTICAL
Dim Shared As Fl_Button Ptr btn(4,44)

' create some buttons with random background and selection color
for x1 as integer = 0 to 4
   For y1 as integer = 0 to 44
    dim as string label1 = "[" & 1 + y1*40 + x1 & "]"
    btn1(x1,y1) = Fl_ButtonNew(x1*50,y1*50,45,45)
    Fl_WidgetCopyLabel btn1(x1,y1), label1
    Fl_WidgetSetColorSel(btn1(x1,y1),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack1

Dim as Fl_Pack ptr pack2 = Fl_PackNew(400,30,270,300)

Fl_GroupBegin pack2
var scr2 = Fl_ScrollNew(0,0,222,322)
Fl_WidgetSetType scr2, FL_SCROLL_VERTICAL

' create some buttons with random background and selection color
for x2 as integer = 0 to 4
   For y2 as integer = 0 to 44
    dim as string label2 = "[" & 1 + y2*40 + x2 & "]"
    btn2(x2,y2) = Fl_ButtonNew(x2*50,y2*50,45,45)
    Fl_WidgetCopyLabel btn2(x2,y2), label2
    Fl_WidgetSetColorSel(btn2(x2,y2),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack2



Fl_WindowShow(win)
Fl_Run

End



an noticed some oddities.
first thing is:
If I place the dim statements for the fl_pack (s) one after the other ie.

Dim as Fl_Pack ptr pack1 = Fl_PackNew(30,30,270,400)
Dim as Fl_Pack ptr pack2 = Fl_PackNew(400,30,270,300)

it produces an undesirable result:

Code: Select all

#include once "fltk-c.bi"
Dim As Fl_Window Ptr win
win = Fl_WindowNew (800, 600, "layout test")
Dim as Fl_Pack ptr pack1 = Fl_PackNew(30,30,270,400)
Dim as Fl_Pack ptr pack2 = Fl_PackNew(400,30,270,300)
Dim Shared As Fl_Button Ptr btn1(4,44)
Dim Shared As Fl_Button Ptr btn2(4,44)

Fl_GroupBegin pack1
var scr1 = Fl_ScrollNew(0,0,200,400)
Fl_WidgetSetType scr1, FL_SCROLL_VERTICAL
Dim Shared As Fl_Button Ptr btn(4,44)

' create some buttons with random background and selection color
for x1 as integer = 0 to 4
   For y1 as integer = 0 to 44
    dim as string label1 = "[" & 1 + y1*40 + x1 & "]"
    btn1(x1,y1) = Fl_ButtonNew(x1*50,y1*50,45,45)
    Fl_WidgetCopyLabel btn1(x1,y1), label1
    Fl_WidgetSetColorSel(btn1(x1,y1),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack1


Fl_GroupBegin pack2
var scr2 = Fl_ScrollNew(0,0,222,322)
Fl_WidgetSetType scr2, FL_SCROLL_VERTICAL

' create some buttons with random background and selection color
for x2 as integer = 0 to 4
   For y2 as integer = 0 to 44
    dim as string label2 = "[" & 1 + y2*40 + x2 & "]"
    btn2(x2,y2) = Fl_ButtonNew(x2*50,y2*50,45,45)
    Fl_WidgetCopyLabel btn2(x2,y2), label2
    Fl_WidgetSetColorSel(btn2(x2,y2),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack2



Fl_WindowShow(win)
Fl_Run

End


i am not sure why it matters where the dim statement for fl_pack are placed but it does.

any how, my goal is to focus on building an expander for fltk for my fbcad project and we'll see how it goes today.
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby D.J.Peters » May 09, 2017 15:02

If you create any kind of a Fl_Group widget like a Fl_Window or other containers for any widgets
FLTK opens for you the newly created container so you can add direktly new child widgets to it's parent the last created container widget.

var win = Fl_WindowNew (800, 600, "layout test")
is the same as :
var win = Fl_WindowNew (800, 600, "layout test") : Fl_WindowBegin(win)

var pack1 = Fl_PackNew(30,30,270,400)
is the same as:
var pack1 = Fl_PackNew(30,30,270,400) : Fl_PackBegin(pack1)

Joshy

read the comments:

Code: Select all

#include once "fltk-c.bi"

Dim Shared As Fl_Button Ptr btn1(4,44)
Dim Shared As Fl_Button Ptr btn2(4,44)

var win = Fl_WindowNew (800, 600, "layout test")
Fl_WindowBegin(win) ' !!! not needed Fl_WindowNew() will open the group for you

  var pack1 = Fl_PackNew( 30,30,270,400)
  Fl_PackBegin(pack1) ' !!! not needed Fl_PackNew() will open the group for you
  ' ...
  Fl_PackEnd(pack1) ' !!! needed if you won't add pack2 to it
 
  var pack2 = Fl_PackNew(400,30,270,300)
  Fl_PackBegin(pack2) ' !!! not needed Fl_PackNew() will open the group for you
  ' ...
  Fl_PackEnd(pack2) ' !!! needed close the newly created group

  Fl_PackBegin(pack1) ' !!! needed
    var scr1 = Fl_ScrollNew(0,0,200,400)
    Fl_WidgetSetType scr1, FL_SCROLL_VERTICAL
    ' create some buttons with random background and selection color
    for x1 as integer = 0 to 4
      For y1 as integer = 0 to 44
        dim as string label1 = "[" & 1 + y1*40 + x1 & "]"
        btn1(x1,y1) = Fl_ButtonNew(x1*50,y1*50,45,45)
        Fl_WidgetCopyLabel(btn1(x1,y1), label1)
        Fl_WidgetSetColorSel(btn1(x1,y1),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                                         Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
      next
    next
  Fl_PackEnd(pack1) ' !!! needed
 
 
  Fl_PackBegin(pack2) ' !!! needed
    var scr2 = Fl_ScrollNew(0,0,222,322)
    Fl_WidgetSetType scr2, FL_SCROLL_VERTICAL
    ' create some buttons with random background and selection color
    for x2 as integer = 0 to 4
      For y2 as integer = 0 to 44
        dim as string label2 = "[" & 1 + y2*40 + x2 & "]"
        btn2(x2,y2) = Fl_ButtonNew(x2*50,y2*50,45,45)
        Fl_WidgetCopyLabel(btn2(x2,y2), label2)
        Fl_WidgetSetColorSel(btn2(x2,y2),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                                         Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
      next
    next
  Fl_PackEnd(pack2) ' !!! needed

Fl_WindowEnd(win) ' not needed Fl_WindowShow will close the group for you

Fl_WindowShow(win)
Fl_Run()
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 09, 2017 16:44

Ok, i removed
Fl_GroupBegin pack1
Fl_GroupBegin pack2

and this works:

Code: Select all

#include once "fltk-c.bi"
Dim As Fl_Window Ptr win
win = Fl_WindowNew (800, 600, "layout test")
Dim as Fl_Pack ptr pack1 = Fl_PackNew(30,30,270,400)
Dim Shared As Fl_Button Ptr btn1(4,44)
Dim Shared As Fl_Button Ptr btn2(4,44)

var scr1 = Fl_ScrollNew(0,0,200,400)
Fl_WidgetSetType scr1, FL_SCROLL_VERTICAL
Dim Shared As Fl_Button Ptr btn(4,44)

' create some buttons with random background and selection color
for x1 as integer = 0 to 4
   For y1 as integer = 0 to 44
    dim as string label1 = "[" & 1 + y1*40 + x1 & "]"
    btn1(x1,y1) = Fl_ButtonNew(x1*50,y1*50,45,45)
    Fl_WidgetCopyLabel btn1(x1,y1), label1
    Fl_WidgetSetColorSel(btn1(x1,y1),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack1

Dim as Fl_Pack ptr pack2 = Fl_PackNew(400,30,270,300)

var scr2 = Fl_ScrollNew(0,0,222,322)
Fl_WidgetSetType scr2, FL_SCROLL_VERTICAL

' create some buttons with random background and selection color
for x2 as integer = 0 to 4
   For y2 as integer = 0 to 44
    dim as string label2 = "[" & 1 + y2*40 + x2 & "]"
    btn2(x2,y2) = Fl_ButtonNew(x2*50,y2*50,45,45)
    Fl_WidgetCopyLabel btn2(x2,y2), label2
    Fl_WidgetSetColorSel(btn2(x2,y2),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack2



Fl_WindowShow(win)
Fl_Run

End



and this does not work:

Code: Select all

#include once "fltk-c.bi"
Dim As Fl_Window Ptr win
win = Fl_WindowNew (800, 600, "layout test")
Dim as Fl_Pack ptr pack1 = Fl_PackNew(30,30,270,400)
Dim as Fl_Pack ptr pack2 = Fl_PackNew(400,30,270,300)

Dim Shared As Fl_Button Ptr btn1(4,44)
Dim Shared As Fl_Button Ptr btn2(4,44)

var scr1 = Fl_ScrollNew(0,0,200,400)
Fl_WidgetSetType scr1, FL_SCROLL_VERTICAL
Dim Shared As Fl_Button Ptr btn(4,44)

' create some buttons with random background and selection color
for x1 as integer = 0 to 4
   For y1 as integer = 0 to 44
    dim as string label1 = "[" & 1 + y1*40 + x1 & "]"
    btn1(x1,y1) = Fl_ButtonNew(x1*50,y1*50,45,45)
    Fl_WidgetCopyLabel btn1(x1,y1), label1
    Fl_WidgetSetColorSel(btn1(x1,y1),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack1


var scr2 = Fl_ScrollNew(0,0,222,322)
Fl_WidgetSetType scr2, FL_SCROLL_VERTICAL

' create some buttons with random background and selection color
for x2 as integer = 0 to 4
   For y2 as integer = 0 to 44
    dim as string label2 = "[" & 1 + y2*40 + x2 & "]"
    btn2(x2,y2) = Fl_ButtonNew(x2*50,y2*50,45,45)
    Fl_WidgetCopyLabel btn2(x2,y2), label2
    Fl_WidgetSetColorSel(btn2(x2,y2),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                             Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
  next
next
Fl_GroupEnd pack2



Fl_WindowShow(win)
Fl_Run

End


the only difference is where the dim statement is located.
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 09, 2017 17:41

the only thing i was trying to point out was that placement of objects into other objects is dependent on the order in which they are in the code. that is if i am correct or not.
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 09, 2017 20:03

using show and hide on the scrolls and redraw on the packs. it kinda works.

Code: Select all

#include once "fltk-c.bi"

Declare function Button1HandleCB cdecl (self as any ptr,event as Fl_Event) as Integer
Declare function Button2HandleCB cdecl (self as any ptr,event as Fl_Event) as Integer

Dim Shared As BOOLEAN showhide(2)
showhide(1)=FALSE
showhide(2)=FALSE
dim as string label

Dim As Fl_Window Ptr win
Dim As Fl_ButtonEx Ptr btn0,btn1,btn2
Dim As Fl_Button Ptr btns1(4,44),btns2(4,44)
Dim Shared As Fl_Scroll Ptr scr0,scr1,scr2
Dim Shared As FL_Pack Ptr pack0,pack1,pack2

win = Fl_WindowNew (800, 600, "layout test")

pack0 = Fl_PackNew(0,0,270,200)

   pack1 = Fl_PackNew(0,50,270,200)
      'Note: this button's width of 50 adjusts to pack1's width of 270
      'Note: this button's height is not dependent on pack's height
      btn1= Fl_ButtonExNew(0,0,50,30,"click me 1")
      Fl_ButtonExSetHandleCB btn1,@Button1HandleCB
   
      'Note: this scroll's width of 50 adjusts to pack1's width of 270
      'Note: this scroll's height is not dependent on pack's height
      scr1 = Fl_ScrollNew(0,0,50,200)
      Fl_WidgetSetType scr1, FL_SCROLL_VERTICAL
      
      ' create some buttons with random background and selection color
      For x as integer = 0 to 4
         For y as integer = 0 to 44
          label = "1"
          btns1(x,y) = Fl_ButtonNew(x*50,y*50,45,45)
          Fl_WidgetCopyLabel btns1(x,y), label
          Fl_WidgetSetColorSel(btns1(x,y),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                                   Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
         next
      Next
   Fl_GroupEnd pack1

   pack2 = Fl_PackNew(0,0,270,200)
      'Note: this button's width of 50 adjusts to pack1's width of 270
      'Note: this button's height is not dependent on pack's height
      btn2= Fl_ButtonExNew(0,0,50,30,"click me 2")
      Fl_ButtonExSetHandleCB btn2,@Button2HandleCB
   
      'Note: this scroll's width of 50 adjusts to pack1's width of 270
      'Note: this scroll's height is not dependent on pack's height
      scr2 = Fl_ScrollNew(0,0,50,200)
      Fl_WidgetSetType scr2, FL_SCROLL_VERTICAL
      
      ' create some buttons with random background and selection color
      For x as integer = 0 to 4
         For y as integer = 0 to 44
          label = "2"
          btns2(x,y) = Fl_ButtonNew(x*50,y*50,45,45)
          Fl_WidgetCopyLabel btns2(x,y), label
          Fl_WidgetSetColorSel(btns2(x,y),Fl_RGB_Color(rnd*255,rnd*255,rnd*255), _
                                   Fl_RGB_Color(rnd*255,rnd*255,rnd*255))
         next
      Next
   Fl_GroupEnd pack2

Fl_GroupEnd pack0


Fl_WindowShow(win)
Fl_Run

End


function Button1HandleCB cdecl (self as any ptr,event as Fl_Event) as integer
   Select case event
      Case 1
         If showhide(1)=FALSE Then
            showhide(1)=TRUE
            Fl_WidgetHide(scr1)
         Else
            showhide(1)=FALSE
            Fl_WidgetShow(scr1)
         EndIf
         Fl_WidgetRedraw(pack0)
         'Fl_WidgetRedraw(pack1)
         'Fl_WidgetRedraw(pack2)
   End Select
   Return 1
end function

Function Button2HandleCB cdecl (self as any ptr,event as Fl_Event) as integer
   Select case event
      Case 1
         If showhide(2)=FALSE Then
            showhide(2)=TRUE
            Fl_WidgetHide(scr2)
         Else
            showhide(2)=FALSE
            Fl_WidgetShow(scr2)
         EndIf
         Fl_WidgetRedraw(pack0)
         'Fl_WidgetRedraw(pack1)
         'Fl_WidgetRedraw(pack2)
   End Select
   Return 1
end function

D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby D.J.Peters » May 09, 2017 20:38

if do this:
' create pack1 as child of Fl_Window
Dim as Fl_Pack ptr pack1 = Fl_PackNew(30,30,270,400)
' craete pack2 as child of pack1
Dim as Fl_Pack ptr pack2 = Fl_PackNew(400,30,270,300)

that means you create pack2 as child of pack1

this is the right order
' create pack1 as child of Fl_Window
var pack1 = Fl_PackNew(30,30,270,400)
Fl_PackEnd(pack1) ' close the childlist of pack1

' create pack2 as child of Fl_Window
var pack2 = Fl_PackNew(400,30,270,300)

Is it too complicated ?

why do not like to use var ?
dim as what_was_the_right_pointer_type = Fl_SomeThingNew()
var simple = Fl_SomeThingNew()

why you don't use code indentation (may be TAB's or spaces)
it's helpfull to see what are childs and what not

I posted your code with some improvments before.
owen
Posts: 433
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby owen » May 10, 2017 0:58

using var or dim to hide a box

this doesn't work

Code: Select all

#include once "fltk-c.bi"

Declare function ButtonHandleCB cdecl (self as any ptr,event as Fl_Event) as integer

Var win = Fl_WindowNew (800, 600, "test")
Var box= Fl_BoxNew(0,0,100,100,"a box")
Var btn= Fl_ButtonExNew(0,0,100,20,"click me")
Fl_ButtonExSetHandleCB btn,@ButtonHandleCB
Fl_WindowShow(win)
Fl_Run

End
function ButtonHandleCB cdecl (self as any ptr,event as Fl_Event) as integer
   Select case event
      Case 1
         Fl_WidgetHide(box)
   End Select
   Return 1
end function


my solution is:

Code: Select all

#include once "fltk-c.bi"

Declare function ButtonHandleCB cdecl (self as any ptr,event as Fl_Event) as integer

Dim Shared As Fl_Box Ptr box

Var win = Fl_WindowNew (800, 600, "test")
box= Fl_BoxNew(0,0,100,100,"a box")
Var btn= Fl_ButtonExNew(0,0,100,20,"click me")
Fl_ButtonExSetHandleCB btn,@ButtonHandleCB
Fl_WindowShow(win)
Fl_Run

End
function ButtonHandleCB cdecl (self as any ptr,event as Fl_Event) as integer
   Select case event
      Case 1
         Fl_WidgetHide(box)
   End Select
   Return 1
end function
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: FLTK C for FreeBASIC Jan 09, 2017

Postby D.J.Peters » May 10, 2017 2:03

Here are your first try without any shared var :-)

Joshy

Code: Select all

#include once "fltk-c.bi"

Declare function ButtonHandleCB cdecl (self as any ptr,event as Fl_Event) as integer

Var win = Fl_WindowNew (800, 600, "test")
Var box= Fl_BoxNew(0,0,100,100,"a box")
Var btn= Fl_ButtonExNew(0,0,100,20,"click me")
Fl_WidgetSetUserData(btn,box)
Fl_ButtonExSetHandleCB(btn,@ButtonHandleCB)
Fl_WindowShow(win)
Fl_Run

' End <--- no once need this :-)
function ButtonHandleCB cdecl (self as any ptr,event as Fl_Event) as integer
   Select case event
      Case 1
         Fl_WidgetHide(Fl_WidgetGetUserData(self))
   End Select
   Return 1
end function

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 3 guests