OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/autocomplete/autocomplete_edit_view_gtk.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 #include <gdk/gdkkeysyms.h> | 8 #include <gdk/gdkkeysyms.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "gfx/color_utils.h" | 31 #include "gfx/color_utils.h" |
32 #include "gfx/font.h" | 32 #include "gfx/font.h" |
33 #include "gfx/gtk_util.h" | 33 #include "gfx/gtk_util.h" |
34 #include "gfx/skia_utils_gtk.h" | 34 #include "gfx/skia_utils_gtk.h" |
35 #include "googleurl/src/gurl.h" | 35 #include "googleurl/src/gurl.h" |
36 #include "grit/generated_resources.h" | 36 #include "grit/generated_resources.h" |
37 #include "net/base/escape.h" | 37 #include "net/base/escape.h" |
38 #include "third_party/undoview/undo_view.h" | 38 #include "third_party/undoview/undo_view.h" |
39 | 39 |
40 #if defined(TOOLKIT_VIEWS) | 40 #if defined(TOOLKIT_VIEWS) |
41 #include "chrome/browser/views/autocomplete/autocomplete_popup_contents_view.h" | 41 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" |
42 #include "chrome/browser/views/location_bar/location_bar_view.h" | 42 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_contents_view.
h" |
| 43 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
43 #else | 44 #else |
44 #include "chrome/browser/autocomplete/autocomplete_popup_view_gtk.h" | 45 #include "chrome/browser/autocomplete/autocomplete_popup_view_gtk.h" |
45 #include "chrome/browser/gtk/gtk_theme_provider.h" | 46 #include "chrome/browser/gtk/gtk_theme_provider.h" |
46 #include "chrome/browser/gtk/location_bar_view_gtk.h" | 47 #include "chrome/browser/gtk/location_bar_view_gtk.h" |
| 48 #include "views/controls/native/native_view_host.h" |
47 #endif | 49 #endif |
48 | 50 |
49 namespace { | 51 namespace { |
50 | 52 |
51 const gchar* kAutocompleteEditViewGtkKey = "__ACE_VIEW_GTK__"; | 53 const gchar* kAutocompleteEditViewGtkKey = "__ACE_VIEW_GTK__"; |
52 | 54 |
53 const char kTextBaseColor[] = "#808080"; | 55 const char kTextBaseColor[] = "#808080"; |
54 const char kSecureSchemeColor[] = "#079500"; | 56 const char kSecureSchemeColor[] = "#079500"; |
55 const char kSecurityErrorSchemeColor[] = "#a20000"; | 57 const char kSecurityErrorSchemeColor[] = "#a20000"; |
56 | 58 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 413 |
412 // Use |signals_| to make sure we don't get called back after destruction. | 414 // Use |signals_| to make sure we don't get called back after destruction. |
413 signals_.Connect(new_toplevel, "set-focus", | 415 signals_.Connect(new_toplevel, "set-focus", |
414 G_CALLBACK(&HandleWindowSetFocusThunk), this); | 416 G_CALLBACK(&HandleWindowSetFocusThunk), this); |
415 } | 417 } |
416 | 418 |
417 void AutocompleteEditViewGtk::SetFocus() { | 419 void AutocompleteEditViewGtk::SetFocus() { |
418 gtk_widget_grab_focus(text_view_); | 420 gtk_widget_grab_focus(text_view_); |
419 } | 421 } |
420 | 422 |
421 int AutocompleteEditViewGtk::TextWidth() { | |
422 int horizontal_border_size = | |
423 gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_), | |
424 GTK_TEXT_WINDOW_LEFT) + | |
425 gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_), | |
426 GTK_TEXT_WINDOW_RIGHT) + | |
427 gtk_text_view_get_left_margin(GTK_TEXT_VIEW(text_view_)) + | |
428 gtk_text_view_get_right_margin(GTK_TEXT_VIEW(text_view_)); | |
429 | |
430 GtkTextIter start, end; | |
431 GdkRectangle first_char_bounds, last_char_bounds; | |
432 gtk_text_buffer_get_start_iter(text_buffer_, &start); | |
433 | |
434 // Use the real end iterator here to take the width of instant suggestion | |
435 // text into account, so that location bar can layout its children correctly. | |
436 gtk_text_buffer_get_end_iter(text_buffer_, &end); | |
437 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_), | |
438 &start, &first_char_bounds); | |
439 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_), | |
440 &end, &last_char_bounds); | |
441 | |
442 gint first_char_start = first_char_bounds.x; | |
443 gint first_char_end = first_char_start + first_char_bounds.width; | |
444 gint last_char_start = last_char_bounds.x; | |
445 gint last_char_end = last_char_start + last_char_bounds.width; | |
446 | |
447 // bounds width could be negative for RTL text. | |
448 if (first_char_start > first_char_end) | |
449 std::swap(first_char_start, first_char_end); | |
450 if (last_char_start > last_char_end) | |
451 std::swap(last_char_start, last_char_end); | |
452 | |
453 gint text_width = first_char_start < last_char_start ? | |
454 last_char_end - first_char_start : first_char_end - last_char_start; | |
455 | |
456 return text_width + horizontal_border_size; | |
457 } | |
458 | |
459 int AutocompleteEditViewGtk::WidthOfTextAfterCursor() { | 423 int AutocompleteEditViewGtk::WidthOfTextAfterCursor() { |
460 // Not used. | 424 // Not used. |
461 return -1; | 425 return -1; |
462 } | 426 } |
463 | 427 |
464 gfx::Font AutocompleteEditViewGtk::GetFont() { | 428 gfx::Font AutocompleteEditViewGtk::GetFont() { |
465 GtkRcStyle* rc_style = gtk_widget_get_modifier_style(text_view_); | 429 GtkRcStyle* rc_style = gtk_widget_get_modifier_style(text_view_); |
466 return gfx::Font((rc_style && rc_style->font_desc) ? | 430 return gfx::Font((rc_style && rc_style->font_desc) ? |
467 rc_style->font_desc : text_view_->style->font_desc); | 431 rc_style->font_desc : text_view_->style->font_desc); |
468 } | 432 } |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 } | 747 } |
784 | 748 |
785 gfx::NativeView AutocompleteEditViewGtk::GetNativeView() const { | 749 gfx::NativeView AutocompleteEditViewGtk::GetNativeView() const { |
786 return alignment_.get(); | 750 return alignment_.get(); |
787 } | 751 } |
788 | 752 |
789 CommandUpdater* AutocompleteEditViewGtk::GetCommandUpdater() { | 753 CommandUpdater* AutocompleteEditViewGtk::GetCommandUpdater() { |
790 return command_updater_; | 754 return command_updater_; |
791 } | 755 } |
792 | 756 |
| 757 #if defined(TOOLKIT_VIEWS) |
| 758 views::View* AutocompleteEditViewGtk::AddToView(views::View* parent) { |
| 759 views::NativeViewHost* host = new views::NativeViewHost; |
| 760 parent->AddChildView(host); |
| 761 host->set_focus_view(parent); |
| 762 host->Attach(GetNativeView()); |
| 763 return host; |
| 764 } |
| 765 |
| 766 bool AutocompleteEditViewGtk::CommitInstantSuggestion( |
| 767 const std::wstring& typed_text, |
| 768 const std::wstring& suggestion) { |
| 769 return CommitInstantSuggestion(); |
| 770 } |
| 771 |
| 772 void AutocompleteEditViewGtk::EnableAccessibility() { |
| 773 accessible_widget_helper_.reset( |
| 774 new AccessibleWidgetHelper(text_view(), model_->profile())); |
| 775 accessible_widget_helper_->SetWidgetName( |
| 776 text_view(), l10n_util::GetStringUTF8(IDS_ACCNAME_LOCATION)); |
| 777 } |
| 778 |
| 779 void AutocompleteEditViewGtk::SetInstantSuggestion(const string16& suggestion) { |
| 780 SetInstantSuggestion(UTF16ToUTF8(suggestion)); |
| 781 } |
| 782 #endif |
| 783 |
| 784 int AutocompleteEditViewGtk::TextWidth() const { |
| 785 int horizontal_border_size = |
| 786 gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_), |
| 787 GTK_TEXT_WINDOW_LEFT) + |
| 788 gtk_text_view_get_border_window_size(GTK_TEXT_VIEW(text_view_), |
| 789 GTK_TEXT_WINDOW_RIGHT) + |
| 790 gtk_text_view_get_left_margin(GTK_TEXT_VIEW(text_view_)) + |
| 791 gtk_text_view_get_right_margin(GTK_TEXT_VIEW(text_view_)); |
| 792 |
| 793 GtkTextIter start, end; |
| 794 GdkRectangle first_char_bounds, last_char_bounds; |
| 795 gtk_text_buffer_get_start_iter(text_buffer_, &start); |
| 796 |
| 797 // Use the real end iterator here to take the width of instant suggestion |
| 798 // text into account, so that location bar can layout its children correctly. |
| 799 gtk_text_buffer_get_end_iter(text_buffer_, &end); |
| 800 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_), |
| 801 &start, &first_char_bounds); |
| 802 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_), |
| 803 &end, &last_char_bounds); |
| 804 |
| 805 gint first_char_start = first_char_bounds.x; |
| 806 gint first_char_end = first_char_start + first_char_bounds.width; |
| 807 gint last_char_start = last_char_bounds.x; |
| 808 gint last_char_end = last_char_start + last_char_bounds.width; |
| 809 |
| 810 // bounds width could be negative for RTL text. |
| 811 if (first_char_start > first_char_end) |
| 812 std::swap(first_char_start, first_char_end); |
| 813 if (last_char_start > last_char_end) |
| 814 std::swap(last_char_start, last_char_end); |
| 815 |
| 816 gint text_width = first_char_start < last_char_start ? |
| 817 last_char_end - first_char_start : first_char_end - last_char_start; |
| 818 |
| 819 return text_width + horizontal_border_size; |
| 820 } |
| 821 |
| 822 #if defined(TOOLKIT_VIEWS) |
| 823 // static |
| 824 AutocompleteEditView* AutocompleteEditViewGtk::Create( |
| 825 AutocompleteEditController* controller, |
| 826 ToolbarModel* toolbar_model, |
| 827 Profile* profile, |
| 828 CommandUpdater* command_updater, |
| 829 bool popup_window_mode, |
| 830 const views::View* location_bar) { |
| 831 AutocompleteEditViewGtk* autocomplete = |
| 832 new AutocompleteEditViewGtk(controller, |
| 833 toolbar_model, |
| 834 profile, |
| 835 command_updater, |
| 836 popup_window_mode, |
| 837 location_bar); |
| 838 autocomplete->Init(); |
| 839 |
| 840 // Make all the children of the widget visible. NOTE: this won't display |
| 841 // anything, it just toggles the visible flag. |
| 842 gtk_widget_show_all(autocomplete->GetNativeView()); |
| 843 // Hide the widget. NativeViewHostGtk will make it visible again as |
| 844 // necessary. |
| 845 gtk_widget_hide(autocomplete->GetNativeView()); |
| 846 |
| 847 autocomplete->EnableAccessibility(); |
| 848 |
| 849 return autocomplete; |
| 850 } |
| 851 #endif |
| 852 |
793 void AutocompleteEditViewGtk::Observe(NotificationType type, | 853 void AutocompleteEditViewGtk::Observe(NotificationType type, |
794 const NotificationSource& source, | 854 const NotificationSource& source, |
795 const NotificationDetails& details) { | 855 const NotificationDetails& details) { |
796 DCHECK(type == NotificationType::BROWSER_THEME_CHANGED); | 856 DCHECK(type == NotificationType::BROWSER_THEME_CHANGED); |
797 | 857 |
798 SetBaseColor(); | 858 SetBaseColor(); |
799 } | 859 } |
800 | 860 |
801 void AutocompleteEditViewGtk::AnimationEnded(const Animation* animation) { | 861 void AutocompleteEditViewGtk::AnimationEnded(const Animation* animation) { |
802 controller_->OnCommitSuggestedText(GetText()); | 862 controller_->OnCommitSuggestedText(GetText()); |
(...skipping 1346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2149 // baseline, so we need to move the |instant_view_| down to make sure it | 2209 // baseline, so we need to move the |instant_view_| down to make sure it |
2150 // has the same baseline as the |text_view_|. | 2210 // has the same baseline as the |text_view_|. |
2151 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_)); | 2211 PangoLayout* layout = gtk_label_get_layout(GTK_LABEL(instant_view_)); |
2152 int height; | 2212 int height; |
2153 pango_layout_get_size(layout, NULL, &height); | 2213 pango_layout_get_size(layout, NULL, &height); |
2154 PangoLayoutIter* iter = pango_layout_get_iter(layout); | 2214 PangoLayoutIter* iter = pango_layout_get_iter(layout); |
2155 int baseline = pango_layout_iter_get_baseline(iter); | 2215 int baseline = pango_layout_iter_get_baseline(iter); |
2156 pango_layout_iter_free(iter); | 2216 pango_layout_iter_free(iter); |
2157 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); | 2217 g_object_set(instant_anchor_tag_, "rise", baseline - height, NULL); |
2158 } | 2218 } |
OLD | NEW |