OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_GTK_LOCATION_BAR_VIEW_GTK_H_ | 5 #ifndef CHROME_BROWSER_GTK_LOCATION_BAR_VIEW_GTK_H_ |
6 #define CHROME_BROWSER_GTK_LOCATION_BAR_VIEW_GTK_H_ | 6 #define CHROME_BROWSER_GTK_LOCATION_BAR_VIEW_GTK_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <gtk/gtk.h> | 9 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h" |
10 | 10 // TODO(msw): remove this file once all includes have been updated. |
11 #include <map> | |
12 #include <string> | |
13 | |
14 #include "app/gtk_signal.h" | |
15 #include "base/basictypes.h" | |
16 #include "base/scoped_ptr.h" | |
17 #include "base/scoped_vector.h" | |
18 #include "chrome/browser/autocomplete/autocomplete_edit.h" | |
19 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" | |
20 #include "chrome/browser/extensions/extension_context_menu_model.h" | |
21 #include "chrome/browser/extensions/image_loading_tracker.h" | |
22 #include "chrome/browser/first_run/first_run.h" | |
23 #include "chrome/browser/gtk/info_bubble_gtk.h" | |
24 #include "chrome/browser/gtk/menu_gtk.h" | |
25 #include "chrome/browser/gtk/owned_widget_gtk.h" | |
26 #include "chrome/browser/ui/omnibox/location_bar.h" | |
27 #include "chrome/common/content_settings_types.h" | |
28 #include "chrome/common/notification_observer.h" | |
29 #include "chrome/common/notification_registrar.h" | |
30 #include "chrome/common/page_transition_types.h" | |
31 #include "third_party/skia/include/core/SkBitmap.h" | |
32 #include "webkit/glue/window_open_disposition.h" | |
33 | |
34 class AutocompleteEditViewGtk; | |
35 class Browser; | |
36 class CommandUpdater; | |
37 class ContentSettingImageModel; | |
38 class ContentSettingBubbleGtk; | |
39 class ExtensionAction; | |
40 class GtkThemeProvider; | |
41 class Profile; | |
42 class SkBitmap; | |
43 class TabContents; | |
44 class ToolbarModel; | |
45 | |
46 class LocationBarViewGtk : public AutocompleteEditController, | |
47 public LocationBar, | |
48 public LocationBarTesting, | |
49 public NotificationObserver { | |
50 public: | |
51 explicit LocationBarViewGtk(Browser* browser); | |
52 virtual ~LocationBarViewGtk(); | |
53 | |
54 void Init(bool popup_window_mode); | |
55 | |
56 void SetProfile(Profile* profile); | |
57 | |
58 // Returns the widget the caller should host. You must call Init() first. | |
59 GtkWidget* widget() { return hbox_.get(); } | |
60 | |
61 // Returns the widget the page info bubble should point to. | |
62 GtkWidget* location_icon_widget() const { return location_icon_image_; } | |
63 | |
64 // Returns the widget the extension installed bubble should point to. | |
65 GtkWidget* location_entry_widget() const { return entry_box_; } | |
66 | |
67 // Returns the current TabContents. | |
68 TabContents* GetTabContents() const; | |
69 | |
70 // Sets |preview_enabled| for the PageActionViewGtk associated with this | |
71 // |page_action|. If |preview_enabled| is true, the view will display the | |
72 // page action's icon even though it has not been activated by the extension. | |
73 // This is used by the ExtensionInstalledBubbleGtk to preview what the icon | |
74 // will look like for the user upon installation of the extension. | |
75 void SetPreviewEnabledPageAction(ExtensionAction *page_action, | |
76 bool preview_enabled); | |
77 | |
78 // Retrieves the GtkWidget which is associated with PageActionView | |
79 // corresponding to |page_action|. | |
80 GtkWidget* GetPageActionWidget(ExtensionAction* page_action); | |
81 | |
82 // Updates the location bar. We also reset the bar's permanent text and | |
83 // security style, and, if |tab_for_state_restoring| is non-NULL, also | |
84 // restore saved state that the tab holds. | |
85 void Update(const TabContents* tab_for_state_restoring); | |
86 | |
87 // Show the bookmark bubble. | |
88 void ShowStarBubble(const GURL& url, bool newly_boomkarked); | |
89 | |
90 // Set the starred state of the bookmark star. | |
91 void SetStarred(bool starred); | |
92 | |
93 // Implement the AutocompleteEditController interface. | |
94 virtual void OnAutocompleteWillClosePopup(); | |
95 virtual void OnAutocompleteLosingFocus(gfx::NativeView view_gaining_focus); | |
96 virtual void OnAutocompleteWillAccept(); | |
97 // For this implementation, the parameter is ignored. | |
98 virtual bool OnCommitSuggestedText(const std::wstring& typed_text); | |
99 virtual bool AcceptCurrentInstantPreview(); | |
100 virtual void OnSetSuggestedSearchText(const string16& suggested_text); | |
101 virtual void OnPopupBoundsChanged(const gfx::Rect& bounds); | |
102 virtual void OnAutocompleteAccept(const GURL& url, | |
103 WindowOpenDisposition disposition, | |
104 PageTransition::Type transition, | |
105 const GURL& alternate_nav_url); | |
106 virtual void OnChanged(); | |
107 virtual void OnSelectionBoundsChanged(); | |
108 virtual void OnKillFocus(); | |
109 virtual void OnSetFocus(); | |
110 virtual void OnInputInProgress(bool in_progress); | |
111 virtual SkBitmap GetFavIcon() const; | |
112 virtual std::wstring GetTitle() const; | |
113 | |
114 // Implement the LocationBar interface. | |
115 virtual void ShowFirstRunBubble(FirstRun::BubbleType bubble_type); | |
116 virtual void SetSuggestedText(const string16& text); | |
117 virtual std::wstring GetInputString() const; | |
118 virtual WindowOpenDisposition GetWindowOpenDisposition() const; | |
119 virtual PageTransition::Type GetPageTransition() const; | |
120 virtual void AcceptInput(); | |
121 virtual void FocusLocation(bool select_all); | |
122 virtual void FocusSearch(); | |
123 virtual void UpdateContentSettingsIcons(); | |
124 virtual void UpdatePageActions(); | |
125 virtual void InvalidatePageActions(); | |
126 virtual void SaveStateToContents(TabContents* contents); | |
127 virtual void Revert(); | |
128 virtual const AutocompleteEditView* location_entry() const; | |
129 virtual AutocompleteEditView* location_entry(); | |
130 virtual LocationBarTesting* GetLocationBarForTesting(); | |
131 | |
132 // Implement the LocationBarTesting interface. | |
133 virtual int PageActionCount(); | |
134 virtual int PageActionVisibleCount(); | |
135 virtual ExtensionAction* GetPageAction(size_t index); | |
136 virtual ExtensionAction* GetVisiblePageAction(size_t index); | |
137 virtual void TestPageActionPressed(size_t index); | |
138 | |
139 // Implement the NotificationObserver interface. | |
140 virtual void Observe(NotificationType type, | |
141 const NotificationSource& source, | |
142 const NotificationDetails& details); | |
143 | |
144 // Edit background color. | |
145 static const GdkColor kBackgroundColor; | |
146 | |
147 private: | |
148 class ContentSettingImageViewGtk : public InfoBubbleGtkDelegate { | |
149 public: | |
150 ContentSettingImageViewGtk(ContentSettingsType content_type, | |
151 const LocationBarViewGtk* parent, | |
152 Profile* profile); | |
153 virtual ~ContentSettingImageViewGtk(); | |
154 | |
155 GtkWidget* widget() { return event_box_.get(); } | |
156 | |
157 void set_profile(Profile* profile) { profile_ = profile; } | |
158 | |
159 bool IsVisible() { return GTK_WIDGET_VISIBLE(widget()); } | |
160 void UpdateFromTabContents(TabContents* tab_contents); | |
161 | |
162 private: | |
163 CHROMEGTK_CALLBACK_1(ContentSettingImageViewGtk, gboolean, OnButtonPressed, | |
164 GdkEvent*); | |
165 | |
166 // InfoBubbleDelegate overrides: | |
167 virtual void InfoBubbleClosing(InfoBubbleGtk* info_bubble, | |
168 bool closed_by_escape); | |
169 | |
170 scoped_ptr<ContentSettingImageModel> content_setting_image_model_; | |
171 | |
172 // The widgets for this content settings view. | |
173 OwnedWidgetGtk event_box_; | |
174 OwnedWidgetGtk image_; | |
175 | |
176 // The owning LocationBarViewGtk. | |
177 const LocationBarViewGtk* parent_; | |
178 | |
179 // The currently active profile. | |
180 Profile* profile_; | |
181 | |
182 // The currently shown info bubble if any. | |
183 ContentSettingBubbleGtk* info_bubble_; | |
184 | |
185 DISALLOW_COPY_AND_ASSIGN(ContentSettingImageViewGtk); | |
186 }; | |
187 | |
188 class PageActionViewGtk : public ImageLoadingTracker::Observer, | |
189 public ExtensionContextMenuModel::PopupDelegate { | |
190 public: | |
191 PageActionViewGtk( | |
192 LocationBarViewGtk* owner, Profile* profile, | |
193 ExtensionAction* page_action); | |
194 virtual ~PageActionViewGtk(); | |
195 | |
196 GtkWidget* widget() { return event_box_.get(); } | |
197 | |
198 ExtensionAction* page_action() { return page_action_; } | |
199 | |
200 void set_preview_enabled(bool preview_enabled) { | |
201 preview_enabled_ = preview_enabled; | |
202 } | |
203 | |
204 bool IsVisible() { return GTK_WIDGET_VISIBLE(widget()); } | |
205 | |
206 // Called to notify the PageAction that it should determine whether to be | |
207 // visible or hidden. |contents| is the TabContents that is active, |url| | |
208 // is the current page URL. | |
209 void UpdateVisibility(TabContents* contents, GURL url); | |
210 | |
211 // A callback from ImageLoadingTracker for when the image has loaded. | |
212 virtual void OnImageLoaded( | |
213 SkBitmap* image, ExtensionResource resource, int index); | |
214 | |
215 // Simulate left mouse click on the page action button. | |
216 void TestActivatePageAction(); | |
217 | |
218 // Overridden from ExtensionContextMenuModel::PopupDelegate: | |
219 virtual void InspectPopup(ExtensionAction* action); | |
220 | |
221 private: | |
222 // Show the popup for this page action. If |devtools| is true, show it | |
223 // with a debugger window attached. Returns true if a popup was shown. | |
224 bool ShowPopup(bool devtools); | |
225 | |
226 CHROMEGTK_CALLBACK_1(PageActionViewGtk, gboolean, OnButtonPressed, | |
227 GdkEvent*); | |
228 CHROMEGTK_CALLBACK_1(PageActionViewGtk, gboolean, OnExposeEvent, | |
229 GdkEventExpose*); | |
230 | |
231 // The location bar view that owns us. | |
232 LocationBarViewGtk* owner_; | |
233 | |
234 // The current profile (not owned by us). | |
235 Profile* profile_; | |
236 | |
237 // The PageAction that this view represents. The PageAction is not owned by | |
238 // us, it resides in the extension of this particular profile. | |
239 ExtensionAction* page_action_; | |
240 | |
241 // A cache of all the different icon paths associated with this page action. | |
242 typedef std::map<std::string, GdkPixbuf*> PixbufMap; | |
243 PixbufMap pixbufs_; | |
244 | |
245 // A cache of the last dynamically generated bitmap and the pixbuf that | |
246 // corresponds to it. We keep track of both so we can free old pixbufs as | |
247 // their icons are replaced. | |
248 SkBitmap last_icon_skbitmap_; | |
249 GdkPixbuf* last_icon_pixbuf_; | |
250 | |
251 // The object that is waiting for the image loading to complete | |
252 // asynchronously. | |
253 ImageLoadingTracker tracker_; | |
254 | |
255 // The widgets for this page action. | |
256 OwnedWidgetGtk event_box_; | |
257 OwnedWidgetGtk image_; | |
258 | |
259 // The tab id we are currently showing the icon for. | |
260 int current_tab_id_; | |
261 | |
262 // The URL we are currently showing the icon for. | |
263 GURL current_url_; | |
264 | |
265 // This is used for post-install visual feedback. The page_action icon | |
266 // is briefly shown even if it hasn't been enabled by its extension. | |
267 bool preview_enabled_; | |
268 | |
269 // The context menu view and model for this extension action. | |
270 scoped_ptr<MenuGtk> context_menu_; | |
271 scoped_refptr<ExtensionContextMenuModel> context_menu_model_; | |
272 | |
273 DISALLOW_COPY_AND_ASSIGN(PageActionViewGtk); | |
274 }; | |
275 friend class PageActionViewGtk; | |
276 | |
277 // Creates, initializes, and packs the location icon, EV certificate name, | |
278 // and optional border. | |
279 void BuildSiteTypeArea(); | |
280 | |
281 // Enable or disable the location icon/EV certificate as a drag source for | |
282 // the URL. | |
283 void SetSiteTypeDragSource(); | |
284 | |
285 GtkWidget* site_type_area() { return site_type_alignment_; } | |
286 | |
287 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, HandleExpose, | |
288 GdkEventExpose*); | |
289 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, OnIconReleased, | |
290 GdkEventButton*); | |
291 CHROMEGTK_CALLBACK_4(LocationBarViewGtk, void, OnIconDragData, | |
292 GdkDragContext*, GtkSelectionData*, guint, guint); | |
293 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnIconDragBegin, | |
294 GdkDragContext*); | |
295 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnIconDragEnd, | |
296 GdkDragContext*); | |
297 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnHboxSizeAllocate, | |
298 GtkAllocation*); | |
299 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnEntryBoxSizeAllocate, | |
300 GtkAllocation*); | |
301 CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, OnStarButtonPress, | |
302 GdkEventButton*); | |
303 | |
304 // Updates the site type area: changes the icon and shows/hides the EV | |
305 // certificate information. | |
306 void UpdateSiteTypeArea(); | |
307 | |
308 // Updates the maximum size of the EV certificate label. | |
309 void UpdateEVCertificateLabelSize(); | |
310 | |
311 // Sets the text that should be displayed in the info label and its associated | |
312 // tooltip text. Call with an empty string if the info label should be | |
313 // hidden. | |
314 void SetInfoText(); | |
315 | |
316 // Set the keyword text for the Search BLAH: keyword box. | |
317 void SetKeywordLabel(const std::wstring& keyword); | |
318 | |
319 // Set the keyword text for the "Press tab to search BLAH" hint box. | |
320 void SetKeywordHintLabel(const std::wstring& keyword); | |
321 | |
322 void ShowFirstRunBubbleInternal(FirstRun::BubbleType bubble_type); | |
323 | |
324 // Show or hide |tab_to_search_box_| and |tab_to_search_hint_| according to | |
325 // the value of |show_selected_keyword_|, |show_keyword_hint_|, and the | |
326 // available horizontal space in the location bar. | |
327 void AdjustChildrenVisibility(); | |
328 | |
329 // Build the star icon. | |
330 void CreateStarButton(); | |
331 | |
332 // Update the star icon after it is toggled or the theme changes. | |
333 void UpdateStarIcon(); | |
334 | |
335 // Returns true if we should only show the URL and none of the extras like | |
336 // the star button or page actions. | |
337 bool ShouldOnlyShowLocation(); | |
338 | |
339 // The outermost widget we want to be hosted. | |
340 OwnedWidgetGtk hbox_; | |
341 | |
342 // Star button. | |
343 OwnedWidgetGtk star_; | |
344 GtkWidget* star_image_; | |
345 bool starred_; | |
346 | |
347 // An icon to the left of the address bar. | |
348 GtkWidget* site_type_alignment_; | |
349 GtkWidget* site_type_event_box_; | |
350 GtkWidget* location_icon_image_; | |
351 GtkWidget* drag_icon_; | |
352 bool enable_location_drag_; | |
353 // TODO(pkasting): Split this label off and move the rest of the items to the | |
354 // left of the address bar. | |
355 GtkWidget* security_info_label_; | |
356 | |
357 // Content setting icons. | |
358 OwnedWidgetGtk content_setting_hbox_; | |
359 ScopedVector<ContentSettingImageViewGtk> content_setting_views_; | |
360 | |
361 // Extension page action icons. | |
362 OwnedWidgetGtk page_action_hbox_; | |
363 ScopedVector<PageActionViewGtk> page_action_views_; | |
364 | |
365 // The widget that contains our tab hints and the location bar. | |
366 GtkWidget* entry_box_; | |
367 | |
368 // Area on the left shown when in tab to search mode. | |
369 GtkWidget* tab_to_search_box_; | |
370 GtkWidget* tab_to_search_magnifier_; | |
371 GtkWidget* tab_to_search_full_label_; | |
372 GtkWidget* tab_to_search_partial_label_; | |
373 | |
374 // Hint to user that they can tab-to-search by hitting tab. | |
375 GtkWidget* tab_to_search_hint_; | |
376 GtkWidget* tab_to_search_hint_leading_label_; | |
377 GtkWidget* tab_to_search_hint_icon_; | |
378 GtkWidget* tab_to_search_hint_trailing_label_; | |
379 | |
380 scoped_ptr<AutocompleteEditViewGtk> location_entry_; | |
381 | |
382 // Alignment used to wrap |location_entry_|. | |
383 GtkWidget* location_entry_alignment_; | |
384 | |
385 Profile* profile_; | |
386 CommandUpdater* command_updater_; | |
387 ToolbarModel* toolbar_model_; | |
388 Browser* browser_; | |
389 | |
390 // When we get an OnAutocompleteAccept notification from the autocomplete | |
391 // edit, we save the input string so we can give it back to the browser on | |
392 // the LocationBar interface via GetInputString(). | |
393 std::wstring location_input_; | |
394 | |
395 // The user's desired disposition for how their input should be opened. | |
396 WindowOpenDisposition disposition_; | |
397 | |
398 // The transition type to use for the navigation. | |
399 PageTransition::Type transition_; | |
400 | |
401 // Used to schedule a task for the first run info bubble. | |
402 ScopedRunnableMethodFactory<LocationBarViewGtk> first_run_bubble_; | |
403 | |
404 // When true, the location bar view is read only and also is has a slightly | |
405 // different presentation (font size / color). This is used for popups. | |
406 bool popup_window_mode_; | |
407 | |
408 // Provides colors and rendering mode. | |
409 GtkThemeProvider* theme_provider_; | |
410 | |
411 NotificationRegistrar registrar_; | |
412 | |
413 // Width of the main |hbox_|. Used to properly elide the EV certificate. | |
414 int hbox_width_; | |
415 | |
416 // Width of the hbox that holds |tab_to_search_box_|, |location_entry_| and | |
417 // |tab_to_search_hint_|. | |
418 int entry_box_width_; | |
419 | |
420 // Indicate if |tab_to_search_box_| should be shown. | |
421 bool show_selected_keyword_; | |
422 | |
423 // Indicate if |tab_to_search_hint_| should be shown. | |
424 bool show_keyword_hint_; | |
425 | |
426 // The last search keyword that was shown via the |tab_to_search_box_|. | |
427 std::wstring last_keyword_; | |
428 | |
429 // True if we should update the instant controller when the edit text changes. | |
430 bool update_instant_; | |
431 | |
432 DISALLOW_COPY_AND_ASSIGN(LocationBarViewGtk); | |
433 }; | |
434 | 11 |
435 #endif // CHROME_BROWSER_GTK_LOCATION_BAR_VIEW_GTK_H_ | 12 #endif // CHROME_BROWSER_GTK_LOCATION_BAR_VIEW_GTK_H_ |
OLD | NEW |