WndEx.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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_BUTTON_UP 6
  19. CWndEx::CWndEx()
  20. {
  21. SetCaptionColorActive(false, TRUE);
  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. SetCaptionColorActive(false, TRUE);
  86. m_DittoWindow.SetCaptionOn(this, CGetSetOptions::GetCaptionPos(), true, g_Opt.m_Theme.GetCaptionSize(), g_Opt.m_Theme.GetCaptionFontSize());
  87. SetAutoMaxDelay(CGetSetOptions::GetAutoMaxDelay());
  88. m_toolTip.Create(this);
  89. CRect r;
  90. GetWindowRect(&r);
  91. ScreenToClient(&r);
  92. m_toolTip.AddTool(this, _T("Ditto"), r, 1);
  93. return 0;
  94. }
  95. bool CWndEx::SetCaptionColorActive(BOOL bPersistant, BOOL ConnectedToClipboard)
  96. {
  97. bool bResult;
  98. if(ConnectedToClipboard == false)
  99. {
  100. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftNotConnected(), g_Opt.m_Theme.CaptionRightNotConnected(), g_Opt.m_Theme.BorderNotConnected());
  101. }
  102. else
  103. {
  104. if(bPersistant)
  105. {
  106. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeftTopMost(), g_Opt.m_Theme.CaptionRightTopMost(), g_Opt.m_Theme.BorderTopMost());
  107. }
  108. else
  109. {
  110. bResult = m_DittoWindow.SetCaptionColors(g_Opt.m_Theme.CaptionLeft(), g_Opt.m_Theme.CaptionRight(), g_Opt.m_Theme.Border());
  111. }
  112. }
  113. m_DittoWindow.SetCaptionTextColor(g_Opt.m_Theme.CaptionTextColor());
  114. return bResult;
  115. }
  116. void CWndEx::SetCaptionOn(int nPos, bool bOnstartup, int captionSize, int captionFontSize)
  117. {
  118. m_DittoWindow.SetCaptionOn(this, nPos, bOnstartup, captionSize, captionFontSize);
  119. }
  120. void CWndEx::OnNcPaint()
  121. {
  122. m_DittoWindow.DoNcPaint(this);
  123. }
  124. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  125. {
  126. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  127. m_DittoWindow.DoNcCalcSize(bCalcValidRects, lpncsp);
  128. }
  129. HITTEST_RET CWndEx::OnNcHitTest(CPoint point)
  130. {
  131. UINT Ret = m_DittoWindow.DoNcHitTest(this, point);
  132. if(Ret == -1)
  133. return CWnd::OnNcHitTest(point);
  134. return Ret;
  135. }
  136. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  137. {
  138. int buttonPressed = m_DittoWindow.DoNcLButtonDown(this, nHitTest, point);
  139. if (buttonPressed != 0)
  140. {
  141. SetTimer(TIMER_BUTTON_UP, 100, NULL);
  142. }
  143. CWnd::OnNcLButtonDown(nHitTest, point);
  144. }
  145. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  146. {
  147. long lRet = m_DittoWindow.DoNcLButtonUp(this, nHitTest, point);
  148. if(lRet > 0)
  149. {
  150. if(lRet == BUTTON_CHEVRON)
  151. {
  152. MinMaxWindow();
  153. }
  154. return;
  155. }
  156. KillTimer(TIMER_BUTTON_UP);
  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 == m_DittoWindow.m_captionBorderWidth)
  166. {
  167. if(m_DittoWindow.m_bMinimized == false)
  168. {
  169. GetWindowRect(m_crFullSizeWindow);
  170. MoveWindow(m_crFullSizeWindow.right - m_DittoWindow.m_captionBorderWidth,
  171. m_crFullSizeWindow.top, m_DittoWindow.m_captionBorderWidth,
  172. m_crFullSizeWindow.Height());
  173. m_DittoWindow.m_bMinimized = true;
  174. m_TimeMinimized = COleDateTime::GetCurrentTime();
  175. OnNcPaint();
  176. }
  177. else
  178. {
  179. CRect cr;
  180. GetWindowRect(cr);
  181. MoveWindow(cr.right - m_crFullSizeWindow.Width(),
  182. cr.top, m_crFullSizeWindow.Width(), cr.Height());
  183. m_crFullSizeWindow.SetRectEmpty();
  184. m_DittoWindow.m_bMinimized = false;
  185. m_TimeMaximized = COleDateTime::GetCurrentTime();
  186. ::SetForegroundWindow(this->GetSafeHwnd());
  187. OnNcPaint();
  188. }
  189. }
  190. if(m_DittoWindow.m_lLeftBorder == m_DittoWindow.m_captionBorderWidth)
  191. {
  192. if(m_DittoWindow.m_bMinimized == false)
  193. {
  194. GetWindowRect(m_crFullSizeWindow);
  195. MoveWindow(m_crFullSizeWindow.left,
  196. m_crFullSizeWindow.top, m_DittoWindow.m_captionBorderWidth,
  197. m_crFullSizeWindow.Height());
  198. m_DittoWindow.m_bMinimized = true;
  199. m_TimeMinimized = COleDateTime::GetCurrentTime();
  200. OnNcPaint();
  201. }
  202. else
  203. {
  204. CRect cr;
  205. GetWindowRect(cr);
  206. MoveWindow(cr.left, cr.top,
  207. m_crFullSizeWindow.Width(), cr.Height());
  208. m_crFullSizeWindow.SetRectEmpty();
  209. m_DittoWindow.m_bMinimized = false;
  210. m_TimeMaximized = COleDateTime::GetCurrentTime();
  211. ::SetForegroundWindow(this->GetSafeHwnd());
  212. OnNcPaint();
  213. }
  214. }
  215. else if(m_DittoWindow.m_lTopBorder == m_DittoWindow.m_captionBorderWidth)
  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. m_DittoWindow.m_captionBorderWidth);
  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. ::SetForegroundWindow(this->GetSafeHwnd());
  238. OnNcPaint();
  239. }
  240. }
  241. else if(m_DittoWindow.m_lBottomBorder == m_DittoWindow.m_captionBorderWidth)
  242. {
  243. if(m_DittoWindow.m_bMinimized == false)
  244. {
  245. GetWindowRect(m_crFullSizeWindow);
  246. MoveWindow(m_crFullSizeWindow.left,
  247. m_crFullSizeWindow.bottom - m_DittoWindow.m_captionBorderWidth,
  248. m_crFullSizeWindow.Width(),
  249. m_DittoWindow.m_captionBorderWidth);
  250. m_DittoWindow.m_bMinimized = true;
  251. m_TimeMinimized = COleDateTime::GetCurrentTime();
  252. OnNcPaint();
  253. }
  254. else
  255. {
  256. CRect cr;
  257. GetWindowRect(cr);
  258. MoveWindow(cr.left,
  259. cr.bottom - m_crFullSizeWindow.Height(),
  260. cr.Width(), m_crFullSizeWindow.Height());
  261. m_crFullSizeWindow.SetRectEmpty();
  262. m_DittoWindow.m_bMinimized = false;
  263. m_TimeMaximized = COleDateTime::GetCurrentTime();
  264. ::SetForegroundWindow(this->GetSafeHwnd());
  265. OnNcPaint();
  266. }
  267. }
  268. }
  269. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  270. {
  271. m_DittoWindow.DoNcMouseMove(this, nHitTest, point);
  272. if((m_bMaxSetTimer == false) && m_DittoWindow.m_bMinimized)
  273. {
  274. COleDateTimeSpan sp = COleDateTime::GetCurrentTime() - m_TimeMinimized;
  275. if(sp.GetTotalSeconds() >= m_lDelayMaxSeconds)
  276. {
  277. SetTimer(TIMER_AUTO_MAX, CGetSetOptions::GetTimeBeforeExpandWindow(), NULL);
  278. m_bMaxSetTimer = true;
  279. }
  280. }
  281. CWnd::OnNcMouseMove(nHitTest, point);
  282. }
  283. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  284. {
  285. m_toolTip.RelayEvent(pMsg);
  286. m_DittoWindow.DoPreTranslateMessage(pMsg);
  287. return CWnd::PreTranslateMessage(pMsg);
  288. }
  289. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  290. {
  291. return CWnd::OnEraseBkgnd(pDC);
  292. }
  293. void CWndEx::OnTimer(UINT_PTR nIDEvent)
  294. {
  295. if(nIDEvent == TIMER_AUTO_MAX)
  296. {
  297. if(m_DittoWindow.m_bMinimized)
  298. {
  299. CPoint cp;
  300. GetCursorPos(&cp);
  301. UINT nHitTest = (UINT)OnNcHitTest(cp);
  302. ScreenToClient(&cp);
  303. if(nHitTest == HTCAPTION)
  304. {
  305. if(m_DittoWindow.m_crCloseBT.PtInRect(cp) == false)
  306. {
  307. if(m_DittoWindow.m_crMinimizeBT.PtInRect(cp) == false)
  308. {
  309. MinMaxWindow(FORCE_MAX);
  310. }
  311. }
  312. }
  313. }
  314. KillTimer(TIMER_AUTO_MAX);
  315. m_bMaxSetTimer = false;
  316. }
  317. else if (nIDEvent == TIMER_BUTTON_UP)
  318. {
  319. if ((GetKeyState(VK_LBUTTON) & 0x100) == 0)
  320. {
  321. m_DittoWindow.DoNcLButtonUp(this, 0, CPoint(0, 0));
  322. KillTimer(TIMER_BUTTON_UP);
  323. }
  324. }
  325. CWnd::OnTimer(nIDEvent);
  326. }
  327. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  328. {
  329. CWnd::OnWindowPosChanging(lpwndpos);
  330. if(m_bMaxSetTimer)
  331. {
  332. KillTimer(TIMER_AUTO_MAX);
  333. m_bMaxSetTimer = false;
  334. }
  335. m_DittoWindow.SnapToEdge(this, lpwndpos);
  336. }
  337. void CWndEx::OnSize(UINT nType, int cx, int cy)
  338. {
  339. CWnd::OnSize(nType, cx, cy);
  340. m_DittoWindow.DoSetRegion(this);
  341. }
  342. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex, BOOL bSysMenu)
  343. {
  344. OnInitMenuPopupEx(pPopupMenu, nIndex, bSysMenu, this);
  345. }
  346. void CWndEx::SetToolTipText(CString text)
  347. {
  348. m_toolTip.UpdateTipText(text, this, 1);
  349. }
  350. void CWndEx::SetCustomWindowTitle(CString title)
  351. {
  352. CString old = m_DittoWindow.m_customWindowTitle;
  353. m_DittoWindow.m_customWindowTitle = title;
  354. m_DittoWindow.m_useCustomWindowTitle = true;
  355. if (old != m_DittoWindow.m_customWindowTitle)
  356. {
  357. this->InvalidateNc();
  358. }
  359. }