Bump version to 24.04.3.4
[LibreOffice.git] / basctl / source / basicide / linenumberwindow.cxx
blob18420199e2a9e4d4769a3b3350185622fb299206
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/.
8 */
10 #include "baside2.hxx"
12 #include <vcl/event.hxx>
13 #include <vcl/textview.hxx>
14 #include <vcl/xtextedt.hxx>
15 #include <vcl/settings.hxx>
17 namespace basctl
19 LineNumberWindow::LineNumberWindow(vcl::Window* pParent, ModulWindow* pModulWindow)
20 : Window(pParent, WB_BORDER)
21 , m_pModulWindow(pModulWindow)
22 , m_nCurYOffset(0)
24 const Wallpaper aBackground(GetSettings().GetStyleSettings().GetWindowColor());
25 SetBackground(aBackground);
26 GetWindow(GetWindowType::Border)->SetBackground(aBackground);
27 m_FontColor = GetSettings().GetStyleSettings().GetWindowTextColor();
28 m_nBaseWidth = GetTextWidth("8");
29 m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
32 LineNumberWindow::~LineNumberWindow() { disposeOnce(); }
34 void LineNumberWindow::dispose()
36 m_pModulWindow.clear();
37 Window::dispose();
40 void LineNumberWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
42 if (SyncYOffset())
43 return;
45 ExtTextEngine* txtEngine = m_pModulWindow->GetEditEngine();
46 if (!txtEngine)
47 return;
49 TextView* txtView = m_pModulWindow->GetEditView();
50 if (!txtView)
51 return;
53 int windowHeight = rRenderContext.GetOutputSize().Height();
54 int nLineHeight = rRenderContext.GetTextHeight();
55 if (!nLineHeight)
57 return;
60 int startY = txtView->GetStartDocPos().Y();
61 const sal_uInt32 nStartLine = startY / nLineHeight + 1;
62 sal_uInt32 nEndLine = (startY + windowHeight) / nLineHeight + 1;
64 if (txtEngine->GetParagraphCount() + 1 < nEndLine)
65 nEndLine = txtEngine->GetParagraphCount() + 1;
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
69 m_nBaseWidth = GetTextWidth("8");
71 // reserve enough for 3 digit minimum, with a bit to spare for comfort
72 m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
73 auto nMaxLineNumber = std::max(nEndLine, txtEngine->GetParagraphCount() + 1);
74 sal_uInt32 i = (nMaxLineNumber + 1) / 1000;
75 while (i)
77 i /= 10;
78 m_nWidth += m_nBaseWidth;
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)
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);
92 // Resize the parent after calculating the new width and height values
93 GetParent()->Resize();
96 void LineNumberWindow::DataChanged(DataChangedEvent const& rDCEvt)
98 Window::DataChanged(rDCEvt);
99 if (rDCEvt.GetType() == DataChangedEventType::SETTINGS
100 && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
102 Color aColor(GetSettings().GetStyleSettings().GetFieldColor());
103 const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
104 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetFieldColor())
106 SetBackground(Wallpaper(aColor));
107 Invalidate();
112 void LineNumberWindow::DoScroll(tools::Long nVertScroll)
114 m_nCurYOffset -= nVertScroll;
115 Window::Scroll(0, nVertScroll);
118 bool LineNumberWindow::SyncYOffset()
120 TextView* pView = m_pModulWindow->GetEditView();
121 if (!pView)
122 return false;
124 tools::Long nViewYOffset = pView->GetStartDocPos().Y();
125 if (m_nCurYOffset == nViewYOffset)
126 return false;
128 m_nCurYOffset = nViewYOffset;
129 Invalidate();
130 return true;
133 } // namespace basctl
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */