Browse Source

changes to copy buffers
[SAB]


git-svn-id: svn://svn.code.sf.net/p/ditto-cp/code/trunk@371 595ec19a-5cb4-439b-94a8-42fb3063c22c

sabrogden 19 years ago
parent
commit
4b8ee344c5
8 changed files with 173 additions and 84 deletions
  1. 1 3
      Clip.cpp
  2. 36 37
      ClipboardSaveRestore.cpp
  3. 45 29
      DittoCopyBuffer.cpp
  4. 9 9
      MainFrm.cpp
  5. 4 0
      Misc.cpp
  6. 3 3
      Options.cpp
  7. 68 2
      QListCtrl.cpp
  8. 7 1
      QListCtrl.h

+ 1 - 3
Clip.cpp

@@ -28,8 +28,7 @@ COleDataObjectEx
 
 HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
 {
-	HGLOBAL hGlobal = ::GetClipboardData(cfFormat);
-    hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
+    HGLOBAL hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
 	if(hGlobal)
 	{
 		if(!::IsValid(hGlobal))
@@ -632,7 +631,6 @@ bool CClip::ModifyMainTable()
 			_T("lParentID = %d, ")
 			_T("lDontAutoDelete = %d, ")
 			_T("QuickPasteText = '%s' ")
-			_T("CopyBuffer = %d ")
 			_T("WHERE lID = %d;"), 
 			m_lShortCut, 
 			m_Desc, 

+ 36 - 37
ClipboardSaveRestore.cpp

@@ -17,43 +17,36 @@ bool CClipboardSaveRestore::Save()
 	bool bRet = false;
 	COleDataObjectEx oleData;
 	CClipFormat cf;
-	FORMATETC fm;
 
-	//Attach to the clipboard
-	if(!oleData.AttachClipboard())
+	if(::OpenClipboard(theApp.m_MainhWnd))
 	{
-		ASSERT(FALSE);
-		Log(_T("CClipboardSaveRestore::Save error opening clipboard"));
-		return bRet;
-	}
-
-	oleData.EnsureClipboardObject();
-
-	oleData.BeginEnumFormats();
-
-	while(oleData.GetNextFormat(&fm))
-	{
-		cf.m_hgData = oleData.GetGlobalData(fm.cfFormat);
-		cf.m_cfType = fm.cfFormat;
-
-		if(cf.m_hgData)
+		int nFormat = EnumClipboardFormats(0);
+		while(nFormat != 0)
 		{
-			int nSize = GlobalSize(cf.m_hgData);
-			if(nSize > 0)
-			{
-				m_Clipboard.Add(cf);
-				bRet = true;
-			}
-			else
+			HGLOBAL hGlobal = ::GetClipboardData(nFormat);
+			LPVOID pvData = GlobalLock(hGlobal);
+			if(pvData)
 			{
-				ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
-				cf.Free();
+				ULONG Size = GlobalSize(hGlobal);
+				if(Size > 0)
+				{
+					LPVOID pvData = GlobalLock(hGlobal);
+					cf.m_hgData = NewGlobalP(pvData, Size);
+					
+					cf.m_cfType = nFormat;
+
+					m_Clipboard.Add(cf);
+					bRet = true;
+				}
+
+				GlobalUnlock(hGlobal);
 			}
-			cf.m_hgData = 0; // m_Formats owns it now
+
+			nFormat = EnumClipboardFormats(nFormat);
 		}
-	}
 
-	oleData.Release();
+		::CloseClipboard();
+	}
 
 	return bRet;
 }
