Home | History | Annotate | Download | only in bookmarks
      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_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
      6 #define CHROME_BROWSER_UI_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
      7 
      8 #include <vector>
      9 
     10 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
     11 #include "ui/base/models/simple_menu_model.h"
     12 #include "ui/base/window_open_disposition.h"
     13 
     14 class Browser;
     15 class BookmarkModel;
     16 class BookmarkNode;
     17 class MenuGtk;  // See below for why we need this.
     18 class Profile;
     19 
     20 namespace content {
     21 class PageNavigator;
     22 }
     23 
     24 // BookmarkNodeMenuModel builds a SimpleMenuModel on demand when the menu is
     25 // shown, and automatically destroys child models when the menu is closed.
     26 class BookmarkNodeMenuModel : public ui::SimpleMenuModel {
     27  public:
     28   BookmarkNodeMenuModel(ui::SimpleMenuModel::Delegate* delegate,
     29                         BookmarkModel* model,
     30                         const BookmarkNode* node,
     31                         content::PageNavigator* page_navigator,
     32                         Profile* profile);
     33   virtual ~BookmarkNodeMenuModel();
     34 
     35   // From SimpleMenuModel. Takes care of deleting submenus.
     36   // Note that this is not virtual. That's OK for our use.
     37   void Clear();
     38 
     39   // From MenuModel via SimpleMenuModel.
     40   virtual void MenuWillShow() OVERRIDE;
     41   virtual void MenuClosed() OVERRIDE;
     42   virtual void ActivatedAt(int index) OVERRIDE;
     43   virtual void ActivatedAt(int index, int event_flags) OVERRIDE;
     44 
     45  protected:
     46   // Adds all bookmark items to the model. Does not clear the model first.
     47   void PopulateMenu();
     48 
     49   // Add a submenu for the given bookmark folder node.
     50   void AddSubMenuForNode(const BookmarkNode* node);
     51 
     52   BookmarkModel* model() const { return model_; }
     53   void set_model(BookmarkModel* model) { model_ = model; }
     54 
     55   const BookmarkNode* node() const { return node_; }
     56   void set_node(const BookmarkNode* node) { node_ = node; }
     57 
     58  private:
     59   // Uses the page navigator to open the bookmark at the given index.
     60   void NavigateToMenuItem(int index, WindowOpenDisposition disposition);
     61 
     62   // The bookmark model whose bookmarks we will show. Note that in the top-level
     63   // bookmark menu, this may be null. (It is set only when the menu is shown.)
     64   BookmarkModel* model_;
     65 
     66   // The bookmark node for the folder that this model will show. Note that in
     67   // the top-level bookmark menu, this may be null, as above.
     68   const BookmarkNode* node_;
     69 
     70   // The page navigator used to open bookmarks in ActivatedAt().
     71   content::PageNavigator* page_navigator_;
     72 
     73   Profile* profile_;
     74 
     75   // A list of the submenus we own and will need to delete.
     76   std::vector<BookmarkNodeMenuModel*> submenus_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(BookmarkNodeMenuModel);
     79 };
     80 
     81 // This is the top-level bookmark menu model. It handles prepending a few fixed
     82 // items before the bookmarks. Child menus are all plain BookmarkNodeMenuModels.
     83 // This class also handles watching the bookmark model and forcing the menu to
     84 // close if it changes while the menu is open.
     85 class BookmarkSubMenuModel : public BookmarkNodeMenuModel,
     86                              public BaseBookmarkModelObserver {
     87  public:
     88   BookmarkSubMenuModel(ui::SimpleMenuModel::Delegate* delegate,
     89                        Browser* browser);
     90 
     91   virtual ~BookmarkSubMenuModel();
     92 
     93   // See below; this is used to allow closing the menu when bookmarks change.
     94   void SetMenuGtk(MenuGtk* menu) { menu_ = menu; }
     95 
     96   // From BookmarkModelObserver, BaseBookmarkModelObserver.
     97   virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
     98   virtual void BookmarkModelChanged() OVERRIDE;
     99   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
    100 
    101   // From MenuModel via BookmarkNodeMenuModel, SimpleMenuModel.
    102   virtual void MenuWillShow() OVERRIDE;
    103   virtual void MenuClosed() OVERRIDE;
    104   virtual void ActivatedAt(int index) OVERRIDE;
    105   virtual void ActivatedAt(int index, int event_flags) OVERRIDE;
    106   virtual bool IsEnabledAt(int index) const OVERRIDE;
    107   virtual bool IsVisibleAt(int index) const OVERRIDE;
    108 
    109   // Returns true if the command id is for a bookmark item.
    110   static bool IsBookmarkItemCommandId(int command_id);
    111 
    112  private:
    113   Browser* browser_;
    114 
    115   // The number of fixed items shown before the bookmarks.
    116   int fixed_items_;
    117   // The index of the first non-bookmark item after the bookmarks.
    118   int bookmark_end_;
    119 
    120   // We need to be able to call Cancel() on the wrench menu when bookmarks
    121   // change. This is a bit of an abstraction violation but it could be converted
    122   // to an interface with just a Cancel() method if necessary.
    123   MenuGtk* menu_;
    124 
    125   // We keep track of whether the bookmark submenu is currently showing. If it's
    126   // not showing, then we don't need to forcibly close the entire wrench menu
    127   // when the bookmark model loads.
    128   bool menu_showing_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(BookmarkSubMenuModel);
    131 };
    132 
    133 #endif  // CHROME_BROWSER_UI_GTK_BOOKMARKS_BOOKMARK_SUB_MENU_MODEL_GTK_H_
    134