2 * Copyright 2003-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
6 * Stefano Ceccherini <stefano.ceccherini@gmail.com>
9 #include <GroupLayout.h>
10 #include <MenuField.h>
12 #include <OptionPopUp.h>
13 #include <PopUpMenu.h>
18 const float kLabelSpace
= 8.0;
19 const float kWidthModifier
= 25.0;
20 const float kHeightModifier
= 10.0;
23 /*! \brief Creates and initializes a BOptionPopUp.
24 \param frame The frame of the control.
25 \param name The name of the control.
26 \param label The label which will be displayed by the control.
27 \param message The message which the control will send when operated.
28 \param resize Resizing flags. They will be passed to the base class.
29 \param flags View flags. They will be passed to the base class.
31 BOptionPopUp::BOptionPopUp(BRect frame
, const char* name
, const char* label
,
32 BMessage
* message
, uint32 resize
, uint32 flags
)
33 : BOptionControl(frame
, name
, label
, message
, resize
, flags
)
35 BPopUpMenu
* popUp
= new BPopUpMenu(label
, true, true);
36 fMenuField
= new BMenuField(Bounds(), "_menu", label
, popUp
);
41 /*! \brief Creates and initializes a BOptionPopUp.
42 \param frame The frame of the control.
43 \param name The name of the control.
44 \param label The label which will be displayed by the control.
45 \param message The message which the control will send when operated.
46 \param fixed It's passed to the BMenuField constructor. If it's true,
47 the BMenuField size will never change.
48 \param resize Resizing flags. They will be passed to the base class.
49 \param flags View flags. They will be passed to the base class.
51 BOptionPopUp::BOptionPopUp(BRect frame
, const char* name
, const char* label
,
52 BMessage
* message
, bool fixed
, uint32 resize
, uint32 flags
)
53 : BOptionControl(frame
, name
, label
, message
, resize
, flags
)
55 BPopUpMenu
* popUp
= new BPopUpMenu(label
, true, true);
56 fMenuField
= new BMenuField(Bounds(), "_menu", label
, popUp
, fixed
);
61 BOptionPopUp::BOptionPopUp(const char* name
, const char* label
,
62 BMessage
* message
, uint32 flags
)
63 : BOptionControl(name
, label
, message
, flags
)
65 // TODO: Is this really needed ? Without this, the view
66 // doesn't get layoutted properly
67 SetLayout(new BGroupLayout(B_HORIZONTAL
));
69 BPopUpMenu
* popUp
= new BPopUpMenu(label
, true, true);
70 fMenuField
= new BMenuField("_menu", label
, popUp
);
75 BOptionPopUp::~BOptionPopUp()
80 /*! \brief Returns a pointer to the BMenuField used internally.
81 \return A Pointer to the BMenuField which the class uses internally.
84 BOptionPopUp::MenuField()
90 /*! \brief Gets the option at the given index.
91 \param index The option's index.
92 \param outName A pointer to a string which will held the option's name,
93 as soon as the function returns.
94 \param outValue A pointer to an integer which will held the option's value,
95 as soon as the funciton returns.
96 \return \c true if The wanted option was found,
100 BOptionPopUp::GetOptionAt(int32 index
, const char** outName
, int32
* outValue
)
103 BMenu
* menu
= fMenuField
->Menu();
106 BMenuItem
* item
= menu
->ItemAt(index
);
109 *outName
= item
->Label();
110 if (outValue
!= NULL
&& item
->Message() != NULL
)
111 item
->Message()->FindInt32("be:value", outValue
);
121 /*! \brief Removes the option at the given index.
122 \param index The index of the option to remove.
125 BOptionPopUp::RemoveOptionAt(int32 index
)
127 BMenu
* menu
= fMenuField
->Menu();
129 delete menu
->RemoveItem(index
);
133 /*! \brief Returns the amount of "Options" (entries) contained in the control.
136 BOptionPopUp::CountOptions() const
138 BMenu
* menu
= fMenuField
->Menu();
139 return (menu
!= NULL
) ? menu
->CountItems() : 0;
143 /*! \brief Adds an option to the control, at the given position.
144 \param name The name of the option to add.
145 \param value The value of the option.
146 \param index The index which the new option will have in the control.
147 \return \c B_OK if the option was added succesfully,
148 \c B_BAD_VALUE if the given index was invalid.
149 \c B_ERROR if something else happened.
152 BOptionPopUp::AddOptionAt(const char* name
, int32 value
, int32 index
)
154 BMenu
* menu
= fMenuField
->Menu();
158 int32 numItems
= menu
->CountItems();
159 if (index
< 0 || index
> numItems
)
162 BMessage
* message
= MakeValueMessage(value
);
166 BMenuItem
* newItem
= new BMenuItem(name
, message
);
167 if (newItem
== NULL
) {
172 if (!menu
->AddItem(newItem
, index
)) {
177 newItem
->SetTarget(this);
179 // We didnt' have any items before, so select the newly added one
187 // BeOS R5 compatibility, do not remove
189 BOptionPopUp::AllAttached()
191 BOptionControl::AllAttached();
195 /*! \brief Sets the divider for the BMenuField and target the menu items to ourselves.
198 BOptionPopUp::AttachedToWindow()
200 BOptionControl::AttachedToWindow();
202 BMenu
* menu
= fMenuField
->Menu();
204 float labelWidth
= fMenuField
->StringWidth(fMenuField
->Label());
205 if (labelWidth
> 0.f
)
206 labelWidth
+= kLabelSpace
;
207 fMenuField
->SetDivider(labelWidth
);
208 menu
->SetTargetForItems(this);
214 BOptionPopUp::MessageReceived(BMessage
* message
)
216 BOptionControl::MessageReceived(message
);
220 /*! \brief Set the label of the control.
221 \param text The new label of the control.
224 BOptionPopUp::SetLabel(const char* text
)
226 BControl::SetLabel(text
);
227 fMenuField
->SetLabel(text
);
228 // We are not sure the menu can keep the whole
229 // string as label, so we check against the current label
230 float newWidth
= fMenuField
->StringWidth(fMenuField
->Label());
232 newWidth
+= kLabelSpace
;
233 fMenuField
->SetDivider(newWidth
);
237 /*! \brief Set the control's value.
238 \param value The new value of the control.
239 Selects the option which has the given value.
242 BOptionPopUp::SetValue(int32 value
)
244 BControl::SetValue(value
);
245 BMenu
* menu
= fMenuField
->Menu();
249 int32 numItems
= menu
->CountItems();
250 for (int32 i
= 0; i
< numItems
; i
++) {
251 BMenuItem
* item
= menu
->ItemAt(i
);
252 if (item
&& item
->Message()) {
254 item
->Message()->FindInt32("be:value", &itemValue
);
255 if (itemValue
== value
) {
256 item
->SetMarked(true);
264 /*! \brief Enables or disables the control.
265 \param state The new control's state.
268 BOptionPopUp::SetEnabled(bool state
)
270 BOptionControl::SetEnabled(state
);
272 fMenuField
->SetEnabled(state
);
276 /*! \brief Gets the preferred size for the control.
277 \param width A pointer to a float which will held the control's
279 \param height A pointer to a float which will held the control's
283 BOptionPopUp::GetPreferredSize(float* _width
, float* _height
)
286 fMenuField
->GetPreferredSize(&width
, &height
);
288 if (_height
!= NULL
) {
289 font_height fontHeight
;
290 GetFontHeight(&fontHeight
);
292 *_height
= max_c(height
, fontHeight
.ascent
+ fontHeight
.descent
293 + fontHeight
.leading
+ kHeightModifier
);
296 if (_width
!= NULL
) {
297 width
+= fMenuField
->StringWidth(BControl::Label())
298 + kLabelSpace
+ kWidthModifier
;
304 /*! \brief Resizes the control to its preferred size.
307 BOptionPopUp::ResizeToPreferred()
310 GetPreferredSize(&width
, &height
);
311 ResizeTo(width
, height
);
313 float newWidth
= fMenuField
->StringWidth(BControl::Label());
314 fMenuField
->SetDivider(newWidth
+ kLabelSpace
);
318 /*! \brief Gets the currently selected option.
319 \param outName A pointer to a string which will held the option's name.
320 \param outValue A pointer to an integer which will held the option's value.
321 \return The index of the selected option.
324 BOptionPopUp::SelectedOption(const char** outName
, int32
* outValue
) const
326 BMenu
* menu
= fMenuField
->Menu();
330 BMenuItem
* marked
= menu
->FindMarked();
335 *outName
= marked
->Label();
336 if (outValue
!= NULL
)
337 marked
->Message()->FindInt32("be:value", outValue
);
339 return menu
->IndexOf(marked
);
343 // Private Unimplemented
344 BOptionPopUp::BOptionPopUp()
346 BOptionControl(BRect(), "", "", NULL
)
351 BOptionPopUp::BOptionPopUp(const BOptionPopUp
& clone
)
353 BOptionControl(clone
.Frame(), "", "", clone
.Message())
359 BOptionPopUp::operator=(const BOptionPopUp
& clone
)
366 status_t
BOptionPopUp::_Reserved_OptionControl_0(void *, ...) { return B_ERROR
; }
367 status_t
BOptionPopUp::_Reserved_OptionControl_1(void *, ...) { return B_ERROR
; }
368 status_t
BOptionPopUp::_Reserved_OptionControl_2(void *, ...) { return B_ERROR
; }
369 status_t
BOptionPopUp::_Reserved_OptionControl_3(void *, ...) { return B_ERROR
; }
370 status_t
BOptionPopUp::_Reserved_OptionPopUp_0(void *, ...) { return B_ERROR
; }
371 status_t
BOptionPopUp::_Reserved_OptionPopUp_1(void *, ...) { return B_ERROR
; }
372 status_t
BOptionPopUp::_Reserved_OptionPopUp_2(void *, ...) { return B_ERROR
; }
373 status_t
BOptionPopUp::_Reserved_OptionPopUp_3(void *, ...) { return B_ERROR
; }
374 status_t
BOptionPopUp::_Reserved_OptionPopUp_4(void *, ...) { return B_ERROR
; }
375 status_t
BOptionPopUp::_Reserved_OptionPopUp_5(void *, ...) { return B_ERROR
; }
376 status_t
BOptionPopUp::_Reserved_OptionPopUp_6(void *, ...) { return B_ERROR
; }
377 status_t
BOptionPopUp::_Reserved_OptionPopUp_7(void *, ...) { return B_ERROR
; }