[futurebasic] Re: [FB] Radio Buttons

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2008 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Thu, 17 Jan 2008 01:24:40 -0500
Robert wrote:

>  I am writing a demo program that will open a window with three  
> radio buttons.



Boys,

Here's some old school, but there's more than one way to skin a  
newbie cat.

Runs in FB and compiles as a Universal Binary via FBtoC, with nary a  
warning.

BASIC is alive and well in Leopard!

Ken

p.s. Those guys who wrote FBtoC must really know what they're  
doing!    ;-)

p.p.s.  Carbon Events!?!? Show offs.   =:-}

/*

Nonsensical Demo

Mess of buttons and menus
all doin' the same thing just
to prove a point for Mr. Spoecker
and give me a chance to relax
at the keyboard.

Ken Shmidheiser
January 17, 2008

*/

begin enum 1
_mainWnd
end enum

begin enum 1
// Radio group
_cGroupBox
_cRadio
_cRadio1
_cRadio2
_cRadio3
_cRadio4
_cRadio5
_cRadio6
_cRadio7
_cRadio8
_cRadio9

// Color popup button
_colorPop

// Regular push buttons
_blackPush
_brownPush
_redPush
_orangePush
_yellowPush
_greenPush
_bluePush
_violetPush
_grayPush
_whitePush

// Static label field
_statLabel

// Main response field
_colorField

// Color field
_rgbField

// Quit button
_quitBtn

end enum


#define FMFontFamily as SInt16
toolbox fn FMGetFontFamilyFromName(Str255 iName) = FMFontFamily


local fn GetSelectedRadioGroupBtn(c as long)
dim as ControlRef @ cRef
dim as OSErr        err

err = fn GetIndexedSubControl(button&(c),¬
       button(c, _FBGetCtlRawValue), cRef)

end fn = usr Handle2Btn(cRef)

local fn BuildPopMenu( id as long )
dim as MenuRef mRef
dim as str255 s

menu id, 0, _enable, ""
s = "Black;Brown;Red;Orange;Yellow;Green;Blue;Violet;Gray;White"
menu id, 1, _enable, s
mRef = fn  GetMenuHandle( id )
DeleteMenu( id)
InsertMenu( mRef,-1 )

end fn

local fn BuildMainWindow
dim as Str255            s
dim as ControlFontStyleRec tfs
dim as RGBColor          backRGB
dim as WindowAttributes     wa
dim as Rect              r

wa = _kWindowCloseBoxAttribute¬
      _kWindowCollapseBoxAttribute

def NewWindowPositionMethod(_kWindowCenterOnMainScreen)
SetRect(r, 0, 0, 355, 416)// w: 355 h: 416
appearance window -_mainWnd,¬
                   "Button Demo", @r,¬
                   _kDocumentWindowClass, wa
def NewWindowPositionMethod(0)
def SetWindowBackground(_kThemeActiveDialogBackgroundBrush, _zTrue)

SetRect(r, 247, 18, 327, 339)
appearance button _cGroupBox,¬
                   _activeBtn, _kControlSupportsEmbedding,,,, @r,¬
                   _kControlRadioGroupProc

SetRect(r, 252, 23, 322, 37)
appearance button _cRadio,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Black", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio, _cGroupBox)

SetRect(r, 252, 56, 322, 70)
appearance button _cRadio1,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Brown", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio1, _cGroupBox)

SetRect(r, 252, 89, 322, 103)
appearance button _cRadio2,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Red", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio2, _cGroupBox)

SetRect(r, 252, 122, 322, 136)
appearance button _cRadio3,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Orange", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio3, _cGroupBox)

SetRect(r, 252, 155, 322, 169)
appearance button _cRadio4,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Yellow", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio4, _cGroupBox)

SetRect(r, 252, 188, 322, 202)
appearance button _cRadio5,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Green", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio5, _cGroupBox)

SetRect(r, 252, 221, 322, 235)
appearance button _cRadio6,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Blue", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio6, _cGroupBox)

SetRect(r, 252, 254, 322, 268)
appearance button _cRadio7,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Violet", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio7, _cGroupBox)

SetRect(r, 252, 287, 322, 301)
appearance button _cRadio8,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "Gray", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio8, _cGroupBox)

