Menu

[r3204]: / branches / experimental / Src / TrunkSrc / UMemoHelper.pas  Maximize  Restore  History

Download this file

237 lines (207 with data), 6.9 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Class that provides information about, and assists with manipulation of, memo
* controls where the functionality is not directly available from the control.
}
unit UMemoHelper;
interface
uses
// Delphi
StdCtrls, Windows;
type
{
TMemoHelper:
Provides information about, and assists with manipulation of, memo controls
where the functionality is not directly available from the control. Some of
the functions also apply to rich edit controls.
}
TMemoHelper = class(TObject)
strict private
fMemo: TCustomMemo; // Memo control that we're manipulating
function LineExtent(const LineIdx: Integer): TSize;
{Gets extent (i.e. width and height) of a memo line.
@param LineIdx [in] Index of line.
@return Width and height of line in pixels.
}
function GetFirstVisibleLine: Integer;
{Gets index of first visible line in memo control.
@return Index of line.
}
function GetRect: TRect;
{Get formatting rectangle of memo control.
@return Required rectangle.
}
procedure SetRect(const R: TRect);
{Set formatting rectangle of memo control.
@param R [in] New value of formatting rectangle.
}
function LineIdxOffset(const LineIdx: Integer): Integer;
{Gets index of line relative to top of memo.
@param LineIdx [in] Index of line.
@return Required index offset.
}
public
constructor Create(const AMemo: TCustomMemo);
{Class constructor. Creates info object for a specified memo.
@param AMemo [in] Memo to be manipulated.
}
function LineLeft(const LineIdx: Integer): Integer;
{Gets left margin of a memo line relative to memo's client area.
@param LineIdx [in] Index of line.
@return Left margin in pixels.
}
function LineTop(const LineIdx: Integer): Integer;
{Gets location of top of specified line relative to memo's client area.
@param LineIdx [in] Index of line.
@return Required offset in pixels.
}
function LineWidth(const LineIdx: Integer): Integer;
{Gets width of a memo line.
@param LineIdx [in] Index of line.
@return Required width in pixels.
}
function LineHeight(const LineIdx: Integer): Integer;
{Gets height of a memo line.
@param LineIdx [in] Index of line.
@return Required height in pixels.
}
procedure SetMargin(const Margin: Byte);
{Sets memo's margin.
@param Margin [in] Required width of margin.
}
function CaretPos: TPoint;
{Gets caret position in memo control in character co-ordinates.
@return Required cursor position.
}
end;
implementation
uses
// Delphi
SysUtils, Messages, Graphics,
// Project
UFontHelper, UGraphicUtils, UStructs;
{ TMemoHelper }
function TMemoHelper.CaretPos: TPoint;
{Gets caret position in memo control in character co-ordinates.
@return Required cursor position.
}
begin
Result.X := fMemo.SelStart - fMemo.Perform(EM_LINEINDEX, WPARAM(-1), 0);
Result.Y := fMemo.Perform(EM_LINEFROMCHAR, WPARAM(-1), 0);
end;
constructor TMemoHelper.Create(const AMemo: TCustomMemo);
{Class constructor. Creates info object for a specified memo.
@param AMemo [in] Memo for which we want info.
}
begin
Assert(Assigned(AMemo), ClassName + '.Create: AMemo is nil');
inherited Create;
fMemo := AMemo;
end;
function TMemoHelper.GetFirstVisibleLine: Integer;
{Gets index of first visible line in memo control.
@return Index of line.
}
begin
Result := fMemo.Perform(EM_GETFIRSTVISIBLELINE, 0, 0);
end;
function TMemoHelper.GetRect: TRect;
{Get formatting rectangle of memo control.
@return Required rectangle.
}
begin
fMemo.Perform(EM_GETRECT, 0, Integer(@Result));
end;
function TMemoHelper.LineExtent(const LineIdx: Integer): TSize;
{Gets extent (i.e. width and height) of a memo line.
@param LineIdx [in] Index of line.
@return Width and height of line in pixels.
}
var
Font: TFont; // memo control's font
begin
// We need a TFont object that represents the memo control's font. But, we
// can't call Font property since it is not exposed by TCustomMemo. Therefore
// we create a TFont from a copy of the font whose handle we get by sending a
// WM_GETFONT message to memo control. We need to copy it since when the
// TFont is freed it deletes the font handle, which is still required by the
// memo control and possibly other controls.
Font := TFont.Create;
try
Font.Handle := TFontHelper.CloneFontHandle(
fMemo.Perform(WM_GETFONT, 0, 0)
);
// Get size of memo line in its current font
Result := StringExtent(fMemo.Lines[LineIdx], Font);
finally
FreeAndNil(Font); // releases HCloneFont handle
end;
end;
function TMemoHelper.LineHeight(const LineIdx: Integer): Integer;
{Gets height of a memo line.
@param LineIdx [in] Index of line.
@return Required height in pixels.
}
begin
Result := LineExtent(LineIdx).cy;
end;
function TMemoHelper.LineIdxOffset(const LineIdx: Integer): Integer;
{Gets index of line relative to top of memo.
@param LineIdx [in] Index of line.
@return Required index offset.
}
begin
Result := LineIdx - GetFirstVisibleLine;
end;
function TMemoHelper.LineLeft(const LineIdx: Integer): Integer;
{Gets left margin of a memo line relative to memo's client area.
@param LineIdx [in] Index of line.
@return Left margin in pixels.
}
begin
Result := GetRect.Left;
end;
function TMemoHelper.LineTop(const LineIdx: Integer): Integer;
{Gets location of top of specified line relative to memo's client area.
@param LineIdx [in] Index of line.
@return Required offset in pixels.
}
begin
// assumes all lines same height
Result := LineIdxOffset(LineIdx) * LineHeight(LineIdx) + GetRect.Top;
end;
function TMemoHelper.LineWidth(const LineIdx: Integer): Integer;
{Gets width of a memo line.
@param LineIdx [in] Index of line.
@return Required width in pixels.
}
begin
Result := LineExtent(LineIdx).cx;
end;
procedure TMemoHelper.SetMargin(const Margin: Byte);
{Sets memo's margin.
@param Margin [in] Required width of margin.
}
var
R: TRectEx; // required bounding rectangle adjusted for margin
begin
R := fMemo.ClientRect;
R.InflateBy(-Margin, -Margin);
SetRect(R);
end;
procedure TMemoHelper.SetRect(const R: TRect);
{Set formatting rectangle of memo control.
@param R [in] New value of formatting rectangle.
}
begin
fMemo.Perform(EM_SETRECT, 0, LPARAM(@R));
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.