MainFrm.cpp 27 KB

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