MainFrm.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. else if(wParam == theApp.m_pPosOne->m_Atom)
  151. {
  152. DoFirstTenPositionsPaste(1);
  153. }
  154. else if(wParam == theApp.m_pPosTwo->m_Atom)
  155. {
  156. DoFirstTenPositionsPaste(2);
  157. }
  158. else if(wParam == theApp.m_pPosThree->m_Atom)
  159. {
  160. DoFirstTenPositionsPaste(3);
  161. }
  162. else if(wParam == theApp.m_pPosFour->m_Atom)
  163. {
  164. DoFirstTenPositionsPaste(4);
  165. }
  166. else if(wParam == theApp.m_pPosFive->m_Atom)
  167. {
  168. DoFirstTenPositionsPaste(5);
  169. }
  170. else if(wParam == theApp.m_pPosSix->m_Atom)
  171. {
  172. DoFirstTenPositionsPaste(6);
  173. }
  174. else if(wParam == theApp.m_pPosSeven->m_Atom)
  175. {
  176. DoFirstTenPositionsPaste(7);
  177. }
  178. else if(wParam == theApp.m_pPosEight->m_Atom)
  179. {
  180. DoFirstTenPositionsPaste(8);
  181. }
  182. else if(wParam == theApp.m_pPosNine->m_Atom)
  183. {
  184. DoFirstTenPositionsPaste(9);
  185. }
  186. else if(wParam == theApp.m_pPosTen->m_Atom)
  187. {
  188. DoFirstTenPositionsPaste(10);
  189. }
  190. return TRUE;
  191. }
  192. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  193. {
  194. try
  195. {
  196. CMainTable Recset;
  197. Recset.m_strSort = "bIsGroup DESC, lDate DESC";
  198. Recset.m_strFilter = "((bIsGroup = TRUE AND lParentID = 0) OR bIsGroup = FALSE)";
  199. Recset.Open("");
  200. if(!Recset.IsEOF())
  201. {
  202. Recset.MoveLast();
  203. if(Recset.GetRecordCount() > nPos)
  204. {
  205. Recset.SetAbsolutePosition(nPos-1);
  206. if(Recset.m_bIsGroup == FALSE)
  207. {
  208. //Don't move these to the top
  209. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  210. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  211. CProcessPaste paste;
  212. paste.GetClipIDs().Add(Recset.m_lID);
  213. paste.m_bActivateTarget = false;
  214. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  215. paste.DoPaste();
  216. theApp.OnPasteCompleted();
  217. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  218. }
  219. }
  220. }
  221. }
  222. catch(CDaoException* e)
  223. {
  224. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  225. ASSERT(0);
  226. e->Delete();
  227. }
  228. }
  229. void CMainFrame::OnTimer(UINT nIDEvent)
  230. {
  231. switch(nIDEvent)
  232. {
  233. case HIDE_ICON_TIMER:
  234. {
  235. m_TrayIcon.HideIcon();
  236. KillTimer(nIDEvent);
  237. break;
  238. }
  239. case KILL_DB_TIMER:
  240. {
  241. if(QuickPaste.CloseQPasteWnd())
  242. {
  243. theApp.CloseDB();
  244. AfxDaoTerm();
  245. KillTimer(KILL_DB_TIMER);
  246. }
  247. break;
  248. }
  249. case REMOVE_OLD_ENTRIES_TIMER:
  250. {
  251. theApp.m_bRemoveOldEntriesPending = false;
  252. RemoveOldEntries();
  253. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  254. break;
  255. }
  256. case CHECK_FOR_UPDATE:
  257. {
  258. CInternetUpdate Update;
  259. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  260. {
  261. SendMessage(WM_CLOSE, 0, 0);
  262. }
  263. KillTimer(CHECK_FOR_UPDATE);
  264. break;
  265. }
  266. case CLOSE_APP:
  267. {
  268. if(theApp.m_bShowingOptions == false)
  269. {
  270. PostMessage(WM_CLOSE, 0, 0);
  271. KillTimer(CLOSE_APP);
  272. }
  273. break;
  274. }
  275. }
  276. CFrameWnd::OnTimer(nIDEvent);
  277. }
  278. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  279. {
  280. if(lParam)
  281. {
  282. if(!m_TrayIcon.Visible())
  283. {
  284. KillTimer(HIDE_ICON_TIMER);
  285. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  286. }
  287. }
  288. if(wParam)
  289. m_TrayIcon.ShowIcon();
  290. else
  291. m_TrayIcon.HideIcon();
  292. return TRUE;
  293. }
  294. void CMainFrame::OnFirstShowquickpaste()
  295. {
  296. QuickPaste.ShowQPasteWnd(this, TRUE);
  297. }
  298. void CMainFrame::OnFirstReconnecttoclipboardchain()
  299. {
  300. ::PostMessage( theApp.GetClipboardViewer(), WM_CV_RECONNECT, 0, 0 );
  301. }
  302. void CMainFrame::OnUpdateFirstReconnecttoclipboardchain(CCmdUI* pCmdUI)
  303. {
  304. if( theApp.IsClipboardViewerConnected() )
  305. pCmdUI->m_pMenu->DeleteMenu(ID_FIRST_RECONNECTTOCLIPBOARDCHAIN, MF_BYCOMMAND);
  306. }
  307. BOOL CMainFrame::ResetKillDBTimer()
  308. {
  309. KillTimer(KILL_DB_TIMER);
  310. SetTimer(KILL_DB_TIMER, ONE_MINUTE*2, NULL);
  311. return TRUE;
  312. }
  313. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  314. {
  315. long lID = (long)wParam;
  316. if(lID > 0)
  317. {
  318. bool bOldState = theApp.EnableCbCopy(false);
  319. theApp.m_bShowingOptions = true;
  320. CCopyProperties props(lID, this);
  321. props.SetHideOnKillFocus(true);
  322. props.DoModal();
  323. theApp.m_bShowingOptions = false;
  324. theApp.EnableCbCopy( bOldState );
  325. }
  326. return TRUE;
  327. }
  328. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  329. {
  330. SetTimer(CLOSE_APP, 100, NULL);
  331. return TRUE;
  332. }
  333. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  334. {
  335. // if the delay is undesirable, this could be altered to save one at a time,
  336. // allowing the processing of other messages between saving clips.
  337. theApp.SaveCopyClips();
  338. return TRUE;
  339. }
  340. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  341. {
  342. // target before mouse messages change the focus
  343. if( theApp.m_bShowingQuickPaste &&
  344. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  345. { theApp.TargetActiveWindow(); }
  346. return CFrameWnd::PreTranslateMessage(pMsg);
  347. }
  348. void CMainFrame::OnClose()
  349. {
  350. theApp.BeforeMainClose();
  351. CFrameWnd::OnClose();
  352. }