SetRect(r, 252, 320, 322, 334)
appearance button _cRadio9,¬
                   _activeBtn, _kControlRadioButtonUncheckedValue,,,¬
                   "White", @r,¬
                   _kControlRadioButtonProc
def EmbedButton(_cRadio9, _cGroupBox)

// Color popup button
SetRect(r, 20, 18, 228, 38)
appearance button _colorPop,¬
                   _activeBtn,, 101, -1,¬
                   "Select a color:", @r,¬
                   _kControlPopupButtonProc

// Push buttons
SetRect(r, 19, 132, 89, 152)
appearance button _blackPush,¬
                   _activeBtn,,,,¬
                   "Black", @r,¬
                   _kControlPushButtonProc

SetRect(r, 19, 162, 89, 182)
appearance button _brownPush,¬
                   _activeBtn,,,,¬
                   "Brown", @r,¬
                   _kControlPushButtonProc

SetRect(r, 19, 192, 89, 212)
appearance button _redPush,¬
                   _activeBtn,,,,¬
                   "Red", @r,¬
                   _kControlPushButtonProc

SetRect(r, 19, 224, 89, 244)
appearance button _orangePush,¬
                   _activeBtn,,,,¬
                   "Orange", @r,¬
                   _kControlPushButtonProc

SetRect(r, 19, 256, 89, 276)
appearance button _yellowPush,¬
                   _activeBtn,,,,¬
                   "Yellow", @r,¬
                   _kControlPushButtonProc

SetRect(r, 101, 132, 171, 152)
appearance button _greenPush,¬
                   _activeBtn,,,,¬
                   "Green", @r,¬
                   _kControlPushButtonProc

SetRect(r, 101, 164, 171, 184)
appearance button _bluePush,¬
                   _activeBtn,,,,¬
                   "Blue", @r,¬
                   _kControlPushButtonProc

SetRect(r, 101, 196, 171, 216)
appearance button _violetPush,¬
                   _activeBtn,,,,¬
                   "Violet", @r,¬
                   _kControlPushButtonProc

SetRect(r, 101, 224, 171, 244)
appearance button _grayPush,¬
                   _activeBtn,,,,¬
                   "Gray", @r,¬
                   _kControlPushButtonProc

SetRect(r, 101, 256, 171, 276)
appearance button _whitePush,¬
                   _activeBtn,,,,¬
                   "White", @r,¬
                   _kControlPushButtonProc

// Static label
SetRect(r, 19, 350, 172, 366)
appearance button _statLabel,¬
                   _activeBtn,,,,, @r,¬
                   _kControlStaticTextProc
tfs.flags = _kControlUseFontMask¬
             _kControlUseSizeMask¬
             _kControlUseFaceMask¬
             _kControlUseJustMask
tfs.font = fn FMGetFontFamilyFromName("Lucida Grande")
tfs.size = 13
tfs.style = 0
tfs.just = _teJustCenter
def SetButtonFontStyle(_statLabel, tfs)
s = "Your selected color is:"
def SetButtonTextString(_statLabel, s)

// Main response field
SetRect(r, 79, 374, 168, 390)
appearance button _colorField,¬
                   _activeBtn,,,,, @r,¬
                   _kControlEditTextProc
def SetButtonFontStyle(_colorField, tfs)
s = "Black"
def SetButtonTextString(_colorField, s)

// Color field

backRGB.red   = 0
backRGB.green = 0
backRGB.blue  = 0

SetRect(r, 23, 374, 65, 390)
appearance button _rgbField,¬
                   _activeBtn,,,,, @r,¬
                   _kControlEditTextProc
tfs.flags = _kControlUseFontMask¬
             _kControlUseSizeMask¬
             _kControlUseFaceMask¬
             _kControlUseJustMask¬
        _kControlUseBackColorMask
tfs.backColor = backRGB
tfs.font = fn FMGetFontFamilyFromName("Lucida Grande")
tfs.size = 13
tfs.style = 0
tfs.just = _teJustLeft
def SetButtonFontStyle(_rgbField, tfs)

// Quit button
SetRect(r, 247, 370, 327, 390)
appearance button _quitBtn,¬
                   _activeBtn,,,,¬
                   "Quit", @r,¬
                   _kControlPushButtonProc

