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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1071 // | 1077 // |
1072 // Then if the key event is one of Tab, Enter and Escape, we need to trigger | 1078 // Then if the key event is one of Tab, Enter and Escape, we need to trigger |
1073 // the corresponding special behavior if IME did not handle it. | 1079 // the corresponding special behavior if IME did not handle it. |
1074 // For Escape key, if the default signal handler returns FALSE, then we know | 1080 // For Escape key, if the default signal handler returns FALSE, then we know |
1075 // it's not handled by IME. | 1081 // it's not handled by IME. |
1076 // | 1082 // |
1077 // For Tab key, as "accepts-tab" property of |text_view_| is set to FALSE, | 1083 // For Tab key, as "accepts-tab" property of |text_view_| is set to FALSE, |
1078 // if IME did not handle it then "move-focus" signal will be emitted by the | 1084 // if IME did not handle it then "move-focus" signal will be emitted by the |
1079 // default signal handler of |text_view_|. So we can intercept "move-focus" | 1085 // default signal handler of |text_view_|. So we can intercept "move-focus" |
1080 // signal of |text_view_| to know if a Tab key press event was handled by IME, | 1086 // signal of |text_view_| to know if a Tab key press event was handled by IME, |
1081 // and trigger Tab to search behavior when necessary in the signal handler. | 1087 // and trigger Tab to search or result traversal behavior when necessary in |
1088 // the signal handler. | |
1082 // | 1089 // |
1083 // But for Enter key, if IME did not handle the key event, the default signal | 1090 // But for Enter key, if IME did not handle the key event, the default signal |
1084 // handler will delete current selection range and insert '\n' and always | 1091 // handler will delete current selection range and insert '\n' and always |
1085 // return TRUE. We need to prevent |text_view_| from performing this default | 1092 // return TRUE. We need to prevent |text_view_| from performing this default |
1086 // action if IME did not handle the key event, because we don't want the | 1093 // action if IME did not handle the key event, because we don't want the |
1087 // content of omnibox to be changed before triggering our special behavior. | 1094 // content of omnibox to be changed before triggering our special behavior. |
1088 // Otherwise our special behavior would not be performed correctly. | 1095 // Otherwise our special behavior would not be performed correctly. |
1089 // | 1096 // |
1090 // But there is no way for us to prevent GtkTextView from handling the key | 1097 // But there is no way for us to prevent GtkTextView from handling the key |
1091 // event and performing built-in operation. So in order to achieve our goal, | 1098 // event and performing built-in operation. So in order to achieve our goal, |
(...skipping 21 matching lines...) Expand all Loading... | |
1113 enter_was_pressed_ = event->keyval == GDK_Return || | 1120 enter_was_pressed_ = event->keyval == GDK_Return || |
1114 event->keyval == GDK_ISO_Enter || | 1121 event->keyval == GDK_ISO_Enter || |
1115 event->keyval == GDK_KP_Enter; | 1122 event->keyval == GDK_KP_Enter; |
1116 | 1123 |
1117 // Set |tab_was_pressed_| to true if it's a Tab key press event, so that our | 1124 // Set |tab_was_pressed_| to true if it's a Tab key press event, so that our |
1118 // handler of "move-focus" signal can trigger Tab to search behavior when | 1125 // handler of "move-focus" signal can trigger Tab to search behavior when |
1119 // necessary. | 1126 // necessary. |
1120 tab_was_pressed_ = (event->keyval == GDK_Tab || | 1127 tab_was_pressed_ = (event->keyval == GDK_Tab || |
1121 event->keyval == GDK_ISO_Left_Tab || | 1128 event->keyval == GDK_ISO_Left_Tab || |
1122 event->keyval == GDK_KP_Tab) && | 1129 event->keyval == GDK_KP_Tab) && |
1123 !(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)); | 1130 !(event->state & GDK_CONTROL_MASK); |
1131 | |
1132 shift_was_pressed_ = event->state & GDK_SHIFT_MASK; | |
1124 | 1133 |
1125 delete_was_pressed_ = event->keyval == GDK_Delete || | 1134 delete_was_pressed_ = event->keyval == GDK_Delete || |
1126 event->keyval == GDK_KP_Delete; | 1135 event->keyval == GDK_KP_Delete; |
1127 | 1136 |
1128 // Reset |enter_was_inserted_|, which may be set in the "insert-text" signal | 1137 // Reset |enter_was_inserted_|, which may be set in the "insert-text" signal |
1129 // handler, so that we'll know if an Enter key event was handled by IME. | 1138 // handler, so that we'll know if an Enter key event was handled by IME. |
1130 enter_was_inserted_ = false; | 1139 enter_was_inserted_ = false; |
1131 | 1140 |
1132 // Reset |paste_clipboard_requested_| to make sure we won't misinterpret this | 1141 // Reset |paste_clipboard_requested_| to make sure we won't misinterpret this |
1133 // key input action as a paste action. | 1142 // key input action as a paste action. |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1709 void OmniboxViewGtk::HandleViewMoveFocus(GtkWidget* widget, | 1718 void OmniboxViewGtk::HandleViewMoveFocus(GtkWidget* widget, |
1710 GtkDirectionType direction) { | 1719 GtkDirectionType direction) { |
1711 if (!tab_was_pressed_) | 1720 if (!tab_was_pressed_) |
1712 return; | 1721 return; |
1713 | 1722 |
1714 // If special behavior is triggered, then stop the signal emission to | 1723 // If special behavior is triggered, then stop the signal emission to |
1715 // prevent the focus from being moved. | 1724 // prevent the focus from being moved. |
1716 bool handled = false; | 1725 bool handled = false; |
1717 | 1726 |
1718 // Trigger Tab to search behavior only when Tab key is pressed. | 1727 // Trigger Tab to search behavior only when Tab key is pressed. |
1719 if (model_->is_keyword_hint()) | 1728 if (model_->is_keyword_hint() && !shift_was_pressed_) { |
1720 handled = model_->AcceptKeyword(); | 1729 handled = model_->AcceptKeyword(); |
1730 } else if (model_->popup_model()->IsOpen()) { | |
1731 if (shift_was_pressed_ && | |
1732 model_->popup_model()->selected_line_state() == | |
1733 AutocompletePopupModel::KEYWORD) { | |
1734 model_->ClearKeyword(GetText()); | |
1735 handled = true; | |
Peter Kasting
2012/01/24 19:14:44
Nit: Factor this line down below the conditional s
| |
1736 } else { | |
1737 model_->OnUpOrDownKeyPressed(shift_was_pressed_ ? -1 : 1); | |
1738 handled = true; | |
1739 } | |
1740 } | |
1721 | 1741 |
1722 if (supports_pre_edit_ && !handled && !pre_edit_.empty()) | 1742 if (supports_pre_edit_ && !handled && !pre_edit_.empty()) |
1723 handled = true; | 1743 handled = true; |
1724 | 1744 |
1725 if (!handled && gtk_widget_get_visible(instant_view_)) | 1745 if (!handled && gtk_widget_get_visible(instant_view_)) |
1726 handled = model_->CommitSuggestedText(true); | 1746 handled = model_->CommitSuggestedText(true); |
1727 | 1747 |
1728 if (!handled) { | |
1729 if (!IsCaretAtEnd()) { | |
1730 OnBeforePossibleChange(); | |
1731 PlaceCaretAt(GetTextLength()); | |
1732 OnAfterPossibleChange(); | |
1733 handled = true; | |
1734 } | |
1735 } | |
1736 | |
1737 if (!handled) | 1748 if (!handled) |
1738 handled = model_->AcceptCurrentInstantPreview(); | 1749 handled = model_->AcceptCurrentInstantPreview(); |
1739 | 1750 |
1740 if (handled) { | 1751 if (handled) { |
1741 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); |
1742 g_signal_stop_emission(widget, signal_id, 0); | 1753 g_signal_stop_emission(widget, signal_id, 0); |
1743 } | 1754 } |
1744 } | 1755 } |
1745 | 1756 |
1746 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { | 1757 void OmniboxViewGtk::HandleCopyClipboard(GtkWidget* sender) { |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2392 | 2403 |
2393 // Make all the children of the widget visible. NOTE: this won't display | 2404 // Make all the children of the widget visible. NOTE: this won't display |
2394 // anything, it just toggles the visible flag. | 2405 // anything, it just toggles the visible flag. |
2395 gtk_widget_show_all(omnibox_view->GetNativeView()); | 2406 gtk_widget_show_all(omnibox_view->GetNativeView()); |
2396 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. | 2407 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. |
2397 gtk_widget_hide(omnibox_view->GetNativeView()); | 2408 gtk_widget_hide(omnibox_view->GetNativeView()); |
2398 | 2409 |
2399 return omnibox_view; | 2410 return omnibox_view; |
2400 } | 2411 } |
2401 #endif | 2412 #endif |
OLD | NEW |