summaryrefslogtreecommitdiff
path: root/cui/source/dialogs/hyphen.cxx
blob: c7f02a1f51d7c41c8ae973e6f0fa08b869dc02b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "hyphen.hxx"
#include "dialmgr.hxx"

#include <editeng/splwrap.hxx>
#include <editeng/svxenum.hxx>
#include <editeng/unolingu.hxx>
#include <svtools/langtab.hxx>
#include <svx/dialmgr.hxx>
#include <svx/dlgutil.hxx>
#include <vcl/msgbox.hxx>
#include <vcl/builderfactory.hxx>

#define HYPH_POS_CHAR       '='

#define CUR_HYPH_POS_CHAR   '-'

using namespace css;

HyphenEdit::HyphenEdit(vcl::Window* pParent)
    : Edit(pParent, WB_LEFT|WB_VCENTER|WB_BORDER|WB_3DLOOK|WB_TABSTOP)
{
}

VCL_BUILDER_FACTORY(HyphenEdit)

void HyphenEdit::KeyInput( const KeyEvent& rKEvt )
{
    sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();

    switch ( nCode )
    {
        case KEY_LEFT:
            static_cast<SvxHyphenWordDialog*>( GetParentDialog() )->SelLeft();
            break;

        case KEY_RIGHT:
            static_cast<SvxHyphenWordDialog*>( GetParentDialog() )->SelRight();
            break;

        case KEY_TAB:
        case KEY_ESCAPE:
        case KEY_RETURN:
            Edit::KeyInput(rKEvt);
            break;
        default:
            Control::KeyInput( rKEvt ); // pass on to the dialog
            break;
    }
}


void SvxHyphenWordDialog::EnableLRBtn_Impl()
{
    const sal_Int32 nLen = m_aEditWord.getLength();

    m_pRightBtn->Disable();
    for ( sal_Int32 i = m_nOldPos + 2; i < nLen; ++i )
    {
        if ( m_aEditWord[ i ] == sal_Unicode( HYPH_POS_CHAR ) )
        {
            m_pRightBtn->Enable();
            break;
        }
    }

    DBG_ASSERT(m_nOldPos < nLen, "nOldPos out of range");
    if (m_nOldPos >= nLen)
        m_nOldPos = nLen - 1;
    m_pLeftBtn->Disable();
    for ( sal_Int32 i = m_nOldPos;  i-- > 0; )
    {
        if ( m_aEditWord[ i ] == sal_Unicode( HYPH_POS_CHAR ) )
        {
            m_pLeftBtn->Enable();
            break;
        }
    }
}


OUString SvxHyphenWordDialog::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 the 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 be 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 hyphenation point because of 1). 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 hyphenation
    // 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.

    OUString aTxt;
    DBG_ASSERT(m_xPossHyph.is(), "missing possible hyphens");
    if (m_xPossHyph.is())
    {
        DBG_ASSERT( m_aActWord == m_xPossHyph->getWord(), "word mismatch"  );

        aTxt = m_xPossHyph->getPossibleHyphens();

        m_nHyphenationPositionsOffset = 0;
        uno::Sequence< sal_Int16 > aHyphenationPositions(
                m_xPossHyph->getHyphenationPositions() );
        sal_Int32 nLen = aHyphenationPositions.getLength();
        const sal_Int16 *pHyphenationPos = aHyphenationPositions.getConstArray();

        // find position nIdx after which all hyphen positions are unusable
        sal_Int32  nIdx = -1;
        sal_Int32  nPos = 0, nPos1 = 0;
        if (nLen)
        {
            sal_Int32 nStart = 0;
            for (sal_Int32 i = 0;  i < nLen;  ++i)
            {
                if (pHyphenationPos[i] > m_nMaxHyphenationPos)
                    break;
                else
                {
                    // find corresponding hyphen positions in string
                    nPos = aTxt.indexOf( sal_Unicode( HYPH_POS_CHAR ), nStart );

                    if (nPos == -1)
                        break;
                    else
                    {
                        nIdx = nPos;
                        nStart = nPos + 1;
                    }
                }
            }
        }
        DBG_ASSERT(nIdx != -1, "no usable hyphenation position");

        // 1) remove all not usable hyphenation positions from the end of the string
        nPos = nIdx == -1 ? 0 : nIdx + 1;
        nPos1 = nPos;   //save for later use in 2) below
        const OUString aTmp( sal_Unicode( HYPH_POS_CHAR ) );
        while (nPos != -1)
        {
            nPos++;
            aTxt = aTxt.replaceFirst( aTmp, "", &nPos);
        }

        // 2) remove all hyphenation positions from the start that are not considered by the core
        const OUString aSearchRange( aTxt.copy( 0, nPos1 ) );
        sal_Int32 nPos2 = aSearchRange.lastIndexOf( '-' );  // the '-' position the core will use by default
        if (nPos2 != -1 )
        {
            OUString aLeft( aSearchRange.copy( 0, nPos2 ) );
            nPos = 0;
            while (nPos != -1)
            {
                nPos++;
                aLeft = aLeft.replaceFirst( aTmp, "", &nPos );
                if (nPos != -1)
                    ++m_nHyphenationPositionsOffset;
            }
            aTxt = aTxt.replaceAt( 0, nPos2, aLeft );
        }
    }
    return aTxt;
}


