source: trunk/Lucide/SOURCE/gui/clipbrd.cpp @ 156

Last change on this file since 156 was 156, checked in by Eugene Romanenko, 15 years ago

optimized unicode conversion, fixes crash in uconv.dll (closes #97)

File size: 5.0 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35#define INCL_WIN
36#define INCL_DOS
37#include <os2.h>
38
39#include <uconv.h>
40#include <unidef.h>
41
42#include <string.h>
43#include <malloc.h>
44#include <stdio.h>
45#include "cpconv.h"
46
47
48#define kUnicodeMime "text/unicode"
49
50inline ULONG RegisterClipboardFormat(PCSZ pcszFormat)
51{
52    ATOM atom = WinFindAtom(WinQuerySystemAtomTable(), pcszFormat);
53    if (!atom) {
54        atom = WinAddAtom(WinQuerySystemAtomTable(), pcszFormat);
55    }
56    return atom;
57}
58
59void initClipbrd()
60{
61    RegisterClipboardFormat( kUnicodeMime );
62}
63
64
65void textToClipbrd( HAB hab, const char *text )
66{
67    void *objtouni, *objtosys;
68    int rc = ULS_SUCCESS;
69    rc = UniCreateUconvObject( (UniChar *)L"UTF-8", &objtouni );
70    if ( rc != ULS_SUCCESS ) {
71        return;
72    }
73    rc = UniCreateUconvObject( (UniChar *)L"", &objtosys );
74    if ( rc != ULS_SUCCESS ) {
75        UniFreeUconvObject( objtouni );
76        return;
77    }
78
79    uconv_attribute_t attr;
80    UniQueryUconvObject( objtouni, &attr, sizeof(attr), NULL, NULL, NULL );
81    attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
82    UniSetUconvObject( objtouni, &attr );
83    UniQueryUconvObject( objtosys, &attr, sizeof(attr), NULL, NULL, NULL );
84    attr.options = UCONV_OPTION_SUBSTITUTE_BOTH;
85    UniSetUconvObject( objtosys, &attr );
86
87    size_t len = 0;
88    size_t olen = 0;
89
90    if ( WinOpenClipbrd( hab ) )
91    {
92        WinEmptyClipbrd( hab );
93
94        size_t cSubs = 0;
95        len = strlen( text );
96        olen = ( len + 2 ) * 2;
97        void *shmemuni = NULL;
98        void *shmemsys = NULL;
99
100        // place to clipboard as unicode
101        if ( DosAllocSharedMem( &shmemuni, NULL, olen, fALLOCSHR ) == 0 )
102        {
103            memset( shmemuni, 0, olen );
104            size_t unilen = olen / sizeof( UniChar );
105            UniChar *tmpuni = (UniChar *)shmemuni;
106            UniUconvToUcs( objtouni, (void **)&text, &len, &tmpuni, &unilen, &cSubs );
107            unilen = UniStrlen( (UniChar *)shmemuni );
108            void *memuni = (void *)new char[ olen ];
109            memcpy( memuni, shmemuni, olen );
110
111            ULONG ulFormatID = RegisterClipboardFormat( kUnicodeMime );
112            WinSetClipbrdData( hab, (ULONG)shmemuni, ulFormatID, CFI_POINTER );
113
114            int liglen = uniLigaturesLength( (UniChar *)memuni );
115            if ( liglen > 0 )  // string contain ligature(s)
116            {
117                unsigned ulen_tmp = ( unilen + liglen + 1 ) * sizeof( UniChar );
118                char *uni_tmp = new char[ ulen_tmp ];
119                uniReplaceLigatures( (UniChar *)memuni, (UniChar *)uni_tmp );
120                delete memuni;
121                memuni = uni_tmp;
122                unilen = UniStrlen( (UniChar *)memuni );
123            }
124            uniConvertSpChars( (UniChar *)memuni );
125
126            // place to clipboard as current codepage
127            if ( DosAllocSharedMem( &shmemsys, NULL, olen, fALLOCSHR ) == 0 )
128            {
129                memset( shmemsys, 0, olen );
130
131                cSubs = 0;
132                tmpuni = (UniChar *)memuni;
133                void *tmpsys = shmemsys;
134                UniUconvFromUcs( objtosys, &tmpuni, &unilen, &tmpsys, &olen, &cSubs );
135
136                WinSetClipbrdData( hab, (ULONG)shmemsys, CF_TEXT, CFI_POINTER );
137            }
138            delete memuni;
139        }
140
141        WinCloseClipbrd( hab );
142    }
143
144    UniFreeUconvObject( objtouni );
145    UniFreeUconvObject( objtosys );
146}
147
Note: See TracBrowser for help on using the repository browser.