[futurebasic] Re: IconRef

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2003 : Group Archive : Group : All Groups

From: "H. Gluender" <h@...>
Date: Tue, 11 Mar 2003 13:34:48 +0100
>>Does anyone have an idea of how to obtain an IconRef for an icon 
>>that is not in the desktop database.  I've been trying to use the 
>>Databrowser control. It appears to only accept the type IconRef 
>>when it displays icons.  I can use the icons that are in the 
>>desktop database but I cant seem to figure out how I can get icons 
>>from a resource file to be used.
>
>
>'~'A
>'                       Runtime : Rntm Appearance.Incl
>'                    CALL Req'd : Off
>'~'B
>
>/*
>How to 'register" an icon and obtain its IconRef.
>
>A well-known icon is obtained from the icns 128
>resource of the application, and used to construct
>a clickable icon button.
>
>Robert P.   11 March 2003
>*/
>
>include "Tlbx icons.incl"
>_kControlIconRefProc = 324
>
>end globals
>
>local mode
>local fn RegisterIcon( inCreator as OSType, inType as OSType, resID as short )
>'~'1
>dim as IconRef           @ result
>dim as FSSpec              appSpec
>dim as ProcessInfoRec      processInfo
>dim as ProcessSerialNumber curPSN
>dim as OSErr               err
>
>result = 0 // default if error
>processInfo.processInfoLength = sizeof( ProcessInfoRec )
>processInfo.processName       = _nil // don't want name
>processInfo.processAppSpec    = appSpec
>curPSN.highLongOfPSN = 0
>curPSN.lowLongOfPSN  = _kCurrentProcess
>long if ( fn GetProcessInformation( curPSN, processInfo ) == _noErr )
>err = fn RegisterIconRefFromResource( inCreator, inType, appSpec, 
>resID, @result )
>end if
>end fn = result // return the IconRef
>
>// main program
>'~'1
>dim as Rect                      r
>dim as IconRef                   myIcnRef
>dim as ControlButtonContentInfo  myCBCInfo
>window 1
>
>// register IconRef from icns 128, with arbitrary creator and type
>myCBCInfo.iconRef     = fn RegisterIcon( _"ABCD", _"EFGH", 128 )
>myCBCInfo.contentType = _kControlContentIconRef
>
>SetRect( r, 180, 120, 308, 248 )
>appearance button -1,, myIcnRef,,,, @r, _kControlIconRefProc
>def SetButtonData( 1, _kControlEntireControl, _"cont", sizeof( 
>myCBCInfo ), myCBCInfo )
>appearance button 1 // make visible
>do
>HandleEvents
>until 0
>
>Robert P.


No doubt, this is the way to go -- via the icon database.

I should like to add two details however.
(1)
There may be disk partitions that need the following addition:

err = FN SetCustomIconsEnabled( appSpec.vRefNum, _zTrue )
before
err = FN RegisterIconRefFromResource( inCreator, inType, appSpec, 
resID, @result )

(2)
For frequent reuse of the icon, "result" should be a global var.
Otherwise, the IconRef should be released after use.
This doesn't mean that the occupied memory is immediately dispose of 
but, if nobody else has registered, it will. (This represents a nice 
example for the understanding of "Reference Counts" that are 
essential for OSX programming.)

err = FN ReleaseIconRef( result )


Happy coding!
-- 


                   Herbie

          ------------------------

          <http://www.gluender.de>