LMD VCL - LMD ElPack FAQ

From LMD
Revision as of 16:32, 10 April 2007 by Wikiadmin (talk | contribs)

Jump to: navigation, search
[edit]

<< Back to Overview page


TElInspector

How to use custom inplace editors?

Inspector uses own collection of inplace editors. You can examine them in ElInspectorEditors.pas We'll define simple descendant of the button edit and will use it for editing of boolean values.

type

  TForm1 = class(TForm)

    ElInspector1: TElInspector;
    ElInspectorRTTIDataSource1: TElInspectorRTTIDataSource;
...

  end;

  TMyElInspectorInplaceButtonEdit = class(TElInspectorInplaceButtonEdit)
  protected
    procedure BtnClick(Sender: TObject); override;
  end;


implementation

{ TMyElInspectorInplaceButtonEdit }

procedure TMyElInspectorInplaceButtonEdit.BtnClick(Sender: TObject);
begin
  if MessageDlg('Yes = True, No = False', mtConfirmation, [mbYes,mbNo], 0) = mrYes then
  begin
    TElInspectorItem(Item).InspectorData.AsString := 'True';
    Editor.Text := 'True';
  end
  else
  begin
    TElInspectorItem(Item).InspectorData.AsString := 'False';
    Editor.Text := 'False';
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
  LT: TElInspectorRegister;
begin
// we must to find current editor for boolean values in inspector's editors list
  for i := 1 to ElInspectorRTTIDataSource1.RegisterList.Count - 1 do
  begin
    LT := TElInspectorRegister(ElInspectorRTTIDataSource1.RegisterList.Items[i]);
    if (LT is TElInspectorTypeInfo) and (TElInspectorTypeInfo(LT).TypeInfo = TypeInfo(Boolean)) then
    begin 
    // remove current boolean editor
      ElInspectorRTTIDataSource1.RegisterList.Remove( ElInspectorRTTIDataSource1.RegisterList.Items[i] );
    // register our editor
      ElInspectorRTTIDataSource1.RegisterList.Insert(i, TElInspectorTypeInfo.Create(TMyElInspectorInplaceButtonEdit, TElBooleanProperty, TypeInfo(Boolean)));
      Break;
    end;
  end;

TStyleManager

How to use StyleName?

Style Manager operates with hierarchical style sheets. These style sheets are similar to Cascading Style Sheets (CSS), used in web pages, but they are more effective and easy to use. Each style sheet contains several styles.

Each style contains one or more property values. When the style is applied to the component, the component is checked for presence of the property with given name. If the property is present, it is set to the value, specified in the style.

Style can be one of three types:

1) Default
2) Class
3) Specific

