Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Determine text dimensions for CreateStaticTextControl



After debugging this outside of my application I have come to two
solutions.  The reason the GetThemeTextDimensions was not returning the
correct width was a bug in the main application.  So the two solutions
that seem to work correctly are the following (the second solution works
for all controls, i.e. radio buttons, push buttons, check box).  FYI the
CalculateTextBounds seems to pad the width with an extra couple of
pixels.

void CalculateTextBounds( ControlRef hControl, CFStringRef cfString,
Rect* pBounds )
{
	if( cfString )
	{
		Point ptPoint = { 0, 0 };
		SInt16 wBaseline = 0;
		GetThemeTextDimensions( cfString, kThemeSystemFont,
kThemeStateActive,
			false, &ptPoint, &wBaseline );
		
		pBounds->right = pBounds->left + ptPoint.h;
		pBounds->bottom = pBounds->top + ptPoint.v;
	}
}

void CalculateControlBounds( ControlRef hControl, CFStringRef cfString,
Rect* pBounds )
{
	if( hControl )
	{
		bool bIsControlVisible = IsControlVisible( hControl );
		
		SetControlVisibility( hControl, false, false );
		
		Rect rectSave = { 0, 0, 0, 0 };
		GetControlBounds( hControl, &rectSave );
		
		Rect rectTest = { 0, 0, 0x7FFF, 0x7FFF };
		SetControlBounds( hControl, &rectTest );
		
		Rect rect = { 0, 0, 0, 0 };
		SInt16 wOffset = 0;
		GetBestControlRect( hControl, &rect, &wOffset );
		
		short wHeight = (rect.bottom-rect.top);
		
		UInt32 i;
		for( i = 1; i < 0x7FFF; i++ )
		{
			Rect rectTest = { 0, 0, 0, i };
			SetControlBounds( hControl, &rectTest );
			
			GetBestControlRect( hControl, &rect, &wOffset );
			
			if( (rect.bottom-rect.top) == wHeight )
			{
				// We're reached the correct width for a
single line control
				break;
			}
		}
		
		*pBounds = rect;

		SetControlBounds( hControl, &rectSave );
		
		SetControlVisibility( hControl, bIsControlVisible, false
);
	}
}

void CreateStaticTextControls( WindowRef hWindow )
{
	CFStringRef cfString = CFSTR( "Test String" );
	ControlRef hControl = NULL;
	Rect bounds1 = { 0, 0, 0, 0 };
	Rect bounds2 = { 0, 0, 0, 0 };

	CreateStaticTextControl( hWindow, &bounds1, cfString, NULL,
&hControl );

	CalculateTextBounds( hControl, cfString, &bounds1 );
	CalculateControlBounds( hControl, cfString, &bounds2 );

	HIRect rect = { bounds1.left, bounds1.top,
bounds1.right-bounds1.left, bounds1.bottom-bounds1.top };
	
	HIViewSetFrame( hControl, &rect );

	HIViewSetVisible( hControl, true );
}

Regards,

-Dan

-----Original Message-----
From: Mike Kluev [mailto:email@hidden] 
Sent: Monday, May 29, 2006 7:54 AM
To: Dan Winkler
Cc: carbon-dev
Subject: Re: Determine text dimensions for CreateStaticTextControl

On 22 May 2006, at 21:09, Dan Winkler wrote:

> Our application uses a custom layout manager for sizing dialog items
> etc.  I've been searching the lists for different ways of calculating
> the size needed to display text using static text control.  I seems
that
> the following does not give the correct size for the text.  Is there
> another function which can be used to calculate the space needed for
> specific text using a static text control?  If I use the dimensions
> returned from the following call to size the control, the text is
> clipped.
> 
> Point ptPoint = { 0, 0 };
> 
> SInt16 wBaseline = 0;
> 
> ::GetThemeTextDimensions( strText, kThemeSystemFont,
> kThemeStateActive, false, &ptPoint, &wBaseline );

Could it be the case that you just pass wrong font constant here?
E.g. the control uses kThemeEmphasizedSystemFont, etc. If that's
not the case and you have a reproducible one page app that shows
this issue I'd like to look at it.

Mike


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.