Skip to content

Commit ca9b748

Browse files
committed
feat: auto-adjust width of various combo boxes
refs #2551
1 parent a90f73c commit ca9b748

14 files changed

Lines changed: 69 additions & 5 deletions

source/apphelpers.pas

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface
1010
Character, DateUtils, laz.VirtualTrees, SynEdit, SynCompletion, fphttpclient, IntfGraphics, FPImage,
1111
{$IFDEF WINDOWS} Windows, Registry, uDarkStyleParams, {$ENDIF} DelphiCompat,
1212
dbconnection, dbstructures, jsonregistry, lazaruscompat, fpjson, SynEditKeyCmds, LazFileUtils, gettext, LazUTF8,
13-
IniFiles, GraphType, Sockets, Contnrs
13+
IniFiles, GraphType, Sockets, Contnrs, StdCtrls
1414
{$IFDEF DARWIN} , MacOSAll {$ENDIF};
1515

1616
type
@@ -170,6 +170,11 @@ TWinControlHelper = class helper for TWinControl
170170
procedure TrySetFocus;
171171
end;
172172

173+
TCustomComboBoxHelper = class helper for TCustomComboBox
174+
public
175+
procedure AutoSizeItemWidth;
176+
end;
177+
173178
//TSimpleKeyValuePairs = TDictionary<String, String>;
174179

175180
TAppSettingDataType = (adInt, adBool, adString);
@@ -3531,6 +3536,29 @@ procedure TWinControlHelper.TrySetFocus;
35313536
end;
35323537
end;
35333538

3539+
{ TCustomComboBoxHelper }
3540+
3541+
procedure TCustomComboBoxHelper.AutoSizeItemWidth;
3542+
var
3543+
I, TextW, MaxTextW, MaxW: Integer;
3544+
ExtraPad: Integer;
3545+
begin
3546+
MaxW := Scale96ToForm(500);
3547+
3548+
Canvas.Font.Assign(Font);
3549+
ExtraPad := Canvas.TextWidth(' ');
3550+
if Items.Count > DropDownCount then
3551+
Inc(ExtraPad, Canvas.TextWidth(' ')); // leave space for scrollbar
3552+
MaxTextW := 0;
3553+
for I := 0 to Items.Count - 1 do
3554+
begin
3555+
TextW := Canvas.TextWidth(Items[I]) + ExtraPad;
3556+
if TextW > MaxTextW then
3557+
MaxTextW := TextW;
3558+
end;
3559+
3560+
ItemWidth := Min(MaxTextW, MaxW);
3561+
end;
35343562

35353563
{ TAppSettings }
35363564