@@ -61,29 +54,35 @@ bool CClipboardSaveRestore::Save()
 bool CClipboardSaveRestore::Restore()
 {
 	bool bRet = false;
-	if(::OpenClipboard(NULL))
+
+	if(::OpenClipboard(theApp.m_MainhWnd))
 	{
 		::EmptyClipboard();
 
+		SetClipboardData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
+
 		int nSize = m_Clipboard.GetSize();
 		for(int nPos = 0; nPos < nSize; nPos++)
 		{
-			CClipFormat cf = m_Clipboard[nPos];
-			if(cf.m_hgData)
+			CClipFormat *pCF = &m_Clipboard.ElementAt(nPos);
+			if(pCF && pCF->m_hgData)
 			{
-				::SetClipboardData(cf.m_cfType, cf.m_hgData);
-				cf.m_hgData = NULL;//clipboard now owns the data
+				::SetClipboardData(pCF->m_cfType, pCF->m_hgData);
+				pCF->m_hgData = NULL;//clipboard now owns the data
 
 				bRet = TRUE;
 			}
 		}
 
-		SetClipboardData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
-
 		::CloseClipboard();
 	}
 
 	m_Clipboard.RemoveAll();
 
+	if(bRet == FALSE)
+	{
+		Log(_T("CClipboardSaveRestore::Restore failed to restore clipboard"));
+	}
+
 	return bRet;
 }

+ 45 - 29
DittoCopyBuffer.cpp

@@ -27,22 +27,29 @@ bool CDittoCopyBuffer::StartCopy(long lCopyBuffer, bool bCut)
 	//Make sure the end copy thread has exited
 	EndRestoreThread();
 
-	m_bActive = true;
-	m_lCurrentDittoBuffer = lCopyBuffer;
-	m_SavedClipboard.Save();
-	if(bCut)
+	if(m_SavedClipboard.Save())
 	{
-		theApp.SendCut();
+		if(bCut)
+		{
+			theApp.SendCut();
+		}
+		else
+		{
+			theApp.SendCopy();
+		}
+
+		//Create a thread to track if they have copied anything, if thread has exited before they have
+		//copied something then the copy buffer copy is canceled
+		AfxBeginThread(CDittoCopyBuffer::StartCopyTimer, (LPVOID)this, THREAD_PRIORITY_LOWEST);
+
+		m_bActive = true;
+		m_lCurrentDittoBuffer = lCopyBuffer;
 	}
 	else
 	{
-		theApp.SendCopy();
+		Log(_T("Start of Ditto Failed to save buffer"));
 	}
 
-	//Create a thread to track if they have copied anything, if thread has exited before they have
-	//copied something then the copy buffer copy is canceled
-	AfxBeginThread(CDittoCopyBuffer::StartCopyTimer, (LPVOID)this, THREAD_PRIORITY_LOWEST);
-
 	return true;
 }
 
@@ -89,14 +96,7 @@ bool CDittoCopyBuffer::EndCopy(long lID)
 	
 	if(PutClipOnDittoCopyBuffer(lID, m_lCurrentDittoBuffer))
 	{
-		Log(StrF(_T("Ditto end copy, saved clip successfully Clip ID = %d"), lID));
-
-		CCopyBufferItem Item;
-		g_Opt.GetCopyBufferItem(m_lCurrentDittoBuffer, Item);
-		if(Item.m_bPlaySoundOnCopy)
-		{
-			PlaySound(_T("ding.wav"), NULL, SND_FILENAME|SND_ASYNC);
-		}
+		Log(StrF(_T("Ditto end copy, saved clip successfully Clip ID = %d"), lID));	
 
 		bRet = true;
 	}
@@ -123,6 +123,13 @@ bool CDittoCopyBuffer::PutClipOnDittoCopyBuffer(long lClipId, long lBuffer)
 
 		theApp.m_db.execDMLEx(_T("UPDATE CopyBuffers SET lClipID = %d WHERE lCopyBuffer = %d"), lClipId, lBuffer);
 
+		CCopyBufferItem Item;
+		g_Opt.GetCopyBufferItem(lBuffer, Item);
+		if(Item.m_bPlaySoundOnCopy)
+		{
+			PlaySound(_T("ding.wav"), NULL, SND_FILENAME|SND_ASYNC);
+		}
+
 		return true;
 	}
 	CATCH_SQLITE_EXCEPTION
