scrap.c

Go to the documentation of this file.
00001 
00002 /*  $Id: scrap.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 /*
00029 scrap.c -- routines that support use of the desk scrap.
00030 */
00031 
00032 #include "frontier.h"
00033 #include "standard.h"
00034 
00035 #include "shelltypes.h"
00036 #include "scrap.h"
00037 #include "error.h"
00038 #include "memory.h"
00039 #include "threads.h"
00040 
00041 
00042 #ifdef WIN95VERSION
00043 
00044     static short scrapcount = 0;
00045 
00046     static UINT win_textscraptype;
00047 
00048     static UINT win_pictscraptype;
00049 
00050     static UINT win_hashscraptype;
00051 
00052     static UINT win_opscraptype;
00053 
00054     static UINT win_wpscraptype;
00055 
00056     static UINT win_menuscraptype;
00057 
00058     static UINT win_scriptscraptype;
00059 
00060 #endif
00061 
00062 
00063 boolean resetscrap (void) {
00064     //Code change by Timothy Paustian Sunday, June 25, 2000 11:05:43 AM
00065     //Carbon Support
00066     #ifdef MACVERSION
00067         #if TARGET_API_MAC_CARBON == 1
00068         return (ClearCurrentScrap() == noErr);
00069         #else
00070         return (ZeroScrap () == noErr);
00071         #endif
00072     #endif
00073 
00074     #ifdef WIN95VERSION
00075         return (EmptyClipboard());
00076     #endif
00077     } /*resetscrap*/
00078 
00079 
00080 short getscrapcount (void) {
00081 
00082     /*
00083     2002-10-13 AR: Commented out variable declaration for theCount
00084     to eliminate compiler warning about an unused variable.
00085     */
00086     
00087     #ifdef MACVERSION
00088         //Code change by Timothy Paustian Sunday, June 25, 2000 11:05:56 AM
00089         //Carbon Support
00090         #if TARGET_API_MAC_CARBON == 1
00091         
00092             ScrapRef    theRef;
00093             //UInt32        theCount;
00094             static short theShortCount = 1;
00095             OSStatus    status;
00096             static ScrapRef lastScrapRef = nil;
00097             
00098             status = GetCurrentScrap(&theRef);
00099             
00100             if(status != noErr)
00101                 return 0;
00102             
00103             assert(theRef != nil);
00104             
00105             if (theRef != lastScrapRef)
00106                 theShortCount++;
00107                 
00108             lastScrapRef = theRef;
00109             
00110             return (theShortCount);
00111             
00112             //status = GetScrapFlavorCount(theRef, &theCount);
00113             //if(status != noErr)
00114             //  return 0;
00115             
00116             //restrict the return value to a short size
00117             //theShortCount = min(theCount, 32000);
00118             //return theShortCount;
00119             
00120         #else
00121             
00122             PScrapStuff pscrap;
00123             
00124             pscrap = InfoScrap ();
00125             
00126             return ((*pscrap).scrapCount);
00127             
00128         #endif
00129         
00130     #endif
00131 
00132     #ifdef WIN95VERSION
00133         return (scrapcount);
00134     #endif
00135     } /*getscrapcount*/
00136 
00137 
00138 #ifdef WIN95VERSION
00139 
00140 short handlescrapdisposed (void) {
00141     
00142     return (++scrapcount);
00143     } /*handlescrapdisposed*/
00144 
00145 
00146 tyscraptype win2shellscraptype (UINT winscraptype) {
00147 
00148     if (winscraptype == 0)
00149         return (0);
00150 
00151     if (winscraptype == win_textscraptype)
00152         return (textscraptype);
00153 
00154     if (winscraptype == win_pictscraptype)
00155         return (pictscraptype);
00156 
00157     if (winscraptype == win_opscraptype)
00158         return (opscraptype);
00159 
00160     if (winscraptype == win_scriptscraptype)
00161         return (scriptscraptype);
00162 
00163     if (winscraptype == win_wpscraptype)
00164         return (wpscraptype);
00165 
00166     if (winscraptype == win_hashscraptype)
00167         return (hashscraptype);
00168 
00169     if (winscraptype == win_menuscraptype)
00170         return (menuscraptype);
00171 
00172     return (noscraptype);
00173     } /*win2shellscraptype*/
00174 
00175 
00176 UINT shell2winscraptype (tyscraptype scraptype) {
00177 
00178     switch (scraptype) {
00179     
00180         case textscraptype:
00181             return win_textscraptype;
00182         
00183         case pictscraptype:
00184             return win_pictscraptype;
00185         
00186         case hashscraptype:
00187             return win_hashscraptype;
00188         
00189         case opscraptype:
00190             return win_opscraptype;
00191         
00192         case wpscraptype:
00193             return win_wpscraptype;
00194         
00195         case menuscraptype:
00196             return win_menuscraptype;
00197         
00198         case scriptscraptype:
00199             return win_scriptscraptype;
00200         }
00201 
00202     return 0;
00203     } /*shell2winscraptype*/
00204 
00205 #endif
00206 
00207 
00208 tyscraptype getscraptype (void) {
00209     
00210     /*
00211     return the type of the first object on the scrap.
00212     */
00213     
00214     #ifdef MACVERSION
00215         //Code change by Timothy Paustian Sunday, June 25, 2000 11:06:10 AM
00216         //Carbon support for new Scrap Manager API
00217         #if TARGET_API_MAC_CARBON == 1
00218         ScrapRef        scrap;
00219         UInt32          infoNumber = 1;
00220         ScrapFlavorInfo info[1];
00221         OSStatus        status;
00222         
00223         status = GetCurrentScrap(&scrap);
00224         if(status != noErr)
00225             return 0;
00226         //now get the first item on the list
00227         status = GetScrapFlavorInfoList(scrap, &infoNumber, info);
00228         if(status != noErr)
00229             return 0;
00230         
00231         return (tyscraptype)info[1].flavorType;
00232         #else
00233         
00234         PScrapStuff pscrap;
00235         OSType **htype;
00236         
00237         pscrap = InfoScrap ();
00238         
00239         htype = (OSType **) (*pscrap).scrapHandle;
00240         
00241         if (htype == nil)
00242             return (0);
00243         
00244         return (**htype);
00245         #endif
00246         
00247     #endif
00248 
00249     #ifdef WIN95VERSION
00250         UINT format = 0;
00251 
00252         while (true) {
00253 
00254             format = EnumClipboardFormats (format);
00255 
00256             if (format == 0)
00257                 return (0);
00258 
00259             if (format == win_textscraptype)
00260                 return (textscraptype);
00261 
00262             if (format == win_pictscraptype)
00263                 return (pictscraptype);
00264 
00265             if (format == win_opscraptype)
00266                 return (opscraptype);
00267 
00268             if (format == win_scriptscraptype)
00269                 return (scriptscraptype);
00270 
00271             if (format == win_wpscraptype)
00272                 return (wpscraptype);
00273 
00274             if (format == win_hashscraptype)
00275                 return (hashscraptype);
00276 
00277             if (format == win_menuscraptype)
00278                 return (menuscraptype);
00279             }
00280     #endif
00281     } /*getscraptype*/
00282 
00283 
00284 boolean getscrap (tyscraptype scraptype, Handle hscrap) {
00285     
00286     /*
00287     return true if something was loaded from the scrap, false 
00288     otherwise.
00289     
00290     12/26/91 dmb: return true if scraptype exists; generate error alert on real error.
00291 
00292     5.0a16 dmb: need to scan more than last 32 bytes for NUL char
00293     */
00294     
00295     #ifdef MACVERSION
00296 #if TARGET_API_MAC_CARBON == 1
00297         ScrapRef            theScrap;
00298         ScrapFlavorType     flavorType = (ScrapFlavorType)scraptype;
00299         Size                byteCount;
00300         OSStatus            status;
00301         ScrapFlavorFlags    flavorFlags;
00302         boolean             retVal = true;
00303         Size                prevCount;
00304 
00305         status = GetCurrentScrap(&theScrap);
00306         if(status != noErr)
00307             oserror (status);
00308 
00309         status = GetScrapFlavorFlags(theScrap, flavorType, &flavorFlags);
00310         if(status == noTypeErr)
00311         {
00312             retVal = false;
00313         }
00314         else if(status < 0)
00315         {
00316             retVal = false;
00317             oserror (status);
00318         }
00319         //if we get here and the handles nil, we really don't want the data
00320         //also if retVal is false, it means the type does not exist
00321         if(hscrap == nil || !retVal)
00322             return retVal;
00323 
00324         status = GetScrapFlavorSize(theScrap, flavorType, &byteCount);
00325         if(status != noErr)
00326             oserror (status);
00327         prevCount = byteCount;
00328         SetHandleSize(hscrap, byteCount);
00329         if (MemError () != noErr)
00330             return (false);
00331         //lock the handle before getting the data
00332         HLock(hscrap);
00333         status = GetScrapFlavorData(theScrap, flavorType, &byteCount, *(hscrap));
00334         HUnlock(hscrap);
00335 
00336         if(status != noErr)
00337             oserror (status);
00338         //only return true if we got all the data.
00339         return (byteCount == prevCount);
00340 
00341         #else //precarbon mac       
00342         long result;
00343         long offset;
00344         
00345         result = GetScrap (hscrap, scraptype, &offset);
00346         
00347         if (result == noTypeErr)
00348             return (false);
00349         
00350         if (result < 0)
00351             oserror (result);
00352 
00353         return (true); /*there was something on the scrap of the given type*/
00354         #endif
00355         
00356     #endif
00357     
00358     #ifdef WIN95VERSION
00359         Handle hdata;
00360         long ctbytes;
00361         char *pdata;
00362         boolean fl;
00363         /*long i;*/
00364         UINT wintype;
00365         
00366         wintype = shell2winscraptype (scraptype);
00367         
00368         if (wintype == 0)
00369             return (false);
00370         
00371         releasethreadglobals (); //getting clipboard data can send us a WM_RENDERFORMAT msg
00372         
00373         hdata = GetClipboardData (wintype);
00374         
00375         grabthreadglobals ();
00376         
00377         if (hdata == NULL) {
00378             
00379             oserror (GetLastError ()); // may be no error
00380             
00381             return (false);
00382             }
00383         
00384         ctbytes = GlobalSize (hdata);
00385         
00386         pdata = GlobalLock (hdata);
00387         
00388         if (pdata == NULL)
00389             return (false);
00390         
00391         if (scraptype == textscraptype) {       //Handle reducing the scrap size to the NULL
00392             /* i = 0x40;
00393              if (i > ctbytes)
00394                  i = ctbytes;
00395 
00396              while (i > 0) {
00397                  if (*(pdata + ctbytes - i) == 0) {
00398                      ctbytes = ctbytes - i;
00399                      break;
00400                      }
00401 
00402                  --i;
00403                  } /%while%/
00404             */
00405 
00406             ctbytes = strlen (pdata); /*7.1b23: call strlen to find the first terminator: removes junk from end*/
00407             } /*if*/
00408 
00409         fl = sethandlecontents (pdata, ctbytes, hscrap);
00410         
00411         GlobalUnlock (hdata);
00412         
00413         return (fl);
00414     #endif
00415     } /*getscrap*/
00416 
00417 
00418 boolean putscrap (tyscraptype scraptype, Handle hscrap) {
00419     
00420     /*
00421     return true if something was put on the scrap, false 
00422     otherwise.
00423 
00424     5.0a16 dmb: handle NULL scrap for delayed rendering
00425 
00426     5.0b13 dmb: report errors (win)
00427     */
00428 
00429     boolean fl;
00430 
00431     #ifdef MACVERSION
00432         //Code change by Timothy Paustian Sunday, June 25, 2000 11:28:22 AM
00433         //New scrap code. We don't need flavorFlags I think
00434         #if TARGET_API_MAC_CARBON == 1
00435         ScrapRef            theScrap;
00436         OSStatus            status;
00437         ScrapFlavorType     flavorType = (ScrapFlavorType) scraptype;
00438         ScrapFlavorFlags    flavorFlags = kScrapFlavorMaskNone;
00439         Size                flavorSize = GetHandleSize(hscrap);
00440         
00441         status = GetCurrentScrap(&theScrap);
00442         if(status != noErr)
00443             return false;
00444         HLock(hscrap);
00445         fl = (PutScrapFlavor (theScrap, flavorType, flavorFlags, flavorSize, *hscrap) == noErr);
00446         HUnlock(hscrap);
00447         return fl;
00448         
00449         #else
00450         
00451         HLock (hscrap);
00452         
00453         fl = PutScrap (GetHandleSize (hscrap), scraptype, *hscrap) == noErr;
00454         
00455         HUnlock (hscrap);
00456 
00457         return (fl);
00458         #endif
00459         
00460     #endif
00461 
00462     #ifdef WIN95VERSION
00463         long ctbytes;
00464         long allocctbytes;
00465         Handle hdata;
00466         char *pdata;
00467         UINT wintype;
00468         
00469         wintype = shell2winscraptype (scraptype);
00470 
00471         if (wintype == 0) {
00472     
00473             oserror (DV_E_CLIPFORMAT);
00474 
00475             return (false);
00476             }
00477         
00478         if (hscrap == NULL)
00479             hdata = NULL;
00480         
00481         else {
00482             ctbytes = gethandlesize (hscrap);
00483 
00484             allocctbytes = ctbytes;
00485 
00486             if (scraptype == textscraptype)
00487                 ++allocctbytes;                         //Allow for NULL termination
00488 
00489             hdata = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, allocctbytes);
00490             pdata = GlobalLock (hdata);
00491             
00492             if (pdata == NULL) {
00493 
00494                 GlobalFree (hdata);
00495                 
00496                 return (false);
00497                 }
00498 
00499             moveleft (*hscrap, pdata, ctbytes);
00500             
00501             if (scraptype == textscraptype)
00502                 *(pdata + ctbytes) = 0;         //NULL terminate TEXT scraps
00503 
00504             //Window handles are not exact size - NULL fill remaining space for safety
00505             if ((GlobalSize (hdata) - allocctbytes) > 0)
00506                 clearbytes (pdata + allocctbytes, GlobalSize (hdata) - allocctbytes);
00507 
00508             GlobalUnlock (hdata);
00509             }
00510         
00511         SetLastError (0);
00512         
00513         fl = (SetClipboardData (wintype, hdata) != NULL);
00514         
00515         if (!fl)
00516             fl = !oserror (GetLastError ());
00517         
00518         return (fl);
00519     #endif
00520     } /*putscrap*/
00521 
00522 
00523 boolean openclipboard (void) {
00524 
00525     #ifdef MACVERSION
00526         return (true);
00527     #endif
00528 
00529     #ifdef WIN95VERSION
00530         return (OpenClipboard (shellframewindow));
00531     #endif
00532     } /*openclipboard*/
00533 
00534 
00535 boolean closeclipboard (void) {
00536 
00537     #ifdef MACVERSION
00538         return (true);
00539     #endif
00540 
00541     #ifdef WIN95VERSION
00542         boolean fl;
00543 
00544         releasethreadglobals (); //closing clipboard can send us a WM_DRAWCLIPBOARD msg
00545         
00546         fl = CloseClipboard ();
00547         
00548         grabthreadglobals ();
00549 
00550         return (fl);
00551     #endif
00552     } /*closeclipboard*/
00553 
00554 
00555 void initclipboard (void) {
00556 
00557     #ifdef WIN95VERSION
00558         win_textscraptype = CF_TEXT;
00559 
00560         win_pictscraptype = CF_METAFILEPICT;
00561         
00562         win_hashscraptype = RegisterClipboardFormat ("Frontier_HASH");
00563 
00564         win_opscraptype = RegisterClipboardFormat ("Frontier_OP");
00565 
00566         win_wpscraptype = RegisterClipboardFormat ("Frontier_WPTX");
00567 
00568         win_menuscraptype = RegisterClipboardFormat ("Frontier_MNBR");
00569 
00570         win_scriptscraptype = RegisterClipboardFormat ("Frontier_SCPT");
00571     #endif
00572     } /*initclipboard*/
00573 
00574 
00575 
00576 
00577 
00578 
00579 

Generated on Wed May 31 18:20:00 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6