CEF4Delphi/source/uCEFStringList.pas

211 lines
4.6 KiB
ObjectPascal
Raw Normal View History

2018-03-29 20:02:04 +02:00
unit uCEFStringList;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
2018-03-29 20:02:04 +02:00
{$I cef.inc}
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2018-03-29 20:02:04 +02:00
interface
uses
{$IFDEF DELPHI16_UP}
System.Classes,
{$ELSE}
Classes,
{$ENDIF}
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
type
2023-07-30 18:47:35 +02:00
/// <summary>
/// CEF string maps are a set of key/value string pairs.
/// </summary>
2018-03-29 20:02:04 +02:00
TCefCustomStringList = class(TInterfacedObject, ICefStringList)
protected
FHandle : TCefStringList;
function GetHandle: TCefStringList; virtual;
2023-07-30 18:47:35 +02:00
/// <summary>
/// Return the number of elements in the string list.
/// </summary>
2018-03-29 20:02:04 +02:00
function GetSize: NativeUInt; virtual;
2023-07-30 18:47:35 +02:00
/// <summary>
/// Retrieve the value at the specified zero-based string list index. Returns
/// true (1) if the value was successfully retrieved.
/// </summary>
2018-03-29 20:02:04 +02:00
function GetValue(index: NativeUInt): ustring; virtual;
2023-07-30 18:47:35 +02:00
/// <summary>
/// Append a new value at the end of the string list.
/// </summary>
2018-03-29 20:02:04 +02:00
procedure Append(const value: ustring); virtual;
2023-07-30 18:47:35 +02:00
/// <summary>
/// Clear the string list.
/// </summary>
2018-03-29 20:02:04 +02:00
procedure Clear; virtual;
2023-07-30 18:47:35 +02:00
/// <summary>
/// Creates a copy of an existing string list.
/// </summary>
2018-03-29 20:02:04 +02:00
function Copy : TCefStringList; virtual;
procedure CopyToStrings(const aStrings : TStrings); virtual;
procedure AddStrings(const aStrings : TStrings); virtual;
public
constructor Create; virtual;
end;
TCefStringListOwn = class(TCefCustomStringList)
public
2023-07-30 18:47:35 +02:00
/// <summary>
/// Allocate a new string map.
/// </summary>
2018-03-29 20:02:04 +02:00
constructor Create; override;
2023-07-30 18:47:35 +02:00
/// <summary>
/// Free the string list.
/// </summary>
2018-03-29 20:02:04 +02:00
destructor Destroy; override;
end;
TCefStringListRef = class(TCefCustomStringList)
public
constructor Create(aHandle : TCefStringList); reintroduce;
end;
implementation
uses
uCEFMiscFunctions, uCEFLibFunctions;
// *****************************************
// ********* TCefCustomStringList **********
// *****************************************
constructor TCefCustomStringList.Create;
begin
inherited Create;
FHandle := nil;
end;
procedure TCefCustomStringList.Append(const value: ustring);
var
TempValue : TCefString;
begin
if (FHandle <> nil) then
begin
TempValue := CefString(value);
cef_string_list_append(FHandle, @TempValue);
end;
end;
procedure TCefCustomStringList.Clear;
begin
if (FHandle <> nil) then cef_string_list_clear(FHandle);
end;
function TCefCustomStringList.GetHandle: TCefStringList;
2018-03-29 20:02:04 +02:00
begin
Result := FHandle;
end;
function TCefCustomStringList.GetSize: NativeUInt;
begin
if (FHandle <> nil) then
Result := cef_string_list_size(FHandle)
else
Result := 0;
end;
function TCefCustomStringList.Copy : TCefStringList;
begin
if (FHandle <> nil) then
Result := cef_string_list_copy(FHandle)
else
Result := nil;
end;
function TCefCustomStringList.GetValue(index: NativeUInt): ustring;
var
TempValue : TCefString;
begin
Result := '';
if (FHandle <> nil) then
begin
CefStringInitialize(@TempValue);
2018-03-29 20:02:04 +02:00
if (cef_string_list_value(FHandle, index, @TempValue) <> 0) then
Result := CefStringClearAndGet(@TempValue);
2018-03-29 20:02:04 +02:00
end;
end;
procedure TCefCustomStringList.CopyToStrings(const aStrings : TStrings);
var
i, j : NativeUInt;
TempValue : TCefString;
2018-03-29 20:02:04 +02:00
begin
if (aStrings <> nil) and (FHandle <> nil) then
begin
i := 0;
j := GetSize;
while (i < j) do
begin
CefStringInitialize(@TempValue);
2018-03-29 20:02:04 +02:00
if (cef_string_list_value(FHandle, i, @TempValue) <> 0) then
aStrings.Add(CefStringClearAndGet(@TempValue));
2018-03-29 20:02:04 +02:00
inc(i);
end;
end;
end;
procedure TCefCustomStringList.AddStrings(const aStrings : TStrings);
var
i : integer;
begin
if (FHandle <> nil) and (aStrings <> nil) and (aStrings.Count > 0) then
for i := 0 to aStrings.Count - 1 do Append(aStrings[i]);
end;
// *****************************************
// *********** TCefStringListOwn ***********
// *****************************************
constructor TCefStringListOwn.Create;
begin
inherited Create;
FHandle := cef_string_list_alloc();
2018-03-29 20:02:04 +02:00
end;
destructor TCefStringListOwn.Destroy;
begin
if (FHandle <> nil) then cef_string_list_free(FHandle);
inherited Destroy;
end;
// *****************************************
// *********** TCefStringListRef ***********
// *****************************************
constructor TCefStringListRef.Create(aHandle : TCefStringList);
begin
inherited Create;
FHandle := aHandle;
end;
end.