From 084615f88bbb4024346c0b9d33053b31e1fe32a3 Mon Sep 17 00:00:00 2001 From: Mike Brashler Date: Wed, 23 Nov 2022 02:18:47 -0800 Subject: [PATCH] sort contest names in dropdown list --- Ini.pas | 33 ++++++++++++++++++++++++++------- Main.dfm | 7 ------- Main.pas | 23 ++++++++++++++++++++--- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Ini.pas b/Ini.pas index 694d9b5..7550c1c 100644 --- a/Ini.pas +++ b/Ini.pas @@ -23,7 +23,6 @@ interface DEFAULTWEBSERVER = 'http://www.dxatlas.com/MorseRunner/MrScore.asp'; type // Adding a contest: Append new TSimContest enum value for each contest. - // Adding a contest: update menu in Main.dfm. TSimContest = (scWpx, scCwt, scFieldDay, scNaQp, scHst, scCQWW); TRunMode = (rmStop, rmPileup, rmSingle, rmWpx, rmHst); @@ -36,14 +35,14 @@ interface // Contest definition. TContestDefinition = record - Name: PChar; // Contest Name. + Name: PChar; // Contest Name. Used in SimContestCombo dropdown box. Key: PChar; // Identifying key (used in Ini files) ExchType1: TExchange1Type; ExchType2: TExchange2Type; ExchFieldEditable: Boolean; // whether the Exchange field is editable ExchDefault: PChar; // contest-specific Exchange default message Msg: PChar; // Exchange error message - T: TSimContest; // used to verify array ordering + T: TSimContest; // used to verify array ordering and lookup by Name end; PContestDefinition = ^TContestDefinition; @@ -60,10 +59,11 @@ TContestDefinition = record Note: The order of this table must match the declared order of TSimContest above. - Adding a contest: update ContestDefinitions[] array. (append at end until sorting is added) + Adding a contest: update ContestDefinitions[] array (append at end + because .INI uses TSimContest value). } ContestDefinitions: array[TSimContest] of TContestDefinition = ( - (Name: 'CQ Wpx'; + (Name: 'CQ WPX'; Key: 'CqWpx'; ExchType1: etRST; ExchType2: etSerialNr; @@ -73,7 +73,7 @@ TContestDefinition = record T:scWpx), // 'expecting RST (e.g. 5NN)' - (Name: 'CWOPS Cwt'; + (Name: 'CWOPS CWT'; Key: 'Cwt'; ExchType1: etOpName; ExchType2: etCwopsNumber; @@ -170,6 +170,7 @@ TContestDefinition = record procedure FromIni; procedure ToIni; function IsNum(Num: String): Boolean; +function FindContestByName(const AContestName : String) : TSimContest; implementation @@ -187,7 +188,8 @@ procedure FromIni; V:= ReadInteger(SEC_TST, 'SimContest', Ord(scWpx)); SimContest := TSimContest(V); ActiveContest := @ContestDefinitions[SimContest]; - MainForm.SimContestCombo.ItemIndex := V; + MainForm.SimContestCombo.ItemIndex := + MainForm.SimContestCombo.Items.IndexOf(ActiveContest.Name); // Adding a contest: read contest-specfic Exchange Strings from .INI file. // load contest-specific Exchange Strings @@ -331,6 +333,23 @@ function IsNum(Num: String): Boolean; end; +function FindContestByName(const AContestName : String) : TSimContest; +var + C : TContestDefinition; +begin + for C in ContestDefinitions do + if CompareText(AContestName, C.Name) = 0 then + begin + Result := C.T; + // DebugLn('Ini.FindContestByName(%s) --> %s', [AContestName, DbgS(Result)]); + Exit; + end; + + raise Exception.Create( + Format('error: ''%s'' is an unsupported contest name', [AContestName])); + Halt; +end; + end. diff --git a/Main.dfm b/Main.dfm index 63b6b8c..2a23e23 100644 --- a/Main.dfm +++ b/Main.dfm @@ -950,13 +950,6 @@ object MainForm: TMainForm TabOrder = 0 TabStop = False OnChange = SimContestComboChange - Items.Strings = ( - 'CQ WPX' - 'CWOPS CWT' - 'ARRL Field Day' - 'NCJ NAQP' - 'HST (High Speed Test)' - 'CQ WW') end object ExchangeEdit: TEdit Left = 76 diff --git a/Main.pas b/Main.pas index 342abc6..d2fe0c7 100644 --- a/Main.pas +++ b/Main.pas @@ -348,6 +348,7 @@ TMainForm = class(TForm) Selected: Boolean); procedure mnuShowCallsignInfoClick(Sender: TObject); procedure SimContestComboChange(Sender: TObject); + procedure SimContestComboPopulate; procedure ExchangeEditExit(Sender: TObject); procedure Edit4Exit(Sender: TObject); @@ -452,6 +453,9 @@ procedure TMainForm.FormCreate(Sender: TObject); ListView2.Visible:= False; ListView2.Clear; + // populate and sort SimContestCombo + SimContestComboPopulate; + // load DXCC support gDXCCList := TDXCC.Create; @@ -890,7 +894,8 @@ procedure TMainForm.SetContest(AContestNum: TSimContest); scCQWW]) then begin ShowMessage('The selected contest is not yet supported.'); - SimContestCombo.ItemIndex:= Ord(Ini.SimContest); + SimContestCombo.ItemIndex := + SimContestCombo.Items.IndexOf(ActiveContest.Name); Exit; end; @@ -903,7 +908,8 @@ procedure TMainForm.SetContest(AContestNum: TSimContest); Ini.SimContest := AContestNum; Ini.ActiveContest := @ContestDefinitions[AContestNum]; - SimContestCombo.ItemIndex := Ord(AContestNum); + SimContestCombo.ItemIndex := + SimContestCombo.Items.IndexOf(Ini.ActiveContest.Name); WipeBoxes; // clear any status messages @@ -1260,7 +1266,18 @@ procedure TMainForm.SetBw(BwNo: integer); procedure TMainForm.SimContestComboChange(Sender: TObject); begin - SetContest(TSimContest(SimContestCombo.ItemIndex)); + SetContest(FindContestByName(SimContestCombo.Items[SimContestCombo.ItemIndex])); +end; + +{ add contest names to SimContest Combo box and sort } +procedure TMainForm.SimContestComboPopulate; +var + C: TContestDefinition; +begin + SimContestCombo.Items.Clear; + for C in ContestDefinitions do + SimContestCombo.Items.Add(C.Name); + SimContestCombo.Sorted:= True; end; procedure TMainForm.ComboBox2Change(Sender: TObject);