MainFrm.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include ".\mainfrm.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. #define WM_ICON_NOTIFY WM_APP+10
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMainFrame
  19. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  20. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  21. //{{AFX_MSG_MAP(CMainFrame)
  22. ON_WM_CREATE()
  23. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  24. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  25. // ON_WM_CHANGECBCHAIN()
  26. // ON_WM_DRAWCLIPBOARD()
  27. ON_WM_TIMER()
  28. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  29. ON_COMMAND(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, OnFirstReconnecttoclipboardchain)
  30. ON_UPDATE_COMMAND_UI(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, OnUpdateFirstReconnecttoclipboardchain)
  31. //}}AFX_MSG_MAP
  32. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  33. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  34. ON_MESSAGE(WM_COPYPROPERTIES, OnCopyProperties)
  35. ON_MESSAGE(WM_CLOSE_APP, OnShutDown)
  36. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  37. ON_WM_CLOSE()
  38. END_MESSAGE_MAP()
  39. static UINT indicators[] =
  40. {
  41. ID_SEPARATOR, // status line indicator
  42. ID_INDICATOR_CAPS,
  43. ID_INDICATOR_NUM,
  44. ID_INDICATOR_SCRL,
  45. };
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CMainFrame construction/destruction
  48. CMainFrame::CMainFrame()
  49. {
  50. }
  51. CMainFrame::~CMainFrame()
  52. {
  53. CGetSetOptions::SetMainHWND(0);
  54. }
  55. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  56. {
  57. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  58. return -1;
  59. SetWindowText(MAIN_WND_TITLE);
  60. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. m_TrayIcon.Create(
  62. NULL, // Let icon deal with its own messages
  63. WM_ICON_NOTIFY, // Icon notify message to use
  64. _T("Ditto"), // tooltip
  65. hIcon,
  66. IDR_MENU, // ID of tray icon
  67. FALSE,
  68. _T(""), // balloon tip
  69. _T(""), // balloon title
  70. NULL, // balloon icon
  71. 20 );
  72. m_TrayIcon.MinimiseToTray(this);
  73. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  74. //Only if in release
  75. #ifndef _DEBUG
  76. {
  77. //If not showing the icon show it for 40 seconds so they can get to the option
  78. //in case they can't remember the hot keys or something like that
  79. if(!(CGetSetOptions::GetShowIconInSysTray()))
  80. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  81. }
  82. #endif
  83. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*5, 0);
  84. theApp.Delayed_RemoveOldEntries(ONE_MINUTE*2);
  85. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  86. // QuickPaste.Create( this );
  87. theApp.AfterMainCreate();
  88. return 0;
  89. }
  90. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  91. {
  92. if( !CFrameWnd::PreCreateWindow(cs) )
  93. return FALSE;
  94. WNDCLASS wc;
  95. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  96. wc.lpfnWndProc = AfxWndProc;
  97. wc.cbClsExtra = 0;
  98. wc.cbWndExtra = 0;
  99. wc.hInstance = AfxGetInstanceHandle();
  100. wc.hIcon = NULL;
  101. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  102. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  103. wc.lpszMenuName = NULL;
  104. wc.lpszClassName = "Ditto";
  105. // Create the QPaste window class
  106. if (!AfxRegisterClass(&wc))
  107. return FALSE;
  108. cs.lpszClass = wc.lpszClassName;
  109. return TRUE;
  110. }
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CMainFrame diagnostics
  113. #ifdef _DEBUG
  114. void CMainFrame::AssertValid() const
  115. {
  116. CFrameWnd::AssertValid();
  117. }
  118. void CMainFrame::Dump(CDumpContext& dc) const
  119. {
  120. CFrameWnd::Dump(dc);
  121. }
  122. #endif //_DEBUG
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CMainFrame message handlers
  125. void CMainFrame::OnFirstOption()
  126. {
  127. DoOptions(this);
  128. }
  129. void CMainFrame::OnFirstExit()
  130. {
  131. this->SendMessage(WM_CLOSE, 0, 0);
  132. }
  133. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  134. {
  135. if( wParam == theApp.m_pDittoHotKey->m_Atom )
  136. {
  137. theApp.TargetActiveWindow();
  138. QuickPaste.ShowQPasteWnd(this);
  139. }
  140. else if( wParam == theApp.m_pCopyHotKey->m_Atom )
  141. {
  142. theApp.TargetActiveWindow();
  143. theApp.m_bShowCopyProperties = true;
  144. //Simulate the Copy
  145. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  146. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  147. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  148. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  149. }
  150. return TRUE;
  151. }
  152. void CMainFrame::OnTimer(UINT nIDEvent)
  153. {
  154. switch(nIDEvent)
  155. {
  156. case HIDE_ICON_TIMER:
  157. {
  158. m_TrayIcon.HideIcon();
  159. KillTimer(nIDEvent);
  160. break;
  161. }
  162. case KILL_DB_TIMER:
  163. {
  164. if(QuickPaste.CloseQPasteWnd())
  165. {
  166. theApp.CloseDB();
  167. AfxDaoTerm();
  168. KillTimer(KILL_DB_TIMER);
  169. }
  170. break;
  171. }
  172. case REMOVE_OLD_ENTRIES_TIMER:
  173. {
  174. theApp.m_bRemoveOldEntriesPending = false;
  175. RemoveOldEntries();
  176. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  177. break;
  178. }
  179. case CHECK_FOR_UPDATE:
  180. {
  181. CInternetUpdate Update;
  182. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  183. {
  184. SendMessage(WM_CLOSE, 0, 0);
  185. }
  186. KillTimer(CHECK_FOR_UPDATE);
  187. break;
  188. }
  189. case CLOSE_APP:
  190. {
  191. if(theApp.m_bShowingOptions == false)
  192. {
  193. PostMessage(WM_CLOSE, 0, 0);
  194. KillTimer(CLOSE_APP);
  195. }
  196. break;
  197. }
  198. }
  199. CFrameWnd::OnTimer(nIDEvent);
  200. }
  201. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  202. {
  203. if(lParam)
  204. {
  205. if(!m_TrayIcon.Visible())
  206. {
  207. KillTimer(HIDE_ICON_TIMER);
  208. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  209. }
  210. }
  211. if(wParam)
  212. m_TrayIcon.ShowIcon();
  213. else
  214. m_TrayIcon.HideIcon();
  215. return TRUE;
  216. }
  217. void CMainFrame::OnFirstShowquickpaste()
  218. {
  219. QuickPaste.ShowQPasteWnd(this, TRUE);
  220. }
  221. void CMainFrame::OnFirstReconnecttoclipboardchain()
  222. {
  223. ::PostMessage( theApp.GetClipboardViewer(), WM_CV_RECONNECT, 0, 0 );
  224. }
  225. void CMainFrame::OnUpdateFirstReconnecttoclipboardchain(CCmdUI* pCmdUI)
  226. {
  227. if( theApp.IsClipboardViewerConnected() )
  228. pCmdUI->m_pMenu->DeleteMenu(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, MF_BYCOMMAND);
  229. }
  230. BOOL CMainFrame::ResetKillDBTimer()
  231. {
  232. KillTimer(KILL_DB_TIMER);
  233. SetTimer(KILL_DB_TIMER, ONE_MINUTE*2, NULL);
  234. return TRUE;
  235. }
  236. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  237. {
  238. long lID = (long)wParam;
  239. if(lID > 0)
  240. {
  241. bool bOldState = theApp.EnableCbCopy(false);
  242. theApp.m_bShowingOptions = true;
  243. CCopyProperties props(lID, this);
  244. props.SetHideOnKillFocus(true);
  245. props.DoModal();
  246. theApp.m_bShowingOptions = false;
  247. theApp.EnableCbCopy( bOldState );
  248. }
  249. return TRUE;
  250. }
  251. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  252. {
  253. SetTimer(CLOSE_APP, 100, NULL);
  254. return TRUE;
  255. }
  256. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  257. {
  258. // if the delay is undesirable, this could be altered to save one at a time,
  259. // allowing the processing of other messages between saving clips.
  260. theApp.SaveCopyClips();
  261. return TRUE;
  262. }
  263. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  264. {
  265. // target before mouse messages change the focus
  266. if( theApp.m_bShowingQuickPaste &&
  267. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  268. { theApp.TargetActiveWindow(); }
  269. return CFrameWnd::PreTranslateMessage(pMsg);
  270. }
  271. void CMainFrame::OnClose()
  272. {
  273. theApp.BeforeMainClose();
  274. CFrameWnd::OnClose();
  275. }