source: branches/2.20_branch/Components/CustomListBox.pas

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

+ components stuff

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1Unit CustomListBox;
2// Enhanced list box with the following additional features
3// - During updates, remembers selection and top index and
4// at the end will attempt to reselect and reposition the same
5// - Remembers which item was right clicked (PopupIndex)
6// - When an item is right clicked, it is selected if it isn't already
7// - Calls an event when presentation parameters (color/pencolor)
8// are changed
9
10Interface
11
12
13{$ifdef win32}
14Uses
15 StdCtrls;
16
17Type
18 TListBoxType = TCustomListBox;
19
20{$else}
21Uses
22 Classes, Forms, StdCtrls;
23
24Type
25 TCustomListBox=Class( TListBox )
26 Protected
27 Procedure SetupComponent; Override;
28 FPopupIndex: longint;
29 Procedure MouseDown( Button: TMouseButton;
30 ShiftState: TShiftState;
31 X, Y: LongInt ); Override;
32
33 // These are used to hold previous selection during Update
34 FSavedSelected:TStringList;
35 FSavedTopItem: string;
36
37 Procedure DefaultHandler( Var message ); override;
38 function GetSelectedObject: TObject;
39 procedure SetSelectedObject(const Value: TObject);
40 function GetSelectedItem: string;
41 procedure SetSelectedItem(const Value: string);
42 Public
43 // These do the same as BeginUpdate and EndUpdate, with additions
44 // (They remember selection & topindex)
45 Procedure StartUpdate; Virtual;
46 Procedure CompleteUpdate; Virtual;
47
48 Property PopupIndex: longint read FPopupIndex;
49 Destructor Destroy; Override;
50
51 // Loads selected items (with associated objects, if any)
52 // into Destination
53 Procedure GetSelectedItems( Destination: TStrings );
54
55 property SelectedItem: string read GetSelectedItem write SetSelectedItem;
56 property SelectedObject: TObject read GetSelectedObject write SetSelectedObject;
57
58 Procedure SelectAll;
59 End;
60 TListBoxType = TCustomListBox;
61
62Exports
63 TCustomListBox,'User','CustomListBox.bmp';
64{$endif}
65
66Implementation
67
68{$ifdef os2}
69Uses
70 Messages, PMWin, OS2Def;
71{$endif}
72
73{$ifdef os2}
74// Loads selected items (with associated objects, if any)
75// into Destination
76Procedure TCustomListBox.GetSelectedItems( Destination: TStrings );
77Var
78 i: longint;
79Begin
80 Destination.Clear;
81 for i:=0 to Items.Count-1 do
82 if Selected[ i ] then
83 Destination.AddObject( Items[ i ],
84 Items.Objects[ i ] );
85End;
86
87Procedure TCustomListBox.DefaultHandler( Var message );
88Begin
89 inherited DefaultHandler( message );
90// stupid private declarations
91// if FUpdatingPP then exit;
92 if TMessage( message ).msg=WM_PRESPARAMCHANGED then
93 exit;
94 // call event...
95End;
96
97Procedure TCustomListBox.StartUpdate;
98Var
99 i: longint;
100Begin
101 FSavedSelected.Clear;
102 for i:=0 to Items.Count-1 do
103 begin
104 if Selected[ i ] then
105 FSavedSelected.Add( Items[ i ] );
106 End;
107 FSavedTopItem:='---';
108 if Items.Count>0 then
109 FSavedTopItem:=Items[ TopIndex ];
110 BeginUpdate;
111End;
112
113Procedure TCustomListBox.CompleteUpdate;
114Var
115 i: longint;
116 Index: longint;
117 FirstSelectedIndex: longint;
118Begin
119 FirstSelectedIndex := -1;
120 for i:=0 to FSavedSelected.Count-1 do
121 begin
122 Index:=Items.IndexOf( FSavedSelected[ i ] );
123 if Index>-1 then
124 begin
125 Selected[ index ]:=true;
126 if FirstSelectedIndex = -1 then
127 FirstSelectedIndex := Index;
128 end;
129 End;
130 FSavedSelected.Clear;
131 Index:=Items.IndexOf( FSavedTopItem );
132 if Index>-1 then
133 TopIndex:=Index;
134 EndUpdate;
135 if FirstSelectedIndex <> -1 then
136 ItemIndex := FirstSelectedIndex;
137End;
138
139Procedure TCustomListBox.MouseDown( Button: TMouseButton;
140 ShiftState: TShiftState;
141 X, Y: LongInt );
142Var
143 ThePoint: TPoint;
144Begin
145 ThePoint.X:= X;
146 ThePoint.Y:= Y;
147 if Button<>mbRight then
148 begin
149 Inherited MouseDown( Button, ShiftState, X, Y);
150
151 if MultiSelect then
152 if not ( SSCtrl in ShiftState )
153 and not ( SSShift in ShiftState ) then
154 ItemIndex:= ItemAtPos( ThePoint, True );
155 exit;
156 end;
157 FPopupIndex:= ItemAtPos( ThePoint, True );
158 if FPopupIndex>-1 then
159 if not Selected[ FPopupIndex ] then
160 // If what they're clicking on is not already selected, select it
161 ItemIndex:= FPopupIndex;
162 Inherited MouseDown( Button, ShiftState, X, Y);
163End;
164
165Procedure TCustomListBox.SetupComponent;
166Begin
167 Inherited SetupComponent;
168 FSavedSelected:= TStringList.Create;
169End;
170
171Destructor TCustomListBox.Destroy;
172Begin
173 FSavedSelected.Destroy;
174 Inherited Destroy;
175End;
176
177function TCustomListBox.GetSelectedObject: TObject;
178begin
179 if ItemIndex <> -1 then
180 Result:= Items.Objects[ ItemIndex ]
181 else
182 Result:= nil;
183end;
184
185procedure TCustomListBox.SetSelectedObject(const Value: TObject);
186begin
187 ItemIndex:= Items.IndexOfObject( Value );
188end;
189
190function TCustomListBox.GetSelectedItem: string;
191begin
192 if ItemIndex <> -1 then
193 Result:= Items[ ItemIndex ]
194 else
195 Result:= '';
196end;
197
198procedure TCustomListBox.SetSelectedItem(const Value: string);
199begin
200 ItemIndex:= Items.IndexOf( Value );
201end;
202
203Procedure TCustomListBox.SelectAll;
204var
205 i: longint;
206begin
207 BeginUpdate;
208 for i := 0 to Items.Count - 1 do
209 Selected[ i ] := true;
210 EndUpdate;
211end;
212
213Initialization
214 {Register classes}
215 RegisterClasses([TCustomListBox]);
216
217{$endif}
218
219End.
220
Note: See TracBrowser for help on using the repository browser.