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