__fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
    const TCustomCommandType * Command, TStrings * CustomCommandOptions,
    unsigned int Flags, TCustomCommand * CustomCommandForOptions,
    const UnicodeString & Site, const TShortCuts * ShortCuts) :
  TCustomDialog(HELP_EXTENSION_OPTIONS)
{
  FCommand = Command;
  FFlags = Flags;
  FCustomCommandOptions = CustomCommandOptions;
  FSite = Site;
  Caption = StripEllipsis(StripHotkey(FCommand->Name));
  Width = ScaleByTextHeight(this, 400);

  bool HasGroups = false;
  int ControlIndex = 0;
  for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  {
    const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);

    if ((Option.Flags & FFlags) != 0)
    {
      UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
      UnicodeString Value;
      if ((CustomCommandForOptions != NULL) &&
          Option.HasPatterns(CustomCommandForOptions))
      {
        Value = CustomCommandForOptions->Complete(Option.Default, true);
      }
      else
      {
        if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
        {
          Value = FCustomCommandOptions->Values[OptionKey];
        }
        else
        {
          Value = Option.Default;
        }
      }

      int Tag = (OptionIndex << 16) + ControlIndex;
      TControl * Control = NULL;
      std::vector<UnicodeString> Values;
      if (Option.Kind == TCustomCommandType::okUnknown)
      {
        Control = NULL;
      }
      else if (Option.Kind == TCustomCommandType::okLabel)
      {
        TLabel * Label = CreateLabel(Option.Caption);
        AddText(Label);
        Control = Label;
      }
      else if (Option.Kind == TCustomCommandType::okLink)
      {
        TStaticText * Label = new TStaticText(this);
        Label->Caption = Option.Caption;
        if (IsHttpOrHttpsUrl(Label->Caption))
        {
          Label->Caption = SecureUrl(Label->Caption);
          LinkLabel(Label);
          Label->TabStop = true;
        }
        else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
        {
          Label->OnClick = LinkLabelClick;
          LinkLabel(Label);
          Label->TabStop = true;
        }
        else
        {
          // keep it plain text, as we have no URL
        }
        AddText(Label);
        Control = Label;
      }
      else if (Option.Kind == TCustomCommandType::okGroup)
      {
        StartGroup(Option.Caption);
        HasGroups = true;
      }
      else if (Option.Kind == TCustomCommandType::okSeparator)
      {
        AddSeparator();
      }
      else if (Option.Kind == TCustomCommandType::okTextBox)
      {
        Control = CreateHistoryComboBox(Option, Value);
      }
      else if (Option.Kind == TCustomCommandType::okFile)
      {
        THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
        TButton * Button = new TButton(this);
        Button->Parent = GetDefaultParent();
        Button->Width = HelpButton->Width;
        Button->Left = GetDefaultParent()->ClientWidth - Button->Width - HorizontalMargin;
        ComboBox->Width = Button->Left - ComboBox->Left - ScaleByTextHeight(this, 6);
        Button->Top = ComboBox->Top - ScaleByTextHeight(this, 2);
        Button->Tag = Tag;
        Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
        Button->OnClick = BrowseButtonClick;
        ScaleButtonControl(Button);
        AddWinControl(Button);
        Control = ComboBox;
      }
      else if (Option.Kind == TCustomCommandType::okDropDownList)
      {
        TComboBox * ComboBox = new TComboBox(this);
        ComboBox->Style = csDropDownList;

        AddOptionComboBox(ComboBox, Value, Option, Values);

        Control = ComboBox;
      }
      else if (Option.Kind == TCustomCommandType::okComboBox)
      {
        TComboBox * ComboBox = new TComboBox(this);
        ComboBox->Style = csDropDown;

        AddOptionComboBox(ComboBox, Value, Option, Values);
        if (ComboBox->ItemIndex < 0)
        {
          ComboBox->Text = Value;
        }

        Control = ComboBox;
      }
      else if (Option.Kind == TCustomCommandType::okCheckBox)
      {
        TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);

        CheckBox->Checked =
          (Option.Params.size() >= 1) &&
          (Value == Option.Params[0]);

        Control = CheckBox;
      }
      else
      {
        DebugFail();
      }

      if (Control != NULL)
      {
        Control->Tag = Tag;
      }
      FControls.push_back(Control);
      FValues.push_back(Values);
      ControlIndex++;
      DebugAssert(static_cast<int>(FControls.size()) == ControlIndex);
    }
  }

  if (ShortCuts != NULL)
  {
    if (HasGroups)
    {
      StartGroup(LoadStr(EXTENSION_GENERAL_GROUP));
    }
    else if (ControlIndex > 0)
    {
      AddSeparator();
    }
    FShortCutCombo = new TComboBox(this);
    AddShortCutComboBox(FShortCutCombo, CreateLabel(LoadStr(EXTENSION_SHORTCUT)), *ShortCuts);
  }
}