forked from Embarcadero/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMX.PythonGUIInputOutput.pas
More file actions
208 lines (181 loc) · 5.21 KB
/
FMX.PythonGUIInputOutput.pas
File metadata and controls
208 lines (181 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{$I ..\Definition.Inc}
unit FMX.PythonGUIInputOutput;
interface
uses
Classes,
{$IFDEF MSWINDOWS}
Winapi.Messages,
Winapi.Windows,
{$ENDIF}
Fmx.Memo,
PythonEngine;
const
PID_SUPPORTED_PLATFORMS = pidAllPlatforms;
type
[ComponentPlatformsAttribute(PID_SUPPORTED_PLATFORMS)]
TPythonGUIInputOutput = class(TPythonInputOutput)
private
{ Private declarations }
FCustomMemo : TCustomMemo;
{$IFDEF MSWINDOWS}
FWinHandle : THandle;
{$ENDIF}
protected
{ Protected declarations }
{$IFDEF MSWINDOWS}
procedure pyGUIOutputWndProc (var Message: TMessage);
{$ENDIF}
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SendData( const Data : AnsiString ); override;
procedure SendUniData( const Data : UnicodeString ); override;
procedure AddPendingWrite; override;
procedure WriteOutput;
public
function ReceiveData: AnsiString; override;
function ReceiveUniData: UnicodeString; override;
public
{ Public declarations }
constructor Create( AOwner : TComponent ); override;
destructor Destroy; override;
procedure DisplayString( const str : string );
published
{ Published declarations }
property Output : TCustomMemo read FCustomMemo write FCustomMemo;
end;
implementation
uses
System.SysUtils, System.UITypes, FMX.DialogService;
{$IFDEF MSWINDOWS}
const
WM_WriteOutput = WM_USER + 1;
{$ENDIF}
{ TPythonGUIInputOutput }
{PROTECTED METHODS}
{------------------------------------------------------------------------------}
procedure TPythonGUIInputOutput.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if Operation = opRemove then
if aComponent = fCustomMemo then
fCustomMemo := nil;
end;
{------------------------------------------------------------------------------}
{$IFDEF MSWINDOWS}
procedure TPythonGUIInputOutput.pyGUIOutputWndProc(var Message: TMessage);
begin
case Message.Msg of
WM_WriteOutput : WriteOutput;
end;{case}
end;
{$ENDIF}
{------------------------------------------------------------------------------}
procedure TPythonGUIInputOutput.SendData( const Data : AnsiString );
begin
if Assigned(FOnSendData) then
inherited
else
DisplayString( string(Data) );
end;
procedure TPythonGUIInputOutput.SendUniData(const Data: UnicodeString);
begin
if Assigned(FOnSendUniData) then
inherited
else
DisplayString( string(Data) );
end;
{------------------------------------------------------------------------------}
function TPythonGUIInputOutput.ReceiveData : AnsiString;
Var
LResult : AnsiString;
begin
if Assigned(FOnReceiveData) then
Result := inherited ReceiveData
else
begin
TDialogService.InputQuery('Query from Python', ['Enter text'], [''],
procedure(const AResult: TModalResult; const AValues: array of string) begin
LResult := AnsiString(AValues[0]);
end
);
Result := LResult;
end;
end;
function TPythonGUIInputOutput.ReceiveUniData: UnicodeString;
Var
LResult: string;
begin
if Assigned( FOnReceiveUniData ) then
Result := inherited ReceiveUniData
else
begin
TDialogService.InputQuery('Query from Python', ['Enter text'], [''],
procedure(const AResult: TModalResult; const AValues: array of string) begin
LResult := AValues[0];
end
);
Result := LResult;
end;
end;
{------------------------------------------------------------------------------}
procedure TPythonGUIInputOutput.AddPendingWrite;
begin
{$IFDEF MSWINDOWS}
PostMessage( fWinHandle, WM_WriteOutput, 0, 0 );
{$ENDIF}
end;
{------------------------------------------------------------------------------}
procedure TPythonGUIInputOutput.WriteOutput;
var
S : string;
begin
if FQueue.Count = 0 then
Exit;
Lock;
try
while FQueue.Count > 0 do
begin
S := FQueue.Strings[ 0 ];
FQueue.Delete(0);
DisplayString( S );
end;
finally
Unlock;
end;
end;
{PUBLIC METHODS}
{------------------------------------------------------------------------------}
constructor TPythonGUIInputOutput.Create( AOwner : TComponent );
begin
inherited Create(AOwner);
{$IFDEF MSWINDOWS}
// Create an internal window for use in delayed writes
// This will allow writes from multiple threads to be queue up and
// then written out to the associated TCustomMemo by the main UI thread.
fWinHandle := System.Classes.AllocateHWnd(pyGUIOutputWndProc);
{$ENDIF}
UnicodeIO := True;
end;
{------------------------------------------------------------------------------}
destructor TPythonGUIInputOutput.Destroy;
begin
{$IFDEF MSWINDOWS}
// Destroy the internal window used for Delayed write operations
System.Classes.DeallocateHWnd(fWinHandle);
{$ENDIF}
inherited Destroy;
end;
{------------------------------------------------------------------------------}
type
TMyCustomMemo = class(TCustomMemo);
procedure TPythonGUIInputOutput.DisplayString( const str : string );
begin
if Assigned(Output) then
begin
if TMyCustomMemo(Output).Lines.Count >= MaxLines then
TMyCustomMemo(Output).Lines.Delete(0);
TMyCustomMemo(Output).Lines.Add(str);
Output.GoToTextEnd();
end;
end;
{------------------------------------------------------------------------------}
end.