diff options
-rw-r--r-- | examples/gui/combobox/frm_main.pas | 22 | ||||
-rw-r--r-- | src/gui/gui_editcombo.pas | 152 | ||||
-rw-r--r-- | src/gui/gui_listbox.pas | 1 |
3 files changed, 88 insertions, 87 deletions
diff --git a/examples/gui/combobox/frm_main.pas b/examples/gui/combobox/frm_main.pas index c4ca0b97..0f91b314 100644 --- a/examples/gui/combobox/frm_main.pas +++ b/examples/gui/combobox/frm_main.pas @@ -18,7 +18,6 @@ type private procedure rbChanged(Sender: TObject); procedure cbAutoCompleteChanged(Sender: TObject); - procedure cbAutoDropDownChanged(Sender: TObject); procedure cbAllowNewChanged(Sender: TObject); procedure btnAdd1Clicked(Sender: TObject); procedure btnFocusClicked(Sender: TObject); @@ -35,7 +34,6 @@ type lblName1: TfpgLabel; lblName2: TfpgLabel; EditCombo1: TfpgEditCombo; - cbAutoDropdown: TfpgCheckBox; lblName3: TfpgLabel; lblName4: TfpgLabel; cbAllowNew: TfpgComboBox; @@ -71,11 +69,6 @@ begin EditCombo1.AutoCompletion := cbAutoComplete.Checked; end; -procedure TMainForm.cbAutoDropDownChanged(Sender: TObject); -begin - EditCombo1.AutoDropDown := cbAutoDropdown.Checked; -end; - procedure TMainForm.cbAllowNewChanged(Sender: TObject); begin if cbAllowNew.Text = 'anNo' then @@ -237,17 +230,6 @@ begin SetPosition(8, 88, 168, 21); end; - cbAutoDropdown := TfpgCheckBox.Create(self); - with cbAutoDropdown do - begin - Name := 'cbAutoDropdown'; - SetPosition(216, 188, 120, 19); - FontDesc := '#Label1'; - TabOrder := 9; - Text := 'Auto Dropdown'; - OnChange := @cbAutoDropDownChanged; - end; - lblName3 := TfpgLabel.Create(self); with lblName3 do begin @@ -270,7 +252,7 @@ begin with cbAllowNew do begin Name := 'cbAllowNew'; - SetPosition(220, 212, 100, 22); + SetPosition(220, 196, 100, 22); FontDesc := '#List'; Items.Add('anNo'); Items.Add('anYes'); @@ -293,10 +275,10 @@ begin begin Name := 'rbName1'; SetPosition(16, 24, 80, 20); + Checked := True; FontDesc := '#Label1'; Text := '8 items'; Tag := 8; - Checked := True; OnChange := @rbChanged; end; diff --git a/src/gui/gui_editcombo.pas b/src/gui/gui_editcombo.pas index 4411b5cc..96d2188f 100644 --- a/src/gui/gui_editcombo.pas +++ b/src/gui/gui_editcombo.pas @@ -67,7 +67,6 @@ type TfpgBaseEditCombo = class(TfpgBaseComboBox) private FAutoCompletion: Boolean; - FAutoDropDown: Boolean; FAllowNew: TAllowNew; FText: string; FSelectedItem: integer; @@ -76,6 +75,8 @@ type procedure SetAllowNew(const AValue: TAllowNew); procedure InternalBtnClick(Sender: TObject); procedure InternalListBoxSelect(Sender: TObject); + procedure InternalListBoxKeyPress(Sender: TObject; var keycode: word; var shiftstate: TShiftState; + var consumed: Boolean); protected FMargin: integer; FDropDown: TfpgPopupWindow; @@ -96,7 +97,6 @@ type procedure HandleResize(awidth, aheight: TfpgCoord); override; procedure HandlePaint; override; property AutoCompletion: Boolean read FAutocompletion write FAutoCompletion default False; - property AutoDropDown: Boolean read FAutoDropDown write FAutoDropDown default False; property AllowNew: TAllowNew read FAllowNew write SetAllowNew default anNo; property BackgroundColor default clBoxColor; property TextColor default clText1; @@ -112,7 +112,6 @@ type TfpgEditCombo = class(TfpgBaseEditCombo) published property AutoCompletion; - property AutoDropDown; property AllowNew; property BackgroundColor; property DropDownCount; @@ -151,6 +150,7 @@ type TDropDownWindow = class(TfpgPopupWindow) private FCallerWidget: TfpgWidget; + ListBox: TfpgListBox; protected procedure HandlePaint; override; procedure HandleKeyChar(var AText: TfpgChar; var shiftstate: TShiftState; var consumed: Boolean); override; @@ -159,7 +159,6 @@ type procedure HandleHide; override; public constructor Create(AOwner: TComponent); override; - ListBox: TfpgListBox; property CallerWidget: TfpgWidget read FCallerWidget write FCallerWidget; end; @@ -284,6 +283,7 @@ begin ddw.Width := Width; ddw.CallerWidget := self; ddw.ListBox.OnSelect := @InternalListBoxSelect; + ddw.ListBox.OnKeyPress := @InternalListBoxKeyPress; // Assign combobox text items to internal listbox if FAutoCompletion then @@ -340,7 +340,8 @@ begin if Items[i]= TDropDownWindow(FDropDown).ListBox.Items[TDropDownWindow(FDropDown).ListBox.FocusItem] then begin FocusItem := i; - FText:= TDropDownWindow(FDropDown).ListBox.Items[TDropDownWindow(FDropDown).ListBox.FocusItem]; + FSelectedItem:= i; + FText:= Items[i]; Break; end; end; @@ -350,6 +351,25 @@ begin Repaint; end; +procedure TfpgBaseEditCombo.InternalListBoxKeyPress(Sender: TObject; var keycode: word; + var shiftstate: TShiftState; var consumed: Boolean); +var + i: Integer; +begin + if ((keycode = keyUp) or (keycode = keyDown)) and (TDropDownWindow(FDropDown).ListBox.FocusItem > -1) then + for i := 0 to Items.Count-1 do + begin + if Items[i]= TDropDownWindow(FDropDown).ListBox.Items[TDropDownWindow(FDropDown).ListBox.FocusItem] then + begin + FSelectedItem:= i; + Break; + end; + end; + + if HasHandle then + Repaint; +end; + procedure TfpgBaseEditCombo.SetText(const AValue: string); var i: integer; @@ -367,6 +387,7 @@ begin begin FocusItem := i; FText:= AValue; + Repaint; Exit; //==> end; end; @@ -402,7 +423,7 @@ begin // Handle only printable characters // Note: This is now UTF-8 compliant! - if (Ord(AText[1]) > 31) and (Ord(AText[1]) < 127) or (Length(AText) > 1) then + if Enabled and (Ord(AText[1]) > 31) and (Ord(AText[1]) < 127) or (Length(AText) > 1) then begin if (FMaxLength <= 0) or (UTF8Length(FText) < FMaxLength) then begin @@ -416,8 +437,7 @@ begin if SameText(UTF8Copy(FItems.Strings[i], 1, UTF8Length(FText)), FText) then begin FSelectedItem:= i; - if AutoDropDown then - DoDropDown; + DoDropDown; Break; end; if FSelectedItem = -1 then @@ -430,8 +450,7 @@ begin if SameText(UTF8Copy(FItems.Strings[i], 1, UTF8Length(FText)), FText) then begin FSelectedItem:= i; - if AutoDropDown then - DoDropDown; + DoDropDown; Break; end; end @@ -458,71 +477,73 @@ var begin hasChanged := False; - consumed := True; + if not Enabled then + consumed := False + else + begin + consumed := True; - case keycode of - keyBackSpace: - begin - if FCursorPos > 0 then + case keycode of + keyBackSpace: begin - UTF8Delete(FText, FCursorPos, 1); - Dec(FCursorPos); - FSelStart := FCursorPos; - if Assigned(FDropDown) then - FDropDown.Close; - FSelectedItem := -1; - for i := 0 to FItems.Count-1 do - if SameText(UTF8Copy(FItems.Strings[i], 1, UTF8Length(FText)), FText) then + if FCursorPos > 0 then begin - FSelectedItem:= i; - if AutoDropDown then + UTF8Delete(FText, FCursorPos, 1); + Dec(FCursorPos); + FSelStart := FCursorPos; + if Assigned(FDropDown) then + FDropDown.Close; + FSelectedItem := -1; + for i := 0 to FItems.Count-1 do + if SameText(UTF8Copy(FItems.Strings[i], 1, UTF8Length(FText)), FText) then + begin + FSelectedItem:= i; DoDropDown; - Break; + Break; + end; + hasChanged := True; + end; + end; + keyDelete: + begin + if FAllowNew <> anNo then + begin + FocusItem := -1; + FSelectedItem := -1; + FNewItem:= True; + hasChanged := True; end; - hasChanged := True; end; - end; - keyDelete: - begin - if FAllowNew <> anNo then + keyReturn, + keyPEnter: begin - FocusItem := -1; - FSelectedItem := -1; - FNewItem:= True; + if FSelectedItem > -1 then + SetText(Items[FSelectedItem]) + else + FocusItem:= -1; + if FNewItem then + case FAllowNew of + anYes: + FItems.Add(FText); + anAsk: + if TfpgMessageDialog.Question(rsNewItemDetected, Format(rsAddNewItem, [FText])) = mbYes then + FItems.Add(FText) + else + begin + FNewItem:= False; + FocusItem := -1; + FText:= ''; + end; + end; hasChanged := True; + if Assigned(FDropDown) then + FDropDown.Close; end; - end; - - keyReturn, - keyPEnter: - begin - if FSelectedItem > -1 then - SetText(Items[FSelectedItem]) - else - FocusItem:= -1; - if FNewItem then - case FAllowNew of - anYes: - FItems.Add(FText); - anAsk: - if TfpgMessageDialog.Question(rsNewItemDetected, Format(rsAddNewItem, [FText])) = mbYes then - FItems.Add(FText) - else - begin - FNewItem:= False; - FocusItem := -1; - FText:= ''; - end; - end; - hasChanged := True; - if Assigned(FDropDown) then - FDropDown.Close; - end; - - else - begin - Consumed := False; + else + begin + Consumed := False; + end; end; end; @@ -727,7 +748,6 @@ begin FMargin := 3; FFocusable := True; FAutocompletion := False; - FAutoDropDown := False; FAllowNew := anNo; FText := ''; diff --git a/src/gui/gui_listbox.pas b/src/gui/gui_listbox.pas index bd493efd..4a4c0798 100644 --- a/src/gui/gui_listbox.pas +++ b/src/gui/gui_listbox.pas @@ -792,7 +792,6 @@ begin if SameText(LeftStr(FItems.Strings[i], Length(AText)), AText) then begin FocusItem := i; - Consumed := True; break; end; end; { for } |