diff options
author | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2007-08-11 13:31:33 +0000 |
---|---|---|
committer | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2007-08-11 13:31:33 +0000 |
commit | ebafc605dc3551b20ec7a8fd6f1d8f068222c86a (patch) | |
tree | 97e704536cbd5c52fac97044b4c77f0c00e55ff0 | |
parent | 71eb8ce02629a33957b7f6c0733c1406b46678a0 (diff) | |
download | fpGUI-ebafc605dc3551b20ec7a8fd6f1d8f068222c86a.tar.xz |
* Renamed the unit gui_grid.pas to gui_basegrid.pas
* Implemented and added a TfpgCustomGrid.
* GridTest example has been updated to demo the TfpgCustomGrid.
-rw-r--r-- | examples/gui/gridtest/gridtest.lpr | 12 | ||||
-rw-r--r-- | src/gui/fpgui_package.lpk | 10 | ||||
-rw-r--r-- | src/gui/fpgui_package.pas | 4 | ||||
-rw-r--r-- | src/gui/gui_basegrid.pas (renamed from src/gui/gui_grid.pas) | 30 | ||||
-rw-r--r-- | src/gui/gui_customgrid.pas | 215 |
5 files changed, 252 insertions, 19 deletions
diff --git a/examples/gui/gridtest/gridtest.lpr b/examples/gui/gridtest/gridtest.lpr index 46ae250b..3f461155 100644 --- a/examples/gui/gridtest/gridtest.lpr +++ b/examples/gui/gridtest/gridtest.lpr @@ -7,9 +7,10 @@ uses cthreads, {$ENDIF}{$ENDIF} Classes, + SysUtils, fpgfx, gui_form, - gui_grid, + gui_customgrid, gui_button, gui_checkbox; @@ -19,7 +20,7 @@ type TMainForm = class(TfpgForm) private btnQuit: TfpgButton; - grdMain: TfpgBaseGrid; + grdMain: TfpgGrid; chkShowHeader: TfpgCheckBox; chkShowGrid: TfpgCheckBox; chkRowSelect: TfpgCheckBox; @@ -61,6 +62,8 @@ begin end; constructor TMainForm.Create(AOwner: TComponent); +var + c: integer; begin inherited Create(AOwner); WindowTitle := 'Grid control test'; @@ -71,12 +74,15 @@ begin btnQuit.ShowImage := True; btnQuit.Anchors := [anRight, anBottom]; - grdMain := TfpgBaseGrid.Create(self); + grdMain := TfpgGrid.Create(self); grdMain.Top := 10; grdMain.Left := 10; grdMain.Width := Width - 20; grdMain.Height := 300; grdMain.Anchors := [anLeft, anTop, anRight, anBottom]; + grdMain.RowCount := 25; + for c := 1 to grdMain.ColumnCount do + grdMain.Columns[c-1].Title := 'Title ' + IntToStr(c); chkShowHeader := CreateCheckBox(self, 10, 320, 'Show Header'); chkShowHeader.Checked := True; diff --git a/src/gui/fpgui_package.lpk b/src/gui/fpgui_package.lpk index 7cb79103..ca4d63a0 100644 --- a/src/gui/fpgui_package.lpk +++ b/src/gui/fpgui_package.lpk @@ -18,7 +18,7 @@ <Description Value="fpGUI - multi-handle redesign"/> <License Value="Modified LGPL"/> <Version Minor="5"/> - <Files Count="17"> + <Files Count="18"> <Item1> <Filename Value="gui_button.pas"/> <UnitName Value="gui_button"/> @@ -80,13 +80,17 @@ <UnitName Value="gui_tab"/> </Item15> <Item16> - <Filename Value="gui_grid.pas"/> - <UnitName Value="gui_grid"/> + <Filename Value="gui_basegrid.pas"/> + <UnitName Value="gui_basegrid"/> </Item16> <Item17> <Filename Value="gui_listview.pas"/> <UnitName Value="gui_listview"/> </Item17> + <Item18> + <Filename Value="gui_customgrid.pas"/> + <UnitName Value="gui_customgrid"/> + </Item18> </Files> <RequiredPkgs Count="2"> <Item1> diff --git a/src/gui/fpgui_package.pas b/src/gui/fpgui_package.pas index 287708f8..61005474 100644 --- a/src/gui/fpgui_package.pas +++ b/src/gui/fpgui_package.pas @@ -9,8 +9,8 @@ interface uses gui_button, gui_combobox, gui_dialogs, gui_edit, gui_form, gui_label, gui_listbox, gui_memo, gui_popupwindow, gui_scrollbar, gui_bevel, - gui_checkbox, gui_radiobutton, gui_trackbar, gui_tab, gui_grid, - gui_listview; + gui_checkbox, gui_radiobutton, gui_trackbar, gui_tab, gui_basegrid, + gui_listview, gui_customgrid; implementation diff --git a/src/gui/gui_grid.pas b/src/gui/gui_basegrid.pas index a0023d9c..7d31022d 100644 --- a/src/gui/gui_grid.pas +++ b/src/gui/gui_basegrid.pas @@ -1,10 +1,9 @@ -unit gui_grid; +unit gui_basegrid; {$mode objfpc}{$H+} { TODO: - * Decendant with TColumn class * Selecting the last fully visible row, scrolls the grid. Selection is corruct, but because of the scroll it is confusing. } @@ -68,8 +67,9 @@ type procedure SetShowHeader(const AValue: boolean); function VisibleLines: integer; function VisibleWidth: integer; - procedure UpdateScrollBars; protected + procedure UpdateScrollBars; virtual; + function GetHeaderText(ACol: integer): string; virtual; function GetColumnWidth(ACol: integer): integer; virtual; procedure SetColumnWidth(ACol: integer; const AValue: integer); virtual; function GetColumnCount: integer; virtual; @@ -88,9 +88,6 @@ type procedure FollowFocus; virtual; property DefaultColWidth: integer read FDefaultColWidth write SetDefaultColWidth default 64; property DefaultRowHeight: integer read FDefaultRowHeight write SetDefaultRowHeight; - public - constructor Create(AOwner: TComponent); override; - destructor Destroy; override; property Font: TfpgFont read FFont; property HeaderFont: TfpgFont read FHeaderFont; property BackgroundColor: TfpgColor read FBackgroundColor write SetBackgroundColor; @@ -106,6 +103,9 @@ type property ColumnWidth[ACol: integer]: integer read GetColumnWidth write SetColumnWidth; property OnFocusChange: TfpgFocusChangeNotify read FOnFocusChange write FOnFocusChange; property OnRowChange: TfpgRowChangeNotify read FOnRowChange write FOnRowChange; + public + constructor Create(AOwner: TComponent); override; + destructor Destroy; override; end; implementation @@ -171,7 +171,7 @@ begin if ACol = 2 then Result := FTemp else - Result := 60+(ACol*16); + Result := FDefaultColWidth+(ACol*16); end; procedure TfpgBaseGrid.SetColumnWidth(ACol: integer; const AValue: integer); @@ -210,6 +210,7 @@ procedure TfpgBaseGrid.DrawHeader(ACol: integer; ARect: TfpgRect; AFlags: intege var s: string; r: TfpgRect; + x: integer; begin // Here we can implement a head style check Canvas.DrawButtonFace(ARect, [btnIsEmbedded]); @@ -230,9 +231,11 @@ begin *) Canvas.SetTextColor(clText1); - s := 'Head ' + IntToStr(ACol); - fpgStyle.DrawString(Canvas, (ARect.Left + (ARect.Width div 2)) - (FHeaderFont.TextWidth(s) div 2), - ARect.Top+1, s, Enabled); + s := GetHeaderText(ACol); + x := (ARect.Left + (ARect.Width div 2)) - (FHeaderFont.TextWidth(s) div 2); + if x < 1 then + x := 1; + fpgStyle.DrawString(Canvas, x, ARect.Top+1, s, Enabled); end; procedure TfpgBaseGrid.DrawGrid(ARow, ACol: integer; ARect: TfpgRect; @@ -350,7 +353,7 @@ begin cw := cw + ColumnWidth[i]; FHScrollBar.Visible := cw > vw; - FVScrollBar.Visible := (RowCount-1 > VisibleLines); + FVScrollBar.Visible := (RowCount > VisibleLines); if FVScrollBar.Visible then begin @@ -385,6 +388,11 @@ begin FHScrollBar.UpdateWindowPosition; end; +function TfpgBaseGrid.GetHeaderText(ACol: integer): string; +begin + Result := 'Head ' + IntToStr(ACol); +end; + procedure TfpgBaseGrid.HandlePaint; var r: TfpgRect; diff --git a/src/gui/gui_customgrid.pas b/src/gui/gui_customgrid.pas new file mode 100644 index 00000000..b4952a69 --- /dev/null +++ b/src/gui/gui_customgrid.pas @@ -0,0 +1,215 @@ +unit gui_customgrid; + +{$mode objfpc}{$H+} + +{ + TODO: + * Column text alignment needs to be implemented. Currently always Centre. +} + +{.$Define DEBUG} + +interface + +uses + Classes, + SysUtils, + gfxbase, + fpgfx, + gui_basegrid; + +type + + // data object for grid columns + TGridColumn = class(TObject) + private + FAlignment: TAlignment; + FTitle: string; + FWidth: integer; + public + constructor Create; + property Width: integer read FWidth write FWidth; + property Title: string read FTitle write FTitle; + property Alignment: TAlignment read FAlignment write FAlignment; + end; + + + TfpgCustomGrid = class(TfpgBaseGrid) + private + FRowCount: integer; + function GetColumns(AIndex: integer): TGridColumn; + protected + FColumns: TList; + function GetColumnCount: integer; override; + procedure SetColumnCount(const AValue: integer); + function GetRowCount: integer; override; + procedure SetRowCount(const AValue: integer); virtual; + function GetColumnWidth(ACol: integer): integer; override; + procedure SetColumnWidth(ACol: integer; const AValue: integer); override; + function GetHeaderText(ACol: integer): string; override; + property RowCount: integer read GetRowCount write SetRowCount; + property ColumnCount: integer read GetColumnCount write SetColumnCount; + property Columns[AIndex: integer]: TGridColumn read GetColumns; + public + constructor Create(AOwner: TComponent); override; + destructor Destroy; override; + function AddColumn(ATitle: string; AWidth: integer): TGridColumn; + end; + + + // Just for testing!!!!! + TfpgGrid = class(TfpgCustomGrid) + published + property Columns; + property DefaultColWidth; + property DefaultRowHeight; + property Font; + property HeaderFont; + property BackgroundColor; + property FocusCol; + property FocusRow; + property RowSelect; + property ColumnCount; + property RowCount; + property ShowHeader; + property ShowGrid; + property HeaderHeight; + property ColResizing; + property ColumnWidth; + property OnFocusChange; + property OnRowChange; + + end; + + +implementation + +{ TGridColumn } + +constructor TGridColumn.Create; +begin + Width := 64; + Title := ''; + Alignment := taCenter; +end; + +{ TfpgCustomGrid } + +function TfpgCustomGrid.GetRowCount: integer; +begin + Result := FRowCount; +end; + +function TfpgCustomGrid.GetColumns(AIndex: integer): TGridColumn; +begin + if (AIndex < 0) or (AIndex > FColumns.Count-1) then + Result := nil + else + Result := TGridColumn(FColumns[AIndex]); +end; + +function TfpgCustomGrid.GetColumnCount: integer; +begin + Result := FColumns.Count; +end; + +procedure TfpgCustomGrid.SetColumnCount(const AValue: integer); +var + n: integer; +begin + n := FColumns.Count; + if (n = AValue) or (AValue < 0) then + Exit; //==> + + if n < AValue then + begin + // adding columns + while n < AValue do + begin + AddColumn('', DefaultColWidth); + inc(n); + end; + end + else + begin + while n > AValue do + begin + TGridColumn(FColumns.Items[n-1]).Free; + dec(n); + FColumns.Count := n; + end; + end; + UpdateScrollBars; + RePaint; +end; + +procedure TfpgCustomGrid.SetRowCount(const AValue: integer); +begin + if FRowCount = AValue then + Exit; //==> + FRowCount := AValue; + if FocusRow > FRowCount then + begin + FocusRow := FRowCount; + end; + RePaint; +end; + +function TfpgCustomGrid.GetColumnWidth(ACol: integer): integer; +begin + if (ACol > 0) and (ACol <= ColumnCount) then + Result := TGridColumn(FColumns[ACol-1]).Width + else + result := DefaultColWidth; +end; + +procedure TfpgCustomGrid.SetColumnWidth(ACol: integer; const AValue: integer); +var + lCol: TGridColumn; +begin + lCol := TGridColumn(FColumns[ACol-1]); + + if lCol.Width <> AValue then + begin + if AValue < 1 then + lCol.Width := 1 + else + lCol.Width := AValue; + UpdateScrollBars; + Repaint; + end; +end; + +function TfpgCustomGrid.GetHeaderText(ACol: integer): string; +begin + Result := TGridColumn(FColumns[ACol-1]).Title; +end; + +constructor TfpgCustomGrid.Create(AOwner: TComponent); +begin + inherited Create(AOwner); + FColumns := TList.Create; + ColumnCount := 5; + RowCount := 5; +end; + +destructor TfpgCustomGrid.Destroy; +begin + SetColumnCount(0); + FColumns.Free; + inherited Destroy; +end; + +function TfpgCustomGrid.AddColumn(ATitle: string; AWidth: integer): TGridColumn; +begin + Result := TGridColumn.Create; + Result.Title := ATitle; + Result.Width := AWidth; + FColumns.Add(Result); + + UpdateScrollBars; + RePaint; +end; + +end. + |