2018-04-11 11:29:21 -07:00
using System ;
using MatterHackers.Agg ;
2017-10-22 19:42:41 -07:00
using MatterHackers.Agg.UI ;
2018-01-10 18:54:51 -08:00
using MatterHackers.Agg.VertexSource ;
2017-10-22 19:42:41 -07:00
2017-10-31 17:27:37 -07:00
namespace MatterHackers.MatterControl.CustomWidgets
2017-10-22 19:42:41 -07:00
{
2018-01-10 09:04:31 -08:00
/// <summary>
2018-04-06 06:06:10 -07:00
/// A container control having a header and a content panel, with optional collapse behavior and right aligned widget. Additionally support persistent expansion state via serializationKey
2018-01-10 09:04:31 -08:00
/// </summary>
2018-01-22 10:32:32 -08:00
public class SectionWidget : FlowLayoutWidget , IIgnoredPopupChild
2017-10-22 19:42:41 -07:00
{
2018-01-10 23:59:36 -08:00
private ExpandCheckboxButton checkbox ;
2018-04-06 06:06:10 -07:00
public SectionWidget ( string sectionTitle , GuiWidget sectionContent , ThemeConfig theme , GuiWidget rightAlignedContent = null , int headingPointSize = - 1 , bool expandingContent = true , bool expanded = true , string serializationKey = null , bool defaultExpansion = false )
2017-10-22 19:42:41 -07:00
: base ( FlowDirection . TopToBottom )
{
this . HAnchor = HAnchor . Stretch ;
this . VAnchor = VAnchor . Fit ;
2018-01-10 15:00:59 -08:00
this . Border = new BorderDouble ( bottom : 1 ) ;
2018-04-12 23:08:59 -07:00
this . BorderColor = theme . GetBorderColor ( 50 ) ;
2018-01-10 13:41:15 -08:00
2018-01-04 10:37:41 -08:00
if ( ! string . IsNullOrEmpty ( sectionTitle ) )
2017-10-31 17:27:37 -07:00
{
2018-01-04 10:37:41 -08:00
// Add heading
2018-01-12 08:26:12 -08:00
var pointSize = ( headingPointSize ) = = - 1 ? theme . DefaultFontSize : headingPointSize ;
2018-01-08 21:29:41 -08:00
2018-04-06 06:06:10 -07:00
if ( serializationKey ! = null )
{
string dbValue = UserSettings . Instance . get ( serializationKey ) ;
expanded = dbValue = = "1" | | ( dbValue = = null & & defaultExpansion ) ;
}
2018-04-12 08:42:10 -07:00
checkbox = new ExpandCheckboxButton ( sectionTitle , theme , pointSize : pointSize , expandable : expandingContent )
2017-10-22 19:42:41 -07:00
{
2018-02-09 22:51:18 -08:00
HAnchor = HAnchor . Stretch ,
Checked = expanded ,
2018-02-17 08:38:43 -08:00
Padding = new BorderDouble ( 0 , 5 , 0 , 6 )
2018-02-09 22:51:18 -08:00
} ;
checkbox . CheckedStateChanged + = ( s , e ) = >
{
ContentPanel . Visible = checkbox . Checked ;
2018-02-17 08:38:43 -08:00
// TODO: Remove this Height = 10 and figure out why the layout engine is not sizing these correctly without this.
2018-02-09 22:51:18 -08:00
ContentPanel . Height = 10 ;
} ;
2018-01-08 21:29:41 -08:00
2018-04-06 06:06:10 -07:00
if ( serializationKey ! = null )
{
checkbox . CheckedStateChanged + = ( s , e ) = >
{
UserSettings . Instance . set ( serializationKey , checkbox . Checked ? "1" : "0" ) ;
} ;
}
2018-01-04 10:37:41 -08:00
if ( rightAlignedContent = = null )
{
2018-02-17 08:38:43 -08:00
this . AddChild ( checkbox ) ;
2018-01-04 10:37:41 -08:00
}
else
{
var headingRow = new FlowLayoutWidget ( )
{
HAnchor = HAnchor . Stretch
} ;
2018-02-17 08:38:43 -08:00
headingRow . AddChild ( checkbox ) ;
2018-01-04 10:37:41 -08:00
headingRow . AddChild ( new HorizontalSpacer ( ) ) ;
headingRow . AddChild ( rightAlignedContent ) ;
this . AddChild ( headingRow ) ;
}
}
2017-10-22 19:42:41 -07:00
2018-01-08 23:34:40 -08:00
sectionContent . Visible = expanded ;
this . SetContentWidget ( sectionContent ) ;
}
2018-01-10 23:59:36 -08:00
public ICheckbox Checkbox = > checkbox ;
2018-01-10 09:04:31 -08:00
public GuiWidget ContentPanel { get ; private set ; }
2018-01-10 13:41:15 -08:00
2018-01-08 23:34:40 -08:00
public void SetContentWidget ( GuiWidget guiWidget )
{
2018-01-10 13:41:15 -08:00
// Close old child
this . ContentPanel ? . Close ( ) ;
// Apply default rules for panel widget
guiWidget . HAnchor = HAnchor . Stretch ;
guiWidget . VAnchor = VAnchor . Fit ;
2018-01-08 23:34:40 -08:00
2018-01-10 13:41:15 -08:00
// Set
this . AddChild ( guiWidget ) ;
2018-01-08 23:34:40 -08:00
2018-01-10 13:41:15 -08:00
// Store
this . ContentPanel = guiWidget ;
2018-04-12 21:20:31 -07:00
this . ContentPanel . BorderColor = Color . Transparent ;
2017-10-22 19:42:41 -07:00
}
2018-01-10 18:54:51 -08:00
public int BorderRadius { get ; set ; } = 0 ;
2018-04-11 11:29:21 -07:00
public bool ExpandableWhenDisabled { get ; set ; }
public override bool Enabled
{
get = > ( this . ExpandableWhenDisabled ) ? true : base . Enabled ;
set
{
if ( this . ExpandableWhenDisabled )
{
this . ContentPanel . Enabled = value ;
}
else
{
base . Enabled = value ;
}
}
}
2018-01-10 18:54:51 -08:00
public override void OnDrawBackground ( Graphics2D graphics2D )
{
if ( this . BorderRadius > 0 )
{
var rect = new RoundedRect ( this . LocalBounds , this . BorderRadius ) ;
graphics2D . Render ( rect , this . BackgroundColor ) ;
}
else
{
base . OnDrawBackground ( graphics2D ) ;
}
}
2017-10-22 19:42:41 -07:00
}
}