WndEx.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. }
  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, TRUE);
  85. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true);
  86. SetAutoMaxDelay(CGetSetOptions::GetAutoMaxDelay());
  87. m_toolTip.Create(this);
  88. CRect r;
  89. GetWindowRect(&r);
  90. ScreenToClient(&r);
  91. m_toolTip.AddTool(this, _T("Ditto"), r, 1);
  92. return 0;
  93. }
  94. bool CWndEx::SetCaptionColorActive(BOOL bPersistant, BOOL ConnectedToClipboard)
  95. {
  96. bool bResult;
  97. if(ConnectedToClipboard == false)
  98. {
  99. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftNotConnected(), g_Opt.m_Theme.CaptionRightNotConnected(), g_Opt.m_Theme.BorderNotConnected());
  100. }
  101. else
  102. {
  103. if(bPersistant)
  104. {
  105. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftTopMost(), g_Opt.m_Theme.CaptionRightTopMost(), g_Opt.m_Theme.BorderTopMost());
  106. }
  107. else
  108. {
  109. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  110. }
  111. }
  112. m_DittoWindow.SetCaptionTextColor(g_Opt.m_Theme.CaptionTextColor());
  113. return bResult;
  114. }
  115. void CWndEx::SetCaptionOn(int nPos, bool bOnstartup)
  116. {
  117. m_DittoWindow.SetCaptionOn(this, nPos, bOnstartup);
  118. }
  119. void CWndEx::OnNcPaint()
  120. {
  121. m_DittoWindow.DoNcPaint(this);
  122. }
  123. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  124. {
  125. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  126. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  127. }
  128. HITTEST_RET CWndEx::OnNcHitTest(CPoint point)
  129. {
  130. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  131. if(Ret == -1)
  132. return CWnd::OnNcHitTest(point);
  133. return Ret;
  134. }
  135. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  136. {
  137. m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  138. CWnd::OnNcLButtonDown(nHitTest, point);
  139. }
  140. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  141. {
  142. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  143. if(lRet > 0)
  144. {
  145. if(lRet == BUTTON_CHEVRON)
  146. {
  147. MinMaxWindow();
  148. }
  149. return;
  150. }
  151. CWnd::OnNcLButtonUp(nHitTest, point);
  152. }
  153. void CWndEx::MinMaxWindow(long lOption)
  154. {
  155. if((m_DittoWindow.m_bMinimized) && (lOption == FORCE_MIN))
  156. return;
  157. if((m_DittoWindow.m_bMinimized == false) && (lOption == FORCE_MAX))
  158. return;
  159. if(m_DittoWindow.m_lRightBorder == CAPTION_BORDER)
  160. {
  161. if(m_DittoWindow.m_bMinimized == false)
  162. {
  163. GetWindowRect(m_crFullSizeWindow);
  164. MoveWindow(m_crFullSizeWindow.right - CAPTION_BORDER,
  165. m_crFullSizeWindow.top, CAPTION_BORDER,
  166. m_crFullSizeWindow.Height());
  167. m_DittoWindow.m_bMinimized = true;
  168. m_TimeMinimized = COleDateTime::GetCurrentTime();
  169. OnNcPaint();
  170. }
  171. else
  172. {
  173. CRect cr;
  174. GetWindowRect(cr);
  175. MoveWindow(cr.right - m_crFullSizeWindow.Width(),
  176. cr.top, m_crFullSizeWindow.Width(), cr.Height());
  177. m_crFullSizeWindow.SetRectEmpty();
  178. m_DittoWindow.m_bMinimized = false;
  179. m_TimeMaximized = COleDateTime::GetCurrentTime();
  180. ::SetForegroundWindow(this->GetSafeHwnd());
  181. OnNcPaint();
  182. }
  183. }
  184. if(m_DittoWindow.m_lLeftBorder == CAPTION_BORDER)
  185. {
  186. if(m_DittoWindow.m_bMinimized == false)
  187. {
  188. GetWindowRect(m_crFullSizeWindow);
  189. MoveWindow(m_crFullSizeWindow.left,
  190. m_crFullSizeWindow.top, CAPTION_BORDER,
  191. m_crFullSizeWindow.Height());
  192. m_DittoWindow.m_bMinimized = true;
  193. m_TimeMinimized = COleDateTime::GetCurrentTime();
  194. OnNcPaint();
  195. }
  196. else
  197. {
  198. CRect cr;
  199. GetWindowRect(cr);
  200. MoveWindow(cr.left, cr.top,
  201. m_crFullSizeWindow.Width(), cr.Height());
  202. m_crFullSizeWindow.SetRectEmpty();
  203. m_DittoWindow.m_bMinimized = false;
  204. m_TimeMaximized = COleDateTime::GetCurrentTime();
  205. ::SetForegroundWindow(this->GetSafeHwnd());
  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. m_TimeMinimized = COleDateTime::GetCurrentTime();
  220. OnNcPaint();
  221. }
  222. else
  223. {
  224. CRect cr;
  225. GetWindowRect(cr);
  226. MoveWindow(cr.left, cr.top,
  227. cr.Width(), m_crFullSizeWindow.Height());
  228. m_crFullSizeWindow.SetRectEmpty();
  229. m_DittoWindow.m_bMinimized = false;
  230. m_TimeMaximized = COleDateTime::GetCurrentTime();
  231. ::SetForegroundWindow(this->GetSafeHwnd());
  232. OnNcPaint();
  233. }
  234. }
  235. else if(m_DittoWindow.m_lBottomBorder == CAPTION_BORDER)
  236. {
  237. if(m_DittoWindow.m_bMinimized == false)
  238. {
  239. GetWindowRect(m_crFullSizeWindow);
  240. MoveWindow(m_crFullSizeWindow.left,
  241. m_crFullSizeWindow.bottom - CAPTION_BORDER,
  242. m_crFullSizeWindow.Width(),
  243. CAPTION_BORDER);
  244. m_DittoWindow.m_bMinimized = true;
  245. m_TimeMinimized = COleDateTime::GetCurrentTime();
  246. OnNcPaint();
  247. }
  248. else
  249. {
  250. CRect cr;
  251. GetWindowRect(cr);
  252. MoveWindow(cr.left,
  253. cr.bottom - m_crFullSizeWindow.Height(),
  254. cr.Width(), m_crFullSizeWindow.Height());
  255. m_crFullSizeWindow.SetRectEmpty();
  256. m_DittoWindow.m_bMinimized = false;
  257. m_TimeMaximized = COleDateTime::GetCurrentTime();
  258. ::SetForegroundWindow(this->GetSafeHwnd());
  259. OnNcPaint();
  260. }
  261. }
  262. }
  263. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  264. {
  265. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  266. if((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  267. {
  268. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_TimeMinimized;
  269. if(sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  270. {
  271. SetTimer(TIMER_AUTO_MAX, CGetSetOptions::GetTimeBeforeExpandWindow(), NULL);
  272. m_bMaxSetTimer = true;
  273. }
  274. }
  275. CWnd::OnNcMouseMove(nHitTest, point);
  276. }
  277. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  278. {
  279. m_toolTip.RelayEvent(pMsg);
  280. m_DittoWindow.DoPreTranslateMessage(pMsg);
  281. return CWnd::PreTranslateMessage(pMsg);
  282. }
  283. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  284. {
  285. return CWnd::OnEraseBkgnd(pDC);
  286. }
  287. void CWndEx::OnTimer(UINT_PTR nIDEvent)
  288. {
  289. if(nIDEvent == TIMER_AUTO_MAX)
  290. {
  291. if(m_DittoWindow.m_bMinimized)
  292. {
  293. CPoint cp;
  294. GetCursorPos(&cp);
  295. UINT nHitTest = (UINT)OnNcHitTest(cp);
  296. ScreenToClient(&cp);
  297. if(nHitTest == HTCAPTION)
  298. {
  299. if(m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  300. {
  301. if(m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  302. {
  303. MinMaxWindow(FORCE_MAX);
  304. }
  305. }
  306. }
  307. }
  308. KillTimer(TIMER_AUTO_MAX);
  309. m_bMaxSetTimer = false;
  310. }
  311. CWnd::OnTimer(nIDEvent);
  312. }
  313. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  314. {
  315. CWnd::OnWindowPosChanging(lpwndpos);
  316. if(m_bMaxSetTimer)
  317. {
  318. KillTimer(TIMER_AUTO_MAX);
  319. m_bMaxSetTimer = false;
  320. }
  321. }
  322. void CWndEx::OnSize(UINT nType, int cx, int cy)
  323. {
  324. CWnd::OnSize(nType, cx, cy);
  325. m_DittoWindow.DoSetRegion(this);
  326. }
  327. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex, BOOL bSysMenu)
  328. {
  329. OnInitMenuPopupEx(pPopupMenu, nIndex, bSysMenu, this);
  330. }
  331. void CWndEx::SetToolTipText(CString text)
  332. {
  333. m_toolTip.UpdateTipText(text, this, 1);
  334. }