ComboInput.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Dialogs.hpp>
  5. //---------------------------------------------------------------------
  6. #include <Common.h>
  7. #include <TextsWin.h>
  8. #include <HelpWin.h>
  9. #include <WinInterface.h>
  10. #include <VCLCommon.h>
  11. #include <CoreMain.h>
  12. #include "ComboInput.h"
  13. //---------------------------------------------------------------------
  14. #pragma resource "*.dfm"
  15. //---------------------------------------------------------------------
  16. bool __fastcall DoComboInputDialog(
  17. const AnsiString Caption, const AnsiString Prompt,
  18. AnsiString & Text, TStrings * Items, TInputValidateEvent OnInputValidate,
  19. bool AllowEmpty, const AnsiString HelpKeyword)
  20. {
  21. bool Result;
  22. TComboInputDialog * ComboInputDialog = new TComboInputDialog(Application);
  23. try
  24. {
  25. ComboInputDialog->Caption = Caption;
  26. ComboInputDialog->Prompt = Prompt;
  27. ComboInputDialog->Text = Text;
  28. ComboInputDialog->Items = Items;
  29. ComboInputDialog->AllowEmpty = AllowEmpty;
  30. ComboInputDialog->OnInputValidate = OnInputValidate;
  31. ComboInputDialog->HelpKeyword = HelpKeyword;
  32. Result = (ComboInputDialog->ShowModal() == mrOk);
  33. if (Result)
  34. {
  35. Text = ComboInputDialog->Text;
  36. }
  37. }
  38. __finally
  39. {
  40. delete ComboInputDialog;
  41. }
  42. return Result;
  43. }
  44. //---------------------------------------------------------------------
  45. void __fastcall SaveSessionInputValidate(TObject * Sender, const AnsiString & Text)
  46. {
  47. TSessionData * Data =
  48. reinterpret_cast<TSessionData *>(dynamic_cast<TForm *>(Sender)->Tag);
  49. SessionNameValidate(Text, Data);
  50. }
  51. //---------------------------------------------------------------------
  52. AnsiString __fastcall DoSaveSessionDialog(const AnsiString DefaultName,
  53. TSessionData * OriginalSession)
  54. {
  55. AnsiString Result;
  56. TComboInputDialog * SaveSessionDialog = NULL;
  57. TStrings * Items = NULL;
  58. try
  59. {
  60. Items = new TStringList();
  61. for (int Index = 0; Index < StoredSessions->Count; Index++)
  62. {
  63. TSessionData * Data = StoredSessions->Sessions[Index];
  64. if (!Data->Special)
  65. {
  66. Items->Add(Data->Name);
  67. }
  68. }
  69. SaveSessionDialog = new TComboInputDialog(Application);
  70. SaveSessionDialog->Items = Items;
  71. SaveSessionDialog->Text = DefaultName;
  72. SaveSessionDialog->Caption = LoadStr(SAVE_SESSION_CAPTION);
  73. SaveSessionDialog->Prompt = LoadStr(SAVE_SESSION_PROMPT);
  74. SaveSessionDialog->OnInputValidate = SaveSessionInputValidate;
  75. SaveSessionDialog->Tag = reinterpret_cast<int>(OriginalSession);
  76. SaveSessionDialog->HelpKeyword = HELP_SESSION_SAVE;
  77. if (SaveSessionDialog->ShowModal() == mrOk)
  78. {
  79. Result = SaveSessionDialog->Text;
  80. }
  81. }
  82. __finally
  83. {
  84. delete SaveSessionDialog;
  85. delete Items;
  86. }
  87. return Result;
  88. }
  89. //---------------------------------------------------------------------------
  90. void __fastcall SessionNameValidate(const AnsiString & Text,
  91. TSessionData * RenamingSession)
  92. {
  93. TSessionData::ValidateName(Text);
  94. assert(StoredSessions);
  95. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  96. if (Data && Data->Special)
  97. {
  98. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  99. qtError, qaOK, HELP_NONE);
  100. Abort();
  101. }
  102. else if (Data && (Data != RenamingSession) &&
  103. MessageDialog(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text)),
  104. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  105. {
  106. Abort();
  107. }
  108. }
  109. //---------------------------------------------------------------------
  110. __fastcall TComboInputDialog::TComboInputDialog(TComponent* AOwner)
  111. : TForm(AOwner)
  112. {
  113. FAllowEmpty = false;
  114. FOnInputValidate = NULL;
  115. UseSystemSettings(this);
  116. }
  117. //---------------------------------------------------------------------
  118. void __fastcall TComboInputDialog::SetItems(TStrings * value)
  119. {
  120. InputCombo->Items = value;
  121. UpdateControls();
  122. }
  123. //---------------------------------------------------------------------
  124. TStrings * __fastcall TComboInputDialog::GetItems()
  125. {
  126. return InputCombo->Items;
  127. }
  128. //---------------------------------------------------------------------
  129. void __fastcall TComboInputDialog::SetText(AnsiString value)
  130. {
  131. InputCombo->Text = value;
  132. UpdateControls();
  133. }
  134. //---------------------------------------------------------------------
  135. AnsiString __fastcall TComboInputDialog::GetText()
  136. {
  137. return InputCombo->Text;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TComboInputDialog::SetPrompt(AnsiString value)
  141. {
  142. InputLabel->Caption = value;
  143. }
  144. //---------------------------------------------------------------------------
  145. AnsiString __fastcall TComboInputDialog::GetPrompt()
  146. {
  147. return InputLabel->Caption;
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TComboInputDialog::InputComboChange(TObject * /*Sender*/)
  151. {
  152. UpdateControls();
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TComboInputDialog::UpdateControls()
  156. {
  157. EnableControl(OKButton, !Text.IsEmpty() || FAllowEmpty);
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TComboInputDialog::FormShow(TObject * /*Sender*/)
  161. {
  162. TBorderIcons BI = BorderIcons;
  163. if (HelpKeyword.IsEmpty())
  164. {
  165. BI >> biHelp;
  166. OKButton->Left = CancelButton->Left;
  167. CancelButton->Left = HelpButton->Left;
  168. HelpButton->Visible = false;
  169. }
  170. else
  171. {
  172. BI << biHelp;
  173. }
  174. BorderIcons = BI;
  175. }
  176. //---------------------------------------------------------------------------
  177. void __fastcall TComboInputDialog::HelpButtonClick(TObject * /*Sender*/)
  178. {
  179. FormHelp(this);
  180. }
  181. //---------------------------------------------------------------------------
  182. void __fastcall TComboInputDialog::FormCloseQuery(TObject * /*Sender*/,
  183. bool & /*CanClose*/)
  184. {
  185. if ((ModalResult == mrOk) && (OnInputValidate != NULL))
  186. {
  187. OnInputValidate(this, Text);
  188. }
  189. }
  190. //---------------------------------------------------------------------------