tvplanit: Improved automatic layout of contact editor dialog. Some cosmetic changes.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4862 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2016-06-28 22:49:33 +00:00
parent e445237ac3
commit 10325574c0
2 changed files with 160 additions and 140 deletions

View File

@ -19,9 +19,9 @@ object ContactEditForm: TContactEditForm
Height = 314 Height = 314
Top = 0 Top = 0
Width = 433 Width = 433
ActivePage = tabContact ActivePage = tabMain
Align = alClient Align = alClient
TabIndex = 1 TabIndex = 0
TabOrder = 0 TabOrder = 0
OnChange = tsContactsChange OnChange = tsContactsChange
object tabMain: TTabSheet object tabMain: TTabSheet
@ -205,7 +205,7 @@ object ContactEditForm: TContactEditForm
Left = 136 Left = 136
Height = 23 Height = 23
Top = 200 Top = 200
Width = 121 Width = 247
MaxLength = 50 MaxLength = 50
OnChange = ItemChanged OnChange = ItemChanged
TabOrder = 10 TabOrder = 10
@ -223,7 +223,7 @@ object ContactEditForm: TContactEditForm
Left = 136 Left = 136
Height = 23 Height = 23
Top = 127 Top = 127
Width = 249 Width = 247
ItemHeight = 15 ItemHeight = 15
MaxLength = 65535 MaxLength = 65535
TabOrder = 5 TabOrder = 5
@ -240,13 +240,10 @@ object ContactEditForm: TContactEditForm
Left = 136 Left = 136
Height = 21 Height = 21
Top = 175 Top = 175
Width = 249 Width = 247
Font.Height = -11 ItemHeight = 15
Font.Name = 'MS Sans Serif'
ItemHeight = 13
MaxLength = 65535 MaxLength = 65535
OnChange = cboxCountryChange OnChange = cboxCountryChange
ParentFont = False
TabOrder = 8 TabOrder = 8
end end
object FirstNameEdit: TEdit object FirstNameEdit: TEdit
@ -384,7 +381,7 @@ object ContactEditForm: TContactEditForm
end end
object tabCustom: TTabSheet object tabCustom: TTabSheet
Caption = 'RSCustom' Caption = 'RSCustom'
ClientHeight = 251 ClientHeight = 286
ClientWidth = 425 ClientWidth = 425
ImageIndex = 2 ImageIndex = 2
object CustomLbl1: TLabel object CustomLbl1: TLabel
@ -393,6 +390,7 @@ object ContactEditForm: TContactEditForm
Top = 12 Top = 12
Width = 64 Width = 64
Caption = 'CustomLbl1' Caption = 'CustomLbl1'
FocusControl = Custom1Edit
ParentColor = False ParentColor = False
end end
object CustomLbl2: TLabel object CustomLbl2: TLabel
@ -401,6 +399,7 @@ object ContactEditForm: TContactEditForm
Top = 36 Top = 36
Width = 64 Width = 64
Caption = 'CustomLbl2' Caption = 'CustomLbl2'
FocusControl = Custom2Edit
ParentColor = False ParentColor = False
end end
object CustomLbl3: TLabel object CustomLbl3: TLabel
@ -409,6 +408,7 @@ object ContactEditForm: TContactEditForm
Top = 60 Top = 60
Width = 64 Width = 64
Caption = 'CustomLbl3' Caption = 'CustomLbl3'
FocusControl = Custom3Edit
ParentColor = False ParentColor = False
end end
object CustomLbl4: TLabel object CustomLbl4: TLabel
@ -417,6 +417,7 @@ object ContactEditForm: TContactEditForm
Top = 84 Top = 84
Width = 64 Width = 64
Caption = 'CustomLbl4' Caption = 'CustomLbl4'
FocusControl = Custom4Edit
ParentColor = False ParentColor = False
end end
object Custom1Edit: TEdit object Custom1Edit: TEdit

View File

