LibreOffice Module sfx2 (master) 1
charwin.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 <vcl/settings.hxx>
21#include <vcl/virdev.hxx>
22#include <vcl/commandevent.hxx>
23#include <vcl/event.hxx>
24#include <vcl/svapp.hxx>
25#include <vcl/weldutils.hxx>
26#include <sfx2/charwin.hxx>
29
30#include <com/sun/star/beans/PropertyValue.hpp>
31
32using namespace com::sun::star;
33
35 : mxVirDev(rVirDev)
36 , mnY(0)
37 , maHasInsert(true)
38{
39}
40
42{
43 CustomWidgetController::SetDrawingArea(pDrawingArea);
45 vcl::Font aFont = rStyleSettings.GetLabelFont();
46 const Size aFontSize = aFont.GetFontSize();
47 aFont.SetFontSize(Size(aFontSize.Width() * 2, aFontSize.Height() * 2));
50 pDrawingArea->set_size_request(mxVirDev->approximate_digit_width() * 2,
51 mxVirDev->GetTextHeight());
52 mxVirDev->Pop();
53}
54
56
58
60{
61 if (rMEvt.IsLeft())
62 {
63 if (!(rMEvt.GetClicks() % 2) && maHasInsert)
64 {
66 }
67
69 return true;
70 }
71
72 return CustomWidgetController::MouseButtonDown(rMEvt);
73}
74
76{
77 bool bRet = false;
78 vcl::KeyCode aCode = rKEvt.GetKeyCode();
79 switch (aCode.GetCode())
80 {
81 case KEY_SPACE:
82 case KEY_RETURN:
84 bRet = true;
85 break;
86 }
87 return bRet;
88}
89
90bool SvxCharView::Command(const CommandEvent& rCommandEvent)
91{
92 if (rCommandEvent.GetCommand() == CommandEventId::ContextMenu)
93 {
94 GrabFocus();
95 Invalidate();
96 createContextMenu(rCommandEvent.GetMousePosPixel());
97 return true;
98 }
99
100 return weld::CustomWidgetController::Command(rCommandEvent);
101}
102
104{
105 if (GetText().isEmpty())
106 return;
107
108 uno::Sequence<beans::PropertyValue> aArgs{ comphelper::makePropertyValue("Symbols", GetText()),
110 "FontName", maFont.GetFamilyName()) };
111
112 comphelper::dispatchCommand(".uno:InsertSymbol", aArgs);
113}
114
116{
117 weld::DrawingArea* pDrawingArea = GetDrawingArea();
118 std::unique_ptr<weld::Builder> xBuilder(
119 Application::CreateBuilder(pDrawingArea, "sfx/ui/charviewmenu.ui"));
120 std::unique_ptr<weld::Menu> xItemMenu(xBuilder->weld_menu("charviewmenu"));
122 xItemMenu->popup_at_rect(pDrawingArea, tools::Rectangle(rPosition, Size(1, 1))));
123 Invalidate();
124}
125
126void SvxCharView::ContextMenuSelect(std::u16string_view rMenuId)
127{
128 if (rMenuId == u"clearchar")
129 maClearClickHdl.Call(this);
130 else if (rMenuId == u"clearallchar")
132}
133
135{
136 rRenderContext.SetFont(maFont);
137
138 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
139 const Color aWindowTextColor(rStyleSettings.GetFieldTextColor());
140 Color aHighlightColor(rStyleSettings.GetHighlightColor());
141 Color aHighlightTextColor(rStyleSettings.GetHighlightTextColor());
142 Color aFillColor(rStyleSettings.GetWindowColor());
143 Color aTextColor(rStyleSettings.GetWindowTextColor());
144 Color aShadowColor(rStyleSettings.GetShadowColor());
145
146 const OUString aText = GetText();
147
148 Size aSize(GetOutputSizePixel());
149 tools::Long nAvailWidth = aSize.Width();
150 tools::Long nWinHeight = aSize.Height();
151
152 bool bGotBoundary = true;
153 bool bShrankFont = false;
154 vcl::Font aOrigFont(rRenderContext.GetFont());
155 Size aFontSize(aOrigFont.GetFontSize());
156 ::tools::Rectangle aBoundRect;
157
158 for (tools::Long nFontHeight = aFontSize.Height(); nFontHeight > 0; nFontHeight -= 1)
159 {
160 if (!rRenderContext.GetTextBoundRect(aBoundRect, aText) || aBoundRect.IsEmpty())
161 {
162 bGotBoundary = false;
163 break;
164 }
165
166 //only shrink in the single glyph large view mode
167 tools::Long nTextWidth = aBoundRect.GetWidth();
168 if (nAvailWidth > nTextWidth)
169 break;
170 vcl::Font aFont(aOrigFont);
171 aFontSize.setHeight(nFontHeight);
172 aFont.SetFontSize(aFontSize);
173 rRenderContext.SetFont(aFont);
174 mnY = (nWinHeight - rRenderContext.GetTextHeight()) / 2;
175 bShrankFont = true;
176 }
177
178 Point aPoint(2, mnY);
179
180 if (!bGotBoundary)
181 aPoint.setX((aSize.Width() - rRenderContext.GetTextWidth(aText)) / 2);
182 else
183 {
184 // adjust position
185 aBoundRect += aPoint;
186
187 // vertical adjustment
188 int nYLDelta = aBoundRect.Top();
189 int nYHDelta = aSize.Height() - aBoundRect.Bottom();
190 if (nYLDelta <= 0)
191 aPoint.AdjustY(-(nYLDelta - 1));
192 else if (nYHDelta <= 0)
193 aPoint.AdjustY(nYHDelta - 1);
194
195 // centrally align glyph
196 aPoint.setX(-aBoundRect.Left() + (aSize.Width() - aBoundRect.GetWidth()) / 2);
197 }
198
199 // tdf#111924 - don't lose focus on context menu
200 if (HasFocus() || HasChildFocus())
201 {
202 rRenderContext.SetFillColor(aHighlightColor);
203 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aSize));
204
205 rRenderContext.SetTextColor(aHighlightTextColor);
206 rRenderContext.DrawText(aPoint, aText);
207 }
208 else
209 {
210 rRenderContext.SetFillColor(aFillColor);
211 rRenderContext.SetLineColor(aShadowColor);
212 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aSize));
213
214 rRenderContext.SetTextColor(aWindowTextColor);
215 rRenderContext.DrawText(aPoint, aText);
216 }
217 rRenderContext.SetFillColor(aFillColor);
218 rRenderContext.SetTextColor(aTextColor);
219
220 if (bShrankFont)
221 rRenderContext.SetFont(aOrigFont);
222}
223
225{
226 maMouseClickHdl = rLink;
227}
228
230{
231 maClearClickHdl = rLink;
232}
233
235{
236 maClearAllClickHdl = rLink;
237}
238
240{
241 tools::Long nWinHeight = GetOutputSizePixel().Height();
242 maFont = rFont;
245 maFont.SetFontSize(mxVirDev->PixelToLogic(Size(0, nWinHeight / 2)));
247
248 mxVirDev->Push(PUSH_ALLFONT);
249 mxVirDev->SetFont(maFont);
250 mnY = (nWinHeight - mxVirDev->GetTextHeight()) / 2;
251 mxVirDev->Pop();
252
253 Invalidate();
254}
255
257{
258 SetFont(GetFont()); //force recalculation of size
259}
260
261void SvxCharView::SetText(const OUString& rText)
262{
263 m_sText = rText;
264 Invalidate();
265}
266
267void SvxCharView::SetHasInsert(bool bInsert) { maHasInsert = bInsert; }
268
269/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
#define PUSH_ALLFONT
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
static std::unique_ptr< weld::Builder > CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false, sal_uInt64 nLOKWindowId=0)
CommandEventId GetCommand() const
const Point & GetMousePosPixel() const
const vcl::KeyCode & GetKeyCode() const
sal_uInt16 GetClicks() const
bool IsLeft() const
const vcl::Font & GetFont() const
void SetFont(const vcl::Font &rNewFont)
void DrawRect(const tools::Rectangle &rRect)
bool GetTextBoundRect(tools::Rectangle &rRect, const OUString &rStr, sal_Int32 nBase=0, sal_Int32 nIndex=0, sal_Int32 nLen=-1, sal_uLong nLayoutWidth=0, KernArraySpan aDXArray=KernArraySpan(), o3tl::span< const sal_Bool > pKashidaArray={}, const SalLayoutGlyphs *pGlyphs=nullptr) const
void SetLineColor()
tools::Long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::text::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
void SetTextColor(const Color &rColor)
void SetFillColor()
tools::Long GetTextHeight() const
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, std::vector< tools::Rectangle > *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
void setX(tools::Long nX)
tools::Long AdjustY(tools::Long nVertMove)
constexpr tools::Long Height() const
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
const Color & GetWindowColor() const
const Color & GetFieldTextColor() const
const Color & GetShadowColor() const
const vcl::Font & GetLabelFont() const
const Color & GetWindowTextColor() const
const Color & GetHighlightColor() const
const Color & GetHighlightTextColor() const
vcl::Font const & GetFont() const
Definition: charwin.hxx:54
SvxCharView(const VclPtr< VirtualDevice > &rVirDev)
Definition: charwin.cxx:34
virtual void GetFocus() override
Definition: charwin.cxx:55
void setMouseClickHdl(const Link< SvxCharView *, void > &rLink)
Definition: charwin.cxx:224
void SetFont(const vcl::Font &rFont)
Definition: charwin.cxx:239
void SetHasInsert(bool bInsert)
Definition: charwin.cxx:267
void ContextMenuSelect(std::u16string_view rIdent)
Definition: charwin.cxx:126
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: charwin.cxx:134
virtual bool Command(const CommandEvent &) override
Definition: charwin.cxx:90
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
Definition: charwin.cxx:41
Link< SvxCharView *, void > maClearClickHdl
Definition: charwin.hxx:39
bool maHasInsert
Definition: charwin.hxx:35
void SetText(const OUString &rText)
Definition: charwin.cxx:261
vcl::Font maFont
Definition: charwin.hxx:34
tools::Long mnY
Definition: charwin.hxx:33
Link< SvxCharView *, void > maMouseClickHdl
Definition: charwin.hxx:38
void createContextMenu(const Point &rPosition)
Definition: charwin.cxx:115
virtual void LoseFocus() override
Definition: charwin.cxx:57
VclPtr< VirtualDevice > mxVirDev
Definition: charwin.hxx:32
virtual bool KeyInput(const KeyEvent &) override
Definition: charwin.cxx:75
Link< SvxCharView *, void > maClearAllClickHdl
Definition: charwin.hxx:40
void setClearClickHdl(const Link< SvxCharView *, void > &rLink)
Definition: charwin.cxx:229
void InsertCharToDoc()
Definition: charwin.cxx:103
void setClearAllClickHdl(const Link< SvxCharView *, void > &rLink)
Definition: charwin.cxx:234
virtual bool MouseButtonDown(const MouseEvent &) override
Definition: charwin.cxx:59
OUString const & GetText() const
Definition: charwin.hxx:56
virtual void Resize() override
Definition: charwin.cxx:256
OUString m_sText
Definition: charwin.hxx:36
constexpr tools::Long GetWidth() const
constexpr tools::Long Top() const
constexpr tools::Long Left() const
constexpr tools::Long Bottom() const
constexpr bool IsEmpty() const
void SetFontSize(const Size &)
void SetTransparent(bool bTransparent)
void SetWeight(FontWeight)
const OUString & GetFamilyName() const
const Size & GetFontSize() const
void SetAlignment(TextAlign)
sal_uInt16 GetCode() const
weld::DrawingArea * GetDrawingArea() const
virtual bool Command(const CommandEvent &)
Size const & GetOutputSizePixel() const
virtual void set_size_request(int nWidth, int nHeight)=0
float u
ALIGN_TOP
WEIGHT_NORMAL
constexpr sal_uInt16 KEY_RETURN
constexpr sal_uInt16 KEY_SPACE
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
long Long
void SetPointFont(OutputDevice &rDevice, const vcl::Font &rFont)