2 {* Font combobox is usefull for selecting font names ,
3 becouse visually express each font (only screen fonts).
5 If font is TrueType small TT icon is created before font name.
8 |<i> Author : Boguslaw Brandys </i>
10 |<i> Email : brandysb@poczta.onet.pl </i>
18 Windows
, Messages
, KOL
;
20 {$R KOLFontComboBox.res}
26 PFontCombo
=^TFontCombo
;
27 TKOLFontComboBox
= PFontCombo
;
28 TFontCombo
= object(TObj
)
31 { Private declarations }
36 fFontHeight
: Integer;
39 procedure DoChange(Obj
: PObj
);
40 procedure DestroyFontList
;
41 procedure MakeFontList
;
42 function GetColor
: TColor
;
43 procedure SetColor(Value
: TColor
);
44 procedure SetFontHeight(const Value
: Integer);
45 procedure SetFontName(const Value
: String);
46 procedure SetCursor(const Value
: HCURSOR
);
47 function DrawOneItem(Sender
: PObj
; DC
: HDC
;
48 const Rect
: TRect
; ItemIdx
: Integer; DrawAction
: TDrawAction
;
49 ItemState
: TDrawState
): Boolean;
51 { Protected declarations }
53 { Public declarations }
54 destructor Destroy
; virtual;
56 function SetAlign(Value
: TControlAlign
): PFontCombo
; overload
;
58 function SetPosition(X
, Y
: integer): PFontCombo
; overload
;
60 function SetSize(X
, Y
: integer): PFontCombo
; overload
;
62 property Color
: TColor read GetColor write SetColor
;
64 property FontName
: String read fFontName write SetFontName
;
65 {* Return selected font name , beside when is set combo is positioned to equal font name}
66 property FontHeight
: Integer read fFontHeight write SetFontHeight
;
67 {* For adjust size of displayed font names}
68 property OnChange
: TOnEvent read fOnChange write fOnChange
;
69 {* Fired when font is changed}
70 property Cursor
: HCURSOR read fCursor write SetCursor
;
76 function NewFontComboBox( AOwner
: PControl
): PFontCombo
;
77 {* Creates new font combobox which is generally an opaque for KOL combobox
78 with additional processing of font's list}
87 TFontDef
= packed record
94 function EnumFontsProc(var EnumLogFont
: TEnumLogFont
;
95 var TextMetric
: TNewTextMetric
; FontType
: Integer; Data
: LPARAM
): Integer;
105 control
:= PFontCombo(Data
);
106 FaceName
:= String(EnumLogFont
.elfLogFont
.lfFaceName
);
107 with EnumLogFont
do begin
108 elfLogFont
.lfHeight
:= -control
.fFontHeight
;
109 elfLogFont
.lfWidth
:= 0;
111 if control
.fControl
.IndexOf(FaceName
) < 0 then
113 if EnumLogFont
.elflogfont
.lfCharSet
= SYMBOL_CHARSET
then font
:=0
115 font
:= CreateFontIndirect(EnumLogFont
.elfLogFont
);
116 i
:= control
.fcontrol
.Add(FaceName
);
117 //j := LongInt(font);
118 {if (FontType and TRUETYPE_FONTTYPE) <> 0 then
120 GetMem( FD
, Sizeof( FD
^ ) );
122 FD
.TrueType
:= (FontType
and TRUETYPE_FONTTYPE
) <> 0;
123 control
.fcontrol
.ItemData
[i
] := DWORD( FD
);
132 function NewFontComboBox
;
137 c
:=NewComboBox(AOwner
,[coReadOnly
,coSort
,coOwnerDrawVariable
]);
139 AOwner
.Add2AutoFree(p
);
143 p
.fControl
.Font
.FontName
:= 'MS Sans Serif';
144 p
.fControl
.Font
.FontHeight
:= 11;
145 p
.TTImage
:= LoadIcon(hInstance
,'TTICON');
146 p
.fCursor
:= LoadCursor(0,IDC_ARROW
);
148 p
.fControl
.OnDrawItem
:= p
.DrawOneItem
;
150 p
.fControl
.OnChange
:=p
.DoChange
;
151 p
.fControl
.CurIndex
:=0;
156 procedure TFontCombo
.DoChange(Obj
: PObj
);
158 fFontName
:= fControl
.Items
[fControl
.CurIndex
];
159 if Assigned(fOnChange
) then fOnChange(@Self
);
163 function TFontCombo
.SetAlign(Value
: TControlAlign
): PFontCombo
;
165 fControl
.Align
:=Value
;
169 function TFontCombo
.SetPosition(X
, Y
: integer): PFontCombo
;
176 function TFontCombo
.SetSize(X
, Y
: integer): PFontCombo
;
179 fControl
.Height
:= Y
;
184 function TFontCombo
.GetColor
: TColor
;
186 Result
:= fControl
.Color
;
190 procedure TFontCombo
.SetColor(Value
: TColor
);
192 fControl
.Color
:= Value
;
195 procedure TFontCombo
.SetFontName(const Value
: String);
199 i
:= fControl
.IndexOf(Value
);
203 fControl
.BeginUpdate
;
204 fControl
.CurIndex
:= i
;
210 fControl
.Text := '???';
217 procedure TFontCombo
.SetFontHeight(const Value
: Integer);
219 if (Value
<> fFontHeight
) then
221 fFontHeight
:= Value
;
226 procedure TFontCombo
.SetCursor(const Value
: HCURSOR
);
229 fControl
.Cursor
:= Value
;
233 function TFontCombo
.DrawOneItem(Sender
: PObj
; DC
: HDC
;
234 const Rect
: TRect
; ItemIdx
: Integer; DrawAction
: TDrawAction
;
235 ItemState
: TDrawState
): Boolean;
242 if ItemIdx
> -1 then begin
243 //j := Sender.ItemData[ItemIdx];
244 FD
:= Pointer( PControl(Sender
).ItemData
[ItemIdx
] );
250 DrawIcon(DC
,Rect
.Left
+2,Rect
.Top
+1,TTImage
);
255 xRect
.Left
:= xRect
.Left
+ 20;
256 if xfont
<> 0 then SelectObject(DC
,xfont
)
258 SelectObject(DC
,PControl(Sender
).Font
.Handle
);
259 DrawText(DC
,PChar(PControl(Sender
).Items
[ItemIdx
]),Length(PControl(Sender
).Items
[ItemIdx
]),xRect
,DT_SINGLELINE
or DT_VCENTER
or DT_NOPREFIX
);
260 if (odaSelect
in DrawAction
) then InvertRect(DC
,Rect
);
267 procedure TFontCombo
.DestroyFontList
;
274 for i
:= 0 to fControl
.Count
-1 do
276 //j := fControl.ItemData[i];
277 // if j < 0 then j := -j;
278 FD
:= Pointer( fControl
.ItemData
[i
] );
283 if xfont
<> 0 then DeleteObject(xfont
);
288 destructor TFontCombo
.Destroy
;
292 /////////////////////////////
293 // CloseHandle(TTImage);
294 //////////////////////////////
295 DestroyIcon( TTImage
); //
296 //////////////////////////////
305 procedure TFontCombo
.MakeFontList
;
311 fControl
.BeginUpdate
;
313 EnumFontFamilies(DC
,nil,@EnumFontsProc
,LongInt(@Self
));