MainFrm.cpp 27 KB

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