Log.cpp 5.9 KB

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