LCL-Gtk3: Convert ampersands to underscores and back in captions as needed. Hints from Anton Kavalenka, issue #41220.

(cherry picked from commit b35ba030b3)

Co-authored-by: Maxim Ganetsky <maxim@lazarus-ide.org>
This commit is contained in:
Maxim Ganetsky 2024-11-11 19:51:34 +00:00 committed by Maxim Ganetsky
parent 1b2d86714b
commit e95c28c9bc
2 changed files with 45 additions and 6 deletions

View File

@ -304,6 +304,7 @@ function Gtk3DefaultContext: TGtk3DeviceContext;
function Gtk3ScreenContext: TGtk3DeviceContext;
function ReplaceAmpersandsWithUnderscores(const S: string): string; inline;
function ReplaceUnderscoresWithAmpersands(const S: string): string; inline;
implementation
@ -2409,12 +2410,50 @@ begin
end;
//various routines for text , copied from gtk2.
//various routines for text
// LCL denotes accelerator keys by '&', while Gtk denotes them with '_'.
// LCL allows to show literal '&' by escaping (doubling) it.
// Gtk allows to show literal '_' by escaping (doubling) it.
// As the situation for LCL and Gtk is symmetric, conversion is made by generic function.
function TransformAmpersandsAndUnderscores(const S: string; const FromChar, ToChar: char): string;
var
i: Integer;
begin
Result := '';
i := 1;
while i <= Length(S) do
begin
if S[i] = ToChar then
Result := Result + ToChar + ToChar
else
if S[i] = FromChar then
if i < Length(S) then
begin
if S[i + 1] = FromChar then
begin
Result := Result + FromChar;
inc(i);
end
else
Result := Result + ToChar;
end
else
Result := Result + FromChar
else
Result := Result + S[i];
inc(i);
end;
end;
function ReplaceAmpersandsWithUnderscores(const S: string): string; inline;
begin
Result := StringReplace(S, '_', '__', [rfReplaceAll]);
Result := StringReplace(Result, '&', '_', [rfReplaceAll]);
Result := TransformAmpersandsAndUnderscores(S, '&', '_');
end;
function ReplaceUnderscoresWithAmpersands(const S: string): string; inline;
begin
Result := TransformAmpersandsAndUnderscores(S, '_', '&');
end;
{-------------------------------------------------------------------------------

View File

@ -3423,7 +3423,7 @@ begin
begin
if PGtkFrame(Widget)^.get_label_widget = nil then
exit;
Result := PGtkFrame(Widget)^.get_label;
Result := ReplaceUnderscoresWithAmpersands(PGtkFrame(Widget)^.get_label);
end;
end;
@ -3438,7 +3438,7 @@ begin
begin
if PGtkFrame(Widget)^.get_label_widget = nil then
PGtkFrame(Widget)^.set_label_widget(TGtkLabel.new(''));
PGtkFrame(Widget)^.set_label(PgChar(AValue));
PGtkFrame(Widget)^.set_label(PgChar(ReplaceAmpersandsWithUnderscores(AValue)));
end;
end;
end;
@ -7029,7 +7029,7 @@ end;
function TGtk3Button.getText: String;
begin
if IsWidgetOK then
Result := PGtkButton(FWidget)^.get_label
Result := ReplaceUnderscoresWithAmpersands(PGtkButton(FWidget)^.get_label())
else
Result := '';
end;