[futurebasic] List for CompositingWnd

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 2004 : Group Archive : Group : All Groups

From: SVANVOORST@...
Date: Sun, 19 Sep 2004 23:42:46 EDT
The following code creates a list control based on a user pane for use in a 
compositing window.   Watch for line breaks.

Steve Van Voorst

'--------- Start of Code ----------

/*
   Creates a list control with user panes.
   Written by S.Van Voorst
*/

include "Tlbx CarbonEvents.Incl"
Include "Tlbx HIView.Incl"

begin globals

_kWindowMetalAttribute        = 1 << 8
_kWindowCompositingAttribute = 1 << 19

_listElements = 51'Number of list items
_visRows = 10'Visible rows in list

_displayH   = 20'Height of display field

_listL = 150'Left coordinate of list
_listT = 50'Top coordinate of list
_listW = 140'List width

begin enum 1
_listBtn 
_scroll
_list
_display
_showBtn
_hideBtn
_quitBtn
end enum

dim as rect   gListRect'Rectangle for list
dim as short gLineHt'Line height based on font size
dim as long   gSelection'Selected item

dim as str255 gList$(_listElements)

end globals

local fn loadList$
'~'1
dim as long x

for x = 1 to _listElements
gList$(x) = "Item "+str$(x)
next

end fn

local fn InstallWindowHandler( w as WindowRef )
'~'1
_nWindEventKinds = 4

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)   = _kEventWindowActivated

events.eventClass(1) = _kEventClassWindow
events.eventKind(1)   = _kEventWindowClose

events.eventClass(2) = _kEventClassControl
events.eventKind(2)   = _kEventControlHit

events.eventClass(3) = _kEventClassMouse
events.eventKind(3)   = _kEventMouseDown

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

dim as WindowRef    @ w
dim as UInt32         eventClass, eventKind
dim as OSStatus       result, ignore, err
dim as long           wNum
dim as long     @ numClicks
dim as ControlRef    @ controlRef
dim as HIPoint     @ pt
Dim as Point   @ p
dim as long topItem
dim as long lineSelected
dim as str63 choice$

result = _eventNotHandledErr
eventClass = FN GetEventClass( theEvent )
eventKind   = fn GetEventKind( theEvent )

select eventClass

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
fn SendFBDialogEvt( _wndClose, wNum )
result = _noErr // FB handled

end select

case _kEventClassControl

ignore = fn GetEventParameter( theEvent, _kEventParamDirectObject, ¬
    _typeControlRef, #0, sizeof( controlRef ), #0, @controlRef )

select eventKind

case _kEventControlHit
fn SendBtnFBDialogEvt( controlRef ) // FB _btnClick

end select

case _kEventClassMouse

ignore = fn GetEventParameter( theEvent, _kEventParamMouseLocation, ¬
       _typeHIPoint, #0, sizeof( pt ), #0, @pt )

ignore = fn GetEventParameter( theEvent, _kEventParamClickCount, ¬
       _typeLongInteger, #0, sizeof( numClicks ), #0, @numClicks )

select eventKind

case _kEventMouseDown

p.h% = pt.x - _listL
p.v% = pt.y - _listT

globaltolocal( p )

long if fn ptinrect( p, gListRect )

topItem = button( _scroll, _FBGetCtlRawValue )

lineSelected = ( p.v%/gLineHt ) + 1

/* 
 Selection = lineSelected + number of items
 previously scrolled out of list view.
*/

gSelection = (topItem - 1) + lineSelected

choice$ =   gList$( gSelection )

def setbuttondata( _display, _kControlEditTextPart,¬
_kControlEditTextTextTag, choice$[0], @ choice$[1])

long if numClicks == 2
beep
end if

ignore = fn HIViewSetNeedsDisplay( button&( _list ), _true )

end if

end select

end select

exitproc = result
end if

local fn changeList
'~'1
Dim as OSStatus   ignore

ignore = fn HIViewSetNeedsDisplay( button&( _list ), _true )

end fn

local fn InstallMyControlAction( c as ControlRef )
'~'1
begin globals
dim as proc sMyControlActionUPP // 'static' var
end globals

long if ( sMyControlActionUPP == _nil )
sMyControlActionUPP = fn NewControlActionUPP( [Proc "MyControlAction" ¬
    + _FBprocToProcPtrOffset] )
end if
SetControlAction( c, sMyControlActionUPP )

end fn

long if 0
"MyControlAction"
enterproc fn MyControlAction( c as ControlRef, partCode as short )

dim as long   prev

prev = fn GetControlValue( c )

select partCode

case _kControlUpButtonPart// for Scroll Bars
SetControlValue( c, prev -1 )
long if (prev ) > Fn getControlMinimum(c)
fn changeList
end if

case _kControlDownButtonPart// for Scroll Bars
SetControlValue( c, prev + 1 )
long if (prev ) < Fn getControlMaximum(c)
fn changeList
end if

case _kInPageUpControlPart// for Scroll Bars
SetControlValue( c, prev - 10 )
long if (prev ) > Fn getControlMinimum(c)
fn changeList
end if

case _kInPageDownControlPart// for Scroll Bars
SetControlValue( c, prev + 10 )
long if (prev ) < Fn getControlMaximum(c)
fn changeList
end if

case _kControlIndicatorPart // Scroll Bar Thumb
fn changeList

end select

exitproc
end if

local fn drawContent(c as controlRef)
'~'1
begin globals
dim as pointer myControlUserPaneDrawUPP
end globals
long if (myControlUserPaneDrawUPP == 0)
myControlUserPaneDrawUPP = fn newcontrolUserPaneDrawUPP¬
([Proc "myUserPaneDrawProc"+ _FBprocToProcPtrOffset])
end if
end fn = fn setcontroldata(c,_kControlEntireControl,_"draw",¬
sizeof(myControlUserPaneDrawUPP),myControlUserPaneDrawUPP)

long if 0
"myUserPaneDrawProc"
enterproc fn myUserPaneDrawProc(c as ControlRef ,part as Sint16)

dim as rect   itemRect
dim as long    count
dim as OSStatus   err, ignore
dim as Ptr      @ themeState
dim as HIRect   bounds
dim as ControlRef   @ controlRef

count = button( _scroll, _FBGetCtlRawValue )

ignore = fn HIViewGetBounds( c, @bounds )

gListRect.left = bounds.origin.x
gListRect.top = bounds.origin.y
gListRect.right = gListRect.left + bounds.size.width
gListRect.bottom = gListRect.top + bounds.size.height

itemRect = gListRect
itemRect.bottom = gListRect.top + gLineHt

do

long if count <= _listElements

err = fn SetThemeBackground( _kThemeBrushWhite,¬
      system( _crntDepth ), _true )
text _sysFont, 12, _boldBit%

def Lbox(itemRect,"   " + gList$(count))' CBOX, RBOX optional

long if count = gSelection'Selected Line
err = fn GetThemeDrawingState( @themeState )'Remember
err = fn SetThemeBackground( _kThemeBrushPrimaryHighlightColor, ¬
      system( _crntDepth ), _true )
def Lbox( itemRect, "   " + gList$(count) )
err = fn SetThemeDrawingState( themeState, _true )'Restore/dispose
end if

end if

OffsetRect( itemRect, 0, gLineHt )
Inc(count)

until itemRect.top% => gListRect.bottom%

Color _zBlack
FrameRect( gListRect )

exitproc
end if

local fn BuildWindow
'~'1
dim as Rect         r
dim as WindowRef @ wRef
dim as OSStatus     err
dim as WindowAttributes   attr
dim as long maxScroll

attr   = _kWindowStandardFloatingAttributes
attr += _kWindowStandardHandlerAttribute
attr += _kWindowCompositingAttribute
attr += _kWindowMetalAttribute

SetRect( r, 0, 0, 470, 370 )
appearance window #-1, "usrPane List",@r, _kDocumentWindowClass, ¬
   attr 

get window 1, wRef
fn InstallWindowHandler( wRef ) // intercept some window CarbonEvents

fn loadList$

text _sysFont,12,_boldBit%'Declare font
gLineHt = usr fontheight'Get font height

// ----- superView ----- //
SetRect( r, _listL, _listT, _listL + _listW + 18, _listT + _visRows*gLineHt + 
2 )
appearance button# _listBtn,,_kControlSupportsEmbedding¬
,,,,@r,_kControlUserPaneProc

// ----- Display   is separate ----- //
SetRect( r, _listL, _listT - (_displayH + 2), _listL + _listW, _listT -2 )
InsetRect( r, 2, 2 )
appearance button _display,,,,,, @r, _kControlEditTextProc

// ----- List is included in superView ----- //
SetRect( r, 0, 0, _listW, _visRows*gLineHt + 2 )
appearance button# _list,,_kControlHandlesTracking,,,,@r,¬
_kControlUserPaneProc

fn drawContent( button&( _list ) )

// ----- Scroll is included in superView ----- //
maxScroll = _listElements - (_visRows) + 1'32K limit
SetRect( r, _listW, 0, _listW + 16, _visRows*gLineHt + 2 )
scroll button# _scroll, 1, 1, maxScroll, _visRows, @r, _scrollOther
fn InstallMyControlAction( button&( _scroll ) )

err = fn HIViewAddSubview( button&( _listBtn ), button&( _scroll ) )
err = fn HIViewAddSubview( button&( _listBtn ), button&( _list ) )

SetRect( r, 70, 300, 150, 320 )
appearance button# _hideBtn,,,,, "Hide", @r, _kControlPushButtonProc

OffsetRect( r, 100, 0 )
appearance button# _showBtn,,,,, "Show", @r, _kControlPushButtonProc

OffsetRect( r, 150, 0 )
appearance button# _quitBtn,,,,, "Quit", @r, _kControlPushButtonProc

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 _showBtn'Show list + display
appearance button# _listBtn
appearance button# _display

case _hideBtn'Hide list + display
appearance button#-_listBtn
appearance button#-_display

case _quitBtn
end

end select

case _wndClose
end

end select
end fn

fn BuildWindow
on dialog fn DoDialog
do
HandleEvents
until 0

'--------- End of Code ---------

--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@...