source: trunk/Sibyl/Addon/EDITLIST.PAS

Last change on this file was 7, checked in by RBRi, 19 years ago

+ sibyl staff

  • Property svn:eol-style set to native
File size: 11.8 KB
Line 
1Unit EditList;
2
3
4Interface
5
6Uses
7 SysUtils, Classes, Forms, Buttons, StdCtrls, ExtCtrls, XplorBtn;
8
9
10Type
11 TEditBrowseEvent = Procedure(Sender:TObject;Var Item:String) Of Object;
12
13 TEditListBox = Class(TControl)
14 Private
15 Edit:TEdit;
16 BtnBrowse:TSpeedButton;
17 FEditIndex:LongInt;
18 FIgnoreExit:Boolean;
19 FOnEnter:TNotifyEvent;
20 FOnExit:TNotifyEvent;
21 FOnBrowse:TEditBrowseEvent;
22 Function GetItems:TStrings;
23 Procedure SetItems(AStrings:TStrings);
24 Function GetCaption:String;
25 Procedure SetCaption(Value:String);
26 Function GetHorzScroll:Boolean;
27 Procedure SetHorzScroll(Value:Boolean);
28 Function GetItemIndex:LongInt;
29 Procedure SetItemIndex(Value:LongInt);
30 Function GetTopIndex:LongInt;
31 Procedure SetTopIndex(Value:LongInt);
32 Procedure SetHint(Value:String);
33 Procedure EvBtnInsert(Sender:TObject);
34 Procedure EvBtnDelete(Sender:TObject);
35 Procedure EvBtnUp(Sender:TObject);
36 Procedure EvBtnDown(Sender:TObject);
37 Procedure EvEditExit(Sender:TObject);
38 Procedure EvEditScan(Sender:TObject;Var KeyCode:TKeyCode);
39 Procedure EvItemFocus(Sender:TObject;Index:LongInt);
40 Procedure EvItemSelect(Sender:TObject;Index:LongInt);
41 Procedure EvItemKeyPress(Sender:TObject;Var key:Char);
42 Procedure EvItemScan(Sender:TObject;Var KeyCode:TKeyCode);
43 Procedure EvListEnter(Sender:TObject);
44 Procedure EvListExit(Sender:TObject);
45 Procedure EvBrowse(Sender:TObject);
46 Procedure WriteBack;
47 Procedure DoEdit(Index:LongInt);
48 Protected
49 ListBox:TListBox;
50 Panel:TPanel;
51 BtnNew:TExplorerButton;
52 BtnDel:TExplorerButton;
53 BtnUp:TExplorerButton;
54 BtnDown:TExplorerButton;
55 Procedure SetupComponent;Override;
56 Procedure SetupShow;Override;
57 Procedure SetFocus;Override;
58 Public
59 Function WriteSCUResource(Stream:TResourceStream):Boolean;Override;
60 Procedure ReadSCUResource(Const ResName:TResourceName;Var Data;DataLen:LongInt);Override;
61 Procedure Clear;Virtual;
62 Procedure BeginUpdate;
63 Procedure EndUpdate;
64 Property ItemIndex:LongInt Read GetItemIndex Write SetItemIndex;
65 Property TopIndex:LongInt Read GetTopIndex Write SetTopIndex;
66 Property XAlign;
67 Property XStretch;
68 Property YAlign;
69 Property YStretch;
70 Published
71 Property Align;
72 Property Caption:String Read GetCaption write SetCaption;
73 Property Color;
74 Property Enabled;
75 Property Font;
76 Property Hint Write SetHint;
77 Property HorzScroll:Boolean Read GetHorzScroll Write SetHorzScroll;
78 Property Items:TStrings Read GetItems Write SetItems;
79 Property ParentColor;
80 Property ParentPenColor;
81 Property ParentFont;
82 Property ParentShowHint;
83 Property PenColor;
84 Property PopupMenu;
85 Property ShowHint;
86 Property TabOrder;
87 Property TabStop;
88 Property Visible;
89 Property ZOrder;
90
91 Property OnBrowse:TEditBrowseEvent Read FOnBrowse Write FOnBrowse;
92 Property OnEnter:TNotifyEvent Read FOnEnter Write FOnEnter;
93 Property OnExit:TNotifyEvent Read FOnExit Write FOnExit;
94 Property OnSetupShow;
95 End;
96
97
98Exports
99 TEditListBox,'User','';
100
101
102Implementation
103
104{$r EditList}
105
106Const
107 BtnSize=26;
108
109
110Procedure TEditListBox.SetupComponent;
111Begin
112 Inherited SetupComponent;
113
114 Name := 'EditListBox';
115 Width := 220;
116 Height := 150;
117 Color := clEntryField;
118 Ownerdraw := False;
119
120 FEditIndex := -1;
121 FIgnoreExit := False;
122
123 ListBox := InsertListBox(SELF,0,0,Width,Height-(BtnSize+2),'');
124 ListBox.Align := alFrame;
125 ListBox.ParentColor := True;
126 ListBox.ParentPenColor := True;
127 ListBox.OnItemFocus := EvItemFocus;
128 ListBox.OnItemSelect := EvItemSelect;
129 ListBox.OnKeyPress := EvItemKeyPress;
130 ListBox.OnScan := EvItemScan;
131 ListBox.OnEnter := EvListEnter;
132 ListBox.OnExit := EvListExit;
133 Include(ListBox.ComponentState, csDetail);
134
135 Panel := InsertPanel(SELF,0,Height-(BtnSize+2),Width,(BtnSize+2),
136 bvNone,bvLowered,1,' EditListBox');
137 Panel.Align := alTop;
138 Panel.Alignment := taLeftJustify;
139 Panel.Color := clLtGray;
140 Panel.ParentPenColor := True;
141 Include(Panel.ComponentState, csDetail);
142
143 BtnNew.Create(Panel);
144 BtnNew.Parent := Panel;
145 BtnNew.SetWindowPos(Panel.Width-4*BtnSize-1,1,BtnSize,BtnSize);
146 BtnNew.Align := alFixedRightBottom;
147 BtnNew.Hint := 'New';
148 BtnNew.NumGlyphs :=2 ;
149 BtnNew.Glyph.LoadFromResourceName('BmpBtnNew');
150 BtnNew.OnClick := EvBtnInsert;
151 Include(BtnNew.ComponentState, csDetail);
152
153 BtnDel.Create(Panel);
154 BtnDel.Parent := Panel;
155 BtnDel.SetWindowPos(Panel.Width-3*BtnSize-1,1,BtnSize,BtnSize);
156 BtnDel.Align := alFixedRightBottom;
157 BtnDel.NumGlyphs := 2;
158 BtnDel.Hint := 'Del';
159 BtnDel.Glyph.LoadFromResourceName('BmpBtnDel');
160 BtnDel.OnClick := EvBtnDelete;
161 Include(BtnDel.ComponentState, csDetail);
162
163 BtnUp.Create(Panel);
164 BtnUp.Parent := Panel;
165 BtnUp.SetWindowPos(Panel.Width-2*BtnSize-1,1,BtnSize,BtnSize);
166 BtnUp.Align := alFixedRightBottom;
167 BtnUp.Hint := 'Up';
168 BtnUp.NumGlyphs := 2;
169 BtnUp.Glyph.LoadFromResourceName('BmpBtnUp');
170 BtnUp.OnClick := EvBtnUp;
171 Include(BtnUp.ComponentState, csDetail);
172
173 BtnDown.Create(Panel);
174 BtnDown.Parent := Panel;
175 BtnDown.SetWindowPos(Panel.Width-BtnSize-1,1,BtnSize,BtnSize);
176 BtnDown.Align := alFixedRightBottom;
177 BtnDown.Hint := 'Down';
178 BtnDown.NumGlyphs := 2;
179 BtnDown.Glyph.LoadFromResourceName('BmpBtnDown');
180 BtnDown.OnClick := EvBtnDown;
181 Include(BtnDown.ComponentState, csDetail);
182
183 Edit.Create(ListBox);
184 Edit.Parent := ListBox;
185 Edit.Visible := False;
186 Edit.OnExit := EvEditExit;
187 Edit.OnScan := EvEditScan;
188 Include(Edit.ComponentState, csDetail);
189 Include(Edit.ComponentState, csAcceptsControls);
190
191 BtnBrowse := InsertSpeedButton(Edit,0,0,0,0,0,'...','Browse');
192 BtnBrowse.OnClick := EvBrowse;
193 Include(BtnBrowse.ComponentState, csDetail);
194End;
195
196
197Procedure TEditListBox.SetupShow;
198Begin
199 Inherited SetupShow;
200
201 IF Not Designed Then
202 Begin
203 ListBox.Items.Add('');
204 End;
205End;
206
207
208Procedure TEditListBox.SetFocus;
209Begin
210 Inherited SetFocus;
211
212 If Not Designed Then ListBox.CaptureFocus;
213End;
214
215
216Function TEditListBox.WriteSCUResource(Stream:TResourceStream):Boolean;
217Var aText:PChar;
218Begin
219 Result := Inherited WriteSCUResource(Stream);
220 If Not Result Then Exit;
221
222 aText := ListBox.Items.GetText;
223 If aText <> Nil Then
224 Begin
225 Result := Stream.NewResourceEntry(rnItems,aText^,Length(aText^)+1);
226 StrDispose(aText);
227 End;
228End;
229
230
231Procedure TEditListBox.ReadSCUResource(Const ResName:TResourceName;Var Data;DataLen:LongInt);
232Var aText:PChar;
233Begin
234 If ResName = rnItems Then
235 Begin
236 aText := @Data;
237 ListBox.Items.SetText(aText);
238 End
239 Else Inherited ReadSCUResource(ResName,Data,DataLen)
240End;
241
242
243Function TEditListBox.GetItemIndex:LongInt;
244Begin
245 Result := ListBox.ItemIndex;
246End;
247
248
249Procedure TEditListBox.SetItemIndex(Value:LongInt);
250Begin
251 ListBox.ItemIndex := Value;
252End;
253
254
255Function TEditListBox.GetTopIndex:LongInt;
256Begin
257 Result := ListBox.TopIndex;
258End;
259
260
261Procedure TEditListBox.SetTopIndex(Value:LongInt);
262Begin
263 ListBox.TopIndex := Value;
264End;
265
266
267Procedure TEditListBox.SetHint(Value:String);
268Begin
269 Panel.Hint := Value;
270 ListBox.Hint := Value;
271 TControl.Hint := Value;
272End;
273
274
275Procedure TEditListBox.Clear;
276Begin
277 ListBox.Clear;
278End;
279
280
281Procedure TEditListBox.BeginUpdate;
282Begin
283 ListBox.BeginUpdate;
284End;
285
286
287Procedure TEditListBox.EndUpdate;
288Begin
289 ListBox.EndUpdate;
290End;
291
292
293Function TEditListBox.GetItems:TStrings;
294Begin
295 Result := ListBox.Items;
296End;
297
298
299Procedure TEditListBox.SetItems(AStrings:TStrings);
300Begin
301 ListBox.Items := AStrings;
302End;
303
304
305Function TEditListBox.GetCaption:String;
306Begin
307 Result := Panel.Caption;
308End;
309
310
311Procedure TEditListBox.SetCaption(Value:String);
312Begin
313 Panel.Caption := Value;
314End;
315
316
317Function TEditListBox.GetHorzScroll:Boolean;
318Begin
319 Result := ListBox.HorzScroll;
320End;
321
322
323Procedure TEditListBox.SetHorzScroll(Value:Boolean);
324Begin
325 ListBox.HorzScroll := Value;
326End;
327
328
329Procedure TEditListBox.EvBtnInsert(Sender:TObject);
330Var
331 i:LongInt;
332Begin
333 If Edit.Showing Then WriteBack;
334
335 If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count) Then
336 Begin
337 i := ListBox.ItemIndex;
338 ListBox.Items.Insert(ListBox.ItemIndex,'');
339 ListBox.ItemIndex := i;
340 End
341 Else i := ListBox.Items.Add('');
342
343 DoEdit(i);
344End;
345
346
347Procedure TEditListBox.EvBtnDelete(Sender:TObject);
348Var
349 i:LongInt;
350Begin
351 If Edit.Showing Then WriteBack;
352
353 If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count) Then
354 Begin
355 i := ListBox.ItemIndex;
356 ListBox.Items.Delete(i);
357 ListBox.ItemIndex := i;
358 End;
359End;
360
361
362Procedure TEditListBox.EvBtnUp(Sender:TObject);
363Var
364 i:LongInt;
365Begin
366 If Edit.Showing Then WriteBack;
367
368 If (ListBox.ItemIndex >= 1) And (ListBox.ItemIndex < ListBox.Items.Count) Then
369 Begin
370 i := ListBox.ItemIndex;
371 ListBox.Items.Exchange(i,i-1);
372 ListBox.ItemIndex := i-1;
373 End;
374End;
375
376
377Procedure TEditListBox.EvBtnDown(Sender:TObject);
378Var
379 i:LongInt;
380Begin
381 If Edit.Showing Then WriteBack;
382
383 If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count-1) Then
384 Begin
385 i := ListBox.ItemIndex;
386 ListBox.Items.Exchange(i,i+1);
387 ListBox.ItemIndex := i+1;
388 End;
389End;
390
391
392Procedure TEditListBox.WriteBack;
393Begin
394 If (FEditIndex >= 0) And (FEditIndex < ListBox.Items.Count) Then
395 Begin
396 ListBox.Items[FEditIndex] := Edit.Caption;
397 ListBox.ItemIndex := FEditIndex;
398
399 If FEditIndex = ListBox.Items.Count-1 Then
400 If Edit.Caption <> '' Then ListBox.Items.Add('');
401 End;
402
403
404 Edit.Visible := False;
405 ListBox.Focus;
406End;
407
408
409Procedure TEditListBox.DoEdit(Index:LongInt);
410Var
411 rc:TRect;
412Begin
413 rc := ListBox.ItemRect(Index);
414 Edit.SetWindowPos(1,rc.Bottom-2,ListBox.Width-2,Edit.Height);
415 Edit.Caption := ListBox.Items[Index];
416 Edit.Visible := True;
417 Edit.Focus;
418 Edit.SelectAll;
419
420
421 If @FOnBrowse <> Nil Then
422 Begin
423 BtnBrowse.SetWindowPos(Edit.Width-25-2,2,25,Edit.Height-4);
424 BtnBrowse.Parent := Edit;
425 End
426 Else BtnBrowse.Parent := Nil;
427
428
429 FEditIndex := Index;
430End;
431
432
433Procedure TEditListBox.EvEditExit(Sender:TObject);
434Begin
435 If FIgnoreExit Then Exit;
436
437 If Edit.Showing Then WriteBack;
438End;
439
440
441Procedure TEditListBox.EvEditScan(Sender:TObject;Var KeyCode:TKeyCode);
442Begin
443 Case KeyCode Of
444 kbCUp:
445 Begin
446 WriteBack;
447 If ListBox.ItemIndex > 0 Then ListBox.ItemIndex := ListBox.ItemIndex -1;
448 KeyCode := 0;
449 End;
450 kbCDown:
451 Begin
452 WriteBack;
453 ListBox.ItemIndex := ListBox.ItemIndex +1;
454 KeyCode := 0;
455 End;
456 kbCR
457 {$IFDEF OS2}
458 ,kbEnter
459 {$ENDIF}
460 :
461 Begin
462 WriteBack;
463 KeyCode := 0;
464 End;
465 kbEsc:
466 Begin
467 Edit.Visible := False;
468 ListBox.Focus;
469 KeyCode := 0;
470 End;
471 End;
472End;
473
474
475Procedure TEditListBox.EvItemFocus(Sender:TObject;Index:LongInt);
476Begin
477 If Edit.Showing Then WriteBack;
478End;
479
480
481Procedure TEditListBox.EvItemSelect(Sender:TObject;Index:LongInt);
482Begin
483 DoEdit(Index);
484End;
485
486
487Procedure TEditListBox.EvItemKeyPress(Sender:TObject;Var key:Char);
488Begin
489 If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count) Then
490 Begin
491 DoEdit(ListBox.ItemIndex);
492 Edit.Caption := key;
493 key := #0;
494 End;
495End;
496
497
498Procedure TEditListBox.EvItemScan(Sender:TObject;Var KeyCode:TKeyCode);
499Begin
500 Case KeyCode Of
501 kbIns:
502 Begin
503 EvBtnInsert(BtnNew);
504 KeyCode := 0;
505 End;
506 kbDel:
507 Begin
508 EvBtnDelete(BtnDel);
509 KeyCode := 0;
510 End;
511 End;
512End;
513
514
515Procedure TEditListBox.EvListEnter(Sender:TObject);
516Begin
517 If FOnEnter <> Nil Then FOnEnter(Self);
518End;
519
520
521Procedure TEditListBox.EvListExit(Sender:TObject);
522Begin
523 If FOnExit <> Nil Then FOnExit(Self);
524End;
525
526
527Procedure TEditListBox.EvBrowse(Sender:TObject);
528Var
529 s:String;
530Begin
531 FIgnoreExit := True;
532 s := Edit.Caption;
533 If FOnBrowse <> Nil Then FOnBrowse(Self, s);
534 Edit.Caption := s;
535 WriteBack;
536 FIgnoreExit := False;
537End;
538
539
540
541Begin
542End.
543
Note: See TracBrowser for help on using the repository browser.