ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/journaling/ibcontrols/IBDynamicGrid.pas
(Generate patch)

Comparing ibx/trunk/ibcontrols/IBDynamicGrid.pas (file contents):
Revision 35 by tony, Tue Jan 26 14:38:47 2016 UTC vs.
Revision 45 by tony, Tue Dec 6 10:33:46 2016 UTC

# Line 150 | Line 150 | end;
150      FGrid: TCustomGrid;
151      FCol,FRow: Integer;
152      FEditText: string;
153 +    function EditingKeyField: boolean;
154    protected
155      procedure WndProc(var TheMessage : TLMessage); override;
156      procedure CloseUp; override;
# Line 166 | Line 167 | end;
167      property OnEditingDone;
168    end;
169  
170 +  TOnSelectPanelEditor = procedure(Sender: TObject; var aEditorPanel: TWinControl) of object;
171 +
172    TDBDynamicGrid = class(TDBGrid)
173    private
174      { Private declarations }
# Line 176 | Line 179 | end;
179      FOnEditorPanelHide: TNotifyEvent;
180      FOnEditorPanelShow: TNotifyEvent;
181      FOnKeyDownHander: TKeyDownHandler;
182 +    FOnSelectPanelEditor: TOnSelectPanelEditor;
183      FResizing: boolean;
184      FWeHaveFocus: boolean;
185      FHidingEditorPanel: boolean;
# Line 223 | Line 227 | end;
227      property OnEditorPanelShow: TNotifyEvent read FOnEditorPanelShow write FOnEditorPanelShow;
228      property OnEditorPanelHide: TNotifyEvent read FOnEditorPanelHide write FOnEditorPanelHide;
229      property OnKeyDownHander: TKeyDownHandler read FOnKeyDownHander write FOnKeyDownHander;
230 +    property OnSelectPanelEditor: TOnSelectPanelEditor read FOnSelectPanelEditor
231 +                                                       write FOnSelectPanelEditor;
232   end;
233  
234    {TIBGridControlLink}
# Line 259 | Line 265 | end;
265      FBookmark: TLocationArray;
266      FDBLookupCellEditor: TDBLookupCellEditor;
267      FActive: boolean;
268 +    FFieldPosition: integer;
269      procedure ColumnHeaderClick(Index: integer);
270      function GetDataSource: TDataSource;
271      function GetEditorBorderStyle: TBorderStyle;
# Line 304 | Line 311 | end;
311  
312   implementation
313  
314 < uses Math, IBQuery, LCLType;
314 > uses LCLType, Variants, EditBtn;
315  
316   { TIBGridControlLink }
317  
# Line 370 | Line 377 | begin
377      else
378      begin
379        for I := 0 to  ColCount - 1 do
380 +        if (I < FixedCols) or Columns[I - FixedCols].Visible then
381           ColSum := ColSum + ColWidths[I];
382  
383        if Colsum <> ClientWidth then
384        begin
385          ResizeColCount := 0;
386          for I := 0 to Columns.Count -1 do
387 <          if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then
387 >          if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn and Columns[I].Visible then
388            begin
389              Inc(ResizeColCount);
390              Colsum := Colsum + TDBDynamicGridColumn(Columns[I]).DesignWidth - Columns[I].Width;
# Line 389 | Line 397 | begin
397              n := (ClientWidth - ColSum) mod ResizeColCount;
398  
399              for I := 0 to Columns.Count -1 do
400 <              if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then
400 >              if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn and Columns[I].Visible then
401                begin
402                  if I = 0 then
403                    Columns[I].Width := Columns[I].Width + adjustment + n
# Line 563 | Line 571 | begin
571                           and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore Return in a CustomMemo}
572  
573      if (AControl <> nil) and (AControl is TCustomGrid)
574 <                         and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a CustomMemo}
574 >                         and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a Custom Grid}
575 >
576 >    if (AControl <> nil) and ((AControl is TDateEdit) or (AControl is TEBEdit))
577 >                         and (Key in [VK_RETURN,VK_UP,VK_DOWN,
578 >                               VK_ESCAPE,VK_LEFT,VK_RIGHT]) then Exit; {Ignore Return in a Data edit}
579  
580      if assigned(FOnKeyDownHander) then
581        OnKeyDownHander(Sender,Key,Shift,Done);
# Line 608 | Line 620 | begin
620    if FEditorPanel <> nil then
621       RemoveFreeNotification(FEditorPanel);
622    FEditorPanel := AValue;
623 <  FreeNotification(FEditorPanel);
623 >  if FEditorPanel <> nil then
624 >     FreeNotification(FEditorPanel);
625   end;
626  
627   procedure TDBDynamicGrid.ChangeBounds(ALeft, ATop, AWidth, AHeight: integer;
# Line 675 | Line 688 | end;
688  
689   procedure TDBDynamicGrid.Notification(AComponent: TComponent;
690    Operation: TOperation);
691 + var i: integer;
692   begin
693    inherited Notification(AComponent, Operation);
694 <  if (Operation = opRemove) and
695 <     (AComponent = FEditorPanel) then FEditorPanel := nil;
694 >  if (Operation = opRemove) and not (csDestroying in ComponentState) then
695 >  begin
696 >    if AComponent = FEditorPanel then
697 >      FEditorPanel := nil
698 >    else
699 >    if AComponent is TControl then
700 >    begin
701 >      for i := 0 to Columns.Count - 1 do
702 >        if TDBDynamicGridColumn(Columns[I]).ColumnTotalsControl = AComponent then
703 >          TDBDynamicGridColumn(Columns[I]).ColumnTotalsControl := nil;
704 >    end;
705 >  end
706   end;
707  
708   procedure TDBDynamicGrid.TopLeftChanged;
# Line 691 | Line 715 | procedure TDBDynamicGrid.UpdateActive;
715   begin
716    inherited UpdateActive;
717  
718 +  if not (csLoading in ComponentState) and assigned(DataLink)
719 +                       and assigned(DataLink.DataSet) and DataLink.DataSet.Active then
720 +    DoGridResize;
721 +
722    if not (csLoading in ComponentState) and assigned(DataLink) and
723       assigned(FEditorPanel) and not FEditorPanel.Visible and
724       assigned(DataLink.DataSet) and (DataLink.DataSet.State = dsInsert) then
# Line 729 | Line 757 | begin
757   end;
758  
759   procedure TDBDynamicGrid.ShowEditorPanel;
760 + var aEditor: TWinControl;
761   begin
762    if (csDesigning in ComponentState) or
763     (DataSource = nil) or (DataSource.DataSet = nil)
764       or ((DataSource.DataSet.RecordCount = 0) and (DataSource.DataSet.State <> dsInsert)) then
765       Exit;
766 <  Editor := FEditorPanel;
766 >  aEditor := FEditorPanel;
767 >  if assigned(FOnSelectPanelEditor) then
768 >    OnSelectPanelEditor(self,aEditor);
769 >  if FEditorPanel <> aEditor then
770 >    SetEditorPanel(aEditor);
771 >  Editor := aEditor;
772    EditorMode := true;
773   end;
774  
# Line 773 | Line 807 | begin
807    Result := inherited Width
808   end;
809  
810 + type
811 +  THackedGrid = class(TIBDynamicGrid)
812 +  public
813 +    property FixedCols;
814 +  end;
815 +
816   { TDBLookupCellEditor }
817  
818 + function TDBLookupCellEditor.EditingKeyField: boolean;
819 + begin
820 +  with TIBDynamicGridColumn(TDBGrid(FGrid).Columns[FCol - THackedGrid(FGrid).FixedCols]) do
821 +    Result := CompareText(FieldName, DBLookupProperties.DataFieldName) = 0;
822 + end;
823 +
824   procedure TDBLookupCellEditor.WndProc(var TheMessage: TLMessage);
825   begin
826    if TheMessage.msg=LM_KILLFOCUS then begin
# Line 793 | Line 839 | begin
839    UpdateData(nil); {Force Record Update}
840    if FGrid<>nil then
841    Begin
842 <    (FGrid as TIBDynamicGrid).EditorTextChanged(FCol, FRow, Trim(Text));
842 >    if EditingKeyField then
843 >    begin
844 >      if not VarIsNull(KeyValue) then
845 >       (FGrid as TIBDynamicGrid).EditorTextChanged(FCol, FRow, KeyValue)
846 >    end
847 >    else
848 >      (FGrid as TIBDynamicGrid).EditorTextChanged(FCol, FRow, Trim(Text));
849      (FGrid as TIBDynamicGrid).UpdateData;
850    end;
851    inherited CloseUp;
# Line 818 | Line 870 | begin
870    CheckAndInsert;
871    Msg.Col := FCol;
872    Msg.Row := FRow;
873 <  Msg.Value:= Trim(Text);
873 >  if EditingKeyField then
874 >  begin
875 >    if not VarIsNull(KeyValue) then
876 >      Msg.Value:= KeyValue
877 >    else
878 >      Msg.Value:= ''
879 >  end
880 >  else
881 >    Msg.Value:= Trim(Text);
882   end;
883  
884   procedure TDBLookupCellEditor.msg_SetGrid(var Msg: TGridMessage);
# Line 906 | Line 966 | begin
966      if DataFieldName <> '' then
967          Editor.DataSource := TDBGrid(Grid).DataSource;
968    end;
969 <  Editor.Text := Editor.FEditText;
969 >  if Editor.EditingKeyField then
970 >  begin
971 >    if not Field.IsNull then
972 >      Editor.KeyValue := Editor.FEditText
973 >  end
974 >  else
975 >    Editor.Text := Editor.FEditText;
976    Editor.SelStart := Length(Editor.Text);
977   end;
978  
# Line 948 | Line 1014 | begin
1014        OnColumnHeaderClick(self,Index);
1015  
1016      FLastColIndex := Index;
1017 <    if assigned(DataSource) and assigned(DataSource.DataSet) and DataSource.DataSet.Active then
1017 >    FFieldPosition := 0;
1018 >    if assigned(DataSource) and assigned(DataSource.DataSet) and DataSource.DataSet.Active
1019 >       and (DataSource.DataSet is TIBParserDataSet) then
1020      begin
1021 +      if FLastColIndex < Columns.Count then
1022 +      {try and cache field position while dataset still open}
1023 +        FFieldPosition := TIBParserDataSet(DataSource.DataSet).Parser.GetFieldPosition(Columns[FLastColIndex].FieldName);
1024        DataSource.DataSet.Active := false;
1025        Application.QueueAsyncCall(@DoReopen,0)
1026      end;
# Line 997 | Line 1068 | begin
1068    for i := 0 to Columns.Count - 1 do
1069    begin
1070      if TIBDynamicGridColumn(columns[i]).InitialSortColumn then
1071 +    begin
1072 +      FFieldPosition := 0;
1073        FLastColIndex := i
1074 +    end
1075    end
1076   end;
1077  
# Line 1014 | Line 1088 | end;
1088  
1089   procedure TIBDynamicGrid.UpdateSQL(Sender: TObject; Parser: TSelectSQLParser);
1090   var OrderBy: string;
1017    FieldPosition: integer;
1091   begin
1092      if assigned(DataSource) and assigned(DataSource.DataSet)
1093        and (DataSource.DataSet is TIBCustomDataSet) then
1094      begin
1095 <      if (FLastColIndex < 0) or (FLastColIndex >= Columns.Count) then Exit;
1096 <      FieldPosition := Parser.GetFieldPosition(Columns[FLastColIndex].FieldName);
1097 <      if FieldPosition > 0 then
1095 >      if (FFieldPosition = 0) and (FLastColIndex >= 0) and (FLastColIndex < Columns.Count) then
1096 >        {Not cached - let's hope we can find it before the dataset is opened.
1097 >         Won't work if dynamic columns}
1098 >        FFieldPosition := Parser.GetFieldPosition(Columns[FLastColIndex].FieldName);
1099 >      if FFieldPosition > 0 then
1100        begin
1101          if Descending then
1102 <          Parser.OrderByClause := IntToStr(FieldPosition) + ' desc'
1102 >          Parser.OrderByClause := IntToStr(FFieldPosition) + ' desc'
1103          else
1104 <          Parser.OrderByClause := IntToStr(FieldPosition) + ' asc';
1104 >          Parser.OrderByClause := IntToStr(FFieldPosition) + ' asc';
1105        end;
1106  
1107        if assigned(FOnUpdateSortOrder) then
# Line 1170 | Line 1245 | end;
1245  
1246   procedure TIBDynamicGrid.Notification(AComponent: TComponent;
1247    Operation: TOperation);
1248 + var i: integer;
1249   begin
1250    inherited Notification(AComponent, Operation);
1251 <  if (Operation = opRemove) and
1252 <     (FIBControlLink <> nil) and (AComponent = DataSource) then FIBControlLink.IBDataSet := nil;
1251 >  if (Operation = opRemove) then
1252 >  begin
1253 >    if (FIBControlLink <> nil) and (AComponent = DataSource) then
1254 >      FIBControlLink.IBDataSet := nil
1255 >    else
1256 >    if AComponent is TDataSource then
1257 >    begin
1258 >      for i := 0 to Columns.Count - 1 do
1259 >        if TIBDynamicGridColumn(Columns[I]).DBLookupProperties.ListSource = AComponent then
1260 >          TIBDynamicGridColumn(Columns[I]).DBLookupProperties.ListSource := nil;
1261 >    end
1262 >  end
1263   end;
1264  
1265   procedure TIBDynamicGrid.UpdateActive;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines