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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_GTK_OMNIBOX_OMNIBOX_POPUP_VIEW_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_OMNIBOX_OMNIBOX_POPUP_VIEW_GTK_H_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "chrome/browser/autocomplete/autocomplete_match.h"
17 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "ui/base/gtk/gtk_signal.h"
21 #include "ui/base/window_open_disposition.h"
22 #include "ui/gfx/font.h"
24 class AutocompleteResult
;
25 class GtkThemeService
;
26 class OmniboxEditModel
;
27 class OmniboxPopupModel
;
35 class OmniboxPopupViewGtk
: public OmniboxPopupView
,
36 public content::NotificationObserver
{
38 OmniboxPopupViewGtk(const gfx::Font
& font
,
39 OmniboxView
* omnibox_view
,
40 OmniboxEditModel
* edit_model
,
41 GtkWidget
* location_bar
);
42 virtual ~OmniboxPopupViewGtk();
44 // Initializes the view.
47 // Overridden from OmniboxPopupView:
48 virtual bool IsOpen() const OVERRIDE
;
49 virtual void InvalidateLine(size_t line
) OVERRIDE
;
50 virtual void UpdatePopupAppearance() OVERRIDE
;
51 virtual gfx::Rect
GetTargetBounds() OVERRIDE
;
52 virtual void PaintUpdatesNow() OVERRIDE
;
53 virtual void OnDragCanceled() OVERRIDE
;
55 // Overridden from content::NotificationObserver:
56 virtual void Observe(int type
,
57 const content::NotificationSource
& source
,
58 const content::NotificationDetails
& details
) OVERRIDE
;
61 // Convert a y-coordinate to the closest line / result.
62 size_t LineFromY(int y
) const;
64 // Return a Rect for the space for a result line. This excludes the border,
65 // but includes the padding. This is the area that is colored for a
67 gfx::Rect
GetRectForLine(size_t line
, int width
) const;
69 // Returns the number of hidden matches at the top of the popup. This is
70 // non-zero when a verbatim match like search-what-you-typed is present but
71 // should not be shown.
72 size_t GetHiddenMatchCount() const;
74 // Returns the current autocomplete result.
75 virtual const AutocompleteResult
& GetResult() const;
78 // Be friendly for unit tests.
79 friend class OmniboxPopupViewGtkTest
;
81 static void SetupLayoutForMatch(
83 const base::string16
& text
,
84 const AutocompleteMatch::ACMatchClassifications
& classifications
,
85 const GdkColor
* base_color
,
86 const GdkColor
* dim_color
,
87 const GdkColor
* url_color
,
88 const std::string
& prefix_text
);
90 virtual void Show(size_t num_results
);
93 // Restack the popup window directly above the browser's toplevel window.
96 // Accept a line of the results, for example, when the user clicks a line.
97 void AcceptLine(size_t line
, WindowOpenDisposition disposition
);
99 // Returns the appropriate icon to display beside |match|.
100 gfx::Image
IconForMatch(const AutocompleteMatch
& match
,
102 bool is_selected_keyword
);
104 // Returns the |index|th element of match, unless we're selected and showing
105 // the associated keyword match.
106 void GetVisibleMatchForInput(size_t index
,
107 const AutocompleteMatch
** match
,
108 bool* is_selected_keyword
);
110 CHROMEGTK_CALLBACK_1(OmniboxPopupViewGtk
, gboolean
, HandleMotion
,
113 CHROMEGTK_CALLBACK_1(OmniboxPopupViewGtk
, gboolean
, HandleButtonPress
,
116 CHROMEGTK_CALLBACK_1(OmniboxPopupViewGtk
, gboolean
, HandleButtonRelease
,
119 CHROMEGTK_CALLBACK_1(OmniboxPopupViewGtk
, gboolean
, HandleExpose
,
122 scoped_ptr
<OmniboxPopupModel
> model_
;
123 OmniboxView
* omnibox_view_
;
124 GtkWidget
* location_bar_
;
126 // Our popup window, which is the only widget used, and we paint it on our
127 // own. This widget shouldn't be exposed outside of this class.
129 // The pango layout object created from the window, cached across exposes.
130 PangoLayout
* layout_
;
132 GtkThemeService
* theme_service_
;
133 content::NotificationRegistrar registrar_
;
135 // Font used for suggestions after being derived from the constructor's
139 // A list of colors which we should use for drawing the popup. These change
140 // between gtk and normal mode.
141 GdkColor border_color_
;
142 GdkColor background_color_
;
143 GdkColor selected_background_color_
;
144 GdkColor hovered_background_color_
;
145 GdkColor content_text_color_
;
146 GdkColor selected_content_text_color_
;
147 GdkColor content_dim_text_color_
;
148 GdkColor selected_content_dim_text_color_
;
149 GdkColor url_text_color_
;
150 GdkColor url_selected_text_color_
;
152 // If the user cancels a dragging action (i.e. by pressing ESC), we don't have
153 // a convenient way to release mouse capture. Instead we use this flag to
154 // simply ignore all remaining drag events, and the eventual mouse release
155 // event. Since OnDragCanceled() can be called when we're not dragging, this
156 // flag is reset to false on a mouse pressed event, to make sure we don't
157 // erroneously ignore the next drag.
158 bool ignore_mouse_drag_
;
160 // Whether our popup is currently open / shown, or closed / hidden.
163 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupViewGtk
);
166 #endif // CHROME_BROWSER_UI_GTK_OMNIBOX_OMNIBOX_POPUP_VIEW_GTK_H_