@@ -132,14 +139,13 @@ bool CDittoCopyBuffer::PutClipOnDittoCopyBuffer(long lClipId, long lBuffer)
 
 bool CDittoCopyBuffer::PastCopyBuffer(long lCopyBuffer)
 {
-	DWORD dNow = GetTickCount();
-	if(((dNow - m_dwLastPaste) < 500) || (dNow < m_dwLastPaste))
+	//Can't paste while another is still active
+	if(WaitForSingleObject(m_RestoreActive, 1) == WAIT_TIMEOUT)
 	{
 		Log(_T("Copy Buffer pasted to fast"));
-		m_dwLastPaste = GetTickCount();
 		return false;
 	}
-	m_dwLastPaste = GetTickCount();
+
 	bool bRet = false;
 
 	Log(StrF(_T("Start - PastCopyBuffer buffer = %d"), m_lCurrentDittoBuffer));
@@ -155,9 +161,10 @@ bool CDittoCopyBuffer::PastCopyBuffer(long lCopyBuffer)
 			m_pClipboard = new CClipboardSaveRestoreCopyBuffer;
 			if(m_pClipboard)
 			{
-				//make sure the previouse paste is done
-				EndRestoreThread();
-
+				//Save the clipboard, 
+				//then put the new data on the clipboard
+				//then send a paste
+				//then wait a little and restore the original clipboard data
 				if(m_pClipboard->Save())
 				{
 					CProcessPaste paste;
@@ -202,7 +209,9 @@ UINT CDittoCopyBuffer::DelayRestoreClipboard(LPVOID pParam)
 		pBuffer->m_RestoreTimer.ResetEvent();
 		pBuffer->m_RestoreActive.ResetEvent();
 
-		DWORD dRes = WaitForSingleObject(pBuffer->m_RestoreTimer, pBuffer->m_pClipboard->m_lRestoreDelay);
+		CClipboardSaveRestoreCopyBuffer *pLocalClipboard = pBuffer->m_pClipboard;
+
+		DWORD dRes = WaitForSingleObject(pBuffer->m_RestoreTimer, pLocalClipboard->m_lRestoreDelay);
 
 		if(GetKeyState(VK_SHIFT) & 0x8000)
 		{
@@ -210,11 +219,18 @@ UINT CDittoCopyBuffer::DelayRestoreClipboard(LPVOID pParam)
 		}
 		else
 		{
-			pBuffer->m_pClipboard->Restore();
+			if(pLocalClipboard->Restore())
+			{
+				Log(_T("CDittoCopyBuffer::DelayRestoreClipboard Successfully"));
+			}
+			else
+			{
+				Log(_T("CDittoCopyBuffer::DelayRestoreClipboard Failed to restore"));
+			}
 		}
 
-		delete pBuffer->m_pClipboard;
-		pBuffer->m_pClipboard = NULL;
+		delete pLocalClipboard;
+		pLocalClipboard = NULL;
 
 		pBuffer->m_RestoreActive.SetEvent();
 	}

+ 9 - 9
MainFrm.cpp

@@ -310,39 +310,39 @@ LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
 	}
 	else if(wParam == theApp.m_pCopyBuffer1->m_Atom)
 	{
-		theApp.m_CopyBuffer.StartCopy(1);
+		theApp.m_CopyBuffer.StartCopy(0);
 	}
 	else if(wParam == theApp.m_pPasteBuffer1->m_Atom)
 	{
-		theApp.m_CopyBuffer.PastCopyBuffer(1);
+		theApp.m_CopyBuffer.PastCopyBuffer(0);
 	}
 	else if(wParam == theApp.m_pCutBuffer1->m_Atom)
 	{
-		theApp.m_CopyBuffer.StartCopy(1, true);
+		theApp.m_CopyBuffer.StartCopy(0, true);
 	}
 	else if(wParam == theApp.m_pCopyBuffer2->m_Atom)
 	{
-		theApp.m_CopyBuffer.StartCopy(2);
+		theApp.m_CopyBuffer.StartCopy(1);
 	}
 	else if(wParam == theApp.m_pPasteBuffer2->m_Atom)
 	{
-		theApp.m_CopyBuffer.PastCopyBuffer(2);
+		theApp.m_CopyBuffer.PastCopyBuffer(1);
 	}
 	else if(wParam == theApp.m_pCutBuffer2->m_Atom)
 	{
-		theApp.m_CopyBuffer.StartCopy(2, true);
+		theApp.m_CopyBuffer.StartCopy(1, true);
 	}
 	else if(wParam == theApp.m_pCopyBuffer3->m_Atom)
 	{
-		theApp.m_CopyBuffer.StartCopy(3);
+		theApp.m_CopyBuffer.StartCopy(2);
 	}
 	else if(wParam == theApp.m_pPasteBuffer3->m_Atom)
 	{	
-		theApp.m_CopyBuffer.PastCopyBuffer(3);
+		theApp.m_CopyBuffer.PastCopyBuffer(2);
 	}
 	else if(wParam == theApp.m_pCutBuffer3->m_Atom)
 	{
-		theApp.m_CopyBuffer.StartCopy(3, true);
+		theApp.m_CopyBuffer.StartCopy(2, true);
 	}
 
 	return TRUE;

+ 4 - 0
Misc.cpp

@@ -1016,6 +1016,10 @@ BYTE GetKeyStateModifiers()
 		m |= HOTKEYF_CONTROL;
 	if( GetKeyState(VK_MENU) & 0x8000 )
 		m |= HOTKEYF_ALT;
+	if( GetKeyState(VK_LWIN) & 0x8000 )
+		m |= HOTKEYF_EXT;
+	if( GetKeyState(VK_RWIN) & 0x8000 )
+		m |= HOTKEYF_EXT;
 	return m;
 }
 

