Upgrade libgit2
[TortoiseGit.git] / src / TortoiseUDiff / FindBar.cpp
blobc022c92e1cc244b44007461e1cac8a0efb9a2af6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012-2013, 2015-2016, 2019-2020, 2023 - TortoiseGit
4 // Copyright (C) 2003-2007, 2012-2013, 2018 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "resource.h"
22 #include "FindBar.h"
23 #include "registry.h"
24 #include <string>
25 #include <Commdlg.h>
26 #include "LoadIconEx.h"
27 #include "Theme.h"
29 CFindBar::CFindBar()
33 CFindBar::~CFindBar()
35 CTheme::Instance().RemoveRegisteredCallback(m_themeCallbackId);
38 LRESULT CFindBar::DlgFunc(HWND /*hwndDlg*/, UINT uMsg, WPARAM wParam, LPARAM /*lParam*/)
40 switch (uMsg)
42 case WM_INITDIALOG:
44 m_hIcon = LoadIconEx(hResource, MAKEINTRESOURCE(IDI_CANCELNORMAL));
45 SendMessage(GetDlgItem(*this, IDC_FINDEXIT), BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(static_cast<HICON>(m_hIcon)));
46 m_themeCallbackId = CTheme::Instance().RegisterThemeChangeCallback([this]() { SetTheme(CTheme::Instance().IsDarkTheme()); });
47 SetTheme(CTheme::Instance().IsDarkTheme());
49 return TRUE;
50 case WM_COMMAND:
51 return DoCommand(LOWORD(wParam), HIWORD(wParam));
52 default:
53 return FALSE;
57 LRESULT CFindBar::DoCommand(int id, int msg)
59 bool bFindPrev = false;
60 switch (id)
62 case IDC_FINDPREV:
63 bFindPrev = true;
64 [[fallthrough]];
65 case IDC_FINDNEXT:
67 DoFind(bFindPrev);
69 break;
70 case IDC_FINDEXIT:
72 ::SendMessage(m_hParent, COMMITMONITOR_FINDEXIT, 0, 0);
74 break;
75 case IDC_FINDTEXT:
77 if (msg == EN_CHANGE)
79 SendMessage(m_hParent, COMMITMONITOR_FINDRESET, 0, 0);
80 DoFind(false);
83 break;
85 return 1;
88 void CFindBar::DoFind(bool bFindPrev)
90 int len = ::GetWindowTextLength(GetDlgItem(*this, IDC_FINDTEXT));
91 auto findtext = std::make_unique<wchar_t[]>(len + 1);
92 if (!::GetWindowText(GetDlgItem(*this, IDC_FINDTEXT), findtext.get(), len + 1))
93 return;
94 std::wstring ft = std::wstring(findtext.get());
95 const bool bCaseSensitive = !!SendMessage(GetDlgItem(*this, IDC_MATCHCASECHECK), BM_GETCHECK, 0, 0);
96 const UINT message = bFindPrev ? COMMITMONITOR_FINDMSGPREV : COMMITMONITOR_FINDMSGNEXT;
97 ::SendMessage(m_hParent, message, bCaseSensitive, reinterpret_cast<LPARAM>(ft.c_str()));
100 void CFindBar::SelectSearchString()
102 SendMessage(GetDlgItem(*this, IDC_FINDTEXT), EM_SETSEL, 0, -1);
105 void CFindBar::SetSearchString(LPCWSTR findStr)
107 ::SetWindowText(GetDlgItem(*this, IDC_FINDTEXT), findStr);
110 void CFindBar::SetTheme(bool bDark)
112 CTheme::Instance().SetThemeForDialog(*this, bDark);