Skip to content

Commit 13ac714

Browse files
committed
Implement reset on double click on the zoomer value
1 parent d74c5bd commit 13ac714

File tree

7 files changed

+155
-38
lines changed

7 files changed

+155
-38
lines changed

src/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@
1212

1313
constexpr int FILENAME_LEN_IN_TITLE = 16;
1414

15-
namespace SliderPercent
16-
{
17-
constexpr const int nDefault = 100;
18-
constexpr const int nMinZoom = 80;
19-
constexpr const int nMaxZoom = 250;
20-
}; // namespace SliderPercent
21-
2215
JsonViewDlg::JsonViewDlg(HINSTANCE hInstance, const NppData& nppData, const bool& isReady, int nCmdId, std::shared_ptr<Setting>& pSetting)
2316
: DockingDlgInterface(IDD_TREEDLG)
2417
, m_NppData(nppData)
2518
, m_IsNppReady(isReady)
2619
, m_nDlgId(nCmdId)
2720
, m_pEditor(std::make_unique<ScintillaEditor>(nppData))
2821
, m_pTreeView(std::make_unique<TreeViewCtrl>())
22+
, m_pTreeViewZoom(std::make_unique<SliderCtrl>())
2923
, m_pSetting(pSetting)
3024
, m_pCurrFileName(std::make_unique<wchar_t[]>(FILENAME_LEN_IN_TITLE))
3125
{
@@ -902,25 +896,14 @@ void JsonViewDlg::EnableControls(const std::vector<DWORD>& ids, bool enable)
902896
EnableWindow(GetDlgItem(getHSelf(), id), enable ? TRUE : FALSE);
903897
}
904898

905-
auto JsonViewDlg::GetSliderPosition() const -> int
899+
auto JsonViewDlg::GetZoomLevel() const -> int
906900
{
907-
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
908-
int pos = static_cast<int>(SendMessage(hSlider, TBM_GETPOS, 0, 0));
909-
910-
return pos;
901+
return m_pTreeViewZoom->GetPosition();
911902
}
912903

913-
void JsonViewDlg::SetSliderPosition(int pos) const
904+
void JsonViewDlg::SetZoomLevel(int pos) const
914905
{
915-
// Set slider position
916-
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
917-
SendMessage(hSlider, TBM_SETPOS, TRUE, pos);
918-
919-
// Set slider position in text value
920-
HWND hZoomPercent = GetDlgItem(getHSelf(), IDC_ZOOM_PERCENT);
921-
wchar_t zoomText[16] {};
922-
wsprintf(zoomText, L"%d%%", pos);
923-
SetWindowText(hZoomPercent, zoomText);
906+
m_pTreeViewZoom->SetPosition(pos);
924907
}
925908

926909
void JsonViewDlg::SetTreeViewZoom(double dwZoomFactor) const
@@ -945,8 +928,8 @@ void JsonViewDlg::SetTreeViewZoom(double dwZoomFactor) const
945928

