Skip to content

Commit

Permalink
Add Window.AutoSize property
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Apr 20, 2021
1 parent 569efee commit b897c3c
Show file tree
Hide file tree
Showing 28 changed files with 599 additions and 295 deletions.
1 change: 1 addition & 0 deletions src/Eto.Gtk/Forms/Controls/LabelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public override string Text
{
Control.ResetWidth();
Control.TextWithMnemonic = value.ToPlatformMnemonic();
InvalidateMeasure();
}
}

Expand Down
83 changes: 24 additions & 59 deletions src/Eto.Gtk/Forms/DialogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,15 @@

namespace Eto.GtkSharp.Forms
{
public class EtoDialog : Gtk.Dialog
{
public EtoDialog()
: base("", null, Gtk.DialogFlags.DestroyWithParent)
{
}

public IGtkWindow Handler { get; set; }

#if GTK3
protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var size = Handler.UserPreferredSize;
if (size.Height > 0)
natural_height = size.Height;
}

protected override void OnGetPreferredWidthForHeight(int height, out int minimum_width, out int natural_width)
{

base.OnGetPreferredWidthForHeight(height, out minimum_width, out natural_width);
var size = Handler.UserPreferredSize;
if (size.Width > 0)
natural_width = size.Width;
}

protected override void OnGetPreferredWidth(out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidth(out minimum_width, out natural_width);
var size = Handler.UserPreferredSize;
if (size.Width > 0)
natural_width = size.Width;
}

protected override void OnGetPreferredHeight(out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeight(out minimum_height, out natural_height);
var size = Handler.UserPreferredSize;
if (size.Height > 0)
natural_height = size.Height;
}
#endif
}

public class DialogHandler : GtkWindow<Gtk.Dialog, Dialog, Dialog.ICallback>, Dialog.IHandler
{
Gtk.Container btcontainer;
Gtk.Box actionarea;
Button defaultButton;

public DialogHandler()
{
Control = new EtoDialog { Handler = this };
Control = new Gtk.Dialog();

Resizable = false;
}
Expand All @@ -66,17 +22,22 @@ protected override void Initialize()
base.Initialize();
Control.KeyPressEvent += Connector.Control_KeyPressEvent;

#if GTK2
Control.VBox.PackStart(WindowActionControl, false, true, 0);
Control.VBox.PackStart(WindowContentControl, true, true, 0);
var vbox = new EtoVBox { Handler = this };
vbox.PackStart(WindowActionControl, false, true, 0);
vbox.PackStart(WindowContentControl, true, true, 0);

#pragma warning disable 612
actionarea = Control.ActionArea;
#pragma warning restore 612

#if GTK2
var content = Control.VBox;
btcontainer = Control.ActionArea;
#else
Control.ContentArea.PackStart(WindowActionControl, false, true, 0);
Control.ContentArea.PackStart(WindowContentControl, true, true, 0);
var content = Control.ContentArea;

Control.ActionArea.NoShowAll = true;
Control.ActionArea.Hide();
actionarea.NoShowAll = true;
actionarea.Hide();

#if GTKCORE
if (Helper.UseHeaderBar)
Expand All @@ -89,8 +50,10 @@ protected override void Initialize()
}
else
#endif
btcontainer = Control.ActionArea;
btcontainer = actionarea;
#endif

content.PackStart(vbox, true, true, 0);
}

public Button AbortButton { get; set; }
Expand Down Expand Up @@ -132,10 +95,12 @@ public Button DefaultButton

public void ShowModal()
{
DisableAutoSizeUpdate++;
ReloadButtons();

Control.Modal = true;
Control.ShowAll();
DisableAutoSizeUpdate--;

do
{
Expand All @@ -153,7 +118,7 @@ public void CleanupButtons()
{
var children = btcontainer.Children;
foreach (var child in children)
Control.ActionArea.Remove(child);
btcontainer.Remove(child);
}

public void ReloadButtons()
Expand All @@ -165,13 +130,13 @@ public void ReloadButtons()
{
if (!Helper.UseHeaderBar)
{
Control.ActionArea.NoShowAll = false;
actionarea.NoShowAll = false;

for (int i = negativeButtons.Count - 1; i >= 0; i--)
Control.ActionArea.PackStart(negativeButtons[i].ToNative(), false, true, 1);
actionarea.PackStart(negativeButtons[i].ToNative(), false, true, 1);

foreach (var button in positiveButtons)
Control.ActionArea.PackStart(button.ToNative(), false, true, 1);
actionarea.PackStart(button.ToNative(), false, true, 1);
}
#if GTKCORE
else
Expand All @@ -191,7 +156,7 @@ public void ReloadButtons()
}
else
{
Control.ActionArea.NoShowAll = true;
actionarea.NoShowAll = true;
if (!Helper.UseHeaderBar)
btcontainer.Hide();
#if GTKCORE
Expand Down
85 changes: 85 additions & 0 deletions src/Eto.Gtk/Forms/EtoControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,91 @@ public IGtkControl Handler
set => _handler = new WeakReference(value);
}

#if GTK3
protected override void OnGetPreferredWidth(out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidth(out minimum_width, out natural_width);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Width > 0)
natural_width = userPreferredSize.Width;

minimum_width = Math.Min(natural_width, minimum_width);
}
}

