OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/views/tabs/browser_tab_strip_controller.h" | 5 #include "chrome/browser/views/tabs/browser_tab_strip_controller.h" |
6 | 6 |
7 #include "chrome/browser/tab_contents/tab_contents.h" | 7 #include "chrome/browser/tab_contents/tab_contents.h" |
| 8 #include "chrome/browser/tab_menu_model.h" |
8 #include "chrome/browser/views/tabs/side_tab_strip.h" | 9 #include "chrome/browser/views/tabs/side_tab_strip.h" |
| 10 #include "views/controls/menu/menu_2.h" |
| 11 #include "views/widget/widget.h" |
| 12 |
| 13 class BrowserTabStripController::TabContextMenuContents |
| 14 : public menus::SimpleMenuModel::Delegate { |
| 15 public: |
| 16 TabContextMenuContents(int tab_index, BrowserTabStripController* controller) |
| 17 : ALLOW_THIS_IN_INITIALIZER_LIST(model_(this)), |
| 18 tab_index_(tab_index), |
| 19 controller_(controller) { |
| 20 Build(); |
| 21 } |
| 22 virtual ~TabContextMenuContents() { |
| 23 menu_->CancelMenu(); |
| 24 } |
| 25 |
| 26 void RunMenuAt(const gfx::Point& point) { |
| 27 menu_->RunMenuAt(point, views::Menu2::ALIGN_TOPLEFT); |
| 28 } |
| 29 |
| 30 // Overridden from menus::SimpleMenuModel::Delegate: |
| 31 virtual bool IsCommandIdChecked(int command_id) const { |
| 32 return controller_->IsCommandCheckedForTab( |
| 33 static_cast<TabStripModel::ContextMenuCommand>(command_id), |
| 34 tab_index_); |
| 35 } |
| 36 virtual bool IsCommandIdEnabled(int command_id) const { |
| 37 return controller_->IsCommandEnabledForTab( |
| 38 static_cast<TabStripModel::ContextMenuCommand>(command_id), |
| 39 tab_index_); |
| 40 } |
| 41 virtual bool GetAcceleratorForCommandId( |
| 42 int command_id, |
| 43 menus::Accelerator* accelerator) { |
| 44 return controller_->tabstrip_->GetWidget()->GetAccelerator(command_id, |
| 45 accelerator); |
| 46 } |
| 47 virtual void ExecuteCommand(int command_id) { |
| 48 controller_->ExecuteCommandForTab( |
| 49 static_cast<TabStripModel::ContextMenuCommand>(command_id), |
| 50 tab_index_); |
| 51 } |
| 52 |
| 53 private: |
| 54 void Build() { |
| 55 menu_.reset(new views::Menu2(&model_)); |
| 56 } |
| 57 |
| 58 TabMenuModel model_; |
| 59 scoped_ptr<views::Menu2> menu_; |
| 60 |
| 61 // The index of the tab we are showing the context menu for. |
| 62 int tab_index_; |
| 63 |
| 64 // A pointer back to our hosting controller, for command state information. |
| 65 BrowserTabStripController* controller_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(TabContextMenuContents); |
| 68 }; |
| 69 |
9 | 70 |
10 //////////////////////////////////////////////////////////////////////////////// | 71 //////////////////////////////////////////////////////////////////////////////// |
11 // BrowserTabStripController, public: | 72 // BrowserTabStripController, public: |
12 | 73 |
13 BrowserTabStripController::BrowserTabStripController(TabStripModel* model, | 74 BrowserTabStripController::BrowserTabStripController(TabStripModel* model, |
14 SideTabStrip* tabstrip) | 75 SideTabStrip* tabstrip) |
15 : model_(model), | 76 : model_(model), |
16 tabstrip_(tabstrip) { | 77 tabstrip_(tabstrip) { |
17 model_->AddObserver(this); | 78 model_->AddObserver(this); |
18 } | 79 } |
19 | 80 |
20 BrowserTabStripController::~BrowserTabStripController() { | 81 BrowserTabStripController::~BrowserTabStripController() { |
| 82 model_->RemoveObserver(this); |
| 83 } |
| 84 |
| 85 void BrowserTabStripController::InitFromModel() { |
| 86 // Walk the model, calling our insertion observer method for each item within |
| 87 // it. |
| 88 for (int i = 0; i < model_->count(); ++i) { |
| 89 TabInsertedAt(model_->GetTabContentsAt(i), i, |
| 90 i == model_->selected_index()); |
| 91 } |
| 92 } |
| 93 |
| 94 bool BrowserTabStripController::IsCommandEnabledForTab( |
| 95 TabStripModel::ContextMenuCommand command_id, int tab_index) const { |
| 96 if (model_->ContainsIndex(tab_index)) |
| 97 return model_->IsContextMenuCommandEnabled(tab_index, command_id); |
| 98 return false; |
| 99 } |
| 100 |
| 101 bool BrowserTabStripController::IsCommandCheckedForTab( |
| 102 TabStripModel::ContextMenuCommand command_id, int tab_index) const { |
| 103 // TODO(beng): move to TabStripModel, see note in IsTabPinned. |
| 104 if (command_id == TabStripModel::CommandTogglePinned) |
| 105 return false; |
| 106 |
| 107 if (model_->ContainsIndex(tab_index)) |
| 108 return model_->IsContextMenuCommandChecked(tab_index, command_id); |
| 109 return false; |
| 110 } |
| 111 |
| 112 void BrowserTabStripController::ExecuteCommandForTab( |
| 113 TabStripModel::ContextMenuCommand command_id, int tab_index) { |
| 114 if (model_->ContainsIndex(tab_index)) |
| 115 model_->ExecuteContextMenuCommand(tab_index, command_id); |
21 } | 116 } |
22 | 117 |
23 //////////////////////////////////////////////////////////////////////////////// | 118 //////////////////////////////////////////////////////////////////////////////// |
24 // BrowserTabStripController, SideTabStripModel implementation: | 119 // BrowserTabStripController, SideTabStripModel implementation: |
25 | 120 |
26 SkBitmap BrowserTabStripController::GetIcon(int index) const { | 121 SkBitmap BrowserTabStripController::GetIcon(int index) const { |
27 return model_->GetTabContentsAt(index)->GetFavIcon(); | 122 return model_->GetTabContentsAt(index)->GetFavIcon(); |
28 } | 123 } |
29 | 124 |
30 string16 BrowserTabStripController::GetTitle(int index) const { | 125 string16 BrowserTabStripController::GetTitle(int index) const { |
(...skipping 15 matching lines...) Expand all Loading... |
46 } | 141 } |
47 | 142 |
48 void BrowserTabStripController::SelectTab(int index) { | 143 void BrowserTabStripController::SelectTab(int index) { |
49 model_->SelectTabContentsAt(index, true); | 144 model_->SelectTabContentsAt(index, true); |
50 } | 145 } |
51 | 146 |
52 void BrowserTabStripController::CloseTab(int index) { | 147 void BrowserTabStripController::CloseTab(int index) { |
53 model_->CloseTabContentsAt(index); | 148 model_->CloseTabContentsAt(index); |
54 } | 149 } |
55 | 150 |
| 151 void BrowserTabStripController::ShowContextMenu(int index, |
| 152 const gfx::Point& p) { |
| 153 if (!context_menu_contents_.get()) |
| 154 context_menu_contents_.reset(new TabContextMenuContents(index, this)); |
| 155 context_menu_contents_->RunMenuAt(p); |
| 156 } |
| 157 |
56 //////////////////////////////////////////////////////////////////////////////// | 158 //////////////////////////////////////////////////////////////////////////////// |
57 // BrowserTabStripController, TabStripModelObserver implementation: | 159 // BrowserTabStripController, TabStripModelObserver implementation: |
58 | 160 |
59 void BrowserTabStripController::TabInsertedAt(TabContents* contents, int index, | 161 void BrowserTabStripController::TabInsertedAt(TabContents* contents, int index, |
60 bool foreground) { | 162 bool foreground) { |
61 tabstrip_->AddTabAt(index); | 163 tabstrip_->AddTabAt(index); |
62 } | 164 } |
63 | 165 |
64 void BrowserTabStripController::TabDetachedAt(TabContents* contents, | 166 void BrowserTabStripController::TabDetachedAt(TabContents* contents, |
65 int index) { | 167 int index) { |
(...skipping 21 matching lines...) Expand all Loading... |
87 } | 189 } |
88 | 190 |
89 void BrowserTabStripController::TabPinnedStateChanged(TabContents* contents, | 191 void BrowserTabStripController::TabPinnedStateChanged(TabContents* contents, |
90 int index) { | 192 int index) { |
91 } | 193 } |
92 | 194 |
93 void BrowserTabStripController::TabBlockedStateChanged(TabContents* contents, | 195 void BrowserTabStripController::TabBlockedStateChanged(TabContents* contents, |
94 int index) { | 196 int index) { |
95 } | 197 } |
96 | 198 |
OLD | NEW |