WndEx.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // WndEx.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "WndEx.h"
  6. #include ".\wndex.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CWndEx
  14. #define CLOSE_WIDTH 12
  15. #define CLOSE_HEIGHT 11
  16. #define CLOSE_BORDER 2
  17. #define TIMER_AUTO_MAX 5
  18. CWndEx::CWndEx()
  19. {
  20. SetCaptionColorActive(false, TRUE);
  21. m_crFullSizeWindow.SetRectEmpty();
  22. m_lDelayMaxSeconds = 2;
  23. m_lButtonDownOnCaption = false;
  24. }
  25. CWndEx::~CWndEx()
  26. {
  27. }
  28. void CWndEx::InvalidateNc()
  29. {
  30. ::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  31. }
  32. void CWndEx::GetWindowRectEx(LPRECT lpRect)
  33. {
  34. if(m_DittoWindow.m_bMinimized)
  35. {
  36. *lpRect = m_crFullSizeWindow;
  37. return;
  38. }
  39. CWnd::GetWindowRect(lpRect);
  40. }
  41. BEGIN_MESSAGE_MAP(CWndEx, CWnd)
  42. //{{AFX_MSG_MAP(CWndEx)
  43. ON_WM_CREATE()
  44. ON_WM_NCPAINT()
  45. ON_WM_NCCALCSIZE()
  46. ON_WM_NCHITTEST()
  47. ON_WM_NCLBUTTONDOWN()
  48. ON_WM_NCMOUSEMOVE()
  49. ON_WM_NCLBUTTONUP()
  50. ON_WM_ERASEBKGND()
  51. ON_WM_TIMER()
  52. ON_WM_WINDOWPOSCHANGING()
  53. ON_WM_INITMENUPOPUP()
  54. //}}AFX_MSG_MAP
  55. ON_WM_SIZE()
  56. END_MESSAGE_MAP()
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CWndEx message handlers
  59. BOOL CWndEx::Create(const CRect& crStart, CWnd* pParentWnd)
  60. {
  61. WNDCLASS wc;
  62. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  63. wc.lpfnWndProc = AfxWndProc;
  64. wc.cbClsExtra = 0;
  65. wc.cbWndExtra = 0;
  66. wc.hInstance = AfxGetInstanceHandle();
  67. wc.hIcon = NULL;
  68. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  69. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  70. wc.lpszMenuName = NULL;
  71. wc.lpszClassName = _T("QPasteClass");
  72. // Create the QPaste window class
  73. if (!AfxRegisterClass(&wc))
  74. return FALSE;
  75. return CWndEx::CreateEx(0, _T("QPasteClass"), _T("Quick Paste"), WS_POPUP,
  76. crStart, pParentWnd, 0);
  77. }
  78. int CWndEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
  79. {
  80. if (CWnd::OnCreate(lpCreateStruct) == -1)
  81. return -1;
  82. m_DittoWindow.DoCreate(this);
  83. m_DittoWindow.m_bDrawMinimize = false;
  84. m_DittoWindow.m_bDrawMaximize = false;
  85. SetCaptionColorActive(false, TRUE);
  86. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true, g_Opt.m_Theme.GetCaptionSize(), g_Opt.m_Theme.GetCaptionFontSize());
  87. SetAutoMaxDelay(CGetSetOptions::GetAutoMaxDelay());
  88. m_toolTip.Create(this);
  89. CRect r;
  90. GetWindowRect(&r);
  91. ScreenToClient(&r);
  92. m_toolTip.AddTool(this, _T("Ditto"), r, 1);
  93. return 0;
  94. }
  95. bool CWndEx::SetCaptionColorActive(BOOL bPersistant, BOOL ConnectedToClipboard)
  96. {
  97. bool bResult;
  98. if(ConnectedToClipboard == false)
  99. {
  100. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftNotConnected(), g_Opt.m_Theme.CaptionRightNotConnected(), g_Opt.m_Theme.BorderNotConnected());
  101. }
  102. else
  103. {
  104. if(bPersistant)
  105. {
  106. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftTopMost(), g_Opt.m_Theme.CaptionRightTopMost(), g_Opt.m_Theme.BorderTopMost());
  107. }
  108. else
  109. {
  110. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  111. }
  112. }
  113. m_DittoWindow.SetCaptionTextColor(g_Opt.m_Theme.CaptionTextColor());
  114. return bResult;
  115. }
  116. void CWndEx::SetCaptionOn(int nPos, bool bOnstartup, int captionSize, int captionFontSize)
  117. {
  118. m_DittoWindow.SetCaptionOn(this, nPos, bOnstartup, captionSize, captionFontSize);
  119. }
  120. void CWndEx::OnNcPaint()
  121. {
  122. m_DittoWindow.DoNcPaint(this);
  123. }
  124. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  125. {
  126. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  127. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  128. }
  129. HITTEST_RET CWndEx::OnNcHitTest(CPoint point)
  130. {
  131. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  132. if(Ret == -1)
  133. return CWnd::OnNcHitTest(point);
  134. return Ret;
  135. }
  136. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  137. {
  138. m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  139. switch (nHitTest)
  140. {
  141. case HTCAPTION:
  142. m_lButtonDownOnCaption = true;
  143. break;
  144. default:
  145. m_lButtonDownOnCaption = false;
  146. }
  147. CWnd::OnNcLButtonDown(nHitTest, point);
  148. }
  149. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  150. {
  151. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  152. if(lRet > 0)
  153. {
  154. if(lRet == BUTTON_CHEVRON)
  155. {
  156. MinMaxWindow();
  157. }
  158. return;
  159. }
  160. CWnd::OnNcLButtonUp(nHitTest, point);
  161. }
  162. void CWndEx::MinMaxWindow(long lOption)
  163. {
  164. if((m_DittoWindow.m_bMinimized) && (lOption == FORCE_MIN))
  165. return;
  166. if((m_DittoWindow.m_bMinimized == false) && (lOption == FORCE_MAX))
  167. return;
  168. if(m_DittoWindow.m_lRightBorder == m_DittoWindow.m_captionBorderWidth)
  169. {
  170. if(m_DittoWindow.m_bMinimized == false)
  171. {
  172. GetWindowRect(m_crFullSizeWindow);
  173. MoveWindow(m_crFullSizeWindow.right - m_DittoWindow.m_captionBorderWidth,
  174. m_crFullSizeWindow.top, m_DittoWindow.m_captionBorderWidth,
  175. m_crFullSizeWindow.Height());
  176. m_DittoWindow.m_bMinimized = true;
  177. m_TimeMinimized = COleDateTime::GetCurrentTime();
  178. OnNcPaint();
  179. }
  180. else
  181. {
  182. CRect cr;
  183. GetWindowRect(cr);
  184. MoveWindow(cr.right - m_crFullSizeWindow.Width(),
  185. cr.top, m_crFullSizeWindow.Width(), cr.Height());
  186. m_crFullSizeWindow.SetRectEmpty();
  187. m_DittoWindow.m_bMinimized = false;
  188. m_TimeMaximized = COleDateTime::GetCurrentTime();
  189. ::SetForegroundWindow(this->GetSafeHwnd());
  190. OnNcPaint();
  191. }
  192. }
  193. if(m_DittoWindow.m_lLeftBorder == m_DittoWindow.m_captionBorderWidth)
  194. {
  195. if(m_DittoWindow.m_bMinimized == false)
  196. {
  197. GetWindowRect(m_crFullSizeWindow);
  198. MoveWindow(m_crFullSizeWindow.left,
  199. m_crFullSizeWindow.top, m_DittoWindow.m_captionBorderWidth,
  200. m_crFullSizeWindow.Height());
  201. m_DittoWindow.m_bMinimized = true;
  202. m_TimeMinimized = COleDateTime::GetCurrentTime();
  203. OnNcPaint();
  204. }
  205. else
  206. {
  207. CRect cr;
  208. GetWindowRect(cr);
  209. MoveWindow(cr.left, cr.top,
  210. m_crFullSizeWindow.Width(), cr.Height());
  211. m_crFullSizeWindow.SetRectEmpty();
  212. m_DittoWindow.m_bMinimized = false;
  213. m_TimeMaximized = COleDateTime::GetCurrentTime();
  214. ::SetForegroundWindow(this->GetSafeHwnd());
  215. OnNcPaint();
  216. }
  217. }
  218. else if(m_DittoWindow.m_lTopBorder == m_DittoWindow.m_captionBorderWidth)
  219. {
  220. if(m_DittoWindow.m_bMinimized == false)
  221. {
  222. GetWindowRect(m_crFullSizeWindow);
  223. MoveWindow(m_crFullSizeWindow.left,
  224. m_crFullSizeWindow.top,
  225. m_crFullSizeWindow.Width(),
  226. m_DittoWindow.m_captionBorderWidth);
  227. m_DittoWindow.m_bMinimized = true;
  228. m_TimeMinimized = COleDateTime::GetCurrentTime();
  229. OnNcPaint();
  230. }
  231. else
  232. {
  233. CRect cr;
  234. GetWindowRect(cr);
  235. MoveWindow(cr.left, cr.top,
  236. cr.Width(), m_crFullSizeWindow.Height());
  237. m_crFullSizeWindow.SetRectEmpty();
  238. m_DittoWindow.m_bMinimized = false;
  239. m_TimeMaximized = COleDateTime::GetCurrentTime();
  240. ::SetForegroundWindow(this->GetSafeHwnd());
  241. OnNcPaint();
  242. }
  243. }
  244. else if(m_DittoWindow.m_lBottomBorder == m_DittoWindow.m_captionBorderWidth)
  245. {
  246. if(m_DittoWindow.m_bMinimized == false)
  247. {
  248. GetWindowRect(m_crFullSizeWindow);
  249. MoveWindow(m_crFullSizeWindow.left,
  250. m_crFullSizeWindow.bottom - m_DittoWindow.m_captionBorderWidth,
  251. m_crFullSizeWindow.Width(),
  252. m_DittoWindow.m_captionBorderWidth);
  253. m_DittoWindow.m_bMinimized = true;
  254. m_TimeMinimized = COleDateTime::GetCurrentTime();
  255. OnNcPaint();
  256. }
  257. else
  258. {
  259. CRect cr;
  260. GetWindowRect(cr);
  261. MoveWindow(cr.left,
  262. cr.bottom - m_crFullSizeWindow.Height(),
  263. cr.Width(), m_crFullSizeWindow.Height());
  264. m_crFullSizeWindow.SetRectEmpty();
  265. m_DittoWindow.m_bMinimized = false;
  266. m_TimeMaximized = COleDateTime::GetCurrentTime();
  267. ::SetForegroundWindow(this->GetSafeHwnd());
  268. OnNcPaint();
  269. }
  270. }
  271. }
  272. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  273. {
  274. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  275. if((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  276. {
  277. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_TimeMinimized;
  278. if(sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  279. {
  280. SetTimer(TIMER_AUTO_MAX, CGetSetOptions::GetTimeBeforeExpandWindow(), NULL);
  281. m_bMaxSetTimer = true;
  282. }
  283. }
  284. CWnd::OnNcMouseMove(nHitTest, point);
  285. }
  286. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  287. {
  288. m_toolTip.RelayEvent(pMsg);
  289. m_DittoWindow.DoPreTranslateMessage(pMsg);
  290. return CWnd::PreTranslateMessage(pMsg);
  291. }
  292. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  293. {
  294. return CWnd::OnEraseBkgnd(pDC);
  295. }
  296. void CWndEx::OnTimer(UINT_PTR nIDEvent)
  297. {
  298. if(nIDEvent == TIMER_AUTO_MAX)
  299. {
  300. if(m_DittoWindow.m_bMinimized)
  301. {
  302. CPoint cp;
  303. GetCursorPos(&cp);
  304. UINT nHitTest = (UINT)OnNcHitTest(cp);
  305. ScreenToClient(&cp);
  306. if(nHitTest == HTCAPTION)
  307. {
  308. if(m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  309. {
  310. if(m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  311. {
  312. MinMaxWindow(FORCE_MAX);
  313. }
  314. }
  315. }
  316. }
  317. KillTimer(TIMER_AUTO_MAX);
  318. m_bMaxSetTimer = false;
  319. }
  320. CWnd::OnTimer(nIDEvent);
  321. }
  322. void CWndEx::SnapToEdge(WINDOWPOS* lpwndpos)
  323. {
  324. if (lpwndpos->cx == 0 &&
  325. lpwndpos->cy == 0)
  326. {
  327. return;
  328. }
  329. const char threshold = 12;
  330. RECT rect = { 0 };
  331. HMONITOR hMonitor;
  332. MONITORINFO mi;
  333. // Grab information about our monitors
  334. // For multi-monitor support, we use this instead of SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
  335. hMonitor = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST);
  336. mi.cbSize = sizeof(mi);
  337. GetMonitorInfo(hMonitor, &mi);
  338. rect = mi.rcWork;
  339. bool edgeMove = true;
  340. bool captionMove = false;
  341. if (m_lButtonDownOnCaption)
  342. {
  343. edgeMove = false;
  344. captionMove = true;
  345. }
  346. // Snap to left
  347. if (lpwndpos->x >= (rect.left - threshold) &&
  348. lpwndpos->x <= (rect.left + threshold))
  349. {
  350. if (edgeMove)
  351. {
  352. int diff = lpwndpos->x - rect.left;
  353. lpwndpos->cx += diff;
  354. }
  355. if (edgeMove || captionMove)
  356. {
  357. lpwndpos->x = rect.left;
  358. }
  359. }
  360. // Snap to right
  361. if ((lpwndpos->x + lpwndpos->cx) >= (rect.right - threshold) &&
  362. (lpwndpos->x + lpwndpos->cx) <= (rect.right + threshold))
  363. {
  364. if (edgeMove)
  365. {
  366. int diff = rect.right - (lpwndpos->x + lpwndpos->cx);
  367. lpwndpos->cx += diff;
  368. }
  369. if (captionMove)
  370. {
  371. lpwndpos->x = (rect.right - lpwndpos->cx);
  372. }
  373. }
  374. // Snap to top
  375. if (lpwndpos->y >= (rect.top - threshold) &&
  376. lpwndpos->y <= (rect.top + threshold))
  377. {
  378. if (edgeMove)
  379. {
  380. int diff = lpwndpos->y - rect.top;
  381. lpwndpos->cy += diff;
  382. }
  383. if (edgeMove || captionMove)
  384. {
  385. lpwndpos->y = rect.top;
  386. }
  387. }
  388. // Snap to bottom
  389. if ((lpwndpos->y + lpwndpos->cy) >= (rect.bottom - threshold) &&
  390. (lpwndpos->y + lpwndpos->cy) <= (rect.bottom + threshold))
  391. {
  392. if (edgeMove)
  393. {
  394. int diff = rect.bottom - (lpwndpos->y + lpwndpos->cy);
  395. lpwndpos->cy += diff;
  396. }
  397. if (captionMove)
  398. {
  399. lpwndpos->y = (rect.bottom - lpwndpos->cy);
  400. }
  401. }
  402. }
  403. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  404. {
  405. CWnd::OnWindowPosChanging(lpwndpos);
  406. if(m_bMaxSetTimer)
  407. {
  408. KillTimer(TIMER_AUTO_MAX);
  409. m_bMaxSetTimer = false;
  410. }
  411. SnapToEdge(lpwndpos);
  412. }
  413. void CWndEx::OnSize(UINT nType, int cx, int cy)
  414. {
  415. CWnd::OnSize(nType, cx, cy);
  416. m_DittoWindow.DoSetRegion(this);
  417. }
  418. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex, BOOL bSysMenu)
  419. {
  420. OnInitMenuPopupEx(pPopupMenu, nIndex, bSysMenu, this);
  421. }
  422. void CWndEx::SetToolTipText(CString text)
  423. {
  424. m_toolTip.UpdateTipText(text, this, 1);
  425. }
  426. void CWndEx::SetCustomWindowTitle(CString title)
  427. {
  428. CString old = m_DittoWindow.m_customWindowTitle;
  429. m_DittoWindow.m_customWindowTitle = title;
  430. m_DittoWindow.m_useCustomWindowTitle = true;
  431. if (old != m_DittoWindow.m_customWindowTitle)
  432. {
  433. this->InvalidateNc();
  434. }
  435. }