Skip to content

Commit 7e8255d

Browse files
committed
Fix pyscripter#312 Wrapper for Vcl Menu and Toolbar.
1 parent 98011f2 commit 7e8255d

File tree

5 files changed

+201
-31
lines changed

5 files changed

+201
-31
lines changed

Source/WrapActions.pas

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ function TActionListAccess.GetSize: Integer;
126126

127127
function TActionListAccess.IndexOf(AValue: PPyObject): Integer;
128128
var
129-
i: Integer;
130129
_obj: TPyObject;
131130
_item: TContainedAction;
132131
begin
@@ -140,12 +139,7 @@ function TActionListAccess.IndexOf(AValue: PPyObject): Integer;
140139
(TPyDelphiObject(_obj).DelphiObject is TContainedAction) then
141140
begin
142141
_item := TContainedAction(TPyDelphiObject(_obj).DelphiObject);
143-
for i := 0 to Container.ActionCount - 1 do
144-
if Container.Actions[i] = _item then
145-
begin
146-
Result := i;
147-
Break;
148-
end;
142+
Result := _item.Index;
149143
end;
150144
end;
151145
end;

Source/WrapDelphi.pas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@
300300
- Wrapping of Records using extended RTTI
301301
- Wrapping of Interfaces using extended RTTI (see unit tests)
302302
303+
2021
304+
- FMX Wrapping by Lucas Belo
305+
- Vcl Menu and Toolbar wrapping by PyScripter
303306
TODO:
304307
- Extend SetProps: if property receiving the value is a TStrings and the value a sequence,
305308
then assign the sequence content to the TStrings.
@@ -308,7 +311,6 @@
308311
- Create a simple app that just initializes Python and executes a script? To avoid having a console...
309312
- Bug with Delphi pyd: can't change the application title, because TApplication creates its own handle
310313
- Wrap TApplicationEvents. In fact define the events used by TApplicationEvents.
311-
- Wrap TMenu and Toolbar
312314
- Wrap TObjectList
313315
- Unit Test all exposed attributes
314316
- Wrap simple types like TMessage

Source/vcl/WrapVclComCtrls.pas

Lines changed: 172 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
interface
66

77
uses
8-
Classes, SysUtils, PythonEngine, WrapDelphi, WrapDelphiClasses,
9-
WrapVclControls, Windows, ComCtrls, TypInfo;
8+
Winapi.Windows,
9+
System.SysUtils,
10+
System.Classes,
11+
System.TypInfo,
12+
Vcl.ComCtrls,
13+
PythonEngine,
14+
WrapDelphi,
15+
WrapDelphiClasses,
16+
WrapVclControls;
1017

1118
type
1219
TTabChangingEventHandler = class(TEventHandler)
@@ -113,10 +120,50 @@ TPyDelphiTrackBar = class (TPyDelphiWinControl)
113120
property DelphiObject: TTrackBar read GetDelphiObject write SetDelphiObject;
114121
end;
115122

123+
TPyDelphiToolButton = class(TPyDelphiGraphicControl)
124+
private
125+
function GetDelphiObject: TToolButton;
126+
procedure SetDelphiObject(const Value: TToolButton);
127+
public
128+
class function DelphiObjectClass: TClass; override;
129+
property DelphiObject: TToolButton read GetDelphiObject
130+
write SetDelphiObject;
131+
end;
132+
133+
TToolbarAccess = class(TContainerAccess)
134+
private
135+
function GetContainer: TToolbar;
136+
public
137+
function GetItem(AIndex: Integer): PPyObject; override;
138+
function GetSize: Integer; override;
139+
function IndexOf(AValue: PPyObject): Integer; override;
140+
class function ExpectedContainerClass: TClass; override;
141+
class function SupportsIndexOf: Boolean; override;
142+
class function Name: string; override;
143+
property Container: TToolbar read GetContainer;
144+
end;
145+
146+
TPyDelphiToolbar = class(TPyDelphiWinControl)
147+
private
148+
function GetDelphiObject: TToolbar;
149+
procedure SetDelphiObject(const Value: TToolbar);
150+
protected
151+
function Get_ButtonCount(AContext: Pointer): PPyObject; cdecl;
152+
function Get_Buttons(AContext: Pointer): PPyObject; cdecl;
153+
public
154+
class function DelphiObjectClass: TClass; override;
155+
class procedure RegisterGetSets(PythonType: TPythonType); override;
156+
class function GetContainerAccessClass: TContainerAccessClass; override;
157+
158+
property DelphiObject: TToolbar read GetDelphiObject
159+
write SetDelphiObject;
160+
end;
161+
116162
implementation
117163

