tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / basctl / source / basicide / linenumberwindow.cxx
blob3f9f97d0e173d071d1bc46b94e84a475b45e18e5
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 // tdf#153853 The line number window does not need to be affected by RTL
25 EnableRTL(false);
27 const Wallpaper aBackground(GetSettings().GetStyleSettings().GetWindowColor());
28 SetBackground(aBackground);
29 GetWindow(GetWindowType::Border)->SetBackground(aBackground);
30 m_FontColor = GetSettings().GetStyleSettings().GetWindowTextColor();
31 m_HighlightColor = GetSettings().GetStyleSettings().GetFaceColor();
32 m_nBaseWidth = GetTextWidth(u"8"_ustr);
33 m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
36 LineNumberWindow::~LineNumberWindow() { disposeOnce(); }
38 void LineNumberWindow::dispose()
40 m_pModulWindow.clear();
41 Window::dispose();
44 void LineNumberWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
46 if (SyncYOffset())
47 return;
49 ExtTextEngine* txtEngine = m_pModulWindow->GetEditEngine();
50 if (!txtEngine)
51 return;
53 TextView* txtView = m_pModulWindow->GetEditView();
54 if (!txtView)
55 return;
57 int windowHeight = rRenderContext.GetOutputSize().Height();
58 int windowWidth = rRenderContext.GetOutputSize().Width();
59 int nLineHeight = rRenderContext.GetTextHeight();
60 if (!nLineHeight)
62 return;
65 int startY = txtView->GetStartDocPos().Y();
66 const sal_uInt32 nStartLine = startY / nLineHeight + 1;
67 sal_uInt32 nEndLine = (startY + windowHeight) / nLineHeight + 1;
69 if (txtEngine->GetParagraphCount() + 1 < nEndLine)
70 nEndLine = txtEngine->GetParagraphCount() + 1;
72 // FIXME: it would be best if we could get notified of a font change
73 // rather than doing that re-calculation at each Paint event
74 m_nBaseWidth = GetTextWidth(u"8"_ustr);
76 // reserve enough for 3 digit minimum, with a bit to spare for comfort
77 m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
78 auto nMaxLineNumber = std::max(nEndLine, txtEngine->GetParagraphCount() + 1);
79 sal_uInt32 i = (nMaxLineNumber + 1) / 1000;
80 while (i)
82 i /= 10;
83 m_nWidth += m_nBaseWidth;
86 vcl::Font aNormalFont = rRenderContext.GetFont();
87 vcl::Font aBoldFont(aNormalFont);
88 aBoldFont.SetWeight(FontWeight::WEIGHT_BOLD);
90 sal_uInt32 nParaEnd = txtView->GetSelection().GetEnd().GetPara() + 1;
91 sal_Int64 y = (nStartLine - 1) * static_cast<sal_Int64>(nLineHeight);
93 for (sal_uInt32 n = nStartLine; n <= nEndLine; ++n, y += nLineHeight)
95 // Font weight for the selected lines is bold
96 if (n == nParaEnd)
98 tools::Rectangle aRect(Point(0, y - m_nCurYOffset),
99 Point(windowWidth, y - m_nCurYOffset + nLineHeight));
100 rRenderContext.SetFillColor(m_HighlightColor);
101 rRenderContext.DrawRect(aRect);
102 rRenderContext.SetFont(aBoldFont);
104 else
106 rRenderContext.SetFont(aNormalFont);
109 rRenderContext.SetTextColor(m_FontColor);
110 const OUString aLineNumber = OUString::number(n);
111 // tdf#153798 - align line numbers to the right
112 rRenderContext.DrawText(
113 Point(m_nWidth - GetTextWidth(aLineNumber) - m_nBaseWidth / 2, y - m_nCurYOffset),
114 aLineNumber);
116 // Restore the original font
117 rRenderContext.SetFont(aNormalFont);
119 // Resize the parent after calculating the new width and height values
120 GetParent()->Resize();
123 void LineNumberWindow::DataChanged(DataChangedEvent const& rDCEvt)
125 Window::DataChanged(rDCEvt);
126 if (rDCEvt.GetType() == DataChangedEventType::SETTINGS
127 && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
129 Color aColor(GetSettings().GetStyleSettings().GetFieldColor());
130 const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
131 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetFieldColor())
133 SetBackground(Wallpaper(aColor));
134 Invalidate();
139 void LineNumberWindow::DoScroll(tools::Long nVertScroll)
141 m_nCurYOffset -= nVertScroll;
142 Window::Scroll(0, nVertScroll);
145 bool LineNumberWindow::SyncYOffset()
147 TextView* pView = m_pModulWindow->GetEditView();
148 if (!pView)
149 return false;
151 tools::Long nViewYOffset = pView->GetStartDocPos().Y();
152 if (m_nCurYOffset == nViewYOffset)
153 return false;
155 m_nCurYOffset = nViewYOffset;
156 Invalidate();
157 return true;
160 } // namespace basctl
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */