Menu

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

Download this file

249 lines (226 with data), 8.7 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
{
* 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 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UMemoCaretPosDisplayMgr;
interface
uses
// Delphi
Generics.Collections, Classes, Controls, StdCtrls;
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
// 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
DisplayCtrl: TLabel; // label in which to display caret info
end;
var
// Maps memo to associated display label and saved event handlers
fMap: TDictionary<TMemo,TAssociations>;
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 UpdateCaretPos(const SourceCtrl: TMemo);
{Updates display of a memo control's caret position.
@param SourceCtrl [in] Memo whose caret position to be displayed.
}
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);
{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.
}
end;
implementation
uses
// Delphi
SysUtils;
{ 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;
end;
fMap.Free;
inherited;
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(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;
// 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.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;
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.