xref: /haiku/src/kits/interface/OptionControl.cpp (revision d8b4d83df2dc358785cb404cc568755e554755b0)
137a821f6SStefano Ceccherini //-----------------------------------------------------------------------------
2*d8b4d83dSStefano Ceccherini //	Copyright (c) 2003-2004 Stefano Ceccherini
337a821f6SStefano Ceccherini //
437a821f6SStefano Ceccherini //	Permission is hereby granted, free of charge, to any person obtaining a
537a821f6SStefano Ceccherini //	copy of this software and associated documentation files (the "Software"),
637a821f6SStefano Ceccherini //	to deal in the Software without restriction, including without limitation
737a821f6SStefano Ceccherini //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
837a821f6SStefano Ceccherini //	and/or sell copies of the Software, and to permit persons to whom the
937a821f6SStefano Ceccherini //	Software is furnished to do so, subject to the following conditions:
1037a821f6SStefano Ceccherini //
1137a821f6SStefano Ceccherini //	The above copyright notice and this permission notice shall be included in
1237a821f6SStefano Ceccherini //	all copies or substantial portions of the Software.
1337a821f6SStefano Ceccherini //
1437a821f6SStefano Ceccherini //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1537a821f6SStefano Ceccherini //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1637a821f6SStefano Ceccherini //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1737a821f6SStefano Ceccherini //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1837a821f6SStefano Ceccherini //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1937a821f6SStefano Ceccherini //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2037a821f6SStefano Ceccherini //	DEALINGS IN THE SOFTWARE.
2137a821f6SStefano Ceccherini //
2237a821f6SStefano Ceccherini //	File Name:		OptionControl.cpp
2337a821f6SStefano Ceccherini //	Description:	An abstract base class for option controls.
2437a821f6SStefano Ceccherini //------------------------------------------------------------------------------
2537a821f6SStefano Ceccherini #include <OptionControl.h>
2637a821f6SStefano Ceccherini 
2737a821f6SStefano Ceccherini #include <cstring>
2837a821f6SStefano Ceccherini 
2937a821f6SStefano Ceccherini /*! \brief Creates and initializes a BOptionControl.
3037a821f6SStefano Ceccherini 	\param frame The control's frame rectangle.
3137a821f6SStefano Ceccherini 	\param name The control's name.
3237a821f6SStefano Ceccherini 	\param label The label displayed by the control.
3337a821f6SStefano Ceccherini 	\param message The message which the control will send when operated.
3437a821f6SStefano Ceccherini 	\param resize Resize mask, passed to the base class's constructor.
3537a821f6SStefano Ceccherini 	\param flags View flags, passed to the base class's constructor.
3637a821f6SStefano Ceccherini */
3737a821f6SStefano Ceccherini BOptionControl::BOptionControl(BRect frame, const char *name, const char *label,
3837a821f6SStefano Ceccherini 								BMessage *message, uint32 resize, uint32 flags)
3937a821f6SStefano Ceccherini 	:
4037a821f6SStefano Ceccherini 	BControl(frame, name, label, message, resize, flags)
4137a821f6SStefano Ceccherini {
4237a821f6SStefano Ceccherini }
4337a821f6SStefano Ceccherini 
4437a821f6SStefano Ceccherini 
45*d8b4d83dSStefano Ceccherini /*! \brief Destructor
46*d8b4d83dSStefano Ceccherini 	It does nothing.
47*d8b4d83dSStefano Ceccherini */
4837a821f6SStefano Ceccherini BOptionControl::~BOptionControl()
4937a821f6SStefano Ceccherini {
5037a821f6SStefano Ceccherini }
5137a821f6SStefano Ceccherini 
5237a821f6SStefano Ceccherini 
53*d8b4d83dSStefano Ceccherini /*! \brief Overrides the base version to take special actions.
54*d8b4d83dSStefano Ceccherini 	\param message The received message.
55*d8b4d83dSStefano Ceccherini 	Calls SetValue() if receives a B_OPTION_CONTROL_VALUE message
56*d8b4d83dSStefano Ceccherini 	which contains a "be:value" int32
57*d8b4d83dSStefano Ceccherini */
5837a821f6SStefano Ceccherini void
5937a821f6SStefano Ceccherini BOptionControl::MessageReceived(BMessage *message)
6037a821f6SStefano Ceccherini {
6137a821f6SStefano Ceccherini 	switch (message->what) {
6237a821f6SStefano Ceccherini 		case B_OPTION_CONTROL_VALUE:
6337a821f6SStefano Ceccherini 		{
6437a821f6SStefano Ceccherini 			int32 value;
6537a821f6SStefano Ceccherini 			if (message->FindInt32("be:value", &value) == B_OK)
6637a821f6SStefano Ceccherini 				SetValue(value);
6737a821f6SStefano Ceccherini 			break;
6837a821f6SStefano Ceccherini 		}
6937a821f6SStefano Ceccherini 		default:
7037a821f6SStefano Ceccherini 			BControl::MessageReceived(message);
7137a821f6SStefano Ceccherini 			break;
7237a821f6SStefano Ceccherini 	}
7337a821f6SStefano Ceccherini }
7437a821f6SStefano Ceccherini 
7537a821f6SStefano Ceccherini 
7637a821f6SStefano Ceccherini /*! \brief Adds an "option" after the last one.
7737a821f6SStefano Ceccherini 	\param name The name of the option.
7837a821f6SStefano Ceccherini 	\param value The value of the option.
7937a821f6SStefano Ceccherini 	\return \c B_OK if the option was added succesfully,
8037a821f6SStefano Ceccherini 		an error code otherwise.
8137a821f6SStefano Ceccherini */
8237a821f6SStefano Ceccherini status_t
8337a821f6SStefano Ceccherini BOptionControl::AddOption(const char *name, int32 value)
8437a821f6SStefano Ceccherini {
8537a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
8637a821f6SStefano Ceccherini 	return AddOptionAt(name, value, numOptions);
8737a821f6SStefano Ceccherini }
8837a821f6SStefano Ceccherini 
8937a821f6SStefano Ceccherini 
9037a821f6SStefano Ceccherini /*! \brief Select the option which has the given value.
9137a821f6SStefano Ceccherini 	\param value The value of the option.
9237a821f6SStefano Ceccherini 	\return \c B_OK if there was an option with that value,
9337a821f6SStefano Ceccherini 		and it was correctly selected, an error code otherwise.
94*d8b4d83dSStefano Ceccherini 	It works like SetValue(value);
9537a821f6SStefano Ceccherini */
9637a821f6SStefano Ceccherini status_t
9737a821f6SStefano Ceccherini BOptionControl::SelectOptionFor(int32 value)
9837a821f6SStefano Ceccherini {
9937a821f6SStefano Ceccherini 	// XXX: I wonder why this method was created in the first place,
10037a821f6SStefano Ceccherini 	// since you can obtain the same result simply by calling SetValue().
10137a821f6SStefano Ceccherini 	// The only difference I can see is that this method iterates over
102*d8b4d83dSStefano Ceccherini 	// all the options contained in the control, and then selects the right one.
10337a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
10437a821f6SStefano Ceccherini 	for (int32 c = 0; c < numOptions; c++) {
105*d8b4d83dSStefano Ceccherini 		const char *name = NULL;
10637a821f6SStefano Ceccherini 		int32 optionValue;
10737a821f6SStefano Ceccherini 		if (GetOptionAt(c, &name, &optionValue) && optionValue == value) {
10837a821f6SStefano Ceccherini 			SetValue(optionValue);
10937a821f6SStefano Ceccherini 			return B_OK;
11037a821f6SStefano Ceccherini 		}
11137a821f6SStefano Ceccherini 	}
11237a821f6SStefano Ceccherini 
11337a821f6SStefano Ceccherini 	return B_ERROR;
11437a821f6SStefano Ceccherini }
11537a821f6SStefano Ceccherini 
11637a821f6SStefano Ceccherini 
11737a821f6SStefano Ceccherini /*! \brief Select the option which has the given name.
11837a821f6SStefano Ceccherini 	\param name The name of the option.
11937a821f6SStefano Ceccherini 	\return \c B_OK if there was an option with that name,
12037a821f6SStefano Ceccherini 		and it was correctly selected, an error code otherwise.
12137a821f6SStefano Ceccherini */
12237a821f6SStefano Ceccherini status_t
12337a821f6SStefano Ceccherini BOptionControl::SelectOptionFor(const char *name)
12437a821f6SStefano Ceccherini {
12537a821f6SStefano Ceccherini 	int32 numOptions = CountOptions();
12637a821f6SStefano Ceccherini 	for (int32 c = 0; c < numOptions; c++) {
12737a821f6SStefano Ceccherini 		const char *optionName = NULL;
12837a821f6SStefano Ceccherini 		int32 optionValue;
12937a821f6SStefano Ceccherini 		if (GetOptionAt(c, &optionName, &optionValue)
13037a821f6SStefano Ceccherini 						&& !strcmp(name, optionName)) {
13137a821f6SStefano Ceccherini 			SetValue(optionValue);
13237a821f6SStefano Ceccherini 			return B_OK;
13337a821f6SStefano Ceccherini 		}
13437a821f6SStefano Ceccherini 	}
13537a821f6SStefano Ceccherini 	return B_ERROR;
13637a821f6SStefano Ceccherini }
13737a821f6SStefano Ceccherini 
13837a821f6SStefano Ceccherini 
13937a821f6SStefano Ceccherini // Protected
14037a821f6SStefano Ceccherini /*! \brief Creates a BMessage which contains the given value.
14137a821f6SStefano Ceccherini 	\param The value to be added to the message.
14237a821f6SStefano Ceccherini 	\return A pointer to a BMessage, NULL if something went wrong.
14337a821f6SStefano Ceccherini */
14437a821f6SStefano Ceccherini BMessage *
14537a821f6SStefano Ceccherini BOptionControl::MakeValueMessage(int32 value)
14637a821f6SStefano Ceccherini {
14737a821f6SStefano Ceccherini 	BMessage *message = new BMessage(B_OPTION_CONTROL_VALUE);
14837a821f6SStefano Ceccherini 	if (message->AddInt32("be:value", value) != B_OK) {
14937a821f6SStefano Ceccherini 		delete message;
15037a821f6SStefano Ceccherini 		message = NULL;
15137a821f6SStefano Ceccherini 	}
15237a821f6SStefano Ceccherini 
15337a821f6SStefano Ceccherini 	return message;
15437a821f6SStefano Ceccherini }
15537a821f6SStefano Ceccherini 
15637a821f6SStefano Ceccherini 
157*d8b4d83dSStefano Ceccherini // Private unimplemented
158*d8b4d83dSStefano Ceccherini BOptionControl::BOptionControl()
159*d8b4d83dSStefano Ceccherini 	:
160*d8b4d83dSStefano Ceccherini 	BControl(BRect(), "", "", NULL, 0, 0)
161*d8b4d83dSStefano Ceccherini {
162*d8b4d83dSStefano Ceccherini }
163*d8b4d83dSStefano Ceccherini 
164*d8b4d83dSStefano Ceccherini 
165*d8b4d83dSStefano Ceccherini BOptionControl::BOptionControl(const BOptionControl & clone)
166*d8b4d83dSStefano Ceccherini 	:
167*d8b4d83dSStefano Ceccherini 	BControl(BRect(), "", "", NULL, 0, 0)
168*d8b4d83dSStefano Ceccherini {
169*d8b4d83dSStefano Ceccherini }
170*d8b4d83dSStefano Ceccherini 
171*d8b4d83dSStefano Ceccherini 
172*d8b4d83dSStefano Ceccherini BOptionControl &
173*d8b4d83dSStefano Ceccherini BOptionControl::operator=(const BOptionControl & clone)
174*d8b4d83dSStefano Ceccherini {
175*d8b4d83dSStefano Ceccherini 	return *this;
176*d8b4d83dSStefano Ceccherini }
177*d8b4d83dSStefano Ceccherini 
178*d8b4d83dSStefano Ceccherini 
17937a821f6SStefano Ceccherini // FBC
18037a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_0(void *, ...) { return B_ERROR; }
18137a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_1(void *, ...) { return B_ERROR; }
18237a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_2(void *, ...) { return B_ERROR; }
18337a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_3(void *, ...) { return B_ERROR; }
18437a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_4(void *, ...) { return B_ERROR; }
18537a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_5(void *, ...) { return B_ERROR; }
18637a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_6(void *, ...) { return B_ERROR; }
18737a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_7(void *, ...) { return B_ERROR; }
18837a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_8(void *, ...) { return B_ERROR; }
18937a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_9(void *, ...) { return B_ERROR; }
19037a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_10(void *, ...) { return B_ERROR; }
19137a821f6SStefano Ceccherini status_t BOptionControl::_Reserved_OptionControl_11(void *, ...) { return B_ERROR; }
192