
{                                                                              }
{ IB_ActionSearch                                                              }
{                                                                              }

{***************************************************************}
{                                                               }
{          IB Objects Components and Resources Library          }
{                                                               }
{          Copyright (C) 1996-2007 Jason Wharton                }
{          and Computer Programming Solutions, Mesa, AZ         }
{                                                               }
{  This source code unit is released under the terms of the     }
{  CPS Trustware License.                                       }
{  It may be distributed or deployed as source code or in       }
{  compiled form only in compliance with the terms and          }
{  conditions of the CPS Trustware License which may be         )
{  examined at http://www.ibobjects.com/ibo_trustware.html      )
{                                                               }
{***************************************************************}

{******************************************************************************}
{  CONTRIBUTED MODIFICATIONS                                                   }
{  Additions or modifications listed below using format:                       }
{                                                                              }
{  Joe Citizen <joe@joecitizen.com>                                            }
{  mm/dd/yyyy                                                                  }
{     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 }
{     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 }
{     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 }
{            Copyright (C) 2001 Joe Citizen                                    }
{                                                                              }
{------------------------------------------------------------------------------}
{  Place your credits and information in the top of the section                }
{  below with any other descriptions or explanations desired.                  }
{------------------------------------------------------------------------------}
{  Rade Vojvodic <rvojvodic@rsdef.org>                                         }
{  06/09/2003                                                                  }
{     Just like others made actions for update bar i made actions that do the  }
{     from the Search Bar                                                      }
{                                                                              }
{  Marco Menardi <mmenaz@lycosmail.com                                         }
{  10/26/2001                                                                  }
{     I've made this Actions "focused dataset" enabled like IB_xxxBars are.    }
{     So there is a ReceiveFocus property and an OnReceiveFocus event added    }
{     Check of Datasource.Dataset being valid in .ExecuteTarget has been       }
{     removed since the Enabled property does enought                          }
{            Copyright (C) 2001 Marco Menardi                                  }
{                                                                              }
{******************************************************************************}

{$INCLUDE IB_Directives.inc}

unit
  IB_ActionSearch;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, Menus,

  IB_Constants,
  IB_FieldDataLink,
  IB_Components;

type
  TIB_CustomActionSearch = class(TAction)
{$I IB_DataLink.INT}
  protected
    procedure SysStateChanged; virtual;
  public
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
    function HandlesTarget( Target: TObject ): Boolean; override;
  end;

  TIB_ActionSearch = class( TIB_CustomActionSearch )
  protected
    procedure SysStateChanged; override;
  public
    constructor Create( AOwner: TComponent ); override;
    procedure ExecuteTarget( Target: TObject ); override;
  end;

  TIB_ActionOrder = class( TIB_CustomActionSearch )
  protected
    procedure SysStateChanged; override;
  public
    constructor Create( AOwner: TComponent ); override;
    procedure ExecuteTarget( Target: TObject ); override;
  end;

  TIB_ActionCount = class( TIB_CustomActionSearch )
  protected
    procedure SysStateChanged; override;
  public
    constructor Create( AOwner: TComponent ); override;
    procedure ExecuteTarget( Target: TObject ); override;
  end;

implementation

uses IBD_Ordering;

constructor TIB_CustomActionSearch.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );

  FIB_DataLink := TIB_DataLink.Create( Self );

  with DataLink do
  begin
    AfterAssignment := IB_DatasetStateChanged;
    OnPreparedChanged := IB_DatasetStateChanged;
    OnActiveChange := IB_DatasetStateChanged;
    OnStateChanged := IB_DatasetStateChanged;
    OnDataChange := IB_DatasetDataChange;
  end;
end;

destructor TIB_CustomActionSearch.Destroy;
begin
  with DataLink do
  begin
    AfterAssignment := nil;
    OnPreparedChanged := nil;
    OnActiveChange := nil;
    OnStateChanged := nil;
    OnDataChange := nil;
  end;
  inherited Destroy;
end;

procedure TIB_CustomActionSearch.SetDataSource;
begin
  if DataLink.DataSource <> AValue then
  begin
    DataLink.DataSource := AValue;
    SysStateChanged;
  end;
end;

function TIB_CustomActionSearch.GetDataSource: TIB_DataSource;
begin
  Result := DataLink.DataSource;
end;

function TIB_CustomActionSearch.GetReceiveFocus: boolean;
begin
  Result := DataLink.ReceiveFocus;
end;

procedure TIB_CustomActionSearch.SetReceiveFocus(AValue: boolean);
begin
  DataLink.ReceiveFocus := AValue;
end;

function TIB_CustomActionSearch.HandlesTarget( Target: TObject ): Boolean;
begin
  Result := True;
end;

procedure TIB_CustomActionSearch.SysStateChanged;
begin
  Enabled := Assigned( DataSource ) and Assigned( DataSource.DataSet );
end;

procedure TIB_CustomActionSearch.IB_DatasetStateChanged( Sender: TIB_DataLink;
                                                   DataSource: TIB_DataSource );
begin
  SysStateChanged;
end;

procedure TIB_CustomActionSearch.IB_DatasetDataChange( Sender: TIB_DataLink;
                                                  DataSource: TIB_DataSource;
                                                  IB_Field: TIB_Column );
begin
  SysStateChanged;
end;

function TIB_CustomActionSearch.GetOnReceiveFocus: TIB_DataLinkEvent;
begin
  Result := DataLink.OnReceiveFocus;
end;

procedure TIB_CustomActionSearch.SetOnReceiveFocus( AValue: TIB_DataLinkEvent );
begin
  DataLink.OnReceiveFocus := AValue;
end;

constructor TIB_ActionSearch.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  Hint := H_SB_SEARCH;
end;

procedure TIB_ActionSearch.SysStateChanged;
begin
  inherited;
  Enabled := Enabled and ( DataSource.Dataset.CanSearch );
end;

procedure TIB_ActionSearch.ExecuteTarget( Target: TObject );
begin
  with DataSource.Dataset do
    if CanSearch then
      if State = dssSearch then
        RecallLastSearch
      else
        Search;
end;

constructor TIB_ActionOrder.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  Hint := H_SB_ORDER;
end;

procedure TIB_ActionOrder.SysStateChanged;
begin
  inherited;
  Enabled := Enabled;
end;

procedure TIB_ActionOrder.ExecuteTarget( Target: TObject );
begin
  with DataSource.Dataset do
  begin
    with TdlgOrdering.Create( Self ) do
    try
      DataSource.Dataset := Self.DataSource.Dataset;
      Caption := C_FRM_Ordering;
      OKBtn.Caption := C_BF_OK;
      CancelBtn.Caption := C_BF_CANCEL;
      ShowModal;
    finally
      Free;
    end;
  end;
end;

constructor TIB_ActionCount.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  Hint := H_SB_COUNT;
end;

procedure TIB_ActionCount.SysStateChanged;
begin
  inherited;
  Enabled := Enabled;
end;

procedure TIB_ActionCount.ExecuteTarget( Target: TObject );
begin
  with DataSource.Dataset do
  begin
    ShowMessage( Format( M_SB_REC_CNT, [ RecordCount ] ));
  end;
end;

end.
