Skip to content

Commit 8fd7eff

Browse files
committed
Add an API to get and set splitter positions as ratios in the WinWebDiff window
1 parent bf45b4f commit 8fd7eff

1 file changed

Lines changed: 38 additions & 67 deletions

File tree

src/WinWebDiffLib/WebDiffWindow.hpp

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,8 @@ class CWebDiffWindow : public IWebDiffWindow
8181
{
8282
HRESULT hr = S_OK;
8383
m_nPanes = nPanes;
84-
if (m_splitterRatios[0] < 0.0)
85-
{
86-
for (int i = 0; i < nPanes - 1; ++i)
87-
m_splitterRatios[i] = 1.0 / nPanes;
88-
}
84+
for (int i = 0; i < nPanes - 1; ++i)
85+
m_splitterRatios[i] = (m_requestedSplitterRatios[i] < 0) ? 1.0 / nPanes : m_requestedSplitterRatios[i];
8986
if (m_hWnd)
9087
{
9188
Close();
@@ -220,7 +217,7 @@ class CWebDiffWindow : public IWebDiffWindow
220217
RaiseEvent(ev);
221218
});
222219
}
223-
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit);
220+
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit, m_splitterRatios);
224221
for (int i = 0; i < m_nPanes; ++i)
225222
m_webWindow[i].SetWindowRect(rects[i]);
226223
}
@@ -427,7 +424,7 @@ class CWebDiffWindow : public IWebDiffWindow
427424
if (!m_hWnd)
428425
return;
429426
m_bHorizontalSplit = horizontalSplit;
430-
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit);
427+
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit, m_splitterRatios);
431428
for (int i = 0; i < m_nPanes; ++i)
432429
m_webWindow[i].SetWindowRect(rects[i]);
433430
}
@@ -441,22 +438,28 @@ class CWebDiffWindow : public IWebDiffWindow
441438

442439
void SetSplitterRatios(const double* ratios, int count) override
443440
{
444-
if (count > std::size(m_splitterRatios) || !m_hWnd || !ratios || count < 1)
441+
if (count > std::size(m_splitterRatios) || !ratios || count < 1)
445442
return;
446443
for (int i = 0; i < count; ++i)
447444
{
448445
if (ratios[i] >= 0.0 && ratios[i] <= 1.0)
446+
{
449447
m_splitterRatios[i] = ratios[i];
448+
m_requestedSplitterRatios[i] = ratios[i];
449+
}
450450
}
451451
ApplySplitterRatios();
452452
}
453453

454454
void ResetSplitterRatios() override
455455
{
456-
if (!m_hWnd || m_nPanes < 2)
456+
if (m_nPanes < 2)
457457
return;
458458
for (int i = 0; i < m_nPanes - 1; ++i)
459+
{
459460
m_splitterRatios[i] = 1.0 / m_nPanes;
461+
m_requestedSplitterRatios[i] = -1.0;
462+
}
460463
ApplySplitterRatios();
461464
}
462465

@@ -1339,76 +1342,33 @@ class CWebDiffWindow : public IWebDiffWindow
13391342
return path;
13401343
}
13411344

1342-
std::vector<RECT> CalcChildWebWindowRect(HWND hWnd, int nPanes, bool bHorizontalSplit)
1345+
std::vector<RECT> CalcChildWebWindowRect(HWND hWnd, int nPanes, bool bHorizontalSplit, const double* ratios)
13431346
{
1344-
std::vector<RECT> childrects;
1347+
std::vector<RECT> rects;
13451348
RECT rcParent;
13461349
GetClientRect(hWnd, &rcParent);
1347-
RECT rc = rcParent;
1348-
if (nPanes > 0)
1349-
{
1350-
if (!bHorizontalSplit)
1351-
{
1352-
int cx = 0; //GetSystemMetrics(SM_CXVSCROLL);
1353-
int width = (rcParent.left + rcParent.right - cx) / nPanes - 2;
1354-
rc.left = 0;
1355-
rc.right = rc.left + width;
1356-
for (int i = 0; i < nPanes - 1; ++i)
1357-
{
1358-
childrects.push_back(rc);
1359-
rc.left = rc.right + 2 * 2;
1360-
rc.right = rc.left + width;
1361-
}
1362-
rc.right = rcParent.right;
1363-
rc.left = rc.right - width - cx;
1364-
childrects.push_back(rc);
1365-
}
1366-
else
1367-
{
1368-
int cy = 0; // GetSystemMetrics(SM_CXVSCROLL);
1369-
int height = (rcParent.top + rcParent.bottom - cy) / nPanes - 2;
1370-
rc.top = 0;
1371-
rc.bottom = rc.top + height;
1372-
for (int i = 0; i < nPanes - 1; ++i)
1373-
{
1374-
childrects.push_back(rc);
1375-
rc.top = rc.bottom + 2 * 2;
1376-
rc.bottom = rc.top + height;
1377-
}
1378-
rc.bottom = rcParent.bottom;
1379-
rc.top = rc.bottom - height - cy;
1380-
childrects.push_back(rc);
1381-
}
1382-
}
1383-
return childrects;
1384-
}
13851350

