source: branches/2.20_branch/Components/CustomMemo.pas@ 471

Last change on this file since 471 was 15, checked in by RBRi, 19 years ago

+ components stuff

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1Unit CustomMemo;
2
3Interface
4
5{$ifdef win32}
6Uses
7 StdCtrls;
8
9type
10 TMemoType = TMemo;
11
12{$else}
13Uses
14 Classes, Forms, StdCtrls, Messages;
15
16{Declare new class}
17Type
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}
44Exports
45 TCustomMemo,'User','CustomMemo.bmp';
46{$endif}
47
48procedure AddSelectedMemoText( Memo: TMemoType;
49 Text: string );
50
51Implementation
52
53{$ifdef os2}
54Uses
55 PMWin, SysUtils, Os2Def;
56{$endif}
57
58procedure AddSelectedMemoText( Memo: TMemoType;
59 Text: string );
60{$ifdef win32}
61var
62 StartPos: integer;
63{$endif}
64begin
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}
73end;
74
75{$ifdef os2}
76Procedure TCustomMemo.SetupComponent;
77begin
78 inherited SetupComponent;
79 FStoreLines := true;
80end;
81
82Procedure TCustomMemo.DestroyWnd;
83Begin
84 if FStoreLines then
85 inherited DestroyWnd
86 else
87 TControl.DestroyWnd;
88End;
89
90Procedure TCustomMemo.AdjustScrollbars;
91begin
92 if TotalHeight < Height - 10 then
93 Scrollbars := ssNone;
94end;
95
96Procedure TCustomMemo.AddText( NewText: PChar );
97Var
98 addPoint: longint;
99Begin
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 ) );
108End;
109
110Procedure TCustomMemo.AddSelectedLine( NewText: string );
111Var
112 currentLength: longint;
113 startOfLine: longint;
114 endOfLine: longint;
115 lineLength: longint;
116 rc: longint;
117 csText: cstring;
118 addPoint: longint;
119 line: longint;
120Begin
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 );
158End;
159
160// If it can be found, make the given text the
161// top of the window
162Function TCustomMemo.JumpToText( Text: string ): boolean;
163Var
164 SearchData: MLE_SearchData;
165 rc: longint;
166 csText: cstring;
167 line: longint;
168 newTopChar: longint;
169Begin
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;
188End;
189
190// Returns contents as Ansi String
191Function TCustomMemo.Text: AnsiString;
192Var
193 pc: PChar;
194Begin
195 pc:=Lines.GetText;
196 Result:=AnsiString( pc );
197 StrDispose( pc );
198End;
199
200// Returns total height of all text
201Function TCustomMemo.TotalHeight: longint;
202Var
203 currentLength: longint;
204 line: longint;
205Begin
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;
213end;
214
215Procedure TCustomMemo.WMChar(Var Msg:TWMChar);
216var
217 CursorPosition: longint;
218 SelectionStart: longint;
219begin
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
266end;
267
268Initialization
269 {Register classes}
270 RegisterClasses([TCustomMemo]);
271{$endif}
272
273End.
274
Note: See TracBrowser for help on using the repository browser.