MainFrm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. #include "focusdll\focusdll.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. #define WM_ICON_NOTIFY WM_APP+10
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CMainFrame
  20. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  21. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  22. //{{AFX_MSG_MAP(CMainFrame)
  23. ON_WM_CREATE()
  24. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  25. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  26. // ON_WM_CHANGECBCHAIN()
  27. // ON_WM_DRAWCLIPBOARD()
  28. ON_WM_TIMER()
  29. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  30. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  31. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  32. //}}AFX_MSG_MAP
  33. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  34. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  35. ON_MESSAGE(WM_COPYPROPERTIES, OnCopyProperties)
  36. ON_MESSAGE(WM_CLOSE_APP, OnShutDown)
  37. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  38. ON_WM_CLOSE()
  39. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  40. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  41. ON_MESSAGE(WM_FOCUS_CHANGED, OnFocusChanged)
  42. END_MESSAGE_MAP()
  43. static UINT indicators[] =
  44. {
  45. ID_SEPARATOR, // status line indicator
  46. ID_INDICATOR_CAPS,
  47. ID_INDICATOR_NUM,
  48. ID_INDICATOR_SCRL,
  49. };
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMainFrame construction/destruction
  52. CMainFrame::CMainFrame()
  53. {
  54. }
  55. CMainFrame::~CMainFrame()
  56. {
  57. if(g_Opt.m_bUseHookDllForFocus)
  58. StopMonitoringFocusChanges();
  59. CGetSetOptions::SetMainHWND(0);
  60. }
  61. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  62. {
  63. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  64. return -1;
  65. if(g_Opt.m_bUseHookDllForFocus)
  66. MonitorFocusChanges(m_hWnd, WM_FOCUS_CHANGED);
  67. SetWindowText(MAIN_WND_TITLE);
  68. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  69. m_TrayIcon.Create(
  70. NULL, // Let icon deal with its own messages
  71. WM_ICON_NOTIFY, // Icon notify message to use
  72. _T("Ditto"), // tooltip
  73. hIcon,
  74. IDR_MENU, // ID of tray icon
  75. FALSE,
  76. _T(""), // balloon tip
  77. _T(""), // balloon title
  78. NULL, // balloon icon
  79. 20 );
  80. m_TrayIcon.SetSingleClickSelect(TRUE);
  81. m_TrayIcon.MinimiseToTray(this);
  82. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  83. //Only if in release
  84. #ifndef _DEBUG
  85. {
  86. //If not showing the icon show it for 40 seconds so they can get to the option
  87. //in case they can't remember the hot keys or something like that
  88. if(!(CGetSetOptions::GetShowIconInSysTray()))
  89. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  90. }
  91. #endif
  92. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*5, 0);
  93. theApp.Delayed_RemoveOldEntries(ONE_MINUTE*2);
  94. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  95. theApp.AfterMainCreate();
  96. return 0;
  97. }
  98. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  99. {
  100. if( !CFrameWnd::PreCreateWindow(cs) )
  101. return FALSE;
  102. WNDCLASS wc;
  103. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  104. wc.lpfnWndProc = AfxWndProc;
  105. wc.cbClsExtra = 0;
  106. wc.cbWndExtra = 0;
  107. wc.hInstance = AfxGetInstanceHandle();
  108. wc.hIcon = NULL;
  109. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  110. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  111. wc.lpszMenuName = NULL;
  112. wc.lpszClassName = "Ditto";
  113. // Create the QPaste window class
  114. if (!AfxRegisterClass(&wc))
  115. return FALSE;
  116. cs.lpszClass = wc.lpszClassName;
  117. return TRUE;
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CMainFrame diagnostics
  121. #ifdef _DEBUG
  122. void CMainFrame::AssertValid() const
  123. {
  124. CFrameWnd::AssertValid();
  125. }
  126. void CMainFrame::Dump(CDumpContext& dc) const
  127. {
  128. CFrameWnd::Dump(dc);
  129. }
  130. #endif //_DEBUG
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CMainFrame message handlers
  133. void CMainFrame::OnFirstOption()
  134. {
  135. DoOptions(this);
  136. }
  137. void CMainFrame::OnFirstExit()
  138. {
  139. this->SendMessage(WM_CLOSE, 0, 0);
  140. }
  141. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  142. {
  143. if(wParam == theApp.m_pDittoHotKey->m_Atom)
  144. {
  145. if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && QuickPaste.IsWindowVisibleEx())
  146. {
  147. QuickPaste.HideQPasteWnd();
  148. }
  149. else
  150. {
  151. theApp.TargetActiveWindow();
  152. QuickPaste.ShowQPasteWnd(this);
  153. }
  154. }
  155. else if(wParam == theApp.m_pCopyHotKey->m_Atom)
  156. {
  157. theApp.TargetActiveWindow();
  158. theApp.m_bShowCopyProperties = true;
  159. //Simulate the Copy
  160. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  161. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  162. Sleep(100);
  163. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  164. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  165. }
  166. else if(wParam == theApp.m_pPosOne->m_Atom)
  167. {
  168. DoFirstTenPositionsPaste(1);
  169. }
  170. else if(wParam == theApp.m_pPosTwo->m_Atom)
  171. {
  172. DoFirstTenPositionsPaste(2);
  173. }
  174. else if(wParam == theApp.m_pPosThree->m_Atom)
  175. {
  176. DoFirstTenPositionsPaste(3);
  177. }
  178. else if(wParam == theApp.m_pPosFour->m_Atom)
  179. {
  180. DoFirstTenPositionsPaste(4);
  181. }
  182. else if(wParam == theApp.m_pPosFive->m_Atom)
  183. {
  184. DoFirstTenPositionsPaste(5);
  185. }
  186. else if(wParam == theApp.m_pPosSix->m_Atom)
  187. {
  188. DoFirstTenPositionsPaste(6);
  189. }
  190. else if(wParam == theApp.m_pPosSeven->m_Atom)
  191. {
  192. DoFirstTenPositionsPaste(7);
  193. }
  194. else if(wParam == theApp.m_pPosEight->m_Atom)
  195. {
  196. DoFirstTenPositionsPaste(8);
  197. }
  198. else if(wParam == theApp.m_pPosNine->m_Atom)
  199. {
  200. DoFirstTenPositionsPaste(9);
  201. }
  202. else if(wParam == theApp.m_pPosTen->m_Atom)
  203. {
  204. DoFirstTenPositionsPaste(10);
  205. }
  206. return TRUE;
  207. }
  208. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  209. {
  210. try
  211. {
  212. CMainTable Recset;
  213. Recset.m_strSort = "bIsGroup DESC, lDate DESC";
  214. Recset.m_strFilter = "((bIsGroup = TRUE AND lParentID = 0) OR bIsGroup = FALSE)";
  215. Recset.Open("");
  216. if(!Recset.IsEOF())
  217. {
  218. Recset.MoveLast();
  219. if(Recset.GetRecordCount() > nPos)
  220. {
  221. Recset.SetAbsolutePosition(nPos-1);
  222. if(Recset.m_bIsGroup == FALSE)
  223. {
  224. //Don't move these to the top
  225. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  226. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  227. CProcessPaste paste;
  228. paste.GetClipIDs().Add(Recset.m_lID);
  229. paste.m_bActivateTarget = false;
  230. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  231. paste.DoPaste();
  232. theApp.OnPasteCompleted();
  233. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  234. }
  235. }
  236. }
  237. }
  238. catch(CDaoException* e)
  239. {
  240. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  241. ASSERT(0);
  242. e->Delete();
  243. }
  244. }
  245. void CMainFrame::OnTimer(UINT nIDEvent)
  246. {
  247. switch(nIDEvent)
  248. {
  249. case HIDE_ICON_TIMER:
  250. {
  251. m_TrayIcon.HideIcon();
  252. KillTimer(nIDEvent);
  253. break;
  254. }
  255. case KILL_DB_TIMER:
  256. {
  257. if(QuickPaste.CloseQPasteWnd())
  258. {
  259. theApp.CloseDB();
  260. AfxDaoTerm();
  261. KillTimer(KILL_DB_TIMER);
  262. }
  263. break;
  264. }
  265. case REMOVE_OLD_ENTRIES_TIMER:
  266. {
  267. theApp.m_bRemoveOldEntriesPending = false;
  268. RemoveOldEntries();
  269. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  270. break;
  271. }
  272. case CHECK_FOR_UPDATE:
  273. {
  274. KillTimer(CHECK_FOR_UPDATE);
  275. CInternetUpdate Update;
  276. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  277. {
  278. SendMessage(WM_CLOSE, 0, 0);
  279. }
  280. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*60*24, NULL);
  281. break;
  282. }
  283. case CLOSE_APP:
  284. {
  285. if(theApp.m_bShowingOptions == false)
  286. {
  287. PostMessage(WM_CLOSE, 0, 0);
  288. KillTimer(CLOSE_APP);
  289. }
  290. break;
  291. }
  292. case HIDE_ERROR_POPUP:
  293. {
  294. KillTimer(HIDE_ERROR_POPUP);
  295. if(theApp.m_pcpSendRecieveError)
  296. {
  297. delete theApp.m_pcpSendRecieveError;
  298. theApp.m_pcpSendRecieveError = NULL;
  299. }
  300. break;
  301. }
  302. }
  303. CFrameWnd::OnTimer(nIDEvent);
  304. }
  305. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  306. {
  307. if(lParam)
  308. {
  309. if(!m_TrayIcon.Visible())
  310. {
  311. KillTimer(HIDE_ICON_TIMER);
  312. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  313. }
  314. }
  315. if(wParam)
  316. m_TrayIcon.ShowIcon();
  317. else
  318. m_TrayIcon.HideIcon();
  319. return TRUE;
  320. }
  321. void CMainFrame::OnFirstShowquickpaste()
  322. {
  323. QuickPaste.ShowQPasteWnd(this, TRUE);
  324. }
  325. void CMainFrame::OnFirstToggleConnectCV()
  326. {
  327. theApp.ToggleConnectCV();
  328. }
  329. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI* pCmdUI)
  330. {
  331. theApp.UpdateMenuConnectCV( pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV );
  332. }
  333. BOOL CMainFrame::ResetKillDBTimer()
  334. {
  335. KillTimer(KILL_DB_TIMER);
  336. SetTimer(KILL_DB_TIMER, ONE_MINUTE*60, NULL);
  337. return TRUE;
  338. }
  339. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  340. {
  341. long lID = (long)wParam;
  342. if(lID > 0)
  343. {
  344. bool bOldState = theApp.EnableCbCopy(false);
  345. theApp.m_bShowingOptions = true;
  346. CCopyProperties props(lID, this);
  347. props.SetHideOnKillFocus(true);
  348. props.DoModal();
  349. theApp.m_bShowingOptions = false;
  350. theApp.EnableCbCopy( bOldState );
  351. }
  352. return TRUE;
  353. }
  354. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  355. {
  356. SetTimer(CLOSE_APP, 100, NULL);
  357. return TRUE;
  358. }
  359. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  360. {
  361. // if the delay is undesirable, this could be altered to save one at a time,
  362. // allowing the processing of other messages between saving clips.
  363. theApp.SaveCopyClips();
  364. return TRUE;
  365. }
  366. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  367. {
  368. // target before mouse messages change the focus
  369. if( theApp.m_bShowingQuickPaste &&
  370. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  371. { theApp.TargetActiveWindow(); }
  372. return CFrameWnd::PreTranslateMessage(pMsg);
  373. }
  374. void CMainFrame::OnClose()
  375. {
  376. theApp.BeforeMainClose();
  377. CFrameWnd::OnClose();
  378. }
  379. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  380. {
  381. LogSendRecieveInfo("---------Start of OnAddToDatabaseFromSocket");
  382. CClipList *pClipList = (CClipList*)wParam;
  383. if(pClipList == NULL)
  384. {
  385. LogSendRecieveInfo("---------ERROR pClipList == NULL");
  386. return FALSE;
  387. }
  388. BOOL bSetToClipBoard = (BOOL)lParam;
  389. if(bSetToClipBoard)
  390. {
  391. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  392. CClip *pClip = pClipList->GetTail();
  393. if(pClip)
  394. {
  395. CClip NewClip;
  396. NewClip = *pClip;
  397. LogSendRecieveInfo("---------After =");
  398. CProcessPaste paste;
  399. //Don't send the paste just load it into memory
  400. paste.m_bSendPaste = false;
  401. paste.m_pOle->LoadFormats(&NewClip.m_Formats);
  402. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  403. LogSendRecieveInfo("---------After LoadFormats");
  404. paste.DoPaste();
  405. }
  406. else
  407. {
  408. LogSendRecieveInfo("---------GetTail returned NULL");
  409. }
  410. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  411. }
  412. pClipList->AddToDB(true);
  413. LogSendRecieveInfo("---------After AddToDB");
  414. CClip *pClip = pClipList->GetTail();
  415. if(pClip)
  416. {
  417. theApp.m_FocusID = pClip->m_ID;
  418. }
  419. theApp.RefreshView();
  420. delete pClipList;
  421. pClipList = NULL;
  422. LogSendRecieveInfo("---------End of OnAddToDatabaseFromSocket");
  423. return TRUE;
  424. }
  425. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  426. {
  427. KillTimer(HIDE_ERROR_POPUP);
  428. if(theApp.m_pcpSendRecieveError)
  429. {
  430. CString csOldText = theApp.m_pcpSendRecieveError->m_csToolTipText;
  431. CString csNewText = (char*)wParam;
  432. CString csCombinedText = csOldText + "\n" + csNewText;
  433. theApp.m_pcpSendRecieveError->Show(csCombinedText, CPoint(20, 20), true);
  434. }
  435. else
  436. {
  437. theApp.m_pcpSendRecieveError = new CPopup(20, 20, ::GetForegroundWindow());
  438. CString csNewText = (char*)wParam;
  439. theApp.m_pcpSendRecieveError->Show(csNewText);
  440. }
  441. SetTimer(HIDE_ERROR_POPUP, 6000, NULL);
  442. return TRUE;
  443. }
  444. LRESULT CMainFrame::OnFocusChanged(WPARAM wParam, LPARAM lParam)
  445. {
  446. if(g_Opt.m_bUseHookDllForFocus == FALSE)
  447. return TRUE;
  448. HWND hFocus = (HWND)wParam;
  449. HWND hParent = hFocus;
  450. HWND hLastGoodParent = hParent;
  451. //only proceed if something changed
  452. if(theApp.m_hTargetWnd == hFocus)
  453. return TRUE;
  454. char cWindowText[100];
  455. ::GetWindowText(hFocus, cWindowText, 100);
  456. HWND hTray = ::FindWindow("Shell_TrayWnd", "");
  457. while(true)
  458. {
  459. hParent = ::GetParent(hParent);
  460. if(hParent == NULL)
  461. break;
  462. hLastGoodParent = hParent;
  463. }
  464. //If the parent is ditto or the tray icon then don't set focus to that window
  465. if(hLastGoodParent != m_hWnd && hLastGoodParent != hTray)
  466. {
  467. theApp.m_hTargetWnd = hFocus;
  468. if(theApp.QPasteWnd() )
  469. theApp.QPasteWnd()->UpdateStatus(true);
  470. }
  471. return TRUE;
  472. }