dbgrids: Stops propagating ENTER key when modifying a field from Jesus

git-svn-id: trunk@7053 -
This commit is contained in:
mattias 2005-04-03 10:13:34 +00:00
parent 73e0927f4b
commit 9f22ae4900
4 changed files with 207 additions and 79 deletions

View File

@ -60,6 +60,7 @@ Type
FOnUpdateData: TNotifyEvent;
FOnActiveChange: TNotifyEvent;
FOnFocusRequest: TNotifyEvent;
FOnLayoutChange: TNotifyEvent;
function FieldCanModify: Boolean;
function GetCanModify: Boolean;
@ -107,6 +108,7 @@ Type
property OnUpdateData: TNotifyEvent read FOnUpdateData write FOnUpdateData;
property OnActiveChange: TNotifyEvent read FOnActiveChange write FOnActiveChange;
property OnFocusRequest: TNotifyEvent read FOnFocusRequest write FOnFocusRequest;
property OnLayoutChange: TNotifyevent read FOnLayoutChange write FOnLayoutChange;
end;
@ -121,6 +123,7 @@ Type
procedure UpdateData(Sender: TObject);
procedure FocusRequest(Sender: TObject);
procedure ActiveChange(Sender: TObject);
procedure LayoutChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
@ -1053,6 +1056,9 @@ begin
if not Active then
FField := nil;
if not Active then
FField := nil;
if Assigned(FOnActiveChange) then
FOnActiveChange(Self);
end;
@ -1119,6 +1125,8 @@ end;
procedure TFieldDataLink.LayoutChanged;
begin
ValidateField;
if Assigned(FOnLayoutChange) then
FOnLayoutChange(Self);
end;
{ Delphi Help ->
@ -1291,6 +1299,9 @@ end.
{ =============================================================================
$Log$
Revision 1.27 2005/04/03 10:13:34 mattias
dbgrids: Stops propagating ENTER key when modifying a field from Jesus
Revision 1.26 2005/04/01 21:13:09 micha
delete reference to field if closing (from jesus) (fixes bug 694)

View File

@ -67,7 +67,7 @@ type
dgMultiselect
);
TDbGridOptions = set of TDbGridOption;
TDbGridStatusItem = (gsVisibleMove,gsUpdatedData);
TDbGridStatusItem = (gsVisibleMove);
TDbGridStatus = set of TDbGridStatusItem;
type
@ -124,26 +124,46 @@ type
Property VisualControl;
end;
{ TColumn }
TColumnTitle = class(TGridColumnTitle)
protected
function GetDefaultCaption: string; override;
end;
{ TColumn }
TColumn = class(TGridColumn)
private
FDisplayFormat: String;
FDisplayFormatChanged: boolean;
FFieldName: String;
FField: TField;
function GetField: TField;
procedure ApplyDisplayFormat;
function GetDisplayFormat: string;
function GetField: TField;
function IsDisplayFormatStored: boolean;
procedure SetDisplayFormat(const AValue: string);
procedure SetField(const AValue: TField);
procedure SetFieldName(const AValue: String);
function GetDataSet: TDataSet;
function GetDataSet: TDataSet;
protected
procedure LinkField;
function GetDefaultDisplayFormat: string;
function GetDisplayName: string; override;
// FPC 1.0 has TAlignment in the DB unit too
function InternalAlignment(var aValue: {$IFDEF VER1_0}Classes.{$ENDIF}TAlignment): boolean; override;
function InternalDefaultReadOnly: boolean; override;
function InternalVisible(var Avalue: Boolean): boolean; override;
function InternalDefaultWidth: Integer; override;
function GetDefaultAlignment: {$IFDEF VER1_0}Classes.{$ENDIF}TAlignment; override;
function InternalDefaultReadOnly: boolean; override;
function GetDefaultVisible: boolean; override;
function InternalDefaultWidth: Integer; override;
function CreateTitle: TGridColumnTitle; override;
public
function IsDefault: boolean; override;
property Field: TField read GetField write SetField;
published
property FieldName: String read FFieldName write SetFieldName;
property DisplayFormat: string read GetDisplayFormat write SetDisplayFormat
stored IsDisplayFormatStored;
end;
TDbGridColumns = class(TGridColumns)
@ -422,7 +442,10 @@ end;
function CalcCanvasCharWidth(Canvas:TCanvas): integer;
begin
//result := Canvas.TextWidth('W l') div 3;
result := Canvas.TextWidth('MX') div 2;
if Canvas.HandleAllocated then
result := Canvas.TextWidth('MX') div 2
else
Result := 8;
end;
{ TCustomdbGrid }
@ -518,7 +541,6 @@ begin
DebugLn('Inserting=',BoolToStr(dsInsert = aDataSet.State));
{$endif}
FDataLink.Modified := False;
Exclude(FGridStatus, gsUpdatedData);
UpdateActive;
end;
@ -650,7 +672,6 @@ begin
else
Exclude(OldOptions, goediting);
if dgTabs in FOptions then
Include(OldOptions, goTabs)
else
@ -703,7 +724,6 @@ begin
{$ifdef dbgdbgrid}
DebugLn('UpdateData: Chk: Field:=',edField.ASString,' END');
{$endif}
Include(FGridStatus, gsUpdatedData);
EditingColumn(FEditingColumn, False);
end;
end;
@ -823,11 +843,14 @@ end;
function TCustomDbGrid.DefaultFieldColWidth(F: TField): Integer;
begin
if not Canvas.HandleAllocated or (F=nil) then
if not HandleAllocated or (F=nil) then
result:=DefaultColWidth
else begin
if F.DisplayWidth = 0 then
result := Canvas.TextWidth( F.DisplayName ) + 4
if Canvas.HandleAllocated then
result := Canvas.TextWidth( F.DisplayName ) + 4
else
Result := DefaultColWidth
else
result := F.DisplayWidth * CalcCanvasCharWidth(Canvas);
end;
@ -1181,7 +1204,7 @@ begin
end else
if (aRow=0)and(ACol>=FixedCols) then begin
FixRectangle;
Canvas.TextRect(ARect,ARect.Left,ARect.Top,GetColumnTitle(aCol));
Canvas.TextRect(ARect,ARect.Left,ARect.Top,GeTGridColumnTitle(aCol));
end;
end else begin
F := GetFieldFromGridColumn(aCol);
@ -1487,12 +1510,14 @@ end;
procedure TCustomDbGrid.EditingColumn(aCol: Integer; Ok: Boolean);
begin
{$ifdef dbgdbgrid} DebugLn('Dbgrid.EditingColumn INIT aCol=', InttoStr(aCol), ' Ok=', BoolToStr(ok)); {$endif}
if Ok then begin
FEditingColumn := aCol;
FDatalink.Modified := True;
end
else
FEditingColumn := -1;
{$ifdef dbgdbgrid} DebugLn('Dbgrid.EditingColumn END'); {$endif}
end;
procedure TCustomDbGrid.EditorCancelEditing;
@ -1895,7 +1920,7 @@ begin
else
if (DataCol>=FixedCols) then begin
R := FixRectangle();
Canvas.TextRect(R,R.Left,R.Top,GetColumnTitle(DataCol));
Canvas.TextRect(R,R.Left,R.Top,GeTGridColumnTitle(DataCol));
end;
end else begin
F := GetFieldFromGridColumn(DataCol);
@ -2126,6 +2151,38 @@ begin
result := FField;
end;
procedure TColumn.ApplyDisplayFormat;
begin
if (FField <> nil) and FDisplayFormatChanged then begin
if (FField is TNumericField) then
TNumericField(FField).DisplayFormat := DisplayFormat
else if (FField is TDateTimeField) then
TDateTimeField(FField).DisplayFormat := DisplayFormat;
end;
end;
function TColumn.GetDisplayFormat: string;
begin
if not FDisplayFormatChanged then
Result := GetDefaultDisplayFormat
else
result := FDisplayFormat;
end;
function TColumn.IsDisplayFormatStored: boolean;
begin
Result := FDisplayFormatChanged;
end;
procedure TColumn.SetDisplayFormat(const AValue: string);
begin
if (not FDisplayFormatChanged)or(CompareText(AValue, FDisplayFormat)<>0) then begin
FDisplayFormat := AValue;
FDisplayFormatChanged:=True;
ColumnChanged;
end;
end;
procedure TColumn.SetField(const AValue: TField);
begin
if FField <> AValue then begin
@ -2160,30 +2217,43 @@ var
TheGrid: TCustomDbGrid;
begin
TheGrid := TCustomDBGrid(Grid);
if (theGrid<>nil)and(TheGrid.Canvas.HandleAllocated)And(FField<>nil) then
if (theGrid<>nil)and(TheGrid.HandleAllocated)and(FField<>nil) then
result := FField.DisplayWidth * CalcCanvasCharWidth(TheGrid.Canvas)
else
result := 64;
end;
function TColumn.CreateTitle: TGridColumnTitle;
begin
Result := TColumnTitle.Create(Self);
end;
function TColumn.IsDefault: boolean;
begin
result := not FDisplayFormatChanged and (inherited IsDefault());
end;
procedure TColumn.LinkField;
var
TheGrid: TCustomDbGrid;
begin
TheGrid:= TCustomDBGrid(Grid);
if (TheGrid<>nil) and TheGrid.FDatalink.Active then
Field := TheGrid.FDataLink.DataSet.FindField(FFieldName)
else
if (TheGrid<>nil) and TheGrid.FDatalink.Active then begin
Field := TheGrid.FDataLink.DataSet.FindField(FFieldName);
ApplyDisplayFormat;
end else
Field := nil;
end;
function TColumn.InternalAlignment(var aValue: {$ifdef ver1_0}Classes.{$ENDIF}TAlignment): boolean;
function TColumn.GetDefaultDisplayFormat: string;
begin
Result := '';
if FField<>nil then begin
Alignment := {$IFNDEF VER1_0}FField.Alignment{$ELSE}Classes.TAlignment(FField.Alignment){$ENDIF};
Result := True;
end else
Result := False;
if FField is TNumericField then
Result := TNumericField(FField).DisplayFormat
else if FField is TDateTimeField then
Result := TDateTimeField(FField).DisplayFormat
end;
end;
function TColumn.InternalDefaultReadOnly: boolean;
@ -2194,27 +2264,52 @@ begin
Result := ((TheGrid<>nil)and(TheGrid.ReadOnly)) or ((FField<>nil)and(FField.ReadOnly))
end;
function TColumn.InternalVisible(var Avalue: Boolean): boolean;
function TColumn.GetDefaultVisible: boolean;
begin
result := false;
if FField<>nil then begin
result := FField.Visible;
result := true;
end;
if FField<>nil then
result := FField.Visible
else
result := True;
end;
function TColumn.GetDisplayName: string;
begin
if FFieldName='' then
Result := 'Column'
result := 'column'
else
Result:=FFieldName;
end;
function TColumn.GetDefaultAlignment: {$IFDEF VER1_0}Classes.{$ENDIF}TAlignment;
begin
if FField<>nil then
result := {$IFNDEF VER1_0}FField.Alignment{$ELSE}Classes.TAlignment(FField.Alignment){$ENDIF}
else
Result := {$IFDEF VER1_0}Classes.{$ENDIF}taLeftJustify;
end;
{ TColumnTitle }
function TColumnTitle.GetDefaultCaption: string;
begin
with (Column as TColumn) do begin
if FieldName<>'' then begin
if FField<>nil then
Result := Field.DisplayName
else
Result := Fieldname;
end else
Result := inherited GetDefaultCaption;
end;
end;
end.
{
$Log$
Revision 1.39 2005/04/03 10:13:34 mattias
dbgrids: Stops propagating ENTER key when modifying a field from Jesus
Revision 1.38 2005/03/29 21:56:02 marc
* patch from Jesus Reyes

View File

@ -255,7 +255,9 @@ type
property Rows[Row: Integer]: PColRowProps read GetRows write SetRows;
end;
TColumnTitle = class(TPersistent)
{ TColumnTitle }
TGridColumnTitle = class(TPersistent)
private
FColumn: TGridColumn;
FCaption: PChar;
@ -281,6 +283,8 @@ type
procedure SetFont(const AValue: TFont);
procedure SetLayout(const AValue: TTextLayout);
property IsDefaultFont: boolean read FIsDefaultTitleFont;
protected
function GetDefaultCaption: string; virtual;
public
constructor Create(TheColumn: TGridColumn); virtual;
destructor Destroy; override;
@ -295,11 +299,13 @@ type
property Font: TFont read GetFont write SetFont stored IsFontStored;
end;
{ TGridColumn }
TGridColumn = class(TCollectionItem)
private
FButtonStyle: TColumnButtonStyle;
FDropDownRows: Longint;
FTitle: TColumnTitle;
FTitle: TGridColumnTitle;
FWidthChanged: boolean;
FAlignment: ^TAlignment;
@ -348,7 +354,7 @@ type
procedure SetPickList(const AValue: TStrings);
procedure SetReadOnly(const AValue: Boolean);
procedure SetSizePriority(const AValue: Integer);
procedure SetTitle(const AValue: TColumnTitle);
procedure SetTitle(const AValue: TGridColumnTitle);
procedure SetVisible(const AValue: Boolean);
procedure SetWidth(const AValue: Integer);
@ -359,22 +365,22 @@ type
procedure Changed(AllItems: Boolean);
{$endif}
function GetDisplayName: string; override;
function InternalAlignment(var AValue: TAlignment): boolean; virtual;
function GetDefaultAlignment: TAlignment; virtual;
function InternalDefaultMaxSize: Integer; virtual;
function InternalDefaultMinSize: Integer; virtual;
function InternalDefaultReadOnly: boolean; virtual;
function InternalDefaultWidth: Integer; virtual;
function InternalVisible(var AValue: boolean): boolean; virtual;
function GetDefaultVisible: boolean; virtual;
procedure ColumnChanged;
procedure AllColumnsChange;
function CreateTitle: TColumnTitle; virtual;
function CreateTitle: TGridColumnTitle; virtual;
property IsDefaultFont: boolean read FIsDefaultFont;
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
procedure FillDefaultFont;
function IsDefault: boolean;
function IsDefault: boolean; virtual;
property Grid: TCustomGrid read GetGrid;
property WidthChanged: boolean read FWidthChanged;
@ -391,7 +397,7 @@ type
property PickList: TStrings read GetPickList write SetPickList;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly stored IsReadOnlyStored;
property SizePriority: Integer read GetSizePriority write SetSizePriority stored IsSizePriorityStored;
property Title: TColumnTitle read FTitle write SetTitle;
property Title: TGridColumnTitle read FTitle write SetTitle;
property Width: Integer read GetWidth write SetWidth stored IsWidthStored default 64;
property Visible: Boolean read GetVisible write SetVisible stored IsVisibleStored default true;
end;
@ -657,7 +663,7 @@ type
function GetColumnFont(Column: Integer; ForTitle: Boolean): TFont;
function GetColumnLayout(Column: Integer; ForTitle: boolean): TTextLayout;
function GetColumnReadOnly(Column: Integer): boolean;
function GetColumnTitle(Column: Integer): string;
function GeTGridColumnTitle(Column: Integer): string;
function GetColumnWidth(Column: Integer): Integer;
function GetDefaultAlignment(Column: Integer): TAlignment; virtual;
function GetDefaultEditor(Column: Integer): TWinControl;
@ -4777,13 +4783,17 @@ begin
VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN,
VK_PRIOR, VK_NEXT:
begin
if ssShift in Shift then
if ssShift in Shift then begin
FeditorKey:=False;
exit;
end;
end;
VK_TAB:
begin
if GoTabs in Options then
if GoTabs in Options then begin
MoveNextAuto;
Key := 0;
end;
end;
VK_RETURN:
begin
@ -4792,6 +4802,7 @@ begin
if EditorAlwaysShown then
EditorShow(True);
end;
Key := 0;
end;
end;
FEditorKey:=False;
@ -4964,7 +4975,7 @@ begin
result := GetDefaultReadOnly(Column);
end;
function TCustomGrid.GetColumnTitle(Column: Integer): string;
function TCustomGrid.GeTGridColumnTitle(Column: Integer): string;
var
C: TGridColumn;
begin
@ -6388,15 +6399,15 @@ begin
end;
{ TColumnTitle }
{ TGridColumnTitle }
procedure TColumnTitle.FontChanged(Sender: TObject);
procedure TGridColumnTitle.FontChanged(Sender: TObject);
begin
FisDefaultTitleFont := False;
FColumn.ColumnChanged;
end;
function TColumnTitle.GetAlignment: TAlignment;
function TGridColumnTitle.GetAlignment: TAlignment;
begin
if FAlignment = nil then
result := taLeftJustify
@ -6404,15 +6415,15 @@ begin
result := FAlignment^;
end;
function TColumnTitle.GetCaption: string;
function TGridColumnTitle.GetCaption: string;
begin
if FCaption = nil then
Result := 'Title'
result := GetDefaultCaption
else
result := FCaption;
end;
function TColumnTitle.GetColor: TColor;
function TGridColumnTitle.GetColor: TColor;
begin
if FColor = nil then
if FColumn.Grid <> nil then
@ -6423,7 +6434,7 @@ begin
result := FColor^;
end;
procedure TColumnTitle.FillTitleDefaultFont;
procedure TGridColumnTitle.FillTitleDefaultFont;
var
TheGrid: TCustomGrid;
begin
@ -6435,12 +6446,12 @@ begin
FFont.Assign( FColumn.Font );
end;
function TColumnTitle.GetFont: TFont;
function TGridColumnTitle.GetFont: TFont;
begin
Result := FFont;
end;
function TColumnTitle.GetLayout: TTextLayout;
function TGridColumnTitle.GetLayout: TTextLayout;
begin
if FLayout = nil then
result := tlCenter
@ -6448,32 +6459,32 @@ begin
result := FLayout^;
end;
function TColumnTitle.IsAlignmentStored: boolean;
function TGridColumnTitle.IsAlignmentStored: boolean;
begin
result := FAlignment <> nil;
end;
function TColumnTitle.IsCaptionStored: boolean;
function TGridColumnTitle.IsCaptionStored: boolean;
begin
result := FCaption <> nil;
end;
function TColumnTitle.IsColorStored: boolean;
function TGridColumnTitle.IsColorStored: boolean;
begin
result := FColor <> nil;
end;
function TColumnTitle.IsFontStored: boolean;
function TGridColumnTitle.IsFontStored: boolean;
begin
result := FFont <> nil;
end;
function TColumnTitle.IsLayoutStored: boolean;
function TGridColumnTitle.IsLayoutStored: boolean;
begin
result := FLayout <> nil;
end;
procedure TColumnTitle.SetAlignment(const AValue: TAlignment);
procedure TGridColumnTitle.SetAlignment(const AValue: TAlignment);
begin
if Falignment = nil then
New(Falignment)
@ -6483,7 +6494,7 @@ begin
FColumn.ColumnChanged;
end;
procedure TColumnTitle.SetCaption(const AValue: string);
procedure TGridColumnTitle.SetCaption(const AValue: string);
begin
if (FCaption=nil)or(CompareText(AValue, FCaption^)<>0) then begin
if FCaption<>nil then
@ -6495,7 +6506,7 @@ begin
end;
end;
procedure TColumnTitle.SetColor(const AValue: TColor);
procedure TGridColumnTitle.SetColor(const AValue: TColor);
begin
if FColor=nil then
New(FColor)
@ -6505,14 +6516,14 @@ begin
FColumn.ColumnChanged;
end;
procedure TColumnTitle.SetFont(const AValue: TFont);
procedure TGridColumnTitle.SetFont(const AValue: TFont);
begin
if AValue.Handle<>FFont.Handle then begin
FFont.Assign(AValue);
end;
end;
procedure TColumnTitle.SetLayout(const AValue: TTextLayout);
procedure TGridColumnTitle.SetLayout(const AValue: TTextLayout);
begin
if FLayout = nil then
New(FLayout)
@ -6522,7 +6533,12 @@ begin
FColumn.ColumnChanged;
end;
constructor TColumnTitle.Create(TheColumn: TGridColumn);
function TGridColumnTitle.GetDefaultCaption: string;
begin
Result := 'Title'
end;
constructor TGridColumnTitle.Create(TheColumn: TGridColumn);
begin
inherited Create;
FColumn := TheColumn;
@ -6533,7 +6549,7 @@ begin
FFont.OnChange := @FontChanged;
end;
destructor TColumnTitle.Destroy;
destructor TGridColumnTitle.Destroy;
begin
if FFont<>nil then FFont.Free;
if FAlignment<>nil then Dispose(FAlignment);
@ -6543,7 +6559,7 @@ begin
inherited Destroy;
end;
function TColumnTitle.IsDefault: boolean;
function TGridColumnTitle.IsDefault: boolean;
begin
result := (FAlignment=nil) and (FColor=nil) and (FCaption=nil) and
IsDefaultFont and (FLayout=nil);
@ -6559,10 +6575,9 @@ end;
function TGridColumn.GetAlignment: TAlignment;
begin
if FAlignment=nil then begin
if not InternalAlignment(Result) then
result := taLeftJustify;
end else
if FAlignment=nil then
Result := GetDefaultAlignment
else
result := FAlignment^;
end;
@ -6646,8 +6661,7 @@ end;
function TGridColumn.GetVisible: Boolean;
begin
if FVisible=nil then begin
if not InternalVisible(result) then
result := True
result := GetDefaultVisible;
end else
result := FVisible^;
end;
@ -6807,7 +6821,7 @@ begin
ColumnChanged;
end;
procedure TGridColumn.SetTitle(const AValue: TColumnTitle);
procedure TGridColumn.SetTitle(const AValue: TGridColumnTitle);
begin
FTitle.Assign(AValue);
end;
@ -6838,9 +6852,9 @@ begin
result := false;
end;
function TGridColumn.InternalVisible(var AValue: boolean): boolean;
function TGridColumn.GetDefaultVisible: boolean;
begin
result := true;
Result := True;
end;
function TGridColumn.InternalDefaultWidth: Integer;
@ -6870,10 +6884,9 @@ begin
Result := 'GridColumn';
end;
function TGridColumn.Internalalignment(var AValue: TAlignment
): boolean;
function TGridColumn.GetDefaultAlignment: TAlignment;
begin
result := false;
result := taLeftJustify;
end;
procedure TGridColumn.ColumnChanged;
@ -6895,9 +6908,9 @@ begin
end;
{$endif}
function TGridColumn.CreateTitle: TColumnTitle;
function TGridColumn.CreateTitle: TGridColumnTitle;
begin
result := TColumnTitle.Create(Self);
result := TGridColumnTitle.Create(Self);
end;
constructor TGridColumn.Create(ACollection: TCollection);

View File

@ -56,6 +56,11 @@ begin
end;
end;
procedure TDBEdit.LayoutChange(Sender: TObject);
begin
DataChange(Sender);
end;
procedure TDBEdit.EditingChange(Sender: TObject);
begin
@ -221,6 +226,7 @@ begin
FDataLink.OnEditingChange := @EditingChange;
FDataLink.OnUpdateData := @UpdateData;
FDataLInk.OnActiveChange := @ActiveChange;
FDatalink.OnLayoutChange := @LayoutChange;
inherited ReadOnly := True;//start read only
end;
@ -235,6 +241,9 @@ end;
{ =============================================================================
$Log$
Revision 1.12 2005/04/03 10:13:34 mattias
dbgrids: Stops propagating ENTER key when modifying a field from Jesus
Revision 1.11 2005/01/06 13:22:24 micha
fix access violation with dbedit (from loesje)