Menu

[r2039]: / trunk / Src / UMemoCaretPosDisplayMgr.pas  Maximize  Restore  History

Download this file

416 lines (384 with data), 15.3 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
404
405
406
407
408
409
410
411
412
413
414
{
* UMemoCaretPosDisplayMgr.pas
*
* Displays the caret position of one or more memo controls in associated label
* controls. Labels are automatically updated whenever the caret position
* changes.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* 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 UMemoCaretPosDisplayMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2010-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UMemoCaretPosDisplayMgr;
interface
uses
// Delphi
Generics.Collections, Classes, Controls, StdCtrls, Messages;
type
{
TMemoCaretPosDisplayMgr:
Class that displays the caret position of one or more memo controls in
associated label controls. Labels are automatically updated whenever the
caret position changes.
}
TMemoCaretPosDisplayMgr = class(TObject)
strict private
type
{
TMemoHook:
Class used to hook into a memo control's message loop and detect
selection changes, triggering an event when detected.
}
TMemoHook = class(TObject)
strict private
var
fMemo: TMemo; // Memo control to be hooked
fMemoWndProc: Pointer; // Memo's original
fWndProcHook: Pointer; // New hook window procedure
fOnSelChange: TNotifyEvent; // OnSelChange event handler
procedure WndProcHook(var Msg: TMessage);
{Window procedure that replaces and calls into memo control's own
window procedure. Detects selection changes and triggers OnSelChange
event.
@param Msg [in/out] Contains information about message. Result field
updated with message return value.
}
function SetWndProc(WndProc: Pointer): Pointer;
{Assigns a new window procedure to memo control.
@param WndProc [in] Pointer to new window procedure.
@return Pointer to old window procedure.
}
public
constructor Create(const AMemo: TMemo);
{Object constructor. Creates hook object for a specified memo.
@param AMemo [in] Memo control to be hooked.
}
destructor Destroy; override;
{Object destructor. Restores memo's original window procedure.
}
property OnSelChange: TNotifyEvent read fOnSelChange write fOnSelChange;
{Event triggered when a selection change in memo control is detected}
end;
type
// Record of values associated with memo control
TAssociations = record
OnKeyUp: TKeyEvent; // Original OnKeyUp event handler
OnMouseUp: TMouseEvent; // Original OnMouseUp event handler
OnEnter: TNotifyEvent; // Original OnEnter event handler
Hook: TMemoHook; // Object that hooks memo's window proc
DisplayCtrl: TLabel; // Label in which to display caret info
end;
var fMap: TDictionary<TMemo,TAssociations>;
{Maps memo to associated display label and saved event handlers}
procedure OnKeyUpHandler(Sender: TObject; var Key: Word;
Shift: TShiftState);
{OnKeyUp event handler for managed memo controls. Calls any original event
handler then updates caret position display.
@param Sender [in] Memo control that triggered event.
@param Key [in/out] Not used. Passed to any original event handler.
@param Shift [in] Not used. Passed to any original event handler.
}
procedure OnMouseUpHandler(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
{OnMouseUp event handler for managed memo controls. Calls any original
event handler then updates caret position display.
@param Sender [in] Memo control that triggered event.
@param Button [in] Not used. Passed to any original event handler.
@param Shift [in] Not used. Passed to any original event handler.
@param X [in] Not used. Passed to any original event handler.
@param Y [in] Not used. Passed to any original event handler.
}
procedure OnEnterHandler(Sender: TObject);
{OnEnter event handler for managed memo controls. Calls any original event
handler then updates caret position display.
@param Sender [in] Memo control that triggered event.
}
procedure OnSelChangeHandler(Sender: TObject);
{Handles events triggered when selection changes are reported be memo
hook. Updates caret position display.
@param Sender [in] Memo control that triggered event.
}
procedure UpdateCaretPos(const SourceCtrl: TMemo);
{Updates display of a memo control's caret position.
@param SourceCtrl [in] Memo whose caret position to be displayed.
}
function FindFirstMemoControl(const ParentCtrl: TWinControl): TMemo;
{Finds first memo child control of given parent control.
@param ParentCtrl [in] Parent containing required memo control.
@return Reference to memo control or nil if ParentCtrl has no memo
child control.
}
public
constructor Create;
{Object constructor. Sets up object.
}
destructor Destroy; override;
{Object destructor. Restores original event handlers to managed memo
controls then clears up object.
}
procedure Manage(const SourceCtrl: TMemo; const DisplayCtrl: TLabel);
overload;
{Registers a memo control to have caret position displayed in an
associated label. NOTE: This method must only be called once the memo
control has initialised otherwise selection change events will not be
detected. Calling during a form's OnShow event is recommended.
@param SourceCtrl [in] Memo control whose caret position is to be
displayed.
@param DisplayCtrl [in] Label used to display caret position.
}
procedure Manage(const ParentCtrl: TWinControl; const DisplayCtrl: TLabel);
overload;
{Registers first memo child control of given parent control to have caret
position displayed in an associated label. NOTE 1: This method must only
be called once the memo control has initialised otherwise selection change
events will not be detected. Calling during a form's OnShow event is
recommended. NOTE 2: Parent control MUST have at least one memo control as
a child.
@param ParentCtrl [in] Parent of memo control whose caret position is to
be displayed.
@param DisplayCtrl [in] Label used to display caret position.
}
end;
implementation
uses
// Delphi
SysUtils, Windows;
{ TMemoCaretPosDisplayMgr }
constructor TMemoCaretPosDisplayMgr.Create;
{Object constructor. Sets up object.
}
begin
inherited Create;
fMap := TDictionary<TMemo,TAssociations>.Create;
end;
destructor TMemoCaretPosDisplayMgr.Destroy;
{Object destructor. Restores original event handlers to managed memo controls
then clears up object.
}
var
DataPair: TPair<TMemo,TAssociations>; // each key and value pair from map
SourceCtrl: TMemo; // each memo control in map
Associations: TAssociations; // associated data for each SourceCtrl
begin
for DataPair in fMap do
begin
// restore saved event handlers
SourceCtrl := DataPair.Key;
Associations := DataPair.Value;
SourceCtrl.OnKeyUp := Associations.OnKeyUp;
SourceCtrl.OnMouseUp := Associations.OnMouseUp;
SourceCtrl.OnEnter := Associations.OnEnter;
Associations.Hook.Free;
end;
fMap.Free;
inherited;
end;
function TMemoCaretPosDisplayMgr.FindFirstMemoControl(
const ParentCtrl: TWinControl): TMemo;
{Finds first memo child control of given parent control.
@param ParentCtrl [in] Parent containing required memo control.
@return Reference to memo control or nil if ParentCtrl has no memo child
control.
}
var
Ctrl: TControl;
Idx: Integer;
begin
for Idx := 0 to Pred(ParentCtrl.ControlCount) do
begin
Ctrl := ParentCtrl.Controls[Idx];
if Ctrl is TMemo then
Exit(Ctrl as TMemo);
end;
Result := nil;
end;
procedure TMemoCaretPosDisplayMgr.Manage(const ParentCtrl: TWinControl;
const DisplayCtrl: TLabel);
{Registers first memo child control of given parent control to have caret
position displayed in an associated label. NOTE 1: This method must only be
called once the memo control has initialised otherwise selection change events
will not be detected. Calling during a form's OnShow event is recommended.
NOTE 2: Parent control MUST have at least one memo control as a child.
@param ParentCtrl [in] Parent of memo control whose caret position is to be
displayed.
@param DisplayCtrl [in] Label used to display caret position.
}
begin
Manage(FindFirstMemoControl(ParentCtrl), DisplayCtrl);
end;
procedure TMemoCaretPosDisplayMgr.Manage(const SourceCtrl: TMemo;
const DisplayCtrl: TLabel);
{Registers a memo control to have caret position displayed in an associated
label.
@param SourceCtrl [in] Memo control whose caret position is to be displayed.
@param DisplayCtrl [in] Label used to display caret position.
}
var
Associations: TAssociations; // data to be associated with memo control
begin
Assert(Assigned(SourceCtrl), ClassName + '.Manage: SourceCtrl is nil');
Assert(Assigned(DisplayCtrl), ClassName + '.Manage: DisplayCtrl is nil');
Assert(not fMap.ContainsKey(SourceCtrl),
ClassName + '.Manage: Source memo already managed');
// save old event handlers
Associations.OnKeyUp := SourceCtrl.OnKeyUp;
Associations.OnMouseUp := SourceCtrl.OnMouseUp;
Associations.OnEnter := SourceCtrl.OnEnter;
// record display label
Associations.DisplayCtrl := DisplayCtrl;
// add menu hook object
Associations.Hook := TMemoHook.Create(SourceCtrl);
Associations.Hook.OnSelChange := OnSelChangeHandler;
// hook required event handlers (each of these calls any saved handler)
SourceCtrl.OnKeyUp := OnKeyUpHandler;
SourceCtrl.OnMouseUp := OnMouseUpHandler;
SourceCtrl.OnEnter := OnEnterHandler;
// add control to list of managed controls
fMap.Add(SourceCtrl, Associations);
// initialise caret position display
UpdateCaretPos(SourceCtrl);
end;
procedure TMemoCaretPosDisplayMgr.OnEnterHandler(Sender: TObject);
{OnEnter event handler for managed memo controls. Calls any original event
handler then updates caret position display.
@param Sender [in] Memo control that triggered event.
}
var
SavedOnEnter: TNotifyEvent; // original event handler
begin
// call any original event hander
SavedOnEnter := fMap[Sender as TMemo].OnEnter;
if Assigned(SavedOnEnter) then
SavedOnEnter(Sender);
UpdateCaretPos(Sender as TMemo);
end;
procedure TMemoCaretPosDisplayMgr.OnKeyUpHandler(Sender: TObject; var Key: Word;
Shift: TShiftState);
{OnKeyUp event handler for managed memo controls. Calls any original event
handler then updates caret position display.
@param Sender [in] Memo control that triggered event.
@param Key [in/out] Not used. Passed to any original event handler.
@param Shift [in] Not used. Passed to any original event handler.
}
var
SavedOnKeyUp: TKeyEvent; // original event handler
begin
// call any original event hander
SavedOnKeyUp := fMap[Sender as TMemo].OnKeyUp;
if Assigned(SavedOnKeyUp) then
SavedOnKeyUp(Sender, Key, Shift);
UpdateCaretPos(Sender as TMemo);
end;
procedure TMemoCaretPosDisplayMgr.OnMouseUpHandler(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
{OnMouseUp event handler for managed memo controls. Calls any original event
handler then updates caret position display.
@param Sender [in] Memo control that triggered event.
@param Button [in] Not used. Passed to any original event handler.
@param Shift [in] Not used. Passed to any original event handler.
@param X [in] Not used. Passed to any original event handler.
@param Y [in] Not used. Passed to any original event handler.
}
var
SavedOnMouseUp: TMouseEvent; // original event handler
begin
// call any original event hander
SavedOnMouseUp := fMap[Sender as TMemo].OnMouseUp;
if Assigned(SavedOnMouseUp) then
SavedOnMouseUp(Sender, Button, Shift, X, Y);
UpdateCaretPos(Sender as TMemo);
end;
procedure TMemoCaretPosDisplayMgr.OnSelChangeHandler(Sender: TObject);
{Handles events triggered when selection changes are reported be memo hook.
Updates caret position display.
@param Sender [in] Memo control that triggered event.
}
begin
UpdateCaretPos(Sender as TMemo);
end;
procedure TMemoCaretPosDisplayMgr.UpdateCaretPos(const SourceCtrl: TMemo);
{Updates display of a memo control's caret position.
@param SourceCtrl [in] Memo whose caret position to be displayed.
}
var
DisplayCtrl: TLabel; // label in which caret position to be displayed
begin
DisplayCtrl := fMap[SourceCtrl].DisplayCtrl;
DisplayCtrl.Caption := Format(
'%d: %d', [SourceCtrl.CaretPos.Y, SourceCtrl.CaretPos.X]
);
end;
{ TMemoCaretPosDisplayMgr.TMemoHook }
constructor TMemoCaretPosDisplayMgr.TMemoHook.Create(const AMemo: TMemo);
{Object constructor. Creates hook object for a specified memo.
@param AMemo [in] Memo control to be hooked.
}
begin
inherited Create;
fMemo := AMemo;
// hook memo's window procedure
fWndProcHook := Classes.MakeObjectInstance(WndProcHook);
fMemoWndProc := SetWndProc(fWndProcHook);
end;
destructor TMemoCaretPosDisplayMgr.TMemoHook.Destroy;
{Object destructor. Restores memo's original window procedure.
}
begin
fOnSelChange := nil;
if Assigned(fWndProcHook) then
begin
// restore original window procedure
SetWndProc(fMemoWndProc);
Classes.FreeObjectInstance(fWndProcHook);
end;
inherited;
end;
function TMemoCaretPosDisplayMgr.TMemoHook.SetWndProc(
WndProc: Pointer): Pointer;
{Assigns a new window procedure to memo control.
@param WndProc [in] Pointer to new window procedure.
@return Pointer to old window procedure.
}
begin
Result := Pointer(
SetWindowLong(fMemo.Handle, GWL_WNDPROC, Integer(WndProc))
);
end;
procedure TMemoCaretPosDisplayMgr.TMemoHook.WndProcHook(var Msg: TMessage);
{Window procedure that replaces and calls into memo control's own window
procedure. Detects selection changes and triggers OnSelChange event.
@param Msg [in/out] Contains information about message. Result field updated
with message return value.
}
begin
Msg.Result := CallWindowProc(
fMemoWndProc, fMemo.Handle, Msg.Msg, Msg.WParam, Msg.LParam
);
if (Msg.Msg = EM_SETSEL) and Assigned(fOnSelChange) then
fOnSelChange(fMemo);
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.