Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

clipboard.cpp

Go to the documentation of this file.
00001 /* $Id: clipboard.cpp,v 1.14 2002/01/07 14:32:21 sandervl Exp $ */
00002 
00003 /*
00004  * Win32 Clipboard API functions for OS/2
00005  *
00006  * Copyright 1998 Sander van Leeuwen
00007  * Copyright 1998 Patrick Haller
00008  * Copyright 1998 Peter Fitzsimmons
00009  *
00010  *
00011  * NOTE: Use OS/2 frame handles for Open32 functions
00012  *
00013  * Project Odin Software License can be found in LICENSE.TXT
00014  *
00015  */
00016 
00017 #include <os2win.h>
00018 #include <string.h>
00019 #include "win32wbase.h"
00020 #include <unicode.h>
00021 
00022 #define DBG_LOCALLOG    DBG_clipboard
00023 #include "dbglocal.h"
00024 
00025 #ifdef USING_OPEN32CLIPBOARD
00026 
00027 #define FLAG_TO_OPEN32       0
00028 #define FLAG_FROM_OPEN32     1
00029 
00030 //******************************************************************************
00031 //******************************************************************************
00032 HGLOBAL GlobalCopy(HGLOBAL hDest, HGLOBAL hSource, BOOL fToOpen32)
00033 {
00034  LPVOID src;
00035  LPVOID dest;
00036  ULONG  size;
00037 
00038       if(fToOpen32 == FLAG_TO_OPEN32) {
00039           src = GlobalLock(hSource);
00040           if(src) {
00041               size = GlobalSize(hSource);
00042               if(hDest == NULL) {
00043                   hDest = O32_GlobalAlloc(GHND, size);
00044               }
00045               dest  = O32_GlobalLock(hDest);
00046               memcpy(dest, src, size);
00047               O32_GlobalUnlock(hDest);
00048           }
00049           GlobalUnlock(hSource);
00050       }
00051       else {
00052           src = O32_GlobalLock(hSource);
00053           if(src) {
00054               size = O32_GlobalSize(hSource);
00055               if(hDest == NULL) {
00056                   hDest = GlobalAlloc(GHND, size);
00057               }
00058               dest  = GlobalLock(hDest);
00059               memcpy(dest, src, size);
00060               GlobalUnlock(hDest);
00061           }
00062           O32_GlobalUnlock(hSource);
00063       }
00064       return hDest;
00065 }
00066 #endif
00067 
00068 //******************************************************************************
00069 //******************************************************************************
00070 BOOL WIN32API ChangeClipboardChain( HWND hwndRemove, HWND hwndNext)
00071 {
00072   Win32BaseWindow *wndRemove, *wndNext;
00073   HWND hwndOS2Remove, hwndOS2Next;
00074 
00075     wndRemove = Win32BaseWindow::GetWindowFromHandle(hwndRemove);
00076     if(!wndRemove) {
00077         dprintf(("ChangeClipboardChain, window %x not found", hwndRemove));
00078         SetLastError(ERROR_INVALID_WINDOW_HANDLE);
00079         return 0;
00080     }
00081     hwndOS2Remove = wndRemove->getOS2WindowHandle();
00082     RELEASE_WNDOBJ(wndRemove);
00083 
00084     wndNext = Win32BaseWindow::GetWindowFromHandle(hwndNext);
00085     if(!wndNext) {
00086         dprintf(("ChangeClipboardChain, window %x not found", hwndNext));
00087         SetLastError(ERROR_INVALID_WINDOW_HANDLE);
00088         return 0;
00089     }
00090     hwndOS2Next = wndNext->getOS2WindowHandle();
00091     RELEASE_WNDOBJ(wndNext);
00092 
00093     dprintf(("USER32:  ChangeClipboardChain\n"));
00094     return O32_ChangeClipboardChain(hwndOS2Remove, hwndOS2Next);
00095 }
00096 //******************************************************************************
00097 //******************************************************************************
00098 BOOL WIN32API CloseClipboard(void)
00099 {
00100     dprintf(("USER32:  CloseClipboard\n"));
00101     return O32_CloseClipboard();
00102 }
00103 //******************************************************************************
00104 //******************************************************************************
00105 int WIN32API CountClipboardFormats(void)
00106 {
00107     dprintf(("USER32:  CountClipboardFormats\n"));
00108     return O32_CountClipboardFormats();
00109 }
00110 //******************************************************************************
00111 //******************************************************************************
00112 BOOL WIN32API EmptyClipboard(void)
00113 {
00114     dprintf(("USER32:  EmptyClipboard\n"));
00115     return O32_EmptyClipboard();
00116 }
00117 //******************************************************************************
00118 //******************************************************************************
00119 UINT WIN32API EnumClipboardFormats(UINT arg1)
00120 {
00121     dprintf(("USER32:  EnumClipboardFormats\n"));
00122     return O32_EnumClipboardFormats(arg1);
00123 }
00124 //******************************************************************************
00125 //******************************************************************************
00126 HANDLE WIN32API GetClipboardData( UINT uFormat)
00127 {
00128 #ifdef USING_OPEN32CLIPBOARD
00129     HANDLE hClipObj = 0, hClipObjOpen32 = 0;
00130 
00131     dprintf(("USER32: GetClipboardData %x", uFormat));
00132     hClipObjOpen32 = O32_GetClipboardData(uFormat);
00133     //memory leak
00134     if(hClipObjOpen32) {
00135         hClipObj = GlobalCopy(NULL, hClipObjOpen32, FLAG_FROM_OPEN32);
00136     }
00137     return hClipObj;
00138 #else
00139     dprintf(("USER32: GetClipboardData %x", uFormat));
00140     return O32_GetClipboardData(uFormat);
00141 #endif
00142 }
00143 //******************************************************************************
00144 //******************************************************************************
00145 int WIN32API GetClipboardFormatNameA(UINT format,LPSTR lpszFormatName,int cchMaxCount)
00146 {
00147   dprintf(("USER32:  GetClipboardFormatNameA %d\n",format));
00148   return O32_GetClipboardFormatName(format,lpszFormatName,cchMaxCount);
00149 }
00150 //******************************************************************************
00151 //******************************************************************************
00152 int WIN32API GetClipboardFormatNameW(UINT format,LPWSTR lpszFormatName,int cchMaxCount)
00153 {
00154   int   rc;
00155   char *astring = (CHAR*)malloc(cchMaxCount);
00156 
00157   dprintf(("USER32:  GetClipboardFormatNameW %d\n",format));
00158   rc = O32_GetClipboardFormatName(format,astring,cchMaxCount);
00159   if (rc) AsciiToUnicode(astring,lpszFormatName);
00160   free(astring);
00161   return(rc);
00162 }
00163 //******************************************************************************
00164 //******************************************************************************
00165 HWND WIN32API GetClipboardOwner(void)
00166 {
00167  HWND hwndOwner;
00168  Win32BaseWindow *window;
00169 
00170     dprintf(("USER32:  GetClipboardOwner\n"));
00171     hwndOwner = O32_GetClipboardOwner();
00172 
00173     window = Win32BaseWindow::GetWindowFromOS2Handle(hwndOwner);
00174     if(!window) {
00175         //an OS/2 window probably owns the clipboard, we pretend nobody owns it
00176         return NULL;
00177     }
00178     hwndOwner = window->getWindowHandle();
00179     RELEASE_WNDOBJ(window);
00180     return hwndOwner;
00181 }
00182 //******************************************************************************
00183 //******************************************************************************
00184 HWND WIN32API GetClipboardViewer(void)
00185 {
00186  Win32BaseWindow *window;
00187  HWND hwndViewer;
00188 
00189     dprintf(("USER32:  GetClipboardViewer\n"));
00190     hwndViewer = O32_GetClipboardViewer();
00191 
00192     window = Win32BaseWindow::GetWindowFromOS2Handle(hwndViewer);
00193     if(!window) {
00194         //probably an OS/2 window, we pretend it's nobody
00195         return NULL;
00196     }
00197     hwndViewer = window->getWindowHandle();
00198     RELEASE_WNDOBJ(window);
00199     return hwndViewer;
00200 }
00201 //******************************************************************************
00202 //******************************************************************************
00203 HWND WIN32API GetOpenClipboardWindow(void)
00204 {
00205  Win32BaseWindow *window;
00206  HWND hwnd;
00207 
00208     dprintf(("USER32:  GetOpenClipboardWindow\n"));
00209     hwnd = O32_GetOpenClipboardWindow();
00210 
00211     window = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
00212     if(!window) {
00213         //probably an OS/2 window, we pretend it's nobody
00214         return NULL;
00215     }
00216     hwnd = window->getWindowHandle();
00217     RELEASE_WNDOBJ(window);
00218     return hwnd;
00219 }
00220 //******************************************************************************
00221 //******************************************************************************
00222 int WIN32API GetPriorityClipboardFormat( PUINT arg1, int arg2)
00223 {
00224     dprintf(("USER32:  GetPriorityClipboardFormat\n"));
00225     return O32_GetPriorityClipboardFormat(arg1, arg2);
00226 }
00227 //******************************************************************************
00228 //******************************************************************************
00229 BOOL WIN32API IsClipboardFormatAvailable( UINT arg1)
00230 {
00231     dprintf(("USER32: IsClipboardFormatAvailable %x", arg1));
00232     return O32_IsClipboardFormatAvailable(arg1);
00233 }
00234 //******************************************************************************
00235 //******************************************************************************
00236 BOOL WIN32API OpenClipboard( HWND hwnd)
00237 {
00238   Win32BaseWindow *window;
00239 
00240     if (hwnd) {
00241         window = Win32BaseWindow::GetWindowFromHandle(hwnd);
00242         if(!window) {
00243             dprintf(("OpenClipboard, window %x not found", hwnd));
00244             SetLastError(ERROR_INVALID_WINDOW_HANDLE);
00245             return 0;
00246         }
00247         hwnd = window->getOS2WindowHandle();
00248         RELEASE_WNDOBJ(window);
00249     }
00250     dprintf(("USER32: OpenClipboard %x", hwnd));
00251     return O32_OpenClipboard(hwnd);
00252 }
00253 //******************************************************************************
00254 //******************************************************************************
00255 UINT WIN32API RegisterClipboardFormatA( LPCSTR arg1)
00256 {
00257     dprintf(("USER32:  RegisterClipboardFormatA %s", arg1));
00258     return O32_RegisterClipboardFormat(arg1);
00259 }
00260 //******************************************************************************
00261 //******************************************************************************
00262 UINT WIN32API RegisterClipboardFormatW(LPCWSTR arg1)
00263 {
00264  UINT  rc;
00265  char *astring = UnicodeToAsciiString((LPWSTR)arg1);
00266 
00267     dprintf(("USER32:  RegisterClipboardFormatW %s\n", astring));
00268     rc = O32_RegisterClipboardFormat(astring);
00269     FreeAsciiString(astring);
00270     dprintf(("USER32:  RegisterClipboardFormatW returned %d\n", rc));
00271     return(rc);
00272 }
00273 //******************************************************************************
00274 //******************************************************************************
00275 HANDLE WIN32API SetClipboardData( UINT uFormat, HANDLE hClipObj)
00276 {
00277 #ifdef USING_OPEN32CLIPBOARD
00278     HANDLE hClipObjOpen32 = 0;
00279 
00280     dprintf(("USER32: SetClipboardData %x %x", uFormat, hClipObj));
00281     //memory leak
00282     if(hClipObj) {
00283         hClipObjOpen32 = GlobalCopy(NULL, hClipObj, FLAG_TO_OPEN32);
00284     }
00285     O32_SetClipboardData(uFormat, hClipObjOpen32);
00286 
00287     return hClipObj;
00288 #else
00289     dprintf(("USER32: SetClipboardData %x %x", uFormat, hClipObj));
00290 
00291     if(uFormat == CF_UNICODETEXT) 
00292     {
00293         //have to translate to CF_TEXT, because WGSS doesn't understand
00294         //unicode text
00295         LPWSTR lpTextUnicode;
00296         LPSTR  lpTextAscii;
00297         DWORD  length;
00298         HANDLE hClipObjAscii;
00299 
00300         lpTextUnicode = (LPWSTR)GlobalLock(hClipObj);
00301         if(lpTextUnicode == NULL) {
00302             dprintf(("!ERROR!: Invalid handle!!"));
00303             SetLastError(ERROR_INVALID_HANDLE);
00304             return 0;
00305         }
00306         length = GlobalSize(hClipObj)/sizeof(WCHAR);
00307         hClipObjAscii = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, length);
00308         lpTextAscii = (LPSTR)GlobalLock(hClipObjAscii);
00309         if(!hClipObjAscii || lpTextAscii == NULL || length == 0) {
00310             dprintf(("!ERROR!: GlobalAlloc %d failed!!", length));
00311             SetLastError(ERROR_INVALID_HANDLE);
00312             return 0;
00313         }
00314         WideCharToMultiByte(CP_ACP, 0, lpTextUnicode, -1, lpTextAscii, length, 0, NULL);
00315         
00316         GlobalUnlock(hClipObjAscii);
00317         GlobalUnlock(hClipObj);
00318         HANDLE ret = O32_SetClipboardData(CF_TEXT, hClipObjAscii);
00319         //hClipObjAscii will be freed when the clipboard is closed
00320         return ret;
00321     }
00322     return O32_SetClipboardData(uFormat, hClipObj);
00323 #endif
00324 }
00325 //******************************************************************************
00326 //******************************************************************************
00327 HWND WIN32API SetClipboardViewer( HWND hwndNew)
00328 {
00329   Win32BaseWindow *wndnew, *wndold;
00330   HWND hwndOld;
00331   HWND hwndOS2New;
00332 
00333     wndnew = Win32BaseWindow::GetWindowFromHandle(hwndNew);
00334     if(!wndnew) {
00335         dprintf(("SetClipboardViewer, window %x not found", hwndNew));
00336         SetLastError(ERROR_INVALID_WINDOW_HANDLE);
00337         return 0;
00338     }
00339     dprintf(("USER32: SetClipboardViewer %x", hwndNew));
00340     hwndOS2New = wndnew->getOS2WindowHandle();
00341     RELEASE_WNDOBJ(wndnew);
00342 
00343     hwndOld = O32_SetClipboardViewer(hwndOS2New);
00344 
00345     wndold = Win32BaseWindow::GetWindowFromOS2Handle(hwndOld);
00346     if(!wndold) {
00347         //probably an OS/2 window, so pretend it's nobody
00348         return 0;
00349     }
00350     hwndOld = wndold->getWindowHandle();
00351     RELEASE_WNDOBJ(wndold);
00352     return hwndOld;
00353 }
00354 //******************************************************************************
00355 //******************************************************************************

Generated on Wed Jan 23 23:17:25 2002 for ODIN-user32 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001