OLD | NEW |
| (Empty) |
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 #include "chrome/browser/chromeos/webui/wrench_menu_ui.h" | |
6 | |
7 #include "base/string_number_conversions.h" | |
8 #include "base/string_util.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "base/weak_ptr.h" | |
12 #include "chrome/app/chrome_command_ids.h" | |
13 #include "chrome/browser/browser_list.h" | |
14 #include "chrome/browser/chromeos/views/native_menu_webui.h" | |
15 #include "chrome/browser/chromeos/views/webui_menu_widget.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/common/url_constants.h" | |
18 #include "content/browser/tab_contents/tab_contents.h" | |
19 #include "content/common/notification_registrar.h" | |
20 #include "content/common/notification_source.h" | |
21 #include "googleurl/src/gurl.h" | |
22 #include "grit/browser_resources.h" | |
23 #include "grit/generated_resources.h" | |
24 #include "ui/base/l10n/l10n_util.h" | |
25 #include "views/controls/menu/menu_2.h" | |
26 | |
27 namespace { | |
28 | |
29 class WrenchMenuSourceDelegate : public chromeos::MenuSourceDelegate { | |
30 public: | |
31 virtual void AddCustomConfigValues(DictionaryValue* config) const { | |
32 // Resources that are necessary to build wrench menu. | |
33 config->SetInteger("IDC_CUT", IDC_CUT); | |
34 config->SetInteger("IDC_COPY", IDC_COPY); | |
35 config->SetInteger("IDC_PASTE", IDC_PASTE); | |
36 config->SetInteger("IDC_ZOOM_MINUS", IDC_ZOOM_MINUS); | |
37 config->SetInteger("IDC_ZOOM_PLUS", IDC_ZOOM_PLUS); | |
38 config->SetInteger("IDC_FULLSCREEN", IDC_FULLSCREEN); | |
39 | |
40 config->SetString("IDS_EDIT2", l10n_util::GetStringUTF8(IDS_EDIT2)); | |
41 config->SetString("IDS_ZOOM_MENU2", | |
42 l10n_util::GetStringUTF8(IDS_ZOOM_MENU2)); | |
43 config->SetString("IDS_CUT", l10n_util::GetStringUTF8(IDS_CUT)); | |
44 config->SetString("IDS_COPY", l10n_util::GetStringUTF8(IDS_COPY)); | |
45 config->SetString("IDS_PASTE", l10n_util::GetStringUTF8(IDS_PASTE)); | |
46 } | |
47 }; | |
48 | |
49 } // namespace | |
50 | |
51 namespace chromeos { | |
52 | |
53 //////////////////////////////////////////////////////////////////////////////// | |
54 // | |
55 // WrenchMenuUI | |
56 // | |
57 //////////////////////////////////////////////////////////////////////////////// | |
58 | |
59 WrenchMenuUI::WrenchMenuUI(TabContents* contents) | |
60 : chromeos::MenuUI( | |
61 contents, | |
62 ALLOW_THIS_IN_INITIALIZER_LIST( | |
63 CreateMenuUIHTMLSource(new WrenchMenuSourceDelegate(), | |
64 chrome::kChromeUIWrenchMenu, | |
65 "WrenchMenu" /* class name */, | |
66 IDR_WRENCH_MENU_JS, | |
67 IDR_WRENCH_MENU_CSS))) { | |
68 registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED, | |
69 Source<Profile>(GetProfile())); | |
70 } | |
71 | |
72 void WrenchMenuUI::ModelUpdated(const ui::MenuModel* new_model) { | |
73 MenuUI::ModelUpdated(new_model); | |
74 UpdateZoomControls(); | |
75 } | |
76 | |
77 void WrenchMenuUI::Observe(NotificationType type, | |
78 const NotificationSource& source, | |
79 const NotificationDetails& details) { | |
80 DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value); | |
81 UpdateZoomControls(); | |
82 } | |
83 | |
84 void WrenchMenuUI::UpdateZoomControls() { | |
85 WebUIMenuWidget* widget = WebUIMenuWidget::FindWebUIMenuWidget( | |
86 tab_contents()->GetNativeView()); | |
87 if (!widget || !widget->is_root()) | |
88 return; | |
89 Browser* browser = BrowserList::GetLastActive(); | |
90 if (!browser) | |
91 return; | |
92 TabContents* selected_tab = browser->GetSelectedTabContents(); | |
93 bool enable_increment = false; | |
94 bool enable_decrement = false; | |
95 int zoom = 100; | |
96 if (selected_tab) | |
97 zoom = selected_tab->GetZoomPercent(&enable_increment, &enable_decrement); | |
98 | |
99 DictionaryValue params; | |
100 params.SetBoolean("plus", enable_increment); | |
101 params.SetBoolean("minus", enable_decrement); | |
102 params.SetString("percent", l10n_util::GetStringFUTF16( | |
103 IDS_ZOOM_PERCENT, UTF8ToUTF16(base::IntToString(zoom)))); | |
104 CallJavascriptFunction("updateZoomControls", params); | |
105 } | |
106 | |
107 views::Menu2* WrenchMenuUI::CreateMenu2(ui::MenuModel* model) { | |
108 views::Menu2* menu = new views::Menu2(model); | |
109 NativeMenuWebUI::SetMenuURL( | |
110 menu, GURL(StringPrintf("chrome://%s", chrome::kChromeUIWrenchMenu))); | |
111 return menu; | |
112 } | |
113 | |
114 } // namespace chromeos | |
OLD | NEW |