ToolTipEx.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. #include "stdafx.h"
  2. #include "cp_main.h"
  3. #include "ToolTipEx.h"
  4. #include "BitmapHelper.h"
  5. #include "Options.h"
  6. #include "ActionEnums.h"
  7. #include "HyperLink.h"
  8. #include <Richedit.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define HIDE_WINDOW_TIMER 1
  15. #define SAVE_SIZE 2
  16. #define TIMER_BUTTON_UP 3
  17. #define TIMER_AUTO_MAX 4
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CToolTipEx
  20. CToolTipEx::CToolTipEx(): m_dwTextStyle(DT_EXPANDTABS | DT_EXTERNALLEADING |
  21. DT_NOPREFIX | DT_WORDBREAK), m_rectMargin(2, 2, 3, 3),
  22. m_pNotifyWnd(NULL), m_clipId(0), m_clipRow(-1)
  23. {
  24. m_showPersistant = false;
  25. m_pToolTipActions = NULL;
  26. m_bMaxSetTimer = false;
  27. m_lDelayMaxSeconds = 2;
  28. }
  29. CToolTipEx::~CToolTipEx()
  30. {
  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_NCLBUTTONDBLCLK()
  42. ON_WM_NCPAINT()
  43. ON_WM_NCCALCSIZE()
  44. ON_WM_NCLBUTTONDOWN()
  45. ON_WM_NCMOUSEMOVE()
  46. ON_WM_NCLBUTTONUP()
  47. ON_WM_ERASEBKGND()
  48. ON_COMMAND(ID_FIRST_REMEMBERWINDOWPOSITION, &CToolTipEx::OnRememberwindowposition)
  49. ON_COMMAND(ID_FIRST_SIZEWINDOWTOCONTENT, &CToolTipEx::OnSizewindowtocontent)
  50. ON_COMMAND(ID_FIRST_SCALEIMAGESTOFITWINDOW, &CToolTipEx::OnScaleimagestofitwindow)
  51. ON_COMMAND(2, OnOptions)
  52. ON_WM_RBUTTONDOWN()
  53. ON_WM_SETFOCUS()
  54. ON_COMMAND(ID_FIRST_HIDEDESCRIPTIONWINDOWONM, &CToolTipEx::OnFirstHidedescriptionwindowonm)
  55. ON_COMMAND(ID_FIRST_WRAPTEXT, &CToolTipEx::OnFirstWraptext)
  56. ON_WM_WINDOWPOSCHANGING()
  57. ON_COMMAND(ID_FIRST_ALWAYSONTOP, &CToolTipEx::OnFirstAlwaysontop)
  58. ON_NOTIFY(EN_MSGFILTER, 1, &CToolTipEx::OnEnMsgfilterRichedit21)
  59. ON_MESSAGE(WM_DPICHANGED, OnDpiChanged)
  60. ON_WM_MOVING()
  61. ON_WM_ENTERSIZEMOVE()
  62. END_MESSAGE_MAP()
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CToolTipEx message handlers
  65. BOOL CToolTipEx::Create(CWnd *pParentWnd)
  66. {
  67. m_saveWindowLockout = true;
  68. // Get the class name and create the window
  69. CString szClassName = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  70. // Create the window - just don't show it yet.
  71. if( !CWnd::CreateEx(0, szClassName, _T(""), WS_POPUP,
  72. 0, 0, 0, 0, pParentWnd->GetSafeHwnd(), 0, NULL))
  73. {
  74. return FALSE;
  75. }
  76. //CString szClassName2 = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  77. //BOOL b = m_imageViewer.Create(_T(""), szClassName2, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, CRect(0, 0, 0, 0), this, 3);
  78. m_imageViewer.Create(this);
  79. m_DittoWindow.DoCreate(this);
  80. m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  81. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true, g_Opt.m_Theme.GetCaptionSize(), g_Opt.m_Theme.GetCaptionFontSize());
  82. m_DittoWindow.m_bDrawMaximize = false;
  83. m_DittoWindow.m_bDrawMinimize = false;
  84. m_DittoWindow.m_bDrawChevron = true;
  85. m_DittoWindow.m_sendWMClose = false;
  86. m_RichEdit.Create(_T(""), _T(""), WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  87. WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_NOHIDESEL |
  88. ES_AUTOHSCROLL, CRect(10, 10, 100, 200), this, 1);
  89. m_RichEdit.SetReadOnly();
  90. m_RichEdit.SetBackgroundColor(FALSE, g_Opt.m_Theme.DescriptionWindowBG());
  91. m_RichEdit.SetEventMask(m_RichEdit.GetEventMask() | ENM_SELCHANGE | ENM_LINK | ENM_MOUSEEVENTS | ENM_SCROLLEVENTS);
  92. m_RichEdit.SetAutoURLDetect(TRUE);
  93. ApplyWordWrap();
  94. SetLogFont(GetSystemToolTipFont(), FALSE);
  95. m_optionsButton.Create(NULL, WS_CHILD | BS_OWNERDRAW | WS_TABSTOP, CRect(0, 0, 0, 0), this, 2);
  96. m_optionsButton.LoadStdImageDPI(m_DittoWindow.m_dpi.GetDPIX(), IDB_COG_16_16, IDB_COG_20_20, IDB_COG_24_24, cog_28, IDB_COG_32_32, _T("PNG"));
  97. m_optionsButton.SetToolTipText(theApp.m_Language.GetString(_T("DescriptionOptionsTooltip"), _T("Description Options")));
  98. m_optionsButton.ShowWindow(SW_SHOW);
  99. m_clipDataStatic.Create(_T("some text"), WS_CHILD | WS_VISIBLE | SS_SIMPLE, CRect(0, 0, 0, 0), this, 3);
  100. m_clipDataFont.CreateFont(-m_DittoWindow.m_dpi.PointsToPixels(8), 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 3, 2, 1, 34, _T("Segoe UI"));
  101. m_clipDataStatic.SetFont(&m_clipDataFont);
  102. m_clipDataStatic.SetBkColor(g_Opt.m_Theme.DescriptionWindowBG());
  103. m_clipDataStatic.SetTextColor(RGB(80, 80, 80));
  104. m_saveWindowLockout = false;
  105. return TRUE;
  106. }
  107. BOOL CToolTipEx::Show(CPoint point)
  108. {
  109. m_reducedWindowSize = false;
  110. if(m_imageViewer.m_pGdiplusBitmap)
  111. {
  112. m_RichEdit.ShowWindow(SW_HIDE);
  113. m_imageViewer.ShowWindow(SW_SHOW);
  114. m_imageViewer.UpdateBitmapSize();
  115. }
  116. else
  117. {
  118. m_RichEdit.ShowWindow(SW_SHOW);
  119. m_imageViewer.ShowWindow(SW_HIDE);
  120. }
  121. CRect rect;
  122. if(CGetSetOptions::GetSizeDescWindowToContent() == FALSE)
  123. {
  124. rect.left = point.x;
  125. rect.top = point.y;
  126. CSize size;
  127. CGetSetOptions::GetDescWndSize(size);
  128. rect.right = rect.left + m_DittoWindow.m_dpi.ScaleX(size.cx);
  129. rect.bottom = rect.top + m_DittoWindow.m_dpi.ScaleY(size.cy);
  130. EnsureWindowVisible(&rect);
  131. }
  132. else
  133. {
  134. rect = GetBoundsRect();
  135. //account for the scroll bars
  136. rect.right += 20;
  137. rect.bottom += 20;
  138. if (m_imageViewer.m_pGdiplusBitmap)
  139. {
  140. int nWidth = m_imageViewer.m_pGdiplusBitmap->GetWidth() + ::GetSystemMetrics(SM_CXVSCROLL);
  141. int nHeight = m_imageViewer.m_pGdiplusBitmap->GetHeight() + ::GetSystemMetrics(SM_CYHSCROLL);
  142. rect.right = rect.left + nWidth;
  143. rect.bottom = rect.top + nHeight;
  144. }
  145. else if(m_csRTF != "")
  146. {
  147. //if showing rtf then increase the size because
  148. //rtf will probably draw bigger
  149. long lNewWidth = (long)rect.Width() + (long)(rect.Width() *1.5);
  150. rect.right = rect.left + lNewWidth;
  151. long lNewHeight = (long)rect.Height() + (long)(rect.Height() *1.5);
  152. rect.bottom = rect.top + lNewHeight;
  153. }
  154. //rect.right += CAPTION_BORDER * 2;
  155. //rect.bottom += CAPTION_BORDER * 2;
  156. CRect rcScreen;
  157. ClientToScreen(rect);
  158. CRect cr(point, point);
  159. int nMonitor = GetMonitorFromRect(&cr);
  160. GetMonitorRect(nMonitor, &rcScreen);
  161. //ensure that we don't go outside the screen
  162. if(point.x < 0)
  163. {
  164. point.x = 5;
  165. m_reducedWindowSize = true;
  166. }
  167. if(point.y < 0)
  168. {
  169. point.y = 5;
  170. m_reducedWindowSize = true;
  171. }
  172. rcScreen.DeflateRect(0, 0, 5, 5);
  173. long lWidth = rect.Width();
  174. long lHeight = rect.Height();
  175. rect.left = point.x;
  176. rect.top = point.y;
  177. rect.right = rect.left + lWidth;
  178. rect.bottom = rect.top + lHeight;
  179. if (rect.right > rcScreen.right)
  180. {
  181. rect.right = rcScreen.right;
  182. m_reducedWindowSize = true;
  183. }
  184. if (rect.bottom > rcScreen.bottom)
  185. {
  186. rect.bottom = rcScreen.bottom;
  187. m_reducedWindowSize = true;
  188. }
  189. }
  190. m_clipDataStatic.SetWindowText(m_clipData);
  191. if (m_DittoWindow.m_bMinimized)
  192. {
  193. //m_DittoWindow.MinMaxWindow(this, FORCE_MAX);
  194. m_DittoWindow.m_bMinimized = false;
  195. }
  196. m_saveWindowLockout = true;
  197. MoveWindow(rect);
  198. ShowWindow(SW_SHOWNA);
  199. this->Invalidate();
  200. this->UpdateWindow();
  201. m_saveWindowLockout = false;
  202. return TRUE;
  203. }
  204. void CToolTipEx::GetWindowRectEx(LPRECT lpRect)
  205. {
  206. if (m_DittoWindow.m_bMinimized)
  207. {
  208. *lpRect = m_DittoWindow.m_crFullSizeWindow;
  209. return;
  210. }
  211. CWnd::GetWindowRect(lpRect);
  212. }
  213. BOOL CToolTipEx::Hide()
  214. {
  215. delete m_imageViewer.m_pGdiplusBitmap;
  216. m_imageViewer.m_pGdiplusBitmap = NULL;
  217. SaveWindowSize();
  218. ShowWindow(SW_HIDE);
  219. m_csRTF = "";
  220. m_csText = "";
  221. m_clipId = 0;
  222. m_clipRow = -1;
  223. m_searchText = _T("");
  224. m_showPersistant = false;
  225. return TRUE;
  226. }
  227. void CToolTipEx::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
  228. {
  229. // toggle ShowPersistent when we double click the caption
  230. if (nHitTest == HTCAPTION)
  231. {
  232. OnFirstAlwaysontop();
  233. }
  234. CWnd::OnNcLButtonDblClk(nHitTest, point);
  235. }
  236. void CToolTipEx::SaveWindowSize()
  237. {
  238. if (::IsWindowVisible(m_hWnd))
  239. {
  240. CRect rect;
  241. if (m_DittoWindow.m_bMinimized)
  242. {
  243. rect = m_DittoWindow.m_crFullSizeWindow;
  244. }
  245. else
  246. {
  247. this->GetWindowRect(&rect);
  248. }
  249. CSize s = rect.Size();
  250. CGetSetOptions::SetDescWndSize(CSize(m_DittoWindow.m_dpi.UnscaleX(s.cx), m_DittoWindow.m_dpi.UnscaleX(s.cy)));
  251. CGetSetOptions::SetDescWndPoint(rect.TopLeft());
  252. OutputDebugString(_T("Saving tooltip size"));
  253. }
  254. }
  255. void CToolTipEx::PostNcDestroy()
  256. {
  257. CWnd::PostNcDestroy();
  258. delete this;
  259. }
  260. BOOL CToolTipEx::PreTranslateMessage(MSG *pMsg)
  261. {
  262. m_DittoWindow.DoPreTranslateMessage(pMsg);
  263. switch (pMsg->message)
  264. {
  265. case WM_KEYDOWN:
  266. switch(pMsg->wParam)
  267. {
  268. case 'C':
  269. if(GetKeyState(VK_CONTROL) &0x8000)
  270. {
  271. m_RichEdit.Copy();
  272. }
  273. break;
  274. }
  275. break;
  276. case WM_RBUTTONDOWN:
  277. {
  278. if (m_RichEdit.m_hWnd == GetFocus()->m_hWnd ||
  279. m_imageViewer.m_hWnd == GetFocus()->m_hWnd)
  280. {
  281. OnOptions();
  282. return TRUE;
  283. }
  284. }
  285. break;
  286. }
  287. if (m_pToolTipActions != NULL)
  288. {
  289. CAccel a;
  290. if (m_pToolTipActions->OnMsg(pMsg, a))
  291. {
  292. switch (a.Cmd)
  293. {
  294. case ActionEnums::CLOSEWINDOW:
  295. /*if (this->m_showPersistant &&
  296. m_DittoWindow.m_bMinimized == false)
  297. {
  298. m_DittoWindow.MinMaxWindow(this, FORCE_MIN);
  299. theApp.m_activeWnd.ReleaseFocus();
  300. return TRUE;
  301. }*/
  302. break;
  303. }
  304. }
  305. }
  306. return CWnd::PreTranslateMessage(pMsg);
  307. }
  308. BOOL CToolTipEx::OnMsg(MSG *pMsg)
  309. {
  310. if(FALSE == IsWindowVisible())
  311. {
  312. return FALSE;
  313. }
  314. switch(pMsg->message)
  315. {
  316. case WM_WINDOWPOSCHANGING:
  317. case WM_LBUTTONDOWN:
  318. {
  319. if (m_showPersistant == false)
  320. {
  321. if (CGetSetOptions::GetMouseClickHidesDescription())
  322. {
  323. if (!IsCursorInToolTip())
  324. {
  325. Hide();
  326. }
  327. }
  328. }
  329. }
  330. break;
  331. case WM_KEYDOWN:
  332. {
  333. WPARAM vk = pMsg->wParam;
  334. if(vk == VK_TAB)
  335. {
  336. m_RichEdit.SetFocus();
  337. return TRUE;
  338. }
  339. else if (vk == VK_CONTROL || vk == VK_SHIFT)
  340. {
  341. return FALSE;
  342. }
  343. else if (vk == VK_UP)
  344. {
  345. return FALSE;
  346. }
  347. else if (vk == VK_DOWN)
  348. {
  349. return FALSE;
  350. }
  351. else if (vk == VK_NEXT)
  352. {
  353. return FALSE;
  354. }
  355. else if (vk == VK_PRIOR)
  356. {
  357. return FALSE;
  358. }
  359. else if (vk == VK_DELETE)
  360. {
  361. return FALSE;
  362. }
  363. if (m_pToolTipActions != NULL)
  364. {
  365. if (m_pToolTipActions->ContainsKey((int)vk))
  366. {
  367. return FALSE;
  368. }
  369. }
  370. if (m_showPersistant == false)
  371. {
  372. Hide();
  373. }
  374. break;
  375. }
  376. case WM_LBUTTONDBLCLK:
  377. case WM_RBUTTONDBLCLK:
  378. case WM_MBUTTONDOWN:
  379. case WM_MBUTTONDBLCLK:
  380. case WM_NCLBUTTONDOWN:
  381. case WM_NCLBUTTONDBLCLK:
  382. case WM_NCRBUTTONDOWN:
  383. case WM_NCRBUTTONDBLCLK:
  384. case WM_NCMBUTTONDOWN:
  385. case WM_NCMBUTTONDBLCLK:
  386. {
  387. if (m_showPersistant == false)
  388. {
  389. Hide();
  390. }
  391. break;
  392. }
  393. case WM_MOUSEWHEEL:
  394. {
  395. if (m_imageViewer.m_pGdiplusBitmap)
  396. {
  397. m_imageViewer.PostMessageW(pMsg->message, pMsg->wParam, pMsg->lParam);
  398. return TRUE;
  399. }
  400. else
  401. {
  402. m_RichEdit.PostMessageW(pMsg->message, pMsg->wParam, pMsg->lParam);
  403. return TRUE;
  404. }
  405. }
  406. break;
  407. }
  408. return FALSE;
  409. }
  410. CRect CToolTipEx::GetBoundsRect()
  411. {
  412. DWORD d = GetTickCount();
  413. CWindowDC dc(NULL);
  414. int nLineWidth = 0;
  415. CRect rect(0, 0, 0, 0);
  416. if(nLineWidth == 0)
  417. {
  418. // Count the number of lines of text
  419. int nStart = 0;
  420. INT nNumLines = 0;
  421. int longestLength = 0;
  422. CString longestString;
  423. do
  424. {
  425. int newStart = m_csText.Find(_T("\n"), nStart);
  426. if (newStart < 0)
  427. {
  428. int length = m_csText.GetLength() - nStart;
  429. if (length > longestLength)
  430. {
  431. longestString = m_csText.Mid(nStart, length);
  432. longestLength = length;
  433. }
  434. break;
  435. }
  436. int length = newStart - nStart;
  437. if(length > longestLength)
  438. {
  439. longestString = m_csText.Mid(nStart, length);
  440. longestLength = length;
  441. }
  442. nNumLines++;
  443. nStart = newStart + 1;
  444. }
  445. while(nStart >= 0 && nNumLines < 100);
  446. CFont *pOldFont = (CFont*)dc.SelectObject((CFont*)&m_Font);
  447. CSize size = dc.GetTextExtent(longestString);
  448. dc.SelectObject(pOldFont);
  449. rect.right = size.cx;
  450. rect.bottom = size.cy * nNumLines;
  451. }
  452. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  453. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  454. if(m_imageViewer.m_pGdiplusBitmap)
  455. {
  456. int nWidth = m_imageViewer.m_pGdiplusBitmap->GetWidth();
  457. int nHeight = m_imageViewer.m_pGdiplusBitmap->GetHeight();
  458. rect.bottom += nHeight;
  459. if((rect.left + nWidth) > rect.right)
  460. {
  461. rect.right = rect.left + nWidth;
  462. }
  463. }
  464. DWORD diff = GetTickCount() - d;
  465. if (diff > 10)
  466. {
  467. Log(StrF(_T("Size To Content: %d\n"), diff));
  468. }
  469. return rect;
  470. }
  471. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  472. {
  473. CString strReturn;
  474. LPCTSTR pstrStart = ref.LockBuffer();
  475. LPCTSTR pstrBuffer = pstrStart;
  476. int nCurrent = 0;
  477. int nStart = 0;
  478. int nEnd = 0;
  479. int nOldStart = 0;
  480. while(nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  481. {
  482. if(*pstrBuffer == ch)
  483. {
  484. nOldStart = nStart;
  485. nStart = nEnd + 1;
  486. nCurrent++;
  487. }
  488. nEnd++;
  489. pstrBuffer++;
  490. }
  491. // May have reached the end of the string
  492. if(*pstrBuffer == _T('\0'))
  493. {
  494. nOldStart = nStart;
  495. nEnd++;
  496. }
  497. ref.UnlockBuffer();
  498. if(nCurrent < nIndex)
  499. {
  500. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  501. return strReturn;
  502. }
  503. return ref.Mid(nOldStart, nEnd - nOldStart - 1);
  504. }
  505. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  506. {
  507. static LOGFONT LogFont;
  508. NONCLIENTMETRICS ncm;
  509. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  510. if(!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
  511. &ncm, 0))
  512. {
  513. return FALSE;
  514. }
  515. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  516. return &LogFont;
  517. }
  518. BOOL CToolTipEx::SetLogFont(LPLOGFONT lpLogFont, BOOL bRedraw /*=TRUE*/)
  519. {
  520. ASSERT(lpLogFont);
  521. if(!lpLogFont)
  522. {
  523. return FALSE;
  524. }
  525. LOGFONT LogFont;
  526. // Store font as the global default
  527. memcpy(&LogFont, lpLogFont, sizeof(LOGFONT));
  528. // Create the actual font object
  529. m_Font.DeleteObject();
  530. m_Font.CreateFontIndirect(&LogFont);
  531. if(bRedraw && ::IsWindow(GetSafeHwnd()))
  532. {
  533. Invalidate();
  534. }
  535. return TRUE;
  536. }
  537. void CToolTipEx::SetGdiplusBitmap(Gdiplus::Bitmap *gdiplusBitmap)
  538. {
  539. delete m_imageViewer.m_pGdiplusBitmap;
  540. m_imageViewer.m_pGdiplusBitmap = NULL;
  541. m_imageViewer.m_pGdiplusBitmap = gdiplusBitmap;
  542. m_imageViewer.UpdateBitmapSize();
  543. Invalidate();
  544. }
  545. void CToolTipEx::OnSize(UINT nType, int cx, int cy)
  546. {
  547. CWnd::OnSize(nType, cx, cy);
  548. if(::IsWindow(m_RichEdit.GetSafeHwnd()) == FALSE)
  549. {
  550. return ;
  551. }
  552. MoveControls();
  553. }
  554. void CToolTipEx::MoveControls()
  555. {
  556. CRect cr;
  557. GetClientRect(cr);
  558. cr.DeflateRect(0, 0, 0, m_DittoWindow.m_dpi.ScaleY(21));
  559. m_RichEdit.MoveWindow(cr);
  560. m_imageViewer.MoveWindow(cr);
  561. m_optionsButton.MoveWindow(cr.left, cr.bottom + m_DittoWindow.m_dpi.ScaleY(2), m_DittoWindow.m_dpi.ScaleX(17), m_DittoWindow.m_dpi.ScaleY(17));
  562. m_clipDataStatic.MoveWindow(cr.left + m_DittoWindow.m_dpi.ScaleX(19), cr.bottom + m_DittoWindow.m_dpi.ScaleY(2), cr.Width() - cr.left + m_DittoWindow.m_dpi.ScaleX(19), m_DittoWindow.m_dpi.ScaleY(17));
  563. this->Invalidate();
  564. if (m_saveWindowLockout == false)
  565. {
  566. SetTimer(SAVE_SIZE, 250, NULL);
  567. }
  568. }
  569. BOOL CToolTipEx::IsCursorInToolTip()
  570. {
  571. CRect cr;
  572. GetWindowRect(cr);
  573. CPoint cursorPos;
  574. GetCursorPos(&cursorPos);
  575. return cr.PtInRect(cursorPos);
  576. }
  577. void CToolTipEx::SetRTFText(const char *pRTF)
  578. {
  579. m_RichEdit.SetRTF(pRTF);
  580. m_csRTF = pRTF;
  581. m_RichEdit.SetSel(0, 0);
  582. HighlightSearchText();
  583. }
  584. //void CToolTipEx::SetRTFText(const CString &csRTF)
  585. //{
  586. // m_RichEdit.SetRTF(csRTF);
  587. // m_csRTF = csRTF;
  588. //}
  589. void CToolTipEx::SetToolTipText(const CString &csText)
  590. {
  591. m_csText = csText;
  592. m_RichEdit.SetFont(&m_Font);
  593. m_RichEdit.SetText(csText);
  594. m_RichEdit.SetSel(0, 0);
  595. CHARFORMAT cfNew;
  596. cfNew.cbSize = sizeof(CHARFORMAT);
  597. cfNew.dwMask = CFM_COLOR;
  598. cfNew.dwEffects = CFM_COLOR;
  599. cfNew.dwEffects &= ~CFE_AUTOCOLOR;
  600. cfNew.crTextColor = g_Opt.m_Theme.DescriptionWindowText();
  601. m_RichEdit.SetDefaultCharFormat(cfNew);
  602. HighlightSearchText();
  603. }
  604. void CToolTipEx::HighlightSearchText()
  605. {
  606. if (m_searchText.GetLength() <= 0)
  607. return;
  608. FINDTEXTEX ft;
  609. long n = -1;
  610. ft.lpstrText = m_searchText;
  611. ft.chrg.cpMin = 0;
  612. ft.chrg.cpMax = -1;
  613. CHARFORMAT cf;
  614. cf.cbSize = sizeof(cf);
  615. cf.dwMask = CFM_COLOR;
  616. cf.dwEffects = CFE_BOLD | ~CFE_AUTOCOLOR;
  617. cf.crTextColor = RGB(255, 0, 0);
  618. do
  619. {
  620. ft.chrg.cpMin = n+1;
  621. n = m_RichEdit.FindText(FR_DOWN, &ft);
  622. if (n != -1)
  623. {
  624. m_RichEdit.SetSel(ft.chrgText);
  625. m_RichEdit.SetSelectionCharFormat(cf);
  626. }
  627. } while (n != -1);
  628. m_RichEdit.SetSel(0, 0);
  629. }
  630. void CToolTipEx::DoSearch()
  631. {
  632. if (m_searchText.GetLength() <= 0)
  633. return;
  634. FINDTEXTEX ft;
  635. long n = -1;
  636. ft.lpstrText = m_searchText;
  637. long start;
  638. long end;
  639. m_RichEdit.GetSel(start, end);
  640. ft.chrg.cpMin = end;
  641. ft.chrg.cpMax = -1;
  642. int searchDirection = FR_DOWN;
  643. if (GetKeyState(VK_SHIFT) & 0x8000)
  644. {
  645. searchDirection = 0;
  646. ft.chrg.cpMin = start;
  647. }
  648. n = m_RichEdit.FindText(searchDirection, &ft);
  649. if (n != -1)
  650. {
  651. m_RichEdit.SetSel(ft.chrgText);
  652. }
  653. else
  654. {
  655. if (searchDirection == 0)
  656. {
  657. ft.chrg.cpMin = m_RichEdit.GetTextLength();
  658. }
  659. else
  660. {
  661. ft.chrg.cpMin = 0;
  662. }
  663. ft.chrg.cpMax = -1;
  664. n = m_RichEdit.FindText(searchDirection, &ft);
  665. if (n != -1)
  666. {
  667. m_RichEdit.SetSel(ft.chrgText);
  668. }
  669. }
  670. }
  671. void CToolTipEx::OnActivate(UINT nState, CWnd *pWndOther, BOOL bMinimized)
  672. {
  673. CWnd::OnActivate(nState, pWndOther, bMinimized);
  674. if (nState == WA_INACTIVE)
  675. {
  676. if(m_pNotifyWnd)
  677. {
  678. m_pNotifyWnd->PostMessage(NM_INACTIVE_TOOLTIPWND, 0, 0);
  679. }
  680. }
  681. }
  682. void CToolTipEx::OnTimer(UINT_PTR nIDEvent)
  683. {
  684. switch(nIDEvent)
  685. {
  686. case HIDE_WINDOW_TIMER:
  687. Hide();
  688. PostMessage(WM_DESTROY, 0, 0);
  689. break;
  690. case SAVE_SIZE:
  691. SaveWindowSize();
  692. KillTimer(SAVE_SIZE);
  693. break;
  694. case TIMER_BUTTON_UP:
  695. {
  696. if ((GetKeyState(VK_LBUTTON) & 0x100) == 0)
  697. {
  698. m_DittoWindow.DoNcLButtonUp(this, 0, CPoint(0, 0));
  699. KillTimer(TIMER_BUTTON_UP);
  700. }
  701. break;
  702. }
  703. case TIMER_AUTO_MAX:
  704. {
  705. if (m_DittoWindow.m_bMinimized)
  706. {
  707. CPoint cp;
  708. GetCursorPos(&cp);
  709. UINT nHitTest = (UINT)OnNcHitTest(cp);
  710. ScreenToClient(&cp);
  711. if (nHitTest == HTCAPTION)
  712. {
  713. if (m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  714. {
  715. if (m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  716. {
  717. m_DittoWindow.MinMaxWindow(this, FORCE_MAX);
  718. }
  719. }
  720. }
  721. }
  722. KillTimer(TIMER_AUTO_MAX);
  723. m_bMaxSetTimer = false;
  724. }
  725. }
  726. CWnd::OnTimer(nIDEvent);
  727. }
  728. void CToolTipEx::OnNcPaint()
  729. {
  730. m_DittoWindow.DoNcPaint(this);
  731. }
  732. void CToolTipEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  733. {
  734. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  735. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  736. }
  737. HITTEST_RET CToolTipEx::OnNcHitTest(CPoint point)
  738. {
  739. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  740. if(Ret == -1)
  741. return CWnd::OnNcHitTest(point);
  742. return Ret;
  743. }
  744. void CToolTipEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  745. {
  746. int buttonPressed = m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  747. if (buttonPressed != 0)
  748. {
  749. SetTimer(TIMER_BUTTON_UP, 100, NULL);
  750. }
  751. CWnd::OnNcLButtonDown(nHitTest, point);
  752. }
  753. void CToolTipEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  754. {
  755. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  756. switch(lRet)
  757. {
  758. case BUTTON_CLOSE:
  759. Hide();
  760. break;
  761. case BUTTON_CHEVRON:
  762. m_DittoWindow.MinMaxWindow(this, SWAP_MIN_MAX);
  763. OnNcPaint();
  764. break;
  765. }
  766. KillTimer(TIMER_BUTTON_UP);
  767. CWnd::OnNcLButtonUp(nHitTest, point);
  768. }
  769. void CToolTipEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  770. {
  771. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  772. if ((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  773. {
  774. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_DittoWindow.m_TimeMinimized;
  775. if (sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  776. {
  777. SetTimer(TIMER_AUTO_MAX, CGetSetOptions::GetTimeBeforeExpandWindow(), NULL);
  778. m_bMaxSetTimer = true;
  779. }
  780. }
  781. CWnd::OnNcMouseMove(nHitTest, point);
  782. }
  783. void CToolTipEx::OnOptions()
  784. {
  785. POINT pp;
  786. CMenu cmPopUp;
  787. CMenu *cmSubMenu = NULL;
  788. GetCursorPos(&pp);
  789. if(cmPopUp.LoadMenu(IDR_DESC_OPTIONS_MENU) != 0)
  790. {
  791. cmSubMenu = cmPopUp.GetSubMenu(0);
  792. if(!cmSubMenu)
  793. {
  794. return ;
  795. }
  796. GetCursorPos(&pp);
  797. //theApp.m_Language.UpdateRightClickMenu(cmSubMenu);
  798. if(CGetSetOptions::GetRememberDescPos())
  799. cmSubMenu->CheckMenuItem(ID_FIRST_REMEMBERWINDOWPOSITION, MF_CHECKED);
  800. if(CGetSetOptions::GetSizeDescWindowToContent())
  801. cmSubMenu->CheckMenuItem(ID_FIRST_SIZEWINDOWTOCONTENT, MF_CHECKED);
  802. if(CGetSetOptions::GetScaleImagesToDescWindow())
  803. cmSubMenu->CheckMenuItem(ID_FIRST_SCALEIMAGESTOFITWINDOW, MF_CHECKED);
  804. if (CGetSetOptions::GetMouseClickHidesDescription())
  805. cmSubMenu->CheckMenuItem(ID_FIRST_HIDEDESCRIPTIONWINDOWONM, MF_CHECKED);
  806. if (CGetSetOptions::GetWrapDescriptionText())
  807. cmSubMenu->CheckMenuItem(ID_FIRST_WRAPTEXT, MF_CHECKED);
  808. if (m_showPersistant)
  809. cmSubMenu->CheckMenuItem(ID_FIRST_ALWAYSONTOP, MF_CHECKED);
  810. UpdateMenuShortCut(cmSubMenu, ID_FIRST_WRAPTEXT, ActionEnums::TOGGLE_DESCRIPTION_WORD_WRAP);
  811. UpdateMenuShortCut(cmSubMenu, ID_FIRST_ALWAYSONTOP, ActionEnums::TOGGLESHOWPERSISTANT);
  812. cmSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, pp.x, pp.y, this, NULL);
  813. }
  814. }
  815. void CToolTipEx::UpdateMenuShortCut(CMenu *subMenu, int id, DWORD action)
  816. {
  817. if (m_pToolTipActions != NULL)
  818. {
  819. CString cs;
  820. subMenu->GetMenuString(id, cs, MF_BYCOMMAND);
  821. CString shortcutText = m_pToolTipActions->GetCmdKeyText(action);
  822. if (shortcutText != _T("") &&
  823. cs.Find("\t" + shortcutText) < 0)
  824. {
  825. cs += "\t";
  826. cs += shortcutText;
  827. subMenu->ModifyMenu(id, MF_BYCOMMAND, id, cs);
  828. }
  829. }
  830. }
  831. void CToolTipEx::OnRememberwindowposition()
  832. {
  833. CGetSetOptions::SetRememberDescPos(!CGetSetOptions::GetRememberDescPos());
  834. }
  835. void CToolTipEx::OnSizewindowtocontent()
  836. {
  837. CGetSetOptions::SetSizeDescWindowToContent(!CGetSetOptions::GetSizeDescWindowToContent());
  838. CRect rect;
  839. this->GetWindowRect(&rect);
  840. Show(rect.TopLeft());
  841. }
  842. void CToolTipEx::OnScaleimagestofitwindow()
  843. {
  844. CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
  845. m_imageViewer.UpdateBitmapSize();
  846. Invalidate();
  847. }
  848. void CToolTipEx::OnRButtonDown(UINT nFlags, CPoint point)
  849. {
  850. OnOptions();
  851. CWnd::OnRButtonDown(nFlags, point);
  852. }
  853. void CToolTipEx::OnSetFocus(CWnd* pOldWnd)
  854. {
  855. CWnd::OnSetFocus(pOldWnd);
  856. m_RichEdit.SetFocus();
  857. }
  858. void CToolTipEx::OnPaint()
  859. {
  860. CPaintDC dc(this); // device context for painting
  861. CRect rect;
  862. GetClientRect(rect);
  863. CBrush Brush, *pOldBrush;
  864. Brush.CreateSolidBrush(g_Opt.m_Theme.DescriptionWindowBG());
  865. pOldBrush = dc.SelectObject(&Brush);
  866. dc.FillRect(&rect, &Brush);
  867. // Cleanup
  868. dc.SelectObject(pOldBrush);
  869. }
  870. void CToolTipEx::OnFirstHidedescriptionwindowonm()
  871. {
  872. CGetSetOptions::SetMouseClickHidesDescription(!CGetSetOptions::GetMouseClickHidesDescription());
  873. }
  874. bool CToolTipEx::ToggleWordWrap()
  875. {
  876. bool didWordWrap = false;
  877. if (m_RichEdit.IsWindowVisible())
  878. {
  879. OnFirstWraptext();
  880. didWordWrap = true;
  881. }
  882. return didWordWrap;
  883. }
  884. void CToolTipEx::OnFirstWraptext()
  885. {
  886. CGetSetOptions::SetWrapDescriptionText(!CGetSetOptions::GetWrapDescriptionText());
  887. ApplyWordWrap();
  888. }
  889. void CToolTipEx::ApplyWordWrap()
  890. {
  891. if (CGetSetOptions::GetWrapDescriptionText())
  892. {
  893. m_RichEdit.SetTargetDevice(NULL, 0);
  894. }
  895. else
  896. {
  897. m_RichEdit.SetTargetDevice(NULL, 1);
  898. }
  899. }
  900. void CToolTipEx::HideWindowInXMilliSeconds(long lms)
  901. {
  902. SetTimer(HIDE_WINDOW_TIMER, lms, NULL);
  903. }
  904. void CToolTipEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  905. {
  906. CWnd::OnWindowPosChanging(lpwndpos);
  907. //m_DittoWindow.SnapToEdge(this, lpwndpos);
  908. }
  909. void CToolTipEx::OnFirstAlwaysontop()
  910. {
  911. m_showPersistant = !m_showPersistant;
  912. if (m_showPersistant)
  913. {
  914. m_DittoWindow.m_customWindowTitle = _T("[Always on top]");
  915. m_DittoWindow.m_useCustomWindowTitle = true;
  916. ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
  917. }
  918. else
  919. {
  920. m_DittoWindow.m_customWindowTitle = _T("");
  921. m_DittoWindow.m_useCustomWindowTitle = true;
  922. }
  923. ::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  924. }
  925. BOOL CToolTipEx::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  926. {
  927. switch (((LPNMHDR)lParam)->code)
  928. {
  929. case EN_LINK:
  930. {
  931. ENLINK *enLinkInfo = (ENLINK *)lParam; // pointer to a ENLINK structure
  932. if (enLinkInfo->msg == WM_LBUTTONUP)
  933. {
  934. CString s;
  935. m_RichEdit.GetTextRange(enLinkInfo->chrg.cpMin, enLinkInfo->chrg.cpMax, s);
  936. CHyperLink::GotoURL(s, SW_SHOW);
  937. }
  938. }
  939. break;
  940. }
  941. return CWnd::OnNotify(wParam, lParam, pResult);
  942. }
  943. void CToolTipEx::OnEnMsgfilterRichedit21(NMHDR *pNMHDR, LRESULT *pResult)
  944. {
  945. MSGFILTER *pMsgFilter = reinterpret_cast<MSGFILTER *>(pNMHDR);
  946. if (pMsgFilter != NULL)
  947. {
  948. switch (pMsgFilter->msg)
  949. {
  950. //handle click on the rich text control when it doesn't have focus
  951. //set focus so the first click is handled by the rich text control
  952. case WM_MOUSEACTIVATE:
  953. m_RichEdit.SetFocus();
  954. break;
  955. }
  956. }
  957. *pResult = 0;
  958. }
  959. LRESULT CToolTipEx::OnDpiChanged(WPARAM wParam, LPARAM lParam)
  960. {
  961. int dpi = HIWORD(wParam);
  962. m_DittoWindow.OnDpiChanged(this, dpi);
  963. RECT* const prcNewWindow = (RECT*)lParam;
  964. SetWindowPos(NULL,
  965. prcNewWindow->left,
  966. prcNewWindow->top,
  967. prcNewWindow->right - prcNewWindow->left,
  968. prcNewWindow->bottom - prcNewWindow->top,
  969. SWP_NOZORDER | SWP_NOACTIVATE);
  970. m_optionsButton.Reset();
  971. m_optionsButton.LoadStdImageDPI(m_DittoWindow.m_dpi.GetDPIX(), IDB_COG_16_16, IDB_COG_20_20, IDB_COG_24_24, cog_28, IDB_COG_32_32, _T("PNG"));
  972. m_clipDataFont.Detach();
  973. m_clipDataFont.CreateFont(-m_DittoWindow.m_dpi.PointsToPixels(8), 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 3, 2, 1, 34, _T("Segoe UI"));
  974. m_clipDataStatic.SetFont(&m_clipDataFont);
  975. m_clipDataStatic.SetBkColor(g_Opt.m_Theme.DescriptionWindowBG());
  976. m_clipDataStatic.SetTextColor(RGB(80, 80, 80));
  977. this->MoveControls();
  978. this->Invalidate();
  979. this->UpdateWindow();
  980. return TRUE;
  981. }
  982. void CToolTipEx::OnMoving(UINT fwSide, LPRECT pRect)
  983. {
  984. CWnd::OnMoving(fwSide, pRect);
  985. m_snap.OnSnapMoving(m_hWnd, pRect);
  986. // TODO: Add your message handler code here
  987. }
  988. void CToolTipEx::OnEnterSizeMove()
  989. {
  990. m_snap.OnSnapEnterSizeMove(m_hWnd);
  991. CWnd::OnEnterSizeMove();
  992. }