Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_view_views.cc

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

Powered by Google App Engine
This is Rietveld 408576698