Log.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <FileCtrl.hpp>
  5. #include <VCLCommon.h>
  6. #include <Common.h>
  7. #include <ScpMain.h>
  8. #include <TextsWin.h>
  9. #include "Log.h"
  10. #include "NonVisual.h"
  11. #include "WinConfiguration.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma link "LogMemo"
  15. #pragma resource "*.dfm"
  16. TLogForm *LogForm = NULL;
  17. //---------------------------------------------------------------------------
  18. TLogForm * __fastcall CreateLogForm(TLogMemo *ALogMemo)
  19. {
  20. assert(!LogForm);
  21. TLogForm * aLogForm = new TLogForm(Application);
  22. try
  23. {
  24. aLogForm->LogMemo = ALogMemo;
  25. aLogForm->Show();
  26. }
  27. catch (...)
  28. {
  29. delete aLogForm;
  30. throw;
  31. }
  32. LogForm = aLogForm;
  33. return aLogForm;
  34. }
  35. //---------------------------------------------------------------------------
  36. TLogForm * __fastcall RequireLogForm(TLogMemo *ALogMemo)
  37. {
  38. if (!LogForm)
  39. {
  40. CreateLogForm(ALogMemo);
  41. }
  42. return LogForm;
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall FreeLogForm()
  46. {
  47. if (LogForm)
  48. {
  49. TLogForm * PLogForm = LogForm;
  50. LogForm = NULL;
  51. // would also free form see TLogForm::FormClose()
  52. // we can't free form directly (cause exception when form is closed by
  53. // button on toolbar, beacuse it destroys the button too)
  54. PLogForm->Close();
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. __fastcall TLogForm::TLogForm(TComponent* Owner)
  59. : FFormRestored(False), TForm(Owner)
  60. {
  61. FStatusBarFile = false;
  62. FLogMemo = NULL;
  63. FSessionLog = NULL;
  64. ShowWindow(Handle, SW_SHOWNA);
  65. UseSystemSettings(this);
  66. }
  67. //---------------------------------------------------------------------------
  68. __fastcall TLogForm::~TLogForm()
  69. {
  70. LogForm = NULL;
  71. LogMemo = NULL;
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TLogForm::SetLogMemo(TLogMemo * value)
  75. {
  76. if (LogMemo != value)
  77. {
  78. TLogMemo * OldLogMemo = LogMemo;
  79. FLogMemo = value;
  80. if (OldLogMemo)
  81. {
  82. assert((OldLogMemo->Parent == this) && (OldLogMemo->OnChange == LogMemoChange));
  83. OldLogMemo->OnChange = NULL;
  84. if (SessionLog == OldLogMemo->SessionLog) SessionLog = NULL;
  85. OldLogMemo->Parent = NULL;
  86. }
  87. if (LogMemo)
  88. {
  89. LogMemo->Align = alClient;
  90. if (!SessionLog) SessionLog = LogMemo->SessionLog;
  91. LogMemo->Parent = this;
  92. // setting Parent usually calls OnChange many times (pending changes are
  93. // inserted to TLogMemo), so we set OnChange handler after Parent.
  94. LogMemo->OnChange = LogMemoChange;
  95. LogMemoChange(LogMemo);
  96. }
  97. }
  98. }
  99. //---------------------------------------------------------------------------
  100. void __fastcall TLogForm::LogMemoChange(TObject * /*Sender*/)
  101. {
  102. assert(LogMemo);
  103. Application->ProcessMessages();
  104. if (!ComponentState.Contains(csDestroying))
  105. {
  106. UpdateActions();
  107. /*if (!SessionLog->Count)
  108. {
  109. LogMemo->Invalidate();
  110. } */
  111. }
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TLogForm::FormClose(TObject * /*Sender*/, TCloseAction & Action)
  115. {
  116. assert(Configuration);
  117. // If log window feature is turned off (log window is being closed
  118. // by turning off this feature e.g. in Preferences Window), really close it
  119. // If log window feature is turned on (log window is being closed by
  120. // close command if this window) we only disable log window feature
  121. // (this function will be than called again, see case 1)
  122. LogMemo = NULL;
  123. if (!Configuration->Logging || (WinConfiguration->LogView != lvWindow) ||
  124. Application->Terminated)
  125. {
  126. Action = caFree;
  127. }
  128. else
  129. {
  130. WinConfiguration->LogView = lvNone;
  131. Action = caFree;
  132. }
  133. WinConfiguration->LogWindowParams = WinConfiguration->StoreForm(this);
  134. }
  135. //---------------------------------------------------------------------------
  136. void __fastcall TLogForm::SetSessionLog(TSessionLog * value)
  137. {
  138. if (FSessionLog != value)
  139. {
  140. FSessionLog = value;
  141. UpdateControls();
  142. }
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TLogForm::UpdateControls()
  146. {
  147. if (SessionLog)
  148. {
  149. FStatusBarFile = SessionLog->LoggingToFile;
  150. SetStatusBarText(SessionLog->LoggingToFile ? SessionLog->LogFileName :
  151. LoadStr(LOG_NOLOGFILE));
  152. assert(SessionLog->Data);
  153. Caption = FMTLOAD(LOG_CAPTION, (SessionLog->Data->SessionName));
  154. }
  155. else
  156. {
  157. FStatusBarFile = False;
  158. SetStatusBarText(LoadStr(LOG_NOLOG));
  159. Caption = LoadStr(LOG_NOLOGCAPTION);
  160. }
  161. }
  162. //---------------------------------------------------------------------------
  163. void __fastcall TLogForm::SetStatusBarText(AnsiString Text)
  164. {
  165. StatusBar->Hint = Text;
  166. if (StatusBar->Canvas->TextWidth(Text) > StatusBar->ClientWidth - 20)
  167. {
  168. StatusBar->ShowHint = true;
  169. if (!FStatusBarFile)
  170. {
  171. Text += "...";
  172. while ((Text.Length() > 3) &&
  173. (StatusBar->Canvas->TextWidth(Text) > StatusBar->ClientWidth - 20))
  174. {
  175. Text.Delete(Text.Length() - 3, 1);
  176. }
  177. }
  178. else
  179. {
  180. Text = MinimizeName(Text, StatusBar->Canvas, StatusBar->ClientWidth - 20);
  181. }
  182. }
  183. else
  184. {
  185. StatusBar->ShowHint = false;
  186. }
  187. StatusBar->SimpleText = Text;
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall TLogForm::StatusBarResize(TObject * /*Sender*/)
  191. {
  192. SetStatusBarText(StatusBar->Hint);
  193. }
  194. //---------------------------------------------------------------------------
  195. void __fastcall TLogForm::CreateParams(TCreateParams & Params)
  196. {
  197. if (!FFormRestored)
  198. {
  199. FFormRestored = True;
  200. assert(Configuration);
  201. WinConfiguration->RestoreForm(WinConfiguration->LogWindowParams, this);
  202. }
  203. TForm::CreateParams(Params);
  204. Params.WndParent = GetDesktopWindow();
  205. }