void SvxHyphenWordDialog::InitControls_Impl()
{
    m_xPossHyph = nullptr;
    if (m_xHyphenator.is())
    {
        lang::Locale aLocale( LanguageTag::convertToLocale(m_nActLanguage) );
        m_xPossHyph = m_xHyphenator->createPossibleHyphens( m_aActWord, aLocale,
                                                        uno::Sequence< beans::PropertyValue >() );
        if (m_xPossHyph.is())
            m_aEditWord = EraseUnusableHyphens_Impl();
    }
    m_pWordEdit->SetText( m_aEditWord );

    m_nOldPos = m_aEditWord.getLength();
    SelLeft();
    EnableLRBtn_Impl();
}


void SvxHyphenWordDialog::ContinueHyph_Impl( sal_Int32 nInsPos )
{
    if ( nInsPos >= 0 && m_xPossHyph.is() )
    {
        if (nInsPos)
        {
            DBG_ASSERT(nInsPos <= m_aEditWord.getLength() - 2, "wrong hyphen position");

            sal_Int32 nIdxPos = -1;
            for (sal_Int32 i = 0; i <= nInsPos; ++i)
            {
                if (HYPH_POS_CHAR == m_aEditWord[ i ])
                    nIdxPos++;
            }
            // take the possible hyphenation positions that got removed from the
            // start of the word into account:
            nIdxPos += m_nHyphenationPositionsOffset;

            uno::Sequence< sal_Int16 > aSeq = m_xPossHyph->getHyphenationPositions();
            sal_Int32 nLen = aSeq.getLength();
            DBG_ASSERT(nLen, "empty sequence");
            DBG_ASSERT(0 <= nIdxPos && nIdxPos < nLen, "index out of range");
            if (nLen && 0 <= nIdxPos && nIdxPos < nLen)
            {
                nInsPos = aSeq.getConstArray()[ nIdxPos ];
                m_pHyphWrapper->InsertHyphen( nInsPos );
            }
        }
        else
        {
            //! calling with 0 as argument will remove hyphens!
            m_pHyphWrapper->InsertHyphen( nInsPos );
        }
    }

    if ( m_pHyphWrapper->FindSpellError() )
    {
        uno::Reference< linguistic2::XHyphenatedWord >  xHyphWord( m_pHyphWrapper->GetLast(), uno::UNO_QUERY );

        // adapt actual word and language to new found hyphenation result
        if(xHyphWord.is())
        {
            m_aActWord = xHyphWord->getWord();
            m_nActLanguage = LanguageTag( xHyphWord->getLocale() ).getLanguageType();
            m_nMaxHyphenationPos = xHyphWord->getHyphenationPos();
            InitControls_Impl();
            SetWindowTitle( m_nActLanguage );
        }
    }
    else
    {
        m_pCloseBtn->Disable();
        EndDialog(RET_OK);
    }
}


