MainFrm.cpp 27 KB

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