LibreOffice Module basctl (master) 1
linenumberwindow.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
10#include "baside2.hxx"
11
12#include <vcl/event.hxx>
13#include <vcl/textview.hxx>
14#include <vcl/xtextedt.hxx>
15#include <vcl/settings.hxx>
16
17namespace basctl
18{
20 : Window(pParent, WB_BORDER)
21 , m_pModulWindow(pModulWindow)
22 , m_nCurYOffset(0)
23{
24 const Wallpaper aBackground(GetSettings().GetStyleSettings().GetWindowColor());
25 SetBackground(aBackground);
26 GetWindow(GetWindowType::Border)->SetBackground(aBackground);
30}
31
33
35{
36 m_pModulWindow.clear();
37 Window::dispose();
38}
39
41{
42 if (SyncYOffset())
43 return;
44
45 ExtTextEngine* txtEngine = m_pModulWindow->GetEditEngine();
46 if (!txtEngine)
47 return;
48
49 TextView* txtView = m_pModulWindow->GetEditView();
50 if (!txtView)
51 return;
52
53 int windowHeight = rRenderContext.GetOutputSize().Height();
54 int nLineHeight = rRenderContext.GetTextHeight();
55 if (!nLineHeight)
56 {
57 return;
58 }
59
60 int startY = txtView->GetStartDocPos().Y();
61 const sal_uInt32 nStartLine = startY / nLineHeight + 1;
62 sal_uInt32 nEndLine = (startY + windowHeight) / nLineHeight + 1;
63
64 if (txtEngine->GetParagraphCount() + 1 < nEndLine)
65 nEndLine = txtEngine->GetParagraphCount() + 1;
66
67 // FIXME: it would be best if we could get notified of a font change
68 // rather than doing that re-calculation at each Paint event
70
71 // reserve enough for 3 digit minimum, with a bit to spare for comfort
73 auto nMaxLineNumber = std::max(nEndLine, txtEngine->GetParagraphCount() + 1);
74 sal_uInt32 i = (nMaxLineNumber + 1) / 1000;
75 while (i)
76 {
77 i /= 10;
79 }
80
81 sal_Int64 y = (nStartLine - 1) * static_cast<sal_Int64>(nLineHeight);
82 rRenderContext.SetTextColor(m_FontColor);
83 for (sal_uInt32 n = nStartLine; n <= nEndLine; ++n, y += nLineHeight)
84 {
85 const OUString aLineNumber = OUString::number(n);
86 // tdf#153798 - align line numbers to the right
87 rRenderContext.DrawText(
88 Point(m_nWidth - GetTextWidth(aLineNumber) - m_nBaseWidth / 2, y - m_nCurYOffset),
89 aLineNumber);
90 }
91
92 // Resize the parent after calculating the new width and height values
93 GetParent()->Resize();
94}
95
97{
98 Window::DataChanged(rDCEvt);
99 if (rDCEvt.GetType() == DataChangedEventType::SETTINGS
100 && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
101 {
102 Color aColor(GetSettings().GetStyleSettings().GetFieldColor());
103 const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
104 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetFieldColor())
105 {
106 SetBackground(Wallpaper(aColor));
107 Invalidate();
108 }
109 }
110}
111
113{
114 m_nCurYOffset -= nVertScroll;
115 Window::Scroll(0, nVertScroll);
116}
117
119{
120 TextView* pView = m_pModulWindow->GetEditView();
121 if (!pView)
122 return false;
123
124 tools::Long nViewYOffset = pView->GetStartDocPos().Y();
125 if (m_nCurYOffset == nViewYOffset)
126 return false;
127
128 m_nCurYOffset = nViewYOffset;
129 Invalidate();
130 return true;
131}
132
133} // namespace basctl
134
135/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const StyleSettings & GetStyleSettings() const
DataChangedEventType GetType() const
AllSettingsFlags GetFlags() const
const AllSettings * GetOldSettings() const
Size GetOutputSize() const
void SetTextColor(const Color &rColor)
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)
constexpr tools::Long Y() const
constexpr tools::Long Height() const
const Color & GetFieldColor() const
const Color & GetWindowTextColor() const
sal_uInt32 GetParagraphCount() const
const Point & GetStartDocPos() const
VclPtr< ModulWindow > m_pModulWindow
void DoScroll(tools::Long nVertScroll)
virtual void DataChanged(DataChangedEvent const &rDCEvt) override
LineNumberWindow(vcl::Window *pParent, ModulWindow *pModulWin)
virtual void dispose() override
virtual ~LineNumberWindow() override
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
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
vcl::Window * GetParent() const
virtual void Resize()
vcl::Window * GetWindow(GetWindowType nType) const
const AllSettings & GetSettings() const
void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
void SetBackground()
float y
sal_Int64 n
int i
long Long
WinBits const WB_BORDER