1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
// début stringgrid FixedCol
// trouver les objets à partir de la colonne fixe:
// - stringgrid "fixe"
// - stringgrid associée
// - Hscrollbar
// - Vscrollbar
procedure ChercheObjetsParColf(colf: TstringColumn;
var gridf, grid: TStringGrid;
var hsb, vsb: TScrollBar;
var split: TSplitter);
var
panel: Tpanel;
i: integer;
begin
gridf:=colf.parent.parent as TStringGrid;
panel:=gridf.parent as Tpanel;
For i:=0 to panel.ChildrenCount-1 do
begin
if (panel.children[i] is TStringGrid) and (panel.children[i]<>gridf) then
begin
grid:=panel.children[i] as TStringGrid;
end
else
begin
if (panel.children[i] is TScrollBar) then
begin
if (panel.children[i] as TScrollBar).orientation=TOrientation.Horizontal then
begin
hsb:=panel.children[i] as TScrollBar;
end
else
begin
if (panel.children[i] as TScrollBar).orientation=TOrientation.Vertical then
begin
vsb:=panel.children[i] as TScrollBar;
end
end;
end
else
begin
if (panel.children[i] is TSplitter) then
begin
split:=panel.children[i] as TSplitter;
end;
end;
end;
end;
end;
procedure ChercheGridParVScrollBar(vsb: TScrollBar; var gridf: TStringGrid);
var
panel: Tpanel;
grid1, grid2 : TStringGrid;
i: integer;
begin
panel:=vsb.parent as Tpanel;
grid1:=nil;
grid2:=nil;
For i:=0 to panel.ChildrenCount-1 do
begin
if (panel.children[i] is TStringGrid) and (grid1=nil)then
begin
grid1:=panel.children[i] as TStringGrid;
end
else
begin
if (panel.children[i] is TStringGrid) and (panel.children[i]<>grid1) and (grid2=nil) then
begin
grid2:=panel.children[i] as TStringGrid;
break;
end
end;
end;
if grid1.Position.X<grid2.Position.X then
gridf:=grid1
else
gridf:=grid2;
end;
procedure ChercheGridParHScrollBar(hsb: TScrollBar; var grid: TStringGrid);
var
panel: Tpanel;
grid1, grid2 : TStringGrid;
i: integer;
begin
panel:=hsb.parent as Tpanel;
grid1:=nil;
grid2:=nil;
For i:=0 to panel.ChildrenCount-1 do
begin
if (panel.children[i] is TStringGrid) and (grid1=nil)then
begin
grid1:=panel.children[i] as TStringGrid;
end
else
begin
if (panel.children[i] is TStringGrid) and (panel.children[i]<>grid1) and (grid2=nil) then
begin
grid2:=panel.children[i] as TStringGrid;
break;
end
end;
end;
if grid1.Position.X<grid2.Position.X then
grid:=grid2
else
grid:=grid1;
end;
procedure ChercheObjetsParGrid(grid1: TStringGrid;
var grid2: TStringGrid;
var hsb, vsb: TScrollBar;
var split: TSplitter);
var
panel: Tpanel;
i: integer;
begin
panel:=grid1.parent as Tpanel;
For i:=0 to panel.ChildrenCount-1 do
begin
if (panel.children[i] is TStringGrid) and (panel.children[i]<>grid1) then
begin
grid2:=panel.children[i] as TStringGrid;
end
else
begin
if (panel.children[i] is TScrollBar) then
begin
if (panel.children[i] as TScrollBar).orientation=TOrientation.Horizontal then
begin
hsb:=panel.children[i] as TScrollBar;
end
else
begin
if (panel.children[i] as TScrollBar).orientation=TOrientation.Vertical then
begin
vsb:=panel.children[i] as TScrollBar;
end
end;
end
else
begin
if (panel.children[i] is TSplitter) then
begin
split:=panel.children[i] as TSplitter;
end;
end;
end;
end;
end;
procedure ChercheHsbParSplit(split: TSplitter; var hsb: TScrollBar);
var
i: integer;
panel: TPanel;
begin
panel:=(split.parent) as Tpanel;
For i:=0 to panel.ChildrenCount-1 do
begin
if (panel.children[i] is TScrollBar) then
begin
if (panel.children[i] as TScrollBar).orientation=TOrientation.Horizontal then
begin
hsb:=panel.children[i] as TScrollBar;
end;
end;
end;
end;
procedure ChercheGrid2ParGrid1(grid1: TStringGrid; var grid2: TStringGrid);
var
panel: Tpanel;
i: integer;
begin
panel:=(grid1.parent) as Tpanel;
For i:=0 to panel.ChildrenCount-1 do
begin
if (panel.children[i] is TStringGrid) and (panel.children[i]<>grid1) then
begin
grid2:=panel.children[i] as TStringGrid;
end
end;
end;
// redimensionnement de la colonne fixe
procedure TForm1.FirstColumnResize(Sender: TObject);
var
P: TPointF;
gridf, grid: TStringGrid;
colf: TStringColumn;
hsb, vsb: TScrollBar;
split: TSplitter;
objet: tfmxobject;
i: integer;
begin
colf:=(sender as TstringColumn);
ChercheObjetsParColf(colf, gridf, grid, hsb, vsb, split);
if colf.width>gridf.width then
begin
gridf.width:=colf.width;
hsb.Margins.Left:=split.Position.X+split.Width;
end
else
colf.width:=firstcolwidth(colf);
// redimenssionnement éventuel du vscrollbar
P:=grid.ViewportPosition;
P.y:=gridf.ViewportPosition.Y;
grid.ViewportPosition:=P;
UpDateScrollBar(grid.VScrollBar, vsb);
end;
// redimensionnement de la stringgrid "fixe"
procedure TForm1.SGfResize(Sender: TObject);
var
gridf: TStringGrid;
begin
gridf:=sender as TStringGrid;
gridf.columns[0].width:=firstcolwidth(gridf.columns[0]);
end;
procedure TForm1.SGfSelectCell(Sender: TObject; const ACol, ARow: Integer;
var CanSelect: Boolean);
var
grid1, grid2: TStringGrid;
begin
grid1:=sender as Tstringgrid;
ChercheGrid2ParGrid1(grid1, grid2);
grid1.OnSelectCell:=nil;
grid2.OnSelectCell:=nil;
if sender=grid1 then
grid2.selectrow(arow)
else
grid1.selectrow(arow);
grid1.OnSelectCell:=SGfSelectCell;
grid2.OnSelectCell:=SGfSelectCell;
end;
function TForm1.Firstcolwidth(colf: TColumn): single;
begin
result:=(colf.parent.parent as TstringGrid).width;
end;
procedure TForm1.splitMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single); // encours new
var
hsb: TScrollBar;
split: TSplitter;
begin
split:=sender as Tsplitter;
ChercheHsbParSplit(split, hsb);
hsb.Margins.Left:=split.Position.X+split.Width;
end;
//synchronisation entre les deux grilles
procedure TForm1.ViewportPositionChange(Sender: TObject;
const OldViewportPosition, NewViewportPosition: TPointF;
const ContentSizeChanged: Boolean);
var
P: TPointF;
grid1, grid2, gridf, grid: TStringGrid;
hsb, vsb: TScrollBar;
split: TSplitter;
begin
if sender is TStringGrid then
begin
grid1:=Sender as TStringGrid;
ChercheObjetsParGrid(grid1, grid2, hsb, vsb, split);
if grid1.Position.X<grid2.Position.X then
begin
P:=grid2.ViewportPosition;
P.y:=grid1.ViewportPosition.Y;
grid2.ViewportPosition:=P;
UpDateScrollBar(grid2.VScrollBar, vsb);
end
else
begin
P.X:=0;
P.Y:=grid1.ViewportPosition.Y;
grid2.ViewportPosition:=P;
UpDateScrollBar(grid1.HScrollBar, hsb);
UpDateScrollBar(grid1.VScrollBar, vsb);
end;
end;
end;
// mise à jour des scorllbar en fonction des scrollbar de la grille "non fixe"
procedure TForm1.UpDateScrollBar(const aGridScrollBar, aToScrollBar: TScrollBar);
begin
if aGridScrollBar=Nil then
Exit;
with aToScrollBar do
begin
Max:=aGridScrollBar.Max;
Min:=aGridScrollBar.Min;
ViewportSize:=aGridScrollBar.ViewportSize;
value:=aGridScrollBar.Value;
end;
end;
// onchange de la vertical scrollbar
procedure TForm1.vsbChange(Sender: TObject);
var
vsb: TScrollBar;
gridf: TStringGrid;
begin
vsb:=sender as TScrollBar;
ChercheGridParVScrollBar(vsb, gridf);
gridf.ViewportPosition:=PointF(0, vsb.Value);
end;
// onchange de la horizontal scrollbar
procedure TForm1.hsbChange(Sender: TObject);
var
P: TPointF;
hsb: TScrollBar;
grid: TStringGrid;
begin
hsb:=Sender as TScrollBar;
ChercheGridParHScrollBar(hsb, grid);
p:=grid.ViewportPosition;
p.x:=hsb.value;
grid.ViewportPosition:=p;
end;
// mise à jour de la grille en fonction du resize d'une colonne "non fixe"
procedure TForm1.DummyHeaderResize(Sender: TObject; var NewSize: Single);
var
grid, gridf: TStringGrid;
hsb, vsb: TScrollBar;
split: TSplitter;
acol: Tcolumn;
begin
if sender<>nil then
begin
acol:=TopengridHeaderitem(sender).Column;
grid:=acol.parent.parent as TStringGrid;
if grid<>nil then
begin
ChercheObjetsParGrid(grid, gridf, hsb, vsb, split);
TopenGridHeaderItem(sender).Resized;
UpDateScrollBar(grid.HScrollBar, hsb);
end;
end;
end;
// présentation des grilles, notamment les titres centrés,
// mais aussi déclaration du OnResizeItemEvent par DummyHeaderResize
procedure TForm1.SGPresentation(grid: TStringGrid);
var
i: integer;
FHeader: THeader;
gridf: TStringGrid;
hsb, vsb: TScrollBar;
split: TSplitter;
begin
with TStyledGrid(grid.Presentation) do
begin
ApplyStyleLookup;
if grid.FindStyleResource<THeader>('header', FHeader) then
FHeader.OnResizeItem:=DummyHeaderResize;
// pour afficher les titres centrés
for i:=0 to Headeritemcount-1 do
HeaderItem[i].TextSettings.HorzAlign:=TTextAlign.Center;
// pour afficher les valeurs à droite si numérique
//for i:=0 to ColumnCount-1 do
// Columns[i].HorzAlign:=TTextAlign.Trailing;
//affichage du titre de la colonne fixe centré
ChercheObjetsParGrid(grid, gridf, hsb, vsb, split);
with TstyledGrid(gridf.Presentation) do
begin
ApplyStyleLookup;
if HeaderItemCount-1>0 then
HeaderItem[0].TextSettings.HorzAlign:=TTextAlign.Center;
end;
end;
end;
// regroupement de toutes les choses à faire dans le OnCreate de la Form
procedure TForm1.FixedColSG(var gridf, grid: TStringGrid);
var
grid2: TStringGrid;
hsb, vsb: TScrollBar;
split: Tsplitter;
aheader: Theader;
HeaderHeight: single;
begin
gridf.Options:=gtf.Options+[TgridOption.RowSelect];
gridf.ShowScrollBars:=false;
grid.ShowScrollBars:=false;
ChercheObjetsParGrid(gridf, grid2, hsb, vsb, split);
// définition de la taille du splitter en fonction de la plateforme
{$IFDEF MSWINDOWS}
split.width:=2;
{$ELSE}
{$IFDEF ANDROID}
split.width:=6;
{$ENDIF}
{$ENDIF}
//marge droite de la horizontalbar
hsb.Margins.Right:=vsb.Width;
//marge haute de la verticalBar
// trouver la hauteur du header de la grid
// Header: Theader;
//Header := THeader((StringGrid1).FindStyleResource('header'));
//if Assigned(Header) then
// Pt.Y := Round(Header.Height);
//ou bien
if gridf.Findstyleresource<THeader>('header', aHeader) then
HeaderHeight:=round(aHeader.height)
else
HeaderHeight:=(cnom.AbsoluteRect.top - gtf.AbsoluteRect.top)-2;
vsb.Margins.Top:=HeaderHeight;
// redimensionnement de la colonne fixe en fonction de la stringgridfixe
gridf.columns[0].width:=FirstColWidth(gtf.columns[0]);
// redimensionnement de la stringgrid"fixe"
gridf.OnResize:=sgfResize;
gridf.onselectcell:=SGfSelectCell;
grid.onselectcell:=SGfSelectCell;
//reporter toutes les grilles concernées sur la procédure générique
gridf.Model.OnViewportPositionChange:=ViewportPositionChange;
grid.Model.OnViewportPositionChange:=ViewportPositionChange;
gridf.Model.CellReturnAction:=TCellReturnAction.GotoNextRow;
grid.Model.CellReturnAction:=TCellReturnAction.GotoNextRow;
SGPresentation(grid);
end;
// fin stringgrid fixedcol |