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 GetWindowCaption : String;
    procedure SetWindowCaption(const AValue : String);
    property WindowCaption : String read GetWindowCaption write SetWindowCaption;

    function GetHeaderText : String;
    procedure SetHeaderText(const AValue : String);
    property HeaderText : String read GetHeaderText write SetHeaderText;

    function ShowModal : Integer;
    procedure Show;
  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
    FWindowCaption : String;
    FHeaderText : String;
    procedure CreateParams(Var params: TCreateParams); override;
    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;
    function GetHeaderText: String;
    function GetWindowCaption: String;
    procedure SetHeaderText(const Value: String);
    procedure SetWindowCaption(const Value: String);
  public
    constructor Create(AOwner: TComponent); override;
    function ShowModal : Integer; override;
    procedure Show;
  published
    procedure FormShow(Sender: TObject);
    property WindowCaption : String read GetWindowCaption write SetWindowCaption;
    property HeaderText : String read GetHeaderText write SetHeaderText;
     
  end;

  TfEditorBaseClass = class of TfEditorBase;

implementation

{$R *.dfm}

uses
  Menus, uDMBase, cxControls, cxEdit, uDialogUtils, uStringsUtils, uDisplayUtils;

{Método que coge el componente editor que tenga el foco y le fuerza a hacer post para que la tabla se entere
}
procedure PostActivecxEditorChanges;
var
  AControl: TWinControl;
  ActiveEditor: TcxCustomEdit;
begin
  ActiveEditor:= nil;

  AControl := Screen.ActiveControl;
  if Supports(AControl, IcxInnerEditHelper) then
    ActiveEditor:= TcxCustomEdit(AControl.Owner)
  else
    if AControl is TcxCustomEdit then
      ActiveEditor:= TcxCustomEdit(AControl);
  if Assigned(ActiveEditor) then
     ActiveEditor.PostEditValue;
end;

{
********************************* 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
    if Modified then
    begin
      if (ShowConfirmMessage('Se han producido cambios', 'Se han producido cambios y no se puede previsualizar hasta que no se guarden.' + #10#13 +
        '¿Desea guardarlos ahora?') = IDYES) then
      begin
        GuardarInterno;
        ImprimirInterno;
        ActualizarEstadoEditor;
      end
      else
        ShowInfoMessage('Recuerde guardar los cambios si quiere previsualizar o imprimir.');
    end
    else
    begin
      ImprimirInterno;
      ActualizarEstadoEditor;
    end
  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
    if Modified then
    begin
      if (ShowConfirmMessage('Se han producido cambios', 'Se han producido cambios y no se puede previsualizar hasta que no se guarden.' + #10#13 +
        '¿Desea guardarlos ahora?') = IDYES) then
      begin
        GuardarInterno;
        PrevisualizarInterno;
        ActualizarEstadoEditor;
      end
      else
        ShowInfoMessage('Recuerde guardar los cambios si quiere previsualizar o imprimir.');
    end
    else
    begin
      PrevisualizarInterno;
      ActualizarEstadoEditor;
    end;
  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
  ScaleFormFont(Self);
  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;

function TfEditorBase.GetHeaderText: String;
begin
  Result := FHeaderText;
end;

function TfEditorBase.GetWindowCaption: String;
begin
  Result := FWindowCaption;
end;

procedure TfEditorBase.GuardarInterno;
begin
  PostActivecxEditorChanges;
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);
var
  AHeaderText,
  AWindowText : String;
begin
  if EsCadenaVacia(ATitulo) then
  begin
    AHeaderText := FHeaderText;
    AWindowText := FHeaderText;
  end
  else begin
    AHeaderText := ATitulo;
    AWindowText := ATitulo;
  end;

  if ReadOnly then
  begin
    AHeaderText := AHeaderText + '   (NO MODIFICABLE)';
    AWindowText := AWindowText + '   (NO MODIFICABLE)';
  end;

  JvNavPanelHeader.Caption := AHeaderText;
  Caption := AWindowText;
end;

procedure TfEditorBase.PrevisualizarInterno;
begin
  //
end;

procedure TfEditorBase.RefrescarInterno;
begin
  //
end;

procedure TfEditorBase.SetHeaderText(const Value: String);
begin
  FHeaderText := Value;
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;

procedure TfEditorBase.SetWindowCaption(const Value: String);
begin
  FWindowCaption := Value;
end;

procedure TfEditorBase.Show;
begin
  inherited Show;
//  Self.WindowState := wsNormal;
//  self.FormStyle := fsNormal;
end;

procedure TfEditorBase.CreateParams(var params: TCreateParams);
begin
  inherited CreateParams(params);

  { Descomentar para crear varios formularios hijos abiertos a la vez
    fuera de la aplicación principal pero cuidado por que las ventanas
    modales como la previsualización también se ven afectadas.

    inherited CreateParams( params );
    params.Style := params.Style and not WS_POPUP;
    params.ExStyle := params.ExStyle and not WS_EX_TOOLWINDOW or WS_EX_APPWINDOW;
  }
end;

function TfEditorBase.ShowModal: Integer;
var
  ABorderIcons : TBorderIcons;
begin
  ABorderIcons := Self.BorderIcons;
  Self.BorderIcons := Self.BorderIcons - [biMinimize];
  try
    // Para tratar de evistar que la ventana modal se quede detrás
    // de la pantalla principal.
    PopupMode := pmExplicit;
    PopupParent := Application.MainForm;

    Result := inherited ShowModal;
  finally
    Self.BorderIcons := ABorderIcons;
  end;
end;

initialization
  RegisterClass(TfEditorBase);

finalization
  UnRegisterClass(TfEditorBase);

end.
