LogMemo.cpp 11 KB

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