{------------------------------------------------------------------------------} { Unit Name: SimpleNameprop Purpose : To demonstrate the use of Delphi's property editor. This package displays three dot's behind the Name property and when those are pressed, the component name is changed to [ClassName][Caption]. F.ex if TButton component with caption 'Add cars' is used, the the Name property is changed to ButtonAddCars Author : Vesa Lappalainen Date : 06.09.00 Changed : ToDo : } {------------------------------------------------------------------------------} unit SimpleNameprop; interface uses Classes, Forms, Controls, DsgnIntf, SysUtils, Dialogs, Registry; type TNameProperty=class(TComponentNameProperty) public procedure Edit; override; function GetAttributes: TPropertyAttributes; override; function HandleName(const s:string):string; virtual; end; procedure Register; implementation uses kPropFunc; //------------------------------------------------------------------------------ procedure Register; begin RegisterPropertyEditor(TypeInfo(TComponentName),TComponent,'Name',TNameProperty); end; //------------------------------------------------------------------------------ function TNameProperty.HandleName(const s: string): string; var p,i:integer; const what = 'ÜÅÄÖüåäö'; cto = 'UAAOuaao'; begin Result := s; while (true ) do begin i := Pos(' ',Result); if ( i = 0 ) then break; Delete(Result,i,1); if ( i > Length(Result) ) then break; Result[i] := AnsiUpperCase(Result[i])[1]; end; for i:=1 to Length(Result) do begin p := Pos(Result[i],what); if ( p > 0 ) then Result[i] := cto[p] else if not ( Result[i] in ['A'..'Z','a'..'z','0'..'9','_'] ) then Result[i] := '_'; end; end; //------------------------------------------------------------------------------ procedure TNameProperty.Edit; var s,cs,ns:string; comp : TComponent; begin s := Value; comp := TComponent(GetComponent(0)); // Typecast safe, because the "editor" // is defined only for TComponent cs := comp.ClassName; if ( cs = '' ) then exit; if ( cs[1] = 'T' ) then delete(cs,1,1); ns := GetStringProperty(comp,'Caption',''); if ( ns = '' ) then ns := GetStringProperty(comp,'Text',''); if ( ns = '' ) then ns := GetStringProperty(comp,'AsString',''); if ( ns <> '' ) then s := HandleName(cs + ns); if InputQuery('Give the name','Component name',s) then Value := HandleName(s); end; //------------------------------------------------------------------------------ function TNameProperty.GetAttributes: TPropertyAttributes; begin Result:=inherited GetAttributes+[paDialog]; end; initialization finalization end.