Line data Source code
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 :
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"
41 :
42 :
43 : namespace textconversiondlgs
44 : {
45 :
46 :
47 : using namespace ::com::sun::star;
48 : using namespace ::com::sun::star::uno;
49 :
50 : #define HEADER_BAR_BITS ( HeaderBarItemBits::LEFT | HeaderBarItemBits::VCENTER | HeaderBarItemBits::CLICKABLE | HeaderBarItemBits::FIXED | HeaderBarItemBits::FIXEDPOS )
51 :
52 0 : 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 0 : , m_nSortColumnIndex(0)
60 : {
61 0 : }
62 :
63 0 : DictionaryList::~DictionaryList()
64 : {
65 0 : disposeOnce();
66 0 : }
67 :
68 0 : void DictionaryList::dispose()
69 : {
70 0 : m_pED_Term.clear();
71 0 : m_pED_Mapping.clear();
72 0 : m_pLB_Property.clear();
73 0 : SvSimpleTable::dispose();
74 0 : }
75 :
76 0 : OUString DictionaryList::getPropertyTypeName( sal_Int16 nConversionPropertyType ) const
77 : {
78 0 : if(!m_pLB_Property || !m_pLB_Property->GetEntryCount())
79 0 : return OUString();
80 :
81 0 : sal_uInt16 nPos = static_cast<sal_uInt16>( nConversionPropertyType )-1;
82 0 : if(nPos<m_pLB_Property->GetEntryCount())
83 0 : return m_pLB_Property->GetEntry(nPos);
84 0 : return m_pLB_Property->GetEntry(0);
85 : }
86 :
87 0 : OUString DictionaryList::makeTabString( const DictionaryEntry& rEntry ) const
88 : {
89 0 : OUString aStr( rEntry.m_aTerm );
90 0 : aStr += "\t";
91 0 : aStr += rEntry.m_aMapping;
92 0 : aStr += "\t";
93 0 : aStr += getPropertyTypeName( rEntry.m_nConversionPropertyType );
94 0 : return aStr;
95 : }
96 :
97 0 : void DictionaryList::save()
98 : {
99 0 : if( !m_xDictionary.is() )
100 0 : return;
101 :
102 0 : Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
103 :
104 : sal_Int32 nN;
105 : DictionaryEntry* pE;
106 :
107 0 : for( nN = m_aToBeDeleted.size(); nN--; )
108 : {
109 0 : pE = m_aToBeDeleted[nN];
110 0 : m_xDictionary->removeEntry( pE->m_aTerm, pE->m_aMapping );
111 : }
112 0 : for( nN = GetRowCount(); nN--; )
113 : {
114 0 : pE = getEntryOnPos( nN );
115 0 : if(pE->m_bNewEntry)
116 : {
117 : try
118 : {
119 0 : m_xDictionary->addEntry( pE->m_aTerm, pE->m_aMapping );
120 0 : xPropertyType->setPropertyType( pE->m_aTerm, pE->m_aMapping, pE->m_nConversionPropertyType );
121 : }
122 0 : catch( uno::Exception& )
123 : {
124 :
125 : }
126 : }
127 : }
128 0 : Reference< util::XFlushable > xFlush( m_xDictionary, uno::UNO_QUERY );
129 0 : if( xFlush.is() )
130 0 : xFlush->flush();
131 : }
132 :
133 0 : void DictionaryList::deleteAll()
134 : {
135 : sal_Int32 nN;
136 0 : for( nN = GetRowCount(); nN--; )
137 0 : deleteEntryOnPos( nN );
138 0 : for( nN = m_aToBeDeleted.size(); nN--; )
139 : {
140 0 : DictionaryEntry* pE = m_aToBeDeleted[nN];
141 0 : delete pE;
142 : }
143 0 : m_aToBeDeleted.clear();
144 0 : }
145 :
146 0 : void DictionaryList::refillFromDictionary( sal_Int32 nTextConversionOptions )
147 : {
148 0 : deleteAll();
149 :
150 0 : if(!m_xDictionary.is())
151 0 : return;
152 :
153 0 : Sequence< OUString > aLeftList( m_xDictionary->getConversionEntries( linguistic2::ConversionDirection_FROM_LEFT ) );
154 0 : sal_Int32 nCount = aLeftList.getLength();
155 :
156 0 : Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
157 :
158 0 : OUString aLeft, aRight;
159 : sal_Int16 nConversionPropertyType;
160 :
161 0 : for(sal_Int32 nN=0; nN<nCount; nN++)
162 : {
163 0 : aLeft = aLeftList[nN];
164 0 : Sequence< OUString > aRightList( m_xDictionary->getConversions(
165 : aLeft, 0, aLeft.getLength()
166 0 : , linguistic2::ConversionDirection_FROM_LEFT, nTextConversionOptions ) );
167 :
168 0 : if(aRightList.getLength()!=1)
169 : {
170 : OSL_FAIL("The Chinese Translation Dictionary should have exactly one Mapping for each term.");
171 0 : continue;
172 : }
173 :
174 0 : aRight = aRightList[0];
175 0 : nConversionPropertyType = linguistic2::ConversionPropertyType::OTHER;
176 0 : if(xPropertyType.is())
177 0 : nConversionPropertyType = xPropertyType->getPropertyType(aLeft, aRight);
178 :
179 0 : DictionaryEntry* pEntry = new DictionaryEntry( aLeft, aRight, nConversionPropertyType );
180 0 : SvTreeListEntry* pLBEntry = InsertEntry( makeTabString( *pEntry ) );
181 0 : pLBEntry->SetUserData( pEntry );
182 0 : }
183 :
184 0 : if( GetEntryCount() > 0 )
185 0 : SelectRow( 0 );
186 : }
187 :
188 0 : DictionaryEntry* DictionaryList::getFirstSelectedEntry() const
189 : {
190 0 : DictionaryEntry* pRet=0;
191 0 : for( sal_Int32 nN=GetRowCount(); nN--; )
192 : {
193 0 : if( IsRowSelected( nN ) )
194 : {
195 0 : pRet = getEntryOnPos( nN );
196 0 : break;
197 : }
198 : }
199 0 : return pRet;
200 : }
201 :
202 0 : DictionaryEntry* DictionaryList::getEntryOnPos( sal_Int32 nPos ) const
203 : {
204 0 : DictionaryEntry* pEntry=0;
205 0 : SvTreeListEntry* pLBEntry = GetEntryOnPos( nPos );
206 0 : if(pLBEntry)
207 0 : pEntry = static_cast<DictionaryEntry*>(pLBEntry->GetUserData());
208 0 : return pEntry;
209 : }
210 :
211 0 : DictionaryEntry* DictionaryList::getTermEntry( const OUString& rTerm ) const
212 : {
213 0 : for( sal_Int32 nN=GetRowCount(); nN--; )
214 : {
215 0 : DictionaryEntry* pE = getEntryOnPos( nN );
216 0 : if( pE && rTerm.equals( pE->m_aTerm ) )
217 0 : return pE;
218 : }
219 0 : return 0;
220 : }
221 :
222 0 : bool DictionaryList::hasTerm( const OUString& rTerm ) const
223 : {
224 0 : return getTermEntry(rTerm) !=0 ;
225 : }
226 :
227 0 : void DictionaryList::addEntry( const OUString& rTerm, const OUString& rMapping
228 : , sal_Int16 nConversionPropertyType, sal_uIntPtr nPos )
229 : {
230 0 : if( hasTerm( rTerm ) )
231 0 : return;
232 :
233 0 : DictionaryEntry* pEntry = new DictionaryEntry( rTerm, rMapping, nConversionPropertyType, true );
234 0 : SvTreeListEntry* pLBEntry = InsertEntryToColumn( makeTabString( *pEntry ), nPos );
235 0 : pLBEntry->SetUserData( pEntry );
236 0 : SelectRow( GetEntryPos( pLBEntry ) );
237 : }
238 :
239 0 : void DictionaryList::deleteEntryOnPos( sal_Int32 nPos )
240 : {
241 0 : SvTreeListEntry* pLBEntry = GetEntryOnPos( nPos );
242 0 : DictionaryEntry* pEntry = getEntryOnPos( nPos );
243 0 : if( pLBEntry )
244 0 : RemoveParentKeepChildren( pLBEntry );
245 0 : if( pEntry )
246 : {
247 0 : if( pEntry->m_bNewEntry )
248 0 : delete pEntry;
249 : else
250 0 : m_aToBeDeleted.push_back( pEntry );
251 : }
252 0 : }
253 :
254 0 : sal_uIntPtr DictionaryList::deleteEntries( const OUString& rTerm )
255 : {
256 0 : sal_uIntPtr nPos = TREELIST_APPEND;
257 0 : for( sal_Int32 nN=GetRowCount(); nN--; )
258 : {
259 0 : DictionaryEntry* pCurEntry = getEntryOnPos( nN );
260 0 : if( rTerm.equals( pCurEntry->m_aTerm ) )
261 : {
262 0 : nPos = nN;
263 0 : SvTreeListEntry* pCurLBEntry = GetEntryOnPos( nN );
264 0 : RemoveParentKeepChildren( pCurLBEntry );
265 0 : if( pCurEntry->m_bNewEntry )
266 0 : delete pCurEntry;
267 : else
268 0 : m_aToBeDeleted.push_back( pCurEntry );
269 : }
270 : }
271 0 : return nPos;
272 : }
273 :
274 0 : void DictionaryList::sortByColumn( sal_uInt16 nSortColumnIndex, bool bSortAtoZ )
275 : {
276 0 : m_nSortColumnIndex=nSortColumnIndex;
277 0 : if( nSortColumnIndex<3 )
278 : {
279 0 : if(bSortAtoZ)
280 0 : GetModel()->SetSortMode(SortAscending);
281 : else
282 0 : GetModel()->SetSortMode(SortDescending);
283 :
284 0 : GetModel()->SetCompareHdl( LINK( this, DictionaryList, CompareHdl));
285 0 : GetModel()->Resort();
286 : }
287 : else
288 0 : GetModel()->SetSortMode(SortNone);
289 0 : }
290 :
291 :
292 0 : IMPL_LINK( DictionaryList, CompareHdl, SvSortData*, pData )
293 : {
294 0 : SvTreeListEntry* pLeft = const_cast<SvTreeListEntry*>(pData->pLeft);
295 0 : SvTreeListEntry* pRight = const_cast<SvTreeListEntry*>(pData->pRight);
296 0 : return (long) ColumnCompare(pLeft,pRight);
297 : }
298 :
299 0 : sal_Int32 DictionaryList::ColumnCompare( SvTreeListEntry* pLeft, SvTreeListEntry* pRight )
300 : {
301 0 : sal_Int32 nCompare = 0;
302 :
303 0 : SvLBoxItem* pLeftItem = getItemAtColumn( pLeft, m_nSortColumnIndex );
304 0 : SvLBoxItem* pRightItem = getItemAtColumn( pRight, m_nSortColumnIndex );
305 :
306 0 : if(pLeftItem != NULL && pRightItem != NULL)
307 : {
308 0 : sal_uInt16 nLeftKind = pLeftItem->GetType();
309 0 : sal_uInt16 nRightKind = pRightItem->GetType();
310 :
311 0 : if(nRightKind == SV_ITEM_ID_LBOXSTRING &&
312 : nLeftKind == SV_ITEM_ID_LBOXSTRING )
313 : {
314 0 : IntlWrapper aIntlWrapper( Application::GetSettings().GetLanguageTag() );
315 0 : const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator();
316 :
317 : nCompare = pCollator->compareString( static_cast<SvLBoxString*>(pLeftItem)->GetText(),
318 0 : static_cast<SvLBoxString*>(pRightItem)->GetText());
319 :
320 0 : if (nCompare == 0)
321 0 : nCompare = -1;
322 : }
323 : }
324 0 : return nCompare;
325 : }
326 :
327 0 : SvLBoxItem* DictionaryList::getItemAtColumn( SvTreeListEntry* pEntry, sal_uInt16 nColumn ) const
328 : {
329 0 : SvLBoxItem* pItem = NULL;
330 0 : if( pEntry )
331 : {
332 0 : sal_uInt16 nCount = pEntry->ItemCount();
333 0 : nColumn++;
334 0 : if( nTreeFlags & SvTreeFlags::CHKBTN )
335 0 : nColumn++;
336 0 : if( nColumn < nCount )
337 0 : pItem = pEntry->GetItem( nColumn );
338 : }
339 0 : return pItem;
340 : }
341 :
342 :
343 :
344 :
345 :
346 0 : DictionaryEntry::DictionaryEntry( const OUString& rTerm, const OUString& rMapping
347 : , sal_Int16 nConversionPropertyType
348 : , bool bNewEntry )
349 : : m_aTerm( rTerm )
350 : , m_aMapping( rMapping )
351 : , m_nConversionPropertyType( nConversionPropertyType )
352 0 : , m_bNewEntry( bNewEntry )
353 : {
354 0 : if( m_nConversionPropertyType == 0 )
355 0 : m_nConversionPropertyType = 1;
356 0 : }
357 :
358 0 : DictionaryEntry::~DictionaryEntry()
359 : {
360 0 : }
361 :
362 0 : bool DictionaryEntry::operator==( const DictionaryEntry& rE ) const
363 : {
364 0 : return m_aTerm == rE.m_aTerm
365 0 : && m_aMapping == rE.m_aMapping
366 0 : && m_nConversionPropertyType == rE.m_nConversionPropertyType;
367 : }
368 :
369 0 : void DictionaryList::setColSizes()
370 : {
371 0 : HeaderBar &rBar = GetTheHeaderBar();
372 0 : if (rBar.GetItemCount() < 3)
373 0 : return;
374 :
375 0 : long nWidth1 = m_pED_Term->get_preferred_size().Width();
376 0 : long nWidth2 = m_pED_Mapping->get_preferred_size().Width();
377 0 : long nWidth3 = m_pLB_Property->get_preferred_size().Width();
378 :
379 0 : long nWidth = GetSizePixel().Width();
380 0 : long nPos3 = nWidth - nWidth3;
381 0 : long nRemainder = nWidth - (nWidth1 + nWidth2 + nWidth3);
382 :
383 0 : long aStaticTabs[] = { 3, 0, nWidth1 + (nRemainder/2), nPos3 };
384 0 : SvSimpleTable::SetTabs(aStaticTabs, MAP_PIXEL);
385 : }
386 :
387 0 : void DictionaryList::Resize()
388 : {
389 0 : SvSimpleTable::Resize();
390 0 : setColSizes();
391 0 : }
392 :
393 0 : void DictionaryList::init(const Reference< linguistic2::XConversionDictionary>& xDictionary,
394 : vcl::Window *pED_Term, vcl::Window *pED_Mapping, ListBox *pLB_Property,
395 : vcl::Window *pFT_Term, vcl::Window *pFT_Mapping, vcl::Window *pFT_Property)
396 : {
397 0 : SetStyle( WB_VSCROLL | WB_TABSTOP );
398 0 : SetSelectionMode( SINGLE_SELECTION );
399 0 : SetBorderStyle( WindowBorderStyle::MONO );
400 0 : SetHighlightRange();
401 :
402 0 : if (m_xDictionary.is())
403 0 : return;
404 :
405 0 : m_xDictionary = xDictionary;
406 :
407 0 : m_pED_Term = pED_Term;
408 0 : m_pED_Mapping = pED_Mapping;
409 0 : m_pLB_Property = pLB_Property;
410 :
411 0 : HeaderBar& rHeaderBar = GetTheHeaderBar();
412 :
413 0 : OUString aColumn1( OutputDevice::GetNonMnemonicString( pFT_Term->GetText() ) );
414 0 : OUString aColumn2( OutputDevice::GetNonMnemonicString( pFT_Mapping->GetText() ) );
415 0 : OUString aColumn3( OutputDevice::GetNonMnemonicString( pFT_Property->GetText() ) );
416 :
417 0 : long nWidth1 = m_pED_Term->get_preferred_size().Width();
418 0 : long nWidth2 = m_pED_Mapping->get_preferred_size().Width();
419 0 : long nWidth3 = m_pLB_Property->get_preferred_size().Width();
420 :
421 0 : HeaderBarItemBits nBits = HEADER_BAR_BITS;
422 0 : rHeaderBar.InsertItem( 1, aColumn1, nWidth1, nBits | HeaderBarItemBits::UPARROW );
423 0 : rHeaderBar.InsertItem( 2, aColumn2, nWidth2, nBits );
424 0 : rHeaderBar.InsertItem( 3, aColumn3, nWidth3, nBits );
425 :
426 0 : long pTabs[] = { 3, 0, nWidth1, nWidth1 + nWidth2 };
427 0 : SetTabs( &pTabs[0], MAP_PIXEL );
428 : }
429 :
430 0 : void ChineseDictionaryDialog::initDictionaryControl(DictionaryList *pList,
431 : const Reference< linguistic2::XConversionDictionary>& xDictionary)
432 : {
433 : //init HeaderBar and set tabs
434 0 : HeaderBar& rHeaderBar = pList->GetTheHeaderBar();
435 : //set hdl
436 0 : rHeaderBar.SetSelectHdl( LINK( this, ChineseDictionaryDialog, HeaderBarClick ) );
437 : //set widgets to track the width of for columns
438 : pList->init(xDictionary,
439 : m_pED_Term, m_pED_Mapping, m_pLB_Property,
440 0 : m_pFT_Term, m_pFT_Mapping, m_pFT_Property);
441 0 : }
442 :
443 0 : ChineseDictionaryDialog::ChineseDictionaryDialog( vcl::Window* pParent )
444 : : ModalDialog(pParent, "ChineseDictionaryDialog",
445 : "svx/ui/chinesedictionary.ui")
446 : , m_nTextConversionOptions(i18n::TextConversionOption::NONE)
447 0 : , m_xContext(0)
448 : {
449 0 : get(m_pRB_To_Simplified, "tradtosimple");
450 0 : get(m_pRB_To_Traditional, "simpletotrad");
451 0 : get(m_pCB_Reverse, "reverse");
452 0 : get(m_pFT_Term, "termft");
453 0 : get(m_pED_Term, "term");
454 0 : get(m_pFT_Mapping, "mappingft");
455 0 : get(m_pED_Mapping, "mapping");
456 0 : get(m_pFT_Property, "propertyft");
457 0 : get(m_pLB_Property, "property");
458 :
459 0 : get(m_pPB_Add, "add");
460 0 : get(m_pPB_Modify, "modify");
461 0 : get(m_pPB_Delete, "delete");
462 :
463 0 : get(mpToSimplifiedContainer, "tradtosimpleview");
464 0 : mpToSimplifiedContainer->set_height_request(mpToSimplifiedContainer->GetTextHeight() * 8);
465 0 : m_pCT_DictionaryToSimplified = VclPtr<DictionaryList>::Create(*mpToSimplifiedContainer, 0);
466 0 : get(mpToTraditionalContainer, "simpletotradview");
467 0 : mpToTraditionalContainer->set_height_request(mpToTraditionalContainer->GetTextHeight() * 8);
468 0 : m_pCT_DictionaryToTraditional = VclPtr<DictionaryList>::Create(*mpToTraditionalContainer, 0);
469 :
470 0 : SvtLinguConfig aLngCfg;
471 : bool bValue;
472 0 : Any aAny( aLngCfg.GetProperty( OUString( UPN_IS_REVERSE_MAPPING ) ) );
473 0 : if( aAny >>= bValue )
474 0 : m_pCB_Reverse->Check( bValue );
475 :
476 0 : m_pLB_Property->SetDropDownLineCount( m_pLB_Property->GetEntryCount() );
477 0 : m_pLB_Property->SelectEntryPos(0);
478 :
479 0 : Reference< linguistic2::XConversionDictionary > xDictionary_To_Simplified(0);
480 0 : Reference< linguistic2::XConversionDictionary > xDictionary_To_Traditional(0);
481 : //get dictionaries
482 : {
483 0 : if(!m_xContext.is())
484 0 : m_xContext = Reference< XComponentContext >( ::cppu::defaultBootstrap_InitialComponentContext() );
485 0 : if(m_xContext.is())
486 : {
487 0 : Reference< linguistic2::XConversionDictionaryList > xDictionaryList = linguistic2::ConversionDictionaryList::create(m_xContext);
488 0 : Reference< container::XNameContainer > xContainer( xDictionaryList->getDictionaryContainer() );
489 0 : if(xContainer.is())
490 : {
491 : try
492 : {
493 0 : OUString aNameTo_Simplified("ChineseT2S");
494 0 : OUString aNameTo_Traditional("ChineseS2T");
495 0 : lang::Locale aLocale;
496 0 : aLocale.Language = "zh";
497 :
498 0 : if( xContainer->hasByName( aNameTo_Simplified ) )
499 0 : xDictionary_To_Simplified = Reference< linguistic2::XConversionDictionary >(
500 0 : xContainer->getByName( aNameTo_Simplified ), UNO_QUERY );
501 : else
502 : {
503 0 : aLocale.Country = "TW";
504 0 : xDictionary_To_Simplified = Reference< linguistic2::XConversionDictionary >(
505 0 : xDictionaryList->addNewDictionary( aNameTo_Simplified
506 : , aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE
507 0 : ), UNO_QUERY );
508 : }
509 0 : if (xDictionary_To_Simplified.is())
510 0 : xDictionary_To_Simplified->setActive( sal_True );
511 :
512 :
513 0 : if( xContainer->hasByName( aNameTo_Traditional ) )
514 0 : xDictionary_To_Traditional = Reference< linguistic2::XConversionDictionary >(
515 0 : xContainer->getByName( aNameTo_Traditional ), UNO_QUERY );
516 : else
517 : {
518 0 : aLocale.Country = "CN";
519 0 : xDictionary_To_Traditional = Reference< linguistic2::XConversionDictionary >(
520 0 : xDictionaryList->addNewDictionary( aNameTo_Traditional
521 : , aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE
522 0 : ), UNO_QUERY );
523 : }
524 0 : if (xDictionary_To_Traditional.is())
525 0 : xDictionary_To_Traditional->setActive( sal_True );
526 :
527 : }
528 0 : catch(const uno::Exception& )
529 : {
530 : }
531 0 : }
532 : }
533 : }
534 :
535 : //init dictionary controls
536 0 : initDictionaryControl(m_pCT_DictionaryToSimplified, xDictionary_To_Simplified);
537 0 : initDictionaryControl(m_pCT_DictionaryToTraditional, xDictionary_To_Traditional);
538 :
539 0 : updateAfterDirectionChange();
540 :
541 0 : m_pED_Term->SetModifyHdl( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
542 0 : m_pED_Mapping->SetModifyHdl( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
543 0 : m_pLB_Property->SetSelectHdl( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
544 :
545 0 : m_pRB_To_Simplified->SetClickHdl( LINK( this, ChineseDictionaryDialog, DirectionHdl ) );
546 0 : m_pRB_To_Traditional->SetClickHdl( LINK( this, ChineseDictionaryDialog, DirectionHdl ) );
547 :
548 0 : m_pCT_DictionaryToSimplified->SetSelectHdl( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
549 0 : m_pCT_DictionaryToTraditional->SetSelectHdl( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
550 :
551 0 : m_pPB_Add->SetClickHdl( LINK( this, ChineseDictionaryDialog, AddHdl ) );
552 0 : m_pPB_Modify->SetClickHdl( LINK( this, ChineseDictionaryDialog, ModifyHdl ) );
553 0 : m_pPB_Delete->SetClickHdl( LINK( this, ChineseDictionaryDialog, DeleteHdl ) );
554 0 : }
555 :
556 0 : ChineseDictionaryDialog::~ChineseDictionaryDialog()
557 : {
558 0 : disposeOnce();
559 0 : }
560 :
561 0 : void ChineseDictionaryDialog::dispose()
562 : {
563 0 : m_xContext=0;
564 0 : m_pCT_DictionaryToSimplified.disposeAndClear();
565 0 : m_pCT_DictionaryToTraditional.disposeAndClear();
566 0 : m_pRB_To_Simplified.clear();
567 0 : m_pRB_To_Traditional.clear();
568 0 : m_pCB_Reverse.clear();
569 0 : m_pFT_Term.clear();
570 0 : m_pED_Term.clear();
571 0 : m_pFT_Mapping.clear();
572 0 : m_pED_Mapping.clear();
573 0 : m_pFT_Property.clear();
574 0 : m_pLB_Property.clear();
575 0 : mpToSimplifiedContainer.clear();
576 0 : mpToTraditionalContainer.clear();
577 0 : m_pPB_Add.clear();
578 0 : m_pPB_Modify.clear();
579 0 : m_pPB_Delete.clear();
580 0 : ModalDialog::dispose();
581 0 : }
582 :
583 0 : void ChineseDictionaryDialog::setDirectionAndTextConversionOptions( bool bDirectionToSimplified, sal_Int32 nTextConversionOptions /*i18n::TextConversionOption*/ )
584 : {
585 0 : if( bDirectionToSimplified == bool(m_pRB_To_Simplified->IsChecked())
586 0 : && nTextConversionOptions == m_nTextConversionOptions )
587 0 : return;
588 :
589 0 : m_nTextConversionOptions = nTextConversionOptions;
590 :
591 0 : if( bDirectionToSimplified )
592 0 : m_pRB_To_Simplified->Check();
593 : else
594 0 : m_pRB_To_Traditional->Check();
595 0 : updateAfterDirectionChange();
596 : }
597 :
598 0 : IMPL_LINK_NOARG(ChineseDictionaryDialog, DirectionHdl)
599 : {
600 0 : updateAfterDirectionChange();
601 0 : return 0;
602 : }
603 :
604 0 : void ChineseDictionaryDialog::updateAfterDirectionChange()
605 : {
606 0 : Reference< linguistic2::XConversionDictionary > xDictionary(0);
607 :
608 0 : if( m_pRB_To_Simplified->IsChecked() )
609 : {
610 0 : mpToTraditionalContainer->Hide();
611 0 : mpToSimplifiedContainer->Show();
612 0 : xDictionary = m_pCT_DictionaryToSimplified->m_xDictionary;
613 : }
614 : else
615 : {
616 0 : mpToSimplifiedContainer->Hide();
617 0 : mpToTraditionalContainer->Show();
618 0 : xDictionary = m_pCT_DictionaryToTraditional->m_xDictionary;
619 : }
620 :
621 0 : updateButtons();
622 0 : }
623 :
624 0 : IMPL_LINK_NOARG(ChineseDictionaryDialog, EditFieldsHdl)
625 : {
626 0 : updateButtons();
627 0 : return 0;
628 : }
629 0 : IMPL_LINK_NOARG(ChineseDictionaryDialog, MappingSelectHdl)
630 : {
631 0 : DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
632 0 : if(pE)
633 : {
634 0 : m_pED_Term->SetText( pE->m_aTerm );
635 0 : m_pED_Mapping->SetText( pE->m_aMapping );
636 0 : sal_Int16 nPos = pE->m_nConversionPropertyType-1;
637 0 : if( nPos<0 || nPos>=m_pLB_Property->GetEntryCount() )
638 0 : nPos=0;
639 0 : if( m_pLB_Property->GetEntryCount() )
640 0 : m_pLB_Property->SelectEntryPos(nPos);
641 : }
642 :
643 0 : updateButtons();
644 0 : return 0;
645 : }
646 :
647 0 : bool ChineseDictionaryDialog::isEditFieldsHaveContent() const
648 : {
649 0 : return !m_pED_Term->GetText().isEmpty() && !m_pED_Mapping->GetText().isEmpty();
650 : }
651 :
652 0 : bool ChineseDictionaryDialog::isEditFieldsContentEqualsSelectedListContent() const
653 : {
654 0 : DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
655 0 : if( pE )
656 : {
657 0 : if( pE->m_aTerm != OUString( m_pED_Term->GetText() ) )
658 0 : return false;
659 0 : if( pE->m_aMapping != OUString( m_pED_Mapping->GetText() ) )
660 0 : return false;
661 0 : if( pE->m_nConversionPropertyType != m_pLB_Property->GetSelectEntryPos()+1 )
662 0 : return false;
663 0 : return true;
664 : }
665 0 : return false;
666 : }
667 :
668 0 : const DictionaryList& ChineseDictionaryDialog::getActiveDictionary() const
669 : {
670 0 : if( m_pRB_To_Traditional->IsChecked() )
671 0 : return *m_pCT_DictionaryToTraditional;
672 0 : return *m_pCT_DictionaryToSimplified;
673 : }
674 :
675 0 : DictionaryList& ChineseDictionaryDialog::getActiveDictionary()
676 : {
677 0 : if( m_pRB_To_Traditional->IsChecked() )
678 0 : return *m_pCT_DictionaryToTraditional;
679 0 : return *m_pCT_DictionaryToSimplified;
680 : }
681 :
682 0 : const DictionaryList& ChineseDictionaryDialog::getReverseDictionary() const
683 : {
684 0 : if( m_pRB_To_Traditional->IsChecked() )
685 0 : return *m_pCT_DictionaryToSimplified;
686 0 : return *m_pCT_DictionaryToTraditional;
687 : }
688 :
689 0 : DictionaryList& ChineseDictionaryDialog::getReverseDictionary()
690 : {
691 0 : if( m_pRB_To_Traditional->IsChecked() )
692 0 : return *m_pCT_DictionaryToSimplified;
693 0 : return *m_pCT_DictionaryToTraditional;
694 : }
695 :
696 0 : void ChineseDictionaryDialog::updateButtons()
697 : {
698 0 : bool bAdd = isEditFieldsHaveContent() && !getActiveDictionary().hasTerm( m_pED_Term->GetText() );
699 0 : m_pPB_Add->Enable( bAdd );
700 :
701 0 : m_pPB_Delete->Enable( !bAdd && getActiveDictionary().GetSelectedRowCount()>0 );
702 :
703 : // DictionaryEntry* pFirstSelectedEntry = getActiveDictionary().getFirstSelectedEntry();
704 :
705 0 : bool bModify = false;
706 : {
707 0 : DictionaryEntry* pFirstSelectedEntry = getActiveDictionary().getFirstSelectedEntry();
708 0 : bModify = !bAdd && getActiveDictionary().GetSelectedRowCount()==1
709 0 : && pFirstSelectedEntry && pFirstSelectedEntry->m_aTerm.equals( m_pED_Term->GetText() );
710 0 : if( bModify && isEditFieldsContentEqualsSelectedListContent() )
711 0 : bModify = false;
712 : }
713 0 : m_pPB_Modify->Enable( bModify );
714 0 : }
715 :
716 0 : IMPL_LINK_NOARG(ChineseDictionaryDialog, AddHdl)
717 : {
718 0 : if( !isEditFieldsHaveContent() )
719 0 : return 0;
720 :
721 0 : sal_Int16 nConversionPropertyType = m_pLB_Property->GetSelectEntryPos()+1;
722 :
723 0 : getActiveDictionary().addEntry( m_pED_Term->GetText(), m_pED_Mapping->GetText(), nConversionPropertyType );
724 :
725 0 : if( m_pCB_Reverse->IsChecked() )
726 : {
727 0 : getReverseDictionary().deleteEntries( m_pED_Mapping->GetText() );
728 0 : getReverseDictionary().addEntry( m_pED_Mapping->GetText(), m_pED_Term->GetText(), nConversionPropertyType );
729 : }
730 :
731 0 : updateButtons();
732 0 : return 0;
733 : }
734 0 : IMPL_LINK_NOARG(ChineseDictionaryDialog, ModifyHdl)
735 : {
736 0 : OUString aTerm( m_pED_Term->GetText() );
737 0 : OUString aMapping( m_pED_Mapping->GetText() );
738 0 : sal_Int16 nConversionPropertyType = m_pLB_Property->GetSelectEntryPos()+1;
739 :
740 0 : DictionaryList& rActive = getActiveDictionary();
741 0 : DictionaryList& rReverse = getReverseDictionary();
742 :
743 0 : DictionaryEntry* pE = rActive.getFirstSelectedEntry();
744 0 : if( pE && pE->m_aTerm != aTerm )
745 0 : return 0;
746 :
747 0 : if( pE )
748 : {
749 0 : if( pE->m_aMapping != aMapping || pE->m_nConversionPropertyType != nConversionPropertyType )
750 : {
751 0 : if( m_pCB_Reverse->IsChecked() )
752 : {
753 0 : sal_uIntPtr nPos = rReverse.deleteEntries( pE->m_aMapping );
754 0 : nPos = rReverse.deleteEntries( aMapping );
755 0 : rReverse.addEntry( aMapping, aTerm, nConversionPropertyType, nPos );
756 : }
757 :
758 0 : sal_uIntPtr nPos = rActive.deleteEntries( aTerm );
759 0 : rActive.addEntry( aTerm, aMapping, nConversionPropertyType, nPos );
760 : }
761 : }
762 :
763 0 : updateButtons();
764 0 : return 0;
765 : }
766 :
767 0 : IMPL_LINK_NOARG(ChineseDictionaryDialog, DeleteHdl)
768 : {
769 0 : DictionaryList& rActive = getActiveDictionary();
770 0 : DictionaryList& rReverse = getReverseDictionary();
771 :
772 0 : if( rActive.GetSelectedRowCount()>0)
773 : {
774 : DictionaryEntry* pEntry;
775 :
776 0 : OUString aMapping;
777 0 : for( sal_Int32 nN=rActive.GetRowCount(); nN--; )
778 : {
779 0 : if( rActive.IsRowSelected( nN ) )
780 : {
781 0 : pEntry = rActive.getEntryOnPos( nN );
782 0 : if(pEntry)
783 : {
784 0 : aMapping = pEntry->m_aMapping;
785 0 : rActive.deleteEntryOnPos( nN );
786 0 : if( m_pCB_Reverse->IsChecked() )
787 0 : rReverse.deleteEntries( aMapping );
788 : }
789 0 : break;
790 : }
791 0 : }
792 : }
793 :
794 0 : updateButtons();
795 0 : return 0;
796 : }
797 :
798 0 : short ChineseDictionaryDialog::Execute()
799 : {
800 0 : sal_Int32 nTextConversionOptions = m_nTextConversionOptions;
801 0 : if(m_nTextConversionOptions & i18n::TextConversionOption::USE_CHARACTER_VARIANTS )
802 0 : nTextConversionOptions = nTextConversionOptions^i18n::TextConversionOption::USE_CHARACTER_VARIANTS ;
803 :
804 0 : m_pCT_DictionaryToSimplified->refillFromDictionary( nTextConversionOptions );
805 0 : m_pCT_DictionaryToTraditional->refillFromDictionary( m_nTextConversionOptions );
806 :
807 0 : short nRet = ModalDialog::Execute();
808 :
809 0 : if( nRet == RET_OK )
810 : {
811 : //save settings to configuration
812 0 : SvtLinguConfig aLngCfg;
813 0 : Any aAny;
814 0 : aAny <<= m_pCB_Reverse->IsChecked();
815 0 : aLngCfg.SetProperty( OUString( UPN_IS_REVERSE_MAPPING ), aAny );
816 :
817 0 : m_pCT_DictionaryToSimplified->save();
818 0 : m_pCT_DictionaryToTraditional->save();
819 : }
820 :
821 0 : m_pCT_DictionaryToSimplified->deleteAll();
822 0 : m_pCT_DictionaryToTraditional->deleteAll();
823 :
824 0 : return nRet;
825 : }
826 :
827 0 : IMPL_LINK(ChineseDictionaryDialog, HeaderBarClick, HeaderBar*, pHeaderBar)
828 : {
829 0 : sal_uInt16 nId = pHeaderBar->GetCurItemId();
830 0 : HeaderBarItemBits nBits = pHeaderBar->GetItemBits(nId);
831 0 : if( nBits & HeaderBarItemBits::CLICKABLE )
832 : {
833 : //set new arrow positions in headerbar
834 0 : pHeaderBar->SetItemBits( getActiveDictionary().getSortColumn()+1, HEADER_BAR_BITS );
835 0 : if( nBits & HeaderBarItemBits::UPARROW )
836 0 : pHeaderBar->SetItemBits( nId, HEADER_BAR_BITS | HeaderBarItemBits::DOWNARROW );
837 : else
838 0 : pHeaderBar->SetItemBits( nId, HEADER_BAR_BITS | HeaderBarItemBits::UPARROW );
839 :
840 : //sort lists
841 0 : nBits = pHeaderBar->GetItemBits(nId);
842 0 : bool bSortAtoZ = bool(nBits & HeaderBarItemBits::UPARROW);
843 0 : getActiveDictionary().sortByColumn(nId-1,bSortAtoZ);
844 0 : getReverseDictionary().sortByColumn(nId-1,bSortAtoZ);
845 : }
846 :
847 0 : return 0;
848 : }
849 :
850 :
851 0 : } //end namespace
852 :
853 :
854 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|