@ -41,7 +41,7 @@ uses
SysUtils, SysUtils,
{$IFDEF VERSION6} Variants, {$ENDIF} {$IFDEF VERSION6} Variants, {$ENDIF}
Classes, Graphics, Controls, Forms, Dialogs, VpData, ExtCtrls, StdCtrls, Classes, Graphics, Controls, Forms, Dialogs, VpData, ExtCtrls, StdCtrls,
VpException, VpMisc, VpBase, VpSR, VpDlg, VpBaseDS, ComCtrls; VpException, VpMisc, VpBase, VpSR, VpDlg, VpBaseDS, ComCtrls, Types;
type type
{ forward declarations } { forward declarations }
@ -108,8 +108,7 @@ type
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure ItemChanged(Sender: TObject); procedure ItemChanged(Sender: TObject);
procedure cboxCountryChange(Sender: TObject); procedure cboxCountryChange(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
Shift: TShiftState);
procedure tsContactsChange(Sender: TObject); procedure tsContactsChange(Sender: TObject);
procedure FormShow(Sender: TObject); procedure FormShow(Sender: TObject);
private private
@ -141,6 +140,7 @@ type
property Placement; property Placement;
end; end;
implementation implementation
{$IFDEF LCL} {$IFDEF LCL}
@ -152,7 +152,7 @@ implementation
uses uses
VpConst; VpConst;
{== Utility functions ===================================} { Utility functions }
function Max(const a, b: Integer): Integer; function Max(const a, b: Integer): Integer;
begin begin
@ -162,14 +162,14 @@ begin
Result := b; Result := b;
end; end;
{== TEditForm ===========================================}
{ TContactEditForm }
procedure TContactEditForm.FormCreate(Sender: TObject); procedure TContactEditForm.FormCreate(Sender: TObject);
begin begin
ReturnCode := rtAbandon; ReturnCode := rtAbandon;
SetCaptions; SetCaptions;
end; end;
{=====}
procedure TContactEditForm.SetCaptions; procedure TContactEditForm.SetCaptions;
begin begin
@ -197,7 +197,6 @@ begin
CustomLbl3.Caption := RSCustom3; CustomLbl3.Caption := RSCustom3;
CustomLbl4.Caption := RSCustom4; CustomLbl4.Caption := RSCustom4;
end; end;
{=====}
procedure TContactEditForm.OKBtnClick(Sender: TObject); procedure TContactEditForm.OKBtnClick(Sender: TObject);
begin begin
@ -208,14 +207,12 @@ begin
ReturnCode := rtCommit; ReturnCode := rtCommit;
Close; Close;
end; end;
{=====}
procedure TContactEditForm.CancelBtnClick(Sender: TObject); procedure TContactEditForm.CancelBtnClick(Sender: TObject);
begin begin
ReturnCode := rtAbandon; ReturnCode := rtAbandon;
Close; Close;
end; end;
{=====}
procedure TContactEditForm.DePopulateSelf; procedure TContactEditForm.DePopulateSelf;
begin begin
@ -257,7 +254,6 @@ begin
Contact.Category := cboxCategory.ItemIndex; Contact.Category := cboxCategory.ItemIndex;
end; end;
{=====}
procedure TContactEditForm.PopulateSelf; procedure TContactEditForm.PopulateSelf;
var var
@ -323,7 +319,6 @@ begin
DisplayCurrentCountry; DisplayCurrentCountry;
end; end;
{=====}
procedure TContactEditForm.ItemChanged(Sender: TObject); procedure TContactEditForm.ItemChanged(Sender: TObject);
begin begin
@ -337,7 +332,6 @@ begin
else else
LastNameEdit.MaxLength := 100; LastNameEdit.MaxLength := 100;
end; end;
{=====}
procedure TContactEditForm.ArrangeControls; procedure TContactEditForm.ArrangeControls;
begin begin
@ -364,34 +358,41 @@ begin
end; end;
tsContacts.ActivePage := tabMain; tsContacts.ActivePage := tabMain;
end; end;
{=====}
procedure TContactEditForm.ResizeControls; procedure TContactEditForm.ResizeControls;
const const
ComboArrowWidth = 32; ComboArrowWidth = 32;
FieldVertSep = 25; FieldVertSep = 25;
FormRightBorder = 20; // FormRightBorder = 20;
MinFormWidth = 265; // MinFormWidth = 265;
FormHeightOffset = 103; // FormHeightOffset = 103;
MinFormHeight = 250; // MinFormHeight = 250;
TopField = 8; TopField = 8;
DIST = 4; // distance between label and edit/combo
type type
TLabelArray = array[0..10] of TLabel; TLabelArray = array of TLabel;
TComboboxArray = array of TCombobox;
TEditArray = array of TEdit;
var var
Labels: TLabelArray; Labels: TLabelArray;
Comboboxes: TComboboxArray;
Edits: TEditArray;
LargestLabel: Integer; LargestLabel: Integer;
WidestField: Integer; WidestField: Integer;
i, j: Integer; i, j: Integer;
OldFont: TFont; OldFont: TFont;
FieldTop: Integer; FieldTop: Integer;
delta: Integer; delta: Integer;
horMargin: Integer; // Margin at left and right from tabsheet to label/edit
corr: Integer; // difference between form's client width and tabsheet width
begin begin
{ Note: The resizing algorithm is dependent upon the labels having their { Note: The resizing algorithm is dependent upon the labels having their
FocusControl property set to the corresponding edit field or combobox. } FocusControl property set to the corresponding edit field or combobox. }
SetLength(Labels, 11);
Labels[0] := LastNameLbl; Labels[0] := LastNameLbl;
Labels[1] := FirstNameLbl; Labels[1] := FirstNameLbl;
Labels[2] := TitleLbl; Labels[2] := TitleLbl;
@ -408,77 +409,50 @@ begin
for i := Low(Labels) to High(Labels) do for i := Low(Labels) to High(Labels) do
LargestLabel := Max(LargestLabel, GetLabelWidth(Labels[i])); LargestLabel := Max(LargestLabel, GetLabelWidth(Labels[i]));
{ Determine height of label based upon whether large or small fonts are { Determine width of label based upon whether large or small fonts are
in effect. } in effect. }
for i := Low(Labels) to High(Labels) do begin for i := Low(Labels) to High(Labels) do begin
Labels[i].Width := LargestLabel; Labels[i].Width := LargestLabel;
Labels[i].FocusControl.Left := LastNameLbl.Left + LargestLabel + 4; Labels[i].FocusControl.Left := LastNameLbl.Left + LargestLabel + DIST;
end; end;
horMargin := Labels[0].Left;
if cboxCountry.Visible then begin widestField := 250;
WidestField := 0;
OldFont := TFont.Create;
try
Canvas.Font.Assign(cboxCountry.Font);
try
for j := 0 to cboxCountry.Items.Count - 1 do begin
i := Canvas.TextWidth(cboxCountry.Items[j]);
if i > WidestField then
WidestField := i;
end;
WidestField := WidestField + ComboArrowWidth;
cboxCountry.Width := WidestField;
finally
Canvas.Font.Assign(OldFont);
end;
finally
OldFont.Free;
end;
if (cboxCountry.Left + cboxCountry.Width + FormRightBorder > MinFormWidth) and
(not cboxState.Visible)
then
Width := cboxCountry.Left + cboxCountry.Width + FormRightBorder
else
Width := MinFormWidth;
end;
if cboxState.Visible then begin { If localization file is loaded determine the width of the country and state
WidestField := 0; comboboxes }
if cboxCountry.Visible or cboxState.Visible then begin
OldFont := TFont.Create; OldFont := TFont.Create;
try try
OldFont.Assign(Canvas.Font); OldFont.Assign(Canvas.Font);
Canvas.Font.Assign(cboxCountry.Font);
try if cboxCountry.Visible then begin
for j := 0 to cboxState.Items.Count - 1 do begin Canvas.Font.Assign(cboxCountry.Font);
i := Canvas.TextWidth(cboxState.Items[j]); for j := 0 to cboxCountry.Items.Count - 1 do
if i > WidestField then widestField := Max(widestField, Canvas.TextWidth(cboxCountry.Items[j]) + ComboArrowWidth);
WidestField := i; end;
end;
WidestField := WidestField + ComboArrowWidth; if cboxState.Visible then begin
cboxState.Width := WidestField; Canvas.Font.Assign(cboxCountry.Font);
finally for j := 0 to cboxState.Items.Count - 1 do
Canvas.Font.Assign(OldFont); widestField := Max(widestfield, Canvas.TextWidth(cboxState.Items[j]) + ComboArrowWidth);
end; end;
finally finally
Canvas.Font.Assign(OldFont);
OldFont.Free; OldFont.Free;
end; end;
if (cboxState.Left + cboxState.Width + FormRightBorder > MinFormWidth) and
(not cboxCountry.Visible)
then
Width := cboxState.Left + cboxState.Width + FormRightBorder
else
Width := MinFormWidth;
end; end;
if (cboxState.Visible) and (cboxCountry.Visible) then begin { Set form width according to widest field }
FieldTop := cboxCountry.Left + cboxCountry.Width + FormRightBorder; corr := ClientWidth - tabMain.ClientWidth;
if cboxState.Left + cboxState.Width + FormRightBorder > FieldTop then ClientWidth := LastNameEdit.Left + widestfield + horMargin + corr;
FieldTop := cboxState.Left + cboxState.Width + FormRightBorder;
if (FieldTop > MinFormWidth) then { Set edit and combo widths }
Width := FieldTop for i:= Low(Labels) to High(Labels) do
else if (Labels[i].FocusControl <> ZipCodeEdit) then
Width := MinFormWidth; Labels[i].FocusControl.Width := widestfield;
end; cboxCountry.Width := widestField;
cboxState.Width := widestField;
{ Vertically arrange the fields. } { Vertically arrange the fields. }
delta := (Labels[0].FocusControl.Height - labels[0].Height) div 2; delta := (Labels[0].FocusControl.Height - labels[0].Height) div 2;
@ -490,27 +464,80 @@ begin
inc(FieldTop, FieldVertSep); inc(FieldTop, FieldVertSep);
end; end;
{ Set form height such that first tab is filled completely by controls }
ClientHeight := cboxCategory.Top + cboxCategory.Height + TopField + ClientHeight := cboxCategory.Top + cboxCategory.Height + TopField +
pnlBottom.Height + tsContacts.Height - tabMain.Height; pnlBottom.Height + tsContacts.Height - tabMain.Height;
{
if FieldTop + FormHeightOffset > MinFormHeight then
Height := FieldTop + FormHeightOffset
else
Height := MinFormHeight;
}
EMailLbl.Left := EMailEdit.Left - GetLabelWidth(EMailLbl) - 8;
Custom1Edit.Left := CustomLbl4.Left + GetLabelWidth(CustomLbl4) + 8; { Page "Contact" }
Custom2Edit.Left := Custom1Edit.Left; SetLength(Comboboxes, 5);
Custom3Edit.Left := Custom1Edit.Left; Comboboxes[0] := cboxPhoneLbl1;
Custom4Edit.Left := Custom1Edit.Left; Comboboxes[1] := cboxPhoneLbl2;
Custom1Edit.Width := ClientWidth - 8 - Custom1Edit.Left - 8; Comboboxes[2] := cboxPhoneLbl3;
Custom2Edit.Width := Custom1Edit.Width; Comboboxes[3] := cboxPhoneLbl4;
Custom3Edit.Width := Custom1Edit.Width; Comboboxes[4] := cboxPhoneLbl5;
Custom4Edit.Width := Custom1Edit.Width;
SetLength(Edits, 5);
Edits[0] := Phone1Edit;
Edits[1] := Phone2Edit;
Edits[2] := Phone3Edit;
Edits[3] := Phone4Edit;
Edits[4] := Phone5Edit;
largestLabel := GetLabelWidth(EMailLbl);
OldFont := TFont.Create;
try
OldFont.Assign(Canvas.Font);
Canvas.Font.Assign(cboxPhoneLbl1.Font);
for i:=0 to cboxPhoneLbl1.Items.Count-1 do
largestLabel := Max(cboxPhoneLbl1.Canvas.TextWidth(cboxPhoneLbl1.Items[i]) + ComboArrowWidth, largestlabel);
finally
Canvas.Font.Assign(OldFont);
OldFont.Free;
end;
FieldTop := TopField;
for i:=Low(Comboboxes) to High(Comboboxes) do begin
Comboboxes[i].Left := horMargin;
Comboboxes[i].Width := largestLabel;
Comboboxes[i].Top := FieldTop;
inc(FieldTop, FieldVertSep);
end;
for i:= Low(Edits) to High(Edits) do begin
Edits[i].Left := cboxPhoneLbl1.Left + cboxPhoneLbl1.Width + DIST;
Edits[i].Width := ClientWidth - Edits[i].Left - horMargin - corr;
Edits[i].Top := Comboboxes[i].Top;
end;
EMailEdit.Left := Phone1Edit.Left;
EMailEdit.Width := Phone1Edit.Width;
EMailEdit.Top := Phone5Edit.Top + FieldVertSep;
EMailLbl.Left := EMailEdit.Left - GetLabelWidth(EMailLbl) - DIST;
EMailLbl.Top := EMailEdit.Top + delta;
{ Page "User-defined" }
SetLength(Labels, 4);
Labels[0] := CustomLbl1;
Labels[1] := CustomLbl2;
Labels[2] := CustomLbl3;
Labels[3] := CustomLbl4;
largestLabel := 0;
for i := Low(Labels) to High(Labels) do
largestLabel := Max(largestLabel, GetLabelWidth(Labels[i]));
FieldTop := TopField;
for i := Low(Labels) to High(Labels) do begin
Labels[i].FocusControl.Left := horMargin + LargestLabel + DIST;
Labels[i].FocusControl.Top := FieldTop;
Labels[i].FocusControl.Width := ClientWidth - Labels[i].FocusControl.Left - horMargin - corr;
Labels[i].Width := LargestLabel;
Labels[i].Left := Labels[i].FocusControl.Left - GetLabelWidth(Labels[i]) - DIST;
Labels[i].Top := FieldTop + delta;
inc(FieldTop, FieldVertSep);
end;
end; end;
{=====}
procedure TContactEditForm.DisplayCurrentCountry; procedure TContactEditForm.DisplayCurrentCountry;
var var
@ -529,7 +556,7 @@ begin
if ControlLink.Localization.Countries.Items[Idx].Address1Caption <> '' then if ControlLink.Localization.Countries.Items[Idx].Address1Caption <> '' then
AddrLbl.Caption := ControlLink.Localization.Countries.Items[Idx].Address1Caption AddrLbl.Caption := ControlLink.Localization.Countries.Items[Idx].Address1Caption
else else
AddrLbl.Caption := 'Address: '; AddrLbl.Caption := RSAddressLbl;
end else begin end else begin
AddrLbl.Visible := False; AddrLbl.Visible := False;
AddressEdit.Visible := False; AddressEdit.Visible := False;
@ -541,7 +568,7 @@ begin
if ControlLink.Localization.Countries.Items[Idx].CityCaption <> '' then if ControlLink.Localization.Countries.Items[Idx].CityCaption <> '' then
CityLbl.Caption := ControlLink.Localization.Countries.Items[Idx].CityCaption CityLbl.Caption := ControlLink.Localization.Countries.Items[Idx].CityCaption
else else
CityLbl.Caption := 'City: '; CityLbl.Caption := RSCityLbl;
end else begin end else begin
CityLbl.Visible := False; CityLbl.Visible := False;
CityEdit.Visible := False; CityEdit.Visible := False;
@ -562,7 +589,7 @@ begin
if ControlLink.Localization.Countries.Items[Idx].StateCaption <> '' then if ControlLink.Localization.Countries.Items[Idx].StateCaption <> '' then
StateLbl.Caption := ControlLink.Localization.Countries.Items[Idx].StateCaption StateLbl.Caption := ControlLink.Localization.Countries.Items[Idx].StateCaption
else else
StateLbl.Caption := 'State: '; StateLbl.Caption := RSStateLbl;
end else begin end else begin
StateLbl.Visible := False; StateLbl.Visible := False;
StateEdit.Visible := False; StateEdit.Visible := False;
@ -575,7 +602,7 @@ begin
if ControlLink.Localization.Countries.Items[Idx].ZipCaption <> '' then if ControlLink.Localization.Countries.Items[Idx].ZipCaption <> '' then
ZipLbl.Caption := ControlLink.Localization.Countries.Items[Idx].ZipCaption ZipLbl.Caption := ControlLink.Localization.Countries.Items[Idx].ZipCaption
else else
ZipLbl.Caption := 'Zip Code: '; ZipLbl.Caption := RSZipCodeLbl;
end else begin end else begin
ZipLbl.Visible := False; ZipLbl.Visible := False;
ZipCodeEdit.Visible := False; ZipCodeEdit.Visible := False;
@ -588,15 +615,40 @@ begin
ResizeControls; ResizeControls;
end; end;
{=====}
procedure TContactEditForm.cboxCountryChange(Sender: TObject); procedure TContactEditForm.cboxCountryChange(Sender: TObject);
begin begin
StateEdit.Text := ''; StateEdit.Text := '';
cboxState.Text := ''; cboxState.Text := '';
DisplayCurrentCountry; DisplayCurrentCountry;
end; end;
{=====}
procedure TContactEditForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then begin
ReturnCode := rtAbandon;
Close;
end;
end;
procedure TContactEditForm.tsContactsChange(Sender: TObject);
begin
if Visible then
if tsContacts.ActivePage = tabMain then
LastNameEdit.SetFocus
else if tsContacts.ActivePage = tabContact then
Phone1Edit.SetFocus
else if tsContacts.ActivePage = tabCustom then
Custom1Edit.SetFocus
else if tsContacts.ActivePage = tabNotes then
NoteEdit.SetFocus;
end;
procedure TContactEditForm.FormShow(Sender: TObject);
begin
if tsContacts.ActivePage = tabMain then
LastNameEdit.SetFocus;
end;
{ TVpContactEditDialog } { TVpContactEditDialog }
@ -631,7 +683,6 @@ begin
DataStore.NotifyDependents; DataStore.NotifyDependents;
end; end;
end; end;
{=====}
function TVpContactEditDialog.AddNewContact: Boolean; function TVpContactEditDialog.AddNewContact: Boolean;
begin begin
@ -650,38 +701,6 @@ begin
end; end;
end; end;
end; end;
{=====}
procedure TContactEditForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then begin
ReturnCode := rtAbandon;
Close;
end;
end;
{=====}
procedure TContactEditForm.tsContactsChange(Sender: TObject);
begin
if Visible then
if tsContacts.ActivePage = tabMain then
LastNameEdit.SetFocus
else if tsContacts.ActivePage = tabContact then
Phone1Edit.SetFocus
else if tsContacts.ActivePage = tabCustom then
Custom1Edit.SetFocus
else if tsContacts.ActivePage = tabNotes then
NoteEdit.SetFocus;
end;
{=====}
procedure TContactEditForm.FormShow(Sender: TObject);
begin
if tsContacts.ActivePage = tabMain then
LastNameEdit.SetFocus;
end;
{=====}
end. end.