[futurebasic] Re: [FB] A Really dumb question (Sorry!)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 2007 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Thu, 16 Aug 2007 23:05:44 -0400
Brian asked,

> I searched the archives and could find how to add a scroll bar to a  
> regular FB edit field
> but I am using NIB resources
>
> What should I be searching for?

You could use a nib, like in my earlier post, or do it in code like  
this (somewhat buggy) example.

Using Interface  Builder you would first create the HiTextView and  
then embed it in a Scroll View using the IB's Layout -> Embed In ->  
Scroll View menu. (IB does the heavy lifting by creating the Scroll  
View in the background and embedding the HiTextView into it  
automatically.)

Programmatic construction of the a similar interface is somewhat  
backwards to the IB method. Here we first create the Scroll View  
which is attached to the parent window, then we build the HiTextView  
and then embed it in the Scroll View.  This all has to be done in a  
Compositing window for everything to work.

An extremely pleasant feature of IB is that it allows you to easily  
bind the views to the window. When window size is adjusted, the  
scrolling HiTextView follows suit. This code implements bindings, but  
in an imperfect sense. Drag around the grow box and shell how the  
view automatically follows. Notice the white space at the top of the  
view, I don't yet know how to fix it, but then again this is a brave  
new world for me.

To be more robust, this demo needs to have Carbon Event handlers  
installed. But it should give you the basic concept -- in code-- of  
what IB's nibs do in the background.

In summation: Use a nib if you can get away with it.

Ken

/*

    Programmatic creation
    of embedded Scrolling
    HiTextView with window
    bindings

    Ken Shmidheiser
    August 16, 2007

*/

include "Tlbx MacTextEditor.incl"

local fn BuildMenus
apple menu "Scrolling HiTextView..."
menu 1, 0, _enable, "File"
edit menu 2
end fn

local fn BuildHiTextViewWindow
dim as Rect               windowBounds
dim as HIRect                myViewRect, parentFrame
dim as HIViewRef           @ myScrollView, @ myTextView
dim as HIViewRef           @ myContentView, myRoot
dim as OSStatus              ignore
dim as TXNObject             tempView
dim as HILayoutInfo          layoutInfo
dim as CFStringRef         @ strRef
dim as CFMutableStringRef    mutStr
dim as str255             s(34)
dim as OSErr                 err

SetRect( windowBounds, 50, 50, 300, 300 )
appearance window -1, "", @windowBounds,¬
_kDocumentWindowClass, _kWindowStandardDocumentAttributes¬
_kWindowStandardHandlerAttribute _kWindowCompositingAttribute
ignore = fn HIScrollViewCreate( _kHIScrollViewOptionsVertScroll,¬
@ myScrollView )
myRoot = fn HIViewGetRoot( window (_wndRef) )
ignore = fn HIViewFindByID( myRoot,¬
kHIViewWindowContentID.signature,¬
kHIViewWindowContentID.id, myContentView )
ignore = fn HIViewAddSubview ( myContentView, myScrollView )

layoutInfo.binding.left.kind     = _kHILayoutBindLeft
layoutInfo.binding.left.toView   = 0
layoutInfo.binding.left.offset   = 0.0

layoutInfo.binding.top.kind      = _kHILayoutBindTop
layoutInfo.binding.top.toView    = 0
layoutInfo.binding.top.offset    = 0.0

layoutInfo.binding.right.kind    = _kHILayoutBindRight
layoutInfo.binding.right.toView  = 0
layoutInfo.binding.right.offset  = 0.0

layoutInfo.binding.bottom.kind   = _kHILayoutBindBottom
layoutInfo.binding.bottom.toView = 0
layoutInfo.binding.bottom.offset = 0.0

ignore = fn HIViewSetLayoutInfo( myScrollView, @layoutInfo )
ignore = fn HIViewGetFrame( ¬
          fn HIViewGetSuperview( myScrollView ), @parentFrame )
ignore = fn HIViewSetFrame( myScrollView, @parentFrame )
ignore = fn HITextViewCreate( #0, 0, 0, @myTextView )
ignore = fn HIViewAddSubview ( myScrollView, myTextView )
ignore = fn HIViewSetVisible ( myScrollView, _true )
ignore = fn HIViewSetVisible ( myTextView, _true )

mutStr = fn CFStringCreateMutable ( _kCFAllocatorDefault, 0 )

r$ = chr$(13)

s( 0) = "35"
s( 1) = "'Twas brillig, and the slithy toves"         + r$
s( 2) = "Did gyre and gimble in the wabe;"            + r$
s( 3) = "All mimsy were the borogoves,"               + r$
s( 4) = "And the mome raths outgrabe."                + r$
s( 5) =  r$
s( 6) = "'Beware the Jabberwock, my son!"             + r$
s( 7) = "The jaws that bite, the claws that catch!"   + r$
s( 8) = "Beware the Jubjub bird, and shun"            + r$
s( 9) = "The frumious Bandersnatch!'"                 + r$
s(10) =  r$
s(11) = "He took his vorpal sword in hand:"           + r$
s(12) = "Long time the manxome foe he sought--"       + r$
s(13) = "So rested he by the Tumtum tree,"            + r$
s(14) = "And stood awhile in thought."                + r$
s(15) =  r$
s(16) = "And as in uffish thought he stood,"          + r$
s(17) = "The Jabberwock, with eyes of flame,"         + r$
s(18) = "Came whiffling through the tulgey wood,"     + r$
s(19) = "And burbled as it came!"                     + r$
s(20) =  r$
s(21) = "One, two! One, two! And through and through" + r$
s(22) = "The vorpal blade went snicker-snack!"        + r$
s(23) = "He left it dead, and with its head"          + r$
s(24) = "He went galumphing back."                    + r$
s(25) =  r$
s(26) = "'And has thou slain the Jabberwock?"         + r$
s(27) = "Come to my arms, my beamish boy!"            + r$
s(28) = "O frabjous day! Callooh! Callay!'"           + r$
s(29) = "He chortled in his joy."                     + r$
s(30) =  r$
s(31) = "'Twas brillig, and the slithy toves"         + r$
s(32) = "Did gyre and gimble in the wabe;"            + r$
s(33) = "All mimsy were the borogoves,"               + r$
s(34) = "And the mome raths outgrabe."                + r$

for i = 1 to val( s(0) )
CFStringAppend( mutStr, fn CFSTR( s(i) ))
next i

err = fn SetControlData( myTextView, 0,¬
_kControlStaticTextCFStringTag, sizeof( CFStringRef ), mutStr )

CFRelease( mutStr )

window 1

end fn


local fn DoMenu
dim as long menuID, itemID

menuID = menu( _menuID )
itemID = menu( _itemID )

select case( menuID )
case 1
select( itemID )
end select
end select
menu

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 : window close 1
end select
end select

end fn

if ( system( _sysVers ) < 1040 )¬
then shutdown "Requires 10.4 or higher"

on menu   fn DoMenu
on dialog fn DoDialog

fn BuildMenus
fn BuildHiTextViewWindow

do
handleevents
until gFBQuit
end