Update git submodules
[LibreOffice.git] / sc / source / ui / pagedlg / tphfedit.cxx
blob617f2208a9f931bf7bafe22ffac1fb00468e1fe0
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 <scitems.hxx>
21 #include <editeng/eeitem.hxx>
23 #include <editeng/editobj.hxx>
24 #include <editeng/editview.hxx>
25 #include <editeng/adjustitem.hxx>
26 #include <editeng/colritem.hxx>
27 #include <editeng/fhgtitem.hxx>
28 #include <sfx2/objsh.hxx>
29 #include <sfx2/sfxdlg.hxx>
31 #include <tphfedit.hxx>
32 #include <editutil.hxx>
33 #include <global.hxx>
34 #include <patattr.hxx>
35 #include <scresid.hxx>
36 #include <globstr.hrc>
37 #include <strings.hrc>
38 #include <tabvwsh.hxx>
39 #include <prevwsh.hxx>
40 #include <AccessibleEditObject.hxx>
42 #include <scabstdlg.hxx>
43 #include <memory>
46 static void lcl_GetFieldData( ScHeaderFieldData& rData )
48 SfxViewShell* pShell = SfxViewShell::Current();
49 if (pShell)
51 if (auto pTabViewShell = dynamic_cast<ScTabViewShell*>( pShell))
52 pTabViewShell->FillFieldData(rData);
53 else if (auto pPreviewShell = dynamic_cast<ScPreviewShell*>( pShell))
54 pPreviewShell->FillFieldData(rData);
59 ScEditWindow::ScEditWindow(ScEditWindowLocation eLoc, weld::Window* pDialog)
60 : eLocation(eLoc)
61 , mbRTL(ScGlobal::IsSystemRTL())
62 , mpDialog(pDialog)
66 void ScEditWindow::makeEditEngine()
68 m_xEditEngine.reset(new ScHeaderEditEngine(EditEngine::CreatePool().get()));
71 ScHeaderEditEngine* ScEditWindow::GetEditEngine() const
73 return static_cast<ScHeaderEditEngine*>(m_xEditEngine.get());
76 void ScEditWindow::SetDrawingArea(weld::DrawingArea* pDrawingArea)
78 OutputDevice& rDevice = pDrawingArea->get_ref_device();
79 Size aSize = rDevice.LogicToPixel(Size(80, 120), MapMode(MapUnit::MapAppFont));
80 pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
82 WeldEditView::SetDrawingArea(pDrawingArea);
84 ScHeaderFieldData aData;
85 lcl_GetFieldData(aData);
86 // fields
87 GetEditEngine()->SetData(aData);
88 if (mbRTL)
89 m_xEditEngine->SetDefaultHorizontalTextDirection(EEHorizontalTextDirection::R2L);
91 Color aBgColor = svtools::ColorConfig().GetColorValue(svtools::DOCCOLOR).nColor;
92 rDevice.SetBackground(aBgColor);
93 m_xEditView->SetBackgroundColor(aBgColor);
95 if (auto tmpAcc = mxAcc.get())
97 OUString sName;
98 switch (eLocation)
100 case Left:
101 sName = ScResId(STR_ACC_LEFTAREA_NAME);
102 break;
103 case Center:
104 sName = ScResId(STR_ACC_CENTERAREA_NAME);
105 break;
106 case Right:
107 sName = ScResId(STR_ACC_RIGHTAREA_NAME);
108 break;
111 tmpAcc->InitAcc(nullptr, m_xEditView.get(),
112 sName, pDrawingArea->get_tooltip_text());
115 SetCursor(m_xEditView->GetCursor());
118 ScEditWindow::~ScEditWindow()
120 // delete Accessible object before deleting EditEngine and EditView
121 if (auto tmp = mxAcc.get())
122 tmp->dispose();
125 void ScEditWindow::SetNumType(SvxNumType eNumType)
127 ScHeaderEditEngine* pEditEngine = GetEditEngine();
128 pEditEngine->SetNumType(eNumType);
129 pEditEngine->UpdateFields();
132 std::unique_ptr<EditTextObject> ScEditWindow::CreateTextObject()
134 // reset paragraph attributes
135 // (GetAttribs at creation of format dialog always returns the set items)
137 const SfxItemSet& rEmpty = m_xEditEngine->GetEmptyItemSet();
138 sal_Int32 nParCnt = m_xEditEngine->GetParagraphCount();
139 for (sal_Int32 i=0; i<nParCnt; i++)
140 m_xEditEngine->SetParaAttribs( i, rEmpty );
142 return m_xEditEngine->CreateTextObject();
145 void ScEditWindow::SetFont( const ScPatternAttr& rPattern )
147 auto pSet = std::make_unique<SfxItemSet>( m_xEditEngine->GetEmptyItemSet() );
148 rPattern.FillEditItemSet( pSet.get() );
149 // FillEditItemSet adjusts font height to 1/100th mm,
150 // but for header/footer twips is needed, as in the PatternAttr:
151 pSet->Put( rPattern.GetItem(ATTR_FONT_HEIGHT).CloneSetWhich(EE_CHAR_FONTHEIGHT) );
152 pSet->Put( rPattern.GetItem(ATTR_CJK_FONT_HEIGHT).CloneSetWhich(EE_CHAR_FONTHEIGHT_CJK) );
153 pSet->Put( rPattern.GetItem(ATTR_CTL_FONT_HEIGHT).CloneSetWhich(EE_CHAR_FONTHEIGHT_CTL) );
154 // font color used, suitable header/footer background color set in ScEditWindow::SetDrawingArea
155 Color aFgColor = svtools::ColorConfig().GetColorValue(svtools::FONTCOLOR, false).nColor;
156 if (aFgColor == COL_AUTO) {
157 Color aBgColor = svtools::ColorConfig().GetColorValue(svtools::DOCCOLOR).nColor;
158 aFgColor = aBgColor.IsDark() ? COL_WHITE : COL_BLACK;
160 pSet->Put(SvxColorItem(aFgColor, EE_CHAR_COLOR));
161 if (mbRTL)
162 pSet->Put( SvxAdjustItem( SvxAdjust::Right, EE_PARA_JUST ) );
163 GetEditEngine()->SetDefaults( std::move(pSet) );
166 void ScEditWindow::SetText( const EditTextObject& rTextObject )
168 GetEditEngine()->SetTextCurrentDefaults(rTextObject);
171 void ScEditWindow::InsertField( const SvxFieldItem& rFld )
173 m_xEditView->InsertField( rFld );
176 void ScEditWindow::SetCharAttributes()
178 SfxObjectShell* pDocSh = SfxObjectShell::Current();
180 SfxViewShell* pViewSh = SfxViewShell::Current();
182 ScTabViewShell* pTabViewSh = dynamic_cast<ScTabViewShell*>( SfxViewShell::Current() );
184 OSL_ENSURE( pDocSh, "Current DocShell not found" );
185 OSL_ENSURE( pViewSh, "Current ViewShell not found" );
187 if ( !(pDocSh && pViewSh) )
188 return;
190 if(pTabViewSh!=nullptr) pTabViewSh->SetInFormatDialog(true);
192 SfxItemSet aSet( m_xEditView->GetAttribs() );
194 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
196 ScopedVclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateScCharDlg(
197 mpDialog, &aSet, pDocSh, false));
198 pDlg->SetText( ScResId( STR_TEXTATTRS ) );
199 if ( pDlg->Execute() == RET_OK )
201 aSet.ClearItem();
202 aSet.Put( *pDlg->GetOutputItemSet() );
203 m_xEditView->SetAttribs( aSet );
206 if(pTabViewSh!=nullptr) pTabViewSh->SetInFormatDialog(false);
209 bool ScEditWindow::KeyInput( const KeyEvent& rKEvt )
211 if ( !rKEvt.GetKeyCode().IsMod1() && !rKEvt.GetKeyCode().IsShift() &&
212 rKEvt.GetKeyCode().IsMod2() && rKEvt.GetKeyCode().GetCode() == KEY_DOWN )
214 aObjectSelectLink.Call(*this);
215 return true;
217 return WeldEditView::KeyInput(rKEvt);
220 void ScEditWindow::GetFocus()
222 assert(m_GetFocusLink);
223 m_GetFocusLink(*this);
225 if (auto tmp = mxAcc.get())
226 tmp->GotFocus();
228 WeldEditView::GetFocus();
231 void ScEditWindow::LoseFocus()
233 if (auto xTemp = mxAcc.get())
234 xTemp->LostFocus();
235 else
236 mxAcc = nullptr;
237 WeldEditView::LoseFocus();
240 bool ScEditWindow::MouseButtonDown(const MouseEvent& rMEvt)
242 bool bHadFocus = HasFocus();
243 bool bRet = WeldEditView::MouseButtonDown(rMEvt);
244 if (!bHadFocus)
246 assert(comphelper::LibreOfficeKit::isActive() || HasFocus());
247 GetFocus();
249 return bRet;
252 css::uno::Reference< css::accessibility::XAccessible > ScEditWindow::CreateAccessible()
254 rtl::Reference<ScAccessibleEditControlObject> tmp = new ScAccessibleEditControlObject(this, ScAccessibleEditObject::EditControl);
255 mxAcc = tmp.get();
256 return tmp;
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */