Basic understanding of FLTK

New to FreeBASIC? Post your questions here.
Post Reply
dkr
Posts: 40
Joined: Nov 20, 2015 15:17
Location: Alabama, USA

Basic understanding of FLTK

Post by dkr »

Hello,
I am trying to write a routine to setup a data acquisition system using the FLTK gui front end. The following code is just a beginning effort. When running the program, Card 3 tab responds correctly in giving the correct outputs for AC/DC and input level. However, Card 1 tab now does not work. When the program had only the round buttons and input level for just tab1 it worked fine. By adding the same code to tab3, now tab1 does not work.

So how does the FLTK library work? I was under the impression that the VAR tab1 --> Fl_TabEnd tab1 only sets up the gui and so on for the other tabs. Do I need a separate Button_Click_Event subroutine for each tab? I thought that the Fl_WidgetSetCallback would call the click of the "Submit" button on either tab1 or tab3.

Do I need separate arrays to gather the data for each tab?

Changing the order of:

Code: Select all

Fl_WidgetSetCallback (Button_Click3,@Button_Click_Event())
Fl_WidgetSetCallback (Button_Click,@Button_Click_Event())
does not change the inability to change CARD 1 (tab1)

thanks for looking.
Darren

Code: Select all

#include once "fltk-c.bi"

#define Fl_RadioGroup      Fl_Group
#define Fl_RadioGroupNew   Fl_GroupNew
#define Fl_RadioGroupBegin Fl_GroupBegin
#define Fl_RadioGroupEnd   Fl_GroupEnd

const NRADIO_GROUPS = 16
dim as Fl_RadioGroup ptr RadioGroups(NRADIO_GROUPS-1)
DIM SHARED AS Fl_Radio_Round_Button PTR dc(NRADIO_GROUPS-1),ac(NRADIO_GROUPS-1)
DIM SHARED AS Fl_Box PTR Box_text
DIM SHARED AS Fl_Button PTR Button_Click,Button_Click3
DIM SHARED AS Fl_Choice PTR chc(16)
DIM SHARED as String dc_lab,ac_lab,chn,c_tab
DIM SHARED AS Integer tab_num

' test of: Fl_TabsEnd, Fl_TabNew and Fl_TabEnd 
' this are macros defined in fltk-c.bi

SUB Button_Click_Event CDECL (widget AS FL_Widget PTR, arg AS ANY PTR)
''Callback function for the Button
  dim i as integer
  PRINT "Button was clicked!"
  for i = 0 to 15
      print "Ch ";str(i);" ";Fl_ButtonGetValue(ac(i));" "; _
         Fl_ButtonGetValue(dc(i)),Fl_ChoiceGetValue(chc(i))
  NEXT
'sleep  
END SUB

sub TabCB cdecl (byval tabs as FL_WIDGET ptr,byval parent as any ptr)
  print "TabCB: " & *Fl_WidgetGetLabel(Fl_TabsGetValue(parent))
  c_tab = *Fl_WidgetGetLabel(Fl_TabsGetValue(parent))
  tab_num = CInt(ltrim(c_tab,"CARD "))
  print "Current tab = ";tab_num
end sub

sub ButtonCB cdecl (byval btn as FL_WIDGET ptr)
  print "ButtonCB: " & *Fl_WidgetGetLabel(btn)
end sub

