Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / unodialogs / textconversiondlgs / chinese_dictionarydialog.cxx
blobddcc2f7c37d37aa8f616040a7895c35657a0b3af
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "chinese_dictionarydialog.hxx"
22 #include <cppuhelper/bootstrap.hxx>
23 #include <com/sun/star/i18n/TextConversionOption.hpp>
24 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
25 #include <com/sun/star/linguistic2/ConversionPropertyType.hpp>
26 #include <com/sun/star/linguistic2/ConversionDictionaryList.hpp>
27 #include <com/sun/star/linguistic2/XConversionPropertyType.hpp>
28 #include <com/sun/star/util/XFlushable.hpp>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <svtools/headbar.hxx>
31 #include <svtools/svlbitm.hxx>
32 #include "svtools/treelistentry.hxx"
33 #include <vcl/msgbox.hxx>
34 #include <vcl/settings.hxx>
35 #include <unotools/lingucfg.hxx>
36 #include <unotools/linguprops.hxx>
37 #include <unotools/intlwrapper.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <vcl/svapp.hxx>
40 #include "helpid.hrc"
43 namespace textconversiondlgs
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
50 #define HEADER_BAR_BITS ( HIB_LEFT | HIB_VCENTER | HIB_CLICKABLE | HIB_FIXED | HIB_FIXEDPOS )
52 DictionaryList::DictionaryList(SvSimpleTableContainer& rParent, WinBits nBits)
53 : SvSimpleTable(rParent, nBits)
54 , m_xDictionary(0)
55 , m_pED_Term(0)
56 , m_pED_Mapping(0)
57 , m_pLB_Property(0)
58 , m_aToBeDeleted()
59 , m_nSortColumnIndex(0)
63 OUString DictionaryList::getPropertyTypeName( sal_Int16 nConversionPropertyType ) const
65 if(!m_pLB_Property || !m_pLB_Property->GetEntryCount())
66 return OUString();
68 sal_uInt16 nPos = static_cast<sal_uInt16>( nConversionPropertyType )-1;
69 if(nPos<m_pLB_Property->GetEntryCount())
70 return m_pLB_Property->GetEntry(nPos);
71 return m_pLB_Property->GetEntry(0);
74 OUString DictionaryList::makeTabString( const DictionaryEntry& rEntry ) const
76 OUString aStr( rEntry.m_aTerm );
77 aStr += "\t";
78 aStr += rEntry.m_aMapping;
79 aStr += "\t";
80 aStr += getPropertyTypeName( rEntry.m_nConversionPropertyType );
81 return aStr;
84 void DictionaryList::save()
86 if( !m_xDictionary.is() )
87 return;
89 Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
91 sal_Int32 nN;
92 DictionaryEntry* pE;
94 for( nN = m_aToBeDeleted.size(); nN--; )
96 pE = m_aToBeDeleted[nN];
97 m_xDictionary->removeEntry( pE->m_aTerm, pE->m_aMapping );
99 for( nN = GetRowCount(); nN--; )
101 pE = getEntryOnPos( nN );
102 if(pE->m_bNewEntry)
106 m_xDictionary->addEntry( pE->m_aTerm, pE->m_aMapping );
107 xPropertyType->setPropertyType( pE->m_aTerm, pE->m_aMapping, pE->m_nConversionPropertyType );
109 catch( uno::Exception& )
115 Reference< util::XFlushable > xFlush( m_xDictionary, uno::UNO_QUERY );
116 if( xFlush.is() )
117 xFlush->flush();
120 void DictionaryList::deleteAll()
122 sal_Int32 nN;
123 for( nN = GetRowCount(); nN--; )
124 deleteEntryOnPos( nN );
125 for( nN = m_aToBeDeleted.size(); nN--; )
127 DictionaryEntry* pE = m_aToBeDeleted[nN];
128 delete pE;
130 m_aToBeDeleted.clear();
133 void DictionaryList::refillFromDictionary( sal_Int32 nTextConversionOptions )
135 deleteAll();
137 if(!m_xDictionary.is())
138 return;
140 Sequence< OUString > aLeftList( m_xDictionary->getConversionEntries( linguistic2::ConversionDirection_FROM_LEFT ) );
141 sal_Int32 nCount = aLeftList.getLength();
143 Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
145 OUString aLeft, aRight;
146 sal_Int16 nConversionPropertyType;
148 for(sal_Int32 nN=0; nN<nCount; nN++)
150 aLeft = aLeftList[nN];
151 Sequence< OUString > aRightList( m_xDictionary->getConversions(
152 aLeft, 0, aLeft.getLength()
153 , linguistic2::ConversionDirection_FROM_LEFT, nTextConversionOptions ) );
155 if(aRightList.getLength()!=1)
157 OSL_FAIL("The Chinese Translation Dictionary should have exactly one Mapping for each term.");
158 continue;
161 aRight = aRightList[0];
162 nConversionPropertyType = linguistic2::ConversionPropertyType::OTHER;
163 if(xPropertyType.is())
164 nConversionPropertyType = xPropertyType->getPropertyType(aLeft, aRight);
166 DictionaryEntry* pEntry = new DictionaryEntry( aLeft, aRight, nConversionPropertyType );
167 SvTreeListEntry* pLBEntry = InsertEntry( makeTabString( *pEntry ) );
168 pLBEntry->SetUserData( pEntry );
171 if( GetEntryCount() > 0 )
172 SelectRow( 0 );
175 DictionaryEntry* DictionaryList::getFirstSelectedEntry() const
177 DictionaryEntry* pRet=0;
178 for( sal_Int32 nN=GetRowCount(); nN--; )
180 if( IsRowSelected( nN ) )
182 pRet = getEntryOnPos( nN );
183 break;
186 return pRet;
189 DictionaryEntry* DictionaryList::getEntryOnPos( sal_Int32 nPos ) const
191 DictionaryEntry* pEntry=0;
192 SvTreeListEntry* pLBEntry = GetEntryOnPos( nPos );
193 if(pLBEntry)
194 pEntry = (DictionaryEntry*)pLBEntry->GetUserData();
195 return pEntry;
198 DictionaryEntry* DictionaryList::getTermEntry( const OUString& rTerm ) const
200 DictionaryEntry* pE = 0;
201 for( sal_Int32 nN=GetRowCount(); nN--; )
203 pE = getEntryOnPos( nN );
204 if( pE && rTerm.equals( pE->m_aTerm ) )
205 return pE;
207 return 0;
210 bool DictionaryList::hasTerm( const OUString& rTerm ) const
212 return getTermEntry(rTerm) !=0 ;
215 void DictionaryList::addEntry( const OUString& rTerm, const OUString& rMapping
216 , sal_Int16 nConversionPropertyType, sal_uIntPtr nPos )
218 if( hasTerm( rTerm ) )
219 return;
221 DictionaryEntry* pEntry = new DictionaryEntry( rTerm, rMapping, nConversionPropertyType, true );
222 SvTreeListEntry* pLBEntry = InsertEntryToColumn( makeTabString( *pEntry ), nPos );
223 pLBEntry->SetUserData( pEntry );
224 SelectRow( GetEntryPos( pLBEntry ) );
227 void DictionaryList::deleteEntryOnPos( sal_Int32 nPos )
229 SvTreeListEntry* pLBEntry = GetEntryOnPos( nPos );
230 DictionaryEntry* pEntry = getEntryOnPos( nPos );
231 if( pLBEntry )
232 RemoveParentKeepChildren( pLBEntry );
233 if( pEntry )
235 if( pEntry->m_bNewEntry )
236 delete pEntry;
237 else
238 m_aToBeDeleted.push_back( pEntry );
242 sal_uIntPtr DictionaryList::deleteEntries( const OUString& rTerm )
244 sal_uIntPtr nPos = TREELIST_APPEND;
245 for( sal_Int32 nN=GetRowCount(); nN--; )
247 DictionaryEntry* pCurEntry = getEntryOnPos( nN );
248 if( rTerm.equals( pCurEntry->m_aTerm ) )
250 nPos = nN;
251 SvTreeListEntry* pCurLBEntry = GetEntryOnPos( nN );
252 RemoveParentKeepChildren( pCurLBEntry );
253 if( pCurEntry->m_bNewEntry )
254 delete pCurEntry;
255 else
256 m_aToBeDeleted.push_back( pCurEntry );
259 return nPos;
262 void DictionaryList::sortByColumn( sal_uInt16 nSortColumnIndex, bool bSortAtoZ )
264 m_nSortColumnIndex=nSortColumnIndex;
265 if( nSortColumnIndex<3 )
267 if(bSortAtoZ)
268 GetModel()->SetSortMode(SortAscending);
269 else
270 GetModel()->SetSortMode(SortDescending);
272 GetModel()->SetCompareHdl( LINK( this, DictionaryList, CompareHdl));
273 GetModel()->Resort();
275 else
276 GetModel()->SetSortMode(SortNone);
279 sal_uInt16 DictionaryList::getSortColumn() const
281 return m_nSortColumnIndex;
284 IMPL_LINK( DictionaryList, CompareHdl, SvSortData*, pData )
286 SvTreeListEntry* pLeft = (SvTreeListEntry*)(pData->pLeft );
287 SvTreeListEntry* pRight = (SvTreeListEntry*)(pData->pRight );
288 return (long) ColumnCompare(pLeft,pRight);
291 sal_Int32 DictionaryList::ColumnCompare( SvTreeListEntry* pLeft, SvTreeListEntry* pRight )
293 sal_Int32 nCompare = 0;
295 SvLBoxItem* pLeftItem = getItemAtColumn( pLeft, m_nSortColumnIndex );
296 SvLBoxItem* pRightItem = getItemAtColumn( pRight, m_nSortColumnIndex );
298 if(pLeftItem != NULL && pRightItem != NULL)
300 sal_uInt16 nLeftKind = pLeftItem->GetType();
301 sal_uInt16 nRightKind = pRightItem->GetType();
303 if(nRightKind == SV_ITEM_ID_LBOXSTRING &&
304 nLeftKind == SV_ITEM_ID_LBOXSTRING )
306 IntlWrapper aIntlWrapper( Application::GetSettings().GetLanguageTag() );
307 const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator();
309 nCompare = pCollator->compareString( ((SvLBoxString*)pLeftItem)->GetText(),
310 ((SvLBoxString*)pRightItem)->GetText());
312 if (nCompare == 0)
313 nCompare = -1;
316 return nCompare;
319 SvLBoxItem* DictionaryList::getItemAtColumn( SvTreeListEntry* pEntry, sal_uInt16 nColumn ) const
321 SvLBoxItem* pItem = NULL;
322 if( pEntry )
324 sal_uInt16 nCount = pEntry->ItemCount();
325 nColumn++;
326 if( nTreeFlags & TREEFLAG_CHKBTN )
327 nColumn++;
328 if( nColumn < nCount )
329 pItem = pEntry->GetItem( nColumn );
331 return pItem;
338 DictionaryEntry::DictionaryEntry( const OUString& rTerm, const OUString& rMapping
339 , sal_Int16 nConversionPropertyType
340 , bool bNewEntry )
341 : m_aTerm( rTerm )
342 , m_aMapping( rMapping )
343 , m_nConversionPropertyType( nConversionPropertyType )
344 , m_bNewEntry( bNewEntry )
346 if( m_nConversionPropertyType == 0 )
347 m_nConversionPropertyType = 1;
350 DictionaryEntry::~DictionaryEntry()
354 bool DictionaryEntry::operator==( const DictionaryEntry& rE ) const
356 return m_aTerm == rE.m_aTerm
357 && m_aMapping == rE.m_aMapping
358 && m_nConversionPropertyType == rE.m_nConversionPropertyType;
361 void DictionaryList::setColSizes()
363 HeaderBar &rBar = GetTheHeaderBar();
364 if (rBar.GetItemCount() < 3)
365 return;
367 long nWidth1 = m_pED_Term->get_preferred_size().Width();
368 long nWidth2 = m_pED_Mapping->get_preferred_size().Width();
369 long nWidth3 = m_pLB_Property->get_preferred_size().Width();
371 long nWidth = GetSizePixel().Width();
372 long nPos3 = nWidth - nWidth3;
373 long nRemainder = nWidth - (nWidth1 + nWidth2 + nWidth3);
375 long aStaticTabs[] = { 3, 0, nWidth1 + (nRemainder/2), nPos3 };
376 SvSimpleTable::SetTabs(aStaticTabs, MAP_PIXEL);
379 void DictionaryList::Resize()
381 SvSimpleTable::Resize();
382 setColSizes();
385 void DictionaryList::init(const Reference< linguistic2::XConversionDictionary>& xDictionary,
386 Window *pED_Term, Window *pED_Mapping, ListBox *pLB_Property,
387 Window *pFT_Term, Window *pFT_Mapping, Window *pFT_Property)
389 SetStyle( WB_VSCROLL | WB_TABSTOP );
390 SetSelectionMode( SINGLE_SELECTION );
391 SetBorderStyle( WINDOW_BORDER_MONO );
392 SetHighlightRange();
394 if (m_xDictionary.is())
395 return;
397 m_xDictionary = xDictionary;
399 m_pED_Term = pED_Term;
400 m_pED_Mapping = pED_Mapping;
401 m_pLB_Property = pLB_Property;
403 HeaderBar& rHeaderBar = GetTheHeaderBar();
405 OUString aColumn1( OutputDevice::GetNonMnemonicString( pFT_Term->GetText() ) );
406 OUString aColumn2( OutputDevice::GetNonMnemonicString( pFT_Mapping->GetText() ) );
407 OUString aColumn3( OutputDevice::GetNonMnemonicString( pFT_Property->GetText() ) );
409 long nWidth1 = m_pED_Term->get_preferred_size().Width();
410 long nWidth2 = m_pED_Mapping->get_preferred_size().Width();
411 long nWidth3 = m_pLB_Property->get_preferred_size().Width();
413 HeaderBarItemBits nBits = HEADER_BAR_BITS;
414 rHeaderBar.InsertItem( 1, aColumn1, nWidth1, nBits | HIB_UPARROW );
415 rHeaderBar.InsertItem( 2, aColumn2, nWidth2, nBits );
416 rHeaderBar.InsertItem( 3, aColumn3, nWidth3, nBits );
418 long pTabs[] = { 3, 0, nWidth1, nWidth1 + nWidth2 };
419 SetTabs( &pTabs[0], MAP_PIXEL );
422 void ChineseDictionaryDialog::initDictionaryControl(DictionaryList *pList,
423 const Reference< linguistic2::XConversionDictionary>& xDictionary)
425 //init HeaderBar and set tabs
426 HeaderBar& rHeaderBar = pList->GetTheHeaderBar();
427 //set hdl
428 rHeaderBar.SetSelectHdl( LINK( this, ChineseDictionaryDialog, HeaderBarClick ) );
429 //set widgets to track the width of for columns
430 pList->init(xDictionary,
431 m_pED_Term, m_pED_Mapping, m_pLB_Property,
432 m_pFT_Term, m_pFT_Mapping, m_pFT_Property);
435 ChineseDictionaryDialog::ChineseDictionaryDialog( Window* pParent )
436 : ModalDialog(pParent, "ChineseDictionaryDialog",
437 "svx/ui/chinesedictionary.ui")
438 , m_nTextConversionOptions(i18n::TextConversionOption::NONE)
439 , m_xContext(0)
441 get(m_pRB_To_Simplified, "tradtosimple");
442 get(m_pRB_To_Traditional, "simpletotrad");
443 get(m_pCB_Reverse, "reverse");
444 get(m_pFT_Term, "termft");
445 get(m_pED_Term, "term");
446 get(m_pFT_Mapping, "mappingft");
447 get(m_pED_Mapping, "mapping");
448 get(m_pFT_Property, "propertyft");
449 get(m_pLB_Property, "property");
451 get(m_pPB_Add, "add");
452 get(m_pPB_Modify, "modify");
453 get(m_pPB_Delete, "delete");
455 get(mpToSimplifiedContainer, "tradtosimpleview");
456 mpToSimplifiedContainer->set_height_request(mpToSimplifiedContainer->GetTextHeight() * 8);
457 m_pCT_DictionaryToSimplified = new DictionaryList(*mpToSimplifiedContainer, 0);
458 get(mpToTraditionalContainer, "simpletotradview");
459 mpToTraditionalContainer->set_height_request(mpToTraditionalContainer->GetTextHeight() * 8);
460 m_pCT_DictionaryToTraditional = new DictionaryList(*mpToTraditionalContainer, 0);
462 SvtLinguConfig aLngCfg;
463 bool bValue;
464 Any aAny( aLngCfg.GetProperty( OUString( UPN_IS_REVERSE_MAPPING ) ) );
465 if( aAny >>= bValue )
466 m_pCB_Reverse->Check( bValue );
468 m_pLB_Property->SetDropDownLineCount( m_pLB_Property->GetEntryCount() );
469 m_pLB_Property->SelectEntryPos(0);
471 Reference< linguistic2::XConversionDictionary > xDictionary_To_Simplified(0);
472 Reference< linguistic2::XConversionDictionary > xDictionary_To_Traditional(0);
473 //get dictionaries
475 if(!m_xContext.is())
476 m_xContext = Reference< XComponentContext >( ::cppu::defaultBootstrap_InitialComponentContext() );
477 if(m_xContext.is())
479 Reference< linguistic2::XConversionDictionaryList > xDictionaryList = linguistic2::ConversionDictionaryList::create(m_xContext);
480 Reference< container::XNameContainer > xContainer( xDictionaryList->getDictionaryContainer() );
481 if(xContainer.is())
485 OUString aNameTo_Simplified("ChineseT2S");
486 OUString aNameTo_Traditional("ChineseS2T");
487 lang::Locale aLocale;
488 aLocale.Language = "zh";
490 if( xContainer->hasByName( aNameTo_Simplified ) )
491 xDictionary_To_Simplified = Reference< linguistic2::XConversionDictionary >(
492 xContainer->getByName( aNameTo_Simplified ), UNO_QUERY );
493 else
495 aLocale.Country = "TW";
496 xDictionary_To_Simplified = Reference< linguistic2::XConversionDictionary >(
497 xDictionaryList->addNewDictionary( aNameTo_Simplified
498 , aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE
499 ), UNO_QUERY );
501 if (xDictionary_To_Simplified.is())
502 xDictionary_To_Simplified->setActive( sal_True );
505 if( xContainer->hasByName( aNameTo_Traditional ) )
506 xDictionary_To_Traditional = Reference< linguistic2::XConversionDictionary >(
507 xContainer->getByName( aNameTo_Traditional ), UNO_QUERY );
508 else
510 aLocale.Country = "CN";
511 xDictionary_To_Traditional = Reference< linguistic2::XConversionDictionary >(
512 xDictionaryList->addNewDictionary( aNameTo_Traditional
513 , aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE
514 ), UNO_QUERY );
516 if (xDictionary_To_Traditional.is())
517 xDictionary_To_Traditional->setActive( sal_True );
520 catch(const uno::Exception& )
527 //init dictionary controls
528 initDictionaryControl(m_pCT_DictionaryToSimplified, xDictionary_To_Simplified);
529 initDictionaryControl(m_pCT_DictionaryToTraditional, xDictionary_To_Traditional);
531 updateAfterDirectionChange();
533 m_pED_Term->SetModifyHdl( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
534 m_pED_Mapping->SetModifyHdl( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
535 m_pLB_Property->SetSelectHdl( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
537 m_pRB_To_Simplified->SetClickHdl( LINK( this, ChineseDictionaryDialog, DirectionHdl ) );
538 m_pRB_To_Traditional->SetClickHdl( LINK( this, ChineseDictionaryDialog, DirectionHdl ) );
540 m_pCT_DictionaryToSimplified->SetSelectHdl( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
541 m_pCT_DictionaryToTraditional->SetSelectHdl( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
543 m_pPB_Add->SetClickHdl( LINK( this, ChineseDictionaryDialog, AddHdl ) );
544 m_pPB_Modify->SetClickHdl( LINK( this, ChineseDictionaryDialog, ModifyHdl ) );
545 m_pPB_Delete->SetClickHdl( LINK( this, ChineseDictionaryDialog, DeleteHdl ) );
548 ChineseDictionaryDialog::~ChineseDictionaryDialog()
550 m_xContext=0;
551 delete m_pCT_DictionaryToSimplified;
552 delete m_pCT_DictionaryToTraditional;
555 void ChineseDictionaryDialog::setDirectionAndTextConversionOptions( bool bDirectionToSimplified, sal_Int32 nTextConversionOptions /*i18n::TextConversionOption*/ )
557 if( bDirectionToSimplified == bool(m_pRB_To_Simplified->IsChecked())
558 && nTextConversionOptions == m_nTextConversionOptions )
559 return;
561 m_nTextConversionOptions = nTextConversionOptions;
563 if( bDirectionToSimplified )
564 m_pRB_To_Simplified->Check();
565 else
566 m_pRB_To_Traditional->Check();
567 updateAfterDirectionChange();
570 IMPL_LINK_NOARG(ChineseDictionaryDialog, DirectionHdl)
572 updateAfterDirectionChange();
573 return 0;
576 void ChineseDictionaryDialog::updateAfterDirectionChange()
578 Reference< linguistic2::XConversionDictionary > xDictionary(0);
580 if( m_pRB_To_Simplified->IsChecked() )
582 mpToTraditionalContainer->Hide();
583 mpToSimplifiedContainer->Show();
584 xDictionary = m_pCT_DictionaryToSimplified->m_xDictionary;
586 else
588 mpToSimplifiedContainer->Hide();
589 mpToTraditionalContainer->Show();
590 xDictionary = m_pCT_DictionaryToTraditional->m_xDictionary;
593 updateButtons();
596 IMPL_LINK_NOARG(ChineseDictionaryDialog, EditFieldsHdl)
598 updateButtons();
599 return 0;
601 IMPL_LINK_NOARG(ChineseDictionaryDialog, MappingSelectHdl)
603 DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
604 if(pE)
606 m_pED_Term->SetText( pE->m_aTerm );
607 m_pED_Mapping->SetText( pE->m_aMapping );
608 sal_Int16 nPos = pE->m_nConversionPropertyType-1;
609 if( nPos<0 || nPos>=m_pLB_Property->GetEntryCount() )
610 nPos=0;
611 if( m_pLB_Property->GetEntryCount() )
612 m_pLB_Property->SelectEntryPos(nPos);
615 updateButtons();
616 return 0;
619 bool ChineseDictionaryDialog::isEditFieldsHaveContent() const
621 return !m_pED_Term->GetText().isEmpty() && !m_pED_Mapping->GetText().isEmpty();
624 bool ChineseDictionaryDialog::isEditFieldsContentEqualsSelectedListContent() const
626 DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
627 if( pE )
629 if( pE->m_aTerm != OUString( m_pED_Term->GetText() ) )
630 return false;
631 if( pE->m_aMapping != OUString( m_pED_Mapping->GetText() ) )
632 return false;
633 if( pE->m_nConversionPropertyType != m_pLB_Property->GetSelectEntryPos()+1 )
634 return false;
635 return true;
637 return false;
640 const DictionaryList& ChineseDictionaryDialog::getActiveDictionary() const
642 if( m_pRB_To_Traditional->IsChecked() )
643 return *m_pCT_DictionaryToTraditional;
644 return *m_pCT_DictionaryToSimplified;
647 DictionaryList& ChineseDictionaryDialog::getActiveDictionary()
649 if( m_pRB_To_Traditional->IsChecked() )
650 return *m_pCT_DictionaryToTraditional;
651 return *m_pCT_DictionaryToSimplified;
654 const DictionaryList& ChineseDictionaryDialog::getReverseDictionary() const
656 if( m_pRB_To_Traditional->IsChecked() )
657 return *m_pCT_DictionaryToSimplified;
658 return *m_pCT_DictionaryToTraditional;
661 DictionaryList& ChineseDictionaryDialog::getReverseDictionary()
663 if( m_pRB_To_Traditional->IsChecked() )
664 return *m_pCT_DictionaryToSimplified;
665 return *m_pCT_DictionaryToTraditional;
668 void ChineseDictionaryDialog::updateButtons()
670 bool bAdd = isEditFieldsHaveContent() && !getActiveDictionary().hasTerm( m_pED_Term->GetText() );
671 m_pPB_Add->Enable( bAdd );
673 m_pPB_Delete->Enable( !bAdd && getActiveDictionary().GetSelectedRowCount()>0 );
675 // DictionaryEntry* pFirstSelectedEntry = getActiveDictionary().getFirstSelectedEntry();
677 bool bModify = false;
679 DictionaryEntry* pFirstSelectedEntry = getActiveDictionary().getFirstSelectedEntry();
680 bModify = !bAdd && getActiveDictionary().GetSelectedRowCount()==1
681 && pFirstSelectedEntry && pFirstSelectedEntry->m_aTerm.equals( m_pED_Term->GetText() );
682 if( bModify && isEditFieldsContentEqualsSelectedListContent() )
683 bModify = false;
685 m_pPB_Modify->Enable( bModify );
688 IMPL_LINK_NOARG(ChineseDictionaryDialog, AddHdl)
690 if( !isEditFieldsHaveContent() )
691 return 0;
693 sal_Int16 nConversionPropertyType = m_pLB_Property->GetSelectEntryPos()+1;
695 getActiveDictionary().addEntry( m_pED_Term->GetText(), m_pED_Mapping->GetText(), nConversionPropertyType );
697 if( m_pCB_Reverse->IsChecked() )
699 getReverseDictionary().deleteEntries( m_pED_Mapping->GetText() );
700 getReverseDictionary().addEntry( m_pED_Mapping->GetText(), m_pED_Term->GetText(), nConversionPropertyType );
703 updateButtons();
704 return 0;
706 IMPL_LINK_NOARG(ChineseDictionaryDialog, ModifyHdl)
708 OUString aTerm( m_pED_Term->GetText() );
709 OUString aMapping( m_pED_Mapping->GetText() );
710 sal_Int16 nConversionPropertyType = m_pLB_Property->GetSelectEntryPos()+1;
712 DictionaryList& rActive = getActiveDictionary();
713 DictionaryList& rReverse = getReverseDictionary();
715 DictionaryEntry* pE = rActive.getFirstSelectedEntry();
716 if( pE && pE->m_aTerm != aTerm )
717 return 0;
719 if( pE )
721 if( pE->m_aMapping != aMapping || pE->m_nConversionPropertyType != nConversionPropertyType )
723 if( m_pCB_Reverse->IsChecked() )
725 sal_uIntPtr nPos = rReverse.deleteEntries( pE->m_aMapping );
726 nPos = rReverse.deleteEntries( aMapping );
727 rReverse.addEntry( aMapping, aTerm, nConversionPropertyType, nPos );
730 sal_uIntPtr nPos = rActive.deleteEntries( aTerm );
731 rActive.addEntry( aTerm, aMapping, nConversionPropertyType, nPos );
735 updateButtons();
736 return 0;
739 IMPL_LINK_NOARG(ChineseDictionaryDialog, DeleteHdl)
741 DictionaryList& rActive = getActiveDictionary();
742 DictionaryList& rReverse = getReverseDictionary();
744 if( rActive.GetSelectedRowCount()>0)
746 DictionaryEntry* pEntry;
748 OUString aMapping;
749 for( sal_Int32 nN=rActive.GetRowCount(); nN--; )
751 if( rActive.IsRowSelected( nN ) )
753 pEntry = rActive.getEntryOnPos( nN );
754 if(pEntry)
756 aMapping = pEntry->m_aMapping;
757 rActive.deleteEntryOnPos( nN );
758 if( m_pCB_Reverse->IsChecked() )
759 rReverse.deleteEntries( aMapping );
761 break;
766 updateButtons();
767 return 0;
770 short ChineseDictionaryDialog::Execute()
772 sal_Int32 nTextConversionOptions = m_nTextConversionOptions;
773 if(m_nTextConversionOptions & i18n::TextConversionOption::USE_CHARACTER_VARIANTS )
774 nTextConversionOptions = nTextConversionOptions^i18n::TextConversionOption::USE_CHARACTER_VARIANTS ;
776 m_pCT_DictionaryToSimplified->refillFromDictionary( nTextConversionOptions );
777 m_pCT_DictionaryToTraditional->refillFromDictionary( m_nTextConversionOptions );
779 short nRet = ModalDialog::Execute();
781 if( nRet == RET_OK )
783 //save settings to configuration
784 SvtLinguConfig aLngCfg;
785 Any aAny;
786 aAny <<= m_pCB_Reverse->IsChecked();
787 aLngCfg.SetProperty( OUString( UPN_IS_REVERSE_MAPPING ), aAny );
789 m_pCT_DictionaryToSimplified->save();
790 m_pCT_DictionaryToTraditional->save();
793 m_pCT_DictionaryToSimplified->deleteAll();
794 m_pCT_DictionaryToTraditional->deleteAll();
796 return nRet;
799 IMPL_LINK(ChineseDictionaryDialog, HeaderBarClick, HeaderBar*, pHeaderBar)
801 sal_uInt16 nId = pHeaderBar->GetCurItemId();
802 HeaderBarItemBits nBits = pHeaderBar->GetItemBits(nId);
803 if( nBits & HIB_CLICKABLE )
805 //set new arrow positions in headerbar
806 pHeaderBar->SetItemBits( getActiveDictionary().getSortColumn()+1, HEADER_BAR_BITS );
807 if( nBits & HIB_UPARROW )
808 pHeaderBar->SetItemBits( nId, HEADER_BAR_BITS | HIB_DOWNARROW );
809 else
810 pHeaderBar->SetItemBits( nId, HEADER_BAR_BITS | HIB_UPARROW );
812 //sort lists
813 nBits = pHeaderBar->GetItemBits(nId);
814 bool bSortAtoZ = nBits & HIB_UPARROW;
815 getActiveDictionary().sortByColumn(nId-1,bSortAtoZ);
816 getReverseDictionary().sortByColumn(nId-1,bSortAtoZ);
819 return 0;
823 } //end namespace
826 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */