source: branches/kmk/Lucide/gui/clipbrd.cpp @ 334

Last change on this file since 334 was 334, checked in by dmik, 11 years ago

branches/kmk: Renamed private os2.h to os2all.h to avoid the clash with the toolkit one.

File size: 6.2 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#include "os2all.h"
36
37#include <uconv.h>
38#include <unidef.h>
39
40#include <string.h>
41#include <malloc.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include "cpconv.h"
45
46#include "UClip.h"
47
48
49static BOOL APIENTRY (*pUWinOpenClipbrd)(HAB)                      = NULL;
50static BOOL APIENTRY (*pUWinEmptyClipbrd)(HAB)                     = NULL;
51static BOOL APIENTRY (*pUWinSetClipbrdData)(HAB,ULONG,ULONG,ULONG) = NULL;
52static BOOL APIENTRY (*pUWinCloseClipbrd)(HAB)                     = NULL;
53
54static bool loadUClip();
55
56static HMODULE ucHandle = NULLHANDLE;
57static bool uclipLoaded = loadUClip();
58
59
60static void freeUClip()
61{
62    if ( ucHandle != NULLHANDLE ) {
63        DosFreeModule( ucHandle );
64    }
65}
66
67static bool loadUClip()
68{
69    bool res = false;
70    do
71    {
72      if ( DosLoadModule( NULL, 0, "UCLIP", &ucHandle ) != 0 )
73        break;
74      if (DosQueryProcAddr(ucHandle,0,"UWinOpenClipbrd",(PFN *)&pUWinOpenClipbrd)!= 0)
75        break;
76      if (DosQueryProcAddr(ucHandle,0,"UWinEmptyClipbrd",(PFN *)&pUWinEmptyClipbrd)!= 0)
77        break;
78      if (DosQueryProcAddr(ucHandle,0,"UWinSetClipbrdData",(PFN *)&pUWinSetClipbrdData)!= 0)
79        break;
80      if (DosQueryProcAddr(ucHandle,0,"UWinCloseClipbrd",(PFN *)&pUWinCloseClipbrd)!= 0)
81        break;
82
83      res = true;
84    } while (0);
85
86    atexit( freeUClip );
87
88    return res;
89}
90
91
92void textToClipbrd( HAB hab, const char *text )
93{
94    void *objtouni, *objtosys;
95    int rc = ULS_SUCCESS;
96    rc = UniCreateUconvObject( (UniChar *)L"UTF-8", &objtouni );
97    if ( rc != ULS_SUCCESS ) {
98        return;
99    }
100    rc = UniCreateUconvObject( (UniChar *)L"", &objtosys );
101    if ( rc != ULS_SUCCESS ) {
102        UniFreeUconvObject( objtouni );
103        return;
104    }
105
106    uconv_attribute_t attr;
107    UniQueryUconvObject( objtouni, &attr, sizeof(attr), NULL, NULL, NULL );
108    attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
109    UniSetUconvObject( objtouni, &attr );
110    UniQueryUconvObject( objtosys, &attr, sizeof(attr), NULL, NULL, NULL );
111    attr.options = UCONV_OPTION_SUBSTITUTE_BOTH;
112    UniSetUconvObject( objtosys, &attr );
113
114    size_t len = 0;
115    size_t olen = 0;
116
117    if ( uclipLoaded ? pUWinOpenClipbrd( hab ) : WinOpenClipbrd( hab ) )
118    {
119        if ( uclipLoaded ) {
120            pUWinEmptyClipbrd( hab );
121        } else {
122            WinEmptyClipbrd( hab );
123        }
124
125        size_t cSubs = 0;
126        len = strlen( text );
127        olen = ( len + 2 ) * 2;
128        void *shmemuni = NULL;
129        void *shmemsys = NULL;
130
131        // place to clipboard as unicode for Odin
132        if ( DosAllocSharedMem( &shmemuni, NULL, olen, fALLOCSHR ) == 0 )
133        {
134            memset( shmemuni, 0, olen );
135            size_t unilen = olen / sizeof( UniChar );
136            UniChar *tmpuni = (UniChar *)shmemuni;
137            UniUconvToUcs( objtouni, (void **)&text, &len, &tmpuni, &unilen, &cSubs );
138            unilen = UniStrlen( (UniChar *)shmemuni );
139            char *memuni = new char[ olen ];
140            memcpy( memuni, shmemuni, olen );
141
142            if ( uclipLoaded ) {
143                pUWinSetClipbrdData( hab,(ULONG)shmemuni,UCLIP_CF_UNICODETEXT,CFI_POINTER );
144            } else {
145                DosFreeMem( shmemuni );
146            }
147
148            int liglen = uniLigaturesLength( (UniChar *)memuni );
149            if ( liglen > 0 )  // string contain ligature(s)
150            {
151                unsigned ulen_tmp = ( unilen + liglen + 1 ) * sizeof( UniChar );
152                char *uni_tmp = new char[ ulen_tmp ];
153                uniReplaceLigatures( (UniChar *)memuni, (UniChar *)uni_tmp );
154                delete memuni;
155                memuni = uni_tmp;
156                unilen = UniStrlen( (UniChar *)memuni );
157            }
158            uniConvertSpChars( (UniChar *)memuni );
159
160            // place to clipboard as current codepage
161            if ( DosAllocSharedMem( &shmemsys, NULL, olen, fALLOCSHR ) == 0 )
162            {
163                memset( shmemsys, 0, olen );
164
165                cSubs = 0;
166                tmpuni = (UniChar *)memuni;
167                void *tmpsys = shmemsys;
168                UniUconvFromUcs( objtosys, &tmpuni, &unilen, &tmpsys, &olen, &cSubs );
169
170                if ( uclipLoaded ) {
171                    pUWinSetClipbrdData( hab, (ULONG)shmemsys, UCLIP_CF_TEXT, CFI_POINTER );
172                } else {
173                    WinSetClipbrdData( hab, (ULONG)shmemsys, CF_TEXT, CFI_POINTER );
174                }
175            }
176            delete memuni;
177        }
178
179        if ( uclipLoaded ) {
180            pUWinCloseClipbrd( hab );
181        } else {
182            WinCloseClipbrd( hab );
183        }
184    }
185
186    UniFreeUconvObject( objtouni );
187    UniFreeUconvObject( objtosys );
188}
189
Note: See TracBrowser for help on using the repository browser.