MainFrm.cpp 26 KB

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