2003-12-29 07:19:42 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2004-01-08 07:26:57 +00:00
|
|
|
// Copyright (c) 2003-2004 Stefano Ceccherini
|
2003-12-29 07:19:42 +00:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
//
|
|
|
|
|
// File Name: OptionControl.cpp
|
|
|
|
|
// Description: An abstract base class for option controls.
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
#include <OptionControl.h>
|
|
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
/*! \brief Creates and initializes a BOptionControl.
|
|
|
|
|
\param frame The control's frame rectangle.
|
|
|
|
|
\param name The control's name.
|
|
|
|
|
\param label The label displayed by the control.
|
|
|
|
|
\param message The message which the control will send when operated.
|
|
|
|
|
\param resize Resize mask, passed to the base class's constructor.
|
|
|
|
|
\param flags View flags, passed to the base class's constructor.
|
|
|
|
|
*/
|
|
|
|
|
BOptionControl::BOptionControl(BRect frame, const char *name, const char *label,
|
|
|
|
|
BMessage *message, uint32 resize, uint32 flags)
|
|
|
|
|
:
|
|
|
|
|
BControl(frame, name, label, message, resize, flags)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-08 07:26:57 +00:00
|
|
|
/*! \brief Destructor
|
|
|
|
|
It does nothing.
|
|
|
|
|
*/
|
2003-12-29 07:19:42 +00:00
|
|
|
BOptionControl::~BOptionControl()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-08 07:26:57 +00:00
|
|
|
/*! \brief Overrides the base version to take special actions.
|
|
|
|
|
\param message The received message.
|
|
|
|
|
Calls SetValue() if receives a B_OPTION_CONTROL_VALUE message
|
|
|
|
|
which contains a "be:value" int32
|
|
|
|
|
*/
|
2003-12-29 07:19:42 +00:00
|
|
|
void
|
|
|
|
|
BOptionControl::MessageReceived(BMessage *message)
|
|
|
|
|
{
|
|
|
|
|
switch (message->what) {
|
|
|
|
|
case B_OPTION_CONTROL_VALUE:
|
|
|
|
|
{
|
|
|
|
|
int32 value;
|
|
|
|
|
if (message->FindInt32("be:value", &value) == B_OK)
|
|
|
|
|
SetValue(value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
BControl::MessageReceived(message);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Adds an "option" after the last one.
|
|
|
|
|
\param name The name of the option.
|
|
|
|
|
\param value The value of the option.
|
|
|
|
|
\return \c B_OK if the option was added succesfully,
|
|
|
|
|
an error code otherwise.
|
|
|
|
|
*/
|
|
|
|
|
status_t
|
|
|
|
|
BOptionControl::AddOption(const char *name, int32 value)
|
|
|
|
|
{
|
|
|
|
|
int32 numOptions = CountOptions();
|
|
|
|
|
return AddOptionAt(name, value, numOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Select the option which has the given value.
|
|
|
|
|
\param value The value of the option.
|
|
|
|
|
\return \c B_OK if there was an option with that value,
|
|
|
|
|
and it was correctly selected, an error code otherwise.
|
2004-01-08 07:26:57 +00:00
|
|
|
It works like SetValue(value);
|
2003-12-29 07:19:42 +00:00
|
|
|
*/
|
|
|
|
|
status_t
|
|
|
|
|
BOptionControl::SelectOptionFor(int32 value)
|
|
|
|
|
{
|
|
|
|
|
// XXX: I wonder why this method was created in the first place,
|
|
|
|
|
// since you can obtain the same result simply by calling SetValue().
|
|
|
|
|
// The only difference I can see is that this method iterates over
|
2004-01-08 07:26:57 +00:00
|
|
|
// all the options contained in the control, and then selects the right one.
|
2003-12-29 07:19:42 +00:00
|
|
|
int32 numOptions = CountOptions();
|
|
|
|
|
for (int32 c = 0; c < numOptions; c++) {
|
2004-01-08 07:26:57 +00:00
|
|
|
const char *name = NULL;
|
2003-12-29 07:19:42 +00:00
|
|
|
int32 optionValue;
|
|
|
|
|
if (GetOptionAt(c, &name, &optionValue) && optionValue == value) {
|
|
|
|
|
SetValue(optionValue);
|
|
|
|
|
return B_OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return B_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Select the option which has the given name.
|
|
|
|
|
\param name The name of the option.
|
|
|
|
|
\return \c B_OK if there was an option with that name,
|
|
|
|
|
and it was correctly selected, an error code otherwise.
|
|
|
|
|
*/
|
|
|
|
|
status_t
|
|
|
|
|
BOptionControl::SelectOptionFor(const char *name)
|
|
|
|
|
{
|
|
|
|
|
int32 numOptions = CountOptions();
|
|
|
|
|
for (int32 c = 0; c < numOptions; c++) {
|
|
|
|
|
const char *optionName = NULL;
|
|
|
|
|
int32 optionValue;
|
|
|
|
|
if (GetOptionAt(c, &optionName, &optionValue)
|
|
|
|
|
&& !strcmp(name, optionName)) {
|
|
|
|
|
SetValue(optionValue);
|
|
|
|
|
return B_OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return B_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Protected
|
|
|
|
|
/*! \brief Creates a BMessage which contains the given value.
|
|
|
|
|
\param The value to be added to the message.
|
|
|
|
|
\return A pointer to a BMessage, NULL if something went wrong.
|
|
|
|
|
*/
|
|
|
|
|
BMessage *
|
|
|
|
|
BOptionControl::MakeValueMessage(int32 value)
|
|
|
|
|
{
|
|
|
|
|
BMessage *message = new BMessage(B_OPTION_CONTROL_VALUE);
|
|
|
|
|
if (message->AddInt32("be:value", value) != B_OK) {
|
|
|
|
|
delete message;
|
|
|
|
|
message = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-08 07:26:57 +00:00
|
|
|
// Private unimplemented
|
|
|
|
|
BOptionControl::BOptionControl()
|
|
|
|
|
:
|
|
|
|
|
BControl(BRect(), "", "", NULL, 0, 0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOptionControl::BOptionControl(const BOptionControl & clone)
|
|
|
|
|
:
|
|
|
|
|
BControl(BRect(), "", "", NULL, 0, 0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOptionControl &
|
|
|
|
|
BOptionControl::operator=(const BOptionControl & clone)
|
|
|
|
|
{
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-12-29 07:19:42 +00:00
|
|
|
// FBC
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_0(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_1(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_2(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_3(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_4(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_5(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_6(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_7(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_8(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_9(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_10(void *, ...) { return B_ERROR; }
|
|
|
|
|
status_t BOptionControl::_Reserved_OptionControl_11(void *, ...) { return B_ERROR; }
|