LogMemo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "LogMemo.h"
  6. #include <StrUtils.hpp>
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. #ifndef DESIGN_ONLY
  10. const TColor LogLineColors[] =
  11. {clGreen, clRed, clMaroon, clBlue, clGray};
  12. #endif
  13. //---------------------------------------------------------------------------
  14. // ValidCtrCheck is used to assure that the components created do not have
  15. // any pure virtual functions.
  16. static inline void ValidCtrCheck(TLogMemo *)
  17. {
  18. new TLogMemo(NULL);
  19. }
  20. //---------------------------------------------------------------------------
  21. namespace Logmemo
  22. {
  23. void __fastcall PACKAGE Register()
  24. {
  25. TComponentClass classes[1] = {__classid(TLogMemo)};
  26. RegisterComponents(L"Scp", classes, 0);
  27. }
  28. }
  29. //---------------------------------------------------------------------------
  30. __fastcall TLogMemo::TLogMemo(TComponent* Owner)
  31. : TCustomRichEdit(Owner)
  32. {
  33. FIndexes = new TList();
  34. FWantScrollToEnd = false;
  35. FUpdating = false;
  36. FReloading = false;
  37. FNeedsRepaint = false;
  38. FLastUpdate = 0;
  39. FShowTypes = DEFAULT_LOGMEMO_SHOWTYPES;
  40. ReadOnly = true;
  41. Font->Name = DEFAULT_LOGMEMO_FONT;
  42. WantReturns = false;
  43. WordWrap = false;
  44. ScrollBars = ssBoth;
  45. FThread = GetCurrentThreadId();
  46. }
  47. //---------------------------------------------------------------------------
  48. __fastcall TLogMemo::~TLogMemo()
  49. {
  50. #ifndef DESIGN_ONLY
  51. // deassociate us from session log change handler
  52. SessionLog = NULL;
  53. #endif
  54. delete FIndexes;
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TLogMemo::WMSetFocus(TWMSetFocus & Message)
  58. {
  59. try
  60. {
  61. TCustomRichEdit::Dispatch(&Message);
  62. }
  63. __finally
  64. {
  65. HideCaret(Handle);
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. bool __fastcall TLogMemo::IsFontStored()
  70. {
  71. return
  72. (Font->Name != DEFAULT_LOGMEMO_FONT) ||
  73. (Font->Charset != DEFAULT_CHARSET) ||
  74. (Font->Color != clWindowText) ||
  75. (Font->Height != -11) ||
  76. (Font->Pitch != TFontPitch::fpDefault) ||
  77. (Font->Size != 8) ||
  78. (Font->Style != TFontStyles());
  79. }
  80. //---------------------------------------------------------------------------
  81. #ifndef DESIGN_ONLY
  82. void __fastcall TLogMemo::SetSessionLog(TSessionLog * value)
  83. {
  84. if (FSessionLog != value)
  85. {
  86. if (SessionLog && (SessionLog->OnChange == SessionLogChange))
  87. {
  88. SessionLog->OnChange = NULL;
  89. }
  90. FSessionLog = value;
  91. if (SessionLog)
  92. {
  93. SessionLog->OnChange = SessionLogChange;
  94. }
  95. ReloadFromLog();
  96. }
  97. }
  98. #endif
  99. //---------------------------------------------------------------------------
  100. void __fastcall TLogMemo::SessionLogChange(TObject * Sender)
  101. {
  102. DebugUsedParam(Sender);
  103. #ifndef DESIGN_ONLY
  104. DebugAssert(Sender && (Sender == (TObject*)SessionLog));
  105. #endif
  106. if (HandleAllocated())
  107. {
  108. unsigned int Ticks = GetTickCount();
  109. if (((FLastUpdate == 0) || (Ticks < FLastUpdate) || (Ticks - FLastUpdate > 200)) &&
  110. (FThread == GetCurrentThreadId()))
  111. {
  112. // forced update
  113. if (!FReloading)
  114. {
  115. UpdateFromLog();
  116. }
  117. }
  118. else
  119. {
  120. // update later, once idle
  121. PostMessage(Handle, WM_LOG_UPDATE, 0, 0);
  122. }
  123. }
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TLogMemo::UpdateFromLog()
  127. {
  128. #ifndef DESIGN_ONLY
  129. if (SessionLog && Parent && !Application->Terminated)
  130. {
  131. DebugAssert(FIndexes->Count == Lines->Count);
  132. FUpdating = true;
  133. bool Updated = false;
  134. SessionLog->Lock();
  135. try
  136. {
  137. ScrollToEnd();
  138. if (Lines->Count && (Indexes[0] < SessionLog->TopIndex))
  139. {
  140. try
  141. {
  142. SendMessage(Handle, WM_SETREDRAW, false, 0);
  143. FNeedsRepaint = true;
  144. while (Lines->Count && (Indexes[0] < SessionLog->TopIndex))
  145. {
  146. FIndexes->Delete(0);
  147. if (Parent)
  148. {
  149. Lines->Delete(0);
  150. Updated = true;
  151. }
  152. }
  153. }
  154. __finally
  155. {
  156. SendMessage(Handle, WM_SETREDRAW, true, 0);
  157. }
  158. }
  159. if (SessionLog->Count)
  160. {
  161. int LastIndex;
  162. if (Lines->Count)
  163. {
  164. LastIndex = Indexes[Lines->Count-1] + 1;
  165. }
  166. else
  167. {
  168. LastIndex = SessionLog->TopIndex;
  169. }
  170. while (Parent && LastIndex <= SessionLog->BottomIndex)
  171. {
  172. if (Parent && ShowTypes.Contains(SessionLog->Type[LastIndex]))
  173. {
  174. SelLength = 0;
  175. SelStart = Lines->Text.Length();
  176. if (Parent) SelAttributes->Color = LogLineColors[SessionLog->Type[LastIndex]];
  177. FIndexes->Add((void*) LastIndex);
  178. try
  179. {
  180. // this usually fails when log window is closed while
  181. // new line is being added (control has no parent)
  182. if (Parent)
  183. {
  184. if (SessionLog->Line[LastIndex].Pos(L"\r"))
  185. {
  186. Lines->Add(ReplaceStr(SessionLog->Line[LastIndex], L"\r", L""));
  187. }
  188. else
  189. {
  190. Lines->Add(SessionLog->Line[LastIndex]);
  191. }
  192. Updated = true;
  193. }
  194. }
  195. catch(...)
  196. {
  197. if (Lines->Count < FIndexes->Count)
  198. {
  199. FIndexes->Delete(FIndexes->Count - 1);
  200. }
  201. DebugAssert(FIndexes->Count == Lines->Count);
  202. // LastIndex is strangely reset to 0 when exception is caught
  203. LastIndex = Indexes[Lines->Count-1];
  204. }
  205. }
  206. LastIndex++;
  207. }
  208. }
  209. if (Parent)
  210. {
  211. ScrollToEnd();
  212. if (FNeedsRepaint)
  213. {
  214. FNeedsRepaint = false;
  215. Invalidate();
  216. }
  217. }
  218. DebugAssert(!Parent || FIndexes->Count == Lines->Count);
  219. FLastUpdate = GetTickCount();
  220. }
  221. __finally
  222. {
  223. SessionLog->Unlock();
  224. FUpdating = false;
  225. }
  226. if (Updated)
  227. {
  228. Change();
  229. }
  230. }
  231. #endif
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TLogMemo::ReloadFromLog()
  235. {
  236. if (Parent && !FReloading)
  237. {
  238. TAutoFlag ReloadingFlag(FReloading);
  239. Lines->BeginUpdate();
  240. try
  241. {
  242. Clear();
  243. FIndexes->Clear();
  244. UpdateFromLog();
  245. }
  246. __finally
  247. {
  248. Lines->EndUpdate();
  249. }
  250. }
  251. }
  252. //---------------------------------------------------------------------------
  253. int __fastcall TLogMemo::GetIndexes(int Index)
  254. {
  255. DebugAssert((Index >= 0) && (Index < Lines->Count) && (FIndexes->Count == Lines->Count));
  256. return ((int)FIndexes->Items[Index]);
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TLogMemo::SetShowTypes(TLogLineTypes value)
  260. {
  261. if (ShowTypes != value)
  262. {
  263. FShowTypes = value;
  264. ReloadFromLog();
  265. }
  266. }
  267. //---------------------------------------------------------------------------
  268. bool __fastcall TLogMemo::StoreShowTypes()
  269. {
  270. return (ShowTypes != DEFAULT_LOGMEMO_SHOWTYPES);
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TLogMemo::ScrollToEnd()
  274. {
  275. TCharRange Selection;
  276. Selection.cpMin = Lines->Text.Length();
  277. Selection.cpMax = Selection.cpMin;
  278. Perform(EM_EXSETSEL, 0, ((long)&Selection));
  279. Perform(EM_SCROLLCARET, 0, 0);
  280. }
  281. //---------------------------------------------------------------------------
  282. void TLogMemo::WMLogUpdate(TMessage & /*Message*/)
  283. {
  284. if (!FReloading)
  285. {
  286. UpdateFromLog();
  287. }
  288. }
  289. //---------------------------------------------------------------------------
  290. void TLogMemo::CMVisibleChanged(TMessage & Message)
  291. {
  292. try
  293. {
  294. TCustomRichEdit::Dispatch(&Message);
  295. }
  296. __finally
  297. {
  298. ScrollToEnd();
  299. }
  300. }
  301. //---------------------------------------------------------------------------
  302. void __fastcall TLogMemo::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y)
  303. {
  304. try
  305. {
  306. TCustomRichEdit::MouseDown(Button, Shift, X, Y);
  307. }
  308. __finally
  309. {
  310. HideCaret(Handle);
  311. }
  312. }
  313. //---------------------------------------------------------------------------
  314. void __fastcall TLogMemo::CMShowingChanged(TMessage & Message)
  315. {
  316. bool VShowing = Showing;
  317. try
  318. {
  319. TCustomRichEdit::Dispatch(&Message);
  320. }
  321. __finally
  322. {
  323. if (VShowing)
  324. {
  325. FWantScrollToEnd = true;
  326. }
  327. HideCaret(Handle);
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TLogMemo::KeyDown(Word & Key, TShiftState Shift)
  332. {
  333. if ((Key == VK_UP) || (Key == VK_DOWN))
  334. {
  335. SendMessage(Handle, EM_LINESCROLL, 0, ( Key == VK_UP ? -1 : 1));
  336. Key = 0;
  337. }
  338. else
  339. {
  340. TCustomRichEdit::KeyDown(Key, Shift);
  341. }
  342. }
  343. //---------------------------------------------------------------------------
  344. void __fastcall TLogMemo::WMKeyDown(TWMKeyDown & Message)
  345. {
  346. try
  347. {
  348. TCustomRichEdit::Dispatch(&Message);
  349. }
  350. __finally
  351. {
  352. HideCaret(Handle);
  353. }
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TLogMemo::WMPaint(TWMPaint & Message)
  357. {
  358. try
  359. {
  360. TCustomRichEdit::Dispatch(&Message);
  361. }
  362. __finally
  363. {
  364. if (FWantScrollToEnd)
  365. {
  366. FWantScrollToEnd = false;
  367. SelLength = 0;
  368. SelStart = Lines->Text.Length();
  369. SendMessage(Handle, EM_LINESCROLL, 0, Lines->Count);
  370. }
  371. HideCaret(Handle);
  372. }
  373. }
  374. //---------------------------------------------------------------------------
  375. int __fastcall TLogMemo::GetLinesVisible()
  376. {
  377. HFONT OldFont;
  378. HDC DC;
  379. TTextMetricW TM;
  380. TRect Rect;
  381. DC = GetDC((HWND)Handle);
  382. OldFont = (HFONT)SelectObject(DC, (HWND)Font->Handle);
  383. try
  384. {
  385. GetTextMetrics(DC, &TM);
  386. Perform(EM_GETRECT, 0, ((int)&Rect));
  387. }
  388. __finally
  389. {
  390. SelectObject(DC, OldFont);
  391. ReleaseDC(Handle, DC);
  392. }
  393. return (Rect.Bottom - Rect.Top) / (TM.tmHeight + TM.tmExternalLeading);
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TLogMemo::SetParent(TWinControl * AParent)
  397. {
  398. TCustomRichEdit::SetParent(AParent);
  399. if (AParent) UpdateFromLog();
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TLogMemo::Change()
  403. {
  404. if (Parent && Visible && !Application->Terminated && !FUpdating)
  405. {
  406. TCustomRichEdit::Change();
  407. }
  408. }