-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathLcl.PythonGUIInputOutput.pas
220 lines (195 loc) · 6.24 KB
/
Lcl.PythonGUIInputOutput.pas
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
209
210
211
212
213
214
215
216
217
218
219
220
(**************************************************************************)
(* This unit is part of the Python for Delphi (P4D) library *)
(* Project home: https://github.com/pyscripter/python4delphi *)
(* *)
(* Project Maintainer: PyScripter (pyscripter@gmail.com) *)
(* Original Authors: Dr. Dietmar Budelsky (dbudelsky@web.de) *)
(* Morgan Martinet (https://github.com/mmm-experts) *)
(* Core developer: Lucas Belo (lucas.belo@live.com) *)
(* Contributors: See contributors.md at project home *)
(* *)
(* LICENCE and Copyright: MIT (see project home) *)
(**************************************************************************)
{$I ..\Definition.Inc}
unit Lcl.PythonGUIInputOutput;
interface
uses
{$IFDEF MSWINDOWS}
Windows, Messages,
{$ENDIF}
SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, PythonEngine;
{$IFDEF MSWINDOWS}
const
WM_WriteOutput = WM_USER + 1;
{$ENDIF}
type
{$IF not Defined(FPC) and (CompilerVersion >= 23)}
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
{$IFEND}
TPythonGUIInputOutput = class(TPythonInputOutput)
private
{ Private declarations }
FCustomMemo : TCustomMemo;
{$IFDEF MSWINDOWS}
FWinHandle : HWND;
{$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;
function ReceiveData : AnsiString; override;
procedure SendUniData( const Data : UnicodeString ); override;
function ReceiveUniData : UnicodeString; override;
procedure AddPendingWrite; override;
procedure WriteOutput;
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
{$IFDEF FPC}
{$IFDEF MSWINDOWS}
Uses
InterfaceBase;
{$ENDIF}
{$ENDIF}
{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
S : string;
begin
if Assigned( FOnReceiveData ) then
Result := inherited ReceiveData
else
begin
InputQuery( 'Query from Python', 'Enter text', S);
Result := AnsiString(S);
end;
end;
function TPythonGUIInputOutput.ReceiveUniData: UnicodeString;
Var
S : string;
begin
if Assigned( FOnReceiveUniData ) then
Result := inherited ReceiveUniData
else
begin
InputQuery( 'Query from Python', 'Enter text', S);
Result := UnicodeString(S);
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.
{$IFDEF FPC}
fWinHandle := WidgetSet.AllocateHWnd(pyGUIOutputWndProc);
{$ELSE}
fWinHandle := Classes.AllocateHWnd(pyGUIOutputWndProc);
{$ENDIF}
{$ENDIF}
UnicodeIO := True;
end;
{------------------------------------------------------------------------------}
destructor TPythonGUIInputOutput.Destroy;
begin
{$IFDEF MSWINDOWS}
// Destroy the internal window used for Delayed write operations
{$IFDEF FPC}
WidgetSet.DeallocateHWnd(fWinHandle);
{$ELSE}
Classes.DeallocateHWnd(fWinHandle);
{$ENDIF}
{$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);
{$IFDEF MSWINDOWS}
SendMessage( Output.Handle, em_ScrollCaret, 0, 0);
{$ENDIF}
end;
end;
{------------------------------------------------------------------------------}
end.