| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 model_(new AutocompleteEditModel(this, controller, profile)), | 89 model_(new AutocompleteEditModel(this, controller, profile)), |
| 90 popup_view_(AutocompletePopupView::CreatePopupView(gfx::Font(), this, | 90 popup_view_(AutocompletePopupView::CreatePopupView(gfx::Font(), this, |
| 91 model_.get(), | 91 model_.get(), |
| 92 profile, | 92 profile, |
| 93 popup_positioner)), | 93 popup_positioner)), |
| 94 controller_(controller), | 94 controller_(controller), |
| 95 toolbar_model_(toolbar_model), | 95 toolbar_model_(toolbar_model), |
| 96 command_updater_(command_updater), | 96 command_updater_(command_updater), |
| 97 popup_window_mode_(popup_window_mode), | 97 popup_window_mode_(popup_window_mode), |
| 98 scheme_security_level_(ToolbarModel::NORMAL), | 98 scheme_security_level_(ToolbarModel::NORMAL), |
| 99 selection_saved_(false), | |
| 100 mark_set_handler_id_(0), | 99 mark_set_handler_id_(0), |
| 101 button_1_pressed_(false), | 100 button_1_pressed_(false), |
| 102 text_selected_during_click_(false), | 101 text_selected_during_click_(false), |
| 103 text_view_focused_before_button_press_(false), | 102 text_view_focused_before_button_press_(false), |
| 104 #if !defined(TOOLKIT_VIEWS) | 103 #if !defined(TOOLKIT_VIEWS) |
| 105 theme_provider_(GtkThemeProvider::GetFrom(profile)), | 104 theme_provider_(GtkThemeProvider::GetFrom(profile)), |
| 106 #endif | 105 #endif |
| 107 tab_was_pressed_(false), | 106 tab_was_pressed_(false), |
| 108 paste_clipboard_requested_(false) { | 107 paste_clipboard_requested_(false) { |
| 109 model_->set_popup_model(popup_view_->GetModel()); | 108 model_->set_popup_model(popup_view_->GetModel()); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 234 |
| 236 void AutocompleteEditViewGtk::SaveStateToTab(TabContents* tab) { | 235 void AutocompleteEditViewGtk::SaveStateToTab(TabContents* tab) { |
| 237 DCHECK(tab); | 236 DCHECK(tab); |
| 238 GetStateAccessor()->SetProperty( | 237 GetStateAccessor()->SetProperty( |
| 239 tab->property_bag(), | 238 tab->property_bag(), |
| 240 AutocompleteEditState(model_->GetStateForTabSwitch(), | 239 AutocompleteEditState(model_->GetStateForTabSwitch(), |
| 241 ViewState(GetSelection()))); | 240 ViewState(GetSelection()))); |
| 242 | 241 |
| 243 // If any text has been selected, register it as the PRIMARY selection so it | 242 // If any text has been selected, register it as the PRIMARY selection so it |
| 244 // can still be pasted via middle-click after the text view is cleared. | 243 // can still be pasted via middle-click after the text view is cleared. |
| 245 if (!selected_text_.empty() && !selection_saved_) { | 244 if (!selected_text_.empty()) { |
| 246 SavePrimarySelection(selected_text_); | 245 SavePrimarySelection(selected_text_); |
| 247 selection_saved_ = true; | |
| 248 } | 246 } |
| 249 } | 247 } |
| 250 | 248 |
| 251 void AutocompleteEditViewGtk::Update(const TabContents* contents) { | 249 void AutocompleteEditViewGtk::Update(const TabContents* contents) { |
| 252 // NOTE: We're getting the URL text here from the ToolbarModel. | 250 // NOTE: We're getting the URL text here from the ToolbarModel. |
| 253 bool visibly_changed_permanent_text = | 251 bool visibly_changed_permanent_text = |
| 254 model_->UpdatePermanentText(toolbar_model_->GetText()); | 252 model_->UpdatePermanentText(toolbar_model_->GetText()); |
| 255 | 253 |
| 256 ToolbarModel::SecurityLevel security_level = | 254 ToolbarModel::SecurityLevel security_level = |
| 257 toolbar_model_->GetSchemeSecurityLevel(); | 255 toolbar_model_->GetSchemeSecurityLevel(); |
| 258 bool changed_security_level = (security_level != scheme_security_level_); | 256 bool changed_security_level = (security_level != scheme_security_level_); |
| 259 scheme_security_level_ = security_level; | 257 scheme_security_level_ = security_level; |
| 260 | 258 |
| 261 // TODO(deanm): This doesn't exactly match Windows. There there is a member | 259 // TODO(deanm): This doesn't exactly match Windows. There there is a member |
| 262 // background_color_. I think we can get away with just the level though. | 260 // background_color_. I think we can get away with just the level though. |
| 263 if (changed_security_level) { | 261 if (changed_security_level) { |
| 264 SetBaseColor(); | 262 SetBaseColor(); |
| 265 } | 263 } |
| 266 | 264 |
| 267 if (contents) { | 265 if (contents) { |
| 268 selected_text_.clear(); | 266 selected_text_.clear(); |
| 269 selection_saved_ = false; | |
| 270 RevertAll(); | 267 RevertAll(); |
| 271 const AutocompleteEditState* state = | 268 const AutocompleteEditState* state = |
| 272 GetStateAccessor()->GetProperty(contents->property_bag()); | 269 GetStateAccessor()->GetProperty(contents->property_bag()); |
| 273 if (state) { | 270 if (state) { |
| 274 model_->RestoreState(state->model_state); | 271 model_->RestoreState(state->model_state); |
| 275 | 272 |
| 276 // Move the marks for the cursor and the other end of the selection to | 273 // Move the marks for the cursor and the other end of the selection to |
| 277 // the previously-saved offsets. | 274 // the previously-saved offsets. |
| 278 GtkTextIter selection_iter, insert_iter; | 275 GtkTextIter selection_iter, insert_iter; |
| 279 ItersFromCharRange( | 276 ItersFromCharRange( |
| (...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 GtkTextIter* location, | 846 GtkTextIter* location, |
| 850 GtkTextMark* mark) { | 847 GtkTextMark* mark) { |
| 851 if (!text_buffer_ || buffer != text_buffer_) | 848 if (!text_buffer_ || buffer != text_buffer_) |
| 852 return; | 849 return; |
| 853 | 850 |
| 854 if (mark != gtk_text_buffer_get_insert(text_buffer_) && | 851 if (mark != gtk_text_buffer_get_insert(text_buffer_) && |
| 855 mark != gtk_text_buffer_get_selection_bound(text_buffer_)) { | 852 mark != gtk_text_buffer_get_selection_bound(text_buffer_)) { |
| 856 return; | 853 return; |
| 857 } | 854 } |
| 858 | 855 |
| 859 // Is no text selected in the GtkTextView? | |
| 860 bool no_text_selected = false; | |
| 861 | |
| 862 // Get the currently-selected text, if there is any. | 856 // Get the currently-selected text, if there is any. |
| 857 std::string new_selected_text; |
| 863 GtkTextIter start, end; | 858 GtkTextIter start, end; |
| 864 if (!gtk_text_buffer_get_selection_bounds(text_buffer_, &start, &end)) { | 859 if (gtk_text_buffer_get_selection_bounds(text_buffer_, &start, &end)) { |
| 865 no_text_selected = true; | |
| 866 } else { | |
| 867 gchar* text = gtk_text_iter_get_text(&start, &end); | 860 gchar* text = gtk_text_iter_get_text(&start, &end); |
| 868 size_t text_len = strlen(text); | 861 size_t text_len = strlen(text); |
| 869 if (!text_len) { | 862 if (text_len) { |
| 870 no_text_selected = true; | 863 new_selected_text = std::string(text, text_len); |
| 871 } else { | |
| 872 selected_text_ = std::string(text, text_len); | |
| 873 selection_saved_ = false; | |
| 874 } | 864 } |
| 875 g_free(text); | 865 g_free(text); |
| 876 } | 866 } |
| 877 | 867 |
| 878 // If the user just selected some text with the mouse (or at least while the | 868 // If the user just selected some text with the mouse (or at least while the |
| 879 // mouse button was down), make sure that we won't blow their selection away | 869 // mouse button was down), make sure that we won't blow their selection away |
| 880 // later by selecting all of the text when the button is released. | 870 // later by selecting all of the text when the button is released. |
| 881 if (button_1_pressed_ && !no_text_selected) { | 871 if (button_1_pressed_ && !new_selected_text.empty()) { |
| 882 text_selected_during_click_ = true; | 872 text_selected_during_click_ = true; |
| 883 } | 873 } |
| 884 | 874 |
| 885 // If we have some previously-selected text but it's no longer highlighted | 875 // If we had some text selected earlier but it's no longer highlighted, we |
| 886 // and we haven't saved it as the selection yet, we save it now. | 876 // might need to save it now... |
| 887 if (!selected_text_.empty() && no_text_selected && !selection_saved_) { | 877 if (!selected_text_.empty() && new_selected_text.empty()) { |
| 888 SavePrimarySelection(selected_text_); | 878 // ... but only if we currently own the selection. We want to manually |
| 889 selection_saved_ = true; | 879 // update the selection when the text is unhighlighted because the user |
| 880 // clicked in a blank area of the text view, but not when it's unhighlighted |
| 881 // because another client or widget took the selection. (This handler gets |
| 882 // called before the default handler, so as long as nobody else took the |
| 883 // selection, the text buffer still owns it even if GTK is about to take it |
| 884 // away in the default handler.) |
| 885 GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
| 886 if (gtk_clipboard_get_owner(clipboard) == G_OBJECT(text_buffer_)) { |
| 887 SavePrimarySelection(selected_text_); |
| 888 } |
| 890 } | 889 } |
| 890 |
| 891 selected_text_ = new_selected_text; |
| 891 } | 892 } |
| 892 | 893 |
| 893 // Just use the default behavior for DnD, except if the drop can be a PasteAndGo | 894 // Just use the default behavior for DnD, except if the drop can be a PasteAndGo |
| 894 // then override. | 895 // then override. |
| 895 void AutocompleteEditViewGtk::HandleDragDataReceived( | 896 void AutocompleteEditViewGtk::HandleDragDataReceived( |
| 896 GdkDragContext* context, gint x, gint y, | 897 GdkDragContext* context, gint x, gint y, |
| 897 GtkSelectionData* selection_data, guint target_type, guint time) { | 898 GtkSelectionData* selection_data, guint target_type, guint time) { |
| 898 // Reset |paste_clipboard_requested_| to make sure we won't misinterpret this | 899 // Reset |paste_clipboard_requested_| to make sure we won't misinterpret this |
| 899 // drop action as a paste action. | 900 // drop action as a paste action. |
| 900 paste_clipboard_requested_ = false; | 901 paste_clipboard_requested_ = false; |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1156 const std::string& selected_text) { | 1157 const std::string& selected_text) { |
| 1157 GtkClipboard* clipboard = | 1158 GtkClipboard* clipboard = |
| 1158 gtk_widget_get_clipboard(text_view_, GDK_SELECTION_PRIMARY); | 1159 gtk_widget_get_clipboard(text_view_, GDK_SELECTION_PRIMARY); |
| 1159 DCHECK(clipboard); | 1160 DCHECK(clipboard); |
| 1160 if (!clipboard) | 1161 if (!clipboard) |
| 1161 return; | 1162 return; |
| 1162 | 1163 |
| 1163 gtk_clipboard_set_text( | 1164 gtk_clipboard_set_text( |
| 1164 clipboard, selected_text.data(), selected_text.size()); | 1165 clipboard, selected_text.data(), selected_text.size()); |
| 1165 } | 1166 } |
| OLD | NEW |