1 | Unit CustomMemo;
|
---|
2 |
|
---|
3 | Interface
|
---|
4 |
|
---|
5 | {$ifdef win32}
|
---|
6 | Uses
|
---|
7 | StdCtrls;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TMemoType = TMemo;
|
---|
11 |
|
---|
12 | {$else}
|
---|
13 | Uses
|
---|
14 | Classes, Forms, StdCtrls, Messages;
|
---|
15 |
|
---|
16 | {Declare new class}
|
---|
17 | Type
|
---|
18 | TCustomMemo=Class(TMemo)
|
---|
19 | Protected
|
---|
20 | FStoreLines: boolean;
|
---|
21 | Procedure DestroyWnd; Override;
|
---|
22 | Procedure SetupComponent; Override;
|
---|
23 | Public
|
---|
24 | Procedure WMChar(Var Msg:TWMChar); message WM_CHAR;
|
---|
25 | Procedure AddSelectedLine( NewText: string );
|
---|
26 | Procedure AddText( NewText: PChar );
|
---|
27 | // If it can be found, make the given text the
|
---|
28 | // top of the window. Returns true if it was found
|
---|
29 | Function JumpToText( Text: string ): boolean;
|
---|
30 | // Returns contents as Ansi String
|
---|
31 | Function Text: AnsiString;
|
---|
32 | // Returns total height of all text. Does not include borders at this time :-(
|
---|
33 | Function TotalHeight: longint;
|
---|
34 | // If the text fits entirely within the control, hide the scrollbars
|
---|
35 | Procedure AdjustScrollbars;
|
---|
36 | Published
|
---|
37 | Property StoreLines: boolean read FStoreLines write FStoreLines;
|
---|
38 | End;
|
---|
39 |
|
---|
40 | TMemoType = TCustomMemo;
|
---|
41 |
|
---|
42 | {Define components to export}
|
---|
43 | {You may define a page of the component palette and a component bitmap file}
|
---|
44 | Exports
|
---|
45 | TCustomMemo,'User','CustomMemo.bmp';
|
---|
46 | {$endif}
|
---|
47 |
|
---|
48 | procedure AddSelectedMemoText( Memo: TMemoType;
|
---|
49 | Text: string );
|
---|
50 |
|
---|
51 | Implementation
|
---|
52 |
|
---|
53 | {$ifdef os2}
|
---|
54 | Uses
|
---|
55 | PMWin, SysUtils, Os2Def;
|
---|
56 | {$endif}
|
---|
57 |
|
---|
58 | procedure AddSelectedMemoText( Memo: TMemoType;
|
---|
59 | Text: string );
|
---|
60 | {$ifdef win32}
|
---|
61 | var
|
---|
62 | StartPos: integer;
|
---|
63 | {$endif}
|
---|
64 | begin
|
---|
65 | {$ifdef win32}
|
---|
66 | StartPos := Length( Memo.Lines.Text );
|
---|
67 | Memo.Lines.Add( Text );
|
---|
68 | Memo.SelStart := StartPos;
|
---|
69 | Memo.SelLength := Length( Memo.Lines.Text ) - StartPos;
|
---|
70 | {$else}
|
---|
71 | Memo.AddSelectedLine( Text );
|
---|
72 | {$endif}
|
---|
73 | end;
|
---|
74 |
|
---|
75 | {$ifdef os2}
|
---|
76 | Procedure TCustomMemo.SetupComponent;
|
---|
77 | begin
|
---|
78 | inherited SetupComponent;
|
---|
79 | FStoreLines := true;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | Procedure TCustomMemo.DestroyWnd;
|
---|
83 | Begin
|
---|
84 | if FStoreLines then
|
---|
85 | inherited DestroyWnd
|
---|
86 | else
|
---|
87 | TControl.DestroyWnd;
|
---|
88 | End;
|
---|
89 |
|
---|
90 | Procedure TCustomMemo.AdjustScrollbars;
|
---|
91 | begin
|
---|
92 | if TotalHeight < Height - 10 then
|
---|
93 | Scrollbars := ssNone;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | Procedure TCustomMemo.AddText( NewText: PChar );
|
---|
97 | Var
|
---|
98 | addPoint: longint;
|
---|
99 | Begin
|
---|
100 | // Set to LF-only format
|
---|
101 | WinSendMsg( Handle, MLM_FORMAT, MLFIE_CFTEXT, 0);
|
---|
102 |
|
---|
103 | addPoint:=WinSendMsg( Handle, MLM_QUERYTEXTLENGTH, 0, 0 );
|
---|
104 |
|
---|
105 | // add text
|
---|
106 | WinSendMsg( Handle, MLM_SETIMPORTEXPORT, LongWord( NewText ), strlen( NewText ) );
|
---|
107 | WinSendMsg( Handle, MLM_IMPORT, LongWord( @addPoint ), strlen( NewText ) );
|
---|
108 | End;
|
---|
109 |
|
---|
110 | Procedure TCustomMemo.AddSelectedLine( NewText: string );
|
---|
111 | Var
|
---|
112 | currentLength: longint;
|
---|
113 | startOfLine: longint;
|
---|
114 | endOfLine: longint;
|
---|
115 | lineLength: longint;
|
---|
116 | rc: longint;
|
---|
117 | csText: cstring;
|
---|
118 | addPoint: longint;
|
---|
119 | line: longint;
|
---|
120 | Begin
|
---|
121 | // Set to LF-only format
|
---|
122 | WinSendMsg( Handle, MLM_FORMAT, MLFIE_NOTRANS, 0 ); {LF!}
|
---|
123 |
|
---|
124 | // If the last line is actually empty, we don't need
|
---|
125 | // to add a new line character.
|
---|
126 | currentLength:=WinSendMsg( Handle, MLM_QUERYTEXTLENGTH, 0, 0 );
|
---|
127 | line:=WinSendMsg( Handle, MLM_LINEFROMCHAR, currentLength, 0 );
|
---|
128 | startOfLine:=WinSendMsg( Handle, MLM_CHARFROMLINE, currentLength, 0 );
|
---|
129 | lineLength:= WinSendMsg( Handle, MLM_QUERYLINELENGTH, startOfLine,0);
|
---|
130 |
|
---|
131 | if lineLength>0 then
|
---|
132 | begin
|
---|
133 | // Find end of text
|
---|
134 | addPoint:= currentLength;
|
---|
135 |
|
---|
136 | // Add a new line
|
---|
137 | csText:=#10;
|
---|
138 | WinSendMsg( Handle, MLM_SETIMPORTEXPORT, LongWord(@csText), 255 );
|
---|
139 | WinSendMsg( Handle, MLM_IMPORT, LongWord( @addPoint ), Length(csText) );
|
---|
140 |
|
---|
141 | // find start of new line
|
---|
142 | startOfLine:=WinSendMsg( Handle, MLM_QUERYTEXTLENGTH, 0, 0 );
|
---|
143 | end;
|
---|
144 |
|
---|
145 | addPoint:=startOfLine;
|
---|
146 |
|
---|
147 | // add text
|
---|
148 | csText:=NewText;
|
---|
149 | WinSendMsg( Handle, MLM_SETIMPORTEXPORT, LongWord(@csText), 255 );
|
---|
150 | WinSendMsg( Handle, MLM_IMPORT, LongWord( @addPoint ), Length(csText) );
|
---|
151 |
|
---|
152 | // find the end of the new line
|
---|
153 | endOfLine:=WinSendMsg( Handle, MLM_QUERYTEXTLENGTH, 0, 0 );
|
---|
154 |
|
---|
155 | // get focus, otherwise selection won't happen
|
---|
156 | Focus;
|
---|
157 | rc:=WinSendMsg( Handle, MLM_SETSEL, startOfLine, endOfLine );
|
---|
158 | End;
|
---|
159 |
|
---|
160 | // If it can be found, make the given text the
|
---|
161 | // top of the window
|
---|
162 | Function TCustomMemo.JumpToText( Text: string ): boolean;
|
---|
163 | Var
|
---|
164 | SearchData: MLE_SearchData;
|
---|
165 | rc: longint;
|
---|
166 | csText: cstring;
|
---|
167 | line: longint;
|
---|
168 | newTopChar: longint;
|
---|
169 | Begin
|
---|
170 | Result:=false;
|
---|
171 | // Search all text
|
---|
172 | SearchData.iptStart:=0;
|
---|
173 | SearchData.iptStop:=-1;
|
---|
174 |
|
---|
175 | csText:=Text;
|
---|
176 | SearchData.pchFind:=@csText;
|
---|
177 |
|
---|
178 | rc:=WinSendMsg( Handle, MLM_SEARCH, 0, LongWord( @SearchData ) );
|
---|
179 | if rc=0 then
|
---|
180 | exit;
|
---|
181 | // find which line the text is on:
|
---|
182 | line:=WinSendMsg( Handle, MLM_LINEFROMCHAR, SearchData.iptStart, 0 );
|
---|
183 | // find the start of the line
|
---|
184 | newTopChar:=WinSendMsg( Handle, MLM_CHARFROMLINE, line, 0 );
|
---|
185 | // set it as the first char in the memo
|
---|
186 | WinSendMsg( Handle, MLM_SETFIRSTCHAR, newTopChar, 0 );
|
---|
187 | Result:=true;
|
---|
188 | End;
|
---|
189 |
|
---|
190 | // Returns contents as Ansi String
|
---|
191 | Function TCustomMemo.Text: AnsiString;
|
---|
192 | Var
|
---|
193 | pc: PChar;
|
---|
194 | Begin
|
---|
195 | pc:=Lines.GetText;
|
---|
196 | Result:=AnsiString( pc );
|
---|
197 | StrDispose( pc );
|
---|
198 | End;
|
---|
199 |
|
---|
200 | // Returns total height of all text
|
---|
201 | Function TCustomMemo.TotalHeight: longint;
|
---|
202 | Var
|
---|
203 | currentLength: longint;
|
---|
204 | line: longint;
|
---|
205 | Begin
|
---|
206 | // Set to LF-only format
|
---|
207 | WinSendMsg( Handle, MLM_FORMAT, MLFIE_NOTRANS, 0 ); {LF!}
|
---|
208 |
|
---|
209 | currentLength:=WinSendMsg( Handle, MLM_QUERYTEXTLENGTH, 0, 0 );
|
---|
210 | line:= WinSendMsg( Handle, MLM_LINEFROMCHAR, currentLength, 0 );
|
---|
211 |
|
---|
212 | Result:= ( line+1 )* Font.Height;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | Procedure TCustomMemo.WMChar(Var Msg:TWMChar);
|
---|
216 | var
|
---|
217 | CursorPosition: longint;
|
---|
218 | SelectionStart: longint;
|
---|
219 | begin
|
---|
220 | If Msg.KeyData And KC_KEYUP = 0 Then
|
---|
221 | begin
|
---|
222 | // keydown
|
---|
223 | if Msg.VirtualKeyCode in [ VK_PAGEDOWN, VK_PAGEUP ] then
|
---|
224 | begin
|
---|
225 | // we want to do some different things from the original
|
---|
226 | // OS/2 actions for page up/down
|
---|
227 | if Msg.KeyData And KC_CTRL <> 0 then
|
---|
228 | begin
|
---|
229 | // Ctrl PgUp/Down now goes to top or bottom of text
|
---|
230 | // not whatever weird behaviour the MLE thinks...
|
---|
231 | if Msg.VirtualKeyCode = VK_PAGEUP then
|
---|
232 | Msg.VirtualKeyCode := VK_HOME
|
---|
233 | else
|
---|
234 | Msg.VirtualKeyCode := VK_END;
|
---|
235 | end;
|
---|
236 |
|
---|
237 | if Msg.KeyData And KC_SHIFT <> 0 then
|
---|
238 | begin
|
---|
239 | // with shift
|
---|
240 | SelectionStart := SendMsg( Handle,
|
---|
241 | MLM_QUERYSEL,
|
---|
242 | MLFQS_ANCHORSEL,
|
---|
243 | 0 );
|
---|
244 | end;
|
---|
245 |
|
---|
246 | inherited WMChar( Msg );
|
---|
247 |
|
---|
248 | if Msg.KeyData And KC_SHIFT <> 0 then
|
---|
249 | begin
|
---|
250 | CursorPosition := SendMsg( Handle,
|
---|
251 | MLM_QUERYSEL,
|
---|
252 | MLFQS_CURSORSEL,
|
---|
253 | 0 );
|
---|
254 |
|
---|
255 | SendMsg( Handle,
|
---|
256 | MLM_SETSEL,
|
---|
257 | SelectionStart,
|
---|
258 | CursorPosition );
|
---|
259 | end;
|
---|
260 | exit;
|
---|
261 | end;
|
---|
262 | end;
|
---|
263 |
|
---|
264 | inherited WMChar( Msg );
|
---|
265 |
|
---|
266 | end;
|
---|
267 |
|
---|
268 | Initialization
|
---|
269 | {Register classes}
|
---|
270 | RegisterClasses([TCustomMemo]);
|
---|
271 | {$endif}
|
---|
272 |
|
---|
273 | End.
|
---|
274 |
|
---|