Log.cpp 6.3 KB

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