1 | Unit CustomFontDialog;
|
---|
2 |
|
---|
3 | Interface
|
---|
4 |
|
---|
5 | Uses
|
---|
6 | Classes, Forms, Dialogs, StdCtrls, Buttons, Graphics;
|
---|
7 |
|
---|
8 | Type
|
---|
9 |
|
---|
10 | TCustomFontDialog=Class(TDialog)
|
---|
11 | Private
|
---|
12 | FNameList: TListBox;
|
---|
13 | FNameLabel: TLabel;
|
---|
14 | FSizeCombo: TComboBox;
|
---|
15 | FSizeLabel: TLabel;
|
---|
16 | FStyleLabel: TLabel;
|
---|
17 | FStyleListbox: TListBox;
|
---|
18 | FExampleText: TEdit;
|
---|
19 | FItalicCheck:TCheckBox;
|
---|
20 | FBoldCheck:TCheckBox;
|
---|
21 | FOutlineCheck:TCheckBox;
|
---|
22 | FUnderscoreCheck:TCheckBox;
|
---|
23 | FStrikeOutCheck:TCheckBox;
|
---|
24 | FActualFaceLabel:TLabel;
|
---|
25 | FOKButton:TButton;
|
---|
26 | FCancelButton:TButton;
|
---|
27 | FEditFont: TFont;
|
---|
28 |
|
---|
29 | FAllowSimulations: boolean;
|
---|
30 |
|
---|
31 | FUpdating: boolean;
|
---|
32 | FFontLoadRequired: boolean;
|
---|
33 |
|
---|
34 | Function GetFaceName: String;
|
---|
35 | Procedure SetFaceName( s: String );
|
---|
36 |
|
---|
37 | Function GetStyleName: String;
|
---|
38 | Procedure SetStyleName( s: String );
|
---|
39 |
|
---|
40 | Function GetPointSize: LongInt;
|
---|
41 | Procedure SetPointSize( Value:LongInt );
|
---|
42 |
|
---|
43 | Function GetStyleFont: TFont;
|
---|
44 |
|
---|
45 | Function GetAttributes: TFontAttributes;
|
---|
46 | Procedure SetAttributes( NewValue: TFontAttributes );
|
---|
47 |
|
---|
48 | Procedure InsertSizes;
|
---|
49 | Procedure InsertStyles;
|
---|
50 |
|
---|
51 | Procedure OnSelectFace( Sender: TObject; Index: LongInt );
|
---|
52 | Procedure OnSelectStyle( Sender: TObject; Index: LongInt );
|
---|
53 | Procedure OnSetSize( Sender: TObject );
|
---|
54 | Procedure OnStyleChanged( Sender:TObject );
|
---|
55 |
|
---|
56 | Procedure OnSizeClick( Sender: TObject; Index: longint );
|
---|
57 |
|
---|
58 | Procedure SetEditFont( NewFont:TFont );
|
---|
59 |
|
---|
60 | Function FindFace( FaceName: string ): TFont;
|
---|
61 |
|
---|
62 | Protected
|
---|
63 | Procedure SetupComponent; Override;
|
---|
64 | Procedure SetupShow; Override;
|
---|
65 | Procedure LayoutControls;
|
---|
66 | Procedure Resize; override;
|
---|
67 | Procedure SelectFont;
|
---|
68 | public
|
---|
69 | Property FaceName: String read GetFaceName write SetFaceName;
|
---|
70 | Property StyleName: String read GetStyleName write SetStyleName;
|
---|
71 | Property PointSize: longint read GetPointSize write SetPointSize;
|
---|
72 | Property Attributes: TFontAttributes read GetAttributes write SetAttributes;
|
---|
73 |
|
---|
74 | Property EditFont: TFont Read FEditFont Write SetEditFont;
|
---|
75 | Property AllowSimulations: boolean read FAllowSimulations write FAllowSimulations;
|
---|
76 | End;
|
---|
77 |
|
---|
78 | Exports
|
---|
79 | TCustomFontDialog, 'User', 'CustomFontDialog.bmp';
|
---|
80 |
|
---|
81 | Implementation
|
---|
82 |
|
---|
83 | uses
|
---|
84 | SysUtils, PmWin,
|
---|
85 | ACLStringUtility;
|
---|
86 |
|
---|
87 | // Returns true if s ends with endstr (case insensitive)
|
---|
88 | Function StringAtEnd( const endStr: string; const s: string ): boolean;
|
---|
89 | Var
|
---|
90 | i, j: integer;
|
---|
91 | Begin
|
---|
92 | Result := false;
|
---|
93 | if Length( s ) < length( endStr ) then
|
---|
94 | exit;
|
---|
95 | j := Length( s );
|
---|
96 | for i := length( endstr ) downto 1 do
|
---|
97 | begin
|
---|
98 | if UpCase( s[ j ] ) <> UpCase( endStr[ i ] ) then
|
---|
99 | exit;
|
---|
100 | dec( j );
|
---|
101 | end;
|
---|
102 | Result := true;
|
---|
103 | End;
|
---|
104 |
|
---|
105 | // Returns S minus count characters from the right
|
---|
106 | Function StringLeftWithout( const S:string; const count:integer ):string;
|
---|
107 | Begin
|
---|
108 | Result := copy( S, 1, length( S )-count );
|
---|
109 | End;
|
---|
110 |
|
---|
111 | Function RemoveIfMatchAtEnd( Var s: string; const SearchString: string ): boolean;
|
---|
112 | begin
|
---|
113 | Result := StringAtEnd( SearchString, s );
|
---|
114 | if Result then
|
---|
115 | s := Trim( StringLeftWithout( s, Length( SearchString ) ) );
|
---|
116 | end;
|
---|
117 |
|
---|
118 | Procedure RemoveStyleNames( Var FaceName: string );
|
---|
119 | Begin
|
---|
120 | FaceName := Trim( FaceName );
|
---|
121 | RemoveIfMatchAtEnd( FaceName, 'Italic' );
|
---|
122 | RemoveIfMatchAtEnd( FaceName, 'Oblique' );
|
---|
123 | RemoveIfMatchAtEnd( FaceName, 'Bold' );
|
---|
124 | RemoveIfMatchAtEnd( FaceName, 'Normal' );
|
---|
125 | end;
|
---|
126 |
|
---|
127 | Function GetFontFamilyName( Font: TFont ): string;
|
---|
128 | begin
|
---|
129 | Result := Font.Family;
|
---|
130 | Result := SubstituteChar( Result, '-', ' ' );
|
---|
131 | Result := SubstituteChar( Result, '_', ' ' );
|
---|
132 |
|
---|
133 | if Result = 'Roman' then
|
---|
134 | if StrStarts( 'Tms Rmn', Font.FaceName ) then
|
---|
135 | // one particularly stupid example
|
---|
136 | Result := 'Tms Rmn';
|
---|
137 |
|
---|
138 | if Result = 'Swiss' then
|
---|
139 | if StrStarts( 'Helv', Font.FaceName ) then
|
---|
140 | // one particularly stupid example
|
---|
141 | Result := 'Helv';
|
---|
142 |
|
---|
143 | // some fonts have family names with styles!
|
---|
144 | RemoveStyleNames( Result );
|
---|
145 |
|
---|
146 | end;
|
---|
147 |
|
---|
148 | Function GetFontStyleName( Font: TFont ): string;
|
---|
149 | var
|
---|
150 | FamilyName: string;
|
---|
151 | begin
|
---|
152 | Result := Font.FaceName;
|
---|
153 | Result := SubstituteChar( Result, '-', ' ' );
|
---|
154 | Result := SubstituteChar( Result, '_', ' ' );
|
---|
155 |
|
---|
156 | FamilyName := GetFontFamilyName( Font );
|
---|
157 | if StrStarts( FamilyName, Result ) then
|
---|
158 | begin
|
---|
159 | Result := StrRightFrom( Result, length( FamilyName ) + 1 );
|
---|
160 | Result := TrimChars( Result, [ ' ', '-', '_' ] );
|
---|
161 |
|
---|
162 | if Result = '' then
|
---|
163 | Result := 'Normal';
|
---|
164 | end;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | Procedure TCustomFontDialog.SetupComponent;
|
---|
168 | Var
|
---|
169 | FontIndex: LongInt;
|
---|
170 | aFont: TFont;
|
---|
171 | FontList: TStringList;
|
---|
172 | FamilyIndex: longint;
|
---|
173 | FontFamilyName: string;
|
---|
174 | FontStyleName: string;
|
---|
175 | Begin
|
---|
176 | Inherited SetupComponent;
|
---|
177 |
|
---|
178 | FAllowSimulations := true;
|
---|
179 |
|
---|
180 | BorderStyle := bsSizeable;
|
---|
181 | BorderIcons := [ biSystemMenu, biMaximize ];
|
---|
182 |
|
---|
183 | Caption := LoadNLSStr( SSelectAFont );
|
---|
184 | Width := 480;
|
---|
185 | Height := 350;
|
---|
186 | MinTrackHeight := 350;
|
---|
187 | MinTrackWidth := 400;
|
---|
188 |
|
---|
189 | // Give controls names for purposes of language support...
|
---|
190 |
|
---|
191 | FNameList := TListBox.Create( self );
|
---|
192 | FNameList.Parent := self;
|
---|
193 | FNameList.Sorted := true;
|
---|
194 | FNameList.Duplicates := false;
|
---|
195 | FNameList.OnItemFocus := OnSelectFace;
|
---|
196 | FNameList.Name := 'NameList';
|
---|
197 |
|
---|
198 | FNameLabel := TLabel.Create( self );
|
---|
199 | FNameLabel.Parent := self;
|
---|
200 | FNameLabel.Caption := '~Name:';
|
---|
201 | FNameLabel.FocusControl := FNameList;
|
---|
202 | FNameLabel.Name := 'NameLabel';
|
---|
203 |
|
---|
204 | FStyleListbox := TListBox.Create( self );
|
---|
205 | FStyleListbox.Parent := self;
|
---|
206 | FStyleListbox.Name := 'StyleListBox';
|
---|
207 | FStyleListBox.Sorted := true;
|
---|
208 | FStyleListBox.OnItemFocus := OnSelectStyle;
|
---|
209 |
|
---|
210 | FStyleLabel := TLabel.Create( self );
|
---|
211 | FStyleLabel.Parent := self;
|
---|
212 | FStyleLabel.Caption := 'S~tyle:';
|
---|
213 | FStyleLabel.FocusControl := FStyleListBox;
|
---|
214 | FStyleLabel.Name := 'StyleLabel';
|
---|
215 |
|
---|
216 | FBoldCheck := TCheckBox.Create( self );
|
---|
217 | FBoldCheck.Parent := self;
|
---|
218 | FBoldCheck.OnClick := OnStyleChanged;
|
---|
219 | FBoldCheck.Caption := '~Bold (Simulated)';
|
---|
220 | FBoldCheck.Name := 'BoldCheck';
|
---|
221 |
|
---|
222 | FItalicCheck := TCheckBox.Create( self );
|
---|
223 | FItalicCheck.Parent := self;
|
---|
224 | FItalicCheck.OnClick := OnStyleChanged;
|
---|
225 | FItalicCheck.Caption := '~Italic (Simulated)';
|
---|
226 | FItalicCheck.Name := 'ItalicCheck';
|
---|
227 |
|
---|
228 | FUnderscoreCheck := TCheckBox.Create( self );
|
---|
229 | FUnderscoreCheck.Parent := self;
|
---|
230 | FUnderscoreCheck.OnClick := OnStyleChanged;
|
---|
231 | FUnderscoreCheck.Caption := LoadNLSStr( SUnderscore );
|
---|
232 | FUnderscoreCheck.Name := 'UnderscoreCheck';
|
---|
233 |
|
---|
234 | FOutlineCheck := TCheckBox.Create( self );
|
---|
235 | FOutlineCheck.Parent := self;
|
---|
236 | FOutlineCheck.OnClick := OnStyleChanged;
|
---|
237 | FOutlineCheck.Caption := LoadNLSStr( SOutline );
|
---|
238 | FOutlineCheck.Name := 'OutlineCheck';
|
---|
239 |
|
---|
240 | FStrikeOutCheck := TCheckBox.Create( self );
|
---|
241 | FStrikeOutCheck.Parent := self;
|
---|
242 | FStrikeOutCheck.OnClick := OnStyleChanged;
|
---|
243 | FStrikeOutCheck.Caption := 'St~rikeout';
|
---|
244 | FStrikeOutCheck.Name := 'StrikeOutCheck';
|
---|
245 |
|
---|
246 | FSizeCombo := TComboBox.Create( self );
|
---|
247 | FSizeCombo.Style := csSimple;
|
---|
248 | FSizeCombo.Parent := self;
|
---|
249 | FSizeCombo.OnChange := OnSetSize;
|
---|
250 | FSizeCombo.OnItemFocus := OnSizeClick;
|
---|
251 | FSizeCombo.Name := 'SizeCombo';
|
---|
252 |
|
---|
253 | FSizeLabel := TLabel.Create( self );
|
---|
254 | FSizeLabel.Parent := self;
|
---|
255 | FSizeLabel.Caption := '~Size:';
|
---|
256 | FSizeLabel.FocusControl := FSizeCombo;
|
---|
257 | FSizeLabel.Name := 'SizeLabel';
|
---|
258 |
|
---|
259 | FExampleText := TEdit.Create( self );
|
---|
260 | FExampleText.Parent := self;
|
---|
261 | FExampleText.Text := 'abc ABC def DEF';
|
---|
262 | FExampleText.Autosize := false;
|
---|
263 | FExampleText.Name := 'ExampleText';
|
---|
264 |
|
---|
265 | FActualFaceLabel := TLabel.Create( self );
|
---|
266 | FActualFaceLabel.Parent := self;
|
---|
267 | FActualFaceLabel.Caption := '';
|
---|
268 | FActualFaceLabel.Name := 'ActualFaceLabel';
|
---|
269 | FActualFaceLabel.Alignment := taRightJustify;
|
---|
270 |
|
---|
271 | For FontIndex := 0 To Screen.FontCount - 1 Do
|
---|
272 | Begin
|
---|
273 | aFont := Screen.Fonts[ FontIndex ];
|
---|
274 |
|
---|
275 | FontStyleName := GetFontStyleName( aFont );
|
---|
276 | FontFamilyName := GetFontFamilyName( aFont );
|
---|
277 |
|
---|
278 | FamilyIndex := FNameList.Items.IndexOf( FontFamilyName );
|
---|
279 | if FamilyIndex = -1 then
|
---|
280 | begin
|
---|
281 | // new family
|
---|
282 | FontList := TStringList.Create;
|
---|
283 | FNameList.Items.AddObject( FontFamilyName, FontList );
|
---|
284 | end
|
---|
285 | else
|
---|
286 | begin
|
---|
287 | FontList := FNameList.Items.Objects[ FamilyIndex ] as TStringList;
|
---|
288 | end;
|
---|
289 | if FontList.IndexOf( FontStyleName ) = -1 then
|
---|
290 | begin
|
---|
291 | // new font for this family
|
---|
292 | FontList.AddObject( FontStyleName, aFont );
|
---|
293 | end;
|
---|
294 | End;
|
---|
295 |
|
---|
296 | FOKButton := InsertButton( Self, 150, 10, 90, 30,
|
---|
297 | LoadNLSStr( SOkButton ),
|
---|
298 | '' );
|
---|
299 | FOKButton.Name := 'OKButton';
|
---|
300 | FOKButton.Default := true;
|
---|
301 | FOKButton.ModalResult := mrOK;
|
---|
302 |
|
---|
303 | FCancelButton := InsertButton( Self, 250, 10, 90, 30,
|
---|
304 | LoadNLSStr( SCancelButton ),
|
---|
305 | '' );
|
---|
306 | FCancelButton.Name := 'CancelButton';
|
---|
307 | FCancelButton.Cancel := true;
|
---|
308 | FCancelButton.ModalResult := mrCancel;
|
---|
309 |
|
---|
310 | LayoutControls;
|
---|
311 |
|
---|
312 | SetEditFont( Screen.DefaultFont );
|
---|
313 | End;
|
---|
314 |
|
---|
315 | Function TCustomFontDialog.FindFace( FaceName: string ): TFont;
|
---|
316 | Var
|
---|
317 | FontIndex: LongInt;
|
---|
318 | aFont: TFont;
|
---|
319 | begin
|
---|
320 | For FontIndex := 0 To Screen.FontCount - 1 Do
|
---|
321 | Begin
|
---|
322 | aFont := Screen.Fonts[ FontIndex ];
|
---|
323 | if AnsiCompareText( aFont.FaceName, FaceName ) = 0 then
|
---|
324 | begin
|
---|
325 | Result := aFont;
|
---|
326 | exit;
|
---|
327 | end;
|
---|
328 | End;
|
---|
329 | Result := nil;
|
---|
330 | end;
|
---|
331 |
|
---|
332 | Procedure TCustomFontDialog.LayoutControls;
|
---|
333 | Var
|
---|
334 | W: longint;
|
---|
335 | H: longint;
|
---|
336 | ExampleH: longint;
|
---|
337 | VSplit: longint;
|
---|
338 | GrpH: longint;
|
---|
339 | TopH: longint;
|
---|
340 | Begin
|
---|
341 | W := ClientWidth;
|
---|
342 | H := ClientHeight;
|
---|
343 |
|
---|
344 | // Example is minimum 40 pixels, or 20% of height
|
---|
345 | ExampleH := 40;
|
---|
346 | if ( H div 5 ) > ExampleH then
|
---|
347 | ExampleH := H div 5;
|
---|
348 |
|
---|
349 | // Base of name/size/style (45 allows for buttons/spacing)
|
---|
350 | VSplit := 45 + ExampleH;
|
---|
351 | TopH := H - VSplit - 25;
|
---|
352 |
|
---|
353 | GrpH := TopH + 20;
|
---|
354 | // Left Bottom Width Height
|
---|
355 | FNameLabel .SetWindowPos( 5, H - 25, W - 210, 20 );
|
---|
356 | FNameList .SetWindowPos( 5, VSplit, W - 210, TopH );
|
---|
357 |
|
---|
358 | FStyleLabel .SetWindowPos( W - 200, H - 25, 120, 20 );
|
---|
359 |
|
---|
360 | FStyleListbox .SetWindowPos( W - 200, H - 100, 145, 75 );
|
---|
361 |
|
---|
362 | FBoldCheck .SetWindowPos( W - 200, H - 125, 145, 20 );
|
---|
363 | FItalicCheck .SetWindowPos( W - 200, H - 145, 145, 20 );
|
---|
364 | FUnderscoreCheck.SetWindowPos( W - 200, H - 165, 145, 20 );
|
---|
365 | FOutlineCheck .SetWindowPos( W - 200, H - 185, 145, 20 );
|
---|
366 | FStrikeOutCheck .SetWindowPos( W - 200, H - 205, 145, 20 );
|
---|
367 |
|
---|
368 | FSizeLabel .SetWindowPos( W - 50, H - 25, 45, 20 );
|
---|
369 | FSizeCombo .SetWindowPos( W - 50, VSplit, 45, TopH );
|
---|
370 |
|
---|
371 | FExampleText .SetWindowPos( 5, 40, W - 10, ExampleH );
|
---|
372 |
|
---|
373 | FActualFaceLabel.SetWindowPos( 180, 10, W - 185, 20 );
|
---|
374 |
|
---|
375 | FOKButton .SetWindowPos( 5, 5, 80, 30 );
|
---|
376 | FCancelButton .SetWindowPos( 90, 5, 80, 30 );
|
---|
377 |
|
---|
378 | End;
|
---|
379 |
|
---|
380 |
|
---|
381 | Procedure TCustomFontDialog.SetupShow;
|
---|
382 | Begin
|
---|
383 | Inherited SetupShow;
|
---|
384 |
|
---|
385 | FNameList.Focus;
|
---|
386 | FOKButton.Default := True;
|
---|
387 |
|
---|
388 | FBoldCheck.Enabled := FAllowSimulations;
|
---|
389 | FItalicCheck.Enabled := FAllowSimulations;
|
---|
390 | if not FAllowSimulations then
|
---|
391 | begin
|
---|
392 | FBoldCheck.Checked := false;
|
---|
393 | FItalicCheck.Checked := false;
|
---|
394 | end;
|
---|
395 |
|
---|
396 | FaceName := GetFontFamilyName( FEditFont );
|
---|
397 | StyleName := GetFontStyleName( FEditFont );
|
---|
398 | PointSize := FEditFont.PointSize;
|
---|
399 | Attributes := FEditFont.Attributes;
|
---|
400 |
|
---|
401 | FFontLoadRequired := false;
|
---|
402 |
|
---|
403 | SelectFont; // display it
|
---|
404 |
|
---|
405 | End;
|
---|
406 |
|
---|
407 | Procedure TCustomFontDialog.Resize;
|
---|
408 | begin
|
---|
409 | inherited Resize;
|
---|
410 | LayoutControls;
|
---|
411 | end;
|
---|
412 |
|
---|
413 | Function TCustomFontDialog.GetFaceName: string;
|
---|
414 | begin
|
---|
415 | if FNameList.ItemIndex = -1 then
|
---|
416 | Result := ''
|
---|
417 | else
|
---|
418 | Result := FNameList.Items[ FNameList.ItemIndex ];
|
---|
419 | end;
|
---|
420 |
|
---|
421 | Procedure TCustomFontDialog.SetFaceName( s: string );
|
---|
422 | begin
|
---|
423 | FNameList.ItemIndex := FNameList.Items.IndexOf( s );
|
---|
424 |
|
---|
425 | if FNameList.ItemIndex > 4 then
|
---|
426 | FNameList.TopIndex := FNameList.ItemIndex - 4
|
---|
427 | else
|
---|
428 | FNameList.TopIndex := 0;
|
---|
429 | end;
|
---|
430 |
|
---|
431 | Function TCustomFontDialog.GetStyleName: String;
|
---|
432 | begin
|
---|
433 | if FStyleListBox.ItemIndex = -1 then
|
---|
434 | Result := ''
|
---|
435 | else
|
---|
436 | Result := FStyleListBox.Items[ FStyleListBox.ItemIndex ];
|
---|
437 | end;
|
---|
438 |
|
---|
439 | Procedure TCustomFontDialog.SetStyleName( s: String );
|
---|
440 | begin
|
---|
441 | FStyleListBox.ItemIndex := FStyleListBox.Items.IndexOf( s );
|
---|
442 |
|
---|
443 | if FNameList.ItemIndex = -1 then
|
---|
444 | FNameList.ItemIndex := 0;
|
---|
445 | end;
|
---|
446 |
|
---|
447 | Function TCustomFontDialog.GetAttributes: TFontAttributes;
|
---|
448 | Begin
|
---|
449 | Result :=[];
|
---|
450 | If FItalicCheck.Checked Then
|
---|
451 | Include( Result, faItalic );
|
---|
452 | If FBoldCheck.Checked Then
|
---|
453 | Include( Result, faBold );
|
---|
454 | If FOutlineCheck.Checked Then
|
---|
455 | Include( Result, faOutline );
|
---|
456 | If FStrikeOutCheck.Checked Then
|
---|
457 | Include( Result, faStrikeOut );
|
---|
458 | If FUnderscoreCheck.Checked Then
|
---|
459 | Include( Result, faUnderScore );
|
---|
460 | End;
|
---|
461 |
|
---|
462 | Procedure TCustomFontDialog.SetAttributes( NewValue: TFontAttributes );
|
---|
463 | Begin
|
---|
464 | if FAllowSimulations then
|
---|
465 | begin
|
---|
466 | FBoldCheck.Checked := faBold in NewValue;
|
---|
467 | FItalicCheck.Checked := faItalic in NewValue;
|
---|
468 | end;
|
---|
469 | FOutlineCheck.Checked := faOutline in NewValue;
|
---|
470 | FStrikeoutCheck.Checked := faStrikeout in NewValue;
|
---|
471 | FUnderscoreCheck.Checked := faUnderscore in NewValue;
|
---|
472 | End;
|
---|
473 |
|
---|
474 | Function TCustomFontDialog.GetPointSize: LongInt;
|
---|
475 | Var
|
---|
476 | S: String;
|
---|
477 | C: Integer;
|
---|
478 | Begin
|
---|
479 | S := FSizeCombo.Text;
|
---|
480 | Val( S, Result, C );
|
---|
481 | If C <> 0 Then
|
---|
482 | // invalid conversion
|
---|
483 | Result := 0;
|
---|
484 | End;
|
---|
485 |
|
---|
486 | Procedure TCustomFontDialog.SetPointSize( Value:LongInt );
|
---|
487 | Begin
|
---|
488 | If Value = 0 Then
|
---|
489 | Value := 8;
|
---|
490 | FSizeCombo.Text := IntToStr( Value );
|
---|
491 |
|
---|
492 | // if there's an exact match, select it, if not select nothing
|
---|
493 | FSizeCombo.ItemIndex := FSizeCombo.Items.IndexOf( IntToStr( Value ) );
|
---|
494 | End;
|
---|
495 |
|
---|
496 | Function TCustomFontDialog.GetStyleFont: TFont;
|
---|
497 | begin
|
---|
498 | if FStyleListBox.ItemIndex = -1 then
|
---|
499 | begin
|
---|
500 | // during startup
|
---|
501 | Result := Screen.DefaultFont;
|
---|
502 | exit;
|
---|
503 | end;
|
---|
504 | Result := FStyleListBox.Items.Objects[ FStyleListBox.ItemIndex ] as TFont;
|
---|
505 |
|
---|
506 | end;
|
---|
507 |
|
---|
508 | Procedure TCustomFontDialog.SelectFont;
|
---|
509 | var
|
---|
510 | Attrs: TFontAttributes;
|
---|
511 | StyleFont: TFont;
|
---|
512 | begin
|
---|
513 | if FFontLoadRequired then
|
---|
514 | // we are starting up and don't want to set edit font yet!
|
---|
515 | exit;
|
---|
516 |
|
---|
517 | Screen.Cursor := crHourGlass; // in case it takes a while to load up the font
|
---|
518 |
|
---|
519 | Attrs := GetAttributes;
|
---|
520 |
|
---|
521 | StyleFont := GetStyleFont;
|
---|
522 |
|
---|
523 | DereferenceFont( FEditFont );
|
---|
524 | FEditFont :=
|
---|
525 | Screen.CreateCompatibleFont(
|
---|
526 | Screen.GetFontFromPointSize( StyleFont.FaceName,
|
---|
527 | GetPointSize ) );
|
---|
528 | FEditFont.Attributes := GetAttributes;
|
---|
529 |
|
---|
530 | FActualFaceLabel.Caption := StyleFont.FaceName;
|
---|
531 |
|
---|
532 | FExampleText.Font := FEditFont;
|
---|
533 |
|
---|
534 | Screen.Cursor := crDefault;
|
---|
535 | end;
|
---|
536 |
|
---|
537 | Procedure TCustomFontDialog.SetEditFont( NewFont:TFont );
|
---|
538 | Begin
|
---|
539 | If NewFont = Nil Then
|
---|
540 | NewFont := Screen.DefaultFont;
|
---|
541 |
|
---|
542 | DereferenceFont( FEditFont );
|
---|
543 | FEditFont := NewFont;
|
---|
544 | ReferenceFont( FEditFont );
|
---|
545 |
|
---|
546 | if Handle <> 0 then
|
---|
547 | begin
|
---|
548 | FaceName := GetFontFamilyName( NewFont );
|
---|
549 | StyleName := GetFontStyleName( NewFont );
|
---|
550 | PointSize := NewFont.PointSize;
|
---|
551 | Attributes := NewFont.Attributes;
|
---|
552 | end
|
---|
553 | else
|
---|
554 | begin
|
---|
555 | FFontLoadRequired := true;
|
---|
556 | end;
|
---|
557 | End;
|
---|
558 |
|
---|
559 | const
|
---|
560 | StandardOutlineSizes: array[ 0 .. 11 ] of longint =
|
---|
561 | (
|
---|
562 | 4, 5, 6, 8, 10, 12,
|
---|
563 | 15, 18, 24, 36, 48, 72
|
---|
564 | );
|
---|
565 |
|
---|
566 | function LongintListCompare( Item1: pointer;
|
---|
567 | Item2: pointer ): longint;
|
---|
568 | begin
|
---|
569 | if item1 < item2 then
|
---|
570 | result := -1
|
---|
571 | else if item1 > item2 then
|
---|
572 | result := 1
|
---|
573 | else
|
---|
574 | result := 0;
|
---|
575 | end;
|
---|
576 |
|
---|
577 | Procedure TCustomFontDialog.InsertStyles;
|
---|
578 | var
|
---|
579 | FontList: TStringList;
|
---|
580 | OldStyleName: string;
|
---|
581 | NewIndex: longint;
|
---|
582 | begin
|
---|
583 | FontList := FNameList.Items.Objects[ FNameList.ItemIndex ] as TStringList;
|
---|
584 | if FStyleListbox.ItemIndex <> -1 then
|
---|
585 | OldStyleName := FStyleListbox.Items[ FStyleListbox.ItemIndex ]
|
---|
586 | else
|
---|
587 | OldStyleName := 'Normal';
|
---|
588 | FStyleListbox.Items.Assign( FontList );
|
---|
589 |
|
---|
590 | NewIndex := FStyleListbox.Items.IndexOf( OldStyleName );
|
---|
591 | if NewIndex = -1 then
|
---|
592 | NewIndex := FStyleListbox.Items.IndexOf( 'Normal' );
|
---|
593 | if NewIndex = -1 then
|
---|
594 | NewIndex := 0;
|
---|
595 | FStyleListbox.ItemIndex := NewIndex;
|
---|
596 | end;
|
---|
597 |
|
---|
598 | Procedure TCustomFontDialog.InsertSizes;
|
---|
599 | var
|
---|
600 | Face: string;
|
---|
601 | TheFont: TFont;
|
---|
602 | FontIndex: longint;
|
---|
603 | SizeIndex: longint;
|
---|
604 | OldSize: longint;
|
---|
605 | SizeString: string;
|
---|
606 |
|
---|
607 | LimitedSizes: boolean;
|
---|
608 | Size: longint;
|
---|
609 | NearestSize: longint;
|
---|
610 | NearestSizeIndex: longint;
|
---|
611 |
|
---|
612 | Sizes: TList;
|
---|
613 |
|
---|
614 | procedure AddSize( const size: longint );
|
---|
615 | begin
|
---|
616 | if Sizes.IndexOf( pointer( size ) ) = -1 then
|
---|
617 | Sizes.Add( pointer( size ) );
|
---|
618 | end;
|
---|
619 | Begin
|
---|
620 | Sizes := TList.Create;
|
---|
621 |
|
---|
622 | Face := GetStyleFont.FaceName;
|
---|
623 | try
|
---|
624 | OldSize := StrToInt( FSizeCombo.Caption );
|
---|
625 | except
|
---|
626 | OldSize := 8;
|
---|
627 | end;
|
---|
628 |
|
---|
629 | FSizeCombo.BeginUpdate;
|
---|
630 | FSizeCombo.Clear;
|
---|
631 |
|
---|
632 | LimitedSizes := true;
|
---|
633 |
|
---|
634 | For FontIndex := 0 To Screen.FontCount - 1 Do
|
---|
635 | Begin
|
---|
636 | TheFont := Screen.Fonts[ FontIndex ];
|
---|
637 | If TheFont.FaceName = Face Then
|
---|
638 | Begin
|
---|
639 | // this is a font for the current face.
|
---|
640 | if TheFont.FontType = ftBitmap then
|
---|
641 | begin
|
---|
642 | // just insert the specified point size
|
---|
643 | AddSize( TheFont.NominalPointSize );
|
---|
644 | end
|
---|
645 | else
|
---|
646 | begin
|
---|
647 | // an outline font...
|
---|
648 | LimitedSizes := false;
|
---|
649 | for SizeIndex := Low( StandardOutlineSizes ) to High( StandardOutlineSizes ) do
|
---|
650 | begin
|
---|
651 | AddSize( StandardOutlineSizes[ SizeIndex ] );
|
---|
652 | end;
|
---|
653 | end;
|
---|
654 | end;
|
---|
655 | End;
|
---|
656 |
|
---|
657 | // sort from small to large
|
---|
658 | Sizes.Sort( LongintListCompare );
|
---|
659 |
|
---|
660 | // add to combobox
|
---|
661 | For SizeIndex := 0 to Sizes.Count - 1 do
|
---|
662 | begin
|
---|
663 | SizeString := IntToStr( longint( Sizes[ SizeIndex ] ) );
|
---|
664 | FSizeCombo.Items.Add( SizeString );
|
---|
665 | end;
|
---|
666 |
|
---|
667 | if LimitedSizes then
|
---|
668 | begin
|
---|
669 | // Find nearest match for old size
|
---|
670 | if Sizes.Count > 0 then
|
---|
671 | begin
|
---|
672 | NearestSizeIndex := 0;
|
---|
673 | NearestSize := longint( Sizes[ 0 ] );
|
---|
674 | for SizeIndex := 1 to Sizes.Count - 1 do
|
---|
675 | begin
|
---|
676 | Size := longint( Sizes[ SizeIndex ] );
|
---|
677 | if Abs( Size - OldSize ) < Abs( NearestSize - OldSize ) then
|
---|
678 | begin
|
---|
679 | // closer,
|
---|
680 | NearestSizeIndex := SizeIndex;
|
---|
681 | NearestSize := Size;
|
---|
682 | end;
|
---|
683 | end;
|
---|
684 | end
|
---|
685 | else
|
---|
686 | begin
|
---|
687 | NearestSizeIndex := -1;
|
---|
688 | end;
|
---|
689 |
|
---|
690 | FSizeCombo.ItemIndex := NearestSizeIndex;
|
---|
691 | end
|
---|
692 | else
|
---|
693 | begin
|
---|
694 | FSizeCombo.Text := IntToStr( OldSize );
|
---|
695 |
|
---|
696 | // if there's an exact match, select it, if not select nothing
|
---|
697 | NearestSizeIndex := FSizeCombo.Items.IndexOf( IntToStr( OldSize ) );
|
---|
698 |
|
---|
699 | FSizeCombo.ItemIndex := NearestSizeIndex
|
---|
700 |
|
---|
701 | end;
|
---|
702 |
|
---|
703 | FSizeCombo.EndUpdate;
|
---|
704 |
|
---|
705 | Sizes.Destroy;
|
---|
706 | End;
|
---|
707 |
|
---|
708 |
|
---|
709 | {$HINTS OFF}
|
---|
710 | Procedure TCustomFontDialog.OnSelectFace( Sender: TObject; Index: LongInt );
|
---|
711 | Begin
|
---|
712 | FUpdating := true;
|
---|
713 | InsertStyles;
|
---|
714 | InsertSizes;
|
---|
715 | FUpdating := false;
|
---|
716 | SelectFont;
|
---|
717 | End;
|
---|
718 |
|
---|
719 | Procedure TCustomFontDialog.OnSelectStyle( Sender: TObject; Index: LongInt );
|
---|
720 | Begin
|
---|
721 | if FUpdating then
|
---|
722 | exit;
|
---|
723 | FUpdating := true;
|
---|
724 | InsertSizes;
|
---|
725 | FUpdating := false;
|
---|
726 | SelectFont;
|
---|
727 | End;
|
---|
728 |
|
---|
729 | Procedure TCustomFontDialog.OnSetSize( Sender: TObject );
|
---|
730 | Begin
|
---|
731 | if FUpdating then
|
---|
732 | exit;
|
---|
733 | SelectFont;
|
---|
734 | End;
|
---|
735 |
|
---|
736 | Procedure TCustomFontDialog.OnSizeClick( Sender: TObject; Index: longint );
|
---|
737 | Begin
|
---|
738 | // make a single click select
|
---|
739 | FSizeCombo.Text := FSizeCombo.Items[ Index ];
|
---|
740 | End;
|
---|
741 |
|
---|
742 | Procedure TCustomFontDialog.OnStyleChanged( Sender:TObject );
|
---|
743 | Begin
|
---|
744 | SelectFont;
|
---|
745 | End;
|
---|
746 |
|
---|
747 | Initialization
|
---|
748 | {Register classes}
|
---|
749 | RegisterClasses([TCustomFontDialog]);
|
---|
750 | End.
|
---|
751 |
|
---|