1{ $Id: wsgrids.pp 55146 2017-06-01 19:36:16Z sekelsenmat $}
2{
3 *****************************************************************************
4 *                                WSGrids.pp                                 *
5 *                                ----------                                 *
6 *                                                                           *
7 *                                                                           *
8 *****************************************************************************
9
10 *****************************************************************************
11  This file is part of the Lazarus Component Library (LCL)
12
13  See the file COPYING.modifiedLGPL.txt, included in this distribution,
14  for details about the license.
15 *****************************************************************************
16}
17unit WSGrids;
18
19{$mode objfpc}{$H+}
20{$I lcl_defines.inc}
21
22interface
23////////////////////////////////////////////////////
24// I M P O R T A N T
25////////////////////////////////////////////////////
26// 1) Only class methods allowed
27// 2) Class methods have to be published and virtual
28// 3) To get as little as posible circles, the uses
29//    clause should contain only those LCL units
30//    needed for registration. WSxxx units are OK
31// 4) To improve speed, register only classes in the
32//    initialization section which actually
33//    implement something
34// 5) To enable your XXX widgetset units, look at
35//    the uses clause of the XXXintf.pp
36////////////////////////////////////////////////////
37uses
38////////////////////////////////////////////////////
39// To get as little as posible circles,
40// uncomment only when needed for registration
41////////////////////////////////////////////////////
42  LCLType, Types, Controls, StdCtrls, Grids, LazUTF8, Graphics,
43////////////////////////////////////////////////////
44  WSLCLClasses, WSControls, WSFactory;
45
46type
47  { TWSCustomGrid }
48
49  TWSCustomGrid = class(TWSCustomControl)
50  published
51    class procedure SendCharToEditor(AEditor:TWinControl; Ch: TUTF8Char); virtual;
52    class function InvalidateStartY(const FixedHeight, RowOffset: Integer): integer; virtual;
53    class function GetEditorBoundsFromCellRect(ACanvas: TCanvas;
54      const ACellRect: TRect; const AColumnLayout: TTextLayout): TRect; virtual;
55  end;
56  TWSCustomGridClass = class of TWSCustomgrid;
57
58  { WidgetSetRegistration }
59
60  function RegisterCustomGrid: Boolean;
61
62implementation
63uses
64  LCLIntf;
65
66type
67  TCustomGridAccess=class(TCustomGrid)
68  end;
69
70{ TWSCustomGrid }
71
72class procedure TWSCustomGrid.SendCharToEditor(AEditor:TWinControl;
73  Ch: TUTF8Char);
74var
75  GMsg: TGridMessage;
76  GridEditor: boolean;
77begin
78  GMsg.Grid := nil;
79  GMsg.Options:= 0;
80  GMsg.LclMsg.Msg:=GM_GETGRID;
81  AEditor.Dispatch(GMsg);
82  GridEditor := (GMsg.Options and EO_IMPLEMENTED<>0) and (GMsg.Grid<>nil);
83
84  GMsg.LclMsg.Msg:=GM_SETVALUE;
85  if Ch=#8 then // backspace
86    GMsg.Value:=''
87  else
88    GMsg.Value:=Ch;
89
90  if GridEditor then
91    AEditor.Dispatch(GMsg)
92  else begin
93    // TODO: Find a generic way ...
94    if AEditor is TCustomEdit then begin
95      TCustomEdit(AEditor).Text:=GMsg.Value;
96      TCustomEdit(AEditor).SelStart:=UTF8Length(GMsg.Value);
97    end else
98    if AEditor is TCustomCombobox then begin
99      TCustomCombobox(AEditor).Text:=GMsg.Value;
100      TCustomCombobox(AEditor).SelStart:=UTF8Length(GMsg.Value);
101    end;
102  end;
103
104  // make sure the grid is notified that some text is changed, some
105  // widgets do not notify when they are modified programmatically.
106  if GMsg.Grid<>nil then
107    with TCustomGridAccess(GMsg.Grid) do
108      EditorTextChanged(Col, Row, GMsg.Value);
109end;
110
111class function TWSCustomGrid.GetEditorBoundsFromCellRect(ACanvas: TCanvas;
112  const ACellRect: TRect; const AColumnLayout: TTextLayout): TRect;
113begin
114  Result := ACellRect;
115  Dec(Result.Right);
116  Dec(Result.Bottom);
117end;
118
119class function TWSCustomGrid.InvalidateStartY(const FixedHeight,
120  RowOffset: Integer): integer;
121begin
122  result := FixedHeight;
123end;
124
125{ WidgetSetRegistration }
126
127function RegisterCustomGrid: Boolean;
128const
129  Done: Boolean = False;
130begin
131  Result := False;
132  if Done then exit;
133  if not WSRegisterCustomGrid then
134    RegisterWSComponent(TCustomGrid, TWSCustomGrid);
135  Done := True;
136  Result := True;
137end;
138
139end.
140