23 |
|
* Contributor(s): ______________________________________. |
24 |
|
* |
25 |
|
*) |
26 |
+ |
|
27 |
|
unit IBDynamicGrid; |
28 |
|
|
29 |
|
{$mode objfpc}{$H+} |
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; |
364 |
|
FResizing := true; |
365 |
|
try |
366 |
|
ColSum := 0; |
364 |
– |
for I := 0 to ColCount - 1 do |
365 |
– |
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; |
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; |
408 |
– |
if CanFocus then SetFocus; |
409 |
– |
DoOnResize; |
410 |
– |
ResetSizes; |
411 |
– |
DoOnChangeBounds; |
412 |
– |
if assigned(FOnEditorPanelHide) then |
413 |
– |
OnEditorPanelHide(self); |
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 |
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; |
560 |
|
Exit; {ignore these keys if we are in a combobox} |
561 |
|
|
562 |
|
if (AControl <> nil) and (AControl is TCustomMemo) |
563 |
< |
and (Key = VK_RETURN) then Exit; {Ignore Return in a CustomMemo} |
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; |
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; |
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; |
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 |
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; |
725 |
|
procedure TDBDynamicGrid.HideEditorPanel; |
726 |
|
begin |
727 |
|
if Editor = FEditorPanel then |
728 |
< |
EditorMode := false; |
728 |
> |
EditorMode := false; |
729 |
|
end; |
730 |
|
|
731 |
|
procedure TDBDynamicGrid.ShowEditorPanel; |
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 |