ComboInput.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Dialogs.hpp>
  5. //---------------------------------------------------------------------
  6. #include <Common.h>
  7. #include <TextsWin.h>
  8. #include <WinInterface.h>
  9. #include <VCLCommon.h>
  10. #include <ScpMain.h>
  11. #include "ComboInput.h"
  12. //---------------------------------------------------------------------
  13. #pragma resource "*.dfm"
  14. //---------------------------------------------------------------------
  15. bool __fastcall DoComboInputDialog(
  16. const AnsiString Caption, const AnsiString Prompt,
  17. AnsiString & Text, TStrings * Items, TCloseQueryEvent OnCloseQuery,
  18. bool AllowEmpty)
  19. {
  20. bool Result;
  21. TComboInputDialog * ComboInputDialog = new TComboInputDialog(Application);
  22. try
  23. {
  24. ComboInputDialog->Caption = Caption;
  25. ComboInputDialog->Prompt = Prompt;
  26. ComboInputDialog->Text = Text;
  27. ComboInputDialog->Items = Items;
  28. ComboInputDialog->AllowEmpty = AllowEmpty;
  29. ComboInputDialog->OnCloseQuery = OnCloseQuery;
  30. Result = (ComboInputDialog->ShowModal() == mrOk);
  31. if (Result)
  32. {
  33. Text = ComboInputDialog->Text;
  34. }
  35. }
  36. __finally
  37. {
  38. delete ComboInputDialog;
  39. }
  40. return Result;
  41. }
  42. //---------------------------------------------------------------------
  43. AnsiString __fastcall DoSaveSessionDialog(
  44. TStoredSessionList * ASessionList, const AnsiString DefaultName)
  45. {
  46. // SessionList is no longer passed to TComboInputDialog, it uses global variable
  47. assert(ASessionList == StoredSessions);
  48. AnsiString Result;
  49. TComboInputDialog * SaveSessionDialog = NULL;
  50. TStrings * Items = NULL;
  51. try
  52. {
  53. Items = new TStringList();
  54. for (int Index = 0; Index < ASessionList->Count; Index++)
  55. {
  56. TSessionData * Data = ASessionList->Sessions[Index];
  57. if (!Data->Special)
  58. {
  59. Items->Add(Data->Name);
  60. }
  61. }
  62. SaveSessionDialog = new TComboInputDialog(Application);
  63. SaveSessionDialog->Items = Items;
  64. SaveSessionDialog->Text = DefaultName;
  65. SaveSessionDialog->Caption = LoadStr(SAVE_SESSION_CAPTION);
  66. SaveSessionDialog->Prompt = LoadStr(SAVE_SESSION_PROMPT);
  67. SaveSessionDialog->OnCloseQuery = SaveSessionDialog->StoredSessionsCloseQuery;
  68. if (SaveSessionDialog->ShowModal() == mrOk)
  69. {
  70. Result = SaveSessionDialog->Text;
  71. }
  72. }
  73. __finally
  74. {
  75. delete SaveSessionDialog;
  76. delete Items;
  77. }
  78. return Result;
  79. }
  80. //---------------------------------------------------------------------
  81. __fastcall TComboInputDialog::TComboInputDialog(TComponent* AOwner)
  82. : TForm(AOwner)
  83. {
  84. FAllowEmpty = false;
  85. UseSystemSettings(this);
  86. }
  87. //---------------------------------------------------------------------
  88. void __fastcall TComboInputDialog::SetItems(TStrings * value)
  89. {
  90. InputCombo->Items = value;
  91. UpdateControls();
  92. }
  93. //---------------------------------------------------------------------
  94. TStrings * __fastcall TComboInputDialog::GetItems()
  95. {
  96. return InputCombo->Items;
  97. }
  98. //---------------------------------------------------------------------
  99. void __fastcall TComboInputDialog::SetText(AnsiString value)
  100. {
  101. InputCombo->Text = value;
  102. UpdateControls();
  103. }
  104. //---------------------------------------------------------------------
  105. AnsiString __fastcall TComboInputDialog::GetText()
  106. {
  107. return InputCombo->Text;
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TComboInputDialog::SetPrompt(AnsiString value)
  111. {
  112. InputLabel->Caption = value;
  113. }
  114. //---------------------------------------------------------------------------
  115. AnsiString __fastcall TComboInputDialog::GetPrompt()
  116. {
  117. return InputLabel->Caption;
  118. }
  119. //---------------------------------------------------------------------------
  120. void __fastcall TComboInputDialog::InputComboChange(TObject * /*Sender*/)
  121. {
  122. UpdateControls();
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TComboInputDialog::UpdateControls()
  126. {
  127. EnableControl(OKButton, !Text.IsEmpty() || FAllowEmpty);
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TComboInputDialog::StoredSessionsCloseQuery(TObject * /*Sender*/,
  131. bool & CanClose)
  132. {
  133. CanClose = true;
  134. if (ModalResult == mrOk)
  135. {
  136. assert(StoredSessions);
  137. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  138. if (Data && Data->Special)
  139. {
  140. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  141. qtError, qaOK, 0);
  142. CanClose = false;
  143. }
  144. else if (Data &&
  145. MessageDialog(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text)),
  146. qtConfirmation, qaYes | qaNo, 0) != qaYes)
  147. {
  148. CanClose = false;
  149. }
  150. }
  151. }
  152. //---------------------------------------------------------------------------