MainFrm.cpp 27 KB

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