WndEx.cpp 11 KB

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