CalcConstraints method

void CalcConstraints(
  1. Integer MinWidth,
  2. Integer MinHeight,
  3. Integer MaxWidth,
  4. Integer MaxHeight,
)

Implementation

void CalcConstraints(Integer MinWidth, Integer MinHeight,
                     Integer MaxWidth, Integer MaxHeight)
{
  const int sNone=0, sReg=1, sStretch=2, sOther=3;

  void DoCalcConstraints(TControl Control,
                         Integer MinWidth, Integer MinHeight,
                         Integer MaxWidth, Integer MaxHeight)
  {
    if(Control.Constraints.MinWidth > 0)
      MinWidth.Value = Control.Constraints.MinWidth;
    else
      MinWidth.Value = 0;
    if(Control.Constraints.MinHeight > 0)
      MinHeight.Value = Control.Constraints.MinHeight;
    else
      MinHeight.Value = 0;
    if(Control.Constraints.MaxWidth > 0)
      MaxWidth.Value = Control.Constraints.MaxWidth;
    else
      MaxWidth.Value = 0;
    if(Control.Constraints.MaxHeight > 0)
      MaxHeight.Value = Control.Constraints.MaxHeight;
    else
      MaxHeight.Value = 0;
    // Allow override of constraints
    Control.ConstrainedResize(MinWidth, MinHeight, MaxWidth, MaxHeight);
  }

  if(!HandleAllocated() || ControlCount == 0)
    return;
  // Adjust min/max size to compensate for non-client area
  TRect R = GetClientRect();
  AdjustClientRect(R);
  if(IsRectEmpty(R))
    return; // Coming from an icon view, don't do constraints
  int AdjustMinWidth = Width - R.width;
  int AdjustMinHeight = Height - R.height;
  int AdjustMaxWidth = Width - R.width;
  int AdjustMaxHeight = Height - R.height;
  if(MinWidth.Value > 0)  MinWidth.Value-=AdjustMinWidth;
  if(MinHeight.Value > 0) MinHeight.Value-=AdjustMinHeight;
  if(MaxWidth.Value > 0)  MaxWidth.Value-=AdjustMaxWidth;
  if(MaxHeight.Value > 0) MaxHeight.Value-=AdjustMaxHeight;
  //Compare incoming min/max constraints with those that we calculate
  try
  {
    int TotalMinWidth = 0,  TotalMinWidth2 = 0;
    int TotalMaxWidth = 0,  TotalMaxWidth2 = 0;
    int TotalMinHeight = 0, TotalMinHeight2 = 0;
    int TotalMaxHeight = 0, TotalMaxHeight2 = 0;
    int WidthScale =sNone; //: TScale;
    int HeightScale=sNone; //: TScale;
    for(int I = 0; I<ControlCount; I++)
    {
      TControl Control = Controls[I];

      if(Control.Visible || (Control.ComponentState.contains(ComponentStates.Designing)) &&
         !(Control.ControlStyle.contains(ControlStyles.NoDesignVisible)))
      {
        Integer ControlMinWidth = Integer();
        Integer ControlMaxWidth = Integer();
        Integer ControlMinHeight = Integer();
        Integer ControlMaxHeight = Integer();
        DoCalcConstraints(Control, ControlMinWidth, ControlMinHeight, ControlMaxWidth, ControlMaxHeight);

        switch(Control.Align)
        {
          case TAlign.Top:
          case TAlign.Bottom:
            WidthScale = sReg;
            break;
          case TAlign.Client:
            WidthScale = sStretch;
            break;
          case TAlign.None:
            if(Anchors.contains(TAnchorKind.Left) && Anchors.contains(TAnchorKind.Right))
            {
              WidthScale = sReg;
              // Adjust Anchored controls
              if(ControlMinWidth.Value > 0)
                ControlMinWidth.Value = R.width - Control.Width - ControlMinWidth.Value;
              if(ControlMaxWidth.Value > 0)
                ControlMaxWidth.Value = R.width + ControlMaxWidth.Value - Control.Width;
            }
            else
              WidthScale = sNone;
            break;
          default:
            WidthScale = sOther;
            break;
        }

        switch(Control.Align)
        {
          case TAlign.Left:
          case TAlign.Right:
            HeightScale = sReg;
            break;
          case TAlign.Client:
            HeightScale = sStretch;
            break;
          case TAlign.None:
            if(Anchors.contains(TAnchorKind.Top) && Anchors.contains(TAnchorKind.Bottom))
            {
              HeightScale = sReg;
              // Adjust Anchored controls
              if(ControlMinHeight.Value > 0)
                ControlMinHeight.Value = R.height - Control.Height - ControlMinHeight.Value;
              if(ControlMaxHeight.Value > 0)
                ControlMaxHeight.Value = R.height + ControlMaxHeight.Value - Control.Height;
            }
            else
              HeightScale = sNone;
            break;
          default:
            HeightScale = sOther;
        }

        // Calculate min/max width
        switch (WidthScale)
        {
          case sReg:
          case sStretch:
            {
              if((ControlMinWidth.Value > 0) && (ControlMinWidth > MinWidth))
              {
                MinWidth.Value = ControlMinWidth.Value;
                if(MinWidth.Value > TotalMinWidth)
                  TotalMinWidth = MinWidth.Value;
              }
              if((ControlMaxWidth.Value > 0) && (ControlMaxWidth < MaxWidth))
              {
                MaxWidth.Value = ControlMaxWidth.Value;
                if(MaxWidth.Value > TotalMaxWidth)
                  TotalMaxWidth = MaxWidth.Value;
              }
            } break;

          case sOther:
            {
              TotalMinWidth2+=Width;
              TotalMaxWidth2+=Width;
            } break;
        }

        // Calculate min/max height
        switch(HeightScale)
        {
          case sReg:
          case sStretch:
            {
              if((ControlMinHeight.Value > 0) && (ControlMinHeight > MinHeight))
              {
                MinHeight.Value = ControlMinHeight.Value;
                if(MinHeight.Value > TotalMinHeight)
                  TotalMinHeight = MinHeight.Value;
              }
              if((ControlMaxHeight.Value > 0) && (ControlMaxHeight < MaxHeight))
              {
                MaxHeight.Value = ControlMaxHeight.Value;
                if(MaxHeight.Value > TotalMaxHeight)
                  TotalMaxHeight = MaxHeight.Value;
              }
            } break;

          case sOther:
            {
              TotalMinHeight2+=Height;
              TotalMaxHeight2+=Height;
            } break;
        }
      }
    }
    if((TotalMinWidth > 0) && (TotalMinWidth+TotalMinWidth2 > MinWidth.Value))
      MinWidth.Value = TotalMinWidth+TotalMinWidth2;
    if((TotalMaxWidth > 0) && ((MaxWidth.Value == 0) || (TotalMaxWidth+TotalMaxWidth2 > MaxWidth.Value)))
      MaxWidth.Value = TotalMaxWidth+TotalMaxWidth2;
    if((TotalMinHeight > 0) && (TotalMinHeight+TotalMinHeight2 > MinHeight.Value))
      MinHeight.Value = TotalMinHeight+TotalMinHeight2;
    if((TotalMaxHeight > 0) && ((MaxHeight.Value == 0) || (TotalMaxHeight+TotalMaxHeight2 > MaxHeight.Value)))
      MaxHeight.Value = TotalMaxHeight+TotalMaxHeight2;
  }
  finally
  {
    if(MinWidth.Value > 0) MinWidth.Value+=AdjustMinWidth;
    if(MinHeight.Value > 0) MinHeight.Value+=AdjustMinHeight;
    if(MaxWidth.Value > 0) MaxWidth.Value+=AdjustMaxWidth;
    if(MaxHeight.Value > 0) MaxHeight.Value+=AdjustMaxHeight;
  }
}