WndEx.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #define TIMER_AUTO_MIN 6
  19. CWndEx::CWndEx()
  20. {
  21. SetCaptionColorActive(false, theApp.GetConnectCV());
  22. m_crFullSizeWindow.SetRectEmpty();
  23. m_lDelayMaxSeconds = 2;
  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. SetAutoHide(CGetSetOptions::GetAutoHide());
  86. SetCaptionColorActive(false, theApp.GetConnectCV());
  87. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true);
  88. SetAutoMaxDelay(CGetSetOptions::GetAutoMaxDelay());
  89. return 0;
  90. }
  91. bool CWndEx::SetCaptionColorActive(BOOL bPersistant, BOOL ConnectedToClipboard)
  92. {
  93. bool bResult;
  94. if(ConnectedToClipboard == false)
  95. {
  96. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftNotConnected(), g_Opt.m_Theme.CaptionRightNotConnected());
  97. }
  98. else
  99. {
  100. if(bPersistant)
  101. {
  102. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftTopMost(), g_Opt.m_Theme.CaptionRightTopMost());
  103. }
  104. else
  105. {
  106. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight());
  107. }
  108. }
  109. m_DittoWindow.SetCaptionTextColor(g_Opt.m_Theme.CaptionTextColor());
  110. return bResult;
  111. }
  112. void CWndEx::SetAutoHide(BOOL bAutoHide)
  113. {
  114. if(bAutoHide)
  115. {
  116. SetTimer(TIMER_AUTO_MIN, 500, NULL);
  117. }
  118. else
  119. {
  120. KillTimer(TIMER_AUTO_MIN);
  121. }
  122. }
  123. void CWndEx::SetCaptionOn(int nPos, bool bOnstartup)
  124. {
  125. m_DittoWindow.SetCaptionOn(this, nPos, bOnstartup);
  126. }
  127. void CWndEx::OnNcPaint()
  128. {
  129. m_DittoWindow.DoNcPaint(this);
  130. }
  131. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  132. {
  133. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  134. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  135. }
  136. HITTEST_RET CWndEx::OnNcHitTest(CPoint point)
  137. {
  138. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  139. if(Ret == -1)
  140. return CWnd::OnNcHitTest(point);
  141. return Ret;
  142. }
  143. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  144. {
  145. m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  146. CWnd::OnNcLButtonDown(nHitTest, point);
  147. }
  148. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  149. {
  150. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  151. if(lRet > 0)
  152. {
  153. if(lRet == BUTTON_CHEVRON)
  154. {
  155. MinMaxWindow();
  156. }
  157. return;
  158. }
  159. CWnd::OnNcLButtonUp(nHitTest, point);
  160. }
  161. void CWndEx::MinMaxWindow(long lOption)
  162. {
  163. if((m_DittoWindow.m_bMinimized) && (lOption == FORCE_MIN))
  164. return;
  165. if((m_DittoWindow.m_bMinimized == false) && (lOption == FORCE_MAX))
  166. return;
  167. if(m_DittoWindow.m_lRightBorder == CAPTION_BORDER)
  168. {
  169. if(m_DittoWindow.m_bMinimized == false)
  170. {
  171. GetWindowRect(m_crFullSizeWindow);
  172. MoveWindow(m_crFullSizeWindow.right - CAPTION_BORDER,
  173. m_crFullSizeWindow.top, CAPTION_BORDER,
  174. m_crFullSizeWindow.Height());
  175. m_DittoWindow.m_bMinimized = true;
  176. m_TimeMinimized = COleDateTime::GetCurrentTime();
  177. OnNcPaint();
  178. }
  179. else
  180. {
  181. CRect cr;
  182. GetWindowRect(cr);
  183. MoveWindow(cr.right - m_crFullSizeWindow.Width(),
  184. cr.top, m_crFullSizeWindow.Width(), cr.Height());
  185. m_crFullSizeWindow.SetRectEmpty();
  186. m_DittoWindow.m_bMinimized = false;
  187. m_TimeMaximized = COleDateTime::GetCurrentTime();
  188. ::SetForegroundWindow(this->GetSafeHwnd());
  189. OnNcPaint();
  190. }
  191. }
  192. if(m_DittoWindow.m_lLeftBorder == CAPTION_BORDER)
  193. {
  194. if(m_DittoWindow.m_bMinimized == false)
  195. {
  196. GetWindowRect(m_crFullSizeWindow);
  197. MoveWindow(m_crFullSizeWindow.left,
  198. m_crFullSizeWindow.top, CAPTION_BORDER,
  199. m_crFullSizeWindow.Height());
  200. m_DittoWindow.m_bMinimized = true;
  201. m_TimeMinimized = COleDateTime::GetCurrentTime();
  202. OnNcPaint();
  203. }
  204. else
  205. {
  206. CRect cr;
  207. GetWindowRect(cr);
  208. MoveWindow(cr.left, cr.top,
  209. m_crFullSizeWindow.Width(), cr.Height());
  210. m_crFullSizeWindow.SetRectEmpty();
  211. m_DittoWindow.m_bMinimized = false;
  212. m_TimeMaximized = COleDateTime::GetCurrentTime();
  213. ::SetForegroundWindow(this->GetSafeHwnd());
  214. OnNcPaint();
  215. }
  216. }
  217. else if(m_DittoWindow.m_lTopBorder == CAPTION_BORDER)
  218. {
  219. if(m_DittoWindow.m_bMinimized == false)
  220. {
  221. GetWindowRect(m_crFullSizeWindow);
  222. MoveWindow(m_crFullSizeWindow.left,
  223. m_crFullSizeWindow.top,
  224. m_crFullSizeWindow.Width(),
  225. CAPTION_BORDER);
  226. m_DittoWindow.m_bMinimized = true;
  227. m_TimeMinimized = COleDateTime::GetCurrentTime();
  228. OnNcPaint();
  229. }
  230. else
  231. {
  232. CRect cr;
  233. GetWindowRect(cr);
  234. MoveWindow(cr.left, cr.top,
  235. cr.Width(), m_crFullSizeWindow.Height());
  236. m_crFullSizeWindow.SetRectEmpty();
  237. m_DittoWindow.m_bMinimized = false;
  238. m_TimeMaximized = COleDateTime::GetCurrentTime();
  239. ::SetForegroundWindow(this->GetSafeHwnd());
  240. OnNcPaint();
  241. }
  242. }
  243. else if(m_DittoWindow.m_lBottomBorder == CAPTION_BORDER)
  244. {
  245. if(m_DittoWindow.m_bMinimized == false)
  246. {
  247. GetWindowRect(m_crFullSizeWindow);
  248. MoveWindow(m_crFullSizeWindow.left,
  249. m_crFullSizeWindow.bottom - CAPTION_BORDER,
  250. m_crFullSizeWindow.Width(),
  251. CAPTION_BORDER);
  252. m_DittoWindow.m_bMinimized = true;
  253. m_TimeMinimized = COleDateTime::GetCurrentTime();
  254. OnNcPaint();
  255. }
  256. else
  257. {
  258. CRect cr;
  259. GetWindowRect(cr);
  260. MoveWindow(cr.left,
  261. cr.bottom - m_crFullSizeWindow.Height(),
  262. cr.Width(), m_crFullSizeWindow.Height());
  263. m_crFullSizeWindow.SetRectEmpty();
  264. m_DittoWindow.m_bMinimized = false;
  265. m_TimeMaximized = COleDateTime::GetCurrentTime();
  266. ::SetForegroundWindow(this->GetSafeHwnd());
  267. OnNcPaint();
  268. }
  269. }
  270. }
  271. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  272. {
  273. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  274. if((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  275. {
  276. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_TimeMinimized;
  277. if(sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  278. {
  279. SetTimer(TIMER_AUTO_MAX, 250, NULL);
  280. m_bMaxSetTimer = true;
  281. }
  282. }
  283. CWnd::OnNcMouseMove(nHitTest, point);
  284. }
  285. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  286. {
  287. m_DittoWindow.DoPreTranslateMessage(pMsg);
  288. return CWnd::PreTranslateMessage(pMsg);
  289. }
  290. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  291. {
  292. return CWnd::OnEraseBkgnd(pDC);
  293. }
  294. void CWndEx::OnTimer(UINT_PTR nIDEvent)
  295. {
  296. if(nIDEvent == TIMER_AUTO_MAX)
  297. {
  298. if(m_DittoWindow.m_bMinimized)
  299. {
  300. CPoint cp;
  301. GetCursorPos(&cp);
  302. UINT nHitTest = (UINT)OnNcHitTest(cp);
  303. ScreenToClient(&cp);
  304. if(nHitTest == HTCAPTION)
  305. {
  306. if(m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  307. {
  308. if(m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  309. {
  310. MinMaxWindow(FORCE_MAX);
  311. }
  312. }
  313. }
  314. }
  315. KillTimer(TIMER_AUTO_MAX);
  316. m_bMaxSetTimer = false;
  317. }
  318. else if(nIDEvent == TIMER_AUTO_MIN)
  319. {
  320. if((m_DittoWindow.m_bMinimized == false) && (g_Opt.m_bShowPersistent))
  321. {
  322. CPoint cp;
  323. CRect cr;
  324. GetCursorPos(&cp);
  325. GetWindowRect(&cr);
  326. if(cr.PtInRect(cp) == false)
  327. {
  328. //hook dll
  329. if(g_Opt.m_bUseHookDllForFocus)
  330. {
  331. if(theApp.m_activeWnd.DittoHasFocus() == false)
  332. {
  333. MinMaxWindow(FORCE_MIN);
  334. }
  335. }
  336. }
  337. }
  338. }
  339. CWnd::OnTimer(nIDEvent);
  340. }
  341. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  342. {
  343. CWnd::OnWindowPosChanging(lpwndpos);
  344. if(m_bMaxSetTimer)
  345. {
  346. KillTimer(TIMER_AUTO_MAX);
  347. m_bMaxSetTimer = false;
  348. }
  349. }
  350. void CWndEx::OnSize(UINT nType, int cx, int cy)
  351. {
  352. CWnd::OnSize(nType, cx, cy);
  353. m_DittoWindow.DoSetRegion(this);
  354. }
  355. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
  356. {
  357. ASSERT(pPopupMenu != NULL);
  358. // Check the enabled state of various menu items.
  359. CCmdUI state;
  360. state.m_pMenu = pPopupMenu;
  361. ASSERT(state.m_pOther == NULL);
  362. ASSERT(state.m_pParentMenu == NULL);
  363. // Determine if menu is popup in top-level menu and set m_pOther to
  364. // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
  365. HMENU hParentMenu;
  366. if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
  367. {
  368. state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
  369. }
  370. else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
  371. {
  372. CWnd* pParent = this;
  373. // Child windows don't have menus--need to go to the top!
  374. if (pParent != NULL &&
  375. (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
  376. {
  377. int nIndexMax = ::GetMenuItemCount(hParentMenu);
  378. for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
  379. {
  380. if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
  381. {
  382. // When popup is found, m_pParentMenu is containing menu.
  383. state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
  390. for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
  391. state.m_nIndex++)
  392. {
  393. state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
  394. if (state.m_nID == 0)
  395. continue; // Menu separator or invalid cmd - ignore it.
  396. ASSERT(state.m_pOther == NULL);
  397. ASSERT(state.m_pMenu != NULL);
  398. if (state.m_nID == (UINT)-1)
  399. {
  400. // Possibly a popup menu, route to first item of that popup.
  401. state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
  402. if (state.m_pSubMenu == NULL ||
  403. (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
  404. state.m_nID == (UINT)-1)
  405. {
  406. continue; // First item of popup can't be routed to.
  407. }
  408. state.DoUpdate(this, TRUE); // Popups are never auto disabled.
  409. }
  410. else
  411. {
  412. // Normal menu item.
  413. // Auto enable/disable if frame window has m_bAutoMenuEnable
  414. // set and command is _not_ a system command.
  415. state.m_pSubMenu = NULL;
  416. state.DoUpdate(this, FALSE);
  417. }
  418. // Adjust for menu deletions and additions.
  419. UINT nCount = pPopupMenu->GetMenuItemCount();
  420. if (nCount < state.m_nIndexMax)
  421. {
  422. state.m_nIndex -= (state.m_nIndexMax - nCount);
  423. while (state.m_nIndex < nCount &&
  424. pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
  425. {
  426. state.m_nIndex++;
  427. }
  428. }
  429. state.m_nIndexMax = nCount;
  430. }
  431. }