The following code creates a text-containing scroll view and demonstrates
auto-hiding scroll bars as the window size is maximized. It also demonstrates a
toolbox function to set the minimum and maximum window resize limits.
Steve Van Voorst
'---------------- Start of Code --------------
/*
Sets window size limits.
Creates a resizable scroll view.
Thanks to R. Purves for assistance.
Written by S. Van Voorst.
*/
include "Tlbx CoreGraphics.Incl"
include "Tlbx HIView.Incl"
begin globals
Toolbox fn SetWindowResizeLimits( WindowRef inWindow, HISize * inMinLimits,
¬
HISize * inMaxLimits ) = OSStatus
_kWindowMetalAttribute = 1 << 8
_kWindowCompositingAttribute = 1 << 19
dim as HIViewRef @ imageView, scrollView, contentView
dim as HIRect myViewRect
begin enum 1
_textBtn
_quitBtn
end enum
end globals
local fn CalculateViewRectangle
'~'1
myViewRect.origin.x = 20.0
myViewRect.origin.y = 30.0
myViewRect.size.width = window(_width) - 40
myViewRect.size.height = window(_height) - 50
end fn
local fn InstallControlHitHandler( c as ControlRef )
'~'1
dim as EventTypeSpec controlEvents
begin globals
dim as proc sControlEventUPP // 'static' var
end globals
controlEvents.eventClass = _kEventClassControl
controlEvents.eventKind = _kEventControlHit
long if ( sControlEventUPP == _nil )
sControlEventUPP = fn NewEventHandlerUPP( [Proc "ControlEventHandler" ¬
+ _FBprocToProcPtrOffset] )
end if
end fn = fn InstallEventHandler( fn GetControlEventTarget( c ), ¬
sControlEventUPP, 1, @controlEvents, #0, #0 )
long if 0
"ControlEventHandler"
enterproc fn ControlEventHandler( nextHandler as EventHandlerCallRef, ¬
theEvent as EventRef, userData as Ptr ) = OSStatus
'~'1
dim as OSStatus ignore
dim as ControlRef @ controlRef
ignore = fn GetEventParameter( theEvent, _kEventParamDirectObject, ¬
_typeControlRef, #0, sizeof( controlRef ), #0, @controlRef )
fn SendBtnFBDialogEvt( controlRef ) // FB _btnClick
exitproc = _noErr // we handled
end if
local fn InstallWindowHandler( w as WindowRef )
'~'1
_nWindEventKinds = 3
dim as EventTypeSpec events(_nWindEventKinds - 1)
begin globals
dim as proc sWindowEventUPP // 'static' var
end globals
long if ( sWindowEventUPP == _nil )
sWindowEventUPP = fn NewEventHandlerUPP( [Proc "WindowEventHandler" ¬
+ _FBprocToProcPtrOffset] )
end if
events.eventClass(0) = _kEventClassWindow
events.eventKind(0) = _kEventWindowClose
events.eventClass(1) = _kEventClassWindow
events.eventKind(1) = _kEventWindowActivated
events.eventClass(2) = _kEventClassWindow
events.eventKind(2) = _kEventWindowBoundsChanged
end fn = fn InstallEventHandler( fn GetWindowEventTarget( w ), ¬
sWindowEventUPP, _nWindEventKinds, @events(0), #0, #0 )
long if 0
"WindowEventHandler"
enterproc fn WindowEventHandler( nextHandler as EventHandlerCallRef, ¬
theEvent as EventRef, userData as Ptr ) = OSStatus
'~'1
dim as WindowRef @ w
dim as ControlRef @ c
dim as UInt32 eventKind
dim as OSStatus result, ignore
dim as long wNum
result = _eventNotHandledErr
eventKind = fn GetEventKind( theEvent )
select fn GetEventClass( theEvent )
case _kEventClassWindow
ignore = fn GetEventParameter( theEvent, _kEventParamDirectObject, ¬
_typeWindowRef, #0, sizeof( WindowRef ), #0, @w )
wNum = fn FBGetWndNumber( w )
select eventKind
case _kEventWindowActivated
fn SendFBDialogEvt( _wndActivate, wNum )
case _kEventWindowClose
// -- Allow FB dialog handler to close window
fn SendFBDialogEvt( _wndClose, wNum )
result = _noErr // we handled
case _kEventWindowBoundsChanged
// -- Recalculate view rectangle
fn CalculateViewRectangle
// -- and redraw the scroll view.
result = fn HIViewSetFrame ( scrollView, @myViewRect )
end select
end select
exitproc = result
end if
local fn buildScrollView
'~'1
dim as WindowRef @ wRef
dim as rect r
dim as ptr textData, p
dim as long size, j, numChars
dim as OSStatus err
dim as CGImageRef @ outImage
get window 1, wRef
// ----- Text Generator -----
textData = fn NewPtr( 3200 )
size = fn GetPtrSize( textData )
j = 0
p = textData
while ( j < size )
p.nil`` = _"A" + rnd( _"z" - _"A" ) - 1
j++
p++
wend
// ----- Place text on a hidden button -----
SetRect( r, 10, 10, 600, 600 )
appearance button# -_textBtn,,,,,, @r, _kControlEditTextProc
def SetButtonData( _textBtn, _kControlEditTextPart,
_kControlEditTextTextTag,¬
size, #textData )
// ----- Create a CGImageRef from the hidden button -----
err = fn HIViewCreateOffScreenImage( button&(_textBtn), 0, @myViewRect,¬
outImage )
button close(_textBtn)'No longer needed.
// ----- Create scroll view -----
err = fn HIScrollViewCreate( _kHIScrollViewOptionsVertScroll¬
_kHIScrollViewOptionsHorizScroll, scrollView )
err = FN HIScrollViewSetScrollBarAutoHide( scrollView, _true )
err = fn HIViewSetVisible( scrollView, _true )
// ----- Get window's content view ------
err = fn HIViewFindByID( fn HIViewGetRoot( wRef ),¬
kHIViewWindowContentID.signature,¬
kHIViewWindowContentID.id, contentView )
// ----- Insert scroll view into content view -----
err = fn HIViewAddSubview( contentView, scrollView )
fn CalculateViewRectangle
err = fn HIViewSetFrame ( scrollView, @myViewRect )
// ----- Create image view using CGImageRef -----
err = fn HIImageViewCreate ( outImage, imageView )
err = FN CGImageRelease ( outImage )
err = fn HIViewSetVisible( imageView, _true )
// ----- Insert image view into scroll view -----
err = fn HIViewAddSubview ( scrollView, imageView )
end fn
LOCAL FN buildWnd
'~'1
dim as WindowRef @ wRef
dim as rect r
dim as HIPoint @ minWndSize, maxWndSize
dim as OSStatus err
dim as WindowAttributes attr
// ----- Window Attributes -----
attr = _kWindowStandardFloatingAttributes
attr += _kWindowStandardHandlerAttribute
attr += _kWindowResizableAttribute
attr += _kWindowCompositingAttribute
attr += _kWindowMetalAttribute
SetRect( r, 40, 50, 400, 400 )
appearance WINDOW -1, "Scroll View Resize_demo", @r, _kDocumentWindowClass,¬
attr
get window 1, wRef
// ----- Intercept some standard window events -----
fn InstallWindowHandler( wRef )
SetRect( r, 80, 0, 160, 20 )
appearance button# _quitBtn, _activeBtn, 1, 1, 1, "Quit", @r,¬
_kControlPushButtonProc
// ----- Intercept _kEventControlHit -----
fn InstallControlHitHandler( button&( _quitBtn) )
fn buildScrollView
// ---- Set Window Resize Limits ----
minWndSize.x = 200.0
minWndSize.y = 200.0
maxWndSize.x = 651.0
maxWndSize.y = 651.0
err = fn SetWindowResizeLimits( wRef, minWndSize, maxWndSize )
appearance window 1
END FN
LOCAL FN doDialog
'~'1
dim as long evnt, id
evnt = DIALOG(0)
id = DIALOG(evnt)
SELECT evnt
case _btnClick
select id
case _quitBtn
end
end select
CASE _wndClose
END
END SELECT
END FN
FN buildWnd
ON DIALOG FN doDialog
DO
HANDLEEVENTS
UNTIL 0
'------------------ End of Code ----------------
--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@...