Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / basctl / source / basicide / linenumberwindow.cxx
blob257ac8fd1a8d1820ef318eaab475f6bd1f622c8a
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/xtextedt.hxx>
13 #include <vcl/settings.hxx>
15 namespace basctl
18 LineNumberWindow::LineNumberWindow (vcl::Window* pParent, ModulWindow* pModulWindow) :
19 Window(pParent, WB_BORDER),
20 m_pModulWindow(pModulWindow),
21 m_nCurYOffset(0)
23 SetBackground(Wallpaper(GetSettings().GetStyleSettings().GetFieldColor()));
24 m_nBaseWidth = GetTextWidth("8");
25 m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
28 LineNumberWindow::~LineNumberWindow()
30 disposeOnce();
33 void LineNumberWindow::dispose()
35 m_pModulWindow.clear();
36 Window::dispose();
39 void LineNumberWindow::Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle&)
41 if(SyncYOffset())
42 return;
44 ExtTextEngine* txtEngine = m_pModulWindow->GetEditEngine();
45 if (!txtEngine)
46 return;
48 TextView* txtView = m_pModulWindow->GetEditView();
49 if (!txtView)
50 return;
52 GetParent()->Resize();
54 int windowHeight = rRenderContext.GetOutputSize().Height();
55 int nLineHeight = rRenderContext.GetTextHeight();
56 if (!nLineHeight)
58 return;
61 int startY = txtView->GetStartDocPos().Y();
62 const sal_uInt32 nStartLine = startY / nLineHeight + 1;
63 sal_uInt32 nEndLine = (startY + windowHeight) / nLineHeight + 1;
65 if (txtEngine->GetParagraphCount() + 1 < nEndLine)
66 nEndLine = txtEngine->GetParagraphCount() + 1;
68 // FIXME: it would be best if we could get notified of a font change
69 // rather than doing that re-calculation at each Paint event
70 m_nBaseWidth = GetTextWidth("8");
72 // reserve enough for 3 digit minimum, with a bit to spare for comfort
73 m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
74 sal_uInt32 i = (nEndLine + 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 for (sal_uInt32 n = nStartLine; n <= nEndLine; ++n, y += nLineHeight)
83 rRenderContext.DrawText(Point(0, y - m_nCurYOffset), OUString::number(n));
86 void LineNumberWindow::DataChanged(DataChangedEvent const & rDCEvt)
88 Window::DataChanged(rDCEvt);
89 if (rDCEvt.GetType() == DataChangedEventType::SETTINGS
90 && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))
92 Color aColor(GetSettings().GetStyleSettings().GetFieldColor());
93 const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
94 if (!pOldSettings || aColor != pOldSettings->GetStyleSettings().GetFieldColor())
96 SetBackground(Wallpaper(aColor));
97 Invalidate();
102 void LineNumberWindow::DoScroll(long nVertScroll)
104 m_nCurYOffset -= nVertScroll;
105 Window::Scroll(0, nVertScroll);
109 bool LineNumberWindow::SyncYOffset()
111 TextView* pView = m_pModulWindow->GetEditView();
112 if (!pView)
113 return false;
115 long nViewYOffset = pView->GetStartDocPos().Y();
116 if (m_nCurYOffset == nViewYOffset)
117 return false;
119 m_nCurYOffset = nViewYOffset;
120 Invalidate();
121 return true;
125 } // namespace basctl
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */