Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cui / source / dialogs / hangulhanjadlg.cxx
blobfb25df938e176f3da079e0d86df24efeb48c4ae4
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 .
20 #include <hangulhanjadlg.hxx>
21 #include <dialmgr.hxx>
23 #include <helpids.h>
24 #include <strings.hrc>
26 #include <algorithm>
27 #include <sal/log.hxx>
28 #include <osl/diagnose.h>
29 #include <tools/debug.hxx>
30 #include <i18nlangtag/languagetag.hxx>
31 #include <vcl/virdev.hxx>
32 #include <vcl/weldutils.hxx>
33 #include <unotools/lingucfg.hxx>
34 #include <unotools/linguprops.hxx>
35 #include <com/sun/star/lang/NoSupportException.hpp>
36 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
37 #include <com/sun/star/linguistic2/ConversionDirection.hpp>
38 #include <com/sun/star/linguistic2/ConversionDictionaryList.hpp>
39 #include <com/sun/star/i18n/TextConversionOption.hpp>
40 #include <com/sun/star/util/XFlushable.hpp>
42 #include <comphelper/processfactory.hxx>
43 #include <comphelper/string.hxx>
45 #define HHC editeng::HangulHanjaConversion
46 #define LINE_CNT static_cast< sal_uInt16 >(2)
47 #define MAXNUM_SUGGESTIONS 50
50 namespace svx
53 using namespace ::com::sun::star;
54 using namespace css::uno;
55 using namespace css::linguistic2;
56 using namespace css::lang;
57 using namespace css::container;
60 namespace
62 class FontSwitch
64 private:
65 OutputDevice& m_rDev;
67 public:
68 FontSwitch( OutputDevice& _rDev, const vcl::Font& _rTemporaryFont )
69 :m_rDev( _rDev )
71 m_rDev.Push( vcl::PushFlags::FONT );
72 m_rDev.SetFont( _rTemporaryFont );
74 ~FontSwitch() COVERITY_NOEXCEPT_FALSE
76 m_rDev.Pop();
80 /** a class which allows to draw two texts in a pseudo-ruby way (which basically
81 means one text above or below the other, and a little bit smaller)
83 class PseudoRubyText
85 public:
86 enum RubyPosition
88 eAbove, eBelow
91 protected:
92 OUString m_sPrimaryText;
93 OUString m_sSecondaryText;
94 RubyPosition m_ePosition;
96 public:
97 PseudoRubyText();
98 void init( const OUString& rPrimaryText, const OUString& rSecondaryText, const RubyPosition& rPosition );
99 const OUString& getPrimaryText() const { return m_sPrimaryText; }
100 const OUString& getSecondaryText() const { return m_sSecondaryText; }
102 public:
103 void Paint( vcl::RenderContext& _rDevice, const ::tools::Rectangle& _rRect,
104 ::tools::Rectangle* _pPrimaryLocation, ::tools::Rectangle* _pSecondaryLocation );
109 PseudoRubyText::PseudoRubyText()
110 : m_ePosition(eAbove)
114 void PseudoRubyText::init( const OUString& rPrimaryText, const OUString& rSecondaryText, const RubyPosition& rPosition )
116 m_sPrimaryText = rPrimaryText;
117 m_sSecondaryText = rSecondaryText;
118 m_ePosition = rPosition;
122 void PseudoRubyText::Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& _rRect,
123 ::tools::Rectangle* _pPrimaryLocation, ::tools::Rectangle* _pSecondaryLocation )
125 // calculate the text flags for the painting
126 constexpr DrawTextFlags nTextStyle = DrawTextFlags::Mnemonic |
127 DrawTextFlags::Left |
128 DrawTextFlags::VCenter;
130 Size aPlaygroundSize(_rRect.GetSize());
132 // the font for the secondary text:
133 vcl::Font aSmallerFont(rRenderContext.GetFont());
134 // heuristic: 80% of the original size
135 aSmallerFont.SetFontHeight( static_cast<tools::Long>( 0.8 * aSmallerFont.GetFontHeight() ) );
137 // let's calculate the size of our two texts
138 ::tools::Rectangle aPrimaryRect = rRenderContext.GetTextRect( _rRect, m_sPrimaryText, nTextStyle );
139 ::tools::Rectangle aSecondaryRect;
141 FontSwitch aFontRestore(rRenderContext, aSmallerFont);
142 aSecondaryRect = rRenderContext.GetTextRect(_rRect, m_sSecondaryText, nTextStyle);
145 // position these rectangles properly
146 // x-axis:
147 sal_Int32 nCombinedWidth = std::max( aSecondaryRect.GetWidth(), aPrimaryRect.GetWidth() );
148 // the rectangle where both texts will reside is as high as possible, and as wide as the
149 // widest of both text rects
150 aPrimaryRect.SetLeft( _rRect.Left() );
151 aSecondaryRect.SetLeft( aPrimaryRect.Left() );
152 aPrimaryRect.SetRight( _rRect.Left() + nCombinedWidth );
153 aSecondaryRect.SetRight( aPrimaryRect.Right() );
155 // y-axis:
156 sal_Int32 nCombinedHeight = aPrimaryRect.GetHeight() + aSecondaryRect.GetHeight();
157 // align to the top, for the moment
158 aPrimaryRect.Move( 0, _rRect.Top() - aPrimaryRect.Top() );
159 aSecondaryRect.Move( 0, aPrimaryRect.Top() + aPrimaryRect.GetHeight() - aSecondaryRect.Top() );
160 // move the rects to the bottom
161 aPrimaryRect.Move( 0, ( aPlaygroundSize.Height() - nCombinedHeight ) / 2 );
162 aSecondaryRect.Move( 0, ( aPlaygroundSize.Height() - nCombinedHeight ) / 2 );
164 // 'til here, everything we did assumes that the secondary text is painted _below_ the primary
165 // text. If this isn't the case, we need to correct the rectangles
166 if (eAbove == m_ePosition)
168 sal_Int32 nVertDistance = aSecondaryRect.Top() - aPrimaryRect.Top();
169 aSecondaryRect.Move( 0, -nVertDistance );
170 aPrimaryRect.Move( 0, nCombinedHeight - nVertDistance );
173 // now draw the texts
174 // as we already calculated the precise rectangles for the texts, we don't want to
175 // use the alignment flags given - within its rect, every text is centered
176 DrawTextFlags nDrawTextStyle( nTextStyle );
177 nDrawTextStyle &= ~DrawTextFlags( DrawTextFlags::Right | DrawTextFlags::Left | DrawTextFlags::Bottom | DrawTextFlags::Top );
178 nDrawTextStyle |= DrawTextFlags::Center | DrawTextFlags::VCenter;
180 rRenderContext.DrawText( aPrimaryRect, m_sPrimaryText, nDrawTextStyle );
182 FontSwitch aFontRestore(rRenderContext, aSmallerFont);
183 rRenderContext.DrawText( aSecondaryRect, m_sSecondaryText, nDrawTextStyle );
186 // outta here
187 if (_pPrimaryLocation)
188 *_pPrimaryLocation = aPrimaryRect;
189 if (_pSecondaryLocation)
190 *_pSecondaryLocation = aSecondaryRect;
193 class RubyRadioButton
195 public:
196 RubyRadioButton(std::unique_ptr<weld::RadioButton> xControl, std::unique_ptr<weld::Image> xImage);
197 void init(const OUString& rPrimaryText, const OUString& rSecondaryText, const PseudoRubyText::RubyPosition& rPosition);
199 void set_sensitive(bool sensitive)
201 m_xControl->set_sensitive(sensitive);
202 m_xImage->set_sensitive(sensitive);
204 void set_active(bool active) { m_xControl->set_active(active); }
205 bool get_active() const { return m_xControl->get_active(); }
207 void connect_toggled(const Link<weld::Toggleable&, void>& rLink) { m_xControl->connect_toggled(rLink); }
209 private:
210 Size GetOptimalSize() const;
211 void Paint(vcl::RenderContext& rRenderContext);
213 ScopedVclPtr<VirtualDevice> m_xVirDev;
214 std::unique_ptr<weld::RadioButton> m_xControl;
215 std::unique_ptr<weld::Image> m_xImage;
216 PseudoRubyText m_aRubyText;
219 RubyRadioButton::RubyRadioButton(std::unique_ptr<weld::RadioButton> xControl, std::unique_ptr<weld::Image> xImage)
220 : m_xVirDev(xControl->create_virtual_device())
221 , m_xControl(std::move(xControl))
222 , m_xImage(std::move(xImage))
224 // expand the point size of the desired font to the equivalent pixel size
225 weld::SetPointFont(*m_xVirDev, m_xControl->get_font());
228 void RubyRadioButton::init( const OUString& rPrimaryText, const OUString& rSecondaryText, const PseudoRubyText::RubyPosition& rPosition )
230 m_aRubyText.init(rPrimaryText, rSecondaryText, rPosition);
232 m_xVirDev->SetOutputSizePixel(GetOptimalSize());
234 Paint(*m_xVirDev);
236 m_xImage->set_image(m_xVirDev.get());
239 void RubyRadioButton::Paint(vcl::RenderContext& rRenderContext)
241 ::tools::Rectangle aOverallRect(Point(0, 0), rRenderContext.GetOutputSizePixel());
242 // inflate the rect a little bit (because the VCL radio button does the same)
243 ::tools::Rectangle aTextRect( aOverallRect );
244 aTextRect.AdjustLeft( 1 ); aTextRect.AdjustRight( -1 );
245 aTextRect.AdjustTop( 1 ); aTextRect.AdjustBottom( -1 );
247 // paint the ruby text
248 ::tools::Rectangle aPrimaryTextLocation;
249 ::tools::Rectangle aSecondaryTextLocation;
251 m_aRubyText.Paint(rRenderContext, aTextRect, &aPrimaryTextLocation, &aSecondaryTextLocation);
254 Size RubyRadioButton::GetOptimalSize() const
256 vcl::Font aSmallerFont(m_xVirDev->GetFont());
257 aSmallerFont.SetFontHeight( static_cast<tools::Long>( 0.8 * aSmallerFont.GetFontHeight() ) );
258 ::tools::Rectangle rect( Point(), Size( SAL_MAX_INT32, SAL_MAX_INT32 ) );
260 Size aPrimarySize = m_xVirDev->GetTextRect( rect, m_aRubyText.getPrimaryText() ).GetSize();
261 Size aSecondarySize;
263 FontSwitch aFontRestore(*m_xVirDev, aSmallerFont);
264 aSecondarySize = m_xVirDev->GetTextRect( rect, m_aRubyText.getSecondaryText() ).GetSize();
267 Size minimumSize;
268 minimumSize.setHeight( aPrimarySize.Height() + aSecondarySize.Height() + 5 );
269 minimumSize.setWidth(std::max(aPrimarySize.Width(), aSecondarySize.Width()) + 5 );
270 return minimumSize;
273 SuggestionSet::SuggestionSet(std::unique_ptr<weld::ScrolledWindow> xScrolledWindow)
274 : ValueSet(std::move(xScrolledWindow))
279 void SuggestionSet::UserDraw( const UserDrawEvent& rUDEvt )
281 vcl::RenderContext* pDev = rUDEvt.GetRenderContext();
282 ::tools::Rectangle aRect = rUDEvt.GetRect();
283 sal_uInt16 nItemId = rUDEvt.GetItemId();
285 OUString sText = *static_cast< OUString* >( GetItemData( nItemId ) );
286 pDev->DrawText( aRect, sText, DrawTextFlags::Center | DrawTextFlags::VCenter );
289 SuggestionDisplay::SuggestionDisplay(weld::Builder& rBuilder)
290 : m_bDisplayListBox( true )
291 , m_bInSelectionUpdate( false )
292 , m_xValueSet(new SuggestionSet(rBuilder.weld_scrolled_window("scrollwin", true)))
293 , m_xValueSetWin(new weld::CustomWeld(rBuilder, "valueset", *m_xValueSet))
294 , m_xListBox(rBuilder.weld_tree_view("listbox"))
296 m_xValueSet->SetSelectHdl( LINK( this, SuggestionDisplay, SelectSuggestionValueSetHdl ) );
297 m_xListBox->connect_changed( LINK( this, SuggestionDisplay, SelectSuggestionListBoxHdl ) );
299 m_xValueSet->SetLineCount( LINE_CNT );
300 m_xValueSet->SetStyle( m_xValueSet->GetStyle() | WB_ITEMBORDER | WB_VSCROLL );
302 auto nItemWidth = 2 * m_xListBox->get_pixel_size("AU").Width();
303 m_xValueSet->SetItemWidth( nItemWidth );
305 Size aSize(m_xListBox->get_approximate_digit_width() * 42, m_xListBox->get_text_height() * 5);
306 m_xValueSet->set_size_request(aSize.Width(), aSize.Height());
307 m_xListBox->set_size_request(aSize.Width(), aSize.Height());
309 implUpdateDisplay();
312 void SuggestionDisplay::implUpdateDisplay()
314 m_xListBox->set_visible(m_bDisplayListBox);
315 if (!m_bDisplayListBox)
316 m_xValueSetWin->show();
317 else
318 m_xValueSetWin->hide();
321 weld::Widget& SuggestionDisplay::implGetCurrentControl()
323 if (m_bDisplayListBox)
324 return *m_xListBox;
325 return *m_xValueSet->GetDrawingArea();
328 void SuggestionDisplay::DisplayListBox( bool bDisplayListBox )
330 if( m_bDisplayListBox == bDisplayListBox )
331 return;
333 weld::Widget& rOldControl = implGetCurrentControl();
334 bool bHasFocus = rOldControl.has_focus();
336 m_bDisplayListBox = bDisplayListBox;
338 if( bHasFocus )
340 weld::Widget& rNewControl = implGetCurrentControl();
341 rNewControl.grab_focus();
344 implUpdateDisplay();
347 IMPL_LINK_NOARG(SuggestionDisplay, SelectSuggestionValueSetHdl, ValueSet*, void)
349 SelectSuggestionHdl(false);
352 IMPL_LINK_NOARG(SuggestionDisplay, SelectSuggestionListBoxHdl, weld::TreeView&, void)
354 SelectSuggestionHdl(true);
357 void SuggestionDisplay::SelectSuggestionHdl(bool bListBox)
359 if( m_bInSelectionUpdate )
360 return;
362 m_bInSelectionUpdate = true;
363 if (bListBox)
365 sal_uInt16 nPos = m_xListBox->get_selected_index();
366 m_xValueSet->SelectItem( nPos+1 ); //itemid == pos+1 (id 0 has special meaning)
368 else
370 sal_uInt16 nPos = m_xValueSet->GetSelectedItemId()-1; //itemid == pos+1 (id 0 has special meaning)
371 m_xListBox->select(nPos);
373 m_bInSelectionUpdate = false;
374 m_aSelectLink.Call( *this );
377 void SuggestionDisplay::SetSelectHdl( const Link<SuggestionDisplay&,void>& rLink )
379 m_aSelectLink = rLink;
382 void SuggestionDisplay::Clear()
384 m_xListBox->clear();
385 m_xValueSet->Clear();
388 void SuggestionDisplay::InsertEntry( const OUString& rStr )
390 m_xListBox->append_text(rStr);
391 sal_uInt16 nItemId = m_xListBox->n_children(); //itemid == pos+1 (id 0 has special meaning)
392 m_xValueSet->InsertItem( nItemId );
393 OUString* pItemData = new OUString( rStr );
394 m_xValueSet->SetItemData( nItemId, pItemData );
397 void SuggestionDisplay::SelectEntryPos( sal_uInt16 nPos )
399 m_xListBox->select(nPos);
400 m_xValueSet->SelectItem( nPos+1 ); //itemid == pos+1 (id 0 has special meaning)
403 sal_uInt16 SuggestionDisplay::GetEntryCount() const
405 return m_xListBox->n_children();
408 OUString SuggestionDisplay::GetEntry( sal_uInt16 nPos ) const
410 return m_xListBox->get_text( nPos );
413 OUString SuggestionDisplay::GetSelectedEntry() const
415 return m_xListBox->get_selected_text();
418 void SuggestionDisplay::SetHelpIds()
420 m_xValueSet->SetHelpId(HID_HANGULDLG_SUGGESTIONS_GRID);
421 m_xListBox->set_help_id(HID_HANGULDLG_SUGGESTIONS_LIST);
424 HangulHanjaConversionDialog::HangulHanjaConversionDialog(weld::Widget* pParent)
425 : GenericDialogController(pParent, "cui/ui/hangulhanjaconversiondialog.ui", "HangulHanjaConversionDialog")
426 , m_bDocumentMode( true )
427 , m_xFind(m_xBuilder->weld_button("find"))
428 , m_xIgnore(m_xBuilder->weld_button("ignore"))
429 , m_xIgnoreAll(m_xBuilder->weld_button("ignoreall"))
430 , m_xReplace(m_xBuilder->weld_button("replace"))
431 , m_xReplaceAll(m_xBuilder->weld_button("replaceall"))
432 , m_xOptions(m_xBuilder->weld_button("options"))
433 , m_xSuggestions(new SuggestionDisplay(*m_xBuilder))
434 , m_xSimpleConversion(m_xBuilder->weld_radio_button("simpleconversion"))
435 , m_xHangulBracketed(m_xBuilder->weld_radio_button("hangulbracket"))
436 , m_xHanjaBracketed(m_xBuilder->weld_radio_button("hanjabracket"))
437 , m_xWordInput(m_xBuilder->weld_entry("wordinput"))
438 , m_xOriginalWord(m_xBuilder->weld_label("originalword"))
439 , m_xHanjaAbove(new RubyRadioButton(m_xBuilder->weld_radio_button("hanja_above"),
440 m_xBuilder->weld_image("hanja_above_img")))
441 , m_xHanjaBelow(new RubyRadioButton(m_xBuilder->weld_radio_button("hanja_below"),
442 m_xBuilder->weld_image("hanja_below_img")))
443 , m_xHangulAbove(new RubyRadioButton(m_xBuilder->weld_radio_button("hangul_above"),
444 m_xBuilder->weld_image("hangul_above_img")))
445 , m_xHangulBelow(new RubyRadioButton(m_xBuilder->weld_radio_button("hangul_below"),
446 m_xBuilder->weld_image("hangul_below_img")))
447 , m_xHangulOnly(m_xBuilder->weld_check_button("hangulonly"))
448 , m_xHanjaOnly(m_xBuilder->weld_check_button("hanjaonly"))
449 , m_xReplaceByChar(m_xBuilder->weld_check_button("replacebychar"))
451 m_xSuggestions->set_size_request(m_xOriginalWord->get_approximate_digit_width() * 42,
452 m_xOriginalWord->get_text_height() * 5);
454 const OUString sHangul(CuiResId(RID_CUISTR_HANGUL));
455 const OUString sHanja(CuiResId(RID_CUISTR_HANJA));
456 m_xHanjaAbove->init( sHangul, sHanja, PseudoRubyText::eAbove );
457 m_xHanjaBelow->init( sHangul, sHanja, PseudoRubyText::eBelow );
458 m_xHangulAbove->init( sHanja, sHangul, PseudoRubyText::eAbove );
459 m_xHangulBelow->init( sHanja, sHangul, PseudoRubyText::eBelow );
461 m_xWordInput->connect_changed( LINK( this, HangulHanjaConversionDialog, OnSuggestionModified ) );
462 m_xSuggestions->SetSelectHdl( LINK( this, HangulHanjaConversionDialog, OnSuggestionSelected ) );
463 m_xReplaceByChar->connect_toggled( LINK( this, HangulHanjaConversionDialog, ClickByCharacterHdl ) );
464 m_xHangulOnly->connect_toggled( LINK( this, HangulHanjaConversionDialog, OnConversionDirectionClicked ) );
465 m_xHanjaOnly->connect_toggled( LINK( this, HangulHanjaConversionDialog, OnConversionDirectionClicked ) );
466 m_xOptions->connect_clicked(LINK(this, HangulHanjaConversionDialog, OnOption));
468 // initial focus
469 FocusSuggestion( );
471 // initial control values
472 m_xSimpleConversion->set_active(true);
474 m_xSuggestions->SetHelpIds();
477 HangulHanjaConversionDialog::~HangulHanjaConversionDialog()
481 void HangulHanjaConversionDialog::FillSuggestions( const css::uno::Sequence< OUString >& _rSuggestions )
483 m_xSuggestions->Clear();
484 for ( auto const & suggestion : _rSuggestions )
485 m_xSuggestions->InsertEntry( suggestion );
487 // select the first suggestion, and fill in the suggestion edit field
488 OUString sFirstSuggestion;
489 if ( m_xSuggestions->GetEntryCount() )
491 sFirstSuggestion = m_xSuggestions->GetEntry( 0 );
492 m_xSuggestions->SelectEntryPos( 0 );
494 m_xWordInput->set_text( sFirstSuggestion );
495 m_xWordInput->save_value();
496 OnSuggestionModified( *m_xWordInput );
499 void HangulHanjaConversionDialog::SetOptionsChangedHdl(const Link<LinkParamNone*,void>& rHdl)
501 m_aOptionsChangedLink = rHdl;
504 void HangulHanjaConversionDialog::SetIgnoreHdl(const Link<weld::Button&,void>& rHdl)
506 m_xIgnore->connect_clicked(rHdl);
509 void HangulHanjaConversionDialog::SetIgnoreAllHdl(const Link<weld::Button&,void>& rHdl)
511 m_xIgnoreAll->connect_clicked(rHdl);
514 void HangulHanjaConversionDialog::SetChangeHdl(const Link<weld::Button&,void>& rHdl )
516 m_xReplace->connect_clicked(rHdl);
519 void HangulHanjaConversionDialog::SetChangeAllHdl(const Link<weld::Button&,void>& rHdl)
521 m_xReplaceAll->connect_clicked(rHdl);
524 void HangulHanjaConversionDialog::SetFindHdl(const Link<weld::Button&,void>& rHdl)
526 m_xFind->connect_clicked(rHdl);
529 void HangulHanjaConversionDialog::SetConversionFormatChangedHdl( const Link<weld::Toggleable&,void>& rHdl )
531 m_xSimpleConversion->connect_toggled( rHdl );
532 m_xHangulBracketed->connect_toggled( rHdl );
533 m_xHanjaBracketed->connect_toggled( rHdl );
534 m_xHanjaAbove->connect_toggled( rHdl );
535 m_xHanjaBelow->connect_toggled( rHdl );
536 m_xHangulAbove->connect_toggled( rHdl );
537 m_xHangulBelow->connect_toggled( rHdl );
540 void HangulHanjaConversionDialog::SetClickByCharacterHdl( const Link<weld::Toggleable&,void>& _rHdl )
542 m_aClickByCharacterLink = _rHdl;
545 IMPL_LINK_NOARG( HangulHanjaConversionDialog, OnSuggestionSelected, SuggestionDisplay&, void )
547 m_xWordInput->set_text(m_xSuggestions->GetSelectedEntry());
548 OnSuggestionModified( *m_xWordInput );
551 IMPL_LINK_NOARG( HangulHanjaConversionDialog, OnSuggestionModified, weld::Entry&, void )
553 m_xFind->set_sensitive(m_xWordInput->get_value_changed_from_saved());
555 bool bSameLen = m_xWordInput->get_text().getLength() == m_xOriginalWord->get_label().getLength();
556 m_xReplace->set_sensitive( m_bDocumentMode && bSameLen );
557 m_xReplaceAll->set_sensitive( m_bDocumentMode && bSameLen );
560 IMPL_LINK(HangulHanjaConversionDialog, ClickByCharacterHdl, weld::Toggleable&, rBox, void)
562 m_aClickByCharacterLink.Call(rBox);
563 bool bByCharacter = rBox.get_active();
564 m_xSuggestions->DisplayListBox( !bByCharacter );
567 IMPL_LINK(HangulHanjaConversionDialog, OnConversionDirectionClicked, weld::Toggleable&, rBox, void)
569 weld::CheckButton* pOtherBox = nullptr;
570 if (&rBox == m_xHangulOnly.get())
571 pOtherBox = m_xHanjaOnly.get();
572 else
573 pOtherBox = m_xHangulOnly.get();
574 bool bBoxChecked = rBox.get_active();
575 if (bBoxChecked)
576 pOtherBox->set_active(false);
577 pOtherBox->set_sensitive(!bBoxChecked);
580 IMPL_LINK_NOARG(HangulHanjaConversionDialog, OnOption, weld::Button&, void)
582 HangulHanjaOptionsDialog aOptDlg(m_xDialog.get());
583 aOptDlg.run();
584 m_aOptionsChangedLink.Call( nullptr );
587 OUString HangulHanjaConversionDialog::GetCurrentString( ) const
589 return m_xOriginalWord->get_label();
592 void HangulHanjaConversionDialog::FocusSuggestion( )
594 m_xWordInput->grab_focus();
597 void HangulHanjaConversionDialog::SetCurrentString( const OUString& _rNewString,
598 const Sequence< OUString >& _rSuggestions, bool _bOriginatesFromDocument )
600 m_xOriginalWord->set_label(_rNewString);
602 bool bOldDocumentMode = m_bDocumentMode;
603 m_bDocumentMode = _bOriginatesFromDocument; // before FillSuggestions!
604 FillSuggestions( _rSuggestions );
606 m_xIgnoreAll->set_sensitive( m_bDocumentMode );
608 // switch the def button depending if we're working for document text
609 if (bOldDocumentMode == m_bDocumentMode)
610 return;
612 weld::Widget* pOldDefButton = nullptr;
613 weld::Widget* pNewDefButton = nullptr;
614 if (m_bDocumentMode)
616 pOldDefButton = m_xFind.get();
617 pNewDefButton = m_xReplace.get();
619 else
621 pOldDefButton = m_xReplace.get();
622 pNewDefButton = m_xFind.get();
625 m_xDialog->change_default_widget(pOldDefButton, pNewDefButton);
628 OUString HangulHanjaConversionDialog::GetCurrentSuggestion( ) const
630 return m_xWordInput->get_text();
633 void HangulHanjaConversionDialog::SetByCharacter( bool _bByCharacter )
635 m_xReplaceByChar->set_active( _bByCharacter );
636 m_xSuggestions->DisplayListBox( !_bByCharacter );
639 void HangulHanjaConversionDialog::SetConversionDirectionState(
640 bool _bTryBothDirections,
641 HHC::ConversionDirection ePrimaryConversionDirection )
643 // default state: try both direction
644 m_xHangulOnly->set_active( false );
645 m_xHangulOnly->set_sensitive(true);
646 m_xHanjaOnly->set_active( false );
647 m_xHanjaOnly->set_sensitive(true);
649 if (!_bTryBothDirections)
651 weld::CheckButton* pBox = ePrimaryConversionDirection == HHC::eHangulToHanja ?
652 m_xHangulOnly.get() : m_xHanjaOnly.get();
653 pBox->set_active(true);
654 OnConversionDirectionClicked(*pBox);
658 bool HangulHanjaConversionDialog::GetUseBothDirections( ) const
660 return !m_xHangulOnly->get_active() && !m_xHanjaOnly->get_active();
663 HHC::ConversionDirection HangulHanjaConversionDialog::GetDirection(
664 HHC::ConversionDirection eDefaultDirection ) const
666 HHC::ConversionDirection eDirection = eDefaultDirection;
667 if (m_xHangulOnly->get_active() && !m_xHanjaOnly->get_active())
668 eDirection = HHC::eHangulToHanja;
669 else if (!m_xHangulOnly->get_active() && m_xHanjaOnly->get_active())
670 eDirection = HHC::eHanjaToHangul;
671 return eDirection;
674 void HangulHanjaConversionDialog::SetConversionFormat( HHC::ConversionFormat _eType )
676 switch ( _eType )
678 case HHC::eSimpleConversion: m_xSimpleConversion->set_active(true); break;
679 case HHC::eHangulBracketed: m_xHangulBracketed->set_active(true); break;
680 case HHC::eHanjaBracketed: m_xHanjaBracketed->set_active(true); break;
681 case HHC::eRubyHanjaAbove: m_xHanjaAbove->set_active(true); break;
682 case HHC::eRubyHanjaBelow: m_xHanjaBelow->set_active(true); break;
683 case HHC::eRubyHangulAbove: m_xHangulAbove->set_active(true); break;
684 case HHC::eRubyHangulBelow: m_xHangulBelow->set_active(true); break;
685 default:
686 OSL_FAIL( "HangulHanjaConversionDialog::SetConversionFormat: unknown type!" );
690 HHC::ConversionFormat HangulHanjaConversionDialog::GetConversionFormat( ) const
692 if ( m_xSimpleConversion->get_active() )
693 return HHC::eSimpleConversion;
694 if ( m_xHangulBracketed->get_active() )
695 return HHC::eHangulBracketed;
696 if ( m_xHanjaBracketed->get_active() )
697 return HHC::eHanjaBracketed;
698 if ( m_xHanjaAbove->get_active() )
699 return HHC::eRubyHanjaAbove;
700 if ( m_xHanjaBelow->get_active() )
701 return HHC::eRubyHanjaBelow;
702 if ( m_xHangulAbove->get_active() )
703 return HHC::eRubyHangulAbove;
704 if ( m_xHangulBelow->get_active() )
705 return HHC::eRubyHangulBelow;
707 OSL_FAIL( "HangulHanjaConversionDialog::GetConversionFormat: no radio checked?" );
708 return HHC::eSimpleConversion;
711 void HangulHanjaConversionDialog::EnableRubySupport( bool bVal )
713 m_xHanjaAbove->set_sensitive( bVal );
714 m_xHanjaBelow->set_sensitive( bVal );
715 m_xHangulAbove->set_sensitive( bVal );
716 m_xHangulBelow->set_sensitive( bVal );
719 void HangulHanjaOptionsDialog::Init()
721 if( !m_xConversionDictionaryList.is() )
723 m_xConversionDictionaryList = ConversionDictionaryList::create( ::comphelper::getProcessComponentContext() );
726 m_aDictList.clear();
727 m_xDictsLB->clear();
729 Reference< XNameContainer > xNameCont = m_xConversionDictionaryList->getDictionaryContainer();
730 if( xNameCont.is() )
732 Sequence< OUString > aDictNames( xNameCont->getElementNames() );
734 const OUString* pDic = aDictNames.getConstArray();
735 sal_Int32 nCount = aDictNames.getLength();
737 sal_Int32 i;
738 for( i = 0 ; i < nCount ; ++i )
740 Any aAny( xNameCont->getByName( pDic[ i ] ) );
741 Reference< XConversionDictionary > xDic;
742 if( ( aAny >>= xDic ) && xDic.is() )
744 if( LANGUAGE_KOREAN == LanguageTag( xDic->getLocale() ).getLanguageType() )
746 m_aDictList.push_back( xDic );
747 AddDict( xDic->getName(), xDic->isActive() );
752 if (m_xDictsLB->n_children())
753 m_xDictsLB->select(0);
756 IMPL_LINK_NOARG(HangulHanjaOptionsDialog, OkHdl, weld::Button&, void)
758 sal_uInt32 nCnt = m_aDictList.size();
759 sal_uInt32 n = 0;
760 sal_uInt32 nActiveDics = 0;
761 Sequence< OUString > aActiveDics;
763 aActiveDics.realloc( nCnt );
764 OUString* pActActiveDic = aActiveDics.getArray();
766 while( nCnt )
768 Reference< XConversionDictionary > xDict = m_aDictList[ n ];
770 DBG_ASSERT( xDict.is(), "-HangulHanjaOptionsDialog::OkHdl(): someone is evaporated..." );
772 bool bActive = m_xDictsLB->get_toggle(n) == TRISTATE_TRUE;
773 xDict->setActive( bActive );
774 Reference< util::XFlushable > xFlush( xDict, uno::UNO_QUERY );
775 if( xFlush.is() )
776 xFlush->flush();
778 if( bActive )
780 pActActiveDic[ nActiveDics ] = xDict->getName();
781 ++nActiveDics;
784 ++n;
785 --nCnt;
788 // save configuration
789 aActiveDics.realloc( nActiveDics );
790 Any aTmp;
791 SvtLinguConfig aLngCfg;
792 aTmp <<= aActiveDics;
793 aLngCfg.SetProperty( UPH_ACTIVE_CONVERSION_DICTIONARIES, aTmp );
795 aTmp <<= m_xIgnorepostCB->get_active();
796 aLngCfg.SetProperty( UPH_IS_IGNORE_POST_POSITIONAL_WORD, aTmp );
798 aTmp <<= m_xShowrecentlyfirstCB->get_active();
799 aLngCfg.SetProperty( UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST, aTmp );
801 aTmp <<= m_xAutoreplaceuniqueCB->get_active();
802 aLngCfg.SetProperty( UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES, aTmp );
804 m_xDialog->response(RET_OK);
807 IMPL_LINK_NOARG(HangulHanjaOptionsDialog, DictsLB_SelectHdl, weld::TreeView&, void)
809 bool bSel = m_xDictsLB->get_selected_index() != -1;
811 m_xEditPB->set_sensitive(bSel);
812 m_xDeletePB->set_sensitive(bSel);
815 IMPL_LINK_NOARG(HangulHanjaOptionsDialog, NewDictHdl, weld::Button&, void)
817 OUString aName;
818 HangulHanjaNewDictDialog aNewDlg(m_xDialog.get());
819 aNewDlg.run();
820 if (!aNewDlg.GetName(aName))
821 return;
823 if( !m_xConversionDictionaryList.is() )
824 return;
828 Reference< XConversionDictionary > xDic =
829 m_xConversionDictionaryList->addNewDictionary( aName, LanguageTag::convertToLocale( LANGUAGE_KOREAN ), ConversionDictionaryType::HANGUL_HANJA );
831 if( xDic.is() )
833 //adapt local caches:
834 m_aDictList.push_back( xDic );
835 AddDict( xDic->getName(), xDic->isActive() );
838 catch( const ElementExistException& )
841 catch( const NoSupportException& )
846 IMPL_LINK_NOARG(HangulHanjaOptionsDialog, EditDictHdl, weld::Button&, void)
848 int nEntry = m_xDictsLB->get_selected_index();
849 DBG_ASSERT(nEntry != -1, "+HangulHanjaEditDictDialog::EditDictHdl(): call of edit should not be possible with no selection!");
850 if (nEntry != -1)
852 HangulHanjaEditDictDialog aEdDlg(m_xDialog.get(), m_aDictList, nEntry);
853 aEdDlg.run();
857 IMPL_LINK_NOARG(HangulHanjaOptionsDialog, DeleteDictHdl, weld::Button&, void)
859 int nSelPos = m_xDictsLB->get_selected_index();
860 if (nSelPos == -1)
861 return;
863 Reference< XConversionDictionary > xDic( m_aDictList[ nSelPos ] );
864 if( !(m_xConversionDictionaryList.is() && xDic.is()) )
865 return;
867 Reference< XNameContainer > xNameCont = m_xConversionDictionaryList->getDictionaryContainer();
868 if( !xNameCont.is() )
869 return;
873 xNameCont->removeByName( xDic->getName() );
875 //adapt local caches:
876 m_aDictList.erase(m_aDictList.begin()+nSelPos );
877 m_xDictsLB->remove(nSelPos);
879 catch( const ElementExistException& )
882 catch( const NoSupportException& )
887 HangulHanjaOptionsDialog::HangulHanjaOptionsDialog(weld::Window* pParent)
888 : GenericDialogController(pParent, "cui/ui/hangulhanjaoptdialog.ui", "HangulHanjaOptDialog")
889 , m_xDictsLB(m_xBuilder->weld_tree_view("dicts"))
890 , m_xIgnorepostCB(m_xBuilder->weld_check_button("ignorepost"))
891 , m_xShowrecentlyfirstCB(m_xBuilder->weld_check_button("showrecentfirst"))
892 , m_xAutoreplaceuniqueCB(m_xBuilder->weld_check_button("autoreplaceunique"))
893 , m_xNewPB(m_xBuilder->weld_button("new"))
894 , m_xEditPB(m_xBuilder->weld_button("edit"))
895 , m_xDeletePB(m_xBuilder->weld_button("delete"))
896 , m_xOkPB(m_xBuilder->weld_button("ok"))
898 m_xDictsLB->set_size_request(m_xDictsLB->get_approximate_digit_width() * 32,
899 m_xDictsLB->get_height_rows(5));
901 m_xDictsLB->enable_toggle_buttons(weld::ColumnToggleType::Check);
903 m_xDictsLB->connect_changed( LINK( this, HangulHanjaOptionsDialog, DictsLB_SelectHdl ) );
905 m_xOkPB->connect_clicked( LINK( this, HangulHanjaOptionsDialog, OkHdl ) );
906 m_xNewPB->connect_clicked( LINK( this, HangulHanjaOptionsDialog, NewDictHdl ) );
907 m_xEditPB->connect_clicked( LINK( this, HangulHanjaOptionsDialog, EditDictHdl ) );
908 m_xDeletePB->connect_clicked( LINK( this, HangulHanjaOptionsDialog, DeleteDictHdl ) );
910 SvtLinguConfig aLngCfg;
911 Any aTmp;
912 bool bVal = bool();
913 aTmp = aLngCfg.GetProperty( UPH_IS_IGNORE_POST_POSITIONAL_WORD );
914 if( aTmp >>= bVal )
915 m_xIgnorepostCB->set_active( bVal );
917 aTmp = aLngCfg.GetProperty( UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST );
918 if( aTmp >>= bVal )
919 m_xShowrecentlyfirstCB->set_active( bVal );
921 aTmp = aLngCfg.GetProperty( UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES );
922 if( aTmp >>= bVal )
923 m_xAutoreplaceuniqueCB->set_active( bVal );
925 Init();
928 HangulHanjaOptionsDialog::~HangulHanjaOptionsDialog()
932 void HangulHanjaOptionsDialog::AddDict(const OUString& rName, bool bChecked)
934 m_xDictsLB->append();
935 int nRow = m_xDictsLB->n_children() - 1;
936 m_xDictsLB->set_toggle(nRow, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
937 m_xDictsLB->set_text(nRow, rName, 0);
938 m_xDictsLB->set_id(nRow, rName);
941 IMPL_LINK_NOARG(HangulHanjaNewDictDialog, OKHdl, weld::Button&, void)
943 OUString aName(comphelper::string::stripEnd(m_xDictNameED->get_text(), ' '));
945 m_bEntered = !aName.isEmpty();
946 if (m_bEntered)
947 m_xDictNameED->set_text(aName); // do this in case of trailing chars have been deleted
949 m_xDialog->response(RET_OK);
952 IMPL_LINK_NOARG(HangulHanjaNewDictDialog, ModifyHdl, weld::Entry&, void)
954 OUString aName(comphelper::string::stripEnd(m_xDictNameED->get_text(), ' '));
956 m_xOkBtn->set_sensitive(!aName.isEmpty());
959 HangulHanjaNewDictDialog::HangulHanjaNewDictDialog(weld::Window* pParent)
960 : GenericDialogController(pParent, "cui/ui/hangulhanjaadddialog.ui", "HangulHanjaAddDialog")
961 , m_bEntered(false)
962 , m_xOkBtn(m_xBuilder->weld_button("ok"))
963 , m_xDictNameED(m_xBuilder->weld_entry("entry"))
965 m_xOkBtn->connect_clicked( LINK( this, HangulHanjaNewDictDialog, OKHdl ) );
966 m_xDictNameED->connect_changed( LINK( this, HangulHanjaNewDictDialog, ModifyHdl ) );
969 HangulHanjaNewDictDialog::~HangulHanjaNewDictDialog()
973 bool HangulHanjaNewDictDialog::GetName( OUString& _rRetName ) const
975 if( m_bEntered )
976 _rRetName = comphelper::string::stripEnd(m_xDictNameED->get_text(), ' ');
978 return m_bEntered;
981 class SuggestionList
983 private:
984 protected:
985 std::vector<OUString> m_vElements;
986 sal_uInt16 m_nNumOfEntries;
987 // index of the internal iterator, used for First() and Next() methods
988 sal_uInt16 m_nAct;
990 const OUString* Next_();
991 public:
992 SuggestionList();
993 ~SuggestionList();
995 void Set( const OUString& _rElement, sal_uInt16 _nNumOfElement );
996 void Reset( sal_uInt16 _nNumOfElement );
997 const OUString & Get( sal_uInt16 _nNumOfElement ) const;
998 void Clear();
1000 const OUString* First();
1001 const OUString* Next();
1003 sal_uInt16 GetCount() const { return m_nNumOfEntries; }
1006 SuggestionList::SuggestionList() :
1007 m_vElements(MAXNUM_SUGGESTIONS)
1009 m_nAct = m_nNumOfEntries = 0;
1012 SuggestionList::~SuggestionList()
1014 Clear();
1017 void SuggestionList::Set( const OUString& _rElement, sal_uInt16 _nNumOfElement )
1019 m_vElements[_nNumOfElement] = _rElement;
1020 ++m_nNumOfEntries;
1023 void SuggestionList::Reset( sal_uInt16 _nNumOfElement )
1025 m_vElements[_nNumOfElement].clear();
1026 --m_nNumOfEntries;
1029 const OUString& SuggestionList::Get( sal_uInt16 _nNumOfElement ) const
1031 return m_vElements[_nNumOfElement];
1034 void SuggestionList::Clear()
1036 if( m_nNumOfEntries )
1038 for (auto & vElement : m_vElements)
1039 vElement.clear();
1040 m_nNumOfEntries = m_nAct = 0;
1044 const OUString* SuggestionList::Next_()
1046 while( m_nAct < m_vElements.size() )
1048 auto & s = m_vElements[ m_nAct ];
1049 if (!s.isEmpty())
1050 return &s;
1051 ++m_nAct;
1054 return nullptr;
1057 const OUString* SuggestionList::First()
1059 m_nAct = 0;
1060 return Next_();
1063 const OUString* SuggestionList::Next()
1065 const OUString* pRet;
1067 if( m_nAct < m_nNumOfEntries )
1069 ++m_nAct;
1070 pRet = Next_();
1072 else
1073 pRet = nullptr;
1075 return pRet;
1079 bool SuggestionEdit::ShouldScroll( bool _bUp ) const
1081 bool bRet = false;
1083 if( _bUp )
1085 if( !m_pPrev )
1086 bRet = m_pScrollBar->vadjustment_get_value() > m_pScrollBar->vadjustment_get_lower();
1088 else
1090 if( !m_pNext )
1091 bRet = m_pScrollBar->vadjustment_get_value() < ( m_pScrollBar->vadjustment_get_upper() - 4 );
1094 return bRet;
1097 void SuggestionEdit::DoJump( bool _bUp )
1099 m_pScrollBar->vadjustment_set_value( m_pScrollBar->vadjustment_get_value() + ( _bUp? -1 : 1 ) );
1100 m_pParent->UpdateScrollbar();
1103 SuggestionEdit::SuggestionEdit(std::unique_ptr<weld::Entry> xEntry, HangulHanjaEditDictDialog* pParent)
1104 : m_pParent(pParent)
1105 , m_pPrev(nullptr)
1106 , m_pNext(nullptr)
1107 , m_pScrollBar(nullptr)
1108 , m_xEntry(std::move(xEntry))
1110 m_xEntry->connect_key_press(LINK(this, SuggestionEdit, KeyInputHdl));
1113 IMPL_LINK(SuggestionEdit, KeyInputHdl, const KeyEvent&, rKEvt, bool)
1115 bool bHandled = false;
1117 const vcl::KeyCode& rKeyCode = rKEvt.GetKeyCode();
1118 sal_uInt16 nMod = rKeyCode.GetModifier();
1119 sal_uInt16 nCode = rKeyCode.GetCode();
1120 if( nCode == KEY_TAB && ( !nMod || KEY_SHIFT == nMod ) )
1122 bool bUp = KEY_SHIFT == nMod;
1123 if( ShouldScroll( bUp ) )
1125 DoJump( bUp );
1126 m_xEntry->select_region(0, -1);
1127 // Tab-travel doesn't really happen, so emulate it by setting a selection manually
1128 bHandled = true;
1131 else if( KEY_UP == nCode || KEY_DOWN == nCode )
1133 bool bUp = KEY_UP == nCode;
1134 if( ShouldScroll( bUp ) )
1136 DoJump( bUp );
1137 bHandled = true;
1139 else if( bUp )
1141 if( m_pPrev )
1143 m_pPrev->grab_focus();
1144 bHandled = true;
1147 else if( m_pNext )
1149 m_pNext->grab_focus();
1150 bHandled = true;
1154 return bHandled;
1157 void SuggestionEdit::init(weld::ScrolledWindow* pScrollBar, SuggestionEdit* pPrev, SuggestionEdit* pNext)
1159 m_pScrollBar = pScrollBar;
1160 m_pPrev = pPrev;
1161 m_pNext = pNext;
1164 namespace
1166 bool GetConversions( const Reference< XConversionDictionary >& _xDict,
1167 const OUString& _rOrg,
1168 Sequence< OUString >& _rEntries )
1170 bool bRet = false;
1171 if( _xDict.is() && !_rOrg.isEmpty() )
1175 _rEntries = _xDict->getConversions( _rOrg,
1177 _rOrg.getLength(),
1178 ConversionDirection_FROM_LEFT,
1179 css::i18n::TextConversionOption::NONE );
1180 bRet = _rEntries.hasElements();
1182 catch( const IllegalArgumentException& )
1187 return bRet;
1191 IMPL_LINK_NOARG( HangulHanjaEditDictDialog, ScrollHdl, weld::ScrolledWindow&, void )
1193 UpdateScrollbar();
1196 IMPL_LINK_NOARG( HangulHanjaEditDictDialog, OriginalModifyHdl, weld::ComboBox&, void )
1198 m_bModifiedOriginal = true;
1199 m_aOriginal = comphelper::string::stripEnd( m_xOriginalLB->get_active_text(), ' ' );
1201 UpdateSuggestions();
1202 UpdateButtonStates();
1205 IMPL_LINK( HangulHanjaEditDictDialog, EditModifyHdl1, weld::Entry&, rEdit, void )
1207 EditModify( &rEdit, 0 );
1210 IMPL_LINK( HangulHanjaEditDictDialog, EditModifyHdl2, weld::Entry&, rEdit, void )
1212 EditModify( &rEdit, 1 );
1215 IMPL_LINK( HangulHanjaEditDictDialog, EditModifyHdl3, weld::Entry&, rEdit, void )
1217 EditModify( &rEdit, 2 );
1220 IMPL_LINK( HangulHanjaEditDictDialog, EditModifyHdl4, weld::Entry&, rEdit, void )
1222 EditModify( &rEdit, 3 );
1225 IMPL_LINK_NOARG( HangulHanjaEditDictDialog, BookLBSelectHdl, weld::ComboBox&, void )
1227 InitEditDictDialog( m_xBookLB->get_active() );
1230 IMPL_LINK_NOARG( HangulHanjaEditDictDialog, NewPBPushHdl, weld::Button&, void )
1232 DBG_ASSERT( m_xSuggestions, "-HangulHanjaEditDictDialog::NewPBPushHdl(): no suggestions... search in hell..." );
1233 Reference< XConversionDictionary > xDict = m_rDictList[ m_nCurrentDict ];
1234 if( xDict.is() && m_xSuggestions )
1236 //delete old entry
1237 bool bRemovedSomething = DeleteEntryFromDictionary( xDict );
1239 OUString aLeft( m_aOriginal );
1240 const OUString* pRight = m_xSuggestions->First();
1241 bool bAddedSomething = false;
1242 while( pRight )
1246 //add new entry
1247 xDict->addEntry( aLeft, *pRight );
1248 bAddedSomething = true;
1250 catch( const IllegalArgumentException& )
1253 catch( const ElementExistException& )
1257 pRight = m_xSuggestions->Next();
1260 if( bAddedSomething || bRemovedSomething )
1261 InitEditDictDialog( m_nCurrentDict );
1263 else
1265 SAL_INFO( "cui.dialogs", "dictionary faded away..." );
1269 bool HangulHanjaEditDictDialog::DeleteEntryFromDictionary( const Reference< XConversionDictionary >& xDict )
1271 bool bRemovedSomething = false;
1272 if( xDict.is() )
1274 OUString aOrg( m_aOriginal );
1275 Sequence< OUString > aEntries;
1276 GetConversions( xDict, m_aOriginal, aEntries );
1278 sal_uInt32 n = aEntries.getLength();
1279 OUString* pEntry = aEntries.getArray();
1280 while( n )
1284 xDict->removeEntry( aOrg, *pEntry );
1285 bRemovedSomething = true;
1287 catch( const NoSuchElementException& )
1288 { // can not be...
1291 ++pEntry;
1292 --n;
1295 return bRemovedSomething;
1298 IMPL_LINK_NOARG( HangulHanjaEditDictDialog, DeletePBPushHdl, weld::Button&, void )
1300 if( DeleteEntryFromDictionary( m_rDictList[ m_nCurrentDict ] ) )
1302 m_aOriginal.clear();
1303 m_bModifiedOriginal = true;
1304 InitEditDictDialog( m_nCurrentDict );
1308 void HangulHanjaEditDictDialog::InitEditDictDialog( sal_uInt32 nSelDict )
1310 if( m_xSuggestions )
1311 m_xSuggestions->Clear();
1313 if( m_nCurrentDict != nSelDict )
1315 m_nCurrentDict = nSelDict;
1316 m_aOriginal.clear();
1317 m_bModifiedOriginal = true;
1320 UpdateOriginalLB();
1322 m_xOriginalLB->set_entry_text( !m_aOriginal.isEmpty() ? m_aOriginal : m_aEditHintText);
1323 m_xOriginalLB->select_entry_region(0, -1);
1324 m_xOriginalLB->grab_focus();
1326 UpdateSuggestions();
1327 UpdateButtonStates();
1330 void HangulHanjaEditDictDialog::UpdateOriginalLB()
1332 m_xOriginalLB->clear();
1333 Reference< XConversionDictionary > xDict = m_rDictList[ m_nCurrentDict ];
1334 if( xDict.is() )
1336 Sequence< OUString > aEntries = xDict->getConversionEntries( ConversionDirection_FROM_LEFT );
1337 sal_uInt32 n = aEntries.getLength();
1338 OUString* pEntry = aEntries.getArray();
1339 while( n )
1341 m_xOriginalLB->append_text( *pEntry );
1343 ++pEntry;
1344 --n;
1347 else
1349 SAL_INFO( "cui.dialogs", "dictionary faded away..." );
1353 void HangulHanjaEditDictDialog::UpdateButtonStates()
1355 bool bHaveValidOriginalString = !m_aOriginal.isEmpty() && m_aOriginal != m_aEditHintText;
1356 bool bNew = bHaveValidOriginalString && m_xSuggestions && m_xSuggestions->GetCount() > 0;
1357 bNew = bNew && ( m_bModifiedSuggestions || m_bModifiedOriginal );
1359 m_xNewPB->set_sensitive( bNew );
1360 m_xDeletePB->set_sensitive(!m_bModifiedOriginal && bHaveValidOriginalString);
1363 void HangulHanjaEditDictDialog::UpdateSuggestions()
1365 Sequence< OUString > aEntries;
1366 bool bFound = GetConversions( m_rDictList[ m_nCurrentDict ], m_aOriginal, aEntries );
1367 if( bFound )
1369 m_bModifiedOriginal = false;
1371 if( m_xSuggestions )
1372 m_xSuggestions->Clear();
1374 //fill found entries into boxes
1375 sal_uInt32 nCnt = aEntries.getLength();
1376 if( nCnt )
1378 if( !m_xSuggestions )
1379 m_xSuggestions.reset(new SuggestionList);
1381 const OUString* pSugg = aEntries.getConstArray();
1382 sal_uInt32 n = 0;
1383 while( nCnt )
1385 m_xSuggestions->Set( pSugg[ n ], sal_uInt16( n ) );
1386 ++n;
1387 --nCnt;
1390 m_bModifiedSuggestions = false;
1393 m_xScrollSB->vadjustment_set_value( 0 );
1394 UpdateScrollbar(); // will force edits to be filled new
1397 void HangulHanjaEditDictDialog::SetEditText(SuggestionEdit& rEdit, sal_uInt16 nEntryNum)
1399 OUString aStr;
1400 if( m_xSuggestions )
1402 aStr = m_xSuggestions->Get(nEntryNum);
1405 rEdit.set_text(aStr);
1408 void HangulHanjaEditDictDialog::EditModify(const weld::Entry* pEdit, sal_uInt8 _nEntryOffset)
1410 m_bModifiedSuggestions = true;
1412 OUString aTxt( pEdit->get_text() );
1413 sal_uInt16 nEntryNum = m_nTopPos + _nEntryOffset;
1414 if( aTxt.isEmpty() )
1416 //reset suggestion
1417 if( m_xSuggestions )
1418 m_xSuggestions->Reset( nEntryNum );
1420 else
1422 //set suggestion
1423 if( !m_xSuggestions )
1424 m_xSuggestions.reset(new SuggestionList);
1425 m_xSuggestions->Set( aTxt, nEntryNum );
1428 UpdateButtonStates();
1431 HangulHanjaEditDictDialog::HangulHanjaEditDictDialog(weld::Window* pParent, HHDictList& _rDictList, sal_uInt32 nSelDict)
1432 : GenericDialogController(pParent, "cui/ui/hangulhanjaeditdictdialog.ui", "HangulHanjaEditDictDialog")
1433 , m_aEditHintText ( CuiResId(RID_CUISTR_EDITHINT) )
1434 , m_rDictList ( _rDictList )
1435 , m_nCurrentDict ( 0xFFFFFFFF )
1436 , m_nTopPos ( 0 )
1437 , m_bModifiedSuggestions ( false )
1438 , m_bModifiedOriginal ( false )
1439 , m_xBookLB(m_xBuilder->weld_combo_box("book"))
1440 , m_xOriginalLB(m_xBuilder->weld_combo_box("original"))
1441 , m_xEdit1(new SuggestionEdit(m_xBuilder->weld_entry("edit1"), this))
1442 , m_xEdit2(new SuggestionEdit(m_xBuilder->weld_entry("edit2"), this))
1443 , m_xEdit3(new SuggestionEdit(m_xBuilder->weld_entry("edit3"), this))
1444 , m_xEdit4(new SuggestionEdit(m_xBuilder->weld_entry("edit4"), this))
1445 , m_xContents(m_xBuilder->weld_widget("box"))
1446 , m_xScrollSB(m_xBuilder->weld_scrolled_window("scrollbar", true))
1447 , m_xNewPB(m_xBuilder->weld_button("new"))
1448 , m_xDeletePB(m_xBuilder->weld_button("delete"))
1450 Size aSize(m_xContents->get_preferred_size());
1451 m_xScrollSB->set_size_request(-1, aSize.Height());
1453 m_xEdit1->init( m_xScrollSB.get(), nullptr, m_xEdit2.get() );
1454 m_xEdit2->init( m_xScrollSB.get(), m_xEdit1.get(), m_xEdit3.get() );
1455 m_xEdit3->init( m_xScrollSB.get(), m_xEdit2.get(), m_xEdit4.get() );
1456 m_xEdit4->init( m_xScrollSB.get(), m_xEdit3.get(), nullptr );
1458 m_xOriginalLB->connect_changed( LINK( this, HangulHanjaEditDictDialog, OriginalModifyHdl ) );
1460 m_xNewPB->connect_clicked( LINK( this, HangulHanjaEditDictDialog, NewPBPushHdl ) );
1461 m_xNewPB->set_sensitive( false );
1463 m_xDeletePB->connect_clicked( LINK( this, HangulHanjaEditDictDialog, DeletePBPushHdl ) );
1464 m_xDeletePB->set_sensitive( false );
1466 static_assert(MAXNUM_SUGGESTIONS >= 5, "number of suggestions should not under-run the value of 5");
1468 // 4 here, because we have 4 edits / page
1469 m_xScrollSB->vadjustment_configure(0, 0, MAXNUM_SUGGESTIONS, 1, 4, 4);
1470 m_xScrollSB->connect_vadjustment_changed(LINK(this, HangulHanjaEditDictDialog, ScrollHdl));
1472 m_xEdit1->connect_changed( LINK( this, HangulHanjaEditDictDialog, EditModifyHdl1 ) );
1473 m_xEdit2->connect_changed( LINK( this, HangulHanjaEditDictDialog, EditModifyHdl2 ) );
1474 m_xEdit3->connect_changed( LINK( this, HangulHanjaEditDictDialog, EditModifyHdl3 ) );
1475 m_xEdit4->connect_changed( LINK( this, HangulHanjaEditDictDialog, EditModifyHdl4 ) );
1477 m_xBookLB->connect_changed( LINK( this, HangulHanjaEditDictDialog, BookLBSelectHdl ) );
1478 sal_uInt32 nDictCnt = m_rDictList.size();
1479 for( sal_uInt32 n = 0 ; n < nDictCnt ; ++n )
1481 Reference< XConversionDictionary > xDic( m_rDictList[n] );
1482 OUString aName;
1483 if( xDic.is() )
1484 aName = xDic->getName();
1485 m_xBookLB->append_text( aName );
1487 m_xBookLB->set_active(nSelDict);
1489 InitEditDictDialog(nSelDict);
1492 HangulHanjaEditDictDialog::~HangulHanjaEditDictDialog()
1496 void HangulHanjaEditDictDialog::UpdateScrollbar()
1498 sal_uInt16 nPos = m_xScrollSB->vadjustment_get_value();
1499 m_nTopPos = nPos;
1501 SetEditText( *m_xEdit1, nPos++ );
1502 SetEditText( *m_xEdit2, nPos++ );
1503 SetEditText( *m_xEdit3, nPos++ );
1504 SetEditText( *m_xEdit4, nPos );
1508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */