- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / corewm / cursor_manager.cc
1 // Copyright (c) 2013 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/corewm/cursor_manager.h"
6
7 #include "base/logging.h"
8 #include "ui/aura/client/cursor_client_observer.h"
9 #include "ui/views/corewm/native_cursor_manager.h"
10 #include "ui/views/corewm/native_cursor_manager_delegate.h"
11
12 namespace views {
13 namespace corewm {
14
15 namespace internal {
16
17 // Represents the cursor state which is composed of cursor type, visibility, and
18 // mouse events enable state. When mouse events are disabled, the cursor is
19 // always invisible.
20 class CursorState {
21  public:
22   CursorState()
23       : cursor_(ui::kCursorNone),
24         visible_(true),
25         scale_(1.f),
26         cursor_set_(ui::CURSOR_SET_NORMAL),
27         mouse_events_enabled_(true),
28         visible_on_mouse_events_enabled_(true) {
29   }
30
31   gfx::NativeCursor cursor() const { return cursor_; }
32   void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; }
33
34   bool visible() const { return visible_; }
35   void SetVisible(bool visible) {
36     if (mouse_events_enabled_)
37       visible_ = visible;
38     // Ignores the call when mouse events disabled.
39   }
40
41   float scale() const { return scale_; }
42   void set_scale(float scale) {
43     scale_ = scale;
44   }
45
46   ui::CursorSetType cursor_set() const { return cursor_set_; }
47   void set_cursor_set(ui::CursorSetType cursor_set) {
48     cursor_set_ = cursor_set;
49   }
50
51   bool mouse_events_enabled() const { return mouse_events_enabled_; }
52   void SetMouseEventsEnabled(bool enabled) {
53     if (mouse_events_enabled_ == enabled)
54       return;
55     mouse_events_enabled_ = enabled;
56
57     // Restores the visibility when mouse events are enabled.
58     if (enabled) {
59       visible_ = visible_on_mouse_events_enabled_;
60     } else {
61       visible_on_mouse_events_enabled_ = visible_;
62       visible_ = false;
63     }
64   }
65
66  private:
67   gfx::NativeCursor cursor_;
68   bool visible_;
69   float scale_;
70   ui::CursorSetType cursor_set_;
71   bool mouse_events_enabled_;
72
73   // The visibility to set when mouse events are enabled.
74   bool visible_on_mouse_events_enabled_;
75
76   DISALLOW_COPY_AND_ASSIGN(CursorState);
77 };
78
79 }  // namespace internal
80
81 CursorManager::CursorManager(scoped_ptr<NativeCursorManager> delegate)
82     : delegate_(delegate.Pass()),
83       cursor_lock_count_(0),
84       current_state_(new internal::CursorState),
85       state_on_unlock_(new internal::CursorState) {
86 }
87
88 CursorManager::~CursorManager() {
89 }
90
91 void CursorManager::SetCursor(gfx::NativeCursor cursor) {
92   state_on_unlock_->set_cursor(cursor);
93   if (cursor_lock_count_ == 0 &&
94       GetCurrentCursor() != state_on_unlock_->cursor()) {
95     delegate_->SetCursor(state_on_unlock_->cursor(), this);
96   }
97 }
98
99 void CursorManager::ShowCursor() {
100   state_on_unlock_->SetVisible(true);
101   if (cursor_lock_count_ == 0 &&
102       IsCursorVisible() != state_on_unlock_->visible()) {
103     delegate_->SetVisibility(state_on_unlock_->visible(), this);
104     FOR_EACH_OBSERVER(aura::client::CursorClientObserver, observers_,
105                       OnCursorVisibilityChanged(true));
106   }
107 }
108
109 void CursorManager::HideCursor() {
110   state_on_unlock_->SetVisible(false);
111   if (cursor_lock_count_ == 0 &&
112       IsCursorVisible() != state_on_unlock_->visible()) {
113     delegate_->SetVisibility(state_on_unlock_->visible(), this);
114     FOR_EACH_OBSERVER(aura::client::CursorClientObserver, observers_,
115                       OnCursorVisibilityChanged(false));
116   }
117 }
118
119 bool CursorManager::IsCursorVisible() const {
120   return current_state_->visible();
121 }
122
123 void CursorManager::SetScale(float scale) {
124   state_on_unlock_->set_scale(scale);
125   if (GetCurrentScale() != state_on_unlock_->scale())
126     delegate_->SetScale(state_on_unlock_->scale(), this);
127 }
128
129 void CursorManager::SetCursorSet(ui::CursorSetType cursor_set) {
130   state_on_unlock_->set_cursor_set(cursor_set);
131   if (GetCurrentCursorSet() != state_on_unlock_->cursor_set())
132     delegate_->SetCursorSet(state_on_unlock_->cursor_set(), this);
133 }
134
135 float CursorManager::GetCurrentScale() const {
136   return current_state_->scale();
137 }
138
139 ui::CursorSetType CursorManager::GetCurrentCursorSet() const {
140   return current_state_->cursor_set();
141 }
142
143 void CursorManager::EnableMouseEvents() {
144   state_on_unlock_->SetMouseEventsEnabled(true);
145   if (cursor_lock_count_ == 0 &&
146       IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
147     delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(),
148                                      this);
149   }
150 }
151
152 void CursorManager::DisableMouseEvents() {
153   state_on_unlock_->SetMouseEventsEnabled(false);
154   if (cursor_lock_count_ == 0 &&
155       IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
156     delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(),
157                                      this);
158   }
159 }
160
161 bool CursorManager::IsMouseEventsEnabled() const {
162   return current_state_->mouse_events_enabled();
163 }
164
165 void CursorManager::SetDisplay(const gfx::Display& display) {
166   delegate_->SetDisplay(display, this);
167 }
168
169 void CursorManager::LockCursor() {
170   cursor_lock_count_++;
171 }
172
173 void CursorManager::UnlockCursor() {
174   cursor_lock_count_--;
175   DCHECK_GE(cursor_lock_count_, 0);
176   if (cursor_lock_count_ > 0)
177     return;
178
179   if (GetCurrentCursor() != state_on_unlock_->cursor()) {
180     delegate_->SetCursor(state_on_unlock_->cursor(), this);
181   }
182   if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
183     delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(),
184                                      this);
185   }
186   if (IsCursorVisible() != state_on_unlock_->visible()) {
187     delegate_->SetVisibility(state_on_unlock_->visible(),
188                              this);
189   }
190 }
191
192 bool CursorManager::IsCursorLocked() const {
193   return cursor_lock_count_ > 0;
194 }
195
196 void CursorManager::AddObserver(
197     aura::client::CursorClientObserver* observer) {
198   observers_.AddObserver(observer);
199 }
200
201 void CursorManager::RemoveObserver(
202     aura::client::CursorClientObserver* observer) {
203   observers_.RemoveObserver(observer);
204 }
205
206 gfx::NativeCursor CursorManager::GetCurrentCursor() const {
207   return current_state_->cursor();
208 }
209
210 bool CursorManager::GetCurrentVisibility() const {
211   return current_state_->visible();
212 }
213
214 bool CursorManager::GetMouseEventsEnabled() const {
215   return current_state_->mouse_events_enabled();
216 }
217
218 void CursorManager::CommitCursor(gfx::NativeCursor cursor) {
219   current_state_->set_cursor(cursor);
220 }
221
222 void CursorManager::CommitVisibility(bool visible) {
223   FOR_EACH_OBSERVER(aura::client::CursorClientObserver, observers_,
224                     OnCursorVisibilityChanged(visible));
225   current_state_->SetVisible(visible);
226 }
227
228 void CursorManager::CommitScale(float scale) {
229   current_state_->set_scale(scale);
230 }
231
232 void CursorManager::CommitCursorSet(ui::CursorSetType cursor_set) {
233   current_state_->set_cursor_set(cursor_set);
234 }
235
236 void CursorManager::CommitMouseEventsEnabled(bool enabled) {
237   current_state_->SetMouseEventsEnabled(enabled);
238 }
239
240 }  // namespace corewm
241 }  // namespace views