[futurebasic] Embedded Controls -- Why and How

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2002 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 15 Nov 2002 11:09:22 -0500
In a recent thread, Ian Mann discovered how to perform a simple
embedding of a list control into a user pane.

Understanding how embedded controls work, and why we use them, will
become increasingly important as many FB^3 users shift to OS X and
the Carbon environment.

For those of us who are right-brained, a very nice pictorial concept
of a simple example of embedded radio buttons in a user pane can be
found by scrolling about a third of the way down on this page:

http://www.mactech.com/macintosh-c/chap07-1.html

In fact, this information, coupled with that in:

http://www.mactech.com/macintosh-c/chap14-1.html

and:

http://www.mactech.com/macintosh-c/chap22-1.html

will provide tremendously valuable insights into the operation of
controls in both Classic and OS X.

This reference is so valuable to me that I downloaded the entire
document and concatenated into one huge PDF file which resides in my
programming utility folder.

Why embed controls?

Embedding allows you to activate, deactivate, hide, show, and move
controls as a group. It also allows you to establish a drawing order
and embedded controls can be embedded into other controls for
additional flexibility.

How do you embed controls?

FB^3 provides some excellent tools. Here is simple demo using three
radio buttons, embedded into an invisible automatic radio button
toggler controller which, in turn, is embedded into a group box. The
state and visibility of the embedded group, is controlled simply by
controlling the group box which is the parent control.

Ken

p.s. Please watch for e-mail line breaks and lost constant
underscores on the Associate server

/*

  Simple example to demonstrate
  embedding principles using radio buttons

  Ken Shmidheiser
  Somerset, KY
  11-15-02

*/

#if cpu68K
compile shutdown "Requires PPC or Carbon Compile"
#endif

begin enum 1
_quitBtn
_stateBtn
_visibilityBtn
_groupBoxBtn
_radioGroupBtn
_radioOneBtn
_radioTwoBtn
_radioThreeBtn
end enum

local fn BuildWindow
dim as rect r

// Create invisible window
setrect( r, 0, 0, 300, 180 )
appearance Window -1, "Embedded Buttons", @r,¬
_kDocumentWindowClass, _kWindowStandardFloatingAttributes

// Set window background
def SetWindowBackground( _kThemeActiveDialogBackgroundBrush,_zTrue)

// Create group box to hold radio buttons
setrect( r, 19, 9, 131, 121 )
appearance button _groupBoxBtn, _activeBtn, 0, 0, 1,¬
"Colors", @r, _kControlGroupBoxTextTitleProc

// Create invisible radio button controller
// to handle auto-toggling of radio buttons
setrect( r, 20, 10, 130, 120 )
appearance button _radioGroupBtn, _activeBtn, 0, 0, 1,¬
, @r, _kControlRadioGroupProc

// Creat the three radio buttons
setrect( r, 30, 40, 100, 52 )
appearance button _radioOneBtn, _activeBtn, 0, 0, 1,¬
"Red", @r, _kControlRadioButtonProc
offsetrect( r, 0, 25 )
appearance button _radioTwoBtn, _activeBtn, 0, 0, 1,¬
"Green", @r, _kControlRadioButtonProc
offsetrect( r, 0, 25 )
appearance button _radioThreeBtn, _activeBtn, 0, 0, 1,¬
"Blue", @r, _kControlRadioButtonProc

// Establish embedding heirarchy

// Embed invisible radio button controller
// into the group box
def embedbutton( _radioGroupBtn,   _groupBoxBtn )

// Embed each radio button into invisible controller
def embedbutton( _radioOneBtn,   _radioGroupBtn )
def embedbutton( _radioTwoBtn,   _radioGroupBtn )
def embedbutton( _radioThreeBtn, _radioGroupBtn )

text _sysFont, 48
setrect( r, 141, 40, 290, 90 )
edit field 1, "Red", @r, _statNoFramed

setrect( r, 20, 140, 95, 160 )
button _visibilityBtn, _activeBtn, "Hide", @r, _push

setrect( r, 105, 140, 210, 160 )
button _stateBtn, _activeBtn, "Deactivate", @r, _push

setrect( r, 220, 140, 280, 160 )
button _quitBtn, _activeBtn, "Quit", @r, _shadow

// Make window visible
window 1

end fn

local fn DoRadioGroup
dim as long     btnID
dim as rgbcolor foreRGB

btnID = button( _radioGroupBtn )

select case btnID
case 1 : edit$(1) = "Red"
case 2 : edit$(1) = "Green"
case 3 : edit$(1) = "Blue"
end select

edit field 1

end fn

// This function controls visibility of group box,
// which in turn affects visibility of all controls
// embedded in it, in this case the radio buttons.
local fn GroupBoxVisibility
dim as handle  btnH
dim as boolean visible

    btnH = button&( _groupBoxBtn )
visible = fn IsControlvisible( btnH )

long if visible
appearance button -_groupBoxBtn
button _visibilityBtn, _activeBtn, "Show"
xelse
appearance button _groupBoxBtn, _activeBtn
button _visibilityBtn, _activeBtn, "Hide"
end if

end fn

// This function controls the state (active or
// inactive ) of the group box, which in turn
// affects state of all controls embedded in it,
// in this case the radio buttons.
local fn GroupBoxState
dim as handle  btnH
dim as boolean active

   btnH = button&( _groupBoxBtn )
active = fn IsControlActive( btnH )

long if active
appearance button _groupBoxBtn, _grayBtn
button _stateBtn, _activeBtn, "Reactivate"
xelse
appearance button _groupBoxBtn, _activeBtn
button _stateBtn, _activeBtn, "Deactivate"
end if

end fn

local fn DoDialog
dim as long evnt, id

evnt = dialog(0)
id = dialog(evnt)

select case( evnt )
case _wndClose
select( id )
case 1 :  gFBQuit = _zTrue
end select
case _btnClick
select( id )
case _quitBtn       : gFBQuit = _zTrue
case _visibilityBtn : fn GroupBoxVisibility
case _stateBtn      : fn GroupBoxState
case _radioGroupBtn : fn DoRadioGroup
end select
end select

end fn

on dialog fn DoDialog

fn buildWindow

do
handleevents
until gFBQuit
end