LogMemo.cpp 10 KB

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