Debian Codegraph @

Examples/GUIStyle/ZZ_Theme/scrollbar_zz.cpp

raw
1 #include <ClanLib/display.h>
2 #include <ClanLib/gl.h>
3
4 #include "scrollbar_zz.h"
5 #include "stylemanager_zz.h"
6 #include "button_zz.h"
7
8 CL_ScrollBar_ZZ::CL_ScrollBar_ZZ(
9 CL_ScrollBar * scrollbar,
10 CL_StyleManager_ZZ * style,
11 ScrollBarType scrollbar_type)
12 :
13 CL_ComponentStyle(scrollbar),
14 timer_scroll_(250),
15 scrollbar_(scrollbar),
16 initialized_(false),
17 style_(style),
18 resources_(style->get_resources()),
19 scrollbar_type_(scrollbar_type)
20 {
21 CL_Rect rect_decrease;
22 CL_Rect rect_increase;
23
24 CL_Component *client_area = scrollbar_->get_client_area();
25
26 if (scrollbar_type_ == type_normal)
27 {
28 if (scrollbar_->is_vertical())
29 {
30 int button_size = scrollbar_->get_width();
31 rect_decrease = CL_Rect(0, 0, button_size, button_size);
32 rect_increase = CL_Rect(0, scrollbar_->get_height() - button_size, button_size, scrollbar_->get_height());
33 }
34 else
35 {
36 int button_size = scrollbar_->get_height();
37 rect_decrease = CL_Rect(0, 0, button_size, button_size);
38 rect_increase = CL_Rect(scrollbar_->get_width() - button_size, 0, scrollbar_->get_width(), button_size);
39 }
40 button_increase_ = new CL_Button(rect_increase, "", scrollbar, style);
41 button_decrease_ = new CL_Button(rect_decrease, "", scrollbar, style);
42 }
43 else
44 {
45 // TODO: add horizontal
46 button_increase_ = new CL_Button(scrollbar_, style_);
47 CL_Button_ZZ * btn_style = (CL_Button_ZZ*)button_increase_->get_style();
48 if (btn_style)
49 {
50 btn_style->set_surfaces(
51 new CL_Surface("ListBox/scroll_btn_down", resources_),
52 new CL_Surface("ListBox/scroll_btn_down", resources_),
53 new CL_Surface("ListBox/scroll_btn_down", resources_),
54 new CL_Surface("ListBox/scroll_btn_down", resources_));
55 btn_style->set_button_type(CL_Button_ZZ::type_bitmap);
56 }
57 button_decrease_ = new CL_Button(scrollbar_, style_);
58 btn_style = (CL_Button_ZZ*)button_decrease_->get_style();
59 if (btn_style)
60 {
61 btn_style->set_surfaces(
62 new CL_Surface("ListBox/scroll_btn_up", resources_),
63 new CL_Surface("ListBox/scroll_btn_up", resources_),
64 new CL_Surface("ListBox/scroll_btn_up", resources_),
65 new CL_Surface("ListBox/scroll_btn_up", resources_));
66 btn_style->set_button_type(CL_Button_ZZ::type_bitmap);
67 }
68 }
69
70 slots_.connect(client_area->sig_paint(), this, &CL_ScrollBar_ZZ::on_paint);
71 slots_.connect(button_increase_->sig_pressed(), this, &CL_ScrollBar_ZZ::start_scroll, 1);
72 slots_.connect(button_decrease_->sig_pressed(), this, &CL_ScrollBar_ZZ::start_scroll, -1);
73 slots_.connect(button_increase_->sig_released(), this, &CL_ScrollBar_ZZ::stop_scroll);
74 slots_.connect(button_decrease_->sig_released(), this, &CL_ScrollBar_ZZ::stop_scroll);
75 slots_.connect(scrollbar_->sig_get_preferred_size(), this, &CL_ScrollBar_ZZ::on_get_preferred_size);
76 slots_.connect(timer_scroll_.sig_timer(), this, &CL_ScrollBar_ZZ::on_timer_scroll);
77 }
78
79 CL_ScrollBar_ZZ::~CL_ScrollBar_ZZ()
80 {
81 delete button_increase_;
82 delete button_decrease_;
83 }
84
85 void CL_ScrollBar_ZZ::start_scroll(int delta)
86 {
87 scroll_delta_ = delta;
88 timer_scroll_.enable();
89 on_timer_scroll();
90 }
91
92 void CL_ScrollBar_ZZ::stop_scroll()
93 {
94 timer_scroll_.disable();
95 }
96
97 void CL_ScrollBar_ZZ::on_timer_scroll()
98 {
99 scrollbar_->set_value(scrollbar_->get_value() + scroll_delta_);
100 }
101
102 void CL_ScrollBar_ZZ::on_paint()
103 {
104 if (!initialized_)
105 {
106 CL_Rect rect_decrease;
107 CL_Rect rect_increase;
108
109 CL_Component *client_area = scrollbar_->get_client_area();
110
111 if (scrollbar_->is_vertical())
112 {
113 int button_size = scrollbar_->get_width();
114 rect_decrease = CL_Rect(0, 0, button_size, button_size);
115 rect_increase = CL_Rect(0, scrollbar_->get_height() - button_size, button_size, scrollbar_->get_height());
116 }
117 else
118 {
119 int button_size = scrollbar_->get_height();
120 rect_decrease = CL_Rect(0, 0, button_size, button_size);
121 rect_increase = CL_Rect(scrollbar_->get_width() - button_size, 0, scrollbar_->get_width(), button_size);
122 }
123
124 CL_Rect client_area_rc = CL_Rect(0, 0, scrollbar_->get_position().get_width() - 1,
125 scrollbar_->get_position().get_height());
126
127 button_increase_->set_position(rect_increase);
128 button_decrease_->set_position(rect_decrease);
129
130 if (scrollbar_->is_vertical())
131 {
132 client_area_rc.top += rect_decrease.get_height();
133 client_area_rc.bottom -= rect_increase.get_height();
134 }
135 else
136 {
137 client_area_rc.left += rect_decrease.get_width();
138 client_area_rc.right -= rect_increase.get_width();
139 }
140
141 client_area->set_position(client_area_rc);
142 scrollbar_->set_max_value(scrollbar_->get_max_value());
143
144 initialized_ = true;
145 }
146
147 int width = scrollbar_->get_width();
148 int height = scrollbar_->get_height();
149
150 CL_Rect rect = scrollbar_->get_slider_rect();
151
152 // Background
153 CL_Display::fill_rect(
154 scrollbar_->client_to_screen(CL_Rect(0,0, width, height)),
155 CL_Color(42, 97, 29));
156
157 // Slider
158
159 CL_Color const clr_pup_1(72, 139, 60);
160 CL_Color const clr_pup_2(49, 111, 36);
161 CL_Color const clr_pup_3(34, 96, 21);
162
163 CL_Color const clr_slider_bgnd_norm(60, 120, 48);
164 CL_Color const clr_slider_bgnd_drag(60, 120, 48);
165
166 if(scrollbar_->is_dragging_slider())
167 {
168 CL_Display::fill_rect(
169 scrollbar_->get_client_area()->client_to_screen(rect),
170 clr_slider_bgnd_drag);
171 }
172 else
173 {
174 CL_Display::fill_rect(
175 scrollbar_->get_client_area()->client_to_screen(rect),
176 clr_slider_bgnd_norm);
177 }
178
179 CL_Color const clr_high_sld(102, 155, 87);
180 CL_Color const clr_low_sld(33, 83, 22);
181
182 }
183
184 void CL_ScrollBar_ZZ::on_get_preferred_size(CL_Size &size)
185 {
186 }
187
188 void CL_ScrollBar_ZZ::set_type(ScrollBarType scrollbar_type)
189 {
190 if (scrollbar_type_ != scrollbar_type)
191 {
192 if (scrollbar_type == type_listbox)
193 {
194 // TODO: add horizontal
195 CL_Button_ZZ * btn_style = (CL_Button_ZZ*)button_increase_->get_style();
196 if (btn_style)
197 {
198 btn_style->set_surfaces(
199 new CL_Surface("ListBox/scroll_btn_down", resources_),
200 new CL_Surface("ListBox/scroll_btn_down", resources_),
201 new CL_Surface("ListBox/scroll_btn_down", resources_),
202 new CL_Surface("ListBox/scroll_btn_down", resources_));
203 btn_style->set_button_type(CL_Button_ZZ::type_bitmap);
204 }
205 btn_style = (CL_Button_ZZ*)button_decrease_->get_style();
206 if (btn_style)
207 {
208 btn_style->set_surfaces(
209 new CL_Surface("ListBox/scroll_btn_up", resources_),
210 new CL_Surface("ListBox/scroll_btn_up", resources_),
211 new CL_Surface("ListBox/scroll_btn_up", resources_),
212 new CL_Surface("ListBox/scroll_btn_up", resources_));
213 btn_style->set_button_type(CL_Button_ZZ::type_bitmap);
214 }
215 }
216 else
217 if (scrollbar_type_ == type_normal)
218 {
219 CL_Rect rect_decrease;
220 CL_Rect rect_increase;
221 if (scrollbar_type_ == type_normal)
222 {
223 if (scrollbar_->is_vertical())
224 {
225 int button_size = scrollbar_->get_width();
226 rect_decrease = CL_Rect(0, 0, button_size, button_size);
227 rect_increase = CL_Rect(0, scrollbar_->get_height() - button_size, button_size, scrollbar_->get_height());
228 }
229 else
230 {
231 int button_size = scrollbar_->get_height();
232 rect_decrease = CL_Rect(0, 0, button_size, button_size);
233 rect_increase = CL_Rect(scrollbar_->get_width() - button_size, 0, scrollbar_->get_width(), button_size);
234 }
235 CL_Button_ZZ * btn_style = (CL_Button_ZZ*)button_decrease_->get_style();
236 if (btn_style)
237 btn_style->set_button_type(CL_Button_ZZ::type_text);
238 btn_style = (CL_Button_ZZ*)button_increase_->get_style();
239 if (btn_style)
240 btn_style->set_button_type(CL_Button_ZZ::type_text);
241 }
242 }
243 }
244 }