Menu

[r2218]: / trunk / Src / FmCompilersDlg.UCompilerListMgr.pas  Maximize  Restore  History

Download this file

271 lines (238 with data), 8.4 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
{
* FmCompilersDlg.UCompilerListMgr.pas
*
* Implements a class that manages display of compiler names and glyphs in an
* owner draw list box. This is a helper class for TCompilersDlg.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is FmCompilersDlg.UCompilerListMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmCompilersDlg.UCompilerListMgr;
interface
uses
// Delphi
Classes, Controls, StdCtrls, Windows,
// Project
Compilers.UGlobals;
type
/// <summary>
/// Manages display of compiler names and glyphs in an owner draw list box.
/// </summary>
/// <remarks>
/// This is a helper class for TCompilersDlg.
/// </remarks>
TCompilerListMgr = class(TObject)
strict private
/// <summary>Reference to managed list box.</summary>
/// <remarks>Must be owenr draw.</remarks>
fLB: TListBox;
/// <summary>List of compilers to be displayed in list box.</summary>
fCompilers: ICompilers;
/// <summary>Reference to OnSelect event handler.</summary>
fOnSelect: TNotifyEvent;
/// <summary>OnClick event handler for list box. Triggers OnSelect event.
/// </summary>
procedure LBClickHandler(Sender: TObject);
/// <summary>OnDrawItem event handler for list box. Performs custom
/// drawing.</summary>
procedure LBDrawItemHandler(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
/// <summary>Triggers OnSelect event.</summary>
procedure DoSelect;
/// <summary>Read accessor for Selected property. Gets compiler associated
/// with selected list item.</summary>
function GetSelected: ICompiler;
public
/// <summary>Object constructor. Sets up object for given list box control
/// and list of compilers.</summary>
constructor Create(const LB: TListBox; const Compilers: ICompilers);
/// <summary>Initialises list box to display required compilers.</summary>
/// <remarks>This initialisation has to be performed when host for is
/// shown and not before.</remarks>
procedure Initialise;
/// <summary>Refreshes display of entire list.</summary>
procedure Refresh; overload;
/// <summary>Refreshes display of list item associated with given compiler.
/// </summary>
procedure Refresh(Compiler: ICompiler); overload;
/// <summary>Event triggered when selected item in list changes.</summary>
/// <remarks>Read Selected property to get newly selected compiler.
/// </remarks>
property OnSelect: TNotifyEvent read fOnSelect write fOnSelect;
/// <summary>Reference to compiler associated with currently selected list
/// item.</summary>
property Selected: ICompiler read GetSelected;
end;
implementation
uses
// Delphi
Graphics, GraphUtil,
// Project
UStructs, UThemesEx;
{ TCompilerListMgr }
constructor TCompilerListMgr.Create(const LB: TListBox;
const Compilers: ICompilers);
begin
inherited Create;
fLB := LB;
fLB.OnClick := LBClickHandler;
fLB.OnDrawItem := LBDrawItemHandler;
fCompilers := Compilers;
end;
procedure TCompilerListMgr.DoSelect;
begin
if Assigned(fOnSelect) then
fOnSelect(Self);
end;
function TCompilerListMgr.GetSelected: ICompiler;
begin
Result := fCompilers[TCompilerID(fLB.ItemIndex)];
end;
procedure TCompilerListMgr.Initialise;
var
CompID: TCompilerID; // loops thru supported compilers
begin
inherited;
// Add empty list items: one per supported compiler
// (note we don't need item text since we handle drawing of list items
// ourselves and get display details from compiler objects
for CompID := Low(TCompilerID) to High(TCompilerID) do
fLB.Items.Add('');
// Select first compiler in list and trigger selection event for it
fLB.ItemIndex := 0;
DoSelect;
end;
procedure TCompilerListMgr.LBClickHandler(Sender: TObject);
begin
DoSelect;
end;
procedure TCompilerListMgr.LBDrawItemHandler(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
LB: TListBox; // list box being drawn
TxtExtent: TSize; // extent of text to be displayed
ItemRect: TRectEx; // rectangle bounding the item being drawn
ImgRect: TRectEx; // bounding rectangle of any glyph
TxtRect: TRectEx; // bounding rectangle of text to be drawn
Bmp: TBitmap; // reference to bitmap to be displayed (or nil if none)
DrawHeight: Integer; // total height of drawing (text below bitmap)
Compiler: ICompiler; // reference to compiler associated with list item
begin
// Copy item rectangle as extended rect
ItemRect := Rect;
// Get reference to list box control
LB := Control as TListBox;
// Get reference to compiler object associated with list item and its bitmap
Compiler := fCompilers[TCompilerID(Index)];
Bmp := Compiler.GetGlyph;
// Set font style: bold if compiler available
if Compiler.IsAvailable then
LB.Canvas.Font.Style := [fsBold]
else
LB.Canvas.Font.Style := [];
// Calculate display rectangles for text and any bitmap
TxtExtent := LB.Canvas.TextExtent(Compiler.GetName);
if Assigned(Bmp) then
begin
// Bitmap included: bitmap drawn above text and bounding box or both centred
// horizontally and vertically
DrawHeight := TxtExtent.cy + 2 + Bmp.Height;
ImgRect := TRectEx.CreateBounds(
(ItemRect.Left + ItemRect.Right - Bmp.Width) div 2,
(ItemRect.Top + ItemRect.Bottom - DrawHeight) div 2,
Bmp.Width,
Bmp.Height
);
TxtRect := TRectEx.CreateBounds(
(ItemRect.Left + ItemRect.Right - TxtExtent.cx) div 2,
ImgRect.Bottom + 2,
TxtExtent
);
end
else
begin
// No bitmap: text centred vertically and horizontally
TxtRect := TRectEx.CreateBounds(
(ItemRect.Left + ItemRect.Right - TxtExtent.cx) div 2,
(ItemRect.Top + ItemRect.Bottom - TxtExtent.cy) div 2,
TxtExtent
);
end;
// Erase background
LB.Canvas.Pen.Color := LB.Color;
LB.Canvas.Brush.Color := LB.Color;
LB.Canvas.FillRect(ItemRect);
if (odFocused in State) or (odSelected in State) then
begin
// Draw highlighting (smaller than item's rectangle)
if odFocused in State then
begin
if ThemeServicesEx.ThemesEnabled then
LB.Canvas.Brush.Color := GetHighLightColor(clHighlight)
else
LB.Canvas.Brush.Color := clHighlight;
LB.Canvas.Font.Color := clHighlightText;
end
else
begin
LB.Canvas.Brush.Color := clBtnFace;
LB.Canvas.Font.Color := LB.Font.Color;
end;
LB.Canvas.Pen.Color := GetShadowColor(LB.Canvas.Brush.Color);
LB.Canvas.Rectangle(ItemRect.Inflate(-4, -4));
end
else
// No highlighting: just ensure font colour correct
LB.Canvas.Font.Color := LB.Font.Color;
// Draw text and any bitamp
LB.Canvas.TextOut(TxtRect.Left, TxtRect.Top, Compiler.GetName);
if Assigned(Bmp) then
LB.Canvas.BrushCopy(ImgRect, Bmp, Bmp.Canvas.ClipRect, clFuchsia);
// Draw separator line if item not last one
if Index < Pred(LB.Count) then
begin
LB.Canvas.Pen.Color := clBtnShadow;
LB.Canvas.MoveTo(ItemRect.Left + 4, ItemRect.Bottom - 1);
LB.Canvas.LineTo(ItemRect.Right - 4, ItemRect.Bottom - 1);
end;
// Remove any focus rectangle
if odFocused in State then
LB.Canvas.DrawFocusRect(ItemRect);
end;
procedure TCompilerListMgr.Refresh;
begin
fLB.Invalidate;
end;
procedure TCompilerListMgr.Refresh(Compiler: ICompiler);
var
InvalidRect: TRectEx;
begin
InvalidRect := fLB.ItemRect(Ord(Compiler.GetID));
InvalidateRect(fLB.Handle, @InvalidRect, False);
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.