{***************************************************************************}
{ 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 IWDBAdvRadioGroup;

interface

{$I TMSDEFS.INC}

uses
  {$IFNDEF LINUX}
  Windows,
  {$ENDIF}
  IWHTMLTag, DB, IWDBStdCtrls, Classes, IWTMSBase, IWAdvRadioGroup, SysUtils
  {$IFDEF TMSIW51}
  , IWDBCommon
  {$ENDIF}
  {$IFDEF TMSIW6}
  , IWRenderContext
  {$ENDIF}
  ;

type
  TTIWDataMapModeType = (dmIndex, dmBinary);

  TTIWDBAdvRadioGroup = class(TTIWAdvRadioGroup)
  private
    FDataField: string;
    FDataSource: TDataSource;
    FDataMapMode: TTIWDataMapModeType;    
  public
    {$IFDEF TMSIW6}
    procedure Notification(AComponent: TComponent; AOperation: TOperation); override;
    procedure SetValue(const AValue: string); override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
  published
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write FDataSource;
    property DataMapMode: TTIWDataMapModeType read FDataMapMode write FDataMapMode default dmIndex;    
  end;

implementation

{ TTIWDBAdvRadioGroup }

{$IFDEF TMSIW6}
function TTIWDBAdvRadioGroup.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWDBAdvRadioGroup.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  LField: TField;
  i, binVal: integer;
  checkedItem: string;
begin

  if CheckDataSource(DataSource, DataField, LField) then
  begin
    //Editable := InEditMode(DataSource.Dataset);

    if DataMapMode = dmIndex then
      ItemIndex := LField.AsInteger
    else
    begin
      checkedItem := LField.AsString;                                                                 
      if (checkedItem = EmptyStr) or (checkedItem = '0') then
        ItemIndex := -1
      else
      begin
        binVal := 1;

        for I := 0 to Items.Count - 1 do
        begin
         if StrToInt(checkedItem) and binVal = binVal then
            ItemIndex := i;

          binVal := binVal * 2;
        end;
      end;
    end;

  end
  else
  begin
    ItemIndex := -1;
  end;                                                    

  {$IFDEF TMSIW6}
  Result := inherited RenderHTML(AContext);
  {$ELSE}
  Result := inherited RenderHTML;
  {$ENDIF}
end;

procedure TTIWDBAdvRadioGroup.Notification(AComponent: TComponent; AOperation: TOperation);
begin
  inherited Notification(AComponent, AOperation);
  if AOperation = opRemove then begin
    if FDatasource = AComponent then begin
      FDatasource := nil;
    end;
  end;
end;

procedure TTIWDBAdvRadioGroup.SetValue(const AValue: string);
var
  LField: TField;
  i, binVal, tempVal: integer;
begin
  inherited SetValue(AValue);
  if CheckDataSource(DataSource, DataField, LField) and Editable then
  begin                      
    Datasource.Edit;


    if DataMapMode = dmIndex then
      LField.AsInteger := StrToInt(AValue)
    else
    begin
      binVal := 1;
      tempVal := 0;

      for I := 0 to Items.Count - 1 do
      begin
//      if Checked.Items[i] = 0 then
        if ItemIndex = i then
          tempVal := tempVal + binVal;
          
        binVal := binVal * 2;
      end;

      LField.AsInteger := tempVal;

    end;

    DataSource.DataSet.Post;
  end;
end;

end.
