📄 jvqcolorcombo.pas
字号:
{-----------------------------------------------------------------------------
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/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvColorCombo.PAS, released on 2002-05-26.
The Initial Developer of the Original Code is Peter Th鰎nqvist [peter3@peter3.com]
Portions created by Peter Th鰎nqvist are Copyright (C) 2002 Peter Th鰎nqvist.
All Rights Reserved.
Contributor(s):
Brian Cook (borland.public.vcl.components.writing)
Last Modified: 2002-11-20
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
VisualCLX only. Ported from JVCL 2.1 !!!
If you set AutoComplete in TJvColorComboBox to True and use the same text for
all Custom colors, the inherited Change behaviour from TJvComboBox makes the *first*
custom color selected, not the last added as it should be thus AutoComplete is
set to default to False. (p3)
-----------------------------------------------------------------------------}
// $Id: JvQColorCombo.pas,v 1.10 2005/02/06 14:06:02 asnepvangers Exp $
{$I jvcl.inc}
unit JvQColorCombo;
{ Comboboxes for displaying colors and fonts }
interface
uses
SysUtils, Classes,
{$IFDEF MSWINDOWS}
Windows, // FontSubstitute
{$ENDIF MSWINDOWS}
QForms, QControls, QDialogs, QGraphics, QStdCtrls, Qt, QTypes,
QWindows, JvQCombobox, JvQConsts;
type
TJvNewColorEvent = procedure(Sender: TObject; Color: TColor; var DisplayName: string; var AllowAdd: Boolean) of
object;
TJvGetColorNameEvent = procedure(Sender: TObject; Index: Integer; Color: TColor; var DisplayName: string) of object;
TJvColorComboOption = (coText, coHex, coRGB, coSysColors, coCustomColors);
TJvColorComboOptions = set of TJvColorComboOption;
TJvColorComboBox = class(TJvCustomComboBox)
private
FColorValue: TColor;
FCustCnt: Integer;
FHiLiteColor: TColor;
FHiLiteText: TColor;
FOptions: TJvColorComboOptions;
FPrefix: string;
FHexPrefix: string;
FOther: string;
FColWidth: Integer;
FExecutingDialog: Boolean;
FNewColor: TJvNewColorEvent;
FOnGetDisplayName: TJvGetColorNameEvent;
FColorNameMap: TStrings;
FOnInsertColor: TJvNewColorEvent;
FOnBeforeCustom: TNotifyEvent;
procedure SetOptions(Value: TJvColorComboOptions);
procedure SetOther(Value: string);
procedure SetColWidth(Value: Integer);
procedure SetColorValue(Value: TColor);
procedure ResetItemHeight;
procedure SetPrefix(const Value: string);
procedure SetHexPrefix(const Value: string);
procedure SetColorNameMap(const Value: TStrings);
procedure InitColorNames;
function GetColor(Index: Integer): TColor;
protected
procedure FontChanged; override ;
procedure ItemDraw(Sender: TObject;Index: Integer; R: TRect;
State: TOwnerDrawState; var Handled: Boolean ) ;
procedure Click; override;
procedure Loaded; override;
function GetColorName(AColor: TColor; const Default: string): string;
function DoNewColor(Color: TColor; var DisplayName: string): Boolean; virtual;
procedure DoGetDisplayName(Index: Integer; AColor: TColor; var DisplayName: string); virtual;
function DoInsertColor(AIndex: Integer; AColor: TColor; var DisplayName: string): Boolean; virtual;
procedure DoBeforeCustom;
procedure InternalInsertColor(AIndex: Integer; AColor: TColor; const DisplayName: string); virtual;
procedure DoNameMapChange(Sender:TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure GetColors; virtual;
procedure GetCustomColors(AList: TList);
// Returns the current name for AColor. Note that this implicitly might call the
// OnGetDisplayName event if the protected GetColorName returns an emtpy string
function ColorName(AColor: TColor): string;
// returns the index of a specific color or -1 if not found
function FindColor(AColor: TColor): Integer;
procedure AddColor(AColor: TColor; const DisplayName: string);
procedure ChangeColor(AIndex: Integer; AColor: TColor; const DisplayName: string);
procedure InsertColor(AIndex: Integer; AColor: TColor; const DisplayName: string);
property Text;
property CustomColorCount: Integer read FCustCnt;
property Colors[Index: Integer]: TColor read GetColor;
published
property Anchors;
property AutoComplete default False;
// property AutoSave;
property Constraints;
// color name map is a Tstrings property that can contain name/value mappings on the form
// colorName=DisplayName
// if the component finds a matching mapping, it will substitute the default value
// with the value in the list, otherwise the default value wil be used
// Example:
// clBlack=Black
property ColorNameMap: TStrings read FColorNameMap write SetColorNameMap;
property ColorValue: TColor read FColorValue write SetColorValue default clBlack;
property ColorDialogText: string read FOther write SetOther;
property ColorWidth: Integer read FColWidth write SetColWidth default 21;
property HexPrefix: string read FHexPrefix write SetHexPrefix;
property NewColorText: string read FPrefix write SetPrefix;
property Options: TJvColorComboOptions read FOptions write SetOptions;
property HiliteColor: TColor read FHiliteColor write FHiLiteColor default clHighLight;
property HiliteText: TColor read FHiliteText write FHiLiteText default clHighLightText;
// called before a new color is inserted as a result of displaying the Custom Colors dialog
property OnNewColor: TJvNewColorEvent read FNewColor write FNewColor;
// called before any color is inserted
property OnInsertColor: TJvNewColorEvent read FOnInsertColor write FOnInsertColor;
// called whenever the displayname of an item is needed
property OnGetDisplayName: TJvGetColorNameEvent read FOnGetDisplayName write FOnGetDisplayName;
// called just before the '(Other)' item is added at the bottom of the list
property OnBeforeCustom: TNotifyEvent read FOnBeforeCustom write FOnBeforeCustom;
property Color;
// property DropDownWidth;
property DragMode;
property Enabled;
property Font;
property InsertMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Sorted;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDropDown;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnStartDrag;
end;
TJvFontComboOption = (foTrueTypeOnly, foFixedPitchOnly,
foScalableOnly, foWysiWyg, foDisableVerify, foPreviewFont, foMRU);
// foDisableVerify: if True, allows you to insert a font name that doesn't exist (by assigning to FontName)
TJvFontComboOptions = set of TJvFontComboOption;
TJvDrawPreviewEvent = procedure(Sender: TObject; const AFontName: string;
var APreviewText: string; ATextWidth: Integer; var DrawPreview: Boolean) of object;
TJvFontComboBox = class(TJvCustomComboBox)
private
FTrueTypeBmp: TBitmap;
FFixBmp: TBitmap;
FHiliteColor: TColor;
FHiliteText: TColor;
FUseImages: Boolean;
FOptions: TJvFontComboOptions;
FMRUCount: Integer;
FWasMouse: Boolean;
FShowMRU: Boolean;
FMaxMRUCount: Integer;
FOnDrawPreviewEvent: TJvDrawPreviewEvent;
procedure SetUseImages(Value: Boolean);
procedure SetOptions(Value: TJvFontComboOptions);
procedure ResetItemHeight;
procedure Reset;
function GetFontName: string;
procedure SetFontName(const Value: string);
function GetSorted: Boolean;
procedure SetSorted(const Value: Boolean);
procedure SetShowMRU(const Value: Boolean);
procedure SetMaxMRUCount(const Value: Integer);
protected
procedure FontChanged; override;
procedure Loaded; override;
procedure GetFonts; virtual;
function DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState): Boolean; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X: Integer; Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer;
Y: Integer); override;
procedure CloseUp; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure SetParent(const AParent: TWinControl); override;
function DoDrawPreview(const AFontName: string; var APreviewText: string;
ATextWidth: Integer): Boolean;virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function AddToMRU: Integer;
procedure ClearMRU;
procedure Click; override;
function FontSubstitute(const AFontName: string): string;
property Text;
property MRUCount: Integer read FMRUCount;
published
property Anchors;
property AutoComplete default False;
property Constraints;
property Color;
property MaxMRUCount: Integer read FMaxMRUCount write SetMaxMRUCount;
property FontName: string read GetFontName write SetFontName;
property DragMode;
property Enabled;
property Font;
property ItemIndex;
property HiliteColor: TColor read FHiliteColor write FHiliteColor default clHighlight;
property HiliteText: TColor read FHiliteText write FHiliteText default clHighlightText;
property Options: TJvFontComboOptions read FOptions write SetOptions default [];
property UseImages: Boolean read FUseImages write SetUseImages default True;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Sorted: Boolean read GetSorted write SetSorted;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDropDown;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnStartDrag;
property OnDrawPreviewEvent: TJvDrawPreviewEvent read FOnDrawPreviewEvent write FOnDrawPreviewEvent;
end;
resourcestring
SOtherCaption = 'Custom...';
SNewColorPrefix = 'Custom';
implementation
{$IFDEF MSWINDOWS}
{$R ..\Resources\JvColorCombo.res}
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
{$R ../Resources/JvColorCombo.res}
{$ENDIF UNIX}
const
ColCount = 20;
ColorValues: array [1..ColCount] of TColor = (
clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray,
clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite,
clMoneyGreen, clSkyBlue, clCream, clMedGray);
SysColCount = 58;
SysColorValues: array [1..SysColCount] of TColor = (
clForeground, clButton, clLight, clMidlight, clDark, clMid,
clText, clBrightText, clButtonText, clBase, clBackground,
clShadow, clHighlight, clHighlightedText,
clInfoText, clInfoBk,
clNormalForeground, clNormalButton, clNormalLight, clNormalMidlight, clNormalDark, clNormalMid,
clNormalText, clNormalBrightText, clNormalButtonText, clNormalBase, clNormalBackground,
clNormalShadow, clNormalHighlight, clNormalHighlightedText,
clDisabledForeground, clDisabledButton, clDisabledLight, clDisabledMidlight, clDisabledDark, clDisabledMid,
clDisabledText, clDisabledBrightText, clDisabledButtonText, clDisabledBase, clDisabledBackground,
clDisabledShadow, clDisabledHighlight, clDisabledHighlightedText,
clActiveForeground, clActiveButton, clActiveLight, clActiveMidlight, clActiveDark, clActiveMid,
clActiveText, clActiveBrightText, clActiveButtonText, clActiveBase, clActiveBackground,
clActiveShadow, clActiveHighlight, clActiveHighlightedText);
function LoadInternalBitmap(ResName: string): TBitmap;
begin
Result := TBitmap.Create;
Result.LoadFromResourceName(hInstance, ResName);
Result.Transparent := True;
end;
function GetItemHeight(Font: TFont): Integer;
var
DC: HDC;
aFont: HFont;
TM: TTextMetric;
begin
DC := GetDC(0);
try
aFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, TM);
SelectObject(DC, aFont);
finally
ReleaseDC(0, DC);
end;
Result := TM.tmHeight {$IFDEF MSWINDOWS} + 2 {$ENDIF MSWINDOWS};
end;
function Max(Val1, Val2: Integer): Integer;
begin
Result := Val1;
if Val2 > Val1 then
Result := Val2;
end;
const
{ EnumFonts Masks }
{$EXTERNALSYM RASTER_FONTTYPE}
RASTER_FONTTYPE = 1;
// {$EXTERNALSYM DEVICE_FONTTYPE}
// DEVICE_FONTTYPE = 2;
{$EXTERNALSYM TRUETYPE_FONTTYPE}
TRUETYPE_FONTTYPE = 4;
function GetFontType(FontFamily: widestring): integer;
var
FontDatabase: QFontDatabaseH;
begin
FontDatabase := QFontDatabase_create;
try
if QFontDatabase_isBitmapScalable(FontDatabase, @FontFamily, nil, nil) then
Result := RASTER_FONTTYPE
else
Result := TRUETYPE_FONTTYPE;
finally
QFontDatabase_destroy(FontDatabase);
end;
end;
function GetPitch(FontFamily: string): integer;
var
fi: QFontInfoH;
f: TFont;
begin
f := TFont.Create;
f.Name := FontFamily;
try
fi := QFontInfo_create(f.Handle);
try
if QFontInfo_fixedPitch(fi) then
Result := FIXED_PITCH
else
Result := VARIABLE_PITCH;
finally
QFontInfo_destroy(fi);
end;
finally
f.Free;
end;
end;
function GetCharSets(FontFamily: WideString): TStringList;
var
FontDatabase: QFontDatabaseH;
CharSets: QStringListH;
begin
Charsets := QStringList_create;
try
FontDatabase := QFontDatabase_create;
try
QFontDatabase_charSets(FontDatabase, CharSets, @FontFamily, true);
Result := QStringListToTStringList(CharSets);
finally
QFontDatabase_destroy(FontDatabase);
end;
finally
QStringList_destroy(Charsets);
end;
end;
function IncludeFont(Options: TJvFontComboOptions; FontFamily: string; FontType: integer): boolean;
begin
Result := True;
if foTrueTypeOnly in Options then
Result := Result and (FontType and TRUETYPE_FONTTYPE > 0);
if foFixedPitchOnly in Options then
Result := Result and (GetPitch(FontFamily) and FIXED_PITCH > 0);
if foScalableOnly in Options then
Result := Result and (FontType and RASTER_FONTTYPE = 0);
end;
function CreateFontList: TStringList;
var
FontFamilies: QStringListH;
FontDatabase: QFontDatabaseH;
begin
FontFamilies := QStringList_create();
try
FontDatabase := QFontDatabase_create();
try
QFontDatabase_families(FontDatabase, FontFamilies, True);
Result := QStringListToTStringList(FontFamilies);
finally
QFontDatabase_destroy(FontDatabase);
end;
finally
QStringList_destroy(FontFamilies);
end;
end;
// === TJvColorComboBox ======================================================
constructor TJvColorComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColorNameMap := TStringlist.Create;
Style := csOwnerDrawFixed;
FColorValue := clBlack;
FColWidth := 21;
FPrefix := SNewColorPrefix;
FHexPrefix := '$';
FOther := SOtherCaption;
FOptions := [coText,coSysColors];
Duplicates := dupAccept;
FHiLiteColor := clHighLight;
FHiLiteText := clHighLightText;
AutoComplete := False;
// make sure that if this is the first time the component is dropped on the form,
// the default Name/Value map is created (thanks to Brian Cook on the borland NG's):
if (Owner <> nil) and ([csDesigning, csLoading] * Owner.ComponentState = [csDesigning]) then
InitColorNames;
TStringlist(FColorNameMap).OnChange := DoNameMapChange;
OnDrawItem := ItemDraw;
end;
destructor TJvColorComboBox.Destroy;
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -