1 | Unit 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 |
|
---|
10 | Interface
|
---|
11 |
|
---|
12 |
|
---|
13 | {$ifdef win32}
|
---|
14 | Uses
|
---|
15 | StdCtrls;
|
---|
16 |
|
---|
17 | Type
|
---|
18 | TListBoxType = TCustomListBox;
|
---|
19 |
|
---|
20 | {$else}
|
---|
21 | Uses
|
---|
22 | Classes, Forms, StdCtrls;
|
---|
23 |
|
---|
24 | Type
|
---|
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 |
|
---|
62 | Exports
|
---|
63 | TCustomListBox,'User','CustomListBox.bmp';
|
---|
64 | {$endif}
|
---|
65 |
|
---|
66 | Implementation
|
---|
67 |
|
---|
68 | {$ifdef os2}
|
---|
69 | Uses
|
---|
70 | Messages, PMWin, OS2Def;
|
---|
71 | {$endif}
|
---|
72 |
|
---|
73 | {$ifdef os2}
|
---|
74 | // Loads selected items (with associated objects, if any)
|
---|
75 | // into Destination
|
---|
76 | Procedure TCustomListBox.GetSelectedItems( Destination: TStrings );
|
---|
77 | Var
|
---|
78 | i: longint;
|
---|
79 | Begin
|
---|
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 ] );
|
---|
85 | End;
|
---|
86 |
|
---|
87 | Procedure TCustomListBox.DefaultHandler( Var message );
|
---|
88 | Begin
|
---|
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...
|
---|
95 | End;
|
---|
96 |
|
---|
97 | Procedure TCustomListBox.StartUpdate;
|
---|
98 | Var
|
---|
99 | i: longint;
|
---|
100 | Begin
|
---|
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;
|
---|
111 | End;
|
---|
112 |
|
---|
113 | Procedure TCustomListBox.CompleteUpdate;
|
---|
114 | Var
|
---|
115 | i: longint;
|
---|
116 | Index: longint;
|
---|
117 | FirstSelectedIndex: longint;
|
---|
118 | Begin
|
---|
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;
|
---|
137 | End;
|
---|
138 |
|
---|
139 | Procedure TCustomListBox.MouseDown( Button: TMouseButton;
|
---|
140 | ShiftState: TShiftState;
|
---|
141 | X, Y: LongInt );
|
---|
142 | Var
|
---|
143 | ThePoint: TPoint;
|
---|
144 | Begin
|
---|
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);
|
---|
163 | End;
|
---|
164 |
|
---|
165 | Procedure TCustomListBox.SetupComponent;
|
---|
166 | Begin
|
---|
167 | Inherited SetupComponent;
|
---|
168 | FSavedSelected:= TStringList.Create;
|
---|
169 | End;
|
---|
170 |
|
---|
171 | Destructor TCustomListBox.Destroy;
|
---|
172 | Begin
|
---|
173 | FSavedSelected.Destroy;
|
---|
174 | Inherited Destroy;
|
---|
175 | End;
|
---|
176 |
|
---|
177 | function TCustomListBox.GetSelectedObject: TObject;
|
---|
178 | begin
|
---|
179 | if ItemIndex <> -1 then
|
---|
180 | Result:= Items.Objects[ ItemIndex ]
|
---|
181 | else
|
---|
182 | Result:= nil;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TCustomListBox.SetSelectedObject(const Value: TObject);
|
---|
186 | begin
|
---|
187 | ItemIndex:= Items.IndexOfObject( Value );
|
---|
188 | end;
|
---|
189 |
|
---|
190 | function TCustomListBox.GetSelectedItem: string;
|
---|
191 | begin
|
---|
192 | if ItemIndex <> -1 then
|
---|
193 | Result:= Items[ ItemIndex ]
|
---|
194 | else
|
---|
195 | Result:= '';
|
---|
196 | end;
|
---|
197 |
|
---|
198 | procedure TCustomListBox.SetSelectedItem(const Value: string);
|
---|
199 | begin
|
---|
200 | ItemIndex:= Items.IndexOf( Value );
|
---|
201 | end;
|
---|
202 |
|
---|
203 | Procedure TCustomListBox.SelectAll;
|
---|
204 | var
|
---|
205 | i: longint;
|
---|
206 | begin
|
---|
207 | BeginUpdate;
|
---|
208 | for i := 0 to Items.Count - 1 do
|
---|
209 | Selected[ i ] := true;
|
---|
210 | EndUpdate;
|
---|
211 | end;
|
---|
212 |
|
---|
213 | Initialization
|
---|
214 | {Register classes}
|
---|
215 | RegisterClasses([TCustomListBox]);
|
---|
216 |
|
---|
217 | {$endif}
|
---|
218 |
|
---|
219 | End.
|
---|
220 |
|
---|