Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit e7e0a2b

Browse files
authored
Merge pull request #24 from nea/master
Added: GetScrollInfo functionality for the Scintilla main view
2 parents 80f9c8a + 8452a45 commit e7e0a2b

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

Demo Plugin/NppManagedPluginDemo/Demo.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.InteropServices;
1010
using System.Text.RegularExpressions;
1111
using Kbg.NppPluginNET.PluginInfrastructure;
12+
using static Kbg.NppPluginNET.PluginInfrastructure.Win32;
1213

1314
namespace Kbg.NppPluginNET
1415
{
@@ -126,6 +127,24 @@ static internal void CommandMenuInit()
126127
PluginBase.SetCommand(14, "---", null);
127128

128129
PluginBase.SetCommand(15, "Dockable Dialog Demo", DockableDlgDemo); idFrmGotToLine = 15;
130+
131+
PluginBase.SetCommand(16, "---", null);
132+
133+
PluginBase.SetCommand(17, "Print Scroll and Row Information", PrintScrollInformation);
134+
}
135+
136+
/// <summary>
137+
///
138+
/// </summary>
139+
static void PrintScrollInformation()
140+
{
141+
ScrollInfo scrollInfo = editor.GetScrollInfo(ScrollInfoMask.SIF_RANGE | ScrollInfoMask.SIF_TRACKPOS | ScrollInfoMask.SIF_PAGE, ScrollInfoBar.SB_VERT);
142+
var scrollRatio = (double)scrollInfo.nTrackPos / (scrollInfo.nMax - scrollInfo.nPage);
143+
var scrollPercentage = Math.Min(scrollRatio, 1) * 100;
144+
editor.ReplaceSel($@"The maximum row in the current document was {scrollInfo.nMax+1}.
145+
A maximum of {scrollInfo.nPage} rows is visible at a time.
146+
The current scroll ratio is {Math.Round(scrollPercentage, 2)}%.
147+
");
129148
}
130149

131150
static internal void SetToolBarIcon()

Visual Studio Project Template C#/PluginInfrastructure/IScintillaGateway.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// NPP plugin platform for .Net v0.93.96 by Kasper B. Graversen etc.
22
using System;
3+
using static Kbg.NppPluginNET.PluginInfrastructure.Win32;
34

45
namespace Kbg.NppPluginNET.PluginInfrastructure
56
{
@@ -23,6 +24,13 @@ public interface IScintillaGateway
2324
/// </summary>
2425
int GetCurrentLineNumber();
2526

27+
/// <summary>
28+
/// Get the scroll information for the current Scintilla window.
29+
/// </summary>
30+
/// <param name="mask">Arguments for the scroll information such as tracking</param>
31+
/// <param name="scrollBar">Which scroll bar information are you looking for</param>
32+
/// <returns>A ScrollInfo struct with information of the current scroll state</returns>
33+
ScrollInfo GetScrollInfo(ScrollInfoMask mask = ScrollInfoMask.SIF_ALL, ScrollInfoBar scrollBar = ScrollInfoBar.SB_BOTH);
2634

2735
/* ++Autogenerated -- start of section automatically generated from Scintilla.iface */
2836
/// <summary>Add text to the document at current position. (Scintilla feature 2001)</summary>

Visual Studio Project Template C#/PluginInfrastructure/ScintillaGateway.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// NPP plugin platform for .Net v0.93.96 by Kasper B. Graversen etc.
22
using System;
3+
using System.Runtime.InteropServices;
34
using System.Text;
5+
using static Kbg.NppPluginNET.PluginInfrastructure.Win32;
46

57
namespace Kbg.NppPluginNET.PluginInfrastructure
68
{
@@ -66,6 +68,21 @@ public int GetCurrentLineNumber()
6668
return LineFromPosition(GetCurrentPos());
6769
}
6870

71+
/// <summary>
72+
/// Get the scroll information for the current Scintilla window.
73+
/// </summary>
74+
/// <param name="mask">Arguments for the scroll information such as tracking</param>
75+
/// <param name="scrollBar">Which scroll bar information are you looking for</param>
76+
/// <returns>A ScrollInfo struct with information of the current scroll state</returns>
77+
public ScrollInfo GetScrollInfo(ScrollInfoMask mask = ScrollInfoMask.SIF_ALL, ScrollInfoBar scrollBar = ScrollInfoBar.SB_BOTH)
78+
{
79+
ScrollInfo scrollInfo = new ScrollInfo();
80+
scrollInfo.cbSize = (uint)Marshal.SizeOf(scrollInfo);
81+
scrollInfo.fMask = (uint)mask;
82+
Win32.GetScrollInfo(scintilla, (int)scrollBar, ref scrollInfo);
83+
return scrollInfo;
84+
}
85+
6986
/* ++Autogenerated -- start of section automatically generated from Scintilla.iface */
7087
/// <summary>Add text to the document at current position. (Scintilla feature 2001)</summary>
7188
public unsafe void AddText(int length, string text)

Visual Studio Project Template C#/PluginInfrastructure/Win32.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,76 @@ namespace Kbg.NppPluginNET.PluginInfrastructure
88
{
99
public class Win32
1010
{
11+
/// <summary>
12+
/// Get the scroll information of a scroll bar or window with scroll bar
13+
/// @see https://msdn.microsoft.com/en-us/library/windows/desktop/bb787537(v=vs.85).aspx
14+
/// </summary>
15+
[StructLayout(LayoutKind.Sequential)]
16+
public struct ScrollInfo
17+
{
18+
/// <summary>
19+
/// Specifies the size, in bytes, of this structure. The caller must set this to sizeof(SCROLLINFO).
20+
/// </summary>
21+
public uint cbSize;
22+
/// <summary>
23+
/// Specifies the scroll bar parameters to set or retrieve.
24+
/// @see ScrollInfoMask
25+
/// </summary>
26+
public uint fMask;
27+
/// <summary>
28+
/// Specifies the minimum scrolling position.
29+
/// </summary>
30+
public int nMin;
31+
/// <summary>
32+
/// Specifies the maximum scrolling position.
33+
/// </summary>
34+
public int nMax;
35+
/// <summary>
36+
/// Specifies the page size, in device units. A scroll bar uses this value to determine the appropriate size of the proportional scroll box.
37+
/// </summary>
38+
public uint nPage;
39+
/// <summary>
40+
/// Specifies the position of the scroll box.
41+
/// </summary>
42+
public int nPos;
43+
/// <summary>
44+
/// Specifies the immediate position of a scroll box that the user is dragging.
45+
/// An application can retrieve this value while processing the SB_THUMBTRACK request code.
46+
/// An application cannot set the immediate scroll position; the SetScrollInfo function ignores this member.
47+
/// </summary>
48+
public int nTrackPos;
49+
}
50+
51+
/// <summary>
52+
/// Used for the ScrollInfo fMask
53+
/// SIF_ALL => Combination of SIF_PAGE, SIF_POS, SIF_RANGE, and SIF_TRACKPOS.
54+
/// SIF_DISABLENOSCROLL => This value is used only when setting a scroll bar's parameters. If the scroll bar's new parameters make the scroll bar unnecessary, disable the scroll bar instead of removing it.
55+
/// SIF_PAGE => The nPage member contains the page size for a proportional scroll bar.
56+
/// SIF_POS => The nPos member contains the scroll box position, which is not updated while the user drags the scroll box.
57+
/// SIF_RANGE => The nMin and nMax members contain the minimum and maximum values for the scrolling range.
58+
/// SIF_TRACKPOS => The nTrackPos member contains the current position of the scroll box while the user is dragging it.
59+
/// </summary>
60+
public enum ScrollInfoMask
61+
{
62+
SIF_RANGE = 0x1,
63+
SIF_PAGE = 0x2,
64+
SIF_POS = 0x4,
65+
SIF_DISABLENOSCROLL = 0x8,
66+
SIF_TRACKPOS = 0x10,
67+
SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
68+
}
69+
70+
/// <summary>
71+
/// Used for the GetScrollInfo() nBar parameter
72+
/// </summary>
73+
public enum ScrollInfoBar
74+
{
75+
SB_HORZ = 0,
76+
SB_VERT = 1,
77+
SB_CTL = 2,
78+
SB_BOTH = 3
79+
}
80+
1181
/// <summary>
1282
/// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
1383
/// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
@@ -229,5 +299,15 @@ public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, ref LangType
229299

230300
[DllImport("kernel32")]
231301
public static extern void OutputDebugString(string lpOutputString);
302+
303+
/// <summary>
304+
/// @see https://msdn.microsoft.com/en-us/library/windows/desktop/bb787583(v=vs.85).aspx
305+
/// </summary>
306+
/// <param name="hwnd"></param>
307+
/// <param name="nBar"></param>
308+
/// <param name="scrollInfo"></param>
309+
/// <returns></returns>
310+
[DllImport("user32")]
311+
public static extern int GetScrollInfo(IntPtr hwnd, int nBar, ref ScrollInfo scrollInfo);
232312
}
233313
}

0 commit comments

Comments
 (0)