{***************************************************************************}
{ 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 IWHTMLEdit;
                       
{$I TMSDEFS.INC}                           

interface
{$R tmsiwhtmled.res}                                              

uses                                                  
   {$IFDEF LINUX}
  QControls, QGraphics, QIWPicCntnr,
  {$ELSE}
  Controls, Windows, Graphics, IWPicCntnr,
  {$ENDIF}
  {$IFDEF DELPHI6_LVL}
  Types,
  {$ENDIF}
  Classes,
  IWControl, IWTypes, SysUtils, IWHTMLTag,
  IWCompEdit, IWCompLabel, IWCompCheckbox, IWExtCtrls,
  IWTMSBase, IWTMSImg, IWFont
  {$IFDEF TMSIW6}
  ,IWRenderContext, IWColor
  {$ENDIF}
  ;

type

  TTIWComboboxes = class(TPersistent)
  private
    FOwner: TComponent;
    FBackColors: Boolean;
    FParagraph: Boolean;
    FFontNames: Boolean;
    FFontSizes: Boolean;
    FFontColors: Boolean;
    procedure SetBackColors(const Value: Boolean);    
    procedure SetParagraph(const Value: Boolean);
    procedure SetFontNames(const Value: Boolean);
    procedure SetFontSizes(const Value: Boolean);
    procedure SetFontColors(const Value: Boolean);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
  published
    property BackColors: Boolean read FBackColors write SetBackColors;
    property Paragraph: Boolean read FParagraph write SetParagraph;
    property FontNames: Boolean read FFontNames write SetFontNames;
    property FontSizes: Boolean read FFontSizes write SetFontSizes;
    property FontColors: Boolean read FFontColors write SetFontColors;
  end;

  TTIWHTMLEdGlyphs = class(TPersistent)
  private
    FOwner: TComponent;
    FBold: Boolean;
    FItalic: Boolean;
    FUnderline: Boolean;
    FStrike: Boolean;
    FSubscript: Boolean;
    FSuperscript: Boolean;
    FLeft: Boolean;
    FCenter: Boolean;
    FRight: Boolean;
    FUnorderedList: Boolean;
    FOrderedList: Boolean;
    FIndent: Boolean;
    FUnIndent: Boolean;
    FCut: Boolean;
    FCopy: Boolean;
    FPaste: Boolean;
    FNew: Boolean;
    FLink: Boolean;
    FInsertImage : Boolean;
    FInsertHorizontalRule: Boolean;
    procedure SetBold(const Value: Boolean);
    procedure SetItalic(const Value: Boolean);
    procedure SetUnderline(const Value: Boolean);
    procedure SetStrike(const Value: Boolean);
    procedure SetSubscript(const Value: Boolean);
    procedure SetSuperscript(const Value: Boolean);
    procedure SetLeft(const Value: Boolean);
    procedure SetCenter(const Value: Boolean);
    procedure SetRight(const Value: Boolean);
    procedure SetUnorderedList(const Value: Boolean);
    procedure SetOrderedList(const Value: Boolean);
    procedure SetIndent(const Value: Boolean);
    procedure SetUnindent(const Value: Boolean);
    procedure SetCut(const Value: Boolean);
    procedure SetCopy(const Value: Boolean);
    procedure SetNew(const Value: Boolean);
    procedure SetPaste(const Value: Boolean);
    procedure SetLink(const Value: Boolean);
    procedure SetInsertImage(const Value : Boolean);
    procedure SetInsertHorizontalRule(const Value : Boolean);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
  published
    property Bold: Boolean read FBold write SetBold default true;
    property Center: Boolean read FCenter write SetCenter default true;
    property Copy: Boolean read FCopy write SetCopy default true;
    property Cut: Boolean read FCut write SetCut default true;
    property Indent: Boolean read FIndent write SetIndent default true;
    property InsertImage : Boolean read FInsertImage write SetInsertImage default true;
    property InsertHorizontalRule : Boolean read FInsertHorizontalRule write SetInsertHorizontalRule default true;
    property Italic: Boolean read FItalic write SetItalic default true;
    property Left: Boolean read FLeft write SetLeft default true;
    property Link: Boolean read FLink write SetLink default true;
    property New: Boolean read FNew write SetNew default true;
    property OrderedList: Boolean read FOrderedList write SetOrderedList default true;
    property Paste: Boolean read FPaste write SetPaste default true;
    property Right: Boolean read FRight write SetRight default true;
    property Strike: Boolean read FStrike write SetStrike default true;
    property Subscript: Boolean read FSubscript write SetSubscript default true;
    property Superscript: Boolean read FSuperscript write SetSuperscript default true;
    property Underline: Boolean read FUnderline write SetUnderline default true;
    property Unindent: Boolean read FUnindent write SetUnindent default true;
    property UnorderedList: Boolean read FUnorderedList write SetUnorderedList default true;
  end;

  TTIWScrollType = (stAuto, stShow, stHidden);

  TTIWCustomHTMLEdit = class(TIWControl)
  private
    FBackground: TColor;
    FComboboxes: TTIWComboboxes;
    FContentEditable: boolean;
    FFont: TIWFont;
    FFontList: TStrings;
    FGlyphs: TTIWHTMLEdGlyphs;
    FLines: TStrings;
    FShowToolBar: Boolean;
    FShowComboboxes: Boolean;
    FViewHTML: boolean;
    FScroll: TTIWScrollType;
    procedure FontChanged(Sender: TObject);
    procedure SetBackground(const AValue: TColor);
    procedure SetComboboxes(const AValue: TTIWComboboxes);
    procedure SetContentEditable(const AValue: boolean);
    procedure SetFont(const AValue: TIWFont);
    procedure SetFontList(const AValue: TStrings);
    procedure SetGlyphs(const Value: TTIWHTMLEdGlyphs);
    procedure SetLines(const AValue: TStrings);
    procedure SetShowToolBar(const AValue: Boolean);
    procedure SetShowComboboxes(const AValue: Boolean);
  protected
    procedure SetValue(const AValue: string); override;
    function HTMLClr(color: TColor): string;
    function GetFonts(fonts: TStrings): string;
  public
    {$IFDEF TMSIW6}
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    procedure Paint; override;
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property BackgroundColor: TColor read FBackground write SetBackground;
    property ComboBoxes: TTIWComboboxes read FComboboxes write SetComboboxes;
    property ContentEditable: Boolean read FContentEditable write SetContentEditable;
    property Font: TIWFont read FFont write SetFont;
    property FontList: TStrings read FFontList write SetFontList;
    property Glyphs: TTIWHTMLEdGlyphs read FGlyphs write SetGlyphs;
    property Lines: TStrings read FLines write SetLines;
    property ShowComboBoxes: Boolean read FShowComboboxes write SetShowComboboxes default true;
    property ShowToolBar: Boolean read FShowToolBar write SetShowToolbar default true;
    property ViewHtml: boolean read FViewHTML write FViewHTML;
    property Scroll: TTIWScrollType read FScroll write FScroll default stAuto;
  end;

  TTIWHTMLEdit = class(TTIWCustomHTMLEdit)
  published
    property BackgroundColor;
    property ComboBoxes;
    property ContentEditable;
    property Font;
    property FontList;
    property Glyphs;
    property Lines;
    property ShowComboBoxes;
    property ShowToolBar;
    property Scroll;
  end;

implementation

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

{ TTIWHTMLEdGlyphs }

constructor TTIWHTMLEdGlyphs.Create(AOwner: TComponent);
begin
  inherited Create;
  FOwner := AOwner;
  FBold := true;
  FItalic := true;
  FUnderline := true;
  FStrike := true;
  FSubscript := true;
  FSuperscript := true;
  FLeft := true;
  FCenter := true;
  FRight := true;
  FUnorderedList := true;
  FOrderedList := true;
  FIndent := true;
  FUnindent := true;
  FCut := true;
  FCopy := true;
  FPaste := true;
  FNew := true;
  FLink := true;
  FInsertImage := true;
  FInsertHorizontalRule := true;
end;

destructor TTIWHTMLEdGlyphs.Destroy;
begin
  inherited;
end;

procedure TTIWHTMLEdGlyphs.SetBold(const Value: Boolean);
begin
  FBold := Value;
end;

procedure TTIWHTMLEdGlyphs.SetCenter(const Value: Boolean);
begin
  FCenter := Value;
end;

procedure TTIWHTMLEdGlyphs.SetCopy(const Value: Boolean);
begin
  FCopy := Value;
end;

procedure TTIWHTMLEdGlyphs.SetCut(const Value: Boolean);
begin
  FCut := Value;
end;

procedure TTIWHTMLEdGlyphs.SetIndent(const Value: Boolean);
begin
  FIndent := Value;
end;

procedure TTIWHTMLEdGlyphs.SetInsertImage(const Value : Boolean);
begin
  FInsertImage := Value;
end;

procedure TTIWHTMLEdGlyphs.SetInsertHorizontalRule(const Value : Boolean);
begin
  FInsertHorizontalRule := Value;
end;

procedure TTIWHTMLEdGlyphs.SetItalic(const Value: Boolean);
begin
  FItalic := Value;
end;
                                  
procedure TTIWHTMLEdGlyphs.SetLeft(const Value: Boolean);
begin
  FLeft := Value;
end;

procedure TTIWHTMLEdGlyphs.SetLink(const Value: Boolean);
begin
  FLink := Value;
end;

procedure TTIWHTMLEdGlyphs.SetNew(const Value: Boolean);
begin
  FNew := Value;
end;

procedure TTIWHTMLEdGlyphs.SetOrderedList(const Value: Boolean);
begin
  FOrderedList := Value;
end;

procedure TTIWHTMLEdGlyphs.SetPaste(const Value: Boolean);
begin
  FPaste := Value;
end;

procedure TTIWHTMLEdGlyphs.SetRight(const Value: Boolean);
begin
  FRight := Value;
end;

procedure TTIWHTMLEdGlyphs.SetStrike(const Value: Boolean);
begin
  FStrike := Value;
end;

procedure TTIWHTMLEdGlyphs.SetSubscript(const Value: Boolean);
begin
  FSubscript := Value;
end;

procedure TTIWHTMLEdGlyphs.SetSuperscript(const Value: Boolean);
begin
  FSuperscript := Value;
end;

procedure TTIWHTMLEdGlyphs.SetUnderline(const Value: Boolean);
begin
  FUnderline := Value;
end;

procedure TTIWHTMLEdGlyphs.SetUnindent(const Value: Boolean);
begin
  FUnindent := Value;
end;

procedure TTIWHTMLEdGlyphs.SetUnorderedList(const Value: Boolean);
begin
  FUnorderedList := Value;
end;

{ TTIWCustomHTMLEdit }

constructor TTIWCustomHTMLEdit.Create(AOwner: TComponent);
begin
  inherited;
  FComboboxes := TTIWComboboxes.Create(Self);
  FGlyphs := TTIWHTMLEdGlyphs.Create(Self);
  FLines := TStringList.Create;
  FFontList := TStringList.Create;
  {$IFDEF TMSIW6}
  SetRenderSize(True);
  {TODO}
  {$ELSE}
  FRenderSize := True;
  FSupportsInput := True;
  FSupportsSubmit := True;
  FEditable := True;
  FDesignMode := True;
  {$ENDIF}
  FFont := TIWFont.Create;
  FFont.OnChange := FontChanged;
  Font.Color := clBlack;
  FBackground := clWhite;
  Font.FontName := 'Arial';
  Font.Size := 12;
  FLines.Add('This is an example of a HTML editor.');
  FViewHtml := false;
  FContentEditable := true;
  FFontList.Add('Arial');
  FFontList.Add('Comic Sans MS');
  FFontList.Add('Courier New');
  FFontList.Add('Lucida Console');
  FFontList.Add('Tahoma');
  FFontList.Add('Times New Roman');
  FFontList.Add('Trebuchet MS');
  FFontList.Add('Verdana');
  Height := 400;
  Width := 600;
  FShowToolBar := true;
  FShowComboBoxes := true;
  FScroll := stAuto;
end;

destructor TTIWCustomHTMLEdit.Destroy;
begin
  FComboboxes.Free;
  FGlyphs.Free;
  FreeAndNil(FFont);
  FreeAndNil(FLines);
  FreeAndNil(FFontList);
  inherited;
end;

procedure TTIWCustomHTMLEdit.FontChanged(Sender: TObject);
begin
  Invalidate;
end;

procedure TTIWCustomHTMLEdit.SetShowToolBar(const AValue: Boolean);
begin
  FShowToolBar := AValue;
  Invalidate;
end;

procedure TTIWCustomHTMLEdit.SetShowComboboxes(const AValue: Boolean);
begin
  FShowComboboxes := AValue;
  Invalidate;
end;

procedure TTIWCustomHTMLEdit.SetContentEditable(const AValue: boolean);
begin
  FContentEditable := AValue;
end;

{$IFDEF TMSIW6}
procedure TTIWCustomHTMLEdit.IWPaint;
{$ELSE}
procedure TTIWCustomHTMLEdit.Paint;
{$ENDIF}
var
  r: TRect;
  down,i: integer;
begin
  Canvas.Font.Name := Font.FontName;
  Canvas.Font.Size := Font.Size;
  Canvas.Font.Color := Font.Color;
  Canvas.Font.Style := Font.Style;

  with Canvas do
  begin
    r := GetClientRect;
    Brush.Color := FBackground;
    Rectangle(r);
    if (FShowToolBar) then
    begin
      TextOut(r.Left+5, r.Top+10,'Toolbar');
      r.top := r.top + 40;
    end;
    MoveTo(r.Left,r.Top);
    LineTo(r.Right,r.Top);
    if (FShowComboboxes) then
    begin
      TextOut(r.Left+5, r.Top+10,'Comboboxes');
      r.top := r.top + 40;
    end;
    MoveTo(r.Left,r.Top);
    LineTo(r.Right,r.Top);
    TextOut(r.Left+5, r.Top+10,'HTMLEditor:');
    if (FLines.Count > 0) then
    begin
      down := 50;
      i := 0;
      while (i < FLines.Count) do
      begin
        TextOut(r.Left + 25, r.Top + down, FLines.Strings[i]);
        down := down + 20;
        i := i + 1;
      end;
    end;
  end;
end;

function TTIWCustomHTMLEdit.HTMLClr(color: TColor):string;
begin
  Result := ColorToRGBString(color);
end;

function TTIWCustomHTMLEdit.GetFonts(fonts: TStrings): string;
var
  i: integer;
begin
  for i:=0 to fonts.Count-1 do
  begin
    if not (fonts.Strings[i] = '') then
      Result := Result + '<option value="' + fonts.Strings[i] + '">' + fonts.Strings[i] + '</option>';
  end;
end;

procedure TTIWCustomHTMLEdit.SetLines(const AValue:TStrings);
begin
  FLines.Assign(AValue);
  Invalidate;
end;

procedure TTIWCustomHTMLEdit.SetFont(const AValue: TIWFont);
begin
  FFont.Assign(AValue);
  Invalidate;
end;

procedure TTIWCustomHTMLEdit.SetFontList(const AValue: TStrings);
begin
  FFontList.Assign(AValue);
end;

procedure TTIWCustomHtmlEdit.SetComboboxes(const Avalue: TTIWComboboxes);
begin
  FComboboxes.Assign(AValue);
end;

procedure TTIWCustomHTMLEdit.SetBackground(const AValue: TColor);
begin
  FBackground := AValue;
  Invalidate;
end;

procedure TTIWCustomHTMLEdit.SetValue(const AValue:string);
begin
//  FLines.Text := StringReplace(AValue,'<BR>',#13,[rfReplaceAll,rfIgnoreCase]);
  FLines.Text := AValue;


end;

function JSAdapt(s:string):string;
var
  i: integer;
begin
  Result := '';
  for i := 1 to length(s) do
  begin
    case s[i] of
    #39:Result := Result + '\'#39;
    '"':Result := Result + '\"';
    '\':Result := Result + '\\'
    else
      Result := Result + s[i];
    end

    {
    if s[i] = #39 then
      Result := Result + '\'#39
    else if s[i] = '"' then
           Result := Result + '\"'
         else
           Result := Result + s[i];
    }
  end;
end;

{$IFDEF TMSIW6}
function TTIWCustomHTMLEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWCustomHTMLEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  html: string;
  ImgName: string;
  initialText,memotext,nline,HeightText, ceditable, dMode: string;
  BoldB, BoldE, ItalicB, ItalicE, UnderlineB, UnderlineE, StrikeB, StrikeE: string;
  i: integer;

begin
  ImgName := '';

  initialText := '';
  for i := 1 to Flines.Count do
  begin
    nline := JSAdapt(Lines.Strings[i - 1]);

//    if (Pos('\',nline) > 0) then
//      outputdebugstring(pchar(nline));

    InitialText := InitialText + nline;
    if (i < FLines.Count) and (Pos('</P>',nline) = 0) and
       (Pos('<BR>',nline) = 0) and (Pos('</LI>',nline) = 0) and (Pos('<LI>',nline) = 0) and (Pos('<UL>',nline) = 0) and (Pos('<OL>',nline) = 0) and (Pos('<HR',nline) = 0) and (Pos('</H>',nline) = 0) then
      InitialText := InitialText + '<BR>';
  end;

  {$IFDEF TMSIW6}
  TIWComponent40Context(AContext).AddToInitProc('FindElem(''' + HTMLName + ''').value="'+ InitialText + '";');
  {$ELSE}
  TIWAppForm(Form).AddToInitProc('FindElem(''' + HTMLName + ''').value="'+ InitialText + '";');
  {$ENDIF}

			//Define height of textarea, depending on the visibility of the comboboxes and the toolbar.
			if ((ShowToolBar) and (ShowComboBoxes)) then
				HeightText := IntToStr(Height - 44)
			else
			begin
				if ((ShowToolBar) or (ShowComboBoxes)) then
					HeightText := IntToStr(Height - 22)
				else
					HeightText := IntToStr(Height);
			end;

			//If the property ContentEditable is false, the textarea isn't editable.  Default is true.
      if (ContentEditable) then
      begin
        ceditable := 'True';
        dMode := 'On';
      end
      else
      begin
        ceditable := 'False';
        dMode := 'Off';
      end;


      html := GetResourceText('TMSIWHTMLED');

    	html := StringReplace(html, '<#BGCOLOR>', HTMLClr(BackgroundColor),[rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, '<#FONTSIZE>', IntToStr(Font.Size),[rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, '<#FONTNAME>', Font.FontName ,[rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, '<#FONTCOLOR>', HTMLClr(Font.Color),[rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, '<#WIDTHTEXT>', IntToStr(Width) , [rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, '<#HEIGHTTEXT>', HeightText,[rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, 'GSubmitter.elements[''<#COMPNAME>''].value', 'FindElem(''' + HTMLName + ''').value', [rfReplaceAll, rfIgnoreCase]);
			html := StringReplace(html, '<#BORDERCOLOR>', '#000000',[rfReplaceAll, rfIgnoreCase]);
      html := StringReplace(html, '<#CONTENTEDITABLE>', ceditable,[rfReplaceAll, rfIgnoreCase]);
      html := StringReplace(html, '<#DESIGNMODE>', dMode,[rfReplaceAll, rfIgnoreCase]);

      html := StringReplace(html, '<#COMPNAME>', HTMLName,[rfReplaceAll, rfIgnoreCase]);

			//Set the checkbox "View HTML" visible or not.
			if (ViewHtml) then
				html := StringReplace(html, '<#DISPLAYHTML>', 'true',[rfReplaceAll, rfIgnoreCase])
			else
				html := StringReplace(html, '<#DISPLAYHTML>', 'false',[rfReplaceAll, rfIgnoreCase]);

  memotext := '';
  for i := 1 to lines.count do
  begin
    //nline := JSAdapt(Lines.Strings[i - 1]);
    nline := Lines.Strings[i - 1];
    if (i < FLines.Count) and (Pos('</P>',nline) = 0) and (Pos('<BR>',nline) = 0)
      and (Pos('<LI>',nline) = 0) and (Pos('</LI>',nline) = 0) and (Pos('</H>',nline) = 0) and (Pos('<HR',nline) = 0) and (Pos('<OL>',nline) = 0) and (Pos('<UL>',nline) = 0) then
       memotext := memotext + ' ' + nline + '<BR>'
    else
      memotext := memotext + ' ' + nline;
  end;

  			//Start html-code to render the Editor.
			html := html + #13'<span ID="' + HTMLName + 'myedit">'#13;
      html := html + '<body unselectable="on">'#13;
			html := html + '<table height="100%" cellspacing="0" cellpadding="0" width="' + IntToStr(Width) + '" border="0">'#13;
			html := html + '<tbody>'#13;

      //render Toolbar
			//If property ShowToolBar is true and at least one glyph is visible --> show toolbar.
			if ((ShowToolBar) and
      ((Glyphs.Bold) or (Glyphs.Italic) or (Glyphs.Underline) or (Glyphs.Strike)
      or (Glyphs.Subscript) or (Glyphs.Superscript) or (Glyphs.Left) or (Glyphs.Center)
      or (Glyphs.Right) or (Glyphs.UnorderedList) or (Glyphs.OrderedList)
      or (Glyphs.Indent) or (Glyphs.UnIndent) or (Glyphs.Cut) or (Glyphs.Copy)
      or (Glyphs.Paste) or (Glyphs.Link) or (Glyphs.InsertImage)
      or (Glyphs.InsertHorizontalRule))) then
			begin
				html := html + '<tr style="HEIGHT: 22px">'#13;
				html := html + '<td>'#13;
				html := html + '<table cellspacing="0" cellpadding="2" border="0">'#13;
				html := html + '<tbody>'#13;
				html := html + '<tr>'#13;

				//generate the glyphs where visibility is true
//				if (Glyphs.Clear)
//					html := html + '<td><img id="' + HTMLName + 'i0" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'ClearArea()" title="New" src="/Files/iwnew.gif"></td>'#13;
				if (Glyphs.Bold) then
					html := html + '<td><img id="' + HTMLName + 'i1" class="'  + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''bold'')" title="Bold" src="'+AContext.WebApplication.AppURLBase+'/Files/iwbold.gif"></td>'#13;
				if (Glyphs.Italic) then
					html := html + '<td><img id="' + HTMLName + 'i2" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''italic'')" title="Italic" src="'+AContext.WebApplication.AppURLBase+'/Files/iwItalic.gif"></td>'#13;
				if (Glyphs.Underline)  then
					html := html + '<td><img id="' + HTMLName + 'i3" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''underline'')" title="Underline" src="'+AContext.WebApplication.AppURLBase+'/Files/iwUnderline.gif"></td>'#13;
				if (Glyphs.Strike) then
					html := html + '<td><img id="' + HTMLName + 'i4" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''strikethrough'')" title="Striketrough" src="'+AContext.WebApplication.AppURLBase+'/Files/iwstrikeout.gif"></td>'#13;
				if (Glyphs.Subscript) then
					html := html + '<td><img id="' + HTMLName + 'i18" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''subscript'')" title="Subscript" src="'+AContext.WebApplication.AppURLBase+'/Files/subscript.gif"></td>'#13;
				if (Glyphs.Superscript) then
					html := html + '<td><img id="' + HTMLName + 'i19" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''superscript'')" title="Superscript" src="'+AContext.WebApplication.AppURLBase+'/Files/superscript.gif"></td>'#13;
				if (Glyphs.Left) then
					html := html + '<td><img id="' + HTMLName + 'i5" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''justifyleft'')" title="Left" src="'+AContext.WebApplication.AppURLBase+'/Files/iwalignleft.gif"></td>'#13;
				if (Glyphs.Center) then
					html := html + '<td><img id="' + HTMLName + 'i6" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''justifycenter'')" title="Center" src="'+AContext.WebApplication.AppURLBase+'/Files/iwaligncentre.gif"></td>'#13;
				if (Glyphs.Right) then
					html := html + '<td><img id="' + HTMLName + 'i7" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''justifyright'')" title="Right" src="'+AContext.WebApplication.AppURLBase+'/Files/iwalignright.gif"></td>'#13;
				if (Glyphs.UnorderedList) then
					html := html + '<td><img id="' + HTMLName + 'i8" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''insertunorderedList'')" title="UnorderedList" src="'+AContext.WebApplication.AppURLBase+'/Files/iwbullets.gif"></td>'#13;
				if (Glyphs.OrderedList) then
					html := html + '<td><img id="' + HTMLName + 'i9" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''insertorderedList'')" title="OrderedList" src="'+AContext.WebApplication.AppURLBase+'/Files/iwnumberedlist.gif"></td>'#13;
				if (Glyphs.Indent) then
					html := html + '<td><img id="' + HTMLName + 'i10" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''indent'')" title="Indent" src="'+AContext.WebApplication.AppURLBase+'/Files/iwindentincrease.gif"></td>'#13;
				if (Glyphs.UnIndent) then
					html := html + '<td><img id="' + HTMLName + 'i11" class="' + HTMLName + 'ToolBarButton"  onclick="' + HTMLName + 'OnClickToolBarButton(''outdent'')" title="Unindent" src="'+AContext.WebApplication.AppURLBase+'/Files/iwindentdecrease.gif"></td>'#13;
				if (Glyphs.Cut) then
					html := html + '<td><img id="' + HTMLName + 'i12" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''cut'')" title="Cut" src="'+AContext.WebApplication.AppURLBase+'/Files/iwcut.gif"></td>'#13;
				if (Glyphs.Copy) then
					html := html + '<td><img id="' + HTMLName + 'i13" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''copy'')" title="Copy" src="'+AContext.WebApplication.AppURLBase+'/Files/iwcopy.gif"></td>'#13;
				if (Glyphs.Paste) then
					html := html + '<td><img id="' + HTMLName + 'i14" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''paste'')" title="Paste" src="'+AContext.WebApplication.AppURLBase+'/Files/iwpaste.gif"></td>'#13;
				if (Glyphs.Link) then
					html := html + '<td><img id="' + HTMLName + 'i15" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''CreateLink'')" title="Hyperlink" src="'+AContext.WebApplication.AppURLBase+'/Files/iwlink.gif"></td>'#13;
				if (Glyphs.InsertImage) then
					html := html + '<td><img id="' + HTMLName + 'i16" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''InsertImage'')" title="Insert Image" src="'+AContext.WebApplication.AppURLBase+'/Files/iwimg.gif"></td>'#13;
				if (Glyphs.InsertHorizontalRule) then
					html := html + '<td><img id="' + HTMLName + 'i17" class="' + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton(''InsertHorizontalRule'')" title="Insert HorizontalRule" src="'+AContext.WebApplication.AppURLBase+'/Files/iwruler.gif"></td>'#13;
//        if (Glyphs.Preformat) then
//          html := html + '<td>#13<img id="' + HTMLName + 'i18" class="'  + HTMLName + 'ToolBarButton" onclick="' + HTMLName + 'OnClickToolBarButton("pre")" title="PRE" src="' + Glyphs.Folder + "/iwpre.gif">#13</td>'#13;

				html := html + '</tr>'#13;
				html := html + '</tbody>'#13;
				html := html + '</table>'#13;
				html := html + '</td>'#13;
				html := html + '</tr>'#13;
			end;
      //end render Toolbar

      //render Comboboxes
			//If property ShowComboboxes is true and at least one Comboboxes is visible --> show comboboxes
			if ((ShowComboBoxes) and
      ((ComboBoxes.Paragraph) or (ComboBoxes.FontNames) or (ComboBoxes.FontSizes) or (ComboBoxes.FontColors))) then
			begin
				html := html + '<tr style="HEIGHT: 22px">'#13;
				html := html + '<td>'#13;
				html := html + '<table cellspacing="0">'#13;
				html := html + '<tbody>'#13;
				html := html + '<tr>'#13;

				if (ComboBoxes.Paragraph)  then
				begin
					html := html + '<td>'#13;
					html := html + '<select id="' + HTMLName + 'formattingList" onchange="' + HTMLName + 'OnSelectedIndexChangedFormattingList(this)">'#13;
					html := html + '<option value="<body>" selected="selected">Normal</option>'#13;
					html := html + '<option value="<h1>">Heading 1</option>'#13;
					html := html + '<option value="<h2>">Heading 2</option>'#13;
					html := html + '<option value="<h3>">Heading 3</option>'#13;
					html := html + '<option value="<h4>">Heading 4</option>'#13;
					html := html + '<option value="<h5>">Heading 5</option>'#13;
					html := html + '<option value="<h6>">Heading 6</option>'#13;
//					html := html + '<option value="<pre>">Formatted</option>'#13;
					html := html + '</select>'#13;
					html := html + '</td>'#13;
				end;

				if (ComboBoxes.FontNames) then
				begin
					html := html + '<td>'#13;
					html := html + '<select id="' + HTMLName + 'fontFaceList" onchange="' + HTMLName + 'OnSelectedIndexChangedFontFaceList(this)">'#13;
					html := html + GetFonts(FFontList);
					html := html + '</select>'#13;
					html := html + '</td>'#13;
				end;

				if (ComboBoxes.FontSizes) then
				begin
					html := html + '<td>'#13;
					html := html + '<select id="' + HTMLName + 'fontSizeList" onchange="' + HTMLName + 'OnSelectedIndexChangedFontSizeList(this)">'#13;
					html := html + '<option value="1">1</option>'#13;
					html := html + '<option value="2">2</option>'#13;
					html := html + '<option value="3" selected="selected">3</option>'#13;
					html := html + '<option value="4">4</option>'#13;
					html := html + '<option value="5">5</option>'#13;
					html := html + '<option value="6">6</option>'#13;
					html := html + '</select>'#13;
					html := html + '</td>'#13;
				end;

				if (ComboBoxes.FontColors) then
				begin
					html := html + '<td>'#13;
					html := html + '<select id="' + HTMLName + 'fontColorList" onchange="' + HTMLName + 'OnSelectedIndexChangedFontColorList(this)">'#13;
					html := html + '<option value="black" style="color:Black" selected="selected">Black</option>'#13;
					html := html + '<option value="navy" style="color:Navy">Navy</option>'#13;
					html := html + '<option value="green" style="color:Green">Green</option>'#13;
					html := html + '<option value="blue" style="color:Blue">Blue</option>'#13;
					html := html + '<option value="cyan" style="color:Cyan">Cyan</option>'#13;
					html := html + '<option value="magenta" style="color:Magenta">Magenta</option>'#13;
					html := html + '<option value="red" style="color:Red">Red</option>'#13;
					html := html + '<option value="yellow" style="color:Yellow">Yellow</option>'#13;
					html := html + '<option value="white" style="color:Black">White</option>'#13;
					html := html + '<option value="silver" style="color:Silver">Silver</option>'#13;
					html := html + '<option value="teal" style="color:Teal">Teal</option>'#13;
					html := html + '</select>'#13;
					html := html + '</td>'#13;                          
				end;

				if (ComboBoxes.BackColors) then
				begin
					html := html + '<td>'#13;
					html := html + '<select id="' + HTMLName + 'backColorList" onchange="' + HTMLName + 'OnSelectedIndexChangedBackColorList(this)">'#13;
					html := html + '<option value="black" style="color:white;background-color:Black">Black</option>'#13;
					html := html + '<option value="navy" style="color:white;background-color:Navy">Navy</option>'#13;
					html := html + '<option value="green" style="background-color:Green">Green</option>'#13;
					html := html + '<option value="blue" style="background-color:Blue">Blue</option>'#13;
					html := html + '<option value="cyan" style="background-color:Cyan">Cyan</option>'#13;
					html := html + '<option value="magenta" style="background-color:Magenta">Magenta</option>'#13;
					html := html + '<option value="red" style="background-color:Red">Red</option>'#13;
					html := html + '<option value="yellow" style="background-color:Yellow">Yellow</option>'#13;
					html := html + '<option value="white" style="background-color:White" selected="selected">White</option>'#13;
					html := html + '<option value="silver" style="background-color:Silver">Silver</option>'#13;
					html := html + '<option value="teal" style="background-color:Teal">Teal</option>'#13;
					html := html + '</select>'#13;
					html := html + '</td>'#13;
        end;

				if (ViewHtml) then
				begin
					html := html + '<td align="right" width="100%">'#13;
					html := html + '<input name="viewHtmlCheckBox" id="' + HTMLName + 'viewHtmlCheckBox" onclick="' + HTMLName + 'OnClickShowHtmlCheckBox(this)" type="checkbox"/>'#13;
					html := html + '<label unselectable="on">View HTML Source</label>'#13;
					html := html + '</td>'#13;
				end;

				html := html + '</tr>'#13;
				html := html + '</tbody>'#13;
				html := html + '</table>'#13;
				html := html + '</td>'#13;
				html := html + '</tr>'#13;
			end
			else
			begin
				if (ViewHtml) then
				begin
					html := html + '<tr style="HEIGHT: 22px">'#13;
					html := html + '<td>'#13;
					html := html + '<table cellspacing="0" cellpadding="2" width="100%" border="0">'#13;
					html := html + '<tbody>'#13;
					html := html + '<tr>'#13;
					html := html + '<td align="right" width="100%">'#13;
					html := html + '<input name="viewHtmlCheckBox" id="' + HTMLName + 'viewHtmlCheckBox" onclick="' + HTMLName + 'OnClickShowHtmlCheckBox(this)" type="checkbox"/>'#13;
					html := html + '<label unselectable="on">View HTML Source</label>'#13;
					html := html + '</td>'#13;
					html := html + '</tr>'#13;
					html := html + '</tbody>'#13;
					html := html + '</table>'#13;
					html := html + '</td>'#13;
					html := html + '</tr>'#13;
				end
			end;
      //End Render Comboboxes

      //  html := StringReplace(html, '<#FONTS>',GetFonts(FFontList), [rfReplaceAll, rfIgnoreCase]);

			html := html + '<tr mytest style="HEIGHT:' + HeightText + 'px;';
      if not (BackgroundColor = clNone) then
      begin
  			html := html + 'background-color:'+HTMLClr(BackgroundColor) + '">'#13;
        html := html + '<td bgcolor="'+HTMLClr(BackgroundColor)+'">'#13;
      end
      else
      begin
  			html := html + '">'#13;
        html := html + '<td>'#13;
      end;


			 html := html + '<textarea style="width:'+IntToStr(Width)+'px;height:20px;display:none;" id="' + HTMLName + 'editorTextArea">'
				  + StrikeB + BoldB + ItalicB + UnderlineB
				  + memotext
				  + UnderlineE + ItalicE + BoldE + StrikeE
				  + '</textarea>'#13;

      html := html + '<iframe onBlur="' + HTMLName + 'OnBlurEditorDiv();" allowtransparency="true" style="filter:chroma(color=#FFFFFF);border:1;height:' + HeightText + 'px;width:'+ IntToStr(Width) +'" class="' + HTMLName + 'editorText" id="' + HTMLName + 'editorDiv">'
				  + '</iframe>'#13;

 	    html := html + '</td>'#13;
			html := html + '</tr>'#13;
			html := html + '</tbody>'#13;
			html := html + '</table>'#13;
			html := html + '</body>'#13;
			html := html + '</span>'#13;
			html := html + '</body>'#13;

                                                
  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;

  html :=	HTMLName + 'Initialize();';
  {$IFDEF TMSIW6}
  TIWComponent40Context(AContext).AddToInitProc(html + EOL);
  {$ELSE}
  AddToInitProc(html + EOL);
  {$ENDIF}

end;

procedure TTIWCustomHTMLEdit.SetGlyphs(const Value: TTIWHTMLEdGlyphs);
begin
  FGlyphs.Assign(Value);
end;                                                       
{ TTIWComboboxes }

constructor TTIWComboboxes.Create(AOwner: TComponent);
begin
  inherited Create;
  FOwner := AOwner;
  FBackColors := true;
  FParagraph := true;
  FFontNames := true;
  FFontSizes := true;
  FFontColors := true;
end;

destructor TTIWComboboxes.Destroy;
begin
  inherited;
end;

procedure TTIWComboboxes.SetBackColors(const Value: Boolean);
begin
  FBackColors := Value;
end;

procedure TTIWComboboxes.SetFontColors(const Value: Boolean);
begin
  FFontColors := Value;
end;

procedure TTIWComboboxes.SetFontNames(const Value: Boolean);
begin
  FFontNames := Value;
end;

procedure TTIWComboboxes.SetFontSizes(const Value: Boolean);
begin
  FFontSizes := Value;
end;

procedure TTIWComboboxes.SetParagraph(const Value: Boolean);
begin
  FParagraph := Value;
end;

end.
