📄 jvqstringgrid.pas
字号:
{******************************************************************************}
{* WARNING: JEDI VCL To CLX Converter generated unit. *}
{* Manual modifications will be lost on next release. *}
{******************************************************************************}
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain A copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvStringGrid.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is S閎astien Buysse [sbuysse att buypin dott com]
Portions created by S閎astien Buysse are Copyright (C) 2001 S閎astien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvQStringGrid.pas,v 1.18 2004/12/21 09:45:18 asnepvangers Exp $
unit JvQStringGrid;
{$I jvcl.inc}
interface
uses
QWindows, QMessages, SysUtils, Classes, QGraphics, QControls, QForms, QGrids, QStdCtrls,
JvQTypes, JvQJCLUtils, JvQExGrids;
const
GM_ACTIVATECELL = WM_USER + 123;
type
// (rom) renamed elements made packed
TGMActivateCell = packed record
Msg: Cardinal;
Column: Integer;
Row: Integer;
Result: Integer;
end;
TJvStringGrid = class;
TExitCellEvent = procedure(Sender: TJvStringGrid; AColumn, ARow: Integer;
const EditText: string) of object;
TGetCellAlignmentEvent = procedure(Sender: TJvStringGrid; AColumn, ARow: Integer;
State: TGridDrawState; var CellAlignment: TAlignment) of object;
TCaptionClickEvent = procedure(Sender: TJvStringGrid; AColumn, ARow: Integer) of object;
TJvSortType = (stNone, stAutomatic, stClassic, stCaseSensitive, stNumeric, stDate, stCurrency);
TProgress = procedure(Sender: TObject; Progression, Total: Integer) of object;
TJvStringGrid = class(TJvExStringGrid)
private
FAlignment: TAlignment;
FSetCanvasProperties: TDrawCellEvent;
FGetCellAlignment: TGetCellAlignmentEvent;
FCaptionClick: TCaptionClickEvent;
FCellOnMouseDown: TGridCoord;
FOnExitCell: TExitCellEvent;
FOnLoadProgress: TProgress;
FOnSaveProgress: TProgress;
FOnHorizontalScroll: TNotifyEvent;
FOnVerticalScroll: TNotifyEvent;
FFixedFont: TFont;
procedure SetAlignment(const Value: TAlignment);
procedure GMActivateCell(var Msg: TGMActivateCell); message GM_ACTIVATECELL;
procedure SetFixedFont(const Value: TFont);
procedure DoFixedFontChange(Sender: TObject);
protected
function CreateEditor: TInplaceEdit; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure ExitCell(const EditText: string; AColumn, ARow: Integer); virtual;
procedure SetCanvasProperties(AColumn, ARow: Longint;
Rect: TRect; State: TGridDrawState); virtual;
procedure DrawCell(AColumn, ARow: Longint;
Rect: TRect; State: TGridDrawState); override;
procedure CaptionClick(AColumn, ARow: Longint); dynamic;
procedure ModifyScrollBar(ScrollBar: TScrollBarKind; ScrollCode: TScrollCode;
Pos: Cardinal; UseRightToLeft: Boolean); override;
function SelectCell(ACol, ARow: Longint): Boolean; override;
procedure DoLoadProgress(Position, Count: Integer);
procedure DoSaveProgress(Position, Count: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetCellAlignment(AColumn, ARow: Longint;
State: TGridDrawState): TAlignment; virtual;
procedure DefaultDrawCell(AColumn, ARow: Longint;
Rect: TRect; State: TGridDrawState); virtual;
procedure ActivateCell(AColumn, ARow: Integer);
procedure InvalidateCell(AColumn, ARow: Integer);
procedure InvalidateCol(AColumn: Integer);
procedure InvalidateRow(ARow: Integer);
procedure MoveColumn(FromIndex, ToIndex: Integer);
procedure MoveRow(FromIndex, ToIndex: Longint);
property InplaceEditor;
// Calculates and sets the width of a specific column or all columns if Index < 0
// based on the text in the affected Cells.
// MinWidth is the minimum width of the column(s). If MinWidth is < 0,
// DefaultColWidth is used instead
procedure AutoSizeCol(Index, MinWidth: Integer);
// Inserts a new row at the specified Index and moves all existing rows >= Index down one step
// Returns the inserted row as an empty TStrings
function InsertRow(Index: Integer): TStrings;
// Inserts a new column at the specified Index and moves all existing columns >= Index to the right
// Returns the inserted column as an empty TStrings
function InsertCol(Index: Integer): TStrings;
// Removes the row at Index and moves all rows > Index up one step
procedure RemoveRow(Index: Integer);
// Removes the column at Index and moves all cols > Index to the left
procedure RemoveCol(Index: Integer);
// Hides the row at Index by setting it's height = -1
// Calling this method repeatedly does nothing (the row retains it's Index even if it's hidden)
procedure HideRow(Index: Integer);
// Shows the row at Index by setting it's height to AHeight
// if AHeight <= 0, DefaultRowHeight is used instead
procedure ShowRow(Index, AHeight: Integer);
// Hides the column at Index by setting it's ColWidth = -1
// Calling this method repeatedly does nothing (the column retains it's Index even if it's hidden)
procedure HideCol(Index: Integer);
// Returns True if the Cell at ACol/ARow is hidden, i.e if it's RowHeight or ColWidth < 0
function IsHidden(ACol, ARow: Integer): Boolean;
// Shows the column at Index by setting it's width to AWidth
// If AWidth <= 0, DefaultColWidth is used instead
procedure ShowCol(Index, AWidth: Integer);
// HideCell hides a cell by hiding the row and column that it belongs to.
// This means that both a row and a column is hidden
procedure HideCell(ACol, ARow: Integer);
// ShowCell shows a previously hidden cell by showing it's corresponding row and column and
// using AWidth/AHeight to set it's size. If AWidth < 0, DefaultColWidth is used instead.
// If AHeight < 0, DefaultRowHeight is used instead. If one dimension of the Cell wasn't
// hidden, nothing happens to that dimension (i.e if ColWidth < 0 but RowHeight := 24, only ColWidth is
// changed to AWidth
procedure ShowCell(ACol, ARow, AWidth, AHeight: Integer);
// Removes the content in the Cells but does not remove any rows or columns
procedure Clear;
// Hides all rows and columns
procedure HideAll;
// Shows all hidden rows and columns, setting their width/height to AWidth/AHeight as necessary
// If AWidth < 0, DefaultColWidth is used. If AHeight < 0, DefaultRowHeight is used
procedure ShowAll(AWidth, AHeight: Integer);
procedure SortGrid(Column: Integer; Ascending: Boolean = True; Fixed: Boolean = False;
SortType: TJvSortType = stClassic; BlankTop: Boolean = True);
// Sort grid using the column inidices in ColOrder. For example if ColOrder contains
// [1, 3, 0, 2], column 3 is used when the items in column 1 are identical
procedure SortGridByCols(ColOrder: array of Integer);
procedure SaveToFile(FileName: string);
procedure LoadFromFile(FileName: string);
procedure LoadFromCSV(FileName: string; Separator: Char = ';'; QuoteChar: Char = '"'; StripQuotes: Boolean = True);
procedure SaveToCSV(FileName: string; Separator: Char = ';'; QuoteChar: Char = '"');
procedure LoadFromStream(Stream: TStream);
procedure SaveToStream(Stream: TStream);
published
property HintColor;
property Alignment: TAlignment read FAlignment write SetAlignment;
property FixedFont: TFont read FFixedFont write SetFixedFont;
property OnExitCell: TExitCellEvent read FOnExitCell write FOnExitCell;
property OnSetCanvasProperties: TDrawCellEvent read FSetCanvasProperties write FSetCanvasProperties;
property OnGetCellAlignment: TGetCellAlignmentEvent read FGetCellAlignment write FGetCellAlignment;
property OnCaptionClick: TCaptionClickEvent read FCaptionClick write FCaptionClick;
property OnMouseEnter;
property OnMouseLeave;
property OnParentColorChange;
property OnLoadProgress: TProgress read FOnLoadProgress write FOnLoadProgress;
property OnSaveProgress: TProgress read FOnSaveProgress write FOnSaveProgress;
property OnVerticalScroll: TNotifyEvent read FOnVerticalScroll write FOnVerticalScroll;
property OnHorizontalScroll: TNotifyEvent read FOnHorizontalScroll write FOnHorizontalScroll;
end;
implementation
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Math,
JvQJVCLUtils;
const
BufSize = 1024;
//=== { TExInplaceEdit } =====================================================
type
TExInplaceEdit = class(TJvExInplaceEdit)
private
FLastCol: Integer;
FLastRow: Integer;
protected
procedure DoEnter; override;
procedure DoExit; override;
public
end;
procedure TExInplaceEdit.DoExit;
begin
TJvStringGrid(Grid).ExitCell(Text, FLastCol, FLastRow);
inherited DoExit;
end;
procedure TExInplaceEdit.DoEnter;
begin
FLastCol := TJvStringGrid(Grid).Col;
FLastRow := TJvStringGrid(Grid).Row;
inherited DoEnter;
end;
//=== { TJvStringGrid } ======================================================
constructor TJvStringGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFixedFont := TFont.Create;
FFixedFont.Assign(Font);
FFixedFont.OnChange := DoFixedFontChange;
// ControlStyle := ControlStyle + [csAcceptsControls];
end;
destructor TJvStringGrid.Destroy;
begin
FreeAndNil(FFixedFont);
inherited Destroy;
end;
procedure TJvStringGrid.SortGrid(Column: Integer;
Ascending, Fixed: Boolean; SortType: TJvSortType; BlankTop: Boolean);
const
cFloatDelta = 0.01;
var
St: string;
TmpC: Currency;
TmpF: Extended;
TmpD: TDateTime;
LStart: Integer;
LEnd: Integer;
procedure ExchangeGridRows(I, J: Integer);
var
K: Integer;
begin
if Fixed then
for K := 0 to ColCount - 1 do
Cols[K].Exchange(I, J)
else
for K := FixedCols to ColCount - 1 do
Cols[K].Exchange(I, J);
end;
function IsSmaller(First, Second: string): Boolean;
function DetectType(const S1, S2: string): TJvSortType;
var
ExtValue: Extended;
CurrValue: Currency;
DateValue: TDateTime;
begin
if TextToFloat(PChar(S1), ExtValue, fvExtended) and TextToFloat(PChar(S2), ExtValue, fvExtended) then
Result := stNumeric
else
if TextToFloat(PChar(S1), CurrValue, fvCurrency) and TextToFloat(PChar(S2), CurrValue, fvCurrency) then
Result := stCurrency
else
if TryStrToDateTime(S1, DateValue) and TryStrToDateTime(S2, DateValue) then
Result := stDate
else
Result := stClassic;
end;
begin
case DetectType(First, Second) of
stNumeric:
Result := StrToFloat(First) < StrToFloat(Second);
stCurrency:
Result := StrToCurr(First) < StrToCurr(Second);
stDate:
Result := StrToDateTime(First) < StrToDateTime(Second);
stClassic:
Result := AnsiCompareText(First, Second) < 0;
else
Result := First > Second;
end;
end;
function IsBigger(First, Second: string): Boolean;
begin
Result := IsSmaller(Second, First);
end;
// (rom) A HeapSort has no worst case for O(X)
// (rom) I donated one a long time ago to JCL
// (p3) maybe implemented a secondary sort index when items are equal?
// (p3) ...or use another stable sort method, like heapsort
procedure QuickSort(L, R: Integer);
var
I, J, m: Integer;
begin
repeat
I := L;
J := R;
m := (L + R) div 2;
St := Cells[Column, m];
repeat
case SortType of
stClassic:
begin
while AnsiCompareText(Cells[Column, I], St) < 0 do
Inc(I);
while AnsiCompareText(Cells[Column, J], St) > 0 do
Dec(J);
end;
stCaseSensitive:
begin
while AnsiCompareStr(Cells[Column, I], St) < 0 do
Inc(I);
while AnsiCompareStr(Cells[Column, J], St) > 0 do
Dec(J);
end;
stNumeric:
begin
TmpF := StrToFloat(St);
while StrToFloat(Cells[Column, I]) < TmpF do
Inc(I);
while StrToFloat(Cells[Column, J]) > TmpF do
Dec(J);
end;
stDate:
begin
TmpD := StrToDateTime(St);
while StrToDateTime(Cells[Column, I]) < TmpD do
Inc(I);
while StrToDateTime(Cells[Column, J]) > TmpD do
Dec(J);
end;
stCurrency:
begin
TmpC := StrToCurr(St);
while StrToCurr(Cells[Column, I]) < TmpC do
Inc(I);
while StrToCurr(Cells[Column, J]) > TmpC do
Dec(J);
end;
stAutomatic:
begin
while IsSmaller(Cells[Column, I], St) do
Inc(I);
while IsBigger(Cells[Column, J], St) do
Dec(J);
end;
end;
if I <= J then
begin
if I <> J then
ExchangeGridRows(I, J);
Inc(I);
Dec(J);
end;
until (I > J);
if L < J then
QuickSort(L, J);
L := I;
until I >= R;
end;
procedure InvertGrid;
var
I, J: Integer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -