Skip to content

Commit

Permalink
Add a better implementation of bordered text box.
Browse files Browse the repository at this point in the history
  • Loading branch information
TautvydasZilys committed May 22, 2022
1 parent f17f03a commit eea7249
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 101 deletions.
2 changes: 2 additions & 0 deletions FileSystemSearch_Win32/FileSystemSearch_Win32.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@
<ClInclude Include="..\Common\Version.rc" />
<ClCompile Include="Source\AppWindows\SearchResultWindow.cpp" />
<ClCompile Include="Source\AppWindows\SearchWindow.cpp" />
<ClCompile Include="Source\Controls\BorderedTextBox.cpp" />
<ClCompile Include="Source\Main.cpp" />
<ClCompile Include="Source\Utilities\NumberFormatter.cpp" />
<ClCompile Include="Source\Utilities\FontCache.cpp" />
<ClCompile Include="$(SolutionDir)Common\PrecompiledHeader.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClInclude Include="Source\Controls\BorderedTextBox.h" />
<ClInclude Include="Source\Utilities\DCHolder.h" />
<ClInclude Include="Source\Utilities\NumberFormatter.h" />
<ClInclude Include="Source\Utilities\WindowPosition.h" />
Expand Down
9 changes: 9 additions & 0 deletions FileSystemSearch_Win32/FileSystemSearch_Win32.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<ClCompile Include="Source\Utilities\NumberFormatter.cpp">
<Filter>Utilities</Filter>
</ClCompile>
<ClCompile Include="Source\Controls\BorderedTextBox.cpp">
<Filter>Controls</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Source\Resources\resource.h">
Expand Down Expand Up @@ -69,6 +72,9 @@
<ClInclude Include="Source\Utilities\NumberFormatter.h">
<Filter>Utilities</Filter>
</ClInclude>
<ClInclude Include="Source\Controls\BorderedTextBox.h">
<Filter>Controls</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Resources">
Expand All @@ -83,6 +89,9 @@
<Filter Include="Utilities">
<UniqueIdentifier>{f3601572-69f9-46b4-a86f-db2c7776e8e1}</UniqueIdentifier>
</Filter>
<Filter Include="Controls">
<UniqueIdentifier>{5893e6a0-6f56-441d-8afa-e2580c442779}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Manifest Include="Source\Resources\FileSystemSearch.exe.manifest">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SearchResultWindow : NonCopyable

void CleanupSearchOperationIfNeeded();

static LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

private:
const FontCache& m_FontCache;
Expand Down
98 changes: 0 additions & 98 deletions FileSystemSearch_Win32/Source/AppWindows/SearchWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "SearchEngine.h"
#include "SearchResultWindow.h"
#include "SearchWindow.h"
#include "Utilities/DCHolder.h"
#include "Utilities/FontCache.h"
#include "Utilities/WindowUtilities.h"

Expand Down Expand Up @@ -142,109 +141,12 @@ LRESULT CALLBACK SearchWindow::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARA
return DefWindowProcW(hWnd, msg, wParam, lParam);
}

void DrawBorder(HWND hWnd, bool hasFocus)
{
RECT windowRect;
GetWindowRect(hWnd, &windowRect);

DCHolder dc(hWnd, nullptr, DCX_WINDOW | DCX_CACHE);
const auto brush = hasFocus ? reinterpret_cast<HBRUSH>(COLOR_HIGHLIGHT + 1) : reinterpret_cast<HBRUSH>(COLOR_WINDOWFRAME + 1);
const int width = windowRect.right - windowRect.left;
const int height = windowRect.bottom - windowRect.top;

RECT leftLine = { 0, 0, 1, height };
auto result = FillRect(dc, &leftLine, brush);
Assert(result != 0);

RECT topLine = { 0, 0, width, 1 };
result = FillRect(dc, &topLine, brush);
Assert(result != 0);

RECT rightLine = { width - 1, 0, width, height };
result = FillRect(dc, &rightLine, brush);
Assert(result != 0);

RECT bottomLine = { 0, height - 1, width, height };
result = FillRect(dc, &bottomLine, brush);
Assert(result != 0);

RECT clientArea = { 1, 1, width - 1, 2 };
result = FillRect(dc, &clientArea, reinterpret_cast<HBRUSH>(kBackgroundColor));
Assert(result != 0);
}

LRESULT CALLBACK SearchWindow::TextBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR /*uIdSubclass*/, DWORD_PTR /*dwRefData*/)
{
switch (msg)
{
// Make top border area 2 pixels high to force text 1 px lower
case WM_NCCALCSIZE:
if (wParam)
{
auto sizeParams = reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);

if (sizeParams->rgrc[0].right - sizeParams->rgrc[0].left >= 3)
{
sizeParams->rgrc[0].left++;
sizeParams->rgrc[0].right--;
}

if (sizeParams->rgrc[0].bottom - sizeParams->rgrc[0].top >= 3)
{
sizeParams->rgrc[0].top += 2;
sizeParams->rgrc[0].bottom--;
}

return WVR_REDRAW;
}
else
{
auto rect = reinterpret_cast<RECT*>(lParam);
rect->top += 2;
rect->left++;
rect->right--;
rect->bottom--;
return 0;
}

case WM_MOUSEMOVE:
break;

case WM_MOUSELEAVE:
DrawBorder(hWnd, false);
break;

case WM_NCPAINT:
{
POINT p;
bool hasFocus = GetFocus() == hWnd || (GetCursorPos(&p) && WindowFromPoint(p) == hWnd);
DrawBorder(hWnd, hasFocus);
return 0;
}
}

return DefSubclassProc(hWnd, msg, wParam, lParam);
}

void SearchWindow::OnCreate(HWND hWnd)
{
m_Hwnd = hWnd;

for (size_t i = 0; i < m_ControlCount; i++)
m_Controls[i] = kControls[i].Create(hWnd, i);

// Subclass all textboxes
auto result = SetWindowSubclass(m_Controls[m_SearchPathTextBox], &SearchWindow::TextBoxWndProc, m_SearchPathTextBox, reinterpret_cast<DWORD_PTR>(this));
Assert(result != FALSE);

result = SetWindowSubclass(m_Controls[m_SearchPatternTextBox], &SearchWindow::TextBoxWndProc, m_SearchPatternTextBox, reinterpret_cast<DWORD_PTR>(this));
Assert(result != FALSE);

result = SetWindowSubclass(m_Controls[m_SearchStringTextBox], &SearchWindow::TextBoxWndProc, m_SearchStringTextBox, reinterpret_cast<DWORD_PTR>(this));
Assert(result != FALSE);

result = SetWindowSubclass(m_Controls[m_IgnoreFilesLargerThanTextBox], &SearchWindow::TextBoxWndProc, m_IgnoreFilesLargerThanTextBox, reinterpret_cast<DWORD_PTR>(this));
Assert(result != FALSE);

// Restrict to numbers only
HWND ignoreFilesLargerThanTextBox = m_Controls[m_IgnoreFilesLargerThanTextBox];
Expand Down
1 change: 0 additions & 1 deletion FileSystemSearch_Win32/Source/AppWindows/SearchWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class SearchWindow : NonCopyable

private:
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK TextBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);

void AdjustSearchWindowPlacement(POINT position, SIZE size, uint32_t dpi);
void OnCreate(HWND hWnd);
Expand Down
Loading

0 comments on commit eea7249

Please sign in to comment.