ToolTipEx.cpp 25 KB

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