MainFrm.cpp 41 KB

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