| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/views/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/property_bag.h" | 8 #include "base/property_bag.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 218 |
| 219 // static | 219 // static |
| 220 const char OmniboxViewViews::kViewClassName[] = "BrowserOmniboxViewViews"; | 220 const char OmniboxViewViews::kViewClassName[] = "BrowserOmniboxViewViews"; |
| 221 | 221 |
| 222 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, | 222 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, |
| 223 ToolbarModel* toolbar_model, | 223 ToolbarModel* toolbar_model, |
| 224 Profile* profile, | 224 Profile* profile, |
| 225 CommandUpdater* command_updater, | 225 CommandUpdater* command_updater, |
| 226 bool popup_window_mode, | 226 bool popup_window_mode, |
| 227 LocationBarView* location_bar) | 227 LocationBarView* location_bar) |
| 228 : textfield_(NULL), | 228 : OmniboxView(profile, controller, toolbar_model, command_updater), |
| 229 textfield_(NULL), |
| 229 popup_window_mode_(popup_window_mode), | 230 popup_window_mode_(popup_window_mode), |
| 230 model_(new OmniboxEditModel(this, controller, profile)), | |
| 231 controller_(controller), | |
| 232 toolbar_model_(toolbar_model), | |
| 233 command_updater_(command_updater), | |
| 234 security_level_(ToolbarModel::NONE), | 231 security_level_(ToolbarModel::NONE), |
| 235 ime_composing_before_change_(false), | 232 ime_composing_before_change_(false), |
| 236 delete_at_end_pressed_(false), | 233 delete_at_end_pressed_(false), |
| 237 location_bar_view_(location_bar), | 234 location_bar_view_(location_bar), |
| 238 ime_candidate_window_open_(false), | 235 ime_candidate_window_open_(false), |
| 239 select_all_on_mouse_release_(false) { | 236 select_all_on_mouse_release_(false) { |
| 240 if (chrome::search::IsInstantExtendedAPIEnabled( | 237 if (chrome::search::IsInstantExtendedAPIEnabled( |
| 241 location_bar_view_->profile())) { | 238 location_bar_view_->profile())) { |
| 242 set_background(views::Background::CreateSolidBackground( | 239 set_background(views::Background::CreateSolidBackground( |
| 243 chrome::search::kOmniboxBackgroundColor)); | 240 chrome::search::kOmniboxBackgroundColor)); |
| 244 } | 241 } |
| 245 } | 242 } |
| 246 | 243 |
| 247 OmniboxViewViews::~OmniboxViewViews() { | 244 OmniboxViewViews::~OmniboxViewViews() { |
| 248 #if defined(OS_CHROMEOS) | 245 #if defined(OS_CHROMEOS) |
| 249 chromeos::input_method::InputMethodManager::GetInstance()-> | 246 chromeos::input_method::InputMethodManager::GetInstance()-> |
| 250 RemoveCandidateWindowObserver(this); | 247 RemoveCandidateWindowObserver(this); |
| 251 #endif | 248 #endif |
| 252 | 249 |
| 253 // Explicitly teardown members which have a reference to us. Just to be safe | 250 // Explicitly teardown members which have a reference to us. Just to be safe |
| 254 // we want them to be destroyed before destroying any other internal state. | 251 // we want them to be destroyed before destroying any other internal state. |
| 255 popup_view_.reset(); | 252 popup_view_.reset(); |
| 256 model_.reset(); | |
| 257 } | 253 } |
| 258 | 254 |
| 259 //////////////////////////////////////////////////////////////////////////////// | 255 //////////////////////////////////////////////////////////////////////////////// |
| 260 // OmniboxViewViews public: | 256 // OmniboxViewViews public: |
| 261 | 257 |
| 262 void OmniboxViewViews::Init(views::View* popup_parent_view) { | 258 void OmniboxViewViews::Init(views::View* popup_parent_view) { |
| 263 // The height of the text view is going to change based on the font used. We | 259 // The height of the text view is going to change based on the font used. We |
| 264 // don't want to stretch the height, and we want it vertically centered. | 260 // don't want to stretch the height, and we want it vertically centered. |
| 265 // TODO(oshima): make sure the above happens with views. | 261 // TODO(oshima): make sure the above happens with views. |
| 266 textfield_ = new AutocompleteTextfield(this, location_bar_view_); | 262 textfield_ = new AutocompleteTextfield(this, location_bar_view_); |
| 267 textfield_->SetController(this); | 263 textfield_->SetController(this); |
| 268 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_URL); | 264 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_URL); |
| 269 | 265 |
| 270 if (popup_window_mode_) | 266 if (popup_window_mode_) |
| 271 textfield_->SetReadOnly(true); | 267 textfield_->SetReadOnly(true); |
| 272 | 268 |
| 273 const int font_size = | 269 const int font_size = |
| 274 !popup_window_mode_ && chrome::search::IsInstantExtendedAPIEnabled( | 270 !popup_window_mode_ && chrome::search::IsInstantExtendedAPIEnabled( |
| 275 location_bar_view_->profile()) ? | 271 location_bar_view_->profile()) ? |
| 276 chrome::search::kOmniboxFontSize : | 272 chrome::search::kOmniboxFontSize : |
| 277 GetEditFontPixelSize(popup_window_mode_); | 273 GetEditFontPixelSize(popup_window_mode_); |
| 278 const int old_size = textfield_->font().GetFontSize(); | 274 const int old_size = textfield_->font().GetFontSize(); |
| 279 if (font_size != old_size) | 275 if (font_size != old_size) |
| 280 textfield_->SetFont(textfield_->font().DeriveFont(font_size - old_size)); | 276 textfield_->SetFont(textfield_->font().DeriveFont(font_size - old_size)); |
| 281 | 277 |
| 282 // Create popup view using the same font as |textfield_|'s. | 278 // Create popup view using the same font as |textfield_|'s. |
| 283 popup_view_.reset( | 279 popup_view_.reset( |
| 284 OmniboxPopupContentsView::Create( | 280 OmniboxPopupContentsView::Create( |
| 285 textfield_->font(), this, model_.get(), location_bar_view_, | 281 textfield_->font(), this, model(), location_bar_view_, |
| 286 popup_parent_view)); | 282 popup_parent_view)); |
| 287 | 283 |
| 288 // A null-border to zero out the focused border on textfield. | 284 // A null-border to zero out the focused border on textfield. |
| 289 const int vertical_margin = !popup_window_mode_ ? | 285 const int vertical_margin = !popup_window_mode_ ? |
| 290 kAutocompleteVerticalMargin : kAutocompleteVerticalMarginInPopup; | 286 kAutocompleteVerticalMargin : kAutocompleteVerticalMarginInPopup; |
| 291 set_border(views::Border::CreateEmptyBorder(vertical_margin, 0, | 287 set_border(views::Border::CreateEmptyBorder(vertical_margin, 0, |
| 292 vertical_margin, 0)); | 288 vertical_margin, 0)); |
| 293 #if defined(OS_CHROMEOS) | 289 #if defined(OS_CHROMEOS) |
| 294 chromeos::input_method::InputMethodManager::GetInstance()-> | 290 chromeos::input_method::InputMethodManager::GetInstance()-> |
| 295 AddCandidateWindowObserver(this); | 291 AddCandidateWindowObserver(this); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 306 // See comments in LocationBarView::Layout as to why this uses -1. | 302 // See comments in LocationBarView::Layout as to why this uses -1. |
| 307 const int start = std::max(0, static_cast<int>(sel.end()) - 1); | 303 const int start = std::max(0, static_cast<int>(sel.end()) - 1); |
| 308 // TODO: add horizontal margin. | 304 // TODO: add horizontal margin. |
| 309 return textfield_->font().GetStringWidth(GetText().substr(start)); | 305 return textfield_->font().GetStringWidth(GetText().substr(start)); |
| 310 } | 306 } |
| 311 | 307 |
| 312 bool OmniboxViewViews::HandleAfterKeyEvent(const views::KeyEvent& event, | 308 bool OmniboxViewViews::HandleAfterKeyEvent(const views::KeyEvent& event, |
| 313 bool handled) { | 309 bool handled) { |
| 314 if (event.key_code() == ui::VKEY_RETURN) { | 310 if (event.key_code() == ui::VKEY_RETURN) { |
| 315 bool alt_held = event.IsAltDown(); | 311 bool alt_held = event.IsAltDown(); |
| 316 model_->AcceptInput(alt_held ? NEW_FOREGROUND_TAB : CURRENT_TAB, false); | 312 model()->AcceptInput(alt_held ? NEW_FOREGROUND_TAB : CURRENT_TAB, false); |
| 317 handled = true; | 313 handled = true; |
| 318 } else if (!handled && event.key_code() == ui::VKEY_ESCAPE) { | 314 } else if (!handled && event.key_code() == ui::VKEY_ESCAPE) { |
| 319 // We can handle the Escape key if textfield did not handle it. | 315 // We can handle the Escape key if textfield did not handle it. |
| 320 // If it's not handled by us, then we need to propagate it up to the parent | 316 // If it's not handled by us, then we need to propagate it up to the parent |
| 321 // widgets, so that Escape accelerator can still work. | 317 // widgets, so that Escape accelerator can still work. |
| 322 handled = model_->OnEscapeKeyPressed(); | 318 handled = model()->OnEscapeKeyPressed(); |
| 323 } else if (event.key_code() == ui::VKEY_CONTROL) { | 319 } else if (event.key_code() == ui::VKEY_CONTROL) { |
| 324 // Omnibox2 can switch its contents while pressing a control key. To switch | 320 // Omnibox2 can switch its contents while pressing a control key. To switch |
| 325 // the contents of omnibox2, we notify the OmniboxEditModel class when the | 321 // the contents of omnibox2, we notify the OmniboxEditModel class when the |
| 326 // control-key state is changed. | 322 // control-key state is changed. |
| 327 model_->OnControlKeyChanged(true); | 323 model()->OnControlKeyChanged(true); |
| 328 } else if (!handled && event.key_code() == ui::VKEY_DELETE && | 324 } else if (!handled && event.key_code() == ui::VKEY_DELETE && |
| 329 event.IsShiftDown()) { | 325 event.IsShiftDown()) { |
| 330 // If shift+del didn't change the text, we let this delete an entry from | 326 // If shift+del didn't change the text, we let this delete an entry from |
| 331 // the popup. We can't check to see if the IME handled it because even if | 327 // the popup. We can't check to see if the IME handled it because even if |
| 332 // nothing is selected, the IME or the TextView still report handling it. | 328 // nothing is selected, the IME or the TextView still report handling it. |
| 333 if (model_->popup_model()->IsOpen()) | 329 if (model()->popup_model()->IsOpen()) |
| 334 model_->popup_model()->TryDeletingCurrentItem(); | 330 model()->popup_model()->TryDeletingCurrentItem(); |
| 335 } else if (!handled && event.key_code() == ui::VKEY_UP) { | 331 } else if (!handled && event.key_code() == ui::VKEY_UP) { |
| 336 model_->OnUpOrDownKeyPressed(-1); | 332 model()->OnUpOrDownKeyPressed(-1); |
| 337 handled = true; | 333 handled = true; |
| 338 } else if (!handled && event.key_code() == ui::VKEY_DOWN) { | 334 } else if (!handled && event.key_code() == ui::VKEY_DOWN) { |
| 339 model_->OnUpOrDownKeyPressed(1); | 335 model()->OnUpOrDownKeyPressed(1); |
| 340 handled = true; | 336 handled = true; |
| 341 } else if (!handled && | 337 } else if (!handled && |
| 342 event.key_code() == ui::VKEY_TAB && | 338 event.key_code() == ui::VKEY_TAB && |
| 343 !event.IsControlDown()) { | 339 !event.IsControlDown()) { |
| 344 if (model_->is_keyword_hint() && !event.IsShiftDown()) { | 340 if (model()->is_keyword_hint() && !event.IsShiftDown()) { |
| 345 handled = model_->AcceptKeyword(); | 341 handled = model()->AcceptKeyword(); |
| 346 } else if (model_->popup_model()->IsOpen()) { | 342 } else if (model()->popup_model()->IsOpen()) { |
| 347 if (event.IsShiftDown() && | 343 if (event.IsShiftDown() && |
| 348 model_->popup_model()->selected_line_state() == | 344 model()->popup_model()->selected_line_state() == |
| 349 OmniboxPopupModel::KEYWORD) { | 345 OmniboxPopupModel::KEYWORD) { |
| 350 model_->ClearKeyword(GetText()); | 346 model()->ClearKeyword(GetText()); |
| 351 } else { | 347 } else { |
| 352 model_->OnUpOrDownKeyPressed(event.IsShiftDown() ? -1 : 1); | 348 model()->OnUpOrDownKeyPressed(event.IsShiftDown() ? -1 : 1); |
| 353 } | 349 } |
| 354 handled = true; | 350 handled = true; |
| 355 } else { | 351 } else { |
| 356 string16::size_type start = 0; | 352 string16::size_type start = 0; |
| 357 string16::size_type end = 0; | 353 string16::size_type end = 0; |
| 358 size_t length = GetTextLength(); | 354 size_t length = GetTextLength(); |
| 359 GetSelectionBounds(&start, &end); | 355 GetSelectionBounds(&start, &end); |
| 360 if (start != end || start < length) { | 356 if (start != end || start < length) { |
| 361 OnBeforePossibleChange(); | 357 OnBeforePossibleChange(); |
| 362 textfield_->SelectRange(ui::Range(length, length)); | 358 textfield_->SelectRange(ui::Range(length, length)); |
| 363 OnAfterPossibleChange(); | 359 OnAfterPossibleChange(); |
| 364 handled = true; | 360 handled = true; |
| 365 } | 361 } |
| 366 | 362 |
| 367 // TODO(Oshima): handle instant | 363 // TODO(Oshima): handle instant |
| 368 } | 364 } |
| 369 } | 365 } |
| 370 // TODO(oshima): page up & down | 366 // TODO(oshima): page up & down |
| 371 | 367 |
| 372 return handled; | 368 return handled; |
| 373 } | 369 } |
| 374 | 370 |
| 375 bool OmniboxViewViews::HandleKeyReleaseEvent(const views::KeyEvent& event) { | 371 bool OmniboxViewViews::HandleKeyReleaseEvent(const views::KeyEvent& event) { |
| 376 // Omnibox2 can switch its contents while pressing a control key. To switch | 372 // Omnibox2 can switch its contents while pressing a control key. To switch |
| 377 // the contents of omnibox2, we notify the OmniboxEditModel class when the | 373 // the contents of omnibox2, we notify the OmniboxEditModel class when the |
| 378 // control-key state is changed. | 374 // control-key state is changed. |
| 379 if (event.key_code() == ui::VKEY_CONTROL) { | 375 if (event.key_code() == ui::VKEY_CONTROL) { |
| 380 // TODO(oshima): investigate if we need to support keyboard with two | 376 // TODO(oshima): investigate if we need to support keyboard with two |
| 381 // controls. | 377 // controls. |
| 382 model_->OnControlKeyChanged(false); | 378 model()->OnControlKeyChanged(false); |
| 383 return true; | 379 return true; |
| 384 } | 380 } |
| 385 return false; | 381 return false; |
| 386 } | 382 } |
| 387 | 383 |
| 388 void OmniboxViewViews::HandleMousePressEvent(const views::MouseEvent& event) { | 384 void OmniboxViewViews::HandleMousePressEvent(const views::MouseEvent& event) { |
| 389 select_all_on_mouse_release_ = | 385 select_all_on_mouse_release_ = |
| 390 (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && | 386 (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && |
| 391 !textfield_->HasFocus(); | 387 !textfield_->HasFocus(); |
| 392 } | 388 } |
| 393 | 389 |
| 394 void OmniboxViewViews::HandleMouseDragEvent(const views::MouseEvent& event) { | 390 void OmniboxViewViews::HandleMouseDragEvent(const views::MouseEvent& event) { |
| 395 select_all_on_mouse_release_ = false; | 391 select_all_on_mouse_release_ = false; |
| 396 } | 392 } |
| 397 | 393 |
| 398 void OmniboxViewViews::HandleMouseReleaseEvent(const views::MouseEvent& event) { | 394 void OmniboxViewViews::HandleMouseReleaseEvent(const views::MouseEvent& event) { |
| 399 if ((event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && | 395 if ((event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && |
| 400 select_all_on_mouse_release_) { | 396 select_all_on_mouse_release_) { |
| 401 // Select all in the reverse direction so as not to scroll the caret | 397 // Select all in the reverse direction so as not to scroll the caret |
| 402 // into view and shift the contents jarringly. | 398 // into view and shift the contents jarringly. |
| 403 SelectAll(true); | 399 SelectAll(true); |
| 404 } | 400 } |
| 405 select_all_on_mouse_release_ = false; | 401 select_all_on_mouse_release_ = false; |
| 406 } | 402 } |
| 407 | 403 |
| 408 void OmniboxViewViews::HandleFocusIn() { | 404 void OmniboxViewViews::HandleFocusIn() { |
| 409 // TODO(oshima): Get control key state. | 405 // TODO(oshima): Get control key state. |
| 410 model_->OnSetFocus(false); | 406 model()->OnSetFocus(false); |
| 411 // Don't call controller_->OnSetFocus as this view has already | 407 // Don't call controller()->OnSetFocus as this view has already |
| 412 // acquired the focus. | 408 // acquired the focus. |
| 413 } | 409 } |
| 414 | 410 |
| 415 void OmniboxViewViews::HandleFocusOut() { | 411 void OmniboxViewViews::HandleFocusOut() { |
| 416 gfx::NativeView native_view = NULL; | 412 gfx::NativeView native_view = NULL; |
| 417 #if defined(USE_AURA) | 413 #if defined(USE_AURA) |
| 418 views::Widget* widget = GetWidget(); | 414 views::Widget* widget = GetWidget(); |
| 419 if (widget) { | 415 if (widget) { |
| 420 aura::RootWindow* root = widget->GetNativeView()->GetRootWindow(); | 416 aura::RootWindow* root = widget->GetNativeView()->GetRootWindow(); |
| 421 if (root) | 417 if (root) |
| 422 native_view = root->GetFocusManager()->GetFocusedWindow(); | 418 native_view = root->GetFocusManager()->GetFocusedWindow(); |
| 423 } | 419 } |
| 424 #endif | 420 #endif |
| 425 model_->OnWillKillFocus(native_view); | 421 model()->OnWillKillFocus(native_view); |
| 426 // Close the popup. | 422 // Close the popup. |
| 427 ClosePopup(); | 423 CloseOmniboxPopup(); |
| 428 // Tell the model to reset itself. | 424 // Tell the model to reset itself. |
| 429 model_->OnKillFocus(); | 425 model()->OnKillFocus(); |
| 430 controller_->OnKillFocus(); | 426 controller()->OnKillFocus(); |
| 431 } | 427 } |
| 432 | 428 |
| 433 void OmniboxViewViews::SetLocationEntryFocusable(bool focusable) { | 429 void OmniboxViewViews::SetLocationEntryFocusable(bool focusable) { |
| 434 textfield_->set_focusable(focusable); | 430 textfield_->set_focusable(focusable); |
| 435 } | 431 } |
| 436 | 432 |
| 437 bool OmniboxViewViews::IsLocationEntryFocusableInRootView() const { | 433 bool OmniboxViewViews::IsLocationEntryFocusableInRootView() const { |
| 438 return textfield_->IsFocusable(); | 434 return textfield_->IsFocusable(); |
| 439 } | 435 } |
| 440 | 436 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 462 } | 458 } |
| 463 | 459 |
| 464 void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 460 void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 465 if (popup_view_->IsOpen()) | 461 if (popup_view_->IsOpen()) |
| 466 popup_view_->UpdatePopupAppearance(); | 462 popup_view_->UpdatePopupAppearance(); |
| 467 } | 463 } |
| 468 | 464 |
| 469 //////////////////////////////////////////////////////////////////////////////// | 465 //////////////////////////////////////////////////////////////////////////////// |
| 470 // OmniboxViewViews, AutocopmleteEditView implementation: | 466 // OmniboxViewViews, AutocopmleteEditView implementation: |
| 471 | 467 |
| 472 OmniboxEditModel* OmniboxViewViews::model() { | |
| 473 return model_.get(); | |
| 474 } | |
| 475 | |
| 476 const OmniboxEditModel* OmniboxViewViews::model() const { | |
| 477 return model_.get(); | |
| 478 } | |
| 479 | |
| 480 void OmniboxViewViews::SaveStateToTab(WebContents* tab) { | 468 void OmniboxViewViews::SaveStateToTab(WebContents* tab) { |
| 481 DCHECK(tab); | 469 DCHECK(tab); |
| 482 | 470 |
| 483 // We don't want to keep the IME status, so force quit the current | 471 // We don't want to keep the IME status, so force quit the current |
| 484 // session here. It may affect the selection status, so order is | 472 // session here. It may affect the selection status, so order is |
| 485 // also important. | 473 // also important. |
| 486 if (textfield_->IsIMEComposing()) { | 474 if (textfield_->IsIMEComposing()) { |
| 487 textfield_->GetTextInputClient()->ConfirmCompositionText(); | 475 textfield_->GetTextInputClient()->ConfirmCompositionText(); |
| 488 textfield_->GetInputMethod()->CancelComposition(textfield_); | 476 textfield_->GetInputMethod()->CancelComposition(textfield_); |
| 489 } | 477 } |
| 490 | 478 |
| 491 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. | 479 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. |
| 492 OmniboxEditModel::State model_state = model_->GetStateForTabSwitch(); | 480 OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); |
| 493 gfx::SelectionModel selection; | 481 gfx::SelectionModel selection; |
| 494 textfield_->GetSelectionModel(&selection); | 482 textfield_->GetSelectionModel(&selection); |
| 495 GetStateAccessor()->SetProperty( | 483 GetStateAccessor()->SetProperty( |
| 496 tab->GetPropertyBag(), | 484 tab->GetPropertyBag(), |
| 497 AutocompleteEditState(model_state, ViewState(selection))); | 485 AutocompleteEditState(model_state, ViewState(selection))); |
| 498 } | 486 } |
| 499 | 487 |
| 500 void OmniboxViewViews::Update(const WebContents* contents) { | 488 void OmniboxViewViews::Update(const WebContents* contents) { |
| 501 // NOTE: We're getting the URL text here from the ToolbarModel. | 489 // NOTE: We're getting the URL text here from the ToolbarModel. |
| 502 bool visibly_changed_permanent_text = | 490 bool visibly_changed_permanent_text = |
| 503 model_->UpdatePermanentText(toolbar_model_->GetText()); | 491 model()->UpdatePermanentText(toolbar_model()->GetText()); |
| 504 ToolbarModel::SecurityLevel security_level = | 492 ToolbarModel::SecurityLevel security_level = |
| 505 toolbar_model_->GetSecurityLevel(); | 493 toolbar_model()->GetSecurityLevel(); |
| 506 bool changed_security_level = (security_level != security_level_); | 494 bool changed_security_level = (security_level != security_level_); |
| 507 security_level_ = security_level; | 495 security_level_ = security_level; |
| 508 | 496 |
| 509 // TODO(oshima): Copied from gtk implementation which is | 497 // TODO(oshima): Copied from gtk implementation which is |
| 510 // slightly different from WIN impl. Find out the correct implementation | 498 // slightly different from WIN impl. Find out the correct implementation |
| 511 // for views-implementation. | 499 // for views-implementation. |
| 512 if (contents) { | 500 if (contents) { |
| 513 RevertAll(); | 501 RevertAll(); |
| 514 const AutocompleteEditState* state = | 502 const AutocompleteEditState* state = |
| 515 GetStateAccessor()->GetProperty(contents->GetPropertyBag()); | 503 GetStateAccessor()->GetProperty(contents->GetPropertyBag()); |
| 516 if (state) { | 504 if (state) { |
| 517 model_->RestoreState(state->model_state); | 505 model()->RestoreState(state->model_state); |
| 518 | 506 |
| 519 // Move the marks for the cursor and the other end of the selection to | 507 // Move the marks for the cursor and the other end of the selection to |
| 520 // the previously-saved offsets (but preserve PRIMARY). | 508 // the previously-saved offsets (but preserve PRIMARY). |
| 521 textfield_->SelectSelectionModel(state->view_state.selection_model); | 509 textfield_->SelectSelectionModel(state->view_state.selection_model); |
| 522 // We do not carry over the current edit history to another tab. | 510 // We do not carry over the current edit history to another tab. |
| 523 // TODO(oshima): consider saving/restoring edit history. | 511 // TODO(oshima): consider saving/restoring edit history. |
| 524 textfield_->ClearEditHistory(); | 512 textfield_->ClearEditHistory(); |
| 525 } | 513 } |
| 526 } else if (visibly_changed_permanent_text) { | 514 } else if (visibly_changed_permanent_text) { |
| 527 RevertAll(); | 515 RevertAll(); |
| 528 } else if (changed_security_level) { | 516 } else if (changed_security_level) { |
| 529 EmphasizeURLComponents(); | 517 EmphasizeURLComponents(); |
| 530 } | 518 } |
| 531 } | 519 } |
| 532 | 520 |
| 533 void OmniboxViewViews::OpenMatch(const AutocompleteMatch& match, | |
| 534 WindowOpenDisposition disposition, | |
| 535 const GURL& alternate_nav_url, | |
| 536 size_t selected_line) { | |
| 537 if (!match.destination_url.is_valid()) | |
| 538 return; | |
| 539 | |
| 540 model_->OpenMatch(match, disposition, alternate_nav_url, selected_line); | |
| 541 } | |
| 542 | |
| 543 string16 OmniboxViewViews::GetText() const { | 521 string16 OmniboxViewViews::GetText() const { |
| 544 // TODO(oshima): IME support | 522 // TODO(oshima): IME support |
| 545 return textfield_->text(); | 523 return textfield_->text(); |
| 546 } | 524 } |
| 547 | 525 |
| 548 bool OmniboxViewViews::IsEditingOrEmpty() const { | |
| 549 return model_->user_input_in_progress() || (GetTextLength() == 0); | |
| 550 } | |
| 551 | |
| 552 int OmniboxViewViews::GetIcon() const { | |
| 553 return IsEditingOrEmpty() ? | |
| 554 AutocompleteMatch::TypeToIcon(model_->CurrentTextType()) : | |
| 555 toolbar_model_->GetIcon(); | |
| 556 } | |
| 557 | |
| 558 void OmniboxViewViews::SetUserText(const string16& text) { | |
| 559 SetUserText(text, text, true); | |
| 560 } | |
| 561 | |
| 562 void OmniboxViewViews::SetUserText(const string16& text, | |
| 563 const string16& display_text, | |
| 564 bool update_popup) { | |
| 565 model_->SetUserText(text); | |
| 566 SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup, | |
| 567 true); | |
| 568 } | |
| 569 | |
| 570 void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text, | 526 void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text, |
| 571 size_t caret_pos, | 527 size_t caret_pos, |
| 572 bool update_popup, | 528 bool update_popup, |
| 573 bool notify_text_changed) { | 529 bool notify_text_changed) { |
| 574 const ui::Range range(caret_pos, caret_pos); | 530 const ui::Range range(caret_pos, caret_pos); |
| 575 SetTextAndSelectedRange(text, range); | 531 SetTextAndSelectedRange(text, range); |
| 576 | 532 |
| 577 if (update_popup) | 533 if (update_popup) |
| 578 UpdatePopup(); | 534 UpdatePopup(); |
| 579 | 535 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 } else { | 567 } else { |
| 612 *start = static_cast<size_t>(range.end()); | 568 *start = static_cast<size_t>(range.end()); |
| 613 *end = static_cast<size_t>(range.start()); | 569 *end = static_cast<size_t>(range.start()); |
| 614 } | 570 } |
| 615 } | 571 } |
| 616 | 572 |
| 617 void OmniboxViewViews::SelectAll(bool reversed) { | 573 void OmniboxViewViews::SelectAll(bool reversed) { |
| 618 textfield_->SelectAll(reversed); | 574 textfield_->SelectAll(reversed); |
| 619 } | 575 } |
| 620 | 576 |
| 621 void OmniboxViewViews::RevertAll() { | |
| 622 ClosePopup(); | |
| 623 model_->Revert(); | |
| 624 TextChanged(); | |
| 625 } | |
| 626 | |
| 627 void OmniboxViewViews::UpdatePopup() { | 577 void OmniboxViewViews::UpdatePopup() { |
| 628 model_->SetInputInProgress(true); | 578 model()->SetInputInProgress(true); |
| 629 if (ime_candidate_window_open_) | 579 if (ime_candidate_window_open_) |
| 630 return; | 580 return; |
| 631 if (!model_->has_focus()) | 581 if (!model()->has_focus()) |
| 632 return; | 582 return; |
| 633 | 583 |
| 634 // Don't inline autocomplete when the caret/selection isn't at the end of | 584 // Don't inline autocomplete when the caret/selection isn't at the end of |
| 635 // the text, or in the middle of composition. | 585 // the text, or in the middle of composition. |
| 636 ui::Range sel; | 586 ui::Range sel; |
| 637 textfield_->GetSelectedRange(&sel); | 587 textfield_->GetSelectedRange(&sel); |
| 638 bool no_inline_autocomplete = | 588 bool no_inline_autocomplete = |
| 639 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); | 589 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); |
| 640 | 590 |
| 641 model_->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); | 591 model()->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); |
| 642 } | |
| 643 | |
| 644 void OmniboxViewViews::ClosePopup() { | |
| 645 model_->StopAutocomplete(); | |
| 646 } | 592 } |
| 647 | 593 |
| 648 void OmniboxViewViews::SetFocus() { | 594 void OmniboxViewViews::SetFocus() { |
| 649 // In views-implementation, the focus is on textfield rather than OmniboxView. | 595 // In views-implementation, the focus is on textfield rather than OmniboxView. |
| 650 textfield_->RequestFocus(); | 596 textfield_->RequestFocus(); |
| 651 } | 597 } |
| 652 | 598 |
| 653 void OmniboxViewViews::OnTemporaryTextMaybeChanged( | 599 void OmniboxViewViews::OnTemporaryTextMaybeChanged( |
| 654 const string16& display_text, | 600 const string16& display_text, |
| 655 bool save_original_selection) { | 601 bool save_original_selection) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 | 642 |
| 697 // When the user has deleted text, we don't allow inline autocomplete. Make | 643 // When the user has deleted text, we don't allow inline autocomplete. Make |
| 698 // sure to not flag cases like selecting part of the text and then pasting | 644 // sure to not flag cases like selecting part of the text and then pasting |
| 699 // (or typing) the prefix of that selection. (We detect these by making | 645 // (or typing) the prefix of that selection. (We detect these by making |
| 700 // sure the caret, which should be after any insertion, hasn't moved | 646 // sure the caret, which should be after any insertion, hasn't moved |
| 701 // forward of the old selection start.) | 647 // forward of the old selection start.) |
| 702 const bool just_deleted_text = | 648 const bool just_deleted_text = |
| 703 (text_before_change_.length() > new_text.length()) && | 649 (text_before_change_.length() > new_text.length()) && |
| 704 (new_sel.start() <= sel_before_change_.GetMin()); | 650 (new_sel.start() <= sel_before_change_.GetMin()); |
| 705 | 651 |
| 706 const bool something_changed = model_->OnAfterPossibleChange( | 652 const bool something_changed = model()->OnAfterPossibleChange( |
| 707 text_before_change_, new_text, new_sel.start(), new_sel.end(), | 653 text_before_change_, new_text, new_sel.start(), new_sel.end(), |
| 708 selection_differs, text_changed, just_deleted_text, | 654 selection_differs, text_changed, just_deleted_text, |
| 709 !textfield_->IsIMEComposing()); | 655 !textfield_->IsIMEComposing()); |
| 710 | 656 |
| 711 // If only selection was changed, we don't need to call |model_|'s | 657 // If only selection was changed, we don't need to call model()'s |
| 712 // OnChanged() method, which is called in TextChanged(). | 658 // OnChanged() method, which is called in TextChanged(). |
| 713 // But we still need to call EmphasizeURLComponents() to make sure the text | 659 // But we still need to call EmphasizeURLComponents() to make sure the text |
| 714 // attributes are updated correctly. | 660 // attributes are updated correctly. |
| 715 if (something_changed && text_changed) | 661 if (something_changed && text_changed) |
| 716 TextChanged(); | 662 TextChanged(); |
| 717 else if (selection_differs) | 663 else if (selection_differs) |
| 718 EmphasizeURLComponents(); | 664 EmphasizeURLComponents(); |
| 719 else if (delete_at_end_pressed_) | 665 else if (delete_at_end_pressed_) |
| 720 model_->OnChanged(); | 666 model()->OnChanged(); |
| 721 | 667 |
| 722 return something_changed; | 668 return something_changed; |
| 723 } | 669 } |
| 724 | 670 |
| 725 gfx::NativeView OmniboxViewViews::GetNativeView() const { | 671 gfx::NativeView OmniboxViewViews::GetNativeView() const { |
| 726 return GetWidget()->GetNativeView(); | 672 return GetWidget()->GetNativeView(); |
| 727 } | 673 } |
| 728 | 674 |
| 729 gfx::NativeView OmniboxViewViews::GetRelativeWindowForPopup() const { | 675 gfx::NativeView OmniboxViewViews::GetRelativeWindowForPopup() const { |
| 730 return GetWidget()->GetTopLevelWidget()->GetNativeView(); | 676 return GetWidget()->GetTopLevelWidget()->GetNativeView(); |
| 731 } | 677 } |
| 732 | 678 |
| 733 CommandUpdater* OmniboxViewViews::GetCommandUpdater() { | |
| 734 return command_updater_; | |
| 735 } | |
| 736 | |
| 737 void OmniboxViewViews::SetInstantSuggestion(const string16& input, | 679 void OmniboxViewViews::SetInstantSuggestion(const string16& input, |
| 738 bool animate_to_complete) { | 680 bool animate_to_complete) { |
| 739 #if defined(OS_WIN) || defined(USE_AURA) | 681 #if defined(OS_WIN) || defined(USE_AURA) |
| 740 location_bar_view_->SetInstantSuggestion(input, animate_to_complete); | 682 location_bar_view_->SetInstantSuggestion(input, animate_to_complete); |
| 741 #endif | 683 #endif |
| 742 } | 684 } |
| 743 | 685 |
| 744 string16 OmniboxViewViews::GetInstantSuggestion() const { | 686 string16 OmniboxViewViews::GetInstantSuggestion() const { |
| 745 #if defined(OS_WIN) || defined(USE_AURA) | 687 #if defined(OS_WIN) || defined(USE_AURA) |
| 746 return location_bar_view_->GetInstantSuggestion(); | 688 return location_bar_view_->GetInstantSuggestion(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 void OmniboxViewViews::ContentsChanged(views::Textfield* sender, | 721 void OmniboxViewViews::ContentsChanged(views::Textfield* sender, |
| 780 const string16& new_contents) { | 722 const string16& new_contents) { |
| 781 } | 723 } |
| 782 | 724 |
| 783 bool OmniboxViewViews::HandleKeyEvent(views::Textfield* textfield, | 725 bool OmniboxViewViews::HandleKeyEvent(views::Textfield* textfield, |
| 784 const views::KeyEvent& event) { | 726 const views::KeyEvent& event) { |
| 785 delete_at_end_pressed_ = false; | 727 delete_at_end_pressed_ = false; |
| 786 | 728 |
| 787 if (event.key_code() == ui::VKEY_BACK) { | 729 if (event.key_code() == ui::VKEY_BACK) { |
| 788 // Checks if it's currently in keyword search mode. | 730 // Checks if it's currently in keyword search mode. |
| 789 if (model_->is_keyword_hint() || model_->keyword().empty()) | 731 if (model()->is_keyword_hint() || model()->keyword().empty()) |
| 790 return false; | 732 return false; |
| 791 // If there is selection, let textfield handle the backspace. | 733 // If there is selection, let textfield handle the backspace. |
| 792 if (textfield_->HasSelection()) | 734 if (textfield_->HasSelection()) |
| 793 return false; | 735 return false; |
| 794 // If not at the begining of the text, let textfield handle the backspace. | 736 // If not at the begining of the text, let textfield handle the backspace. |
| 795 if (textfield_->GetCursorPosition()) | 737 if (textfield_->GetCursorPosition()) |
| 796 return false; | 738 return false; |
| 797 model_->ClearKeyword(GetText()); | 739 model()->ClearKeyword(GetText()); |
| 798 return true; | 740 return true; |
| 799 } | 741 } |
| 800 | 742 |
| 801 if (event.key_code() == ui::VKEY_DELETE && !event.IsAltDown()) { | 743 if (event.key_code() == ui::VKEY_DELETE && !event.IsAltDown()) { |
| 802 delete_at_end_pressed_ = | 744 delete_at_end_pressed_ = |
| 803 (!textfield_->HasSelection() && | 745 (!textfield_->HasSelection() && |
| 804 textfield_->GetCursorPosition() == textfield_->text().length()); | 746 textfield_->GetCursorPosition() == textfield_->text().length()); |
| 805 } | 747 } |
| 806 | 748 |
| 807 return false; | 749 return false; |
| 808 } | 750 } |
| 809 | 751 |
| 810 void OmniboxViewViews::OnBeforeUserAction(views::Textfield* sender) { | 752 void OmniboxViewViews::OnBeforeUserAction(views::Textfield* sender) { |
| 811 OnBeforePossibleChange(); | 753 OnBeforePossibleChange(); |
| 812 } | 754 } |
| 813 | 755 |
| 814 void OmniboxViewViews::OnAfterUserAction(views::Textfield* sender) { | 756 void OmniboxViewViews::OnAfterUserAction(views::Textfield* sender) { |
| 815 OnAfterPossibleChange(); | 757 OnAfterPossibleChange(); |
| 816 } | 758 } |
| 817 | 759 |
| 818 void OmniboxViewViews::OnAfterCutOrCopy() { | 760 void OmniboxViewViews::OnAfterCutOrCopy() { |
| 819 ui::Range selection_range; | 761 ui::Range selection_range; |
| 820 textfield_->GetSelectedRange(&selection_range); | 762 textfield_->GetSelectedRange(&selection_range); |
| 821 ui::Clipboard* cb = views::ViewsDelegate::views_delegate->GetClipboard(); | 763 ui::Clipboard* cb = views::ViewsDelegate::views_delegate->GetClipboard(); |
| 822 string16 selected_text; | 764 string16 selected_text; |
| 823 cb->ReadText(ui::Clipboard::BUFFER_STANDARD, &selected_text); | 765 cb->ReadText(ui::Clipboard::BUFFER_STANDARD, &selected_text); |
| 824 const string16 text = textfield_->text(); | 766 const string16 text = textfield_->text(); |
| 825 GURL url; | 767 GURL url; |
| 826 bool write_url; | 768 bool write_url; |
| 827 model_->AdjustTextForCopy(selection_range.GetMin(), selected_text == text, | 769 model()->AdjustTextForCopy(selection_range.GetMin(), selected_text == text, |
| 828 &selected_text, &url, &write_url); | 770 &selected_text, &url, &write_url); |
| 829 ui::ScopedClipboardWriter scw(cb, ui::Clipboard::BUFFER_STANDARD); | 771 ui::ScopedClipboardWriter scw(cb, ui::Clipboard::BUFFER_STANDARD); |
| 830 scw.WriteText(selected_text); | 772 scw.WriteText(selected_text); |
| 831 if (write_url) { | 773 if (write_url) { |
| 832 BookmarkNodeData data; | 774 BookmarkNodeData data; |
| 833 data.ReadFromTuple(url, text); | 775 data.ReadFromTuple(url, text); |
| 834 data.WriteToClipboard(NULL); | 776 data.WriteToClipboard(NULL); |
| 835 } | 777 } |
| 836 } | 778 } |
| 837 | 779 |
| 838 void OmniboxViewViews::OnWriteDragData(ui::OSExchangeData* data) { | 780 void OmniboxViewViews::OnWriteDragData(ui::OSExchangeData* data) { |
| 839 ui::Range selection_range; | 781 ui::Range selection_range; |
| 840 textfield_->GetSelectedRange(&selection_range); | 782 textfield_->GetSelectedRange(&selection_range); |
| 841 string16 selected_text = textfield_->GetSelectedText(); | 783 string16 selected_text = textfield_->GetSelectedText(); |
| 842 const string16 text = textfield_->text(); | 784 const string16 text = textfield_->text(); |
| 843 GURL url; | 785 GURL url; |
| 844 bool write_url; | 786 bool write_url; |
| 845 model_->AdjustTextForCopy(selection_range.start(), selected_text == text, | 787 model()->AdjustTextForCopy(selection_range.start(), selected_text == text, |
| 846 &selected_text, &url, &write_url); | 788 &selected_text, &url, &write_url); |
| 847 data->SetString(selected_text); | 789 data->SetString(selected_text); |
| 848 if (write_url) | 790 if (write_url) |
| 849 data->SetURL(url, selected_text); | 791 data->SetURL(url, selected_text); |
| 850 } | 792 } |
| 851 | 793 |
| 852 void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { | 794 void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { |
| 853 // Minor note: We use IDC_ for command id here while the underlying textfield | 795 // Minor note: We use IDC_ for command id here while the underlying textfield |
| 854 // is using IDS_ for all its command ids. This is because views cannot depend | 796 // is using IDS_ for all its command ids. This is because views cannot depend |
| 855 // on IDC_ for now. | 797 // on IDC_ for now. |
| 856 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 798 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
| 857 IDS_EDIT_SEARCH_ENGINES); | 799 IDS_EDIT_SEARCH_ENGINES); |
| 858 | 800 |
| 859 int paste_position = menu_contents->GetIndexOfCommandId(IDS_APP_PASTE); | 801 int paste_position = menu_contents->GetIndexOfCommandId(IDS_APP_PASTE); |
| 860 if (paste_position >= 0) | 802 if (paste_position >= 0) |
| 861 menu_contents->InsertItemWithStringIdAt( | 803 menu_contents->InsertItemWithStringIdAt( |
| 862 paste_position + 1, IDS_PASTE_AND_GO, IDS_PASTE_AND_GO); | 804 paste_position + 1, IDS_PASTE_AND_GO, IDS_PASTE_AND_GO); |
| 863 } | 805 } |
| 864 | 806 |
| 865 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { | 807 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { |
| 866 return (command_id == IDS_PASTE_AND_GO) ? | 808 return (command_id == IDS_PASTE_AND_GO) ? |
| 867 model_->CanPasteAndGo(GetClipboardText()) : | 809 model()->CanPasteAndGo(GetClipboardText()) : |
| 868 command_updater_->IsCommandEnabled(command_id); | 810 command_updater()->IsCommandEnabled(command_id); |
| 869 } | 811 } |
| 870 | 812 |
| 871 bool OmniboxViewViews::IsItemForCommandIdDynamic(int command_id) const { | 813 bool OmniboxViewViews::IsItemForCommandIdDynamic(int command_id) const { |
| 872 return command_id == IDS_PASTE_AND_GO; | 814 return command_id == IDS_PASTE_AND_GO; |
| 873 } | 815 } |
| 874 | 816 |
| 875 string16 OmniboxViewViews::GetLabelForCommandId(int command_id) const { | 817 string16 OmniboxViewViews::GetLabelForCommandId(int command_id) const { |
| 876 if (command_id == IDS_PASTE_AND_GO) { | 818 if (command_id == IDS_PASTE_AND_GO) { |
| 877 return l10n_util::GetStringUTF16( | 819 return l10n_util::GetStringUTF16( |
| 878 model_->IsPasteAndSearch(GetClipboardText()) ? | 820 model()->IsPasteAndSearch(GetClipboardText()) ? |
| 879 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO); | 821 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO); |
| 880 } | 822 } |
| 881 | 823 |
| 882 return string16(); | 824 return string16(); |
| 883 } | 825 } |
| 884 | 826 |
| 885 void OmniboxViewViews::ExecuteCommand(int command_id) { | 827 void OmniboxViewViews::ExecuteCommand(int command_id) { |
| 886 if (command_id == IDS_PASTE_AND_GO) { | 828 if (command_id == IDS_PASTE_AND_GO) { |
| 887 model_->PasteAndGo(GetClipboardText()); | 829 model()->PasteAndGo(GetClipboardText()); |
| 888 return; | 830 return; |
| 889 } | 831 } |
| 890 | 832 |
| 891 command_updater_->ExecuteCommand(command_id); | 833 command_updater()->ExecuteCommand(command_id); |
| 892 } | 834 } |
| 893 | 835 |
| 894 #if defined(OS_CHROMEOS) | 836 #if defined(OS_CHROMEOS) |
| 895 void OmniboxViewViews::CandidateWindowOpened( | 837 void OmniboxViewViews::CandidateWindowOpened( |
| 896 chromeos::input_method::InputMethodManager* manager) { | 838 chromeos::input_method::InputMethodManager* manager) { |
| 897 ime_candidate_window_open_ = true; | 839 ime_candidate_window_open_ = true; |
| 898 ClosePopup(); | 840 CloseOmniboxPopup(); |
| 899 } | 841 } |
| 900 | 842 |
| 901 void OmniboxViewViews::CandidateWindowClosed( | 843 void OmniboxViewViews::CandidateWindowClosed( |
| 902 chromeos::input_method::InputMethodManager* manager) { | 844 chromeos::input_method::InputMethodManager* manager) { |
| 903 ime_candidate_window_open_ = false; | 845 ime_candidate_window_open_ = false; |
| 904 UpdatePopup(); | 846 UpdatePopup(); |
| 905 } | 847 } |
| 906 #endif | 848 #endif |
| 907 | 849 |
| 908 //////////////////////////////////////////////////////////////////////////////// | 850 //////////////////////////////////////////////////////////////////////////////// |
| 909 // OmniboxViewViews, private: | 851 // OmniboxViewViews, private: |
| 910 | 852 |
| 911 size_t OmniboxViewViews::GetTextLength() const { | 853 size_t OmniboxViewViews::GetTextLength() const { |
| 912 // TODO(oshima): Support instant, IME. | 854 // TODO(oshima): Support instant, IME. |
| 913 return textfield_->text().length(); | 855 return textfield_->text().length(); |
| 914 } | 856 } |
| 915 | 857 |
| 858 int OmniboxViewViews::GetOmniboxTextLength() const { |
| 859 return static_cast<int>(GetTextLength()); |
| 860 } |
| 861 |
| 916 void OmniboxViewViews::EmphasizeURLComponents() { | 862 void OmniboxViewViews::EmphasizeURLComponents() { |
| 917 // See whether the contents are a URL with a non-empty host portion, which we | 863 // See whether the contents are a URL with a non-empty host portion, which we |
| 918 // should emphasize. To check for a URL, rather than using the type returned | 864 // should emphasize. To check for a URL, rather than using the type returned |
| 919 // by Parse(), ask the model, which will check the desired page transition for | 865 // by Parse(), ask the model, which will check the desired page transition for |
| 920 // this input. This can tell us whether an UNKNOWN input string is going to | 866 // this input. This can tell us whether an UNKNOWN input string is going to |
| 921 // be treated as a search or a navigation, and is the same method the Paste | 867 // be treated as a search or a navigation, and is the same method the Paste |
| 922 // And Go system uses. | 868 // And Go system uses. |
| 923 string16 text = GetText(); | 869 string16 text = GetText(); |
| 924 url_parse::Component scheme, host; | 870 url_parse::Component scheme, host; |
| 925 AutocompleteInput::ParseForEmphasizeComponents(text, model_->GetDesiredTLD(), | 871 AutocompleteInput::ParseForEmphasizeComponents(text, model()->GetDesiredTLD(), |
| 926 &scheme, &host); | 872 &scheme, &host); |
| 927 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); | 873 const bool emphasize = model()->CurrentTextIsURL() && (host.len > 0); |
| 928 | 874 |
| 929 SkColor base_color = LocationBarView::GetColor( | 875 SkColor base_color = LocationBarView::GetColor( |
| 930 security_level_, | 876 security_level_, |
| 931 emphasize ? LocationBarView::DEEMPHASIZED_TEXT : LocationBarView::TEXT); | 877 emphasize ? LocationBarView::DEEMPHASIZED_TEXT : LocationBarView::TEXT); |
| 932 ApplyURLStyle(textfield_, 0, text.length(), base_color, false); | 878 ApplyURLStyle(textfield_, 0, text.length(), base_color, false); |
| 933 | 879 |
| 934 if (emphasize) { | 880 if (emphasize) { |
| 935 SkColor normal_color = | 881 SkColor normal_color = |
| 936 LocationBarView::GetColor(security_level_, LocationBarView::TEXT); | 882 LocationBarView::GetColor(security_level_, LocationBarView::TEXT); |
| 937 ApplyURLStyle(textfield_, host.begin, host.end(), normal_color, false); | 883 ApplyURLStyle(textfield_, host.begin, host.end(), normal_color, false); |
| 938 } | 884 } |
| 939 | 885 |
| 940 // Emphasize the scheme for security UI display purposes (if necessary). | 886 // Emphasize the scheme for security UI display purposes (if necessary). |
| 941 if (!model_->user_input_in_progress() && scheme.is_nonempty() && | 887 if (!model()->user_input_in_progress() && scheme.is_nonempty() && |
| 942 (security_level_ != ToolbarModel::NONE)) { | 888 (security_level_ != ToolbarModel::NONE)) { |
| 943 SkColor security_color = LocationBarView::GetColor( | 889 SkColor security_color = LocationBarView::GetColor( |
| 944 security_level_, LocationBarView::SECURITY_TEXT); | 890 security_level_, LocationBarView::SECURITY_TEXT); |
| 945 bool use_strikethrough = (security_level_ == ToolbarModel::SECURITY_ERROR); | 891 bool use_strikethrough = (security_level_ == ToolbarModel::SECURITY_ERROR); |
| 946 ApplyURLStyle(textfield_, scheme.begin, scheme.end(), | 892 ApplyURLStyle(textfield_, scheme.begin, scheme.end(), |
| 947 security_color, use_strikethrough); | 893 security_color, use_strikethrough); |
| 948 } | 894 } |
| 949 } | 895 } |
| 950 | 896 |
| 951 void OmniboxViewViews::TextChanged() { | |
| 952 EmphasizeURLComponents(); | |
| 953 model_->OnChanged(); | |
| 954 } | |
| 955 | |
| 956 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, | 897 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, |
| 957 const ui::Range& range) { | 898 const ui::Range& range) { |
| 958 if (text != GetText()) | 899 if (text != GetText()) |
| 959 textfield_->SetText(text); | 900 textfield_->SetText(text); |
| 960 textfield_->SelectRange(range); | 901 textfield_->SelectRange(range); |
| 961 } | 902 } |
| 962 | 903 |
| 963 string16 OmniboxViewViews::GetSelectedText() const { | 904 string16 OmniboxViewViews::GetSelectedText() const { |
| 964 // TODO(oshima): Support instant, IME. | 905 // TODO(oshima): Support instant, IME. |
| 965 return textfield_->GetSelectedText(); | 906 return textfield_->GetSelectedText(); |
| 966 } | 907 } |
| OLD | NEW |