#
# Copyright © 2020 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#
import System
import System.Windows.Controls as WPFControls

def createLabel(text, row, col, parent=None):
    control = WPFControls.Label()
    control.Content = text
    WPFControls.Grid.SetRow(control, row)
    WPFControls.Grid.SetColumn(control, col)

    if parent is not None:
        parent.Children.Add(control)

    return control


def createComboBox(contents, row, col, parent=None):
    control = WPFControls.ComboBox()
    
    for c in contents:
        control.Items.Add(c)
    
    WPFControls.Grid.SetRow(control, row)
    WPFControls.Grid.SetColumn(control, col) 

    if parent is not None:
        parent.Children.Add(control)

    return control 


def createButton(label, cb, row, col, parent=None):
    control = WPFControls.Button()
    control.Content = label
    control.Click += cb
    
    WPFControls.Grid.SetRow(control, row)
    WPFControls.Grid.SetColumn(control, col) 

    if parent is not None:
        parent.Children.Add(control)

    return control


def getGridLength(value):
    g = None

    if value=="*":
        g = System.Windows.GridLength(1.0, System.Windows.GridUnitType.Star)
    elif value=="auto":
        g = System.Windows.GridLength.Auto
    else:
        g = System.Windows.GridLength(float(value), System.Windows.GridUnitType.Pixel)

    return g


def getMargin(l, t, r, b):
    return System.Windows.Thickness(float(l), float(t), float(r), float(b))


def createGrid(rows, cols):
    grid = WPFControls.Grid()

    for r in rows:
        d = WPFControls.RowDefinition()
        d.Height = getGridLength(r)
        grid.RowDefinitions.Add(d)

    for c in cols:
        d = WPFControls.ColumnDefinition()
        d.Width = getGridLength(c)
        grid.ColumnDefinitions.Add(d)

    return grid
