File: comboex.pas

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (412 lines) | stat: -rw-r--r-- 15,116 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
{ Visual components TComboBoxEx and TCheckComboBox

  Copyright (C) 2014 Vojtěch Čihák, e-mail: cihakvjtch@seznam.cz

  This library is free software; you can redistribute it and/or modify it under the terms of the
  GNU Library General Public License as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you permission to link this
  library with independent modules to produce an executable, regardless of the license terms of
  these independent modules,and to copy and distribute the resulting executable under terms of
  your choice, provided that you also meet, for each linked independent module, the terms and
  conditions of the license of that module. An independent module is a module which is not derived
  from or based on this library. If you modify this library, you may extend this exception to your
  version of the library, but you are not obligated to do so. If you do not wish to do so, delete
  this exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  the GNU Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License along with this
  library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
  Floor, Boston, MA 02110-1335, USA.
}

unit ComboEx;
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, types,
  LCLIntf, LCLType, LMessages, LResources, LazLoggerBase,
  ImgList, Controls, StdCtrls, ComCtrls, ExtCtrls, Graphics, GraphUtil,
  Themes, Forms;

type
  {$PACKENUM 2}
  TAutoCompleteOption = (acoAutoSuggest, acoAutoAppend, acoSearch, acoFilterPrefixes,
                         acoUseTab, acoUpDownKeyDropsList, acoRtlReading);
  TAutoCompleteOptions = set of TAutoCompleteOption;
  TComboBoxExStyle = (csExDropDown, csExSimple, csExDropDownList);
  TComboBoxExStyleEx = (csExCaseSensitive, csExNoEditImage, csExNoEditImageIndent,
                        csExNoSizeLimit, csExPathWordBreak);
  TComboBoxExStyles = set of TComboBoxExStyleEx;
  TCustomData = Pointer;
  TListControlItems = class;  { forward }
  TListItemsCompare = function (AList: TListControlItems; AItem1, AItem2: Integer): Integer;
  TListItemsSortType = TSortType;
  { Events }
  TCheckItemChange = procedure(Sender: TObject; AIndex: Integer) of object;
  TListControlItem = class;  { forward }
  TListCompareEvent = function(AList: TListControlItems; AItem1, AItem2: TListControlItem): Integer of object;

  { TListControlItem }
  TListControlItem = class(TCollectionItem)
  private
    FCaption: TTranslateString;
    FData: TCustomData;
    FImageIndex: TImageIndex;
    procedure SetCaption(const AValue: TTranslateString);
    procedure SetImageIndex(AValue: TImageIndex);
  public
    property Data: TCustomData read FData write FData;
    constructor Create(ACollection: TCollection); override;
  published
    property Caption: TTranslateString read FCaption write SetCaption;
    property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
  end;

  { TComboExItem }
  TComboExItem = class(TListControlItem)
  private
    FIndent: Integer;
    FOverlayImageIndex: TImageIndex;
    FSelectedImageIndex: TImageIndex;
    procedure SetIndent(AValue: Integer);
    procedure SetOverlayImageIndex(AValue: TImageIndex);
    procedure SetSelectedImageIndex(AValue: TImageIndex);
  protected const
    cDefCaption = 'ItemEx';
  public
    constructor Create(ACollection: TCollection); override;
    destructor Destroy; override;
  published
    property Indent: Integer read FIndent write SetIndent default -1;
    property OverlayImageIndex: TImageIndex read FOverlayImageIndex write SetOverlayImageIndex default -1;
    property SelectedImageIndex: TImageIndex read FSelectedImageIndex write SetSelectedImageIndex default -1;
  end;

  { TListControlItems }
  TListControlItems = class(TOwnedCollection)
  private
    FCaseSensitive: Boolean;
    FSortType: TListItemsSortType;
    FOnCompare: TListCompareEvent;
    FCompare: TListItemsCompare;
    function GetItems(AIndex: Integer): TListControlItem;
    procedure SetCaseSensitive(AValue: Boolean);
    procedure SetSortType(AValue: TListItemsSortType);
  protected
    function CompareItems(AItem1, AItem2: TListControlItem): Integer; virtual;
    function DoCustomSort(AItem1, AItem2: TListControlItem): Integer;
    function DoOnCompare(AItem1, AItem2: TListControlItem): Integer;
    procedure Update(AItem: TCollectionItem); override;
  public
    function Add: TListControlItem;
    procedure CustomSort(ACompare: TListItemsCompare);
    procedure Sort;
    property Items[AIndex: Integer]: TListControlItem read GetItems; default;
  published
    property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive default False;
    property SortType: TListItemsSortType read FSortType write SetSortType default stNone;
    property OnCompare: TListCompareEvent read FOnCompare write FOnCompare;
  end;

  { TComboExItems }
  TComboExItems = class(TListControlItems)
  private
    function GetComboItems(AIndex: Integer): TComboExItem;
  protected
    FAddingOrDeletingItem: Boolean;
    procedure Notify(Item: TCollectionItem; Action: TCollectionNotification); override;
    procedure Update(Item: TCollectionItem); override;
  public
    function Add: TComboExItem;
    function AddItem(const ACaption: string; AImageIndex: SmallInt = -1;
                 AOverlayImageIndex: SmallInt = -1; ASelectedImageIndex: SmallInt = -1;
                 AIndent: SmallInt = -1; AData: TCustomData = nil): TComboExItem;
    function Insert(AIndex: Integer): TComboExItem;
    property ComboItems[AIndex: Integer]: TComboExItem read GetComboItems; default;
  end;

  { TCustomComboBoxEx }
  TCustomComboBoxEx = class(TCustomComboBox)
  private
    FAutoCompleteOptions: TAutoCompleteOptions;
    FImages: TCustomImageList;
    FItemsEx: TComboExItems;
    FStyle: TComboBoxExStyle;
    FStyleEx: TComboBoxExStyles;
    FImagesWidth: Integer;
    procedure SetImages(AValue: TCustomImageList);
    procedure SetImagesWidth(const aImagesWidth: Integer);
    procedure SetStyle(AValue: TComboBoxExStyle); reintroduce;
    procedure SetStyleEx(AValue: TComboBoxExStyles);
  protected const
    cDefAutoCompOpts = [acoAutoAppend];
    cDefStyle = csExDropDown;
  protected
    FNeedMeasure: Boolean;
    FRightToLeft: Boolean;
    FTextHeight: SmallInt;
    procedure CMBiDiModeChanged(var Message: TLMessage); message CM_BIDIMODECHANGED;
    procedure DrawItem(Index: Integer; ARect: TRect; State: TOwnerDrawState); override;
    procedure FontChanged(Sender: TObject); override;
    procedure InitializeWnd; override;
    procedure SetItemHeight(const AValue: Integer); override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    function Add: Integer; overload;
    procedure Add(const ACaption: string; AIndent: Integer = -1;
                  AImgIdx: TImageIndex = -1; AOverlayImgIdx: TImageIndex = -1;
                  ASelectedImgIdx: TImageIndex = -1); overload;
    procedure AddItem(const Item: String; AnObject: TObject); override;
    procedure AssignItemsEx(AItems: TStrings); overload;
    procedure AssignItemsEx(AItemsEx: TComboExItems); overload;
    procedure Clear; override;
    procedure Delete(AIndex: Integer);
    procedure DeleteSelected;
    procedure Insert(AIndex: Integer; const ACaption: string; AIndent: Integer = -1;
                     AImgIdx: TImageIndex = -1; AOverlayImgIdx: TImageIndex = -1;
                     ASelectedImgIdx: TImageIndex = -1);
    property AutoCompleteOptions: TAutoCompleteOptions read FAutoCompleteOptions
             write FAutoCompleteOptions default cDefAutoCompOpts;
    property Images: TCustomImageList read FImages write SetImages;
    property ImagesWidth: Integer read FImagesWidth write SetImagesWidth default 0;
    property ItemsEx: TComboExItems read FItemsEx write FItemsEx;
    property Style: TComboBoxExStyle read FStyle write SetStyle default cDefStyle;
    property StyleEx: TComboBoxExStyles read FStyleEx write SetStyleEx default [];
  end;

  { TComboBoxEx }
  TComboBoxEx = class(TCustomComboBoxEx)
  published
    property Align;
    property Anchors;
    property ArrowKeysTraverseList;
    property AutoComplete;
    property AutoCompleteOptions;
    property AutoCompleteText;
    property AutoDropDown;
    property AutoSelect;
    property AutoSize;
    property BidiMode;
    property BorderSpacing;
    property BorderStyle;
    property CharCase;
    property Color;
    property Constraints;
    property DragCursor;
    property DragKind;
    property DragMode;
    property DropDownCount;
    property Enabled;
    property Font;
    property Images;
    property ImagesWidth;
    property ItemHeight;
    property ItemsEx;  { do not change order; ItemsEx must be before ItemIndex }
    property ItemIndex;
    property ItemWidth;
    property MaxLength;
    property OnChange;
    property OnChangeBounds;
    property OnClick;
    property OnCloseUp;
    property OnContextPopup;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDropDown;
    property OnEditingDone;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnGetItems;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseEnter;
    property OnMouseLeave;
    property OnMouseMove;
    property OnMouseUp;
    property OnMouseWheel;
    property OnMouseWheelDown;
    property OnMouseWheelUp;
    property OnSelect;
    property OnStartDock;
    property OnStartDrag;
    property OnUTF8KeyPress;
    property ParentBidiMode;
    property ParentColor;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ReadOnly;
    property ShowHint;
    property Style;
    property StyleEx;
    property TabOrder;
    property TabStop;
    property Text;
    property Visible;
  end;

  { TCheckComboItemState }
  TCheckComboItemState = class
  public
    State: TCheckBoxState;
    Enabled: Boolean;
    Data: TObject;
  end;

  { TCustomCheckCombo }
  TCustomCheckCombo = class(TCustomComboBox)
  private
    FAllowGrayed: Boolean;
    FOnItemChange: TCheckItemChange;
    procedure AsyncCheckItemStates(Data: PtrInt);
    function GetChecked(AIndex: Integer): Boolean;
    function GetCount: Integer;
    function GetItemEnabled(AIndex: Integer): Boolean;
    function GetObject(AIndex: Integer): TObject;
    function GetState(AIndex: Integer): TCheckBoxState;
    procedure SetChecked(AIndex: Integer; AValue: Boolean);
    procedure SetItemEnabled(AIndex: Integer; AValue: Boolean);
    procedure SetObject(AIndex: Integer; AValue: TObject);
    procedure SetState(AIndex: Integer; AValue: TCheckBoxState);
  protected
    FCheckHighlight: Boolean;
    FCheckSize: TSize;
    FDropped: Boolean;
    FHilightedIndex: Integer;
    FHiLiteLeft: Integer;
    FHiLiteRight: Integer;
    FNeedMeasure: Boolean;
    FRejectDropDown: Boolean;
    FRejectToggleOnSelect: Boolean;
    FRightToLeft: Boolean;
    FTextHeight: SmallInt;
    procedure CMBiDiModeChanged(var Message: TLMessage); message CM_BIDIMODECHANGED;
    procedure ClearItemStates;
    procedure CloseUp; override;
    procedure DrawItem(Index: Integer; ARect: TRect; State: TOwnerDrawState); override;
    procedure DropDown; override;
    procedure FontChanged(Sender: TObject); override;
    procedure InitializeWnd; override;
    procedure InitItemStates;
    procedure CheckItemStates;
    procedure QueueCheckItemStates;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure Loaded; override;
    procedure MouseLeave; override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    procedure SetItemHeight(const AValue: Integer); override;
    procedure SetItems(const Value: TStrings); override;
    procedure Select; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure AddItem(const AItem: string; AState: TCheckBoxState; AEnabled: Boolean = True); reintroduce;
    procedure AssignItems(AItems: TStrings);
    procedure Clear; override;
    procedure DeleteItem(AIndex: Integer);
    procedure CheckAll(AState: TCheckBoxState; AAllowGrayed: Boolean = True; AAllowDisabled: Boolean = True);
    procedure Toggle(AIndex: Integer);
    property AllowGrayed: Boolean read FAllowGrayed write FAllowGrayed default False;
    property Count: Integer read GetCount;
    property Checked[AIndex: Integer]: Boolean read GetChecked write SetChecked;
    property ItemEnabled[AIndex: Integer]: Boolean read GetItemEnabled write SetItemEnabled;
    property Objects[AIndex: Integer]: TObject read GetObject write SetObject;
    property State[AIndex: Integer]: TCheckBoxState read GetState write SetState;
    property OnItemChange: TCheckItemChange read FOnItemChange write FOnItemChange;
  end;

  { TCheckComboBox }
  TCheckComboBox = class(TCustomCheckCombo)
  published
    property Align;
    property AllowGrayed;
    property Anchors;
    property ArrowKeysTraverseList;
    property AutoDropDown;
    property AutoSize;
    property BidiMode;
    property BorderSpacing;
    property BorderStyle;
    property Color;
    property Constraints;
    property Count;
    property DragCursor;
    property DragKind;
    property DragMode;
    property DropDownCount;
    property Enabled;
    property Font;
    property ItemHeight;
    property ItemIndex;
    property Items;
    property ItemWidth;
    property MaxLength;
    property OnChange;
    property OnChangeBounds;
    property OnClick;
    property OnCloseUp;
    property OnContextPopup;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnDropDown;
    property OnEditingDone;
    property OnEnter;
    property OnExit;
    property OnGetItems;
    property OnItemChange;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseEnter;
    property OnMouseLeave;
    property OnMouseMove;
    property OnMouseUp;
    property OnMouseWheel;
    property OnMouseWheelDown;
    property OnMouseWheelUp;
    property OnStartDrag;
    property OnSelect;
    property OnUTF8KeyPress;
    property ParentBidiMode;
    property ParentColor;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Sorted;
    property TabOrder;
    property TabStop;
    property Text;
    property Visible;
  end;

procedure Register;

implementation

{$include comboex.inc}

procedure Register;
begin
  RegisterComponents('Misc', [TComboBoxEx, TCheckComboBox]);
end;

end.