File: frmfilesearcher.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (166 lines) | stat: -rw-r--r-- 3,868 bytes parent folder | download
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
unit frmfilesearcher;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ButtonPanel, ExtCtrls, StdCtrls, FileCtrl, filebrowsertypes,
  ctrlfilebrowser, Types, Masks;

type

  { TFileSearcherForm }

  TFileSearcherForm = class(TForm)
    bpFileSearch: TButtonPanel;
    cbFilter: TFilterComboBox;
    edtSearch: TEdit;
    Label1: TLabel;
    LBFiles: TListBox;
    procedure cbFilterChange(Sender: TObject);
    procedure edtSearchChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LBFilesDblClick(Sender: TObject);
    procedure LBFilesDrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
  private
    FMask : TMaskList;
    FController: TFileBrowserController;
    FResults: TFileSearchResults;

    procedure DoFilter;
  public
    Function GetSelectedItems : TFileEntryArray;
  end;

var
  FileSearcherForm: TFileSearcherForm;

implementation

uses LCLType, LazIDEIntf;

{$R *.lfm}

{ TFileSearcherForm }


procedure TFileSearcherForm.edtSearchChange(Sender: TObject);
begin
  DoFilter;
end;

procedure TFileSearcherForm.DoFilter;

var
  lMatchOptions : TFilenameMatchOptions;
  Idx : Integer;
  lMatch : TFileSearchMatch;

begin
  if Not Assigned(FController) or (Length(edtSearch.Text)<2) then
    exit;
  lMatchOptions:=[];
  if (fsoMatchOnlyFileName in FController.SearchOptions) then
    Include(lMatchOptions,fmoFileNameOnly);
  if (fsoUseLetters in FController.SearchOptions) then
    Include(lMatchOptions,fmoLetters);
  FResults.Clear;
  LBFiles.Items.BeginUpdate;
  try
    LBFiles.Items.Clear;
    FController.FindFiles(edtSearch.Text,FResults,lMatchOptions,FMask);
    for Idx:=0 to FResults.Count-1 do
      begin
      lMatch:=FResults[Idx];
      LBFiles.Items.AddObject(lMatch.FileName,lMatch);
      end;
  finally
    LBFiles.Items.EndUpdate;
  end;
end;

function TFileSearcherForm.GetSelectedItems: TFileEntryArray;

var
  Idx,I: Integer;

begin
  Result:=[];
  SetLength(Result,LBFiles.SelCount);
  idx:=0;
  For I:=0 to LBFiles.Count-1 do
    if LBFiles.Selected[I] then
      begin
      Result[Idx]:=TFileEntry(LBFiles.Items.Objects[i]);
      Inc(Idx);
      end;
end;

procedure TFileSearcherForm.cbFilterChange(Sender: TObject);
begin
  FreeAndNil(FMask);
  if cBFilter.Text<>'' then
    FMask:=TMaskList.Create(cbFilter.Mask);
  DoFilter;
end;

procedure TFileSearcherForm.FormCreate(Sender: TObject);

begin
  FController:=LazarusIDE.OwningComponent.FindComponent('IDEFileBrowserController') as TFileBrowserController;
  if cbFilter.Mask<>'' then
    FMask:=TMaskList.Create(cbFilter.Mask);
  FResults:=TFileSearchResults.Create;
end;

procedure TFileSearcherForm.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FMask);
  FreeAndNil(FResults);
end;

procedure TFileSearcherForm.LBFilesDblClick(Sender: TObject);
begin
  Modalresult:=mrOK;
end;

procedure TFileSearcherForm.LBFilesDrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);

Var
  W,L : Integer;
  lRect : TRect;
  C : TColor;
  S,Term : String;
  lCanvas : TCanvas;
  lMatch : TFileSearchMatch;
  lPos : TMatchPosition;

begin
  lCanvas:=LBFiles.Canvas;
  lMatch:=fResults[Index];
  S:=lMatch.FileName;
  lRect:=aRect;
  if not (odSelected in State) then
    begin
    c:=lCanvas.Brush.Color;
    lCanvas.Brush.Color:=clHighlight;
    for lPos in lMatch.MatchPositions do
      begin
      Term:=Copy(S,lPos.Pos,lPos.Len);
      W:=lCanvas.TextWidth(Term);
      L:=lCanvas.TextWidth(Copy(S,1,lPos.Pos-1));
      lRect.Left:=aRect.Left+L;
      lRect.Right:=aRect.Left+L+W;
      if lrect.Right>aRect.Right then
        lrect.Right:=aRect.Right;
      lCanvas.FillRect(lRect);
      end;
    lCanvas.Brush.Color:=C;
    end;
  lCanvas.TextRect(aRect,aRect.Left,aRect.Top,S);
end;

end.