This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSliderTextControl.cpp
More file actions
218 lines (186 loc) · 5.49 KB
/
SliderTextControl.cpp
File metadata and controls
218 lines (186 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright 2015-2023 Stefano Ceccherini <stefano.ceccherini@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "SliderTextControl.h"
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <MessageRunner.h>
#include <Slider.h>
#include <String.h>
#include <StringView.h>
#include <algorithm>
#include <cstdlib>
const int32 kTextControlMessage = '9TCM';
const int32 kTextControlRunnerMessage = '9RTC';
const int32 kSliderModificationMessage = '9SMM';
class SizeSlider : public BSlider {
public:
SizeSlider(const char* name, const char* label,
BMessage* message, int32 minValue,
int32 maxValue, int32 stepValue,
orientation posture,
thumb_style thumbType = B_BLOCK_THUMB,
uint32 flags = B_NAVIGABLE | B_WILL_DRAW
| B_FRAME_EVENTS);
virtual void SetValue(int32 value);
private:
int32 fStep;
};
SliderTextControl::SliderTextControl(const char* name, const char* label,
BMessage* message, int32 minValue,
int32 maxValue, int32 stepValue, const char* unit,
orientation posture,
thumb_style thumbType,
uint32 flags)
:
BControl(name, label, message, flags),
fSizeSlider(NULL),
fSizeTextControl(NULL),
fTextModificationRunner(NULL),
fWhat(message->what)
{
fSizeSlider = new SizeSlider("size_slider", label,
new BMessage(*message), minValue, maxValue, stepValue, B_HORIZONTAL);
fSizeSlider->SetModificationMessage(new BMessage(kSliderModificationMessage));
fSizeSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
fSizeSlider->SetHashMarkCount(8);
BString minLabel;
minLabel << minValue << " " << unit;
BString maxLabel;
maxLabel << maxValue << " " << unit;
fSizeSlider->SetLimitLabels(minLabel.String(), maxLabel.String());
fSizeTextControl = new BTextControl("", "", new BMessage(kTextControlMessage));
fTextLabel = new BStringView("label", unit);
BLayoutBuilder::Group<>(this)
.AddGroup(B_HORIZONTAL, 1)
.Add(fSizeSlider, 20)
.AddGroup(B_VERTICAL, 1)
.AddStrut(1)
.Add(fSizeTextControl)
.End()
.Add(fTextLabel)
.End();
fSizeTextControl->SetModificationMessage(new BMessage(kTextControlMessage));
fSizeTextControl->SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER,
B_ALIGN_TOP));
fSizeTextControl->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
}
void
SliderTextControl::AttachedToWindow()
{
fSizeTextControl->SetTarget(this);
fSizeSlider->SetTarget(this);
}
void
SliderTextControl::MessageReceived(BMessage* message)
{
if (message->what == fWhat) {
BString sizeString;
sizeString << fSizeSlider->Value();
fSizeTextControl->SetModificationMessage(NULL);
fSizeTextControl->SetText(sizeString);
BControl::SetValue(fSizeSlider->Value());
fSizeTextControl->SetModificationMessage(new BMessage(kTextControlMessage));
Window()->PostMessage(message, Target());
return;
}
switch (message->what) {
case kTextControlMessage:
{
if (fTextModificationRunner == NULL) {
BMessenger thisMessenger(this);
fTextModificationRunner = new BMessageRunner(thisMessenger,
new BMessage(kTextControlRunnerMessage), 1000000, 1);
} else
fTextModificationRunner->SetInterval(1000000);
break;
}
case kTextControlRunnerMessage:
{
// Apply limits
int32 value = atoi(fSizeTextControl->TextView()->Text());
int32 min;
int32 max;
fSizeSlider->GetLimits(&min, &max);
value = std::max(value, min);
value = std::min(value, max);
BControl::SetValue(value);
fSizeSlider->SetValue(value);
BString text;
text << value;
if (::strcmp(text, fSizeTextControl->Text()) != 0) {
// Avoid sending the modification message
fSizeTextControl->SetModificationMessage(NULL);
fSizeTextControl->SetText(text.String());
fSizeTextControl->SetModificationMessage(new BMessage(kTextControlMessage));
}
BMessage whatMessage(fWhat);
whatMessage.AddInt32("be:value", value);
Window()->PostMessage(&whatMessage, Target());
delete fTextModificationRunner;
fTextModificationRunner = NULL;
break;
}
case kSliderModificationMessage:
{
// Update text in real time
BString sizeString;
sizeString << (int32)fSizeSlider->Value();
fSizeTextControl->SetModificationMessage(NULL);
fSizeTextControl->SetText(sizeString);
fSizeTextControl->SetModificationMessage(new BMessage(kTextControlMessage));
break;
}
default:
BControl::MessageReceived(message);
break;
}
}
void
SliderTextControl::SetValue(int32 value)
{
fSizeSlider->SetValue(value);
BString sizeString;
sizeString << (int32)fSizeSlider->Value();
fSizeTextControl->SetText(sizeString);
BControl::SetValue(value);
}
/* virtual */
void
SliderTextControl::SetEnabled(bool enable)
{
BControl::SetEnabled(enable);
fSizeSlider->SetEnabled(enable);
fSizeTextControl->SetEnabled(enable);
}
// SizeSlider
SizeSlider::SizeSlider(const char* name, const char* label,
BMessage* message, int32 minValue,
int32 maxValue, int32 stepValue, orientation posture,
thumb_style thumbType,
uint32 flags)
:
BSlider(name, label, message, minValue, maxValue, posture, thumbType, flags),
fStep(stepValue)
{
}
/* virtual */
void
SizeSlider::SetValue(int32 value)
{
int32 max;
int32 min;
GetLimits(&min, &max);
const int32 numValues = 1 + (max - min) / fStep;
int32 validValues[numValues];
for (int32 i = 0; i < numValues; i++)
validValues[i] = min + fStep * i;
for (int32 i = 0; i < numValues - 1; i++) {
if (value > validValues[i] && value < validValues[i + 1]) {
value = value > validValues[i] + (validValues[i + 1] - validValues[i]) / 2
? validValues[i + 1] : validValues[i];
}
}
BSlider::SetValue(value);
}