-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathProjectFormSelection.pas
171 lines (154 loc) · 3.84 KB
/
ProjectFormSelection.pas
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
unit ProjectFormSelection;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TSelectionForm = class(TForm)
List: TListBox;
pnlTop: TPanel;
edtFind: TEdit;
btnNext: TButton;
procedure ListDblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure edtFindChange(Sender: TObject);
procedure ListDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
public
Lines: TStringList;
OnlyNumberedTitles: boolean;
Selected: integer;
function TitleToNumber(index: integer): integer;
end;
var
SelectionForm: TSelectionForm;
implementation
{$R *.dfm}
function TSelectionForm.TitleToNumber(index: integer): integer;
var j: integer;
s: string;
begin
result := 0;
if cardinal(index)>=cardinal(Lines.Count) then
exit;
s := Lines[index];
if (s='') or (s[1]='[') then
exit;
j := 0;
while s[j+1] in ['0'..'9'] do inc(j);
if j>0 then
result := StrToInt(copy(s,1,j));
end;
procedure TSelectionForm.ListDblClick(Sender: TObject);
begin
Selected := List.ItemIndex;
Close;
end;
procedure TSelectionForm.FormCreate(Sender: TObject);
begin
Lines := TStringList.Create;
end;
procedure TSelectionForm.FormDestroy(Sender: TObject);
begin
Lines.Free;
end;
procedure TSelectionForm.FormShow(Sender: TObject);
var i,n,max: integer;
begin
max := 0;
for i := Lines.Count-1 downto 0 do begin
n := TitleToNumber(i);
if n>0 then begin
if n>max then
max := n;
continue;
end;
if not OnlyNumberedTitles then
continue;
Lines.Delete(i);
if Selected>=i then
if Selected=i then
Selected := -1 else
dec(Selected);
end;
if max>0 then
Caption := format('%s - Maximum title # is %d',[Caption,max]);
List.Count := Lines.Count;
if cardinal(Selected)<cardinal(Lines.Count) then begin
List.TopIndex := Selected-10;
List.ItemIndex := Selected;
end;
edtFind.SetFocus;
Selected := -1;
end;
procedure TSelectionForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_RETURN:
ListDblClick(nil);
VK_ESCAPE:
Close;
VK_F3:
edtFindChange(nil);
end;
end;
procedure TSelectionForm.edtFindChange(Sender: TObject);
var Search: string;
i,found: integer;
begin
edtFind.SetFocus;
Search := UpperCase(edtFind.Text);
if Search='' then
exit;
found := -1;
for i := List.ItemIndex+1 to Lines.Count-1 do
if Pos(Search,UpperCase(Lines[i]))>0 then begin
found := i;
break;
end;
if found<0 then
for i := 0 to List.ItemIndex-1 do
if Pos(Search,UpperCase(Lines[i]))>0 then begin
found := i;
break;
end;
if found>=0 then begin
List.TopIndex := found-10;
List.ItemIndex := found;
end;
end;
procedure TSelectionForm.ListDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var List: TCustomListBox absolute Control;
Data,Number: string;
i,sp: integer;
begin
with List.Canvas do begin
FillRect(Rect);
if cardinal(Index)<cardinal(Lines.Count) then begin
Data := Lines[index];
if Data<>'' then begin
if Data[1]<>'[' then begin
sp := 0;
while Data[sp+1] in ['0'..'9'] do inc(sp);
Number := copy(Data,1,sp);
delete(Data,1,sp);
sp := 3;
for i := 1 to length(Data) do
if Data[i]=' ' then
inc(sp) else
break;
end else
sp := 1;
TextOut(Rect.Left+sp*8,Rect.Top,Data);
TextOut(Rect.Right-TextWidth(Number)-12,Rect.Top,Number);
end;
end;
end;
end;
end.