1386-
void ApplySplitterRatios()
1387-
{
1388-
if (!m_hWnd || m_nPanes < 2)
1389-
return;
1390-
1391-
RECT rcParent;
1392-
GetClientRect(m_hWnd, &rcParent);
1351+
if (nPanes < 1)
1352+
return rects;
13931353

1394-
std::vector<RECT> rects;
1395-
rects.resize(m_nPanes);
1354+
rects.resize(nPanes);
13961355

1397-
if (!m_bHorizontalSplit)
1356+
if (!bHorizontalSplit)
13981357
{
1399-
// Vertical split: distribute width based on ratios
1358+
// Vertical split
14001359
int cx = 0;
14011360
int totalWidth = rcParent.right - rcParent.left - cx;
14021361
int accumulatedWidth = 0;
14031362

1404-
for (int i = 0; i < m_nPanes; ++i)
1363+
// Ratio-based division
1364+
for (int i = 0; i < nPanes; ++i)
14051365
{
14061366
rects[i].top = rcParent.top;
14071367
rects[i].bottom = rcParent.bottom;
14081368

1409-
if (i < m_nPanes - 1)
1369+
if (i < nPanes - 1)
14101370
{
1411-
int paneWidth = static_cast<int>(totalWidth * m_splitterRatios[i]);
1371+
int paneWidth = static_cast<int>(totalWidth * ratios[i]);
14121372
rects[i].left = accumulatedWidth;
14131373
rects[i].right = accumulatedWidth + paneWidth;
14141374
accumulatedWidth = rects[i].right + 4; // 4 pixels for splitter
@@ -1423,19 +1383,20 @@ class CWebDiffWindow : public IWebDiffWindow
14231383
}
14241384
else
14251385
{
1426-
// Horizontal split: distribute height based on ratios
1386+
// Horizontal split
14271387
int cy = 0;
14281388
int totalHeight = rcParent.bottom - rcParent.top - cy;
14291389
int accumulatedHeight = 0;
14301390

1431-
for (int i = 0; i < m_nPanes; ++i)
1391+
// Ratio-based division
1392+
for (int i = 0; i < nPanes; ++i)
14321393
{
14331394
rects[i].left = rcParent.left;
14341395
rects[i].right = rcParent.right;
14351396

1436-
if (i < m_nPanes - 1)
1397+
if (i < nPanes - 1)
14371398
{
1438-
int paneHeight = static_cast<int>(totalHeight * m_splitterRatios[i]);
1399+
int paneHeight = static_cast<int>(totalHeight * ratios[i]);
14391400
rects[i].top = accumulatedHeight;
14401401
rects[i].bottom = accumulatedHeight + paneHeight;
14411402
accumulatedHeight = rects[i].bottom + 4; // 4 pixels for splitter
@@ -1449,6 +1410,15 @@ class CWebDiffWindow : public IWebDiffWindow
14491410
}
14501411
}
14511412

1413+
return rects;
1414+
}
1415+
1416+
void ApplySplitterRatios()
1417+
{
1418+
if (!m_hWnd || m_nPanes < 2)
1419+
return;
1420+
1421+
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit, m_splitterRatios);
14521422
for (int i = 0; i < m_nPanes; ++i)
14531423
m_webWindow[i].SetWindowRect(rects[i]);
14541424
}
@@ -1720,4 +1690,5 @@ class CWebDiffWindow : public IWebDiffWindow
17201690
};
17211691
LogCallback m_logCallback;
17221692
double m_splitterRatios[2] = { -1.0, -1.0 };
1693+
double m_requestedSplitterRatios[2] = { -1.0, -1.0 };
17231694
};

0 commit comments

Comments
 (0)