unit uEditorBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  uGUIBase, uCustomEditor, ToolWin, ComCtrls, JvExControls,
  JvComponent, JvNavigationPane, ActnList, TBX, TB2Item, ImgList,
  PngImageList, StdActns, JvFormAutoSize, AppEvnts, uCustomView, uViewBase,
  JvAppStorage, JvAppRegistryStorage, JvFormPlacement, pngimage, ExtCtrls,
  JvComponentBase, TB2Dock, TB2Toolbar, dxLayoutLookAndFeels, TBXStatusBars,
  JvExComCtrls, JvStatusBar;

type
  IEditorBase = interface(ICustomEditor)
    ['{CB8CDE00-B225-4A1D-9A5C-EC6FBE2C854B}']
    function ShowModal : Integer;
  end;
  
  TfEditorBase = class(TCustomEditor, IEditorBase)
    actAcercaDe: TAction;
    actAnterior: TAction;
    actBuscar: TAction;
    actCancelarCambios: TAction;
    actCerrar: TAction;
    actConfPagina: TAction;
    actCopiar: TEditCopy;
    actCortar: TEditCut;
    actDeshacer: TEditUndo;
    actEliminar: TAction;
    actGuardar: TAction;
    actGuardarCerrar: TAction;
    actImprimir: TAction;
    actLimpiar: TEditDelete;
    actModificar: TAction;
    actNuevo: TAction;
    actPegar: TEditPaste;
    actPrevisualizar: TAction;
    actRefrescar: TAction;
    actSeleccionarTodo: TEditSelectAll;
    actSiguiente: TAction;
    EditorActionList: TActionList;
    JvNavPanelHeader: TJvNavPanelHeader;
    LargeImages: TPngImageList;
    SmallImages: TPngImageList;
    TBXDock: TTBXDock;
    TBXItem1: TTBXItem;
    TBXItem10: TTBXItem;
    TBXItem11: TTBXItem;
    TBXItem12: TTBXItem;
    TBXItem13: TTBXItem;
    TBXItem14: TTBXItem;
    TBXItem15: TTBXItem;
    TBXItem16: TTBXItem;
    TBXItem17: TTBXItem;
    TBXItem18: TTBXItem;
    TBXItem19: TTBXItem;
    TBXItem2: TTBXItem;
    TBXItem20: TTBXItem;
    TBXItem21: TTBXItem;
    TBXItem22: TTBXItem;
    TBXItem23: TTBXItem;
    TBXItem24: TTBXItem;
    TBXItem25: TTBXItem;
    TBXItem26: TTBXItem;
    TBXItem27: TTBXItem;
    TBXItem28: TTBXItem;
    TBXItem29: TTBXItem;
    TBXItem3: TTBXItem;
    TBXItem30: TTBXItem;
    TBXItem31: TTBXItem;
    TBXItem32: TTBXItem;
    TBXItem4: TTBXItem;
    TBXItem5: TTBXItem;
    TBXItem6: TTBXItem;
    TBXItem8: TTBXItem;
    TBXItem9: TTBXItem;
    tbxMain: TTBXToolbar;
    tbxMenu: TTBXToolbar;
    TBXSeparatorItem1: TTBXSeparatorItem;
    TBXSeparatorItem10: TTBXSeparatorItem;
    TBXSeparatorItem11: TTBXSeparatorItem;
    TBXSeparatorItem12: TTBXSeparatorItem;
    TBXSeparatorItem13: TTBXSeparatorItem;
    TBXSeparatorItem2: TTBXSeparatorItem;
    TBXSeparatorItem3: TTBXSeparatorItem;
    TBXSeparatorItem4: TTBXSeparatorItem;
    TBXSeparatorItem5: TTBXSeparatorItem;
    TBXSeparatorItem7: TTBXSeparatorItem;
    TBXSeparatorItem8: TTBXSeparatorItem;
    TBXSeparatorItem9: TTBXSeparatorItem;
    TBXSubmenuItem1: TTBXSubmenuItem;
    TBXSubmenuItem4: TTBXSubmenuItem;
    TBXSubmenuItem5: TTBXSubmenuItem;
    TBXSubmenuItem6: TTBXSubmenuItem;
    TBXSubmenuItem7: TTBXSubmenuItem;
    JvFormStorage: TJvFormStorage;
    JvAppRegistryStorage: TJvAppRegistryStorage;
    Image1: TImage;
    TBXSeparatorItem15: TTBXSeparatorItem;
    StatusBarImages: TPngImageList;
    StatusBar: TJvStatusBar;
    procedure actCerrarExecute(Sender: TObject);
    procedure actGuardarCerrarExecute(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure CustomEditorActivate(Sender: TObject);
    procedure actModificarExecute(Sender: TObject);
    procedure actGuardarExecute(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actNuevoExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actConfPaginaExecute(Sender: TObject);
    procedure actCancelarCambiosExecute(Sender: TObject);
    procedure actDuplicarExecute(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actGuardarCerrarUpdate(Sender: TObject);
    procedure actGuardarUpdate(Sender: TObject);

  protected
    procedure ActualizarEstadoEditor; virtual;
    function ModifiedQuery : Boolean;
    procedure ModificarInterno; virtual;
    procedure EliminarInterno; virtual;
    procedure GuardarInterno; virtual;
    procedure NuevoInterno; virtual;
    procedure ImprimirInterno; virtual;
    procedure PrevisualizarInterno; virtual;
    procedure ConfPaginaInterno; virtual;
    procedure RefrescarInterno; virtual;
    procedure CancelarCambiosInterno; virtual;
    procedure CerrarInterno; virtual;
    procedure DuplicarInterno; virtual;
    procedure PonerTitulos(const ATitulo: String = ''); virtual;
    procedure SetReadOnly(Value: Boolean); override;

  public
    constructor Create(AOwner: TComponent); override;
    function ShowModal : Integer;
  published
    procedure FormShow(Sender: TObject); virtual;
  end;

  TfEditorBaseClass = class of TfEditorBase;

implementation

{$R *.dfm}

uses
  Menus, uDataModuleBase, cxControls, uDialogUtils;

{
********************************* TfEditorBase *********************************
}
procedure TfEditorBase.actCancelarCambiosExecute(Sender: TObject);
begin
  CancelarCambiosInterno;
  ActualizarEstadoEditor;
end;

procedure TfEditorBase.actCerrarExecute(Sender: TObject);
begin
  CerrarInterno;
end;

procedure TfEditorBase.actConfPaginaExecute(Sender: TObject);
begin
  ConfPaginaInterno;
  ActualizarEstadoEditor;
end;

procedure TfEditorBase.actDuplicarExecute(Sender: TObject);
begin
  DuplicarInterno;
  ActualizarEstadoEditor;
end;

procedure TfEditorBase.actEliminarExecute(Sender: TObject);
begin
  if actEliminar.Enabled then
  begin
    EliminarInterno;
    ActualizarEstadoEditor;
  end;
end;

procedure TfEditorBase.actEliminarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := not ReadOnly;
end;

procedure TfEditorBase.actGuardarCerrarExecute(Sender: TObject);
begin
  ShowHourglassCursor;
  try
    if actGuardar.Execute then
      actCerrar.Execute;
  finally
    HideHourglassCursor;
  end;
end;

procedure TfEditorBase.actGuardarCerrarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := not ReadOnly;
end;

procedure TfEditorBase.actGuardarExecute(Sender: TObject);
begin
  GuardarInterno;
  ActualizarEstadoEditor;
end;

procedure TfEditorBase.actGuardarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := not ReadOnly;
end;

procedure TfEditorBase.actImprimirExecute(Sender: TObject);
begin
  if actImprimir.Enabled then
  begin
    ImprimirInterno;
    ActualizarEstadoEditor;
  end;
end;

procedure TfEditorBase.actModificarExecute(Sender: TObject);
begin
  if actModificar.Enabled then
  begin
    ModificarInterno;
    ActualizarEstadoEditor;
  end;
end;

procedure TfEditorBase.actModificarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := not ReadOnly;
end;

procedure TfEditorBase.actNuevoExecute(Sender: TObject);
begin
  if actNuevo.Enabled then
  begin
    NuevoInterno;
    ActualizarEstadoEditor;
  end;
end;

procedure TfEditorBase.actPrevisualizarExecute(Sender: TObject);
begin
  if actPrevisualizar.Enabled then
  begin
    PrevisualizarInterno;
    ActualizarEstadoEditor;
  end;
end;

procedure TfEditorBase.actRefrescarExecute(Sender: TObject);
begin
  if actRefrescar.Enabled then
  begin
    RefrescarInterno;
    ActualizarEstadoEditor;
  end;
end;

procedure TfEditorBase.ActualizarEstadoEditor;
begin
  PonerTitulos;
end;

procedure TfEditorBase.CancelarCambiosInterno;
begin
  //
end;

procedure TfEditorBase.CerrarInterno;
begin
  Close;
end;

procedure TfEditorBase.ConfPaginaInterno;
begin
  //
end;

constructor TfEditorBase.Create(AOwner: TComponent);
var
  APath : String;
begin
  inherited;
  with JvFormStorage do
  begin
    if Pos('_', Self.Name) = 0 then
      APath := Self.Name
    else
      APath := Copy(Self.Name, 0, (Pos('_', Self.Name)-1));
    AppStoragePath := APath;
  end;
  JvNavPanelHeader.StyleManager := dmBase.StyleManager;
end;

procedure TfEditorBase.CustomEditorActivate(Sender: TObject);
begin
  PonerTitulos;
end;

procedure TfEditorBase.DuplicarInterno;
begin
  //
end;

procedure TfEditorBase.EliminarInterno;
begin
  //
end;

procedure TfEditorBase.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if Valid then
    CanClose := ModifiedQuery
  else
    CanClose := False;
end;

procedure TfEditorBase.FormShow(Sender: TObject);
begin
  ActualizarEstadoEditor;
  if Assigned(Parent) then
  begin
    StatusBar.Visible := False;
    actCerrar.ShortCut := 0
  end
  else begin
    StatusBar.Visible := True;
    actCerrar.ShortCut := ShortCut(VK_ESCAPE, []);
  end;
end;

procedure TfEditorBase.GuardarInterno;
begin
  //
end;

procedure TfEditorBase.ImprimirInterno;
begin
  //
end;

procedure TfEditorBase.ModificarInterno;
begin
  //
end;

function TfEditorBase.ModifiedQuery: Boolean;
var
  Respuesta: Integer;
begin
  Result := True;
  if Modified then
  begin

    Respuesta := ShowConfirmMessage('Atención',
      'Se han producido cambios',
      '¿Desea guardar los cambios que se han producido antes de cerrar?',
      [TDlgButton_SI, TDlgButton_NO, TDlgButton_CANCELAR]);

    case Respuesta of
      IDYES   : actGuardar.Execute;
      IDNO    : actCancelarCambios.Execute;
      else
        Result := False;
    end;
  end
end;

procedure TfEditorBase.NuevoInterno;
begin
  //
end;

procedure TfEditorBase.PonerTitulos(const ATitulo: String);
begin
  if ReadOnly then
  begin
     JvNavPanelHeader.Caption := ATitulo + '   (NO MODIFICABLE)';
     Caption := ATitulo + '   (NO MODIFICABLE)';
  end
  else
  begin
     JvNavPanelHeader.Caption := ATitulo;
     Caption := ATitulo;
  end;
end;

procedure TfEditorBase.PrevisualizarInterno;
begin
  //
end;

procedure TfEditorBase.RefrescarInterno;
begin
  //
end;

procedure TfEditorBase.SetReadOnly(Value: Boolean);
var
  i: integer;
begin
  inherited;

  if ReadOnly then
     for i:=0 to Self.ComponentCount-1 do
     begin
         If Self.Components[i] Is TfrViewBase then
           (Self.Components[i] as TfrViewBase).ReadOnly := ReadOnly
     end;
end;

function TfEditorBase.ShowModal: Integer;
var
  ABorderIcons : TBorderIcons;
begin
  ABorderIcons := Self.BorderIcons;
  Self.BorderIcons := Self.BorderIcons - [biMinimize];
  try
    Result := inherited ShowModal;
  finally
    Self.BorderIcons := ABorderIcons;
  end;
end;

initialization
  RegisterClass(TfEditorBase);

finalization
  UnRegisterClass(TfEditorBase);

end.
