btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / interface / Spinner.cpp
blob04e6dd1481225865f47defcde1c2942f4199f759
1 /*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * John Scipione, jscipione@gmail.com
7 */
10 #include <Spinner.h>
12 #include <stdint.h>
13 #include <stdlib.h>
15 #include <PropertyInfo.h>
16 #include <String.h>
17 #include <TextView.h>
20 static property_info sProperties[] = {
22 "MaxValue",
23 { B_GET_PROPERTY, 0 },
24 { B_DIRECT_SPECIFIER, 0 },
25 "Returns the maximum value of the spinner.",
27 { B_INT32_TYPE }
30 "MaxValue",
31 { B_SET_PROPERTY, 0 },
32 { B_DIRECT_SPECIFIER, 0},
33 "Sets the maximum value of the spinner.",
35 { B_INT32_TYPE }
39 "MinValue",
40 { B_GET_PROPERTY, 0 },
41 { B_DIRECT_SPECIFIER, 0 },
42 "Returns the minimum value of the spinner.",
44 { B_INT32_TYPE }
47 "MinValue",
48 { B_SET_PROPERTY, 0 },
49 { B_DIRECT_SPECIFIER, 0},
50 "Sets the minimum value of the spinner.",
52 { B_INT32_TYPE }
56 "Value",
57 { B_GET_PROPERTY, 0 },
58 { B_DIRECT_SPECIFIER, 0 },
59 "Returns the value of the spinner.",
61 { B_INT32_TYPE }
64 "Value",
65 { B_SET_PROPERTY, 0 },
66 { B_DIRECT_SPECIFIER, 0},
67 "Sets the value of the spinner.",
69 { B_INT32_TYPE }
72 { 0 }
76 // #pragma mark - BSpinner
79 BSpinner::BSpinner(BRect frame, const char* name, const char* label,
80 BMessage* message, uint32 resizingMode, uint32 flags)
82 BAbstractSpinner(frame, name, label, message, resizingMode, flags)
84 _InitObject();
88 BSpinner::BSpinner(const char* name, const char* label,
89 BMessage* message, uint32 flags)
91 BAbstractSpinner(name, label, message, flags)
93 _InitObject();
97 BSpinner::BSpinner(BMessage* data)
99 BAbstractSpinner(data)
101 _InitObject();
103 if (data->FindInt32("_min", &fMinValue) != B_OK)
104 fMinValue = INT32_MIN;
106 if (data->FindInt32("_max", &fMaxValue) != B_OK)
107 fMaxValue = INT32_MAX;
109 if (data->FindInt32("_val", &fValue) != B_OK)
110 fValue = 0;
114 BSpinner::~BSpinner()
119 BArchivable*
120 BSpinner::Instantiate(BMessage* data)
122 if (validate_instantiation(data, "Spinner"))
123 return new BSpinner(data);
125 return NULL;
129 status_t
130 BSpinner::Archive(BMessage* data, bool deep) const
132 status_t status = BAbstractSpinner::Archive(data, deep);
133 data->AddString("class", "Spinner");
135 if (status == B_OK)
136 status = data->AddInt32("_min", fMinValue);
138 if (status == B_OK)
139 status = data->AddInt32("_max", fMaxValue);
141 if (status == B_OK)
142 status = data->AddInt32("_val", fValue);
144 return status;
148 status_t
149 BSpinner::GetSupportedSuites(BMessage* message)
151 message->AddString("suites", "suite/vnd.Haiku-intenger-spinner");
153 BPropertyInfo prop_info(sProperties);
154 message->AddFlat("messages", &prop_info);
156 return BView::GetSupportedSuites(message);
160 void
161 BSpinner::AttachedToWindow()
163 SetValue(fValue);
165 BAbstractSpinner::AttachedToWindow();
169 void
170 BSpinner::Decrement()
172 SetValue(Value() - 1);
176 void
177 BSpinner::Increment()
179 SetValue(Value() + 1);
183 void
184 BSpinner::SetEnabled(bool enable)
186 if (IsEnabled() == enable)
187 return;
189 SetIncrementEnabled(enable && Value() < fMaxValue);
190 SetDecrementEnabled(enable && Value() > fMinValue);
192 BAbstractSpinner::SetEnabled(enable);
196 void
197 BSpinner::SetMinValue(int32 min)
199 fMinValue = min;
200 if (fValue < fMinValue)
201 SetValue(fMinValue);
205 void
206 BSpinner::SetMaxValue(int32 max)
208 fMaxValue = max;
209 if (fValue > fMaxValue)
210 SetValue(fMaxValue);
214 void
215 BSpinner::Range(int32* min, int32* max)
217 *min = fMinValue;
218 *max = fMaxValue;
222 void
223 BSpinner::SetRange(int32 min, int32 max)
225 SetMinValue(min);
226 SetMaxValue(max);
230 void
231 BSpinner::SetValue(int32 value)
233 // clip to range
234 if (value < fMinValue)
235 value = fMinValue;
236 else if (value > fMaxValue)
237 value = fMaxValue;
239 // update the text view
240 BString valueString;
241 valueString << value;
242 TextView()->SetText(valueString.String());
244 // update the up and down arrows
245 SetIncrementEnabled(IsEnabled() && value < fMaxValue);
246 SetDecrementEnabled(IsEnabled() && value > fMinValue);
248 if (value == fValue)
249 return;
251 fValue = value;
252 ValueChanged();
254 Invoke();
255 Invalidate();
259 void
260 BSpinner::SetValueFromText()
262 SetValue(atol(TextView()->Text()));
266 // #pragma mark - BSpinner private methods
269 void
270 BSpinner::_InitObject()
272 fMinValue = INT32_MIN;
273 fMaxValue = INT32_MAX;
274 fValue = 0;
276 TextView()->SetAlignment(B_ALIGN_RIGHT);
277 for (uint32 c = 0; c <= 42; c++)
278 TextView()->DisallowChar(c);
280 TextView()->DisallowChar(',');
282 for (uint32 c = 46; c <= 47; c++)
283 TextView()->DisallowChar(c);
285 for (uint32 c = 58; c <= 127; c++)
286 TextView()->DisallowChar(c);
290 // FBC padding
292 void BSpinner::_ReservedSpinner20() {}
293 void BSpinner::_ReservedSpinner19() {}
294 void BSpinner::_ReservedSpinner18() {}
295 void BSpinner::_ReservedSpinner17() {}
296 void BSpinner::_ReservedSpinner16() {}
297 void BSpinner::_ReservedSpinner15() {}
298 void BSpinner::_ReservedSpinner14() {}
299 void BSpinner::_ReservedSpinner13() {}
300 void BSpinner::_ReservedSpinner12() {}
301 void BSpinner::_ReservedSpinner11() {}
302 void BSpinner::_ReservedSpinner10() {}
303 void BSpinner::_ReservedSpinner9() {}
304 void BSpinner::_ReservedSpinner8() {}
305 void BSpinner::_ReservedSpinner7() {}
306 void BSpinner::_ReservedSpinner6() {}
307 void BSpinner::_ReservedSpinner5() {}
308 void BSpinner::_ReservedSpinner4() {}
309 void BSpinner::_ReservedSpinner3() {}
310 void BSpinner::_ReservedSpinner2() {}
311 void BSpinner::_ReservedSpinner1() {}