protected override void OnGetPreferredWidthForHeight(int height, out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidthForHeight(height, out minimum_width, out natural_width);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Width > 0)
natural_width = userPreferredSize.Width;

minimum_width = Math.Min(natural_width, minimum_width);
}
}

protected override void OnGetPreferredHeight(out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeight(out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnAdjustSizeAllocation(Gtk.Orientation orientation, out int minimum_size, out int natural_size, out int allocated_pos, out int allocated_size)
{
base.OnAdjustSizeAllocation(orientation, out minimum_size, out natural_size, out allocated_pos, out allocated_size);
var h = Handler;
if (h != null)
{
var preferredSize = orientation == Gtk.Orientation.Horizontal ? h.UserPreferredSize.Width : h.UserPreferredSize.Height;

if (preferredSize > 0)
natural_size = preferredSize;

minimum_size = Math.Min(natural_size, minimum_size);
}
}

protected override void OnAdjustSizeRequest(Gtk.Orientation orientation, out int minimum_size, out int natural_size)
{
base.OnAdjustSizeRequest(orientation, out minimum_size, out natural_size);
var h = Handler;
if (h != null)
{
var preferredSize = orientation == Gtk.Orientation.Horizontal ? h.UserPreferredSize.Width : h.UserPreferredSize.Height;

if (preferredSize > 0)
natural_size = preferredSize;

minimum_size = Math.Min(natural_size, minimum_size);
}
}
#endif

}

public partial class EtoVBox : Gtk.VBox
{
WeakReference _handler;
public IGtkControl Handler
{
get => _handler?.Target as IGtkControl;
set => _handler = new WeakReference(value);
}

#if GTK3
protected override void OnGetPreferredWidth(out int minimum_width, out int natural_width)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/EtoControls.tt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Eto.Drawing;
namespace Eto.GtkSharp.Forms
{
<#
var controls = new[] { "Fixed", "EventBox" };
var controls = new[] { "Fixed", "EventBox", "VBox" };

foreach (var control in controls)
{
Expand Down
66 changes: 5 additions & 61 deletions src/Eto.Gtk/Forms/FormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,6 @@

namespace Eto.GtkSharp.Forms
{
public class EtoWindow : Gtk.Window
{
public IGtkWindow Handler { get; set; }
public EtoWindow(Gtk.WindowType type) : base(type)
{
}

#if GTK3
protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var size = Handler.UserPreferredSize;
if (size.Height > 0)
natural_height = size.Height;
}

protected override void OnGetPreferredWidthForHeight(int height, out int minimum_width, out int natural_width)
{

base.OnGetPreferredWidthForHeight(height, out minimum_width, out natural_width);
var size = Handler.UserPreferredSize;
if (size.Width > 0)
natural_width = size.Width;
}

protected override void OnGetPreferredWidth(out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidth(out minimum_width, out natural_width);
var size = Handler.UserPreferredSize;
if (size.Width > 0)
natural_width = size.Width;
}

protected override void OnGetPreferredHeight(out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeight(out minimum_height, out natural_height);
var size = Handler.UserPreferredSize;
if (size.Height > 0)
natural_height = size.Height;
}
#endif
}

public class FormHandler : GtkWindow<Gtk.Window, Form, Form.ICallback>, Form.IHandler
{
public FormHandler(Gtk.Window window)
Expand All @@ -55,35 +12,22 @@ public FormHandler(Gtk.Window window)

public FormHandler()
{
Control = new EtoWindow(Gtk.WindowType.Toplevel) { Handler = this };
Control = new Gtk.Window(Gtk.WindowType.Toplevel);
#if GTK2
Control.AllowGrow = true;
#else
Control.Resizable = true;
#endif
Resizable = true;
Control.SetPosition(Gtk.WindowPosition.Center);

var vbox = new Gtk.VBox();
var vbox = new EtoVBox { Handler = this };
vbox.PackStart(WindowActionControl, false, true, 0);
vbox.PackStart(WindowContentControl, true, true, 0);
Control.Child = vbox;
}

#if NET40
public void Show ()
{
Control.Child.ShowAll ();
if (ShowActivated || !Control.AcceptFocus)
Control.Show ();
else {
Control.AcceptFocus = false;
Control.Show ();
Control.AcceptFocus = true;
}
}
#else
public async void Show()
{
DisableAutoSizeUpdate++;
Control.Child.ShowAll();
if (ShowActivated || !Control.AcceptFocus)
Control.Show();
Expand All @@ -94,8 +38,8 @@ public async void Show()
await Task.Delay(1); // why??? Only way I can get it to work properly on ubuntu 16.04
Control.AcceptFocus = CanFocus; // in case user changes it right after this call, but should be true
}
DisableAutoSizeUpdate--;
}
#endif

static object ShowActivated_Key = new object();

Expand Down
Loading

0 comments on commit b897c3c

Please sign in to comment.