xref: /haiku/src/kits/interface/OptionControl.cpp (revision ba69bf196e96d91a1a63d677ea0c7e035b02b09e)
10e726a56SJérôme Duval /*
20e726a56SJérôme Duval  * Copyright 2003-2006, Haiku, Inc.
30e726a56SJérôme Duval  * Distributed under the terms of the MIT license.
40e726a56SJérôme Duval  *
50e726a56SJérôme Duval  * Authors:
60e726a56SJérôme Duval  *		Stefano Ceccherini (burton666@libero.it)
70e726a56SJérôme Duval  * 		Jerome Duval
80e726a56SJérôme Duval  *
90e726a56SJérôme Duval  * Description:	An abstract base class for option controls.
100e726a56SJérôme Duval  */
110e726a56SJérôme Duval 
1237a821f6SStefano Ceccherini #include <OptionControl.h>
1337a821f6SStefano Ceccherini 
1437a821f6SStefano Ceccherini #include <cstring>
1537a821f6SStefano Ceccherini 
160e726a56SJérôme Duval 
1737a821f6SStefano Ceccherini /*! \brief Creates and initializes a BOptionControl.
1837a821f6SStefano Ceccherini 	\param frame The control's frame rectangle.
1937a821f6SStefano Ceccherini 	\param name The control's name.
2037a821f6SStefano Ceccherini 	\param label The label displayed by the control.
2137a821f6SStefano Ceccherini 	\param message The message which the control will send when operated.
2237a821f6SStefano Ceccherini 	\param resize Resize mask, passed to the base class's constructor.
2337a821f6SStefano Ceccherini 	\param flags View flags, passed to the base class's constructor.
2437a821f6SStefano Ceccherini */
BOptionControl(BRect frame,const char * name,const char * label,BMessage * message,uint32 resize,uint32 flags)2537a821f6SStefano Ceccherini BOptionControl::BOptionControl(BRect frame, const char *name, const char *label,
2637a821f6SStefano Ceccherini 								BMessage *message, uint32 resize, uint32 flags)
2737a821f6SStefano Ceccherini 	:
2837a821f6SStefano Ceccherini 	BControl(frame, name, label, message, resize, flags)
2937a821f6SStefano Ceccherini {
3037a821f6SStefano Ceccherini }
3137a821f6SStefano Ceccherini 
3237a821f6SStefano Ceccherini 
BOptionControl(const char * name,const char * label,BMessage * message,uint32 flags)33*ba69bf19SStefano Ceccherini BOptionControl::BOptionControl(const char *name, const char *label,
34*ba69bf19SStefano Ceccherini 								BMessage *message, uint32 flags)
35*ba69bf19SStefano Ceccherini 	:
36*ba69bf19SStefano Ceccherini 	BControl(name, label, message, flags)
37*ba69bf19SStefano Ceccherini {
38*ba69bf19SStefano Ceccherini }
39*ba69bf19SStefano Ceccherini 
40*ba69bf19SStefano Ceccherini 
41d8b4d83dSStefano Ceccherini /*! \brief Destructor
42d8b4d83dSStefano Ceccherini 	It does nothing.
43d8b4d83dSStefano Ceccherini */
~BOptionControl()4437a821f6SStefano Ceccherini BOptionControl::~BOptionControl()
4537a821f6SStefano Ceccherini {
4637a821f6SStefano Ceccherini }
4737a821f6SStefano Ceccherini 
4837a821f6SStefano Ceccherini 
49d8b4d83dSStefano Ceccherini /*! \brief Overrides the base version to take special actions.
50d8b4d83dSStefano Ceccherini 	\param message The received message.
51d8b4d83dSStefano Ceccherini 	Calls SetValue() if receives a B_OPTION_CONTROL_VALUE message
52d8b4d83dSStefano Ceccherini 	which contains a "be:value" int32
53d8b4d83dSStefano Ceccherini */
5437a821f6SStefano Ceccherini void
MessageReceived(BMessage * message)5537a821f6SStefano Ceccherini BOptionControl::MessageReceived(BMessage *message)
5637a821f6SStefano Ceccherini {
5737a821f6SStefano Ceccherini 	switch (message->what) {
5837a821f6SStefano Ceccherini 		case B_OPTION_CONTROL_VALUE:
5937a821f6SStefano Ceccherini 		{
6037a821f6SStefano Ceccherini 			int32 value;
610e726a56SJérôme Duval 			if (message->FindInt32("be:value", &value) == B_OK) {
6237a821f6SStefano Ceccherini 				SetValue(value);
630e726a56SJérôme Duval 				Invoke();
640e726a56SJérôme Duval 			}
6537a821f6SStefano Ceccherini 			break;
6637a821f6SStefano Ceccherini 		}
6737a821f6SStefano Ceccherini 		default:
6837a821f6SStefano Ceccherini 			BControl::MessageReceived(message);
6937a821f6SStefano Ceccherini 			break;
7037a821f6SStefano Ceccherini 	}
7137a821f6SStefano Ceccherini }
7237a821f6SStefano Ceccherini 
7337a821f6SStefano Ceccherini 
7437a821f6SStefano Ceccherini /*! \brief Adds an "option" after the last one.
7537a821f6SStefano Ceccherini 	\param name The name of the option.
7637a821f6SStefano Ceccherini 	\param value The value of the option.
7737a821f6SStefano Ceccherini 	\return \c B_OK if the option was added succesfully,
7837a821f6SStefano Ceccherini 		an error code otherwise.
7937a821f6SStefano Ceccherini */
8037a821f6SStefano Ceccherini status_t
AddOption(const char * name,int32 value)8137a821f6SStefano Ceccherini BOptionControl::AddOption(const char *name, int32 value)
8237a821f6SStefano Ceccherini {
8337a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
8437a821f6SStefano Ceccherini 	return AddOptionAt(name, value, numOptions);
8537a821f6SStefano Ceccherini }
8637a821f6SStefano Ceccherini 
8737a821f6SStefano Ceccherini 
8837a821f6SStefano Ceccherini /*! \brief Select the option which has the given value.
8937a821f6SStefano Ceccherini 	\param value The value of the option.
9037a821f6SStefano Ceccherini 	\return \c B_OK if there was an option with that value,
9137a821f6SStefano Ceccherini 		and it was correctly selected, an error code otherwise.
92d8b4d83dSStefano Ceccherini 	It works like SetValue(value);
9337a821f6SStefano Ceccherini */
9437a821f6SStefano Ceccherini status_t
SelectOptionFor(int32 value)9537a821f6SStefano Ceccherini BOptionControl::SelectOptionFor(int32 value)
9637a821f6SStefano Ceccherini {
9737a821f6SStefano Ceccherini 	// XXX: I wonder why this method was created in the first place,
9837a821f6SStefano Ceccherini 	// since you can obtain the same result simply by calling SetValue().
9937a821f6SStefano Ceccherini 	// The only difference I can see is that this method iterates over
100d8b4d83dSStefano Ceccherini 	// all the options contained in the control, and then selects the right one.
10137a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
10237a821f6SStefano Ceccherini 	for (int32 c = 0; c < numOptions; c++) {
103d8b4d83dSStefano Ceccherini 		const char *name = NULL;
10437a821f6SStefano Ceccherini 		int32 optionValue;
10537a821f6SStefano Ceccherini 		if (GetOptionAt(c, &name, &optionValue) && optionValue == value) {
10637a821f6SStefano Ceccherini 			SetValue(optionValue);
10737a821f6SStefano Ceccherini 			return B_OK;
10837a821f6SStefano Ceccherini 		}
10937a821f6SStefano Ceccherini 	}
11037a821f6SStefano Ceccherini 
11137a821f6SStefano Ceccherini 	return B_ERROR;
11237a821f6SStefano Ceccherini }
11337a821f6SStefano Ceccherini 
11437a821f6SStefano Ceccherini 
11537a821f6SStefano Ceccherini /*! \brief Select the option which has the given name.
11637a821f6SStefano Ceccherini 	\param name The name of the option.
11737a821f6SStefano Ceccherini 	\return \c B_OK if there was an option with that name,
11837a821f6SStefano Ceccherini 		and it was correctly selected, an error code otherwise.
11937a821f6SStefano Ceccherini */
12037a821f6SStefano Ceccherini status_t
SelectOptionFor(const char * name)12137a821f6SStefano Ceccherini BOptionControl::SelectOptionFor(const char *name)
12237a821f6SStefano Ceccherini {
12337a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
12437a821f6SStefano Ceccherini 	for (int32 c = 0; c < numOptions; c++) {
12537a821f6SStefano Ceccherini 		const char *optionName = NULL;
12637a821f6SStefano Ceccherini 		int32 optionValue;
12737a821f6SStefano Ceccherini 		if (GetOptionAt(c, &optionName, &optionValue)
12837a821f6SStefano Ceccherini 						&& !strcmp(name, optionName)) {
12937a821f6SStefano Ceccherini 			SetValue(optionValue);
13037a821f6SStefano Ceccherini 			return B_OK;
13137a821f6SStefano Ceccherini 		}
13237a821f6SStefano Ceccherini 	}
13337a821f6SStefano Ceccherini 	return B_ERROR;
13437a821f6SStefano Ceccherini }
13537a821f6SStefano Ceccherini 
13637a821f6SStefano Ceccherini 
13737a821f6SStefano Ceccherini // Protected
13837a821f6SStefano Ceccherini /*! \brief Creates a BMessage which contains the given value.
13937a821f6SStefano Ceccherini 	\param The value to be added to the message.
14037a821f6SStefano Ceccherini 	\return A pointer to a BMessage, NULL if something went wrong.
14137a821f6SStefano Ceccherini */
14237a821f6SStefano Ceccherini BMessage *
MakeValueMessage(int32 value)14337a821f6SStefano Ceccherini BOptionControl::MakeValueMessage(int32 value)
14437a821f6SStefano Ceccherini {
14537a821f6SStefano Ceccherini 	BMessage *message = new BMessage(B_OPTION_CONTROL_VALUE);
14637a821f6SStefano Ceccherini 	if (message->AddInt32("be:value", value) != B_OK) {
14737a821f6SStefano Ceccherini 		delete message;
14837a821f6SStefano Ceccherini 		message = NULL;
14937a821f6SStefano Ceccherini 	}
15037a821f6SStefano Ceccherini 
15137a821f6SStefano Ceccherini 	return message;
15237a821f6SStefano Ceccherini }
15337a821f6SStefano Ceccherini 
15437a821f6SStefano Ceccherini 
155d8b4d83dSStefano Ceccherini // Private unimplemented
BOptionControl()156d8b4d83dSStefano Ceccherini BOptionControl::BOptionControl()
157d8b4d83dSStefano Ceccherini 	:
158d8b4d83dSStefano Ceccherini 	BControl(BRect(), "", "", NULL, 0, 0)
159d8b4d83dSStefano Ceccherini {
160d8b4d83dSStefano Ceccherini }
161d8b4d83dSStefano Ceccherini 
162d8b4d83dSStefano Ceccherini 
BOptionControl(const BOptionControl & clone)163d8b4d83dSStefano Ceccherini BOptionControl::BOptionControl(const BOptionControl & clone)
164d8b4d83dSStefano Ceccherini 	:
165d8b4d83dSStefano Ceccherini 	BControl(BRect(), "", "", NULL, 0, 0)
166d8b4d83dSStefano Ceccherini {
167d8b4d83dSStefano Ceccherini }
168d8b4d83dSStefano Ceccherini 
169d8b4d83dSStefano Ceccherini 
170d8b4d83dSStefano Ceccherini BOptionControl &
operator =(const BOptionControl & clone)171d8b4d83dSStefano Ceccherini BOptionControl::operator=(const BOptionControl & clone)
172d8b4d83dSStefano Ceccherini {
173d8b4d83dSStefano Ceccherini 	return *this;
174d8b4d83dSStefano Ceccherini }
175d8b4d83dSStefano Ceccherini 
176d8b4d83dSStefano Ceccherini 
17737a821f6SStefano Ceccherini // FBC
_Reserved_OptionControl_0(void *,...)17837a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_0(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_1(void *,...)17937a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_1(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_2(void *,...)18037a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_2(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_3(void *,...)18137a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_3(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_4(void *,...)18237a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_4(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_5(void *,...)18337a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_5(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_6(void *,...)18437a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_6(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_7(void *,...)18537a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_7(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_8(void *,...)18637a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_8(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_9(void *,...)18737a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_9(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_10(void *,...)18837a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_10(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_11(void *,...)18937a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_11(void *, ...) { return B_ERROR; }
190