lcl: MaxLength redo:

- MaxLength = 0 => no limit, MaxLength > 0 => limit. Default = 0 for memo, combo and edit
  - fix gtk, gtk2 code where MaxLength = -1 was used for unlimited length in Memo (why? combo and edit used 0)
  - fix qt combobox SetLength (was occasional forgotten?)
  - cleanup

git-svn-id: trunk@17365 -
This commit is contained in:
paul 2008-11-13 06:42:37 +00:00
parent cd39f7986e
commit 25e9426c10
8 changed files with 66 additions and 37 deletions

View File

@ -149,13 +149,15 @@ end;
Set the maximum length for user input. Set the maximum length for user input.
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
procedure TCustomComboBox.SetMaxLength(Val : integer); procedure TCustomComboBox.SetMaxLength(AValue: integer);
begin begin
if Val < 0 then Val:= 0; if AValue < 0 then
if Val<>MaxLength then begin AValue := 0;
fMaxlength:=Val; if AValue <> MaxLength then
begin
FMaxlength := AValue;
if HandleAllocated then if HandleAllocated then
TWSCustomComboBoxClass(WidgetSetClass).SetMaxLength(Self, Val); TWSCustomComboBoxClass(WidgetSetClass).SetMaxLength(Self, AValue);
end; end;
end; end;
@ -166,11 +168,11 @@ end;
Get the maximum length for user input. Get the maximum length for user input.
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
function TCustomComboBox.GetMaxLength : integer; function TCustomComboBox.GetMaxLength: integer;
begin begin
if HandleAllocated then if HandleAllocated then
FMaxLength := TWSCustomComboBoxClass(WidgetSetClass).GetMaxLength(Self); FMaxLength := TWSCustomComboBoxClass(WidgetSetClass).GetMaxLength(Self);
Result:=FMaxLength; Result := FMaxLength;
end; end;
{------------------------------------------------------------------------------ {------------------------------------------------------------------------------
@ -749,9 +751,9 @@ begin
ControlStyle := ControlStyle - [csCaptureMouse]; ControlStyle := ControlStyle - [csCaptureMouse];
SetInitialBounds(0,0,GetControlClassDefaultSize.X,GetControlClassDefaultSize.Y); SetInitialBounds(0,0,GetControlClassDefaultSize.X,GetControlClassDefaultSize.Y);
FItems := TStringlist.Create; FItems := TStringlist.Create;
FItemIndex:=-1; FItemIndex := -1;
FMaxLength := -1; FMaxLength := 0;
FDropDownCount:=8; FDropDownCount := 8;
FCanvas := TControlCanvas.Create; FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Control := Self; TControlCanvas(FCanvas).Control := Self;
ArrowKeysTraverseList := True; ArrowKeysTraverseList := True;

View File

@ -50,7 +50,7 @@ begin
//TCustomMemo also inherits from here but it's create changes fcompstyle to csMemo //TCustomMemo also inherits from here but it's create changes fcompstyle to csMemo
ControlStyle := ControlStyle - [csCaptureMouse]; ControlStyle := ControlStyle - [csCaptureMouse];
FCompStyle := csEdit; FCompStyle := csEdit;
FMaxLength:= -1; FMaxLength := 0;
ParentColor := false; ParentColor := false;
TabStop := true; TabStop := true;
SetInitialBounds(0,0,GetControlClassDefaultSize.X,GetControlClassDefaultSize.Y); SetInitialBounds(0,0,GetControlClassDefaultSize.X,GetControlClassDefaultSize.Y);
@ -294,10 +294,14 @@ end;
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
procedure TCustomEdit.SetMaxLength(Value : Integer); procedure TCustomEdit.SetMaxLength(Value : Integer);
begin begin
if Value=MaxLength then exit; if Value < 0 then
FMaxLength := Value; Value := 0;
if HandleAllocated then if Value <> MaxLength then
TWSCustomEditClass(WidgetSetClass).SetMaxLength(Self, Value); begin
FMaxLength := Value;
if HandleAllocated then
TWSCustomEditClass(WidgetSetClass).SetMaxLength(Self, Value);
end;
end; end;
{------------------------------------------------------------------------------ {------------------------------------------------------------------------------

View File

@ -1972,17 +1972,16 @@ begin
if (NewTextLength = 1) and (char^ = #13) and (LineEnding = #10) then if (NewTextLength = 1) and (char^ = #13) and (LineEnding = #10) then
char^ := #10; char^ := #10;
Memo:= TCustomMemo(Data); Memo:= TCustomMemo(Data);
if Memo.MaxLength < 0 then Exit; if Memo.MaxLength <= 0 then Exit;
CurrLength:= gtk_text_get_length(PGtkText(widget)); CurrLength:= gtk_text_get_length(PGtkText(widget));
if CurrLength + NewTextLength <= Memo.MaxLength then Exit; if CurrLength + NewTextLength <= Memo.MaxLength then Exit;
CutLength:= CurrLength + NewTextLength - Memo.MaxLength; CutLength:= CurrLength + NewTextLength - Memo.MaxLength;
if NewTextLength - CutLength > 0 then begin if NewTextLength - CutLength > 0 then
gtk_editable_insert_text(PGtkEditable(widget), char, gtk_editable_insert_text(PGtkEditable(widget), char,
NewTextLength - CutLength, Position); NewTextLength - CutLength, Position);
end;
g_signal_stop_emission_by_name(PGtkObject(widget), 'insert_text'); g_signal_stop_emission_by_name(PGtkObject(widget), 'insert_text');
end; end;

View File

@ -1817,17 +1817,16 @@ end;
class procedure TGtkWSCustomMemo.SetMaxLength(const ACustomEdit: TCustomEdit; class procedure TGtkWSCustomMemo.SetMaxLength(const ACustomEdit: TCustomEdit;
NewLength: integer); NewLength: integer);
var var
ImplWidget : PGtkWidget; ImplWidget: PGtkWidget;
i: integer; i: integer;
begin begin
if (ACustomEdit.MaxLength >= 0) then begin if (ACustomEdit.MaxLength > 0) then
//debugln('TGtkWSCustomMemo.SetMaxLength ',DbgSName(ACustomEdit)); begin
ImplWidget:= GetWidgetInfo(PGtkWidget(ACustomEdit.Handle), true)^.CoreWidget; ImplWidget := GetWidgetInfo(PGtkWidget(ACustomEdit.Handle), true)^.CoreWidget;
i:= gtk_text_get_length(GTK_TEXT(ImplWidget)); i := gtk_text_get_length(GTK_TEXT(ImplWidget));
if i > ACustomEdit.MaxLength then begin if i > ACustomEdit.MaxLength then
gtk_editable_delete_text(PGtkOldEditable(ImplWidget), gtk_editable_delete_text(PGtkOldEditable(ImplWidget),
ACustomEdit.MaxLength, i); ACustomEdit.MaxLength, i);
end;
end; end;
end; end;

View File

@ -50,18 +50,19 @@ begin
{ GTK2 does not provide its own max. length for memos { GTK2 does not provide its own max. length for memos
so we have to do our own. } so we have to do our own. }
if TControl(WidgetInfo^.LCLObject) is TCustomMemo then begin if TControl(WidgetInfo^.LCLObject) is TCustomMemo then
Memo:= TCustomMemo(WidgetInfo^.LCLObject); begin
if Memo.MaxLength < 0 then Exit; Memo := TCustomMemo(WidgetInfo^.LCLObject);
if Memo.MaxLength <= 0 then Exit;
CurrLength:= gtk_text_buffer_get_char_count(TextBuffer); CurrLength := gtk_text_buffer_get_char_count(TextBuffer);
if CurrLength + NewTextLength <= Memo.MaxLength then Exit; if CurrLength + NewTextLength <= Memo.MaxLength then
Exit;
CutLength:= CurrLength + NewTextLength - Memo.MaxLength; CutLength := CurrLength + NewTextLength - Memo.MaxLength;
if NewTextLength - CutLength > 0 then begin if NewTextLength - CutLength > 0 then
gtk_text_buffer_insert(TextBuffer, StartIter, TheText, NewTextLength - CutLength); gtk_text_buffer_insert(TextBuffer, StartIter, TheText, NewTextLength - CutLength);
end;
g_signal_stop_emission_by_name(PGtkObject(Textbuffer), 'insert-text'); g_signal_stop_emission_by_name(PGtkObject(Textbuffer), 'insert-text');
end; end;

View File

@ -4336,8 +4336,10 @@ end;
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
function TQtWidgetSet.SetFocus(hWnd: HWND): HWND; function TQtWidgetSet.SetFocus(hWnd: HWND): HWND;
{$IFDEF UNIX}
var var
QtEdit: IQtEdit; QtEdit: IQtEdit;
{$ENDIF}
begin begin
if hwnd<>0 then if hwnd<>0 then
begin begin

View File

@ -96,6 +96,7 @@ type
NewTraverseList: boolean); override; NewTraverseList: boolean); override;
class procedure SetDropDownCount(const ACustomComboBox: TCustomComboBox; NewCount: Integer); override; class procedure SetDropDownCount(const ACustomComboBox: TCustomComboBox; NewCount: Integer); override;
class procedure SetItemIndex(const ACustomComboBox: TCustomComboBox; NewIndex: integer); override; class procedure SetItemIndex(const ACustomComboBox: TCustomComboBox; NewIndex: integer); override;
class procedure SetMaxLength(const ACustomComboBox: TCustomComboBox; NewLength: integer); override;
class procedure SetStyle(const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle); override; class procedure SetStyle(const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle); override;
class procedure Sort(const ACustomComboBox: TCustomComboBox; AList: TStrings; IsSorted: boolean); override; class procedure Sort(const ACustomComboBox: TCustomComboBox; AList: TStrings; IsSorted: boolean); override;
@ -1320,6 +1321,28 @@ begin
TQtComboBox(ACustomComboBox.Handle).setCurrentIndex(NewIndex); TQtComboBox(ACustomComboBox.Handle).setCurrentIndex(NewIndex);
end; end;
class procedure TQtWSCustomComboBox.SetMaxLength(
const ACustomComboBox: TCustomComboBox; NewLength: integer);
var
Widget: TQtWidget;
QtEdit: IQtEdit;
MaxLength: Integer;
begin
if not WSCheckHandleAllocated(ACustomComboBox, 'SetMaxLength') then
Exit;
Widget := TQtWidget(ACustomComboBox.Handle);
if Supports(Widget, IQtEdit, QtEdit) then
begin
// qt doesn't accept -1
MaxLength := QtEdit.getMaxLength;
if (NewLength <= 0) or (NewLength > QtMaxEditLength) then
NewLength := QtMaxEditLength;
if NewLength <> MaxLength then
QtEdit.setMaxLength(NewLength);
end;
end;
class procedure TQtWSCustomComboBox.SetStyle( class procedure TQtWSCustomComboBox.SetStyle(
const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle); const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle);
begin begin

View File

@ -322,7 +322,7 @@ type
procedure SetDroppedDown(const AValue: Boolean); virtual; procedure SetDroppedDown(const AValue: Boolean); virtual;
procedure SetItemHeight(const AValue: Integer); virtual; procedure SetItemHeight(const AValue: Integer); virtual;
procedure SetItemIndex(Val: integer); virtual; procedure SetItemIndex(Val: integer); virtual;
procedure SetMaxLength(Val: integer); virtual; procedure SetMaxLength(AValue: integer); virtual;
procedure SetSelLength(Val: integer); virtual; procedure SetSelLength(Val: integer); virtual;
procedure SetSelStart(Val: integer); virtual; procedure SetSelStart(Val: integer); virtual;
procedure SetSelText(const Val: string); virtual; procedure SetSelText(const Val: string); virtual;
@ -384,9 +384,8 @@ type
property SelStart: integer read GetSelStart write SetSelStart;// byte position property SelStart: integer read GetSelStart write SetSelStart;// byte position
property SelText: String read GetSelText write SetSelText; property SelText: String read GetSelText write SetSelText;
property Style: TComboBoxStyle read FStyle write SetStyle; property Style: TComboBoxStyle read FStyle write SetStyle;
property Text;
published
property TabStop default true; property TabStop default true;
property Text;
end; end;
@ -729,7 +728,7 @@ type
property CaretPos: TPoint read GetCaretPos write SetCaretPos; property CaretPos: TPoint read GetCaretPos write SetCaretPos;
property CharCase: TEditCharCase read FCharCase write SetCharCase default ecNormal; property CharCase: TEditCharCase read FCharCase write SetCharCase default ecNormal;
property EchoMode: TEchoMode read FEchoMode write SetEchoMode default emNormal; property EchoMode: TEchoMode read FEchoMode write SetEchoMode default emNormal;
property MaxLength: Integer read FMaxLength write SetMaxLength default -1; property MaxLength: Integer read FMaxLength write SetMaxLength default 0;
property Modified: Boolean read GetModified write SetModified; property Modified: Boolean read GetModified write SetModified;
property OnChange: TNotifyEvent read FOnChange write FOnChange; property OnChange: TNotifyEvent read FOnChange write FOnChange;
property PasswordChar: Char read FPasswordChar write SetPasswordChar default #0; property PasswordChar: Char read FPasswordChar write SetPasswordChar default #0;