{********************************************************************}
{ IntraWeb script editor property editor                             }
{ for Delphi & C++Builder                                            }
{                                                                    }
{ written by : TMS Software                                          }
{               copyright © 2002 - 2005                              }
{               Email : info@tmssoftware.com                         }
{               Web : http://www.tmssoftware.com                     }
{********************************************************************}
unit IWMenuScriptEdit;

interface
{$I TMSDEFS.INC}
uses
  Classes, AdvMemo, Dialogs, uMenuScriptEdit, Forms, Controls,
  IWTMSMenus, Menus, SysUtils,
{$IFDEF DELPHI6_LVL}
  DesignIntf, DesignEditors
{$ELSE}
  DsgnIntf
{$ENDIF}
  ;

type
  TIWMenuScriptEventProperty = class(TClassProperty)
  public
    function GetAttributes:TPropertyAttributes; override;
    procedure Edit; override;
  end;

  TIWMenuEditor = class(TDefaultEditor)
  protected
  {$IFNDEF DELPHI6_LVL}
    procedure EditProperty(PropertyEditor: TPropertyEditor;
      var Continue, FreeEditor: Boolean); override;
  {$ELSE}
    procedure EditProperty(const PropertyEditor: IProperty; var Continue: Boolean); override;
  {$ENDIF}
  public
  end;



procedure Register;

implementation

procedure Register;
begin
  RegisterPropertyEditor(TypeInfo(TMenuClientEvents),TTIWMainMenu,'ClientEvents',TIWMenuScriptEventProperty);
  RegisterPropertyEditor(TypeInfo(TMenuClientEvents),TTIWSideMenu,'ClientEvents',TIWMenuScriptEventProperty);

  RegisterComponentEditor(TTIWMainMenu,TIWMenuEditor);
  RegisterComponentEditor(TTIWSideMenu,TIWMenuEditor);
end;


{ TIWScriptEventProperty }

procedure TIWMenuScriptEventProperty.Edit;
var
  SE: TTMSMenuScriptEdit;
  i: Integer;
  MainMenu: TMenu;
  ClientEvents: TMenuClientEvents;
begin

  SE := TTMSMenuScriptEdit.Create(Application);

  try
    MainMenu := nil;
    ClientEvents := nil;

    if GetComponent(0) is TTIWMainMenu then
    begin
      MainMenu := (GetComponent(0) as TTIWMainMenu).Menu;
      ClientEvents := (GetComponent(0) as TTIWMainMenu).ClientEvents;
    end;

    if GetComponent(0) is TTIWSideMenu then
    begin
      MainMenu := (GetComponent(0) as TTIWSideMenu).Menu;
      ClientEvents := (GetComponent(0) as TTIWSideMenu).ClientEvents;
    end;

    if Assigned(ClientEvents) then
    begin
      for i := 1 to ClientEvents.Count do
      begin
        SE.MenuNames.Add(ClientEvents[i - 1].ItemName);
        SE.MenuScripts.Add(ClientEvents[i - 1].ItemScript);
      end;
    end;

    if Assigned(MainMenu) then
    begin
      for i := 1 to MainMenu.Items.Count do
        SE.AddToTree(nil,MainMenu.Items[i - 1]);

      if MainMenu.Items.Count > 0  then
        SE.TreeView1.Selected := SE.TreeView1.Items[0];
      SE.Select;
    end;


    if SE.ShowModal = mrOk then
    begin
      if Assigned(ClientEvents) then
      begin
        ClientEvents.Clear;
        for i := 1 to SE.MenuScripts.Count do
        begin
          if Trim(SE.MenuScripts[i - 1]) <> '' then
            with ClientEvents.Add do
            begin
              ItemName := SE.MenuNames[i - 1];
              ItemScript := SE.MenuScripts[i - 1];
            end;
        end;
        Modified;
      end;
    end;
  finally
    SE.Free;
  end;
 
end;

function TIWMenuScriptEventProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog];
end;


{$IFDEF DELPHI6_LVL}
procedure TIWMenuEditor.EditProperty(const PropertyEditor: IProperty; var Continue: Boolean);
{$ELSE}
procedure TIWMenuEditor.EditProperty(PropertyEditor: TPropertyEditor;
  var Continue, FreeEditor: Boolean);
{$ENDIF}
var
  PropName: string;
begin
  PropName := PropertyEditor.GetName;
  if (CompareText(PropName, 'ClientEvents') = 0) then
  begin
    PropertyEditor.Edit;
    Continue := False;
  end;
end;



end.




