MainFrm.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. #include "tinyxml.h"
  14. #include "Path.h"
  15. #include "DittoCopyBuffer.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. #define WM_ICON_NOTIFY WM_APP+10
  22. #define MYWM_NOTIFYICON (WM_USER+1)
  23. bool CShowMainFrame::m_bShowingMainFrame = false;
  24. CShowMainFrame::CShowMainFrame(): m_bHideMainFrameOnExit(false), m_hWnd(NULL)
  25. {
  26. if(m_bShowingMainFrame == false)
  27. {
  28. theApp.m_pMainFrame->m_TrayIcon.MaximiseFromTray(theApp.m_pMainFrame);
  29. m_bHideMainFrameOnExit = true;
  30. m_bShowingMainFrame = true;
  31. }
  32. m_hWnd = theApp.m_pMainFrame->GetSafeHwnd();
  33. }
  34. CShowMainFrame::~CShowMainFrame()
  35. {
  36. if(m_bHideMainFrameOnExit && m_hWnd && ::IsWindow(m_hWnd))
  37. {
  38. theApp.m_pMainFrame->m_TrayIcon.MinimiseToTray(theApp.m_pMainFrame);
  39. m_bShowingMainFrame = false;
  40. }
  41. }
  42. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  43. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  44. //{{AFX_MSG_MAP(CMainFrame)
  45. ON_WM_CREATE()
  46. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  47. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  48. ON_WM_TIMER()
  49. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  50. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  51. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  52. ON_COMMAND(ID_FIRST_HELP, OnFirstHelp)
  53. //}}AFX_MSG_MAP
  54. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  55. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  56. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  57. ON_WM_CLOSE()
  58. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  59. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  60. ON_MESSAGE(WM_FOCUS_CHANGED, OnFocusChanged)
  61. ON_MESSAGE(WM_CUSTOMIZE_TRAY_MENU, OnCustomizeTrayMenu)
  62. ON_COMMAND(ID_FIRST_IMPORT, OnFirstImport)
  63. ON_MESSAGE(WM_EDIT_WND_CLOSING, OnEditWndClose)
  64. ON_WM_DESTROY()
  65. ON_COMMAND(ID_FIRST_NEWCLIP, OnFirstNewclip)
  66. ON_MESSAGE(WM_SET_CONNECTED, OnSetConnected)
  67. ON_MESSAGE(WM_LOAD_ClIP_ON_CLIPBOARD, OnLoadClipOnClipboard)
  68. END_MESSAGE_MAP()
  69. static UINT indicators[] =
  70. {
  71. ID_SEPARATOR, // status line indicator
  72. ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
  73. };
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMainFrame construction/destruction
  76. CMainFrame::CMainFrame()
  77. {
  78. m_pEditFrameWnd = NULL;
  79. m_keyStateModifiers = 0;
  80. m_startKeyStateTime = 0;
  81. m_bMovedSelectionMoveKeyState = false;
  82. m_keyModifiersTimerCount = 0;
  83. }
  84. CMainFrame::~CMainFrame()
  85. {
  86. if(g_Opt.m_bUseHookDllForFocus)
  87. {
  88. Log(_T("Unloading focus dll for tracking focus changes"));
  89. StopMonitoringFocusChanges();
  90. }
  91. CGetSetOptions::SetMainHWND(0);
  92. }
  93. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  94. {
  95. if(CFrameWnd::OnCreate(lpCreateStruct) == - 1)
  96. {
  97. return - 1;
  98. }
  99. //Center the main window so message boxes are in the center
  100. CRect rcScreen;
  101. GetMonitorRect(0, &rcScreen);
  102. CPoint cpCenter = rcScreen.CenterPoint();
  103. MoveWindow(cpCenter.x, cpCenter.x, - 2, - 2);
  104. //Then set the main window to transparent so it's never shown
  105. //if it is shown then only the task tray icon
  106. m_Transparency.SetTransparent(m_hWnd, 0, true);
  107. SetWindowText(_T(""));
  108. if(g_Opt.m_bUseHookDllForFocus)
  109. {
  110. Log(_T("Loading hook dll to track focus changes"));
  111. MonitorFocusChanges(m_hWnd, WM_FOCUS_CHANGED);
  112. }
  113. else
  114. {
  115. Log(_T("Setting polling timer to track focus"));
  116. SetTimer(ACTIVE_WINDOW_TIMER, g_Opt.FocusWndTimerTimeout(), 0);
  117. }
  118. SetWindowText(_T("Ditto"));
  119. HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  120. m_TrayIcon.Create(NULL, WM_ICON_NOTIFY, _T("Ditto"), hIcon, IDR_MENU, FALSE, _T(""), _T(""), NULL, 20);
  121. m_TrayIcon.SetSingleClickSelect(TRUE);
  122. m_TrayIcon.MinimiseToTray(this);
  123. m_TrayIcon.SetMenuDefaultItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  124. //Only if in release
  125. #ifndef _DEBUG
  126. {
  127. //If not showing the icon show it for 40 seconds so they can get to the option
  128. //in case they can't remember the hot keys or something like that
  129. if(!(CGetSetOptions::GetShowIconInSysTray()))
  130. {
  131. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  132. }
  133. }
  134. #endif
  135. SetTimer(CLOSE_WINDOW_TIMER, ONE_HOUR *24, 0);
  136. SetTimer(REMOVE_OLD_REMOTE_COPIES, ONE_DAY, 0);
  137. SetTimer(REMOVE_OLD_ENTRIES_TIMER, ONE_MINUTE *15, 0);
  138. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  139. theApp.AfterMainCreate();
  140. m_thread.Start(this);
  141. return 0;
  142. }
  143. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT &cs)
  144. {
  145. if(cs.hMenu != NULL)
  146. {
  147. ::DestroyMenu(cs.hMenu); // delete menu if loaded
  148. cs.hMenu = NULL; // no menu for this window
  149. }
  150. if(!CFrameWnd::PreCreateWindow(cs))
  151. {
  152. return FALSE;
  153. }
  154. WNDCLASS wc;
  155. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  156. wc.lpfnWndProc = AfxWndProc;
  157. wc.cbClsExtra = 0;
  158. wc.cbWndExtra = 0;
  159. wc.hInstance = AfxGetInstanceHandle();
  160. wc.hIcon = NULL;
  161. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  162. wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  163. wc.lpszMenuName = NULL;
  164. wc.lpszClassName = _T("Ditto");
  165. // Create the QPaste window class
  166. if(!AfxRegisterClass(&wc))
  167. {
  168. return FALSE;
  169. }
  170. cs.lpszClass = wc.lpszClassName;
  171. return TRUE;
  172. }
  173. /////////////////////////////////////////////////////////////////////////////
  174. // CMainFrame diagnostics
  175. #ifdef _DEBUG
  176. void CMainFrame::AssertValid()const
  177. {
  178. CFrameWnd::AssertValid();
  179. }
  180. void CMainFrame::Dump(CDumpContext &dc)const
  181. {
  182. CFrameWnd::Dump(dc);
  183. }
  184. #endif //_DEBUG
  185. /////////////////////////////////////////////////////////////////////////////
  186. // CMainFrame message handlers
  187. void CMainFrame::OnFirstOption()
  188. {
  189. theApp.ShowOptionsDlg();
  190. }
  191. void CMainFrame::OnFirstExit()
  192. {
  193. this->SendMessage(WM_CLOSE, 0, 0);
  194. }
  195. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  196. {
  197. if(theApp.m_pDittoHotKey && wParam == theApp.m_pDittoHotKey->m_Atom)
  198. {
  199. //If they still have the shift/ctrl keys down
  200. if(m_keyStateModifiers != 0 && m_quickPaste.IsWindowVisibleEx())
  201. {
  202. Log(_T("On Show Ditto HotKey, key state modifiers are still down, moving selection"));
  203. if(m_bMovedSelectionMoveKeyState == false)
  204. {
  205. Log(_T("Setting flag m_bMovedSelectionMoveKeyState to true, will paste when modifer keys are up"));
  206. }
  207. m_quickPaste.MoveSelection(true);
  208. m_bMovedSelectionMoveKeyState = true;
  209. }
  210. else if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && m_quickPaste.IsWindowVisibleEx())
  211. {
  212. Log(_T("On Show Ditto HotKey, window is alread visible, hiding window"));
  213. m_quickPaste.HideQPasteWnd();
  214. }
  215. else
  216. {
  217. Log(_T("On Show Ditto HotKey, showing window"));
  218. m_keyModifiersTimerCount = 0;
  219. m_bMovedSelectionMoveKeyState = false;
  220. m_startKeyStateTime = GetTickCount();
  221. m_keyStateModifiers = CAccels::GetKeyStateModifiers();
  222. SetTimer(KEY_STATE_MODIFIERS, 50, NULL);
  223. theApp.m_activeWnd.TrackActiveWnd(NULL);
  224. m_quickPaste.ShowQPasteWnd(this, false, true, FALSE);
  225. }
  226. KillTimer(CLOSE_WINDOW_TIMER);
  227. SetTimer(CLOSE_WINDOW_TIMER, ONE_HOUR *24, 0);
  228. }
  229. else if(theApp.m_pPosOne && wParam == theApp.m_pPosOne->m_Atom)
  230. {
  231. Log(_T("Pos 1 hot key"));
  232. DoFirstTenPositionsPaste(0);
  233. }
  234. else if(theApp.m_pPosTwo && wParam == theApp.m_pPosTwo->m_Atom)
  235. {
  236. Log(_T("Pos 2 hot key"));
  237. DoFirstTenPositionsPaste(1);
  238. }
  239. else if(theApp.m_pPosThree && wParam == theApp.m_pPosThree->m_Atom)
  240. {
  241. Log(_T("Pos 3 hot key"));
  242. DoFirstTenPositionsPaste(2);
  243. }
  244. else if(theApp.m_pPosFour && wParam == theApp.m_pPosFour->m_Atom)
  245. {
  246. Log(_T("Pos 4 hot key"));
  247. DoFirstTenPositionsPaste(3);
  248. }
  249. else if(theApp.m_pPosFive && wParam == theApp.m_pPosFive->m_Atom)
  250. {
  251. Log(_T("Pos 5 hot key"));
  252. DoFirstTenPositionsPaste(4);
  253. }
  254. else if(theApp.m_pPosSix && wParam == theApp.m_pPosSix->m_Atom)
  255. {
  256. Log(_T("Pos 6 hot key"));
  257. DoFirstTenPositionsPaste(5);
  258. }
  259. else if(theApp.m_pPosSeven && wParam == theApp.m_pPosSeven->m_Atom)
  260. {
  261. Log(_T("Pos 7 hot key"));
  262. DoFirstTenPositionsPaste(6);
  263. }
  264. else if(theApp.m_pPosEight && wParam == theApp.m_pPosEight->m_Atom)
  265. {
  266. Log(_T("Pos 8 hot key"));
  267. DoFirstTenPositionsPaste(7);
  268. }
  269. else if(theApp.m_pPosNine && wParam == theApp.m_pPosNine->m_Atom)
  270. {
  271. Log(_T("Pos 9 hot key"));
  272. DoFirstTenPositionsPaste(8);
  273. }
  274. else if(theApp.m_pPosTen && wParam == theApp.m_pPosTen->m_Atom)
  275. {
  276. Log(_T("Pos 10 hot key"));
  277. DoFirstTenPositionsPaste(9);
  278. }
  279. else if(theApp.m_pCopyBuffer1 && wParam == theApp.m_pCopyBuffer1->m_Atom)
  280. {
  281. Log(_T("Copy buffer 1 hot key"));
  282. theApp.m_CopyBuffer.StartCopy(0);
  283. }
  284. else if(theApp.m_pPasteBuffer1 && wParam == theApp.m_pPasteBuffer1->m_Atom)
  285. {
  286. Log(_T("Paste buffer 1 hot key"));
  287. theApp.m_CopyBuffer.PastCopyBuffer(0);
  288. }
  289. else if(theApp.m_pCutBuffer1 && wParam == theApp.m_pCutBuffer1->m_Atom)
  290. {
  291. Log(_T("Cut buffer 1 hot key"));
  292. theApp.m_CopyBuffer.StartCopy(0, true);
  293. }
  294. else if(theApp.m_pCopyBuffer2 && wParam == theApp.m_pCopyBuffer2->m_Atom)
  295. {
  296. Log(_T("Copy buffer 2 hot key"));
  297. theApp.m_CopyBuffer.StartCopy(1);
  298. }
  299. else if(theApp.m_pPasteBuffer2 && wParam == theApp.m_pPasteBuffer2->m_Atom)
  300. {
  301. Log(_T("Paste buffer 2 hot key"));
  302. theApp.m_CopyBuffer.PastCopyBuffer(1);
  303. }
  304. else if(theApp.m_pCutBuffer2 && wParam == theApp.m_pCutBuffer2->m_Atom)
  305. {
  306. Log(_T("Cut buffer 2 hot key"));
  307. theApp.m_CopyBuffer.StartCopy(1, true);
  308. }
  309. else if(theApp.m_pCopyBuffer3 && wParam == theApp.m_pCopyBuffer3->m_Atom)
  310. {
  311. Log(_T("Copy buffer 3 hot key"));
  312. theApp.m_CopyBuffer.StartCopy(2);
  313. }
  314. else if(theApp.m_pPasteBuffer3 && wParam == theApp.m_pPasteBuffer3->m_Atom)
  315. {
  316. Log(_T("Paste buffer 3 hot key"));
  317. theApp.m_CopyBuffer.PastCopyBuffer(2);
  318. }
  319. else if(theApp.m_pCutBuffer3 && wParam == theApp.m_pCutBuffer3->m_Atom)
  320. {
  321. Log(_T("Cut buffer 3 hot key"));
  322. theApp.m_CopyBuffer.StartCopy(2, true);
  323. }
  324. return TRUE;
  325. }
  326. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  327. {
  328. try
  329. {
  330. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, bIsGroup, lDate FROM Main ")_T("WHERE ((bIsGroup = 1 AND lParentID = -1) OR bIsGroup = 0) ")_T("ORDER BY bIsGroup ASC, lDate DESC ")_T("LIMIT 1 OFFSET %d"), nPos);
  331. if(q.eof() == false)
  332. {
  333. if(q.getIntField(_T("bIsGroup")) == FALSE)
  334. {
  335. //Don't move these to the top
  336. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  337. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  338. CProcessPaste paste;
  339. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  340. paste.m_bActivateTarget = false;
  341. paste.m_bSendPaste = g_Opt.m_bSendPasteOnFirstTenHotKeys ? true : false;
  342. paste.DoPaste();
  343. theApp.OnPasteCompleted();
  344. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  345. }
  346. }
  347. }
  348. CATCH_SQLITE_EXCEPTION
  349. }
  350. void CMainFrame::DoDittoCopyBufferPaste(int nCopyBuffer)
  351. {
  352. try
  353. {
  354. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CopyBuffer = %d"), nCopyBuffer);
  355. if(q.eof() == false)
  356. {
  357. //Don't move these to the top
  358. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  359. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  360. CProcessPaste paste;
  361. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  362. paste.m_bActivateTarget = false;
  363. paste.DoPaste();
  364. theApp.OnPasteCompleted();
  365. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  366. }
  367. }
  368. CATCH_SQLITE_EXCEPTION
  369. }
  370. void CMainFrame::OnTimer(UINT nIDEvent)
  371. {
  372. switch(nIDEvent)
  373. {
  374. case HIDE_ICON_TIMER:
  375. {
  376. m_TrayIcon.HideIcon();
  377. KillTimer(nIDEvent);
  378. }
  379. break;
  380. case CLOSE_WINDOW_TIMER:
  381. {
  382. m_quickPaste.CloseQPasteWnd();
  383. }
  384. break;
  385. case REMOVE_OLD_ENTRIES_TIMER:
  386. {
  387. m_thread.FireDeleteEntries();
  388. }
  389. break;
  390. case REMOVE_OLD_REMOTE_COPIES:
  391. {
  392. m_thread.FireRemoveRemoteFiles();
  393. }
  394. break;
  395. case KEY_STATE_MODIFIERS:
  396. m_keyModifiersTimerCount++;
  397. if(m_keyStateModifiers != 0)
  398. {
  399. BYTE keyState = CAccels::GetKeyStateModifiers();
  400. //Have they release the key state modifiers yet(ctrl, shift, alt)
  401. if((m_keyStateModifiers &keyState) == 0)
  402. {
  403. KillTimer(KEY_STATE_MODIFIERS);
  404. long waitTime = (long)(GetTickCount() - m_startKeyStateTime);
  405. if(m_bMovedSelectionMoveKeyState || m_keyModifiersTimerCount > g_Opt.GetKeyStateWaitTimerCount())
  406. {
  407. Log(StrF(_T("Timer KEY_STATE_MODIFIERS timeout count hit(%d), count (%d), time (%d), Move Selection from Modifer (%d) sending paste"), g_Opt.GetKeyStateWaitTimerCount(), m_keyModifiersTimerCount, waitTime, m_bMovedSelectionMoveKeyState));
  408. m_quickPaste.OnKeyStateUp();
  409. }
  410. else
  411. {
  412. Log(StrF(_T("Timer KEY_STATE_MODIFIERS count NOT hit(%d), count (%d) time (%d)"), g_Opt.GetKeyStateWaitTimerCount(), m_keyModifiersTimerCount, waitTime));
  413. m_quickPaste.SetKeyModiferState(false);
  414. }
  415. m_keyStateModifiers = 0;
  416. m_keyModifiersTimerCount = 0;
  417. m_bMovedSelectionMoveKeyState = 0;
  418. }
  419. }
  420. else
  421. {
  422. KillTimer(KEY_STATE_MODIFIERS);
  423. }
  424. break;
  425. case ACTIVE_WINDOW_TIMER:
  426. {
  427. if(m_quickPaste.IsWindowVisibleEx())
  428. {
  429. theApp.m_activeWnd.TrackActiveWnd(NULL);
  430. }
  431. }
  432. break;
  433. case FOCUS_CHANGED_TIMER:
  434. {
  435. KillTimer(FOCUS_CHANGED_TIMER);
  436. //Log(StrF(_T("Focus Timer %d"), m_tempFocusWnd));
  437. theApp.m_activeWnd.TrackActiveWnd(m_tempFocusWnd);
  438. }
  439. break;
  440. }
  441. CFrameWnd::OnTimer(nIDEvent);
  442. }
  443. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  444. {
  445. if(lParam)
  446. {
  447. if(!m_TrayIcon.Visible())
  448. {
  449. KillTimer(HIDE_ICON_TIMER);
  450. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  451. }
  452. }
  453. if(wParam)
  454. {
  455. m_TrayIcon.ShowIcon();
  456. }
  457. else
  458. {
  459. m_TrayIcon.HideIcon();
  460. }
  461. return TRUE;
  462. }
  463. void CMainFrame::OnFirstShowquickpaste()
  464. {
  465. m_quickPaste.ShowQPasteWnd(this, true, false, FALSE);
  466. }
  467. void CMainFrame::OnFirstToggleConnectCV()
  468. {
  469. theApp.ToggleConnectCV();
  470. }
  471. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI *pCmdUI)
  472. {
  473. theApp.UpdateMenuConnectCV(pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV);
  474. }
  475. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  476. {
  477. Log(_T("Start of function OnClipboardCopied, adding clip to thread for processing"));
  478. CClip *pClip = (CClip*)wParam;
  479. if(pClip != NULL)
  480. {
  481. m_thread.AddClipToSave(pClip);
  482. }
  483. Log(_T("End of function OnClipboardCopied"));
  484. return TRUE;
  485. }
  486. BOOL CMainFrame::PreTranslateMessage(MSG *pMsg)
  487. {
  488. // target before mouse messages change the focus
  489. if(theApp.m_bShowingQuickPaste && WM_MOUSEFIRST <= pMsg->message && pMsg->message <= WM_MOUSELAST)
  490. {
  491. if(g_Opt.m_bUseHookDllForFocus == false)
  492. {
  493. theApp.m_activeWnd.TrackActiveWnd(NULL);
  494. }
  495. }
  496. return CFrameWnd::PreTranslateMessage(pMsg);
  497. }
  498. void CMainFrame::OnClose()
  499. {
  500. CloseAllOpenDialogs();
  501. if(m_pEditFrameWnd)
  502. {
  503. if(m_pEditFrameWnd->CloseAll() == false)
  504. {
  505. return ;
  506. }
  507. }
  508. Log(_T("OnClose - before stop MainFrm thread"));
  509. m_thread.Stop();
  510. Log(_T("OnClose - after stop MainFrm thread"));
  511. theApp.BeforeMainClose();
  512. CFrameWnd::OnClose();
  513. }
  514. bool CMainFrame::CloseAllOpenDialogs()
  515. {
  516. bool bRet = false;
  517. DWORD dwordProcessId;
  518. DWORD dwordChildWindowProcessId;
  519. GetWindowThreadProcessId(this->m_hWnd, &dwordProcessId);
  520. ASSERT(dwordProcessId);
  521. CWnd *pTempWnd = GetDesktopWindow()->GetWindow(GW_CHILD);
  522. while((pTempWnd = pTempWnd->GetWindow(GW_HWNDNEXT)) != NULL)
  523. {
  524. if(pTempWnd->GetSafeHwnd() == NULL)
  525. {
  526. break;
  527. }
  528. GetWindowThreadProcessId(pTempWnd->GetSafeHwnd(), &dwordChildWindowProcessId);
  529. if(dwordChildWindowProcessId == dwordProcessId)
  530. {
  531. TCHAR szTemp[100];
  532. GetClassName(pTempWnd->GetSafeHwnd(), szTemp, 100);
  533. // #32770 is class name for dialogs so don't process the message if it is a dialog
  534. if(STRCMP(szTemp, _T("#32770")) == 0)
  535. {
  536. pTempWnd->SendMessage(WM_CLOSE, 0, 0);
  537. bRet = true;
  538. }
  539. }
  540. }
  541. MSG msg;
  542. while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  543. {
  544. TranslateMessage(&msg);
  545. DispatchMessage(&msg);
  546. }
  547. return bRet;
  548. }
  549. LRESULT CMainFrame::OnLoadClipOnClipboard(WPARAM wParam, LPARAM lParam)
  550. {
  551. CClip *pClip = (CClip*)wParam;
  552. if(pClip == NULL)
  553. {
  554. LogSendRecieveInfo("---------ERROR OnLoadClipOnClipboard pClip == NULL");
  555. return FALSE;
  556. }
  557. if(pClip)
  558. {
  559. CProcessPaste paste;
  560. paste.m_bSendPaste = false;
  561. LogSendRecieveInfo("---------OnLoadClipOnClipboard - Before PutFormats on clipboard");
  562. paste.m_pOle->PutFormatOnClipboard(&pClip->m_Formats);
  563. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  564. LogSendRecieveInfo("---------OnLoadClipOnClipboard - After PutFormats on clipboard");
  565. LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - Setting clip id: %d on ole clipboard"), pClip->m_ID));
  566. paste.GetClipIDs().Add(pClip->m_ID);
  567. paste.DoPaste();
  568. LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - After paste clip id: %d on ole clipboard"), pClip->m_ID));
  569. }
  570. delete pClip;
  571. return TRUE;
  572. }
  573. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  574. {
  575. CClipList *pClipList = (CClipList*)wParam;
  576. if(pClipList == NULL)
  577. {
  578. LogSendRecieveInfo("---------OnAddToDatabaseFromSocket - ERROR pClipList == NULL");
  579. return FALSE;
  580. }
  581. BOOL bSetToClipBoard = (BOOL)lParam;
  582. if(bSetToClipBoard)
  583. {
  584. CClip *pClip = pClipList->GetTail();
  585. if(pClip)
  586. {
  587. LogSendRecieveInfo("OnAddToDatabaseFromSocket - Adding clip from socket setting clip to be put on clipboard");
  588. pClip->m_param1 = TRUE;
  589. }
  590. }
  591. m_thread.AddRemoteClipToSave(pClipList);
  592. delete pClipList;
  593. return TRUE;
  594. }
  595. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  596. {
  597. CString csNewText = (TCHAR*)wParam;
  598. ShowErrorMessage(_T("Ditto - Send/Receive Error"), csNewText);
  599. return TRUE;
  600. }
  601. CString WndName(HWND hParent)
  602. {
  603. TCHAR cWindowText[200];
  604. ::GetWindowText(hParent, cWindowText, 100);
  605. int nCount = 0;
  606. while(STRLEN(cWindowText) <= 0)
  607. {
  608. hParent = ::GetParent(hParent);
  609. if(hParent == NULL)
  610. {
  611. break;
  612. }
  613. ::GetWindowText(hParent, cWindowText, 100);
  614. nCount++;
  615. if(nCount > 100)
  616. {
  617. Log(_T("GetTargetName reached maximum search depth of 100"));
  618. break;
  619. }
  620. }
  621. return cWindowText;
  622. }
  623. LRESULT CMainFrame::OnFocusChanged(WPARAM wParam, LPARAM lParam)
  624. {
  625. if(m_quickPaste.IsWindowVisibleEx())
  626. {
  627. HWND focus = (HWND)wParam;
  628. static DWORD dLastDittoHasFocusTick = 0;
  629. //Sometimes when we bring ditto up there will come a null focus
  630. //rite after that
  631. if(focus == NULL && (GetTickCount() - dLastDittoHasFocusTick < 500))
  632. {
  633. Log(_T("NULL focus within 500 ticks of bringing up ditto"));
  634. return TRUE;
  635. }
  636. else if(focus == NULL)
  637. {
  638. Log(_T("NULL focus received"));
  639. }
  640. if(theApp.m_activeWnd.DittoHasFocus())
  641. {
  642. dLastDittoHasFocusTick = GetTickCount();
  643. }
  644. //Log(StrF(_T("OnFocusChanged %d, title %s"), focus, WndName(focus)));
  645. m_tempFocusWnd = focus;
  646. KillTimer(FOCUS_CHANGED_TIMER);
  647. SetTimer(FOCUS_CHANGED_TIMER, g_Opt.FocusChangedDelay(), NULL);
  648. }
  649. return TRUE;
  650. }
  651. void CMainFrame::OnFirstHelp()
  652. {
  653. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  654. csFile += "DittoGettingStarted.htm";
  655. CHyperLink::GotoURL(csFile, SW_SHOW);
  656. }
  657. LRESULT CMainFrame::OnCustomizeTrayMenu(WPARAM wParam, LPARAM lParam)
  658. {
  659. CMenu *pMenu = (CMenu*)wParam;
  660. if(pMenu)
  661. {
  662. theApp.m_Language.UpdateTrayIconRightClickMenu(pMenu);
  663. }
  664. return true;
  665. }
  666. void CMainFrame::ShowErrorMessage(CString csTitle, CString csMessage)
  667. {
  668. Log(StrF(_T("ShowErrorMessage %s - %s"), csTitle, csMessage));
  669. CToolTipEx *pErrorWnd = new CToolTipEx;
  670. pErrorWnd->Create(this);
  671. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  672. CPoint pt;
  673. CRect rcScreen;
  674. GetMonitorRect(0, &rcScreen);
  675. pt = rcScreen.BottomRight();
  676. CRect cr = pErrorWnd->GetBoundsRect();
  677. pt.x -= max(cr.Width() + 50, 150);
  678. pt.y -= max(cr.Height() + 50, 150);
  679. pErrorWnd->Show(pt);
  680. pErrorWnd->HideWindowInXMilliSeconds(4000);
  681. }
  682. void CMainFrame::OnFirstImport()
  683. {
  684. theApp.ImportClips(theApp.m_MainhWnd);
  685. }
  686. void CMainFrame::ShowEditWnd(CClipIDs &Ids)
  687. {
  688. CWaitCursor wait;
  689. bool bCreatedWindow = false;
  690. if(m_pEditFrameWnd == NULL)
  691. {
  692. m_pEditFrameWnd = new CEditFrameWnd;
  693. m_pEditFrameWnd->LoadFrame(IDR_MAINFRAME);
  694. bCreatedWindow = true;
  695. }
  696. if(m_pEditFrameWnd)
  697. {
  698. m_pEditFrameWnd->EditIds(Ids);
  699. m_pEditFrameWnd->SetNotifyWnd(m_hWnd);
  700. if(bCreatedWindow)
  701. {
  702. CSize sz;
  703. CPoint pt;
  704. CGetSetOptions::GetEditWndSize(sz);
  705. CGetSetOptions::GetEditWndPoint(pt);
  706. CRect cr(pt, sz);
  707. EnsureWindowVisible(&cr);
  708. m_pEditFrameWnd->MoveWindow(cr);
  709. }
  710. m_pEditFrameWnd->ShowWindow(SW_SHOW);
  711. m_pEditFrameWnd->SetForegroundWindow();
  712. m_pEditFrameWnd->SetFocus();
  713. }
  714. }
  715. LRESULT CMainFrame::OnEditWndClose(WPARAM wParam, LPARAM lParam)
  716. {
  717. m_pEditFrameWnd = NULL;
  718. return TRUE;
  719. }
  720. LRESULT CMainFrame::OnSetConnected(WPARAM wParam, LPARAM lParam)
  721. {
  722. if(wParam)
  723. {
  724. theApp.SetConnectCV(true);
  725. }
  726. else if(lParam)
  727. {
  728. theApp.SetConnectCV(false);
  729. }
  730. return TRUE;
  731. }
  732. void CMainFrame::OnDestroy()
  733. {
  734. CFrameWnd::OnDestroy();
  735. if(m_pEditFrameWnd)
  736. {
  737. m_pEditFrameWnd->DestroyWindow();
  738. }
  739. }
  740. void CMainFrame::OnFirstNewclip()
  741. {
  742. CClipIDs IDs;
  743. IDs.Add( - 1);
  744. theApp.EditItems(IDs, true);
  745. }