Menu

[r3207]: / trunk / Src / UDropDownButtons.pas  Maximize  Restore  History

Download this file

232 lines (207 with data), 7.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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2009-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a class that is used to manage image lists containing drop down
* button images for display in UI widgets.
}
unit UDropDownButtons;
interface
uses
// Delphi
Classes, Controls, Windows, Graphics,
// Project
UUIWidgetImages;
type
{
TDropDownButtons:
Class used to manage image lists containing drop down button images for
display in UI widgets. The class also maps drop down button identifiers to
indexes into image lists. Different images are loaded depending on whether
themes are active. Update is called to change bitmaps when themes are
changed.
}
TDropDownButtons = class(TUIWidgetImages)
strict private
class function ButtonImageIdx(const Hot, Focussed: Boolean): Integer;
{Gets index of a drop down button in image list.
@param Hot [in] Flag indicating whether button is to be hot or normal.
@param Focussed [in] Flag indicating whether button is focussed.
@return Required image index.
}
strict protected
procedure RecreateImages; override;
{Recreates image list containing appropriate drop-down buttons images.
Images depend on whether themes are active.
}
function GetImageSize: TSize; override;
{Gets size of drop down button images. Size of image may differ depending
on whether themes are active.
@return Size of button in pixels.
}
public
constructor Create(AOwner: TComponent);
{Object constructor. Sets up object and loads button images into image
list.
@param AOwner [in] Component that owns this object.
}
procedure Draw(const Canvas: TCanvas; const TopLeft: TPoint;
const Hot, Focussed: Boolean);
{Draws the required drop down button on a canvas.
@param Canvas [in] Canvas where button is to be drawn.
@param TopLeft [in] Top left corner of button in canvas.
@param Hot [in] Whether button to be drawn in hot state.
@param Focussed [in] Whether button is to display a focus rectangle.
}
property Images;
{Image list containing drop down buttons. Inherited protected property
made public}
end;
implementation
uses
// Delphi
Themes,
// Project
UStructs, UThemesEx;
{ TDropDownButtons }
class function TDropDownButtons.ButtonImageIdx(const Hot,
Focussed: Boolean): Integer;
{Gets index of a drop down button in image list.
@param Hot [in] Flag indicating whether button is to be hot or normal.
@param Focussed [in] Flag indicating whether button is focussed.
@return Required image index.
}
begin
Result := Ord(Hot) + Ord(Focussed) shl 1;
end;
constructor TDropDownButtons.Create(AOwner: TComponent);
{Object constructor. Sets up object and loads button images into image list.
@param AOwner [in] Component that owns this object.
}
begin
inherited Create(TImageList.Create(AOwner), True);
end;
procedure TDropDownButtons.Draw(const Canvas: TCanvas; const TopLeft: TPoint;
const Hot, Focussed: Boolean);
{Draws the required drop down button on a canvas.
@param Canvas [in] Canvas where button is to be drawn.
@param TopLeft [in] Top left corner of button in canvas.
@param Hot [in] Whether button to be drawn in hot state.
@param Focussed [in] Whether button is to display a focus rectangle.
}
begin
Images.Draw(
Canvas,
TopLeft.X,
TopLeft.Y,
ButtonImageIdx(Hot, Focussed)
);
end;
function TDropDownButtons.GetImageSize: TSize;
{Gets size of drop down button images. Size of image may differ depending on
whether themes are active.
@return Size of button in pixels.
}
var
BtnBmp: TBitmap; // Bitmap used to load combo box buttons resource
begin
// We return same size whether themes are active or not, and this is the size
// of non themed buttons. This is because non-themed buttons don't have a
// fixed size. We assume all differenr button states are same size.
BtnBmp := TBitmap.Create;
try
BtnBmp.Handle := LoadBitmap(0, MakeIntResource(OBM_DNARROW));
Result.cx := BtnBmp.Width;
Result.cy := BtnBmp.Height;
finally
BtnBmp.Free;
end;
end;
procedure TDropDownButtons.RecreateImages;
{Recreates image list containing appropriate drop-down buttons images. Images
depend on whether themes are active.
}
// ---------------------------------------------------------------------------
procedure GetNonThemedButton(const Bmp: TBitmap; const Hot: Boolean);
{Draws a non-themed button on a bitmap.
@param Bmp [in] Bitmap to receive drawing.
@param Hot [in] Whether to draw button in hot or normal state.
}
const
// Map of index of drop down buttons in image list to id of standard UI
// bitmap representing button in normal and hot styles
cUnThemedComboBoxButtons: array[Boolean]
of Integer = (OBM_DNARROW, OBM_DNARROW);
begin
Bmp.Handle := LoadBitmap(
0, MakeIntResource(cUnThemedComboBoxButtons[Hot])
);
end;
procedure GetThemedButton(const Bmp: TBitmap; const Hot: Boolean);
{Draws a themed button on a bitmap.
@param Bmp [in] Bitmap to receive drawing.
@param Hot [in] Whether to draw button in hot or normal state.
}
const
// Map of index of drop down buttons in image list to themed element
// representing button in normal and hot styles
cThemedComboBoxButtons: array[Boolean]
of TThemedComboBox = (tcDropDownButtonNormal, tcDropDownButtonHot);
var
BtnRect: TRect; // bounds rectangle of bitmap
begin
BtnRect := TRectEx.Create(0, 0, Bmp.Width, Bmp.Height);
Bmp.Canvas.FillRect(BtnRect);
ThemeServicesEx.DrawElement(
cThemedComboBoxButtons[Hot], Bmp, BtnRect
);
end;
// ---------------------------------------------------------------------------
const
cMaskColour: TColor = clFuchsia; // image list mask colour
var
BtnBmp: TBitmap; // bitmap on which buttons are drawn
FocusRect: TRectEx; // bounds of any required focus rectangle
Hot: Boolean; // loops thru not-hot / hot
Focussed: Boolean; // loops thru not-focussed / focussed
begin
// Load check boxes into image list
// We load standard combo box buttons into a bitmap which is then added to
// image list.
BtnBmp := TBitmap.Create;
try
// Prepare bitmap
BtnBmp.Width := Images.Width;
BtnBmp.Height := Images.Height;
BtnBmp.Canvas.Brush.Color := cMaskColour;
// Load normal then hot bitmaps
for Focussed := Low(Boolean) to High(Boolean) do
begin
for Hot := Low(Boolean) to High(Boolean) do
begin
if ThemeServicesEx.ThemesEnabled then
GetThemedButton(BtnBmp, Hot)
else
GetNonThemedButton(BtnBmp, Hot);
if Focussed then
begin
FocusRect := TRectEx.CreateBounds(
0, 0, BtnBmp.Width, BtnBmp.Height
);
FocusRect.InflateBy(-2, -2);
BtnBmp.Canvas.DrawFocusRect(FocusRect);
end;
Images.AddMasked(BtnBmp, cMaskColour);
end;
end;
finally
BtnBmp.Free;
end;
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.