Menu

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

Download this file

320 lines (292 with data), 10.0 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
* UDropDownButtons.pas
*
* Defines a class that is used to manage image lists containing drop down
* button images for display in UI widgets.
*
* $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 UDropDownButtons.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UDropDownButtons;
interface
uses
// Delphi
Classes, SyncObjs, Controls, Types, Windows, Graphics;
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(TObject)
strict private
const
cLockTimeout = 10000; {10 secs} // Event signalling timeout
var
fLock: TSimpleEvent; // Signal locks access to Update event
fImages: TImageList; // Image list that stores drop down button images
fOnChange: TNotifyEvent; // OnChange event handler
function ButtonSize: TSize;
{Gets size of drop down button images.
@return Size of button in pixels.
}
procedure SetImgListSize;
{Sets size of bitmaps expected by image list per dimensions of buttons
used.
}
procedure Update;
{Updates button images in image list. Buttons used depend on whether
themes are active.
}
procedure ThemeChangeListener(Sender: TObject);
{Handles theme services change event. Updates button images according to
whether themes are in use or not.
@param Sender [in] Not used.
}
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.
}
public
constructor Create(AOwner: TComponent);
{Class constructor. Sets up object and loads button images into image
list.
}
destructor Destroy; override;
{Class destructor. Tears down 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: TImageList read fImages;
{Image list containing drop down buttons}
property OnChange: TNotifyEvent
read fOnChange write fOnChange;
{Event triggered when image list changes, i.e. when themes change}
end;
implementation
uses
// Delphi
SysUtils, 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;
function TDropDownButtons.ButtonSize: TSize;
{Gets size of drop down button images.
@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
FreeAndNil(BtnBmp);
end;
end;
constructor TDropDownButtons.Create(AOwner: TComponent);
{Class constructor. Sets up object and loads button images into image
list.
}
begin
inherited Create;
fImages := TImageList.Create(AOwner);
fImages.Width := 16;
fImages.Height := 16;
fLock := TSimpleEvent.Create; // lock for protected sections: needed by Update
fLock.SetEvent;
Update; // loads check boxes into image list
ThemeServicesEx.AddChangeEventHandler(ThemeChangeListener);
end;
destructor TDropDownButtons.Destroy;
{Class destructor. Tears down object.
}
begin
ThemeServicesEx.RemoveChangeEventHandler(ThemeChangeListener);
FreeAndNil(fLock);
FreeAndNil(fImages);
inherited;
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
fImages.Draw(
Canvas,
TopLeft.X,
TopLeft.Y,
ButtonImageIdx(Hot, Focussed)
);
end;
procedure TDropDownButtons.SetImgListSize;
{Sets size of bitmaps expected by image list per dimensions of buttons used.
}
var
Size: TSize; // required size
begin
Size := ButtonSize;
fImages.Width := Size.cx;
fImages.Height := Size.cy;
end;
procedure TDropDownButtons.ThemeChangeListener(Sender: TObject);
{Handles theme services change event. Updates button images according to
whether themes are in use or not.
@param Sender [in] Not used.
}
begin
Update;
end;
procedure TDropDownButtons.Update;
{Updates button images in image list. Buttons used 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
// Wait for any lock to be opened
fLock.WaitFor(cLockTimeout);
// Close lock: this is used to prevent image list from being modified
// asynchronously
fLock.ResetEvent;
try
// Initialise image list
fImages.Clear;
SetImgListSize;
// 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 := fImages.Width;
BtnBmp.Height := fImages.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;
fImages.AddMasked(BtnBmp, cMaskColour);
end;
end;
finally
FreeAndNil(BtnBmp);
end;
finally
// Open lock: this permits modification of image list
fLock.SetEvent;
end;
// Trigger event notifying that check boxes have changed
if Assigned(fOnChange) then
fOnChange(Self);
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.