b13acaf037a7b5b3078e6bdf845036318972ac0f
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / bookmarks / bookmark_bar_view.h
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.
4
5 #ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_
7
8 #include <set>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "chrome/browser/bookmarks/bookmark_stats.h"
17 #include "chrome/browser/ui/bookmarks/bookmark_bar.h"
18 #include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h"
19 #include "chrome/browser/ui/views/bookmarks/bookmark_bubble_view_observer.h"
20 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_observer.h"
21 #include "chrome/browser/ui/views/detachable_toolbar_view.h"
22 #include "components/bookmarks/core/browser/bookmark_model_observer.h"
23 #include "components/bookmarks/core/browser/bookmark_node_data.h"
24 #include "ui/gfx/animation/animation_delegate.h"
25 #include "ui/views/context_menu_controller.h"
26 #include "ui/views/controls/button/button.h"
27 #include "ui/views/controls/button/menu_button_listener.h"
28 #include "ui/views/controls/menu/menu_types.h"
29 #include "ui/views/drag_controller.h"
30
31 class BookmarkContextMenu;
32 class Browser;
33 class BrowserView;
34 class Profile;
35
36 namespace content {
37 class PageNavigator;
38 }
39
40 namespace gfx {
41 class SlideAnimation;
42 }
43
44 namespace views {
45 class CustomButton;
46 class MenuButton;
47 class MenuItemView;
48 class TextButton;
49 }
50
51 // BookmarkBarView renders the BookmarkModel.  Each starred entry on the
52 // BookmarkBar is rendered as a MenuButton. An additional MenuButton aligned to
53 // the right allows the user to quickly see recently starred entries.
54 //
55 // BookmarkBarView shows the bookmarks from a specific Profile. BookmarkBarView
56 // waits until the HistoryService for the profile has been loaded before
57 // creating the BookmarkModel.
58 class BookmarkBarView : public DetachableToolbarView,
59                         public BookmarkModelObserver,
60                         public views::MenuButtonListener,
61                         public views::ButtonListener,
62                         public views::ContextMenuController,
63                         public views::DragController,
64                         public gfx::AnimationDelegate,
65                         public BookmarkMenuControllerObserver,
66                         public BookmarkBarInstructionsDelegate,
67                         public BookmarkBubbleViewObserver {
68  public:
69   // The internal view class name.
70   static const char kViewClassName[];
71
72   // Constant used in Browser View, as well as here.
73   // How inset the bookmarks bar is when displayed on the new tab page.
74   static const int kNewtabHorizontalPadding;
75
76   // Maximum size of buttons on the bookmark bar.
77   static const int kMaxButtonWidth;
78
79   // Number of pixels the attached bookmark bar overlaps with the toolbar.
80   static const int kToolbarAttachedBookmarkBarOverlap;
81
82   // |browser_view| can be NULL during tests.
83   BookmarkBarView(Browser* browser, BrowserView* browser_view);
84   virtual ~BookmarkBarView();
85
86   static void DisableAnimationsForTesting(bool disabled);
87
88   // Returns the current browser.
89   Browser* browser() const { return browser_; }
90
91   // Sets the PageNavigator that is used when the user selects an entry on
92   // the bookmark bar.
93   void SetPageNavigator(content::PageNavigator* navigator);
94
95   // Sets whether the containing browser is showing an infobar.  This affects
96   // layout during animation.
97   void set_infobar_visible(bool infobar_visible) {
98     infobar_visible_ = infobar_visible;
99   }
100
101   // Changes the state of the bookmark bar.
102   void SetBookmarkBarState(BookmarkBar::State state,
103                            BookmarkBar::AnimateChangeType animate_type);
104
105   // Returns the toolbar overlap when fully detached.
106   int GetFullyDetachedToolbarOverlap() const;
107
108   // Whether or not we are animating.
109   bool is_animating();
110
111   // If |loc| is over a bookmark button the node is returned corresponding to
112   // the button and |model_start_index| is set to 0. If a overflow button is
113   // showing and |loc| is over the overflow button, the bookmark bar node is
114   // returned and |model_start_index| is set to the index of the first node
115   // contained in the overflow menu.
116   const BookmarkNode* GetNodeForButtonAtModelIndex(const gfx::Point& loc,
117                                                    int* model_start_index);
118
119   // Returns the MenuButton for node.
120   views::MenuButton* GetMenuButtonForNode(const BookmarkNode* node);
121
122   // Returns the position to anchor the menu for |button| at.
123   void GetAnchorPositionForButton(views::MenuButton* button,
124                                   views::MenuAnchorPosition* anchor);
125
126   // Returns the button responsible for showing bookmarks in the other bookmark
127   // folder.
128   views::MenuButton* other_bookmarked_button() const {
129     return other_bookmarked_button_;
130   }
131
132   // Returns the button used when not all the items on the bookmark bar fit.
133   views::MenuButton* overflow_button() const { return overflow_button_; }
134
135   // Returns the active MenuItemView, or NULL if a menu isn't showing.
136   views::MenuItemView* GetMenu();
137
138   // Returns the context menu, or null if one isn't showing.
139   views::MenuItemView* GetContextMenu();
140
141   // Returns the drop MenuItemView, or NULL if a menu isn't showing.
142   views::MenuItemView* GetDropMenu();
143
144   // If a button is currently throbbing, it is stopped. If immediate is true
145   // the throb stops immediately, otherwise it stops after a couple more
146   // throbs.
147   void StopThrobbing(bool immediate);
148
149   // Returns the tooltip text for the specified url and title. The returned
150   // text is clipped to fit within the bounds of the monitor. |context| is
151   // used to determine which gfx::Screen is used to retrieve bounds.
152   //
153   // Note that we adjust the direction of both the URL and the title based on
154   // the locale so that pure LTR strings are displayed properly in RTL locales.
155   static base::string16 CreateToolTipForURLAndTitle(const views::Widget* widget,
156                                               const gfx::Point& screen_loc,
157                                               const GURL& url,
158                                               const base::string16& title,
159                                               Profile* profile);
160
161   // DetachableToolbarView methods:
162   virtual bool IsDetached() const OVERRIDE;
163   virtual double GetAnimationValue() const OVERRIDE;
164   virtual int GetToolbarOverlap() const OVERRIDE;
165
166   // View methods:
167   virtual gfx::Size GetPreferredSize() OVERRIDE;
168   virtual gfx::Size GetMinimumSize() OVERRIDE;
169   virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
170   virtual void Layout() OVERRIDE;
171   virtual void ViewHierarchyChanged(
172       const ViewHierarchyChangedDetails& details) OVERRIDE;
173   virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
174   virtual bool GetDropFormats(
175       int* formats,
176       std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
177   virtual bool AreDropTypesRequired() OVERRIDE;
178   virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
179   virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE;
180   virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
181   virtual void OnDragExited() OVERRIDE;
182   virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
183   virtual void OnThemeChanged() OVERRIDE;
184   virtual const char* GetClassName() const OVERRIDE;
185
186   // AccessiblePaneView:
187   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
188
189   // gfx::AnimationDelegate:
190   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
191   virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
192
193   // BookmarkMenuControllerObserver:
194   virtual void BookmarkMenuControllerDeleted(
195       BookmarkMenuController* controller) OVERRIDE;
196
197   // BookmarkBarInstructionsDelegate:
198   virtual void ShowImportDialog() OVERRIDE;
199
200   // BookmarkBubbleViewObserver:
201   virtual void OnBookmarkBubbleShown(const GURL& url) OVERRIDE;
202   virtual void OnBookmarkBubbleHidden() OVERRIDE;
203
204   // BookmarkModelObserver:
205   virtual void BookmarkModelLoaded(BookmarkModel* model,
206                                    bool ids_reassigned) OVERRIDE;
207   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
208   virtual void BookmarkNodeMoved(BookmarkModel* model,
209                                  const BookmarkNode* old_parent,
210                                  int old_index,
211                                  const BookmarkNode* new_parent,
212                                  int new_index) OVERRIDE;
213   virtual void BookmarkNodeAdded(BookmarkModel* model,
214                                  const BookmarkNode* parent,
215                                  int index) OVERRIDE;
216   virtual void BookmarkNodeRemoved(BookmarkModel* model,
217                                    const BookmarkNode* parent,
218                                    int old_index,
219                                    const BookmarkNode* node,
220                                    const std::set<GURL>& removed_urls) OVERRIDE;
221   virtual void BookmarkAllNodesRemoved(
222       BookmarkModel* model,
223       const std::set<GURL>& removed_urls) OVERRIDE;
224   virtual void BookmarkNodeChanged(BookmarkModel* model,
225                                    const BookmarkNode* node) OVERRIDE;
226   virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
227                                              const BookmarkNode* node) OVERRIDE;
228   virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
229                                           const BookmarkNode* node) OVERRIDE;
230
231   // views::DragController:
232   virtual void WriteDragDataForView(views::View* sender,
233                                     const gfx::Point& press_pt,
234                                     ui::OSExchangeData* data) OVERRIDE;
235   virtual int GetDragOperationsForView(views::View* sender,
236                                        const gfx::Point& p) OVERRIDE;
237   virtual bool CanStartDragForView(views::View* sender,
238                                    const gfx::Point& press_pt,
239                                    const gfx::Point& p) OVERRIDE;
240
241   // views::MenuButtonListener:
242   virtual void OnMenuButtonClicked(views::View* view,
243                                    const gfx::Point& point) OVERRIDE;
244
245   // views::ButtonListener:
246   virtual void ButtonPressed(views::Button* sender,
247                              const ui::Event& event) OVERRIDE;
248
249   // views::ContextMenuController:
250   virtual void ShowContextMenuForView(views::View* source,
251                                       const gfx::Point& point,
252                                       ui::MenuSourceType source_type) OVERRIDE;
253
254  private:
255   class ButtonSeparatorView;
256   struct DropInfo;
257   struct DropLocation;
258
259   friend class BookmarkBarViewEventTestBase;
260   FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, SwitchProfile);
261   FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest,
262                            NoAppsShortcutWithoutInstantExtended);
263   FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest,
264                            AppsShortcutVisibility);
265
266   // Used to identify what the user is dropping onto.
267   enum DropButtonType {
268     DROP_BOOKMARK,
269     DROP_OTHER_FOLDER,
270     DROP_OVERFLOW
271   };
272
273   // Creates recent bookmark button and when visible button as well as
274   // calculating the preferred height.
275   void Init();
276
277   // NOTE: unless otherwise stated all methods that take an int for an index are
278   // in terms of the bookmark bar view. Typically the view index and model index
279   // are the same, but they may differ during animations or drag and drop.
280   //
281   // It's easy to get the mapping wrong. For this reason all these methods are
282   // private.
283
284   // Returns the number of buttons corresponding to starred urls/folders. This
285   // is equivalent to the number of children the bookmark bar node from the
286   // bookmark bar model has.
287   int GetBookmarkButtonCount();
288
289   // Returns the button at the specified index.
290   views::TextButton* GetBookmarkButton(int index);
291
292   // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
293   // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
294   BookmarkLaunchLocation GetBookmarkLaunchLocation() const;
295
296   // Returns the index of the first hidden bookmark button. If all buttons are
297   // visible, this returns GetBookmarkButtonCount().
298   int GetFirstHiddenNodeIndex();
299
300   // Creates the button showing the other bookmarked items.
301   views::MenuButton* CreateOtherBookmarkedButton();
302
303   // Creates the button used when not all bookmark buttons fit.
304   views::MenuButton* CreateOverflowButton();
305
306   // Creates the button for rendering the specified bookmark node.
307   views::View* CreateBookmarkButton(const BookmarkNode* node);
308
309   // Creates the button for rendering the apps page shortcut.
310   views::TextButton* CreateAppsPageShortcutButton();
311
312   // Configures the button from the specified node. This sets the text,
313   // and icon.
314   void ConfigureButton(const BookmarkNode* node, views::TextButton* button);
315
316   // Implementation for BookmarkNodeAddedImpl.
317   void BookmarkNodeAddedImpl(BookmarkModel* model,
318                              const BookmarkNode* parent,
319                              int index);
320
321   // Implementation for BookmarkNodeRemoved.
322   void BookmarkNodeRemovedImpl(BookmarkModel* model,
323                                const BookmarkNode* parent,
324                                int index);
325
326   // If the node is a child of the root node, the button is updated
327   // appropriately.
328   void BookmarkNodeChangedImpl(BookmarkModel* model, const BookmarkNode* node);
329
330   // Shows the menu used during drag and drop for the specified node.
331   void ShowDropFolderForNode(const BookmarkNode* node);
332
333   // Cancels the timer used to show a drop menu.
334   void StopShowFolderDropMenuTimer();
335
336   // Stars the timer used to show a drop menu for node.
337   void StartShowFolderDropMenuTimer(const BookmarkNode* node);
338
339   // Calculates the location for the drop in |location|.
340   void CalculateDropLocation(const ui::DropTargetEvent& event,
341                              const BookmarkNodeData& data,
342                              DropLocation* location);
343
344   // Writes a BookmarkNodeData for node to data.
345   void WriteBookmarkDragData(const BookmarkNode* node,
346                              ui::OSExchangeData* data);
347
348   // This determines which view should throb and starts it
349   // throbbing (e.g when the bookmark bubble is showing).
350   // If |overflow_only| is true, start throbbing only if |node| is hidden in
351   // the overflow menu.
352   void StartThrobbing(const BookmarkNode* node, bool overflow_only);
353
354   // Returns the view to throb when a node is removed. |parent| is the parent of
355   // the node that was removed, and |old_index| the index of the node that was
356   // removed.
357   views::CustomButton* DetermineViewToThrobFromRemove(
358       const BookmarkNode* parent,
359       int old_index);
360
361   // Updates the colors for all the child objects in the bookmarks bar.
362   void UpdateColors();
363
364   // Updates the visibility of |other_bookmarked_button_|. Also shows or hide
365   // the separator if required.
366   void UpdateOtherBookmarksVisibility();
367
368   // Updates the visibility of |bookmarks_separator_view_|.
369   void UpdateBookmarksSeparatorVisibility();
370
371   // This method computes the bounds for the bookmark bar items. If
372   // |compute_bounds_only| = TRUE, the bounds for the items are just computed,
373   // but are not set. This mode is used by GetPreferredSize() to obtain the
374   // desired bounds. If |compute_bounds_only| = FALSE, the bounds are set.
375   gfx::Size LayoutItems(bool compute_bounds_only);
376
377   // Updates the visibility of the apps shortcut based on the pref value.
378   void OnAppsPageShortcutVisibilityPrefChanged();
379
380   // Needed to react to kShowAppsShortcutInBookmarkBar changes.
381   PrefChangeRegistrar profile_pref_registrar_;
382
383   // Used for opening urls.
384   content::PageNavigator* page_navigator_;
385
386   // Model providing details as to the starred entries/folders that should be
387   // shown. This is owned by the Profile.
388   BookmarkModel* model_;
389
390   // Used to manage showing a Menu, either for the most recently bookmarked
391   // entries, or for the starred folder.
392   BookmarkMenuController* bookmark_menu_;
393
394   // Used when showing a menu for drag and drop. That is, if the user drags
395   // over a folder this becomes non-null and manages the menu showing the
396   // contents of the node.
397   BookmarkMenuController* bookmark_drop_menu_;
398
399   // If non-NULL we're showing a context menu for one of the items on the
400   // bookmark bar.
401   scoped_ptr<BookmarkContextMenu> context_menu_;
402
403   // Shows the other bookmark entries.
404   views::MenuButton* other_bookmarked_button_;
405
406   // Shows the Apps page shortcut.
407   views::TextButton* apps_page_shortcut_;
408
409   // Task used to delay showing of the drop menu.
410   base::WeakPtrFactory<BookmarkBarView> show_folder_method_factory_;
411
412   // Used to track drops on the bookmark bar view.
413   scoped_ptr<DropInfo> drop_info_;
414
415   // Visible if not all the bookmark buttons fit.
416   views::MenuButton* overflow_button_;
417
418   // Shows a text and a link to import bookmarks if there are no bookmarks in
419   // the Bookmarks Bar.
420   views::View* instructions_;
421
422   ButtonSeparatorView* bookmarks_separator_view_;
423
424   Browser* browser_;
425   BrowserView* browser_view_;
426
427   // True if the owning browser is showing an infobar.
428   bool infobar_visible_;
429
430   // Animation controlling showing and hiding of the bar.
431   scoped_ptr<gfx::SlideAnimation> size_animation_;
432
433   // If the bookmark bubble is showing, this is the visible ancestor of the URL.
434   // The visible ancestor is either the other_bookmarked_button_,
435   // overflow_button_ or a button on the bar.
436   views::CustomButton* throbbing_view_;
437
438   BookmarkBar::State bookmark_bar_state_;
439
440   // Are we animating to or from the detached state?
441   bool animating_detached_;
442
443   DISALLOW_COPY_AND_ASSIGN(BookmarkBarView);
444 };
445
446 #endif  // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_