MainFrm.cpp 31 KB

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