+ 3 - 3
Options.cpp

@@ -1648,7 +1648,7 @@ void CGetSetOptions::GetCopyBufferItem(int nPos, CCopyBufferItem &Item)
 void CGetSetOptions::SetCopyBufferItem(int nPos, CCopyBufferItem &Item)
 {
 	SetProfileLong(StrF(_T("CopyBufferCopyHotKey_%d"), nPos), Item.m_lCopyHotKey);
-	GetProfileLong(StrF(_T("CopyBufferPasteHotKey_%d"), nPos), Item.m_lPasteHotKey);
-	GetProfileLong(StrF(_T("CopyBufferCutHotKey_%d"), nPos), Item.m_lCutHotKey);
-	GetProfileLong(StrF(_T("CopyBufferPlaySound_%d"), nPos), Item.m_bPlaySoundOnCopy);
+	SetProfileLong(StrF(_T("CopyBufferPasteHotKey_%d"), nPos), Item.m_lPasteHotKey);
+	SetProfileLong(StrF(_T("CopyBufferCutHotKey_%d"), nPos), Item.m_lCutHotKey);
+	SetProfileLong(StrF(_T("CopyBufferPlaySound_%d"), nPos), Item.m_bPlaySoundOnCopy);
 }

+ 68 - 2
QListCtrl.cpp

@@ -7,6 +7,7 @@
 #include "ProcessPaste.h"
 #include "BitmapHelper.h"
 #include "MainTableFunctions.h"
+#include "DittoCopyBuffer.h"
 #include <atlbase.h>
 
 #ifdef _DEBUG
@@ -206,6 +207,21 @@ void CQListCtrl::GetSelectionIndexes(ARRAY &arr)
 	}
 }
 
