FLTK how to set one of two buttons "on"

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

FLTK how to set one of two buttons "on"

Postby dkr » Feb 10, 2017 18:54

Hello,
I have made a simple program to convert decibels to pressure using the FLTK gui. I would like to set one of two buttons to "on". Only one or the other can be "on". This sets the reference pressure to either psi or pascals. I would like to set Button_radio_psi to "on"

Thanks,
Darren

Code: Select all

' Convert decibel to pressure (RMS)
'
#INCLUDE "fltk-c.bi"
#include "string.bi"

'Window and widgets:
DIM SHARED AS Fl_Window PTR Window_Main
DIM SHARED AS Fl_Input PTR SPL_text
DIM SHARED AS Fl_Input PTR Pressure_text
DIM SHARED AS Fl_Button PTR Button_Compute
DIM SHARED AS Fl_Group PTR Group_radio
DIM SHARED AS Fl_Radio_Round_Button PTR Button_radio_psi,Button_radio_pascal
DIM SHARED AS DOUBLE p0


SUB Create_Window_Main ()
'Window with widgets:

    Window_Main = Fl_WindowNew (300, 300, "SPL <> Pressure")
    Group_radio = Fl_GroupNew (50, 20, 120, 90, "Choose a Ref. Pressure")
        Fl_GroupBegin (Group_radio)
        Button_radio_psi = Fl_Radio_Round_ButtonNew (20, 40, 100, 20, "psi")
        Button_radio_pascal = Fl_Radio_Round_ButtonNew (20, 60, 100, 20, "Pascals")
    Fl_GroupEnd (Group_radio)

    SPL_text = Fl_InputNew (100, 100, 180, 20, "SPL")
    Pressure_text = Fl_InputNew (100, 130, 180, 20, "Pressure")
    Button_Compute = Fl_ButtonNew (100, 200, 100, 20, "Compute")
    'Fl_Input_SetValue (Input_text, "170")

END SUB

SUB Button_Ref CDECL (widget AS FL_Widget PTR, arg as ANY PTR)
    SELECT CASE arg
        CASE Button_radio_psi
            p0 = 2.90075E-9
        CASE Button_radio_pascal
            p0 = 2.0e-5
    END SELECT
    'print "p0 = ";p0
END SUB

SUB Button_Compute_Event CDECL (widget AS FL_Widget PTR)
'Callback function for Button

  DIM AS STRING spl_txt,pp_txt
  dim as double spl,pprime
  'p0 = 2.90075E-9

  spl_txt = *Fl_Input_GetValue (SPL_text)
  pp_txt  = *Fl_Input_GetValue (Pressure_text)
  if spl_txt <> "" then
        spl = val(spl_txt)
        pprime = 10^(spl/20)*p0
        pp_txt = Format(pprime,"#.####E+0#")
    else
        pprime = val(pp_txt)
        spl = 20*(log(pprime/p0)/log(10))
        spl_txt = Format(spl,"###.0#")
  end if
  Fl_Input_SetValue (SPL_text, spl_txt)
  Fl_Input_SetValue (Pressure_text, pp_txt)

END SUB


'Main program:
Create_Window_Main ()
Fl_WidgetSetCallbackArg (Button_radio_psi, @Button_Ref, Button_radio_psi)
Fl_WidgetSetCallbackArg (Button_radio_pascal, @Button_Ref, Button_radio_pascal)
Fl_WidgetSetCallback0 (Button_Compute, @Button_Compute_Event())
Fl_WindowShow (Window_Main)
Fl_Run

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

Re: FLTK how to set one of two buttons "on"

Postby D.J.Peters » Feb 10, 2017 21:09

May be Fl_ButtonSetValue() is what you are looking for.

Joshy

Code: Select all

#include once "fltk-c.bi"
sub ButtonCB cdecl (byval btn1 as FL_WIDGET ptr,byval btn2 as any ptr)
  Fl_ButtonSetValue(cptr(FL_BUTTON ptr,Btn1),1)
  Fl_ButtonSetValue(cptr(FL_BUTTON ptr,Btn2),0) 
end sub
'
' main
'
var Win = Fl_WindowNew(320,200,"Fl_ButtonSetValue")
var Btn1 = Fl_ButtonNew(10,10,32,32,"A")
var Btn2 = Fl_ButtonNew(42,10,32,32,"B")
Fl_ButtonSetValue(Btn2,1)
Fl_WidgetSetCallbackArg Btn1,@ButtonCB,Btn2
Fl_WidgetSetCallbackArg Btn2,@ButtonCB,Btn1
Fl_WindowShow Win
Fl_Run
D.J.Peters
Posts: 7904
Joined: May 28, 2005 3:28

Re: FLTK how to set one of two buttons "on"

Postby D.J.Peters » Feb 10, 2017 21:12

Or you can use radio buttons (where only one can be pressed at once)

Joshy
dkr
Posts: 34
Joined: Nov 20, 2015 15:17
Location: Alabama, USA

Re: FLTK how to set one of two buttons "on"

Postby dkr » Feb 10, 2017 21:39

Yes, I used radio buttons. Just was unsure how to make sure one was set.

I will look at your code.

Thanks,
Darren
D.J.Peters
Posts: 7904
Joined: May 28, 2005 3:28

Re: FLTK how to set one of two buttons "on"

Postby D.J.Peters » Feb 10, 2017 22:06

Code: Select all

'Main program:
Create_Window_Main ()
Fl_WidgetSetCallbackArg (Button_radio_psi, @Button_Ref, Button_radio_psi)
Fl_ButtonSetValue(Button_radio_psi,1)  ' <--- set it pressed
Fl_WidgetSetCallbackArg (Button_radio_pascal, @Button_Ref, Button_radio_pascal)
Fl_WidgetSetCallback0 (Button_Compute, @Button_Compute_Event())
Fl_WindowShow (Window_Main)
Fl_Run
ike
Posts: 383
Joined: Jan 17, 2011 18:59

Re: FLTK how to set one of two buttons "on"

Postby ike » Apr 04, 2017 15:31

declare sub Fl_ButtonSetOnly(byval btn as Fl_Button ptr)

Return to “Beginners”

Who is online

Users browsing this forum: wallyg and 1 guest