📄 eccpropertyhandler.cpp
字号:
{
if (!PropertyExists(comp, prName)) return NULL;
if (IsClass(comp, prName)) return NULL;
return GetPropValue(comp, prName, true);
// Parameter true: Strings are preferred returntype (see TypeInfo.pas).
}
//---------------------------------------------------------------------------
Variant TPropertyHandler::GetValue(String prName)
/* Same as other GetValue() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return NULL;
return GetValue(FDefaultComponent, prName);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
String TPropertyHandler::GetNameValue(TComponent* comp, String prName)
/* Returns "PropertyName=Value" for property prName of comp.
If property does not exist the return value is "".
If property is a class (ie. TFont) the return value is a string which
contains the returnstring from GetClassNamesValues().
*/
{
if (!PropertyExists(comp, prName)) return "";
if (IsClass(comp, prName)) return GetClassNamesValues(comp, prName);
return BuildNameValue(comp, prName);
}
//---------------------------------------------------------------------------
String TPropertyHandler::GetNameValue(String prName)
/* Same as other GetNameValue() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return "";
return GetNameValue(FDefaultComponent, prName);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
String TPropertyHandler::GetNamesValues(TComponent* comp, String prNames)
/* Returns "PropertyName.Property=Value" strings for all passed prNames
(including subprops) of comp as a String.
Note that multiple props should be seperated by ';' and are all in one string.
The Name of the comp is not a part of the result. (ie. Not f_Main.Left=xx but Left=xx).
-Example:
{
TPropertyHandler ph;
Memo1->Lines->Text = ph.GetNamesValues(CheckBox1, "Name;Checked;Cursor" );
}
*/
{
String list, prop, name1, name2;
int prop_start = 1, prop_end, sep;
while (prop_start < prNames.Length())
{
// Extract (next) prop from props:
prop_end = prNames.Pos(";"); // Find end of prop.
if (!prop_end) prop_end = prNames.Length() + 1; // Last prop.
else prNames[prop_end] = ' '; // Remove ';'.
prop = prNames.SubString(prop_start, prop_end - prop_start);
prop_start = prop_end + 1;
// Get the value:
if (!PropertyExists(comp, prop)) continue;
if (IsClass(comp, prop))
{ list = list + GetClassNamesValues(comp, prop) + "\n"; }
else list = list + BuildNameValue(comp, prop) + "\n";
}
return list;
}
//---------------------------------------------------------------------------
String TPropertyHandler::GetNamesValues(String prNames)
/* Same as other GetNamesValues() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return "";
return GetNamesValues(FDefaultComponent, prNames);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
String TPropertyHandler::GetAllNamesValues(TComponent* comp)
/* Returns "PropertyName.Property=Value" string for all properties
(including subprops) of comp, this list is sorted.
Events are not returned.
*/
{
String list;
TStringList *tempList = new TStringList;
try
{
tempList->Sorted = true;
PTypeInfo TypeInfo = (PTypeInfo)comp->ClassInfo();
PPropList PropList = new TPropList;
GetPropInfos(TypeInfo, PropList);
String data;
for (int i=0; i < PropertyCount(comp); i++)
{
String prName = String(PropList[i]->Name);
if (PropIsType(comp, prName, tkMethod)) continue; // Skip events.
if (IsClass(comp, prName))
{ data = GetClassNamesValues(comp, prName); }
else data = BuildNameValue(comp, prName);
if (!data.IsEmpty()) tempList->Append( data );
}
delete[] PropList;
list = tempList->Text;
}
__finally
{
delete tempList;
}
return list;
}
//---------------------------------------------------------------------------
String TPropertyHandler::GetAllNamesValues()
/* Same as other GetAllNamesValues() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return "";
return GetAllNamesValues(FDefaultComponent);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void TPropertyHandler::SetValue(TComponent* comp, String prName, Variant prValue)
/* There seems to be a bug(?) in the SetPropValue function of TypInfo.pas
when using the Delphi class Variant.
-Example:
{
TPropertyHandler ph(CheckBox1);
ph.SetValue("Checked", 1); // > Ok.
ph.SetValue("Checked", "true"); // > Ok.
ph.SetValue("Checked", true); // >> WRONG!
}
The last line does not work as expected because true is passed as value -1,
instead of +1, and SetPropValue handles the negative value incorrect.
*/
{
if ((prValue.Type() == varBoolean) && (prValue == -1)) prValue = 1; // Fixes the bug.
SetPropValue(comp, prName, prValue);
}
//---------------------------------------------------------------------------
void TPropertyHandler::SetValue(String prName, Variant prValue)
{
if (!DefaultComponentExists()) return;
SetValue(FDefaultComponent, prName, prValue);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void TPropertyHandler::SetNameValue(TComponent* comp, String prName_Value)
/* prName_Value is assumed to be formatted like:
"propname.subprop=value" or "propname=value".
Make sure this is correct, else this func will not work as expected.
If prName_Value is a multi-line string, SetNamesValues() will be called.
Returns false if an error occured.
*/
{
if (prName_Value.LastDelimiter("\r\n"))
{ // Multi-line string, so call SetNamesValues():
SetNamesValues(comp, prName_Value);
return;
}
String name2, name1, value; // Reads like this: "name2.name1=value".
int sep = prName_Value.Pos("=");
String names = prName_Value.SubString(1, sep - 1);
value = prName_Value.SubString(sep + 1, prName_Value.Length() - sep + 1);
sep = names.Pos(".");
if (sep)
{ // Format is "propname.subprop=val":
name1 = names.SubString(sep + 1, names.Length() - sep + 1);
name2 = names.SubString(1, sep - 1);
// name2 is the subcomp of comp.
if (!IsPublishedProp(comp, name2)) // No warning.
{ /* name2 is not a component of comp,
but it might be a component of its own,
like CheckBox1 is a sub-component of Form1. */
if ((comp = GetSubComponent(comp, name2)) == NULL) return;
else
{
SetPropValue(comp, name1, value);
return;
}
}
if (!IsClass(comp, name2)) return;
// Get pointer to subObj:
TObject *subObj = (TObject *)GetOrdProp(comp, name2);
if (subObj == NULL) return;
SetPropValue(subObj, name1, value);
}
else
{ // Format is "propname=val":
SetPropValue(comp, names, value);
}
}
//---------------------------------------------------------------------------
void TPropertyHandler::SetNameValue(String prName_Value)
{
if (!DefaultComponentExists()) return;
SetNameValue(FDefaultComponent, prName_Value);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void TPropertyHandler::SetNamesValues(TComponent* comp, String prNames_Values)
/* prNames_Values is assumed to be a collection of lines
seperated by '\n' (ie. Text property of TStringList).
Every line should be formatted like:
"propname.subprop=value" or "propname=value".
It calls SetNameValue() for every line in prNames_Values.
*/
{
// Process every line:
TStringList *list = new TStringList;
try
{
list->Text = prNames_Values;
int pos = 0;
while (pos < list->Count)
{
SetNameValue(comp, list->Strings[pos]);
++pos;
}
}
__finally
{
delete list;
}
}
//---------------------------------------------------------------------------
void TPropertyHandler::SetNamesValues(String prNames_Values)
{
if (!DefaultComponentExists()) return;
SetNamesValues(FDefaultComponent, prNames_Values);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
TComponent* TPropertyHandler::GetSubComponent(TComponent* comp, String prName)
/* Returns a TComponent* for a child component of comp with name prName.
If there is no child-component return value is NULL.
*/
{
for (int i = 0; i < comp->ComponentCount; i++)
{
if (comp->Components[i]->Name == prName) return comp->Components[i];
}
return NULL;
}
//---------------------------------------------------------------------------
TComponent* TPropertyHandler::GetSubComponent(String prName)
{
if (!DefaultComponentExists()) return NULL;
return GetSubComponent(FDefaultComponent, prName);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//===========================================================================
} // namespace ecc;
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -