support max-width-chars property for labels

Change-Id: I23245919c5180035fedb308fb5cc97bf3238ec07
This commit is contained in:
Caolán McNamara
2012-10-08 21:01:24 +01:00
parent 34638df699
commit cd5d41470d
3 changed files with 62 additions and 21 deletions

View File

@@ -44,6 +44,8 @@ class UserDrawEvent;
class VCL_DLLPUBLIC FixedText : public Control class VCL_DLLPUBLIC FixedText : public Control
{ {
private: private:
sal_Int32 m_nMaxWidthChars;
using Control::ImplInitSettings; using Control::ImplInitSettings;
using Window::ImplInit; using Window::ImplInit;
SAL_DLLPRIVATE void ImplInit( Window* pParent, WinBits nStyle ); SAL_DLLPRIVATE void ImplInit( Window* pParent, WinBits nStyle );
@@ -71,10 +73,14 @@ public:
virtual void StateChanged( StateChangedType nType ); virtual void StateChanged( StateChangedType nType );
virtual void DataChanged( const DataChangedEvent& rDCEvt ); virtual void DataChanged( const DataChangedEvent& rDCEvt );
static Size CalcMinimumTextSize( Control const* pControl, long nMaxWidth = 0 ); void setMaxWidthChars(sal_Int32 nWidth);
Size CalcMinimumSize( long nMaxWidth = 0 ) const; sal_Int32 getMaxWidthChars() const { return m_nMaxWidthChars; }
static Size CalcMinimumTextSize(Control const* pControl, long nMaxWidth = 0x7fffffff);
static Size getTextDimensions(Control const *pControl, const OUString &rTxt, long nMaxWidth);
Size CalcMinimumSize(long nMaxWidth = 0x7fffffff) const;
virtual Size GetOptimalSize(WindowSizeType eType) const; virtual Size GetOptimalSize(WindowSizeType eType) const;
virtual void take_properties(Window &rOther); virtual void take_properties(Window &rOther);
virtual bool set_property(const rtl::OString &rKey, const rtl::OString &rValue);
}; };
// ------------- // -------------

View File

