MainFrm.cpp 13 KB

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