946929
void JsonViewDlg::UpdateUIOnZoom(int zoomPercentage) const
947930
{
948-
// Update slider
949-
SetSliderPosition(zoomPercentage);
931+
// Update zoom level on slider
932+
SetZoomLevel(zoomPercentage);
950933

951934
// Update the Tree view
952935
double zoomFactor = zoomPercentage / 100.0;
@@ -955,15 +938,17 @@ void JsonViewDlg::UpdateUIOnZoom(int zoomPercentage) const
955938

956939
void JsonViewDlg::HandleZoomOnScroll(WPARAM wParam) const
957940
{
958-
int pos = GetSliderPosition(); // Current slider position
941+
int pos = GetZoomLevel(); // Current zoom level
959942
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
960943

961-
// Adjust zoom based on scroll direction
962-
if (delta > 0 && pos < SliderPercent::nMaxZoom)
944+
const auto& zoomRange = m_pTreeViewZoom->GetRange();
945+
const bool isZoom = delta > 0;
946+
947+
if (isZoom && pos < zoomRange.m_nMaxZoom)
963948
{
964949
pos += 10; // Zoom in
965950
}
966-
else if (delta < 0 && pos > SliderPercent::nMinZoom)
951+
else if (!isZoom && pos > zoomRange.m_nMinZoom)
967952
{
968953
pos -= 10; // Zoom out
969954
}
@@ -1106,18 +1091,13 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
11061091
::SetWindowLongPtr(getHSelf(), GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
11071092

11081093
m_pTreeView->OnInit(getHSelf(), IDC_TREE);
1094+
m_pTreeViewZoom->OnInit(getHSelf(), IDC_ZOOM_SLIDER, IDC_ZOOM_PERCENT);
11091095

11101096
PrepareButtons();
11111097

11121098
// Set default node path as JSON
11131099
SetDlgItemText(_hSelf, IDC_EDT_NODEPATH, JSON_ROOT);
11141100

1115-
// Set slider range from 80% to 200%
1116-
// Set initial position to 100% (no zoom)
1117-
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
1118-
SendMessage(hSlider, TBM_SETRANGE, TRUE, MAKELPARAM(SliderPercent::nMinZoom, SliderPercent::nMaxZoom));
1119-
SendMessage(hSlider, TBM_SETPOS, TRUE, SliderPercent::nDefault);
1120-
11211101
return TRUE;
11221102
}
11231103

@@ -1205,7 +1185,7 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
12051185

12061186
if (reinterpret_cast<HWND>(lParam) == hSlider)
12071187
{
1208-
int pos = GetSliderPosition();
1188+
int pos = m_pTreeViewZoom->GetPosition();
12091189
UpdateUIOnZoom(pos);
12101190

12111191
return TRUE;

src/NppJsonViewer/JsonViewDlg.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "PluginInterface.h"
1010
#include "resource.h"
1111
#include "TreeViewCtrl.h"
12+
#include "SliderCtrl.h"
1213
#include "ScintillaEditor.h"
1314
#include "JsonHandler.h"
1415
#include "JsonNode.h"
@@ -89,8 +90,8 @@ class JsonViewDlg
8990
void ShowControls(const std::vector<DWORD>& ids, bool show);
9091
void EnableControls(const std::vector<DWORD>& ids, bool enable);
9192

92-
auto GetSliderPosition() const -> int;
93-
void SetSliderPosition(int pos) const;
93+
auto GetZoomLevel() const -> int;
94+
void SetZoomLevel(int pos) const;
9495
void SetTreeViewZoom(double dwZoomFactor) const;
9596
void UpdateUIOnZoom(int zoomPercentage) const;
9697
void HandleZoomOnScroll(WPARAM wParam) const;
@@ -124,5 +125,6 @@ class JsonViewDlg
124125
std::unique_ptr<wchar_t[]> m_pCurrFileName;
125126
std::unique_ptr<ScintillaEditor> m_pEditor = nullptr;
126127
std::unique_ptr<TreeViewCtrl> m_pTreeView = nullptr;
128+
std::unique_ptr<SliderCtrl> m_pTreeViewZoom = nullptr;
127129
std::shared_ptr<Setting> m_pSetting = nullptr;
128130
};

src/NppJsonViewer/NPPJSONViewer.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<ClCompile Include="ScintillaEditor.cpp" />
198198
<ClCompile Include="SettingsDlg.cpp" />
199199
<ClCompile Include="ShortcutCommand.cpp" />
200+
<ClCompile Include="SliderCtrl.cpp" />
200201
<ClCompile Include="TreeViewCtrl.cpp" />
201202
</ItemGroup>
202203
<ItemGroup>
@@ -221,6 +222,7 @@
221222
<ClInclude Include="ScintillaEditor.h" />
222223
<ClInclude Include="SettingsDlg.h" />
223224
<ClInclude Include="ShortcutCommand.h" />
225+
<ClInclude Include="SliderCtrl.h" />
224226
<ClInclude Include="StopWatch.h" />
225227
<ClCompile Include="TreeHandler.h" />
226228
<ClInclude Include="TrackingStream.h" />

src/NppJsonViewer/NPPJSONViewer.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<ClCompile Include="TreeHandler.h">
6161
<Filter>Header Files</Filter>
6262
</ClCompile>
63+
<ClCompile Include="SliderCtrl.cpp">
64+
<Filter>Source Files</Filter>
65+
</ClCompile>
6366
</ItemGroup>
6467
<ItemGroup>
6568
<ClInclude Include="resource.h">
@@ -134,6 +137,9 @@
134137
<ClInclude Include="TrackingStream.h">
135138
<Filter>Header Files</Filter>
136139
</ClInclude>
140+
<ClInclude Include="SliderCtrl.h">
141+
<Filter>Header Files</Filter>
142+
</ClInclude>
137143
</ItemGroup>
138144
<ItemGroup>
139145
<ResourceCompile Include="resource.rc">

src/NppJsonViewer/SliderCtrl.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "SliderCtrl.h"
2+
3+
#include <string>
4+
5+
SliderCtrl::SliderCtrl(const SliderRange& sliderRange)
6+
: m_sliderRange(sliderRange)
7+
{
8+
}
9+
10+
SliderCtrl::~SliderCtrl()
11+
{
12+
// Restore the original window procedure on cleanup
13+
SetWindowLongPtr(m_hSelf, GWLP_WNDPROC, (LONG_PTR)m_oldSliderProc);
14+
}
15+
16+
void SliderCtrl::OnInit(HWND hParent, int sliderID, int sliderInfoID)
17+
{
18+
m_hParent = hParent;
19+
m_hSelf = GetDlgItem(m_hParent, sliderID);
20+
m_hSelfInfo = GetDlgItem(m_hParent, sliderInfoID);
21+
22+
// Set slider range and initial position
23+
SendMessage(m_hSelf, TBM_SETRANGE, TRUE, MAKELPARAM(m_sliderRange.m_nMinZoom, m_sliderRange.m_nMaxZoom));
24+
SendMessage(m_hSelf, TBM_SETPOS, TRUE, m_sliderRange.m_nDefault);
25+
26+
UpdateInfo(m_sliderRange.m_nDefault);
27+
28+
// Subclass the slider control to handle double-click events
29+
m_oldSliderProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(m_hSelfInfo, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(runWinProc)));
30+
31+
// Save this instance for access in the static window procedure
32+
SetWindowLongPtr(m_hSelfInfo, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
33+
}
34+
35+
auto SliderCtrl::GetPosition() const -> int
36+
{
37+
int pos = static_cast<int>(SendMessage(m_hSelf, TBM_GETPOS, 0, 0));
38+
return pos;
39+
}
40+
41+
void SliderCtrl::SetPosition(int pos) const
42+
{
43+
// Set slider position
44+
SendMessage(m_hSelf, TBM_SETPOS, TRUE, pos);
45+
46+
// Set slider position in text value
47+
UpdateInfo(pos);
48+
}
49+
50+
void SliderCtrl::UpdateInfo(int zoomPercentage) const
51+
{
52+
std::wstring sliderInfoText = std::to_wstring(zoomPercentage) + L"%";
53+
SetWindowText(m_hSelfInfo, sliderInfoText.c_str());
54+
}
55+
56+
LRESULT SliderCtrl::runWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
57+
{
58+
SliderCtrl* pThis = reinterpret_cast<SliderCtrl*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
59+
60+
if (pThis)
61+
{
62+
if (message == WM_LBUTTONDBLCLK)
63+
{
64+
// Reset slider to 100% on double-click
65+
// Also notify its parent to adjust tree view as well
66+
pThis->SetPosition(100);
67+
SendMessage(pThis->m_hParent, WM_HSCROLL, NULL, reinterpret_cast<LPARAM>(pThis->m_hSelf));
68+
}
69+
70+
// Call the original window procedure for other messages
71+
return CallWindowProc(pThis->m_oldSliderProc, hWnd, message, wParam, lParam);
72+
}
73+
74+
return FALSE;
75+
}

src/NppJsonViewer/SliderCtrl.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
4+
#include <Windows.h>
5+
#include <CommCtrl.h>
6+
7+
struct SliderRange
8+
{
9+
int m_nDefault = 100;
10+
int m_nMinZoom = 80;
11+
int m_nMaxZoom = 250;
12+
}; // namespace SliderRange
13+
14+
class SliderCtrl
15+
{
16+
HWND m_hParent = nullptr;
17+
HWND m_hSelf = nullptr;
18+
HWND m_hSelfInfo = nullptr;
19+
HWND m_hTreeView = nullptr;
20+
WNDPROC m_oldSliderProc = nullptr;
21+
const SliderRange m_sliderRange {};
22+
23+
public:
24+
explicit SliderCtrl(const SliderRange& sliderRange = {});
25+
26+
~SliderCtrl();
27+
28+
void OnInit(HWND hParent, int sliderID, int sliderInfoID);
29+
30+
HWND GetSliderHandle() const
31+
{
32+
return m_hSelf;
33+
}
34+
35+
HWND GetSliderInfoHandle() const
36+
{
37+
return m_hSelfInfo;
38+
}
39+
40+
auto GetRange() const -> const SliderRange&
41+
{
42+
return m_sliderRange;
43+
}
44+
45+
auto GetPosition() const -> int;
46+
void SetPosition(int pos) const;
47+
void UpdateInfo(int zoomPercentage) const;
48+
49+
private:
50+
// Static window procedure for the slider
51+
static INT_PTR CALLBACK runWinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
52+
};

src/NppJsonViewer/resource.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ BEGIN
8989
EDITTEXT IDC_EDT_SEARCH,60,2,99,12,ES_AUTOHSCROLL
9090
PUSHBUTTON "",IDC_BTN_SEARCH,160,2,16,12,BS_ICON
9191
CONTROL "",IDC_ZOOM_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | TBS_NOTICKS,2,16,58,12
92-
RTEXT "100%",IDC_ZOOM_PERCENT,60,16,22,12,NOT WS_GROUP,WS_EX_RIGHT
92+
RTEXT "100%",IDC_ZOOM_PERCENT,60,16,22,12,SS_NOTIFY | NOT WS_GROUP,WS_EX_RIGHT
9393
CONTROL "",IDC_TREE,"SysTreeView32",TVS_HASBUTTONS | TVS_HASLINES | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS | TVS_FULLROWSELECT | WS_HSCROLL | WS_TABSTOP,2,30,173,264,WS_EX_CLIENTEDGE
9494
EDITTEXT IDC_EDT_NODEPATH,2,296,173,12,ES_AUTOHSCROLL | ES_READONLY
9595
END

0 commit comments

Comments
 (0)