CP_Main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // CP_Main.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainFrm.h"
  6. #include "Misc.h"
  7. #include "SelectDB.h"
  8. #include ".\cp_main.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // The one and only CCP_MainApp object
  16. CCP_MainApp theApp;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CCP_MainApp
  19. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  20. //{{AFX_MSG_MAP(CCP_MainApp)
  21. // NOTE - the ClassWizard will add and remove mapping macros here.
  22. // DO NOT EDIT what you see in these blocks of generated code!
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CCP_MainApp construction
  27. CCP_MainApp::CCP_MainApp()
  28. {
  29. m_bAppRunning = false;
  30. m_bAppExiting = false;
  31. m_MainhWnd = NULL;
  32. m_pMainFrame = NULL;
  33. m_bShowingQuickPaste = false;
  34. m_bShowingOptions = false;
  35. m_bShowCopyProperties = false;
  36. m_bRemoveOldEntriesPending = false;
  37. m_pDatabase = NULL;
  38. // Place all significant initialization in InitInstance
  39. }
  40. CCP_MainApp::~CCP_MainApp()
  41. {
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CCP_MainApp initialization
  45. BOOL CCP_MainApp::InitInstance()
  46. {
  47. LOG("InitInstance");
  48. AfxEnableControlContainer();
  49. m_hMutex = CreateMutex(NULL, FALSE, "Ditto Is Now Running");
  50. DWORD dwError = GetLastError();
  51. if(dwError == ERROR_ALREADY_EXISTS)
  52. {
  53. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  54. if(hWnd)
  55. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  56. return TRUE;
  57. }
  58. m_cfIgnoreClipboard = ::RegisterClipboardFormat("Clipboard Viewer Ignore");
  59. if(CheckDBExists(CGetSetOptions::GetDBPath()) == FALSE)
  60. {
  61. AfxMessageBox("Error Opening Database.");
  62. return TRUE;
  63. }
  64. AfxOleInit();
  65. CMainFrame* pFrame = new CMainFrame;
  66. m_pMainWnd = m_pMainFrame = pFrame;
  67. // prevent no one having focus on startup
  68. TargetActiveWindow();
  69. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  70. pFrame->ShowWindow(SW_SHOW);
  71. pFrame->UpdateWindow();
  72. // prevent no one having focus on startup
  73. ReleaseFocus();
  74. return TRUE;
  75. }
  76. void CCP_MainApp::AfterMainCreate()
  77. {
  78. m_MainhWnd = m_pMainFrame->m_hWnd;
  79. ASSERT( ::IsWindow(m_MainhWnd) );
  80. g_Opt.SetMainHWND((long)m_MainhWnd);
  81. g_HotKeys.Init( m_MainhWnd );
  82. // create hotkeys here. They are automatically deleted on exit
  83. m_pDittoHotKey = new CHotKey("DittoHotKey",704); //704 is ctrl-tilda
  84. m_pCopyHotKey = new CHotKey("CopyHotKey");
  85. g_HotKeys.RegisterAll();
  86. // CopyThread initialization
  87. StartCopyThread();
  88. m_bAppRunning = true;
  89. }
  90. void CCP_MainApp::BeforeMainClose()
  91. {
  92. ASSERT( m_bAppRunning && !m_bAppExiting );
  93. m_bAppRunning = false;
  94. m_bAppExiting = true;
  95. g_HotKeys.UnregisterAll();
  96. StopCopyThread();
  97. }
  98. /*
  99. Re: Targeting the previous focus window
  100. We usually gain focus after the following messages:
  101. keyboard: WM_KEYUP(0x0101),WM_CHAR(0x0102),WM_HOTKEY(0x0312)
  102. mouse: WM_MOUSEFIRST(0x0200),WM_MOUSEMOVE(0x0200),WM_LBUTTONDOWN(0x0201)
  103. CMainFrame::PreTranslateMessage is used to intercept messages before
  104. they are processed (before we are actually given focus) in order to
  105. save the previous window that had focus.
  106. - It currently just handles "activating" mouse messages when showing.
  107. ShowQPasteWnd also has a call to "TargetActiveWindow" which handles
  108. finding the Target on hotkey activation.
  109. This works well for most window switching (mouse or hotkey), but does
  110. not work well for <Alt>-<Tab> or other window switching applications
  111. (e.g. the taskbar tray), since the previous window was only a means to
  112. switching and not the target itself.
  113. - one solution might be to always monitor the current foreground
  114. window using system hooks or a short (e.g. 1 sec) timer... though this
  115. *might* be cpu intensive (slow). I'm currently looking into using
  116. WH_CBT system hooks in a separate dll (see: robpitt's
  117. http://website.lineone.net/~codebox/focuslog.zip).
  118. */
  119. bool CCP_MainApp::TargetActiveWindow()
  120. {
  121. HWND hOld = m_hTargetWnd;
  122. HWND hNew = ::GetForegroundWindow();
  123. if( hNew == m_hTargetWnd || !::IsWindow(hNew) || IsAppWnd(hNew) )
  124. return false;
  125. m_hTargetWnd = hNew;
  126. if( QPasteWnd() )
  127. QPasteWnd()->UpdateStatus(true);
  128. // Tracking / Debugging
  129. /*
  130. LOG( StrF(
  131. "Target Changed" \
  132. "\n\tOld = 0x%08x: \"%s\"" \
  133. "\n\tNew = 0x%08x: \"%s\"\n",
  134. hOld, (LPCTSTR) GetWndText(hOld),
  135. hNew, (LPCTSTR) GetWndText(hNew) ) );
  136. */
  137. return true;
  138. }
  139. bool CCP_MainApp::ActivateTarget()
  140. {
  141. if( !::IsWindow(m_hTargetWnd) || m_hTargetWnd == ::GetForegroundWindow() )
  142. return false;
  143. ::SetForegroundWindow( m_hTargetWnd );
  144. // ::SetFocus( m_hTargetWnd );
  145. return true;
  146. }
  147. bool CCP_MainApp::ReleaseFocus()
  148. {
  149. if( IsAppWnd(::GetForegroundWindow()) )
  150. return ActivateTarget();
  151. return false;
  152. }
  153. // sends Ctrl-V to the TargetWnd
  154. void CCP_MainApp::SendPaste()
  155. {
  156. if( !ActivateTarget() )
  157. {
  158. SetStatus("SendPaste FAILED!");
  159. return;
  160. }
  161. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  162. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
  163. keybd_event('V', 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  164. keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  165. }
  166. // CopyThread
  167. void CCP_MainApp::StartCopyThread()
  168. {
  169. ASSERT( m_MainhWnd );
  170. CClipTypes* pTypes = LoadTypesFromDB();
  171. // initialize to:
  172. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  173. // - true = use Asynchronous communication (PostMessage)
  174. // - true = enable copying on clipboard changes
  175. // - pTypes = the supported types to use
  176. m_CopyThread.Init( CCopyConfig( m_MainhWnd, true, true, pTypes ) );
  177. VERIFY( m_CopyThread.CreateThread(CREATE_SUSPENDED) );
  178. m_CopyThread.ResumeThread();
  179. }
  180. void CCP_MainApp::StopCopyThread()
  181. {
  182. EnableCbCopy(false);
  183. m_CopyThread.Quit();
  184. SaveCopyClips();
  185. }
  186. // Allocates a new CClipTypes
  187. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  188. {
  189. CClipTypes* pTypes = new CClipTypes;
  190. try
  191. {
  192. CTypesTable recset;
  193. recset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM Types" ,NULL);
  194. while(!recset.IsEOF())
  195. {
  196. pTypes->Add( GetFormatID(recset.m_TypeText) );
  197. recset.MoveNext();
  198. }
  199. recset.Close();
  200. }
  201. catch(CDaoException* e)
  202. {
  203. ASSERT(FALSE);
  204. e->Delete();
  205. }
  206. if( pTypes->GetSize() <= 0 )
  207. {
  208. pTypes->Add(CF_TEXT);
  209. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  210. }
  211. return pTypes;
  212. }
  213. void CCP_MainApp::ReloadTypes()
  214. {
  215. CClipTypes* pTypes = LoadTypesFromDB();
  216. if( pTypes )
  217. m_CopyThread.SetSupportedTypes( pTypes );
  218. }
  219. long CCP_MainApp::SaveCopyClips()
  220. {
  221. long lID = 0;
  222. int count;
  223. CClipList* pClips = m_CopyThread.GetClips(); // we now own pClips
  224. if( !pClips )
  225. return 0;
  226. count = pClips->AddToDB( true );
  227. if( count > 0 )
  228. {
  229. OnCopyCompleted( lID, count );
  230. lID = pClips->GetTail()->m_ID;
  231. }
  232. delete pClips;
  233. return lID;
  234. }
  235. void CCP_MainApp::RefreshView()
  236. {
  237. if( m_bShowingQuickPaste )
  238. {
  239. ASSERT( QPasteWnd() );
  240. QPasteWnd()->PostMessage(WM_REFRESH_VIEW);
  241. }
  242. }
  243. void CCP_MainApp::OnPasteCompleted()
  244. {
  245. // the list only changes if UpdateTimeOnPaste is true (updated time)
  246. if( g_Opt.m_bUpdateTimeOnPaste )
  247. RefreshView();
  248. }
  249. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  250. {
  251. if( count <= 0 )
  252. return;
  253. // queue a message to RemoveOldEntries
  254. Delayed_RemoveOldEntries( 60000 );
  255. // update copy statistics
  256. CGetSetOptions::SetTripCopyCount( -count );
  257. CGetSetOptions::SetTotalCopyCount( -count );
  258. RefreshView();
  259. ShowCopyProperties( lLastID );
  260. }
  261. void CCP_MainApp::SetStatus( const char* status, bool bRepaintImmediately )
  262. {
  263. m_Status = status;
  264. if( QPasteWnd() )
  265. QPasteWnd()->UpdateStatus( bRepaintImmediately );
  266. }
  267. void CCP_MainApp::ShowPersistent( bool bVal )
  268. {
  269. g_Opt.SetShowPersistent( bVal );
  270. // give some visual indication
  271. if( m_bShowingQuickPaste )
  272. {
  273. ASSERT( QPasteWnd() );
  274. QPasteWnd()->SetCaptionColorActive( !g_Opt.m_bShowPersistent );
  275. QPasteWnd()->RefreshNc();
  276. }
  277. }
  278. void CCP_MainApp::ShowCopyProperties( long lID )
  279. {
  280. if( m_bShowCopyProperties && lID > 0 )
  281. {
  282. HWND hWndFocus = ::GetForegroundWindow();
  283. m_bShowCopyProperties = false;
  284. ::SendMessage(m_MainhWnd, WM_COPYPROPERTIES, lID, 0); // modal
  285. ::SetForegroundWindow(hWndFocus);
  286. }
  287. }
  288. void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
  289. {
  290. if( !m_bRemoveOldEntriesPending )
  291. {
  292. m_bRemoveOldEntriesPending = true;
  293. ((CMainFrame*)theApp.m_pMainWnd)->SetTimer( REMOVE_OLD_ENTRIES_TIMER, delay, 0 );
  294. }
  295. }
  296. /////////////////////////////////////////////////////////////////////////////
  297. // CCP_MainApp message handlers
  298. int CCP_MainApp::ExitInstance()
  299. {
  300. LOG("ExitInstance");
  301. if(CGetSetOptions::GetCompactAndRepairOnExit())
  302. CompactDatabase();
  303. CloseDB();
  304. return CWinApp::ExitInstance();
  305. }
  306. CDaoDatabase* CCP_MainApp::EnsureOpenDB(CString csName)
  307. {
  308. if(!m_pDatabase)
  309. m_pDatabase = new CDaoDatabase;
  310. if(!m_pDatabase->IsOpen())
  311. {
  312. if(csName == "")
  313. m_pDatabase->Open(GetDBName());
  314. else
  315. m_pDatabase->Open(csName);
  316. }
  317. if(m_pMainWnd)
  318. ((CMainFrame *)m_pMainWnd)->ResetKillDBTimer();
  319. return m_pDatabase;
  320. }
  321. BOOL CCP_MainApp::CloseDB()
  322. {
  323. if(m_pDatabase)
  324. {
  325. if(m_pDatabase->IsOpen())
  326. m_pDatabase->Close();
  327. delete m_pDatabase;
  328. m_pDatabase = NULL;
  329. }
  330. return TRUE;
  331. }
  332. // return TRUE if there is more idle processing to do
  333. BOOL CCP_MainApp::OnIdle(LONG lCount)
  334. {
  335. // let winapp handle its idle processing
  336. if( CWinApp::OnIdle(lCount) )
  337. return TRUE;
  338. return FALSE;
  339. }