ToolTipEx.cpp 28 KB

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