ToolTipEx.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. case VK_F3:
  273. {
  274. DoSearch();
  275. }
  276. break;
  277. }
  278. break;
  279. case WM_RBUTTONDOWN:
  280. {
  281. if (m_RichEdit.m_hWnd == GetFocus()->m_hWnd ||
  282. m_imageViewer.m_hWnd == GetFocus()->m_hWnd)
  283. {
  284. OnOptions();
  285. return TRUE;
  286. }
  287. }
  288. break;
  289. }
  290. if (m_pToolTipActions != NULL)
  291. {
  292. CAccel a;
  293. if (m_pToolTipActions->OnMsg(pMsg, a))
  294. {
  295. switch (a.Cmd)
  296. {
  297. case ActionEnums::CLOSEWINDOW:
  298. /*if (this->m_showPersistant &&
  299. m_DittoWindow.m_bMinimized == false)
  300. {
  301. m_DittoWindow.MinMaxWindow(this, FORCE_MIN);
  302. theApp.m_activeWnd.ReleaseFocus();
  303. return TRUE;
  304. }*/
  305. break;
  306. }
  307. }
  308. }
  309. return CWnd::PreTranslateMessage(pMsg);
  310. }
  311. BOOL CToolTipEx::OnMsg(MSG *pMsg)
  312. {
  313. if(FALSE == IsWindowVisible())
  314. {
  315. return FALSE;
  316. }
  317. switch(pMsg->message)
  318. {
  319. case WM_WINDOWPOSCHANGING:
  320. case WM_LBUTTONDOWN:
  321. {
  322. if (m_showPersistant == false)
  323. {
  324. if (CGetSetOptions::GetMouseClickHidesDescription())
  325. {
  326. if (!IsCursorInToolTip())
  327. {
  328. Hide();
  329. }
  330. }
  331. }
  332. }
  333. break;
  334. case WM_KEYDOWN:
  335. {
  336. WPARAM vk = pMsg->wParam;
  337. if(vk == VK_TAB)
  338. {
  339. m_RichEdit.SetFocus();
  340. return TRUE;
  341. }
  342. else if (vk == VK_CONTROL || vk == VK_SHIFT)
  343. {
  344. return FALSE;
  345. }
  346. else if (vk == VK_UP)
  347. {
  348. return FALSE;
  349. }
  350. else if (vk == VK_DOWN)
  351. {
  352. return FALSE;
  353. }
  354. if (m_pToolTipActions != NULL)
  355. {
  356. if (m_pToolTipActions->ContainsKey((int)vk))
  357. {
  358. return FALSE;
  359. }
  360. }
  361. if (m_showPersistant == false)
  362. {
  363. Hide();
  364. }
  365. break;
  366. }
  367. case WM_LBUTTONDBLCLK:
  368. case WM_RBUTTONDBLCLK:
  369. case WM_MBUTTONDOWN:
  370. case WM_MBUTTONDBLCLK:
  371. case WM_NCLBUTTONDOWN:
  372. case WM_NCLBUTTONDBLCLK:
  373. case WM_NCRBUTTONDOWN:
  374. case WM_NCRBUTTONDBLCLK:
  375. case WM_NCMBUTTONDOWN:
  376. case WM_NCMBUTTONDBLCLK:
  377. {
  378. if (m_showPersistant == false)
  379. {
  380. Hide();
  381. }
  382. break;
  383. }
  384. case WM_MOUSEWHEEL:
  385. {
  386. if (m_imageViewer.m_pBitmap)
  387. {
  388. m_imageViewer.PostMessageW(pMsg->message, pMsg->wParam, pMsg->lParam);
  389. return TRUE;
  390. }
  391. else
  392. {
  393. m_RichEdit.PostMessageW(pMsg->message, pMsg->wParam, pMsg->lParam);
  394. return TRUE;
  395. }
  396. }
  397. break;
  398. }
  399. return FALSE;
  400. }
  401. CRect CToolTipEx::GetBoundsRect()
  402. {
  403. DWORD d = GetTickCount();
  404. CWindowDC dc(NULL);
  405. int nLineWidth = 0;
  406. CRect rect(0, 0, 0, 0);
  407. if(nLineWidth == 0)
  408. {
  409. // Count the number of lines of text
  410. int nStart = 0;
  411. INT nNumLines = 0;
  412. int longestLength = 0;
  413. CString longestString;
  414. do
  415. {
  416. int newStart = m_csText.Find(_T("\n"), nStart);
  417. if (newStart < 0)
  418. {
  419. int length = m_csText.GetLength() - nStart;
  420. if (length > longestLength)
  421. {
  422. longestString = m_csText.Mid(nStart, length);
  423. longestLength = length;
  424. }
  425. break;
  426. }
  427. int length = newStart - nStart;
  428. if(length > longestLength)
  429. {
  430. longestString = m_csText.Mid(nStart, length);
  431. longestLength = length;
  432. }
  433. nNumLines++;
  434. nStart = newStart + 1;
  435. }
  436. while(nStart >= 0 && nNumLines < 100);
  437. CFont *pOldFont = (CFont*)dc.SelectObject((CFont*)&m_Font);
  438. CSize size = dc.GetTextExtent(longestString);
  439. dc.SelectObject(pOldFont);
  440. rect.right = size.cx;
  441. rect.bottom = size.cy * nNumLines;
  442. }
  443. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  444. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  445. if(m_imageViewer.m_pBitmap)
  446. {
  447. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap);
  448. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap);
  449. rect.bottom += nHeight;
  450. if((rect.left + nWidth) > rect.right)
  451. {
  452. rect.right = rect.left + nWidth;
  453. }
  454. }
  455. DWORD diff = GetTickCount() - d;
  456. if (diff > 10)
  457. {
  458. Log(StrF(_T("Size To Content: %d\n"), diff));
  459. }
  460. return rect;
  461. }
  462. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  463. {
  464. CString strReturn;
  465. LPCTSTR pstrStart = ref.LockBuffer();
  466. LPCTSTR pstrBuffer = pstrStart;
  467. int nCurrent = 0;
  468. int nStart = 0;
  469. int nEnd = 0;
  470. int nOldStart = 0;
  471. while(nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  472. {
  473. if(*pstrBuffer == ch)
  474. {
  475. nOldStart = nStart;
  476. nStart = nEnd + 1;
  477. nCurrent++;
  478. }
  479. nEnd++;
  480. pstrBuffer++;
  481. }
  482. // May have reached the end of the string
  483. if(*pstrBuffer == _T('\0'))
  484. {
  485. nOldStart = nStart;
  486. nEnd++;
  487. }
  488. ref.UnlockBuffer();
  489. if(nCurrent < nIndex)
  490. {
  491. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  492. return strReturn;
  493. }
  494. return ref.Mid(nOldStart, nEnd - nOldStart - 1);
  495. }
  496. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  497. {
  498. static LOGFONT LogFont;
  499. NONCLIENTMETRICS ncm;
  500. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  501. if(!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
  502. &ncm, 0))
  503. {
  504. return FALSE;
  505. }
  506. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  507. return &LogFont;
  508. }
  509. BOOL CToolTipEx::SetLogFont(LPLOGFONT lpLogFont, BOOL bRedraw /*=TRUE*/)
  510. {
  511. ASSERT(lpLogFont);
  512. if(!lpLogFont)
  513. {
  514. return FALSE;
  515. }
  516. LOGFONT LogFont;
  517. // Store font as the global default
  518. memcpy(&LogFont, lpLogFont, sizeof(LOGFONT));
  519. // Create the actual font object
  520. m_Font.DeleteObject();
  521. m_Font.CreateFontIndirect(&LogFont);
  522. if(bRedraw && ::IsWindow(GetSafeHwnd()))
  523. {
  524. Invalidate();
  525. }
  526. return TRUE;
  527. }
  528. void CToolTipEx::SetBitmap(CBitmap *pBitmap)
  529. {
  530. DELETE_BITMAP
  531. m_imageViewer.m_pBitmap = pBitmap;
  532. m_imageViewer.UpdateBitmapSize();
  533. if (m_imageViewer.m_pBitmap != NULL)
  534. {
  535. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_imageViewer.m_pBitmap);
  536. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_imageViewer.m_pBitmap);
  537. Invalidate();
  538. }
  539. }
  540. void CToolTipEx::OnSize(UINT nType, int cx, int cy)
  541. {
  542. CWnd::OnSize(nType, cx, cy);
  543. if(::IsWindow(m_RichEdit.GetSafeHwnd()) == FALSE)
  544. {
  545. return ;
  546. }
  547. CRect cr;
  548. GetClientRect(cr);
  549. cr.DeflateRect(0, 0, 0, theApp.m_metrics.ScaleY(21));
  550. m_RichEdit.MoveWindow(cr);
  551. m_imageViewer.MoveWindow(cr);
  552. m_optionsButton.MoveWindow(cr.left, cr.bottom + theApp.m_metrics.ScaleY(2), theApp.m_metrics.ScaleX(17), theApp.m_metrics.ScaleY(17));
  553. m_clipDataStatic.MoveWindow(cr.left + theApp.m_metrics.ScaleX(19), cr.bottom + theApp.m_metrics.ScaleY(2), cr.Width() - cr.left + theApp.m_metrics.ScaleX(19), theApp.m_metrics.ScaleY(17));
  554. this->Invalidate();
  555. m_DittoWindow.DoSetRegion(this);
  556. if (m_saveWindowLockout == false)
  557. {
  558. SetTimer(SAVE_SIZE, 250, NULL);
  559. }
  560. }
  561. BOOL CToolTipEx::IsCursorInToolTip()
  562. {
  563. CRect cr;
  564. GetWindowRect(cr);
  565. CPoint cursorPos;
  566. GetCursorPos(&cursorPos);
  567. return cr.PtInRect(cursorPos);
  568. }
  569. void CToolTipEx::SetRTFText(const char *pRTF)
  570. {
  571. m_RichEdit.SetRTF(pRTF);
  572. m_csRTF = pRTF;
  573. m_RichEdit.SetSel(0, 0);
  574. HighlightSearchText();
  575. }
  576. //void CToolTipEx::SetRTFText(const CString &csRTF)
  577. //{
  578. // m_RichEdit.SetRTF(csRTF);
  579. // m_csRTF = csRTF;
  580. //}
  581. void CToolTipEx::SetToolTipText(const CString &csText)
  582. {
  583. m_csText = csText;
  584. m_RichEdit.SetFont(&m_Font);
  585. m_RichEdit.SetText(csText);
  586. m_RichEdit.SetSel(0, 0);
  587. CHARFORMAT cfNew;
  588. cfNew.cbSize = sizeof(CHARFORMAT);
  589. cfNew.dwMask = CFM_COLOR;
  590. cfNew.dwEffects = CFM_COLOR;
  591. cfNew.dwEffects &= ~CFE_AUTOCOLOR;
  592. cfNew.crTextColor = g_Opt.m_Theme.DescriptionWindowText();
  593. m_RichEdit.SetDefaultCharFormat(cfNew);
  594. HighlightSearchText();
  595. }
  596. void CToolTipEx::HighlightSearchText()
  597. {
  598. if (m_searchText.GetLength() <= 0)
  599. return;
  600. FINDTEXTEX ft;
  601. long n = -1;
  602. ft.lpstrText = m_searchText;
  603. ft.chrg.cpMin = 0;
  604. ft.chrg.cpMax = -1;
  605. CHARFORMAT cf;
  606. cf.cbSize = sizeof(cf);
  607. cf.dwMask = CFM_COLOR;
  608. cf.dwEffects = CFE_BOLD | ~CFE_AUTOCOLOR;
  609. cf.crTextColor = RGB(255, 0, 0);
  610. do
  611. {
  612. ft.chrg.cpMin = n+1;
  613. n = m_RichEdit.FindText(FR_DOWN, &ft);
  614. if (n != -1)
  615. {
  616. m_RichEdit.SetSel(ft.chrgText);
  617. m_RichEdit.SetSelectionCharFormat(cf);
  618. }
  619. } while (n != -1);
  620. m_RichEdit.SetSel(0, 0);
  621. }
  622. void CToolTipEx::DoSearch()
  623. {
  624. if (m_searchText.GetLength() <= 0)
  625. return;
  626. FINDTEXTEX ft;
  627. long n = -1;
  628. ft.lpstrText = m_searchText;
  629. long start;
  630. long end;
  631. m_RichEdit.GetSel(start, end);
  632. ft.chrg.cpMin = end;
  633. ft.chrg.cpMax = -1;
  634. int searchDirection = FR_DOWN;
  635. if (GetKeyState(VK_SHIFT) & 0x8000)
  636. {
  637. searchDirection = 0;
  638. ft.chrg.cpMin = start;
  639. }
  640. n = m_RichEdit.FindText(searchDirection, &ft);
  641. if (n != -1)
  642. {
  643. m_RichEdit.SetSel(ft.chrgText);
  644. }
  645. else
  646. {
  647. if (searchDirection == 0)
  648. {
  649. ft.chrg.cpMin = m_RichEdit.GetTextLength();
  650. }
  651. else
  652. {
  653. ft.chrg.cpMin = 0;
  654. }
  655. ft.chrg.cpMax = -1;
  656. n = m_RichEdit.FindText(searchDirection, &ft);
  657. if (n != -1)
  658. {
  659. m_RichEdit.SetSel(ft.chrgText);
  660. }
  661. }
  662. }
  663. void CToolTipEx::OnActivate(UINT nState, CWnd *pWndOther, BOOL bMinimized)
  664. {
  665. CWnd::OnActivate(nState, pWndOther, bMinimized);
  666. if (nState == WA_INACTIVE)
  667. {
  668. if(m_pNotifyWnd)
  669. {
  670. m_pNotifyWnd->PostMessage(NM_INACTIVE_TOOLTIPWND, 0, 0);
  671. }
  672. }
  673. }
  674. void CToolTipEx::OnTimer(UINT_PTR nIDEvent)
  675. {
  676. switch(nIDEvent)
  677. {
  678. case HIDE_WINDOW_TIMER:
  679. Hide();
  680. PostMessage(WM_DESTROY, 0, 0);
  681. break;
  682. case SAVE_SIZE:
  683. SaveWindowSize();
  684. KillTimer(SAVE_SIZE);
  685. break;
  686. case TIMER_BUTTON_UP:
  687. {
  688. if ((GetKeyState(VK_LBUTTON) & 0x100) == 0)
  689. {
  690. m_DittoWindow.DoNcLButtonUp(this, 0, CPoint(0, 0));
  691. KillTimer(TIMER_BUTTON_UP);
  692. }
  693. break;
  694. }
  695. case TIMER_AUTO_MAX:
  696. {
  697. if (m_DittoWindow.m_bMinimized)
  698. {
  699. CPoint cp;
  700. GetCursorPos(&cp);
  701. UINT nHitTest = (UINT)OnNcHitTest(cp);
  702. ScreenToClient(&cp);
  703. if (nHitTest == HTCAPTION)
  704. {
  705. if (m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  706. {
  707. if (m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  708. {
  709. m_DittoWindow.MinMaxWindow(this, FORCE_MAX);
  710. }
  711. }
  712. }
  713. }
  714. KillTimer(TIMER_AUTO_MAX);
  715. m_bMaxSetTimer = false;
  716. }
  717. }
  718. CWnd::OnTimer(nIDEvent);
  719. }
  720. void CToolTipEx::OnNcPaint()
  721. {
  722. m_DittoWindow.DoNcPaint(this);
  723. }
  724. void CToolTipEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  725. {
  726. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  727. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  728. }
  729. HITTEST_RET CToolTipEx::OnNcHitTest(CPoint point)
  730. {
  731. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  732. if(Ret == -1)
  733. return CWnd::OnNcHitTest(point);
  734. return Ret;
  735. }
  736. void CToolTipEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  737. {
  738. int buttonPressed = m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  739. if (buttonPressed != 0)
  740. {
  741. SetTimer(TIMER_BUTTON_UP, 100, NULL);
  742. }
  743. CWnd::OnNcLButtonDown(nHitTest, point);
  744. }
  745. void CToolTipEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  746. {
  747. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  748. switch(lRet)
  749. {
  750. case BUTTON_CLOSE:
  751. Hide();
  752. break;
  753. case BUTTON_CHEVRON:
  754. m_DittoWindow.MinMaxWindow(this, SWAP_MIN_MAX);
  755. OnNcPaint();
  756. break;
  757. }
  758. KillTimer(TIMER_BUTTON_UP);
  759. CWnd::OnNcLButtonUp(nHitTest, point);
  760. }
  761. void CToolTipEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  762. {
  763. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  764. if ((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  765. {
  766. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_DittoWindow.m_TimeMinimized;
  767. if (sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  768. {
  769. SetTimer(TIMER_AUTO_MAX, CGetSetOptions::GetTimeBeforeExpandWindow(), NULL);
  770. m_bMaxSetTimer = true;
  771. }
  772. }
  773. CWnd::OnNcMouseMove(nHitTest, point);
  774. }
  775. void CToolTipEx::OnOptions()
  776. {
  777. POINT pp;
  778. CMenu cmPopUp;
  779. CMenu *cmSubMenu = NULL;
  780. GetCursorPos(&pp);
  781. if(cmPopUp.LoadMenu(IDR_DESC_OPTIONS_MENU) != 0)
  782. {
  783. cmSubMenu = cmPopUp.GetSubMenu(0);
  784. if(!cmSubMenu)
  785. {
  786. return ;
  787. }
  788. GetCursorPos(&pp);
  789. //theApp.m_Language.UpdateRightClickMenu(cmSubMenu);
  790. if(CGetSetOptions::GetRememberDescPos())
  791. cmSubMenu->CheckMenuItem(ID_FIRST_REMEMBERWINDOWPOSITION, MF_CHECKED);
  792. if(CGetSetOptions::GetSizeDescWindowToContent())
  793. cmSubMenu->CheckMenuItem(ID_FIRST_SIZEWINDOWTOCONTENT, MF_CHECKED);
  794. if(CGetSetOptions::GetScaleImagesToDescWindow())
  795. cmSubMenu->CheckMenuItem(ID_FIRST_SCALEIMAGESTOFITWINDOW, MF_CHECKED);
  796. if (CGetSetOptions::GetMouseClickHidesDescription())
  797. cmSubMenu->CheckMenuItem(ID_FIRST_HIDEDESCRIPTIONWINDOWONM, MF_CHECKED);
  798. if (CGetSetOptions::GetWrapDescriptionText())
  799. cmSubMenu->CheckMenuItem(ID_FIRST_WRAPTEXT, MF_CHECKED);
  800. if (m_showPersistant)
  801. cmSubMenu->CheckMenuItem(ID_FIRST_ALWAYSONTOP, MF_CHECKED);
  802. UpdateMenuShortCut(cmSubMenu, ID_FIRST_WRAPTEXT, ActionEnums::TOGGLE_DESCRIPTION_WORD_WRAP);
  803. UpdateMenuShortCut(cmSubMenu, ID_FIRST_ALWAYSONTOP, ActionEnums::TOGGLESHOWPERSISTANT);
  804. cmSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, pp.x, pp.y, this, NULL);
  805. }
  806. }
  807. void CToolTipEx::UpdateMenuShortCut(CMenu *subMenu, int id, DWORD action)
  808. {
  809. if (m_pToolTipActions != NULL)
  810. {
  811. CString cs;
  812. subMenu->GetMenuString(id, cs, MF_BYCOMMAND);
  813. CString shortcutText = m_pToolTipActions->GetCmdKeyText(action);
  814. if (shortcutText != _T("") &&
  815. cs.Find("\t" + shortcutText) < 0)
  816. {
  817. cs += "\t";
  818. cs += shortcutText;
  819. subMenu->ModifyMenu(id, MF_BYCOMMAND, id, cs);
  820. }
  821. }
  822. }
  823. void CToolTipEx::OnRememberwindowposition()
  824. {
  825. CGetSetOptions::SetRememberDescPos(!CGetSetOptions::GetRememberDescPos());
  826. }
  827. void CToolTipEx::OnSizewindowtocontent()
  828. {
  829. CGetSetOptions::SetSizeDescWindowToContent(!CGetSetOptions::GetSizeDescWindowToContent());
  830. CRect rect;
  831. this->GetWindowRect(&rect);
  832. Show(rect.TopLeft());
  833. }
  834. void CToolTipEx::OnScaleimagestofitwindow()
  835. {
  836. CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
  837. m_imageViewer.UpdateBitmapSize();
  838. Invalidate();
  839. }
  840. void CToolTipEx::OnRButtonDown(UINT nFlags, CPoint point)
  841. {
  842. OnOptions();
  843. CWnd::OnRButtonDown(nFlags, point);
  844. }
  845. void CToolTipEx::OnSetFocus(CWnd* pOldWnd)
  846. {
  847. CWnd::OnSetFocus(pOldWnd);
  848. m_RichEdit.SetFocus();
  849. }
  850. void CToolTipEx::OnPaint()
  851. {
  852. CPaintDC dc(this); // device context for painting
  853. CRect rect;
  854. GetClientRect(rect);
  855. CBrush Brush, *pOldBrush;
  856. Brush.CreateSolidBrush(g_Opt.m_Theme.DescriptionWindowBG());
  857. pOldBrush = dc.SelectObject(&Brush);
  858. dc.FillRect(&rect, &Brush);
  859. // Cleanup
  860. dc.SelectObject(pOldBrush);
  861. }
  862. void CToolTipEx::OnFirstHidedescriptionwindowonm()
  863. {
  864. CGetSetOptions::SetMouseClickHidesDescription(!CGetSetOptions::GetMouseClickHidesDescription());
  865. }
  866. bool CToolTipEx::ToggleWordWrap()
  867. {
  868. bool didWordWrap = false;
  869. if (m_RichEdit.IsWindowVisible())
  870. {
  871. OnFirstWraptext();
  872. didWordWrap = true;
  873. }
  874. return didWordWrap;
  875. }
  876. void CToolTipEx::OnFirstWraptext()
  877. {
  878. CGetSetOptions::SetWrapDescriptionText(!CGetSetOptions::GetWrapDescriptionText());
  879. ApplyWordWrap();
  880. }
  881. void CToolTipEx::ApplyWordWrap()
  882. {
  883. if (CGetSetOptions::GetWrapDescriptionText())
  884. {
  885. m_RichEdit.SetTargetDevice(NULL, 0);
  886. }
  887. else
  888. {
  889. m_RichEdit.SetTargetDevice(NULL, 1);
  890. }
  891. }
  892. void CToolTipEx::HideWindowInXMilliSeconds(long lms)
  893. {
  894. SetTimer(HIDE_WINDOW_TIMER, lms, NULL);
  895. }
  896. void CToolTipEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  897. {
  898. CWnd::OnWindowPosChanging(lpwndpos);
  899. m_DittoWindow.SnapToEdge(this, lpwndpos);
  900. }
  901. void CToolTipEx::OnFirstAlwaysontop()
  902. {
  903. m_showPersistant = !m_showPersistant;
  904. if (m_showPersistant)
  905. {
  906. m_DittoWindow.m_customWindowTitle = _T("[Always on top]");
  907. m_DittoWindow.m_useCustomWindowTitle = true;
  908. ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
  909. }
  910. else
  911. {
  912. m_DittoWindow.m_customWindowTitle = _T("");
  913. m_DittoWindow.m_useCustomWindowTitle = true;
  914. }
  915. ::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  916. }