{------------------------------------------------------------------------------} { Unit Name: StringGridIni Purpose : A string grid that saves the settings Author : Rami Lehtiniemi Date : 2000 Changed : 04.03.2000/vl + better system to change colors (OnBeforeDraw) ToDo : } {------------------------------------------------------------------------------} unit StringGridIni; interface uses {$ifdef DELPHI5} Windows, {$else} Types, {$endif} SysUtils, Classes, {$ifdef CLX} QGraphics, QControls, QForms, QDialogs, QGrids, {$else} // Windows, Messages, Graphics, Controls, Forms, Dialogs, Grids, {$endif} kIniSave; type TStringGridIni = class(TStringGrid) private { Private declarations } FIni: TIniSave; FOnBeforeDraw: TDrawCellEvent; protected { Protected declarations } function GetAsString : string; virtual; procedure SetAsString(s:string); virtual; procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override; public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Loaded; override; procedure DeleteRow(ARow: Longint); override; procedure InsertRow(ARow: Longint); virtual; published { Published declarations } property Ini : TIniSave read FIni write FIni; property OnBeforeDraw : TDrawCellEvent read FOnBeforeDraw write FOnBeforeDraw; property AsString : string read GetAsString write SetAsString stored false; end; procedure Register; implementation uses kDouble; procedure Register; begin RegisterComponents('KaveOptions', [TStringGridIni]); end; procedure TStringGridIni.Loaded; begin inherited; if ( Ini.SaveName = '' ) then Ini.SaveName := Self.Name; Ini.Load; end; constructor TStringGridIni.Create(AOwner: TComponent); begin inherited Create(AOwner); FIni := TIniSave.Create(self); Ini.AutoSave := True; Ini.PropName := 'AsString'; // Ini.IniSection:= 'Grid'; end; destructor TStringGridIni.Destroy; begin Ini.Save; Ini.Free; inherited Destroy; end; procedure TStringGridIni.SetAsString(s:string); // virtual; var i: word; begin if ( csDesigning in ComponentState ) then exit; for i := 0 to Self.ColCount-1 do begin Self.ColWidths[i] := ExtractInt(s,',',Self.DefaultColWidth); end; end; function TStringGridIni.GetAsString : string; // virtual; var i: word; begin Result := ''; if ( csDesigning in ComponentState ) then exit; for i := 0 to Self.ColCount-1 do begin Result := Result + IntToStr(Self.ColWidths[i]) + ','; end; if (Length(result) > 0) then delete(result,Length(result),1); end; procedure TStringGridIni.DrawCell(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState); begin if ( Assigned(OnBeforeDraw) ) then begin OnBeforeDraw(self,ACol,ARow,ARect,AState); {$ifdef CLX} if ( Color <> Canvas.Brush.Color ) then Canvas.FillRect(ARect); {$else} // VCL Canvas draws TextRect with color, CLX not {$endif} end; inherited; end; procedure TStringGridIni.DeleteRow(ARow: Longint); begin inherited; end; procedure TStringGridIni.InsertRow(ARow: Integer); var i:integer; begin if ( ARow > RowCount ) then ARow := RowCount-1; RowCount := RowCount + 1; for i:=RowCount-1 downto ARow+1 do MoveRow(i,i-1); end; end.