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 
17 namespace basctl
18 {
LineNumberWindow(vcl::Window * pParent,ModulWindow * pModulWindow)19 LineNumberWindow::LineNumberWindow(vcl::Window* pParent, ModulWindow* pModulWindow)
20     : Window(pParent, WB_BORDER)
21     , m_pModulWindow(pModulWindow)
22     , m_nCurYOffset(0)
23 {
24     SetBackground(Wallpaper(GetSettings().GetStyleSettings().GetFieldColor()));
25     m_nBaseWidth = GetTextWidth("8");
26     m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
27 }
28 
~LineNumberWindow()29 LineNumberWindow::~LineNumberWindow() { disposeOnce(); }
30 
dispose()31 void LineNumberWindow::dispose()
32 {
33     m_pModulWindow.clear();
34     Window::dispose();
35 }
36 
Paint(vcl::RenderContext & rRenderContext,const tools::Rectangle &)37 void LineNumberWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
38 {
39     if (SyncYOffset())
40         return;
41 
42     ExtTextEngine* txtEngine = m_pModulWindow->GetEditEngine();
43     if (!txtEngine)
44         return;
45 
46     TextView* txtView = m_pModulWindow->GetEditView();
47     if (!txtView)
48         return;
49 
50     GetParent()->Resize();
51 
52     int windowHeight = rRenderContext.GetOutputSize().Height();
53     int nLineHeight = rRenderContext.GetTextHeight();
54     if (!nLineHeight)
55     {
56         return;
57     }
58 
59     int startY = txtView->GetStartDocPos().Y();
60     const sal_uInt32 nStartLine = startY / nLineHeight + 1;
61     sal_uInt32 nEndLine = (startY + windowHeight) / nLineHeight + 1;
62 
63     if (txtEngine->GetParagraphCount() + 1 < nEndLine)
64         nEndLine = txtEngine->GetParagraphCount() + 1;
65 
66     // FIXME: it would be best if we could get notified of a font change
67     // rather than doing that re-calculation at each Paint event
68     m_nBaseWidth = GetTextWidth("8");
69 
70     // reserve enough for 3 digit minimum, with a bit to spare for comfort
71     m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
72     auto nMaxLineNumber = std::max(nEndLine, txtEngine->GetParagraphCount() + 1);
73     sal_uInt32 i = (nMaxLineNumber + 1) / 1000;
74     while (i)
75     {
76         i /= 10;
77         m_nWidth += m_nBaseWidth;
78     }
79 
80     sal_Int64 y = (nStartLine - 1) * static_cast<sal_Int64>(nLineHeight);
81     for (sal_uInt32 n = nStartLine; n <= nEndLine; ++n, y += nLineHeight)
82         rRenderContext.DrawText(Point(0, y - m_nCurYOffset), OUString::number(n));
83 }
84 
DataChanged(DataChangedEvent const & rDCEvt)85 void LineNumberWindow::DataChanged(DataChangedEvent const& rDCEvt)
86 {
87     Window::DataChanged(rDCEvt);
88     if (rDCEvt.GetType() == DataChangedEventType::SETTINGS
89         && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
90     {
91         Color aColor(GetSettings().GetStyleSettings().GetFieldColor());
92         const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
93         if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetFieldColor())
94         {
95             SetBackground(Wallpaper(aColor));
96             Invalidate();
97         }
98     }
99 }
100 
DoScroll(tools::Long nVertScroll)101 void LineNumberWindow::DoScroll(tools::Long nVertScroll)
102 {
103     m_nCurYOffset -= nVertScroll;
104     Window::Scroll(0, nVertScroll);
105 }
106 
SyncYOffset()107 bool LineNumberWindow::SyncYOffset()
108 {
109     TextView* pView = m_pModulWindow->GetEditView();
110     if (!pView)
111         return false;
112 
113     tools::Long nViewYOffset = pView->GetStartDocPos().Y();
114     if (m_nCurYOffset == nViewYOffset)
115         return false;
116 
117     m_nCurYOffset = nViewYOffset;
118     Invalidate();
119     return true;
120 }
121 
122 } // namespace basctl
123 
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
125