unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jpeg, ExtCtrls,
  NxEdit6;

type
  TForm1 = class(TForm)
    tagBokisCar: TNxTagsEdit6;
    lbxMake: TListBox;
    lbxModel: TListBox;
    lbxEngine: TListBox;
    lsbColour: TListBox;
    ldxType: TListBox;
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    LTagChars: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    LSelectedIdx: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    LTagCount: TLabel;
    LMax: TLabel;
    procedure ListBoxMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure tagBokisCarGetTagColor(Sender: TObject; Index: Integer; var FillColor: TColor;
      var BorderColor);
    procedure tagBokisCarTagDelete(Sender: TObject; Index: Integer; var Accept: Boolean);
    procedure tagBokisCarDragDrop(Sender, Source: TObject; X, Y: Integer; Area: TNxTagArea);
    procedure tagBokisCarDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState;
      var Accept: Boolean; Area: TNxTagArea);
    procedure tagBokisCarMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer;
      Area: TNxTagArea);
    procedure tagBokisCarTagAdd(Sender: TObject; var Value: WideString; var Accept: Boolean);
    procedure tagBokisCarChange(Sender: TObject);
    procedure tagBokisCarMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer; Area: TNxTagArea);
  private
    procedure UpdateCharLengthCaption;
    procedure UpdateTagCountCaption;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}




procedure TForm1.FormCreate(Sender: TObject);
begin
   tagBokisCar.Tags.Clear;
   LMax.Visible := False;
end;

procedure TForm1.UpdateCharLengthCaption;
begin
   //                                the TStrings items       +   the text in the TNxEdit
   LTagChars.Caption := IntToStr(Length(tagBokisCar.Tags.Text)+Length(tagBokisCar.Text) );
end;

procedure TForm1.UpdateTagCountCaption;
begin
   LTagCount.Caption := IntToStr(tagBokisCar.Tags.Count);
   LMax.Visible      := (tagBokisCar.Tags.Count = tagBokisCar.Max);
end;


procedure TForm1.ListBoxMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   TListBox(Sender).BeginDrag(False);
end;


{--------------------------------NxTagEdit6 events--------------------------------------}


procedure TForm1.tagBokisCarChange(Sender: TObject);
begin
   //event triggered by typing in 'blank' edit area
   UpdateCharLengthCaption;
end;

procedure TForm1.tagBokisCarGetTagColor(Sender: TObject; Index: Integer;
  var FillColor: TColor; var BorderColor);
begin
   FillColor := clMoneyGreen;  //default
   if tagBokisCar.Tags[Index] = 'red'    then FillColor := clRed;
   if tagBokisCar.Tags[Index] = 'blue'   then FillColor := clNavy;
   if tagBokisCar.Tags[Index] = 'orange' then FillColor := clWebOrange;
   if tagBokisCar.Tags[Index] = 'white'  then FillColor := clWhite;
   if tagBokisCar.Tags[Index] = 'silver' then FillColor := clSilver;
end;


procedure TForm1.tagBokisCarTagAdd(Sender: TObject; var Value: WideString; var Accept: Boolean);
begin
   UpdateTagCountCaption;
   UpdateCharLengthCaption;
end;

procedure TForm1.tagBokisCarTagDelete(Sender: TObject; Index: Integer; var Accept: Boolean);
begin
   if tagBokisCar.Tags[Index] = 'red'    then
   begin
      TaskMessageDlg('Cannot delete tag',
      ' It may be a Nissan or a Hyundai, but it is definitely red ', mtError, [mbOK], 0);
      Accept := False;
      exit
   end;
end;


procedure TForm1.tagBokisCarMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer;
  Area: TNxTagArea);
begin
   LSelectedIdx.Caption := IntToStr(tagBokisCar.SelectedTag);
end;


procedure TForm1.tagBokisCarMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer; Area: TNxTagArea);
begin
   //capture any tag delete (not possible in OnTagDelete since deletion is after event).
   if Area = csTagArea then
   begin
      UpdateTagCountCaption;
      UpdateCharLengthCaption;
   end;
end;

procedure TForm1.tagBokisCarDragDrop(Sender, Source: TObject; X, Y: Integer;
  Area: TNxTagArea);
{ docwiki: The Source is the object being dragged, the Sender is the potential drop site,
           and X and Y are screen coordinates in pixels}
var
   n: Integer;
   NewStr: String;
   NewIdx: Integer;
   Found: Boolean;
begin
   //already in tags?
   Found := False;
   NewStr := TListBox(Source).Items[TListBox(Source).ItemIndex];
   for n := 0 to (Sender as TNxTagsEdit6).Tags.Count - 1 do
      if (Sender as TNxTagsEdit6).Tags[n] = NewStr
      then Found := True;
   //
   if Found then exit;
   //is the mouse over the tags, or the 'blank' edit area?
   NewIdx := (Sender AS TNxTagsEdit6).SelectedTag;
   case Area of
      csTagArea:  (Sender AS TNxTagsEdit6).Tags.Insert(NewIdx, NewStr);  //insert into tags
      csEditArea: (Sender AS TNxTagsEdit6).Tags.Add(NewStr);             //add to end
   end;
   UpdateTagCountCaption;
   UpdateCharLengthCaption;
end;

procedure TForm1.tagBokisCarDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState;
  var Accept: Boolean; Area: TNxTagArea);
begin
   //if mouse is over the tag area of the control, show the current selected tag
   if (Area = csTagArea) then LSelectedIdx.Caption := IntToStr(tagBokisCar.SelectedTag);
   //valid for both the tags area and the 'blank' edit area.
   Accept := (Source is TListBox);
end;


end.
