Style changes, some more error checks, removed ifdeffed broken

behaviour.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40018 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2010-12-29 08:10:25 +00:00
parent 566d89aec9
commit f95b5003de

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2003-2007, Haiku, Inc. * Copyright 2003-2010, Haiku, Inc.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
@ -15,11 +15,6 @@
#include <stdio.h> #include <stdio.h>
// If enabled, behaves like in BeOS R5, in that when you call
// SelectOptionFor() or SetValue(), the selected item isn't marked, and
// so SelectedOption() will return -1. This is broken, IMHO.
#define BEHAVE_LIKE_R5 0
const float kLabelSpace = 8.0; const float kLabelSpace = 8.0;
const float kWidthModifier = 25.0; const float kWidthModifier = 25.0;
const float kHeightModifier = 10.0; const float kHeightModifier = 10.0;
@ -33,11 +28,11 @@ const float kHeightModifier = 10.0;
\param resize Resizing flags. They will be passed to the base class. \param resize Resizing flags. They will be passed to the base class.
\param flags View flags. They will be passed to the base class. \param flags View flags. They will be passed to the base class.
*/ */
BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label, BOptionPopUp::BOptionPopUp(BRect frame, const char* name, const char* label,
BMessage *message, uint32 resize, uint32 flags) BMessage* message, uint32 resize, uint32 flags)
: BOptionControl(frame, name, label, message, resize, flags) : BOptionControl(frame, name, label, message, resize, flags)
{ {
BPopUpMenu *popUp = new BPopUpMenu(label, true, true); BPopUpMenu* popUp = new BPopUpMenu(label, true, true);
fMenuField = new BMenuField(Bounds(), "_menu", label, popUp); fMenuField = new BMenuField(Bounds(), "_menu", label, popUp);
AddChild(fMenuField); AddChild(fMenuField);
} }
@ -53,11 +48,11 @@ BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label,
\param resize Resizing flags. They will be passed to the base class. \param resize Resizing flags. They will be passed to the base class.
\param flags View flags. They will be passed to the base class. \param flags View flags. They will be passed to the base class.
*/ */
BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label, BOptionPopUp::BOptionPopUp(BRect frame, const char* name, const char* label,
BMessage *message, bool fixed, uint32 resize, uint32 flags) BMessage* message, bool fixed, uint32 resize, uint32 flags)
: BOptionControl(frame, name, label, message, resize, flags) : BOptionControl(frame, name, label, message, resize, flags)
{ {
BPopUpMenu *popUp = new BPopUpMenu(label, true, true); BPopUpMenu* popUp = new BPopUpMenu(label, true, true);
fMenuField = new BMenuField(Bounds(), "_menu", label, popUp, fixed); fMenuField = new BMenuField(Bounds(), "_menu", label, popUp, fixed);
AddChild(fMenuField); AddChild(fMenuField);
} }
@ -71,7 +66,7 @@ BOptionPopUp::BOptionPopUp(const char* name, const char* label,
// doesn't get layoutted properly // doesn't get layoutted properly
SetLayout(new BGroupLayout(B_HORIZONTAL)); SetLayout(new BGroupLayout(B_HORIZONTAL));
BPopUpMenu *popUp = new BPopUpMenu(label, true, true); BPopUpMenu* popUp = new BPopUpMenu(label, true, true);
fMenuField = new BMenuField("_menu", label, popUp); fMenuField = new BMenuField("_menu", label, popUp);
AddChild(fMenuField); AddChild(fMenuField);
} }
@ -85,7 +80,7 @@ BOptionPopUp::~BOptionPopUp()
/*! \brief Returns a pointer to the BMenuField used internally. /*! \brief Returns a pointer to the BMenuField used internally.
\return A Pointer to the BMenuField which the class uses internally. \return A Pointer to the BMenuField which the class uses internally.
*/ */
BMenuField * BMenuField*
BOptionPopUp::MenuField() BOptionPopUp::MenuField()
{ {
return fMenuField; return fMenuField;
@ -102,13 +97,13 @@ BOptionPopUp::MenuField()
\c false otherwise. \c false otherwise.
*/ */
bool bool
BOptionPopUp::GetOptionAt(int32 index, const char **outName, int32 *outValue) BOptionPopUp::GetOptionAt(int32 index, const char** outName, int32* outValue)
{ {
bool result = false; bool result = false;
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
if (menu != NULL) { if (menu != NULL) {
BMenuItem *item = menu->ItemAt(index); BMenuItem* item = menu->ItemAt(index);
if (item != NULL) { if (item != NULL) {
if (outName != NULL) if (outName != NULL)
*outName = item->Label(); *outName = item->Label();
@ -129,14 +124,9 @@ BOptionPopUp::GetOptionAt(int32 index, const char **outName, int32 *outValue)
void void
BOptionPopUp::RemoveOptionAt(int32 index) BOptionPopUp::RemoveOptionAt(int32 index)
{ {
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
if (menu != NULL) { if (menu != NULL)
BMenuItem *item = menu->ItemAt(index); delete menu->RemoveItem(index);
if (item != NULL) {
menu->RemoveItem(item);
delete item;
}
}
} }
@ -145,7 +135,7 @@ BOptionPopUp::RemoveOptionAt(int32 index)
int32 int32
BOptionPopUp::CountOptions() const BOptionPopUp::CountOptions() const
{ {
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
return (menu != NULL) ? menu->CountItems() : 0; return (menu != NULL) ? menu->CountItems() : 0;
} }
@ -159,9 +149,9 @@ BOptionPopUp::CountOptions() const
\c B_ERROR if something else happened. \c B_ERROR if something else happened.
*/ */
status_t status_t
BOptionPopUp::AddOptionAt(const char *name, int32 value, int32 index) BOptionPopUp::AddOptionAt(const char* name, int32 value, int32 index)
{ {
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
if (menu == NULL) if (menu == NULL)
return B_ERROR; return B_ERROR;
@ -169,17 +159,21 @@ BOptionPopUp::AddOptionAt(const char *name, int32 value, int32 index)
if (index < 0 || index > numItems) if (index < 0 || index > numItems)
return B_BAD_VALUE; return B_BAD_VALUE;
BMessage *message = MakeValueMessage(value); BMessage* message = MakeValueMessage(value);
if (message == NULL) if (message == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
BMenuItem *newItem = new BMenuItem(name, message); BMenuItem* newItem = new BMenuItem(name, message);
if (newItem == NULL) { if (newItem == NULL) {
delete message; delete message;
return B_NO_MEMORY; return B_NO_MEMORY;
} }
menu->AddItem(newItem, index); if (!menu->AddItem(newItem, index)) {
delete newItem;
return B_NO_MEMORY;
}
newItem->SetTarget(this); newItem->SetTarget(this);
// We didnt' have any items before, so select the newly added one // We didnt' have any items before, so select the newly added one
@ -196,7 +190,7 @@ BOptionPopUp::AddOptionAt(const char *name, int32 value, int32 index)
void void
BOptionPopUp::AllAttached() BOptionPopUp::AllAttached()
{ {
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
if (menu != NULL) { if (menu != NULL) {
float labelWidth = fMenuField->StringWidth(fMenuField->Label()); float labelWidth = fMenuField->StringWidth(fMenuField->Label());
fMenuField->SetDivider(labelWidth + kLabelSpace); fMenuField->SetDivider(labelWidth + kLabelSpace);
@ -206,7 +200,7 @@ BOptionPopUp::AllAttached()
void void
BOptionPopUp::MessageReceived(BMessage *message) BOptionPopUp::MessageReceived(BMessage* message)
{ {
BOptionControl::MessageReceived(message); BOptionControl::MessageReceived(message);
} }
@ -216,12 +210,12 @@ BOptionPopUp::MessageReceived(BMessage *message)
\param text The new label of the control. \param text The new label of the control.
*/ */
void void
BOptionPopUp::SetLabel(const char *text) BOptionPopUp::SetLabel(const char* text)
{ {
BControl::SetLabel(text); BControl::SetLabel(text);
fMenuField->SetLabel(text); fMenuField->SetLabel(text);
// We are not sure the menu can keep the whole // We are not sure the menu can keep the whole
// string as label, so we ask it what label it's got // string as label, so we check against the current label
float newWidth = fMenuField->StringWidth(fMenuField->Label()); float newWidth = fMenuField->StringWidth(fMenuField->Label());
fMenuField->SetDivider(newWidth + kLabelSpace); fMenuField->SetDivider(newWidth + kLabelSpace);
} }
@ -235,23 +229,18 @@ void
BOptionPopUp::SetValue(int32 value) BOptionPopUp::SetValue(int32 value)
{ {
BControl::SetValue(value); BControl::SetValue(value);
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
if (menu == NULL) if (menu == NULL)
return; return;
int32 numItems = menu->CountItems(); int32 numItems = menu->CountItems();
for (int32 i = 0; i < numItems; i++) { for (int32 i = 0; i < numItems; i++) {
BMenuItem *item = menu->ItemAt(i); BMenuItem* item = menu->ItemAt(i);
if (item && item->Message()) { if (item && item->Message()) {
int32 itemValue; int32 itemValue;
item->Message()->FindInt32("be:value", &itemValue); item->Message()->FindInt32("be:value", &itemValue);
if (itemValue == value) { if (itemValue == value) {
item->SetMarked(true); item->SetMarked(true);
#if BEHAVE_LIKE_R5
item->SetMarked(false);
#endif
break; break;
} }
} }
@ -317,22 +306,22 @@ BOptionPopUp::ResizeToPreferred()
\return The index of the selected option. \return The index of the selected option.
*/ */
int32 int32
BOptionPopUp::SelectedOption(const char **outName, int32 *outValue) const BOptionPopUp::SelectedOption(const char** outName, int32* outValue) const
{ {
BMenu *menu = fMenuField->Menu(); BMenu* menu = fMenuField->Menu();
if (menu != NULL) { if (menu == NULL)
BMenuItem *marked = menu->FindMarked(); return B_ERROR;
if (marked != NULL) {
if (outName != NULL) BMenuItem* marked = menu->FindMarked();
*outName = marked->Label(); if (marked == NULL)
if (outValue != NULL) return -1;
marked->Message()->FindInt32("be:value", outValue);
if (outName != NULL)
return menu->IndexOf(marked); *outName = marked->Label();
} if (outValue != NULL)
} marked->Message()->FindInt32("be:value", outValue);
return B_ERROR; return menu->IndexOf(marked);
} }
@ -344,7 +333,7 @@ BOptionPopUp::BOptionPopUp()
} }
BOptionPopUp::BOptionPopUp(const BOptionPopUp &clone) BOptionPopUp::BOptionPopUp(const BOptionPopUp& clone)
: :
BOptionControl(clone.Frame(), "", "", clone.Message()) BOptionControl(clone.Frame(), "", "", clone.Message())
{ {
@ -352,7 +341,7 @@ BOptionPopUp::BOptionPopUp(const BOptionPopUp &clone)
BOptionPopUp & BOptionPopUp &
BOptionPopUp::operator=(const BOptionPopUp & clone) BOptionPopUp::operator=(const BOptionPopUp& clone)
{ {
return *this; return *this;
} }