MainFrm.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainFrm.h"
  6. #include "afxole.h"
  7. #include "Misc.h"
  8. #include "CopyProperties.h"
  9. #include "InternetUpdate.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #define WM_ICON_NOTIFY WM_APP+10
  16. #define ONE_MINUTE 60000
  17. #define KILL_DB_TIMER 1
  18. #define HIDE_ICON_TIMER 2
  19. #define REMOVE_OLD_ENTRIES_TIMER 3
  20. #define CHECK_FOR_UPDATE 4
  21. #define CLOSE_APP 5
  22. #define TIMER_CHECK_TOP_LEVEL_VIEWER 6
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMainFrame
  25. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  26. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  27. //{{AFX_MSG_MAP(CMainFrame)
  28. ON_WM_CREATE()
  29. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  30. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  31. ON_WM_CHANGECBCHAIN()
  32. ON_WM_DRAWCLIPBOARD()
  33. ON_WM_TIMER()
  34. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  35. ON_COMMAND(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, OnFirstReconnecttoclipboardchain)
  36. ON_UPDATE_COMMAND_UI(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, OnUpdateFirstReconnecttoclipboardchain)
  37. //}}AFX_MSG_MAP
  38. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  39. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  40. ON_MESSAGE(WM_RECONNECT_TO_COPY_CHAIN, OnReconnectToCopyChain)
  41. ON_MESSAGE(WM_IS_TOP_VIEWER, OnGetIsTopView)
  42. ON_MESSAGE(WM_COPYPROPERTIES, OnCopyProperties)
  43. ON_MESSAGE(WM_CLOSE_APP, OnShutDown)
  44. END_MESSAGE_MAP()
  45. static UINT indicators[] =
  46. {
  47. ID_SEPARATOR, // status line indicator
  48. ID_INDICATOR_CAPS,
  49. ID_INDICATOR_NUM,
  50. ID_INDICATOR_SCRL,
  51. };
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CMainFrame construction/destruction
  54. CMainFrame::CMainFrame()
  55. {
  56. m_lReconectCount = 0;
  57. }
  58. CMainFrame::~CMainFrame()
  59. {
  60. if(m_hNextClipboardViewer)
  61. {
  62. if(::IsWindow(m_hNextClipboardViewer))
  63. ChangeClipboardChain(m_hNextClipboardViewer);
  64. }
  65. CGetSetOptions::SetMainHWND(0);
  66. }
  67. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  68. {
  69. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  70. return -1;
  71. SetWindowText(MAIN_WND_TITLE);
  72. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  73. m_TrayIcon.Create(
  74. NULL, // Let icon deal with its own messages
  75. WM_ICON_NOTIFY, // Icon notify message to use
  76. _T("Ditto"), // tooltip
  77. hIcon,
  78. IDR_MENU, // ID of tray icon
  79. FALSE,
  80. _T(""), // balloon tip
  81. _T(""), // balloon title
  82. NULL, // balloon icon
  83. 20 );
  84. m_TrayIcon.MinimiseToTray(this);
  85. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  86. //Only if in release
  87. #ifndef _DEBUG
  88. {
  89. //If not showing the icon show it for 40 seconds so they can get to the option
  90. //in case they can't remember the hot keys or something like that
  91. if(!(CGetSetOptions::GetShowIconInSysTray()))
  92. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  93. }
  94. #endif
  95. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*5, 0);
  96. SetTimer(REMOVE_OLD_ENTRIES_TIMER, ONE_MINUTE*2, 0);
  97. SetTimer(TIMER_CHECK_TOP_LEVEL_VIEWER, ONE_MINUTE, 0);
  98. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  99. //Set up the clip board viewer
  100. theApp.m_bHandleClipboardDataChange = false;
  101. m_hNextClipboardViewer = SetClipboardViewer();
  102. theApp.m_bHandleClipboardDataChange = true;
  103. theApp.m_MainhWnd = m_hWnd;
  104. CGetSetOptions::SetMainHWND((long)m_hWnd);
  105. //Set up the hot key
  106. CGetSetOptions::RegisterHotKey(theApp.m_MainhWnd,
  107. CGetSetOptions::GetHotKey(),
  108. theApp.m_atomHotKey);
  109. CGetSetOptions::RegisterHotKey(theApp.m_MainhWnd,
  110. CGetSetOptions::GetNamedCopyHotKey(),
  111. theApp.m_atomNamedCopy);
  112. return 0;
  113. }
  114. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  115. {
  116. if( !CFrameWnd::PreCreateWindow(cs) )
  117. return FALSE;
  118. WNDCLASS wc;
  119. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  120. wc.lpfnWndProc = AfxWndProc;
  121. wc.cbClsExtra = 0;
  122. wc.cbWndExtra = 0;
  123. wc.hInstance = AfxGetInstanceHandle();
  124. wc.hIcon = NULL;
  125. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  126. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  127. wc.lpszMenuName = NULL;
  128. wc.lpszClassName = "Ditto";
  129. // Create the QPaste window class
  130. if (!AfxRegisterClass(&wc))
  131. return FALSE;
  132. cs.lpszClass = wc.lpszClassName;
  133. return TRUE;
  134. }
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CMainFrame diagnostics
  137. #ifdef _DEBUG
  138. void CMainFrame::AssertValid() const
  139. {
  140. CFrameWnd::AssertValid();
  141. }
  142. void CMainFrame::Dump(CDumpContext& dc) const
  143. {
  144. CFrameWnd::Dump(dc);
  145. }
  146. #endif //_DEBUG
  147. /////////////////////////////////////////////////////////////////////////////
  148. // CMainFrame message handlers
  149. void CMainFrame::OnFirstOption()
  150. {
  151. DoOptions(this);
  152. }
  153. void CMainFrame::OnFirstExit()
  154. {
  155. this->SendMessage(WM_CLOSE, 0, 0);
  156. }
  157. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  158. {
  159. if(wParam == theApp.m_atomHotKey)
  160. {
  161. QuickPaste.ShowQPasteWnd(this);
  162. }
  163. else if(wParam == theApp.m_atomNamedCopy)
  164. {
  165. theApp.ShowCopyProperties = true;
  166. //Simulate the Copy
  167. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  168. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  169. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  170. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  171. }
  172. return TRUE;
  173. }
  174. void CMainFrame::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  175. {
  176. // If the next window in the chain is being removed, reset our
  177. // "next window" handle.
  178. if(m_hNextClipboardViewer == hWndRemove)
  179. {
  180. m_hNextClipboardViewer = hWndAfter;
  181. }
  182. // If there is a next clipboard viewer, pass the message on to it.
  183. else if (m_hNextClipboardViewer != NULL)
  184. {
  185. ::SendMessage ( m_hNextClipboardViewer, WM_CHANGECBCHAIN,
  186. (WPARAM) hWndRemove, (LPARAM) hWndAfter );
  187. }
  188. }
  189. //Message that the clipboard data has changed
  190. void CMainFrame::OnDrawClipboard()
  191. {
  192. if(!theApp.m_bHandleClipboardDataChange)
  193. return;
  194. m_Copy.DoCopy();
  195. if (m_hNextClipboardViewer != NULL)
  196. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  197. }
  198. void CMainFrame::OnTimer(UINT nIDEvent)
  199. {
  200. switch(nIDEvent)
  201. {
  202. case HIDE_ICON_TIMER:
  203. {
  204. m_TrayIcon.HideIcon();
  205. KillTimer(nIDEvent);
  206. break;
  207. }
  208. case KILL_DB_TIMER:
  209. {
  210. if(QuickPaste.CloseQPasteWnd())
  211. {
  212. theApp.CloseDB();
  213. AfxDaoTerm();
  214. KillTimer(KILL_DB_TIMER);
  215. }
  216. break;
  217. }
  218. case REMOVE_OLD_ENTRIES_TIMER:
  219. {
  220. RemoveOldEntries();
  221. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  222. break;
  223. }
  224. case CHECK_FOR_UPDATE:
  225. {
  226. CInternetUpdate Update;
  227. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  228. {
  229. SendMessage(WM_CLOSE, 0, 0);
  230. }
  231. KillTimer(CHECK_FOR_UPDATE);
  232. break;
  233. }
  234. case CLOSE_APP:
  235. {
  236. if(theApp.m_bShowingOptions == false)
  237. {
  238. PostMessage(WM_CLOSE, 0, 0);
  239. KillTimer(CLOSE_APP);
  240. }
  241. break;
  242. }
  243. case TIMER_CHECK_TOP_LEVEL_VIEWER:
  244. {
  245. if(OnGetIsTopView(0, 0) == FALSE)
  246. {
  247. OnReconnectToCopyChain(0, 0);
  248. m_lReconectCount++;
  249. if(m_lReconectCount > 10)
  250. KillTimer(TIMER_CHECK_TOP_LEVEL_VIEWER);
  251. }
  252. break;
  253. }
  254. }
  255. CFrameWnd::OnTimer(nIDEvent);
  256. }
  257. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  258. {
  259. if(lParam)
  260. {
  261. if(!m_TrayIcon.Visible())
  262. {
  263. KillTimer(HIDE_ICON_TIMER);
  264. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  265. }
  266. }
  267. if(wParam)
  268. m_TrayIcon.ShowIcon();
  269. else
  270. m_TrayIcon.HideIcon();
  271. return TRUE;
  272. }
  273. void CMainFrame::OnFirstShowquickpaste()
  274. {
  275. QuickPaste.ShowQPasteWnd(this, TRUE);
  276. }
  277. LRESULT CMainFrame::OnReconnectToCopyChain(WPARAM wParam, LPARAM lParam)
  278. {
  279. if(GetClipboardViewer() != this)
  280. {
  281. //Remove it from the change
  282. ChangeClipboardChain(m_hNextClipboardViewer);
  283. //reset it as the top viewer
  284. m_bCallingSetClipboardViewer = TRUE;
  285. m_hNextClipboardViewer = SetClipboardViewer();
  286. m_bCallingSetClipboardViewer = FALSE;
  287. return TRUE;
  288. }
  289. return FALSE;
  290. }
  291. void CMainFrame::OnFirstReconnecttoclipboardchain()
  292. {
  293. OnReconnectToCopyChain(0, 0);
  294. }
  295. void CMainFrame::OnUpdateFirstReconnecttoclipboardchain(CCmdUI* pCmdUI)
  296. {
  297. if(GetClipboardViewer() == this)
  298. pCmdUI->m_pMenu->DeleteMenu(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, MF_BYCOMMAND);
  299. }
  300. LRESULT CMainFrame::OnGetIsTopView(WPARAM wParam, LPARAM lParam)
  301. {
  302. return (GetClipboardViewer() == this);
  303. }
  304. BOOL CMainFrame::ResetKillDBTimer()
  305. {
  306. KillTimer(KILL_DB_TIMER);
  307. SetTimer(KILL_DB_TIMER, ONE_MINUTE*2, NULL);
  308. return TRUE;
  309. }
  310. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  311. {
  312. long lID = (long)wParam;
  313. if(lID > 0)
  314. {
  315. theApp.m_bHandleClipboardDataChange = false;
  316. theApp.m_bShowingOptions = true;
  317. CCopyProperties props(lID, this);
  318. props.SetHideOnKillFocus(true);
  319. props.DoModal();
  320. theApp.m_bHandleClipboardDataChange = true;
  321. theApp.m_bShowingOptions = false;
  322. }
  323. return TRUE;
  324. }
  325. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  326. {
  327. SetTimer(CLOSE_APP, 100, NULL);
  328. return TRUE;
  329. }