LibreOffice Module cui (master)  1
hyphen.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <hyphen.hxx>
21 
22 #include <com/sun/star/linguistic2/XLinguProperties.hpp>
23 
24 #include <editeng/splwrap.hxx>
25 #include <editeng/unolingu.hxx>
26 #include <svtools/langtab.hxx>
27 #include <sal/log.hxx>
29 #include <tools/debug.hxx>
30 
31 #define HYPH_POS_CHAR '='
32 
33 #define CUR_HYPH_POS_CHAR '-'
34 
35 using namespace css;
36 
37 IMPL_LINK_NOARG(SvxHyphenWordDialog, CursorChangeHdl_Impl, weld::Entry&, void)
38 {
39  int nStart, nEnd;
40  m_xWordEdit->get_selection_bounds(nStart, nEnd);
41  if (nStart == m_nOldPos && nEnd == m_nOldPos + 1)
42  return;
43  bool bReSelect;
44  if (nStart <= m_nOldPos)
45  bReSelect = !SelLeft();
46  else
47  bReSelect = !SelRight();
48  if (bReSelect)
49  select_region(m_nOldPos, m_nOldPos + 1);
50 }
51 
53 {
54  const sal_Int32 nLen = m_aEditWord.getLength();
55 
56  m_xRightBtn->set_sensitive(false);
57  for ( sal_Int32 i = m_nOldPos + 2; i < nLen; ++i )
58  {
59  if ( m_aEditWord[ i ] == sal_Unicode( HYPH_POS_CHAR ) )
60  {
61  m_xRightBtn->set_sensitive(true);
62  break;
63  }
64  }
65 
66  DBG_ASSERT(m_nOldPos < nLen, "nOldPos out of range");
67  if (m_nOldPos >= nLen)
68  m_nOldPos = nLen - 1;
69  m_xLeftBtn->set_sensitive(false);
70  for ( sal_Int32 i = m_nOldPos; i-- > 0; )
71  {
72  if ( m_aEditWord[ i ] == sal_Unicode( HYPH_POS_CHAR ) )
73  {
74  m_xLeftBtn->set_sensitive(true);
75  break;
76  }
77  }
78 }
79 
81 {
82  // returns a String showing only those hyphen positions which will result
83  // in a line break if hyphenation is done there
84  // 1) we will need to discard all hyphenation positions at the end that
85  // will not result in a line break where the text to the left still fits
86  // on the line.
87  // 2) since as from OOo 3.2 '-' are part of a word and thus text like
88  // 'multi-line-editor' is regarded as single word we also need to discard those
89  // hyphenation positions to the left of the rightmost '-' that is still left of
90  // the rightmost valid hyphenation position according to 1)
91 
92  // Example:
93  // If the possible hyphenation position in 'multi-line-editor' are to be marked
94  // by '=' then the text will look like this: 'mul=ti-line-ed=it=or'.
95  // If now the first line is only large enough for 'multi-line-edi' we need to discard
96  // the last possible hyphenation point because of 1). The right most valid
97  // hyphenation position is "ed=itor". The first '-' left of this position is
98  // "line-ed", thus because of 2) we now need to discard all possible hyphenation
99  // positions to the left of that as well. Thus in the end leaving us with just
100  // 'multi-line-ed=itor' as return value for this function. (Just one valid hyphenation
101  // position for the user to choose from. However ALL the '-' characters in the word
102  // will ALWAYS be valid implicit hyphenation positions for the core to choose from!
103  // And thus even if this word is skipped in the hyphenation dialog it will still be broken
104  // right after 'multi-line-' (actually it might already be broken up that way before
105  // the hyphenation dialog is called!).
106  // Thus rule 2) just eliminates those positions which will not be used by the core at all
107  // even if the user were to select one of them.
108 
109  OUString aTxt;
110  DBG_ASSERT(m_xPossHyph.is(), "missing possible hyphens");
111  if (m_xPossHyph.is())
112  {
113  DBG_ASSERT( m_aActWord == m_xPossHyph->getWord(), "word mismatch" );
114 
115  aTxt = m_xPossHyph->getPossibleHyphens();
116 
117  m_nHyphenationPositionsOffset = 0;
118  uno::Sequence< sal_Int16 > aHyphenationPositions(
119  m_xPossHyph->getHyphenationPositions() );
120  sal_Int32 nLen = aHyphenationPositions.getLength();
121  const sal_Int16 *pHyphenationPos = aHyphenationPositions.getConstArray();
122 
123  // find position nIdx after which all hyphen positions are unusable
124  sal_Int32 nIdx = -1;
125  sal_Int32 nPos = 0, nPos1 = 0;
126  if (nLen)
127  {
128  sal_Int32 nStart = 0;
129  for (sal_Int32 i = 0; i < nLen; ++i)
130  {
131  if (pHyphenationPos[i] > m_nMaxHyphenationPos)
132  break;
133  else
134  {
135  // find corresponding hyphen positions in string
136  nPos = aTxt.indexOf( sal_Unicode( HYPH_POS_CHAR ), nStart );
137 
138  if (nPos == -1)
139  break;
140  else
141  {
142  nIdx = nPos;
143  nStart = nPos + 1;
144  }
145  }
146  }
147  }
148  DBG_ASSERT(nIdx != -1, "no usable hyphenation position");
149 
150  // 1) remove all not usable hyphenation positions from the end of the string
151  nPos = nIdx == -1 ? 0 : nIdx + 1;
152  nPos1 = nPos; //save for later use in 2) below
153  const OUString aTmp( sal_Unicode( HYPH_POS_CHAR ) );
154  while (nPos != -1)
155  {
156  nPos++;
157  aTxt = aTxt.replaceFirst( aTmp, "", &nPos);
158  }
159 
160  // 2) remove all hyphenation positions from the start that are not considered by the core
161  const OUString aSearchRange( aTxt.copy( 0, nPos1 ) );
162  sal_Int32 nPos2 = aSearchRange.lastIndexOf( '-' ); // the '-' position the core will use by default
163  if (nPos2 != -1 )
164  {
165  OUString aLeft( aSearchRange.copy( 0, nPos2 ) );
166  nPos = 0;
167  while (nPos != -1)
168  {
169  nPos++;
170  aLeft = aLeft.replaceFirst( aTmp, "", &nPos );
171  if (nPos != -1)
172  ++m_nHyphenationPositionsOffset;
173  }
174  aTxt = aTxt.replaceAt( 0, nPos2, aLeft );
175  }
176  }
177  return aTxt;
178 }
179 
181 {
182  m_xPossHyph = nullptr;
183  if (m_xHyphenator.is())
184  {
185  lang::Locale aLocale( LanguageTag::convertToLocale(m_nActLanguage) );
186  m_xPossHyph = m_xHyphenator->createPossibleHyphens( m_aActWord, aLocale,
187  uno::Sequence< beans::PropertyValue >() );
188  if (m_xPossHyph.is())
189  m_aEditWord = EraseUnusableHyphens_Impl();
190  }
191  m_xWordEdit->set_text(m_aEditWord);
192 
193  m_nOldPos = m_aEditWord.getLength();
194  SelLeft();
195  EnableLRBtn_Impl();
196 }
197 
198 void SvxHyphenWordDialog::ContinueHyph_Impl( sal_Int32 nInsPos )
199 {
200  if ( nInsPos >= 0 && m_xPossHyph.is() )
201  {
202  if (nInsPos)
203  {
204  DBG_ASSERT(nInsPos <= m_aEditWord.getLength() - 2, "wrong hyphen position");
205 
206  sal_Int32 nIdxPos = -1;
207  for (sal_Int32 i = 0; i <= nInsPos; ++i)
208  {
209  if (HYPH_POS_CHAR == m_aEditWord[ i ])
210  nIdxPos++;
211  }
212  // take the possible hyphenation positions that got removed from the
213  // start of the word into account:
214  nIdxPos += m_nHyphenationPositionsOffset;
215 
216  uno::Sequence< sal_Int16 > aSeq = m_xPossHyph->getHyphenationPositions();
217  sal_Int32 nLen = aSeq.getLength();
218  DBG_ASSERT(nLen, "empty sequence");
219  DBG_ASSERT(0 <= nIdxPos && nIdxPos < nLen, "index out of range");
220  if (nLen && 0 <= nIdxPos && nIdxPos < nLen)
221  {
222  nInsPos = aSeq.getConstArray()[ nIdxPos ];
223  m_pHyphWrapper->InsertHyphen( nInsPos );
224  }
225  }
226  else
227  {
229  m_pHyphWrapper->InsertHyphen( nInsPos );
230  }
231  }
232 
233  if ( m_pHyphWrapper->FindSpellError() )
234  {
235  uno::Reference< linguistic2::XHyphenatedWord > xHyphWord( m_pHyphWrapper->GetLast(), uno::UNO_QUERY );
236 
237  // adapt actual word and language to new found hyphenation result
238  if(xHyphWord.is())
239  {
240  m_aActWord = xHyphWord->getWord();
241  m_nActLanguage = LanguageTag( xHyphWord->getLocale() ).getLanguageType();
242  m_nMaxHyphenationPos = xHyphWord->getHyphenationPos();
243  InitControls_Impl();
244  SetWindowTitle( m_nActLanguage );
245  }
246  }
247  else
248  {
249  m_xCloseBtn->set_sensitive(false);
250  m_xDialog->response(RET_OK);
251  }
252 }
253 
255 {
256  bool bRet = false;
257  DBG_ASSERT( m_nOldPos > 0, "invalid hyphenation position" );
258  if (m_nOldPos > 0)
259  {
260  OUString aTxt( m_aEditWord );
261  for( sal_Int32 i = m_nOldPos - 1; i > 0; --i )
262  {
263  DBG_ASSERT(i <= aTxt.getLength(), "index out of range");
264  if (aTxt[ i ] == sal_Unicode( HYPH_POS_CHAR ))
265  {
266  aTxt = aTxt.replaceAt( i, 1, OUString( CUR_HYPH_POS_CHAR ) );
267 
268  m_nOldPos = i;
269  m_xWordEdit->set_text(aTxt);
270  select_region(i, i + 1);
271  m_xWordEdit->grab_focus();
272  bRet = true;
273  break;
274  }
275  }
276  EnableLRBtn_Impl();
277  }
278  return bRet;
279 }
280 
282 {
283  bool bRet = false;
284  OUString aTxt( m_aEditWord );
285  for ( sal_Int32 i = m_nOldPos + 1; i < aTxt.getLength(); ++i )
286  {
287  if (aTxt[ i ] == sal_Unicode( HYPH_POS_CHAR ))
288  {
289  aTxt = aTxt.replaceAt( i, 1, OUString( CUR_HYPH_POS_CHAR ) );
290 
291  m_nOldPos = i;
292  m_xWordEdit->set_text(aTxt);
293  select_region(i, i + 1);
294  m_xWordEdit->grab_focus();
295  bRet = true;
296  break;
297  }
298  }
299  EnableLRBtn_Impl();
300  return bRet;
301 }
302 
304 {
305  if( !m_bBusy )
306  {
307  m_bBusy = true;
308  ContinueHyph_Impl( /*m_nHyphPos*/m_nOldPos );
309  m_bBusy = false;
310  }
311 }
312 
313 IMPL_LINK_NOARG(SvxHyphenWordDialog, HyphenateAllHdl_Impl, weld::Button&, void)
314 {
315  if( !m_bBusy )
316  {
317  try
318  {
319  uno::Reference< linguistic2::XLinguProperties > xProp( LinguMgr::GetLinguPropertySet() );
320 
321  xProp->setIsHyphAuto( true );
322 
323  m_bBusy = true;
324  ContinueHyph_Impl( /*m_nHyphPos*/m_nOldPos );
325  m_bBusy = false;
326 
327  xProp->setIsHyphAuto( false );
328  }
329  catch (uno::Exception &)
330  {
331  SAL_WARN( "cui.dialogs", "Hyphenate All failed" );
332  }
333  }
334 }
335 
337 {
338  if( !m_bBusy )
339  {
340  m_bBusy = true;
341  ContinueHyph_Impl( 0 );
342  m_bBusy = false;
343  }
344 }
345 
347 {
348  if( !m_bBusy )
349  {
350  m_bBusy = true;
351  ContinueHyph_Impl();
352  m_bBusy = false;
353  }
354 }
355 
357 {
358  if( !m_bBusy )
359  {
360  m_bBusy = true;
361  m_xDialog->response(RET_CANCEL);
362  m_bBusy = false;
363  }
364 }
365 
367 {
368  if( !m_bBusy )
369  {
370  m_bBusy = true;
371  SelLeft();
372  m_bBusy = false;
373  }
374 }
375 
377 {
378  if( !m_bBusy )
379  {
380  m_bBusy = true;
381  SelRight();
382  m_bBusy = false;
383  }
384 }
385 
386 void SvxHyphenWordDialog::select_region(int nStart, int nEnd)
387 {
388  int nScrollPos = nStart + m_nWordEditWidth/2;
389  if (nScrollPos > m_aEditWord.getLength())
390  nScrollPos = m_aEditWord.getLength() - m_nWordEditWidth/2;
391  if (nScrollPos < 0)
392  nScrollPos = 0;
393  m_xWordEdit->set_position(nScrollPos);
394  m_xWordEdit->select_region(nStart, nEnd);
395 }
396 
398 {
399  select_region(m_nOldPos, m_nOldPos + 1);
400 }
401 
402 // class SvxHyphenWordDialog ---------------------------------------------
403 
405  const OUString &rWord, LanguageType nLang,
406  weld::Window* pParent,
407  uno::Reference< linguistic2::XHyphenator > const &xHyphen,
408  SvxSpellWrapper* pWrapper)
409  : SfxDialogController(pParent, "cui/ui/hyphenate.ui", "HyphenateDialog")
410  , m_pHyphWrapper(pWrapper)
411  , m_aActWord(rWord)
412  , m_nActLanguage(nLang)
413  , m_nMaxHyphenationPos(0)
414  , m_nOldPos(0)
415  , m_nHyphenationPositionsOffset(0)
416  , m_bBusy(false)
417  , m_xWordEdit(m_xBuilder->weld_entry("worded"))
418  , m_xLeftBtn(m_xBuilder->weld_button("left"))
419  , m_xRightBtn(m_xBuilder->weld_button("right"))
420  , m_xOkBtn(m_xBuilder->weld_button("ok"))
421  , m_xContBtn(m_xBuilder->weld_button("continue"))
422  , m_xDelBtn(m_xBuilder->weld_button("delete"))
423  , m_xHyphAll(m_xBuilder->weld_button("hyphall"))
424  , m_xCloseBtn(m_xBuilder->weld_button("close"))
425 {
426  m_nWordEditWidth = m_xWordEdit->get_width_chars();
427  m_aLabel = m_xDialog->get_title();
428  m_xHyphenator = xHyphen;
429 
430  uno::Reference< linguistic2::XHyphenatedWord > xHyphWord( m_pHyphWrapper ?
431  m_pHyphWrapper->GetLast() : nullptr, uno::UNO_QUERY );
432  DBG_ASSERT( xHyphWord.is(), "hyphenation result missing" );
433  if (xHyphWord.is())
434  {
435  DBG_ASSERT( m_aActWord == xHyphWord->getWord(), "word mismatch" );
436  DBG_ASSERT( m_nActLanguage == LanguageTag( xHyphWord->getLocale() ).getLanguageType(), "language mismatch" );
437  m_nMaxHyphenationPos = xHyphWord->getHyphenationPos();
438  }
439 
441  m_xWordEdit->grab_focus();
442 
443  m_xLeftBtn->connect_clicked( LINK( this, SvxHyphenWordDialog, Left_Impl ) );
444  m_xRightBtn->connect_clicked( LINK( this, SvxHyphenWordDialog, Right_Impl ) );
445  m_xOkBtn->connect_clicked( LINK( this, SvxHyphenWordDialog, CutHdl_Impl ) );
446  m_xContBtn->connect_clicked( LINK( this, SvxHyphenWordDialog, ContinueHdl_Impl ) );
447  m_xDelBtn->connect_clicked( LINK( this, SvxHyphenWordDialog, DeleteHdl_Impl ) );
448  m_xHyphAll->connect_clicked( LINK( this, SvxHyphenWordDialog, HyphenateAllHdl_Impl ) );
449  m_xCloseBtn->connect_clicked( LINK( this, SvxHyphenWordDialog, CancelHdl_Impl ) );
450  m_xWordEdit->connect_focus_in( LINK( this, SvxHyphenWordDialog, GetFocusHdl_Impl ) );
451  m_xWordEdit->connect_cursor_position( LINK( this, SvxHyphenWordDialog, CursorChangeHdl_Impl ) );
452 
453  SetWindowTitle( nLang );
454 
455  // disable controls if service is not available
456  if (!m_xHyphenator.is())
457  m_xDialog->set_sensitive(false);
458 }
459 
461 {
462  if (m_xCloseBtn->get_sensitive())
464 }
465 
467 {
468  m_xDialog->set_title(m_aLabel + " (" + SvtLanguageTable::GetLanguageString(nLang) + ")");
469 }
470 
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString m_aActWord
Definition: hyphen.hxx:39
LanguageType m_nActLanguage
Definition: hyphen.hxx:40
std::unique_ptr< weld::Button > m_xCloseBtn
Definition: hyphen.hxx:54
void InitControls_Impl()
Definition: hyphen.cxx:180
RET_CANCEL
std::unique_ptr< weld::Button > m_xLeftBtn
Definition: hyphen.hxx:48
static css::uno::Reference< css::linguistic2::XLinguProperties > GetLinguPropertySet()
sal_uInt16 sal_Unicode
void select_region(int nStart, int nEnd)
Definition: hyphen.cxx:386
void ContinueHyph_Impl(sal_Int32 nInsPos=-1)
Definition: hyphen.cxx:198
virtual void SpellEnd()
void SetWindowTitle(LanguageType nLang)
Definition: hyphen.cxx:466
#define HYPH_POS_CHAR
Definition: hyphen.cxx:31
#define DBG_ASSERT(sCon, aError)
#define CUR_HYPH_POS_CHAR
Definition: hyphen.cxx:33
std::unique_ptr< weld::Button > m_xHyphAll
Definition: hyphen.hxx:53
int i
std::unique_ptr< weld::Button > m_xContBtn
Definition: hyphen.hxx:51
sal_Int16 m_nMaxHyphenationPos
Definition: hyphen.hxx:41
OUString m_aLabel
Definition: hyphen.hxx:34
IMPL_LINK_NOARG(SvxHyphenWordDialog, CursorChangeHdl_Impl, weld::Entry &, void)
Definition: hyphen.cxx:37
SvxSpellWrapper *const m_pHyphWrapper
Definition: hyphen.hxx:35
std::unique_ptr< weld::Entry > m_xWordEdit
Definition: hyphen.hxx:47
RET_OK
void EnableLRBtn_Impl()
Definition: hyphen.cxx:52
std::unique_ptr< weld::Button > m_xDelBtn
Definition: hyphen.hxx:52
static OUString GetLanguageString(const LanguageType eType)
const css::uno::Reference< css::uno::XInterface > & GetLast()
virtual ~SvxHyphenWordDialog() override
Definition: hyphen.cxx:460
#define SAL_WARN(area, stream)
OUString EraseUnusableHyphens_Impl()
Definition: hyphen.cxx:80
css::uno::Reference< css::linguistic2::XHyphenator > m_xHyphenator
Definition: hyphen.hxx:36
sal_Int32 nPos
std::unique_ptr< weld::Button > m_xRightBtn
Definition: hyphen.hxx:49
SvxHyphenWordDialog(const OUString &rWord, LanguageType nLang, weld::Window *pParent, css::uno::Reference< css::linguistic2::XHyphenator > const &xHyphen, SvxSpellWrapper *pWrapper)
Definition: hyphen.cxx:404
std::unique_ptr< weld::Button > m_xOkBtn
Definition: hyphen.hxx:50
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)