LibreOffice Module starmath (master) 1
utility.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <strings.hrc>
21#include <smmod.hxx>
22#include <utility.hxx>
23#include <dialog.hxx>
24#include <view.hxx>
25
26#include <comphelper/lok.hxx>
28
29// return pointer to active SmViewShell, if this is not possible
30// return 0 instead.
34{
36 SmViewShell* pSmView = dynamic_cast<SmViewShell*>(pView);
38 {
39 auto* pWindow = static_cast<SmGraphicWindow*>(LokStarMathHelper(pView).GetGraphicWindow());
40 if (pWindow)
41 pSmView = &pWindow->GetGraphicWidget().GetView();
42 }
43 return pSmView;
44}
45
46
47/**************************************************************************/
48
50{
51 aFontVec.clear();
52}
53
55{
56 Clear();
57 nMaxItems = rList.nMaxItems;
58 aFontVec = rList.aFontVec;
59
60 return *this;
61}
62
63vcl::Font SmFontPickList::Get(sal_uInt16 nPos) const
64{
65 return nPos < aFontVec.size() ? aFontVec[nPos] : vcl::Font();
66}
67
68namespace {
69
70bool lcl_CompareItem(const vcl::Font & rFirstFont, const vcl::Font & rSecondFont)
71{
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();
77}
78
79OUString lcl_GetStringItem(const vcl::Font &rFont)
80{
81 OUStringBuffer aString(rFont.GetFamilyName());
82
83 if (IsItalic( rFont ))
84 {
85 aString.append(", " + SmResId(RID_FONTITALIC));
86 }
87 if (IsBold( rFont ))
88 {
89 aString.append(", " + SmResId(RID_FONTBOLD));
90 }
91
92 return aString.makeStringAndClear();
93}
94
95}
96
98{
99 for (size_t nPos = 0; nPos < aFontVec.size(); nPos++)
100 if (lcl_CompareItem( aFontVec[nPos], rFont))
101 {
102 aFontVec.erase( aFontVec.begin() + nPos );
103 break;
104 }
105
106 aFontVec.push_front( rFont );
107
108 if (aFontVec.size() > nMaxItems)
109 {
110 aFontVec.pop_back();
111 }
112}
113
115{
116 Insert(rDialog.GetFont());
117}
118
120{
121 rDialog.SetFont(Get());
122}
123
124
125/**************************************************************************/
126
127SmFontPickListBox::SmFontPickListBox(std::unique_ptr<weld::ComboBox> pWidget)
128 : SmFontPickList(4)
129 , m_xWidget(std::move(pWidget))
130{
131 m_xWidget->connect_changed(LINK(this, SmFontPickListBox, SelectHdl));
132}
133
135{
136 const int nPos = m_xWidget->get_active();
137 if (nPos != 0)
138 {
140 OUString aString = m_xWidget->get_text(nPos);
141 m_xWidget->remove(nPos);
142 m_xWidget->insert_text(0, aString);
143 }
144
145 m_xWidget->set_active(0);
146}
147
149{
150 *static_cast<SmFontPickList *>(this) = rList;
151
152 for (decltype(aFontVec)::size_type nPos = 0; nPos < aFontVec.size(); nPos++)
153 m_xWidget->insert_text(nPos, lcl_GetStringItem(aFontVec[nPos]));
154
155 if (!aFontVec.empty())
156 m_xWidget->set_active_text(lcl_GetStringItem(aFontVec.front()));
157
158 return *this;
159}
160
162{
164
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);
171
172 while (m_xWidget->get_count() > nMaxItems)
173 m_xWidget->remove(m_xWidget->get_count() - 1);
174}
175
176bool IsItalic( const vcl::Font &rFont )
177{
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;
181}
182
183
184bool IsBold( const vcl::Font &rFont )
185{
186 FontWeight eWeight = rFont.GetWeight();
187 return eWeight > WEIGHT_NORMAL;
188}
189
190
192{
193 SetSize( GetFontSize() );
194 SetTransparent( true );
197}
198
199void SmFace::SetSize(const Size& rSize)
200{
201 Size aSize (rSize);
202
203 // check the requested size against minimum value
204 const int nMinVal = o3tl::convert(2, o3tl::Length::pt, SmO3tlLengthUnit());
205
206 if (aSize.Height() < nMinVal)
207 aSize.setHeight( nMinVal );
208
213
214 Font::SetFontSize(aSize);
215}
216
217
219{
220 if (nBorderWidth < 0)
221 return GetDefaultBorderWidth();
222 else
223 return nBorderWidth;
224}
225
227{
228 Font::operator = (rFace);
229 nBorderWidth = -1;
230 return *this;
231}
232
233
234SmFace & 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();
239
240 rFace.SetSize(Size(tools::Long(rFaceSize.Width() * rFrac),
241 tools::Long(rFaceSize.Height() * rFrac)));
242 return rFace;
243}
244
245/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Image > m_xWidget
vcl::Window * GetGraphicWindow()
static SAL_WARN_UNUSED_RESULT SfxViewShell * Current()
constexpr tools::Long Height() const
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
void SetSize(const Size &rSize)
Definition: utility.cxx:199
tools::Long nBorderWidth
Definition: utility.hxx:47
SmFace & operator=(const SmFace &rFace)
Definition: utility.cxx:226
tools::Long GetBorderWidth() const
Definition: utility.cxx:218
void Impl_Init()
Definition: utility.cxx:191
tools::Long GetDefaultBorderWidth() const
Definition: utility.hxx:68
const vcl::Font & GetFont() const
Definition: dialog.hxx:99
void SetFont(const vcl::Font &rFont)
Definition: dialog.cxx:297
SmFontPickListBox(std::unique_ptr< weld::ComboBox > pWidget)
Definition: utility.cxx:127
virtual void Insert(const vcl::Font &rFont) override
Definition: utility.cxx:161
SmFontPickListBox & operator=(const SmFontPickList &rList)
Definition: utility.cxx:148
std::unique_ptr< weld::ComboBox > m_xWidget
Definition: utility.hxx:110
void Clear()
Definition: utility.cxx:49
void ReadFrom(const SmFontDialog &rDialog)
Definition: utility.cxx:114
vcl::Font Get(sal_uInt16 nPos=0) const
Definition: utility.cxx:63
void WriteTo(SmFontDialog &rDialog) const
Definition: utility.cxx:119
std::deque< vcl::Font > aFontVec
Definition: utility.hxx:86
virtual void Insert(const vcl::Font &rFont)
Definition: utility.cxx:97
sal_uInt16 nMaxItems
Definition: utility.hxx:85
SmFontPickList & operator=(const SmFontPickList &rList)
Definition: utility.cxx:54
SmViewShell & GetView()
Definition: view.hxx:115
SmGraphicWidget & GetGraphicWidget()
Definition: view.hxx:304
FontFamily GetFamilyType()
void SetTransparent(bool bTransparent)
void SetColor(const Color &)
FontItalic GetItalic()
const OUString & GetFamilyName() const
const Size & GetFontSize() const
void SetAlignment(TextAlign)
FontWeight GetWeight()
rtl_TextEncoding GetCharSet() const
constexpr ::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
FontItalic
ITALIC_NORMAL
ITALIC_OBLIQUE
ALIGN_BASELINE
WEIGHT_NORMAL
sal_uInt16 nPos
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
FontWeight
long Long
OUString SmResId(TranslateId aId)
Definition: smmod.cxx:42
IMPL_LINK_NOARG(SmFontPickListBox, SelectHdl, weld::ComboBox &, void)
Definition: utility.cxx:134
bool IsItalic(const vcl::Font &rFont)
Definition: utility.cxx:176
SmFace & operator*=(SmFace &rFace, const Fraction &rFrac)
Definition: utility.cxx:234
bool IsBold(const vcl::Font &rFont)
Definition: utility.cxx:184
SmViewShell * SmGetActiveView()
! Since this method is based on the current focus it is somewhat ! unreliable and may return unexpect...
Definition: utility.cxx:33
o3tl::Length SmO3tlLengthUnit()
Definition: utility.hxx:125