Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/Eto.Gtk/Forms/Controls/GroupBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Eto.GtkSharp.Forms.Controls
{
public class GroupBoxHandler : GtkPanel<Gtk.Frame, GroupBox, GroupBox.ICallback>, GroupBox.IHandler
{
static readonly object TextColor_Key = new object();

public GroupBoxHandler ()
{
Control = new EtoFrame { Handler = this };
Expand All @@ -13,10 +15,15 @@ public override string Text {
get { return Control.Label; }
set
{
var needsFont = Control.LabelWidget == null && Widget.Properties.ContainsKey(GtkControl.Font_Key);
// GTK replaces the label when the title changes, so reapply assigned styles.
Control.Label = value;
if (needsFont)
Control.LabelWidget?.SetFont(Font.ToPango());
if (Control.LabelWidget is Gtk.Widget label)
{
if (Widget.Properties.ContainsKey(GtkControl.Font_Key))
label.SetFont(Font.ToPango());
if (Widget.Properties.Get<Color?>(TextColor_Key) is Color color)
label.SetForeground(color);
}
}
}

Expand All @@ -27,15 +34,15 @@ public override Size ClientSize {
else {
var label = Control.LabelWidget;
var size = Size;
size.Height -= label.Allocation.Height + 10;
size.Height -= (label?.Allocation.Height ?? 0) + 10;
size.Width -= 10;
return size;
}
}
set {
var label = Control.LabelWidget;
var size = value;
size.Height += label.Allocation.Height + 10;
size.Height += (label?.Allocation.Height ?? 0) + 10;
size.Width += 10;
Size = size;
}
Expand All @@ -54,8 +61,17 @@ protected override void SetContainerContent(Gtk.Widget content)

public Color TextColor
{
get { return Control.LabelWidget.GetForeground(); }
set { Control.LabelWidget.SetForeground(value); }
get
{
if (Control.LabelWidget is Gtk.Widget label)
return label.GetForeground();
return Widget.Properties.Get<Color?>(TextColor_Key) ?? Control.GetForeground();
}
set
{
Widget.Properties.Set<Color?>(TextColor_Key, value);
Control.LabelWidget?.SetForeground(value);
}
}
}
}
49 changes: 49 additions & 0 deletions test/Eto.Test.Gtk/UnitTests/GroupBoxTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#if GTK3
using Eto.GtkSharp;
using Eto.Test.UnitTests;
using NUnit.Framework;
using g = Gtk;

namespace Eto.Test.Gtk.UnitTests
{
[TestFixture]
public class GroupBoxTests : TestBase
{
[Test]
public void TitleChangesShouldPreserveAssignedStyles()
{
Invoke(() =>
{
using var font = new Font(FontFamilies.Sans, 17, FontStyle.Bold);
using var groupBox = new GroupBox { Font = font, TextColor = Colors.Red };
foreach (var title in new[] { "Title", "Renamed" })
{
groupBox.Text = title;
AssertLabelStyle(groupBox, font, Colors.Red);
}

groupBox.Text = null;
Assert.That(((g.Frame)groupBox.ControlObject).LabelWidget, Is.Null);
groupBox.TextColor = default(Color);
groupBox.Text = "Restored";
AssertLabelStyle(groupBox, font, default(Color));
});
}

static void AssertLabelStyle(GroupBox groupBox, Font font, Color color)
{
var label = ((g.Frame)groupBox.ControlObject).LabelWidget;
Assert.That(label, Is.Not.Null);
var actualFont = label.GetFont();
var expectedFont = font.ToPango();
Assert.Multiple(() =>
{
Assert.That(actualFont.Size, Is.EqualTo(expectedFont.Size));
Assert.That(actualFont.Weight, Is.EqualTo(expectedFont.Weight));
Assert.That(actualFont.Style, Is.EqualTo(expectedFont.Style));
Assert.That(label.GetForeground().ToArgb(), Is.EqualTo(color.ToArgb()));
});
}
}
}
#endif
Loading