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/gtk/omnibox/omnibox_view_gtk.h" | 5 #include "chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h" |
6 | 6 |
7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 | 548 |
549 void OmniboxViewGtk::SetUserText(const string16& text) { | 549 void OmniboxViewGtk::SetUserText(const string16& text) { |
550 SetUserText(text, text, true); | 550 SetUserText(text, text, true); |
551 } | 551 } |
552 | 552 |
553 void OmniboxViewGtk::SetUserText(const string16& text, | 553 void OmniboxViewGtk::SetUserText(const string16& text, |
554 const string16& display_text, | 554 const string16& display_text, |
555 bool update_popup) { | 555 bool update_popup) { |
556 model_->SetUserText(text); | 556 model_->SetUserText(text); |
557 // TODO(deanm): something about selection / focus change here. | 557 // TODO(deanm): something about selection / focus change here. |
558 SetWindowTextAndCaretPos(display_text, display_text.length()); | 558 SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup, |
559 if (update_popup) | 559 true); |
560 UpdatePopup(); | |
561 TextChanged(); | |
562 } | 560 } |
563 | 561 |
564 void OmniboxViewGtk::SetWindowTextAndCaretPos(const string16& text, | 562 void OmniboxViewGtk::SetWindowTextAndCaretPos(const string16& text, |
565 size_t caret_pos) { | 563 size_t caret_pos, |
| 564 bool update_popup, |
| 565 bool notify_text_changed) { |
566 CharRange range(static_cast<int>(caret_pos), static_cast<int>(caret_pos)); | 566 CharRange range(static_cast<int>(caret_pos), static_cast<int>(caret_pos)); |
567 SetTextAndSelectedRange(text, range); | 567 SetTextAndSelectedRange(text, range); |
| 568 |
| 569 if (update_popup) |
| 570 UpdatePopup(); |
| 571 |
| 572 if (notify_text_changed) |
| 573 TextChanged(); |
568 } | 574 } |
569 | 575 |
570 void OmniboxViewGtk::SetForcedQuery() { | 576 void OmniboxViewGtk::SetForcedQuery() { |
571 const string16 current_text(GetText()); | 577 const string16 current_text(GetText()); |
572 const size_t start = current_text.find_first_not_of(kWhitespaceUTF16); | 578 const size_t start = current_text.find_first_not_of(kWhitespaceUTF16); |
573 if (start == string16::npos || (current_text[start] != '?')) { | 579 if (start == string16::npos || (current_text[start] != '?')) { |
574 SetUserText(ASCIIToUTF16("?")); | 580 SetUserText(ASCIIToUTF16("?")); |
575 } else { | 581 } else { |
576 StartUpdatingHighlightedText(); | 582 StartUpdatingHighlightedText(); |
577 SetSelectedRange(CharRange(current_text.size(), start + 1)); | 583 SetSelectedRange(CharRange(current_text.size(), start + 1)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 model_->StopAutocomplete(); | 638 model_->StopAutocomplete(); |
633 } | 639 } |
634 | 640 |
635 void OmniboxViewGtk::OnTemporaryTextMaybeChanged( | 641 void OmniboxViewGtk::OnTemporaryTextMaybeChanged( |
636 const string16& display_text, | 642 const string16& display_text, |
637 bool save_original_selection) { | 643 bool save_original_selection) { |
638 if (save_original_selection) | 644 if (save_original_selection) |
639 saved_temporary_selection_ = GetSelection(); | 645 saved_temporary_selection_ = GetSelection(); |
640 | 646 |
641 StartUpdatingHighlightedText(); | 647 StartUpdatingHighlightedText(); |
642 SetWindowTextAndCaretPos(display_text, display_text.length()); | 648 SetWindowTextAndCaretPos(display_text, display_text.length(), false, false); |
643 FinishUpdatingHighlightedText(); | 649 FinishUpdatingHighlightedText(); |
644 TextChanged(); | 650 TextChanged(); |
645 } | 651 } |
646 | 652 |
647 bool OmniboxViewGtk::OnInlineAutocompleteTextMaybeChanged( | 653 bool OmniboxViewGtk::OnInlineAutocompleteTextMaybeChanged( |
648 const string16& display_text, | 654 const string16& display_text, |
649 size_t user_text_length) { | 655 size_t user_text_length) { |
650 if (display_text == GetText()) | 656 if (display_text == GetText()) |
651 return false; | 657 return false; |
652 | 658 |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1070 // | 1076 // |
1071 // Then if the key event is one of Tab, Enter and Escape, we need to trigger | 1077 // Then if the key event is one of Tab, Enter and Escape, we need to trigger |
1072 // the corresponding special behavior if IME did not handle it. | 1078 // the corresponding special behavior if IME did not handle it. |
1073 // For Escape key, if the default signal handler returns FALSE, then we know | 1079 // For Escape key, if the default signal handler returns FALSE, then we know |
1074 // it's not handled by IME. | 1080 // it's not handled by IME. |
1075 // | 1081 // |
1076 // For Tab key, as "accepts-tab" property of |text_view_| is set to FALSE, | 1082 // For Tab key, as "accepts-tab" property of |text_view_| is set to FALSE, |
1077 // if IME did not handle it then "move-focus" signal will be emitted by the | 1083 // if IME did not handle it then "move-focus" signal will be emitted by the |
1078 // default signal handler of |text_view_|. So we can intercept "move-focus" | 1084 // default signal handler of |text_view_|. So we can intercept "move-focus" |
1079 // signal of |text_view_| to know if a Tab key press event was handled by IME, | 1085 // signal of |text_view_| to know if a Tab key press event was handled by IME, |
1080 // and trigger Tab to search behavior when necessary in the signal handler. | 1086 // and trigger Tab to search or result traversal behavior when necessary in |
| 1087 // the signal handler. |
1081 // | 1088 // |
1082 // But for Enter key, if IME did not handle the key event, the default signal | 1089 // But for Enter key, if IME did not handle the key event, the default signal |
1083 // handler will delete current selection range and insert '\n' and always | 1090 // handler will delete current selection range and insert '\n' and always |
1084 // return TRUE. We need to prevent |text_view_| from performing this default | 1091 // return TRUE. We need to prevent |text_view_| from performing this default |
1085 // action if IME did not handle the key event, because we don't want the | 1092 // action if IME did not handle the key event, because we don't want the |
1086 // content of omnibox to be changed before triggering our special behavior. | 1093 // content of omnibox to be changed before triggering our special behavior. |
1087 // Otherwise our special behavior would not be performed correctly. | 1094 // Otherwise our special behavior would not be performed correctly. |
1088 // | 1095 // |
1089 // But there is no way for us to prevent GtkTextView from handling the key | 1096 // But there is no way for us to prevent GtkTextView from handling the key |
1090 // event and performing built-in operation. So in order to achieve our goal, | 1097 // event and performing built-in operation. So in order to achieve our goal, |
(...skipping 21 matching lines...) Expand all Loading... |
1112 enter_was_pressed_ = event->keyval == GDK_Return || | 1119 enter_was_pressed_ = event->keyval == GDK_Return || |
1113 event->keyval == GDK_ISO_Enter || | 1120 event->keyval == GDK_ISO_Enter || |
1114 event->keyval == GDK_KP_Enter; | 1121 event->keyval == GDK_KP_Enter; |
1115 | 1122 |
1116 // Set |tab_was_pressed_| to true if it's a Tab key press event, so that our | 1123 // Set |tab_was_pressed_| to true if it's a Tab key press event, so that our |
1117 // handler of "move-focus" signal can trigger Tab to search behavior when | 1124 // handler of "move-focus" signal can trigger Tab to search behavior when |
1118 // necessary. | 1125 // necessary. |
1119 tab_was_pressed_ = (event->keyval == GDK_Tab || | 1126 tab_was_pressed_ = (event->keyval == GDK_Tab || |
1120 event->keyval == GDK_ISO_Left_Tab || | 1127 event->keyval == GDK_ISO_Left_Tab || |
1121 event->keyval == GDK_KP_Tab) && | 1128 event->keyval == GDK_KP_Tab) && |
1122 !(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)); | 1129 !(event->state & GDK_CONTROL_MASK); |
| 1130 |
| 1131 shift_was_pressed_ = event->state & GDK_SHIFT_MASK; |
1123 | 1132 |
1124 delete_was_pressed_ = event->keyval == GDK_Delete || | 1133 delete_was_pressed_ = event->keyval == GDK_Delete || |
1125 event->keyval == GDK_KP_Delete; | 1134 event->keyval == GDK_KP_Delete; |
1126 | 1135 |
1127 // Reset |enter_was_inserted_|, which may be set in the "insert-text" signal | 1136 // Reset |enter_was_inserted_|, which may be set in the "insert-text" signal |
1128 // handler, so that we'll know if an Enter key event was handled by IME. | 1137 // handler, so that we'll know if an Enter key event was handled by IME. |
1129 enter_was_inserted_ = false; | 1138 enter_was_inserted_ = false; |
1130 | 1139 |
1131 // Reset |paste_clipboard_requested_| to make sure we won't misinterpret this | 1140 // Reset |paste_clipboard_requested_| to make sure we won't misinterpret this |
1132 // key input action as a paste action. | 1141 // key input action as a paste action. |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1710 void OmniboxViewGtk::HandleViewMoveFocus(GtkWidget* widget, | 1719 void OmniboxViewGtk::HandleViewMoveFocus(GtkWidget* widget, |
1711 GtkDirectionType direction) { | 1720 GtkDirectionType direction) { |
1712 if (!tab_was_pressed_) | 1721 if (!tab_was_pressed_) |
1713 return; | 1722 return; |
1714 | 1723 |
1715 // If special behavior is triggered, then stop the signal emission to | 1724 // If special behavior is triggered, then stop the signal emission to |
1716 // prevent the focus from being moved. | 1725 // prevent the focus from being moved. |
1717 bool handled = false; | 1726 bool handled = false; |
1718 | 1727 |
1719 // Trigger Tab to search behavior only when Tab key is pressed. | 1728 // Trigger Tab to search behavior only when Tab key is pressed. |
1720 if (model_->is_keyword_hint()) | 1729 if (model_->is_keyword_hint() && !shift_was_pressed_) { |
1721 handled = model_->AcceptKeyword(); | 1730 handled = model_->AcceptKeyword(); |
| 1731 } else if (model_->popup_model()->IsOpen()) { |
| 1732 if (shift_was_pressed_ && |
| 1733 model_->popup_model()->selected_line_state() == |
| 1734 AutocompletePopupModel::KEYWORD) |
| 1735 model_->ClearKeyword(GetText()); |
| 1736 else |
| 1737 model_->OnUpOrDownKeyPressed(shift_was_pressed_ ? -1 : 1); |
| 1738 |
| 1739 handled = true; |
| 1740 } |
1722 | 1741 |
1723 if (supports_pre_edit_ && !handled && !pre_edit_.empty()) | 1742 if (supports_pre_edit_ && !handled && !pre_edit_.empty()) |
1724 handled = true; | 1743 handled = true; |
1725 | 1744 |
1726 if (!handled && gtk_widget_get_visible(instant_view_)) | 1745 if (!handled && gtk_widget_get_visible(instant_view_)) |
1727 handled = model_->CommitSuggestedText(true); | 1746 handled = model_->CommitSuggestedText(true); |
1728 | 1747 |
1729 if (!handled) { | |
1730 if (!IsCaretAtEnd()) { | |
1731 OnBeforePossibleChange(); | |
1732 PlaceCaretAt(GetTextLength()); | |
1733 OnAfterPossibleChange(); | |
1734 handled = true; | |
1735 } | |
1736 } | |
1737 | |
1738 if (!handled) | 1748 if (!handled) |
1739 handled = model_->AcceptCurrentInstantPreview(); | 1749 handled = model_->AcceptCurrentInstantPreview(); |
1740 | 1750 |
1741 if (handled) { | 1751 if (handled) { |
1742 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET); | 1752 static guint signal_id = g_signal_lookup("move-focus", GTK_TYPE_WIDGET); |
1743 g_signal_stop_emission(widget, signal_id, 0); | 1753 g_signal_stop_emission(widget, signal_id, 0); |
1744 } | 1754 } |
1745 } | 1755 } |
1746 | 1756 |
1747 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { | 1757 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { |
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2394 | 2404 |
2395 // Make all the children of the widget visible. NOTE: this won't display | 2405 // Make all the children of the widget visible. NOTE: this won't display |
2396 // anything, it just toggles the visible flag. | 2406 // anything, it just toggles the visible flag. |
2397 gtk_widget_show_all(omnibox_view->GetNativeView()); | 2407 gtk_widget_show_all(omnibox_view->GetNativeView()); |
2398 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. | 2408 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. |
2399 gtk_widget_hide(omnibox_view->GetNativeView()); | 2409 gtk_widget_hide(omnibox_view->GetNativeView()); |
2400 | 2410 |
2401 return omnibox_view; | 2411 return omnibox_view; |
2402 } | 2412 } |
2403 #endif | 2413 #endif |
OLD | NEW |