svx: prefix members of SdrObjEditView
[LibreOffice.git] / starmath / source / utility.cxx
blob4a9225f6b3632928bb4a15caeef45b58ee68111b
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 <strings.hrc>
21 #include <smmod.hxx>
22 #include <utility.hxx>
23 #include <dialog.hxx>
24 #include <view.hxx>
26 #include <comphelper/lok.hxx>
27 #include <sfx2/lokcomponenthelpers.hxx>
29 // return pointer to active SmViewShell, if this is not possible
30 // return 0 instead.
31 //!! Since this method is based on the current focus it is somewhat
32 //!! unreliable and may return unexpected 0 pointers!
33 SmViewShell * SmGetActiveView()
35 SfxViewShell *pView = SfxViewShell::Current();
36 SmViewShell* pSmView = dynamic_cast<SmViewShell*>(pView);
37 if (!pSmView && comphelper::LibreOfficeKit::isActive())
39 auto* pWindow = static_cast<SmGraphicWindow*>(LokStarMathHelper(pView).GetGraphicWindow());
40 if (pWindow)
41 pSmView = &pWindow->GetGraphicWidget().GetView();
43 return pSmView;
47 /**************************************************************************/
49 void SmFontPickList::Clear()
51 aFontVec.clear();
54 SmFontPickList& SmFontPickList::operator = (const SmFontPickList& rList)
56 Clear();
57 nMaxItems = rList.nMaxItems;
58 aFontVec = rList.aFontVec;
60 return *this;
63 vcl::Font SmFontPickList::Get(sal_uInt16 nPos) const
65 return nPos < aFontVec.size() ? aFontVec[nPos] : vcl::Font();
68 namespace {
70 bool lcl_CompareItem(const vcl::Font & rFirstFont, const vcl::Font & rSecondFont)
72 return rFirstFont.GetFamilyName() == rSecondFont.GetFamilyName() &&
73 rFirstFont.GetFamilyType() == rSecondFont.GetFamilyType() &&
74 rFirstFont.GetCharSet() == rSecondFont.GetCharSet() &&
75 rFirstFont.GetWeight() == rSecondFont.GetWeight() &&
76 rFirstFont.GetItalic() == rSecondFont.GetItalic();
79 OUString lcl_GetStringItem(const vcl::Font &rFont)
81 OUStringBuffer aString(rFont.GetFamilyName());
83 if (IsItalic( rFont ))
85 aString.append(", " + SmResId(RID_FONTITALIC));
87 if (IsBold( rFont ))
89 aString.append(", " + SmResId(RID_FONTBOLD));
92 return aString.makeStringAndClear();
97 void SmFontPickList::Insert(const vcl::Font &rFont)
99 for (size_t nPos = 0; nPos < aFontVec.size(); nPos++)
100 if (lcl_CompareItem( aFontVec[nPos], rFont))
102 aFontVec.erase( aFontVec.begin() + nPos );
103 break;
106 aFontVec.push_front( rFont );
108 if (aFontVec.size() > nMaxItems)
110 aFontVec.pop_back();
114 void SmFontPickList::ReadFrom(const SmFontDialog& rDialog)
116 Insert(rDialog.GetFont());
119 void SmFontPickList::WriteTo(SmFontDialog& rDialog) const
121 rDialog.SetFont(Get());
125 /**************************************************************************/
127 SmFontPickListBox::SmFontPickListBox(std::unique_ptr<weld::ComboBox> pWidget)
128 : SmFontPickList(4)
129 , m_xWidget(std::move(pWidget))
131 m_xWidget->connect_changed(LINK(this, SmFontPickListBox, SelectHdl));
134 IMPL_LINK_NOARG(SmFontPickListBox, SelectHdl, weld::ComboBox&, void)
136 const int nPos = m_xWidget->get_active();
137 if (nPos != 0)
139 SmFontPickList::Insert(Get(nPos));
140 OUString aString = m_xWidget->get_text(nPos);
141 m_xWidget->remove(nPos);
142 m_xWidget->insert_text(0, aString);
145 m_xWidget->set_active(0);
148 SmFontPickListBox& SmFontPickListBox::operator=(const SmFontPickList& rList)
150 *static_cast<SmFontPickList *>(this) = rList;
152 for (decltype(aFontVec)::size_type nPos = 0; nPos < aFontVec.size(); nPos++)
153 m_xWidget->insert_text(nPos, lcl_GetStringItem(aFontVec[nPos]));
155 if (!aFontVec.empty())
156 m_xWidget->set_active_text(lcl_GetStringItem(aFontVec.front()));
158 return *this;
161 void SmFontPickListBox::Insert(const vcl::Font &rFont)
163 SmFontPickList::Insert(rFont);
165 OUString aEntry(lcl_GetStringItem(aFontVec.front()));
166 int nPos = m_xWidget->find_text(aEntry);
167 if (nPos != -1)
168 m_xWidget->remove(nPos);
169 m_xWidget->insert_text(0, aEntry);
170 m_xWidget->set_active(0);
172 while (m_xWidget->get_count() > nMaxItems)
173 m_xWidget->remove(m_xWidget->get_count() - 1);
176 bool IsItalic( const vcl::Font &rFont )
178 FontItalic eItalic = rFont.GetItalic();
179 // the code below leaves only _NONE and _DONTKNOW as not italic
180 return eItalic == ITALIC_OBLIQUE || eItalic == ITALIC_NORMAL;
184 bool IsBold( const vcl::Font &rFont )
186 FontWeight eWeight = rFont.GetWeight();
187 return eWeight > WEIGHT_NORMAL;
191 void SmFace::Impl_Init()
193 SetSize( GetFontSize() );
194 SetTransparent( true );
195 SetAlignment( ALIGN_BASELINE );
196 SetColor( COL_AUTO );
199 void SmFace::SetSize(const Size& rSize)
201 Size aSize (rSize);
203 // check the requested size against minimum value
204 const int nMinVal = o3tl::convert(2, o3tl::Length::pt, SmO3tlLengthUnit());
206 if (aSize.Height() < nMinVal)
207 aSize.setHeight( nMinVal );
209 //! we don't force a maximum value here because this may prevent eg the
210 //! parentheses in "left ( ... right )" from matching up with large
211 //! bodies (eg stack{...} with many entries).
212 //! Of course this is holds only if characters are used and not polygons.
214 Font::SetFontSize(aSize);
218 tools::Long SmFace::GetBorderWidth() const
220 if (nBorderWidth < 0)
221 return GetDefaultBorderWidth();
222 else
223 return nBorderWidth;
226 SmFace & SmFace::operator = (const SmFace &rFace)
228 Font::operator = (rFace);
229 nBorderWidth = -1;
230 return *this;
234 SmFace & operator *= (SmFace &rFace, const Fraction &rFrac)
235 // scales the width and height of 'rFace' by 'rFrac' and returns a
236 // reference to 'rFace'.
237 // It's main use is to make scaling fonts look easier.
238 { const Size &rFaceSize = rFace.GetFontSize();
240 rFace.SetSize(Size(tools::Long(rFaceSize.Width() * rFrac),
241 tools::Long(rFaceSize.Height() * rFrac)));
242 return rFace;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */