ToolTipEx.cpp 26 KB

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