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
520
521
522
523
524
525
526
527
528
529
530
531
|
/* -*- 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 "cuires.hrc"
#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 <tools/shl.hxx>
#include <vcl/msgbox.hxx>
#define HYPH_POS_CHAR '='
#define CONTINUE_HYPH USHRT_MAX
#define CUR_HYPH_POS_CHAR '-'
HyphenEdit::HyphenEdit(Window* pParent)
: Edit(pParent, WB_LEFT|WB_VCENTER|WB_BORDER|WB_3DLOOK|WB_TABSTOP)
{
}
extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeHyphenEdit(Window *pParent, VclBuilder::stringmap &)
{
return new HyphenEdit(pParent);
}
void HyphenEdit::KeyInput( const KeyEvent& rKEvt )
{
sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
switch ( nCode )
{
case KEY_LEFT:
( (SvxHyphenWordDialog*)GetParentDialog() )->SelLeft();
break;
case KEY_RIGHT:
( (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()
{
String aTxt( aEditWord );
xub_StrLen nLen = aTxt.Len();
xub_StrLen i;
m_pRightBtn->Disable();
for ( i = nOldPos + 2; i < nLen; ++i )
{
if ( aTxt.GetChar( i ) == sal_Unicode( HYPH_POS_CHAR ) )
{
m_pRightBtn->Enable();
break;
}
}
DBG_ASSERT(nOldPos < aTxt.Len(), "nOldPos out of range");
if (nOldPos >= aTxt.Len())
nOldPos = aTxt.Len() - 1;
m_pLeftBtn->Disable();
for ( i = nOldPos; i-- > 0; )
{
if ( aTxt.GetChar( i ) == sal_Unicode( HYPH_POS_CHAR ) )
{
m_pLeftBtn->Enable();
break;
}
}
}
String SvxHyphenWordDialog::EraseUnusableHyphens_Impl(
uno::Reference< linguistic2::XPossibleHyphens > &rxPossHyph,
sal_uInt16 _nMaxHyphenationPos )
{
// 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 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 == String( 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;
xub_StrLen nPos = 0, nPos1 = 0, nPos2 = 0;
if (nLen)
{
xub_StrLen nStart = 0;
for (sal_Int32 i = 0; i < nLen; ++i)
{
if (pHyphenationPos[i] > _nMaxHyphenationPos)
break;
else
{
// find corresponding hyphen pos in string
nPos = aTxt.Search( sal_Unicode( HYPH_POS_CHAR ), nStart );
if (nStart == STRING_NOTFOUND)
break;
else
{
nIdx = nPos;
nStart = nPos + 1;
}
}
}
}
DBG_ASSERT(nIdx != STRING_NOTFOUND, "no usable hyphenation position");
// 1) remove all not usable hyphenation positions from the end of the string
nPos = nIdx == STRING_NOTFOUND ? 0 : nIdx + 1;
nPos1 = nPos; //save for later use in 2) below
const OUString aTmp( sal_Unicode( HYPH_POS_CHAR ) );
const OUString 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;
}
void SvxHyphenWordDialog::InitControls_Impl()
{
xPossHyph = NULL;
if (xHyphenator.is())
{
lang::Locale aLocale( LanguageTag(nActLanguage).getLocale() );
xPossHyph = xHyphenator->createPossibleHyphens( aActWord, aLocale,
uno::Sequence< beans::PropertyValue >() );
if (xPossHyph.is())
aEditWord = EraseUnusableHyphens_Impl( xPossHyph, nMaxHyphenationPos );
}
m_pWordEdit->SetText( aEditWord );
nOldPos = aEditWord.Len();
SelLeft();
EnableLRBtn_Impl();
}
void SvxHyphenWordDialog::ContinueHyph_Impl( sal_uInt16 nInsPos )
{
if ( nInsPos != CONTINUE_HYPH && xPossHyph.is())
{
if (nInsPos)
{
String aTmp( aEditWord );
DBG_ASSERT(nInsPos <= aTmp.Len() - 2, "wrong hyphen position");
sal_Int16 nIdxPos = -1;
for (sal_uInt16 i = 0; i <= nInsPos; ++i)
{
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(0 <= nIdxPos && nIdxPos < nLen, "index out of range");
if (nLen && 0 <= nIdxPos && nIdxPos < nLen)
{
nInsPos = aSeq.getConstArray()[ nIdxPos ];
pHyphWrapper->InsertHyphen( nInsPos );
}
}
else
{
//! calling with 0 as argument will remove hyphens!
pHyphWrapper->InsertHyphen( nInsPos );
}
}
if ( pHyphWrapper->FindSpellError() )
{
uno::Reference< linguistic2::XHyphenatedWord > xHyphWord( pHyphWrapper->GetLast(), uno::UNO_QUERY );
// adapt actual word and language to new found hyphenation result
if(xHyphWord.is())
{
aActWord = String( xHyphWord->getWord() );
nActLanguage = LanguageTag( xHyphWord->getLocale() ).getLanguageType();
nMaxHyphenationPos = xHyphWord->getHyphenationPos();
InitControls_Impl();
SetWindowTitle( nActLanguage );
}
}
else
EndDialog( RET_OK );
}
sal_uInt16 SvxHyphenWordDialog::GetHyphIndex_Impl()
{
sal_uInt16 nPos = 0;
String aTxt( m_pWordEdit->GetText() );
for ( sal_uInt16 i=0 ; i < aTxt.Len(); ++i )
{
sal_Unicode cChar = aTxt.GetChar( i );
if ( cChar == CUR_HYPH_POS_CHAR )
break;
if ( cChar != HYPH_POS_CHAR )
nPos++;
}
return nPos;
}
void SvxHyphenWordDialog::SelLeft()
{
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;
m_pWordEdit->SetText( aTxt );
m_pWordEdit->GrabFocus();
m_pWordEdit->SetSelection( Selection( i, i + 1 ) );
break;
}
}
nHyphPos = GetHyphIndex_Impl();
EnableLRBtn_Impl();
}
}
void SvxHyphenWordDialog::SelRight()
{
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;
m_pWordEdit->SetText( aTxt );
m_pWordEdit->GrabFocus();
m_pWordEdit->SetSelection( Selection( i, i + 1 ) );
break;
}
}
nHyphPos = GetHyphIndex_Impl();
EnableLRBtn_Impl();
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, CutHdl_Impl)
{
if( !bBusy )
{
bBusy = sal_True;
ContinueHyph_Impl( /*nHyphPos*/nOldPos );
bBusy = sal_False;
}
return 0;
}
IMPL_LINK( SvxHyphenWordDialog, HyphenateAllHdl_Impl, Button *, EMPTYARG /*pButton*/ )
{
if( !bBusy )
{
try
{
uno::Reference< beans::XPropertySet > xProp( SvxGetLinguPropertySet() );
const OUString aName( "IsHyphAuto" );
uno::Any aAny;
aAny <<= sal_True;
xProp->setPropertyValue( aName, aAny );
bBusy = sal_True;
ContinueHyph_Impl( /*nHyphPos*/nOldPos );
bBusy = sal_False;
aAny <<= sal_False;
xProp->setPropertyValue( aName, aAny );
}
catch (uno::Exception &e)
{
(void) e;
DBG_ASSERT( 0, "Hyphenate All failed" );
}
}
return 0;
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, DeleteHdl_Impl)
{
if( !bBusy )
{
bBusy = sal_True;
ContinueHyph_Impl();
bBusy = sal_False;
}
return 0;
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, ContinueHdl_Impl)
{
if( !bBusy )
{
bBusy = sal_True;
ContinueHyph_Impl( CONTINUE_HYPH );
bBusy = sal_False;
}
return 0;
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, CancelHdl_Impl)
{
if( !bBusy )
{
bBusy = sal_True;
pHyphWrapper->SpellEnd();
EndDialog( RET_CANCEL );
bBusy = sal_False;
}
return 0;
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, Left_Impl)
{
if( !bBusy )
{
bBusy = sal_True;
SelLeft();
bBusy = sal_False;
}
return 0;
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, Right_Impl)
{
if( !bBusy )
{
bBusy = sal_True;
SelRight();
bBusy = sal_False;
}
return 0;
}
IMPL_LINK_NOARG(SvxHyphenWordDialog, GetFocusHdl_Impl)
{
m_pWordEdit->SetSelection( Selection( nOldPos, nOldPos + 1 ) );
return 0;
}
// class SvxHyphenWordDialog ---------------------------------------------
SvxHyphenWordDialog::SvxHyphenWordDialog(
const String &rWord, LanguageType nLang,
Window* pParent,
uno::Reference< linguistic2::XHyphenator > &xHyphen,
SvxSpellWrapper* pWrapper)
: SfxModalDialog(pParent, "HyphenateDialog", "cui/ui/hyphenate.ui")
, pHyphWrapper(NULL)
, xHyphenator(NULL)
, xPossHyph(NULL)
, nActLanguage(LANGUAGE_NONE)
, nMaxHyphenationPos(0)
, nHyphPos(0)
, nOldPos(0)
, nHyphenationPositionsOffset(0)
, bBusy(sal_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");
aLabel = GetText();
aActWord = rWord;
nActLanguage = nLang;
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 == LanguageTag( xHyphWord->getLocale() ).getLanguageType(), "language mismatch" );
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 (!xHyphenator.is())
Enable( sal_False );
}
SvxHyphenWordDialog::~SvxHyphenWordDialog()
{
}
void SvxHyphenWordDialog::SetWindowTitle( LanguageType nLang )
{
String aLangStr( SvtLanguageTable::GetLanguageString( nLang ) );
OUString aTmp( aLabel );
aTmp += " (";
aTmp += aLangStr;
aTmp += ")";
SetText( aTmp );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|