| 1 |
{ |
| 2 |
Double Commander Components |
| 3 |
------------------------------------------------------------------------- |
| 4 |
Extended ComboBox classes |
| 5 |
|
| 6 |
Copyright (C) 2012 Przemyslaw Nagay ([email protected]) |
| 7 |
Copyright (C) 2015-2023 Alexander Koblov ([email protected]) |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU General Public License as |
| 11 |
published by the Free Software Foundation; either version 2 of the |
| 12 |
License, or (at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
} |
| 22 |
|
| 23 |
unit KASComboBox; |
| 24 |
|
| 25 |
{$mode objfpc}{$H+} |
| 26 |
|
| 27 |
interface |
| 28 |
|
| 29 |
uses |
| 30 |
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, |
| 31 |
ColorBox, Buttons, LMessages, Types, KASButton; |
| 32 |
|
| 33 |
const |
| 34 |
DEF_COLOR_STYLE = [cbStandardColors, cbExtendedColors, |
| 35 |
cbSystemColors, cbPrettyNames]; |
| 36 |
|
| 37 |
type |
| 38 |
|
| 39 |
{ TComboBoxWithDelItems } |
| 40 |
|
| 41 |
{en |
| 42 |
Combo box that allows removing items with Shift+Delete. |
| 43 |
} |
| 44 |
TComboBoxWithDelItems = class(TComboBox) |
| 45 |
protected |
| 46 |
procedure KeyDown(var Key: Word; Shift: TShiftState); override; |
| 47 |
end; |
| 48 |
|
| 49 |
{ TComboBoxAutoWidth } |
| 50 |
|
| 51 |
TComboBoxAutoWidth = class(TComboBox) |
| 52 |
protected |
| 53 |
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; |
| 54 |
WithThemeSpace: Boolean); override; |
| 55 |
procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; |
| 56 |
const AXProportion, AYProportion: Double); override; |
| 57 |
end; |
| 58 |
|
| 59 |
{ TKASColorBox } |
| 60 |
|
| 61 |
TKASColorBox = class(TColorBox) |
| 62 |
protected |
| 63 |
procedure SetCustomColor(AColor: TColor); |
| 64 |
function PickCustomColor: Boolean; override; |
| 65 |
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; |
| 66 |
WithThemeSpace: Boolean); override; |
| 67 |
procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; |
| 68 |
const AXProportion, AYProportion: Double); override; |
| 69 |
public |
| 70 |
constructor Create(AOwner: TComponent); override; |
| 71 |
published |
| 72 |
property DefaultColorColor default clNone; |
| 73 |
property Style default DEF_COLOR_STYLE; |
| 74 |
end; |
| 75 |
|
| 76 |
{ TKASColorBoxButton } |
| 77 |
|
| 78 |
TKASColorBoxButton = class(TCustomControl) |
| 79 |
private |
| 80 |
function GetSelected: TColor; |
| 81 |
function GetStyle: TColorBoxStyle; |
| 82 |
function GetOnChange: TNotifyEvent; |
| 83 |
function GetColorDialog: TColorDialog; |
| 84 |
procedure SetSelected(AValue: TColor); |
| 85 |
procedure SetStyle(AValue: TColorBoxStyle); |
| 86 |
procedure SetOnChange(AValue: TNotifyEvent); |
| 87 |
procedure SetColorDialog(AValue: TColorDialog); |
| 88 |
protected |
| 89 |
FButton: TKASButton; |
| 90 |
FColorBox: TKASColorBox; |
| 91 |
procedure DoAutoSize; override; |
| 92 |
procedure EnabledChanged; override; |
| 93 |
procedure ButtonClick(Sender: TObject); |
| 94 |
class function GetControlClassDefaultSize: TSize; override; |
| 95 |
procedure CMParentColorChanged(var Message: TLMessage); message CM_PARENTCOLORCHANGED; |
| 96 |
public |
| 97 |
constructor Create(AOwner: TComponent); override; |
| 98 |
procedure SetFocus; override; |
| 99 |
function Focused: Boolean; override; |
| 100 |
property Selected: TColor read GetSelected write SetSelected default clBlack; |
| 101 |
published |
| 102 |
property Align; |
| 103 |
property Anchors; |
| 104 |
property TabOrder; |
| 105 |
property Constraints; |
| 106 |
property BorderSpacing; |
| 107 |
property AutoSize default True; |
| 108 |
property OnChange: TNotifyEvent read GetOnChange write SetOnChange; |
| 109 |
property ColorDialog: TColorDialog read GetColorDialog write SetColorDialog; |
| 110 |
property Style: TColorBoxStyle read GetStyle write SetStyle default DEF_COLOR_STYLE; |
| 111 |
end; |
| 112 |
|
| 113 |
procedure Register; |
| 114 |
|
| 115 |
implementation |
| 116 |
|
| 117 |
uses |
| 118 |
LCLType, LCLIntf; |
| 119 |
|
| 120 |
procedure Register; |
| 121 |
begin |
| 122 |
RegisterComponents('KASComponents',[TComboBoxWithDelItems, TComboBoxAutoWidth, |
| 123 |
TKASColorBox, TKASColorBoxButton]); |
| 124 |
end; |
| 125 |
|
| 126 |
procedure CalculateSize(ComboBox: TCustomComboBox; |
| 127 |
var PreferredWidth: Integer; PreferredHeight: Integer); |
| 128 |
var |
| 129 |
DC: HDC; |
| 130 |
R: TRect; |
| 131 |
I, M: Integer; |
| 132 |
Flags: Cardinal; |
| 133 |
OldFont: HGDIOBJ; |
| 134 |
MaxWidth: Integer; |
| 135 |
LabelText: String; |
| 136 |
Idx: Integer = -1; |
| 137 |
begin |
| 138 |
with ComboBox do |
| 139 |
begin |
| 140 |
MaxWidth:= Constraints.MinMaxWidth(10000); |
| 141 |
|
| 142 |
if Items.Count = 0 then |
| 143 |
LabelText:= Text |
| 144 |
else begin |
| 145 |
M := Canvas.TextWidth(Text); |
| 146 |
for I := 0 to Items.Count - 1 do |
| 147 |
begin |
| 148 |
Flags := Canvas.TextWidth(Items[I]); |
| 149 |
if Flags > M then |
| 150 |
begin |
| 151 |
M := Flags; |
| 152 |
Idx := I; |
| 153 |
end; |
| 154 |
end; |
| 155 |
if Idx < 0 then |
| 156 |
LabelText := Text |
| 157 |
else begin |
| 158 |
LabelText := Items[Idx]; |
| 159 |
end; |
| 160 |
end; |
| 161 |
|
| 162 |
if LabelText = '' then begin |
| 163 |
PreferredWidth := 1; |
| 164 |
Exit; |
| 165 |
end; |
| 166 |
|
| 167 |
DC := GetDC(Parent.Handle); |
| 168 |
try |
| 169 |
LabelText:= LabelText + 'W'; |
| 170 |
R := Rect(0, 0, MaxWidth, 10000); |
| 171 |
OldFont := SelectObject(DC, HGDIOBJ(Font.Reference.Handle)); |
| 172 |
Flags := DT_CALCRECT or DT_EXPANDTABS; |
| 173 |
|
| 174 |
DrawText(DC, PChar(LabelText), Length(LabelText), R, Flags); |
| 175 |
SelectObject(DC, OldFont); |
| 176 |
PreferredWidth := (R.Right - R.Left) + PreferredHeight; |
| 177 |
finally |
| 178 |
ReleaseDC(Parent.Handle, DC); |
| 179 |
end; |
| 180 |
end; |
| 181 |
end; |
| 182 |
|
| 183 |
function CalculateHeight(ComboBox: TCustomComboBox): Integer; |
| 184 |
var |
| 185 |
DC: HDC; |
| 186 |
R: TRect; |
| 187 |
Flags: Cardinal; |
| 188 |
OldFont: HGDIOBJ; |
| 189 |
LabelText: String; |
| 190 |
MaxHeight: Integer; |
| 191 |
begin |
| 192 |
with ComboBox do |
| 193 |
begin |
| 194 |
MaxHeight:= Constraints.MinMaxHeight(10000); |
| 195 |
|
| 196 |
DC := GetDC(Parent.Handle); |
| 197 |
try |
| 198 |
LabelText:= Items.Text; |
| 199 |
R := Rect(0, 0, 10000, MaxHeight); |
| 200 |
OldFont := SelectObject(DC, HGDIOBJ(Font.Reference.Handle)); |
| 201 |
Flags := DT_CALCRECT or DT_EXPANDTABS or DT_SINGLELINE; |
| 202 |
|
| 203 |
DrawText(DC, PChar(LabelText), Length(LabelText), R, Flags); |
| 204 |
SelectObject(DC, OldFont); |
| 205 |
Result := (R.Bottom - R.Top); |
| 206 |
finally |
| 207 |
ReleaseDC(Parent.Handle, DC); |
| 208 |
end; |
| 209 |
end; |
| 210 |
end; |
| 211 |
|
| 212 |
{ TComboBoxWithDelItems } |
| 213 |
|
| 214 |
procedure TComboBoxWithDelItems.KeyDown(var Key: Word; Shift: TShiftState); |
| 215 |
var |
| 216 |
Index: Integer; |
| 217 |
begin |
| 218 |
if DroppedDown and (Key = VK_DELETE) and (Shift = [ssShift]) then |
| 219 |
begin |
| 220 |
Index := ItemIndex; |
| 221 |
if (Index >= 0) and (Index < Items.Count) then |
| 222 |
begin |
| 223 |
Items.Delete(Index); |
| 224 |
ItemIndex := Index; |
| 225 |
Key := 0; |
| 226 |
end; |
| 227 |
end; |
| 228 |
inherited KeyDown(Key, Shift); |
| 229 |
end; |
| 230 |
|
| 231 |
{ TComboBoxAutoWidth } |
| 232 |
|
| 233 |
procedure TComboBoxAutoWidth.CalculatePreferredSize(var PreferredWidth, |
| 234 |
PreferredHeight: Integer; WithThemeSpace: Boolean); |
| 235 |
begin |
| 236 |
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight, WithThemeSpace); |
| 237 |
|
| 238 |
if csDesigning in ComponentState then Exit; |
| 239 |
if (Parent = nil) or (not Parent.HandleAllocated) then Exit; |
| 240 |
|
| 241 |
CalculateSize(Self, PreferredWidth, PreferredHeight); |
| 242 |
end; |
| 243 |
|
| 244 |
procedure TComboBoxAutoWidth.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; |
| 245 |
const AXProportion, AYProportion: Double); |
| 246 |
begin |
| 247 |
// Don't auto adjust horizontal layout |
| 248 |
inherited DoAutoAdjustLayout(AMode, 1.0, AYProportion); |
| 249 |
end; |
| 250 |
|
| 251 |
{ TKASColorBox } |
| 252 |
|
| 253 |
procedure TKASColorBox.SetCustomColor(AColor: TColor); |
| 254 |
var |
| 255 |
Index: Integer; |
| 256 |
begin |
| 257 |
for Index:= Ord(cbCustomColor in Style) to Items.Count - 1 do |
| 258 |
begin |
| 259 |
if Colors[Index] = AColor then |
| 260 |
begin |
| 261 |
Selected:= AColor; |
| 262 |
Exit; |
| 263 |
end; |
| 264 |
end; |
| 265 |
if cbCustomColor in Style then |
| 266 |
begin |
| 267 |
Items.Objects[0]:= TObject(PtrInt(AColor)); |
| 268 |
end; |
| 269 |
Items.AddObject('$' + HexStr(AColor, 8), TObject(PtrInt(AColor))); |
| 270 |
Selected:= AColor; |
| 271 |
end; |
| 272 |
|
| 273 |
function TKASColorBox.PickCustomColor: Boolean; |
| 274 |
begin |
| 275 |
Result:= inherited PickCustomColor; |
| 276 |
SetCustomColor(Colors[0]); |
| 277 |
end; |
| 278 |
|
| 279 |
procedure TKASColorBox.CalculatePreferredSize(var PreferredWidth, |
| 280 |
PreferredHeight: Integer; WithThemeSpace: Boolean); |
| 281 |
begin |
| 282 |
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight, WithThemeSpace); |
| 283 |
|
| 284 |
if csDesigning in ComponentState then Exit; |
| 285 |
if (Parent = nil) or (not Parent.HandleAllocated) then Exit; |
| 286 |
|
| 287 |
if (csSubComponent in ComponentStyle) then |
| 288 |
begin |
| 289 |
ItemHeight:= CalculateHeight(Self); |
| 290 |
if (Parent.Anchors * [akLeft, akRight] = [akLeft, akRight]) then |
| 291 |
Exit; |
| 292 |
end; |
| 293 |
|
| 294 |
CalculateSize(Self, PreferredWidth, PreferredHeight); |
| 295 |
PreferredWidth+= ColorRectWidth + ColorRectOffset; |
| 296 |
end; |
| 297 |
|
| 298 |
procedure TKASColorBox.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; |
| 299 |
const AXProportion, AYProportion: Double); |
| 300 |
begin |
| 301 |
if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then |
| 302 |
begin |
| 303 |
if ColorRectWidthStored then |
| 304 |
ColorRectWidth:= Round(ColorRectWidth * AXProportion); |
| 305 |
end; |
| 306 |
// Don't auto adjust horizontal layout |
| 307 |
inherited DoAutoAdjustLayout(AMode, 1.0, AYProportion); |
| 308 |
end; |
| 309 |
|
| 310 |
constructor TKASColorBox.Create(AOwner: TComponent); |
| 311 |
begin |
| 312 |
inherited Create(AOwner); |
| 313 |
Style:= DEF_COLOR_STYLE; |
| 314 |
DefaultColorColor:= clNone; |
| 315 |
end; |
| 316 |
|
| 317 |
{ TKASColorBoxButton } |
| 318 |
|
| 319 |
function TKASColorBoxButton.GetSelected: TColor; |
| 320 |
begin |
| 321 |
Result:= FColorBox.Selected; |
| 322 |
end; |
| 323 |
|
| 324 |
function TKASColorBoxButton.GetStyle: TColorBoxStyle; |
| 325 |
begin |
| 326 |
Result:= FColorBox.Style; |
| 327 |
end; |
| 328 |
|
| 329 |
function TKASColorBoxButton.GetOnChange: TNotifyEvent; |
| 330 |
begin |
| 331 |
Result:= FColorBox.OnChange; |
| 332 |
end; |
| 333 |
|
| 334 |
function TKASColorBoxButton.GetColorDialog: TColorDialog; |
| 335 |
begin |
| 336 |
Result:= FColorBox.ColorDialog; |
| 337 |
end; |
| 338 |
|
| 339 |
procedure TKASColorBoxButton.SetSelected(AValue: TColor); |
| 340 |
begin |
| 341 |
FColorBox.SetCustomColor(AValue); |
| 342 |
end; |
| 343 |
|
| 344 |
procedure TKASColorBoxButton.SetStyle(AValue: TColorBoxStyle); |
| 345 |
begin |
| 346 |
FColorBox.Style:= AValue; |
| 347 |
end; |
| 348 |
|
| 349 |
procedure TKASColorBoxButton.SetOnChange(AValue: TNotifyEvent); |
| 350 |
begin |
| 351 |
FColorBox.OnChange:= AValue; |
| 352 |
end; |
| 353 |
|
| 354 |
procedure TKASColorBoxButton.SetColorDialog(AValue: TColorDialog); |
| 355 |
begin |
| 356 |
FColorBox.ColorDialog:= AValue; |
| 357 |
end; |
| 358 |
|
| 359 |
procedure TKASColorBoxButton.DoAutoSize; |
| 360 |
begin |
| 361 |
inherited DoAutoSize; |
| 362 |
FButton.Constraints.MinWidth:= FButton.Height; |
| 363 |
end; |
| 364 |
|
| 365 |
procedure TKASColorBoxButton.EnabledChanged; |
| 366 |
begin |
| 367 |
if Enabled then |
| 368 |
FColorBox.Font.Color:= clDefault |
| 369 |
else begin |
| 370 |
FColorBox.Font.Color:= clGrayText; |
| 371 |
end; |
| 372 |
inherited EnabledChanged; |
| 373 |
end; |
| 374 |
|
| 375 |
procedure TKASColorBoxButton.ButtonClick(Sender: TObject); |
| 376 |
Var |
| 377 |
FreeDialog: Boolean; |
| 378 |
begin |
| 379 |
if csDesigning in ComponentState then Exit; |
| 380 |
with FColorBox do |
| 381 |
begin |
| 382 |
FreeDialog:= (ColorDialog = nil); |
| 383 |
if FreeDialog then |
| 384 |
begin |
| 385 |
ColorDialog:= TColorDialog.Create(GetTopParent); |
| 386 |
end; |
| 387 |
try |
| 388 |
with ColorDialog do |
| 389 |
begin |
| 390 |
Color:= FColorBox.Selected; |
| 391 |
if Execute Then |
| 392 |
begin |
| 393 |
FColorBox.SetCustomColor(Color); |
| 394 |
Invalidate; |
| 395 |
end; |
| 396 |
end; |
| 397 |
finally |
| 398 |
if FreeDialog Then |
| 399 |
begin |
| 400 |
ColorDialog.Free; |
| 401 |
ColorDialog:= nil; |
| 402 |
end; |
| 403 |
end; |
| 404 |
end; |
| 405 |
end; |
| 406 |
|
| 407 |
class function TKASColorBoxButton.GetControlClassDefaultSize: TSize; |
| 408 |
begin |
| 409 |
Result:= TKASColorBox.GetControlClassDefaultSize; |
| 410 |
Result.cx += Result.cy; |
| 411 |
end; |
| 412 |
|
| 413 |
procedure TKASColorBoxButton.CMParentColorChanged(var Message: TLMessage); |
| 414 |
begin |
| 415 |
if inherited ParentColor then |
| 416 |
begin |
| 417 |
inherited SetColor(Parent.Color); |
| 418 |
inherited ParentColor:= True; |
| 419 |
end; |
| 420 |
end; |
| 421 |
|
| 422 |
constructor TKASColorBoxButton.Create(AOwner: TComponent); |
| 423 |
begin |
| 424 |
FButton:= TKASButton.Create(Self); |
| 425 |
FColorBox:= TKASColorBox.Create(Self); |
| 426 |
|
| 427 |
inherited Create(AOwner); |
| 428 |
|
| 429 |
ControlStyle:= ControlStyle + [csNoFocus]; |
| 430 |
BorderStyle:= bsNone; |
| 431 |
TabStop:= True; |
| 432 |
inherited TabStop:= False; |
| 433 |
|
| 434 |
with FColorBox do |
| 435 |
begin |
| 436 |
SetSubComponent(True); |
| 437 |
Align:= alClient; |
| 438 |
ParentColor:= False; |
| 439 |
ParentFont:= True; |
| 440 |
Parent:= Self; |
| 441 |
end; |
| 442 |
with FButton do |
| 443 |
begin |
| 444 |
Align:= alRight; |
| 445 |
Caption:= '..'; |
| 446 |
BorderSpacing.Left:= 2; |
| 447 |
OnClick:= @ButtonClick; |
| 448 |
Parent:= Self; |
| 449 |
end; |
| 450 |
|
| 451 |
AutoSize:= True; |
| 452 |
Color:= clWindow; |
| 453 |
inherited ParentColor:= True; |
| 454 |
end; |
| 455 |
|
| 456 |
procedure TKASColorBoxButton.SetFocus; |
| 457 |
begin |
| 458 |
FColorBox.SetFocus; |
| 459 |
end; |
| 460 |
|
| 461 |
function TKASColorBoxButton.Focused: Boolean; |
| 462 |
begin |
| 463 |
Result:= FColorBox.Focused; |
| 464 |
end; |
| 465 |
|
| 466 |
end. |