sal_uInt16 SvxHyphenWordDialog::GetHyphIndex_Impl()
{
    sal_uInt16 nPos = 0;
    const OUString aTxt( m_pWordEdit->GetText() );

    for ( sal_Int32 i=0; i < aTxt.getLength(); ++i )
    {
        sal_Unicode cChar = aTxt[ i ];
        if ( cChar == CUR_HYPH_POS_CHAR )
            break;
        if ( cChar != HYPH_POS_CHAR )
            nPos++;
    }
    return nPos;
}


void SvxHyphenWordDialog::SelLeft()
{
    DBG_ASSERT( m_nOldPos > 0, "invalid hyphenation position" );
    if (m_nOldPos > 0)
    {
        OUString aTxt( m_aEditWord );
        for( sal_Int32 i = m_nOldPos - 1;  i > 0; --i )
        {
            DBG_ASSERT(i <= aTxt.getLength(), "index out of range");
            if (aTxt[ i ] == sal_Unicode( HYPH_POS_CHAR ))
            {
                aTxt = aTxt.replaceAt( i, 1, OUString( CUR_HYPH_POS_CHAR ) );

                m_nOldPos = i;
                m_pWordEdit->SetText( aTxt );
                m_pWordEdit->GrabFocus();
                m_pWordEdit->SetSelection( Selection( i, i + 1 ) );
                break;
            }
        }
        EnableLRBtn_Impl();
    }
}


void SvxHyphenWordDialog::SelRight()
{
    OUString aTxt( m_aEditWord );
    for ( sal_Int32 i = m_nOldPos + 1;  i < aTxt.getLength();  ++i )
    {
        if (aTxt[ i ] == sal_Unicode( HYPH_POS_CHAR ))
        {
            aTxt = aTxt.replaceAt( i, 1, OUString( CUR_HYPH_POS_CHAR ) );

            m_nOldPos = i;
            m_pWordEdit->SetText( aTxt );
            m_pWordEdit->GrabFocus();
            m_pWordEdit->SetSelection( Selection( i, i + 1 ) );
            break;
        }
    }
    EnableLRBtn_Impl();
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, CutHdl_Impl, Button*, void)
{
    if( !m_bBusy )
    {
        m_bBusy = true;
        ContinueHyph_Impl( /*m_nHyphPos*/m_nOldPos );
        m_bBusy = false;
    }
}


IMPL_LINK_NOARG( SvxHyphenWordDialog, HyphenateAllHdl_Impl, Button *, void )
{
    if( !m_bBusy )
    {
        try
        {
            uno::Reference< linguistic2::XLinguProperties >  xProp( LinguMgr::GetLinguPropertySet() );

            xProp->setIsHyphAuto( true );

            m_bBusy = true;
            ContinueHyph_Impl( /*m_nHyphPos*/m_nOldPos );
            m_bBusy = false;

            xProp->setIsHyphAuto( false );
        }
        catch (uno::Exception &)
        {
            SAL_WARN( "cui.dialogs", "Hyphenate All failed" );
        }
    }
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, DeleteHdl_Impl, Button*, void)
{
    if( !m_bBusy )
    {
        m_bBusy = true;
        ContinueHyph_Impl( 0 );
        m_bBusy = false;
    }
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, ContinueHdl_Impl, Button*, void)
{
    if( !m_bBusy )
    {
        m_bBusy = true;
        ContinueHyph_Impl();
        m_bBusy = false;
    }
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, CancelHdl_Impl, Button*, void)
{
    if( !m_bBusy )
    {
        m_bBusy = true;
        EndDialog();
        m_bBusy = false;
    }
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, Left_Impl, Button*, void)
{
    if( !m_bBusy )
    {
        m_bBusy = true;
        SelLeft();
        m_bBusy = false;
    }
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, Right_Impl, Button*, void)
{
    if( !m_bBusy )
    {
        m_bBusy = true;
        SelRight();
        m_bBusy = false;
    }
}


