108c97b50SAxel Dörfler/*
21f424632SJohn Scipione * Copyright 2005-2014 Haiku Inc. All rights reserved.
308c97b50SAxel Dörfler * Distributed under the terms of the MIT License.
408c97b50SAxel Dörfler */
508c97b50SAxel Dörfler
608c97b50SAxel Dörfler
708c97b50SAxel Dörfler#include <ChannelControl.h>
8976f53a9SStefano Ceccherini#include <PropertyInfo.h>
9976f53a9SStefano Ceccherini
100b421b80SStefano Ceccherini#include <map>
110b421b80SStefano Ceccherini#include <string>
120b421b80SStefano Ceccherini
130b421b80SStefano Ceccherinistruct limit_label {
140b421b80SStefano Ceccherini	std::string min_label;
150b421b80SStefano Ceccherini	std::string max_label;
160b421b80SStefano Ceccherini};
170b421b80SStefano Ceccherini
180b421b80SStefano Ceccherinitypedef std::map<int32, limit_label> label_map;
19976f53a9SStefano Ceccherini
207730601aSStefano Ceccherinistatic property_info
21859c5ac3SStefano CeccherinisPropertyInfo[] = {
227730601aSStefano Ceccherini	{ "ChannelCount",
23f4fc3d62SJérôme Duval		{ B_GET_PROPERTY, B_SET_PROPERTY, 0 },
24f4fc3d62SJérôme Duval		{ B_DIRECT_SPECIFIER, 0 }, NULL, 0, { B_INT32_TYPE }
25f4fc3d62SJérôme Duval	},
261f424632SJohn Scipione
277730601aSStefano Ceccherini	{ "CurrentChannel",
28f4fc3d62SJérôme Duval		{ B_GET_PROPERTY, B_SET_PROPERTY, 0 },
291f424632SJohn Scipione		{ B_DIRECT_SPECIFIER, 0 }, NULL, 0, { B_INT32_TYPE }
30f4fc3d62SJérôme Duval	},
311f424632SJohn Scipione
327730601aSStefano Ceccherini	{ "MaxLimitLabel",
33f4fc3d62SJérôme Duval		{ B_GET_PROPERTY, B_SET_PROPERTY, 0 },
341f424632SJohn Scipione		{ B_DIRECT_SPECIFIER, 0 }, NULL, 0, { B_STRING_TYPE }
35f4fc3d62SJérôme Duval	},
361f424632SJohn Scipione
377730601aSStefano Ceccherini	{ "MinLimitLabel",
38f4fc3d62SJérôme Duval		{ B_GET_PROPERTY, B_SET_PROPERTY, 0 },
391f424632SJohn Scipione		{ B_DIRECT_SPECIFIER, 0 }, NULL, 0, { B_STRING_TYPE }
40f4fc3d62SJérôme Duval	},
411f424632SJohn Scipione
427730601aSStefano Ceccherini	{ 0 }
43976f53a9SStefano Ceccherini};
4408c97b50SAxel Dörfler
4508c97b50SAxel Dörfler
461f424632SJohn ScipioneBChannelControl::BChannelControl(BRect frame, const char* name,
471f424632SJohn Scipione	const char* label, BMessage* model, int32 channel_count,
481f424632SJohn Scipione	uint32 resizingMode, uint32 flags)
491f424632SJohn Scipione	:
501f424632SJohn Scipione	BControl(frame, name, label, model, resizingMode, flags),
51422c0791SStefano Ceccherini	fChannelCount(channel_count),
52422c0791SStefano Ceccherini	fCurrentChannel(0),
53422c0791SStefano Ceccherini	fChannelMin(NULL),
54422c0791SStefano Ceccherini	fChannelMax(NULL),
55422c0791SStefano Ceccherini	fChannelValues(NULL),
56422c0791SStefano Ceccherini	fMultiLabels(NULL),
571c3a6ab0SStefano Ceccherini	fModificationMsg(NULL)
581c3a6ab0SStefano Ceccherini{
59422c0791SStefano Ceccherini	fChannelMin = new int32[channel_count];
60422c0791SStefano Ceccherini	memset(fChannelMin, 0, sizeof(int32) * channel_count);
611f424632SJohn Scipione
62422c0791SStefano Ceccherini	fChannelMax = new int32[channel_count];
6320c7c2f9SStefano Ceccherini	for (int32 i = 0; i < channel_count; i++)
6420c7c2f9SStefano Ceccherini		fChannelMax[i] = 100;
6520c7c2f9SStefano Ceccherini
66422c0791SStefano Ceccherini	fChannelValues = new int32[channel_count];
67422c0791SStefano Ceccherini	memset(fChannelValues, 0, sizeof(int32) * channel_count);
680b421b80SStefano Ceccherini
690b421b80SStefano Ceccherini	fMultiLabels = (void*)new label_map;
7008c97b50SAxel Dörfler}
7108c97b50SAxel Dörfler
7208c97b50SAxel Dörfler
731f424632SJohn ScipioneBChannelControl::BChannelControl(const char* name, const char* label,
741f424632SJohn Scipione	BMessage* model, int32 channelCount, uint32 flags)
751f424632SJohn Scipione	:
761f424632SJohn Scipione	BControl(name, label, model, flags),
771f424632SJohn Scipione	fChannelCount(channelCount),
78a0b1d631SStephan Aßmus	fCurrentChannel(0),
79a0b1d631SStephan Aßmus	fChannelMin(NULL),
80a0b1d631SStephan Aßmus	fChannelMax(NULL),
81a0b1d631SStephan Aßmus	fChannelValues(NULL),
82a0b1d631SStephan Aßmus	fMultiLabels(NULL),
83a0b1d631SStephan Aßmus	fModificationMsg(NULL)
84a0b1d631SStephan Aßmus{
851f424632SJohn Scipione	fChannelMin = new int32[channelCount];
861f424632SJohn Scipione	memset(fChannelMin, 0, sizeof(int32) * channelCount);
87a0b1d631SStephan Aßmus
881f424632SJohn Scipione	fChannelMax = new int32[channelCount];
891f424632SJohn Scipione	for (int32 i = 0; i < channelCount; i++)
90a0b1d631SStephan Aßmus		fChannelMax[i] = 100;
91a0b1d631SStephan Aßmus
921f424632SJohn Scipione	fChannelValues = new int32[channelCount];
931f424632SJohn Scipione	memset(fChannelValues, 0, sizeof(int32) * channelCount);
940b421b80SStefano Ceccherini
950b421b80SStefano Ceccherini	fMultiLabels = (void*)new label_map;
96a0b1d631SStephan Aßmus}
97a0b1d631SStephan Aßmus
98a0b1d631SStephan Aßmus
991f424632SJohn ScipioneBChannelControl::BChannelControl(BMessage* archive)
1001f424632SJohn Scipione	:
1011f424632SJohn Scipione	BControl(archive),
102016e74b2SStefano Ceccherini	fChannelCount(0),
103016e74b2SStefano Ceccherini	fCurrentChannel(0),
104016e74b2SStefano Ceccherini	fChannelMin(NULL),
105016e74b2SStefano Ceccherini	fChannelMax(NULL),
106016e74b2SStefano Ceccherini	fChannelValues(NULL),
107016e74b2SStefano Ceccherini	fMultiLabels(NULL),
108016e74b2SStefano Ceccherini	fModificationMsg(NULL)
10908c97b50SAxel Dörfler{
110016e74b2SStefano Ceccherini	archive->FindInt32("be:_m_channel_count", &fChannelCount);
111016e74b2SStefano Ceccherini	archive->FindInt32("be:_m_value_channel", &fCurrentChannel);
1121f424632SJohn Scipione
113016e74b2SStefano Ceccherini	if (fChannelCount > 0) {
114016e74b2SStefano Ceccherini		fChannelMin = new int32[fChannelCount];
115016e74b2SStefano Ceccherini		memset(fChannelMin, 0, sizeof(int32) * fChannelCount);
1161f424632SJohn Scipione
117016e74b2SStefano Ceccherini		fChannelMax = new int32[fChannelCount];
118a85419eeSStefano Ceccherini		for (int32 i = 0; i < fChannelCount; i++)
119a85419eeSStefano Ceccherini			fChannelMax[i] = 100;
1201f424632SJohn Scipione
121016e74b2SStefano Ceccherini		fChannelValues = new int32[fChannelCount];
122016e74b2SStefano Ceccherini		memset(fChannelValues, 0, sizeof(int32) * fChannelCount);
1231f424632SJohn Scipione
124016e74b2SStefano Ceccherini		for (int32 c = 0; c < fChannelCount; c++) {
125016e74b2SStefano Ceccherini			archive->FindInt32("be:_m_channel_min", c, &fChannelMin[c]);
126016e74b2SStefano Ceccherini			archive->FindInt32("be:_m_channel_max", c, &fChannelMax[c]);
127016e74b2SStefano Ceccherini			archive->FindInt32("be:_m_channel_val", c, &fChannelValues[c]);
128016e74b2SStefano Ceccherini		}
129016e74b2SStefano Ceccherini	}
1301f424632SJohn Scipione
1311f424632SJohn Scipione	const char* label = NULL;
132a85419eeSStefano Ceccherini	if (archive->FindString("be:_m_min_label", &label) == B_OK)
133016e74b2SStefano Ceccherini		fMinLabel = label;
1341f424632SJohn Scipione
135016e74b2SStefano Ceccherini	if (archive->FindString("be:_m_max_label", &label) == B_OK)
136016e74b2SStefano Ceccherini		fMaxLabel = label;
1371f424632SJohn Scipione
1381f424632SJohn Scipione	BMessage* modificationMessage = new BMessage;
139016e74b2SStefano Ceccherini	if (archive->FindMessage("_mod_msg", modificationMessage) == B_OK)
140016e74b2SStefano Ceccherini		fModificationMsg = modificationMessage;
141016e74b2SStefano Ceccherini	else
142016e74b2SStefano Ceccherini		delete modificationMessage;
1430b421b80SStefano Ceccherini
1440b421b80SStefano Ceccherini	fMultiLabels = (void*)new label_map;
14508c97b50SAxel Dörfler}
14608c97b50SAxel Dörfler
14708c97b50SAxel Dörfler
14808c97b50SAxel DörflerBChannelControl::~BChannelControl()
14908c97b50SAxel Dörfler{
150422c0791SStefano Ceccherini	delete[] fChannelMin;
151422c0791SStefano Ceccherini	delete[] fChannelMax;
152422c0791SStefano Ceccherini	delete[] fChannelValues;
153016e74b2SStefano Ceccherini	delete fModificationMsg;
1540b421b80SStefano Ceccherini	delete reinterpret_cast<label_map*>(fMultiLabels);
15508c97b50SAxel Dörfler}
15608c97b50SAxel Dörfler
15708c97b50SAxel Dörfler
15808c97b50SAxel Dörflerstatus_t
1591f424632SJohn ScipioneBChannelControl::Archive(BMessage* data, bool deep) const
16008c97b50SAxel Dörfler{
1611f424632SJohn Scipione	status_t status = BControl::Archive(data, deep);
1622c323083SStefano Ceccherini	if (status == B_OK)
1631f424632SJohn Scipione		status = data->AddInt32("be:_m_channel_count", fChannelCount);
1641f424632SJohn Scipione
165016e74b2SStefano Ceccherini	if (status == B_OK)
1661f424632SJohn Scipione		status = data->AddInt32("be:_m_value_channel", fCurrentChannel);
1671f424632SJohn Scipione
1682c323083SStefano Ceccherini	if (status == B_OK)
1691f424632SJohn Scipione		status = data->AddString("be:_m_min_label", fMinLabel.String());
1701f424632SJohn Scipione
1712c323083SStefano Ceccherini	if (status == B_OK)
1721f424632SJohn Scipione		status = data->AddString("be:_m_max_label", fMaxLabel.String());
1731f424632SJohn Scipione
1742c323083SStefano Ceccherini	if (status == B_OK && fChannelValues != NULL
1752c323083SStefano Ceccherini		&& fChannelMax != NULL && fChannelMin != NULL) {
1762c323083SStefano Ceccherini		for (int32 i = 0; i < fChannelCount; i++) {
1771f424632SJohn Scipione			status = data->AddInt32("be:_m_channel_min", fChannelMin[i]);
1782c323083SStefano Ceccherini			if (status < B_OK)
1792c323083SStefano Ceccherini				break;
1801f424632SJohn Scipione
1811f424632SJohn Scipione			status = data->AddInt32("be:_m_channel_max", fChannelMax[i]);
1822c323083SStefano Ceccherini			if (status < B_OK)
1832c323083SStefano Ceccherini				break;
1841f424632SJohn Scipione
1851f424632SJohn Scipione			status = data->AddInt32("be:_m_channel_val", fChannelValues[i]);
1862c323083SStefano Ceccherini			if (status < B_OK)
1872c323083SStefano Ceccherini				break;
1882c323083SStefano Ceccherini		}
1892c323083SStefano Ceccherini	}
1902c323083SStefano Ceccherini
1912c323083SStefano Ceccherini	return status;
19208c97b50SAxel Dörfler}
19308c97b50SAxel Dörfler
19408c97b50SAxel Dörfler
19508c97b50SAxel Dörflervoid
1961f424632SJohn ScipioneBChannelControl::FrameResized(float newWidth, float newHeight)
19708c97b50SAxel Dörfler{
1981f424632SJohn Scipione	BView::FrameResized(newWidth, newHeight);
19908c97b50SAxel Dörfler}
20008c97b50SAxel Dörfler
20108c97b50SAxel Dörfler
20208c97b50SAxel Dörflervoid
2031f424632SJohn ScipioneBChannelControl::SetFont(const BFont* font, uint32 mask)
20408c97b50SAxel Dörfler{
2051c3a6ab0SStefano Ceccherini	BView::SetFont(font, mask);
20608c97b50SAxel Dörfler}
20708c97b50SAxel Dörfler
20808c97b50SAxel Dörfler
20908c97b50SAxel Dörflervoid
21008c97b50SAxel DörflerBChannelControl::AttachedToWindow()
21108c97b50SAxel Dörfler{
2121c3a6ab0SStefano Ceccherini	BControl::AttachedToWindow();
21308c97b50SAxel Dörfler}
21408c97b50SAxel Dörfler
21508c97b50SAxel Dörfler
21608c97b50SAxel Dörflervoid
21708c97b50SAxel DörflerBChannelControl::DetachedFromWindow()
21808c97b50SAxel Dörfler{
2191c3a6ab0SStefano Ceccherini	BControl::DetachedFromWindow();
22008c97b50SAxel Dörfler}
22108c97b50SAxel Dörfler
22208c97b50SAxel Dörfler
22308c97b50SAxel Dörflervoid
22408c97b50SAxel DörflerBChannelControl::ResizeToPreferred()
22508c97b50SAxel Dörfler{
22624ab95eeSJérôme Duval	BControl::ResizeToPreferred();
22708c97b50SAxel Dörfler}
22808c97b50SAxel Dörfler
22908c97b50SAxel Dörfler
23008c97b50SAxel Dörflervoid
2311f424632SJohn ScipioneBChannelControl::MessageReceived(BMessage* message)
23208c97b50SAxel Dörfler{
2331c3a6ab0SStefano Ceccherini	BControl::MessageReceived(message);
23408c97b50SAxel Dörfler}
23508c97b50SAxel Dörfler
23608c97b50SAxel Dörfler
2371f424632SJohn ScipioneBHandler*
2381f424632SJohn ScipioneBChannelControl::ResolveSpecifier(BMessage* message, int32 index,
2391f424632SJohn Scipione	BMessage* specifier, int32 what, const char* property)
24008c97b50SAxel Dörfler{
2411f424632SJohn Scipione	BHandler* target = this;
2422c323083SStefano Ceccherini	BPropertyInfo propertyInfo(sPropertyInfo);
2431f424632SJohn Scipione	if (propertyInfo.FindMatch(message, index, specifier, what, property)
2441f424632SJohn Scipione			< B_OK) {
2451f424632SJohn Scipione		target = BControl::ResolveSpecifier(message, index, specifier,
2461f424632SJohn Scipione			what, property);
2471f424632SJohn Scipione	}
2481f424632SJohn Scipione
2492c323083SStefano Ceccherini	return target;
25008c97b50SAxel Dörfler}
25108c97b50SAxel Dörfler
25208c97b50SAxel Dörfler
25308c97b50SAxel Dörflerstatus_t
2541f424632SJohn ScipioneBChannelControl::GetSupportedSuites(BMessage* data)
25508c97b50SAxel Dörfler{
256976f53a9SStefano Ceccherini	if (data == NULL)
257976f53a9SStefano Ceccherini		return B_BAD_VALUE;
2581f424632SJohn Scipione
259f4fc3d62SJérôme Duval	status_t err = data->AddString("suites", "suite/vnd.Be-channel-control");
260976f53a9SStefano Ceccherini
261976f53a9SStefano Ceccherini	BPropertyInfo propertyInfo(sPropertyInfo);
262f4fc3d62SJérôme Duval	if (err == B_OK)
263f4fc3d62SJérôme Duval		err = data->AddFlat("messages", &propertyInfo);
264976f53a9SStefano Ceccherini
265f4fc3d62SJérôme Duval	if (err == B_OK)
266f4fc3d62SJérôme Duval		return BControl::GetSupportedSuites(data);
2671f424632SJohn Scipione
268f4fc3d62SJérôme Duval	return err;
26908c97b50SAxel Dörfler}
27008c97b50SAxel Dörfler
27108c97b50SAxel Dörfler
27208c97b50SAxel Dörflervoid
2731f424632SJohn ScipioneBChannelControl::SetModificationMessage(BMessage* message)
27408c97b50SAxel Dörfler{
2751c3a6ab0SStefano Ceccherini	delete fModificationMsg;
2761c3a6ab0SStefano Ceccherini	fModificationMsg = message;
27708c97b50SAxel Dörfler}
27808c97b50SAxel Dörfler
27908c97b50SAxel Dörfler
2801f424632SJohn ScipioneBMessage*
28108c97b50SAxel DörflerBChannelControl::ModificationMessage() const
28208c97b50SAxel Dörfler{
2831c3a6ab0SStefano Ceccherini	return fModificationMsg;
28408c97b50SAxel Dörfler}
28508c97b50SAxel Dörfler
28608c97b50SAxel Dörfler
28708c97b50SAxel Dörflerstatus_t
2881f424632SJohn ScipioneBChannelControl::Invoke(BMessage* message)
28908c97b50SAxel Dörfler{
290fd873802SStefano Ceccherini	bool notify = false;
291fd873802SStefano Ceccherini	BMessage invokeMessage(InvokeKind(&notify));
292fd873802SStefano Ceccherini
2931f424632SJohn Scipione	if (message != NULL)
2941f424632SJohn Scipione		invokeMessage = *message;
295fd873802SStefano Ceccherini	else if (Message() != NULL)
296fd873802SStefano Ceccherini		invokeMessage = *Message();
297fd873802SStefano Ceccherini
298fd873802SStefano Ceccherini	invokeMessage.AddInt32("be:current_channel", fCurrentChannel);
299fd873802SStefano Ceccherini
300fd873802SStefano Ceccherini	return BControl::Invoke(&invokeMessage);
30108c97b50SAxel Dörfler}
30208c97b50SAxel Dörfler
30308c97b50SAxel Dörfler
30408c97b50SAxel Dörflerstatus_t
3051f424632SJohn ScipioneBChannelControl::InvokeChannel(BMessage* message, int32 fromChannel,
3061f424632SJohn Scipione	int32 channelCount, const bool* _mask)
30708c97b50SAxel Dörfler{
3082c323083SStefano Ceccherini	bool notify = false;
3092c323083SStefano Ceccherini	BMessage invokeMessage(InvokeKind(&notify));
3102c323083SStefano Ceccherini
3111f424632SJohn Scipione	if (message != NULL)
3121f424632SJohn Scipione		invokeMessage = *message;
3132c323083SStefano Ceccherini	else if (Message() != NULL)
3142c323083SStefano Ceccherini		invokeMessage = *Message();
3151f424632SJohn Scipione
3162c323083SStefano Ceccherini	invokeMessage.AddInt32("be:current_channel", fCurrentChannel);
317789b86ecSJérôme Duval	if (channelCount < 0)
3182c323083SStefano Ceccherini		channelCount = fChannelCount - fromChannel;
3191f424632SJohn Scipione
320789b86ecSJérôme Duval	for (int32 i = 0; i < channelCount; i++) {
3211f424632SJohn Scipione		invokeMessage.AddInt32("be:channel_value",
3221f424632SJohn Scipione			fChannelValues[fromChannel + i]);
3231f424632SJohn Scipione		invokeMessage.AddBool("be:channel_changed", _mask ? _mask[i] : true);
3242c323083SStefano Ceccherini	}
3251f424632SJohn Scipione
3262c323083SStefano Ceccherini	return BControl::Invoke(&invokeMessage);
32708c97b50SAxel Dörfler}
32808c97b50SAxel Dörfler
32908c97b50SAxel Dörfler
33008c97b50SAxel Dörflerstatus_t
3311f424632SJohn ScipioneBChannelControl::InvokeNotifyChannel(BMessage* message, uint32 kind,
3321f424632SJohn Scipione	int32 fromChannel, int32 channelCount, const bool* _mask)
33308c97b50SAxel Dörfler{
3341c3a6ab0SStefano Ceccherini	BeginInvokeNotify(kind);
3351f424632SJohn Scipione	status_t status = InvokeChannel(message, fromChannel, channelCount, _mask);
3361c3a6ab0SStefano Ceccherini	EndInvokeNotify();
3371f424632SJohn Scipione
3381c3a6ab0SStefano Ceccherini	return status;
33908c97b50SAxel Dörfler}
34008c97b50SAxel Dörfler
34108c97b50SAxel Dörfler
34208c97b50SAxel Dörflervoid
34308c97b50SAxel DörflerBChannelControl::SetValue(int32 value)
34408c97b50SAxel Dörfler{
3451c3a6ab0SStefano Ceccherini	// Get real
346422c0791SStefano Ceccherini	if (value > fChannelMax[fCurrentChannel])
347422c0791SStefano Ceccherini		value = fChannelMax[fCurrentChannel];
3481f424632SJohn Scipione
349a8e3f157SStefano Ceccherini	if (value < fChannelMin[fCurrentChannel])
350422c0791SStefano Ceccherini		value = fChannelMin[fCurrentChannel];
3511f424632SJohn Scipione
352422c0791SStefano Ceccherini	if (value != fChannelValues[fCurrentChannel]) {
353422c0791SStefano Ceccherini		StuffValues(fCurrentChannel, 1, &value);
3541c3a6ab0SStefano Ceccherini		BControl::SetValue(value);
3551c3a6ab0SStefano Ceccherini	}
35608c97b50SAxel Dörfler}
35708c97b50SAxel Dörfler
35808c97b50SAxel Dörfler
35908c97b50SAxel Dörflerstatus_t
36008c97b50SAxel DörflerBChannelControl::SetCurrentChannel(int32 channel)
36108c97b50SAxel Dörfler{
362422c0791SStefano Ceccherini	if (channel < 0 || channel >= fChannelCount)
3631c3a6ab0SStefano Ceccherini		return B_BAD_INDEX;
3641f424632SJohn Scipione
365422c0791SStefano Ceccherini	if (channel != fCurrentChannel) {
366422c0791SStefano Ceccherini		fCurrentChannel = channel;
367422c0791SStefano Ceccherini		BControl::SetValue(fChannelValues[fCurrentChannel]);
3681c3a6ab0SStefano Ceccherini	}
3691f424632SJohn Scipione
3701c3a6ab0SStefano Ceccherini	return B_OK;
37108c97b50SAxel Dörfler}
37208c97b50SAxel Dörfler
37308c97b50SAxel Dörfler
37408c97b50SAxel Dörflerint32
37508c97b50SAxel DörflerBChannelControl::CurrentChannel() const
37608c97b50SAxel Dörfler{
377422c0791SStefano Ceccherini	return fCurrentChannel;
37808c97b50SAxel Dörfler}
37908c97b50SAxel Dörfler
38008c97b50SAxel Dörfler
38108c97b50SAxel Dörflerint32
38208c97b50SAxel DörflerBChannelControl::CountChannels() const
38308c97b50SAxel Dörfler{
384422c0791SStefano Ceccherini	return fChannelCount;
38508c97b50SAxel Dörfler}
38608c97b50SAxel Dörfler
38708c97b50SAxel Dörfler
38808c97b50SAxel Dörflerstatus_t
38908c97b50SAxel DörflerBChannelControl::SetChannelCount(int32 channel_count)
39008c97b50SAxel Dörfler{
3911c3a6ab0SStefano Ceccherini	if (channel_count < 0 || channel_count >= MaxChannelCount())
3921c3a6ab0SStefano Ceccherini		return B_BAD_VALUE;
393859c5ac3SStefano Ceccherini
3941c3a6ab0SStefano Ceccherini	// TODO: Currently we only grow the buffer. Test what BeOS does
395422c0791SStefano Ceccherini	if (channel_count > fChannelCount) {
3961f424632SJohn Scipione		int32* newMin = new int32[channel_count];
3971f424632SJohn Scipione		int32* newMax = new int32[channel_count];
3981f424632SJohn Scipione		int32* newVal = new int32[channel_count];
3991f424632SJohn Scipione
400422c0791SStefano Ceccherini		memcpy(newMin, fChannelMin, fChannelCount);
401422c0791SStefano Ceccherini		memcpy(newMax, fChannelMax, fChannelCount);
402422c0791SStefano Ceccherini		memcpy(newVal, fChannelValues, fChannelCount);
4031f424632SJohn Scipione
404422c0791SStefano Ceccherini		delete[] fChannelMin;
405422c0791SStefano Ceccherini		delete[] fChannelMax;
406422c0791SStefano Ceccherini		delete[] fChannelValues;
4071f424632SJohn Scipione
408422c0791SStefano Ceccherini		fChannelMin = newMin;
409422c0791SStefano Ceccherini		fChannelMax = newMax;
410422c0791SStefano Ceccherini		fChannelValues = newVal;
4111c3a6ab0SStefano Ceccherini	}
4121f424632SJohn Scipione
413422c0791SStefano Ceccherini	fChannelCount = channel_count;
4141f424632SJohn Scipione
4151c3a6ab0SStefano Ceccherini	return B_OK;
41608c97b50SAxel Dörfler}
41708c97b50SAxel Dörfler
41808c97b50SAxel Dörfler
41908c97b50SAxel Dörflerint32
42008c97b50SAxel DörflerBChannelControl::ValueFor(int32 channel) const
42108c97b50SAxel Dörfler{
4221c3a6ab0SStefano Ceccherini	int32 value = 0;
4231c3a6ab0SStefano Ceccherini	if (GetValue(&value, channel, 1) <= 0)
4241c3a6ab0SStefano Ceccherini		return -1;
4251f424632SJohn Scipione
4261c3a6ab0SStefano Ceccherini	return value;
42708c97b50SAxel Dörfler}
42808c97b50SAxel Dörfler
42908c97b50SAxel Dörfler
43008c97b50SAxel Dörflerint32
4311f424632SJohn ScipioneBChannelControl::GetValue(int32* outValues, int32 fromChannel,
43208c97b50SAxel Dörfler	int32 channelCount) const
43308c97b50SAxel Dörfler{
4341c3a6ab0SStefano Ceccherini	int32 i = 0;
4351c3a6ab0SStefano Ceccherini	for (i = 0; i < channelCount; i++)
436422c0791SStefano Ceccherini		outValues[i] = fChannelValues[fromChannel + i];
4371c3a6ab0SStefano Ceccherini
4381c3a6ab0SStefano Ceccherini	return i;
43908c97b50SAxel Dörfler}
44008c97b50SAxel Dörfler
44108c97b50SAxel Dörfler
44208c97b50SAxel Dörflerstatus_t
44308c97b50SAxel DörflerBChannelControl::SetValueFor(int32 channel, int32 value)
44408c97b50SAxel Dörfler{
4451c3a6ab0SStefano Ceccherini	return SetValue(channel, 1, &value);
44608c97b50SAxel Dörfler}
44708c97b50SAxel Dörfler
44808c97b50SAxel Dörfler
44908c97b50SAxel Dörflerstatus_t
45008c97b50SAxel DörflerBChannelControl::SetValue(int32 fromChannel, int32 channelCount,
4511f424632SJohn Scipione	const int32* values)
45208c97b50SAxel Dörfler{
4531f424632SJohn Scipione	return StuffValues(fromChannel, channelCount, values);
45408c97b50SAxel Dörfler}
45508c97b50SAxel Dörfler
45608c97b50SAxel Dörfler
45708c97b50SAxel Dörflerstatus_t
45808c97b50SAxel DörflerBChannelControl::SetAllValue(int32 values)
45908c97b50SAxel Dörfler{
4601f424632SJohn Scipione	int32* newValues = new int32[fChannelCount];
4615040785eSStefano Ceccherini	for (int32 i = 0; i < fChannelCount; i++) {
4625040785eSStefano Ceccherini		int32 limitedValue = max_c(values, MinLimitList()[i]);
4635040785eSStefano Ceccherini		limitedValue = min_c(limitedValue, MaxLimitList()[i]);
4641f424632SJohn Scipione
4655040785eSStefano Ceccherini		newValues[i] = limitedValue;
4665040785eSStefano Ceccherini	}
4671f424632SJohn Scipione
468a85419eeSStefano Ceccherini	delete[] fChannelValues;
46920c7c2f9SStefano Ceccherini	fChannelValues = newValues;
470789b86ecSJérôme Duval	BControl::SetValue(fChannelValues[fCurrentChannel]);
471789b86ecSJérôme Duval
47220c7c2f9SStefano Ceccherini	return B_OK;
47308c97b50SAxel Dörfler}
47408c97b50SAxel Dörfler
47508c97b50SAxel Dörfler
47608c97b50SAxel Dörflerstatus_t
47708c97b50SAxel DörflerBChannelControl::SetLimitsFor(int32 channel, int32 minimum, int32 maximum)
47808c97b50SAxel Dörfler{
479fd873802SStefano Ceccherini	return SetLimitsFor(channel, 1, &minimum, &maximum);
48008c97b50SAxel Dörfler}
48108c97b50SAxel Dörfler
48208c97b50SAxel Dörfler
48308c97b50SAxel Dörflerstatus_t
4841f424632SJohn ScipioneBChannelControl::GetLimitsFor(int32 channel, int32* minimum,
4851f424632SJohn Scipione	int32* maximum) const
48608c97b50SAxel Dörfler{
487fd873802SStefano Ceccherini	return GetLimitsFor(channel, 1, minimum, maximum);
48808c97b50SAxel Dörfler}
48908c97b50SAxel Dörfler
49008c97b50SAxel Dörfler
49108c97b50SAxel Dörflerstatus_t
49208c97b50SAxel DörflerBChannelControl::SetLimitsFor(int32 fromChannel, int32 channelCount,
4931f424632SJohn Scipione	const int32* minimum, const int32* maximum)
49408c97b50SAxel Dörfler{
49518108ef7SJérôme Duval	if (fromChannel + channelCount > CountChannels())
49618108ef7SJérôme Duval		channelCount = CountChannels() - fromChannel;
4971f424632SJohn Scipione
4981f424632SJohn Scipione	for (int i = 0; i < channelCount; i++) {
49918108ef7SJérôme Duval		if (minimum[i] > maximum[i])
50018108ef7SJérôme Duval			return B_BAD_VALUE;
5011f424632SJohn Scipione
50218108ef7SJérôme Duval		fChannelMin[fromChannel + i] = minimum[i];
50318108ef7SJérôme Duval		fChannelMax[fromChannel + i] = maximum[i];
50418108ef7SJérôme Duval		if (fChannelValues[fromChannel + i] < minimum[i])
50518108ef7SJérôme Duval			fChannelValues[fromChannel + i] = minimum[i];
50618108ef7SJérôme Duval		else if (fChannelValues[fromChannel + i] > maximum[i])
50718108ef7SJérôme Duval			fChannelValues[fromChannel + i] = maximum[i];
50818108ef7SJérôme Duval	}
5091f424632SJohn Scipione
51018108ef7SJérôme Duval	return B_OK;
51108c97b50SAxel Dörfler}
51208c97b50SAxel Dörfler
51308c97b50SAxel Dörfler
51408c97b50SAxel Dörflerstatus_t
51508c97b50SAxel DörflerBChannelControl::GetLimitsFor(int32 fromChannel, int32 channelCount,
5161f424632SJohn Scipione	int32* minimum, int32* maximum) const
51708c97b50SAxel Dörfler{
51818108ef7SJérôme Duval	if (minimum == NULL || maximum == NULL)
51918108ef7SJérôme Duval		return B_BAD_VALUE;
52018108ef7SJérôme Duval
52118108ef7SJérôme Duval	if (fChannelMin == NULL || fChannelMax == NULL)
52218108ef7SJérôme Duval		return B_ERROR;
52318108ef7SJérôme Duval	if (fromChannel + channelCount > CountChannels())
52418108ef7SJérôme Duval		channelCount = CountChannels() - fromChannel;
5251f424632SJohn Scipione
5261f424632SJohn Scipione	for (int i = 0; i < channelCount; i++) {
52718108ef7SJérôme Duval		minimum[i] = fChannelMin[fromChannel + i];
52818108ef7SJérôme Duval		maximum[i] = fChannelMax[fromChannel + i];
52918108ef7SJérôme Duval	}
5301f424632SJohn Scipione
53118108ef7SJérôme Duval	return B_OK;
53208c97b50SAxel Dörfler}
53308c97b50SAxel Dörfler
53408c97b50SAxel Dörfler
53508c97b50SAxel Dörflerstatus_t
53608c97b50SAxel DörflerBChannelControl::SetLimits(int32 minimum, int32 maximum)
53708c97b50SAxel Dörfler{
5387730601aSStefano Ceccherini	if (minimum > maximum)
5397730601aSStefano Ceccherini		return B_BAD_VALUE;
5401f424632SJohn Scipione
5417730601aSStefano Ceccherini	int32 numChannels = CountChannels();
5427730601aSStefano Ceccherini
5437730601aSStefano Ceccherini	for (int32 c = 0; c < numChannels; c++) {
54457735c40SMarcus Overhagen		fChannelMin[c] = minimum;
54557735c40SMarcus Overhagen		fChannelMax[c] = maximum;
54657735c40SMarcus Overhagen		if (fChannelValues[c] < minimum)
54757735c40SMarcus Overhagen			fChannelValues[c] = minimum;
54857735c40SMarcus Overhagen		else if (fChannelValues[c] > maximum)
54957735c40SMarcus Overhagen			fChannelValues[c] = maximum;
5507730601aSStefano Ceccherini	}
5517730601aSStefano Ceccherini
55257735c40SMarcus Overhagen	return B_OK;
55308c97b50SAxel Dörfler}
55408c97b50SAxel Dörfler
55508c97b50SAxel Dörfler
55608c97b50SAxel Dörflerstatus_t
5571f424632SJohn ScipioneBChannelControl::GetLimits(int32* outMinimum, int32* outMaximum) const
55808c97b50SAxel Dörfler{
5597730601aSStefano Ceccherini	if (outMinimum == NULL || outMaximum == NULL)
5607730601aSStefano Ceccherini		return B_BAD_VALUE;
5617730601aSStefano Ceccherini
5627730601aSStefano Ceccherini	if (fChannelMin == NULL || fChannelMax == NULL)
5637730601aSStefano Ceccherini		return B_ERROR;
5647730601aSStefano Ceccherini
5657730601aSStefano Ceccherini	int32 numChannels = CountChannels();
5667730601aSStefano Ceccherini	for (int32 c = 0; c < numChannels; c++) {
5677730601aSStefano Ceccherini		outMinimum[c] = fChannelMin[c];
5687730601aSStefano Ceccherini		outMaximum[c] = fChannelMax[c];
5697730601aSStefano Ceccherini	}
5707730601aSStefano Ceccherini
5717730601aSStefano Ceccherini	return B_OK;
57208c97b50SAxel Dörfler}
57308c97b50SAxel Dörfler
57408c97b50SAxel Dörfler
57508c97b50SAxel Dörflerstatus_t
5761f424632SJohn ScipioneBChannelControl::SetLimitLabels(const char* minLabel, const char* maxLabel)
57708c97b50SAxel Dörfler{
578fd873802SStefano Ceccherini	if (minLabel != fMinLabel)
579fd873802SStefano Ceccherini		fMinLabel = minLabel;
5801f424632SJohn Scipione
581fd873802SStefano Ceccherini	if (maxLabel != fMaxLabel)
582fd873802SStefano Ceccherini		fMaxLabel = maxLabel;
583fd873802SStefano Ceccherini
584fd873802SStefano Ceccherini	Invalidate();
585fd873802SStefano Ceccherini
586fd873802SStefano Ceccherini	return B_OK;
58708c97b50SAxel Dörfler}
58808c97b50SAxel Dörfler
58908c97b50SAxel Dörfler
5901f424632SJohn Scipioneconst char*
59108c97b50SAxel DörflerBChannelControl::MinLimitLabel() const
59208c97b50SAxel Dörfler{
593fd873802SStefano Ceccherini	return fMinLabel.String();
59408c97b50SAxel Dörfler}
59508c97b50SAxel Dörfler
59608c97b50SAxel Dörfler
5971f424632SJohn Scipioneconst char*
59808c97b50SAxel DörflerBChannelControl::MaxLimitLabel() const
59908c97b50SAxel Dörfler{
600fd873802SStefano Ceccherini	return fMaxLabel.String();
60108c97b50SAxel Dörfler}
60208c97b50SAxel Dörfler
60308c97b50SAxel Dörfler
60408c97b50SAxel Dörflerstatus_t
6051f424632SJohn ScipioneBChannelControl::SetLimitLabelsFor(int32 channel, const char* minLabel,
6061f424632SJohn Scipione	const char* maxLabel)
60708c97b50SAxel Dörfler{
6080b421b80SStefano Ceccherini	(*(label_map*)fMultiLabels)[channel].max_label = maxLabel;
6090b421b80SStefano Ceccherini	(*(label_map*)fMultiLabels)[channel].min_label = minLabel;
6100b421b80SStefano Ceccherini	return B_OK;
61108c97b50SAxel Dörfler}
61208c97b50SAxel Dörfler
61308c97b50SAxel Dörfler
61408c97b50SAxel Dörflerstatus_t
6151f424632SJohn ScipioneBChannelControl::SetLimitLabelsFor(int32 fromChannel, int32 channelCount,
6161f424632SJohn Scipione	const char* minLabel, const char* maxLabel)
61708c97b50SAxel Dörfler{
6180b421b80SStefano Ceccherini	for (int32 i = fromChannel; i < fromChannel + channelCount; i++) {
6190b421b80SStefano Ceccherini		SetLimitLabelsFor(i, minLabel, maxLabel);
6200b421b80SStefano Ceccherini	}
6210b421b80SStefano Ceccherini	return B_OK;
62208c97b50SAxel Dörfler}
62308c97b50SAxel Dörfler
62408c97b50SAxel Dörfler
6251f424632SJohn Scipioneconst char*
62608c97b50SAxel DörflerBChannelControl::MinLimitLabelFor(int32 channel) const
62708c97b50SAxel Dörfler{
6280b421b80SStefano Ceccherini	if (fMultiLabels != NULL) {
6290b421b80SStefano Ceccherini		label_map::const_iterator iter = ((label_map*)fMultiLabels)->find(channel);
6300b421b80SStefano Ceccherini		if (iter != ((label_map*)fMultiLabels)->end())
6310b421b80SStefano Ceccherini			return (*iter).second.min_label.c_str();
6320b421b80SStefano Ceccherini	}
63308c97b50SAxel Dörfler	return NULL;
63408c97b50SAxel Dörfler}
63508c97b50SAxel Dörfler
63608c97b50SAxel Dörfler
6371f424632SJohn Scipioneconst char*
63808c97b50SAxel DörflerBChannelControl::MaxLimitLabelFor(int32 channel) const
63908c97b50SAxel Dörfler{
6400b421b80SStefano Ceccherini	if (fMultiLabels != NULL) {
6410b421b80SStefano Ceccherini		label_map::const_iterator iter = ((label_map*)fMultiLabels)->find(channel);
6420b421b80SStefano Ceccherini		if (iter != ((label_map*)fMultiLabels)->end())
6430b421b80SStefano Ceccherini			return (*iter).second.max_label.c_str();
6440b421b80SStefano Ceccherini	}
64508c97b50SAxel Dörfler	return NULL;
64608c97b50SAxel Dörfler}
64708c97b50SAxel Dörfler
64808c97b50SAxel Dörfler
6491f424632SJohn Scipionestatus_t
65008c97b50SAxel DörflerBChannelControl::StuffValues(int32 fromChannel, int32 channelCount,
6511f424632SJohn Scipione	const int32* values)
65208c97b50SAxel Dörfler{
6531f424632SJohn Scipione	if (values == NULL)
654fd873802SStefano Ceccherini		return B_BAD_VALUE;
655fd873802SStefano Ceccherini
65620c7c2f9SStefano Ceccherini	if (fromChannel < 0 || fromChannel > fChannelCount
6571f424632SJohn Scipione		|| fromChannel + channelCount > fChannelCount) {
658fd873802SStefano Ceccherini		return B_BAD_INDEX;
6591f424632SJohn Scipione	}
660fd873802SStefano Ceccherini
661859c5ac3SStefano Ceccherini	for (int32 i = 0; i < channelCount; i++) {
6621f424632SJohn Scipione		if (values[i] <= fChannelMax[fromChannel + i]
6631f424632SJohn Scipione			&& values[i] >= fChannelMin[fromChannel + i]) {
6641f424632SJohn Scipione			fChannelValues[fromChannel + i] = values[i];
6651f424632SJohn Scipione		}
666fd873802SStefano Ceccherini	}
667fd873802SStefano Ceccherini
6681f424632SJohn Scipione	// if the current channel was updated, update also the control value
6691f424632SJohn Scipione	if (fCurrentChannel >= fromChannel
6701f424632SJohn Scipione		&& fCurrentChannel <= fromChannel + channelCount) {
671fd873802SStefano Ceccherini		BControl::SetValue(fChannelValues[fCurrentChannel]);
6721f424632SJohn Scipione	}
673fd873802SStefano Ceccherini
674fd873802SStefano Ceccherini	return B_OK;
67508c97b50SAxel Dörfler}
67608c97b50SAxel Dörfler
67708c97b50SAxel Dörfler
6781f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_0(void*, ...) {}
6791f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_1(void*, ...) {}
6801f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_2(void*, ...) {}
6811f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_3(void*, ...) {}
6821f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_4(void*, ...) {}
6831f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_5(void*, ...) {}
6841f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_6(void*, ...) {}
6851f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_7(void*, ...) {}
6861f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_8(void*, ...) {}
6871f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_9(void*, ...) {}
6881f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_10(void*, ...) {}
6891f424632SJohn Scipionevoid BChannelControl::_Reserverd_ChannelControl_11(void*, ...) {}
690