blob: 249fadb4f10f6cc0c46af96b3ccb631b99c015c2 [file] [log] [blame]
[email protected]6bcf6c62012-03-15 13:50:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f7817822009-09-24 05:11:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome_frame/find_dialog.h"
6
[email protected]6bcf6c62012-03-15 13:50:007#include <richedit.h>
[email protected]f7817822009-09-24 05:11:588
[email protected]7e49ad32012-06-14 14:22:079#include "base/guid.h"
[email protected]6bcf6c62012-03-15 13:50:0010#include "base/utf_string_conversions.h"
[email protected]f7817822009-09-24 05:11:5811#include "chrome_frame/chrome_frame_automation.h"
12
13const int kMaxFindChars = 1024;
14
15HHOOK CFFindDialog::msg_hook_ = NULL;
16
17CFFindDialog::CFFindDialog() {}
18
19void CFFindDialog::Init(ChromeFrameAutomationClient* automation_client) {
20 automation_client_ = automation_client;
21}
22
23LRESULT CFFindDialog::OnDestroy(UINT msg, WPARAM wparam, LPARAM lparam,
24 BOOL& handled) {
[email protected]6bcf6c62012-03-15 13:50:0025 // In order to cancel the selection when the Find dialog is dismissed, we
26 // do a fake search for a string that is unlikely to appear on the page.
27 // TODO(robertshield): Change this to plumb through a StopFinding automation
28 // message that triggers a ViewMsg_StopFinding.
[email protected]7e49ad32012-06-14 14:22:0729 std::string guid(base::GenerateGUID());
[email protected]6bcf6c62012-03-15 13:50:0030 automation_client_->FindInPage(ASCIIToWide(guid), FWD, CASE_SENSITIVE, false);
31
[email protected]f7817822009-09-24 05:11:5832 UninstallMessageHook();
33 return 0;
34}
35
36LRESULT CFFindDialog::OnFind(WORD wNotifyCode, WORD wID, HWND hWndCtl,
37 BOOL& bHandled) {
[email protected]6bcf6c62012-03-15 13:50:0038 string16 find_text(kMaxFindChars, L'\0');
39 find_text.resize(GetDlgItemText(IDC_FIND_TEXT, &find_text[0], kMaxFindChars));
40
41 // Repeated searches for the same string should move to the next instance.
42 bool find_next = (find_text == last_find_text_);
43 if (!find_next)
44 last_find_text_ = find_text;
[email protected]f7817822009-09-24 05:11:5845
46 bool match_case = IsDlgButtonChecked(IDC_MATCH_CASE) == BST_CHECKED;
47 bool search_down = IsDlgButtonChecked(IDC_DIRECTION_DOWN) == BST_CHECKED;
48
49 automation_client_->FindInPage(find_text,
50 search_down ? FWD : BACK,
51 match_case ? CASE_SENSITIVE : IGNORE_CASE,
[email protected]6bcf6c62012-03-15 13:50:0052 find_next);
[email protected]f7817822009-09-24 05:11:5853
54 return 0;
55}
56
57LRESULT CFFindDialog::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl,
58 BOOL& bHandled) {
59 DestroyWindow();
60 return 0;
61}
62
63LRESULT CFFindDialog::OnInitDialog(UINT msg, WPARAM wparam, LPARAM lparam,
64 BOOL& handled) {
65 // Init() must be called before Create() or DoModal()!
[email protected]b0febbf2009-11-12 17:49:3566 DCHECK(automation_client_.get());
[email protected]f7817822009-09-24 05:11:5867
68 InstallMessageHook();
69 SendDlgItemMessage(IDC_FIND_TEXT, EM_EXLIMITTEXT, 0, kMaxFindChars);
70 BOOL result = CheckRadioButton(IDC_DIRECTION_DOWN, IDC_DIRECTION_UP,
71 IDC_DIRECTION_DOWN);
72
73 HWND text_field = GetDlgItem(IDC_FIND_TEXT);
74 ::SetFocus(text_field);
75
76 return FALSE; // we set the focus ourselves.
77}
78
79LRESULT CALLBACK CFFindDialog::GetMsgProc(int code, WPARAM wparam,
80 LPARAM lparam) {
81 // Mostly borrowed from http://support.microsoft.com/kb/q187988/
82 // and http://www.codeproject.com/KB/atl/cdialogmessagehook.aspx.
[email protected]b0febbf2009-11-12 17:49:3583 LPMSG msg = reinterpret_cast<LPMSG>(lparam);
84 if (code >= 0 && wparam == PM_REMOVE &&
85 msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST) {
86 HWND hwnd = GetActiveWindow();
87 if (::IsWindow(hwnd) && ::IsDialogMessage(hwnd, msg)) {
88 // The value returned from this hookproc is ignored, and it cannot
89 // be used to tell Windows the message has been handled. To avoid
90 // further processing, convert the message to WM_NULL before
91 // returning.
92 msg->hwnd = NULL;
93 msg->message = WM_NULL;
94 msg->lParam = 0L;
95 msg->wParam = 0;
96 }
97 }
98
99 // Passes the hook information to the next hook procedure in
100 // the current hook chain.
[email protected]f7817822009-09-24 05:11:58101 return ::CallNextHookEx(msg_hook_, code, wparam, lparam);
102}
103
104bool CFFindDialog::InstallMessageHook() {
105 // Make sure we only call this once.
106 DCHECK(msg_hook_ == NULL);
[email protected]b0febbf2009-11-12 17:49:35107 msg_hook_ = ::SetWindowsHookEx(WH_GETMESSAGE, &CFFindDialog::GetMsgProc,
108 _AtlBaseModule.m_hInst, GetCurrentThreadId());
[email protected]f7817822009-09-24 05:11:58109 DCHECK(msg_hook_ != NULL);
110 return msg_hook_ != NULL;
111}
112
113bool CFFindDialog::UninstallMessageHook() {
114 DCHECK(msg_hook_ != NULL);
115 BOOL result = ::UnhookWindowsHookEx(msg_hook_);
116 DCHECK(result);
117 msg_hook_ = NULL;
118
119 return result != FALSE;
120}