cws tl74: #i109418# fixed an issue with the hyphenation dialog

This commit is contained in:
Thomas Lange [tl] 2010-03-09 14:22:30 +01:00
parent 4f4953cc81
commit 8de5feec5d
2 changed files with 160 additions and 102 deletions

View file

@ -48,25 +48,31 @@
using namespace ::com::sun::star;
// define ----------------------------------------------------------------
#define HYPHHERE '-'
#define HYPH_POS_CHAR '='
#define CONTINUE_HYPH USHRT_MAX
// Dies ist nur ein Hack, weil an dieser Stelle das extern char aus hmwrap
// nicht bekannt ist, wird demnaechst in hmwrap durch ein define ersetzt.
#define SW_SOFT_HYPHEN '='
#define CUR_HYPH_POS_CHAR '-'
// class SvxHyphenEdit ---------------------------------------------------
SvxHyphenEdit::SvxHyphenEdit( Window* pParent, const ResId& rResId ) :
// class HyphenEdit_Impl -------------------------------------------------------
class HyphenEdit_Impl : public Edit
{
public:
HyphenEdit_Impl( Window* pParent, const ResId& rResId );
protected:
virtual void KeyInput( const KeyEvent &rKEvt );
};
HyphenEdit_Impl::HyphenEdit_Impl( Window* pParent, const ResId& rResId ) :
Edit( pParent, rResId )
{
}
void SvxHyphenEdit::KeyInput( const KeyEvent& rKEvt )
void HyphenEdit_Impl::KeyInput( const KeyEvent& rKEvt )
{
// sal_uInt16 nMod = rKEvt.GetKeyCode().GetModifier();
sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
@ -92,6 +98,7 @@ void SvxHyphenEdit::KeyInput( const KeyEvent& rKEvt )
}
}
// struct SvxHyphenWordDialog_Impl ---------------------------------------------
struct SvxHyphenWordDialog_Impl
@ -100,7 +107,7 @@ struct SvxHyphenWordDialog_Impl
// Window * m_pParent;
FixedText aWordFT;
SvxHyphenEdit aWordEdit;
HyphenEdit_Impl aWordEdit;
ImageButton aLeftBtn;
ImageButton aRightBtn;
OKButton aOkBtn;
@ -114,11 +121,13 @@ struct SvxHyphenWordDialog_Impl
SvxSpellWrapper* pHyphWrapper;
uno::Reference< linguistic2::XHyphenator > xHyphenator;
uno::Reference< linguistic2::XPossibleHyphens > xPossHyph;
String aActWord; // actual (to be displayed) word
LanguageType nActLanguage; // and language
String aEditWord; // aEditWord and aWordEdit.GetText() differ only by the character for the current selected hyphenation position
String aActWord; // actual word to be hyphenated
LanguageType nActLanguage; // and its language
sal_uInt16 nMaxHyphenationPos; // right most valid hyphenation pos
sal_uInt16 nHyphPos;
sal_uInt16 nOldPos;
sal_Int32 nHyphenationPositionsOffset;
sal_Bool bBusy;
@ -128,6 +137,8 @@ struct SvxHyphenWordDialog_Impl
void InitControls_Impl();
void ContinueHyph_Impl( sal_uInt16 nInsPos = 0 );
sal_uInt16 GetHyphIndex_Impl();
void SelLeft_Impl();
void SelRight_Impl();
DECL_LINK( Left_Impl, Button* );
DECL_LINK( Right_Impl, Button* );
@ -177,6 +188,7 @@ SvxHyphenWordDialog_Impl::SvxHyphenWordDialog_Impl(
nMaxHyphenationPos ( 0 ),
nHyphPos ( 0 ),
nOldPos ( 0 ),
nHyphenationPositionsOffset( 0 ),
bBusy ( sal_False )
{
aActWord = rWord;
@ -184,6 +196,19 @@ SvxHyphenWordDialog_Impl::SvxHyphenWordDialog_Impl(
xHyphenator = xHyphen;
pHyphWrapper = pWrapper;
uno::Reference< linguistic2::XHyphenatedWord > xHyphWord( pHyphWrapper ?
pHyphWrapper->GetLast() : NULL, uno::UNO_QUERY );
DBG_ASSERT( xHyphWord.is(), "hyphenation result missing" );
if (xHyphWord.is())
{
DBG_ASSERT( aActWord == String( xHyphWord->getWord() ), "word mismatch" );
DBG_ASSERT( nActLanguage == SvxLocaleToLanguage( xHyphWord->getLocale() ), "language mismatch" );
nMaxHyphenationPos = xHyphWord->getHyphenationPos();
}
InitControls_Impl();
aWordEdit.GrabFocus();
aLeftBtn.SetClickHdl( LINK( this, SvxHyphenWordDialog_Impl, Left_Impl ) );
aRightBtn.SetClickHdl( LINK( this, SvxHyphenWordDialog_Impl, Right_Impl ) );
aOkBtn.SetClickHdl( LINK( this, SvxHyphenWordDialog_Impl, CutHdl_Impl ) );
@ -202,14 +227,14 @@ SvxHyphenWordDialog_Impl::~SvxHyphenWordDialog_Impl()
void SvxHyphenWordDialog_Impl::EnableLRBtn_Impl()
{
String aTxt( aWordEdit.GetText() );
String aTxt( aEditWord );
xub_StrLen nLen = aTxt.Len();
xub_StrLen i;
aRightBtn.Disable();
for ( i = nOldPos + 2; i < nLen; ++i )
{
if ( aTxt.GetChar( i ) == sal_Unicode( SW_SOFT_HYPHEN ) )
if ( aTxt.GetChar( i ) == sal_Unicode( HYPH_POS_CHAR ) )
{
aRightBtn.Enable();
break;
@ -222,7 +247,7 @@ void SvxHyphenWordDialog_Impl::EnableLRBtn_Impl()
aLeftBtn.Disable();
for ( i = nOldPos; i-- > 0; )
{
if ( aTxt.GetChar( i ) == sal_Unicode( SW_SOFT_HYPHEN ) )
if ( aTxt.GetChar( i ) == sal_Unicode( HYPH_POS_CHAR ) )
{
aLeftBtn.Enable();
break;
@ -237,21 +262,48 @@ String SvxHyphenWordDialog_Impl::EraseUnusableHyphens_Impl(
{
// returns a String showing only those hyphen positions which will result
// in a line break if hyphenation is done there
// 1) we will need to discard all hyphenation positions at th end that
// will not result in a line break where the text to the left still fits
// on the line.
// 2) since as from OOo 3.2 '-' are part of a word an thus text like
// 'multi-line-editor' is regarded as single word we also need to discard those
// hyphenation positions to the left of the rightmost '-' that is still left of
// the rightmost valid hyphenation position according to 1)
//
// Example:
// If the possible hyphenation position in 'multi-line-editor' are to eb marked
// by '=' then the text will look like this 'mul=ti-line-ed=it=or'.
// If now the first line is only large enough for 'multi-line-edi' we need to discard
// the last possible hyphnation point because of 1). The the right most valid
// hyphenation position is "ed=itor". The first '-' left of this position is
// "line-ed", thus because of 2) we now need to discard all possible hyphneation
// positions to the left of that as well. Thus in the end leaving us with just
// 'multi-line-ed=itor' as return value for this function. (Just one valid hyphenation
// position for the user too choose from. However ALL the '-' characters in the word
// will ALWAYS be valid implicit hyphenation positions for the core to choose from!
// And thus even if this word is skipped in the hyphenation dialog it will still be broken
// right after 'multi-line-' (actually it might already be broken up that way before
// the hyphenation dialog is called!).
// Thus rule 2) just eliminates those positions which will not be used by the core at all
// even if the user were to select one of them.
String aTxt;
DBG_ASSERT(rxPossHyph.is(), "missing possible hyphens");
if (rxPossHyph.is())
{
DBG_ASSERT( aActWord == rxPossHyph->getWord(), "word mismatch" );
aTxt = String( rxPossHyph->getPossibleHyphens() );
nHyphenationPositionsOffset = 0;
uno::Sequence< sal_Int16 > aHyphenationPositions(
rxPossHyph->getHyphenationPositions() );
sal_Int32 nLen = aHyphenationPositions.getLength();
const sal_Int16 *pHyphenationPos = aHyphenationPositions.getConstArray();
// find position nIdx after which all hyphen positions are unusable
xub_StrLen nIdx = STRING_NOTFOUND,
nPos;
xub_StrLen nIdx = STRING_NOTFOUND;
xub_StrLen nPos = 0, nPos1 = 0, nPos2 = 0;
if (nLen)
{
xub_StrLen nStart = 0;
@ -262,7 +314,7 @@ String SvxHyphenWordDialog_Impl::EraseUnusableHyphens_Impl(
else
{
// find corresponding hyphen pos in string
nPos = aTxt.Search( sal_Unicode( SW_SOFT_HYPHEN ), nStart );
nPos = aTxt.Search( sal_Unicode( HYPH_POS_CHAR ), nStart );
if (nStart == STRING_NOTFOUND)
break;
@ -276,12 +328,29 @@ String SvxHyphenWordDialog_Impl::EraseUnusableHyphens_Impl(
}
DBG_ASSERT(nIdx != STRING_NOTFOUND, "no usable hyphenation position");
// remove not usable hyphens from string
// 1) remove all not usable hyphenation positions from the end of the string
nPos = nIdx == STRING_NOTFOUND ? 0 : nIdx + 1;
String aTmp( sal_Unicode( SW_SOFT_HYPHEN ) ),
aEmpty;
nPos1 = nPos; //save for later use in 2) below
const String aTmp( sal_Unicode( HYPH_POS_CHAR ) );
const String aEmpty;
while (nPos != STRING_NOTFOUND)
nPos = aTxt.SearchAndReplace( aTmp, aEmpty, nPos + 1 );
// 2) remove all hyphenation positions from the start that are not considered by the core
const String aSearchRange( aTxt.Copy( 0, nPos1 ) );
nPos2 = aSearchRange.SearchBackward( '-' ); // the '-' position the core will use by default
if (nPos2 != STRING_NOTFOUND)
{
String aLeft( aSearchRange.Copy( 0, nPos2 ) );
nPos = 0;
while (nPos != STRING_NOTFOUND)
{
nPos = aLeft.SearchAndReplace( aTmp, aEmpty, nPos + 1 );
if (nPos != STRING_NOTFOUND)
++nHyphenationPositionsOffset;
}
aTxt.Replace( 0, nPos2, aLeft );
}
}
return aTxt;
}
@ -289,7 +358,6 @@ String SvxHyphenWordDialog_Impl::EraseUnusableHyphens_Impl(
void SvxHyphenWordDialog_Impl::InitControls_Impl()
{
String aTxt;
xPossHyph = NULL;
if (xHyphenator.is())
{
@ -297,15 +365,12 @@ void SvxHyphenWordDialog_Impl::InitControls_Impl()
xPossHyph = xHyphenator->createPossibleHyphens( aActWord, aLocale,
uno::Sequence< beans::PropertyValue >() );
if (xPossHyph.is())
{
aTxt = EraseUnusableHyphens_Impl( xPossHyph, nMaxHyphenationPos );
}
m_pDialog->SetWindowTitle( nActLanguage );
aEditWord = EraseUnusableHyphens_Impl( xPossHyph, nMaxHyphenationPos );
}
aWordEdit.SetText( aTxt );
aWordEdit.SetText( aEditWord );
nOldPos = aTxt.Len();
m_pDialog->SelLeft();
nOldPos = aEditWord.Len();
SelLeft_Impl();
EnableLRBtn_Impl();
}
@ -316,23 +381,24 @@ void SvxHyphenWordDialog_Impl::ContinueHyph_Impl( sal_uInt16 nInsPos )
{
if (nInsPos)
{
String aTmp( aWordEdit.GetText() );
String aTmp( aEditWord );
DBG_ASSERT(nInsPos <= aTmp.Len() - 2, "wrong hyphen position");
sal_uInt16 nIdxPos = 0;
sal_uInt16 i = 0;
sal_Unicode c;
while (i < aTmp.Len() && HYPHHERE != (c = aTmp.GetChar(i++)) )
sal_Int16 nIdxPos = -1;
for (sal_uInt16 i = 0; i <= nInsPos; ++i)
{
if (SW_SOFT_HYPHEN == c)
if (HYPH_POS_CHAR == aTmp.GetChar( i ))
nIdxPos++;
}
// take the possible hyphenation positions that got removed from the
// start of the wor dinot account:
nIdxPos += nHyphenationPositionsOffset;
uno::Sequence< sal_Int16 > aSeq = xPossHyph->getHyphenationPositions();
sal_Int32 nLen = aSeq.getLength();
DBG_ASSERT(nLen, "empty sequence");
DBG_ASSERT(nIdxPos < nLen, "index out of range");
if (nLen && nIdxPos < nLen)
DBG_ASSERT(0 <= nIdxPos && nIdxPos < nLen, "index out of range");
if (nLen && 0 <= nIdxPos && nIdxPos < nLen)
{
nInsPos = aSeq.getConstArray()[ nIdxPos ];
pHyphWrapper->InsertHyphen( nInsPos );
@ -356,6 +422,7 @@ void SvxHyphenWordDialog_Impl::ContinueHyph_Impl( sal_uInt16 nInsPos )
nActLanguage = SvxLocaleToLanguage( xHyphWord->getLocale() );
nMaxHyphenationPos = xHyphWord->getHyphenationPos();
InitControls_Impl();
m_pDialog->SetWindowTitle( nActLanguage );
}
}
else
@ -370,24 +437,69 @@ sal_uInt16 SvxHyphenWordDialog_Impl::GetHyphIndex_Impl()
for ( sal_uInt16 i=0 ; i < aTxt.Len(); ++i )
{
sal_Unicode nChar = aTxt.GetChar( i );
if ( nChar == HYPHHERE )
sal_Unicode cChar = aTxt.GetChar( i );
if ( cChar == CUR_HYPH_POS_CHAR )
break;
if ( nChar != SW_SOFT_HYPHEN )
if ( cChar != HYPH_POS_CHAR )
nPos++;
}
return nPos;
}
void SvxHyphenWordDialog_Impl::SelLeft_Impl()
{
DBG_ASSERT( nOldPos > 0, "invalid hyphenation position" );
if (nOldPos > 0)
{
String aTxt( aEditWord );
for ( xub_StrLen i = nOldPos - 1; i > 0; --i)
{
DBG_ASSERT(i <= aTxt.Len(), "index out of range");
if (aTxt.GetChar( i ) == sal_Unicode( HYPH_POS_CHAR ))
{
aTxt.SetChar( i, sal_Unicode( CUR_HYPH_POS_CHAR ) );
nOldPos = i;
aWordEdit.SetText( aTxt );
aWordEdit.GrabFocus();
aWordEdit.SetSelection( Selection( i, i + 1 ) );
break;
}
}
nHyphPos = GetHyphIndex_Impl();
EnableLRBtn_Impl();
}
}
void SvxHyphenWordDialog_Impl::SelRight_Impl()
{
String aTxt( aEditWord );
for ( xub_StrLen i = nOldPos + 1; i < aTxt.Len(); ++i )
{
if (aTxt.GetChar( i ) == sal_Unicode( HYPH_POS_CHAR ))
{
aTxt.SetChar( i, sal_Unicode( CUR_HYPH_POS_CHAR ) );
nOldPos = i;
aWordEdit.SetText( aTxt );
aWordEdit.GrabFocus();
aWordEdit.SetSelection( Selection( i, i + 1 ) );
break;
}
}
nHyphPos = GetHyphIndex_Impl();
EnableLRBtn_Impl();
}
IMPL_LINK( SvxHyphenWordDialog_Impl, CutHdl_Impl, Button *, EMPTYARG )
{
if( !bBusy )
{
bBusy = sal_True;
ContinueHyph_Impl( nHyphPos );
ContinueHyph_Impl( /*nHyphPos*/nOldPos );
bBusy = sal_False;
}
return 0;
@ -408,7 +520,7 @@ IMPL_LINK( SvxHyphenWordDialog_Impl, HyphenateAllHdl_Impl, Button *, EMPTYARG /*
xProp->setPropertyValue( aName, aAny );
bBusy = sal_True;
ContinueHyph_Impl( nHyphPos );
ContinueHyph_Impl( /*nHyphPos*/nOldPos );
bBusy = sal_False;
aAny <<= sal_False;
@ -466,7 +578,7 @@ IMPL_LINK( SvxHyphenWordDialog_Impl, Left_Impl, Button *, EMPTYARG )
if( !bBusy )
{
bBusy = sal_True;
m_pDialog->SelLeft();
SelLeft_Impl();
bBusy = sal_False;
}
return 0;
@ -478,7 +590,7 @@ IMPL_LINK( SvxHyphenWordDialog_Impl, Right_Impl, Button *, EMPTYARG )
if( !bBusy )
{
bBusy = sal_True;
m_pDialog->SelRight();
SelRight_Impl();
bBusy = sal_False;
}
return 0;
@ -507,14 +619,6 @@ SvxHyphenWordDialog::SvxHyphenWordDialog(
FreeResource();
uno::Reference< linguistic2::XHyphenatedWord > xHyphWord( pWrapper ?
pWrapper->GetLast() : uno::Reference< uno::XInterface > () , uno::UNO_QUERY );
DBG_ASSERT( xHyphWord.is(), "missing hyphenated word" );
m_pImpl->nMaxHyphenationPos = xHyphWord.is() ? xHyphWord->getHyphenationPos() : 0;
m_pImpl->InitControls_Impl();
m_pImpl->aWordEdit.GrabFocus();
SetWindowTitle( nLang );
// disable controls if service is not available
@ -541,48 +645,13 @@ void SvxHyphenWordDialog::SetWindowTitle( LanguageType nLang )
void SvxHyphenWordDialog::SelLeft()
{
String aTxt( m_pImpl->aWordEdit.GetText() );
for ( xub_StrLen i = m_pImpl->nOldPos + 1; i-- > 0 ; )
{
DBG_ASSERT(i <= aTxt.Len(), "index out of range");
if( aTxt.GetChar( i ) == sal_Unicode( SW_SOFT_HYPHEN ) )
{
aTxt.SetChar( i, sal_Unicode( HYPHHERE ) );
if ( m_pImpl->nOldPos != 0 && m_pImpl->nOldPos != aTxt.Len() )
aTxt.SetChar( m_pImpl->nOldPos, sal_Unicode( SW_SOFT_HYPHEN ) );
m_pImpl->nOldPos = i;
m_pImpl->aWordEdit.SetText( aTxt );
m_pImpl->aWordEdit.GrabFocus();
m_pImpl->aWordEdit.SetSelection( Selection( i, i + 1 ) );
break;
}
}
m_pImpl->nHyphPos = m_pImpl->GetHyphIndex_Impl();
m_pImpl->EnableLRBtn_Impl();
m_pImpl->SelRight_Impl();
}
void SvxHyphenWordDialog::SelRight()
{
String aTxt( m_pImpl->aWordEdit.GetText() );
for ( xub_StrLen i = m_pImpl->nOldPos + 1; i < aTxt.Len(); ++i )
{
if( aTxt.GetChar( i ) == sal_Unicode( SW_SOFT_HYPHEN ) )
{
aTxt.SetChar( i, sal_Unicode( HYPHHERE ) );
if ( m_pImpl->nOldPos != 0 && m_pImpl->nOldPos != aTxt.Len() )
aTxt.SetChar( m_pImpl->nOldPos, sal_Unicode( SW_SOFT_HYPHEN ) );
m_pImpl->nOldPos = i;
m_pImpl->aWordEdit.SetText( aTxt );
m_pImpl->aWordEdit.GrabFocus();
m_pImpl->aWordEdit.SetSelection( Selection( i, i + 1 ) );
break;
}
}
m_pImpl->nHyphPos = m_pImpl->GetHyphIndex_Impl();
m_pImpl->EnableLRBtn_Impl();
m_pImpl->SelLeft_Impl();
}

View file

@ -47,17 +47,6 @@ namespace linguistic2{
class SvxSpellWrapper;
// class SvxHyphenEdit ---------------------------------------------------
class SvxHyphenEdit : public Edit
{
public:
SvxHyphenEdit( Window* pParent, const ResId& rResId );
protected:
virtual void KeyInput( const KeyEvent &rKEvt );
};
// class SvxHyphenWordDialog ---------------------------------------------
struct SvxHyphenWordDialog_Impl;