ToolTipEx.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. #include "stdafx.h"
  2. #include "cp_main.h"
  3. #include "ToolTipEx.h"
  4. #include "BitmapHelper.h"
  5. #include "Options.h"
  6. #include <Richedit.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define DELETE_BITMAP if(m_imageViewer.m_pBitmap) \
  13. { \
  14. m_imageViewer.m_pBitmap->DeleteObject(); \
  15. delete m_imageViewer.m_pBitmap; \
  16. m_imageViewer.m_pBitmap = NULL; \
  17. }
  18. #define HIDE_WINDOW_TIMER 1
  19. #define SAVE_SIZE 2
  20. #define TIMER_BUTTON_UP 3
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CToolTipEx
  23. CToolTipEx::CToolTipEx(): m_dwTextStyle(DT_EXPANDTABS | DT_EXTERNALLEADING |
  24. DT_NOPREFIX | DT_WORDBREAK), m_rectMargin(2, 2, 3, 3),
  25. m_pNotifyWnd(NULL), m_clipId(0), m_clipRow(-1)
  26. {
  27. }
  28. CToolTipEx::~CToolTipEx()
  29. {
  30. DELETE_BITMAP
  31. m_Font.DeleteObject();
  32. m_clipDataFont.DeleteObject();
  33. }
  34. BEGIN_MESSAGE_MAP(CToolTipEx, CWnd)
  35. //{{AFX_MSG_MAP(CToolTipEx)
  36. ON_WM_PAINT()
  37. ON_WM_SIZE()
  38. ON_WM_NCHITTEST()
  39. ON_WM_ACTIVATE()
  40. ON_WM_TIMER()
  41. ON_WM_NCPAINT()
  42. ON_WM_NCCALCSIZE()
  43. ON_WM_NCLBUTTONDOWN()
  44. ON_WM_NCMOUSEMOVE()
  45. ON_WM_NCLBUTTONUP()
  46. ON_WM_ERASEBKGND()
  47. ON_COMMAND(ID_FIRST_REMEMBERWINDOWPOSITION, &CToolTipEx::OnRememberwindowposition)
  48. ON_COMMAND(ID_FIRST_SIZEWINDOWTOCONTENT, &CToolTipEx::OnSizewindowtocontent)
  49. ON_COMMAND(ID_FIRST_SCALEIMAGESTOFITWINDOW, &CToolTipEx::OnScaleimagestofitwindow)
  50. ON_COMMAND(2, OnOptions)
  51. ON_WM_RBUTTONDOWN()
  52. ON_WM_SETFOCUS()
  53. ON_COMMAND(ID_FIRST_HIDEDESCRIPTIONWINDOWONM, &CToolTipEx::OnFirstHidedescriptionwindowonm)
  54. ON_COMMAND(ID_FIRST_WRAPTEXT, &CToolTipEx::OnFirstWraptext)
  55. ON_WM_WINDOWPOSCHANGING()
  56. END_MESSAGE_MAP()
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CToolTipEx message handlers
  59. BOOL CToolTipEx::Create(CWnd *pParentWnd)
  60. {
  61. m_saveWindowLockout = true;
  62. // Get the class name and create the window
  63. CString szClassName = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  64. // Create the window - just don't show it yet.
  65. if( !CWnd::CreateEx(0, szClassName, _T(""), WS_POPUP,
  66. 0, 0, 0, 0, pParentWnd->GetSafeHwnd(), 0, NULL))
  67. {
  68. return FALSE;
  69. }
  70. //CString szClassName2 = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  71. //BOOL b = m_imageViewer.Create(_T(""), szClassName2, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, CRect(0, 0, 0, 0), this, 3);
  72. m_imageViewer.Create(this);
  73. m_DittoWindow.DoCreate(this);
  74. m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  75. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true, g_Opt.m_Theme.GetCaptionSize(), g_Opt.m_Theme.GetCaptionFontSize());
  76. m_DittoWindow.m_bDrawMaximize = false;
  77. m_DittoWindow.m_bDrawMinimize = false;
  78. m_DittoWindow.m_bDrawChevron = false;
  79. m_DittoWindow.m_sendWMClose = false;
  80. m_RichEdit.Create(_T(""), _T(""), WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  81. WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_NOHIDESEL |
  82. ES_AUTOHSCROLL, CRect(10, 10, 100, 200), this, 1);
  83. m_RichEdit.SetReadOnly();
  84. m_RichEdit.SetBackgroundColor(FALSE, g_Opt.m_Theme.DescriptionWindowBG());
  85. ApplyWordWrap();
  86. SetLogFont(GetSystemToolTipFont(), FALSE);
  87. m_optionsButton.Create(NULL, WS_CHILD | BS_OWNERDRAW | WS_TABSTOP, CRect(0, 0, 0, 0), this, 2);
  88. m_optionsButton.LoadStdImageDPI(IDB_COG_16_16, IDB_COG_20_20, IDB_COG_24_24, cog_28, IDB_COG_32_32, _T("PNG"));
  89. m_optionsButton.SetToolTipText(theApp.m_Language.GetString(_T("DescriptionOptionsTooltip"), _T("Description Options")));
  90. m_optionsButton.ShowWindow(SW_SHOW);
  91. m_clipDataStatic.Create(_T("some text"), WS_CHILD | WS_VISIBLE | SS_SIMPLE, CRect(0, 0, 0, 0), this, 3);
  92. m_clipDataFont.CreateFont(-theApp.m_metrics.PointsToPixels(8), 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 3, 2, 1, 34, _T("Segoe UI"));
  93. m_clipDataStatic.SetFont(&m_clipDataFont);
  94. m_clipDataStatic.SetBkColor(g_Opt.m_Theme.DescriptionWindowBG());
  95. m_clipDataStatic.SetTextColor(RGB(127, 127, 127));
  96. m_saveWindowLockout = false;
  97. return TRUE;
  98. }
  99. BOOL CToolTipEx::Show(CPoint point)
  100. {
  101. m_reducedWindowSize = false;
  102. if(m_imageViewer.m_pBitmap)
  103. {
  104. m_RichEdit.ShowWindow(SW_HIDE);
  105. m_imageViewer.ShowWindow(SW_SHOW);
  106. m_imageViewer.UpdateBitmapSize();
  107. }
  108. else
  109. {
  110. m_RichEdit.ShowWindow(SW_SHOW);
  111. m_imageViewer.ShowWindow(SW_HIDE);
  112. }
  113. CRect rect;
  114. if(CGetSetOptions::GetSizeDescWindowToContent() == FALSE)
  115. {
  116. rect.left = point.x;
  117. rect.top = point.y;
  118. CSize size;
  119. CGetSetOptions::GetDescWndSize(size);
  120. rect.right = rect.left + size.cx;
  121. rect.bottom = rect.top + size.cy;
  122. EnsureWindowVisible(&rect);
  123. }
  124. else
  125. {
  126. rect = GetBoundsRect();
  127. //account for the scroll bars
  128. rect.right += 20;
  129. rect.bottom += 20;
  130. if (m_imageViewer.m_pBitmap)
  131. {
  132. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap) + ::GetSystemMetrics(SM_CXVSCROLL);
  133. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap) + ::GetSystemMetrics(SM_CYHSCROLL);
  134. rect.right = rect.left + nWidth;
  135. rect.bottom = rect.top + nHeight;
  136. }
  137. else if(m_csRTF != "")
  138. {
  139. //if showing rtf then increase the size because
  140. //rtf will probably draw bigger
  141. long lNewWidth = (long)rect.Width() + (long)(rect.Width() *1.5);
  142. rect.right = rect.left + lNewWidth;
  143. long lNewHeight = (long)rect.Height() + (long)(rect.Height() *1.5);
  144. rect.bottom = rect.top + lNewHeight;
  145. }
  146. rect.right += CAPTION_BORDER * 2;
  147. rect.bottom += CAPTION_BORDER * 2;
  148. CRect rcScreen;
  149. ClientToScreen(rect);
  150. CRect cr(point, point);
  151. int nMonitor = GetMonitorFromRect(&cr);
  152. GetMonitorRect(nMonitor, &rcScreen);
  153. //ensure that we don't go outside the screen
  154. if(point.x < 0)
  155. {
  156. point.x = 5;
  157. m_reducedWindowSize = true;
  158. }
  159. if(point.y < 0)
  160. {
  161. point.y = 5;
  162. m_reducedWindowSize = true;
  163. }
  164. rcScreen.DeflateRect(0, 0, 5, 5);
  165. long lWidth = rect.Width();
  166. long lHeight = rect.Height();
  167. rect.left = point.x;
  168. rect.top = point.y;
  169. rect.right = rect.left + lWidth;
  170. rect.bottom = rect.top + lHeight;
  171. if (rect.right > rcScreen.right)
  172. {
  173. rect.right = rcScreen.right;
  174. m_reducedWindowSize = true;
  175. }
  176. if (rect.bottom > rcScreen.bottom)
  177. {
  178. rect.bottom = rcScreen.bottom;
  179. m_reducedWindowSize = true;
  180. }
  181. }
  182. m_clipDataStatic.SetWindowText(m_clipData);
  183. m_saveWindowLockout = true;
  184. MoveWindow(rect);
  185. ShowWindow(SW_SHOWNA);
  186. this->Invalidate();
  187. this->UpdateWindow();
  188. m_saveWindowLockout = false;
  189. return TRUE;
  190. }
  191. BOOL CToolTipEx::Hide()
  192. {
  193. DELETE_BITMAP
  194. SaveWindowSize();
  195. ShowWindow(SW_HIDE);
  196. m_csRTF = "";
  197. m_csText = "";
  198. m_clipId = 0;
  199. m_clipRow = -1;
  200. m_searchText = _T("");
  201. return TRUE;
  202. }
  203. void CToolTipEx::SaveWindowSize()
  204. {
  205. if (::IsWindowVisible(m_hWnd))
  206. {
  207. CRect rect;
  208. this->GetWindowRect(&rect);
  209. CGetSetOptions::SetDescWndSize(rect.Size());
  210. CGetSetOptions::SetDescWndPoint(rect.TopLeft());
  211. OutputDebugString(_T("Saving tooltip size"));
  212. }
  213. }
  214. void CToolTipEx::PostNcDestroy()
  215. {
  216. CWnd::PostNcDestroy();
  217. delete this;
  218. }
  219. BOOL CToolTipEx::PreTranslateMessage(MSG *pMsg)
  220. {
  221. m_DittoWindow.DoPreTranslateMessage(pMsg);
  222. switch(pMsg->message)
  223. {
  224. case WM_KEYDOWN:
  225. switch(pMsg->wParam)
  226. {
  227. case VK_ESCAPE:
  228. Hide();
  229. return TRUE;
  230. case 'C':
  231. if(GetKeyState(VK_CONTROL) &0x8000)
  232. {
  233. m_RichEdit.Copy();
  234. }
  235. break;
  236. case VK_F3:
  237. {
  238. DoSearch();
  239. }
  240. break;
  241. }
  242. break;
  243. case WM_RBUTTONDOWN:
  244. {
  245. if (m_RichEdit.m_hWnd == GetFocus()->m_hWnd ||
  246. m_imageViewer.m_hWnd == GetFocus()->m_hWnd)
  247. {
  248. OnOptions();
  249. return TRUE;
  250. }
  251. }
  252. break;
  253. }
  254. return CWnd::PreTranslateMessage(pMsg);
  255. }
  256. BOOL CToolTipEx::OnMsg(MSG *pMsg)
  257. {
  258. if(FALSE == IsWindowVisible())
  259. {
  260. return FALSE;
  261. }
  262. switch(pMsg->message)
  263. {
  264. case WM_WINDOWPOSCHANGING:
  265. case WM_LBUTTONDOWN:
  266. {
  267. if (CGetSetOptions::GetMouseClickHidesDescription())
  268. {
  269. if (!IsCursorInToolTip())
  270. {
  271. Hide();
  272. }
  273. }
  274. }
  275. break;
  276. case WM_KEYDOWN:
  277. {
  278. WPARAM vk = pMsg->wParam;
  279. if(vk == VK_ESCAPE)
  280. {
  281. Hide();
  282. return TRUE;
  283. }
  284. else if(vk == VK_TAB)
  285. {
  286. m_RichEdit.SetFocus();
  287. return TRUE;
  288. }
  289. else if(vk == 'N')
  290. {
  291. return FALSE;
  292. }
  293. else if (vk == 'P')
  294. {
  295. return FALSE;
  296. }
  297. else if (vk == VK_UP)
  298. {
  299. return FALSE;
  300. }
  301. else if (vk == VK_DOWN)
  302. {
  303. return FALSE;
  304. }
  305. else if(vk == VK_F3)
  306. {
  307. DoSearch();
  308. return TRUE;
  309. }
  310. else if(vk == VK_SHIFT)
  311. {
  312. return FALSE;
  313. }
  314. else if(vk == VK_NEXT)
  315. {
  316. return FALSE;
  317. }
  318. else if (vk == VK_PRIOR)
  319. {
  320. return FALSE;
  321. }
  322. Hide();
  323. break;
  324. }
  325. case WM_LBUTTONDBLCLK:
  326. case WM_RBUTTONDBLCLK:
  327. case WM_MBUTTONDOWN:
  328. case WM_MBUTTONDBLCLK:
  329. case WM_NCLBUTTONDOWN:
  330. case WM_NCLBUTTONDBLCLK:
  331. case WM_NCRBUTTONDOWN:
  332. case WM_NCRBUTTONDBLCLK:
  333. case WM_NCMBUTTONDOWN:
  334. case WM_NCMBUTTONDBLCLK:
  335. {
  336. Hide();
  337. break;
  338. }
  339. }
  340. return FALSE;
  341. }
  342. CRect CToolTipEx::GetBoundsRect()
  343. {
  344. DWORD d = GetTickCount();
  345. CWindowDC dc(NULL);
  346. int nLineWidth = 0;
  347. CRect rect(0, 0, 0, 0);
  348. if(nLineWidth == 0)
  349. {
  350. // Count the number of lines of text
  351. int nStart = 0;
  352. INT nNumLines = 0;
  353. int longestLength = 0;
  354. CString longestString;
  355. do
  356. {
  357. int newStart = m_csText.Find(_T("\n"), nStart);
  358. if (newStart < 0)
  359. {
  360. int length = m_csText.GetLength() - nStart;
  361. if (length > longestLength)
  362. {
  363. longestString = m_csText.Mid(nStart, length);
  364. longestLength = length;
  365. }
  366. break;
  367. }
  368. int length = newStart - nStart;
  369. if(length > longestLength)
  370. {
  371. longestString = m_csText.Mid(nStart, length);
  372. longestLength = length;
  373. }
  374. nNumLines++;
  375. nStart = newStart + 1;
  376. }
  377. while(nStart >= 0 && nNumLines < 100);
  378. CFont *pOldFont = (CFont*)dc.SelectObject((CFont*)&m_Font);
  379. CSize size = dc.GetTextExtent(longestString);
  380. dc.SelectObject(pOldFont);
  381. rect.right = size.cx;
  382. rect.bottom = size.cy * nNumLines;
  383. }
  384. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  385. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  386. if(m_imageViewer.m_pBitmap)
  387. {
  388. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap);
  389. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap);
  390. rect.bottom += nHeight;
  391. if((rect.left + nWidth) > rect.right)
  392. {
  393. rect.right = rect.left + nWidth;
  394. }
  395. }
  396. DWORD diff = GetTickCount() - d;
  397. if (diff > 10)
  398. {
  399. Log(StrF(_T("Size To Content: %d\n"), diff));
  400. }
  401. return rect;
  402. }
  403. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  404. {
  405. CString strReturn;
  406. LPCTSTR pstrStart = ref.LockBuffer();
  407. LPCTSTR pstrBuffer = pstrStart;
  408. int nCurrent = 0;
  409. int nStart = 0;
  410. int nEnd = 0;
  411. int nOldStart = 0;
  412. while(nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  413. {
  414. if(*pstrBuffer == ch)
  415. {
  416. nOldStart = nStart;
  417. nStart = nEnd + 1;
  418. nCurrent++;
  419. }
  420. nEnd++;
  421. pstrBuffer++;
  422. }
  423. // May have reached the end of the string
  424. if(*pstrBuffer == _T('\0'))
  425. {
  426. nOldStart = nStart;
  427. nEnd++;
  428. }
  429. ref.UnlockBuffer();
  430. if(nCurrent < nIndex)
  431. {
  432. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  433. return strReturn;
  434. }
  435. return ref.Mid(nOldStart, nEnd - nOldStart - 1);
  436. }
  437. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  438. {
  439. static LOGFONT LogFont;
  440. NONCLIENTMETRICS ncm;
  441. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  442. if(!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
  443. &ncm, 0))
  444. {
  445. return FALSE;
  446. }
  447. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  448. return &LogFont;
  449. }
  450. BOOL CToolTipEx::SetLogFont(LPLOGFONT lpLogFont, BOOL bRedraw /*=TRUE*/)
  451. {
  452. ASSERT(lpLogFont);
  453. if(!lpLogFont)
  454. {
  455. return FALSE;
  456. }
  457. LOGFONT LogFont;
  458. // Store font as the global default
  459. memcpy(&LogFont, lpLogFont, sizeof(LOGFONT));
  460. // Create the actual font object
  461. m_Font.DeleteObject();
  462. m_Font.CreateFontIndirect(&LogFont);
  463. if(bRedraw && ::IsWindow(GetSafeHwnd()))
  464. {
  465. Invalidate();
  466. }
  467. return TRUE;
  468. }
  469. void CToolTipEx::SetBitmap(CBitmap *pBitmap)
  470. {
  471. DELETE_BITMAP
  472. m_imageViewer.m_pBitmap = pBitmap;
  473. m_imageViewer.UpdateBitmapSize();
  474. if (m_imageViewer.m_pBitmap != NULL)
  475. {
  476. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap);
  477. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap);
  478. Invalidate();
  479. }
  480. }
  481. void CToolTipEx::OnSize(UINT nType, int cx, int cy)
  482. {
  483. CWnd::OnSize(nType, cx, cy);
  484. if(::IsWindow(m_RichEdit.GetSafeHwnd()) == FALSE)
  485. {
  486. return ;
  487. }
  488. CRect cr;
  489. GetClientRect(cr);
  490. cr.DeflateRect(0, 0, 0, theApp.m_metrics.ScaleY(21));
  491. m_RichEdit.MoveWindow(cr);
  492. m_imageViewer.MoveWindow(cr);
  493. m_optionsButton.MoveWindow(cr.left, cr.bottom + theApp.m_metrics.ScaleY(2), theApp.m_metrics.ScaleX(17), theApp.m_metrics.ScaleY(17));
  494. m_clipDataStatic.MoveWindow(cr.left + theApp.m_metrics.ScaleX(19), cr.bottom + theApp.m_metrics.ScaleY(2), cr.Width() - cr.left + theApp.m_metrics.ScaleX(19), theApp.m_metrics.ScaleY(17));
  495. this->Invalidate();
  496. m_DittoWindow.DoSetRegion(this);
  497. if (m_saveWindowLockout == false)
  498. {
  499. SetTimer(SAVE_SIZE, 250, NULL);
  500. }
  501. }
  502. BOOL CToolTipEx::IsCursorInToolTip()
  503. {
  504. CRect cr;
  505. GetWindowRect(cr);
  506. CPoint cursorPos;
  507. GetCursorPos(&cursorPos);
  508. return cr.PtInRect(cursorPos);
  509. }
  510. void CToolTipEx::SetRTFText(const char *pRTF)
  511. {
  512. m_RichEdit.SetRTF(pRTF);
  513. m_csRTF = pRTF;
  514. m_RichEdit.SetSel(0, 0);
  515. HighlightSearchText();
  516. }
  517. //void CToolTipEx::SetRTFText(const CString &csRTF)
  518. //{
  519. // m_RichEdit.SetRTF(csRTF);
  520. // m_csRTF = csRTF;
  521. //}
  522. void CToolTipEx::SetToolTipText(const CString &csText)
  523. {
  524. m_csText = csText;
  525. m_RichEdit.SetFont(&m_Font);
  526. m_RichEdit.SetText(csText);
  527. m_RichEdit.SetSel(0, 0);
  528. CHARFORMAT cfNew;
  529. cfNew.cbSize = sizeof(CHARFORMAT);
  530. cfNew.dwMask = CFM_COLOR;
  531. cfNew.dwEffects = CFM_COLOR;
  532. cfNew.dwEffects &= ~CFE_AUTOCOLOR;
  533. cfNew.crTextColor = g_Opt.m_Theme.DescriptionWindowText();
  534. m_RichEdit.SetDefaultCharFormat(cfNew);
  535. HighlightSearchText();
  536. }
  537. void CToolTipEx::HighlightSearchText()
  538. {
  539. if (m_searchText.GetLength() <= 0)
  540. return;
  541. FINDTEXTEX ft;
  542. long n = -1;
  543. ft.lpstrText = m_searchText;
  544. ft.chrg.cpMin = 0;
  545. ft.chrg.cpMax = -1;
  546. CHARFORMAT cf;
  547. cf.cbSize = sizeof(cf);
  548. cf.dwMask = CFM_COLOR;
  549. cf.dwEffects = CFE_BOLD | ~CFE_AUTOCOLOR;
  550. cf.crTextColor = RGB(255, 0, 0);
  551. do
  552. {
  553. ft.chrg.cpMin = n+1;
  554. n = m_RichEdit.FindText(FR_DOWN, &ft);
  555. if (n != -1)
  556. {
  557. m_RichEdit.SetSel(ft.chrgText);
  558. m_RichEdit.SetSelectionCharFormat(cf);
  559. }
  560. } while (n != -1);
  561. m_RichEdit.SetSel(0, 0);
  562. }
  563. void CToolTipEx::DoSearch()
  564. {
  565. if (m_searchText.GetLength() <= 0)
  566. return;
  567. FINDTEXTEX ft;
  568. long n = -1;
  569. ft.lpstrText = m_searchText;
  570. long start;
  571. long end;
  572. m_RichEdit.GetSel(start, end);
  573. ft.chrg.cpMin = end;
  574. ft.chrg.cpMax = -1;
  575. int searchDirection = FR_DOWN;
  576. if (GetKeyState(VK_SHIFT) & 0x8000)
  577. {
  578. searchDirection = 0;
  579. ft.chrg.cpMin = start;
  580. }
  581. n = m_RichEdit.FindText(searchDirection, &ft);
  582. if (n != -1)
  583. {
  584. m_RichEdit.SetSel(ft.chrgText);
  585. }
  586. else
  587. {
  588. if (searchDirection == 0)
  589. {
  590. ft.chrg.cpMin = m_RichEdit.GetTextLength();
  591. }
  592. else
  593. {
  594. ft.chrg.cpMin = 0;
  595. }
  596. ft.chrg.cpMax = -1;
  597. n = m_RichEdit.FindText(searchDirection, &ft);
  598. if (n != -1)
  599. {
  600. m_RichEdit.SetSel(ft.chrgText);
  601. }
  602. }
  603. }
  604. void CToolTipEx::OnActivate(UINT nState, CWnd *pWndOther, BOOL bMinimized)
  605. {
  606. CWnd::OnActivate(nState, pWndOther, bMinimized);
  607. if (nState == WA_INACTIVE)
  608. {
  609. if(m_pNotifyWnd)
  610. {
  611. m_pNotifyWnd->PostMessage(NM_INACTIVE_TOOLTIPWND, 0, 0);
  612. }
  613. }
  614. }
  615. void CToolTipEx::OnTimer(UINT_PTR nIDEvent)
  616. {
  617. switch(nIDEvent)
  618. {
  619. case HIDE_WINDOW_TIMER:
  620. Hide();
  621. PostMessage(WM_DESTROY, 0, 0);
  622. break;
  623. case SAVE_SIZE:
  624. SaveWindowSize();
  625. KillTimer(SAVE_SIZE);
  626. break;
  627. case TIMER_BUTTON_UP:
  628. {
  629. if ((GetKeyState(VK_LBUTTON) & 0x100) == 0)
  630. {
  631. m_DittoWindow.DoNcLButtonUp(this, 0, CPoint(0, 0));
  632. KillTimer(TIMER_BUTTON_UP);
  633. }
  634. break;
  635. }
  636. }
  637. CWnd::OnTimer(nIDEvent);
  638. }
  639. void CToolTipEx::OnNcPaint()
  640. {
  641. m_DittoWindow.DoNcPaint(this);
  642. }
  643. void CToolTipEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  644. {
  645. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  646. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  647. }
  648. HITTEST_RET CToolTipEx::OnNcHitTest(CPoint point)
  649. {
  650. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  651. if(Ret == -1)
  652. return CWnd::OnNcHitTest(point);
  653. return Ret;
  654. }
  655. void CToolTipEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  656. {
  657. int buttonPressed = m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  658. if (buttonPressed != 0)
  659. {
  660. SetTimer(TIMER_BUTTON_UP, 100, NULL);
  661. }
  662. CWnd::OnNcLButtonDown(nHitTest, point);
  663. }
  664. void CToolTipEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  665. {
  666. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  667. switch(lRet)
  668. {
  669. case BUTTON_CLOSE:
  670. Hide();
  671. break;
  672. }
  673. KillTimer(TIMER_BUTTON_UP);
  674. CWnd::OnNcLButtonUp(nHitTest, point);
  675. }
  676. void CToolTipEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  677. {
  678. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  679. CWnd::OnNcMouseMove(nHitTest, point);
  680. }
  681. void CToolTipEx::OnOptions()
  682. {
  683. POINT pp;
  684. CMenu cmPopUp;
  685. CMenu *cmSubMenu = NULL;
  686. GetCursorPos(&pp);
  687. if(cmPopUp.LoadMenu(IDR_DESC_OPTIONS_MENU) != 0)
  688. {
  689. cmSubMenu = cmPopUp.GetSubMenu(0);
  690. if(!cmSubMenu)
  691. {
  692. return ;
  693. }
  694. GetCursorPos(&pp);
  695. //theApp.m_Language.UpdateRightClickMenu(cmSubMenu);
  696. if(CGetSetOptions::GetRememberDescPos())
  697. cmSubMenu->CheckMenuItem(ID_FIRST_REMEMBERWINDOWPOSITION, MF_CHECKED);
  698. if(CGetSetOptions::GetSizeDescWindowToContent())
  699. cmSubMenu->CheckMenuItem(ID_FIRST_SIZEWINDOWTOCONTENT, MF_CHECKED);
  700. if(CGetSetOptions::GetScaleImagesToDescWindow())
  701. cmSubMenu->CheckMenuItem(ID_FIRST_SCALEIMAGESTOFITWINDOW, MF_CHECKED);
  702. if (CGetSetOptions::GetMouseClickHidesDescription())
  703. cmSubMenu->CheckMenuItem(ID_FIRST_HIDEDESCRIPTIONWINDOWONM, MF_CHECKED);
  704. if (CGetSetOptions::GetWrapDescriptionText())
  705. cmSubMenu->CheckMenuItem(ID_FIRST_WRAPTEXT, MF_CHECKED);
  706. cmSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, pp.x, pp.y, this, NULL);
  707. }
  708. }
  709. void CToolTipEx::OnRememberwindowposition()
  710. {
  711. CGetSetOptions::SetRememberDescPos(!CGetSetOptions::GetRememberDescPos());
  712. }
  713. void CToolTipEx::OnSizewindowtocontent()
  714. {
  715. CGetSetOptions::SetSizeDescWindowToContent(!CGetSetOptions::GetSizeDescWindowToContent());
  716. CRect rect;
  717. this->GetWindowRect(&rect);
  718. Show(rect.TopLeft());
  719. }
  720. void CToolTipEx::OnScaleimagestofitwindow()
  721. {
  722. CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
  723. m_imageViewer.UpdateBitmapSize();
  724. Invalidate();
  725. }
  726. void CToolTipEx::OnRButtonDown(UINT nFlags, CPoint point)
  727. {
  728. OnOptions();
  729. CWnd::OnRButtonDown(nFlags, point);
  730. }
  731. void CToolTipEx::OnSetFocus(CWnd* pOldWnd)
  732. {
  733. CWnd::OnSetFocus(pOldWnd);
  734. m_RichEdit.SetFocus();
  735. }
  736. void CToolTipEx::OnPaint()
  737. {
  738. CPaintDC dc(this); // device context for painting
  739. CRect rect;
  740. GetClientRect(rect);
  741. CBrush Brush, *pOldBrush;
  742. Brush.CreateSolidBrush(g_Opt.m_Theme.DescriptionWindowBG());
  743. pOldBrush = dc.SelectObject(&Brush);
  744. dc.FillRect(&rect, &Brush);
  745. // Cleanup
  746. dc.SelectObject(pOldBrush);
  747. }
  748. void CToolTipEx::OnFirstHidedescriptionwindowonm()
  749. {
  750. CGetSetOptions::SetMouseClickHidesDescription(!CGetSetOptions::GetMouseClickHidesDescription());
  751. }
  752. void CToolTipEx::OnFirstWraptext()
  753. {
  754. CGetSetOptions::SetWrapDescriptionText(!CGetSetOptions::GetWrapDescriptionText());
  755. ApplyWordWrap();
  756. }
  757. void CToolTipEx::ApplyWordWrap()
  758. {
  759. if (CGetSetOptions::GetWrapDescriptionText())
  760. {
  761. m_RichEdit.SetTargetDevice(NULL, 0);
  762. }
  763. else
  764. {
  765. m_RichEdit.SetTargetDevice(NULL, 1);
  766. }
  767. }
  768. void CToolTipEx::HideWindowInXMilliSeconds(long lms)
  769. {
  770. SetTimer(HIDE_WINDOW_TIMER, lms, NULL);
  771. }
  772. void CToolTipEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  773. {
  774. CWnd::OnWindowPosChanging(lpwndpos);
  775. m_DittoWindow.SnapToEdge(this, lpwndpos);
  776. }