+bool CQListCtrl::PutSelectedItemOnDittoCopyBuffer(long lBuffer)
+{
+	bool bRet = false;
+	ARRAY arr;
+	GetSelectionItemData(arr);
+	int nCount = arr.GetSize();
+	if(nCount > 0 && arr[0])
+	{
+		CDittoCopyBuffer::PutClipOnDittoCopyBuffer(arr[0], lBuffer);
+		bRet = true;
+	}
+
+	return bRet;
+}
+
 void CQListCtrl::GetSelectionItemData(ARRAY &arr)
 {
 	DWORD dwData;
@@ -788,8 +804,24 @@ BOOL CQListCtrl::PreTranslateMessage(MSG* pMsg)
 {
 	DWORD dID;
 	if(m_Accels.OnMsg(pMsg, dID))
-		if(GetParent()->SendMessage(NM_SELECT_DB_ID, dID, 0) )
-			return TRUE;
+	{
+		switch(dID)
+		{
+		case COPY_BUFFER_HOT_KEY_1_ID:
+			PutSelectedItemOnDittoCopyBuffer(0);
+			break;
+		case COPY_BUFFER_HOT_KEY_2_ID:
+			PutSelectedItemOnDittoCopyBuffer(1);
+			break;
+		case COPY_BUFFER_HOT_KEY_3_ID:
+			PutSelectedItemOnDittoCopyBuffer(2);
+			break;
+		default:
+			GetParent()->SendMessage(NM_SELECT_DB_ID, dID, 0);
+		}
+
+		return TRUE;
+	}
 
 	if(m_pToolTip)
 	{
@@ -1133,7 +1165,41 @@ void CQListCtrl::DestroyAndCreateAccelerator(BOOL bCreate, CppSQLite3DB &db)
 	m_Accels.m_Map.RemoveAll();
 
 	if(bCreate)
+	{
 		CMainTableFunctions::LoadAcceleratorKeys(m_Accels, db);
+
+		LoadDittoCopyBufferHotkeys();
+	}
+}
+
+void CQListCtrl::LoadDittoCopyBufferHotkeys()
+{
+	CCopyBufferItem Item;
+	CAccel a;
+
+	g_Opt.GetCopyBufferItem(0, Item);	
+	if(Item.m_lCopyHotKey > 0)
+	{
+		a.Cmd = COPY_BUFFER_HOT_KEY_1_ID;
+		a.Key = Item.m_lCopyHotKey;
+		m_Accels.AddAccel(a);
+	}
+
+	g_Opt.GetCopyBufferItem(1, Item);
+	if(Item.m_lCopyHotKey > 0)
+	{
+		a.Cmd = COPY_BUFFER_HOT_KEY_2_ID;
+		a.Key = Item.m_lCopyHotKey;
+		m_Accels.AddAccel(a);
+	}
+
+	g_Opt.GetCopyBufferItem(2, Item);
+	if(Item.m_lCopyHotKey > 0)
+	{
+		a.Cmd = COPY_BUFFER_HOT_KEY_3_ID;
+		a.Key = Item.m_lCopyHotKey;
+		m_Accels.AddAccel(a);
+	}
 }
 
 void CQListCtrl::OnKillFocus(CWnd* pNewWnd)

+ 7 - 1
QListCtrl.h

@@ -31,6 +31,10 @@
 #define NM_REFRESH_VISIBLE_ROWS		WM_USER+0x117
 #define NM_ITEM_DELETED				WM_USER+0x118
 
+#define COPY_BUFFER_HOT_KEY_1_ID	-100
+#define COPY_BUFFER_HOT_KEY_2_ID	-101
+#define COPY_BUFFER_HOT_KEY_3_ID	-102
+
 
 //#define NM_LIST_CUT			        WM_USER+0x111
 //#define NM_LIST_COPY		        WM_USER+0x112
@@ -92,7 +96,8 @@ public:
 	BOOL SetCaret(int nRow, BOOL bFocus = TRUE);
 	long GetCaret();
 	// moves the caret to the given index, selects it, and ensures it is visible.
-	BOOL SetListPos( int index );
+	BOOL SetListPos(int index);
+	bool PutSelectedItemOnDittoCopyBuffer(long lBuffer);
 
 	DWORD GetItemData(int nItem);
 	void GetToolTipText(int nItem, CString &csText);
@@ -120,6 +125,7 @@ protected:
 	void SendSelection(ARRAY &arrItems);
 	BOOL GetClipData(int nItem, CClipFormat &Clip);
 	BOOL DrawBitMap(int nItem, CRect &crRect, CDC *pDC);
+	void LoadDittoCopyBufferHotkeys();
 
 	BOOL DrawText(int nItem, CRect &crRect, CDC *pDC);