118164
uses
119-
WrapDelphiTypes, ExtCtrls;
165+
WrapDelphiTypes,
166+
Vcl.ExtCtrls;
120167

121168
{ Register the wrappers, the globals and the constants }
122169
type
@@ -148,6 +195,8 @@ procedure TComCtrlsRegistration.RegisterWrappers(APyDelphiWrapper: TPyDelphiWrap
148195
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiPageControl);
149196
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiTabSheet);
150197
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiTrackBar);
198+
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiToolButton);
199+
APyDelphiWrapper.RegisterDelphiWrapper(TPyDelphiToolbar);
151200

152201
APyDelphiWrapper.EventHandlers.RegisterHandler(TTabChangingEventHandler);
153202
end;
@@ -641,9 +690,128 @@ procedure TPyDelphiTrackBar.SetDelphiObject(const Value: TTrackBar);
641690
inherited DelphiObject := Value;
642691
end;
643692

693+
{ TPyDelphiToolButton }
694+
695+
class function TPyDelphiToolButton.DelphiObjectClass: TClass;
696+
begin
697+
Result := TToolButton;
698+
end;
699+
700+
function TPyDelphiToolButton.GetDelphiObject: TToolButton;
701+
begin
702+
Result := TToolButton(inherited DelphiObject);
703+
end;
704+
705+
procedure TPyDelphiToolButton.SetDelphiObject(const Value: TToolButton);
706+
begin
707+
inherited DelphiObject := Value;
708+
end;
709+
710+
{ TToolbarAccess }
711+
712+
class function TToolbarAccess.ExpectedContainerClass: TClass;
713+
begin
714+
Result := TToolbar;
715+
end;
716+
717+
function TToolbarAccess.GetContainer: TToolbar;
718+
begin
719+
Result := TToolbar(inherited Container);
720+
end;
721+
722+
function TToolbarAccess.GetItem(AIndex: Integer): PPyObject;
723+
begin
724+
Result := Wrap( Container.Buttons[AIndex] );
725+
end;
726+
727+
function TToolbarAccess.GetSize: Integer;
728+
begin
729+
Result := Container.ButtonCount;
730+
end;
731+
732+
function TToolbarAccess.IndexOf(AValue: PPyObject): Integer;
733+
var
734+
_obj: TPyObject;
735+
_item: TToolButton;
736+
begin
737+
Result := -1;
738+
with GetPythonEngine do
739+
begin
740+
if IsDelphiObject(AValue) then
741+
begin
742+
_obj := PythonToDelphi(AValue);
743+
if (_obj is TPyDelphiObject) and
744+
(TPyDelphiObject(_obj).DelphiObject is TToolButton) then
745+
begin
746+
_item := TToolButton(TPyDelphiObject(_obj).DelphiObject);
747+
Result := _item.Index;
748+
end;
749+
end;
750+
end;
751+
end;
752+
753+
class function TToolbarAccess.Name: string;
754+
begin
755+
Result := 'Toolbar.Buttons'
756+
end;
757+
758+
class function TToolbarAccess.SupportsIndexOf: Boolean;
759+
begin
760+
Result := True;
761+
end;
762+
763+
{ TPyDelphiToolbar }
764+
765+
class function TPyDelphiToolbar.DelphiObjectClass: TClass;
766+
begin
767+
Result := TToolbar;
768+
end;
769+
770+
class function TPyDelphiToolbar.GetContainerAccessClass: TContainerAccessClass;
771+
begin
772+
Result := TToolbarAccess;
773+
end;
774+
775+
function TPyDelphiToolbar.GetDelphiObject: TToolbar;
776+
begin
777+
Result := TToolbar(inherited DelphiObject);
778+
end;
779+
780+
function TPyDelphiToolbar.Get_Buttons(AContext: Pointer): PPyObject;
781+
begin
782+
Adjust(@Self);
783+
Result := PyDelphiWrapper.DefaultContainerType.CreateInstance;
784+
with PythonToDelphi(Result) as TPyDelphiContainer do
785+
Setup(Self.PyDelphiWrapper, TToolbarAccess.Create(Self.PyDelphiWrapper,
786+
Self.DelphiObject));
787+
end;
788+
789+
function TPyDelphiToolbar.Get_ButtonCount(AContext: Pointer): PPyObject;
790+
begin
791+
Adjust(@Self);
792+
Result := GetPythonEngine.PyLong_FromLong(DelphiObject.ButtonCount);
793+
end;
794+
795+
class procedure TPyDelphiToolbar.RegisterGetSets(PythonType: TPythonType);
796+
begin
797+
inherited;
798+
with PythonType do
799+
begin
800+
AddGetSet('ButtonCount', @TPyDelphiToolbar.Get_ButtonCount, nil,
801+
'Indicates the number of buttons in the toolbar.', nil);
802+
AddGetSet('Actions', @TPyDelphiToolbar.Get_Buttons, nil,
803+
'Lists the buttons of the toolbar.', nil);
804+
end;
805+
end;
806+
807+
procedure TPyDelphiToolbar.SetDelphiObject(const Value: TToolbar);
808+
begin
809+
inherited DelphiObject := Value;
810+
end;
811+
644812
initialization
645813
RegisteredUnits.Add( TComCtrlsRegistration.Create );
646814
{$IFNDEF FPC}
647-
Classes.RegisterClasses([TDateTimePicker]);
815+
System.Classes.RegisterClasses([TDateTimePicker]);
648816
{$ENDIF FPC}
649817
end.

