summaryrefslogtreecommitdiff
path: root/cui/source/options/optdict.cxx
blob: f850929a21ffc7e20e3842a9eb3cecd53b801116 (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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// include ---------------------------------------------------------------
#include <tools/shl.hxx>
#include <editeng/unolingu.hxx>
#include <svx/dlgutil.hxx>
#include <sfx2/sfxuno.hxx>
#include <svl/eitem.hxx>
#include <com/sun/star/frame/XStorable.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <unotools/intlwrapper.hxx>
#include <vcl/svapp.hxx>
#include <vcl/msgbox.hxx>
#include <svx/dialogs.hrc>

#define _SVX_OPTDICT_CXX

#include <linguistic/misc.hxx>
#include <cuires.hrc>
#include "optdict.hrc"
#include "optdict.hxx"
#include <dialmgr.hxx>
#include <svx/svxerr.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::linguistic2;

// static ----------------------------------------------------------------

static const short  NOACTDICT   = -1;

static long nStaticTabs[]=
{
    2,10,71,120
};

// static function -------------------------------------------------------

static String getNormDicEntry_Impl(const rtl::OUString &rText)
{
    rtl::OUString aTmp(comphelper::string::stripEnd(rText, '.'));
    return comphelper::string::remove(aTmp, '=');
}

// Compare Dictionary Entry  result
enum CDE_RESULT { CDE_EQUAL, CDE_SIMILAR, CDE_DIFFERENT };

static CDE_RESULT cmpDicEntry_Impl( const String &rText1, const String &rText2 )
{
    CDE_RESULT eRes = CDE_DIFFERENT;

    if (rText1 == rText2)
        eRes = CDE_EQUAL;
    else
    {   // similar = equal up to trailing '.' and hyphenation positions
        // marked with '='
        if (getNormDicEntry_Impl( rText1 ) == getNormDicEntry_Impl( rText2 ))
            eRes = CDE_SIMILAR;
    }

    return eRes;
}

// class SvxNewDictionaryDialog -------------------------------------------

SvxNewDictionaryDialog::SvxNewDictionaryDialog( Window* pParent,
        Reference< XSpellChecker1 >  &xSpl ) :

    ModalDialog( pParent, CUI_RES( RID_SFXDLG_NEWDICT ) ),

    aNewDictBox     ( this, CUI_RES( GB_NEWDICT ) ),
    aNameText       ( this, CUI_RES( FT_DICTNAME ) ),
    aNameEdit       ( this, CUI_RES( ED_DICTNAME ) ),
    aLanguageText   ( this, CUI_RES( FT_DICTLANG ) ),
    aLanguageLB     ( this, CUI_RES( LB_DICTLANG ) ),
    aExceptBtn      ( this, CUI_RES( BTN_EXCEPT ) ),
    aOKBtn          ( this, CUI_RES( BTN_NEWDICT_OK ) ),
    aCancelBtn      ( this, CUI_RES( BTN_NEWDICT_ESC ) ),
    aHelpBtn        ( this, CUI_RES( BTN_NEWDICT_HLP ) ),
    xSpell( xSpl )
{
    // install handler
    aNameEdit.SetModifyHdl(
        LINK( this, SvxNewDictionaryDialog, ModifyHdl_Impl ) );
    aOKBtn.SetClickHdl( LINK( this, SvxNewDictionaryDialog, OKHdl_Impl ) );

    // display languages
    aLanguageLB.SetLanguageList( LANG_LIST_ALL, sal_True, sal_True );
    aLanguageLB.SelectEntryPos(0);

    aNameText.SetAccessibleRelationMemberOf( &aNewDictBox );
    aNameEdit.SetAccessibleRelationMemberOf( &aNewDictBox );
    aLanguageText.SetAccessibleRelationMemberOf( &aNewDictBox );
    aLanguageLB.SetAccessibleRelationMemberOf( &aNewDictBox );

    FreeResource();
}

// -----------------------------------------------------------------------

IMPL_LINK_NOARG(SvxNewDictionaryDialog, OKHdl_Impl)
{
    String sDict = aNameEdit.GetText();
    sDict.EraseTrailingChars();
    // add extension for personal dictionaries
    sDict.AppendAscii(".dic");

    Reference< XDictionaryList >  xDicList( SvxGetDictionaryList() );

    Sequence< Reference< XDictionary >  > aDics;
    if (xDicList.is())
        aDics = xDicList->getDictionaries();
    const Reference< XDictionary >  *pDic = aDics.getConstArray();
    sal_Int32 nCount = (sal_uInt16) aDics.getLength();

    sal_Bool bFound = sal_False;
    sal_uInt16 i;
    for (i = 0; !bFound && i < nCount; ++i )
        if ( sDict.EqualsIgnoreCaseAscii( String(pDic[i]->getName()) ))
            bFound = sal_True;

    if ( bFound )
    {
        // duplicate names?
        InfoBox( this, CUI_RESSTR( RID_SVXSTR_OPT_DOUBLE_DICTS ) ).Execute();
        aNameEdit.GrabFocus();
        return 0;
    }

    // create and add
    sal_uInt16 nLang = aLanguageLB.GetSelectLanguage();
    try
    {
        // create new dictionary
        DictionaryType eType = aExceptBtn.IsChecked() ?
                DictionaryType_NEGATIVE : DictionaryType_POSITIVE;
        if (xDicList.is())
        {
            lang::Locale aLocale( SvxCreateLocale(nLang) );
            String aURL( linguistic::GetWritableDictionaryURL( sDict ) );
            xNewDic = Reference< XDictionary > (
                    xDicList->createDictionary( sDict, aLocale, eType, aURL ) , UNO_QUERY );
            xNewDic->setActive( sal_True );
        }
        DBG_ASSERT(xNewDic.is(), "NULL pointer");
    }
    catch(...)
    {
        xNewDic = NULL;

        // error: couldn't create new dictionary
        SfxErrorContext aContext( ERRCTX_SVX_LINGU_DICTIONARY, String(),
            this, RID_SVXERRCTX, &CUI_MGR() );
        ErrorHandler::HandleError( *new StringErrorInfo(
                ERRCODE_SVX_LINGU_DICT_NOTWRITEABLE, sDict ) );

        EndDialog( RET_CANCEL );
    }

    if (xDicList.is() && xNewDic.is())
    {
        xDicList->addDictionary( Reference< XDictionary > ( xNewDic, UNO_QUERY ) );

        // refresh list of dictionaries
        //! dictionaries may have been added/removed elsewhere too.
        aDics = xDicList->getDictionaries();
    }
    pDic = aDics.getConstArray();
    nCount = (sal_uInt16) aDics.getLength();


    EndDialog( RET_OK );
    return 0;
}

// -----------------------------------------------------------------------

IMPL_LINK_NOARG_INLINE_START(SvxNewDictionaryDialog, ModifyHdl_Impl)
{
    if ( aNameEdit.GetText().Len() )
        aOKBtn.Enable();
    else
        aOKBtn.Disable();
    return 0;
}
IMPL_LINK_NOARG_INLINE_END(SvxNewDictionaryDialog, ModifyHdl_Impl)

//==========================================================================
//
// class SvxEditDictionaryDialog -------------------------------------------
//
//==========================================================================

SvxEditDictionaryDialog::SvxEditDictionaryDialog(
            Window* pParent,
            const String& rName,
            Reference< XSpellChecker1 >  &xSpl ) :

    ModalDialog( pParent, CUI_RES( RID_SFXDLG_EDITDICT ) ),

    aBookFT         ( this, CUI_RES( FT_BOOK ) ),
    aAllDictsLB     ( this, CUI_RES( LB_ALLDICTS ) ),
    aLangFT         ( this, CUI_RES( FT_DICTLANG ) ),
    aLangLB         ( this, CUI_RES( LB_DICTLANG ) ),

    aWordFT         ( this, CUI_RES( FT_WORD ) ),
    aWordED         ( this, CUI_RES( ED_WORD ) ),
    aReplaceFT      ( this, CUI_RES( FT_REPLACE ) ),
    aReplaceED      ( this, CUI_RES( ED_REPLACE ) ),
    aWordsLB        ( this, CUI_RES( TLB_REPLACE ) ),
    aNewReplacePB   ( this, CUI_RES( PB_NEW_REPLACE ) ),
    aDeletePB       ( this, CUI_RES( PB_DELETE_REPLACE ) ),
    aEditDictsBox   ( this, CUI_RES( GB_EDITDICTS ) ),
    aHelpBtn        ( this, CUI_RES( BTN_EDITHELP ) ),
    aCloseBtn       ( this, CUI_RES( BTN_EDITCLOSE ) ),
    sModify         (CUI_RES(STR_MODIFY)),
    sNew            (aNewReplacePB.GetText()),
    aDecoView       ( this),
    xSpell          ( xSpl ),
    nOld            ( NOACTDICT ),
    bFirstSelect    (sal_True),
    bDoNothing      (sal_False)

{
    if (SvxGetDictionaryList().is())
        aDics = SvxGetDictionaryList()->getDictionaries();

    aWordsLB.SetSelectHdl(LINK(this, SvxEditDictionaryDialog, SelectHdl));
    aWordsLB.SetTabs(nStaticTabs);

    //! we use an algorithm of our own to insert elements sorted
    aWordsLB.SetStyle(aWordsLB.GetStyle()|/*WB_SORT|*/WB_HSCROLL|WB_CLIPCHILDREN);


    nWidth=aWordED.GetSizePixel().Width();
    // install handler
    aNewReplacePB.SetClickHdl(
        LINK( this, SvxEditDictionaryDialog, NewDelHdl));
    aDeletePB.SetClickHdl(
        LINK( this, SvxEditDictionaryDialog, NewDelHdl));

    aLangLB.SetSelectHdl(
        LINK( this, SvxEditDictionaryDialog, SelectLangHdl_Impl ) );
    aAllDictsLB.SetSelectHdl(
        LINK( this, SvxEditDictionaryDialog, SelectBookHdl_Impl ) );

    aWordED.SetModifyHdl(LINK(this, SvxEditDictionaryDialog, ModifyHdl));
    aReplaceED.SetModifyHdl(LINK(this, SvxEditDictionaryDialog, ModifyHdl));
    aWordED.SetActionHdl(LINK(this, SvxEditDictionaryDialog, NewDelHdl));
    aReplaceED.SetActionHdl(LINK(this, SvxEditDictionaryDialog, NewDelHdl));

    // fill listbox with all available WB's
    const Reference< XDictionary >  *pDic = aDics.getConstArray();
    sal_Int32 nCount = aDics.getLength();

    String aLookUpEntry;
    for ( sal_Int32 i = 0; i < nCount; ++i )
    {
        Reference< XDictionary >  xDic( pDic[i], UNO_QUERY );
        if (xDic.is())
        {
            sal_Bool bNegative = xDic->getDictionaryType() == DictionaryType_NEGATIVE ?
                                sal_True : sal_False;
            String aDicName( xDic->getName() );
            const String aTxt( ::GetDicInfoStr( aDicName, SvxLocaleToLanguage( xDic->getLocale() ),
                                                 bNegative ) );
            aAllDictsLB.InsertEntry( aTxt );

            if (rName == aDicName)
                aLookUpEntry = aTxt;
        }
    }

    aLangLB.SetLanguageList( LANG_LIST_ALL, sal_True, sal_True );

    aReplaceED.SetSpaces(sal_True);
    aWordED.SetSpaces(sal_True);

    if ( nCount > 0 )
    {
        aAllDictsLB.SelectEntry( aLookUpEntry );
        sal_uInt16 nPos = aAllDictsLB.GetSelectEntryPos();

        if ( nPos == LISTBOX_ENTRY_NOTFOUND )
        {
            nPos = 0;
            aAllDictsLB.SelectEntryPos( nPos );
        }
        Reference< XDictionary >  xDic;
        if (nPos != LISTBOX_ENTRY_NOTFOUND)
            xDic = Reference< XDictionary > ( aDics.getConstArray()[ nPos ], UNO_QUERY );
        if (xDic.is())
            SetLanguage_Impl( SvxLocaleToLanguage( xDic->getLocale() ) );

        // check if dictionary is read-only
        SetDicReadonly_Impl(xDic);
        sal_Bool bEnable = !IsDicReadonly_Impl();
        aNewReplacePB   .Enable( sal_False );
        aDeletePB       .Enable( sal_False );
        aLangFT.Enable( bEnable );
        aLangLB.Enable( bEnable );
        ShowWords_Impl( nPos );

    }
    else
    {
        aNewReplacePB.Disable();
        aDeletePB    .Disable();
    }
    FreeResource();
}

// -----------------------------------------------------------------------

SvxEditDictionaryDialog::~SvxEditDictionaryDialog()
{
}

// -----------------------------------------------------------------------

void SvxEditDictionaryDialog::Paint( const Rectangle& rRect )
{
    ModalDialog::Paint(rRect );

    Rectangle aRect(aEditDictsBox.GetPosPixel(),aEditDictsBox.GetSizePixel());

    sal_uInt16 nStyle=BUTTON_DRAW_NOFILL;
    aDecoView.DrawButton( aRect, nStyle);
}

// -----------------------------------------------------------------------

void SvxEditDictionaryDialog::SetDicReadonly_Impl(
            Reference< XDictionary >  &xDic )
{
    // enable or disable new and delete button according to file attributes
    bDicIsReadonly = sal_True;
    if (xDic.is())
    {
        Reference< frame::XStorable >  xStor( xDic, UNO_QUERY );
        if (   !xStor.is()              // non persistent dictionary
            || !xStor->hasLocation()    // not yet persistent
            || !xStor->isReadonly() )
        {
            bDicIsReadonly = sal_False;
        }
    }
}

// -----------------------------------------------------------------------

void SvxEditDictionaryDialog::SetLanguage_Impl( util::Language nLanguage )
{
    // select language
    aLangLB.SelectLanguage( nLanguage );
}

sal_uInt16 SvxEditDictionaryDialog::GetLBInsertPos(const String &rDicWord)
{
    sal_uInt16 nPos = USHRT_MAX;

    IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
    const CollatorWrapper* pCollator = aIntlWrapper.getCollator();
    sal_uInt16 j;
    for( j = 0; j < aWordsLB.GetEntryCount(); j++ )
    {
        SvLBoxEntry* pEntry = aWordsLB.GetEntry(j);
        DBG_ASSERT( pEntry, "NULL pointer");
        String aNormEntry( getNormDicEntry_Impl( rDicWord ) );
        StringCompare eCmpRes = (StringCompare)pCollator->
            compareString( aNormEntry, getNormDicEntry_Impl( aWordsLB.GetEntryText(pEntry, 0) ) );
        if( COMPARE_LESS == eCmpRes )
            break;
    }
    if (j < aWordsLB.GetEntryCount())   // entry found?
        nPos = j;

    return nPos;
}

void SvxEditDictionaryDialog::RemoveDictEntry(SvLBoxEntry* pEntry)
{
    sal_uInt16 nLBPos = aAllDictsLB.GetSelectEntryPos();

    if ( pEntry != NULL && nLBPos != LISTBOX_ENTRY_NOTFOUND )
    {
        String sTmpShort(aWordsLB.GetEntryText(pEntry, 0));

        Reference< XDictionary >  xDic = aDics.getConstArray()[ nLBPos ];
        if (xDic->remove( sTmpShort ))  // sal_True on success
        {
            aWordsLB.GetModel()->Remove(pEntry);
        }
    }
}

// -----------------------------------------------------------------------

IMPL_LINK_NOARG(SvxEditDictionaryDialog, SelectBookHdl_Impl)
{
    sal_uInt16 nPos = aAllDictsLB.GetSelectEntryPos();

    if ( nPos != LISTBOX_ENTRY_NOTFOUND )
    {
        aNewReplacePB.Enable( sal_False );
        aDeletePB    .Enable( sal_False );
        // display dictionary
        ShowWords_Impl( nPos );
        // enable or disable new and delete button according to file attributes
        Reference< XDictionary >  xDic( aDics.getConstArray()[ nPos ], UNO_QUERY );
        if (xDic.is())
            SetLanguage_Impl( SvxLocaleToLanguage( xDic->getLocale() ) );

        SetDicReadonly_Impl(xDic);
        sal_Bool bEnable = !IsDicReadonly_Impl();
        aLangFT.Enable( bEnable );
        aLangLB.Enable( bEnable );
    }
    return 0;
}

// -----------------------------------------------------------------------

IMPL_LINK_NOARG(SvxEditDictionaryDialog, SelectLangHdl_Impl)
{
    sal_uInt16 nDicPos = aAllDictsLB.GetSelectEntryPos();
    sal_uInt16 nLang = aLangLB.GetSelectLanguage();
    Reference< XDictionary >  xDic( aDics.getConstArray()[ nDicPos ], UNO_QUERY );
    sal_Int16 nOldLang = SvxLocaleToLanguage( xDic->getLocale() );

    if ( nLang != nOldLang )
    {
        QueryBox aBox( this, CUI_RES( RID_SFXQB_SET_LANGUAGE ) );
        String sTxt( aBox.GetMessText() );
        sTxt.SearchAndReplaceAscii( "%1", aAllDictsLB.GetSelectEntry() );
        aBox.SetMessText( sTxt );

        if ( aBox.Execute() == RET_YES )
        {
            xDic->setLocale( SvxCreateLocale( nLang ) );
            sal_Bool bNegativ = xDic->getDictionaryType() == DictionaryType_NEGATIVE;

            const String sName(
                ::GetDicInfoStr( xDic->getName(),
                                 SvxLocaleToLanguage( xDic->getLocale() ),
                                 bNegativ ) );
            aAllDictsLB.RemoveEntry( nDicPos );
            aAllDictsLB.InsertEntry( sName, nDicPos );
            aAllDictsLB.SelectEntryPos( nDicPos );
        }
        else
            SetLanguage_Impl( nOldLang );
    }
    return 1;
}

// -----------------------------------------------------------------------

void SvxEditDictionaryDialog::ShowWords_Impl( sal_uInt16 nId )
{
    Reference< XDictionary >  xDic = aDics.getConstArray()[ nId ];

    nOld = nId;
    EnterWait();

    String aStr;

    aWordED.SetText(aStr);
    aReplaceED.SetText(aStr);

    if(xDic->getDictionaryType() != DictionaryType_POSITIVE)
    {
        nStaticTabs[0]=2;

        // make controls for replacement text active
        if(!aReplaceFT.IsVisible())
        {
            Size aSize=aWordED.GetSizePixel();
            aSize.Width()=nWidth;
            aWordED.SetSizePixel(aSize);
            aReplaceFT.Show();
            aReplaceED.Show();
        }
    }
    else
    {
        nStaticTabs[0]=1;

        // deactivate controls for replacement text
        if(aReplaceFT.IsVisible())
        {
            Size aSize=aWordED.GetSizePixel();
            aSize.Width()=aWordsLB.GetSizePixel().Width();
            aWordED.SetSizePixel(aSize);
            aReplaceFT.Hide();
            aReplaceED.Hide();
        }

    }

    aWordsLB.SetTabs(nStaticTabs);
    aWordsLB.Clear();

    Sequence< Reference< XDictionaryEntry >  > aEntries( xDic->getEntries() );
    const Reference< XDictionaryEntry >  *pEntry = aEntries.getConstArray();
    sal_Int32 nCount = aEntries.getLength();

    for (sal_Int32 i = 0;  i < nCount;  i++)
    {
        aStr = String(pEntry[i]->getDictionaryWord());
        sal_uInt16 nPos = GetLBInsertPos( aStr );
        if(pEntry[i]->isNegative())
        {
            aStr += '\t';
            aStr += String(pEntry[i]->getReplacementText());
        }
        aWordsLB.InsertEntry(aStr, 0, sal_False, nPos == USHRT_MAX ?  LIST_APPEND : nPos);
    }

    if (aWordsLB.GetEntryCount())
    {
        aWordED   .SetText( aWordsLB.GetEntryText((sal_uLong)0, 0) );
        aReplaceED.SetText( aWordsLB.GetEntryText((sal_uLong)0, 1) );
    }

    LeaveWait();
}

// -----------------------------------------------------------------------

IMPL_LINK(SvxEditDictionaryDialog, SelectHdl, SvTabListBox*, pBox)
{
    if(!bDoNothing)
    {
        if(!bFirstSelect)
        {
            SvLBoxEntry* pEntry = pBox->FirstSelected();
            String sTmpShort(pBox->GetEntryText(pEntry, 0));
            // without this the curser is always at the beginning of a word, if the text
            // is set over the ModifyHdl, although you're editing there at the moment
            if(aWordED.GetText() != sTmpShort)
                aWordED.SetText(sTmpShort);
            aReplaceED.SetText(pBox->GetEntryText(pEntry, 1));
        }
        else
            bFirstSelect = sal_False;

        // entries in the list box should exactly correspond to those from the
        // dictionary. Thus:
        aNewReplacePB.Enable(sal_False);
        aDeletePB    .Enable( sal_True && !IsDicReadonly_Impl() );
    }
    return 0;
};

// -----------------------------------------------------------------------

IMPL_LINK(SvxEditDictionaryDialog, NewDelHdl, PushButton*, pBtn)
{
    SvLBoxEntry* pEntry = aWordsLB.FirstSelected();

    if(pBtn == &aDeletePB)
    {
        DBG_ASSERT(pEntry, "keine Eintrag selektiert");
        String aStr;

        aWordED.SetText(aStr);
        aReplaceED.SetText(aStr);
        aDeletePB.Disable();

        RemoveDictEntry(pEntry);    // remove entry from dic and list-box
    }
    if(pBtn == &aNewReplacePB || aNewReplacePB.IsEnabled())
    {
        SvLBoxEntry* _pEntry = aWordsLB.FirstSelected();
        XubString aNewWord(aWordED.GetText());
        String sEntry(aNewWord);
        XubString aReplaceStr(aReplaceED.GetText());

        sal_Int16 nAddRes = DIC_ERR_UNKNOWN;
        sal_uInt16 nPos = aAllDictsLB.GetSelectEntryPos();
        if ( nPos != LISTBOX_ENTRY_NOTFOUND && aNewWord.Len() > 0)
        {
            DBG_ASSERT(nPos < aDics.getLength(), "invalid dictionary index");
            Reference< XDictionary >  xDic( aDics.getConstArray()[ nPos ], UNO_QUERY );
            if (xDic.is())
            {
                // make changes in dic

                //! ...IsVisible should reflect whether the dictionary is a negativ
                //! or not (hopefully...)
                sal_Bool bIsNegEntry = aReplaceFT.IsVisible();
                ::rtl::OUString aRplcText;
                if(bIsNegEntry)
                    aRplcText = aReplaceStr;

                if (_pEntry) // entry selected in aWordsLB ie action = modify entry
                    xDic->remove( aWordsLB.GetEntryText( _pEntry, 0 ) );
                // if remove has failed the following add should fail too
                // and thus a warning message should be triggered...

                Reference<XDictionary> aXDictionary(xDic, UNO_QUERY);
                nAddRes = linguistic::AddEntryToDic( aXDictionary,
                            aNewWord, bIsNegEntry,
                            aRplcText, SvxLocaleToLanguage( xDic->getLocale() ), sal_False );
             }
        }
        if (DIC_ERR_NONE != nAddRes)
            SvxDicError( this, nAddRes );

        if(DIC_ERR_NONE == nAddRes && sEntry.Len())
        {
            // insert new entry in list-box etc...

            aWordsLB.SetUpdateMode(sal_False);
            sal_uInt16 _nPos = USHRT_MAX;

            if(aReplaceFT.IsVisible())
            {
                sEntry += '\t';
                sEntry += aReplaceStr;
            }

            SvLBoxEntry* pNewEntry = NULL;
            if(_pEntry) // entry selected in aWordsLB ie action = modify entry
            {
                aWordsLB.SetEntryText( sEntry, _pEntry );
                pNewEntry = _pEntry;
            }
            else
            {
                _nPos = GetLBInsertPos( aNewWord );
                SvLBoxEntry* pInsEntry = aWordsLB.InsertEntry(sEntry, 0, sal_False,
                            _nPos == USHRT_MAX ? LIST_APPEND : (sal_uInt32)_nPos);
                pNewEntry = pInsEntry;
            }

            aWordsLB.MakeVisible( pNewEntry );
            aWordsLB.SetUpdateMode(sal_True);
            // if the request came from the ReplaceEdit, give focus to the ShortEdit
            if(aReplaceED.HasFocus())
                aWordED.GrabFocus();
        }
    }
    else
    {
        // this can only be an enter in one of the two edit fields
        // which means EndDialog() - has to be evaluated in KeyInput
        return 0;
    }
    ModifyHdl(&aWordED);
    return 1;
}

// -----------------------------------------------------------------------

IMPL_LINK(SvxEditDictionaryDialog, ModifyHdl, Edit*, pEdt)
{
    SvLBoxEntry* pFirstSel = aWordsLB.FirstSelected();
    String rEntry = pEdt->GetText();

    xub_StrLen nWordLen=rEntry.Len();
    const String& rRepString = aReplaceED.GetText();

    sal_Bool bEnableNewReplace  = sal_False;
    sal_Bool bEnableDelete      = sal_False;
    String aNewReplaceText  = sNew;

    if(pEdt == &aWordED)
    {
        if(nWordLen>0)
        {
            sal_Bool bFound = sal_False;
            sal_Bool bTmpSelEntry=sal_False;
            CDE_RESULT eCmpRes = CDE_DIFFERENT;

            for(sal_uInt16 i = 0; i < aWordsLB.GetEntryCount(); i++)
            {
                SvLBoxEntry*  pEntry = aWordsLB.GetEntry( i );
                String aTestStr( aWordsLB.GetEntryText(pEntry, 0) );
                eCmpRes = cmpDicEntry_Impl( rEntry, aTestStr );
                if(CDE_DIFFERENT != eCmpRes)
                {
                    if(rRepString.Len())
                        bFirstSelect = sal_True;
                    bDoNothing=sal_True;
                    aWordsLB.SetCurEntry(pEntry);
                    bDoNothing=sal_False;
                    pFirstSel = pEntry;
                    aReplaceED.SetText(aWordsLB.GetEntryText(pEntry, 1));

                    if (CDE_SIMILAR == eCmpRes)
                    {
                        aNewReplaceText = sModify;
                        bEnableNewReplace = sal_True;
                    }
                    bFound= sal_True;
                    break;
                }
                else if(getNormDicEntry_Impl(aTestStr).Search(
                            getNormDicEntry_Impl( rEntry ) ) == 0
                        && !bTmpSelEntry)
                {
                    bDoNothing=sal_True;
                    aWordsLB.MakeVisible(pEntry);
                    bDoNothing=sal_False;
                    bTmpSelEntry=sal_True;

                    aNewReplaceText = sNew;
                    bEnableNewReplace = sal_True;
                }
            }

            if(!bFound)
            {
                aWordsLB.SelectAll(sal_False);
                pFirstSel = 0;

                aNewReplaceText = sNew;
                bEnableNewReplace = sal_True;
            }
            bEnableDelete = CDE_DIFFERENT != eCmpRes;
        }
        else if(aWordsLB.GetEntryCount()>0)
        {
            SvLBoxEntry*  pEntry = aWordsLB.GetEntry( 0 );
            bDoNothing=sal_True;
            aWordsLB.MakeVisible(pEntry);
            bDoNothing=sal_False;
        }
    }
    else if(pEdt == &aReplaceED)
    {
        String aReplaceText;
        String aWordText;
        if (pFirstSel)  // a aWordsLB entry is selected
        {
            aWordText    = aWordsLB.GetEntryText( pFirstSel, 0 );
             aReplaceText = aWordsLB.GetEntryText( pFirstSel, 1 );

            aNewReplaceText = sModify;
            bEnableDelete = sal_True;
        }
        sal_Bool bIsChange =
                CDE_EQUAL != cmpDicEntry_Impl(aWordED.GetText(), aWordText)
             || CDE_EQUAL != cmpDicEntry_Impl(aReplaceED.GetText(), aReplaceText);
        if (aWordED.GetText().Len()  &&  bIsChange)
            bEnableNewReplace = sal_True;
    }

    aNewReplacePB.SetText( aNewReplaceText );
    aNewReplacePB.Enable( bEnableNewReplace && !IsDicReadonly_Impl() );
    aDeletePB    .Enable( bEnableDelete     && !IsDicReadonly_Impl() );

    return 0;
}

//=========================================================
//SvxDictEdit
//=========================================================
void SvxDictEdit::KeyInput( const KeyEvent& rKEvt )
{
    const KeyCode aKeyCode = rKEvt.GetKeyCode();
    const sal_uInt16 nModifier = aKeyCode.GetModifier();
    if( aKeyCode.GetCode() == KEY_RETURN )
    {
        // if there's nothing done on enter, call the
        // base class after all to close the dialog
        if(!nModifier && !aActionLink.Call(this))
                 Edit::KeyInput(rKEvt);
    }
    else if(bSpaces || aKeyCode.GetCode() != KEY_SPACE)
        Edit::KeyInput(rKEvt);
}


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