OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/find_bar/find_bar_controller.h" | 5 #include "chrome/browser/ui/find_bar/find_bar_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/utf_string_conversions.h" |
10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "chrome/browser/renderer_host/render_view_host.h" |
11 #include "chrome/browser/tab_contents/navigation_entry.h" | 13 #include "chrome/browser/tab_contents/navigation_entry.h" |
12 #include "chrome/browser/ui/find_bar/find_bar.h" | 14 #include "chrome/browser/ui/find_bar/find_bar.h" |
13 #include "chrome/browser/ui/find_bar/find_bar_state.h" | 15 #include "chrome/browser/ui/find_bar/find_bar_state.h" |
14 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
15 #include "chrome/common/notification_details.h" | 17 #include "chrome/common/notification_details.h" |
16 #include "chrome/common/notification_source.h" | 18 #include "chrome/common/notification_source.h" |
17 #include "gfx/rect.h" | 19 #include "gfx/rect.h" |
18 | 20 |
19 // The minimum space between the FindInPage window and the search result. | 21 // The minimum space between the FindInPage window and the search result. |
20 static const int kMinFindWndDistanceFromSelection = 5; | 22 static const int kMinFindWndDistanceFromSelection = 5; |
21 | 23 |
22 FindBarController::FindBarController(FindBar* find_bar) | 24 FindBarController::FindBarController(FindBar* find_bar) |
23 : find_bar_(find_bar), | 25 : find_bar_(find_bar), |
24 tab_contents_(NULL), | 26 tab_contents_(NULL), |
25 last_reported_matchcount_(0) { | 27 last_reported_matchcount_(0) { |
26 } | 28 } |
27 | 29 |
28 FindBarController::~FindBarController() { | 30 FindBarController::~FindBarController() { |
29 DCHECK(!tab_contents_); | 31 DCHECK(!tab_contents_); |
30 } | 32 } |
31 | 33 |
32 void FindBarController::Show() { | 34 void FindBarController::Show() { |
33 // Only show the animation if we're not already showing a find bar for the | 35 // Only show the animation if we're not already showing a find bar for the |
34 // selected TabContents. | 36 // selected TabContents. |
35 if (!tab_contents_->find_ui_active()) { | 37 if (!tab_contents_->find_ui_active()) { |
36 MaybeSetPrepopulateText(); | 38 MaybeSetPrepopulateText(); |
37 | 39 |
38 tab_contents_->set_find_ui_active(true); | 40 tab_contents_->set_find_ui_active(true); |
39 find_bar_->Show(true); | 41 find_bar_->Show(true); |
| 42 } else { |
| 43 MaybeOverrideText(); |
40 } | 44 } |
41 find_bar_->SetFocusAndSelection(); | 45 find_bar_->SetFocusAndSelection(); |
42 } | 46 } |
43 | 47 |
44 void FindBarController::EndFindSession(SelectionAction action) { | 48 void FindBarController::EndFindSession(SelectionAction action) { |
45 find_bar_->Hide(true); | 49 find_bar_->Hide(true); |
46 | 50 |
47 // |tab_contents_| can be NULL for a number of reasons, for example when the | 51 // |tab_contents_| can be NULL for a number of reasons, for example when the |
48 // tab is closing. We must guard against that case. See issue 8030. | 52 // tab is closing. We must guard against that case. See issue 8030. |
49 if (tab_contents_) { | 53 if (tab_contents_) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 !find_result.final_update()) | 201 !find_result.final_update()) |
198 return; // Don't let interim result override match count. | 202 return; // Don't let interim result override match count. |
199 last_reported_matchcount_ = find_result.number_of_matches(); | 203 last_reported_matchcount_ = find_result.number_of_matches(); |
200 } | 204 } |
201 | 205 |
202 find_bar_->UpdateUIForFindResult(find_result, tab_contents_->find_text()); | 206 find_bar_->UpdateUIForFindResult(find_result, tab_contents_->find_text()); |
203 } | 207 } |
204 | 208 |
205 void FindBarController::MaybeSetPrepopulateText() { | 209 void FindBarController::MaybeSetPrepopulateText() { |
206 #if !defined(OS_MACOSX) | 210 #if !defined(OS_MACOSX) |
207 // Find out what we should show in the find text box. Usually, this will be | 211 // Find out what we should show in the find text box. If this tab contains any |
208 // the last search in this tab, but if no search has been issued in this tab | 212 // selected text, we use that text. Otherwise, we use the last search in this |
209 // we use the last search string (from any tab). | 213 // tab. If no search has been issued in this tab we use the last search string |
210 string16 find_string = tab_contents_->find_text(); | 214 // (from any tab). |
| 215 string16 find_string = GetSelectedText(); |
| 216 if (find_string.empty()) |
| 217 find_string = tab_contents_->find_text(); |
211 if (find_string.empty()) | 218 if (find_string.empty()) |
212 find_string = tab_contents_->previous_find_text(); | 219 find_string = tab_contents_->previous_find_text(); |
213 if (find_string.empty()) { | 220 if (find_string.empty()) { |
214 find_string = | 221 find_string = |
215 FindBarState::GetLastPrepopulateText(tab_contents_->profile()); | 222 FindBarState::GetLastPrepopulateText(tab_contents_->profile()); |
216 } | 223 } |
217 | 224 |
218 // Update the find bar with existing results and search text, regardless of | 225 // Update the find bar with existing results and search text, regardless of |
219 // whether or not the find bar is visible, so that if it's subsequently | 226 // whether or not the find bar is visible, so that if it's subsequently |
220 // shown it is showing the right state for this tab. We update the find text | 227 // shown it is showing the right state for this tab. We update the find text |
221 // _first_ since the FindBarView checks its emptiness to see if it should | 228 // _first_ since the FindBarView checks its emptiness to see if it should |
222 // clear the result count display when there's nothing in the box. | 229 // clear the result count display when there's nothing in the box. |
223 find_bar_->SetFindText(find_string); | 230 find_bar_->SetFindText(find_string); |
224 #else | 231 #else |
225 // Having a per-tab find_string is not compatible with OS X's find pasteboard, | 232 // Having a per-tab find_string is not compatible with OS X's find pasteboard, |
226 // so we always have the same find text in all find bars. This is done through | 233 // so we always have the same find text in all find bars. This is done through |
227 // the find pasteboard mechanism, so don't set the text here. | 234 // the find pasteboard mechanism, so don't set the text here. |
228 #endif | 235 #endif |
229 } | 236 } |
| 237 |
| 238 void FindBarController::MaybeOverrideText() { |
| 239 #if !defined(OS_MACOSX) |
| 240 string16 selected_text = GetSelectedText(); |
| 241 if (!selected_text.empty()) |
| 242 find_bar_->SetFindText(selected_text); |
| 243 #else |
| 244 // Having a per-tab find_string is not compatible with OS X's find pasteboard, |
| 245 // so we always have the same find text in all find bars. This is done through |
| 246 // the find pasteboard mechanism, so don't set the text here. |
| 247 #endif |
| 248 } |
| 249 |
| 250 string16 FindBarController::GetSelectedText() { |
| 251 return UTF8ToUTF16(tab_contents_->render_view_host()->selected_text()); |
| 252 } |
OLD | NEW |