MainFrm.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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. #include "DatabaseUtilities.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. #define WM_ICON_NOTIFY WM_APP+10
  26. #define MYWM_NOTIFYICON (WM_USER+1)
  27. #define WM_TRAYNOTIFY WM_USER + 100
  28. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  29. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  30. //{{AFX_MSG_MAP(CMainFrame)
  31. ON_WM_CREATE()
  32. ON_COMMAND(ID_FIRST_OPTION, OnFirstOption)
  33. ON_COMMAND(ID_FIRST_EXIT, OnFirstExit)
  34. ON_WM_TIMER()
  35. ON_COMMAND(ID_FIRST_SHOWQUICKPASTE, OnFirstShowquickpaste)
  36. ON_COMMAND(ID_FIRST_TOGGLECONNECTCV, OnFirstToggleConnectCV)
  37. ON_UPDATE_COMMAND_UI(ID_FIRST_TOGGLECONNECTCV, OnUpdateFirstToggleConnectCV)
  38. ON_COMMAND(ID_FIRST_HELP, OnFirstHelp)
  39. //}}AFX_MSG_MAP
  40. ON_MESSAGE(WM_HOTKEY, OnHotKey)
  41. ON_MESSAGE(WM_SHOW_TRAY_ICON, OnShowTrayIcon)
  42. ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
  43. ON_WM_CLOSE()
  44. ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
  45. ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
  46. ON_MESSAGE(WM_SHOW_ERROR_MSG, OnErrorMsg)
  47. ON_COMMAND(ID_FIRST_IMPORT, OnFirstImport)
  48. ON_MESSAGE(WM_EDIT_WND_CLOSING, OnEditWndClose)
  49. ON_WM_DESTROY()
  50. ON_COMMAND(ID_FIRST_NEWCLIP, OnFirstNewclip)
  51. ON_MESSAGE(WM_SET_CONNECTED, OnSetConnected)
  52. ON_MESSAGE(WM_OPEN_CLOSE_WINDWOW, OnOpenCloseWindow)
  53. ON_MESSAGE(WM_LOAD_ClIP_ON_CLIPBOARD, OnLoadClipOnClipboard)
  54. ON_COMMAND(ID_FIRST_GLOBALHOTKEYS, &CMainFrame::OnFirstGlobalhotkeys)
  55. ON_MESSAGE(WM_GLOBAL_CLIPS_CLOSED, OnGlobalClipsClosed)
  56. ON_MESSAGE(WM_OPTIONS_CLOSED, OnOptionsClosed)
  57. ON_MESSAGE(WM_SHOW_OPTIONS, OnShowOptions)
  58. ON_COMMAND(ID_FIRST_DELETECLIPDATA, &CMainFrame::OnFirstDeleteclipdata)
  59. ON_MESSAGE(WM_DELETE_CLIPS_CLOSED, OnDeleteClipDataClosed)
  60. ON_COMMAND(ID_FIRST_SAVECURRENTCLIPBOARD, &CMainFrame::OnFirstSavecurrentclipboard)
  61. ON_MESSAGE(WM_SAVE_CLIPBOARD, &CMainFrame::OnSaveClipboardMessage)
  62. ON_MESSAGE(WM_READD_TASKBAR_ICON, OnReAddTaskBarIcon)
  63. ON_MESSAGE(WM_REOPEN_DATABASE, &CMainFrame::OnReOpenDatabase)
  64. ON_MESSAGE(WM_SHOW_MSG_WINDOW, &CMainFrame::OnShowMsgWindow)
  65. ON_MESSAGE(WM_SHOW_DITTO_GROUP, &CMainFrame::OnShowDittoGroup)
  66. ON_COMMAND(ID_FIRST_FIXUPSTICKYCLIPORDER, &CMainFrame::OnFirstFixupstickycliporder)
  67. ON_MESSAGE(WM_DISPLAYCHANGE, &CMainFrame::OnResolutionChange)
  68. ON_MESSAGE(WM_TRAYNOTIFY, &CMainFrame::OnTrayNotification)
  69. ON_MESSAGE(WM_PLAIN_TEXT_PASTE, &CMainFrame::OnPlainTextPaste)
  70. ON_WM_WININICHANGE()
  71. ON_COMMAND(ID_FIRST_SHOWSTARTUPMESSAGE, &CMainFrame::OnFirstShowstartupmessage)
  72. ON_UPDATE_COMMAND_UI(ID_FIRST_SHOWSTARTUPMESSAGE, &CMainFrame::OnUpdateFirstShowstartupmessage)
  73. ON_COMMAND(ID_FIRST_BACKUPDATABASE, &CMainFrame::OnFirstBackupdatabase)
  74. ON_COMMAND(ID_FIRST_RESTOREDATABASE, &CMainFrame::OnFirstRestoredatabase)
  75. ON_MESSAGE(WM_BACKUP_DB, OnBackupDb)
  76. ON_MESSAGE(WM_RESTORE_DB, OnRestoreDb)
  77. ON_COMMAND(ID_FIRST_DELETEALLNONUSEDCLIPS, &CMainFrame::OnFirstDeleteallnonusedclips)
  78. ON_MESSAGE(WM_PASTE_CLIP, OnPasteClip)
  79. ON_MESSAGE(WM_EDIT_CLIP, OnEditClip)
  80. END_MESSAGE_MAP()
  81. static UINT indicators[] =
  82. {
  83. ID_SEPARATOR, // status line indicator
  84. ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
  85. };
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CMainFrame construction/destruction
  88. CMainFrame::CMainFrame()
  89. {
  90. m_pEditFrameWnd = NULL;
  91. m_keyStateModifiers = 0;
  92. m_startKeyStateTime = 0;
  93. m_bMovedSelectionMoveKeyState = false;
  94. m_keyModifiersTimerCount = 0;
  95. m_pGlobalClips = NULL;
  96. m_pOptions = NULL;
  97. m_pDeleteClips = NULL;
  98. m_doubleClickGroupId = -1;
  99. m_doubleClickGroupStartTime = 0;
  100. }
  101. CMainFrame::~CMainFrame()
  102. {
  103. CGetSetOptions::SetMainHWND(0);
  104. }
  105. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  106. {
  107. if(CFrameWnd::OnCreate(lpCreateStruct) == - 1)
  108. {
  109. return - 1;
  110. }
  111. m_PowerManager.Start(m_hWnd);
  112. ////Center the main window so message boxes are in the center
  113. CRect rcScreen = DefaultMonitorRect();
  114. CPoint cpCenter = rcScreen.CenterPoint();
  115. MoveWindow(cpCenter.x, cpCenter.x, 1, 1);
  116. m_startupScreenWidth = GetScreenWidth();
  117. m_startupScreenHeight = GetScreenHeight();
  118. //Then set the main window to transparent so it's never shown
  119. //if it is shown then only the task tray icon
  120. //m_Transparency.SetTransparent(m_hWnd, 0, true);
  121. SetWindowText(_T(""));
  122. Log(_T("Setting polling timer to track focus"));
  123. SetTimer(ACTIVE_WINDOW_TIMER, g_Opt.FocusWndTimerTimeout(), 0);
  124. SetTimer(READ_RANDOM_DB_FILE, g_Opt.ReadRandomFileInterval() * 1000, 0);
  125. SetWindowText(_T("Ditto"));
  126. m_trayIcon.Create(this, IDR_MENU, _T("Ditto"), CTrayNotifyIcon::LoadIcon(IDR_MAINFRAME), WM_TRAYNOTIFY, 0, 1);
  127. m_trayIcon.SetDefaultMenuItem(ID_FIRST_SHOWQUICKPASTE, FALSE);
  128. m_trayIcon.MinimiseToTray(this);
  129. if (CGetSetOptions::GetShowStartupMessage())
  130. {
  131. CString msg = theApp.m_Language.GetString(_T("StartupMsg"), _T("Ditto is running minimized, Ditto can be opened by hot keys or by clicking the task tray icon"));
  132. m_trayIcon.SetBalloonDetails(msg, _T("Ditto"), CTrayNotifyIcon::BalloonStyle::Info, CGetSetOptions::GetBalloonTimeout());
  133. }
  134. theApp.m_Language.UpdateTrayIconRightClickMenu(&m_trayIcon.GetMenu());
  135. //Only if in release
  136. #ifndef _DEBUG
  137. {
  138. //If not showing the icon show it for 40 seconds so they can get to the option
  139. //in case they can't remember the hot keys or something like that
  140. if(!(CGetSetOptions::GetShowIconInSysTray()))
  141. {
  142. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  143. }
  144. }
  145. #endif
  146. //SetTimer(CLOSE_WINDOW_TIMER, ONE_HOUR*24, 0);
  147. SetTimer(REMOVE_OLD_TEMP_FILES, ONE_HOUR * 6, 0);
  148. SetTimer(REMOVE_OLD_ENTRIES_TIMER, ONE_MINUTE*15, 0);
  149. SetTimer(CLOSE_NO_DB_WINDOW_TIMER, 10000, 0);
  150. //found on some computers GetTickCount gettickcount returns a smaller value than other, can't explain
  151. //check here to see if we need to make an adjustment
  152. IdleSeconds();
  153. m_ulCopyGap = CGetSetOptions::GetCopyGap();
  154. theApp.AfterMainCreate();
  155. m_thread.Start(this);
  156. return 0;
  157. }
  158. LRESULT CMainFrame::OnPlainTextPaste(WPARAM wParam, LPARAM lParam)
  159. {
  160. DoTextOnlyPaste();
  161. return 1;
  162. }
  163. LRESULT CMainFrame::OnTrayNotification(WPARAM wParam, LPARAM lParam)
  164. {
  165. if (WM_MOUSEFIRST <= LOWORD(lParam) && LOWORD(lParam) <= WM_MOUSELAST)
  166. {
  167. theApp.m_activeWnd.TrackActiveWnd(true);
  168. }
  169. //click on balloon
  170. if (lParam == 0x405)
  171. {
  172. SetTimer(DELAYED_SHOW_DITTO_TIMER, 100, NULL);
  173. }
  174. m_trayIcon.OnTrayNotification(wParam, lParam);
  175. return 0L;
  176. }
  177. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT &cs)
  178. {
  179. if(cs.hMenu != NULL)
  180. {
  181. ::DestroyMenu(cs.hMenu); // delete menu if loaded
  182. cs.hMenu = NULL; // no menu for this window
  183. }
  184. if(!CFrameWnd::PreCreateWindow(cs))
  185. {
  186. return FALSE;
  187. }
  188. WNDCLASS wc;
  189. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  190. wc.lpfnWndProc = AfxWndProc;
  191. wc.cbClsExtra = 0;
  192. wc.cbWndExtra = 0;
  193. wc.hInstance = AfxGetInstanceHandle();
  194. wc.hIcon = NULL;
  195. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  196. wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  197. wc.lpszMenuName = NULL;
  198. wc.lpszClassName = _T("Ditto");
  199. // Create the QPaste window class
  200. if(!AfxRegisterClass(&wc))
  201. {
  202. return FALSE;
  203. }
  204. cs.lpszClass = wc.lpszClassName;
  205. return TRUE;
  206. }
  207. /////////////////////////////////////////////////////////////////////////////
  208. // CMainFrame diagnostics
  209. #ifdef _DEBUG
  210. void CMainFrame::AssertValid()const
  211. {
  212. CFrameWnd::AssertValid();
  213. }
  214. void CMainFrame::Dump(CDumpContext &dc)const
  215. {
  216. CFrameWnd::Dump(dc);
  217. }
  218. #endif //_DEBUG
  219. /////////////////////////////////////////////////////////////////////////////
  220. // CMainFrame message handlers
  221. void CMainFrame::OnFirstExit()
  222. {
  223. this->SendMessage(WM_CLOSE, 0, 0);
  224. }
  225. LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
  226. {
  227. if(theApp.m_pDittoHotKey && wParam == theApp.m_pDittoHotKey->m_Atom ||
  228. theApp.m_pDittoHotKey2 && wParam == theApp.m_pDittoHotKey2->m_Atom ||
  229. theApp.m_pDittoHotKey3 && wParam == theApp.m_pDittoHotKey3->m_Atom)
  230. {
  231. //If they still have the shift/ctrl keys down
  232. if(m_keyStateModifiers != 0 && m_quickPaste.IsWindowVisibleEx())
  233. {
  234. Log(_T("On Show Ditto HotKey, key state modifiers are still down, moving selection"));
  235. if(m_bMovedSelectionMoveKeyState == false)
  236. {
  237. Log(_T("Setting flag m_bMovedSelectionMoveKeyState to true, will paste when modifer keys are up"));
  238. }
  239. m_quickPaste.MoveSelection(true);
  240. m_bMovedSelectionMoveKeyState = true;
  241. }
  242. else if(g_Opt.m_HideDittoOnHotKeyIfAlreadyShown && m_quickPaste.IsWindowTopLevel() && g_Opt.GetShowPersistent() == FALSE)
  243. {
  244. Log(_T("On Show Ditto HotKey, window is alread visible, hiding window"));
  245. m_quickPaste.HideQPasteWnd();
  246. }
  247. else
  248. {
  249. Log(_T("On Show Ditto HotKey, showing window"));
  250. StartKeyModifyerTimer();
  251. ShowQPasteWithActiveWindowCheck();
  252. }
  253. //KillTimer(CLOSE_WINDOW_TIMER);
  254. //SetTimer(CLOSE_WINDOW_TIMER, ONE_HOUR *24, 0);
  255. }
  256. else if(theApp.m_pPosOne && wParam == theApp.m_pPosOne->m_Atom)
  257. {
  258. Log(_T("Pos 1 hot key"));
  259. DoFirstTenPositionsPaste(0);
  260. }
  261. else if(theApp.m_pPosTwo && wParam == theApp.m_pPosTwo->m_Atom)
  262. {
  263. Log(_T("Pos 2 hot key"));
  264. DoFirstTenPositionsPaste(1);
  265. }
  266. else if(theApp.m_pPosThree && wParam == theApp.m_pPosThree->m_Atom)
  267. {
  268. Log(_T("Pos 3 hot key"));
  269. DoFirstTenPositionsPaste(2);
  270. }
  271. else if(theApp.m_pPosFour && wParam == theApp.m_pPosFour->m_Atom)
  272. {
  273. Log(_T("Pos 4 hot key"));
  274. DoFirstTenPositionsPaste(3);
  275. }
  276. else if(theApp.m_pPosFive && wParam == theApp.m_pPosFive->m_Atom)
  277. {
  278. Log(_T("Pos 5 hot key"));
  279. DoFirstTenPositionsPaste(4);
  280. }
  281. else if(theApp.m_pPosSix && wParam == theApp.m_pPosSix->m_Atom)
  282. {
  283. Log(_T("Pos 6 hot key"));
  284. DoFirstTenPositionsPaste(5);
  285. }
  286. else if(theApp.m_pPosSeven && wParam == theApp.m_pPosSeven->m_Atom)
  287. {
  288. Log(_T("Pos 7 hot key"));
  289. DoFirstTenPositionsPaste(6);
  290. }
  291. else if(theApp.m_pPosEight && wParam == theApp.m_pPosEight->m_Atom)
  292. {
  293. Log(_T("Pos 8 hot key"));
  294. DoFirstTenPositionsPaste(7);
  295. }
  296. else if(theApp.m_pPosNine && wParam == theApp.m_pPosNine->m_Atom)
  297. {
  298. Log(_T("Pos 9 hot key"));
  299. DoFirstTenPositionsPaste(8);
  300. }
  301. else if(theApp.m_pPosTen && wParam == theApp.m_pPosTen->m_Atom)
  302. {
  303. Log(_T("Pos 10 hot key"));
  304. DoFirstTenPositionsPaste(9);
  305. }
  306. else if(theApp.m_pCopyBuffer1 && wParam == theApp.m_pCopyBuffer1->m_Atom)
  307. {
  308. Log(_T("Copy buffer 1 hot key"));
  309. theApp.m_CopyBuffer.StartCopy(0);
  310. }
  311. else if(theApp.m_pPasteBuffer1 && wParam == theApp.m_pPasteBuffer1->m_Atom)
  312. {
  313. Log(_T("Paste buffer 1 hot key"));
  314. theApp.m_CopyBuffer.PastCopyBuffer(0);
  315. }
  316. else if(theApp.m_pCutBuffer1 && wParam == theApp.m_pCutBuffer1->m_Atom)
  317. {
  318. Log(_T("Cut buffer 1 hot key"));
  319. theApp.m_CopyBuffer.StartCopy(0, true);
  320. }
  321. else if(theApp.m_pCopyBuffer2 && wParam == theApp.m_pCopyBuffer2->m_Atom)
  322. {
  323. Log(_T("Copy buffer 2 hot key"));
  324. theApp.m_CopyBuffer.StartCopy(1);
  325. }
  326. else if(theApp.m_pPasteBuffer2 && wParam == theApp.m_pPasteBuffer2->m_Atom)
  327. {
  328. Log(_T("Paste buffer 2 hot key"));
  329. theApp.m_CopyBuffer.PastCopyBuffer(1);
  330. }
  331. else if(theApp.m_pCutBuffer2 && wParam == theApp.m_pCutBuffer2->m_Atom)
  332. {
  333. Log(_T("Cut buffer 2 hot key"));
  334. theApp.m_CopyBuffer.StartCopy(1, true);
  335. }
  336. else if(theApp.m_pCopyBuffer3 && wParam == theApp.m_pCopyBuffer3->m_Atom)
  337. {
  338. Log(_T("Copy buffer 3 hot key"));
  339. theApp.m_CopyBuffer.StartCopy(2);
  340. }
  341. else if(theApp.m_pPasteBuffer3 && wParam == theApp.m_pPasteBuffer3->m_Atom)
  342. {
  343. Log(_T("Paste buffer 3 hot key"));
  344. theApp.m_CopyBuffer.PastCopyBuffer(2);
  345. }
  346. else if(theApp.m_pCutBuffer3 && wParam == theApp.m_pCutBuffer3->m_Atom)
  347. {
  348. Log(_T("Cut buffer 3 hot key"));
  349. theApp.m_CopyBuffer.StartCopy(2, true);
  350. }
  351. else if(theApp.m_pTextOnlyPaste && wParam == theApp.m_pTextOnlyPaste->m_Atom)
  352. {
  353. DoTextOnlyPaste();
  354. }
  355. else if(theApp.m_pSaveClipboard && wParam == theApp.m_pSaveClipboard->m_Atom)
  356. {
  357. OnFirstSavecurrentclipboard();
  358. }
  359. else if (theApp.m_pCopyAndSaveClipboard && wParam == theApp.m_pCopyAndSaveClipboard->m_Atom)
  360. {
  361. Log(StrF(_T("START of copy and save clipboard, sending copy")));
  362. theApp.m_activeWnd.SendCopy(CopyReasonEnum::COPY_TO_UNKOWN);
  363. int delay = g_Opt.GetCopyAndSveDelay();
  364. Log(StrF(_T("Copy and save clipboard, sending copy, delaying %dms before saving clipboard"), delay));
  365. Sleep(delay);
  366. Log(StrF(_T("Copy and save clipboard, saving clipboard")));
  367. OnFirstSavecurrentclipboard();
  368. Log(StrF(_T("END of copy and save clipboard")));
  369. }
  370. else
  371. {
  372. for(int i = 0; i < g_HotKeys.GetCount(); i++)
  373. {
  374. if(g_HotKeys[i] != NULL &&
  375. g_HotKeys[i]->m_Atom == wParam &&
  376. g_HotKeys[i]->m_clipId > 0)
  377. {
  378. if(g_HotKeys[i]->m_hkType == CHotKey::PASTE_OPEN_CLIP)
  379. {
  380. Log(StrF(_T("Pasting clip from global shortcut, clipId: %d"), g_HotKeys[i]->m_clipId));
  381. PasteOrShowGroup(g_HotKeys[i]->m_clipId, -1, FALSE, TRUE, false);
  382. }
  383. else if(g_HotKeys[i]->m_hkType == CHotKey::MOVE_TO_GROUP)
  384. {
  385. Log(StrF(_T("Global hot key to save clip to group Id: %d, Sending copy to save selection to this group"), g_HotKeys[i]->m_clipId));
  386. KillTimer(GROUP_DOUBLE_CLICK);
  387. m_doubleClickGroupId = -1;
  388. m_doubleClickGroupStartTime = 0;
  389. theApp.SetActiveGroupId(g_HotKeys[i]->m_clipId);
  390. theApp.m_activeWnd.SendCopy(CopyReasonEnum::COPY_TO_GROUP);
  391. }
  392. break;
  393. }
  394. }
  395. }
  396. return TRUE;
  397. }
  398. void CMainFrame::ShowQPasteWithActiveWindowCheck()
  399. {
  400. //Before we show our window find the current focused window for paste into
  401. theApp.m_activeWnd.TrackActiveWnd(true);
  402. if (CGetSetOptions::GetOpenToGroupByActiveExe() &&
  403. theApp.m_activeWnd.ActiveWnd() != NULL)
  404. {
  405. CString exeName = GetProcessName(theApp.m_activeWnd.ActiveWnd());
  406. if (exeName != _T(""))
  407. {
  408. theApp.TryEnterOldGroupState();
  409. CString query = StrF(_T("SELECT lID FROM Main WHERE bIsGroup = 1 AND mText = '%s' COLLATE NOCASE"), exeName);
  410. CppSQLite3Query q = theApp.m_db.execQueryEx(query);
  411. if (q.eof() == false)
  412. {
  413. int groupId = q.getIntField(_T("lID"));
  414. //this will revert back to the old group on hide of ditto
  415. theApp.EnterGroupID(groupId, TRUE, TRUE);
  416. Log(StrF(_T("Opening Ditto to Group based on found group name, name: %s, GroupId: %d"), exeName, groupId));
  417. }
  418. else
  419. {
  420. theApp.TryEnterOldGroupState();
  421. }
  422. }
  423. }
  424. m_quickPaste.ShowQPasteWnd(this, false, true, FALSE);
  425. }
  426. void CMainFrame::DoTextOnlyPaste()
  427. {
  428. CClipboardSaveRestore textOnlyPaste;
  429. Log(_T("Text Only paste, saving clipboard to be restored later"));
  430. textOnlyPaste.Save(TRUE);
  431. Log(_T("Text Only paste, Add cf_text or cf_unicodetext to clipboard"));
  432. textOnlyPaste.RestoreTextOnly();
  433. DWORD pasteDelay = g_Opt.GetTextOnlyPasteDelay();
  434. Log(StrF(_T("Text Only paste, delaying %d ms before sending paste"), pasteDelay));
  435. Sleep(pasteDelay);
  436. Log(_T("Text Only paste, Sending paste"));
  437. theApp.m_activeWnd.SendPaste(false);
  438. Log(_T("Text Only paste, Post sending paste"));
  439. }
  440. void CMainFrame::DoFirstTenPositionsPaste(int nPos)
  441. {
  442. try
  443. {
  444. CString csSort = _T("");
  445. CString strFilter = _T("");
  446. bool pastedFromGroup = false;
  447. if (theApp.m_GroupID < 0 ||
  448. CGetSetOptions::GetUseUISelectedGroupForLastTenCopies() == FALSE)
  449. {
  450. //do not change this this directly relates to the views in the Main table
  451. csSort = "Main.bIsGroup ASC, "
  452. "Main.stickyClipOrder DESC, "
  453. "Main.clipOrder DESC";
  454. if (g_Opt.m_bShowAllClipsInMainList)
  455. {
  456. if (CGetSetOptions::GetShowGroupsInMainList())
  457. {
  458. //found to be slower on large databases
  459. strFilter = "((Main.bIsGroup = 1 AND Main.lParentID = -1) OR Main.bIsGroup = 0)";
  460. }
  461. else
  462. {
  463. strFilter = "(Main.bIsGroup = 0)";
  464. }
  465. }
  466. else
  467. {
  468. strFilter = "((Main.bIsGroup = 1 AND Main.lParentID = -1) OR (Main.bIsGroup = 0 AND Main.lParentID = -1))";
  469. }
  470. }
  471. else
  472. {
  473. pastedFromGroup = true;
  474. //do not change this this directly relates to the views in the Main table
  475. csSort = "Main.bIsGroup ASC, "
  476. "Main.stickyClipGroupOrder DESC, "
  477. "Main.clipGroupOrder DESC";
  478. if (theApp.m_GroupID >= 0)
  479. {
  480. strFilter.Format(_T("Main.lParentID = %d"), theApp.m_GroupID);
  481. }
  482. }
  483. CString query = StrF(_T("SELECT lID, bIsGroup FROM Main WHERE %s ORDER BY %s LIMIT 1 OFFSET %d"), strFilter, csSort, nPos);
  484. Log(StrF(_T("Doing Last Ten Paste, Index: %d Query: %s"), nPos, query));
  485. CppSQLite3Query q = theApp.m_db.execQueryEx(query);
  486. if(q.eof() == false)
  487. {
  488. PasteOrShowGroup(q.getIntField(_T("lID")), CGetSetOptions::GetMoveClipsOnGlobal10(), false, g_Opt.m_bSendPasteOnFirstTenHotKeys, pastedFromGroup);
  489. }
  490. }
  491. CATCH_SQLITE_EXCEPTION
  492. }
  493. void CMainFrame::StartKeyModifyerTimer()
  494. {
  495. if (g_Opt.m_moveSelectionOnOpenHotkey)
  496. {
  497. m_keyModifiersTimerCount = 0;
  498. m_bMovedSelectionMoveKeyState = false;
  499. m_startKeyStateTime = GetTickCount();
  500. m_keyStateModifiers = CAccels::GetKeyStateModifiers();
  501. SetTimer(KEY_STATE_MODIFIERS, 50, NULL);
  502. }
  503. }
  504. void CMainFrame::PasteOrShowGroup(int dbId, BOOL updateClipTime, BOOL activeTarget, BOOL sendPaste, bool pastedFromGroup)
  505. {
  506. try
  507. {
  508. bool isGroup = false;
  509. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lID = %d"), dbId);
  510. if(q.eof() == false)
  511. {
  512. if(q.getIntField(_T("bIsGroup")) > 0)
  513. {
  514. isGroup = true;
  515. }
  516. }
  517. if(isGroup)
  518. {
  519. DWORD maxDiff = (DWORD)CGetSetOptions::GetGroupDoubleClickTimeMS();
  520. DWORD diff = GetTickCount() - m_doubleClickGroupStartTime;
  521. if(m_doubleClickGroupId == dbId &&
  522. diff < maxDiff)
  523. {
  524. Log(StrF(_T("Second Press of group hot key, group Id: %d, Sending copy to save selection to this group"), dbId));
  525. KillTimer(GROUP_DOUBLE_CLICK);
  526. m_doubleClickGroupId = -1;
  527. m_doubleClickGroupStartTime = 0;
  528. theApp.SetActiveGroupId(dbId);
  529. theApp.m_activeWnd.SendCopy(CopyReasonEnum::COPY_TO_GROUP);
  530. }
  531. else
  532. {
  533. m_doubleClickGroupId = dbId;
  534. m_doubleClickGroupStartTime = GetTickCount();
  535. int doubleClickTime = CGetSetOptions::GetGroupDoubleClickTimeMS();
  536. SetTimer(GROUP_DOUBLE_CLICK, doubleClickTime, 0);
  537. Log(StrF(_T("First Press of group hot key, group Id: %d, timout: %d"), dbId, doubleClickTime));
  538. }
  539. }
  540. else
  541. {
  542. KillTimer(GROUP_DOUBLE_CLICK);
  543. m_doubleClickGroupId = -1;
  544. m_doubleClickGroupStartTime = 0;
  545. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  546. if (updateClipTime != -1)
  547. {
  548. g_Opt.m_bUpdateTimeOnPaste = updateClipTime;
  549. }
  550. CProcessPaste paste;
  551. paste.m_pastedFromGroup = pastedFromGroup;
  552. paste.GetClipIDs().Add(dbId);
  553. if (activeTarget != -1)
  554. {
  555. paste.m_bActivateTarget = activeTarget ? true : false;;
  556. }
  557. if (sendPaste != -1)
  558. {
  559. paste.m_bSendPaste = sendPaste ? true : false;
  560. }
  561. paste.DoPaste();
  562. theApp.OnPasteCompleted();
  563. if (updateClipTime != -1)
  564. {
  565. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  566. }
  567. }
  568. }
  569. CATCH_SQLITE_EXCEPTION
  570. }
  571. void CMainFrame::DoDittoCopyBufferPaste(int nCopyBuffer)
  572. {
  573. try
  574. {
  575. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CopyBuffer = %d"), nCopyBuffer);
  576. if(q.eof() == false)
  577. {
  578. //Don't move these to the top
  579. BOOL bItWas = g_Opt.m_bUpdateTimeOnPaste;
  580. g_Opt.m_bUpdateTimeOnPaste = FALSE;
  581. CProcessPaste paste;
  582. paste.GetClipIDs().Add(q.getIntField(_T("lID")));
  583. paste.m_bActivateTarget = false;
  584. paste.DoPaste();
  585. theApp.OnPasteCompleted();
  586. g_Opt.m_bUpdateTimeOnPaste = bItWas;
  587. }
  588. }
  589. CATCH_SQLITE_EXCEPTION
  590. }
  591. void CMainFrame::OnTimer(UINT_PTR nIDEvent)
  592. {
  593. switch(nIDEvent)
  594. {
  595. case HIDE_ICON_TIMER:
  596. {
  597. m_trayIcon.Hide();
  598. KillTimer(nIDEvent);
  599. }
  600. break;
  601. case CLOSE_WINDOW_TIMER:
  602. {
  603. //m_quickPaste.CloseQPasteWnd();
  604. }
  605. break;
  606. case REMOVE_OLD_ENTRIES_TIMER:
  607. {
  608. m_thread.FireDeleteEntries();
  609. }
  610. break;
  611. case REMOVE_OLD_TEMP_FILES:
  612. {
  613. m_thread.FireRemoveTempFiles();
  614. }
  615. break;
  616. case KEY_STATE_MODIFIERS:
  617. m_keyModifiersTimerCount++;
  618. if(m_keyStateModifiers != 0)
  619. {
  620. BYTE keyState = CAccels::GetKeyStateModifiers();
  621. //Have they release the key state modifiers yet(ctrl, shift, alt)
  622. if((m_keyStateModifiers &keyState) == 0)
  623. {
  624. KillTimer(KEY_STATE_MODIFIERS);
  625. long waitTime = (long)(GetTickCount() - m_startKeyStateTime);
  626. if(m_bMovedSelectionMoveKeyState || m_keyModifiersTimerCount > g_Opt.GetKeyStateWaitTimerCount())
  627. {
  628. 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));
  629. m_quickPaste.OnKeyStateUp();
  630. }
  631. else
  632. {
  633. Log(StrF(_T("Timer KEY_STATE_MODIFIERS count NOT hit(%d), count (%d) time (%d)"), g_Opt.GetKeyStateWaitTimerCount(), m_keyModifiersTimerCount, waitTime));
  634. m_quickPaste.SetKeyModiferState(false);
  635. }
  636. m_keyStateModifiers = 0;
  637. m_keyModifiersTimerCount = 0;
  638. m_bMovedSelectionMoveKeyState = 0;
  639. }
  640. }
  641. else
  642. {
  643. KillTimer(KEY_STATE_MODIFIERS);
  644. }
  645. break;
  646. case ACTIVE_WINDOW_TIMER:
  647. {
  648. if(theApp.m_bShowingQuickPaste)
  649. {
  650. theApp.m_activeWnd.TrackActiveWnd(false);
  651. }
  652. }
  653. break;
  654. case READ_RANDOM_DB_FILE:
  655. {
  656. m_thread.FireReadDbFile();
  657. }
  658. break;
  659. case GROUP_DOUBLE_CLICK:
  660. {
  661. KillTimer(GROUP_DOUBLE_CLICK);
  662. Log(StrF(_T("Processing single click of groupId %d in timer, opening ditto to this group"), m_doubleClickGroupId));
  663. DWORD maxDiff = (DWORD)(CGetSetOptions::GetGroupDoubleClickTimeMS() * 1.5);
  664. DWORD diff = GetTickCount() - m_doubleClickGroupStartTime;
  665. if(diff < maxDiff)
  666. {
  667. if(m_doubleClickGroupId > -1)
  668. {
  669. if (theApp.EnterGroupID(m_doubleClickGroupId, FALSE, TRUE))
  670. {
  671. theApp.m_activeWnd.TrackActiveWnd(true);
  672. StartKeyModifyerTimer();
  673. m_quickPaste.ShowQPasteWnd(this, false, true, FALSE);
  674. }
  675. }
  676. }
  677. else
  678. {
  679. 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));
  680. }
  681. m_doubleClickGroupId = -1;
  682. m_doubleClickGroupStartTime = 0;
  683. }
  684. break;
  685. case SCREEN_RESOLUTION_CHANGED:
  686. {
  687. KillTimer(SCREEN_RESOLUTION_CHANGED);
  688. m_quickPaste.OnScreenResolutionChange();
  689. }
  690. break;
  691. case DELAYED_SHOW_DITTO_TIMER:
  692. {
  693. KillTimer(DELAYED_SHOW_DITTO_TIMER);
  694. m_quickPaste.ShowQPasteWnd(this, false, false, FALSE);
  695. }
  696. break;
  697. case SET_WINDOWS_THEME_TIMER:
  698. {
  699. KillTimer(SET_WINDOWS_THEME_TIMER);
  700. auto theme = g_Opt.GetTheme();
  701. if (theme == _T(""))
  702. {
  703. g_Opt.m_Theme.Load(theme);
  704. auto visible = m_quickPaste.IsWindowVisibleEx();
  705. m_quickPaste.CloseQPasteWnd();
  706. if (visible)
  707. {
  708. m_quickPaste.ShowQPasteWnd(this, true, false, true);
  709. }
  710. }
  711. }
  712. break;
  713. case CLOSE_NO_DB_WINDOW_TIMER:
  714. KillTimer(CLOSE_NO_DB_WINDOW_TIMER);
  715. theApp.CloseNoDbWindow();
  716. break;
  717. }
  718. CFrameWnd::OnTimer(nIDEvent);
  719. }
  720. LRESULT CMainFrame::OnShowTrayIcon(WPARAM wParam, LPARAM lParam)
  721. {
  722. if(lParam)
  723. {
  724. if(!m_trayIcon.IsHidden())
  725. {
  726. KillTimer(HIDE_ICON_TIMER);
  727. SetTimer(HIDE_ICON_TIMER, 40000, 0);
  728. }
  729. }
  730. if(wParam)
  731. {
  732. m_trayIcon.Show();
  733. }
  734. else
  735. {
  736. m_trayIcon.Hide();
  737. }
  738. return TRUE;
  739. }
  740. void CMainFrame::OnFirstShowquickpaste()
  741. {
  742. m_quickPaste.ShowQPasteWnd(this, false, false, FALSE);
  743. }
  744. void CMainFrame::OnFirstToggleConnectCV()
  745. {
  746. theApp.ToggleConnectCV();
  747. }
  748. void CMainFrame::OnUpdateFirstToggleConnectCV(CCmdUI *pCmdUI)
  749. {
  750. theApp.UpdateMenuConnectCV(pCmdUI->m_pMenu, ID_FIRST_TOGGLECONNECTCV);
  751. }
  752. LRESULT CMainFrame::OnClipboardCopied(WPARAM wParam, LPARAM lParam)
  753. {
  754. Log(_T("Start of function OnClipboardCopied, adding clip to thread for processing"));
  755. CClip *pClip = (CClip*)wParam;
  756. if(pClip != NULL)
  757. {
  758. m_thread.AddClipToSave(pClip);
  759. }
  760. Log(_T("End of function OnClipboardCopied"));
  761. return TRUE;
  762. }
  763. BOOL CMainFrame::PreTranslateMessage(MSG *pMsg)
  764. {
  765. //forward the mouse wheel onto the window under the cursor
  766. //normally windows only sends it to the window with focus, bypass this
  767. if ((pMsg->message == WM_MOUSEWHEEL || pMsg->message == WM_MOUSEHWHEEL) &&
  768. ::GetCapture() == nullptr)
  769. {
  770. POINT mouse;
  771. GetCursorPos(&mouse);
  772. HWND hwndFromPoint = ::WindowFromPoint(mouse);
  773. if (pMsg->hwnd != hwndFromPoint)
  774. {
  775. DWORD winProcessId = 0;
  776. ::GetWindowThreadProcessId(hwndFromPoint, &winProcessId);
  777. if (winProcessId == ::GetCurrentProcessId()) //no-fail!
  778. {
  779. pMsg->hwnd = hwndFromPoint;
  780. }
  781. }
  782. if (GetKeyState(VK_SHIFT) & 0x8000)
  783. pMsg->message = WM_MOUSEHWHEEL;
  784. }
  785. return CFrameWnd::PreTranslateMessage(pMsg);
  786. }
  787. void CMainFrame::OnClose()
  788. {
  789. CloseAllOpenDialogs();
  790. if(m_pEditFrameWnd)
  791. {
  792. if(m_pEditFrameWnd->CloseAll() == false)
  793. {
  794. return ;
  795. }
  796. }
  797. Log(_T("OnClose - before stop MainFrm thread"));
  798. m_thread.Stop();
  799. Log(_T("OnClose - after stop MainFrm thread"));
  800. theApp.BeforeMainClose();
  801. m_PowerManager.Close();
  802. CFrameWnd::OnClose();
  803. }
  804. bool CMainFrame::CloseAllOpenDialogs()
  805. {
  806. bool bRet = false;
  807. DWORD dwordProcessId;
  808. DWORD dwordChildWindowProcessId;
  809. GetWindowThreadProcessId(this->m_hWnd, &dwordProcessId);
  810. ASSERT(dwordProcessId);
  811. CWnd *pTempWnd = GetDesktopWindow()->GetWindow(GW_CHILD);
  812. while((pTempWnd = pTempWnd->GetWindow(GW_HWNDNEXT)) != NULL)
  813. {
  814. if(pTempWnd->GetSafeHwnd() == NULL)
  815. {
  816. break;
  817. }
  818. GetWindowThreadProcessId(pTempWnd->GetSafeHwnd(), &dwordChildWindowProcessId);
  819. if(dwordChildWindowProcessId == dwordProcessId)
  820. {
  821. TCHAR szTemp[100];
  822. GetClassName(pTempWnd->GetSafeHwnd(), szTemp, 100);
  823. // #32770 is class name for dialogs so don't process the message if it is a dialog
  824. if(STRCMP(szTemp, _T("#32770")) == 0)
  825. {
  826. pTempWnd->SendMessage(WM_CLOSE, 0, 0);
  827. bRet = true;
  828. }
  829. }
  830. }
  831. MSG msg;
  832. while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  833. {
  834. TranslateMessage(&msg);
  835. DispatchMessage(&msg);
  836. }
  837. return bRet;
  838. }
  839. LRESULT CMainFrame::OnLoadClipOnClipboard(WPARAM wParam, LPARAM lParam)
  840. {
  841. CClip *pClip = (CClip*)wParam;
  842. if(pClip == NULL)
  843. {
  844. LogSendRecieveInfo("---------ERROR OnLoadClipOnClipboard pClip == NULL");
  845. return FALSE;
  846. }
  847. if(pClip)
  848. {
  849. CProcessPaste paste;
  850. paste.m_bSendPaste = false;
  851. paste.m_bActivateTarget = false;
  852. paste.m_pasteOptions.m_delayRenderLockout = GetTickCount();
  853. LogSendRecieveInfo("---------OnLoadClipOnClipboard - Before PutFormats on clipboard");
  854. paste.m_pOle->PutFormatOnClipboard(&pClip->m_Formats);
  855. paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  856. LogSendRecieveInfo("---------OnLoadClipOnClipboard - After PutFormats on clipboard");
  857. LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - Setting clip id: %d on ole clipboard"), pClip->m_id));
  858. paste.GetClipIDs().Add(pClip->m_id);
  859. paste.DoPaste();
  860. LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - After paste clip id: %d on ole clipboard"), pClip->m_id));
  861. }
  862. delete pClip;
  863. return TRUE;
  864. }
  865. LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
  866. {
  867. CClipList *pClipList = (CClipList*)wParam;
  868. if(pClipList == NULL)
  869. {
  870. LogSendRecieveInfo("---------OnAddToDatabaseFromSocket - ERROR pClipList == NULL");
  871. return FALSE;
  872. }
  873. DWORD flags = (DWORD)lParam;
  874. if(flags & REMOTE_CLIP_ADD_TO_CLIPBOARD)
  875. {
  876. CClip *pClip = pClipList->GetTail();
  877. if(pClip)
  878. {
  879. LogSendRecieveInfo("OnAddToDatabaseFromSocket - Adding clip from socket setting clip to be put on clipboard");
  880. pClip->m_param1 |= REMOTE_CLIP_ADD_TO_CLIPBOARD;
  881. }
  882. }
  883. if (flags & REMOTE_CLIP_MANUAL_SEND)
  884. {
  885. CClip *pClip = pClipList->GetTail();
  886. if (pClip)
  887. {
  888. LogSendRecieveInfo("OnAddToDatabaseFromSocket - Adding clip from socket setting clip was a manual send from other side");
  889. pClip->m_param1 |= REMOTE_CLIP_MANUAL_SEND;
  890. }
  891. }
  892. m_thread.AddRemoteClipToSave(pClipList);
  893. delete pClipList;
  894. return TRUE;
  895. }
  896. LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
  897. {
  898. CString csNewText = (TCHAR*)wParam;
  899. ShowErrorMessage(_T("Ditto - Send/Receive Error"), csNewText);
  900. return TRUE;
  901. }
  902. LRESULT CMainFrame::OnErrorMsg(WPARAM wParam, LPARAM lParam)
  903. {
  904. CString csNewText = (TCHAR*)wParam;
  905. ShowErrorMessage(_T("Ditto"), csNewText);
  906. return TRUE;
  907. }
  908. CString WndName(HWND hParent)
  909. {
  910. TCHAR cWindowText[200];
  911. ::GetWindowText(hParent, cWindowText, 100);
  912. int nCount = 0;
  913. while(STRLEN(cWindowText) <= 0)
  914. {
  915. hParent = ::GetParent(hParent);
  916. if(hParent == NULL)
  917. {
  918. break;
  919. }
  920. ::GetWindowText(hParent, cWindowText, 100);
  921. nCount++;
  922. if(nCount > 100)
  923. {
  924. Log(_T("GetTargetName reached maximum search depth of 100"));
  925. break;
  926. }
  927. }
  928. return cWindowText;
  929. }
  930. void CMainFrame::OnFirstHelp()
  931. {
  932. CHyperLink::GotoURL(_T("https://github.com/sabrogden/Ditto/wiki"), SW_SHOW);
  933. }
  934. void CMainFrame::ShowErrorMessage(CString csTitle, CString csMessage)
  935. {
  936. Log(StrF(_T("ShowErrorMessage %s - %s"), csTitle, csMessage));
  937. m_trayIcon.SetBalloonDetails(csMessage, csTitle, CTrayNotifyIcon::BalloonStyle::Error, CGetSetOptions::GetBalloonTimeout());
  938. }
  939. void CMainFrame::OnFirstImport()
  940. {
  941. theApp.ImportClips(theApp.m_MainhWnd);
  942. }
  943. void CMainFrame::ShowEditWnd(CClipIDs &Ids)
  944. {
  945. CWaitCursor wait;
  946. bool bCreatedWindow = false;
  947. if(m_pEditFrameWnd == NULL)
  948. {
  949. m_pEditFrameWnd = new CEditFrameWnd;
  950. m_pEditFrameWnd->LoadFrame(IDR_MAINFRAME);
  951. bCreatedWindow = true;
  952. }
  953. if(m_pEditFrameWnd)
  954. {
  955. m_pEditFrameWnd->EditIds(Ids);
  956. m_pEditFrameWnd->SetNotifyWnd(m_hWnd);
  957. if(bCreatedWindow)
  958. {
  959. CSize sz;
  960. CPoint pt;
  961. CGetSetOptions::GetEditWndSize(sz);
  962. CGetSetOptions::GetEditWndPoint(pt);
  963. CRect cr(pt, sz);
  964. EnsureWindowVisible(&cr);
  965. m_pEditFrameWnd->MoveWindow(cr);
  966. }
  967. m_pEditFrameWnd->ShowWindow(SW_SHOW);
  968. m_pEditFrameWnd->SetForegroundWindow();
  969. m_pEditFrameWnd->SetFocus();
  970. }
  971. }
  972. LRESULT CMainFrame::OnEditWndClose(WPARAM wParam, LPARAM lParam)
  973. {
  974. m_pEditFrameWnd = NULL;
  975. return TRUE;
  976. }
  977. LRESULT CMainFrame::OnSetConnected(WPARAM wParam, LPARAM lParam)
  978. {
  979. if(wParam)
  980. {
  981. theApp.SetConnectCV(true);
  982. }
  983. else if(lParam)
  984. {
  985. theApp.SetConnectCV(false);
  986. }
  987. return TRUE;
  988. }
  989. LRESULT CMainFrame::OnOpenCloseWindow(WPARAM wParam, LPARAM lParam)
  990. {
  991. if(wParam)
  992. {
  993. StartKeyModifyerTimer();
  994. ShowQPasteWithActiveWindowCheck();
  995. }
  996. else if(lParam)
  997. {
  998. m_quickPaste.HideQPasteWnd();
  999. }
  1000. return TRUE;
  1001. }
  1002. void CMainFrame::OnDestroy()
  1003. {
  1004. CFrameWnd::OnDestroy();
  1005. if(m_pEditFrameWnd)
  1006. {
  1007. m_pEditFrameWnd->DestroyWindow();
  1008. }
  1009. }
  1010. void CMainFrame::OnFirstNewclip()
  1011. {
  1012. CClipIDs IDs;
  1013. IDs.Add( - 1);
  1014. theApp.EditItems(IDs, true);
  1015. }
  1016. void CMainFrame::OnFirstOption()
  1017. {
  1018. if(m_pOptions != NULL)
  1019. {
  1020. ::SetForegroundWindow(m_pOptions->m_hWnd);
  1021. }
  1022. else
  1023. {
  1024. m_pOptions = new COptionsSheet(_T(""));
  1025. if(m_pOptions != NULL)
  1026. {
  1027. ((COptionsSheet*)m_pOptions)->SetNotifyWnd(m_hWnd);
  1028. m_pOptions->Create();
  1029. m_pOptions->ShowWindow(SW_SHOW);
  1030. }
  1031. }
  1032. }
  1033. void CMainFrame::OnFirstGlobalhotkeys()
  1034. {
  1035. if(m_pGlobalClips != NULL)
  1036. {
  1037. ::SetForegroundWindow(m_pGlobalClips->m_hWnd);
  1038. }
  1039. else
  1040. {
  1041. m_pGlobalClips = new GlobalClips();
  1042. CAlphaBlend tran;
  1043. tran.SetTransparent(m_hWnd, 0, 1);
  1044. if(m_pGlobalClips != NULL)
  1045. {
  1046. ((GlobalClips*)m_pGlobalClips)->SetNotifyWnd(m_hWnd);
  1047. m_pGlobalClips->Create(IDD_GLOBAL_CLIPS, NULL);
  1048. m_pGlobalClips->ShowWindow(SW_SHOW);
  1049. }
  1050. }
  1051. }
  1052. LRESULT CMainFrame::OnShowOptions(WPARAM wParam, LPARAM lParam)
  1053. {
  1054. OnFirstOption();
  1055. return 0;
  1056. }
  1057. LRESULT CMainFrame::OnOptionsClosed(WPARAM wParam, LPARAM lParam)
  1058. {
  1059. BOOL themeChanged = (BOOL)wParam;
  1060. m_trayIcon.MinimiseToTray(this);
  1061. CAlphaBlend tran;
  1062. tran.SetTransparent(m_hWnd, 255, 0);
  1063. delete m_pOptions;
  1064. m_pOptions = NULL;
  1065. if (themeChanged)
  1066. {
  1067. g_Opt.m_Theme.Load(g_Opt.GetTheme());
  1068. m_quickPaste.CloseQPasteWnd();
  1069. }
  1070. else
  1071. {
  1072. if (m_quickPaste.m_pwndPaste != NULL)
  1073. {
  1074. m_quickPaste.m_pwndPaste->PostMessage(NM_POST_OPTIONS_WINDOW);
  1075. }
  1076. }
  1077. m_trayIcon.SetMenu(NULL, IDR_MENU);
  1078. theApp.m_Language.UpdateTrayIconRightClickMenu(&m_trayIcon.GetMenu());
  1079. if (CGetSetOptions::GetShowIconInSysTray())
  1080. {
  1081. m_trayIcon.Show();
  1082. }
  1083. else
  1084. {
  1085. m_trayIcon.Hide();
  1086. }
  1087. return 0;
  1088. }
  1089. LRESULT CMainFrame::OnGlobalClipsClosed(WPARAM wParam, LPARAM lParam)
  1090. {
  1091. m_trayIcon.MinimiseToTray(this);
  1092. CAlphaBlend tran;
  1093. tran.SetTransparent(m_hWnd, 255, 0);
  1094. delete m_pGlobalClips;
  1095. m_pGlobalClips = NULL;
  1096. return 0;
  1097. }
  1098. void CMainFrame::RefreshShowInTaskBar()
  1099. {
  1100. BOOL windowVisible = m_quickPaste.IsWindowVisibleEx();
  1101. m_quickPaste.CloseQPasteWnd();
  1102. if (windowVisible)
  1103. {
  1104. m_quickPaste.ShowQPasteWnd(this, true, false, true);
  1105. }
  1106. }
  1107. LRESULT CMainFrame::OnDeleteClipDataClosed(WPARAM wParam, LPARAM lParam)
  1108. {
  1109. m_trayIcon.MinimiseToTray(this);
  1110. CAlphaBlend tran;
  1111. tran.SetTransparent(m_hWnd, 255, 0);
  1112. delete m_pDeleteClips;
  1113. m_pDeleteClips = NULL;
  1114. return 0;
  1115. }
  1116. void CMainFrame::OnFirstDeleteclipdata()
  1117. {
  1118. //this->ShowWindow(SW_HIDE);
  1119. if (m_pDeleteClips != NULL)
  1120. {
  1121. ::SetForegroundWindow(m_pDeleteClips->m_hWnd);
  1122. }
  1123. else
  1124. {
  1125. m_pDeleteClips = new CDeleteClipData();
  1126. CAlphaBlend tran;
  1127. tran.SetTransparent(m_hWnd, 0, 1);
  1128. if (m_pDeleteClips != NULL)
  1129. {
  1130. ((CDeleteClipData*) m_pDeleteClips)->SetNotifyWnd(m_hWnd);
  1131. m_pDeleteClips->Create(IDD_DELETE_CLIP_DATA, NULL);
  1132. m_pDeleteClips->ShowWindow(SW_SHOW);
  1133. }
  1134. }
  1135. }
  1136. LRESULT CMainFrame::OnSaveClipboardMessage(WPARAM wParam, LPARAM lParam)
  1137. {
  1138. OnFirstSavecurrentclipboard();
  1139. return TRUE;
  1140. }
  1141. void CMainFrame::OnFirstSavecurrentclipboard()
  1142. {
  1143. Log(_T("Start Saving the current clipboard to the database"));
  1144. CClip* pClip = new CClip;
  1145. if(pClip)
  1146. {
  1147. CClipTypes* pTypes = theApp.LoadTypesFromDB();
  1148. if(pTypes)
  1149. {
  1150. if(pClip->LoadFromClipboard(pTypes, false, _T("")))
  1151. {
  1152. Log(_T("Loaded clips from the clipboard, sending message to save to the db"));
  1153. ::PostMessage(m_hWnd, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  1154. }
  1155. else
  1156. {
  1157. Log(_T("Failed to load clips from the clipboard, not saving to db"));
  1158. delete pClip;
  1159. pClip = NULL;
  1160. }
  1161. }
  1162. else
  1163. {
  1164. Log(_T("Failed to load supported types from the db, not saving to the db"));
  1165. }
  1166. }
  1167. Log(_T("Start Saving the current clipboard to the database"));
  1168. }
  1169. LRESULT CMainFrame::OnReAddTaskBarIcon(WPARAM wParam, LPARAM lParam)
  1170. {
  1171. if(CGetSetOptions::GetShowIconInSysTray())
  1172. {
  1173. m_trayIcon.SetIcon(CTrayNotifyIcon::LoadIcon(IDR_MAINFRAME));
  1174. }
  1175. return TRUE;
  1176. }
  1177. LRESULT CMainFrame::OnReOpenDatabase(WPARAM wParam, LPARAM lParam)
  1178. {
  1179. Log(StrF(_T("OnReOpenDatabase, Start closing and reopening database Delay: %d"), CGetSetOptions::GetWindowsResumeDelayReOpenDbMS()));
  1180. try
  1181. {
  1182. Sleep(CGetSetOptions::GetWindowsResumeDelayReOpenDbMS());
  1183. m_quickPaste.CloseQPasteWnd();
  1184. theApp.m_db.close();
  1185. OpenDatabase(CGetSetOptions::GetDBPath());
  1186. }
  1187. CATCH_SQLITE_EXCEPTION
  1188. Log(StrF(_T("OnReOpenDatabase, End closing and reopening database Delay: %d"), CGetSetOptions::GetWindowsResumeDelayReOpenDbMS()));
  1189. return TRUE;
  1190. }
  1191. LRESULT CMainFrame::OnShowMsgWindow(WPARAM wParam, LPARAM lParam)
  1192. {
  1193. CString *pMsg = (CString*)wParam;
  1194. int clipId = (int)lParam;
  1195. m_trayIcon.SetBalloonDetails(pMsg->GetBuffer(), _T("Ditto"), CTrayNotifyIcon::BalloonStyle::Info, CGetSetOptions::GetBalloonTimeout());
  1196. delete pMsg;
  1197. return TRUE;
  1198. }
  1199. LRESULT CMainFrame::OnShowDittoGroup(WPARAM wParam, LPARAM lParam)
  1200. {
  1201. int groupId = (int)wParam;
  1202. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lID = %d"), groupId);
  1203. if(q.eof() == false)
  1204. {
  1205. if(q.getIntField(_T("bIsGroup")) > 0)
  1206. {
  1207. PasteOrShowGroup(groupId, FALSE, FALSE, FALSE, false);
  1208. }
  1209. }
  1210. return TRUE;
  1211. }
  1212. void CMainFrame::OnFirstFixupstickycliporder()
  1213. {
  1214. ReOrderStickyClips(-1, theApp.m_db);
  1215. }
  1216. LRESULT CMainFrame::OnResolutionChange(WPARAM wParam, LPARAM lParam)
  1217. {
  1218. if (m_startupScreenWidth != GetScreenWidth() ||
  1219. m_startupScreenHeight != GetScreenHeight())
  1220. {
  1221. m_startupScreenWidth = GetScreenWidth();
  1222. m_startupScreenHeight = GetScreenHeight();
  1223. SetTimer(SCREEN_RESOLUTION_CHANGED, 1000, NULL);
  1224. }
  1225. return TRUE;
  1226. }
  1227. void CMainFrame::OnWinIniChange(LPCTSTR lpszSection)
  1228. {
  1229. CFrameWnd::OnWinIniChange(lpszSection);
  1230. if (lpszSection != NULL &&
  1231. wcscmp(lpszSection, L"ImmersiveColorSet") == 0)
  1232. {
  1233. Log(StrF(_T("OnWinIniChange %s, setting timer to 1000ms to change theme"), lpszSection));
  1234. KillTimer(SET_WINDOWS_THEME_TIMER);
  1235. SetTimer(SET_WINDOWS_THEME_TIMER, 1000, NULL);
  1236. }
  1237. }
  1238. void CMainFrame::OnFirstShowstartupmessage()
  1239. {
  1240. BOOL existing = CGetSetOptions::GetShowStartupMessage();
  1241. CGetSetOptions::SetShowStartupMessage(!existing);
  1242. }
  1243. void CMainFrame::OnUpdateFirstShowstartupmessage(CCmdUI *pCmdUI)
  1244. {
  1245. if (pCmdUI == NULL ||
  1246. pCmdUI->m_pMenu == NULL)
  1247. {
  1248. return;
  1249. }
  1250. if (CGetSetOptions::GetShowStartupMessage())
  1251. {
  1252. pCmdUI->m_pMenu->CheckMenuItem(ID_FIRST_SHOWSTARTUPMESSAGE, MF_CHECKED);
  1253. }
  1254. else
  1255. {
  1256. pCmdUI->m_pMenu->CheckMenuItem(ID_FIRST_SHOWSTARTUPMESSAGE, MF_UNCHECKED);
  1257. }
  1258. }
  1259. void CMainFrame::OnFirstBackupdatabase()
  1260. {
  1261. BackupDbPrompt(m_hWnd);
  1262. }
  1263. void CMainFrame::OnFirstRestoredatabase()
  1264. {
  1265. RestoreDbPrompt(m_hWnd);
  1266. }
  1267. LRESULT CMainFrame::OnBackupDb(WPARAM wParam, LPARAM lParam)
  1268. {
  1269. OnFirstBackupdatabase();
  1270. return TRUE;
  1271. }
  1272. LRESULT CMainFrame::OnRestoreDb(WPARAM wParam, LPARAM lParam)
  1273. {
  1274. OnFirstRestoredatabase();
  1275. return TRUE;
  1276. }
  1277. void CMainFrame::OnFirstDeleteallnonusedclips()
  1278. {
  1279. int nRet = MessageBox(theApp.m_Language.GetString("Delete_All_Non_Used_Clips", "Delete all clips that are not groups, in groups, marked as never auto delete, has a shortcut key or marked as sticky.\r\n\r\nThis cannot be undone."), _T("Ditto"), MB_YESNO | MB_TOPMOST);
  1280. if (nRet == IDNO)
  1281. {
  1282. return;
  1283. }
  1284. DeleteNonUsedClips(false);
  1285. theApp.RefreshView();
  1286. }
  1287. LRESULT CMainFrame::OnPasteClip(WPARAM wParam, LPARAM lParam)
  1288. {
  1289. PasteOrShowGroup(wParam, TRUE, FALSE, TRUE, false);
  1290. return TRUE;
  1291. }
  1292. LRESULT CMainFrame::OnEditClip(WPARAM wParam, LPARAM lParam)
  1293. {
  1294. CClipIDs IDs;
  1295. IDs.Add(wParam);
  1296. theApp.EditItems(IDs, true);
  1297. return TRUE;
  1298. }