{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2009                                        }
{            Email : info@tmssoftware.com                                   }
{            Web : http://www.tmssoftware.com                               }
{                                                                           }
{ The source code is given as is. The author is not responsible             }
{ for any possible damage done due to the use of this code.                 }
{ The component can be freely used in any application. The complete         }
{ source code remains property of the author and may not be distributed,    }
{ published, given or sold in any form as such. No parts of the source      }
{ code can be included in any other component or application without        }
{ written authorization of the author.                                      }
{***************************************************************************}

unit IWAdvCheckGroup;

interface

{$I TMSDEFS.INC}

uses
  {$IFDEF LINUX}
  QControls, QGraphics, QForms, QIWPicCntnr,
  {$ELSE}
  Controls, Graphics, Forms, Windows, IWPicCntnr,
  {$ENDIF}
  {$IFDEF DELPHI6_LVL}
  Types,
  {$ENDIF}
  Classes,
  {$IFDEF TMSIW51}
  IWBaseControl, IWColor,
  {$ENDIF}
  IWControl, IWTypes, IWFont, SysUtils, IWCompButton,
  IWCompEdit, IWCompLabel, IWBaseInterfaces, IWCompCheckbox, IWExtCtrls, IWHTMLTag, IWCompListbox,
  IWTMSBase,IWCompRectangle
  {$IFDEF TMSIW6}
  , IWRenderContext
  {$ENDIF}
  {$IFDEF TMSIW7}
  , IWBaseHTMLControl, IWScriptEvents
  {$ENDIF}
  {$IFDEF TMSIW9}
  ,IWXMLTag
  {$ENDIF}
  ;

type
  TIntList = class(TList)
  private
    FOnChange: TNotifyEvent;
    procedure SetInteger(Index: Integer; Value: Integer);
    function GetInteger(Index: Integer): Integer;
    function GetStrValue: string;
    procedure SetStrValue(const Value: string);
  public
    constructor Create;
    property Items[index: Integer]: Integer read GetInteger write SetInteger;
    default;
    procedure Add(Value: Integer);
    procedure Delete(Index: Integer);
    property StrValue: string read GetStrValue write SetStrValue;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
  end;

  TTIWAdvCheckGroup = class(TIWControl)
  private
    FBackGroundColor: TIWColor;
    FBorderColor: TIWColor;
    FBorderWidht: integer;
    FCaption: string;
    FCaptionFont: TIWFont;
    FColumnBorderColor: TIWColor;
    FColumnBorderWidth: integer;    
    FColumns: integer;
    FChecked: TIntList;
    FEnabledItems: TIntList;
    FFont: TIWFont;
    FItems: TStringList;
    FCheckChecked: string;
    FCheckCheckedDisabled: string;
    FCheckUnchecked: string;
    FCheckUncheckedDisabled: string;    
    FLayout: TIWRadioGroupLayout;
    FValues: TStringList;
    procedure SetCaptionFont(const Value: TIWFont);
    procedure SetFont(const Value: TIWFont);    
    procedure SetItems(const Value: TStringList);
    procedure SetValues(const Value: TStringList);
    procedure DrawCheck(R:TRect;State,Enabled: Boolean);
  protected
    {$IFDEF TMSIW6}
    procedure IWPaint; override;
    {$ELSE}
    procedure Paint; override;
    {$ENDIF}
    procedure SetValue(const AValue: string); override;
    procedure RenderCB(Contents: TIWHTMLTagCollection; i: integer);
    procedure RenderCBImage(Contents: TIWHTMLTagCollection; i: integer);
    function ReplaceInput(Index: Integer): string;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    {$IFDEF TMSIW6}
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
    property Checked: TIntList read FChecked write FChecked;
    property EnabledItems: TIntList read FEnabledItems write FEnabledItems;
  published
    property BackGroundColor: TIWColor read FBackGroundColor write FBackGroundColor default clNone;
    property BorderColor: TIWColor read FBorderColor write FBorderColor default clWebSilver;
    property BorderWidth: integer read FBorderWidht write FBorderWidht default 1;
    property Caption: string read FCaption write FCaption;
    property CaptionFont: TIWFont read FCaptionFont write SetCaptionFont;
    property ColumnBorderColor: TIWColor read FColumnBorderColor write FColumnBorderColor default clNone;
    property ColumnBorderWidth: integer read FColumnBorderWidth write FColumnBorderWidth default 0;    
    property Columns: integer read FColumns write FColumns default 1;
    property Font: TIWFont read FFont write SetFont;
    property Items: TStringList read FItems write SetItems;
    property Values: TStringList read FValues write SetValues;    
    property Layout: TIWRadioGroupLayout read FLayout write FLayout default glVertical;
    property CheckChecked: string read FCheckChecked write FCheckChecked;
    property CheckCheckedDisabled: string read FCheckCheckedDisabled write FCheckCheckedDisabled;
    property CheckUnchecked: string read FCheckUnchecked write FCheckUnchecked;
    property CheckUncheckedDisabled: string read FCheckUncheckedDisabled write FCheckUncheckedDisabled;
    property Width default 120;
  end;

implementation

uses
  {$IFDEF LINUX}
  QImgList,
  {$ELSE}
  ImgList, ShellAPI, CommCtrl,
  {$ENDIF}
  IWApplication, IWServerControllerBase,IWResourceStrings,
  SWStrings, SWSystem, IWAppForm, IWServer //, IWBaseHTMLControl
  ;

{$IFDEF TMSIW6}
procedure TTIWAdvCheckGroup.IWPaint;
{$ELSE}
procedure TTIWAdvCheckGroup.Paint;
{$ENDIF}
var
  i, colwidth, itemTop, rowCount, itemsPerCol, checkSize, lineHeight, colCount: integer;
  R, Rchk: TRect;
  defaultStr: string;
begin
  Canvas.Font.Assign(Self.Font);
  Canvas.Brush.Color := Color;
  Canvas.Pen.Color := BorderColor;
  Canvas.Pen.Width := BorderWidth;
  R := Rect(2, 2, Width - 2, Height - 2);

  InflateRect(R,-1,-1);
  {$IFNDEF LINUX}
  SetBKMode(Canvas.Handle,TRANSPARENT);

  if (Items.Count = 0) and (Caption = EmptyStr) then
  begin
    defaultStr := 'TIWADVCheckGroup';
    DrawText(Canvas.Handle,PChar(defaultStr),Length(defaultStr),R,DT_LEFT);
  end
  else
  begin
    DrawText(Canvas.Handle,PChar(Caption),Length(Caption),R,DT_LEFT);

    rowCount := 0;
    colCount := 1;
    checkSize := 18;
    lineHeight := 25;
    itemsPerCol := Items.Count div Columns;
    if (Items.Count mod Columns) > 0 then
      itemsPerCol := itemsPerCol + 1;  

    with Canvas do
    begin
      for I := 0 to Items.Count - 1 do
      begin

        if Layout = glVertical then
        begin

          colwidth := Width div Columns * (i div itemsPerCol);

          itemTop := rowCount * 18;
          rowCount := rowCount + 1;

          if rowCount = itemsPerCol then
              rowCount := 0;

        end
        else
        begin
          colwidth := Width div Columns * (colCount-1);

          itemTop := rowCount * checkSize;

          if (colCount = Columns) then
          begin
            rowCount := rowCount + 1;
            colCount := 0;
          end;

          colCount := colCount + 1;

        end;

        Rchk := Rect(2+colwidth, itemtop+lineHeight, 2+checkSize+colwidth, itemtop+lineHeight+checkSize);
        //DrawCheck(Rchk, StrToBool(iif(Checked.Items[i] = 0, 'True', 'False')), StrToBool(iif(EnabledItems.Items[i] = 1, 'True', 'False')));
        DrawCheck(Rchk, StrToBool(iif(i = 0, 'True', 'False')), True);
        TextOut(lineHeight+colwidth, itemtop+lineHeight, Items[i]);

      end;
    end;
  end;
  {$ENDIF}
end;

procedure TTIWAdvCheckGroup.DrawCheck(R:TRect;State,Enabled: Boolean);
  var
    DrawState: Integer;
    DrawRect: TRect;
    CheckSize: integer;
    DRect: TRect;
    Hold: integer;
  begin
        CheckSize := 16;

        if State then
          DrawState := DFCS_BUTTONCHECK or DFCS_CHECKED
        else
          DrawState := DFCS_BUTTONCHECK;

        if not Enabled then
          DrawState := DrawState or DFCS_INACTIVE;

        DrawRect.Left := R.Left + (R.Right - R.Left - CheckSize) div 2;
        DrawRect.Top:= R.Top + (R.Bottom - R.Top - CheckSize) div 2;
        DrawRect.Right := DrawRect.Left + CheckSize;
        DrawRect.Bottom := DrawRect.Top + CheckSize;

        if UseRightToLeftAlignment then
        begin
          DRect := DrawRect;

          DrawRect.Left := ClientWidth - DrawRect.Left;
          DrawRect.Right := ClientWidth - DrawRect.Right;

          Hold := DrawRect.Left;
          DrawRect.Left := DrawRect.Right;
          DrawRect.Right := Hold;
        end;

        DrawFrameControl(Canvas.Handle,DrawRect,DFC_BUTTON,DrawState);

        if UseRightToLeftAlignment then
        begin
          DrawRect := DRect;
        end;
  end;


constructor TTIWAdvCheckGroup.Create(AOwner: TComponent);
begin
  inherited;
  BackGroundColor := clNone;
  //FCaption := 'Caption';
  FCaptionFont := TIWFont.Create;
  ColumnBorderColor := clNone;
  ColumnBorderWidth := 0;  
  Columns := 1;
  BorderWidth := 1;
  FChecked := TIntList.Create;
  FFont := TIWFont.Create;
  FEnabledItems := TIntList.Create;
  FItems := TStringList.Create;
  FValues := TStringList.Create;
  Width := 120;

  FNeedsFormTag := True;
  //FSupportsInput := True;
  //FSupportsSubmit := true;
end;

destructor TTIWAdvCheckGroup.Destroy;
begin
  FCaptionFont.Free;
  FChecked.Free;
  FFont.Free;
  FEnabledItems.Free;
  FItems.Free;
  FValues.Free;
  inherited;
end;

procedure TTIWAdvCheckGroup.RenderCB(Contents: TIWHTMLTagCollection; i: Integer);
begin
  with Contents.AddTag('INPUT') do
  begin
    AddStringParam('TYPE', 'CHECKBOX');
    Add(iif(Checked.Items[i] = 0, ' CHECKED'));
    AddStringParam('NAME', HTMLName + '_CHK');
    AddStringParam('id', HTMLName + '_CHK_'+IntToStr(i+1));
    AddStringParam('VALUE', IntToStr(i));

    if Enabled and Editable and (EnabledItems.Items[i] = 1) then
      AddStringParam('onClick', HTMLName + 'CheckClick(' + IntToStr(i) + ',true);');

    Add(iif((not Enabled) or (not Editable) or (EnabledItems.Items[i] = 0) , 'DISABLED'));

  end;

  //with Contents.AddTag('SPAN') do
  //begin
    //if Enabled and Editable and (EnabledItems.Items[i] = 1) then
    //  AddStringParam('onClick', HTMLName + 'CheckClick(' + IntToStr(i) + ',false);');

    //Contents.AddText(Items.Strings[i]);
      Contents.AddText(ReplaceInput(i));
  //end;

end;

procedure TTIWAdvCheckGroup.RenderCBImage(Contents: TIWHTMLTagCollection; i: Integer);
begin

  with Contents.AddTag('IMG') do
  begin
//    Add(iif((not Enabled) or (not Editable) or (EnabledItems.Items[i] = 0) , 'DISABLED'));

    if (not Enabled) or (not Editable) or (EnabledItems.Items[i] = 0) then
      Add(iif(Checked.Items[i] = 0, 'SRC=' + CheckCheckedDisabled, 'SRC=' + CheckUncheckedDisabled))
    else
      Add(iif(Checked.Items[i] = 0, 'SRC=' + CheckChecked, 'SRC=' + CheckUnchecked));

    AddStringParam('NAME', HTMLName + '_CHK');
    AddStringParam('id', HTMLName + '_CHK_'+IntToStr(i+1));

    if Enabled and Editable and (EnabledItems.Items[i] = 1) then
      AddStringParam('onClick', HTMLName + 'CheckClick(' + IntToStr(i) + ');');

  end;

  //with Contents.AddTag('SPAN') do
  //begin
  //  if Enabled and Editable and (EnabledItems.Items[i] = 1) then
  //    AddStringParam('onClick', HTMLName + 'CheckClick(' + IntToStr(i) + ');');

  //Contents.AddText(Items.Strings[i]);
    Contents.AddText(ReplaceInput(i));
  //end;

end;


{$IFDEF TMSIW6}
function TTIWAdvCheckGroup.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvCheckGroup.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  i, j: Integer;
  columnItem, itemCount, itemsPerCol, valueCount: integer;
  tempCount, hiddenChecked: string;
  script, colBorder: string;
begin

  Result := TIWHTMLTag.CreateTag('DIV'); try

    with Result.Contents.AddTag('FIELDSET') do
    begin

      AddStringParam('style','border:' + IntToStr(BorderWidth)
      + 'px solid ' + ColorToRGBString(BorderColor) + ';'
      + iif(BackGroundColor <> clNone, 'background-color:' + ColorToRGBString(BackGroundColor) + ';', '')      
      + 'width:' + IntToStr(Width) + 'px;height:auto;');

      if Caption <> EmptyStr then
      begin
        with Contents.AddTag('LEGEND') do
        begin
           AddStringParam('style',
           'font-family:' + iif(CaptionFont.FontFamily <> EmptyStr, CaptionFont.FontFamily, CaptionFont.FontName + ';')
           + iif(CaptionFont.Color <> clNone, 'color:' + ColorToRGBString(CaptionFont.Color) + ';', '')
           + 'font-size:' + IntToStr(CaptionFont.Size) + 'pt;'
           + iif(fsBold in CaptionFont.Style, 'font-weight:bold;', 'font-weight:normal;')
           + iif(fsItalic in CaptionFont.Style, 'font-style:italic;', 'font-style:normal;')
           );
           Contents.AddText(Caption);
        end;
      end;

      columnItem := 0;
      Contents.AddText('<TABLE cellspacing=0 cellpadding=4 style="font-size:' + IntToStr(FFont.Size)+ 'pt;'
        + iif(fsBold in FFont.Style, 'font-weight:bold;', '')
        + iif(fsItalic in FFont.Style, 'font-style:italic;', '')
        + 'font-family:' + iif(FFont.FontFamily <> EmptyStr, FFont.FontFamily, FFont.FontName + ';')
        + 'color:' + ColorToRGBString(Font.Color) + ';width:100%;height:auto;">');


      if Layout = glHorizontal then
      begin

        for i := 0 to Items.Count - 1 do
        begin

          if (columnItem = 0) then
              Contents.AddText('<TR>');

          if (columnItem = Columns - 1) or (ColumnBorderColor = clNone) or (ColumnBorderWidth = 0) then
            colBorder := EmptyStr
          else
            colBorder := 'border-right:' + IntToStr(ColumnBorderWidth)
              + 'px solid ' + ColorToRGBString(ColumnBorderColor) + ';';

          Contents.AddText('<TD style="' + colBorder +'vertical-align:top;width:' + IntToStr(100 div Columns) + '%">');

          if (CheckChecked = EmptyStr) or (CheckUnchecked = EmptyStr)
            or (CheckCheckedDisabled = EmptyStr) or (CheckUncheckedDisabled = EmptyStr) then
            RenderCB(Contents,i)
          else
            RenderCBImage(Contents,i);

          Contents.AddText('</TD>');
          columnItem := columnItem + 1;

          if (columnItem = Columns) then
          begin
            Contents.AddText('</TR>');
            columnItem := 0;
          end;

        end;
      end
      else
      begin

        tempCount := FloatToStr(Items.count div Columns);
        itemCount := StrToInt(tempCount);
        itemsPerCol := (Items.count mod Columns);
        if itemsPerCol > 0 then
          itemCount := itemCount + 1;

        for j := 0 to itemCount - 1 do
        begin

          for i := 0 to Columns - 1 do
          begin

            if (columnItem = 0) then
              Contents.AddText('<TR>');

            if (columnItem = Columns - 1) or (ColumnBorderColor = clNone) or (ColumnBorderWidth = 0) then
              colBorder := EmptyStr
            else
              colBorder := 'border-right:' + IntToStr(ColumnBorderWidth)
                + 'px solid ' + ColorToRGBString(ColumnBorderColor) + ';';

            Contents.AddText('<TD style="' + colBorder + 'vertical-align:top;width:' + IntToStr(100 div Columns) + '%">');

            if (CheckChecked = EmptyStr) or (CheckUnchecked = EmptyStr)
              or (CheckCheckedDisabled = EmptyStr) or (CheckUncheckedDisabled = EmptyStr) then
            begin
              if i = 0 then
                RenderCB(Contents, j)
              else if Items.Count > j+(i*itemCount) then
                RenderCB(Contents, j+(i*itemCount));
            end
            else
            begin
              if i = 0 then
                RenderCBImage(Contents, j)
              else if Items.Count > j+(i*itemCount) then
                RenderCBImage(Contents, j+(i*itemCount));
            end;

            Contents.AddText('</TD>');
            columnItem := columnItem + 1;

            if (columnItem = Columns) then
            begin
              Contents.AddText('</TR>');
              columnItem := 0;
            end;

          end

        end;
        
      end;

      Contents.AddText('</TABLE>');

    end;
  except FreeAndNil(Result); raise; end;

  //Prefill hidden input
  hiddenChecked := EmptyStr;
  for i := 0 to Items.Count - 1 do
  begin
      hiddenChecked := hiddenChecked + IntToStr(Checked.Items[i]);
  end;

  hiddenChecked := hiddenChecked + '|';

  if Values.Count > 0 then
    valueCount := Values.Count
  else
    valueCount := Items.Count;

  for i := 0 to valueCount - 1 do
  begin
     if i < Values.Count then
       hiddenChecked := hiddenChecked + StringReplace(Values.Strings[i], '"', '''', [rfReplaceAll]);

     if i < valueCount - 1 then
       hiddenChecked := hiddenChecked + '^';
  end;                          

  //add hidden input
  with Result.Contents.AddTag('INPUT') do
  begin
    AddStringParam('TYPE', 'hidden');
    AddStringParam('NAME', HTMLName);
    AddStringParam('ID', HTMLName + '_INPUT');
    AddStringParam('VALUE', hiddenChecked);
  end;

  if (CheckChecked = EmptyStr) or (CheckUnchecked = EmptyStr)
    or (CheckCheckedDisabled = EmptyStr) or (CheckUncheckedDisabled = EmptyStr) then
    begin
      script := script
       + '<script>'#13
       + 'function ' + HTMLName + 'CheckClick(index,chk,input)'#13
       + '{'#13
       + '  var ctrl = FindElem(''' + HTMLName + '_CHK_''+(index+1));'#13
       + '  if (input) '#13
       + '    ctrl.checked = true;'#13
       + '  else if (!chk) '#13
       + '    ctrl.checked = !ctrl.checked;'#13
       + ''#13
       + '  var hi = FindElem(''' + HTMLName + '_INPUT'');'#13
       + '  var oldval = hi.value;'#13
       + '  var updval = 1;'#13
       + '  if (ctrl.checked)'
       + '    updval = 0;'#13
       + '  hi.value = oldval.substr(0,index) + updval + oldval.substr(index + 1, oldval.length - index + 1);'#13
       + '}'#13
       + '</script>'#13
       ;
    end
    else
    begin
      script := script
       + '<script>'#13
       + 'function ' + HTMLName + 'CheckClick(index,chk,input)'#13
       + '{'#13
       + '  var ctrl = FindElem(''' + HTMLName +'_CHK_''+(index+1));'#13
       + '  if (ctrl.src.indexOf("' + CheckChecked + '") >= 0)'#13
       + '  {'#13
       + '    if (! input)'#13
       + '      ctrl.src = ctrl.src.replace("' + CheckChecked + '", "' + CheckUnChecked + '");'#13
       + '  }'#13
       + '  else'#13
       + '    ctrl.src = ctrl.src.replace("' + CheckUnChecked + '", "' + CheckChecked + '");'#13
       + ''#13
       + '  var hi = FindElem(''' + HTMLName + '_INPUT'');'#13
       + '  var oldval = hi.value;'#13
       + '  var updval = 1;'#13
       + '  if (ctrl.src.indexOf("' + CheckChecked + '") >= 0)'#13
       + '    updval = 0;'#13
       + '  hi.value = oldval.substr(0,index) + updval + oldval.substr(index + 1, oldval.length - index + 1);'#13
       + '}'#13
       + '</script>'#13
       ;
    end;

    script := script
     + '<script>'
     + 'function ' + HTMLNAME + 'SetCustomValues() '#13
     + '{'#13
     + '  var hi = FindElem(''' + HTMLName + '_INPUT'');'#13
     + '  var shi = hi.value.split("|");'#13
     + '  hi.value = shi[0] + "|";'#13
     + '  for (i = 0; i < ' + IntToStr(valueCount) + '; i++)'#13
     + '  {'#13
     + '    ctrl = FindElem("'+ HTMLName +'VAL" + i);'#13
     + '    if (ctrl)'#13
     + '      hi.value += ctrl.value;'#13
     + '    if (i < ' + IntToStr(valueCount) + ' - 1)'#13
     + '      hi.value += "^";'#13
     + '  }'#13
     + '}'#13
     + '</script>'
     ;

  Result.Contents.AddText(script);

end;

function TTIWAdvCheckGroup.ReplaceInput   (Index: integer): string;
var
  itemText, itemValue, onClick, onInputClick: string;
begin
  itemText := Items.Strings[Index];
  onClick := 'onClick="' + HTMLName + 'CheckClick(' + IntToStr(Index) + ',false);"';
  onInputClick := 'onClick="' + HTMLName + 'CheckClick(' + IntToStr(Index) + ',false,true);"';

  if (pos('<INPUT', itemText) > 0) then
  begin

    if pos('width=', itemText) > 0 then
    begin
      itemText := StringReplace(itemText, 'width="', 'style="width:', [rfIgnoreCase]);

      if pos('" color=', itemText) > 0 then
        itemText := StringReplace(itemText, '" color="', ';background-color:', [rfIgnoreCase]);
    end
    else if pos('color=', itemText) > 0 then
        itemText := StringReplace(itemText, 'color="', 'background-color:', [rfIgnoreCase]);

    if Assigned(Values) then
    begin
      if Values.Count > 0 then
        itemValue := StringReplace(Values.Strings[Index], '"', '''', [rfReplaceAll])
      else
        itemValue := EmptyStr;
    end;

    if Enabled and Editable and (EnabledItems.Items[Index] = 1) then
      itemText := '<SPAN ' + onClick + '>' + itemText;

    itemText := StringReplace(itemText, '<INPUT', '</SPAN><INPUT VALUE="' + itemValue
      + '" ' + onInputClick + ' ONBLUR="' + HTMLName + 'SetCustomValues()" TYPE="TEXT" ID="' + HTMLName + 'VAL' + IntToStr(Index) + '"', [rfIgnoreCase]);
  end
  else
  begin
    if Enabled and Editable and (EnabledItems.Items[Index] = 1) then
      itemText := '<SPAN ' + onClick + '>' + itemText + '</SPAN>'
  end;

  Result := itemText;
end;

procedure TTIWAdvCheckGroup.SetCaptionFont(const Value: TIWFont);
begin
  FCaptionFont.Assign(Value);
  Changed;
end;

procedure TTIWAdvCheckGroup.SetFont(const Value: TIWFont);
begin
  FFont.Assign(Value);
  Changed;
end;

procedure TTIWAdvCheckGroup.SetItems(const Value: TStringList);
begin
  FItems.Assign(Value);
end;

procedure TTIWAdvCheckGroup.SetValue(const AValue: string);
var
  i, vp: integer;
  VarVal, tempval, su: string;
begin

  //OutputDebugString(pchar(AValue));

  if pos('|', AValue) > 0 then
  begin
    VarVal := AValue;

    //Retrieve CheckBox Indexes
    vp := pos('|', VarVal);
    if vp > 0 then
    begin
      su := copy(VarVal, 1, vp - 1);
      Delete(VarVal, 1, vp);

      for i := 1 to Length(su) do
      begin
        tempval := su[i];
        FChecked.Items[i - 1] := StrToInt(tempval);
      end;
    end;

    //Retrieve custom values
    vp := pos('^', VarVal);
    if vp > 0 then
    begin
       Values.Clear();
       VarVal := VarVal + '^';

       while pos('^', VarVal) > 0 do
       begin
         vp := pos('^', VarVal);
         su := copy(VarVal, 1, vp - 1);
         Delete(VarVal, 1, vp);

//         OutputDebugString(pchar('add value: ' + su));
         Values.Add(su);
       end;       

    end;

  end
  else
  begin

      if (Length(AValue) > 0) then
      begin
        for i := 1 to Length(AValue) do
        begin
          tempval := AValue[i];
          FChecked.Items[i - 1] := StrToInt(tempval);
        end;
      end;

  end;
end;

procedure TTIWAdvCheckGroup.SetValues(const Value: TStringList);
begin
  FValues.Assign(Value);
end;

{ TIntList }

constructor TIntList.Create;
begin
  inherited Create;
end;

procedure TIntList.SetInteger(Index: Integer; Value: Integer);
begin
  if Index < 0 then
    Exit;

  while Index >= Count do
  begin
    Add(1);
  end;

  inherited Items[Index] := Pointer(Value);
  if Assigned(OnChange) then
    OnChange(Self);
end;

function TIntList.GetInteger(Index: Integer): Integer;
begin
  Result := 0;
  if Index < 0 then
    Exit;

  while Index >= Count do
  begin
    Add(1);
  end;

  Result := Integer(inherited Items[Index]);
end;

procedure TIntList.Add(Value: Integer);
begin
  inherited Add(Pointer(Value));
  if Assigned(OnChange) then
    OnChange(Self);
end;

procedure TIntList.Delete(Index: Integer);
begin
  inherited Delete(Index);
  if Assigned(OnChange) then
    OnChange(Self);
end;

function TIntList.GetStrValue: string;
var
  i: integer;
begin
  for i := 1 to Count do
    if i = 1 then
      Result := IntToStr(Items[i - 1])
    else
      Result := Result + ',' + IntToStr(Items[i - 1]);
end;

procedure TIntList.SetStrValue(const Value: string);
var
  sl: TStringList;
  i: Integer;
begin
  sl := TStringList.Create;
  sl.CommaText := Value;
  Clear;
  for i := 1 to sl.Count do
    Add(StrToInt(sl.Strings[i - 1]));
  sl.Free;
end;

end.
