1 /*
2 * Copyright 2003-2006, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 * Stefano Ceccherini (burton666@libero.it)
7 * Jerome Duval
8 *
9 * Description: An abstract base class for option controls.
10 */
11
12 #include <OptionControl.h>
13
14 #include <cstring>
15
16
17 /*! \brief Creates and initializes a BOptionControl.
18 \param frame The control's frame rectangle.
19 \param name The control's name.
20 \param label The label displayed by the control.
21 \param message The message which the control will send when operated.
22 \param resize Resize mask, passed to the base class's constructor.
23 \param flags View flags, passed to the base class's constructor.
24 */
BOptionControl(BRect frame,const char * name,const char * label,BMessage * message,uint32 resize,uint32 flags)25 BOptionControl::BOptionControl(BRect frame, const char *name, const char *label,
26 BMessage *message, uint32 resize, uint32 flags)
27 :
28 BControl(frame, name, label, message, resize, flags)
29 {
30 }
31
32
BOptionControl(const char * name,const char * label,BMessage * message,uint32 flags)33 BOptionControl::BOptionControl(const char *name, const char *label,
34 BMessage *message, uint32 flags)
35 :
36 BControl(name, label, message, flags)
37 {
38 }
39
40
41 /*! \brief Destructor
42 It does nothing.
43 */
~BOptionControl()44 BOptionControl::~BOptionControl()
45 {
46 }
47
48
49 /*! \brief Overrides the base version to take special actions.
50 \param message The received message.
51 Calls SetValue() if receives a B_OPTION_CONTROL_VALUE message
52 which contains a "be:value" int32
53 */
54 void
MessageReceived(BMessage * message)55 BOptionControl::MessageReceived(BMessage *message)
56 {
57 switch (message->what) {
58 case B_OPTION_CONTROL_VALUE:
59 {
60 int32 value;
61 if (message->FindInt32("be:value", &value) == B_OK) {
62 SetValue(value);
63 Invoke();
64 }
65 break;
66 }
67 default:
68 BControl::MessageReceived(message);
69 break;
70 }
71 }
72
73
74 /*! \brief Adds an "option" after the last one.
75 \param name The name of the option.
76 \param value The value of the option.
77 \return \c B_OK if the option was added succesfully,
78 an error code otherwise.
79 */
80 status_t
AddOption(const char * name,int32 value)81 BOptionControl::AddOption(const char *name, int32 value)
82 {
83 int32 numOptions = CountOptions();
84 return AddOptionAt(name, value, numOptions);
85 }
86
87
88 /*! \brief Select the option which has the given value.
89 \param value The value of the option.
90 \return \c B_OK if there was an option with that value,
91 and it was correctly selected, an error code otherwise.
92 It works like SetValue(value);
93 */
94 status_t
SelectOptionFor(int32 value)95 BOptionControl::SelectOptionFor(int32 value)
96 {
97 // XXX: I wonder why this method was created in the first place,
98 // since you can obtain the same result simply by calling SetValue().
99 // The only difference I can see is that this method iterates over
100 // all the options contained in the control, and then selects the right one.
101 int32 numOptions = CountOptions();
102 for (int32 c = 0; c < numOptions; c++) {
103 const char *name = NULL;
104 int32 optionValue;
105 if (GetOptionAt(c, &name, &optionValue) && optionValue == value) {
106 SetValue(optionValue);
107 return B_OK;
108 }
109 }
110
111 return B_ERROR;
112 }
113
114
115 /*! \brief Select the option which has the given name.
116 \param name The name of the option.
117 \return \c B_OK if there was an option with that name,
118 and it was correctly selected, an error code otherwise.
119 */
120 status_t
SelectOptionFor(const char * name)121 BOptionControl::SelectOptionFor(const char *name)
122 {
123 int32 numOptions = CountOptions();
124 for (int32 c = 0; c < numOptions; c++) {
125 const char *optionName = NULL;
126 int32 optionValue;
127 if (GetOptionAt(c, &optionName, &optionValue)
128 && !strcmp(name, optionName)) {
129 SetValue(optionValue);
130 return B_OK;
131 }
132 }
133 return B_ERROR;
134 }
135
136
137 // Protected
138 /*! \brief Creates a BMessage which contains the given value.
139 \param The value to be added to the message.
140 \return A pointer to a BMessage, NULL if something went wrong.
141 */
142 BMessage *
MakeValueMessage(int32 value)143 BOptionControl::MakeValueMessage(int32 value)
144 {
145 BMessage *message = new BMessage(B_OPTION_CONTROL_VALUE);
146 if (message->AddInt32("be:value", value) != B_OK) {
147 delete message;
148 message = NULL;
149 }
150
151 return message;
152 }
153
154
155 // Private unimplemented
BOptionControl()156 BOptionControl::BOptionControl()
157 :
158 BControl(BRect(), "", "", NULL, 0, 0)
159 {
160 }
161
162
BOptionControl(const BOptionControl & clone)163 BOptionControl::BOptionControl(const BOptionControl & clone)
164 :
165 BControl(BRect(), "", "", NULL, 0, 0)
166 {
167 }
168
169
170 BOptionControl &
operator =(const BOptionControl & clone)171 BOptionControl::operator=(const BOptionControl & clone)
172 {
173 return *this;
174 }
175
176
177 // FBC
_Reserved_OptionControl_0(void *,...)178 status_t BOptionControl::_Reserved_OptionControl_0(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_1(void *,...)179 status_t BOptionControl::_Reserved_OptionControl_1(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_2(void *,...)180 status_t BOptionControl::_Reserved_OptionControl_2(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_3(void *,...)181 status_t BOptionControl::_Reserved_OptionControl_3(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_4(void *,...)182 status_t BOptionControl::_Reserved_OptionControl_4(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_5(void *,...)183 status_t BOptionControl::_Reserved_OptionControl_5(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_6(void *,...)184 status_t BOptionControl::_Reserved_OptionControl_6(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_7(void *,...)185 status_t BOptionControl::_Reserved_OptionControl_7(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_8(void *,...)186 status_t BOptionControl::_Reserved_OptionControl_8(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_9(void *,...)187 status_t BOptionControl::_Reserved_OptionControl_9(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_10(void *,...)188 status_t BOptionControl::_Reserved_OptionControl_10(void *, ...) { return B_ERROR; }
_Reserved_OptionControl_11(void *,...)189 status_t BOptionControl::_Reserved_OptionControl_11(void *, ...) { return B_ERROR; }
190