- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / controls / scrollbar / native_scroll_bar.cc
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/controls/scrollbar/native_scroll_bar.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/message_loop/message_loop.h"
11 #include "ui/events/event.h"
12 #include "ui/views/controls/scrollbar/native_scroll_bar_wrapper.h"
13 #include "ui/views/widget/widget.h"
14
15 #if defined(USE_AURA)
16 #include "ui/views/controls/scrollbar/native_scroll_bar_views.h"
17 #endif
18
19 namespace views {
20
21 // static
22 const char NativeScrollBar::kViewClassName[] = "NativeScrollBar";
23
24 ////////////////////////////////////////////////////////////////////////////////
25 // NativeScrollBar, public:
26 NativeScrollBar::NativeScrollBar(bool is_horizontal)
27     : ScrollBar(is_horizontal),
28       native_wrapper_(NULL) {
29 }
30
31 NativeScrollBar::~NativeScrollBar() {
32 }
33
34 // static
35 int NativeScrollBar::GetHorizontalScrollBarHeight(
36     const ui::NativeTheme* theme) {
37   return NativeScrollBarWrapper::GetHorizontalScrollBarHeight(theme);
38 }
39
40 // static
41 int NativeScrollBar::GetVerticalScrollBarWidth(
42     const ui::NativeTheme* theme) {
43   return NativeScrollBarWrapper::GetVerticalScrollBarWidth(theme);
44 }
45
46 ////////////////////////////////////////////////////////////////////////////////
47 // NativeScrollBar, View overrides:
48 gfx::Size NativeScrollBar::GetPreferredSize() {
49   if (native_wrapper_)
50     return native_wrapper_->GetView()->GetPreferredSize();
51   return gfx::Size();
52 }
53
54 void NativeScrollBar::Layout() {
55   if (native_wrapper_) {
56     native_wrapper_->GetView()->SetBounds(0, 0, width(), height());
57     native_wrapper_->GetView()->Layout();
58   }
59 }
60
61 void NativeScrollBar::ViewHierarchyChanged(
62     const ViewHierarchyChangedDetails& details) {
63   Widget* widget;
64   if (details.is_add && !native_wrapper_ && (widget = GetWidget())) {
65     native_wrapper_ = NativeScrollBarWrapper::CreateWrapper(this);
66     AddChildView(native_wrapper_->GetView());
67   }
68 }
69
70 const char* NativeScrollBar::GetClassName() const {
71   return kViewClassName;
72 }
73
74 // Overridden from View for keyboard UI.
75 bool NativeScrollBar::OnKeyPressed(const ui::KeyEvent& event) {
76   if (!native_wrapper_)
77     return false;
78   return native_wrapper_->GetView()->OnKeyPressed(event);
79 }
80
81 void NativeScrollBar::OnGestureEvent(ui::GestureEvent* event) {
82   if (!native_wrapper_)
83     return;
84   native_wrapper_->GetView()->OnGestureEvent(event);
85 }
86
87 bool NativeScrollBar::OnMouseWheel(const ui::MouseWheelEvent& event) {
88   if (!native_wrapper_)
89     return false;
90   return native_wrapper_->GetView()->OnMouseWheel(event);
91 }
92
93 ////////////////////////////////////////////////////////////////////////////////
94 // NativeScrollBar, ScrollBar overrides:
95 void NativeScrollBar::Update(int viewport_size,
96                              int content_size,
97                              int current_pos) {
98   ScrollBar::Update(viewport_size, content_size, current_pos);
99
100   if (native_wrapper_)
101     native_wrapper_->Update(viewport_size, content_size, current_pos);
102 }
103
104 int NativeScrollBar::GetLayoutSize() const {
105   return IsHorizontal() ?
106       GetHorizontalScrollBarHeight(GetNativeTheme()) :
107       GetVerticalScrollBarWidth(GetNativeTheme());
108 }
109
110 int NativeScrollBar::GetPosition() const {
111   if (!native_wrapper_)
112     return 0;
113   return native_wrapper_->GetPosition();
114 }
115
116 }  // namespace views
117