xref: /haiku/src/kits/interface/OptionControl.cpp (revision 0e726a568446023fb88b9160a9f7a0a85658ff4b)
1*0e726a56SJérôme Duval /*
2*0e726a56SJérôme Duval  * Copyright 2003-2006, Haiku, Inc.
3*0e726a56SJérôme Duval  * Distributed under the terms of the MIT license.
4*0e726a56SJérôme Duval  *
5*0e726a56SJérôme Duval  * Authors:
6*0e726a56SJérôme Duval  *		Stefano Ceccherini (burton666@libero.it)
7*0e726a56SJérôme Duval  * 		Jerome Duval
8*0e726a56SJérôme Duval  *
9*0e726a56SJérôme Duval  * Description:	An abstract base class for option controls.
10*0e726a56SJérôme Duval  */
11*0e726a56SJérôme Duval 
1237a821f6SStefano Ceccherini #include <OptionControl.h>
1337a821f6SStefano Ceccherini 
1437a821f6SStefano Ceccherini #include <cstring>
1537a821f6SStefano Ceccherini 
16*0e726a56SJé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 */
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 
33d8b4d83dSStefano Ceccherini /*! \brief Destructor
34d8b4d83dSStefano Ceccherini 	It does nothing.
35d8b4d83dSStefano Ceccherini */
3637a821f6SStefano Ceccherini BOptionControl::~BOptionControl()
3737a821f6SStefano Ceccherini {
3837a821f6SStefano Ceccherini }
3937a821f6SStefano Ceccherini 
4037a821f6SStefano Ceccherini 
41d8b4d83dSStefano Ceccherini /*! \brief Overrides the base version to take special actions.
42d8b4d83dSStefano Ceccherini 	\param message The received message.
43d8b4d83dSStefano Ceccherini 	Calls SetValue() if receives a B_OPTION_CONTROL_VALUE message
44d8b4d83dSStefano Ceccherini 	which contains a "be:value" int32
45d8b4d83dSStefano Ceccherini */
4637a821f6SStefano Ceccherini void
4737a821f6SStefano Ceccherini BOptionControl::MessageReceived(BMessage *message)
4837a821f6SStefano Ceccherini {
4937a821f6SStefano Ceccherini 	switch (message->what) {
5037a821f6SStefano Ceccherini 		case B_OPTION_CONTROL_VALUE:
5137a821f6SStefano Ceccherini 		{
5237a821f6SStefano Ceccherini 			int32 value;
53*0e726a56SJérôme Duval 			if (message->FindInt32("be:value", &value) == B_OK) {
5437a821f6SStefano Ceccherini 				SetValue(value);
55*0e726a56SJérôme Duval 				Invoke();
56*0e726a56SJérôme Duval 			}
5737a821f6SStefano Ceccherini 			break;
5837a821f6SStefano Ceccherini 		}
5937a821f6SStefano Ceccherini 		default:
6037a821f6SStefano Ceccherini 			BControl::MessageReceived(message);
6137a821f6SStefano Ceccherini 			break;
6237a821f6SStefano Ceccherini 	}
6337a821f6SStefano Ceccherini }
6437a821f6SStefano Ceccherini 
6537a821f6SStefano Ceccherini 
6637a821f6SStefano Ceccherini /*! \brief Adds an "option" after the last one.
6737a821f6SStefano Ceccherini 	\param name The name of the option.
6837a821f6SStefano Ceccherini 	\param value The value of the option.
6937a821f6SStefano Ceccherini 	\return \c B_OK if the option was added succesfully,
7037a821f6SStefano Ceccherini 		an error code otherwise.
7137a821f6SStefano Ceccherini */
7237a821f6SStefano Ceccherini status_t
7337a821f6SStefano Ceccherini BOptionControl::AddOption(const char *name, int32 value)
7437a821f6SStefano Ceccherini {
7537a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
7637a821f6SStefano Ceccherini 	return AddOptionAt(name, value, numOptions);
7737a821f6SStefano Ceccherini }
7837a821f6SStefano Ceccherini 
7937a821f6SStefano Ceccherini 
8037a821f6SStefano Ceccherini /*! \brief Select the option which has the given value.
8137a821f6SStefano Ceccherini 	\param value The value of the option.
8237a821f6SStefano Ceccherini 	\return \c B_OK if there was an option with that value,
8337a821f6SStefano Ceccherini 		and it was correctly selected, an error code otherwise.
84d8b4d83dSStefano Ceccherini 	It works like SetValue(value);
8537a821f6SStefano Ceccherini */
8637a821f6SStefano Ceccherini status_t
8737a821f6SStefano Ceccherini BOptionControl::SelectOptionFor(int32 value)
8837a821f6SStefano Ceccherini {
8937a821f6SStefano Ceccherini 	// XXX: I wonder why this method was created in the first place,
9037a821f6SStefano Ceccherini 	// since you can obtain the same result simply by calling SetValue().
9137a821f6SStefano Ceccherini 	// The only difference I can see is that this method iterates over
92d8b4d83dSStefano Ceccherini 	// all the options contained in the control, and then selects the right one.
9337a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
9437a821f6SStefano Ceccherini 	for (int32 c = 0; c < numOptions; c++) {
95d8b4d83dSStefano Ceccherini 		const char *name = NULL;
9637a821f6SStefano Ceccherini 		int32 optionValue;
9737a821f6SStefano Ceccherini 		if (GetOptionAt(c, &name, &optionValue) && optionValue == value) {
9837a821f6SStefano Ceccherini 			SetValue(optionValue);
9937a821f6SStefano Ceccherini 			return B_OK;
10037a821f6SStefano Ceccherini 		}
10137a821f6SStefano Ceccherini 	}
10237a821f6SStefano Ceccherini 
10337a821f6SStefano Ceccherini 	return B_ERROR;
10437a821f6SStefano Ceccherini }
10537a821f6SStefano Ceccherini 
10637a821f6SStefano Ceccherini 
10737a821f6SStefano Ceccherini /*! \brief Select the option which has the given name.
10837a821f6SStefano Ceccherini 	\param name The name of the option.
10937a821f6SStefano Ceccherini 	\return \c B_OK if there was an option with that name,
11037a821f6SStefano Ceccherini 		and it was correctly selected, an error code otherwise.
11137a821f6SStefano Ceccherini */
11237a821f6SStefano Ceccherini status_t
11337a821f6SStefano Ceccherini BOptionControl::SelectOptionFor(const char *name)
11437a821f6SStefano Ceccherini {
11537a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
11637a821f6SStefano Ceccherini 	for (int32 c = 0; c < numOptions; c++) {
11737a821f6SStefano Ceccherini 		const char *optionName = NULL;
11837a821f6SStefano Ceccherini 		int32 optionValue;
11937a821f6SStefano Ceccherini 		if (GetOptionAt(c, &optionName, &optionValue)
12037a821f6SStefano Ceccherini 						&& !strcmp(name, optionName)) {
12137a821f6SStefano Ceccherini 			SetValue(optionValue);
12237a821f6SStefano Ceccherini 			return B_OK;
12337a821f6SStefano Ceccherini 		}
12437a821f6SStefano Ceccherini 	}
12537a821f6SStefano Ceccherini 	return B_ERROR;
12637a821f6SStefano Ceccherini }
12737a821f6SStefano Ceccherini 
12837a821f6SStefano Ceccherini 
12937a821f6SStefano Ceccherini // Protected
13037a821f6SStefano Ceccherini /*! \brief Creates a BMessage which contains the given value.
13137a821f6SStefano Ceccherini 	\param The value to be added to the message.
13237a821f6SStefano Ceccherini 	\return A pointer to a BMessage, NULL if something went wrong.
13337a821f6SStefano Ceccherini */
13437a821f6SStefano Ceccherini BMessage *
13537a821f6SStefano Ceccherini BOptionControl::MakeValueMessage(int32 value)
13637a821f6SStefano Ceccherini {
13737a821f6SStefano Ceccherini 	BMessage *message = new BMessage(B_OPTION_CONTROL_VALUE);
13837a821f6SStefano Ceccherini 	if (message->AddInt32("be:value", value) != B_OK) {
13937a821f6SStefano Ceccherini 		delete message;
14037a821f6SStefano Ceccherini 		message = NULL;
14137a821f6SStefano Ceccherini 	}
14237a821f6SStefano Ceccherini 
14337a821f6SStefano Ceccherini 	return message;
14437a821f6SStefano Ceccherini }
14537a821f6SStefano Ceccherini 
14637a821f6SStefano Ceccherini 
147d8b4d83dSStefano Ceccherini // Private unimplemented
148d8b4d83dSStefano Ceccherini BOptionControl::BOptionControl()
149d8b4d83dSStefano Ceccherini 	:
150d8b4d83dSStefano Ceccherini 	BControl(BRect(), "", "", NULL, 0, 0)
151d8b4d83dSStefano Ceccherini {
152d8b4d83dSStefano Ceccherini }
153d8b4d83dSStefano Ceccherini 
154d8b4d83dSStefano Ceccherini 
155d8b4d83dSStefano Ceccherini BOptionControl::BOptionControl(const BOptionControl & clone)
156d8b4d83dSStefano Ceccherini 	:
157d8b4d83dSStefano Ceccherini 	BControl(BRect(), "", "", NULL, 0, 0)
158d8b4d83dSStefano Ceccherini {
159d8b4d83dSStefano Ceccherini }
160d8b4d83dSStefano Ceccherini 
161d8b4d83dSStefano Ceccherini 
162d8b4d83dSStefano Ceccherini BOptionControl &
163d8b4d83dSStefano Ceccherini BOptionControl::operator=(const BOptionControl & clone)
164d8b4d83dSStefano Ceccherini {
165d8b4d83dSStefano Ceccherini 	return *this;
166d8b4d83dSStefano Ceccherini }
167d8b4d83dSStefano Ceccherini 
168d8b4d83dSStefano Ceccherini 
16937a821f6SStefano Ceccherini // FBC
17037a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_0(void *, ...) { return B_ERROR; }
17137a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_1(void *, ...) { return B_ERROR; }
17237a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_2(void *, ...) { return B_ERROR; }
17337a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_3(void *, ...) { return B_ERROR; }
17437a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_4(void *, ...) { return B_ERROR; }
17537a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_5(void *, ...) { return B_ERROR; }
17637a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_6(void *, ...) { return B_ERROR; }
17737a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_7(void *, ...) { return B_ERROR; }
17837a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_8(void *, ...) { return B_ERROR; }
17937a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_9(void *, ...) { return B_ERROR; }
18037a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_10(void *, ...) { return B_ERROR; }
18137a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_11(void *, ...) { return B_ERROR; }
182