OLD | NEW |
| (Empty) |
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_gtk.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "ui/base/keycodes/keyboard_codes_posix.h" | |
10 #include "ui/views/controls/scrollbar/native_scroll_bar.h" | |
11 #include "ui/views/controls/scrollbar/native_scroll_bar_views.h" | |
12 #include "ui/views/controls/scrollbar/scroll_bar.h" | |
13 #include "ui/views/widget/widget.h" | |
14 | |
15 namespace views { | |
16 | |
17 //////////////////////////////////////////////////////////////////////////////// | |
18 // NativeScrollBarGtk, public: | |
19 | |
20 NativeScrollBarGtk::NativeScrollBarGtk(NativeScrollBar* scroll_bar) | |
21 : NativeControlGtk(), | |
22 native_scroll_bar_(scroll_bar) { | |
23 set_focus_view(scroll_bar); | |
24 } | |
25 | |
26 NativeScrollBarGtk::~NativeScrollBarGtk() { | |
27 } | |
28 | |
29 //////////////////////////////////////////////////////////////////////////////// | |
30 // NativeScrollBarGtk, View overrides: | |
31 | |
32 void NativeScrollBarGtk::Layout() { | |
33 NativeControlGtk::Layout(); | |
34 } | |
35 | |
36 gfx::Size NativeScrollBarGtk::GetPreferredSize() { | |
37 if (native_scroll_bar_->IsHorizontal()) | |
38 return gfx::Size(0, GetHorizontalScrollBarHeight()); | |
39 return gfx::Size(GetVerticalScrollBarWidth(), 0); | |
40 } | |
41 | |
42 // TODO(oshima|jcampan): key/mouse events are not delievered and | |
43 // the following code is not tested. It requires the focus manager to be fully | |
44 // implemented. | |
45 bool NativeScrollBarGtk::OnKeyPressed(const KeyEvent& event) { | |
46 if (!native_view()) | |
47 return false; | |
48 switch (event.key_code()) { | |
49 case ui::VKEY_UP: | |
50 if (!native_scroll_bar_->IsHorizontal()) | |
51 MoveStep(false /* negative */); | |
52 break; | |
53 case ui::VKEY_DOWN: | |
54 if (!native_scroll_bar_->IsHorizontal()) | |
55 MoveStep(true /* positive */); | |
56 break; | |
57 case ui::VKEY_LEFT: | |
58 if (native_scroll_bar_->IsHorizontal()) | |
59 MoveStep(false /* negative */); | |
60 break; | |
61 case ui::VKEY_RIGHT: | |
62 if (native_scroll_bar_->IsHorizontal()) | |
63 MoveStep(true /* positive */); | |
64 break; | |
65 case ui::VKEY_PRIOR: | |
66 MovePage(false /* negative */); | |
67 break; | |
68 case ui::VKEY_NEXT: | |
69 MovePage(true /* positive */); | |
70 break; | |
71 case ui::VKEY_HOME: | |
72 MoveTo(0); | |
73 break; | |
74 case ui::VKEY_END: | |
75 MoveToBottom(); | |
76 break; | |
77 default: | |
78 return false; | |
79 } | |
80 return true; | |
81 } | |
82 | |
83 bool NativeScrollBarGtk::OnMouseWheel(const MouseWheelEvent& e) { | |
84 if (!native_view()) | |
85 return false; | |
86 MoveBy(-e.offset()); // e.GetOffset() > 0 means scroll up. | |
87 return true; | |
88 } | |
89 | |
90 //////////////////////////////////////////////////////////////////////////////// | |
91 // NativeScrollBarGtk, NativeControlGtk overrides: | |
92 | |
93 void NativeScrollBarGtk::CreateNativeControl() { | |
94 GtkObject* adj = gtk_adjustment_new(native_scroll_bar_->GetMinPosition(), | |
95 native_scroll_bar_->GetMinPosition(), | |
96 native_scroll_bar_->GetMaxPosition(), | |
97 10, 10, | |
98 0); | |
99 GtkWidget* widget; | |
100 if (native_scroll_bar_->IsHorizontal()) { | |
101 widget = gtk_hscrollbar_new(GTK_ADJUSTMENT(adj)); | |
102 } else { | |
103 widget = gtk_vscrollbar_new(GTK_ADJUSTMENT(adj)); | |
104 } | |
105 | |
106 gtk_range_set_update_policy(GTK_RANGE(widget), GTK_UPDATE_CONTINUOUS); | |
107 | |
108 g_signal_connect(adj, "value-changed", | |
109 G_CALLBACK(CallValueChanged), this); | |
110 | |
111 NativeControlCreated(widget); | |
112 } | |
113 | |
114 //////////////////////////////////////////////////////////////////////////////// | |
115 // NativeScrollBarGtk, NativeScrollBarWrapper overrides: | |
116 | |
117 int NativeScrollBarGtk::GetPosition() const { | |
118 return static_cast<int>(gtk_range_get_value(GTK_RANGE(native_view()))); | |
119 } | |
120 | |
121 View* NativeScrollBarGtk::GetView() { | |
122 return this; | |
123 } | |
124 | |
125 void NativeScrollBarGtk::Update(int viewport_size, | |
126 int content_size, | |
127 int current_pos) { | |
128 if (!native_view()) | |
129 return; | |
130 | |
131 if (content_size < 0) | |
132 content_size = 0; | |
133 | |
134 if (current_pos < 0) | |
135 current_pos = 0; | |
136 | |
137 if (current_pos > content_size) | |
138 current_pos = content_size; | |
139 | |
140 ScrollBarController* controller = native_scroll_bar_->controller(); | |
141 int step = controller->GetScrollIncrement(native_scroll_bar_, | |
142 false /* step */, | |
143 true /* positive */); | |
144 int page = controller->GetScrollIncrement(native_scroll_bar_, | |
145 true /* page */, true); | |
146 GtkObject* adj = gtk_adjustment_new(current_pos, | |
147 native_scroll_bar_->GetMinPosition(), | |
148 content_size, | |
149 step, page, | |
150 viewport_size); | |
151 gtk_range_set_adjustment(GTK_RANGE(native_view()), GTK_ADJUSTMENT(adj)); | |
152 g_signal_connect(adj, "value-changed", G_CALLBACK(CallValueChanged), this); | |
153 } | |
154 | |
155 //////////////////////////////////////////////////////////////////////////////// | |
156 // NativeScrollBarGtk, private: | |
157 | |
158 void NativeScrollBarGtk::ValueChanged() { | |
159 ScrollBarController* controller = native_scroll_bar_->controller(); | |
160 controller->ScrollToPosition(native_scroll_bar_, GetPosition()); | |
161 } | |
162 | |
163 // static | |
164 void NativeScrollBarGtk::CallValueChanged(GtkWidget* widget, | |
165 NativeScrollBarGtk* scroll_bar) { | |
166 scroll_bar->ValueChanged(); | |
167 } | |
168 | |
169 void NativeScrollBarGtk::MoveBy(int o) { | |
170 MoveTo(GetPosition() + o); | |
171 } | |
172 | |
173 void NativeScrollBarGtk::MovePage(bool positive) { | |
174 ScrollBarController* controller = native_scroll_bar_->controller(); | |
175 MoveBy(controller->GetScrollIncrement(native_scroll_bar_, true, positive)); | |
176 } | |
177 | |
178 void NativeScrollBarGtk::MoveStep(bool positive) { | |
179 ScrollBarController* controller = native_scroll_bar_->controller(); | |
180 MoveBy(controller->GetScrollIncrement(native_scroll_bar_, false, positive)); | |
181 } | |
182 | |
183 void NativeScrollBarGtk::MoveTo(int p) { | |
184 if (p < native_scroll_bar_->GetMinPosition()) | |
185 p = native_scroll_bar_->GetMinPosition(); | |
186 if (p > native_scroll_bar_->GetMaxPosition()) | |
187 p = native_scroll_bar_->GetMaxPosition(); | |
188 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(native_view())); | |
189 gtk_adjustment_set_value(adj, p); | |
190 } | |
191 | |
192 void NativeScrollBarGtk::MoveToBottom() { | |
193 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(native_view())); | |
194 MoveTo(adj->upper); | |
195 } | |
196 | |
197 //////////////////////////////////////////////////////////////////////////////// | |
198 // NativewScrollBarWrapper, public: | |
199 | |
200 // static | |
201 NativeScrollBarWrapper* NativeScrollBarWrapper::CreateWrapper( | |
202 NativeScrollBar* scroll_bar) { | |
203 if (Widget::IsPureViews()) | |
204 return new NativeScrollBarViews(scroll_bar); | |
205 return new NativeScrollBarGtk(scroll_bar); | |
206 } | |
207 | |
208 // static | |
209 int NativeScrollBarWrapper::GetHorizontalScrollBarHeight() { | |
210 // TODO(oshima): get this from gtk's widget property "slider-width". | |
211 return 20; | |
212 } | |
213 | |
214 // static | |
215 int NativeScrollBarWrapper::GetVerticalScrollBarWidth() { | |
216 // TODO(oshima): get this from gtk's widget property "slider-width". | |
217 return 20; | |
218 } | |
219 | |
220 } // namespace views | |
OLD | NEW |