diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-27 08:11:23 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-27 08:11:23 +0000 |
commit | 1b24f85903fca15bb44122c43c2e0e4548d1e094 (patch) | |
tree | 92454c88407e7f4f3faf0268503e7184b84df197 /chrome/browser | |
parent | 1222ccea51b22334002559b2f728d6b41a191a8d (diff) | |
download | chromium_src-1b24f85903fca15bb44122c43c2e0e4548d1e094.zip chromium_src-1b24f85903fca15bb44122c43c2e0e4548d1e094.tar.gz chromium_src-1b24f85903fca15bb44122c43c2e0e4548d1e094.tar.bz2 |
Linux Omnibox mouse support.
- Paint both concepts of hovered and selected lines.
- Track mouse movements and button up/down.
TEST=Select, drag, and click entries in the Omnibox results. Should behave just like Windows.
Review URL: http://codereview.chromium.org/92124
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14591 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rwxr-xr-x | chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc | 77 | ||||
-rwxr-xr-x | chrome/browser/autocomplete/autocomplete_popup_view_gtk.h | 30 |
2 files changed, 103 insertions, 4 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc index b9ddf1b..b21152b 100755 --- a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc +++ b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.cc @@ -29,6 +29,7 @@ namespace { const GdkColor kBorderColor = GDK_COLOR_RGB(0xc7, 0xca, 0xce); const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xff, 0xff, 0xff); const GdkColor kSelectedBackgroundColor = GDK_COLOR_RGB(0xdf, 0xe6, 0xf6); +const GdkColor kHoveredBackgroundColor = GDK_COLOR_RGB(0xef, 0xf2, 0xfa); const GdkColor kContentTextColor = GDK_COLOR_RGB(0x00, 0x00, 0x00); const GdkColor kURLTextColor = GDK_COLOR_RGB(0x00, 0x88, 0x00); @@ -210,6 +211,16 @@ AutocompletePopupViewGtk::AutocompletePopupViewGtk( pango_layout_set_font_description(layout_, pfd); pango_font_description_free(pfd); + gtk_widget_add_events(window_, GDK_BUTTON_MOTION_MASK | + GDK_POINTER_MOTION_MASK | + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK); + g_signal_connect(window_, "motion-notify-event", + G_CALLBACK(&HandleMotionThunk), this); + g_signal_connect(window_, "button-press-event", + G_CALLBACK(&HandleButtonPressThunk), this); + g_signal_connect(window_, "button-release-event", + G_CALLBACK(&HandleButtonReleaseThunk), this); g_signal_connect(window_, "expose-event", G_CALLBACK(&HandleExposeThunk), this); } @@ -273,6 +284,64 @@ void AutocompletePopupViewGtk::Hide() { opened_ = false; } +size_t AutocompletePopupViewGtk::LineFromY(int y) { + size_t line = std::max(y - kBorderThickness, 0) / kHeightPerResult; + return std::min(line, model_->result().size() - 1); +} + +void AutocompletePopupViewGtk::AcceptLine(size_t line, + WindowOpenDisposition disposition) { + const AutocompleteMatch& match = model_->result().match_at(line); + // OpenURL() may close the popup, which will clear the result set and, by + // extension, |match| and its contents. So copy the relevant strings out to + // make sure they stay alive until the call completes. + const GURL url(match.destination_url); + std::wstring keyword; + const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword); + edit_view_->OpenURL(url, disposition, match.transition, GURL(), line, + is_keyword_hint ? std::wstring() : keyword); +} + +gboolean AutocompletePopupViewGtk::HandleMotion(GtkWidget* widget, + GdkEventMotion* event) { + // TODO(deanm): Windows has a bunch of complicated logic here. + size_t line = LineFromY(event->y); + // There is both a hovered and selected line, hovered just means your mouse + // is over it, but selected is what's showing in the location edit. + model_->SetHoveredLine(line); + // Select the line if the user has the left mouse button down. + if (event->state & GDK_BUTTON1_MASK) + model_->SetSelectedLine(line, false); + return TRUE; +} + +gboolean AutocompletePopupViewGtk::HandleButtonPress(GtkWidget* widget, + GdkEventButton* event) { + // Very similar to HandleMotion. + size_t line = LineFromY(event->y); + model_->SetHoveredLine(line); + if (event->button == 1) + model_->SetSelectedLine(line, false); + return TRUE; +} + +gboolean AutocompletePopupViewGtk::HandleButtonRelease(GtkWidget* widget, + GdkEventButton* event) { + size_t line = LineFromY(event->y); + switch (event->button) { + case 1: // Left click. + AcceptLine(line, CURRENT_TAB); + break; + case 2: // Middle click. + AcceptLine(line, NEW_BACKGROUND_TAB); + break; + default: + // Don't open the result. + break; + } + return TRUE; +} + gboolean AutocompletePopupViewGtk::HandleExpose(GtkWidget* widget, GdkEventExpose* event) { const AutocompleteResult& result = model_->result(); @@ -321,9 +390,11 @@ gboolean AutocompletePopupViewGtk::HandleExpose(GtkWidget* widget, GdkRectangle result_rect = GetRectForLine(i, window_rect.width); bool is_selected = (model_->selected_line() == i); - if (is_selected) { - gdk_gc_set_rgb_fg_color(gc_, &kSelectedBackgroundColor); - // This entry is selected, fill a rect with the selection color. + bool is_hovered = (model_->hovered_line() == i); + if (is_selected || is_hovered) { + gdk_gc_set_rgb_fg_color(gc_, is_selected ? &kSelectedBackgroundColor : + &kHoveredBackgroundColor); + // This entry is selected or hovered, fill a rect with the color. gdk_draw_rectangle(drawable, gc_, TRUE, result_rect.x, result_rect.y, result_rect.width, result_rect.height); diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h index 199fb23..0811e83 100755 --- a/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h +++ b/chrome/browser/autocomplete/autocomplete_popup_view_gtk.h @@ -40,14 +40,42 @@ class AutocompletePopupViewGtk : public AutocompletePopupView { void Show(size_t num_results); void Hide(); + // Convert a y-coordinate to the closest line / result. + size_t LineFromY(int y); + + // Accept a line of the results, for example, when the user clicks a line. + void AcceptLine(size_t line, WindowOpenDisposition disposition); + static gboolean HandleExposeThunk(GtkWidget* widget, GdkEventExpose* event, gpointer userdata) { return reinterpret_cast<AutocompletePopupViewGtk*>(userdata)-> HandleExpose(widget, event); } - gboolean HandleExpose(GtkWidget* widget, GdkEventExpose* event); + static gboolean HandleMotionThunk(GtkWidget* widget, GdkEventMotion* event, + gpointer userdata) { + return reinterpret_cast<AutocompletePopupViewGtk*>(userdata)-> + HandleMotion(widget, event); + } + gboolean HandleMotion(GtkWidget* widget, GdkEventMotion* event); + + static gboolean HandleButtonPressThunk(GtkWidget* widget, + GdkEventButton* event, + gpointer userdata) { + return reinterpret_cast<AutocompletePopupViewGtk*>(userdata)-> + HandleButtonPress(widget, event); + } + gboolean HandleButtonPress(GtkWidget* widget, GdkEventButton* event); + + static gboolean HandleButtonReleaseThunk(GtkWidget* widget, + GdkEventButton* event, + gpointer userdata) { + return reinterpret_cast<AutocompletePopupViewGtk*>(userdata)-> + HandleButtonRelease(widget, event); + } + gboolean HandleButtonRelease(GtkWidget* widget, GdkEventButton* event); + scoped_ptr<AutocompletePopupModel> model_; AutocompleteEditViewGtk* edit_view_; |