@@ -1748,7 +1748,7 @@ Size PushButton::GetOptimalSize(WindowSizeType eType) const
bool PushButton::set_property(const rtl::OString &rKey, const rtl::OString &rValue) bool PushButton::set_property(const rtl::OString &rKey, const rtl::OString &rValue)
{ {
if (rKey.equalsL(RTL_CONSTASCII_STRINGPARAM("has-default"))) if (rKey == "has-default")
{ {
WinBits nBits = GetStyle(); WinBits nBits = GetStyle();
nBits &= ~(WB_DEFBUTTON); nBits &= ~(WB_DEFBUTTON);
@@ -2912,7 +2912,7 @@ void RadioButton::SetState( sal_Bool bCheck )
bool RadioButton::set_property(const rtl::OString &rKey, const rtl::OString &rValue) bool RadioButton::set_property(const rtl::OString &rKey, const rtl::OString &rValue)
{ {
if (rKey.equalsL(RTL_CONSTASCII_STRINGPARAM("active"))) if (rKey == "active")
SetState(toBool(rValue)); SetState(toBool(rValue));
else else
return Window::set_property(rKey, rValue); return Window::set_property(rKey, rValue);
@@ -3885,7 +3885,7 @@ void CheckBox::SetState( TriState eState )
bool CheckBox::set_property(const rtl::OString &rKey, const rtl::OString &rValue) bool CheckBox::set_property(const rtl::OString &rKey, const rtl::OString &rValue)
{ {
if (rKey.equalsL(RTL_CONSTASCII_STRINGPARAM("active"))) if (rKey == "active")
SetState(toBool(rValue) ? STATE_CHECK : STATE_NOCHECK); SetState(toBool(rValue) ? STATE_CHECK : STATE_NOCHECK);
else else
return Window::set_property(rKey, rValue); return Window::set_property(rKey, rValue);

View File

@@ -154,16 +154,18 @@ void FixedText::ImplInitSettings( sal_Bool bFont,
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
FixedText::FixedText( Window* pParent, WinBits nStyle ) : FixedText::FixedText( Window* pParent, WinBits nStyle )
Control( WINDOW_FIXEDTEXT ) : Control(WINDOW_FIXEDTEXT)
, m_nMaxWidthChars(-1)
{ {
ImplInit( pParent, nStyle ); ImplInit( pParent, nStyle );
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
FixedText::FixedText( Window* pParent, const ResId& rResId ) : FixedText::FixedText( Window* pParent, const ResId& rResId )
Control( WINDOW_FIXEDTEXT ) : Control(WINDOW_FIXEDTEXT)
, m_nMaxWidthChars(-1)
{ {
rResId.SetRT( RSC_TEXT ); rResId.SetRT( RSC_TEXT );
WinBits nStyle = ImplInitRes( rResId ); WinBits nStyle = ImplInitRes( rResId );
@@ -188,8 +190,9 @@ void FixedText::take_properties(Window &rOther)
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
FixedText::FixedText( Window* pParent, const ResId& rResId, bool bDisableAccessibleLabelForRelation ) : FixedText::FixedText( Window* pParent, const ResId& rResId, bool bDisableAccessibleLabelForRelation )
Control( WINDOW_FIXEDTEXT ) : Control( WINDOW_FIXEDTEXT )
, m_nMaxWidthChars(-1)
{ {
rResId.SetRT( RSC_TEXT ); rResId.SetRT( RSC_TEXT );
WinBits nStyle = ImplInitRes( rResId ); WinBits nStyle = ImplInitRes( rResId );
@@ -397,14 +400,20 @@ void FixedText::DataChanged( const DataChangedEvent& rDCEvt )
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
Size FixedText::CalcMinimumTextSize( Control const *pControl, long nMaxWidth ) Size FixedText::getTextDimensions(Control const *pControl, const OUString &rTxt, long nMaxWidth)
{ {
sal_uInt16 nStyle = ImplGetTextStyle( pControl->GetStyle() ); sal_uInt16 nStyle = ImplGetTextStyle( pControl->GetStyle() );
if ( !( pControl->GetStyle() & WB_NOLABEL ) ) if ( !( pControl->GetStyle() & WB_NOLABEL ) )
nStyle |= TEXT_DRAW_MNEMONIC; nStyle |= TEXT_DRAW_MNEMONIC;
Size aSize = pControl->GetTextRect( Rectangle( Point(), Size( (nMaxWidth ? nMaxWidth : 0x7fffffff), 0x7fffffff ) ), return pControl->GetTextRect(Rectangle( Point(), Size(nMaxWidth, 0x7fffffff)),
pControl->GetText(), nStyle ).GetSize(); rTxt, nStyle).GetSize();
}
Size FixedText::CalcMinimumTextSize( Control const *pControl, long nMaxWidth )
{
Size aSize = getTextDimensions(pControl, pControl->GetText(), nMaxWidth);
if ( pControl->GetStyle() & WB_EXTRAOFFSET ) if ( pControl->GetStyle() & WB_EXTRAOFFSET )
aSize.Width() += 2; aSize.Width() += 2;
@@ -426,22 +435,48 @@ Size FixedText::CalcMinimumSize( long nMaxWidth ) const
Size FixedText::GetOptimalSize(WindowSizeType eType) const Size FixedText::GetOptimalSize(WindowSizeType eType) const
{ {
switch (eType) { switch (eType)
case WINDOWSIZE_MINIMUM: {
return CalcMinimumSize(); case WINDOWSIZE_MINIMUM:
default: {
return Control::GetOptimalSize( eType ); sal_Int32 nMaxAvailWidth = 0x7fffffff;
const OUString &rTxt = GetText();
if (m_nMaxWidthChars != -1 && m_nMaxWidthChars < rTxt.getLength())
{
nMaxAvailWidth = getTextDimensions(this,
rTxt.copy(0, m_nMaxWidthChars), 0x7fffffff).Width();
}
return CalcMinimumSize(nMaxAvailWidth);
}
default:
return Control::GetOptimalSize( eType );
} }
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
void FixedText::FillLayoutData() const void FixedText::FillLayoutData() const
{ {
mpControlData->mpLayoutData = new vcl::ControlLayoutData(); mpControlData->mpLayoutData = new vcl::ControlLayoutData();
ImplDraw( const_cast<FixedText*>(this), 0, Point(), GetOutputSizePixel(), true ); ImplDraw( const_cast<FixedText*>(this), 0, Point(), GetOutputSizePixel(), true );
} }
void FixedText::setMaxWidthChars(sal_Int32 nWidth)
{
m_nMaxWidthChars = nWidth;
queue_resize();
}
bool FixedText::set_property(const rtl::OString &rKey, const rtl::OString &rValue)
{
if (rKey == "max-width-chars")
{
setMaxWidthChars(rValue.toInt32());
}
else
return Control::set_property(rKey, rValue);
}
// ======================================================================= // =======================================================================
void FixedLine::ImplInit( Window* pParent, WinBits nStyle ) void FixedLine::ImplInit( Window* pParent, WinBits nStyle )
@@ -705,7 +740,7 @@ Size FixedLine::GetOptimalSize(WindowSizeType eType) const
{ {
switch (eType) { switch (eType) {
case WINDOWSIZE_MINIMUM: case WINDOWSIZE_MINIMUM:
return CalcWindowSize( FixedText::CalcMinimumTextSize ( this ) ); return CalcWindowSize( FixedText::CalcMinimumTextSize ( this, 0x7fffffff ) );
default: default:
return Control::GetOptimalSize( eType ); return Control::GetOptimalSize( eType );
} }