{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2007                                        }
{            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 IWColorComboBox;

interface

{$I TMSDEFS.INC}
uses
  {$IFDEF LINUX}
  QControls, QGraphics, QIWPicCntnr,
  {$ELSE}
  Controls, Windows, Graphics, IWPicCntnr, IWTMSImg,
  {$ENDIF}
  {$IFDEF DELPHI6_LVL}
  Types,
  {$ENDIF}
  {$IFDEF TMSIW51}
  IWColor,
  {$ENDIF}
  Classes,
  IWControl, IWTypes, SysUtils, IWHTMLTag, IWExtCtrls, IWTMSBase, IWFont, IWCompListBox
  {$IFDEF TMSIW6}
  ,IWRenderContext
  {$ENDIF}
  ;
type
  TIWComboBoxColor = class;
  TTIWColorComboBox = class;

  TIWComboBoxColor = class(TCollectionItem)
  private
    FClr : TIWColor;
    FColorName : string;
    FTextColor : TIWColor;
    procedure SetColor(const Value : TIWColor);
    procedure SetTextColor(const Value : TIWColor);
    procedure SetColorName(const Value : string);
    function GetColorComboBox: TTIWColorComboBox;
  protected
    function GetDisplayName: string; override;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
    property ColorComboBox: TTIWColorComboBox read GetColorComboBox;
  published
    property Color : TIWColor read FClr write SetColor;
    property ColorName : string read FColorName write SetColorName;
    property TextColor : TIWColor read FTextColor write SetTextColor;
  end;

  TIWComboBoxColors = class(TOwnedCollection)
  private
    FOwner: TTIWColorComboBox;
    function GetColor(Index: integer): TIWComboBoxColor;
    procedure SetColor(Index: integer; const Value: TIWComboBoxColor);

  protected
  public
    function CreateItemClass: TCollectionItemClass; virtual;
    constructor Create(AOwner: TTIWColorComboBox);
    function Add: TIWComboBoxColor;
    property Colors[index: integer]: TIWComboBoxColor read GetColor write SetColor; default;
  published
  end;

  TTIWColorComboBox = class(TIWCustomComboBox)
  private
    FFont : TIWFont;
    FColors : TIWComboBoxColors;
    FShowColorNames : Boolean;
    FSelectedIndex : Integer;
    procedure SetShowColorNames(const Value : Boolean);
    procedure SetSelectedIndex(const Value : Integer);
    procedure SetFont(const  Value : TIWFont);
    procedure SetColors(const Value : TIWComboBoxColors);
  protected
    {$IFDEF TMSIW6}
    procedure IWPaint; override;
    {$ELSE}
    procedure Paint; override;
    {$ENDIF}
    procedure Submit(const AValue: string); override;
    procedure SetValue(const Avalue: string); override;    
  public
    {$IFDEF TMSIW6}
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
    constructor Create(AOwner: TComponent); override;
    function CreateColors: TIWComboBoxColors; virtual;
    destructor Destroy; override;
  published
    property Font : TIWFont read FFont write SetFont;
    property Colors : TIWComboBoxColors read FColors write SetColors;
    property SelectedIndex : Integer read FSelectedIndex write SetSelectedIndex;
    property ShowColorNames : Boolean read FShowColorNames write SetShowColorNames;
  end;

implementation

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


{ TTIWColorComboBox }

constructor TTIWColorComboBox.Create(AOwner: TComponent);
begin
  inherited;
  FFont := TIWFont.Create;
  FSelectedIndex := -1;
  Self.Width := 120;
  Self.Height := 25;
  FShowColorNames := true;
  FColors := CreateColors;
  FColors.Add;
  FColors[0].Color := clBlack;
  FColors[0].ColorName := 'Black';
  FColors[0].TextColor := clWhite;
  FColors.Add;
  FColors[1].Color := clMaroon;
  FColors[1].ColorName := 'Maroon';
  FColors[1].TextColor := clWhite;
  FColors.Add;
  FColors[2].Color := clGreen;
  FColors[2].ColorName := 'Green';
  FColors[2].TextColor := clWhite;
  FColors.Add;
  FColors[3].Color := clOlive;
  FColors[3].ColorName := 'Olive';
  FColors[3].TextColor := clBlack;
  FColors.Add;
  FColors[4].Color := clNavy;
  FColors[4].ColorName := 'Navy';
  FColors[4].TextColor := clWhite;
  FColors.Add;
  FColors[5].Color := clPurple;
  FColors[5].ColorName := 'Purple';
  FColors[5].TextColor := clWhite;
  FColors.Add;
  FColors[6].Color := clTeal;
  FColors[6].ColorName := 'Teal';
  FColors[6].TextColor := clWhite;
  FColors.Add;
  FColors[7].Color := clSilver;
  FColors[7].ColorName := 'Silver';
  FColors[7].TextColor := clBlack;
  FColors.Add;
  FColors[8].Color := clGray;
  FColors[8].ColorName := 'Gray';
  FColors[8].TextColor := clWhite;
  FColors.Add;
  FColors[9].Color := clRed;
  FColors[9].ColorName := 'Red';
  FColors[9].TextColor := clBlack;
  FColors.Add;
  FColors[10].Color := clLime;
  FColors[10].ColorName := 'Lime';
  FColors[10].TextColor := clBlack;
  FColors.Add;
  FColors[11].Color := clYellow;
  FColors[11].ColorName := 'Yellow';
  FColors[11].TextColor := clBlack;
  FColors.Add;
  FColors[12].Color := clBlue;
  FColors[12].ColorName := 'Blue';
  FColors[12].TextColor := clBlack;
  FColors.Add;
  FColors[13].Color := clFuchsia;
  FColors[13].ColorName := 'Fuchsia';
  FColors[13].TextColor := clBlack;
  FColors.Add;
  FColors[14].Color := clAqua;
  FColors[14].ColorName := 'Aqua';
  FColors[14].TextColor := clBlack;
  FColors.Add;
  FColors[15].Color := clWhite;
  FColors[15].ColorName := 'White';
  FColors[15].TextColor := clBlack;
end;

destructor TTIWColorComboBox.Destroy;
begin
  FFont.Free;
  FColors.Free;
  inherited;
end;

function TTIWColorComboBox.CreateColors: TIWComboBoxColors;
begin
  Result := TIWComboBoxColors.Create(Self);
end;

{$IFDEF TMSIW6}
procedure TTIWColorComboBox.IWPaint;
{$ELSE}
procedure TTIWColorComboBox.Paint;
{$ENDIF}
var
  R : TRect;
  RArrow : TRect;
begin
  r := GetClientRect;

  Canvas.Brush.Color := clWhite;
  Canvas.Pen.Color := clGray;
  Canvas.Rectangle(R);

  R.Left := R.Left + 2;
  R.Right := R.Right - 19;
  R.Top := R.Top + 2;
  R.Bottom := R.Bottom - 2;
  Canvas.Rectangle(R);

  RArrow.Left := R.Right + 2;
  RArrow.Right := R.Right + 17;
  RArrow.Top := R.Top;
  RArrow.Bottom := R.Bottom;

  Canvas.Brush.Color := clBtnFace;
  Canvas.Rectangle(RArrow);

  Canvas.Pen.Color := clBlack;
  Canvas.MoveTo(RArrow.Left + (RArrow.Right - RArrow.Left) div 2 - 3, (RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top - 2);
  Canvas.LineTo(RArrow.Right - (RArrow.Right - RArrow.Left) div 2 + 3,(RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top - 2);
  Canvas.MoveTo(RArrow.Left + (RArrow.Right - RArrow.Left) div 2 - 2, (RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top - 1);
  Canvas.LineTo(RArrow.Right - (RArrow.Right - RArrow.Left) div 2 + 2,(RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top - 1);
  Canvas.MoveTo(RArrow.Left + (RArrow.Right - RArrow.Left) div 2 - 1, (RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top);
  Canvas.LineTo(RArrow.Right - (RArrow.Right - RArrow.Left) div 2 + 1,(RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top);
  Canvas.MoveTo(RArrow.Left + (RArrow.Right - RArrow.Left) div 2, (RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top + 1);
  Canvas.LineTo(RArrow.Right - (RArrow.Right - RArrow.Left) div 2,(RArrow.Bottom - RArrow.Top) div 2 + RArrow.Top + 1);

  Canvas.Pen.Color := clWhite;
  Canvas.MoveTo(RArrow.Left, RArrow.Top);
  Canvas.LineTo(RArrow.Right,RArrow.Top);
  Canvas.MoveTo(RArrow.Left, RArrow.Top);
  Canvas.LineTo(RArrow.Left, RArrow.Bottom);

  R := GetClientRect();
  Canvas.Pen.Color := clBlack;
  Canvas.MoveTo(R.Left + 1,R.Top + 1);
  Canvas.LineTo(R.Right - 1,r.top + 1);
  Canvas.MoveTo(R.Left + 1,R.top + 1);
  Canvas.LineTo(R.Left + 1,R.Bottom - 1);
end;

{$IFDEF TMSIW6}
function TTIWColorComboBox.RenderHTML(
  AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWColorComboBox.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  html : string;
  i : integer;
  TextColor : string;
  StrSelected : string;
  Width : string;
begin
  html := '';
  {$IFDEF TMSIW6}
  TIWComponent40Context(AContext).AddToInitProc('FindElem(''' + HTMLName + ''').value="' + IntToStr(SelectedIndex) + '";' + EOL);
  {$ELSE}
  TIWAppForm(Form).AddToInitProc('FindElem(''' + HTMLName + ''').value="' + IntToStr(SelectedIndex) + '";' + EOL);
  {$ENDIF}

  //Modified 09/30/2003
    html := html
    + '<script>'#13
    + '  function ' + HTMLName + 'UpdateSelIndex(name) {'#13
    + '    FindElem(''' + HTMLName + ''').value = document.getElementById(name).selectedIndex;'#13
    +    IIF(Assigned(OnChange),' return SubmitClick('#39 + HTMLName + #39 + ','#39'false'#39');'#13,'')
    + '  }'#13
    + '</script>'#13;
{
  html := html
    + '<script>'#13
    + '  function ' + HTMLName + 'UpdateSelIndex(name) {'#13
    + '    FindElem(''' + HTMLName + ''').value = document.getElementById(name).selectedIndex;'#13
    + '  }{'#13
    + '</script>'#13;
}
  //End Modified

  if (ShowColorNames) then
  begin
    Width := '';
    html := html
      + '<select onchange="' + HTMLName + 'UpdateSelIndex(''' + HTMLName + 'sel'')" id="' + HTMLName + 'sel">';
  end
  else
  begin
    Width := ' width:' + IntToStr(Self.Width) + 'px;';
    html := html
      + '<select onchange="' + HTMLName + 'UpdateSelIndex(''' + HTMLName + 'sel'')" id="' + HTMLName + 'sel" style="width:' + IntToStr(self.Width) + 'px">';
  end;

  for i:=0 to FColors.Count - 1 do
  begin
    if (ShowColorNames) then
      TextColor := FColors[i].ColorName
    else
      TextColor := '';

    if (i = SelectedIndex) then
      StrSelected := 'selected="selected"'
    else
      StrSelected := '';

    html := html
      + '<option ' + StrSelected + ' value="' + FColors[i].FColorName + '" style="' + Width + 'color:'
      + ColorToRGBString(FColors[i].FTextColor) + ';background-color: '
      + ColorToRGBString(FColors[i].Color) + '">'
      + TextColor + '</option>';
  end;
  html := html
    + '</select>';

  Result := TIWHTMLTag.CreateTag('DIV');
  Result.Contents.AddText(html);
  with Result.Contents.AddTag('INPUT') do
  begin
    AddStringParam('TYPE','hidden');
    AddStringParam('NAME',HTMLName);
    AddStringParam('ID',HTMLName+'_INPUT');
  end;
end;

procedure TTIWColorComboBox.SetValue(const AValue : string);
begin
  FSelectedIndex := StrToInt(AValue);
  if (FSelectedIndex = -1) and (FColors.Count > 0) then
    FSelectedIndex := 0;
end;

procedure TTIWColorComboBox.SetColors(const Value : TIWComboBoxColors);
begin
  FColors := Value;
end;

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

procedure TTIWColorComboBox.SetShowColorNames(const Value : Boolean);
begin
  FShowColorNames := Value;
end;

procedure TTIWColorComboBox.SetSelectedIndex(const Value : Integer);
begin
  if (Value < -1) then
    FSelectedIndex := -1
  else
  begin
    if (Value < FColors.Count) then
      FSelectedIndex := Value;
  end;
end;

procedure TTIWColorComboBox.Submit(const AValue: string);
begin
  inherited;

end;

{ TIWColorComboBoxColor }
procedure TIWComboBoxColor.Assign(Source: TPersistent);
begin
  ColorName := (Source as TIWComboBoxColor).ColorName;
  Color := (Source as TIWComboBoxColor).Color;
end;

constructor TIWComboBoxColor.Create(Collection: TCollection);
begin
  inherited;
  FClr := clWhite;
  FColorName := 'White';
  FTextColor := clBlack;
end;

destructor TIWComboBoxColor.Destroy;
begin
  inherited;
end;

function TIWComboBoxColor.GetColorComboBox: TTIWColorComboBox;
begin
  Result := TIWComboBoxColors(Collection).FOwner;
end;

function TIWComboBoxColor.GetDisplayName: string;
begin
  if ColorName <> '' then
    Result := ColorName
  else
    Result := 'TIWComboBoxColor' + IntToStr(Index);
end;

procedure TIWComboBoxColor.SetColor(const Value: TIWColor);
begin
  FClr := Value;
end;

procedure TIWComboBoxColor.SetColorName(const Value: string);
begin
  FColorName := Value;
end;

procedure TIWComboBoxColor.SetTextColor(const Value : TIWColor);
begin
  FTextColor := Value;
end;

{ TIWColorComboColors}
function TIWComboBoxColors.Add: TIWComboBoxColor;
begin
  Result := TIWComboBoxColor(inherited Add);
end;

constructor TIWComboBoxColors.Create(AOwner: TTIWColorComboBox);
begin
  inherited Create(AOwner, CreateItemClass);
  FOwner := AOwner;
end;

function TIWComboBoxColors.CreateItemClass: TCollectionItemClass;
begin
  Result := TIWComboBoxColor;
end;

function TIWComboBoxColors.GetColor(Index: integer): TIWComboBoxColor;
begin
  Result := TIWComboBoxColor(inherited Items[index]);
end;

procedure TIWComboBoxColors.SetColor(Index: integer;
  const Value: TIWComboBoxColor);
begin
  Items[Index] := Value;
end;

end.
