Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / ui / views / chrome_views_delegate.cc
blobf658bf2080d4c9baa45a3e16027a36fdc5da987a
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.
5 #include "chrome/browser/ui/views/chrome_views_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/prefs/scoped_user_pref_update.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views.h"
16 #include "chrome/common/pref_names.h"
17 #include "ui/base/ui_base_switches.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/views/widget/native_widget.h"
21 #include "ui/views/widget/widget.h"
23 #if defined(OS_WIN)
24 #include <dwmapi.h>
25 #include "base/win/windows_version.h"
26 #include "chrome/browser/app_icon_win.h"
27 #include "ui/base/win/shell.h"
28 #endif
30 #if defined(USE_AURA)
31 #include "ui/aura/root_window.h"
32 #endif
34 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
35 #include "chrome/browser/ui/host_desktop.h"
36 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
37 #include "ui/views/widget/native_widget_aura.h"
38 #endif
40 #if defined(USE_ASH)
41 #include "ash/shell.h"
42 #include "chrome/browser/ui/ash/ash_init.h"
43 #include "chrome/browser/ui/ash/ash_util.h"
44 #endif
46 namespace {
48 // If the given window has a profile associated with it, use that profile's
49 // preference service. Otherwise, store and retrieve the data from Local State.
50 // This function may return NULL if the necessary pref service has not yet
51 // been initialized.
52 // TODO(mirandac): This function will also separate windows by profile in a
53 // multi-profile environment.
54 PrefService* GetPrefsForWindow(const views::Widget* window) {
55 Profile* profile = reinterpret_cast<Profile*>(
56 window->GetNativeWindowProperty(Profile::kProfileKey));
57 if (!profile) {
58 // Use local state for windows that have no explicit profile.
59 return g_browser_process->local_state();
61 return profile->GetPrefs();
64 } // namespace
66 ///////////////////////////////////////////////////////////////////////////////
67 // ChromeViewsDelegate, views::ViewsDelegate implementation:
69 void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget* window,
70 const std::string& window_name,
71 const gfx::Rect& bounds,
72 ui::WindowShowState show_state) {
73 PrefService* prefs = GetPrefsForWindow(window);
74 if (!prefs)
75 return;
77 DCHECK(prefs->FindPreference(window_name.c_str()));
78 DictionaryPrefUpdate update(prefs, window_name.c_str());
79 DictionaryValue* window_preferences = update.Get();
80 window_preferences->SetInteger("left", bounds.x());
81 window_preferences->SetInteger("top", bounds.y());
82 window_preferences->SetInteger("right", bounds.right());
83 window_preferences->SetInteger("bottom", bounds.bottom());
84 window_preferences->SetBoolean("maximized",
85 show_state == ui::SHOW_STATE_MAXIMIZED);
86 gfx::Rect work_area(gfx::Screen::GetScreenFor(window->GetNativeView())->
87 GetDisplayNearestWindow(window->GetNativeView()).work_area());
88 window_preferences->SetInteger("work_area_left", work_area.x());
89 window_preferences->SetInteger("work_area_top", work_area.y());
90 window_preferences->SetInteger("work_area_right", work_area.right());
91 window_preferences->SetInteger("work_area_bottom", work_area.bottom());
94 bool ChromeViewsDelegate::GetSavedWindowPlacement(
95 const std::string& window_name,
96 gfx::Rect* bounds,
97 ui::WindowShowState* show_state) const {
98 PrefService* prefs = g_browser_process->local_state();
99 if (!prefs)
100 return false;
102 DCHECK(prefs->FindPreference(window_name.c_str()));
103 const DictionaryValue* dictionary = prefs->GetDictionary(window_name.c_str());
104 int left, top, right, bottom;
105 if (!dictionary || !dictionary->GetInteger("left", &left) ||
106 !dictionary->GetInteger("top", &top) ||
107 !dictionary->GetInteger("right", &right) ||
108 !dictionary->GetInteger("bottom", &bottom))
109 return false;
111 bounds->SetRect(left, top, right - left, bottom - top);
113 bool maximized = false;
114 if (dictionary)
115 dictionary->GetBoolean("maximized", &maximized);
116 *show_state = maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL;
118 return true;
121 void ChromeViewsDelegate::NotifyAccessibilityEvent(
122 views::View* view, ui::AccessibilityTypes::Event event_type) {
123 AccessibilityEventRouterViews::GetInstance()->HandleAccessibilityEvent(
124 view, event_type);
127 void ChromeViewsDelegate::NotifyMenuItemFocused(const string16& menu_name,
128 const string16& menu_item_name,
129 int item_index,
130 int item_count,
131 bool has_submenu) {
132 AccessibilityEventRouterViews::GetInstance()->HandleMenuItemFocused(
133 menu_name, menu_item_name, item_index, item_count, has_submenu);
136 #if defined(OS_WIN)
137 HICON ChromeViewsDelegate::GetDefaultWindowIcon() const {
138 return GetAppIcon();
140 #endif
142 views::NonClientFrameView* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
143 views::Widget* widget) {
144 #if defined(USE_ASH)
145 if (chrome::IsNativeViewInAsh(widget->GetNativeView()))
146 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget);
147 #endif
148 return NULL;
151 bool ChromeViewsDelegate::UseTransparentWindows() const {
152 #if defined(USE_ASH)
153 // TODO(scottmg): http://crbug.com/133312. This needs context to determine
154 // if it's desktop or ash.
155 #if defined(OS_CHROMEOS)
156 return true;
157 #else
158 NOTIMPLEMENTED();
159 return false;
160 #endif
161 #else
162 return false;
163 #endif
166 void ChromeViewsDelegate::AddRef() {
167 g_browser_process->AddRefModule();
170 void ChromeViewsDelegate::ReleaseRef() {
171 g_browser_process->ReleaseModule();
174 content::WebContents* ChromeViewsDelegate::CreateWebContents(
175 content::BrowserContext* browser_context,
176 content::SiteInstance* site_instance) {
177 return NULL;
180 void ChromeViewsDelegate::OnBeforeWidgetInit(
181 views::Widget::InitParams* params,
182 views::internal::NativeWidgetDelegate* delegate) {
183 // If we already have a native_widget, we don't have to try to come
184 // up with one.
185 if (params->native_widget)
186 return;
188 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
189 bool use_non_toplevel_window =
190 params->parent && params->type != views::Widget::InitParams::TYPE_MENU;
192 #if defined(OS_WIN)
193 // On desktop Linux Chrome must run in an environment that supports a variety
194 // of window managers, some of which do not play nicely with parts of our UI
195 // that have specific expectations about window sizing and placement. For this
196 // reason windows opened as top level (params.top_level) are always
197 // constrained by the browser frame, so we can position them correctly. This
198 // has some negative side effects, like dialogs being clipped by the browser
199 // frame, but the side effects are not as bad as the poor window manager
200 // interactions. On Windows however these WM interactions are not an issue, so
201 // we open windows requested as top_level as actual top level windows on the
202 // desktop.
203 use_non_toplevel_window = use_non_toplevel_window &&
204 (!params->top_level ||
205 chrome::GetHostDesktopTypeForNativeView(params->parent) !=
206 chrome::HOST_DESKTOP_TYPE_NATIVE);
208 if (!ui::win::IsAeroGlassEnabled()) {
209 // If we don't have composition (either because Glass is not enabled or
210 // because it was disabled at the command line), anything that requires
211 // transparency will be broken with a toplevel window, so force the use of
212 // a non toplevel window.
213 if (params->opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW &&
214 params->type != views::Widget::InitParams::TYPE_MENU)
215 use_non_toplevel_window = true;
216 } else {
217 // If we're on Vista+ with composition enabled, then we can use toplevel
218 // windows for most things (they get blended via WS_EX_COMPOSITED, which
219 // allows for animation effects, but also exceeding the bounds of the parent
220 // window).
221 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
222 params->parent &&
223 params->type != views::Widget::InitParams::TYPE_CONTROL &&
224 params->type != views::Widget::InitParams::TYPE_WINDOW) {
225 // When we set this to false, we get a DesktopNativeWidgetAura from the
226 // default case (not handled in this function).
227 use_non_toplevel_window = false;
230 #endif // OS_WIN
231 #endif // USE_AURA
233 #if defined(OS_CHROMEOS)
234 // When we are doing straight chromeos builds, we still need to handle the
235 // toplevel window case.
236 // There may be a few remaining widgets in Chrome OS that are not top level,
237 // but have neither a context nor a parent. Provide a fallback context so
238 // users don't crash. Developers will hit the DCHECK and should provide a
239 // context.
240 DCHECK(params->parent || params->context || params->top_level)
241 << "Please provide a parent or context for this widget.";
242 if (!params->parent && !params->context)
243 params->context = ash::Shell::GetPrimaryRootWindow();
244 #elif defined(USE_AURA)
245 // While the majority of the time, context wasn't plumbed through due to the
246 // existence of a global StackingClient, if this window is a toplevel, it's
247 // possible that there is no contextual state that we can use.
248 if (params->parent == NULL && params->context == NULL && params->top_level) {
249 // We need to make a decision about where to place this window based on the
250 // desktop type.
251 switch (chrome::GetActiveDesktop()) {
252 case chrome::HOST_DESKTOP_TYPE_NATIVE:
253 // If we're native, we should give this window its own toplevel desktop
254 // widget.
255 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
256 break;
257 #if defined(USE_ASH)
258 case chrome::HOST_DESKTOP_TYPE_ASH:
259 // If we're in ash, give this window the context of the main monitor.
260 params->context = ash::Shell::GetPrimaryRootWindow();
261 break;
262 #endif
263 default:
264 NOTREACHED();
266 } else if (use_non_toplevel_window) {
267 params->native_widget = new views::NativeWidgetAura(delegate);
268 } else if (params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
269 // TODO(erg): Once we've threaded context to everywhere that needs it, we
270 // should remove this check here.
271 gfx::NativeView to_check =
272 params->context ? params->context : params->parent;
273 if (chrome::GetHostDesktopTypeForNativeView(to_check) ==
274 chrome::HOST_DESKTOP_TYPE_NATIVE) {
275 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
278 #endif
281 #if !defined(OS_CHROMEOS)
282 base::TimeDelta
283 ChromeViewsDelegate::GetDefaultTextfieldObscuredRevealDuration() {
284 return base::TimeDelta();
286 #endif