There is one default style per style sheet. It is an ancestor for all other styles. Specific styles are applied when they are explicitly referred to by the component (in it's StyleName property).
Name of Specific style should begin by point (ex. '.CustomStyle') for prevention of crossing with names of classes.
Class styles are applied when there is no specific style name specified for the component. In this case the style with the name equal to component's class is searched for and applied. For example, if the component TElEdit refers to style manager, but there is no style name specified, Style Manager tries to find the style named 'TElEdit'. If it finds, this style is applied.

HTML controls

Could you tell me how to get the HTML text on an HTML Label (View, TElXTreeCell etc.) to center?

You may use
'<p align="Center"></p>'
tag to set desired text alignment.

El(X)Tree

How to draw part of item's text to pass over the several columns?

You can use OnItemPostDraw event to redraw part of item. For example I can redraw item text over first two columns:

uses elstrutils;

procedure TForm1.ElTree1ItemPostDraw(Sender: TObject; Canvas: TCanvas;
  Item: TElTreeItem; ItemRect: TRect; var DrawFocusRect: Boolean);
var
  R: TRect;
  s: TElFString;
  i: integer;
begin
  R := ItemRect;
  R.Right := ElTree1.HeaderSections[0].Width + ElTree1.HeaderSections[1].Width - 1;
  R.Top := R.Top + Item.Height div 2; // dirty calculation. use HTMLRender to render text if you need exact result
  // this is main column so we need to take into account space for lines and buttons
  R.Left := R.Left + Item.IndentAdjust;
  if (ElTree1.ShowRoot and ElTree1.ShowLines) or (ElTree1.ShowButtons and ElTree1.ShowRootButtons) then
    Inc(R.Left, ElTree1.ItemIndent)
  else
    Inc(R.Left, ElTree1.ItemIndent div 5);
  // offset rect if we use images
  R.Left := R.Left + 20 ; // paste here image width + 4 instead of 20
  Canvas.Brush.Style := bsSolid;
  Canvas.Brush.Color := clWhite; // set item's color if need
  // my Item.Text = '<html><b>Some text 1</b> <p>'+#13#10+'Some very very very very very very long text 2</html>'
  i := Pos(#10, Item.Text);
  s := '';
  if i > 0 then
    s := Copy(Item.Text, i + 1, length(Item.Text) - i - 8); // 8 to ignore '</html>'
  DrawTextW(Canvas.Handle, PWideChar(s), length(s), R, DT_NOPREFIX and DT_Left);
end;

ElDB(X)Tree

How could I display images in an Item if I've stored image's indexes in a database field?

1. Set TElDBXTree.ShowImages = True
2. Assign list of images (TImageList, TElImageList etc) to TElDBXTree.Images (and TElDBXTree.Images2 if you need it) properties
3. Open TElDBXTree.StylesDefs property in Object Inspector and choice "Table field" radiobutton in "Item images" radiogroup. It enforce setting Item.ImagesFromBase property to True for items.
4. Set data fields, where values for 1st (and 2st) image indexes was stored (look at Normal and State combos).

Hereon you should see images on the left side of the item's text.

How to make the sorted column drawn with an special background color

There are several ways to reach it.

1) you may use OnSortEnd event to set custom style to each cell

ex:

procedure TForm1.ElXTree1SortEnd(Sender: TObject);
var
  i, j: integer;
begin
  for j := 0 to ElXTree1.Items.Count - 1 do
  begin
    for i := 0 to ElXTree1.HeaderSections.Count - 1 do
    begin
      ElXTree1.Items.Item[j].Cells[i].UseOwnStyle := True;
      if (i = ElXTree1.SortSection) and
       (ElXTree1.HeaderSections.Sections[ElXTree1.SortSection].SortMode <> hsmNone)
      then
      begin
        ElXTree1.Items.Item[j].Cells[i].Style.CellBackGroundColor := clMoneyGreen;
        ElXTree1.Items.Item[j].Cells[i].Style.ParentColors := false;
        ElXTree1.Items.Item[j].Cells[i].Style.UseBackGroundColor := true;
      end
      else
      begin
        ElXTree1.Items.Item[j].Cells[i].Style.CellBackGroundColor := clWhite;
        ElXTree1.Items.Item[j].Cells[i].Style.ParentColors := false;
        ElXTree1.Items.Item[j].Cells[i].Style.UseBackGroundColor := true;
      end;
    end;
  end;
end;

2) you may use the cascading property of style to reach the same effect:

ex:

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: integer;
begin
// create test tree with 5 items. set custom colors in cells but not activate it
  for j := 0 to 5 do
  begin
    ElXTree1.Items.AddChild(nil,'Item'+Inttostr(j));
    ElXTree1.Items.Item[j].Cells[1].Text := Inttostr(j);
    for i := 0 to ElXTree1.HeaderSections.Count - 1 do
    begin
      ElXTree1.Items.Item[j].Cells[i].UseOwnStyle := True;
      ElXTree1.Items.Item[j].Cells[i].Style.CellBackGroundColor := clMoneyGreen;
      ElXTree1.Items.Item[j].Cells[i].Style.ParentColors := true;
    end;
  end;
end;

procedure TForm1.ElXTree1SortEnd(Sender: TObject);
var
  i, j: integer;
begin
// in this procedure we activate using own colors for cells in sorted section
  for j := 0 to ElXTree1.Items.Count - 1 do
  begin
    for i := 0 to ElXTree1.HeaderSections.Count - 1 do
    begin
      if (i = ElXTree1.SortSection) and
       (ElXTree1.HeaderSections.Sections[ElXTree1.SortSection].SortMode <> hsmNone) 
      then
        ElXTree1.Items.Item[j].Cells[i].Style.ParentColors := false
      else
        ElXTree1.Items.Item[j].Cells[i].Style.ParentColors := true;
    end;
  end;
end;

3) if the tree contains a lot of items more optimized to set colors in OnItemPreDraw event:

ex:

procedure TForm1.ElXTree1ItemPreDraw(Sender: TObject; Item: TElXTreeItem);
var
  i: integer;
begin
  for i := 0 to ElXTree1.HeaderSections.Count - 1 do
  begin
    Item.Cells[i].UseOwnStyle := True;
    if (i = ElXTree1.SortSection) and
       (ElXTree1.HeaderSections.Sections[ElXTree1.SortSection].SortMode <> hsmNone)
    then
    begin
      Item.Cells[i].Style.CellBackGroundColor := clMoneyGreen;
      Item.Cells[i].Style.ParentColors := false;
      Item.Cells[i].Style.UseBackGroundColor := true;
    end
    else
    begin
      Item.Cells[i].Style.CellBackGroundColor := clWhite;
      Item.Cells[i].Style.ParentColors := false;
      Item.Cells[i].Style.UseBackGroundColor := true;
    end;
  end;
end;

3a) ... or use virtual mode of tree

[ Please look examle of virtual tree in our demo]