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
  
     | 
    
      {%MainUnit ../dbctrls.pp}
{******************************************************************************
                                     TDBLookupComboBox
                    data aware lookup Combo Box, base found in dbctrls.pp
 ******************************************************************************
 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
// included by dbctrls.pp
{ TDBLookupComboBox }
constructor TDBLookupComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FLookup := TDBLookup.Create(Self);
  EmptyValue := '';
  DisplayEmpty := '';
  FDataLink.OnActiveChange := @ActiveChange;
  inherited DropDownCount := 7;
end;
procedure TDBLookupComboBox.UpdateData(Sender: TObject);
var
  i: Integer;
begin
  // combo that has edit control may have unreliable itemindex like bug 20950
  if Style.HasEditBox then
    ItemIndex := Items.IndexOf(Text);
  i := ItemIndex;
  if i <> -1 then
    FLookup.UpdateData(i)
  else
  if FDatalink.EditingSource then
    FDatalink.Dataset.Cancel
  else
    UpdateLookup;
end;
procedure TDBLookupComboBox.DefineProperties(Filer: TFiler);
begin
  inherited DefineProperties(Filer);
  Filer.DefineProperty('DropDownCount', @ReadDropDownCount, nil, false);
end;
function TDBLookupComboBox.DoEdit: boolean;
begin
  if IsUnbound then begin
    if FDetectedEvents and DBCBEVENT_CHANGE <> 0 then
      DoOnChange;
    if DetectedEvents and DBCBEVENT_SELECT <>0 then begin
      FLookup.UpdateData(ItemIndex);
      DoOnSelect;
    end;
    if DetectedEvents and DBCBEVENT_CLOSEUP <>0 then
      DoOnCloseUp;
  end
  else
    Result := inherited DoEdit;
end;
procedure TDBLookupComboBox.DoAutoCompleteSelect;
begin
  if IsUnbound then
    FLookup.UpdateData(ItemIndex)
  else
    UpdateData(Self);
end;
function TDBLookupComboBox.IsUnbound: boolean;
begin
  result := (FDataLink.DataSource=nil) or (DataField='');
end;
procedure TDBLookupComboBox.ActiveChange(Sender: TObject);
begin
  if FDataLink.Active then
    UpdateLookup;
end;
function TDBLookupComboBox.GetDisplayEmpty: String;
begin
  Result := FLookup.DisplayEmpty;
end;
function TDBLookupComboBox.GetDropDownRows: Integer;
begin
  result := inherited DropDownCount;
end;
function TDBLookupComboBox.GetEmptyValue: String;
begin
 Result := FLookup.EmptyValue;
end;
procedure TDBLookupComboBox.DataChange(Sender: TObject);
begin
  UpdateItemIndex;
end;
procedure TDBLookupComboBox.DoOnSelect;
begin
  if not Style.HasEditBox then
    UpdateRecord;
  inherited DoOnSelect;
end;
procedure TDBLookupComboBox.DestroyWnd;
begin
  inherited;
  //after handle destroy Items address changes
  FLookup.ControlItems := Items;
end;
procedure TDBLookupComboBox.KeyDown(var Key: Word; Shift: TShiftState);
begin
  if FLookup.HandleNullKey(Key, Shift) then
  begin
    //clear selection
    ItemIndex := -1;
    //Text := '';
  end;
  inherited KeyDown(Key, Shift);
end;
procedure TDBLookupComboBox.UTF8KeyPress(var UTF8Key: TUTF8Char);
begin
  if (not IsUnbound) and (not FDataLink.CanModify) then
    UTF8Key := '';
  inherited UTF8KeyPress(UTF8Key);
end;
procedure TDBLookupComboBox.Loaded;
begin
  inherited Loaded;
  UpdateLookup;
end;
procedure TDBLookupComboBox.InitializeWnd;
begin
  inherited InitializeWnd;
  //after handle allocation Items address changes
  FLookup.ControlItems := Items;
end;
function TDBLookupComboBox.GetKeyField: string;
begin
  Result := FLookup.KeyField;
end;
function TDBLookupComboBox.GetKeyValue: variant;
begin
  result := FLookup.GetKeyValue(ItemIndex);
end;
function TDBLookupComboBox.GetListField: string;
begin
  Result := FLookup.ListField;
end;
function TDBLookupComboBox.GetListFieldIndex: Integer;
begin
  Result := FLookup.ListFieldIndex;
end;
function TDBLookupComboBox.GetListSource: TDataSource;
begin
  Result := FLookup.ListSource;
end;
function TDBLookupComboBox.GetLookupCache: boolean;
begin
  Result := FLookup.LookupCache;
end;
function TDBLookupComboBox.GetNullValueKey: TShortCut;
begin
  result := FLookup.NullValueKey;
end;
procedure TDBLookupComboBox.SetDisplayEmpty(AValue: String);
begin
  FLookup.DisplayEmpty:=AValue;
  UpdateLookup;
end;
procedure TDBLookupComboBox.SetDropDownRows(AValue: Integer);
begin
  inherited DropDownCount := AValue;
end;
procedure TDBLookupComboBox.SetEmptyValue(AValue: String);
begin
  FLookup.EmptyValue:=AValue;
  UpdateLookup;
end;
function TDBLookupComboBox.GetScrollListDataset: Boolean;
begin
  Result := FLookup.ScrollListDataset;
end;
procedure TDBLookupComboBox.ReadDropDownCount(Reader: TReader);
begin
  inherited DropDownCount := Reader.ReadInteger;
end;
procedure TDBLookupComboBox.SetKeyField(const Value: string);
begin
  FLookup.KeyField := Value;
  UpdateLookup;
end;
procedure TDBLookupComboBox.SetKeyValue(const AValue: variant);
begin
  ItemIndex := FLookup.GetKeyIndex(AValue);
end;
procedure TDBLookupComboBox.SetListField(const Value: string);
begin
  FLookup.ListField := Value;
  UpdateLookup;
end;
procedure TDBLookupComboBox.SetListFieldIndex(const Value: Integer);
begin
  FLookup.ListFieldIndex := Value;
  UpdateLookup;
end;
procedure TDBLookupComboBox.SetListSource(const Value: TDataSource);
begin
  FLookup.ListSource := Value;
  UpdateLookup;
end;
procedure TDBLookupComboBox.SetLookupCache(const Value: boolean);
begin
  FLookup.LookupCache := Value;
  UpdateLookup;
end;
procedure TDBLookupComboBox.SetNullValueKey(const AValue: TShortCut);
begin
  FLookup.NullValueKey := AValue;
end;
procedure TDBLookupComboBox.SetScrollListDataset(AValue: Boolean);
begin
  FLookup.ScrollListDataset := AValue;
end;
procedure TDBLookupComboBox.UpdateLookup;
begin
  if ([csLoading, csDestroying] * ComponentState) = [] then
  begin
    FLookup.Initialize(FDataLink, Items);
    UpdateItemIndex;
  end;
end;
procedure TDBLookupComboBox.UpdateItemIndex;
var
  i: Integer;
begin
  i := FLookup.GetKeyIndex;
  ItemIndex := i;
  if i = -1 then
    Text := '';
end;
 
     |