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

Comparing ibx/trunk/ibcontrols/IBDynamicGrid.pas (file contents):
Revision 27 by tony, Tue Apr 14 13:10:23 2015 UTC vs.
Revision 35 by tony, Tue Jan 26 14:38:47 2016 UTC

# Line 23 | Line 23
23   *  Contributor(s): ______________________________________.
24   *
25   *)
26 +
27   unit IBDynamicGrid;
28  
29   {$mode objfpc}{$H+}
# Line 91 | Line 92 | type
92      FListSource: TDataSource;
93      FOnAutoInsert: TAutoInsert;
94      FOnCanAutoInsert: TCanAutoInsert;
95 +    FOnCloseUp: TNotifyEvent;
96      FOnDrawItem: TDrawItemEvent;
97      FOwner: TIBDynamicGridColumn;
98      FRelationName: string;
# Line 117 | Line 119 | type
119      property Style: TComboBoxStyle read FStyle write FStyle default csDropDown;
120      property OnAutoInsert: TAutoInsert read FOnAutoInsert write FOnAutoInsert;
121      property OnCanAutoInsert: TCanAutoInsert read FOnCanAutoInsert write FOnCanAutoInsert;
122 +    property OnCloseUp: TNotifyEvent read FOnCloseUp write FOnCloseUp;
123      property OnDrawItem: TDrawItemEvent read FOnDrawItem write FOnDrawItem;
124   end;
125  
# Line 177 | Line 180 | end;
180      FWeHaveFocus: boolean;
181      FHidingEditorPanel: boolean;
182      FAllowHide: boolean;
183 +    FMouseDown: boolean;
184 +    function ActiveControl: TControl;
185      procedure DoShowEditorPanel(Data: PtrInt);
186      procedure PositionTotals;
187      procedure KeyDownHandler(Sender: TObject; var Key: Word; Shift: TShiftState);
188 +    procedure PerformEditorHide(Data: PtrInt);
189      procedure SetEditorPanel(AValue: TWinControl);
190    protected
191      procedure ChangeBounds(ALeft, ATop, AWidth, AHeight: integer; KeepBase: boolean); override;
# Line 358 | Line 364 | begin
364    FResizing := true;
365    try
366      ColSum := 0;
361    for I := 0 to  ColCount - 1 do
362       ColSum := ColSum + ColWidths[I];
367  
368 <    if Colsum <> ClientWidth then
368 >    if (ColCount = 1) and TDBDynamicGridColumn(Columns[0]).AutoSizeColumn then
369 >      Columns[0].Width := ClientWidth
370 >    else
371      begin
372 <      ResizeColCount := 0;
373 <      for I := 0 to Columns.Count -1 do
374 <        if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then
375 <        begin
376 <          Inc(ResizeColCount);
377 <          Colsum := Colsum + TDBDynamicGridColumn(Columns[I]).DesignWidth - Columns[I].Width;
378 <          Columns[I].Width := TDBDynamicGridColumn(Columns[I]).DesignWidth;
379 <        end;
380 <
381 <        if (Colsum < ClientWidth) and (ResizeColCount > 0) then
382 <        begin
383 <          adjustment := (ClientWidth - ColSum) div ResizeColCount;
384 <          n := (ClientWidth - ColSum) mod ResizeColCount;
385 <
386 <          for I := 0 to Columns.Count -1 do
387 <            if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then
388 <            begin
389 <              if I = 0 then
390 <                Columns[I].Width := Columns[I].Width + adjustment + n
391 <              else
392 <                Columns[I].Width := Columns[I].Width + adjustment;
393 <            end;
394 <        end;
372 >      for I := 0 to  ColCount - 1 do
373 >         ColSum := ColSum + ColWidths[I];
374 >
375 >      if Colsum <> ClientWidth then
376 >      begin
377 >        ResizeColCount := 0;
378 >        for I := 0 to Columns.Count -1 do
379 >          if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then
380 >          begin
381 >            Inc(ResizeColCount);
382 >            Colsum := Colsum + TDBDynamicGridColumn(Columns[I]).DesignWidth - Columns[I].Width;
383 >            Columns[I].Width := TDBDynamicGridColumn(Columns[I]).DesignWidth;
384 >          end;
385 >
386 >          if (Colsum < ClientWidth) and (ResizeColCount > 0) then
387 >          begin
388 >            adjustment := (ClientWidth - ColSum) div ResizeColCount;
389 >            n := (ClientWidth - ColSum) mod ResizeColCount;
390 >
391 >            for I := 0 to Columns.Count -1 do
392 >              if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then
393 >              begin
394 >                if I = 0 then
395 >                  Columns[I].Width := Columns[I].Width + adjustment + n
396 >                else
397 >                  Columns[I].Width := Columns[I].Width + adjustment;
398 >              end;
399 >          end;
400 >      end;
401      end;
402      PositionTotals;
403      UpdateEditorPanelBounds;
# Line 399 | Line 411 | begin
411    inherited DoEditorHide;
412    if Editor = FEditorPanel then
413    begin
414 <    if (FExpandedRow >= 0) and (FExpandedRow < RowCount) then
415 <      RowHeights[FExpandedRow] := DefaultRowHeight;
414 >    if FMouseDown then
415 >      Application.QueueAsyncCall(@PerformEditorHide,FExpandedRow)
416 >    else
417 >      PerformEditorHide(FExpandedRow);
418      FExpandedRow := -1;
405    if CanFocus then SetFocus;
406    if assigned(FOnEditorPanelHide) then
407       OnEditorPanelHide(self);
408    DoOnResize;
409    ResetSizes;
410    Invalidate;
419    end;
420   end;
421  
422   procedure TDBDynamicGrid.DoEditorShow;
423   begin
424 +  if assigned(DataSource) and assigned(DataSource.DataSet) and
425 +             DataSource.DataSet.Active then
426 +  begin
427 +    if (DataSource.DataSet.RecordCount = 0) and (DataSource.DataSet.State <> dsInsert) then
428 +      DataSource.DataSet.Append
429 +  end;
430    if Editor = FEditorPanel then
431    begin
432      if ExpandEditorPanelBelowRow then
# Line 425 | Line 439 | begin
439      FEditorPanel.PerformTab(true);  {Select First Control}
440      if assigned(FOnEditorPanelShow) then
441         OnEditorPanelShow(self);
442 +    if assigned(Editor) and Editor.Visible then
443 +      Editor.SetFocus;
444    end
445    else
446      inherited DoEditorShow;
# Line 489 | Line 505 | begin
505      inherited KeyDown(Key, Shift);
506   end;
507  
508 + function TDBDynamicGrid.ActiveControl: TControl;
509 + var AParent: TWinControl;
510 + begin
511 +  Result := nil;
512 +  AParent := Parent;
513 +  while (AParent <> nil) and  not (AParent is TCustomForm) do
514 +    AParent := AParent.Parent;
515 +  if (AParent <> nil) and (AParent is TCustomForm)then
516 +      Result := TCustomForm(AParent).ActiveControl;
517 + end;
518 +
519   procedure TDBDynamicGrid.DoShowEditorPanel(Data: PtrInt);
520   begin
521    if AppDestroying in Application.Flags then Exit;
# Line 520 | Line 547 | end;
547   procedure TDBDynamicGrid.KeyDownHandler(Sender: TObject; var Key: Word;
548    Shift: TShiftState);
549   var Done: boolean;
550 +    AControl: TControl;
551   begin
552    if Visible and assigned(FEditorPanel) and FEditorPanel.Visible and FWeHaveFocus then
553    begin
554      Done := false;
555 +    AControl := ActiveControl;
556 +    if (AControl <> nil) and (AControl is TCustomComboBox)
557 +                         and ((Key in [VK_UP,VK_DOWN]) or
558 +                         (TCustomComboBox(AControl).DroppedDown and (Key = VK_RETURN)) or
559 +                         ((TCustomComboBox(AControl).Text <> '') and (Key =  VK_ESCAPE))) then
560 +      Exit; {ignore these keys if we are in a  combobox}
561 +
562 +    if (AControl <> nil) and (AControl is TCustomMemo)
563 +                         and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore Return in a CustomMemo}
564 +
565 +    if (AControl <> nil) and (AControl is TCustomGrid)
566 +                         and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a CustomMemo}
567 +
568      if assigned(FOnKeyDownHander) then
569        OnKeyDownHander(Sender,Key,Shift,Done);
570      if Done then Exit;
# Line 546 | Line 587 | begin
587    end
588   end;
589  
590 + procedure TDBDynamicGrid.PerformEditorHide(Data: PtrInt);
591 + var ExpandedRow: integer;
592 + begin
593 +  if AppDestroying in Application.Flags then Exit;
594 +  ExpandedRow := integer(Data);
595 +  if (ExpandedRow >= 0) and (ExpandedRow < RowCount) then
596 +    RowHeights[ExpandedRow] := DefaultRowHeight;
597 +  if CanFocus then SetFocus;
598 +  DoOnResize;
599 +  ResetSizes;
600 +  DoOnChangeBounds;
601 +  if assigned(FOnEditorPanelHide) then
602 +     OnEditorPanelHide(self);
603 + end;
604 +
605   procedure TDBDynamicGrid.SetEditorPanel(AValue: TWinControl);
606   begin
607    if FEditorPanel = AValue then Exit;
# Line 580 | Line 636 | begin
636    inherited Loaded;
637    if assigned(FEditorPanel) and not (csDesigning in ComponentState)then
638      FEditorPanel.Visible := false;
639 <  DoGridResize
639 >  if Visible then
640 >    DoGridResize
641   end;
642  
643   procedure TDBDynamicGrid.DoOnResize;
# Line 604 | Line 661 | procedure TDBDynamicGrid.MouseDown(Butto
661    Y: Integer);
662   var Coord: TGridCoord;
663   begin
664 <  inherited MouseDown(Button, Shift, X, Y);
664 >  FMouseDown := true;
665 >  try
666 >    inherited MouseDown(Button, Shift, X, Y);
667 >  finally
668 >    FMouseDown := false;
669 >  end;
670  
671    Coord := MouseCoord(X,Y);
672    if (Coord.X = 0) and (Coord.Y > 0) then
# Line 630 | Line 692 | begin
692    inherited UpdateActive;
693  
694    if not (csLoading in ComponentState) and assigned(DataLink) and
695 +     assigned(FEditorPanel) and not FEditorPanel.Visible and
696       assigned(DataLink.DataSet) and (DataLink.DataSet.State = dsInsert) then
697       Application.QueueAsyncCall(@DoShowEditorPanel,0);
698   end;
# Line 662 | Line 725 | end;
725   procedure TDBDynamicGrid.HideEditorPanel;
726   begin
727    if Editor = FEditorPanel then
728 <    EditorMode := false;
728 >      EditorMode := false;
729   end;
730  
731   procedure TDBDynamicGrid.ShowEditorPanel;
# Line 770 | Line 833 | begin
833    FCol := Msg.Col;
834    FRow := Msg.Row;
835    FEditText := Msg.Value;
773  SelStart := Length(Text);
836    TIBDynamicGrid(FGrid).SetupEditor(self,FCol);
837   end;
838  
# Line 819 | Line 881 | begin
881      Editor.OnAutoInsert := OnAutoInsert;
882      Editor.OnCanAutoInsert := OnCanAutoInsert;
883      Editor.OnDrawItem := OnDrawItem;
884 +    Editor.OnCloseUp := OnCloseUp;
885  
886      {Setup Data Links}
887      if KeyField <> '' then
# Line 844 | Line 907 | begin
907          Editor.DataSource := TDBGrid(Grid).DataSource;
908    end;
909    Editor.Text := Editor.FEditText;
910 +  Editor.SelStart := Length(Editor.Text);
911   end;
912  
913   procedure TIBDynamicGridColumn.SetInitialSortColumn(AValue: boolean);
# Line 957 | Line 1021 | begin
1021      begin
1022        if (FLastColIndex < 0) or (FLastColIndex >= Columns.Count) then Exit;
1023        FieldPosition := Parser.GetFieldPosition(Columns[FLastColIndex].FieldName);
1024 <      if FieldPosition = 0 then Exit;
1025 <
1026 <      if Descending then
1027 <        Parser.OrderByClause := IntToStr(FieldPosition) + ' desc'
1028 <      else
1029 <        Parser.OrderByClause := IntToStr(FieldPosition) + ' asc';
1024 >      if FieldPosition > 0 then
1025 >      begin
1026 >        if Descending then
1027 >          Parser.OrderByClause := IntToStr(FieldPosition) + ' desc'
1028 >        else
1029 >          Parser.OrderByClause := IntToStr(FieldPosition) + ' asc';
1030 >      end;
1031  
1032        if assigned(FOnUpdateSortOrder) then
1033        begin
# Line 1049 | Line 1114 | end;
1114   procedure TIBDynamicGrid.Loaded;
1115   begin
1116    inherited Loaded;
1117 +  IBControlLinkChanged;
1118    ProcessColumns;
1119   end;
1120  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines