Make more use of unique_ptr
[TortoiseGit.git] / src / TortoiseUDiff / FindBar.cpp
blobf134eee1c31f74c95a73fb03d81ad4f224335085
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012 - TortoiseGit
4 // Copyright (C) 2003-2007, 2012 - 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>
27 using namespace std;
29 CFindBar::CFindBar()
30 : m_hParent(NULL)
31 , m_hIcon(NULL)
35 CFindBar::~CFindBar(void)
37 DestroyIcon(m_hIcon);
40 LRESULT CFindBar::DlgFunc(HWND /*hwndDlg*/, UINT uMsg, WPARAM wParam, LPARAM lParam)
42 UNREFERENCED_PARAMETER(lParam);
43 switch (uMsg)
45 case WM_INITDIALOG:
47 m_hIcon = (HICON)::LoadImage(hResource, MAKEINTRESOURCE(IDI_CANCELNORMAL), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
48 SendMessage(GetDlgItem(*this, IDC_FINDEXIT), BM_SETIMAGE, IMAGE_ICON, (LPARAM)m_hIcon);
50 return TRUE;
51 case WM_COMMAND:
52 return DoCommand(LOWORD(wParam), HIWORD(wParam));
53 default:
54 return FALSE;
58 LRESULT CFindBar::DoCommand(int id, int msg)
60 bool bFindPrev = false;
61 switch (id)
63 case IDC_FINDPREV:
64 bFindPrev = true;
65 // fallthrough
66 case IDC_FINDNEXT:
68 DoFind(bFindPrev);
70 break;
71 case IDC_FINDEXIT:
73 ::SendMessage(m_hParent, COMMITMONITOR_FINDEXIT, 0, 0);
75 break;
76 case IDC_FINDTEXT:
78 if (msg == EN_CHANGE)
80 SendMessage(m_hParent, COMMITMONITOR_FINDRESET, 0, 0);
81 DoFind(false);
84 break;
86 return 1;
89 void CFindBar::DoFind(bool bFindPrev)
91 int len = ::GetWindowTextLength(GetDlgItem(*this, IDC_FINDTEXT));
92 std::unique_ptr<TCHAR[]> findtext(new TCHAR[len + 1]);
93 ::GetWindowText(GetDlgItem(*this, IDC_FINDTEXT), findtext.get(), len + 1);
94 wstring ft = wstring(findtext.get());
95 const bool bCaseSensitive = !!SendMessage(GetDlgItem(*this, IDC_MATCHCASECHECK), BM_GETCHECK, 0, NULL);
96 const UINT message = bFindPrev ? COMMITMONITOR_FINDMSGPREV : COMMITMONITOR_FINDMSGNEXT;
97 ::SendMessage(m_hParent, message, (WPARAM)bCaseSensitive, (LPARAM)ft.c_str());