Source/vcl/WrapVclControls.pas

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
interface
66

77
uses
8-
Classes, SysUtils, PythonEngine, WrapDelphi, WrapDelphiClasses, Controls,
9-
TypInfo, Types;
8+
System.Classes,
9+
System.SysUtils,
10+
System.TypInfo,
11+
System.Types,
12+
Vcl.Controls,
13+
PythonEngine,
14+
WrapDelphi,
15+
WrapDelphiClasses;
1016

1117
type
1218
{

Source/vcl/WrapVclMenus.pas

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,23 @@ function TMenuItemAccess.GetSize: Integer;
185185

186186
function TMenuItemAccess.IndexOf(AValue: PPyObject): Integer;
187187
var
188-
_obj: TPyObject;
189-
_item: TMenuItem;
190-
begin
191-
Result := -1;
192-
with GetPythonEngine do
193-
begin
194-
if IsDelphiObject(AValue) then
195-
begin
196-
_obj := PythonToDelphi(AValue);
197-
if (_obj is TPyDelphiObject) and
198-
(TPyDelphiObject(_obj).DelphiObject is TMenuItem) then
199-
begin
200-
_item := TMenuItem(TPyDelphiObject(_obj).DelphiObject);
201-
Result := Container.IndexOf(_item);
202-
end;
203-
end;
204-
end;
188+
_obj: TPyObject;
189+
_item: TMenuItem;
190+
begin
191+
Result := -1;
192+
with GetPythonEngine do
193+
begin
194+
if IsDelphiObject(AValue) then
195+
begin
196+
_obj := PythonToDelphi(AValue);
197+
if (_obj is TPyDelphiObject) and
198+
(TPyDelphiObject(_obj).DelphiObject is TMenuItem) then
199+
begin
200+
_item := TMenuItem(TPyDelphiObject(_obj).DelphiObject);
201+
Result := Container.IndexOf(_item);
202+
end;
203+
end;
204+
end;
205205
end;
206206

207207
class function TMenuItemAccess.Name: string;

0 commit comments

Comments
 (0)