-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPLAPPS.PAS
252 lines (215 loc) · 5.26 KB
/
APLAPPS.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
{$I COMPILER.INC}
unit AplApps;
interface
uses
AplObj,
AplConst,
MemDrv,
Lists,
AplTypes,
AplUtils,
KeyDrv,
MouseDrv,
AplStr;
const
mUser = 10000;
type
PApplication = ^TApplication;
PApplicationErrorProc = ^TApplicationErrorProc;
PMessage = ^TMessage;
PKeyMessage = ^TKeyMessage;
PMouseMessage = ^TMouseMessage;
PMessageObject = ^TMessageObject;
PEventObject = ^TEventObject;
TApplicationErrorProc = procedure(var ASender; ACode: word);
TMessageType = (mMessage, mMouse, mKey);
TMessageObject = object(TStatusObject)
private
public
procedure ProcessMessage(var AMessage: PMessage); virtual;
end;
TEventObject = object(TMessageObject)
private
public
procedure InvokeSender(AEventProc: pointer; ASender: PObject; var AEvent: TEvent);
procedure Invoke(AEventProc: pointer; var AEvent: TEvent);
end;
TMessage = object(TObject)
private
public
Sender: PObject;
Target: PObject;
Code: word;
MessageType: TMessageType;
Processed: boolean;
Data: pointer;
constructor Create(var ASender; ACode: word);
procedure Init; virtual;
end;
TMouseMessage = object(TMessage)
private
public
MouseState: TMouseState;
constructor Create(var ASender; ACode: word; var AMouseState: TMouseState);
procedure Init; virtual;
end;
TKeyMessage = object(TMessage)
private
Key: word;
ScanCode: byte;
ShiftState: TKeyboardFlags;
constructor Create(var ASender; ACode: word; AKey: word);
procedure Init; virtual;
end;
TMessageQueue = object(TObjectQueue)
private
public
function Enq(AObject: PMessage): integer;
function Deq: PMessage;
end;
TApplication = object(TEventObject)
private
public
Closed: boolean;
OnError: PApplicationErrorProc;
MessageQueue: TMessageQueue;
destructor Free; virtual;
procedure Init; virtual;
procedure Run; virtual;
procedure Close; virtual;
procedure ProcessMessages; virtual;
procedure ProcessEvents; virtual;
procedure Error(var ASender; ACode: word); virtual;
procedure PostMessage(ASender, ATarget: PObject; ACode: word);
procedure SendMessage(ASender, ATarget: PObject; ACode: word);
end;
implementation
var
Application: PApplication;
function AllocError(ASize: word): boolean;
begin
if Assigned(Application) then
Application^.Error(Application, ecNotEnoughMemory);
end;
procedure TApplication.Error(var ASender; ACode: word);
begin
if Assigned(OnError) then
TApplicationErrorProc(OnError)(ASender, ACode);
end;
procedure TApplication.Init;
begin
inherited Init;
Application := @self;
MessageQueue.Create;
Closed := false;
OnError := nil;
Memory.OnAllocError := @AllocError;
end;
procedure TApplication.PostMessage(ASender, ATarget: PObject; ACode: word);
var
message: PMessage;
begin
message := New(PMessage, Create(ASender, ACode));
if message = nil then
exit;
message^.Target := ATarget;
PMessageObject(ATarget)^.ProcessMessage(message);
FreeAndNil(message);
end;
procedure TApplication.SendMessage(ASender, ATarget: PObject; ACode: word);
var
message: PMessage;
begin
message := New(PMessage, Create(ASender, ACode));
if message = nil then
exit;
message^.Target := ATarget;
MessageQueue.Enq(message);
end;
destructor TApplication.Free;
begin
MessageQueue.DisposeObjects := true;
MessageQueue.Free;
inherited Free;
end;
procedure TApplication.ProcessMessages;
begin
end;
procedure TApplication.ProcessEvents;
begin
end;
procedure TApplication.Run;
begin
repeat
ProcessMessages;
ProcessEvents;
if Closed then
exit;
until Closed;
end;
procedure TApplication.Close;
begin
Closed := true;
end;
constructor TMessage.Create(var ASender; ACode: word);
begin
inherited Create;
Sender := @ASender;
Code := ACode;
end;
procedure TMessage.Init;
begin
inherited Init;
Sender := nil;
Target := nil;
Code := 0;
MessageType := mMessage;
Processed := false;
Data := nil;
end;
constructor TMouseMessage.Create(var ASender; ACode: word; var AMouseState: TMouseState);
begin
inherited Create(ASender, ACode);
MouseState.Assign(AMouseState);
end;
procedure TMouseMessage.Init;
begin
inherited Init;
MouseState.Create;
MessageType := mMouse;
end;
constructor TKeyMessage.Create(var ASender; ACode: word; AKey: word);
begin
inherited Create(ASender, ACode);
Key := AKey;
end;
procedure TKeyMessage.Init;
begin
inherited Init;
Code := 0;
Key := 0;
MessageType := mKey;
end;
function TMessageQueue.Enq(AObject: PMessage): integer;
begin
Enq := inherited Enq(AObject);
end;
function TMessageQueue.Deq: PMessage;
begin
Deq := PMessage(inherited Deq);
end;
procedure TMessageObject.ProcessMessage(var AMessage: PMessage);
begin
end;
procedure TEventObject.InvokeSender(AEventProc: pointer; ASender: PObject;
var AEvent: TEvent);
begin
AEvent.Sender := ASender;
if Assigned(AEventProc) then
TEventProc(AEventProc)(AEvent);
end;
procedure TEventObject.Invoke(AEventProc: pointer; var AEvent: TEvent);
begin
InvokeSender(AEventProc, @self, AEvent);
end;
end.