Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/wrench_menu_model.cc

Issue 2800015: GTK: First draft of the unified cut/copy/paste and +/-/Fullscreen menu items. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: estade cleanups Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/wrench_menu_model.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/wrench_menu_model.h" 5 #include "chrome/browser/wrench_menu_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath>
8 9
9 #include "app/l10n_util.h" 10 #include "app/l10n_util.h"
11 #include "app/menus/button_menu_item_model.h"
10 #include "app/resource_bundle.h" 12 #include "app/resource_bundle.h"
11 #include "base/command_line.h" 13 #include "base/command_line.h"
12 #include "chrome/app/chrome_dll_resource.h" 14 #include "chrome/app/chrome_dll_resource.h"
13 #include "chrome/browser/browser.h" 15 #include "chrome/browser/browser.h"
16 #include "chrome/browser/host_zoom_map.h"
14 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/defaults.h" 18 #include "chrome/browser/defaults.h"
16 #include "chrome/browser/page_menu_model.h" 19 #include "chrome/browser/page_menu_model.h"
17 #include "chrome/browser/profile.h" 20 #include "chrome/browser/profile.h"
18 #include "chrome/browser/sync/profile_sync_service.h" 21 #include "chrome/browser/sync/profile_sync_service.h"
19 #include "chrome/browser/sync/sync_ui_util.h" 22 #include "chrome/browser/sync/sync_ui_util.h"
23 #include "chrome/browser/tab_contents/tab_contents.h"
20 #include "chrome/browser/upgrade_detector.h" 24 #include "chrome/browser/upgrade_detector.h"
21 #include "chrome/common/chrome_switches.h" 25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/notification_source.h"
27 #include "chrome/common/notification_type.h"
22 #include "grit/chromium_strings.h" 28 #include "grit/chromium_strings.h"
23 #include "grit/generated_resources.h" 29 #include "grit/generated_resources.h"
24 #include "grit/theme_resources.h" 30 #include "grit/theme_resources.h"
25 31
26 //////////////////////////////////////////////////////////////////////////////// 32 ////////////////////////////////////////////////////////////////////////////////
27 // ToolsMenuModel 33 // ToolsMenuModel
28 34
29 ToolsMenuModel::ToolsMenuModel(menus::SimpleMenuModel::Delegate* delegate, 35 ToolsMenuModel::ToolsMenuModel(menus::SimpleMenuModel::Delegate* delegate,
30 Browser* browser) 36 Browser* browser)
31 : SimpleMenuModel(delegate) { 37 : SimpleMenuModel(delegate) {
(...skipping 22 matching lines...) Expand all
54 AddItemWithStringId(IDC_DEV_TOOLS_CONSOLE, IDS_DEV_TOOLS_CONSOLE); 60 AddItemWithStringId(IDC_DEV_TOOLS_CONSOLE, IDS_DEV_TOOLS_CONSOLE);
55 } 61 }
56 } 62 }
57 63
58 //////////////////////////////////////////////////////////////////////////////// 64 ////////////////////////////////////////////////////////////////////////////////
59 // WrenchMenuModel 65 // WrenchMenuModel
60 66
61 WrenchMenuModel::WrenchMenuModel(menus::SimpleMenuModel::Delegate* delegate, 67 WrenchMenuModel::WrenchMenuModel(menus::SimpleMenuModel::Delegate* delegate,
62 Browser* browser) 68 Browser* browser)
63 : menus::SimpleMenuModel(delegate), 69 : menus::SimpleMenuModel(delegate),
70 delegate_(delegate),
64 browser_(browser) { 71 browser_(browser) {
65 Build(); 72 Build();
73 UpdateZoomControls();
74
75 registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED,
76 Source<Profile>(browser_->profile()));
66 } 77 }
67 78
68 WrenchMenuModel::~WrenchMenuModel() { 79 WrenchMenuModel::~WrenchMenuModel() {
69 } 80 }
70 81
71 static bool CalculateEnabled() { 82 static bool CalculateEnabled() {
72 CommandLine* cl = CommandLine::ForCurrentProcess(); 83 CommandLine* cl = CommandLine::ForCurrentProcess();
73 if (cl->HasSwitch(switches::kNewWrenchMenu)) { 84 if (cl->HasSwitch(switches::kNewWrenchMenu)) {
74 // Honor the switch if present. 85 // Honor the switch if present.
75 std::string value = cl->GetSwitchValueASCII(switches::kNewWrenchMenu); 86 std::string value = cl->GetSwitchValueASCII(switches::kNewWrenchMenu);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 if (GetCommandIdAt(index) == IDC_ABOUT && 125 if (GetCommandIdAt(index) == IDC_ABOUT &&
115 Singleton<UpgradeDetector>::get()->notify_upgrade()) { 126 Singleton<UpgradeDetector>::get()->notify_upgrade()) {
116 // Show the exclamation point next to the menu item. 127 // Show the exclamation point next to the menu item.
117 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 128 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
118 *icon = *rb.GetBitmapNamed(IDR_UPDATE_AVAILABLE); 129 *icon = *rb.GetBitmapNamed(IDR_UPDATE_AVAILABLE);
119 return true; 130 return true;
120 } 131 }
121 return false; 132 return false;
122 } 133 }
123 134
135 bool WrenchMenuModel::IsLabelForCommandIdDynamic(int command_id) const {
136 return false;
137 }
138
139 string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const {
140 // TODO(erg): Hook up percentage calculation once I add that widget.
141 return string16();
142 }
143
144 void WrenchMenuModel::ExecuteCommand(int command_id) {
145 if (delegate_)
146 delegate_->ExecuteCommand(command_id);
147 }
148
149 void WrenchMenuModel::Observe(NotificationType type,
150 const NotificationSource& source,
151 const NotificationDetails& details) {
152 DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value);
153 UpdateZoomControls();
154 }
155
124 void WrenchMenuModel::Build() { 156 void WrenchMenuModel::Build() {
125 AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB); 157 AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
126 AddItemWithStringId(IDC_NEW_WINDOW, IDS_NEW_WINDOW); 158 AddItemWithStringId(IDC_NEW_WINDOW, IDS_NEW_WINDOW);
127 AddItemWithStringId(IDC_NEW_INCOGNITO_WINDOW, IDS_NEW_INCOGNITO_WINDOW); 159 AddItemWithStringId(IDC_NEW_INCOGNITO_WINDOW, IDS_NEW_INCOGNITO_WINDOW);
128 160
129 AddSeparator(); 161 AddSeparator();
162 #if defined(OS_LINUX)
163 edit_menu_item_model_.reset(new menus::ButtonMenuItemModel(IDS_EDIT, this));
164 edit_menu_item_model_->AddItemWithStringId(IDC_CUT, IDS_CUT);
165 edit_menu_item_model_->AddItemWithStringId(IDC_COPY, IDS_COPY);
166 edit_menu_item_model_->AddItemWithStringId(IDC_PASTE, IDS_PASTE);
167 AddButtonItem(0, edit_menu_item_model_.get());
168 #else
169 // TODO(port): Move to the above.
130 CreateCutCopyPaste(); 170 CreateCutCopyPaste();
171 #endif
172
131 AddSeparator(); 173 AddSeparator();
174 #if defined(OS_LINUX)
175 zoom_menu_item_model_.reset(
176 new menus::ButtonMenuItemModel(IDS_ZOOM_MENU, this));
177 zoom_menu_item_model_->AddItemWithStringId(IDC_ZOOM_PLUS, IDS_ZOOM_PLUS2);
178 zoom_menu_item_model_->AddItemWithStringId(IDC_ZOOM_MINUS, IDS_ZOOM_MINUS2);
179 zoom_menu_item_model_->AddSpace();
180 zoom_menu_item_model_->AddItemWithImage(
181 IDC_FULLSCREEN, IDR_FULLSCREEN_MENU_BUTTON);
182 AddButtonItem(0, zoom_menu_item_model_.get());
183 #else
184 // TODO(port): Move to the above.
132 CreateZoomFullscreen(); 185 CreateZoomFullscreen();
186 #endif
187
133 AddSeparator(); 188 AddSeparator();
134 AddItemWithStringId(IDC_SAVE_PAGE, IDS_SAVE_PAGE); 189 AddItemWithStringId(IDC_SAVE_PAGE, IDS_SAVE_PAGE);
135 AddItemWithStringId(IDC_FIND, IDS_FIND); 190 AddItemWithStringId(IDC_FIND, IDS_FIND);
136 AddItemWithStringId(IDC_PRINT, IDS_PRINT); 191 AddItemWithStringId(IDC_PRINT, IDS_PRINT);
137 192
138 tools_menu_model_.reset(new ToolsMenuModel(delegate(), browser_)); 193 tools_menu_model_.reset(new ToolsMenuModel(delegate(), browser_));
139 AddSubMenuWithStringId(IDC_ZOOM_MENU, IDS_TOOLS_MENU, 194 AddSubMenuWithStringId(IDC_ZOOM_MENU, IDS_TOOLS_MENU,
140 tools_menu_model_.get()); 195 tools_menu_model_.get());
141 196
142 AddSeparator(); 197 AddSeparator();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 } 232 }
178 233
179 void WrenchMenuModel::CreateZoomFullscreen() { 234 void WrenchMenuModel::CreateZoomFullscreen() {
180 // WARNING: views/wrench_menu assumes these items are added in this order. If 235 // WARNING: views/wrench_menu assumes these items are added in this order. If
181 // you change the order you'll need to update wrench_menu as well. 236 // you change the order you'll need to update wrench_menu as well.
182 AddItemWithStringId(IDC_ZOOM_PLUS, IDS_ZOOM_PLUS); 237 AddItemWithStringId(IDC_ZOOM_PLUS, IDS_ZOOM_PLUS);
183 AddItemWithStringId(IDC_ZOOM_MINUS, IDS_ZOOM_MINUS); 238 AddItemWithStringId(IDC_ZOOM_MINUS, IDS_ZOOM_MINUS);
184 AddItemWithStringId(IDC_FULLSCREEN, IDS_FULLSCREEN); 239 AddItemWithStringId(IDC_FULLSCREEN, IDS_FULLSCREEN);
185 } 240 }
186 241
242 void WrenchMenuModel::UpdateZoomControls() {
243 bool enable_increment, enable_decrement;
244 int zoom_percent =
245 static_cast<int>(GetZoom(&enable_increment, &enable_decrement) * 100);
246
247 // TODO(erg): Route the stringified zoom_percent through
248 // GetLabelForCommandId(). Also make a way to use enable_increment/decrement.
249 zoom_label_ = l10n_util::GetStringFUTF16(
250 IDS_ZOOM_PERCENT, IntToString16(zoom_percent));
251 }
252
253 double WrenchMenuModel::GetZoom(bool* enable_increment,
254 bool* enable_decrement) {
255 TabContents* selected_tab = browser_->GetSelectedTabContents();
256 *enable_decrement = *enable_increment = false;
257 if (!selected_tab)
258 return 1;
259
260 HostZoomMap* zoom_map = selected_tab->profile()->GetHostZoomMap();
261 if (!zoom_map)
262 return 1;
263
264 // This code comes from WebViewImpl::setZoomLevel.
265 int zoom_level = zoom_map->GetZoomLevel(selected_tab->GetURL());
266 double value = static_cast<double>(
267 std::max(std::min(std::pow(1.2, zoom_level), 3.0), .5));
268 *enable_decrement = (value != .5);
269 *enable_increment = (value != 3.0);
270 return value;
271 }
272
187 string16 WrenchMenuModel::GetSyncMenuLabel() const { 273 string16 WrenchMenuModel::GetSyncMenuLabel() const {
188 return sync_ui_util::GetSyncMenuLabel( 274 return sync_ui_util::GetSyncMenuLabel(
189 browser_->profile()->GetOriginalProfile()->GetProfileSyncService()); 275 browser_->profile()->GetOriginalProfile()->GetProfileSyncService());
190 } 276 }
191 277
192 string16 WrenchMenuModel::GetAboutEntryMenuLabel() const { 278 string16 WrenchMenuModel::GetAboutEntryMenuLabel() const {
193 if (Singleton<UpgradeDetector>::get()->notify_upgrade()) { 279 if (Singleton<UpgradeDetector>::get()->notify_upgrade()) {
194 return l10n_util::GetStringFUTF16( 280 return l10n_util::GetStringFUTF16(
195 IDS_UPDATE_NOW, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); 281 IDS_UPDATE_NOW, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
196 } 282 }
197 return l10n_util::GetStringFUTF16( 283 return l10n_util::GetStringFUTF16(
198 IDS_ABOUT, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); 284 IDS_ABOUT, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
199 } 285 }
200 286
201 bool WrenchMenuModel::IsDynamicItem(int index) const { 287 bool WrenchMenuModel::IsDynamicItem(int index) const {
202 int command_id = GetCommandIdAt(index); 288 int command_id = GetCommandIdAt(index);
203 return command_id == IDC_SYNC_BOOKMARKS || 289 return command_id == IDC_SYNC_BOOKMARKS ||
204 command_id == IDC_ABOUT; 290 command_id == IDC_ABOUT;
205 } 291 }
OLDNEW
« no previous file with comments | « chrome/browser/wrench_menu_model.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698