var win = Fl_WindowNew(500,660,"multi_card_input_v1.bas")
 var tabs = Fl_TabsNew(10,10,500-20,660-20)
  Fl_WidgetSetCallbackArg tabs,@TabCB,tabs
  var tab1 = Fl_TabNew(10,35,500-20,660-45,"CARD 1 ")
   Button_Click = Fl_ButtonNew (100, 50, 100, 20, "Submit")
   for i as integer = 0 to NRADIO_GROUPS-1
     dc_lab = "dc"+str(i+1)
     ac_lab = "ac"+STR(i+1)
     chn = "Ch."+STR(i+1)
     Box_text = Fl_BoxNew(10,80+i*30,50,20,"")
     Fl_WidgetCopyLabel(Box_text,chn)
     RadioGroups(i) = Fl_RadioGroupNew(80,80+i*30,300,30)
     Fl_RadioGroupBegin(RadioGroups(i))
       ac(i) = Fl_Radio_Round_ButtonNew(80,80+i*30,50,20,ac_lab)
       Fl_WidgetCopyLabel(ac(i),ac_lab)
       dc(i) = Fl_Radio_Round_ButtonNew(170,80+i*30,50,20,dc_lab)
       Fl_WidgetCopyLabel(dc(i),dc_lab)
     Fl_RadioGroupEnd(RadioGroups(i)) 
     chc(i) = Fl_ChoiceNew(250,80+i*30,128,20)    
     Fl_Menu_Add3 chc(i),"Input Level/10.0|Input Level/3.16|Input Level/1.00|Input Level/0.316"
     Fl_ChoiceSetValue chc(i),3
     Fl_ButtonSetOnly(ac(i))
     
   next 
  Fl_TabEnd tab1
  var tab2 = Fl_TabNew(10,35,500-20,660-45,"CARD 2 ")
   Fl_WidgetSetCallback0 Fl_ButtonNew(50,60,90,25,"Button C"),@ButtonCB
   Fl_WidgetSetCallback0 Fl_ButtonNew(50,90,90,25,"Button D"),@ButtonCB
  Fl_TabEnd tab2
  var tab3 = Fl_TabNew(10,35,500-20,660-45,"CARD 3 ")
  Button_Click3 = Fl_ButtonNew (100, 50, 100, 20, "Submit")
   for i as integer = 0 to NRADIO_GROUPS-1
     dc_lab = "dc"+str(i+1)
     ac_lab = "ac"+STR(i+1)
     chn = "Ch."+STR(i+1)
     Box_text = Fl_BoxNew(10,80+i*30,50,20,"")
     Fl_WidgetCopyLabel(Box_text,chn)
     RadioGroups(i) = Fl_RadioGroupNew(80,80+i*30,300,30)
     Fl_RadioGroupBegin(RadioGroups(i))
       ac(i) = Fl_Radio_Round_ButtonNew(80,80+i*30,50,20,ac_lab)
       Fl_WidgetCopyLabel(ac(i),ac_lab)
       dc(i) = Fl_Radio_Round_ButtonNew(170,80+i*30,50,20,dc_lab)
       Fl_WidgetCopyLabel(dc(i),dc_lab)
     Fl_RadioGroupEnd(RadioGroups(i)) 
     chc(i) = Fl_ChoiceNew(250,80+i*30,128,20)    
     Fl_Menu_Add3 chc(i),"Input Level/10.0|Input Level/3.16|Input Level/1.00|Input Level/0.316"
     Fl_ChoiceSetValue chc(i),3
     Fl_ButtonSetOnly(ac(i))
     
   next  
  Fl_TabEnd tab3
 Fl_TabsEnd tabs
Fl_WindowEnd win

Fl_WindowShow win
Fl_WidgetSetCallback (Button_Click3,@Button_Click_Event())
Fl_WidgetSetCallback (Button_Click,@Button_Click_Event())
tab_num = 1
Fl_Run
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Basic understanding of FLTK

Post by badidea »

Is Fl_ButtonSetOnly(ac(i)) in tab3-section correct?
dkr
Posts: 40
Joined: Nov 20, 2015 15:17
Location: Alabama, USA

Re: Basic understanding of FLTK

Post by dkr »

I think Fl_ButtonSetOnly(ac(i)) is correct for both tabs. It sets the default to AC coupling for all channels. After looking more at it today, I think the issue is the ac() and dc() and chc() pointers are somehow fixed to the last tab initialization. I probably will have to make a separate Button_Click_Event subroutine for each tab. That is my main question.

Thanks
Darren
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Basic understanding of FLTK

Post by badidea »

Ah, I understand the problem now. When either of the 'submit' buttons is pressed the results are always of tab3.

This makes sense. The pointers ac(i) [for example] first point to the GUI-elements in tab1, but when tab3 is created, ac(i) then point to the GUI-elements on that tab. And nothing points to the elements of tab1 any more. The GUI-elements of tab1 still exist, but no way to access the data. Possibly even a memory leak, although FLTK or freebasic will probably solve that of program exit.

So yes, I think you need pointers to both tab1 and tab3 elements.
dkr
Posts: 40
Joined: Nov 20, 2015 15:17
Location: Alabama, USA

Re: Basic understanding of FLTK

Post by dkr »

Ok, Thank you. I will add pointers for each tab.

Darren
Post Reply