MainFrm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. #include "HyperLink.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define WM_ICON_NOTIFY WM_APP+10
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMainFrame
  21. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  22. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  23. //{{AFX_MSG_MAP(CMainFrame)
  24. ON_WM_CREATE()
  25. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  26. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  27. ON_WM_TIMER()
  28. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  29. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  30. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  31. ON_COMMAND(ID_FIRST_HELP, OnFirstHelp)
  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. QuickPaste.UpdateFont();
  137. }
  138. void CMainFrame::OnFirstExit()
  139. {
  140. this->SendMessage(WM_CLOSE, 0, 0);
  141. }
  142. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  143. {
  144. if(wParam == theApp.m_pDittoHotKey->m_Atom)
  145. {
  146. if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && QuickPaste.IsWindowVisibleEx())
  147. {
  148. QuickPaste.HideQPasteWnd();
  149. }
  150. else
  151. {
  152. theApp.TargetActiveWindow();
  153. QuickPaste.ShowQPasteWnd(this);
  154. }
  155. }
  156. else if(wParam == theApp.m_pCopyHotKey->m_Atom)
  157. {
  158. theApp.TargetActiveWindow();
  159. theApp.m_bShowCopyProperties = true;
  160. //Simulate the Copy
  161. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  162. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  163. Sleep(100);
  164. keybd_event('C', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  165. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  166. }
  167. else if(wParam == theApp.m_pPosOne->m_Atom)
  168. {
  169. DoFirstTenPositionsPaste(1);
  170. }
  171. else if(wParam == theApp.m_pPosTwo->m_Atom)
  172. {
  173. DoFirstTenPositionsPaste(2);
  174. }
  175. else if(wParam == theApp.m_pPosThree->m_Atom)
  176. {
  177. DoFirstTenPositionsPaste(3);
  178. }
  179. else if(wParam == theApp.m_pPosFour->m_Atom)
  180. {
  181. DoFirstTenPositionsPaste(4);
  182. }
  183. else if(wParam == theApp.m_pPosFive->m_Atom)
  184. {
  185. DoFirstTenPositionsPaste(5);
  186. }
  187. else if(wParam == theApp.m_pPosSix->m_Atom)
  188. {
  189. DoFirstTenPositionsPaste(6);
  190. }
  191. else if(wParam == theApp.m_pPosSeven->m_Atom)
  192. {
  193. DoFirstTenPositionsPaste(7);
  194. }
  195. else if(wParam == theApp.m_pPosEight->m_Atom)
  196. {
  197. DoFirstTenPositionsPaste(8);
  198. }
  199. else if(wParam == theApp.m_pPosNine->m_Atom)
  200. {
  201. DoFirstTenPositionsPaste(9);
  202. }
  203. else if(wParam == theApp.m_pPosTen->m_Atom)
  204. {
  205. DoFirstTenPositionsPaste(10);
  206. }
  207. return TRUE;
  208. }
  209. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  210. {
  211. try
  212. {
  213. CMainTable Recset;
  214. Recset.m_strSort = "bIsGroup DESC, lDate DESC";
  215. Recset.m_strFilter = "((bIsGroup = TRUE AND lParentID = 0) OR bIsGroup = FALSE)";
  216. Recset.Open("");
  217. if(!Recset.IsEOF())
  218. {
  219. Recset.MoveLast();
  220. if(Recset.GetRecordCount() > (nPos-1))
  221. {
  222. Recset.SetAbsolutePosition(nPos-1);
  223. if(Recset.m_bIsGroup == FALSE)
  224. {
  225. //Don't move these to the top
  226. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  227. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  228. CProcessPaste paste;
  229. paste.GetClipIDs().Add(Recset.m_lID);
  230. paste.m_bActivateTarget = false;
  231. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  232. paste.DoPaste();
  233. theApp.OnPasteCompleted();
  234. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  235. }
  236. }
  237. }
  238. }
  239. catch(CDaoException* e)
  240. {
  241. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  242. ASSERT(0);
  243. e->Delete();
  244. }
  245. }
  246. void CMainFrame::OnTimer(UINT nIDEvent)
  247. {
  248. switch(nIDEvent)
  249. {
  250. case HIDE_ICON_TIMER:
  251. {
  252. m_TrayIcon.HideIcon();
  253. KillTimer(nIDEvent);
  254. break;
  255. }
  256. case KILL_DB_TIMER:
  257. {
  258. if(QuickPaste.CloseQPasteWnd())
  259. {
  260. theApp.CloseDB();
  261. AfxDaoTerm();
  262. KillTimer(KILL_DB_TIMER);
  263. }
  264. break;
  265. }
  266. case REMOVE_OLD_ENTRIES_TIMER:
  267. {
  268. theApp.m_bRemoveOldEntriesPending = false;
  269. RemoveOldEntries();
  270. KillTimer(REMOVE_OLD_ENTRIES_TIMER);
  271. break;
  272. }
  273. case CHECK_FOR_UPDATE:
  274. {
  275. KillTimer(CHECK_FOR_UPDATE);
  276. CInternetUpdate Update;
  277. if(Update.CheckForUpdate(NULL, TRUE, FALSE))
  278. {
  279. SendMessage(WM_CLOSE, 0, 0);
  280. }
  281. SetTimer(CHECK_FOR_UPDATE, ONE_MINUTE*60*24, NULL);
  282. break;
  283. }
  284. case CLOSE_APP:
  285. {
  286. if(theApp.m_bShowingOptions == false)
  287. {
  288. PostMessage(WM_CLOSE, 0, 0);
  289. KillTimer(CLOSE_APP);
  290. }
  291. break;
  292. }
  293. case HIDE_ERROR_POPUP:
  294. {
  295. KillTimer(HIDE_ERROR_POPUP);
  296. if(theApp.m_pcpSendRecieveError)
  297. {
  298. delete theApp.m_pcpSendRecieveError;
  299. theApp.m_pcpSendRecieveError = NULL;
  300. }
  301. break;
  302. }
  303. }
  304. CFrameWnd::OnTimer(nIDEvent);
  305. }
  306. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  307. {
  308. if(lParam)
  309. {
  310. if(!m_TrayIcon.Visible())
  311. {
  312. KillTimer(HIDE_ICON_TIMER);
  313. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  314. }
  315. }
  316. if(wParam)
  317. m_TrayIcon.ShowIcon();
  318. else
  319. m_TrayIcon.HideIcon();
  320. return TRUE;
  321. }
  322. void CMainFrame::OnFirstShowquickpaste()
  323. {
  324. QuickPaste.ShowQPasteWnd(this, TRUE);
  325. }
  326. void CMainFrame::OnFirstToggleConnectCV()
  327. {
  328. theApp.ToggleConnectCV();
  329. }
  330. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI* pCmdUI)
  331. {
  332. theApp.UpdateMenuConnectCV( pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV );
  333. }
  334. BOOL CMainFrame::ResetKillDBTimer()
  335. {
  336. KillTimer(KILL_DB_TIMER);
  337. SetTimer(KILL_DB_TIMER, 20000, NULL);
  338. return TRUE;
  339. }
  340. LRESULT CMainFrame::OnCopyProperties(WPARAM wParam, LPARAM lParam)
  341. {
  342. long lID = (long)wParam;
  343. if(lID > 0)
  344. {
  345. bool bOldState = theApp.EnableCbCopy(false);
  346. theApp.m_bShowingOptions = true;
  347. CCopyProperties props(lID, this);
  348. props.SetHideOnKillFocus(true);
  349. props.DoModal();
  350. theApp.m_bShowingOptions = false;
  351. theApp.EnableCbCopy( bOldState );
  352. }
  353. return TRUE;
  354. }
  355. LRESULT CMainFrame::OnShutDown(WPARAM wParam, LPARAM lParam)
  356. {
  357. SetTimer(CLOSE_APP, 100, NULL);
  358. return TRUE;
  359. }
  360. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  361. {
  362. // if the delay is undesirable, this could be altered to save one at a time,
  363. // allowing the processing of other messages between saving clips.
  364. theApp.SaveCopyClips();
  365. return TRUE;
  366. }
  367. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  368. {
  369. // target before mouse messages change the focus
  370. if( theApp.m_bShowingQuickPaste &&
  371. WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST )
  372. { theApp.TargetActiveWindow(); }
  373. return CFrameWnd::PreTranslateMessage(pMsg);
  374. }
  375. void CMainFrame::OnClose()
  376. {
  377. theApp.BeforeMainClose();
  378. CFrameWnd::OnClose();
  379. }
  380. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  381. {
  382. LogSendRecieveInfo("---------Start of OnAddToDatabaseFromSocket");
  383. CClipList *pClipList = (CClipList*)wParam;
  384. if(pClipList == NULL)
  385. {
  386. LogSendRecieveInfo("---------ERROR pClipList == NULL");
  387. return FALSE;
  388. }
  389. BOOL bSetToClipBoard = (BOOL)lParam;
  390. if(bSetToClipBoard)
  391. {
  392. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  393. CClip *pClip = pClipList->GetTail();
  394. if(pClip)
  395. {
  396. CClip NewClip;
  397. NewClip = *pClip;
  398. LogSendRecieveInfo("---------After =");
  399. CProcessPaste paste;
  400. //Don't send the paste just load it into memory
  401. paste.m_bSendPaste = false;
  402. paste.m_pOle->LoadFormats(&NewClip.m_Formats, false);
  403. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  404. LogSendRecieveInfo("---------After LoadFormats");
  405. paste.DoPaste();
  406. }
  407. else
  408. {
  409. LogSendRecieveInfo("---------GetTail returned NULL");
  410. }
  411. LogSendRecieveInfo("---------Start of Set to ClipBoard");
  412. }
  413. pClipList->AddToDB(true);
  414. LogSendRecieveInfo("---------After AddToDB");
  415. CClip *pClip = pClipList->GetTail();
  416. if(pClip)
  417. {
  418. theApp.m_FocusID = pClip->m_ID;
  419. }
  420. theApp.RefreshView();
  421. delete pClipList;
  422. pClipList = NULL;
  423. LogSendRecieveInfo("---------End of OnAddToDatabaseFromSocket");
  424. theApp.Delayed_RemoveOldEntries(60000);
  425. return TRUE;
  426. }
  427. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  428. {
  429. KillTimer(HIDE_ERROR_POPUP);
  430. if(theApp.m_pcpSendRecieveError)
  431. {
  432. CString csOldText = theApp.m_pcpSendRecieveError->m_csToolTipText;
  433. CString csNewText = (char*)wParam;
  434. CString csCombinedText = csOldText + "\n" + csNewText;
  435. theApp.m_pcpSendRecieveError->Show(csCombinedText, CPoint(20, 20), true);
  436. }
  437. else
  438. {
  439. theApp.m_pcpSendRecieveError = new CPopup(20, 20, ::GetForegroundWindow());
  440. CString csNewText = (char*)wParam;
  441. theApp.m_pcpSendRecieveError->Show(csNewText);
  442. }
  443. SetTimer(HIDE_ERROR_POPUP, 6000, NULL);
  444. return TRUE;
  445. }
  446. LRESULT CMainFrame::OnFocusChanged(WPARAM wParam, LPARAM lParam)
  447. {
  448. if(g_Opt.m_bUseHookDllForFocus == FALSE)
  449. return TRUE;
  450. HWND hFocus = (HWND)wParam;
  451. HWND hParent = hFocus;
  452. HWND hLastGoodParent = hParent;
  453. //only proceed if something changed
  454. if(theApp.m_hTargetWnd == hFocus)
  455. return TRUE;
  456. char cWindowText[100];
  457. ::GetWindowText(hFocus, cWindowText, 100);
  458. HWND hTray = ::FindWindow("Shell_TrayWnd", "");
  459. while(true)
  460. {
  461. hParent = ::GetParent(hParent);
  462. if(hParent == NULL)
  463. break;
  464. hLastGoodParent = hParent;
  465. }
  466. //If the parent is ditto or the tray icon then don't set focus to that window
  467. if(hLastGoodParent != m_hWnd && hLastGoodParent != hTray)
  468. {
  469. theApp.m_hTargetWnd = hFocus;
  470. if(theApp.QPasteWnd() )
  471. theApp.QPasteWnd()->UpdateStatus(true);
  472. }
  473. return TRUE;
  474. }
  475. void CMainFrame::OnFirstHelp()
  476. {
  477. CHyperLink::GotoURL("Help\\DittoGettingStarted.htm", SW_SHOW);
  478. }