Menu

[r4379]: / branches / parsnip / Src / Lib / SynEdit / SynEditKbdHandler.pas  Maximize  Restore  History

Download this file

404 lines (349 with data), 10.5 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynEditKeyCmds.pas, released 2000-04-07.
The Original Code is based on the mwKeyCmds.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is Brad Stowers.
Unicode translation by Maël Hörz.
All Rights Reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynEditKbdHandler.pas,v 1.10.2.1 2004/08/31 12:55:17 maelh Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
-------------------------------------------------------------------------------}
{$IFNDEF QSYNEDITKBDHANDLER}
unit SynEditKbdHandler;
{$ENDIF}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
Types,
QGraphics,
QControls,
QForms,
QSynEditTypes,
{$ELSE}
Windows,
Messages,
Graphics,
Controls,
Forms,
SynEditTypes,
{$ENDIF}
SysUtils,
Classes;
type
{ This class provides a TWinControl-Object which supports only the
needed Methods }
TKeyboardControl = class(TWinControl)
public
property OnKeyDown;
property OnKeyPress;
property OnMouseDown;
end;
TMouseCursorEvent = procedure(Sender: TObject; const aLineCharPos: TBufferCoord;
var aCursor: TCursor) of object;
TMethodList = class
private
fData: TList;
function GetItem(Index: integer): TMethod;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
procedure Add(aHandler: TMethod);
procedure Remove(aHandler: TMethod);
property Items[Index: Integer]: TMethod read GetItem; default;
property Count: Integer read GetCount;
end;
TSynEditKbdHandler = class (TObject)
private
fKeyPressChain: TMethodList;
fKeyDownChain: TMethodList;
fKeyUpChain: TMethodList;
fMouseDownChain: TMethodList;
fMouseUpChain: TMethodList;
fMouseCursorChain: TMethodList;
{ avoid infinite recursiveness }
fInKeyPress: Boolean;
fInKeyDown: Boolean;
fInKeyUp: Boolean;
fInMouseDown: Boolean;
fInMouseUp: Boolean;
fInMouseCursor: Boolean;
public
constructor Create;
destructor Destroy; override;
procedure ExecuteKeyPress(Sender: TObject; var Key: WideChar);
procedure ExecuteKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure ExecuteKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure ExecuteMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ExecuteMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ExecuteMouseCursor(Sender: TObject; const aLineCharPos: TBufferCoord;
var aCursor: TCursor);
procedure AddKeyDownHandler(aHandler: TKeyEvent);
procedure RemoveKeyDownHandler(aHandler: TKeyEvent);
procedure AddKeyUpHandler(aHandler: TKeyEvent);
procedure RemoveKeyUpHandler(aHandler: TKeyEvent);
procedure AddKeyPressHandler(aHandler: TKeyPressWEvent);
procedure RemoveKeyPressHandler(aHandler: TKeyPressWEvent);
procedure AddMouseDownHandler(aHandler: TMouseEvent);
procedure RemoveMouseDownHandler(aHandler: TMouseEvent);
procedure AddMouseUpHandler(aHandler: TMouseEvent);
procedure RemoveMouseUpHandler(aHandler: TMouseEvent);
procedure AddMouseCursorHandler(aHandler: TMouseCursorEvent);
procedure RemoveMouseCursorHandler(aHandler: TMouseCursorEvent);
end;
implementation
{ TSynEditKbdHandler }
procedure TSynEditKbdHandler.AddKeyDownHandler(aHandler: TKeyEvent);
begin
fKeyDownChain.Add(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.AddKeyUpHandler(aHandler: TKeyEvent);
begin
fKeyUpChain.Add(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.AddKeyPressHandler(aHandler: TKeyPressWEvent);
begin
fKeyPressChain.Add(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.AddMouseDownHandler(aHandler: TMouseEvent);
begin
fMouseDownChain.Add(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.AddMouseUpHandler(aHandler: TMouseEvent);
begin
fMouseUpChain.Add(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.AddMouseCursorHandler(aHandler: TMouseCursorEvent);
begin
fMouseCursorChain.Add(TMethod(aHandler));
end;
constructor TSynEditKbdHandler.Create;
begin
{ Elements to handle KeyDown-Events }
fKeyDownChain := TMethodList.Create;
{ Elements to handle KeyUp-Events }
fKeyUpChain := TMethodList.Create;
{ Elements to handle KeyPress-Events }
fKeyPressChain := TMethodList.Create;
{ Elements to handle MouseDown Events }
fMouseDownChain := TMethodList.Create;
{ Elements to handle MouseUp Events }
fMouseUpChain := TMethodList.Create;
{ Elements to handle MouseCursor Events }
fMouseCursorChain := TMethodList.Create;
end;
destructor TSynEditKbdHandler.Destroy;
begin
fKeyPressChain.Free;
fKeyDownChain.Free;
fKeyUpChain.Free;
fMouseDownChain.Free;
fMouseUpChain.Free;
fMouseCursorChain.Free;
inherited Destroy;
end;
procedure TSynEditKbdHandler.ExecuteKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
idx: Integer;
begin
if fInKeyDown then
exit;
fInKeyDown := True;
try
with fKeyDownChain do
begin
for idx := Count - 1 downto 0 do
begin
TKeyEvent(Items[idx])(Sender, Key, Shift);
if (Key = 0) then
begin
fInKeyDown := False;
exit;
end;
end;
end;
finally
fInKeyDown := False;
end;
end;
procedure TSynEditKbdHandler.ExecuteKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
idx: Integer;
begin
if fInKeyUp then
exit;
fInKeyUp := True;
try
with fKeyUpChain do
begin
for idx := Count - 1 downto 0 do
begin
TKeyEvent(Items[idx])(Sender,Key,Shift);
if (Key = 0) then
begin
fInKeyUp := False;
exit;
end;
end;
end;
finally
fInKeyUp := False;
end;
end;
procedure TSynEditKbdHandler.ExecuteKeyPress(Sender: TObject; var Key: WideChar);
var
idx: Integer;
begin
if fInKeyPress then
exit;
fInKeyPress := True;
try
with fKeyPressChain do
begin
for idx := Count - 1 downto 0 do
begin
TKeyPressWEvent(Items[idx])(Sender, Key);
if (Key = #0) then
begin
fInKeyPress := False;
exit;
end;
end;
end;
finally
fInKeyPress := False;
end;
end;
procedure TSynEditKbdHandler.ExecuteMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
cHandler: Integer;
begin
if fInMouseDown then
Exit;
fInMouseDown := True;
try
for cHandler := fMouseDownChain.Count - 1 downto 0 do
TMouseEvent(fMouseDownChain[cHandler])(Sender, Button, Shift, X, Y);
finally
fInMouseDown := False;
end;
end;
procedure TSynEditKbdHandler.ExecuteMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
cHandler: Integer;
begin
if fInMouseUp then
Exit;
fInMouseUp := True;
try
for cHandler := fMouseUpChain.Count - 1 downto 0 do
TMouseEvent(fMouseUpChain[cHandler])(Sender, Button, Shift, X, Y);
finally
fInMouseUp := False;
end;
end;
procedure TSynEditKbdHandler.ExecuteMouseCursor(Sender: TObject;
const aLineCharPos: TBufferCoord; var aCursor: TCursor);
var
cHandler: Integer;
begin
if fInMouseCursor then
Exit;
fInMouseCursor := True;
try
for cHandler := fMouseCursorChain.Count - 1 downto 0 do
TMouseCursorEvent(fMouseCursorChain[cHandler])(Sender, aLineCharPos, aCursor);
finally
fInMouseCursor := False;
end;
end;
procedure TSynEditKbdHandler.RemoveKeyDownHandler(aHandler: TKeyEvent);
begin
fKeyDownChain.Remove(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.RemoveKeyUpHandler(aHandler: TKeyEvent);
begin
fKeyUpChain.Remove(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.RemoveKeyPressHandler(aHandler: TKeyPressWEvent);
begin
fKeyPressChain.Remove(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.RemoveMouseDownHandler(aHandler: TMouseEvent);
begin
fMouseDownChain.Remove(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.RemoveMouseUpHandler(aHandler: TMouseEvent);
begin
fMouseUpChain.Remove(TMethod(aHandler));
end;
procedure TSynEditKbdHandler.RemoveMouseCursorHandler(aHandler: TMouseCursorEvent);
begin
fMouseCursorChain.Remove(TMethod(aHandler));
end;
{ TMethodList }
procedure TMethodList.Add(aHandler: TMethod);
begin
fData.Add(aHandler.Data);
fData.Add(aHandler.Code);
end;
constructor TMethodList.Create;
begin
fData := TList.Create;
end;
destructor TMethodList.Destroy;
begin
fData.Free;
end;
function TMethodList.GetCount: Integer;
begin
Result := fData.Count div 2;
end;
function TMethodList.GetItem(Index: Integer): TMethod;
begin
Index := Index * 2;
Result.Data := fData[Index];
Result.Code := fData[Index + 1];
end;
procedure TMethodList.Remove(aHandler: TMethod);
var
cPos: Integer;
begin
cPos := fData.Count - 2;
while cPos >= 0 do
begin
if (fData.List[cPos] = aHandler.Data) and (fData.List[cPos + 1] = aHandler.Code) then
begin
fData.Delete(cPos);
fData.Delete(cPos);
Exit;
end;
Dec(cPos, 2);
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.