Commit 9935bf7b authored by mathieu _alkama_ m's avatar mathieu _alkama_ m Committed by Gargaj
Browse files

Make the clipboard on windows great again (#124)

Fixes crashes on clipboard handling.
Also dont paste when there's nothing to paste.
parent edcc7916
...@@ -193,6 +193,8 @@ void ShaderEditor::Copy() ...@@ -193,6 +193,8 @@ void ShaderEditor::Copy()
void ShaderEditor::Paste() void ShaderEditor::Paste()
{ {
int n = Clipboard::GetContentsLength(); int n = Clipboard::GetContentsLength();
if (n == 0) return;
char * p = new char[n + 1]; char * p = new char[n + 1];
memset(p,0,n+1); memset(p,0,n+1);
Clipboard::GetContents( p, n ); Clipboard::GetContents( p, n );
......
...@@ -8,16 +8,27 @@ namespace Clipboard ...@@ -8,16 +8,27 @@ namespace Clipboard
if ( !::OpenClipboard( hWnd ) ) if ( !::OpenClipboard( hWnd ) )
return; return;
EmptyClipboard(); HGLOBAL hData = GlobalAlloc( GMEM_MOVEABLE, ( len + 1 ) * sizeof( char ) );
if ( !hData ) {
CloseClipboard();
return;
}
HGLOBAL h = GlobalAlloc( GMEM_MOVEABLE, ( len + 1 ) * sizeof( char ) ); WCHAR * pMem = (WCHAR*) GlobalLock( hData );
WCHAR * pMem = (WCHAR*) GlobalLock( h ); if ( !pMem ) {
GlobalFree( hData );
CloseClipboard();
return;
}
ZeroMemory( pMem, ( len + 1 ) * sizeof( char ) ); ZeroMemory( pMem, ( len + 1 ) * sizeof( char ) );
CopyMemory( pMem, data, ( len + 1 ) * sizeof( char ) ); CopyMemory( pMem, data, ( len + 1 ) * sizeof( char ) );
GlobalUnlock( h ); GlobalUnlock( hData );
SetClipboardData( CF_TEXT, h );
EmptyClipboard();
SetClipboardData( CF_TEXT, hData );
CloseClipboard(); CloseClipboard();
} }
...@@ -29,9 +40,21 @@ namespace Clipboard ...@@ -29,9 +40,21 @@ namespace Clipboard
return 0; return 0;
HANDLE hData = GetClipboardData( CF_TEXT ); HANDLE hData = GetClipboardData( CF_TEXT );
if ( !hData ) {
CloseClipboard();
return 0;
}
const char * buffer = (const char*) GlobalLock( hData ); const char * buffer = (const char*) GlobalLock( hData );
if ( !buffer ) {
CloseClipboard();
return 0;
}
int n = strlen( buffer ); int n = strlen( buffer );
GlobalUnlock( hData );
CloseClipboard(); CloseClipboard();
return n; return n;
...@@ -44,9 +67,21 @@ namespace Clipboard ...@@ -44,9 +67,21 @@ namespace Clipboard
return; return;
HANDLE hData = GetClipboardData( CF_TEXT ); HANDLE hData = GetClipboardData( CF_TEXT );
if ( !hData ) {
CloseClipboard();
return;
}
const char * buffer = (const char*) GlobalLock( hData ); const char * buffer = (const char*) GlobalLock( hData );
if ( !buffer ) {
CloseClipboard();
return;
}
strncpy( data, buffer, len ); strncpy( data, buffer, len );
GlobalUnlock( hData );
CloseClipboard(); CloseClipboard();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment