summaryrefslogtreecommitdiff
path: root/gui/combobox.inc
blob: 33774b931f7ad4c290992aabc1ecdf6cbdb24047 (plain)
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
{
    fpGUI  -  Free Pascal Graphical User Interface
    Copyright (C) 2000 - 2001 by
      Areca Systems GmbH / Sebastian Guenther
    Copyright (C) 2006 by Graeme Geldenhuys 
      member of the fpGUI development team.

    Combobox class declarations

    See the file COPYING.fpGUI, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************}


{%mainunit fpgui.pp}

{$IFDEF read_interface}

  { Combobox widget declarations }

  { TCustomComboBox }
  
  TComboBoxPopup = class(TPopupWindow)
  private
    FListBox: TListBox;
    procedure   ItemSelected(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    property    ListBox: TListBox read FListBox;
  end;
  

  TCustomComboBox = class(TWidget)
  private
    FItemIndex: Integer;
    FItems: TStrings;
    FOnChange: TNotifyEvent;
    procedure   ButtonClick(Sender: TObject);
    procedure   DropDownDeactivate(Sender: TObject);
    procedure   DropDownDestroy(Sender: TObject);
    procedure   SetItemIndex(const AValue: Integer);
  protected
    FButton: TGenericButton;
//    FDropDown: TCustomForm;
    FDropDown: TComboBoxPopup;
    lbl: TLabel;
    procedure   Click; override;
    procedure   Paint(Canvas: TFCanvas); override;
    procedure   CalcSizes; override;
    procedure   Resized; override;
    function    DistributeEvent(Event: TEventObj): Boolean; override;
    property    CanExpandWidth default True;
//    property    DropDownCount: integer read FDropDownCount write FDropDownCount;
    property    ItemIndex: Integer read FItemIndex write SetItemIndex default -1;
    property    OnChange: TNotifyEvent read FOnChange write FOnChange;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    property    Items: TStrings read FItems write FItems;
  end;


  TComboBox = class(TCustomComboBox)
  published
    property    CanExpandWidth;
    property    CanExpandHeight;
    property    Enabled;
    property    Text;
    property    ItemIndex;
    property    OnChange;
  end;

{$ENDIF read_interface}



{$IFDEF read_implementation}


{ Combobox widget implementation }

type
  TArrowButton = class(TGenericButton)
  protected
    procedure Paint(Canvas: TFCanvas); override;
    procedure CalcSizes; override;
  end;


procedure TArrowButton.Paint(Canvas: TFCanvas);
begin
  inherited Paint(Canvas);
  Style.DrawComboBoxArrow(Canvas, Rect(0, 0, Width, Height),
    (wsClicked in WidgetState) and (wsMouseInside in WidgetState),
    wsEnabled in WidgetState);
end;


procedure TArrowButton.CalcSizes;
begin
  FMinSize := Style.GetComboBoxArrowSize;
end;


procedure TComboBoxPopup.ItemSelected(Sender: TObject);
begin
  Close;
end;


constructor TComboBoxPopup.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];

  FListBox := TListBox.Create(self);
  FListBox.Name       := 'listbox';
  FListBox.Parent     := self;
  FListBox.HotTrack   := True;
//  FListBox.OnClick    := @ItemSelected;   // Listbox needs OnSelect event
end;


// -------------------------------------------------------------------
//   TCustomComboBox
// -------------------------------------------------------------------

constructor TCustomComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FCanExpandWidth := True;
  WidgetStyle     := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
  FItems := TStringList.Create;
  FItemIndex := -1;

  FButton := TArrowButton.Create(Self);
  FButton.Name := 'FButton';
  FButton.Embedded        := True;
  FButton.CanExpandWidth  := False;
  FButton.OnClick         := @ButtonClick;
  FButton.Parent          := Self;
end;


destructor TCustomComboBox.Destroy;
begin
  FDropDown.Free;
  inherited Destroy;
end;


procedure TCustomComboBox.Paint(Canvas: TFCanvas);
var
  Pt: TPoint;
  ItemRect: TRect;
  ItemFlags: TItemFlags;
begin
  ItemFlags := [];
  Style.DrawEditBox(Canvas, Rect(0, 0, Width, Height));
  
  if Text <> '' then
  begin
    Style.SetUIColor(Canvas, clWindowText);
    Pt.x := 4;
    Pt.y := (BoundsSize.cy - Canvas.FontCellHeight) div 2;
    
    if (wsHasFocus in WidgetState) and FindForm.IsActive then
    begin
      Include(ItemFlags, ifFocused);
      Include(ItemFlags, ifSelected);
    end;

    ItemRect := Rect(0, 0, (Width - FButton.Width), Height);
//    InflateRect(ItemRect, -1, -1);
    ItemRect.TopLeft := ItemRect.TopLeft + 1;
    ItemRect.BottomRight := ItemRect.BottomRight - 2;

    Style.DrawItemBefore(Canvas, ItemRect, ItemFlags);
    Style.DrawText(Canvas, Pt, Text, WidgetState);
    Style.DrawItemAfter(Canvas, ItemRect, ItemFlags);
  end
  else
  begin
    if (wsHasFocus in WidgetState) and FindForm.IsActive then
      Style.DrawFocusRect(Canvas, Rect(0, 0, Width, Height));
  end;
end;


procedure TCustomComboBox.CalcSizes;
begin
  with Style.GetEditBoxBorders do
    FMinSize := gfxBase.Size(FButton.MinSize.cx,
      Max(FindForm.Wnd.Canvas.FontCellHeight, FButton.MinSize.cy)) +
      TopLeft + BottomRight;
end;


procedure TCustomComboBox.Resized;
begin
  with Style.GetEditBoxBorders do
    FButton.SetBounds(Point(Width - Right - FButton.MinSize.cx, Top),
      FButton.MinSize);
end;


function TCustomComboBox.DistributeEvent(Event: TEventObj): Boolean;
begin
  Result := Event.SendToChild(FButton) or
    inherited DistributeEvent(Event);
end;


procedure TCustomComboBox.ButtonClick(Sender: TObject);
var
  l: TCustomListBox;
begin
  if Assigned(FDropDown) and FDropDown.Visible then
  begin
    FDropDown.Close;
    Exit; //==>
  end;
  
  if not Assigned(FDropDown) then
  begin
    FDropDown := TComboBoxPopup.Create(Self);
    FDropDown.OnDeactivate        := @DropDownDeactivate;
    FDropDown.OnDestroy           := @DropDownDestroy;
    FDropDown.ListBox.Items.Text  := FItems.Text;
    FDropDown.ListBox.FItemIndex  := FItemIndex;
    FDropDown.ListBox.OnClick     := @DropDownDeactivate;
  end;

  FDropDown.SetPosition(ClientToScreen(Point(0, Height)));
  FDropDown.Show;
end;


procedure TCustomComboBox.DropDownDeactivate(Sender: TObject);
begin
  LAYOUTTRACE('TCustomComboBox.DropDownDestroy for %s:%s', [Name, ClassName]);
  ItemIndex := FDropDown.ListBox.ItemIndex;
  FDropDown.Close;
  SetFocus;
end;


procedure TCustomComboBox.DropDownDestroy(Sender: TObject);
begin
  LAYOUTTRACE('TCustomComboBox.DropDownDestroy for %s:%s', [Name, ClassName]);
  FDropDown := nil;
end;


procedure TCustomComboBox.SetItemIndex(const AValue: Integer);
begin
  if FItemIndex <> AValue then
  begin
    if AValue < FItems.Count then
      FItemIndex := AValue;
    if FItemIndex = -1 then
      Text := ''
    else
      Text := FItems[FItemIndex];

    // fire event
    if Assigned(OnChange) then
      OnChange(Self);
  end;
end;


{ This event causes the combobox to drop open when you click anywhere in the
  component, or press the spacebar key. }
procedure TCustomComboBox.Click;
begin
  ButtonClick(nil);
  inherited Click;
end;


{$ENDIF read_implementation}