1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 #include <memory>
20 #include <sal/config.h>
21 #include <tools/debug.hxx>
22 
23 #include <osl/diagnose.h>
24 #include <o3tl/any.hxx>
25 #include <svx/rubydialog.hxx>
26 #include <sfx2/app.hxx>
27 #include <sfx2/dispatch.hxx>
28 #include <sfx2/sfxsids.hrc>
29 #include <sfx2/viewfrm.hxx>
30 #include <sfx2/viewsh.hxx>
31 #include <svl/eitem.hxx>
32 #include <com/sun/star/frame/XController.hpp>
33 #include <com/sun/star/style/XStyle.hpp>
34 #include <com/sun/star/text/XRubySelection.hpp>
35 #include <com/sun/star/beans/PropertyValues.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/beans/XPropertySetInfo.hpp>
38 #include <com/sun/star/container/XNameContainer.hpp>
39 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
40 #include <com/sun/star/text/RubyAdjust.hpp>
41 #include <com/sun/star/view/XSelectionChangeListener.hpp>
42 #include <com/sun/star/view/XSelectionSupplier.hpp>
43 #include <cppuhelper/implbase.hxx>
44 #include <svtools/colorcfg.hxx>
45 #include <vcl/event.hxx>
46 #include <vcl/settings.hxx>
47 #include <vcl/svapp.hxx>
48 
49 using namespace css::uno;
50 using namespace css::frame;
51 using namespace css::text;
52 using namespace css::beans;
53 using namespace css::style;
54 using namespace css::view;
55 using namespace css::lang;
56 using namespace css::container;
57 
58 SFX_IMPL_CHILDWINDOW(SvxRubyChildWindow, SID_RUBY_DIALOG);
59 
60 namespace
61 {
62 
63 static const sal_Char cRubyBaseText[] = "RubyBaseText";
64 static const sal_Char cRubyText[] = "RubyText";
65 static const sal_Char cRubyAdjust[] = "RubyAdjust";
66 static const sal_Char cRubyPosition[] = "RubyPosition";
67 static const sal_Char cRubyCharStyleName[] = "RubyCharStyleName";
68 
69 } // end anonymous namespace
70 
SvxRubyChildWindow(vcl::Window * _pParent,sal_uInt16 nId,SfxBindings * pBindings,SfxChildWinInfo const * pInfo)71 SvxRubyChildWindow::SvxRubyChildWindow(vcl::Window* _pParent, sal_uInt16 nId,SfxBindings* pBindings, SfxChildWinInfo const * pInfo)
72     : SfxChildWindow(_pParent, nId)
73 {
74     std::shared_ptr<SvxRubyDialog> xDlg(new SvxRubyDialog(pBindings, this, _pParent->GetFrameWeld()));
75     SetController(xDlg);
76     xDlg->Initialize(pInfo);
77 }
78 
GetInfo() const79 SfxChildWinInfo SvxRubyChildWindow::GetInfo() const
80 {
81     return SfxChildWindow::GetInfo();
82 }
83 
84 class SvxRubyData_Impl : public cppu::WeakImplHelper<css::view::XSelectionChangeListener>
85 {
86     Reference<XModel> xModel;
87     Reference<XRubySelection> xSelection;
88     Sequence<PropertyValues> aRubyValues;
89     Reference<XController> xController;
90     bool bHasSelectionChanged;
91 
92 public:
93     SvxRubyData_Impl();
94     virtual ~SvxRubyData_Impl() override;
95 
96     void SetController(const Reference<XController>& xCtrl);
GetModel()97     Reference<XModel> const & GetModel()
98     {
99         if (!xController.is())
100             xModel = nullptr;
101         else
102             xModel = xController->getModel();
103         return xModel;
104     }
HasSelectionChanged() const105     bool HasSelectionChanged() const
106     {
107         return bHasSelectionChanged;
108     }
GetRubySelection()109     Reference<XRubySelection> const & GetRubySelection()
110     {
111         xSelection.set(xController, UNO_QUERY);
112         return xSelection;
113     }
UpdateRubyValues()114     void UpdateRubyValues()
115     {
116         if (!xSelection.is())
117             aRubyValues.realloc(0);
118         else
119             aRubyValues = xSelection->getRubyList(false);
120         bHasSelectionChanged = false;
121     }
GetRubyValues()122     Sequence<PropertyValues>& GetRubyValues()
123     {
124         return aRubyValues;
125     }
126     void AssertOneEntry();
127 
128     virtual void SAL_CALL selectionChanged(const css::lang::EventObject& aEvent) override;
129     virtual void SAL_CALL disposing( const css::lang::EventObject& Source) override;
130 
131 };
132 
SvxRubyData_Impl()133 SvxRubyData_Impl::SvxRubyData_Impl()
134     : bHasSelectionChanged(false)
135 {
136 }
137 
~SvxRubyData_Impl()138 SvxRubyData_Impl::~SvxRubyData_Impl()
139 {
140 }
141 
SetController(const Reference<XController> & xCtrl)142 void SvxRubyData_Impl::SetController(const Reference<XController>& xCtrl)
143 {
144     if (xCtrl.get() != xController.get())
145     {
146         try
147         {
148             Reference<XSelectionSupplier> xSelSupp(xController, UNO_QUERY);
149             if (xSelSupp.is())
150                 xSelSupp->removeSelectionChangeListener(this);
151 
152             bHasSelectionChanged = true;
153             xController = xCtrl;
154             xSelSupp.set(xController, UNO_QUERY);
155             if (xSelSupp.is())
156                 xSelSupp->addSelectionChangeListener(this);
157         }
158         catch (const Exception&)
159         {
160         }
161     }
162 }
163 
selectionChanged(const EventObject &)164 void SvxRubyData_Impl::selectionChanged(const EventObject& )
165 {
166     bHasSelectionChanged = true;
167 }
168 
disposing(const EventObject &)169 void SvxRubyData_Impl::disposing(const EventObject&)
170 {
171     try
172     {
173         Reference<XSelectionSupplier> xSelSupp(xController, UNO_QUERY);
174         if (xSelSupp.is())
175             xSelSupp->removeSelectionChangeListener(this);
176     }
177     catch (const Exception&)
178     {
179     }
180     xController = nullptr;
181 }
182 
AssertOneEntry()183 void SvxRubyData_Impl::AssertOneEntry()
184 {
185     //create one entry
186     if (!aRubyValues.hasElements())
187     {
188         aRubyValues.realloc(1);
189         Sequence<PropertyValue>& rValues = aRubyValues.getArray()[0];
190         rValues.realloc(5);
191         PropertyValue* pValues = rValues.getArray();
192         pValues[0].Name = cRubyBaseText;
193         pValues[1].Name = cRubyText;
194         pValues[2].Name = cRubyAdjust;
195         pValues[3].Name = cRubyPosition;
196         pValues[4].Name = cRubyCharStyleName;
197     }
198 }
199 
SvxRubyDialog(SfxBindings * pBind,SfxChildWindow * pCW,weld::Window * pParent)200 SvxRubyDialog::SvxRubyDialog(SfxBindings* pBind, SfxChildWindow* pCW, weld::Window* pParent)
201     : SfxModelessDialogController(pBind, pCW, pParent,
202                         "svx/ui/asianphoneticguidedialog.ui", "AsianPhoneticGuideDialog")
203     , nLastPos(0)
204     , nCurrentEdit(0)
205     , bModified(false)
206     , pBindings(pBind)
207     , m_pImpl( new SvxRubyData_Impl )
208     , m_xLeftFT(m_xBuilder->weld_label("basetextft"))
209     , m_xRightFT(m_xBuilder->weld_label("rubytextft"))
210     , m_xLeft1ED(m_xBuilder->weld_entry("Left1ED"))
211     , m_xRight1ED(m_xBuilder->weld_entry("Right1ED"))
212     , m_xLeft2ED(m_xBuilder->weld_entry("Left2ED"))
213     , m_xRight2ED(m_xBuilder->weld_entry("Right2ED"))
214     , m_xLeft3ED(m_xBuilder->weld_entry("Left3ED"))
215     , m_xRight3ED(m_xBuilder->weld_entry("Right3ED"))
216     , m_xLeft4ED(m_xBuilder->weld_entry("Left4ED"))
217     , m_xRight4ED(m_xBuilder->weld_entry("Right4ED"))
218     , m_xScrolledWindow(m_xBuilder->weld_scrolled_window("scrolledwindow"))
219     , m_xAdjustLB(m_xBuilder->weld_combo_box("adjustlb"))
220     , m_xPositionLB(m_xBuilder->weld_combo_box("positionlb"))
221     , m_xCharStyleFT(m_xBuilder->weld_label("styleft"))
222     , m_xCharStyleLB(m_xBuilder->weld_combo_box("stylelb"))
223     , m_xStylistPB(m_xBuilder->weld_button("styles"))
224     , m_xApplyPB(m_xBuilder->weld_button("ok"))
225     , m_xClosePB(m_xBuilder->weld_button("cancel"))
226     , m_xContentArea(m_xDialog->weld_content_area())
227     , m_xGrid(m_xBuilder->weld_widget("grid"))
228     , m_xPreviewWin(new RubyPreview)
229     , m_xPreview(new weld::CustomWeld(*m_xBuilder, "preview", *m_xPreviewWin))
230 {
231     m_xCharStyleLB->make_sorted();
232     m_xPreviewWin->setRubyDialog(this);
233     m_xScrolledWindow->set_user_managed_scrolling();
234     m_xScrolledWindow->set_size_request(-1, m_xGrid->get_preferred_size().Height());
235     m_xScrolledWindow->set_vpolicy(VclPolicyType::NEVER);
236 
237     aEditArr[0] = m_xLeft1ED.get(); aEditArr[1] = m_xRight1ED.get();
238     aEditArr[2] = m_xLeft2ED.get(); aEditArr[3] = m_xRight2ED.get();
239     aEditArr[4] = m_xLeft3ED.get(); aEditArr[5] = m_xRight3ED.get();
240     aEditArr[6] = m_xLeft4ED.get(); aEditArr[7] = m_xRight4ED.get();
241 
242     m_xApplyPB->connect_clicked(LINK(this, SvxRubyDialog, ApplyHdl_Impl));
243     m_xClosePB->connect_clicked(LINK(this, SvxRubyDialog, CloseHdl_Impl));
244     m_xStylistPB->connect_clicked(LINK(this, SvxRubyDialog, StylistHdl_Impl));
245     m_xAdjustLB->connect_changed(LINK(this, SvxRubyDialog, AdjustHdl_Impl));
246     m_xPositionLB->connect_changed(LINK(this, SvxRubyDialog, PositionHdl_Impl));
247     m_xCharStyleLB->connect_changed(LINK(this, SvxRubyDialog, CharStyleHdl_Impl));
248 
249     Link<weld::ScrolledWindow&, void> aScrLk(LINK(this, SvxRubyDialog, ScrollHdl_Impl));
250     m_xScrolledWindow->connect_vadjustment_changed(aScrLk);
251 
252     Link<weld::Entry&,void> aEditLk(LINK(this, SvxRubyDialog, EditModifyHdl_Impl));
253     Link<weld::Widget&,void> aFocusLk(LINK(this, SvxRubyDialog, EditFocusHdl_Impl));
254     Link<const KeyEvent&,bool> aKeyUpDownLk(LINK(this, SvxRubyDialog, KeyUpDownHdl_Impl));
255     Link<const KeyEvent&,bool> aKeyTabUpDownLk(LINK(this, SvxRubyDialog, KeyUpDownTabHdl_Impl));
256     for (sal_uInt16 i = 0; i < 8; i++)
257     {
258         aEditArr[i]->connect_changed(aEditLk);
259         aEditArr[i]->connect_focus_in(aFocusLk);
260         if (!i || 7 == i)
261             aEditArr[i]->connect_key_press(aKeyTabUpDownLk);
262         else
263             aEditArr[i]->connect_key_press(aKeyUpDownLk);
264     }
265 }
266 
~SvxRubyDialog()267 SvxRubyDialog::~SvxRubyDialog()
268 {
269     ClearCharStyleList();
270     EventObject aEvent;
271     m_pImpl->disposing(aEvent);
272 }
273 
ClearCharStyleList()274 void SvxRubyDialog::ClearCharStyleList()
275 {
276     m_xCharStyleLB->clear();
277 }
278 
Close()279 void SvxRubyDialog::Close()
280 {
281     if (IsClosing())
282         return;
283     SfxViewFrame* pViewFrame = SfxViewFrame::Current();
284     if (pViewFrame)
285         pViewFrame->ToggleChildWindow(SID_RUBY_DIALOG);
286 }
287 
Activate()288 void SvxRubyDialog::Activate()
289 {
290     SfxModelessDialogController::Activate();
291     std::unique_ptr<SfxPoolItem> pState;
292     //get selection from current view frame
293     SfxViewFrame* pCurFrm = SfxViewFrame::Current();
294     Reference< XController > xCtrl = pCurFrm->GetFrame().GetController();
295     m_pImpl->SetController(xCtrl);
296     if (m_pImpl->HasSelectionChanged())
297     {
298 
299         Reference< XRubySelection > xRubySel = m_pImpl->GetRubySelection();
300         m_pImpl->UpdateRubyValues();
301         EnableControls(xRubySel.is());
302         if (xRubySel.is())
303         {
304             Reference< XModel > xModel = m_pImpl->GetModel();
305             const OUString sCharStyleSelect = m_xCharStyleLB->get_active_text();
306             ClearCharStyleList();
307             Reference<XStyleFamiliesSupplier> xSupplier(xModel, UNO_QUERY);
308             if (xSupplier.is())
309             {
310                 try
311                 {
312                     Reference<XNameAccess> xFam = xSupplier->getStyleFamilies();
313                     Any aChar = xFam->getByName("CharacterStyles");
314                     Reference<XNameContainer> xChar;
315                     aChar >>= xChar;
316                     Reference<XIndexAccess> xCharIdx(xChar, UNO_QUERY);
317                     if (xCharIdx.is())
318                     {
319                         OUString sUIName("DisplayName");
320                         for (sal_Int32 nStyle = 0; nStyle < xCharIdx->getCount(); nStyle++)
321                         {
322                             Any aStyle = xCharIdx->getByIndex(nStyle);
323                             Reference<XStyle> xStyle;
324                             aStyle >>= xStyle;
325                             Reference<XPropertySet> xPrSet(xStyle, UNO_QUERY);
326                             OUString sName, sCoreName;
327                             if (xPrSet.is())
328                             {
329                                 Reference<XPropertySetInfo> xInfo = xPrSet->getPropertySetInfo();
330                                 if (xInfo->hasPropertyByName(sUIName))
331                                 {
332                                     Any aName = xPrSet->getPropertyValue(sUIName);
333                                     aName >>= sName;
334                                 }
335                             }
336                             if (xStyle.is())
337                             {
338                                 sCoreName = xStyle->getName();
339                                 if (sName.isEmpty())
340                                     sName = sCoreName;
341                             }
342                             if (!sName.isEmpty())
343                             {
344                                 m_xCharStyleLB->append(sCoreName, sName);
345 
346                             }
347                         }
348                     }
349                 }
350                 catch (const Exception&)
351                 {
352                     OSL_FAIL("exception in style access");
353                 }
354                 if (!sCharStyleSelect.isEmpty())
355                     m_xCharStyleLB->set_active_text(sCharStyleSelect);
356             }
357             m_xCharStyleLB->set_sensitive(xSupplier.is());
358             m_xCharStyleFT->set_sensitive(xSupplier.is());
359         }
360         Update();
361         m_xPreviewWin->Invalidate();
362     }
363 }
364 
SetRubyText(sal_Int32 nPos,weld::Entry & rLeft,weld::Entry & rRight)365 void SvxRubyDialog::SetRubyText(sal_Int32 nPos, weld::Entry& rLeft, weld::Entry& rRight)
366 {
367     OUString sLeft, sRight;
368     const Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
369     bool bEnable = aRubyValues.getLength() > nPos;
370     if (bEnable)
371     {
372         const Sequence<PropertyValue> aProps = aRubyValues.getConstArray()[nPos];
373         for (const PropertyValue& rProp : aProps)
374         {
375             if (rProp.Name == cRubyBaseText)
376                 rProp.Value >>= sLeft;
377             else if (rProp.Name == cRubyText)
378                 rProp.Value >>= sRight;
379         }
380     }
381     else if (!nPos)
382     {
383         bEnable = true;
384     }
385     rLeft.set_sensitive(bEnable);
386     rRight.set_sensitive(bEnable);
387     rLeft.set_text(sLeft);
388     rRight.set_text(sRight);
389     rLeft.save_value();
390     rRight.save_value();
391 }
392 
GetRubyText()393 void SvxRubyDialog::GetRubyText()
394 {
395     long nTempLastPos = GetLastPos();
396     for (int i = 0; i < 8; i+=2)
397     {
398         if (aEditArr[i]->get_sensitive() &&
399             (aEditArr[i]->get_value_changed_from_saved() ||
400              aEditArr[i + 1]->get_value_changed_from_saved()))
401         {
402             Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
403             DBG_ASSERT(aRubyValues.getLength() > (i / 2 + nTempLastPos), "wrong index" );
404             SetModified(true);
405             Sequence<PropertyValue>& rProps = aRubyValues.getArray()[i / 2 + nTempLastPos];
406             for (PropertyValue & propVal : rProps)
407             {
408                 if (propVal.Name == cRubyBaseText)
409                     propVal.Value <<= aEditArr[i]->get_text();
410                 else if (propVal.Name == cRubyText)
411                     propVal.Value <<= aEditArr[i + 1]->get_text();
412             }
413         }
414     }
415 }
416 
Update()417 void SvxRubyDialog::Update()
418 {
419     const Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
420     sal_Int32 nLen = aRubyValues.getLength();
421     m_xScrolledWindow->vadjustment_configure(0, 0, !nLen ? 1 : nLen, 1, 4, 4);
422     if (nLen > 4)
423         m_xScrolledWindow->set_vpolicy(VclPolicyType::ALWAYS);
424     else
425         m_xScrolledWindow->set_vpolicy(VclPolicyType::NEVER);
426     SetLastPos(0);
427     SetModified(false);
428 
429     sal_Int16 nAdjust = -1;
430     sal_Int16 nPosition = -1;
431     OUString sCharStyleName, sTmp;
432     bool bCharStyleEqual = true;
433     for (sal_Int32 nRuby = 0; nRuby < nLen; nRuby++)
434     {
435         const Sequence<PropertyValue> &rProps = aRubyValues.getConstArray()[nRuby];
436         for (const PropertyValue& rProp : rProps)
437         {
438             if (nAdjust > -2 && rProp.Name == cRubyAdjust)
439             {
440                 sal_Int16 nTmp = sal_Int16();
441                 rProp.Value >>= nTmp;
442                 if (!nRuby)
443                     nAdjust = nTmp;
444                 else if(nAdjust != nTmp)
445                     nAdjust = -2;
446             }
447             if (nPosition > -2 && rProp.Name == cRubyPosition )
448             {
449                 sal_Int16 nTmp = sal_Int16();
450                 rProp.Value >>= nTmp;
451                 if (!nRuby)
452                     nPosition = nTmp;
453                 else if(nPosition != nTmp)
454                     nPosition = -2;
455             }
456             if (bCharStyleEqual && rProp.Name == cRubyCharStyleName)
457             {
458                 rProp.Value >>= sTmp;
459                 if (!nRuby)
460                     sCharStyleName = sTmp;
461                 else if (sCharStyleName != sTmp)
462                     bCharStyleEqual = false;
463             }
464         }
465     }
466     if (!nLen)
467     {
468         //enable selection if the ruby list is empty
469         nAdjust = 0;
470         nPosition = 0;
471     }
472     if (nAdjust > -1)
473         m_xAdjustLB->set_active(nAdjust);
474     else
475         m_xAdjustLB->set_active(-1);
476     if (nPosition > -1)
477         m_xPositionLB->set_active(nPosition);
478     if (!nLen || (bCharStyleEqual && sCharStyleName.isEmpty()))
479         sCharStyleName = "Rubies";
480     if (!sCharStyleName.isEmpty())
481     {
482         for (int i = 0, nEntryCount = m_xCharStyleLB->get_count(); i < nEntryCount; i++)
483         {
484             OUString sCoreName = m_xCharStyleLB->get_id(i);
485             if (sCharStyleName == sCoreName)
486             {
487                 m_xCharStyleLB->set_active(i);
488                 break;
489             }
490         }
491     }
492     else
493         m_xCharStyleLB->set_active(-1);
494 
495     ScrollHdl_Impl(*m_xScrolledWindow);
496 }
497 
GetCurrentText(OUString & rBase,OUString & rRuby)498 void SvxRubyDialog::GetCurrentText(OUString& rBase, OUString& rRuby)
499 {
500     rBase = aEditArr[nCurrentEdit * 2]->get_text();
501     rRuby = aEditArr[nCurrentEdit * 2 + 1]->get_text();
502 }
503 
IMPL_LINK(SvxRubyDialog,ScrollHdl_Impl,weld::ScrolledWindow &,rScroll,void)504 IMPL_LINK(SvxRubyDialog, ScrollHdl_Impl, weld::ScrolledWindow&, rScroll, void)
505 {
506     int nPos = rScroll.vadjustment_get_value();
507     if (GetLastPos() != nPos)
508     {
509         GetRubyText();
510     }
511     SetRubyText(nPos++, *m_xLeft1ED, *m_xRight1ED);
512     SetRubyText(nPos++, *m_xLeft2ED, *m_xRight2ED);
513     SetRubyText(nPos++, *m_xLeft3ED, *m_xRight3ED);
514     SetRubyText(nPos, *m_xLeft4ED, *m_xRight4ED);
515     SetLastPos(nPos - 3);
516     m_xPreviewWin->Invalidate();
517 }
518 
IMPL_LINK_NOARG(SvxRubyDialog,ApplyHdl_Impl,weld::Button &,void)519 IMPL_LINK_NOARG(SvxRubyDialog, ApplyHdl_Impl, weld::Button&, void)
520 {
521     const Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
522     if (!aRubyValues.hasElements())
523     {
524         AssertOneEntry();
525         PositionHdl_Impl(*m_xPositionLB);
526         AdjustHdl_Impl(*m_xAdjustLB);
527         CharStyleHdl_Impl(*m_xCharStyleLB);
528     }
529     GetRubyText();
530     //reset all edit fields - SaveValue is called
531     ScrollHdl_Impl(*m_xScrolledWindow);
532 
533     Reference<XRubySelection> xSelection = m_pImpl->GetRubySelection();
534     if (IsModified() && xSelection.is())
535     {
536         try
537         {
538             xSelection->setRubyList(aRubyValues, false);
539         }
540         catch (const Exception&)
541         {
542             OSL_FAIL("Exception caught");
543         }
544     }
545 }
546 
IMPL_LINK_NOARG(SvxRubyDialog,CloseHdl_Impl,weld::Button &,void)547 IMPL_LINK_NOARG(SvxRubyDialog, CloseHdl_Impl, weld::Button&, void)
548 {
549     Close();
550 }
551 
IMPL_LINK_NOARG(SvxRubyDialog,StylistHdl_Impl,weld::Button &,void)552 IMPL_LINK_NOARG(SvxRubyDialog, StylistHdl_Impl, weld::Button&, void)
553 {
554     std::unique_ptr<SfxPoolItem> pState;
555     SfxItemState eState = pBindings->QueryState(SID_STYLE_DESIGNER, pState);
556     if (eState <= SfxItemState::SET || !pState || !static_cast<SfxBoolItem*>(pState.get())->GetValue())
557     {
558         pBindings->GetDispatcher()->Execute(SID_STYLE_DESIGNER,
559                                             SfxCallMode::ASYNCHRON | SfxCallMode::RECORD);
560     }
561 }
562 
IMPL_LINK(SvxRubyDialog,AdjustHdl_Impl,weld::ComboBox &,rBox,void)563 IMPL_LINK(SvxRubyDialog, AdjustHdl_Impl, weld::ComboBox&, rBox, void)
564 {
565     AssertOneEntry();
566     sal_Int16 nAdjust = rBox.get_active();
567     Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
568     for (PropertyValues & rProps : aRubyValues)
569     {
570         for (PropertyValue & propVal : rProps)
571         {
572             if (propVal.Name == cRubyAdjust)
573                 propVal.Value <<= nAdjust;
574         }
575         SetModified(true);
576     }
577     m_xPreviewWin->Invalidate();
578 }
579 
IMPL_LINK(SvxRubyDialog,PositionHdl_Impl,weld::ComboBox &,rBox,void)580 IMPL_LINK(SvxRubyDialog, PositionHdl_Impl, weld::ComboBox&, rBox, void)
581 {
582     AssertOneEntry();
583     sal_Int16 nPosition = rBox.get_active();
584     Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
585     for (PropertyValues & rProps : aRubyValues)
586     {
587         for (PropertyValue & propVal : rProps)
588         {
589             if (propVal.Name == cRubyPosition)
590                 propVal.Value <<= nPosition;
591         }
592         SetModified(true);
593     }
594     m_xPreviewWin->Invalidate();
595 }
596 
IMPL_LINK_NOARG(SvxRubyDialog,CharStyleHdl_Impl,weld::ComboBox &,void)597 IMPL_LINK_NOARG(SvxRubyDialog, CharStyleHdl_Impl, weld::ComboBox&, void)
598 {
599     AssertOneEntry();
600     OUString sStyleName;
601     if (m_xCharStyleLB->get_active() != -1)
602         sStyleName = m_xCharStyleLB->get_active_id();
603     Sequence<PropertyValues>& aRubyValues = m_pImpl->GetRubyValues();
604     for (PropertyValues & rProps : aRubyValues)
605     {
606         for (PropertyValue & propVal : rProps)
607         {
608             if (propVal.Name == cRubyCharStyleName)
609             {
610                 propVal.Value <<= sStyleName;
611             }
612         }
613         SetModified(true);
614     }
615 }
616 
IMPL_LINK(SvxRubyDialog,EditFocusHdl_Impl,weld::Widget &,rEdit,void)617 IMPL_LINK(SvxRubyDialog, EditFocusHdl_Impl, weld::Widget&, rEdit, void)
618 {
619     for (sal_uInt16 i = 0; i < 8; i++)
620     {
621         if (&rEdit == aEditArr[i])
622         {
623             nCurrentEdit = i / 2;
624             break;
625         }
626     }
627     m_xPreviewWin->Invalidate();
628 }
629 
IMPL_LINK(SvxRubyDialog,EditModifyHdl_Impl,weld::Entry &,rEdit,void)630 IMPL_LINK(SvxRubyDialog, EditModifyHdl_Impl, weld::Entry&, rEdit, void)
631 {
632     EditFocusHdl_Impl(rEdit);
633 }
634 
EditScrollHdl_Impl(sal_Int32 nParam)635 bool SvxRubyDialog::EditScrollHdl_Impl(sal_Int32 nParam)
636 {
637     bool bRet = false;
638     //scroll forward
639     if (nParam > 0 && (aEditArr[7]->has_focus() || aEditArr[6]->has_focus() ))
640     {
641         if (m_xScrolledWindow->vadjustment_get_upper() >
642             m_xScrolledWindow->vadjustment_get_value() + m_xScrolledWindow->vadjustment_get_page_size())
643         {
644             m_xScrolledWindow->vadjustment_set_value(m_xScrolledWindow->vadjustment_get_value() + 1);
645             aEditArr[6]->grab_focus();
646             bRet = true;
647         }
648     }
649     //scroll backward
650     else if (m_xScrolledWindow->vadjustment_get_value() && (aEditArr[0]->has_focus()||aEditArr[1]->has_focus()) )
651     {
652         m_xScrolledWindow->vadjustment_set_value(m_xScrolledWindow->vadjustment_get_value() - 1);
653         aEditArr[1]->grab_focus();
654         bRet = true;
655     }
656     if (bRet)
657         ScrollHdl_Impl(*m_xScrolledWindow);
658     return bRet;
659 }
660 
EditJumpHdl_Impl(sal_Int32 nParam)661 bool SvxRubyDialog::EditJumpHdl_Impl(sal_Int32 nParam)
662 {
663     bool bHandled = false;
664     sal_uInt16 nIndex = USHRT_MAX;
665     for (sal_uInt16 i = 0; i < 8; i++)
666     {
667         if(aEditArr[i]->has_focus())
668             nIndex = i;
669     }
670     if (nIndex < 8)
671     {
672         if (nParam > 0)
673         {
674             if (nIndex < 6)
675                 aEditArr[nIndex + 2]->grab_focus();
676             else if( EditScrollHdl_Impl(nParam))
677                 aEditArr[nIndex]->grab_focus();
678         }
679         else
680         {
681             if (nIndex > 1)
682                 aEditArr[nIndex - 2]->grab_focus();
683             else if( EditScrollHdl_Impl(nParam))
684                 aEditArr[nIndex]->grab_focus();
685         }
686         bHandled = true;
687     }
688     return bHandled;
689 }
690 
AssertOneEntry()691 void SvxRubyDialog::AssertOneEntry()
692 {
693     m_pImpl->AssertOneEntry();
694 }
695 
EnableControls(bool bEnable)696 void SvxRubyDialog::EnableControls(bool bEnable)
697 {
698     m_xContentArea->set_sensitive(bEnable);
699     m_xApplyPB->set_sensitive(bEnable);
700 }
701 
RubyPreview()702 RubyPreview::RubyPreview()
703     : m_pParentDlg(nullptr)
704 {
705 }
706 
~RubyPreview()707 RubyPreview::~RubyPreview()
708 {
709 }
710 
Paint(vcl::RenderContext & rRenderContext,const tools::Rectangle &)711 void RubyPreview::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
712 {
713     rRenderContext.Push(PushFlags::ALL);
714 
715     rRenderContext.SetMapMode(MapMode(MapUnit::MapTwip));
716 
717     Size aWinSize = rRenderContext.GetOutputSize();
718 
719     const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
720     svtools::ColorConfig aColorConfig;
721 
722     Color aNewTextColor(aColorConfig.GetColorValue(svtools::FONTCOLOR).nColor);
723     Color aNewFillColor(rStyleSettings.GetWindowColor());
724 
725     vcl::Font aFont = rRenderContext.GetFont();
726     aFont.SetFontHeight(aWinSize.Height() / 4);
727     aFont.SetFillColor(aNewFillColor);
728     aFont.SetColor(aNewTextColor);
729     rRenderContext.SetFont(aFont);
730 
731     tools::Rectangle aRect(Point(0, 0), aWinSize);
732     rRenderContext.SetLineColor();
733     rRenderContext.SetFillColor(aFont.GetFillColor());
734     rRenderContext.DrawRect(aRect);
735 
736     OUString sBaseText, sRubyText;
737     m_pParentDlg->GetCurrentText(sBaseText, sRubyText);
738 
739     long nTextHeight = rRenderContext.GetTextHeight();
740     long nBaseWidth = rRenderContext.GetTextWidth(sBaseText);
741 
742     vcl::Font aRubyFont(aFont);
743     aRubyFont.SetFontHeight(aRubyFont.GetFontHeight() * 70 / 100);
744     rRenderContext.SetFont(aRubyFont);
745     long nRubyWidth = rRenderContext.GetTextWidth(sRubyText);
746     rRenderContext.SetFont(aFont);
747 
748     RubyAdjust nAdjust = static_cast<RubyAdjust>(m_pParentDlg->m_xAdjustLB->get_active());
749     //use center if no adjustment is available
750     if (nAdjust > RubyAdjust_INDENT_BLOCK)
751         nAdjust = RubyAdjust_CENTER;
752 
753     //which part is stretched ?
754     bool bRubyStretch = nBaseWidth >= nRubyWidth;
755 
756     long nCenter = aWinSize.Width() / 2;
757     long nHalfWidth = std::max( nBaseWidth, nRubyWidth ) /2;
758     long nLeftStart = nCenter - nHalfWidth;
759     long nRightEnd = nCenter + nHalfWidth;
760 
761     // Default values for TOP or no selection
762     long nYRuby = aWinSize.Height() / 4 - nTextHeight / 2;
763     long nYBase = aWinSize.Height() * 3 / 4 - nTextHeight / 2;
764 
765     sal_Int16 nRubyPos = m_pParentDlg->m_xPositionLB->get_active();
766     if ( nRubyPos == 1 )    // BOTTOM
767     {
768         long nTmp = nYRuby;
769         nYRuby = nYBase;
770         nYBase = nTmp;
771     }
772     else if ( nRubyPos == 2 ) // RIGHT ( vertically )
773     {
774         // Align the ruby text and base text to the vertical center.
775         nYBase =  ( aWinSize.Height() - nTextHeight ) / 2;
776         nYRuby = ( aWinSize.Height() - nRubyWidth ) / 2;
777 
778         // Align the ruby text at the right side of the base text
779         nAdjust = RubyAdjust_RIGHT;
780         nHalfWidth = nBaseWidth / 2;
781         nLeftStart = nCenter - nHalfWidth;
782         nRightEnd = nCenter + nHalfWidth + nRubyWidth + nTextHeight;
783         // Render base text first, then render ruby text on the right.
784         bRubyStretch = true;
785 
786         aRubyFont.SetVertical(true);
787         aRubyFont.SetOrientation(2700);
788     }
789 
790     long nYOutput;
791     long nOutTextWidth;
792     OUString sOutputText;
793 
794     if (bRubyStretch)
795     {
796         rRenderContext.DrawText(Point(nLeftStart , nYBase),  sBaseText);
797         nYOutput = nYRuby;
798         sOutputText = sRubyText;
799         nOutTextWidth = nRubyWidth;
800         rRenderContext.SetFont(aRubyFont);
801     }
802     else
803     {
804         rRenderContext.SetFont(aRubyFont);
805         rRenderContext.DrawText(Point(nLeftStart , nYRuby),  sRubyText);
806         nYOutput = nYBase;
807         sOutputText = sBaseText;
808         nOutTextWidth = nBaseWidth;
809         rRenderContext.SetFont(aFont);
810     }
811 
812     switch (nAdjust)
813     {
814         case RubyAdjust_LEFT:
815             rRenderContext.DrawText(Point(nLeftStart , nYOutput),  sOutputText);
816         break;
817         case RubyAdjust_RIGHT:
818             rRenderContext.DrawText(Point(nRightEnd - nOutTextWidth, nYOutput), sOutputText);
819         break;
820         case RubyAdjust_INDENT_BLOCK:
821         {
822             long nCharWidth = rRenderContext.GetTextWidth("X");
823             if (nOutTextWidth < (nRightEnd - nLeftStart - nCharWidth))
824             {
825                 nCharWidth /= 2;
826                 nLeftStart += nCharWidth;
827                 nRightEnd -= nCharWidth;
828             }
829             [[fallthrough]];
830         }
831         case RubyAdjust_BLOCK:
832         {
833             if (sOutputText.getLength() > 1)
834             {
835                 sal_Int32 nCount = sOutputText.getLength();
836                 long nSpace = ((nRightEnd - nLeftStart) - rRenderContext.GetTextWidth(sOutputText)) / (nCount - 1);
837                 for (sal_Int32 i = 0; i < nCount; i++)
838                 {
839                     OUString sChar(sOutputText[i]);
840                     rRenderContext.DrawText(Point(nLeftStart , nYOutput),  sChar);
841                     long nCharWidth = rRenderContext.GetTextWidth(sChar);
842                     nLeftStart += nCharWidth + nSpace;
843                 }
844                 break;
845             }
846             [[fallthrough]];
847         }
848         case RubyAdjust_CENTER:
849             rRenderContext.DrawText(Point(nCenter - nOutTextWidth / 2 , nYOutput),  sOutputText);
850         break;
851         default: break;
852     }
853     rRenderContext.Pop();
854 }
855 
SetDrawingArea(weld::DrawingArea * pDrawingArea)856 void RubyPreview::SetDrawingArea(weld::DrawingArea* pDrawingArea)
857 {
858     pDrawingArea->set_size_request(pDrawingArea->get_approximate_digit_width() * 40,
859                                    pDrawingArea->get_text_height() * 7);
860     CustomWidgetController::SetDrawingArea(pDrawingArea);
861 }
862 
IMPL_LINK(SvxRubyDialog,KeyUpDownHdl_Impl,const KeyEvent &,rKEvt,bool)863 IMPL_LINK(SvxRubyDialog, KeyUpDownHdl_Impl, const KeyEvent&, rKEvt, bool)
864 {
865     bool bHandled = false;
866     const vcl::KeyCode& rKeyCode = rKEvt.GetKeyCode();
867     sal_uInt16 nCode = rKeyCode.GetCode();
868     if (KEY_UP == nCode || KEY_DOWN == nCode)
869     {
870         sal_Int32 nParam = KEY_UP == nCode ? -1 : 1;
871         bHandled = EditJumpHdl_Impl(nParam);
872     }
873     return bHandled;
874 }
875 
IMPL_LINK(SvxRubyDialog,KeyUpDownTabHdl_Impl,const KeyEvent &,rKEvt,bool)876 IMPL_LINK(SvxRubyDialog, KeyUpDownTabHdl_Impl, const KeyEvent&, rKEvt, bool)
877 {
878     bool bHandled = false;
879     const vcl::KeyCode& rKeyCode = rKEvt.GetKeyCode();
880     sal_uInt16 nMod = rKeyCode.GetModifier();
881     sal_uInt16 nCode = rKeyCode.GetCode();
882     if (nCode == KEY_TAB && (!nMod || KEY_SHIFT == nMod))
883     {
884         sal_Int32 nParam = KEY_SHIFT == nMod ? -1 : 1;
885         if (EditScrollHdl_Impl(nParam))
886             bHandled = true;
887     }
888     if (!bHandled)
889         bHandled = KeyUpDownHdl_Impl(rKEvt);
890     return bHandled;
891 }
892 
893 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
894