--- ibx/trunk/ibcontrols/IBDynamicGrid.pas 2015/07/18 12:30:52 34 +++ ibx/trunk/ibcontrols/IBDynamicGrid.pas 2016/01/26 14:38:47 35 @@ -23,6 +23,7 @@ * Contributor(s): ______________________________________. * *) + unit IBDynamicGrid; {$mode objfpc}{$H+} @@ -179,10 +180,12 @@ end; FWeHaveFocus: boolean; FHidingEditorPanel: boolean; FAllowHide: boolean; + FMouseDown: boolean; function ActiveControl: TControl; procedure DoShowEditorPanel(Data: PtrInt); procedure PositionTotals; procedure KeyDownHandler(Sender: TObject; var Key: Word; Shift: TShiftState); + procedure PerformEditorHide(Data: PtrInt); procedure SetEditorPanel(AValue: TWinControl); protected procedure ChangeBounds(ALeft, ATop, AWidth, AHeight: integer; KeepBase: boolean); override; @@ -361,34 +364,40 @@ begin FResizing := true; try ColSum := 0; - for I := 0 to ColCount - 1 do - ColSum := ColSum + ColWidths[I]; - if Colsum <> ClientWidth then + if (ColCount = 1) and TDBDynamicGridColumn(Columns[0]).AutoSizeColumn then + Columns[0].Width := ClientWidth + else begin - ResizeColCount := 0; - for I := 0 to Columns.Count -1 do - if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then - begin - Inc(ResizeColCount); - Colsum := Colsum + TDBDynamicGridColumn(Columns[I]).DesignWidth - Columns[I].Width; - Columns[I].Width := TDBDynamicGridColumn(Columns[I]).DesignWidth; - end; - - if (Colsum < ClientWidth) and (ResizeColCount > 0) then - begin - adjustment := (ClientWidth - ColSum) div ResizeColCount; - n := (ClientWidth - ColSum) mod ResizeColCount; - - for I := 0 to Columns.Count -1 do - if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then - begin - if I = 0 then - Columns[I].Width := Columns[I].Width + adjustment + n - else - Columns[I].Width := Columns[I].Width + adjustment; - end; - end; + for I := 0 to ColCount - 1 do + ColSum := ColSum + ColWidths[I]; + + if Colsum <> ClientWidth then + begin + ResizeColCount := 0; + for I := 0 to Columns.Count -1 do + if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then + begin + Inc(ResizeColCount); + Colsum := Colsum + TDBDynamicGridColumn(Columns[I]).DesignWidth - Columns[I].Width; + Columns[I].Width := TDBDynamicGridColumn(Columns[I]).DesignWidth; + end; + + if (Colsum < ClientWidth) and (ResizeColCount > 0) then + begin + adjustment := (ClientWidth - ColSum) div ResizeColCount; + n := (ClientWidth - ColSum) mod ResizeColCount; + + for I := 0 to Columns.Count -1 do + if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then + begin + if I = 0 then + Columns[I].Width := Columns[I].Width + adjustment + n + else + Columns[I].Width := Columns[I].Width + adjustment; + end; + end; + end; end; PositionTotals; UpdateEditorPanelBounds; @@ -402,20 +411,22 @@ begin inherited DoEditorHide; if Editor = FEditorPanel then begin - if (FExpandedRow >= 0) and (FExpandedRow < RowCount) then - RowHeights[FExpandedRow] := DefaultRowHeight; + if FMouseDown then + Application.QueueAsyncCall(@PerformEditorHide,FExpandedRow) + else + PerformEditorHide(FExpandedRow); FExpandedRow := -1; - if CanFocus then SetFocus; - DoOnResize; - ResetSizes; - DoOnChangeBounds; - if assigned(FOnEditorPanelHide) then - OnEditorPanelHide(self); end; end; procedure TDBDynamicGrid.DoEditorShow; begin + if assigned(DataSource) and assigned(DataSource.DataSet) and + DataSource.DataSet.Active then + begin + if (DataSource.DataSet.RecordCount = 0) and (DataSource.DataSet.State <> dsInsert) then + DataSource.DataSet.Append + end; if Editor = FEditorPanel then begin if ExpandEditorPanelBelowRow then @@ -428,6 +439,8 @@ begin FEditorPanel.PerformTab(true); {Select First Control} if assigned(FOnEditorPanelShow) then OnEditorPanelShow(self); + if assigned(Editor) and Editor.Visible then + Editor.SetFocus; end else inherited DoEditorShow; @@ -547,7 +560,11 @@ begin Exit; {ignore these keys if we are in a combobox} if (AControl <> nil) and (AControl is TCustomMemo) - and (Key = VK_RETURN) then Exit; {Ignore Return in a CustomMemo} + and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore Return in a CustomMemo} + + if (AControl <> nil) and (AControl is TCustomGrid) + and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a CustomMemo} + if assigned(FOnKeyDownHander) then OnKeyDownHander(Sender,Key,Shift,Done); if Done then Exit; @@ -570,6 +587,21 @@ begin end end; +procedure TDBDynamicGrid.PerformEditorHide(Data: PtrInt); +var ExpandedRow: integer; +begin + if AppDestroying in Application.Flags then Exit; + ExpandedRow := integer(Data); + if (ExpandedRow >= 0) and (ExpandedRow < RowCount) then + RowHeights[ExpandedRow] := DefaultRowHeight; + if CanFocus then SetFocus; + DoOnResize; + ResetSizes; + DoOnChangeBounds; + if assigned(FOnEditorPanelHide) then + OnEditorPanelHide(self); +end; + procedure TDBDynamicGrid.SetEditorPanel(AValue: TWinControl); begin if FEditorPanel = AValue then Exit; @@ -604,7 +636,8 @@ begin inherited Loaded; if assigned(FEditorPanel) and not (csDesigning in ComponentState)then FEditorPanel.Visible := false; - DoGridResize + if Visible then + DoGridResize end; procedure TDBDynamicGrid.DoOnResize; @@ -628,7 +661,12 @@ procedure TDBDynamicGrid.MouseDown(Butto Y: Integer); var Coord: TGridCoord; begin - inherited MouseDown(Button, Shift, X, Y); + FMouseDown := true; + try + inherited MouseDown(Button, Shift, X, Y); + finally + FMouseDown := false; + end; Coord := MouseCoord(X,Y); if (Coord.X = 0) and (Coord.Y > 0) then @@ -654,6 +692,7 @@ begin inherited UpdateActive; if not (csLoading in ComponentState) and assigned(DataLink) and + assigned(FEditorPanel) and not FEditorPanel.Visible and assigned(DataLink.DataSet) and (DataLink.DataSet.State = dsInsert) then Application.QueueAsyncCall(@DoShowEditorPanel,0); end; @@ -686,7 +725,7 @@ end; procedure TDBDynamicGrid.HideEditorPanel; begin if Editor = FEditorPanel then - EditorMode := false; + EditorMode := false; end; procedure TDBDynamicGrid.ShowEditorPanel; @@ -982,12 +1021,13 @@ begin begin if (FLastColIndex < 0) or (FLastColIndex >= Columns.Count) then Exit; FieldPosition := Parser.GetFieldPosition(Columns[FLastColIndex].FieldName); - if FieldPosition = 0 then Exit; - - if Descending then - Parser.OrderByClause := IntToStr(FieldPosition) + ' desc' - else - Parser.OrderByClause := IntToStr(FieldPosition) + ' asc'; + if FieldPosition > 0 then + begin + if Descending then + Parser.OrderByClause := IntToStr(FieldPosition) + ' desc' + else + Parser.OrderByClause := IntToStr(FieldPosition) + ' asc'; + end; if assigned(FOnUpdateSortOrder) then begin