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.
4
5#include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
6
7#include "ui/aura/window_event_dispatcher.h"
8#include "ui/aura/window_tree_host.h"
9#include "ui/base/cursor/cursor_loader.h"
10#include "ui/views/widget/desktop_aura/desktop_cursor_loader_updater.h"
11
12namespace views {
13
14DesktopNativeCursorManager::DesktopNativeCursorManager(
15    scoped_ptr<DesktopCursorLoaderUpdater> cursor_loader_updater)
16    : cursor_loader_updater_(cursor_loader_updater.Pass()),
17      cursor_loader_(ui::CursorLoader::Create()) {
18  if (cursor_loader_updater_.get())
19    cursor_loader_updater_->OnCreate(1.0f, cursor_loader_.get());
20}
21
22DesktopNativeCursorManager::~DesktopNativeCursorManager() {
23}
24
25gfx::NativeCursor DesktopNativeCursorManager::GetInitializedCursor(int type) {
26  gfx::NativeCursor cursor(type);
27  cursor_loader_->SetPlatformCursor(&cursor);
28  return cursor;
29}
30
31void DesktopNativeCursorManager::AddHost(aura::WindowTreeHost* host) {
32  hosts_.insert(host);
33}
34
35void DesktopNativeCursorManager::RemoveHost(aura::WindowTreeHost* host) {
36  hosts_.erase(host);
37}
38
39void DesktopNativeCursorManager::SetDisplay(
40    const gfx::Display& display,
41    wm::NativeCursorManagerDelegate* delegate) {
42  cursor_loader_->UnloadAll();
43  cursor_loader_->set_rotation(display.rotation());
44  cursor_loader_->set_scale(display.device_scale_factor());
45
46  if (cursor_loader_updater_.get())
47    cursor_loader_updater_->OnDisplayUpdated(display, cursor_loader_.get());
48
49  SetCursor(delegate->GetCursor(), delegate);
50}
51
52void DesktopNativeCursorManager::SetCursor(
53    gfx::NativeCursor cursor,
54    wm::NativeCursorManagerDelegate* delegate) {
55  gfx::NativeCursor new_cursor = cursor;
56  cursor_loader_->SetPlatformCursor(&new_cursor);
57  delegate->CommitCursor(new_cursor);
58
59  if (delegate->IsCursorVisible()) {
60    for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
61      (*i)->SetCursor(new_cursor);
62  }
63}
64
65void DesktopNativeCursorManager::SetVisibility(
66    bool visible,
67    wm::NativeCursorManagerDelegate* delegate) {
68  delegate->CommitVisibility(visible);
69
70  if (visible) {
71    SetCursor(delegate->GetCursor(), delegate);
72  } else {
73    gfx::NativeCursor invisible_cursor(ui::kCursorNone);
74    cursor_loader_->SetPlatformCursor(&invisible_cursor);
75    for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
76      (*i)->SetCursor(invisible_cursor);
77  }
78
79  for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
80    (*i)->OnCursorVisibilityChanged(visible);
81}
82
83void DesktopNativeCursorManager::SetCursorSet(
84    ui::CursorSetType cursor_set,
85    wm::NativeCursorManagerDelegate* delegate) {
86  NOTIMPLEMENTED();
87}
88
89void DesktopNativeCursorManager::SetMouseEventsEnabled(
90    bool enabled,
91    wm::NativeCursorManagerDelegate* delegate) {
92  delegate->CommitMouseEventsEnabled(enabled);
93
94  // TODO(erg): In the ash version, we set the last mouse location on Env. I'm
95  // not sure this concept makes sense on the desktop.
96
97  SetVisibility(delegate->IsCursorVisible(), delegate);
98
99  for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
100    (*i)->dispatcher()->OnMouseEventsEnableStateChanged(enabled);
101}
102
103}  // namespace views
104