Home | History | Annotate | Download | only in toolbar
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_
      6 #define CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_
      7 #pragma once
      8 
      9 #include "base/memory/scoped_ptr.h"
     10 #include "chrome/browser/tabs/tab_strip_model_observer.h"
     11 #include "content/common/notification_observer.h"
     12 #include "content/common/notification_registrar.h"
     13 #include "ui/base/models/accelerator.h"
     14 #include "ui/base/models/button_menu_item_model.h"
     15 #include "ui/base/models/simple_menu_model.h"
     16 
     17 class Browser;
     18 class TabStripModel;
     19 
     20 namespace {
     21 class MockWrenchMenuModel;
     22 }  // namespace
     23 
     24 // A menu model that builds the contents of an encoding menu.
     25 class EncodingMenuModel : public ui::SimpleMenuModel,
     26                           public ui::SimpleMenuModel::Delegate {
     27  public:
     28   explicit EncodingMenuModel(Browser* browser);
     29   virtual ~EncodingMenuModel();
     30 
     31   // Overridden from ui::SimpleMenuModel::Delegate:
     32   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
     33   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
     34   virtual bool GetAcceleratorForCommandId(
     35       int command_id,
     36       ui::Accelerator* accelerator) OVERRIDE;
     37   virtual void ExecuteCommand(int command_id) OVERRIDE;
     38 
     39  private:
     40   void Build();
     41 
     42   Browser* browser_;  // weak
     43 
     44   DISALLOW_COPY_AND_ASSIGN(EncodingMenuModel);
     45 };
     46 
     47 // A menu model that builds the contents of the zoom menu.
     48 class ZoomMenuModel : public ui::SimpleMenuModel {
     49  public:
     50   explicit ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate);
     51   virtual ~ZoomMenuModel();
     52 
     53  private:
     54   void Build();
     55 
     56   DISALLOW_COPY_AND_ASSIGN(ZoomMenuModel);
     57 };
     58 
     59 class ToolsMenuModel : public ui::SimpleMenuModel {
     60  public:
     61   ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate, Browser* browser);
     62   virtual ~ToolsMenuModel();
     63 
     64  private:
     65   void Build(Browser* browser);
     66 
     67   scoped_ptr<EncodingMenuModel> encoding_menu_model_;
     68 
     69   DISALLOW_COPY_AND_ASSIGN(ToolsMenuModel);
     70 };
     71 
     72 // A menu model that builds the contents of the wrench menu.
     73 class WrenchMenuModel : public ui::SimpleMenuModel,
     74                         public ui::SimpleMenuModel::Delegate,
     75                         public ui::ButtonMenuItemModel::Delegate,
     76                         public TabStripModelObserver,
     77                         public NotificationObserver {
     78  public:
     79   WrenchMenuModel(ui::AcceleratorProvider* provider, Browser* browser);
     80   virtual ~WrenchMenuModel();
     81 
     82   // Overridden for ButtonMenuItemModel::Delegate:
     83   virtual bool DoesCommandIdDismissMenu(int command_id) const OVERRIDE;
     84 
     85   // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel:
     86   virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
     87   virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE;
     88   virtual bool GetIconForCommandId(int command_id,
     89                                    SkBitmap* icon) const OVERRIDE;
     90   virtual void ExecuteCommand(int command_id) OVERRIDE;
     91   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
     92   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
     93   virtual bool IsCommandIdVisible(int command_id) const OVERRIDE;
     94   virtual bool GetAcceleratorForCommandId(
     95       int command_id,
     96       ui::Accelerator* accelerator) OVERRIDE;
     97 
     98   // Overridden from TabStripModelObserver:
     99   virtual void TabSelectedAt(TabContentsWrapper* old_contents,
    100                              TabContentsWrapper* new_contents,
    101                              int index,
    102                              bool user_gesture) OVERRIDE;
    103   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
    104                              TabContentsWrapper* old_contents,
    105                              TabContentsWrapper* new_contents,
    106                              int index) OVERRIDE;
    107   virtual void TabStripModelDeleted() OVERRIDE;
    108 
    109   // Overridden from NotificationObserver:
    110   virtual void Observe(NotificationType type,
    111                        const NotificationSource& source,
    112                        const NotificationDetails& details) OVERRIDE;
    113 
    114   // Getters.
    115   Browser* browser() const { return browser_; }
    116 
    117   // Calculates |zoom_label_| in response to a zoom change.
    118   void UpdateZoomControls();
    119 
    120  private:
    121   // Testing constructor used for mocking.
    122   friend class ::MockWrenchMenuModel;
    123   WrenchMenuModel();
    124 
    125   void Build();
    126 
    127   // Adds custom items to the menu. Deprecated in favor of a cross platform
    128   // model for button items.
    129   void CreateCutCopyPaste();
    130   void CreateZoomFullscreen();
    131 
    132   string16 GetSyncMenuLabel() const;
    133 
    134   // Models for the special menu items with buttons.
    135   scoped_ptr<ui::ButtonMenuItemModel> edit_menu_item_model_;
    136   scoped_ptr<ui::ButtonMenuItemModel> zoom_menu_item_model_;
    137 
    138   // Label of the zoom label in the zoom menu item.
    139   string16 zoom_label_;
    140 
    141   // Tools menu.
    142   scoped_ptr<ToolsMenuModel> tools_menu_model_;
    143 
    144   ui::AcceleratorProvider* provider_;  // weak
    145 
    146   Browser* browser_;  // weak
    147   TabStripModel* tabstrip_model_; // weak
    148 
    149   NotificationRegistrar registrar_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel);
    152 };
    153 
    154 #endif  // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_
    155