Skip to content

Commit feb3550

Browse files
committed
Wrapper for TLinkLabel Embarcadero/DelphiVCL4Python#44
1 parent 04430e8 commit feb3550

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

Source/vcl/WrapVclExtCtrls.pas

+119-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface
66

77
uses
88
Classes, SysUtils, PythonEngine, WrapDelphi, WrapDelphiClasses, WrapVclControls,
9-
Windows, ExtCtrls;
9+
Windows, ExtCtrls, TypInfo, Rtti;
1010

1111
type
1212
TPyDelphiShape = class (TPyDelphiGraphicControl)
@@ -169,6 +169,37 @@ TPyDelphiColorBox = class (TPyDelphiWinControl)
169169
end;
170170
{$ENDIF FPC}
171171

172+
TSysLinkEventHandler = class(TEventHandler)
173+
protected
174+
procedure DoEvent(Sender: TObject; const Link: string; LinkType: TSysLinkType);
175+
public
176+
constructor Create(PyDelphiWrapper : TPyDelphiWrapper; Component : TObject;
177+
PropertyInfo : PPropInfo; Callable : PPyObject); override;
178+
class function GetTypeInfo : PTypeInfo; override;
179+
end;
180+
181+
TPyDelphiCustomLinkLabel = class (TPyDelphiWinControl)
182+
private
183+
function GetDelphiObject: TCustomLinkLabel;
184+
procedure SetDelphiObject(const Value: TCustomLinkLabel);
185+
public
186+
class function DelphiObjectClass : TClass; override;
187+
// Properties
188+
property DelphiObject: TCustomLinkLabel read GetDelphiObject write SetDelphiObject;
189+
end;
190+
191+
TPyDelphiLinkLabel = class (TPyDelphiCustomLinkLabel)
192+
private
193+
function GetDelphiObject: TLinkLabel;
194+
procedure SetDelphiObject(const Value: TLinkLabel);
195+
public
196+
class function DelphiObjectClass : TClass; override;
197+
// Properties
198+
property DelphiObject: TLinkLabel read GetDelphiObject write SetDelphiObject;
199+
end;
200+
201+
function SysLinkTypeToPython(const ASymLinkType: TSysLinkType): PPyObject;
202+
172203
implementation
173204

174205
{ Register the wrappers, the globals and the constants }
@@ -216,8 +247,18 @@ procedure TExtCtrlsRegistration.RegisterWrappers(APyDelphiWrapper: TPyDelphiWrap
216247
{$IFNDEF FPC}
217248
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiColorBox);
218249
{$ENDIF FPC}
250+
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiCustomLinkLabel);
251+
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiLinkLabel);
252+
253+
// Event handlers
254+
APyDelphiWrapper.EventHandlers.RegisterHandler(TSysLinkEventHandler);
219255
end;
220256

257+
function SysLinkTypeToPython(const ASymLinkType: TSysLinkType): PPyObject;
258+
begin
259+
Result := GetPythonEngine.PyUnicodeFromString(
260+
TRttiEnumerationType.GetName<TSysLinkType>(ASymLinkType));
261+
end;
221262

222263
{ TPyDelphiShape }
223264

@@ -493,6 +534,83 @@ procedure TPyDelphiColorBox.SetDelphiObject(const Value: TColorBox);
493534
end;
494535
{$ENDIF FPC}
495536

537+
{ TSysLinkEventHandler }
538+
539+
constructor TSysLinkEventHandler.Create(PyDelphiWrapper: TPyDelphiWrapper;
540+
Component: TObject; PropertyInfo: PPropInfo; Callable: PPyObject);
541+
var
542+
LMethod : TMethod;
543+
begin
544+
inherited;
545+
LMethod.Code := @TSysLinkEventHandler.DoEvent;
546+
LMethod.Data := Self;
547+
SetMethodProp(Component, PropertyInfo, LMethod);
548+
end;
549+
550+
procedure TSysLinkEventHandler.DoEvent(Sender: TObject; const Link: string;
551+
LinkType: TSysLinkType);
552+
var
553+
LPyTuple, LPySender, LPyLink, LPyLinkType, PyResult: PPyObject;
554+
begin
555+
Assert(Assigned(PyDelphiWrapper));
556+
if Assigned(Callable) and PythonOK then
557+
with GetPythonEngine do begin
558+
LPySender := PyDelphiWrapper.Wrap(Sender);
559+
LPyLink := PyUnicodeFromString(Link);
560+
LPyLinkType := SysLinkTypeToPython(LinkType);
561+
LPyTuple := PyTuple_New(3);
562+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 0, LPySender);
563+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 1, LPyLink);
564+
GetPythonEngine.PyTuple_SetItem(LPyTuple, 2, LPyLinkType);
565+
try
566+
PyResult := PyObject_CallObject(Callable, LPyTuple);
567+
Py_XDECREF(PyResult);
568+
finally
569+
Py_DECREF(LPyTuple);
570+
end;
571+
CheckError;
572+
end;
573+
end;
574+
575+
class function TSysLinkEventHandler.GetTypeInfo: PTypeInfo;
576+
begin
577+
Result := System.TypeInfo(TSysLinkEvent);
578+
end;
579+
580+
{ TPyDelphiCustomLinkLabel }
581+
582+
class function TPyDelphiCustomLinkLabel.DelphiObjectClass: TClass;
583+
begin
584+
Result := TCustomLinkLabel;
585+
end;
586+
587+
function TPyDelphiCustomLinkLabel.GetDelphiObject: TCustomLinkLabel;
588+
begin
589+
Result := TCustomLinkLabel(inherited DelphiObject);
590+
end;
591+
592+
procedure TPyDelphiCustomLinkLabel.SetDelphiObject(
593+
const Value: TCustomLinkLabel);
594+
begin
595+
inherited DelphiObject := Value;
596+
end;
597+
598+
{ TPyDelphiLinkLabel }
599+
600+
class function TPyDelphiLinkLabel.DelphiObjectClass: TClass;
601+
begin
602+
Result := TLinkLabel;
603+
end;
604+
605+
function TPyDelphiLinkLabel.GetDelphiObject: TLinkLabel;
606+
begin
607+
Result := TLinkLabel(inherited DelphiObject);
608+
end;
609+
610+
procedure TPyDelphiLinkLabel.SetDelphiObject(const Value: TLinkLabel);
611+
begin
612+
inherited DelphiObject := Value;
613+
end;
496614

497615
initialization
498616
RegisteredUnits.Add( TExtCtrlsRegistration.Create );

0 commit comments

Comments
 (0)