WndEx.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. }
  24. CWndEx::~CWndEx()
  25. {
  26. }
  27. void CWndEx::InvalidateNc()
  28. {
  29. ::RedrawWindow( m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_NOCHILDREN );
  30. }
  31. void CWndEx::GetWindowRectEx(LPRECT lpRect)
  32. {
  33. if(m_DittoWindow.m_bMinimized)
  34. {
  35. *lpRect = m_crFullSizeWindow;
  36. return;
  37. }
  38. CWnd::GetWindowRect(lpRect);
  39. }
  40. bool CWndEx::SetCaptionColors( COLORREF left, COLORREF right )
  41. {
  42. m_DittoWindow.SetCaptionColors(left, right);
  43. return true;
  44. }
  45. BEGIN_MESSAGE_MAP(CWndEx, CWnd)
  46. //{{AFX_MSG_MAP(CWndEx)
  47. ON_WM_CREATE()
  48. ON_WM_NCPAINT()
  49. ON_WM_NCCALCSIZE()
  50. ON_WM_NCHITTEST()
  51. ON_WM_NCLBUTTONDOWN()
  52. ON_WM_NCMOUSEMOVE()
  53. ON_WM_NCLBUTTONUP()
  54. ON_WM_ERASEBKGND()
  55. ON_WM_TIMER()
  56. ON_WM_WINDOWPOSCHANGING()
  57. ON_WM_INITMENUPOPUP()
  58. //}}AFX_MSG_MAP
  59. ON_WM_SIZE()
  60. END_MESSAGE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CWndEx message handlers
  63. BOOL CWndEx::Create(const CRect& crStart, CWnd* pParentWnd)
  64. {
  65. WNDCLASS wc;
  66. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  67. wc.lpfnWndProc = AfxWndProc;
  68. wc.cbClsExtra = 0;
  69. wc.cbWndExtra = 0;
  70. wc.hInstance = AfxGetInstanceHandle();
  71. wc.hIcon = NULL;
  72. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  73. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  74. wc.lpszMenuName = NULL;
  75. wc.lpszClassName = _T("QPasteClass");
  76. // Create the QPaste window class
  77. if (!AfxRegisterClass(&wc))
  78. return FALSE;
  79. return CWndEx::CreateEx(0, _T("QPasteClass"), _T("Quick Paste"), WS_POPUP,
  80. crStart, pParentWnd, 0);
  81. }
  82. int CWndEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
  83. {
  84. if (CWnd::OnCreate(lpCreateStruct) == -1)
  85. return -1;
  86. m_DittoWindow.DoCreate(this);
  87. m_DittoWindow.m_bDrawMinimize = false;
  88. m_DittoWindow.m_bDrawMaximize = false;
  89. SetAutoHide(CGetSetOptions::GetAutoHide());
  90. SetCaptionColorActive(false, theApp.GetConnectCV());
  91. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true);
  92. return 0;
  93. }
  94. bool CWndEx::SetCaptionColorActive(bool bActive, bool ConnectedToClipboard)
  95. {
  96. bool bResult;
  97. if(ConnectedToClipboard == false)
  98. {
  99. bResult = SetCaptionColors(RGB(255, 0, 0), RGB(255, 0, 0));
  100. }
  101. else
  102. {
  103. if(bActive)
  104. bResult = SetCaptionColors(::GetSysColor(COLOR_ACTIVECAPTION), ::GetSysColor(COLOR_GRADIENTACTIVECAPTION));
  105. else
  106. bResult = SetCaptionColors(::GetSysColor(COLOR_INACTIVECAPTION), ::GetSysColor(COLOR_GRADIENTINACTIVECAPTION));
  107. }
  108. return bResult;
  109. }
  110. void CWndEx::SetAutoHide(BOOL bAutoHide)
  111. {
  112. if(bAutoHide)
  113. {
  114. SetTimer(TIMER_AUTO_MIN, 500, NULL);
  115. }
  116. else
  117. {
  118. KillTimer(TIMER_AUTO_MIN);
  119. }
  120. }
  121. void CWndEx::SetCaptionOn(int nPos, bool bOnstartup)
  122. {
  123. m_DittoWindow.SetCaptionOn(this, nPos, bOnstartup);
  124. }
  125. void CWndEx::OnNcPaint()
  126. {
  127. m_DittoWindow.DoNcPaint(this);
  128. }
  129. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  130. {
  131. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  132. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  133. }
  134. UINT CWndEx::OnNcHitTest(CPoint point)
  135. {
  136. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  137. if(Ret == -1)
  138. return CWnd::OnNcHitTest(point);
  139. return Ret;
  140. }
  141. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  142. {
  143. m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  144. CWnd::OnNcLButtonDown(nHitTest, point);
  145. }
  146. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  147. {
  148. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  149. if(lRet > 0)
  150. {
  151. if(lRet == BUTTON_CHEVRON)
  152. {
  153. MinMaxWindow();
  154. }
  155. return;
  156. }
  157. CWnd::OnNcLButtonUp(nHitTest, point);
  158. }
  159. void CWndEx::MinMaxWindow(long lOption)
  160. {
  161. if((m_DittoWindow.m_bMinimized) && (lOption == FORCE_MIN))
  162. return;
  163. if((m_DittoWindow.m_bMinimized == false) && (lOption == FORCE_MAX))
  164. return;
  165. if(m_DittoWindow.m_lRightBorder == CAPTION_BORDER)
  166. {
  167. if(m_DittoWindow.m_bMinimized == false)
  168. {
  169. GetWindowRect(m_crFullSizeWindow);
  170. MoveWindow(m_crFullSizeWindow.right - CAPTION_BORDER,
  171. m_crFullSizeWindow.top, CAPTION_BORDER,
  172. m_crFullSizeWindow.Height());
  173. m_DittoWindow.m_bMinimized = true;
  174. OnNcPaint();
  175. }
  176. else
  177. {
  178. CRect cr;
  179. GetWindowRect(cr);
  180. MoveWindow(cr.right - m_crFullSizeWindow.Width(),
  181. cr.top, m_crFullSizeWindow.Width(), cr.Height());
  182. m_crFullSizeWindow.SetRectEmpty();
  183. m_DittoWindow.m_bMinimized = false;
  184. OnNcPaint();
  185. }
  186. }
  187. if(m_DittoWindow.m_lLeftBorder == CAPTION_BORDER)
  188. {
  189. if(m_DittoWindow.m_bMinimized == false)
  190. {
  191. GetWindowRect(m_crFullSizeWindow);
  192. MoveWindow(m_crFullSizeWindow.left,
  193. m_crFullSizeWindow.top, CAPTION_BORDER,
  194. m_crFullSizeWindow.Height());
  195. m_DittoWindow.m_bMinimized = true;
  196. OnNcPaint();
  197. }
  198. else
  199. {
  200. CRect cr;
  201. GetWindowRect(cr);
  202. MoveWindow(cr.left, cr.top,
  203. m_crFullSizeWindow.Width(), cr.Height());
  204. m_crFullSizeWindow.SetRectEmpty();
  205. m_DittoWindow.m_bMinimized = false;
  206. OnNcPaint();
  207. }
  208. }
  209. else if(m_DittoWindow.m_lTopBorder == CAPTION_BORDER)
  210. {
  211. if(m_DittoWindow.m_bMinimized == false)
  212. {
  213. GetWindowRect(m_crFullSizeWindow);
  214. MoveWindow(m_crFullSizeWindow.left,
  215. m_crFullSizeWindow.top,
  216. m_crFullSizeWindow.Width(),
  217. CAPTION_BORDER);
  218. m_DittoWindow.m_bMinimized = true;
  219. OnNcPaint();
  220. }
  221. else
  222. {
  223. CRect cr;
  224. GetWindowRect(cr);
  225. MoveWindow(cr.left, cr.top,
  226. cr.Width(), m_crFullSizeWindow.Height());
  227. m_crFullSizeWindow.SetRectEmpty();
  228. m_DittoWindow.m_bMinimized = false;
  229. OnNcPaint();
  230. }
  231. }
  232. else if(m_DittoWindow.m_lBottomBorder == CAPTION_BORDER)
  233. {
  234. if(m_DittoWindow.m_bMinimized == false)
  235. {
  236. GetWindowRect(m_crFullSizeWindow);
  237. MoveWindow(m_crFullSizeWindow.left,
  238. m_crFullSizeWindow.bottom - CAPTION_BORDER,
  239. m_crFullSizeWindow.Width(),
  240. CAPTION_BORDER);
  241. m_DittoWindow.m_bMinimized = true;
  242. OnNcPaint();
  243. }
  244. else
  245. {
  246. CRect cr;
  247. GetWindowRect(cr);
  248. MoveWindow(cr.left,
  249. cr.bottom - m_crFullSizeWindow.Height(),
  250. cr.Width(), m_crFullSizeWindow.Height());
  251. m_crFullSizeWindow.SetRectEmpty();
  252. m_DittoWindow.m_bMinimized = false;
  253. OnNcPaint();
  254. }
  255. }
  256. }
  257. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  258. {
  259. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  260. if((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  261. {
  262. SetTimer(TIMER_AUTO_MAX, 250, NULL);
  263. m_bMaxSetTimer = true;
  264. }
  265. CWnd::OnNcMouseMove(nHitTest, point);
  266. }
  267. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  268. {
  269. m_DittoWindow.DoPreTranslateMessage(pMsg);
  270. return CWnd::PreTranslateMessage(pMsg);
  271. }
  272. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  273. {
  274. return CWnd::OnEraseBkgnd(pDC);
  275. }
  276. void CWndEx::OnTimer(UINT nIDEvent)
  277. {
  278. if(nIDEvent == TIMER_AUTO_MAX)
  279. {
  280. if(m_DittoWindow.m_bMinimized)
  281. {
  282. CPoint cp;
  283. GetCursorPos(&cp);
  284. UINT nHitTest = OnNcHitTest(cp);
  285. ScreenToClient(&cp);
  286. if(nHitTest == HTCAPTION)
  287. {
  288. if(m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  289. {
  290. if(m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  291. {
  292. MinMaxWindow(FORCE_MAX);
  293. }
  294. }
  295. }
  296. }
  297. KillTimer(TIMER_AUTO_MAX);
  298. m_bMaxSetTimer = false;
  299. }
  300. else if(nIDEvent == TIMER_AUTO_MIN)
  301. {
  302. if((m_DittoWindow.m_bMinimized == false) && (g_Opt.m_bShowPersistent))
  303. {
  304. CPoint cp;
  305. CRect cr;
  306. GetCursorPos(&cp);
  307. GetWindowRect(&cr);
  308. if(cr.PtInRect(cp) == false)
  309. {
  310. //theApp.m_bDittoHasFocus is only set when we are using the
  311. //hook dll
  312. if(g_Opt.m_bUseHookDllForFocus)
  313. {
  314. if(theApp.m_bDittoHasFocus == false)
  315. {
  316. MinMaxWindow(FORCE_MIN);
  317. }
  318. }
  319. }
  320. }
  321. }
  322. CWnd::OnTimer(nIDEvent);
  323. }
  324. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  325. {
  326. CWnd::OnWindowPosChanging(lpwndpos);
  327. if(m_bMaxSetTimer)
  328. {
  329. KillTimer(TIMER_AUTO_MAX);
  330. m_bMaxSetTimer = false;
  331. }
  332. }
  333. void CWndEx::OnSize(UINT nType, int cx, int cy)
  334. {
  335. CWnd::OnSize(nType, cx, cy);
  336. m_DittoWindow.DoSetRegion(this);
  337. }
  338. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
  339. {
  340. ASSERT(pPopupMenu != NULL);
  341. // Check the enabled state of various menu items.
  342. CCmdUI state;
  343. state.m_pMenu = pPopupMenu;
  344. ASSERT(state.m_pOther == NULL);
  345. ASSERT(state.m_pParentMenu == NULL);
  346. // Determine if menu is popup in top-level menu and set m_pOther to
  347. // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
  348. HMENU hParentMenu;
  349. if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
  350. {
  351. state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
  352. }
  353. else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
  354. {
  355. CWnd* pParent = this;
  356. // Child windows don't have menus--need to go to the top!
  357. if (pParent != NULL &&
  358. (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
  359. {
  360. int nIndexMax = ::GetMenuItemCount(hParentMenu);
  361. for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
  362. {
  363. if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
  364. {
  365. // When popup is found, m_pParentMenu is containing menu.
  366. state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
  373. for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
  374. state.m_nIndex++)
  375. {
  376. state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
  377. if (state.m_nID == 0)
  378. continue; // Menu separator or invalid cmd - ignore it.
  379. ASSERT(state.m_pOther == NULL);
  380. ASSERT(state.m_pMenu != NULL);
  381. if (state.m_nID == (UINT)-1)
  382. {
  383. // Possibly a popup menu, route to first item of that popup.
  384. state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
  385. if (state.m_pSubMenu == NULL ||
  386. (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
  387. state.m_nID == (UINT)-1)
  388. {
  389. continue; // First item of popup can't be routed to.
  390. }
  391. state.DoUpdate(this, TRUE); // Popups are never auto disabled.
  392. }
  393. else
  394. {
  395. // Normal menu item.
  396. // Auto enable/disable if frame window has m_bAutoMenuEnable
  397. // set and command is _not_ a system command.
  398. state.m_pSubMenu = NULL;
  399. state.DoUpdate(this, FALSE);
  400. }
  401. // Adjust for menu deletions and additions.
  402. UINT nCount = pPopupMenu->GetMenuItemCount();
  403. if (nCount < state.m_nIndexMax)
  404. {
  405. state.m_nIndex -= (state.m_nIndexMax - nCount);
  406. while (state.m_nIndex < nCount &&
  407. pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
  408. {
  409. state.m_nIndex++;
  410. }
  411. }
  412. state.m_nIndexMax = nCount;
  413. }
  414. }