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 */ 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 33 /*! \brief Destructor 34 It does nothing. 35 */ 36 BOptionControl::~BOptionControl() 37 { 38 } 39 40 41 /*! \brief Overrides the base version to take special actions. 42 \param message The received message. 43 Calls SetValue() if receives a B_OPTION_CONTROL_VALUE message 44 which contains a "be:value" int32 45 */ 46 void 47 BOptionControl::MessageReceived(BMessage *message) 48 { 49 switch (message->what) { 50 case B_OPTION_CONTROL_VALUE: 51 { 52 int32 value; 53 if (message->FindInt32("be:value", &value) == B_OK) { 54 SetValue(value); 55 Invoke(); 56 } 57 break; 58 } 59 default: 60 BControl::MessageReceived(message); 61 break; 62 } 63 } 64 65 66 /*! \brief Adds an "option" after the last one. 67 \param name The name of the option. 68 \param value The value of the option. 69 \return \c B_OK if the option was added succesfully, 70 an error code otherwise. 71 */ 72 status_t 73 BOptionControl::AddOption(const char *name, int32 value) 74 { 75 int32 numOptions = CountOptions(); 76 return AddOptionAt(name, value, numOptions); 77 } 78 79 80 /*! \brief Select the option which has the given value. 81 \param value The value of the option. 82 \return \c B_OK if there was an option with that value, 83 and it was correctly selected, an error code otherwise. 84 It works like SetValue(value); 85 */ 86 status_t 87 BOptionControl::SelectOptionFor(int32 value) 88 { 89 // XXX: I wonder why this method was created in the first place, 90 // since you can obtain the same result simply by calling SetValue(). 91 // The only difference I can see is that this method iterates over 92 // all the options contained in the control, and then selects the right one. 93 int32 numOptions = CountOptions(); 94 for (int32 c = 0; c < numOptions; c++) { 95 const char *name = NULL; 96 int32 optionValue; 97 if (GetOptionAt(c, &name, &optionValue) && optionValue == value) { 98 SetValue(optionValue); 99 return B_OK; 100 } 101 } 102 103 return B_ERROR; 104 } 105 106 107 /*! \brief Select the option which has the given name. 108 \param name The name of the option. 109 \return \c B_OK if there was an option with that name, 110 and it was correctly selected, an error code otherwise. 111 */ 112 status_t 113 BOptionControl::SelectOptionFor(const char *name) 114 { 115 int32 numOptions = CountOptions(); 116 for (int32 c = 0; c < numOptions; c++) { 117 const char *optionName = NULL; 118 int32 optionValue; 119 if (GetOptionAt(c, &optionName, &optionValue) 120 && !strcmp(name, optionName)) { 121 SetValue(optionValue); 122 return B_OK; 123 } 124 } 125 return B_ERROR; 126 } 127 128 129 // Protected 130 /*! \brief Creates a BMessage which contains the given value. 131 \param The value to be added to the message. 132 \return A pointer to a BMessage, NULL if something went wrong. 133 */ 134 BMessage * 135 BOptionControl::MakeValueMessage(int32 value) 136 { 137 BMessage *message = new BMessage(B_OPTION_CONTROL_VALUE); 138 if (message->AddInt32("be:value", value) != B_OK) { 139 delete message; 140 message = NULL; 141 } 142 143 return message; 144 } 145 146 147 // Private unimplemented 148 BOptionControl::BOptionControl() 149 : 150 BControl(BRect(), "", "", NULL, 0, 0) 151 { 152 } 153 154 155 BOptionControl::BOptionControl(const BOptionControl & clone) 156 : 157 BControl(BRect(), "", "", NULL, 0, 0) 158 { 159 } 160 161 162 BOptionControl & 163 BOptionControl::operator=(const BOptionControl & clone) 164 { 165 return *this; 166 } 167 168 169 // FBC 170 status_t BOptionControl::_Reserved_OptionControl_0(void *, ...) { return B_ERROR; } 171 status_t BOptionControl::_Reserved_OptionControl_1(void *, ...) { return B_ERROR; } 172 status_t BOptionControl::_Reserved_OptionControl_2(void *, ...) { return B_ERROR; } 173 status_t BOptionControl::_Reserved_OptionControl_3(void *, ...) { return B_ERROR; } 174 status_t BOptionControl::_Reserved_OptionControl_4(void *, ...) { return B_ERROR; } 175 status_t BOptionControl::_Reserved_OptionControl_5(void *, ...) { return B_ERROR; } 176 status_t BOptionControl::_Reserved_OptionControl_6(void *, ...) { return B_ERROR; } 177 status_t BOptionControl::_Reserved_OptionControl_7(void *, ...) { return B_ERROR; } 178 status_t BOptionControl::_Reserved_OptionControl_8(void *, ...) { return B_ERROR; } 179 status_t BOptionControl::_Reserved_OptionControl_9(void *, ...) { return B_ERROR; } 180 status_t BOptionControl::_Reserved_OptionControl_10(void *, ...) { return B_ERROR; } 181 status_t BOptionControl::_Reserved_OptionControl_11(void *, ...) { return B_ERROR; } 182