WndEx.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. ::RedrawWindow( m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_NOCHILDREN );
  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. OnNcPaint();
  189. }
  190. }
  191. if(m_DittoWindow.m_lLeftBorder == CAPTION_BORDER)
  192. {
  193. if(m_DittoWindow.m_bMinimized == false)
  194. {
  195. GetWindowRect(m_crFullSizeWindow);
  196. MoveWindow(m_crFullSizeWindow.left,
  197. m_crFullSizeWindow.top, CAPTION_BORDER,
  198. m_crFullSizeWindow.Height());
  199. m_DittoWindow.m_bMinimized = true;
  200. m_TimeMinimized = COleDateTime::GetCurrentTime();
  201. OnNcPaint();
  202. }
  203. else
  204. {
  205. CRect cr;
  206. GetWindowRect(cr);
  207. MoveWindow(cr.left, cr.top,
  208. m_crFullSizeWindow.Width(), cr.Height());
  209. m_crFullSizeWindow.SetRectEmpty();
  210. m_DittoWindow.m_bMinimized = false;
  211. m_TimeMaximized = COleDateTime::GetCurrentTime();
  212. OnNcPaint();
  213. }
  214. }
  215. else if(m_DittoWindow.m_lTopBorder == CAPTION_BORDER)
  216. {
  217. if(m_DittoWindow.m_bMinimized == false)
  218. {
  219. GetWindowRect(m_crFullSizeWindow);
  220. MoveWindow(m_crFullSizeWindow.left,
  221. m_crFullSizeWindow.top,
  222. m_crFullSizeWindow.Width(),
  223. CAPTION_BORDER);
  224. m_DittoWindow.m_bMinimized = true;
  225. m_TimeMinimized = COleDateTime::GetCurrentTime();
  226. OnNcPaint();
  227. }
  228. else
  229. {
  230. CRect cr;
  231. GetWindowRect(cr);
  232. MoveWindow(cr.left, cr.top,
  233. cr.Width(), m_crFullSizeWindow.Height());
  234. m_crFullSizeWindow.SetRectEmpty();
  235. m_DittoWindow.m_bMinimized = false;
  236. m_TimeMaximized = COleDateTime::GetCurrentTime();
  237. OnNcPaint();
  238. }
  239. }
  240. else if(m_DittoWindow.m_lBottomBorder == CAPTION_BORDER)
  241. {
  242. if(m_DittoWindow.m_bMinimized == false)
  243. {
  244. GetWindowRect(m_crFullSizeWindow);
  245. MoveWindow(m_crFullSizeWindow.left,
  246. m_crFullSizeWindow.bottom - CAPTION_BORDER,
  247. m_crFullSizeWindow.Width(),
  248. CAPTION_BORDER);
  249. m_DittoWindow.m_bMinimized = true;
  250. m_TimeMinimized = COleDateTime::GetCurrentTime();
  251. OnNcPaint();
  252. }
  253. else
  254. {
  255. CRect cr;
  256. GetWindowRect(cr);
  257. MoveWindow(cr.left,
  258. cr.bottom - m_crFullSizeWindow.Height(),
  259. cr.Width(), m_crFullSizeWindow.Height());
  260. m_crFullSizeWindow.SetRectEmpty();
  261. m_DittoWindow.m_bMinimized = false;
  262. m_TimeMaximized = COleDateTime::GetCurrentTime();
  263. OnNcPaint();
  264. }
  265. }
  266. }
  267. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  268. {
  269. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  270. if((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  271. {
  272. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_TimeMinimized;
  273. if(sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  274. {
  275. SetTimer(TIMER_AUTO_MAX, 250, NULL);
  276. m_bMaxSetTimer = true;
  277. }
  278. }
  279. CWnd::OnNcMouseMove(nHitTest, point);
  280. }
  281. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  282. {
  283. m_DittoWindow.DoPreTranslateMessage(pMsg);
  284. return CWnd::PreTranslateMessage(pMsg);
  285. }
  286. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  287. {
  288. return CWnd::OnEraseBkgnd(pDC);
  289. }
  290. void CWndEx::OnTimer(UINT nIDEvent)
  291. {
  292. if(nIDEvent == TIMER_AUTO_MAX)
  293. {
  294. if(m_DittoWindow.m_bMinimized)
  295. {
  296. CPoint cp;
  297. GetCursorPos(&cp);
  298. UINT nHitTest = OnNcHitTest(cp);
  299. ScreenToClient(&cp);
  300. if(nHitTest == HTCAPTION)
  301. {
  302. if(m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  303. {
  304. if(m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  305. {
  306. MinMaxWindow(FORCE_MAX);
  307. }
  308. }
  309. }
  310. }
  311. KillTimer(TIMER_AUTO_MAX);
  312. m_bMaxSetTimer = false;
  313. }
  314. else if(nIDEvent == TIMER_AUTO_MIN)
  315. {
  316. if((m_DittoWindow.m_bMinimized == false) && (g_Opt.m_bShowPersistent))
  317. {
  318. CPoint cp;
  319. CRect cr;
  320. GetCursorPos(&cp);
  321. GetWindowRect(&cr);
  322. if(cr.PtInRect(cp) == false)
  323. {
  324. //theApp.m_bDittoHasFocus is only set when we are using the
  325. //hook dll
  326. if(g_Opt.m_bUseHookDllForFocus)
  327. {
  328. if(theApp.m_bDittoHasFocus == false)
  329. {
  330. MinMaxWindow(FORCE_MIN);
  331. }
  332. }
  333. }
  334. }
  335. }
  336. CWnd::OnTimer(nIDEvent);
  337. }
  338. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  339. {
  340. CWnd::OnWindowPosChanging(lpwndpos);
  341. if(m_bMaxSetTimer)
  342. {
  343. KillTimer(TIMER_AUTO_MAX);
  344. m_bMaxSetTimer = false;
  345. }
  346. }
  347. void CWndEx::OnSize(UINT nType, int cx, int cy)
  348. {
  349. CWnd::OnSize(nType, cx, cy);
  350. m_DittoWindow.DoSetRegion(this);
  351. }
  352. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
  353. {
  354. ASSERT(pPopupMenu != NULL);
  355. // Check the enabled state of various menu items.
  356. CCmdUI state;
  357. state.m_pMenu = pPopupMenu;
  358. ASSERT(state.m_pOther == NULL);
  359. ASSERT(state.m_pParentMenu == NULL);
  360. // Determine if menu is popup in top-level menu and set m_pOther to
  361. // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
  362. HMENU hParentMenu;
  363. if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
  364. {
  365. state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
  366. }
  367. else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
  368. {
  369. CWnd* pParent = this;
  370. // Child windows don't have menus--need to go to the top!
  371. if (pParent != NULL &&
  372. (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
  373. {
  374. int nIndexMax = ::GetMenuItemCount(hParentMenu);
  375. for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
  376. {
  377. if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
  378. {
  379. // When popup is found, m_pParentMenu is containing menu.
  380. state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
  381. break;
  382. }
  383. }
  384. }
  385. }
  386. state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
  387. for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
  388. state.m_nIndex++)
  389. {
  390. state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
  391. if (state.m_nID == 0)
  392. continue; // Menu separator or invalid cmd - ignore it.
  393. ASSERT(state.m_pOther == NULL);
  394. ASSERT(state.m_pMenu != NULL);
  395. if (state.m_nID == (UINT)-1)
  396. {
  397. // Possibly a popup menu, route to first item of that popup.
  398. state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
  399. if (state.m_pSubMenu == NULL ||
  400. (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
  401. state.m_nID == (UINT)-1)
  402. {
  403. continue; // First item of popup can't be routed to.
  404. }
  405. state.DoUpdate(this, TRUE); // Popups are never auto disabled.
  406. }
  407. else
  408. {
  409. // Normal menu item.
  410. // Auto enable/disable if frame window has m_bAutoMenuEnable
  411. // set and command is _not_ a system command.
  412. state.m_pSubMenu = NULL;
  413. state.DoUpdate(this, FALSE);
  414. }
  415. // Adjust for menu deletions and additions.
  416. UINT nCount = pPopupMenu->GetMenuItemCount();
  417. if (nCount < state.m_nIndexMax)
  418. {
  419. state.m_nIndex -= (state.m_nIndexMax - nCount);
  420. while (state.m_nIndex < nCount &&
  421. pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
  422. {
  423. state.m_nIndex++;
  424. }
  425. }
  426. state.m_nIndexMax = nCount;
  427. }
  428. }