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
|
unit gui_combobox;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
gfx_widget,
gui_form,
gfxbase,
gui_button,
fpgfx;
type
{ TfpgCustomComboBox }
TfpgCustomComboBox = class(TfpgWidget)
private
FDropDownCount: integer;
FDropDown: TfpgForm;
FBackgroundColor: TfpgColor;
FFocusItem: integer;
FFont: TfpgFont;
FInternalBtn: TfpgButton;
FItems: TStringList;
FOnChange: TNotifyEvent;
procedure SetBackgroundColor(const AValue: TfpgColor);
procedure SetDropDownCount(const AValue: integer);
procedure DoDropDown;
procedure InternalBtnClick(Sender: TObject);
procedure InternalListBoxSelect(Sender: TObject);
procedure SetFocusItem(const AValue: integer);
protected
FMargin: integer;
property DropDownCount: integer read FDropDownCount write SetDropDownCount default 8;
procedure HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override;
procedure HandlePaint; override;
property Items: TStringList read FItems;
property FocusItem: integer read FFocusItem write SetFocusItem;
property Font: TfpgFont read FFont;
property BackgroundColor: TfpgColor read FBackgroundColor write SetBackgroundColor;
function Text: string;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AfterConstruction; override;
end;
TfpgComboBox = class(TfpgCustomComboBox)
published
property DropDownCount;
property Items;
property FocusItem;
property BackgroundColor;
property OnChange;
end;
function CreateComboBox(AOwner: TComponent; x, y, w: TfpgCoord; AList: TStringList): TfpgComboBox;
implementation
uses
Math,
gui_listbox;
var
OriginalFocusRoot: TfpgWidget;
type
// This is so we can access protected methods
TPrivateWidget = class(TfpgWidget)
end;
{ TDropDownWindow }
TDropDownWindow = class(TfpgForm)
private
FCallerWidget: TfpgWidget;
protected
procedure HandlePaint; override;
procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
procedure HandleShow; override;
procedure HandleHide; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
ListBox: TfpgListBox;
property CallerWidget: TfpgWidget read FCallerWidget write FCallerWidget;
end;
{ TDropDownWindow }
procedure TDropDownWindow.HandlePaint;
begin
Canvas.BeginDraw;
inherited HandlePaint;
Canvas.Clear(clWhite);
Canvas.EndDraw;
end;
procedure TDropDownWindow.HandleKeyPress(var keycode: word;
var shiftstate: TShiftState; var consumed: boolean);
begin
inherited HandleKeyPress(keycode, shiftstate, consumed);
if keycode = keyEscape then
begin
Close;
consumed := True;
end;
end;
procedure TDropDownWindow.HandleShow;
begin
FocusRootWidget := ListBox;
ListBox.Left := 0;
ListBox.Top := 0;
ListBox.Width := Width;
ListBox.Height := Height;
inherited HandleShow;
CaptureMouse;
end;
procedure TDropDownWindow.HandleHide;
begin
FocusRootWidget := OriginalFocusRoot;
OriginalFocusRoot := nil;
inherited HandleHide;
if Assigned(FocusRootWidget) then
FocusRootWidget.SetFocus;
end;
constructor TDropDownWindow.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
WindowType := wtPopup;
WindowAttributes := [];
WindowPosition := wpUser;
ListBox := TfpgListBox.Create(self);
ListBox.PopupFrame := True;
end;
destructor TDropDownWindow.Destroy;
begin
ReleaseMouse;
inherited Destroy;
end;
function CreateComboBox(AOwner: TComponent; x, y, w: TfpgCoord; AList: TStringList): TfpgComboBox;
begin
Result := TfpgComboBox.Create(AOwner);
Result.Left := x;
Result.Top := y;
Result.Width := w;
Result.Focusable := True;
Result.Height := 23; // replace this with font height + margins
{$Note We still need to handle the AList param as well.}
end;
{ TfpgCustomComboBox }
procedure TfpgCustomComboBox.SetDropDownCount(const AValue: integer);
begin
if FDropDownCount = AValue then
Exit;
FDropDownCount := AValue;
end;
procedure TfpgCustomComboBox.SetBackgroundColor(const AValue: TfpgColor);
begin
if FBackgroundColor <> AValue then
begin
FBackgroundColor := AValue;
Repaint;
end;
end;
procedure TfpgCustomComboBox.DoDropDown;
var
pt: TPoint;
ddw: TDropDownWindow;
rowcount: integer;
begin
if (not Assigned(FDropDown)) or (not FDropDown.HasHandle) then
begin
OriginalFocusRoot := FocusRootWidget;
pt := WindowToScreen(Parent, Point(Left, Top+Height));
FDropDown := TDropDownWindow.Create(nil);
ddw := TDropDownWindow(FDropDown);
ddw.Left := pt.X;
ddw.Top := pt.Y;
ddw.Width := Width;
// adjust the height of the dropdown
rowcount := FItems.Count;
if rowcount > FDropDownCount then
rowcount := FDropDownCount;
if rowcount < 1 then
rowcount := 1;
ddw.Height := (ddw.ListBox.RowHeight * rowcount) + 4;
ddw.CallerWidget := self;
ddw.ListBox.OnSelect := @InternalListBoxSelect;
// Assign combobox text items to internal listbox and set default focusitem
ddw.ListBox.Items.Assign(FItems);
ddw.ListBox.FocusItem := FFocusItem;
FDropDown.Show;
ddw.ListBox.SetFocus;
end
else
begin
FDropDown.Close;
FreeAndNil(FDropDown);
end;
end;
procedure TfpgCustomComboBox.InternalBtnClick(Sender: TObject);
begin
DoDropDown;
end;
procedure TfpgCustomComboBox.InternalListBoxSelect(Sender: TObject);
begin
FFocusItem := TDropDownWindow(FDropDown).ListBox.FocusItem;
FDropDown.Close;
if HasHandle then
Repaint;
if Assigned(FOnChange) then
FOnChange(self);
end;
procedure TfpgCustomComboBox.SetFocusItem(const AValue: integer);
begin
if FFocusItem = AValue then
Exit; //==>
FFocusItem := AValue;
end;
procedure TfpgCustomComboBox.HandleLMouseDown(x, y: integer; shiftstate: TShiftState);
begin
inherited HandleLMouseDown(x, y, shiftstate);
DoDropDown;
end;
procedure TfpgCustomComboBox.HandlePaint;
var
r: TfpgRect;
begin
inherited HandlePaint;
Canvas.BeginDraw;
Canvas.ClearClipRect;
Canvas.DrawControlFrame(0, 0, Width, Height);
// internal background rectangle (without frame)
r.Left := 2;
r.Top := 2;
r.Width := Width - 4;
r.Height := Height - 4;
Canvas.SetClipRect(r);
if Enabled then
Canvas.SetColor(FBackgroundColor)
else
Canvas.SetColor(clWindowBackground);
Canvas.FillRectangle(2, 2, Width - 4, Height - 4);
// fpgStyle.DrawButtonFace(canvas, width - min(height, 20)-3, 2, height-4, height-4, [btnIsEmbedded]);
// fpgStyle.DrawDirectionArrow(canvas, width - height + 1, 1, height-2, height-2, 1);
Canvas.SetFont(Font);
if Focused then
begin
Canvas.SetColor(clSelection);
Canvas.SetTextColor(clSelectionText);
end
else
begin
Canvas.SetColor(FBackgroundColor);
Canvas.SetTextColor(clText1);
end;
Canvas.FillRectangle(r);
Canvas.SetClipRect(r);
// Draw select item's text
if FocusItem > -1 then
Canvas.DrawString(FMargin+1, FMargin, Text);
Canvas.EndDraw;
end;
function TfpgCustomComboBox.Text: string;
begin
if (FocusItem > 0) and (FocusItem <= FItems.Count) then
Result := FItems.Strings[FocusItem-1]
else
Result := '';
end;
constructor TfpgCustomComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBackgroundColor := clBoxColor;
FDropDownCount := 8;
FWidth := 120;
FHeight := 23;
FFocusItem := 0;
FMargin := 3;
FFont := fpgGetFont('#List');
FItems := TStringList.Create;
FOnChange := nil;
end;
destructor TfpgCustomComboBox.Destroy;
begin
FItems.Free;
inherited Destroy;
end;
procedure TfpgCustomComboBox.AfterConstruction;
begin
inherited AfterConstruction;
if not Assigned(FInternalBtn) then
begin
FInternalBtn := CreateButton(self, (Width-19), 2, 18, '', @InternalBtnClick);
FInternalBtn.Height := 19;
FInternalBtn.Embedded := True;
FInternalBtn.Parent := self;
FInternalBtn.ImageName := 'sys.sb.down';
FInternalBtn.ShowImage := True;
end;
end;
end.
|