📄 eccpropertyhandler.cpp
字号:
/* ==========================================================================
Ecc - Erik's Code Collection
Copyright (C) 2003 - Erik Dienske
This file is part of Ecc.
Ecc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Ecc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ecc; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
===========================================================================*/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "EccPropertyHandler.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
/* TPropertyHandler v1.0
=====================
Author: Erik Dienske
TPropertyHandler allows control of the properties and values of
descendants of TComponent (VCL components).
Every function has a description, take a look at the header file too.
------
Example 1:
TPropertyHandler ph(f_Main);
Caption = ph.GetValue("BorderIcons");
Example 2:
TPropertyHandler ph;
Caption = ph.GetNameValue(Form1, "WindowState");
Example 3:
TPropertyHandler* ph = new TPropertyHandler(CheckBox1);
Caption = ph.SetValue("Checked","true");
delete ph;
------
If you #define PROPERTYHANDLER_SHOW_WARNINGS warnings will be displayed
(using ShowMessage()) if a property does not exist or if the
default Component is not defined - for debugging purposes.
------
If a returned string contains several lines it does not display nice in
components like a Memo if you use: Memo->Lines->Add ..
but is does look right if you use: Memo->Lines->Text = ..
*/
//===========================================================================
namespace ecc {
//===========================================================================
//===========================================================================
//== Constructors and destructor:
//===========================================================================
TPropertyHandler::TPropertyHandler(void)
/* Constructor that does not set a default TComponent.
*/
{
FDefaultComponent = NULL;
}
//---------------------------------------------------------------------------
TPropertyHandler::TPropertyHandler(TComponent* comp)
/* Constructor that sets a default TComponent for easier function-calls.
*/
{
FDefaultComponent = comp;
}
//---------------------------------------------------------------------------
//TPropertyHandler::~TPropertyHandler(void)
///* Destructor.
//*/
//{
//}
//===========================================================================
//== Private functions: (For internal use by TPropertyHandler)
//===========================================================================
bool TPropertyHandler::DefaultComponentExists()
/* Shows a message if FDefaultComponent is not initialized.
This way you can debug your program.
To turn the warning off #define PROPERTYHANDLER_SHOW_WARNINGS.
*/
{
#ifdef PROPERTYHANDLER_SHOW_WARNINGS
if (FDefaultComponent == NULL)
{
ShowMessage(
"TPropertyHandler warning:\n\n"
"Default Component is not defined." );
}
#endif
return (FDefaultComponent != NULL);
}
//---------------------------------------------------------------------------
String TPropertyHandler::BuildNameValue(TComponent* comp, String prName)
/* Returns string: "PropertyName=Value".
!! Caution: Assumes all passed parameters are valid!
This function only exists to present clear code in the other functions.
*/
{
return prName + "=" + GetPropValue(comp, prName, true);
}
//---------------------------------------------------------------------------
String TPropertyHandler::GetClassNamesValues(TComponent* comp, String prName)
/* Returns "PropertyName.SubPropertyName=Value" strings
for property prName of Component comp as a String.
!! Caution: Assumes prName is an existing property that is a class!
This function is used by other functions such as GetNameValue().
*/
{
TObject *subObj = (TObject *)GetOrdProp(comp, prName);
if (subObj == NULL) return ""; // In case the prop-value is empty (like Action).
/* TObject* subObj is needed to find out if the subprop points to
another Component (like ActiveControl).
If it is only the value of that property is returned (ie.: "CheckBox1").
If it is a class like Font all of the props+values of
comp->Font are returned. */
if (dynamic_cast<TComponent*>(subObj))
{ // Find out if subObj points to another Component by casting.
return //compName + "." +
prName + "=" + GetValue((TComponent*)subObj,"Name");
}
/* subObj does not point to another Component, but we can convert
it to a TComponent* subComp so it can be used te get the subProps+values.
This sounds weird but don't forget that a TFont encapsulated in a TForm
becomes a descendant of TForm, which is a descendant of TComponent!!
*/
TComponent* subComp = (TComponent*)subObj;
TStringList *tempList = new TStringList;
String temp;
try // StringList created with new should always be used in a try...finally block.
{
PTypeInfo TypeInfo = (PTypeInfo)subComp->ClassInfo();
PPropList PropList = new TPropList;
GetPropInfos(TypeInfo, PropList);
tempList->Sorted = true;
for (int i=0; i < PropertyCount(subComp); i++)
{
String subProp = String(PropList[i]->Name);
tempList->Add(
prName + "." + subProp
+ "=" + GetPropValue(subComp, subProp, true) );
}
delete[] PropList;
temp = tempList->Text;
// Remove trailing carriage-returns:
int cr = temp.LastDelimiter("\r");
if (cr) temp.Delete(cr,2);
}
__finally
{
delete tempList;
}
return temp;
}
//===========================================================================
//== Published 'information'-functions:
//===========================================================================
int TPropertyHandler::PropertyCount(TComponent* comp)
/* Returns the number of properties of comp, including the events.
*/
{
PTypeInfo TypeInfo = (PTypeInfo)comp->ClassInfo();
PTypeData TypeData = GetTypeData(TypeInfo);
return (int)TypeData->PropCount;
}
//---------------------------------------------------------------------------
int TPropertyHandler::PropertyCount()
/* Same as other PropertyCount() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return -1;
return PropertyCount(FDefaultComponent);
}
//---------------------------------------------------------------------------
bool TPropertyHandler::IsClass(TComponent* comp, String prName)
/* Returns true if prName is a class (like the property Font).
*/
{
Typinfo::TTypeKind kind = PropType((TObject*)comp, prName);
return (kind == tkClass);
}
//---------------------------------------------------------------------------
bool TPropertyHandler::IsClass(String prName)
/* Same as other IsClass() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return false;
return IsClass(FDefaultComponent, prName);
}
//---------------------------------------------------------------------------
bool TPropertyHandler::PropertyExists(TComponent* comp, String prName)
/* Checks if property prName exists for TComponent comp.
*/
{
if (!IsPublishedProp(comp, prName))
{
#ifdef PROPERTYHANDLER_SHOW_WARNINGS
ShowMessage( "TPropertyHandler warning:\n\n"
"Property '" + GetName(comp) + "." + prName + "' does not exist." );
#endif
return false;
}
return true;
}
//---------------------------------------------------------------------------
bool TPropertyHandler::PropertyExists(String prName)
/* Same as other PropertyExists() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return false;
return PropertyExists(FDefaultComponent, prName);
}
//===========================================================================
//== Published Get... -functions:
//===========================================================================
String TPropertyHandler::GetName(TComponent* comp)
/* Returns value of Name property for any TComponent (comp) as String.
*/
{
return comp->Name;
}
//---------------------------------------------------------------------------
String TPropertyHandler::GetName()
/* Same as other GetName() function, for use with the default component.
*/
{
if (!DefaultComponentExists()) return "";
return GetName(FDefaultComponent);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Variant TPropertyHandler::GetValue(TComponent* comp, String prName)
/* Returns property value for any TComponent (comp) as Variant.
If property does not exist the return value is NULL.
If property is a class the return value is NULL.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -