[futurebasic] Re: [FB] Alternative for _kControlContentCIconRes in the age of no resource editors

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2013 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Wed, 8 May 2013 05:12:52 -0400
Kinda variation on a theme.

Throw any kind of image you want at the button.

Ken


include "Tlbx CoreGraphics.incl"
include "Tlbx ControlDefinitions.incl"

include resources "ButtonImage.png"

_nfoBtn = 1

local fn CreateCGImageFromBundleResource( fileName as CFStringRef ) as CGImageRef
'~'1
dim as CGImageRef        image : image = NULL
dim as CGImageSourceRef  source
dim as CFURLRef          url

url = fn CFBundleCopyResourceURL( fn CFBundleGetMainBundle(), fileName, 0, 0 )
long if ( url )
source = fn CGImageSourceCreateWithURL( url, 0 )
CFRelease( url )
long if ( source )
image = fn CGImageSourceCreateImageAtIndex( source, 0, 0 )
CFRelease( source )
end if
end if
end fn = image     // User must release


dim as OSStatus                  err     : err = _noErr
dim as ControlRef                control : control = NULL
dim as CGImageRef                image   : image = NULL
dim as UInt32                    imageWidth, imageHeight
dim as rect                      r
dim as ControlButtonContentInfo  content

image = fn CreateCGImageFromBundleResource( @"ButtonImage.png" )

content.contentType = _kControlContentCGImageRef
content.u.imageRef  = image

imageWidth  = fn CGImageGetWidth( image )
imageHeight = fn CGImageGetHeight( image )

setrect( r, 0,  0, 300, 500 )
appearance window 1, "Image In Button", ( 0, 0 )-( imageWidth + 40, imageHeight + 40 ), _kDocumentWindowClass, _kWindowCompositingAttribute

setrect( r, 10,  10, 200, 400 )
appearance button _nfoBtn, _activeBtn, 0,,,, ( 20, 20 )-( imageWidth + 20, imageHeight + 20 ), _kControlBevelButtonSmallBevelProc
err = fn SetBevelButtonContentInfo( button&( _nfoBtn ), @content )
CGImageRelease( image )

RunApplicationEventLoop()