Log.cpp 6.0 KB

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