Cleaned up, added a ToDo comment where it behaves (possibly) strange.

The properties are now the same as in the Be version - there is no reason (IMO) for usage
comments like these; it's a public class, and its scripting usage should be documented in
the BeBook, if anywhere.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12503 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-04-29 00:58:39 +00:00
parent aed75ba2f6
commit 55932743b4
+220 -237
View File
@@ -1,5 +1,5 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2005, Haiku
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"), // copy of this software and associated documentation files (the "Software"),
@@ -24,170 +24,132 @@
// Description: BControl is the base class for user-event handling objects. // Description: BControl is the base class for user-event handling objects.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <string.h>
#include <stdlib.h>
// System Includes -------------------------------------------------------------
#include <Control.h> #include <Control.h>
#include <PropertyInfo.h> #include <PropertyInfo.h>
#include <Window.h> #include <Window.h>
#include <Errors.h> #include <Errors.h>
#include <Debug.h> #include <Debug.h>
// Project Includes ------------------------------------------------------------ #include <string.h>
#include <stdlib.h>
// Local Includes --------------------------------------------------------------
// Local Defines --------------------------------------------------------------- static property_info sPropertyList[] = {
// Globals ---------------------------------------------------------------------
static property_info prop_list[] =
{
{ {
"Enabled", "Enabled",
{ B_GET_PROPERTY, 0 }, { B_GET_PROPERTY, B_SET_PROPERTY },
{ B_DIRECT_SPECIFIER, 0 }, { B_DIRECT_SPECIFIER },
"Returns whether or not the BControl is currently enabled.", 0, NULL, 0,
{ B_BOOL_TYPE, 0 } { B_BOOL_TYPE }
},
{
"Enabled",
{ B_SET_PROPERTY, 0 },
{ B_DIRECT_SPECIFIER, 0 },
"Enables or disables the BControl.", 0,
{ B_BOOL_TYPE, 0 }
}, },
{ {
"Label", "Label",
{ B_GET_PROPERTY, 0 }, { B_GET_PROPERTY, B_SET_PROPERTY },
{ B_DIRECT_SPECIFIER, 0 }, { B_DIRECT_SPECIFIER },
"Returns the BControl's label.", 0, NULL, 0,
{ B_STRING_TYPE, 0 } { B_STRING_TYPE }
},
{
"Label",
{ B_SET_PROPERTY, 0 },
{ B_DIRECT_SPECIFIER, 0 },
"Sets the label of the BControl.", 0,
{ B_STRING_TYPE, 0 }
}, },
{ {
"Value", "Value",
{ B_GET_PROPERTY, 0 }, { B_GET_PROPERTY, B_SET_PROPERTY },
{ B_DIRECT_SPECIFIER, 0 }, { B_DIRECT_SPECIFIER },
"Returns the BControl's value.", 0, NULL, 0,
{ B_INT32_TYPE, 0 } { B_INT32_TYPE }
}, },
{ {}
"Value",
{ B_SET_PROPERTY, 0 },
{ B_DIRECT_SPECIFIER, 0 },
"Sets the value of the BControl.", 0,
{ B_INT32_TYPE, 0 },
},
{ 0 }
}; };
//------------------------------------------------------------------------------
BControl::BControl(BRect frame, const char *name, const char *label, BControl::BControl(BRect frame, const char *name, const char *label,
BMessage *message, uint32 resizingMode, uint32 flags) BMessage *message, uint32 resizingMode, uint32 flags)
: BView(frame, name, resizingMode, flags) : BView(frame, name, resizingMode, flags)
{ {
InitData(NULL); InitData(NULL);
SetLabel(label); SetLabel(label);
SetMessage(message); SetMessage(message);
} }
//------------------------------------------------------------------------------
BControl::~BControl() BControl::~BControl()
{ {
if (fLabel) free(fLabel);
free(fLabel);
SetMessage(NULL); SetMessage(NULL);
} }
//------------------------------------------------------------------------------
BControl::BControl(BMessage *archive) BControl::BControl(BMessage *archive)
: BView(archive) : BView(archive)
{ {
InitData(archive); InitData(archive);
BMessage message; BMessage message;
if (archive->FindMessage("_msg", &message) == B_OK) if (archive->FindMessage("_msg", &message) == B_OK)
SetMessage(new BMessage(message)); SetMessage(new BMessage(message));
const char *label; const char *label;
if (archive->FindString("_label", &label) != B_OK) if (archive->FindString("_label", &label) != B_OK)
SetLabel(label); SetLabel(label);
int32 value; int32 value;
if (archive->FindInt32("_val", &value) != B_OK) if (archive->FindInt32("_val", &value) != B_OK)
SetValue(value); SetValue(value);
bool toggle; bool toggle;
if (archive->FindBool("_disable", &toggle) != B_OK) if (archive->FindBool("_disable", &toggle) != B_OK)
SetEnabled(!toggle); SetEnabled(!toggle);
if (archive->FindBool("be:wants_nav", &toggle) != B_OK) if (archive->FindBool("be:wants_nav", &toggle) != B_OK)
fWantsNav = toggle; fWantsNav = toggle;
} }
//------------------------------------------------------------------------------
BArchivable *BControl::Instantiate(BMessage *archive)
BArchivable *
BControl::Instantiate(BMessage *archive)
{ {
if (validate_instantiation(archive, "BControl")) if (validate_instantiation(archive, "BControl"))
return new BControl(archive); return new BControl(archive);
else
return NULL; return NULL;
} }
//------------------------------------------------------------------------------
status_t BControl::Archive(BMessage *archive, bool deep) const
status_t
BControl::Archive(BMessage *archive, bool deep) const
{ {
status_t err = BView::Archive(archive, deep); status_t status = BView::Archive(archive, deep);
if (err != B_OK) if (status == B_OK && Message())
return err; status = archive->AddMessage("_msg", Message ());
if (Message()) if (status == B_OK && fLabel)
err = archive->AddMessage("_msg", Message ()); status = archive->AddString("_label", fLabel);
if (err != B_OK) if (status == B_OK && fValue != B_CONTROL_OFF)
return err; status = archive->AddInt32("_val", fValue);
if (fLabel) if (status == B_OK && !fEnabled)
err = archive->AddString("_label", fLabel); status = archive->AddBool("_disable", true);
if (err != B_OK ) return status;
return err;
if (fValue != B_CONTROL_OFF)
err = archive->AddInt32("_val", fValue);
if (err != B_OK)
return err;
if (!fEnabled)
err = archive->AddBool("_disable", true);
return err;
} }
//------------------------------------------------------------------------------
void BControl::WindowActivated(bool active)
void
BControl::WindowActivated(bool active)
{ {
BView::WindowActivated(active); BView::WindowActivated(active);
if (IsFocus()) if (IsFocus())
Invalidate(Bounds()); Invalidate(Bounds());
} }
//------------------------------------------------------------------------------
void BControl::AttachedToWindow()
void
BControl::AttachedToWindow()
{ {
if (Parent()) if (Parent()) {
{ // inherit the color from parent
rgb_color color = Parent()->ViewColor(); rgb_color color = Parent()->ViewColor();
SetViewColor(color); SetViewColor(color);
@@ -195,77 +157,58 @@ void BControl::AttachedToWindow()
} }
if (!Messenger().IsValid()) if (!Messenger().IsValid())
BInvoker::SetTarget(BMessenger(Window(), NULL)); SetTarget(Window());
BView::AttachedToWindow(); BView::AttachedToWindow();
} }
//------------------------------------------------------------------------------
void BControl::MessageReceived(BMessage *message)
{
bool handled = false;
BMessage reply(B_REPLY);
if (message->what == B_GET_PROPERTY || message->what == B_SET_PROPERTY)
{ void
BPropertyInfo propInfo(prop_list); BControl::MessageReceived(BMessage *message)
{
if (message->what == B_GET_PROPERTY || message->what == B_SET_PROPERTY) {
BMessage reply(B_REPLY);
bool handled = false;
BMessage specifier; BMessage specifier;
int32 index; int32 index;
int32 form; int32 form;
const char *property; const char *property;
if (message->GetCurrentSpecifier(&index, &specifier, &form, &property) == B_OK) {
if (message->GetCurrentSpecifier(&index, &specifier, &form, &property) == B_OK) if (strcmp(property, "Label") == 0) {
{ if (message->what == B_GET_PROPERTY) {
if (strcmp(property, "Label") == 0)
{
if (message->what == B_GET_PROPERTY)
{
reply.AddString("result", fLabel); reply.AddString("result", fLabel);
handled = true; handled = true;
} } else {
else // B_GET_PROPERTY
{
const char *label; const char *label;
if (message->FindString("data", &label) == B_OK) {
if (message->FindString("data", &label) == B_OK)
{
SetLabel(label); SetLabel(label);
reply.AddInt32("error", B_OK); reply.AddInt32("error", B_OK);
handled = true; handled = true;
} }
} }
} } else if (strcmp(property, "Value") == 0) {
else if (strcmp(property, "Value") == 0) if (message->what == B_GET_PROPERTY) {
{
if (message->what == B_GET_PROPERTY)
{
reply.AddInt32("result", fValue); reply.AddInt32("result", fValue);
handled = true; handled = true;
} } else {
else // B_GET_PROPERTY
{
int32 value; int32 value;
if (message->FindInt32("data", &value) == B_OK) {
if (message->FindInt32("data", &value) == B_OK)
{
SetValue(value); SetValue(value);
reply.AddInt32("error", B_OK); reply.AddInt32("error", B_OK);
handled = true; handled = true;
} }
} }
} } else if (strcmp(property, "Enabled") == 0) {
else if (strcmp(property, "Enabled") == 0) if (message->what == B_GET_PROPERTY) {
{
if (message->what == B_GET_PROPERTY)
{
reply.AddBool("result", fEnabled); reply.AddBool("result", fEnabled);
handled = true; handled = true;
} } else {
else // B_GET_PROPERTY
{
bool enabled; bool enabled;
if (message->FindBool("data", &enabled) == B_OK) {
if (message->FindBool("data", &enabled) == B_OK)
{
SetEnabled(enabled); SetEnabled(enabled);
reply.AddInt32("error", B_OK); reply.AddInt32("error", B_OK);
handled = true; handled = true;
@@ -273,109 +216,120 @@ void BControl::MessageReceived(BMessage *message)
} }
} }
} }
if (handled) {
message->SendReply(&reply);
return;
}
} }
if (handled) BView::MessageReceived(message);
message->SendReply(&reply);
else
BView::MessageReceived(message);
} }
//------------------------------------------------------------------------------
void BControl::MakeFocus(bool focused)
void
BControl::MakeFocus(bool focused)
{ {
if (focused == IsFocus()) if (focused == IsFocus())
return; return;
BView::MakeFocus(focused); BView::MakeFocus(focused);
if(Window()) if (Window()) {
{
fFocusChanging = true; fFocusChanging = true;
Invalidate(Bounds()); Invalidate(Bounds());
Flush(); Flush();
fFocusChanging = false; fFocusChanging = false;
} }
} }
//------------------------------------------------------------------------------
void BControl::KeyDown(const char *bytes, int32 numBytes)
void
BControl::KeyDown(const char *bytes, int32 numBytes)
{ {
if (*bytes == B_ENTER || *bytes == B_SPACE) if (*bytes == B_ENTER || *bytes == B_SPACE) {
{
if (!fEnabled) if (!fEnabled)
return; return;
if (Value()) SetValue(Value() ? B_CONTROL_OFF : B_CONTROL_ON);
SetValue(B_CONTROL_OFF);
else
SetValue(B_CONTROL_ON);
Invoke(); Invoke();
} } else
else
BView::KeyDown(bytes, numBytes); BView::KeyDown(bytes, numBytes);
} }
//------------------------------------------------------------------------------
void BControl::MouseDown(BPoint point)
void
BControl::MouseDown(BPoint point)
{ {
BView::MouseDown(point); BView::MouseDown(point);
} }
//------------------------------------------------------------------------------
void BControl::MouseUp(BPoint point)
void
BControl::MouseUp(BPoint point)
{ {
BView::MouseUp(point); BView::MouseUp(point);
} }
//------------------------------------------------------------------------------
void BControl::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
void
BControl::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
{ {
BView::MouseMoved(point, transit, message); BView::MouseMoved(point, transit, message);
} }
//------------------------------------------------------------------------------
void BControl::DetachedFromWindow()
void
BControl::DetachedFromWindow()
{ {
BView::DetachedFromWindow(); BView::DetachedFromWindow();
} }
//------------------------------------------------------------------------------
void BControl::SetLabel(const char *string)
void
BControl::SetLabel(const char *string)
{ {
if (fLabel && string && strcmp(fLabel, string) == 0) if (fLabel && string && strcmp(fLabel, string) == 0)
return; return;
if (fLabel) free(fLabel);
free(fLabel); fLabel = strdup(string ? string : B_EMPTY_STRING);
if (string)
fLabel = strdup(string);
else
fLabel = strdup(B_EMPTY_STRING);
Invalidate(); Invalidate();
} }
//------------------------------------------------------------------------------
const char *BControl::Label() const
const char *
BControl::Label() const
{ {
return fLabel; return fLabel;
} }
//------------------------------------------------------------------------------
void BControl::SetValue(int32 value)
void
BControl::SetValue(int32 value)
{ {
if (fValue == value) if (fValue == value)
return; return;
fValue = value; fValue = value;
if (Window()) if (Window()) {
{
Invalidate(Bounds()); Invalidate(Bounds());
Flush(); Flush();
} }
} }
//------------------------------------------------------------------------------
int32 BControl::Value() const
int32
BControl::Value() const
{ {
return fValue; return fValue;
} }
//------------------------------------------------------------------------------
void BControl::SetEnabled(bool enabled)
void
BControl::SetEnabled(bool enabled)
{ {
if (fEnabled == enabled) if (fEnabled == enabled)
return; return;
@@ -387,45 +341,49 @@ void BControl::SetEnabled(bool enabled)
else else
BView::SetFlags(Flags() & ~B_NAVIGABLE); BView::SetFlags(Flags() & ~B_NAVIGABLE);
if (Window()) if (Window()) {
{
Invalidate(Bounds()); Invalidate(Bounds());
Flush(); Flush();
} }
} }
//------------------------------------------------------------------------------
bool BControl::IsEnabled() const
bool
BControl::IsEnabled() const
{ {
return fEnabled; return fEnabled;
} }
//------------------------------------------------------------------------------
void BControl::GetPreferredSize(float *width, float *height)
void
BControl::GetPreferredSize(float *_width, float *_height)
{ {
BView::GetPreferredSize(width, height); BView::GetPreferredSize(_width, _height);
} }
//------------------------------------------------------------------------------
void BControl::ResizeToPreferred()
void
BControl::ResizeToPreferred()
{ {
BView::ResizeToPreferred(); BView::ResizeToPreferred();
} }
//------------------------------------------------------------------------------
status_t BControl::Invoke(BMessage *message)
status_t
BControl::Invoke(BMessage *message)
{ {
bool notify = false; bool notify = false;
uint32 kind = InvokeKind(&notify); uint32 kind = InvokeKind(&notify);
BMessage clone(kind);
status_t err = B_BAD_VALUE;
if (!message && !notify) if (!message && !notify)
message = Message(); message = Message();
if (!message) BMessage clone(kind);
{
if (!message) {
if (!IsWatched()) if (!IsWatched())
return err; return B_BAD_VALUE;
} } else
else
clone = *message; clone = *message;
clone.AddInt64("when", (int64)system_time()); clone.AddInt64("when", (int64)system_time());
@@ -433,85 +391,110 @@ status_t BControl::Invoke(BMessage *message)
clone.AddInt32("be:value", fValue); clone.AddInt32("be:value", fValue);
clone.AddMessenger("be:sender", BMessenger(this)); clone.AddMessenger("be:sender", BMessenger(this));
// ToDo: is this correct? If message == NULL (even if IsWatched()), we always return B_BAD_VALUE
status_t err;
if (message) if (message)
err = BInvoker::Invoke(&clone); err = BInvoker::Invoke(&clone);
else
err = B_BAD_VALUE;
// TODO: asynchronous messaging // TODO: asynchronous messaging
SendNotices(kind, &clone); SendNotices(kind, &clone);
return err; return err;
} }
//------------------------------------------------------------------------------
BHandler *BControl::ResolveSpecifier(BMessage *message, int32 index,
BMessage *specifier, int32 what,
const char *property)
{
BPropertyInfo propInfo(prop_list);
if (propInfo.FindMatch(message, 0, specifier, what, property) < B_OK)
return BView::ResolveSpecifier(message, index, specifier, what, BHandler *
property); BControl::ResolveSpecifier(BMessage *message, int32 index,
else BMessage *specifier, int32 what, const char *property)
{
BPropertyInfo propInfo(sPropertyList);
if (propInfo.FindMatch(message, 0, specifier, what, property) >= B_OK)
return this; return this;
return BView::ResolveSpecifier(message, index, specifier, what,
property);
} }
//------------------------------------------------------------------------------
status_t BControl::GetSupportedSuites(BMessage *message)
status_t
BControl::GetSupportedSuites(BMessage *message)
{ {
message->AddString("suites", "suite/vnd.Be-control"); message->AddString("suites", "suite/vnd.Be-control");
BPropertyInfo prop_info(prop_list); BPropertyInfo propInfo(sPropertyList);
message->AddFlat("messages", &prop_info); message->AddFlat("messages", &propInfo);
return BView::GetSupportedSuites(message); return BView::GetSupportedSuites(message);
} }
//------------------------------------------------------------------------------
void BControl::AllAttached()
void
BControl::AllAttached()
{ {
BView::AllAttached(); BView::AllAttached();
} }
//------------------------------------------------------------------------------
void BControl::AllDetached()
void
BControl::AllDetached()
{ {
BView::AllDetached(); BView::AllDetached();
} }
//------------------------------------------------------------------------------
status_t BControl::Perform(perform_code d, void *arg)
status_t
BControl::Perform(perform_code d, void *arg)
{ {
return BView::Perform(d, arg); return BView::Perform(d, arg);
} }
//------------------------------------------------------------------------------
bool BControl::IsFocusChanging() const
bool
BControl::IsFocusChanging() const
{ {
return fFocusChanging; return fFocusChanging;
} }
//------------------------------------------------------------------------------
bool BControl::IsTracking() const
bool
BControl::IsTracking() const
{ {
return fTracking; return fTracking;
} }
//------------------------------------------------------------------------------
void BControl::SetTracking(bool state)
void
BControl::SetTracking(bool state)
{ {
fTracking = state; fTracking = state;
} }
//------------------------------------------------------------------------------
void BControl::SetValueNoUpdate(int32 value)
void
BControl::SetValueNoUpdate(int32 value)
{ {
fValue = value; fValue = value;
} }
//------------------------------------------------------------------------------
void BControl::_ReservedControl1() {} void BControl::_ReservedControl1() {}
void BControl::_ReservedControl2() {} void BControl::_ReservedControl2() {}
void BControl::_ReservedControl3() {} void BControl::_ReservedControl3() {}
void BControl::_ReservedControl4() {} void BControl::_ReservedControl4() {}
//------------------------------------------------------------------------------
BControl &BControl::operator=(const BControl &)
BControl &
BControl::operator=(const BControl &)
{ {
return *this; return *this;
} }
//------------------------------------------------------------------------------
void BControl::InitData(BMessage *data)
void
BControl::InitData(BMessage *data)
{ {
fLabel = NULL; fLabel = NULL;
SetLabel(B_EMPTY_STRING); SetLabel(B_EMPTY_STRING);
@@ -524,4 +507,4 @@ void BControl::InitData(BMessage *data)
if (data && data->HasString("_fname")) if (data && data->HasString("_fname"))
SetFont(be_plain_font, B_FONT_FAMILY_AND_STYLE); SetFont(be_plain_font, B_FONT_FAMILY_AND_STYLE);
} }
//------------------------------------------------------------------------------