12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_tree_host.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/cursor/cursor_loader.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/views/widget/desktop_aura/desktop_cursor_loader_updater.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace views {
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DesktopNativeCursorManager::DesktopNativeCursorManager(
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    scoped_ptr<DesktopCursorLoaderUpdater> cursor_loader_updater)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : cursor_loader_updater_(cursor_loader_updater.Pass()),
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      cursor_loader_(ui::CursorLoader::Create()) {
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (cursor_loader_updater_.get())
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cursor_loader_updater_->OnCreate(1.0f, cursor_loader_.get());
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DesktopNativeCursorManager::~DesktopNativeCursorManager() {
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochgfx::NativeCursor DesktopNativeCursorManager::GetInitializedCursor(int type) {
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  gfx::NativeCursor cursor(type);
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  cursor_loader_->SetPlatformCursor(&cursor);
287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return cursor;
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void DesktopNativeCursorManager::AddHost(aura::WindowTreeHost* host) {
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  hosts_.insert(host);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void DesktopNativeCursorManager::RemoveHost(aura::WindowTreeHost* host) {
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  hosts_.erase(host);
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DesktopNativeCursorManager::SetDisplay(
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Display& display,
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    wm::NativeCursorManagerDelegate* delegate) {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  cursor_loader_->UnloadAll();
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cursor_loader_->set_rotation(display.rotation());
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cursor_loader_->set_scale(display.device_scale_factor());
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (cursor_loader_updater_.get())
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    cursor_loader_updater_->OnDisplayUpdated(display, cursor_loader_.get());
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetCursor(delegate->GetCursor(), delegate);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DesktopNativeCursorManager::SetCursor(
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeCursor cursor,
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    wm::NativeCursorManagerDelegate* delegate) {
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::NativeCursor new_cursor = cursor;
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  cursor_loader_->SetPlatformCursor(&new_cursor);
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delegate->CommitCursor(new_cursor);
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (delegate->IsCursorVisible()) {
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      (*i)->SetCursor(new_cursor);
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DesktopNativeCursorManager::SetVisibility(
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool visible,
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    wm::NativeCursorManagerDelegate* delegate) {
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delegate->CommitVisibility(visible);
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (visible) {
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SetCursor(delegate->GetCursor(), delegate);
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::NativeCursor invisible_cursor(ui::kCursorNone);
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    cursor_loader_->SetPlatformCursor(&invisible_cursor);
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      (*i)->SetCursor(invisible_cursor);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    (*i)->OnCursorVisibilityChanged(visible);
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void DesktopNativeCursorManager::SetCursorSet(
84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ui::CursorSetType cursor_set,
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    wm::NativeCursorManagerDelegate* delegate) {
86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  NOTIMPLEMENTED();
87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DesktopNativeCursorManager::SetMouseEventsEnabled(
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool enabled,
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    wm::NativeCursorManagerDelegate* delegate) {
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delegate->CommitMouseEventsEnabled(enabled);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(erg): In the ash version, we set the last mouse location on Env. I'm
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // not sure this concept makes sense on the desktop.
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetVisibility(delegate->IsCursorVisible(), delegate);
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    (*i)->dispatcher()->OnMouseEventsEnableStateChanged(enabled);
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace views
104