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

This commit is contained in:
Maxim Ganetsky 2024-11-11 22:46:51 +03:00
parent c97c2774da
commit b35ba030b3
2 changed files with 45 additions and 6 deletions

View File

@ -304,6 +304,7 @@ function Gtk3DefaultContext: TGtk3DeviceContext;
function Gtk3ScreenContext: TGtk3DeviceContext; function Gtk3ScreenContext: TGtk3DeviceContext;
function ReplaceAmpersandsWithUnderscores(const S: string): string; inline; function ReplaceAmpersandsWithUnderscores(const S: string): string; inline;
function ReplaceUnderscoresWithAmpersands(const S: string): string; inline;
implementation implementation
@ -2409,12 +2410,50 @@ begin
end; 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; function ReplaceAmpersandsWithUnderscores(const S: string): string; inline;
begin begin
Result := StringReplace(S, '_', '__', [rfReplaceAll]); Result := TransformAmpersandsAndUnderscores(S, '&', '_');
Result := StringReplace(Result, '&', '_', [rfReplaceAll]); end;
function ReplaceUnderscoresWithAmpersands(const S: string): string; inline;
begin
Result := TransformAmpersandsAndUnderscores(S, '_', '&');
end; end;
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------

View File

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