IMPL_LINK_NOARG(SvxHyphenWordDialog, GetFocusHdl_Impl, Control&, void)
{
    m_pWordEdit->SetSelection( Selection( m_nOldPos, m_nOldPos + 1 ) );
}


// class SvxHyphenWordDialog ---------------------------------------------

SvxHyphenWordDialog::SvxHyphenWordDialog(
    const OUString &rWord, LanguageType nLang,
    vcl::Window* pParent,
    uno::Reference< linguistic2::XHyphenator >  &xHyphen,
    SvxSpellWrapper* pWrapper)
    : SfxModalDialog(pParent, "HyphenateDialog", "cui/ui/hyphenate.ui")
    , m_pHyphWrapper(pWrapper)
    , m_xHyphenator(nullptr)
    , m_xPossHyph(nullptr)
    , m_aActWord(rWord)
    , m_nActLanguage(nLang)
    , m_nMaxHyphenationPos(0)
    , m_nOldPos(0)
    , m_nHyphenationPositionsOffset(0)
    , m_bBusy(false)
{
    get(m_pWordEdit, "worded");
    get(m_pLeftBtn, "left");
    get(m_pRightBtn, "right");
    get(m_pOkBtn, "ok");
    get(m_pContBtn, "continue");
    get(m_pDelBtn, "delete");
    get(m_pHyphAll, "hyphall");
    get(m_pCloseBtn, "close");

    m_aLabel = GetText();
    m_xHyphenator = xHyphen;

    uno::Reference< linguistic2::XHyphenatedWord >  xHyphWord( m_pHyphWrapper ?
            m_pHyphWrapper->GetLast() : nullptr, uno::UNO_QUERY );
    DBG_ASSERT( xHyphWord.is(), "hyphenation result missing" );
    if (xHyphWord.is())
    {
        DBG_ASSERT( m_aActWord == xHyphWord->getWord(), "word mismatch" );
        DBG_ASSERT( m_nActLanguage == LanguageTag( xHyphWord->getLocale() ).getLanguageType(), "language mismatch" );
        m_nMaxHyphenationPos = xHyphWord->getHyphenationPos();
    }

    InitControls_Impl();
    m_pWordEdit->GrabFocus();

    m_pLeftBtn->SetClickHdl( LINK( this, SvxHyphenWordDialog, Left_Impl ) );
    m_pRightBtn->SetClickHdl( LINK( this, SvxHyphenWordDialog, Right_Impl ) );
    m_pOkBtn->SetClickHdl( LINK( this, SvxHyphenWordDialog, CutHdl_Impl ) );
    m_pContBtn->SetClickHdl( LINK( this, SvxHyphenWordDialog, ContinueHdl_Impl ) );
    m_pDelBtn->SetClickHdl( LINK( this, SvxHyphenWordDialog, DeleteHdl_Impl ) );
    m_pHyphAll->SetClickHdl( LINK( this, SvxHyphenWordDialog, HyphenateAllHdl_Impl ) );
    m_pCloseBtn->SetClickHdl( LINK( this, SvxHyphenWordDialog, CancelHdl_Impl ) );
    m_pWordEdit->SetGetFocusHdl( LINK( this, SvxHyphenWordDialog, GetFocusHdl_Impl ) );

    SetWindowTitle( nLang );

    // disable controls if service is not available
    if (!m_xHyphenator.is())
        Enable( false );
}

SvxHyphenWordDialog::~SvxHyphenWordDialog()
{
    disposeOnce();
}

void SvxHyphenWordDialog::dispose()
{
    if (m_pCloseBtn->IsEnabled())
        m_pHyphWrapper->SpellEnd();
    m_pWordEdit.clear();
    m_pLeftBtn.clear();
    m_pRightBtn.clear();
    m_pOkBtn.clear();
    m_pContBtn.clear();
    m_pDelBtn.clear();
    m_pHyphAll.clear();
    m_pCloseBtn.clear();
    SfxModalDialog::dispose();
}

void SvxHyphenWordDialog::SetWindowTitle( LanguageType nLang )
{
    SetText( m_aLabel + " (" + SvtLanguageTable::GetLanguageString( nLang ) + ")" );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */