-
Notifications
You must be signed in to change notification settings - Fork 163
/
CClipboard.cpp
695 lines (628 loc) · 19.5 KB
/
CClipboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
Copyright (C) 2008, kobake
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include <ShellAPI.h>// HDROP
#include "CClipboard.h"
#include "doc/CEditDoc.h"
#include "charset/CCodeMediator.h"
#include "charset/CCodeFactory.h"
#include "charset/CShiftJis.h"
#include "charset/CUtf8.h"
#include "CEol.h"
// MinGW<=4.5.0のコンパイルエラー対策
#ifndef CF_DIBV5
#define CF_DIBV5 17
#endif
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
// コンストラクタ・デストラクタ //
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
CClipboard::CClipboard(HWND hwnd)
{
m_hwnd = hwnd;
m_bOpenResult = ::OpenClipboard(hwnd);
}
CClipboard::~CClipboard()
{
Close();
}
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
// インターフェース //
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
void CClipboard::Empty()
{
::EmptyClipboard();
}
void CClipboard::Close()
{
if(m_bOpenResult){
::CloseClipboard();
m_bOpenResult=FALSE;
}
}
bool CClipboard::SetText(
const wchar_t* pData, //!< コピーするUNICODE文字列
int nDataLen, //!< pDataの長さ(文字単位)
bool bColumnSelect,
bool bLineSelect,
UINT uFormat
)
{
if( !m_bOpenResult ){
return false;
}
/*
// テキスト形式のデータ (CF_OEMTEXT)
HGLOBAL hgClipText = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
nTextLen + 1
);
if( hgClipText ){
char* pszClip = GlobalLockChar( hgClipText );
memcpy( pszClip, pszText, nTextLen );
pszClip[nTextLen] = '\0';
::GlobalUnlock( hgClipText );
::SetClipboardData( CF_OEMTEXT, hgClipText );
}
*/
// UNICODE形式のデータ (CF_UNICODETEXT)
HGLOBAL hgClipText = NULL;
bool bUnicodeText = (uFormat == (UINT)-1 || uFormat == CF_UNICODETEXT);
while(bUnicodeText){
//領域確保
hgClipText = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
(nDataLen + 1) * sizeof(wchar_t)
);
if( !hgClipText )break;
//確保した領域にデータをコピー
wchar_t* pszClip = GlobalLockWChar( hgClipText );
wmemcpy( pszClip, pData, nDataLen ); //データ
pszClip[nDataLen] = L'\0'; //終端ヌル
::GlobalUnlock( hgClipText );
//クリップボードに設定
::SetClipboardData( CF_UNICODETEXT, hgClipText );
bUnicodeText = false;
}
// 1回しか通らない. breakでここまで飛ぶ
// バイナリ形式のデータ
// (int) 「データ」の長さ
// 「データ」
HGLOBAL hgClipSakura = NULL;
//サクラエディタ専用フォーマットを取得
CLIPFORMAT uFormatSakuraClip = CClipboard::GetSakuraFormat();
bool bSakuraText = (uFormat == (UINT)-1 || uFormat == uFormatSakuraClip);
while(bSakuraText){
if( 0 == uFormatSakuraClip )break;
//領域確保
hgClipSakura = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
sizeof(int) + (nDataLen + 1) * sizeof(wchar_t)
);
if( !hgClipSakura )break;
//確保した領域にデータをコピー
BYTE* pClip = GlobalLockBYTE( hgClipSakura );
*((int*)pClip) = nDataLen; pClip += sizeof(int); //データの長さ
wmemcpy( (wchar_t*)pClip, pData, nDataLen ); pClip += nDataLen*sizeof(wchar_t); //データ
*((wchar_t*)pClip) = L'\0'; pClip += sizeof(wchar_t); //終端ヌル
::GlobalUnlock( hgClipSakura );
//クリップボードに設定
::SetClipboardData( uFormatSakuraClip, hgClipSakura );
bSakuraText = false;
}
// 1回しか通らない. breakでここまで飛ぶ
// 矩形選択を示すダミーデータ
HGLOBAL hgClipMSDEVColumn = NULL;
if( bColumnSelect ){
UINT uFormat = ::RegisterClipboardFormat( _T("MSDEVColumnSelect") );
if( 0 != uFormat ){
hgClipMSDEVColumn = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
1
);
if( hgClipMSDEVColumn ){
BYTE* pClip = GlobalLockBYTE( hgClipMSDEVColumn );
pClip[0] = 0;
::GlobalUnlock( hgClipMSDEVColumn );
::SetClipboardData( uFormat, hgClipMSDEVColumn );
}
}
}
/* 行選択を示すダミーデータ */
HGLOBAL hgClipMSDEVLine = NULL; // VS2008 以前の形式
if( bLineSelect ){
UINT uFormat = ::RegisterClipboardFormat( _T("MSDEVLineSelect") );
if( 0 != uFormat ){
hgClipMSDEVLine = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
1
);
if( hgClipMSDEVLine ){
BYTE* pClip = (BYTE*)::GlobalLock( hgClipMSDEVLine );
pClip[0] = 0x01;
::GlobalUnlock( hgClipMSDEVLine );
::SetClipboardData( uFormat, hgClipMSDEVLine );
}
}
}
HGLOBAL hgClipMSDEVLine2 = NULL; // VS2010 形式
if( bLineSelect ){
UINT uFormat = ::RegisterClipboardFormat( _T("VisualStudioEditorOperationsLineCutCopyClipboardTag") );
if( 0 != uFormat ){
hgClipMSDEVLine2 = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
1
);
if( hgClipMSDEVLine2 ){
BYTE* pClip = (BYTE*)::GlobalLock( hgClipMSDEVLine2 );
pClip[0] = 0x01; // ※ ClipSpy で調べるとデータはこれとは違うが内容には無関係に動くっぽい
::GlobalUnlock( hgClipMSDEVLine2 );
::SetClipboardData( uFormat, hgClipMSDEVLine2 );
}
}
}
if( bColumnSelect && !hgClipMSDEVColumn ){
return false;
}
if( bLineSelect && !(hgClipMSDEVLine && hgClipMSDEVLine2) ){
return false;
}
if( !(hgClipText && hgClipSakura) ){
return false;
}
return true;
}
bool CClipboard::SetHtmlText(const CNativeW& cmemBUf)
{
if( !m_bOpenResult ){
return false;
}
CNativeA cmemUtf8;
CUtf8().UnicodeToCode(cmemBUf, cmemUtf8._GetMemory());
CNativeA cmemHeader;
char szFormat[32];
size_t size = cmemUtf8.GetStringLength() + 134;
cmemHeader.AppendString("Version:0.9\r\n");
cmemHeader.AppendString("StartHTML:00000097\r\n");
sprintf( szFormat, "EndHTML:%08Id\r\n", size + 36 );
cmemHeader.AppendString(szFormat);
cmemHeader.AppendString("StartFragment:00000134\r\n");
sprintf( szFormat, "EndFragment:%08Id\r\n", size );
cmemHeader.AppendString(szFormat);
cmemHeader.AppendString("<html><body>\r\n<!--StartFragment -->\r\n");
CNativeA cmemFooter;
cmemFooter.AppendString("\r\n<!--EndFragment-->\r\n</body></html>\r\n");
HGLOBAL hgClipText = NULL;
size_t nLen = cmemHeader.GetStringLength() + cmemUtf8.GetStringLength() + cmemFooter.GetStringLength();
//領域確保
hgClipText = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
nLen + 1
);
if( !hgClipText ) return false;
//確保した領域にデータをコピー
char* pszClip = GlobalLockChar( hgClipText );
memcpy_raw( pszClip, cmemHeader.GetStringPtr(), cmemHeader.GetStringLength() ); //データ
memcpy_raw( pszClip + cmemHeader.GetStringLength(), cmemUtf8.GetStringPtr(), cmemUtf8.GetStringLength() ); //データ
memcpy_raw( pszClip + cmemHeader.GetStringLength() + cmemUtf8.GetStringLength(), cmemFooter.GetStringPtr(), cmemFooter.GetStringLength() ); //データ
pszClip[nLen] = '\0'; //終端ヌル
::GlobalUnlock( hgClipText );
//クリップボードに設定
UINT uFormat = ::RegisterClipboardFormat( _T("HTML Format") );
::SetClipboardData( uFormat, hgClipText );
return true;
}
/*! テキストを取得する
@param [out] cmemBuf 取得したテキストの格納先
@param [in,out] pbColumnSelect 矩形選択形式
@param [in,out] pbLineSelect 行選択形式
@param [in] cEol HDROP形式のときの改行コード
@param [in] uGetFormat クリップボード形式
*/
bool CClipboard::GetText(CNativeW* cmemBuf, bool* pbColumnSelect, bool* pbLineSelect, const CEol& cEol, UINT uGetFormat)
{
if( !m_bOpenResult ){
return false;
}
if( NULL != pbColumnSelect ){
*pbColumnSelect = false;
}
if( NULL != pbLineSelect ){
*pbLineSelect = false;
}
//矩形選択や行選択のデータがあれば取得
if( NULL != pbColumnSelect || NULL != pbLineSelect ){
UINT uFormat = 0;
while( ( uFormat = ::EnumClipboardFormats( uFormat ) ) != 0 ){
// Jul. 2, 2005 genta : check return value of GetClipboardFormatName
TCHAR szFormatName[128];
if( ::GetClipboardFormatName( uFormat, szFormatName, _countof(szFormatName) - 1 ) ){
if( NULL != pbColumnSelect && 0 == lstrcmpi( _T("MSDEVColumnSelect"), szFormatName ) ){
*pbColumnSelect = true;
break;
}
if( NULL != pbLineSelect && 0 == lstrcmpi( _T("MSDEVLineSelect"), szFormatName ) ){
*pbLineSelect = true;
break;
}
if( NULL != pbLineSelect && 0 == lstrcmpi( _T("VisualStudioEditorOperationsLineCutCopyClipboardTag"), szFormatName ) ){
*pbLineSelect = true;
break;
}
}
}
}
//サクラ形式のデータがあれば取得
CLIPFORMAT uFormatSakuraClip = CClipboard::GetSakuraFormat();
if( (uGetFormat == -1 || uGetFormat == uFormatSakuraClip)
&& ::IsClipboardFormatAvailable( uFormatSakuraClip ) ){
HGLOBAL hSakura = ::GetClipboardData( uFormatSakuraClip );
if (hSakura != NULL) {
BYTE* pData = (BYTE*)::GlobalLock(hSakura);
size_t nLength = *((int*)pData);
const wchar_t* szData = (const wchar_t*)(pData + sizeof(int));
cmemBuf->SetString( szData, nLength );
::GlobalUnlock(hSakura);
return true;
}
}
//UNICODE形式のデータがあれば取得
// From Here 2005/05/29 novice UNICODE TEXT 対応処理を追加
HGLOBAL hUnicode = NULL;
if( uGetFormat == -1 || uGetFormat == CF_UNICODETEXT ){
hUnicode = ::GetClipboardData( CF_UNICODETEXT );
}
if( hUnicode != NULL ){
//DWORD nLen = GlobalSize(hUnicode);
wchar_t* szData = GlobalLockWChar(hUnicode);
cmemBuf->SetString( szData );
::GlobalUnlock(hUnicode);
return true;
}
// To Here 2005/05/29 novice
//OEMTEXT形式のデータがあれば取得
HGLOBAL hText = NULL;
if( uGetFormat == -1 || uGetFormat == CF_OEMTEXT ){
hText = ::GetClipboardData( CF_OEMTEXT );
}
if( hText != NULL ){
char* szData = GlobalLockChar(hText);
//SJIS→UNICODE
CMemory cmemSjis( szData, GlobalSize(hText) );
CNativeW cmemUni;
CShiftJis::SJISToUnicode(cmemSjis, &cmemUni);
cmemSjis.Clean();
// '\0'までを取得
cmemUni._SetStringLength(auto_strlen(cmemUni.GetStringPtr()));
cmemUni.swap(*cmemBuf);
::GlobalUnlock(hText);
return true;
}
/* 2008.09.10 bosagami パス貼り付け対応 */
//HDROP形式のデータがあれば取得
if( (uGetFormat == -1 || uGetFormat == CF_HDROP)
&& ::IsClipboardFormatAvailable(CF_HDROP) ){
HDROP hDrop = (HDROP)::GetClipboardData(CF_HDROP);
if(hDrop != NULL){
TCHAR sTmpPath[_MAX_PATH + 1] = {0};
const int nMaxCnt = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
for(int nLoop = 0; nLoop < nMaxCnt; nLoop++){
DragQueryFile(hDrop, nLoop, sTmpPath, _countof(sTmpPath) - 1);
// 2012.10.05 Moca ANSI版に合わせて最終行にも改行コードをつける
cmemBuf->AppendStringT(sTmpPath);
if(nMaxCnt > 1){
cmemBuf->AppendString( cEol.GetValue2() );
}
}
return true;
}
}
return false;
}
struct SSystemClipFormatNames
{
CLIPFORMAT m_nClipFormat;
const wchar_t* m_pszName;
};
static const SSystemClipFormatNames sClipFormatNames[] =
{
{CF_TEXT ,L"CF_TEXT"},
{CF_BITMAP ,L"CF_BITMAP"},
{CF_METAFILEPICT,L"CF_METAFILEPICT"},
{CF_SYLK ,L"CF_SYLK"},
{CF_DIF ,L"CF_DIF"},
{CF_TIFF ,L"CF_TIFF"},
{CF_OEMTEXT ,L"CF_OEMTEXT"},
{CF_DIB ,L"CF_DIB"},
{CF_PALETTE ,L"CF_PALETTE"},
{CF_PENDATA ,L"CF_PENDATA"},
{CF_RIFF ,L"CF_RIFF"},
{CF_WAVE ,L"CF_WAVE"},
{CF_UNICODETEXT ,L"CF_UNICODETEXT"},
{CF_ENHMETAFILE ,L"CF_ENHMETAFILE"},
{CF_HDROP ,L"CF_HDROP"},
{CF_LOCALE ,L"CF_LOCALE"},
{CF_DIBV5 ,L"CF_DIBV5"},
};
static CLIPFORMAT GetClipFormat(const wchar_t* pFormatName)
{
CLIPFORMAT uFormat = (CLIPFORMAT)-1;
if( pFormatName[0] == L'\0' ){
return uFormat;
}
for(int i = 0; i < _countof(sClipFormatNames); i++){
if( 0 == wcsicmp(pFormatName, sClipFormatNames[i].m_pszName) ){
uFormat = sClipFormatNames[i].m_nClipFormat;
}
}
if( uFormat == (CLIPFORMAT)-1 ){
bool bNumber = true;
for( int i =0; pFormatName[i]; i++ ){
if( !WCODE::Is09(pFormatName[i]) ){
bNumber = false;
}
}
if( bNumber ){
uFormat = _wtoi(pFormatName);
}else{
uFormat = ::RegisterClipboardFormat( to_tchar(pFormatName) );
}
}
return uFormat;
}
bool CClipboard::IsIncludeClipboradFormat(const wchar_t* pFormatName)
{
CLIPFORMAT uFormat = GetClipFormat(pFormatName);
if( ::IsClipboardFormatAvailable(uFormat) ){
return true;
}
return false;
}
static int GetEndModeByMode(int nMode, int nEndMode)
{
if( nEndMode == -1 ){
switch(nMode){
case -1: nEndMode = 0; break;
case CODE_AUTODETECT: nEndMode = 0; break;
case CODE_SJIS: nEndMode = 1; break;
case CODE_EUC: nEndMode = 1; break;
case CODE_LATIN1: nEndMode = 1; break;
case CODE_UNICODE: nEndMode = 2; break;
case CODE_UNICODEBE: nEndMode = 2; break;
case CODE_UTF8: nEndMode = 1; break;
case CODE_CESU8: nEndMode = 1; break;
case CODE_UTF7: nEndMode = 1; break;
default: nEndMode = 0; break;
}
}
return nEndMode;
}
bool CClipboard::SetClipboradByFormat(const CStringRef& cstr, const wchar_t* pFormatName, int nMode, int nEndMode)
{
CLIPFORMAT uFormat = GetClipFormat(pFormatName);
if( uFormat == (CLIPFORMAT)-1 ){
return false;
}
if( nMode == -2 ){
if( uFormat == CF_UNICODETEXT || uFormat == GetSakuraFormat() ){
return SetText(cstr.GetPtr(), cstr.GetLength(), false, false, uFormat);
}
return false;
}
CMemory cmemBuf;
char* pBuf = NULL;
size_t nTextByteLen = 0;
if( nMode == -1 ){
// バイナリモード U+00 - U+ffを0x00 - 0xffにマッピング
cmemBuf.AllocBuffer(cstr.GetLength());
cmemBuf._SetRawLength(cstr.GetLength());
pBuf = (char*)cmemBuf.GetRawPtr();
size_t len = cstr.GetLength();
const wchar_t* pMem = cstr.GetPtr();
for(size_t i = 0; i < len; i++){
pBuf[i] = (unsigned char)pMem[i];
if( 0xff < pMem[i] ){
return false;
}
}
nTextByteLen = len;
}else{
ECodeType eMode = (ECodeType)nMode;
if( !IsValidCodeType(eMode) ){
return false;
}
if( eMode == CODE_UNICODE ){
pBuf = (char*)cstr.GetPtr();
nTextByteLen = cstr.GetLength() * sizeof(wchar_t);
}else{
CCodeBase* pCode = CCodeFactory::CreateCodeBase(eMode, GetDllShareData().m_Common.m_sFile.GetAutoMIMEdecode());
if( RESULT_FAILURE == pCode->UnicodeToCode(cstr, &cmemBuf) ){
return false;
}
delete pCode;
pBuf = (char*)cmemBuf.GetRawPtr();
nTextByteLen = cmemBuf.GetRawLength();
}
}
nEndMode = GetEndModeByMode(nMode, nEndMode);
size_t nulLen = 0;
switch( nEndMode ){
case 1: nulLen = 1; break;
case 2: nulLen = 2; break;
case 4: nulLen = 4; break;
case 0: nulLen = 0; break;
default: nulLen = 0; break;
}
HGLOBAL hgClipText = ::GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
nTextByteLen + nulLen
);
if( !hgClipText ){
return false;
}
char* pszClip = GlobalLockChar( hgClipText );
memcpy( pszClip, pBuf, nTextByteLen );
if( nulLen ){
memset( &pszClip[nTextByteLen], 0, nulLen );
}
::GlobalUnlock( hgClipText );
::SetClipboardData( uFormat, hgClipText );
return true;
}
static int GetLengthByMode(HGLOBAL hClipData, const BYTE* pData, int nMode, int nEndMode)
{
size_t nMemLength = ::GlobalSize(hClipData);
nEndMode = GetEndModeByMode(nMode, nEndMode);
size_t nLength;
if( nEndMode == 1 ) {
nLength = strnlen((const char *)pData, nMemLength);
}else if( nEndMode == 2 ){
nLength = wcsnlen((const wchar_t *)pData, nMemLength / 2) * 2;
}else if( nEndMode == 4 ){
const wchar32_t* pData32 = (const wchar32_t*)pData;
const size_t len = nMemLength / 4;
nLength = 0;
while( pData32[nLength] != 0 && nLength < len ){
nLength++;
}
nLength *= 4;
}else{
nLength = nMemLength;
}
return nLength;
}
/*!
指定のクリップボード形式で取得
@param nMode -2:通常のサクラの処理, -1:バイナリモード, それ以外:文字コード
@param nEndMode -1:文字コードに依存 0:GlobalSize 1:strlen 2:wcslen 4:wchar32_tの文字列
@date 2013.06.12 Moca 新規作成
*/
bool CClipboard::GetClipboradByFormat(CNativeW& mem, const wchar_t* pFormatName, int nMode, int nEndMode, const CEol& cEol)
{
mem.SetString(L"");
CLIPFORMAT uFormat = GetClipFormat(pFormatName);
if( uFormat == (CLIPFORMAT)-1 ){
return false;
}
if( !::IsClipboardFormatAvailable(uFormat) ){
return false;
}
if( nMode == -2 ){
bool bret = false;
if( -1 != GetDataType() ){
bret = GetText(&mem, NULL, NULL, cEol, uFormat);
if( !bret ){
mem.SetString(L"");
}
}
return bret;
}
HGLOBAL hClipData = ::GetClipboardData( uFormat );
if( hClipData != NULL ){
bool retVal = true;
const BYTE* pData = (BYTE*)::GlobalLock( hClipData );
if( pData == NULL ){
return false;
}
// 長さオプションの解釈
size_t nLength = GetLengthByMode(hClipData, pData, nMode, nEndMode);
// エンコードオプション
if( nMode == -1 ){
// バイナリモード。1byteをU+00-U+ffにマッピング
mem.AllocStringBuffer(nLength);
mem._SetStringLength(nLength);
wchar_t* pBuf = (wchar_t *)mem.GetStringPtr();
for( size_t i = 0; i < nLength; i++ ){
pBuf[i] = (unsigned char)pData[i];
}
}else{
ECodeType eMode = (ECodeType)nMode;
if( !IsValidCodeType(eMode) ){
// コード不明と99は自動判別
ECodeType nBomCode = CCodeMediator::DetectUnicodeBom((const char*)pData, nLength);
if( nBomCode != CODE_NONE ){
eMode = nBomCode;
}else{
const STypeConfig& type = CEditDoc::GetInstance(0)->m_cDocType.GetDocumentAttribute();
CCodeMediator mediator(type.m_encoding);
eMode = mediator.CheckKanjiCode((const char*)pData, nLength);
}
if( !IsValidCodeType(eMode) ){
eMode = CODE_DEFAULT;
}
if( -1 == nEndMode ){
// nLength 再設定
nLength = GetLengthByMode(hClipData, pData, eMode, nEndMode);
}
}
if( eMode == CODE_UNICODE ){
mem.SetString((wchar_t *)pData, nLength / sizeof(wchar_t));
}else{
CMemory cmem;
cmem.SetRawData(pData, nLength);
if( NULL != cmem.GetRawPtr() ){
CCodeBase* pCode = CCodeFactory::CreateCodeBase(eMode, GetDllShareData().m_Common.m_sFile.GetAutoMIMEdecode());
if( RESULT_FAILURE == pCode->CodeToUnicode(cmem, &mem) ){
mem.SetString(L"");
retVal = false;
}
delete pCode;
}
}
}
::GlobalUnlock(hClipData);
return retVal;
}
return false;
}
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
// staticインターフェース //
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
//! クリップボード内に、サクラエディタで扱えるデータがあればtrue
bool CClipboard::HasValidData()
{
//扱える形式が1つでもあればtrue
if(::IsClipboardFormatAvailable(CF_OEMTEXT))return true;
if(::IsClipboardFormatAvailable(CF_UNICODETEXT))return true;
if(::IsClipboardFormatAvailable(GetSakuraFormat()))return true;
/* 2008.09.10 bosagami パス貼り付け対応 */
if(::IsClipboardFormatAvailable(CF_HDROP))return true;
return false;
}
//!< サクラエディタ独自のクリップボードデータ形式
CLIPFORMAT CClipboard::GetSakuraFormat()
{
/*
2007.09.30 kobake
UNICODE形式でクリップボードデータを保持するよう変更したため、
以前のバージョンのクリップボードデータと競合しないように
フォーマット名を変更
*/
return (CLIPFORMAT)::RegisterClipboardFormat( _T("SAKURAClipW") );
}
//!< クリップボードデータ形式(CF_UNICODETEXT等)の取得
int CClipboard::GetDataType()
{
//扱える形式が1つでもあればtrue
// 2013.06.11 GetTextの取得順に変更
if(::IsClipboardFormatAvailable(GetSakuraFormat()))return GetSakuraFormat();
if(::IsClipboardFormatAvailable(CF_UNICODETEXT))return CF_UNICODETEXT;
if(::IsClipboardFormatAvailable(CF_OEMTEXT))return CF_OEMTEXT;
if(::IsClipboardFormatAvailable(CF_HDROP))return CF_HDROP;
return -1;
}