ToolTipEx.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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 DELETE_BITMAP if(m_imageViewer.m_pBitmap) \
  14. { \
  15. m_imageViewer.m_pBitmap->DeleteObject(); \
  16. delete m_imageViewer.m_pBitmap; \
  17. m_imageViewer.m_pBitmap = NULL; \
  18. }
  19. #define HIDE_WINDOW_TIMER 1
  20. #define SAVE_SIZE 2
  21. #define TIMER_BUTTON_UP 3
  22. #define TIMER_AUTO_MAX 4
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CToolTipEx
  25. CToolTipEx::CToolTipEx(): m_dwTextStyle(DT_EXPANDTABS | DT_EXTERNALLEADING |
  26. DT_NOPREFIX | DT_WORDBREAK), m_rectMargin(2, 2, 3, 3),
  27. m_pNotifyWnd(NULL), m_clipId(0), m_clipRow(-1)
  28. {
  29. m_showPersistant = false;
  30. m_pToolTipActions = NULL;
  31. m_bMaxSetTimer = false;
  32. m_lDelayMaxSeconds = 2;
  33. }
  34. CToolTipEx::~CToolTipEx()
  35. {
  36. DELETE_BITMAP
  37. m_Font.DeleteObject();
  38. m_clipDataFont.DeleteObject();
  39. }
  40. BEGIN_MESSAGE_MAP(CToolTipEx, CWnd)
  41. //{{AFX_MSG_MAP(CToolTipEx)
  42. ON_WM_PAINT()
  43. ON_WM_SIZE()
  44. ON_WM_NCHITTEST()
  45. ON_WM_ACTIVATE()
  46. ON_WM_TIMER()
  47. ON_WM_NCLBUTTONDBLCLK()
  48. ON_WM_NCPAINT()
  49. ON_WM_NCCALCSIZE()
  50. ON_WM_NCLBUTTONDOWN()
  51. ON_WM_NCMOUSEMOVE()
  52. ON_WM_NCLBUTTONUP()
  53. ON_WM_ERASEBKGND()
  54. ON_COMMAND(ID_FIRST_REMEMBERWINDOWPOSITION, &CToolTipEx::OnRememberwindowposition)
  55. ON_COMMAND(ID_FIRST_SIZEWINDOWTOCONTENT, &CToolTipEx::OnSizewindowtocontent)
  56. ON_COMMAND(ID_FIRST_SCALEIMAGESTOFITWINDOW, &CToolTipEx::OnScaleimagestofitwindow)
  57. ON_COMMAND(2, OnOptions)
  58. ON_WM_RBUTTONDOWN()
  59. ON_WM_SETFOCUS()
  60. ON_COMMAND(ID_FIRST_HIDEDESCRIPTIONWINDOWONM, &CToolTipEx::OnFirstHidedescriptionwindowonm)
  61. ON_COMMAND(ID_FIRST_WRAPTEXT, &CToolTipEx::OnFirstWraptext)
  62. ON_WM_WINDOWPOSCHANGING()
  63. ON_COMMAND(ID_FIRST_ALWAYSONTOP, &CToolTipEx::OnFirstAlwaysontop)
  64. END_MESSAGE_MAP()
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CToolTipEx message handlers
  67. BOOL CToolTipEx::Create(CWnd *pParentWnd)
  68. {
  69. m_saveWindowLockout = true;
  70. // Get the class name and create the window
  71. CString szClassName = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  72. // Create the window - just don't show it yet.
  73. if( !CWnd::CreateEx(0, szClassName, _T(""), WS_POPUP,
  74. 0, 0, 0, 0, pParentWnd->GetSafeHwnd(), 0, NULL))
  75. {
  76. return FALSE;
  77. }
  78. //CString szClassName2 = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS, LoadCursor(NULL, IDC_ARROW));
  79. //BOOL b = m_imageViewer.Create(_T(""), szClassName2, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, CRect(0, 0, 0, 0), this, 3);
  80. m_imageViewer.Create(this);
  81. m_DittoWindow.DoCreate(this);
  82. m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  83. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true, g_Opt.m_Theme.GetCaptionSize(), g_Opt.m_Theme.GetCaptionFontSize());
  84. m_DittoWindow.m_bDrawMaximize = false;
  85. m_DittoWindow.m_bDrawMinimize = false;
  86. m_DittoWindow.m_bDrawChevron = true;
  87. m_DittoWindow.m_sendWMClose = false;
  88. m_RichEdit.Create(_T(""), _T(""), WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  89. WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_NOHIDESEL |
  90. ES_AUTOHSCROLL, CRect(10, 10, 100, 200), this, 1);
  91. m_RichEdit.SetReadOnly();
  92. m_RichEdit.SetBackgroundColor(FALSE, g_Opt.m_Theme.DescriptionWindowBG());
  93. ApplyWordWrap();
  94. SetLogFont(GetSystemToolTipFont(), FALSE);
  95. m_optionsButton.Create(NULL, WS_CHILD | BS_OWNERDRAW | WS_TABSTOP, CRect(0, 0, 0, 0), this, 2);
  96. m_optionsButton.LoadStdImageDPI(IDB_COG_16_16, IDB_COG_20_20, IDB_COG_24_24, cog_28, IDB_COG_32_32, _T("PNG"));
  97. m_optionsButton.SetToolTipText(theApp.m_Language.GetString(_T("DescriptionOptionsTooltip"), _T("Description Options")));
  98. m_optionsButton.ShowWindow(SW_SHOW);
  99. m_clipDataStatic.Create(_T("some text"), WS_CHILD | WS_VISIBLE | SS_SIMPLE, CRect(0, 0, 0, 0), this, 3);
  100. m_clipDataFont.CreateFont(-theApp.m_metrics.PointsToPixels(8), 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 3, 2, 1, 34, _T("Segoe UI"));
  101. m_clipDataStatic.SetFont(&m_clipDataFont);
  102. m_clipDataStatic.SetBkColor(g_Opt.m_Theme.DescriptionWindowBG());
  103. m_clipDataStatic.SetTextColor(RGB(80, 80, 80));
  104. m_saveWindowLockout = false;
  105. return TRUE;
  106. }
  107. BOOL CToolTipEx::Show(CPoint point)
  108. {
  109. m_reducedWindowSize = false;
  110. if(m_imageViewer.m_pBitmap)
  111. {
  112. m_RichEdit.ShowWindow(SW_HIDE);
  113. m_imageViewer.ShowWindow(SW_SHOW);
  114. m_imageViewer.UpdateBitmapSize();
  115. }
  116. else
  117. {
  118. m_RichEdit.ShowWindow(SW_SHOW);
  119. m_imageViewer.ShowWindow(SW_HIDE);
  120. }
  121. CRect rect;
  122. if(CGetSetOptions::GetSizeDescWindowToContent() == FALSE)
  123. {
  124. rect.left = point.x;
  125. rect.top = point.y;
  126. CSize size;
  127. CGetSetOptions::GetDescWndSize(size);
  128. rect.right = rect.left + size.cx;
  129. rect.bottom = rect.top + size.cy;
  130. EnsureWindowVisible(&rect);
  131. }
  132. else
  133. {
  134. rect = GetBoundsRect();
  135. //account for the scroll bars
  136. rect.right += 20;
  137. rect.bottom += 20;
  138. if (m_imageViewer.m_pBitmap)
  139. {
  140. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap) + ::GetSystemMetrics(SM_CXVSCROLL);
  141. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap) + ::GetSystemMetrics(SM_CYHSCROLL);
  142. rect.right = rect.left + nWidth;
  143. rect.bottom = rect.top + nHeight;
  144. }
  145. else if(m_csRTF != "")
  146. {
  147. //if showing rtf then increase the size because
  148. //rtf will probably draw bigger
  149. long lNewWidth = (long)rect.Width() + (long)(rect.Width() *1.5);
  150. rect.right = rect.left + lNewWidth;
  151. long lNewHeight = (long)rect.Height() + (long)(rect.Height() *1.5);
  152. rect.bottom = rect.top + lNewHeight;
  153. }
  154. rect.right += CAPTION_BORDER * 2;
  155. rect.bottom += CAPTION_BORDER * 2;
  156. CRect rcScreen;
  157. ClientToScreen(rect);
  158. CRect cr(point, point);
  159. int nMonitor = GetMonitorFromRect(&cr);
  160. GetMonitorRect(nMonitor, &rcScreen);
  161. //ensure that we don't go outside the screen
  162. if(point.x < 0)
  163. {
  164. point.x = 5;
  165. m_reducedWindowSize = true;
  166. }
  167. if(point.y < 0)
  168. {
  169. point.y = 5;
  170. m_reducedWindowSize = true;
  171. }
  172. rcScreen.DeflateRect(0, 0, 5, 5);
  173. long lWidth = rect.Width();
  174. long lHeight = rect.Height();
  175. rect.left = point.x;
  176. rect.top = point.y;
  177. rect.right = rect.left + lWidth;
  178. rect.bottom = rect.top + lHeight;
  179. if (rect.right > rcScreen.right)
  180. {
  181. rect.right = rcScreen.right;
  182. m_reducedWindowSize = true;
  183. }
  184. if (rect.bottom > rcScreen.bottom)
  185. {
  186. rect.bottom = rcScreen.bottom;
  187. m_reducedWindowSize = true;
  188. }
  189. }
  190. m_clipDataStatic.SetWindowText(m_clipData);
  191. if (m_DittoWindow.m_bMinimized)
  192. {
  193. //m_DittoWindow.MinMaxWindow(this, FORCE_MAX);
  194. m_DittoWindow.m_bMinimized = false;
  195. }
  196. m_saveWindowLockout = true;
  197. MoveWindow(rect);
  198. ShowWindow(SW_SHOWNA);
  199. this->Invalidate();
  200. this->UpdateWindow();
  201. m_saveWindowLockout = false;
  202. return TRUE;
  203. }
  204. void CToolTipEx::GetWindowRectEx(LPRECT lpRect)
  205. {
  206. if (m_DittoWindow.m_bMinimized)
  207. {
  208. *lpRect = m_DittoWindow.m_crFullSizeWindow;
  209. return;
  210. }
  211. CWnd::GetWindowRect(lpRect);
  212. }
  213. BOOL CToolTipEx::Hide()
  214. {
  215. DELETE_BITMAP
  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. CGetSetOptions::SetDescWndSize(rect.Size());
  249. CGetSetOptions::SetDescWndPoint(rect.TopLeft());
  250. OutputDebugString(_T("Saving tooltip size"));
  251. }
  252. }
  253. void CToolTipEx::PostNcDestroy()
  254. {
  255. CWnd::PostNcDestroy();
  256. delete this;
  257. }
  258. BOOL CToolTipEx::PreTranslateMessage(MSG *pMsg)
  259. {
  260. m_DittoWindow.DoPreTranslateMessage(pMsg);
  261. switch(pMsg->message)
  262. {
  263. case WM_KEYDOWN:
  264. switch(pMsg->wParam)
  265. {
  266. case 'C':
  267. if(GetKeyState(VK_CONTROL) &0x8000)
  268. {
  269. m_RichEdit.Copy();
  270. }
  271. break;
  272. }
  273. break;
  274. case WM_RBUTTONDOWN:
  275. {
  276. if (m_RichEdit.m_hWnd == GetFocus()->m_hWnd ||
  277. m_imageViewer.m_hWnd == GetFocus()->m_hWnd)
  278. {
  279. OnOptions();
  280. return TRUE;
  281. }
  282. }
  283. break;
  284. }
  285. if (m_pToolTipActions != NULL)
  286. {
  287. CAccel a;
  288. if (m_pToolTipActions->OnMsg(pMsg, a))
  289. {
  290. switch (a.Cmd)
  291. {
  292. case ActionEnums::CLOSEWINDOW:
  293. /*if (this->m_showPersistant &&
  294. m_DittoWindow.m_bMinimized == false)
  295. {
  296. m_DittoWindow.MinMaxWindow(this, FORCE_MIN);
  297. theApp.m_activeWnd.ReleaseFocus();
  298. return TRUE;
  299. }*/
  300. break;
  301. }
  302. }
  303. }
  304. return CWnd::PreTranslateMessage(pMsg);
  305. }
  306. BOOL CToolTipEx::OnMsg(MSG *pMsg)
  307. {
  308. if(FALSE == IsWindowVisible())
  309. {
  310. return FALSE;
  311. }
  312. switch(pMsg->message)
  313. {
  314. case WM_WINDOWPOSCHANGING:
  315. case WM_LBUTTONDOWN:
  316. {
  317. if (m_showPersistant == false)
  318. {
  319. if (CGetSetOptions::GetMouseClickHidesDescription())
  320. {
  321. if (!IsCursorInToolTip())
  322. {
  323. Hide();
  324. }
  325. }
  326. }
  327. }
  328. break;
  329. case WM_KEYDOWN:
  330. {
  331. WPARAM vk = pMsg->wParam;
  332. if(vk == VK_TAB)
  333. {
  334. m_RichEdit.SetFocus();
  335. return TRUE;
  336. }
  337. else if (vk == VK_CONTROL || vk == VK_SHIFT)
  338. {
  339. return FALSE;
  340. }
  341. else if (vk == VK_UP)
  342. {
  343. return FALSE;
  344. }
  345. else if (vk == VK_DOWN)
  346. {
  347. return FALSE;
  348. }
  349. if (m_pToolTipActions != NULL)
  350. {
  351. if (m_pToolTipActions->ContainsKey((int)vk))
  352. {
  353. return FALSE;
  354. }
  355. }
  356. if (m_showPersistant == false)
  357. {
  358. Hide();
  359. }
  360. break;
  361. }
  362. case WM_LBUTTONDBLCLK:
  363. case WM_RBUTTONDBLCLK:
  364. case WM_MBUTTONDOWN:
  365. case WM_MBUTTONDBLCLK:
  366. case WM_NCLBUTTONDOWN:
  367. case WM_NCLBUTTONDBLCLK:
  368. case WM_NCRBUTTONDOWN:
  369. case WM_NCRBUTTONDBLCLK:
  370. case WM_NCMBUTTONDOWN:
  371. case WM_NCMBUTTONDBLCLK:
  372. {
  373. if (m_showPersistant == false)
  374. {
  375. Hide();
  376. }
  377. break;
  378. }
  379. case WM_MOUSEWHEEL:
  380. {
  381. if (m_imageViewer.m_pBitmap)
  382. {
  383. m_imageViewer.PostMessageW(pMsg->message, pMsg->wParam, pMsg->lParam);
  384. return TRUE;
  385. }
  386. else
  387. {
  388. m_RichEdit.PostMessageW(pMsg->message, pMsg->wParam, pMsg->lParam);
  389. return TRUE;
  390. }
  391. }
  392. break;
  393. }
  394. return FALSE;
  395. }
  396. CRect CToolTipEx::GetBoundsRect()
  397. {
  398. DWORD d = GetTickCount();
  399. CWindowDC dc(NULL);
  400. int nLineWidth = 0;
  401. CRect rect(0, 0, 0, 0);
  402. if(nLineWidth == 0)
  403. {
  404. // Count the number of lines of text
  405. int nStart = 0;
  406. INT nNumLines = 0;
  407. int longestLength = 0;
  408. CString longestString;
  409. do
  410. {
  411. int newStart = m_csText.Find(_T("\n"), nStart);
  412. if (newStart < 0)
  413. {
  414. int length = m_csText.GetLength() - nStart;
  415. if (length > longestLength)
  416. {
  417. longestString = m_csText.Mid(nStart, length);
  418. longestLength = length;
  419. }
  420. break;
  421. }
  422. int length = newStart - nStart;
  423. if(length > longestLength)
  424. {
  425. longestString = m_csText.Mid(nStart, length);
  426. longestLength = length;
  427. }
  428. nNumLines++;
  429. nStart = newStart + 1;
  430. }
  431. while(nStart >= 0 && nNumLines < 100);
  432. CFont *pOldFont = (CFont*)dc.SelectObject((CFont*)&m_Font);
  433. CSize size = dc.GetTextExtent(longestString);
  434. dc.SelectObject(pOldFont);
  435. rect.right = size.cx;
  436. rect.bottom = size.cy * nNumLines;
  437. }
  438. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  439. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  440. if(m_imageViewer.m_pBitmap)
  441. {
  442. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap);
  443. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap);
  444. rect.bottom += nHeight;
  445. if((rect.left + nWidth) > rect.right)
  446. {
  447. rect.right = rect.left + nWidth;
  448. }
  449. }
  450. DWORD diff = GetTickCount() - d;
  451. if (diff > 10)
  452. {
  453. Log(StrF(_T("Size To Content: %d\n"), diff));
  454. }
  455. return rect;
  456. }
  457. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  458. {
  459. CString strReturn;
  460. LPCTSTR pstrStart = ref.LockBuffer();
  461. LPCTSTR pstrBuffer = pstrStart;
  462. int nCurrent = 0;
  463. int nStart = 0;
  464. int nEnd = 0;
  465. int nOldStart = 0;
  466. while(nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  467. {
  468. if(*pstrBuffer == ch)
  469. {
  470. nOldStart = nStart;
  471. nStart = nEnd + 1;
  472. nCurrent++;
  473. }
  474. nEnd++;
  475. pstrBuffer++;
  476. }
  477. // May have reached the end of the string
  478. if(*pstrBuffer == _T('\0'))
  479. {
  480. nOldStart = nStart;
  481. nEnd++;
  482. }
  483. ref.UnlockBuffer();
  484. if(nCurrent < nIndex)
  485. {
  486. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  487. return strReturn;
  488. }
  489. return ref.Mid(nOldStart, nEnd - nOldStart - 1);
  490. }
  491. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  492. {
  493. static LOGFONT LogFont;
  494. NONCLIENTMETRICS ncm;
  495. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  496. if(!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
  497. &ncm, 0))
  498. {
  499. return FALSE;
  500. }
  501. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  502. return &LogFont;
  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. // Create the actual font object
  515. m_Font.DeleteObject();
  516. m_Font.CreateFontIndirect(&LogFont);
  517. if(bRedraw && ::IsWindow(GetSafeHwnd()))
  518. {
  519. Invalidate();
  520. }
  521. return TRUE;
  522. }
  523. void CToolTipEx::SetBitmap(CBitmap *pBitmap)
  524. {
  525. DELETE_BITMAP
  526. m_imageViewer.m_pBitmap = pBitmap;
  527. m_imageViewer.UpdateBitmapSize();
  528. if (m_imageViewer.m_pBitmap != NULL)
  529. {
  530. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap);
  531. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap);
  532. Invalidate();
  533. }
  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. }