appearance button _cGroupBox,, 1
appearance window _mainWnd

end fn


// Set text in an Edit Text field
local fn DoColor( colorStr as str15 )
dim as ControlFontStyleRec tfs
dim as RGBColor          backRGB

select case ( colorStr )
case "Black"
backRGB.red   = 0
backRGB.green = 0
backRGB.blue  = 0

case "Brown"
backRGB.red   = 60000
backRGB.green = 40000
backRGB.blue  = 20000

case "Red"
backRGB.red   = 65535
backRGB.green = 0
backRGB.blue  = 0

case "Orange"
backRGB.red   = 65535
backRGB.green = 30000
backRGB.blue  = 0

case "Yellow"
backRGB.red   = 65535
backRGB.green = 65535
backRGB.blue  = 0

case "Green"
backRGB.red   = 0
backRGB.green = 65535
backRGB.blue  = 0

case "Blue"
backRGB.red   = 0
backRGB.green = 0
backRGB.blue  = 65535

case "Violet"
backRGB.red   = 65535
backRGB.green = 0
backRGB.blue  = 65535

case "Gray"
backRGB.red   = 50000
backRGB.green = 50000
backRGB.blue  = 50000

case "White"
backRGB.red   = 65535
backRGB.green = 65535
backRGB.blue  = 65535

end select

tfs.flags = _kControlUseBackColorMask
tfs.backColor = backRGB
def SetButtonFontStyle ( _rgbField, tfs        )
def SetButtonTextString( _rgbField, ""         )
def SetButtonTextString( _colorField, colorStr )

end fn


// Get text from a popup menu item
local fn GetPopupMenuText( menuID as long )
dim as long    mItem
dim as str255  txtStr
dim as MenuRef  mRef

mItem = button( menuID )
mRef = button( menuID, _FBgetControlMenuHandle )
call GetMenuItemText( mRef, mItem, txtStr )

fn DoColor( txtStr )

end fn


local fn DoDialog
dim as long    ev, id
dim as OSStatus ignore

ev = dialog(0)
id = dialog(ev)

select case (ev)

// Cheap way to kill Edit Text field entry
ignore = fn ClearKeyboardFocus( window( _wndRef ) )

// Bring window to front on click
case _wndClick : window id

// End program when window is closed
case _wndClose : end

// Handle various button clicks...
case _btnClick
// ... in our main window
select window(_outputWnd)
case _mainWnd
select id

/*
Radio buttons are embedded in
an invisble group button. When
a radio button is clicked, the
group button automatically
returns the value of the radio
button clicked. There is no
need to handle each radio
button individually using this
embedding method.
*/
case _cGroupBox
// Get value of radio button clicked
// as returned by the group box
select fn GetSelectedRadioGroupBtn(id)
case _cRadio  : fn DoColor( "Black"  )
case _cRadio1 : fn DoColor( "Brown"  )
case _cRadio2 : fn DoColor( "Red"    )
case _cRadio3 : fn DoColor( "Orange" )
case _cRadio4 : fn DoColor( "Yellow" )
case _cRadio5 : fn DoColor( "Green"  )
case _cRadio6 : fn DoColor( "Blue"   )
case _cRadio7 : fn DoColor( "Violet" )
case _cRadio8 : fn DoColor( "Gray"   )
case _cRadio9 : fn DoColor( "White"  )
end select

// Push buttons
case _colorPop   : fn GetPopupMenuText( _colorPop )

case _blackPush  : fn DoColor( "Black"  )
case _brownPush  : fn DoColor( "Brown"  )
case _redPush    : fn DoColor( "Red"    )
case _orangePush : fn DoColor( "Orange" )
case _yellowPush : fn DoColor( "Yellow" )
case _greenPush  : fn DoColor( "Green"  )
case _bluePush   : fn DoColor( "Blue"   )
case _violetPush : fn DoColor( "Violet" )
case _grayPush   : fn DoColor( "Gray"   )
case _whitePush  : fn DoColor( "White"  )

// Quit button
case _quitBtn : end

end select
end select
end select

end fn

'-------- Main Event Loop --------

on dialog fn DoDialog

fn BuildPopMenu(101)
fn BuildMainWindow

do
HandleEvents
until gFBQuit