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