source/connections.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ procedure Tconnform.ListSessionsFocusChanged(Sender: TBaseVirtualTree;
10541054
ColorBoxBackgroundColor.ItemIndex := 0;
10551055
editDatabases.Text := Sess.AllDatabasesStr;
10561056
comboLibrary.Items := Sess.GetLibraries;
1057+
comboLibrary.AutoSizeItemWidth;
10571058
comboLibrary.ItemIndex := comboLibrary.Items.IndexOf(Sess.LibraryOrProvider);
10581059
if (comboLibrary.ItemIndex = -1) and (comboLibrary.Items.Count > 0) then begin
10591060
comboLibrary.ItemIndex := 0;
@@ -1450,6 +1451,7 @@ procedure Tconnform.comboNetTypeChange(Sender: TObject);
14501451
mainform.LogSQL(Libs.CommaText);
14511452
if Libs.Text <> comboLibrary.Items.Text then begin
14521453
comboLibrary.Items := Libs;
1454+
comboLibrary.AutoSizeItemWidth;
14531455
comboLibrary.ItemIndex := comboLibrary.Items.IndexOf(Params.DefaultLibrary);
14541456
end;
14551457

source/createdatabase.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ procedure TCreateDatabaseForm.FormShow(Sender: TObject);
113113
comboCollation.Items.Add(CollationTable.Col('Collation'));
114114
CollationTable.Next;
115115
end;
116+
comboCollation.AutoSizeItemWidth;
116117
// Pre-select best fitting collation
117118
comboCollation.ItemIndex := comboCollation.Items.IndexOf(CurrentCollation);
118119
if comboCollation.ItemIndex = -1 then

source/customize_highlighter.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ procedure TfrmCustomizeHighlighter.FormCreate(Sender: TObject);
128128
for i:=0 to Highlighters.Count-1 do begin
129129
comboHighlighter.Items.Add(Highlighters[i].GetLanguageName);
130130
end;
131+
comboHighlighter.AutoSizeItemWidth;
131132
end;
132133

133134
procedure TfrmCustomizeHighlighter.FormDestroy(Sender: TObject);

source/event_editor.pas

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ constructor TfrmEventEditor.Create(AOwner: TComponent);
8080
Mainform.SynCompletionProposal.AddEditor(SynMemoBody);
8181
comboEveryInterval.Items := Explode('|', 'YEAR|QUARTER|MONTH|DAY|HOUR|MINUTE|WEEK|SECOND|YEAR_MONTH|'+
8282
'DAY_HOUR|DAY_MINUTE|DAY_SECOND|HOUR_MINUTE|HOUR_SECOND|MINUTE_SECOND');
83+
comboEveryInterval.AutoSizeItemWidth;
8384
grpState.Items := Explode('|', 'Enable|Disable|Disable on slave');
8485
FMainSynMemo := SynMemoBody;
8586
btnSave.Hint := ShortCutToText(MainForm.actSaveSQL.ShortCut);
@@ -365,9 +366,13 @@ procedure TfrmEventEditor.chkStartsEndsClick(Sender: TObject);
365366

366367

367368
procedure TfrmEventEditor.comboDefinerDropDown(Sender: TObject);
369+
var
370+
combo: TCustomComboBox;
368371
begin
369372
// Populate definers from mysql.user
370-
(Sender as TComboBox).Items.Assign(DBObject.Connection.AllUserHostCombinations);
373+
combo := Sender as TComboBox;
374+
combo.Items.Assign(DBObject.Connection.AllUserHostCombinations);
375+
combo.AutoSizeItemWidth;
371376
end;
372377

373378

source/extfiledialog.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ procedure TfrmExtFileDialog.AddFileType(FileMask, DisplayName: String);
118118
FFilterNames.Add(DisplayName);
119119
FFilterMasks.Add(FileMask);
120120
comboFileType.Items.Add(DisplayName + ' (' + FileMask + ')');
121+
comboFileType.AutoSizeItemWidth;
121122
end;
122123

123124
procedure TfrmExtFileDialog.FormCreate(Sender: TObject);
@@ -169,6 +170,7 @@ procedure TfrmExtFileDialog.FormShow(Sender: TObject);
169170
SetFilterIndex(FFilterIndex);
170171

171172
comboEncoding.Items.AddStrings(FEncodings, True);
173+
comboEncoding.AutoSizeItemWidth;
172174
if (FEncodingIndex >=0) and (FEncodingIndex < comboEncoding.Items.Count) then
173175
comboEncoding.ItemIndex := FEncodingIndex;
174176

source/grideditlinks.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ function TEnumEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode;
989989
FCombo.Items.AddStrings(DisplayList)
990990
else
991991
FCombo.Items.AddStrings(ValueList);
992+
FCombo.AutoSizeItemWidth;
992993
FCombo.ItemIndex := ValueList.IndexOf(FCellText);
993994
if AllowCustomText and FAllowEdit then begin
994995
FCombo.Style := csDropDown;

source/insertfiles.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ procedure TfrmInsertFiles.comboDBsChange(Sender: TObject);
335335
if DBObjects[i].NodeType in [lntTable, lntView] then
336336
comboTables.Items.Add(DBObjects[i].Name);
337337
end;
338+
comboTables.AutoSizeItemWidth;
338339
if comboTables.Items.Count > 0 then
339340
comboTables.ItemIndex := 0;
340341
comboTables.OnChange(Sender);

source/routine_editor.pas

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ procedure TfrmRoutineEditor.Init(Obj: TDBObject);
137137
comboReturns.Clear;
138138
for i:=0 to High(Obj.Connection.Datatypes) do
139139
comboReturns.Items.Add(Obj.Connection.Datatypes[i].Name);
140+
comboReturns.AutoSizeItemWidth;
140141
chkDeterministic.Checked := False;
141142
SelectNode(listParameters, nil);
142143
listParameters.Clear;
@@ -225,9 +226,13 @@ procedure TfrmRoutineEditor.comboTypeSelect(Sender: TObject);
225226

226227

227228
procedure TfrmRoutineEditor.comboDefinerDropDown(Sender: TObject);
229+
var
230+
combo: TCustomComboBox;
228231
begin
229232
// Populate definers from mysql.user
230-
(Sender as TComboBox).Items.Assign(DBObject.Connection.AllUserHostCombinations);
233+
combo := Sender as TComboBox;
234+
combo.Items.Assign(DBObject.Connection.AllUserHostCombinations);
235+
combo.AutoSizeItemWidth;
231236
end;
232237

233238

source/table_editor.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,12 @@ procedure TfrmTableEditor.Init(Obj: TDBObject);
345345
//comboCollation.Left := lblCollation.Left + TExtForm.ScaleFromDesign(150, Self);
346346
//comboCollation.Width := comboRowFormat.Width;
347347
comboCollation.Items := DBObject.Connection.CollationList;
348+
comboCollation.AutoSizeItemWidth;
348349
//chkCharsetConvert.Left := comboCollation.Left + comboCollation.Width + 10;
349350
//comboEngine.Left := comboCollation.Left;
350351
//comboEngine.Width := comboCollation.Width;
351352
comboEngine.Items := DBObject.Connection.TableEngines;
353+
comboEngine.AutoSizeItemWidth;
352354
comboEngine.Items.Insert(0, '<'+_('Server default')+'>');
353355
comboEngine.ItemIndex := 0;
354356
//memoUnionTables.Left := comboCollation.Left;

0 commit comments

Comments
 (0)