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