1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2024-11-24 08:02:15 +02:00
CEF4Delphi/source/uCEFTextfieldComponent.pas

513 lines
16 KiB
ObjectPascal
Raw Normal View History

unit uCEFTextfieldComponent;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
{$I cef.inc}
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
interface
uses
{$IFDEF DELPHI16_UP}
{$IFDEF MSWINDOWS}WinApi.Windows,{$ENDIF} System.Classes,
{$ELSE}
{$IFDEF MSWINDOWS}Windows,{$ENDIF} Classes,
{$IFDEF FPC}
LCLProc, LCLType, LCLIntf, LResources, InterfaceBase,
{$ENDIF}
{$ENDIF}
2022-10-14 16:35:50 +02:00
uCEFTypes, uCEFInterfaces, uCEFConstants, uCEFViewsFrameworkEvents, uCEFViewComponent;
type
2022-10-14 16:35:50 +02:00
{$IFNDEF FPC}{$IFDEF DELPHI16_UP}[ComponentPlatformsAttribute(pfidWindows or pfidOSX or pfidLinux)]{$ENDIF}{$ENDIF}
TCEFTextfieldComponent = class(TCEFViewComponent, ICefTextfieldDelegateEvents)
protected
FTextfield : ICefTextfield;
// ICefTextfieldDelegateEvents
FOnTextfieldKeyEvent : TOnTextfieldKeyEventEvent;
FOnAfterUserAction : TOnAfterUserActionEvent;
procedure DestroyView; override;
procedure Initialize; override;
function GetInitialized : boolean; override;
function GetAsView : ICefView; override;
function GetAsTextfield : ICefTextfield; override;
function GetIsPasswordInput : boolean;
function GetIsReadOnly : boolean;
function GetText : ustring;
function GetHasSelection : boolean;
function GetSelectedText : ustring;
function GetSelectedRange : TCefRange;
function GetCursorPosition : NativeUInt;
function GetTextColor : TCefColor;
function GetSelectionTextColor : TCefColor;
function GetSelectionBackgroundColor : TCefColor;
function GetPlaceholderText : ustring;
procedure SetPasswordInput(password_input: boolean);
procedure SetReadOnly(read_only: boolean);
procedure SetText(const text_: ustring);
procedure SetTextColor(color: TCefColor);
procedure SetSelectionTextColor(color: TCefColor);
procedure SetSelectionBackgroundColor(color: TCefColor);
procedure SetPlaceholderText(const text_: ustring);
2023-09-29 19:23:38 +02:00
procedure SetSelectedRange(const range: TCefRange);
// ICefTextfieldDelegateEvents
procedure doOnKeyEvent(const textfield: ICefTextfield; const event: TCefKeyEvent; var aResult : boolean);
procedure doOnAfterUserAction(const textfield: ICefTextfield);
// ICefViewDelegateEvents
procedure doCreateCustomView; override;
public
2023-09-29 19:23:38 +02:00
/// <summary>
/// Create a new Textfield.
/// </summary>
procedure CreateTextField;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Appends |text| to the previously-existing text.
/// </summary>
procedure AppendText(const text_: ustring);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Inserts |text| at the current cursor position replacing any selected text.
/// </summary>
procedure InsertOrReplaceText(const text_: ustring);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Selects all text. If |reversed| is true (1) the range will end at the
/// logical beginning of the text; this generally shows the leading portion of
/// text that overflows its display area.
/// </summary>
procedure SelectAll(reversed: boolean);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Clears the text selection and sets the caret to the end.
/// </summary>
procedure ClearSelection;
2023-09-29 19:23:38 +02:00
/// <summary>
/// <para>Sets the font list. The format is "<FONT_FAMILY_LIST>,[STYLES] <SIZE>",
/// where:</para>
/// <code>
/// - FONT_FAMILY_LIST is a comma-separated list of font family names,
/// - STYLES is an optional space-separated list of style names (case-sensitive
/// "Bold" and "Italic" are supported), and
/// - SIZE is an integer font size in pixels with the suffix "px".
/// </code>
/// <para>Here are examples of valid font description strings:</para>
/// <code>
/// - "Arial, Helvetica, Bold Italic 14px"
/// - "Arial, 14px"
/// </code>
/// </summary>
procedure SetFontList(const font_list: ustring);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Applies |color| to the specified |range| without changing the default
/// color. If |range| is NULL the color will be set on the complete text
/// contents.
/// </summary>
procedure ApplyTextColor(color: TCefColor; const range: TCefRange);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Applies |style| to the specified |range| without changing the default
/// style. If |add| is true (1) the style will be added, otherwise the style
/// will be removed. If |range| is NULL the style will be set on the complete
/// text contents.
/// </summary>
procedure ApplyTextStyle(style: TCefTextStyle; add: boolean; const range: TCefRange);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns true (1) if the action associated with the specified command id is
/// enabled. See additional comments on execute_command().
/// </summary>
2020-07-18 19:12:01 +02:00
function IsCommandEnabled(command_id: TCefTextFieldCommands): boolean;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Performs the action associated with the specified command id.
/// </summary>
2020-07-18 19:12:01 +02:00
procedure ExecuteCommand(command_id: TCefTextFieldCommands);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Clears Edit history.
/// </summary>
procedure ClearEditHistory;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Set the accessible name that will be exposed to assistive technology (AT).
/// </summary>
procedure SetAccessibleName(const name_: ustring);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Sets the placeholder text color.
/// </summary>
procedure SetPlaceholderTextColor(color: TCefColor);
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns true (1) if the text will be displayed as asterisks.
/// </summary>
property PasswordInput : boolean read GetIsPasswordInput write SetPasswordInput;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns true (1) if the text is read-only.
/// </summary>
property ReadOnly : boolean read GetIsReadOnly write SetReadOnly;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns the currently displayed text.
/// </summary>
property Text : ustring read GetText write SetText;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns the currently selected text.
/// </summary>
property SelectedText : ustring read GetSelectedText;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns the selected logical text range.
/// </summary>
property SelectedRange : TCefRange read GetSelectedRange write SetSelectedRange;
/// <summary>
/// Returns the current cursor position.
/// </summary>
property CursorPosition : NativeUInt read GetCursorPosition;
/// <summary>
/// Returns the text color.
/// </summary>
property TextColor : TCefColor read GetTextColor write SetTextColor;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns the selection text color.
/// </summary>
property SelectionTextColor : TCefColor read GetSelectionTextColor write SetSelectionTextColor;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns the selection background color.
/// </summary>
property SelectionBackgroundColor : TCefColor read GetSelectionBackgroundColor write SetSelectionBackgroundColor;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns the placeholder text that will be displayed when the Textfield is
/// NULL.
/// </summary>
property PlaceholderText : ustring read GetPlaceholderText write SetPlaceholderText;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Returns true (1) if there is any selected text.
/// </summary>
property HasSelection : boolean read GetHasSelection;
published
2023-09-29 19:23:38 +02:00
/// <summary>
/// Called when |textfield| recieves a keyboard event. |event| contains
/// information about the keyboard event. Return true (1) if the keyboard
/// event was handled or false (0) otherwise for default handling.
/// </summary>
property OnTextfieldKeyEvent : TOnTextfieldKeyEventEvent read FOnTextfieldKeyEvent write FOnTextfieldKeyEvent;
2023-09-29 19:23:38 +02:00
/// <summary>
/// Called after performing a user action that may change |textfield|.
/// </summary>
property OnAfterUserAction : TOnAfterUserActionEvent read FOnAfterUserAction write FOnAfterUserAction;
end;
{$IFDEF FPC}
procedure Register;
{$ENDIF}
// *********************************************************
// ********************** ATTENTION ! **********************
// *********************************************************
// ** **
// ** MANY OF THE EVENTS IN CEF4DELPHI COMPONENTS LIKE **
// ** TCHROMIUM, TFMXCHROMIUM OR TCEFAPPLICATION ARE **
// ** EXECUTED IN A CEF THREAD BY DEFAULT. **
// ** **
// ** WINDOWS CONTROLS MUST BE CREATED AND DESTROYED IN **
// ** THE SAME THREAD TO AVOID ERRORS. **
// ** SOME OF THEM RECREATE THE HANDLERS IF THEY ARE **
// ** MODIFIED AND CAN CAUSE THE SAME ERRORS. **
// ** **
// ** DON'T CREATE, MODIFY OR DESTROY WINDOWS CONTROLS **
// ** INSIDE THE CEF4DELPHI EVENTS AND USE **
// ** SYNCHRONIZATION OBJECTS TO PROTECT VARIABLES AND **
// ** FIELDS IF THEY ARE ALSO USED IN THE MAIN THREAD. **
// ** **
// ** READ THIS FOR MORE INFORMATION : **
// ** https://www.briskbard.com/index.php?pageid=cef **
// ** **
// ** USE OUR FORUMS FOR MORE QUESTIONS : **
// ** https://www.briskbard.com/forum/ **
// ** **
// *********************************************************
// *********************************************************
implementation
uses
uCEFTextfieldDelegate, uCEFMiscFunctions, uCEFTask, uCEFTextfield;
procedure TCEFTextfieldComponent.Initialize;
begin
inherited Initialize;
FTextfield := nil;
FOnTextfieldKeyEvent := nil;
FOnAfterUserAction := nil;
end;
procedure TCEFTextfieldComponent.DestroyView;
begin
FTextfield := nil;
end;
procedure TCEFTextfieldComponent.CreateTextField;
begin
CreateView;
end;
procedure TCEFTextfieldComponent.doOnKeyEvent(const textfield : ICefTextfield;
const event : TCefKeyEvent;
var aResult : boolean);
begin
if assigned(FOnTextfieldKeyEvent) then
FOnTextfieldKeyEvent(self, textfield, event, aResult);
end;
procedure TCEFTextfieldComponent.doOnAfterUserAction(const textfield: ICefTextfield);
begin
if assigned(FOnAfterUserAction) then
FOnAfterUserAction(self, textfield);
end;
procedure TCEFTextfieldComponent.doCreateCustomView;
var
TempDelegate : ICefTextfieldDelegate;
begin
if (FTextfield = nil) then
try
TempDelegate := TCustomTextfieldDelegate.Create(self);
FTextfield := TCefTextfieldRef.CreateTextField(TempDelegate);
finally
TempDelegate := nil;
end;
end;
function TCEFTextfieldComponent.GetInitialized : boolean;
begin
Result := (FTextfield <> nil);
end;
function TCEFTextfieldComponent.GetAsView : ICefView;
begin
Result := FTextfield as ICefView;
end;
function TCEFTextfieldComponent.GetAsTextfield : ICefTextfield;
begin
Result := FTextfield;
end;
function TCEFTextfieldComponent.GetIsPasswordInput : boolean;
begin
Result := Initialized and FTextfield.IsPasswordInput;
end;
function TCEFTextfieldComponent.GetIsReadOnly : boolean;
begin
Result := Initialized and FTextfield.IsReadOnly;
end;
function TCEFTextfieldComponent.GetText : ustring;
begin
if Initialized then
Result := FTextfield.GetText
else
Result := '';
end;
function TCEFTextfieldComponent.GetHasSelection : boolean;
begin
Result := Initialized and FTextfield.HasSelection;
end;
function TCEFTextfieldComponent.GetSelectedText : ustring;
begin
if Initialized then
Result := FTextfield.SelectedText
else
Result := '';
end;
function TCEFTextfieldComponent.GetSelectedRange : TCefRange;
var
TempRange : TCefRange;
begin
if Initialized then
TempRange := FTextfield.GetSelectedRange
else
begin
TempRange.from := 0;
TempRange.to_ := 0;
end;
Result := TempRange;
end;
function TCEFTextfieldComponent.GetCursorPosition : NativeUInt;
begin
if Initialized then
Result := FTextfield.GetCursorPosition
else
Result := 0;
end;
function TCEFTextfieldComponent.GetTextColor : TCefColor;
begin
if Initialized then
Result := FTextfield.GetTextColor
else
Result := 0;
end;
function TCEFTextfieldComponent.GetSelectionTextColor : TCefColor;
begin
if Initialized then
Result := FTextfield.GetSelectionTextColor
else
Result := 0;
end;
function TCEFTextfieldComponent.GetSelectionBackgroundColor : TCefColor;
begin
if Initialized then
Result := FTextfield.GetSelectionBackgroundColor
else
Result := 0;
end;
function TCEFTextfieldComponent.GetPlaceholderText : ustring;
begin
if Initialized then
Result := FTextfield.GetPlaceholderText
else
Result := '';
end;
procedure TCEFTextfieldComponent.SetPasswordInput(password_input: boolean);
begin
if Initialized then
FTextfield.SetPasswordInput(password_input);
end;
procedure TCEFTextfieldComponent.SetReadOnly(read_only: boolean);
begin
if Initialized then
FTextfield.SetReadOnly(read_only);
end;
procedure TCEFTextfieldComponent.SetText(const text_: ustring);
begin
if Initialized then
FTextfield.SetText(text_);
end;
procedure TCEFTextfieldComponent.SetTextColor(color: TCefColor);
begin
if Initialized then
FTextfield.SetTextColor(color);
end;
procedure TCEFTextfieldComponent.SetSelectionTextColor(color: TCefColor);
begin
if Initialized then
FTextfield.SetSelectionTextColor(color);
end;
procedure TCEFTextfieldComponent.SetSelectionBackgroundColor(color: TCefColor);
begin
if Initialized then
FTextfield.SetSelectionBackgroundColor(color);
end;
procedure TCEFTextfieldComponent.SetPlaceholderText(const text_: ustring);
begin
if Initialized then
FTextfield.SetPlaceholderText(text_);
end;
procedure TCEFTextfieldComponent.AppendText(const text_: ustring);
begin
if Initialized then
FTextfield.AppendText(text_);
end;
procedure TCEFTextfieldComponent.InsertOrReplaceText(const text_: ustring);
begin
if Initialized then
FTextfield.InsertOrReplaceText(text_);
end;
procedure TCEFTextfieldComponent.SelectAll(reversed: boolean);
begin
if Initialized then
FTextfield.SelectAll(reversed);
end;
procedure TCEFTextfieldComponent.ClearSelection;
begin
if Initialized then
FTextfield.ClearSelection;
end;
2023-09-29 19:23:38 +02:00
procedure TCEFTextfieldComponent.SetSelectedRange(const range: TCefRange);
begin
if Initialized then
FTextfield.SelectRange(range);
end;
procedure TCEFTextfieldComponent.SetFontList(const font_list: ustring);
begin
if Initialized then
FTextfield.SetFontList(font_list);
end;
procedure TCEFTextfieldComponent.ApplyTextColor(color: TCefColor; const range: TCefRange);
begin
if Initialized then
FTextfield.ApplyTextColor(color, range);
end;
procedure TCEFTextfieldComponent.ApplyTextStyle(style: TCefTextStyle; add: boolean; const range: TCefRange);
begin
if Initialized then
FTextfield.ApplyTextStyle(style, add, range);
end;
2020-07-18 19:12:01 +02:00
function TCEFTextfieldComponent.IsCommandEnabled(command_id: TCefTextFieldCommands): boolean;
begin
Result := Initialized and FTextfield.IsCommandEnabled(command_id);
end;
2020-07-18 19:12:01 +02:00
procedure TCEFTextfieldComponent.ExecuteCommand(command_id: TCefTextFieldCommands);
begin
if Initialized then
FTextfield.ExecuteCommand(command_id);
end;
procedure TCEFTextfieldComponent.ClearEditHistory;
begin
if Initialized then
FTextfield.ClearEditHistory;
end;
procedure TCEFTextfieldComponent.SetAccessibleName(const name_: ustring);
begin
if Initialized then
FTextfield.SetAccessibleName(name_);
end;
procedure TCEFTextfieldComponent.SetPlaceholderTextColor(color: TCefColor);
begin
if Initialized then
FTextfield.SetPlaceholderTextColor(color);
end;
{$IFDEF FPC}
procedure Register;
begin
{$I res/tceftextfieldcomponent.lrs}
RegisterComponents('Chromium Views Framework', [TCEFTextfieldComponent]);
end;
{$ENDIF}
end.