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