MainFrm.cpp 13 KB

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