ToolTipEx.cpp 26 KB

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