Quintiq file version 2.0
#parent: #root
Method NewUnit (
  internal[GUIComponent] parent,
  Unit parentunit,
  Number x,
  Number y
) as Unit id:Method_DialogCreateEditUnit_NewUnit
{
  #keys: '[121072.0.301552331]'
  Body:
  [*
    // To create a new unit
    
    data := MacroPlan.Unit( relshadow, 
                            Parent := parentunit,
                            Currency_MP := MacroPlan.BaseCurrency(),
                            UnitOfMeasure_MP := MacroPlan.DefaultUnitOfMeasure(),
                            ID := '' 
                            );
    Dialog.Data( data );
    
    Dialog.SetDefaultCapacity();
    Dialog.SetDefaultValue( parentunit, null( Group ) );
    Dialog.EnableDisableComponent();
    Dialog.SetTooltip();
    
    result := this.DoModal( parent );
    
    if( result > 0 )
    {
      // Calculate the default x and y position, then set to data
      gridx := 0;
      gridy := 0;
      scv := DataHolderView.Data();
      Node::NearestGridPoint( x - scv.OffsetX(), y - scv.OffsetY(), gridx, gridy, scv.GridPointDistance() );
      data.DefaultGridX( gridx );
      data.DefaultGridY( gridy );
    
      data.Commit();
    }
    
    return data.WrappedInstance();
  *]
}
