Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / control / charwin.cxx
blob6e404c7257d74c2a3bdb67c2ee3e3c6c622341a6
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 <vcl/settings.hxx>
21 #include <vcl/virdev.hxx>
22 #include <vcl/event.hxx>
23 #include <vcl/svapp.hxx>
24 #include <sfx2/charwin.hxx>
25 #include <comphelper/dispatchcommand.hxx>
26 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/beans/PropertyValue.hpp>
30 using namespace com::sun::star;
32 SvxCharView::SvxCharView(const VclPtr<VirtualDevice>& rVirDev)
33 : mxVirDev(rVirDev)
34 , mnY(0)
35 , maPosition(0, 0)
36 , maHasInsert(true)
40 void SvxCharView::SetDrawingArea(weld::DrawingArea* pDrawingArea)
42 CustomWidgetController::SetDrawingArea(pDrawingArea);
43 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
44 vcl::Font aFont = rStyleSettings.GetLabelFont();
45 const Size aFontSize = aFont.GetFontSize();
46 aFont.SetFontSize(Size(aFontSize.Width() * 2.5, aFontSize.Height() * 2.5));
47 mxVirDev->Push(PUSH_ALLFONT);
48 mxVirDev->SetFont(aFont);
49 pDrawingArea->set_size_request(mxVirDev->approximate_digit_width() * 2,
50 mxVirDev->GetTextHeight());
51 mxVirDev->Pop();
54 void SvxCharView::GetFocus() { Invalidate(); }
56 void SvxCharView::LoseFocus() { Invalidate(); }
58 bool SvxCharView::MouseButtonDown(const MouseEvent& rMEvt)
60 if (rMEvt.IsLeft())
62 if (!(rMEvt.GetClicks() % 2) && maHasInsert)
64 InsertCharToDoc();
67 maMouseClickHdl.Call(this);
70 if (rMEvt.IsRight())
72 Point aPosition(rMEvt.GetPosPixel());
73 maPosition = aPosition;
74 GrabFocus();
75 Invalidate();
76 createContextMenu();
79 return true;
82 bool SvxCharView::KeyInput(const KeyEvent& rKEvt)
84 bool bRet = false;
85 vcl::KeyCode aCode = rKEvt.GetKeyCode();
86 switch (aCode.GetCode())
88 case KEY_SPACE:
89 case KEY_RETURN:
90 InsertCharToDoc();
91 bRet = true;
92 break;
94 return bRet;
97 void SvxCharView::InsertCharToDoc()
99 if (GetText().isEmpty())
100 return;
102 uno::Sequence<beans::PropertyValue> aArgs(2);
103 aArgs[0].Name = "Symbols";
104 aArgs[0].Value <<= GetText();
106 aArgs[1].Name = "FontName";
107 aArgs[1].Value <<= maFont.GetFamilyName();
109 comphelper::dispatchCommand(".uno:InsertSymbol", aArgs);
112 void SvxCharView::createContextMenu()
114 weld::DrawingArea* pDrawingArea = GetDrawingArea();
115 std::unique_ptr<weld::Builder> xBuilder(
116 Application::CreateBuilder(pDrawingArea, "sfx/ui/charviewmenu.ui"));
117 std::unique_ptr<weld::Menu> xItemMenu(xBuilder->weld_menu("charviewmenu"));
118 ContextMenuSelect(
119 xItemMenu->popup_at_rect(pDrawingArea, tools::Rectangle(maPosition, Size(1, 1))));
120 Invalidate();
123 void SvxCharView::ContextMenuSelect(const OString& rMenuId)
125 if (rMenuId == "clearchar")
126 maClearClickHdl.Call(this);
127 else if (rMenuId == "clearallchar")
128 maClearAllClickHdl.Call(this);
131 void SvxCharView::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
133 rRenderContext.SetFont(maFont);
135 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
136 const Color aWindowTextColor(rStyleSettings.GetFieldTextColor());
137 Color aHighlightColor(rStyleSettings.GetHighlightColor());
138 Color aHighlightTextColor(rStyleSettings.GetHighlightTextColor());
139 Color aFillColor(rStyleSettings.GetWindowColor());
140 Color aTextColor(rStyleSettings.GetWindowTextColor());
142 const OUString aText = GetText();
144 Size aSize(GetOutputSizePixel());
145 tools::Long nAvailWidth = aSize.Width();
146 tools::Long nWinHeight = aSize.Height();
148 bool bGotBoundary = true;
149 bool bShrankFont = false;
150 vcl::Font aOrigFont(rRenderContext.GetFont());
151 Size aFontSize(aOrigFont.GetFontSize());
152 ::tools::Rectangle aBoundRect;
154 for (tools::Long nFontHeight = aFontSize.Height(); nFontHeight > 0; nFontHeight -= 1)
156 if (!rRenderContext.GetTextBoundRect(aBoundRect, aText) || aBoundRect.IsEmpty())
158 bGotBoundary = false;
159 break;
162 //only shrink in the single glyph large view mode
163 tools::Long nTextWidth = aBoundRect.GetWidth();
164 if (nAvailWidth > nTextWidth)
165 break;
166 vcl::Font aFont(aOrigFont);
167 aFontSize.setHeight(nFontHeight);
168 aFont.SetFontSize(aFontSize);
169 rRenderContext.SetFont(aFont);
170 mnY = (nWinHeight - rRenderContext.GetTextHeight()) / 2;
171 bShrankFont = true;
174 Point aPoint(2, mnY);
176 if (!bGotBoundary)
177 aPoint.setX((aSize.Width() - rRenderContext.GetTextWidth(aText)) / 2);
178 else
180 // adjust position
181 aBoundRect += aPoint;
183 // vertical adjustment
184 int nYLDelta = aBoundRect.Top();
185 int nYHDelta = aSize.Height() - aBoundRect.Bottom();
186 if (nYLDelta <= 0)
187 aPoint.AdjustY(-(nYLDelta - 1));
188 else if (nYHDelta <= 0)
189 aPoint.AdjustY(nYHDelta - 1);
191 // centrally align glyph
192 aPoint.setX(-aBoundRect.Left() + (aSize.Width() - aBoundRect.GetWidth()) / 2);
195 if (HasFocus())
197 rRenderContext.SetFillColor(aHighlightColor);
198 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aSize));
200 rRenderContext.SetTextColor(aHighlightTextColor);
201 rRenderContext.DrawText(aPoint, aText);
203 else
205 rRenderContext.SetFillColor(aFillColor);
206 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aSize));
208 rRenderContext.SetTextColor(aWindowTextColor);
209 rRenderContext.DrawText(aPoint, aText);
211 rRenderContext.SetFillColor(aFillColor);
212 rRenderContext.SetTextColor(aTextColor);
214 if (bShrankFont)
215 rRenderContext.SetFont(aOrigFont);
218 void SvxCharView::setMouseClickHdl(const Link<SvxCharView*, void>& rLink)
220 maMouseClickHdl = rLink;
223 void SvxCharView::setClearClickHdl(const Link<SvxCharView*, void>& rLink)
225 maClearClickHdl = rLink;
228 void SvxCharView::setClearAllClickHdl(const Link<SvxCharView*, void>& rLink)
230 maClearAllClickHdl = rLink;
233 void SvxCharView::SetFont(const vcl::Font& rFont)
235 tools::Long nWinHeight = GetOutputSizePixel().Height();
236 maFont = rFont;
237 maFont.SetWeight(WEIGHT_NORMAL);
238 maFont.SetAlignment(ALIGN_TOP);
239 maFont.SetFontSize(mxVirDev->PixelToLogic(Size(0, nWinHeight / 2)));
240 maFont.SetTransparent(true);
242 mxVirDev->Push(PUSH_ALLFONT);
243 mxVirDev->SetFont(maFont);
244 mnY = (nWinHeight - mxVirDev->GetTextHeight()) / 2;
245 mxVirDev->Pop();
247 Invalidate();
250 void SvxCharView::Resize()
252 SetFont(GetFont()); //force recalculation of size
255 void SvxCharView::SetText(const OUString& rText)
257 m_sText = rText;
258 Invalidate();
261 void SvxCharView::SetHasInsert(bool bInsert) { maHasInsert = bInsert; }
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */