shellscrap.c

Go to the documentation of this file.
00001 
00002 /*  $Id: shellscrap.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 #include "frontier.h"
00030 #include "standard.h"
00031 
00032 #include "kb.h"
00033 #include "memory.h"
00034 #include "scrap.h"
00035 #include "strings.h"
00036 #include "shell.h"
00037 #include "shellhooks.h"
00038 
00039 
00040 
00041 
00042 typedef struct tyscraprecord {
00043 
00044     short count; /*for comparison to toolbox scrapcount*/
00045     
00046     boolean floptionkey; /*4.1b7 dmb: was scrap read w/Option key down?*/
00047     
00048     boolean fldirty; /*needs to be put into the desktop scrap*/
00049     
00050     short fllocked; /*for Win reentrancy*/
00051 
00052     Handle hscrap; /*if not nil, points to a chunk of text*/
00053     
00054     tyscraptype type; /*one type at a time*/
00055     
00056     shelldisposescrapcallback disposeroutine;
00057     
00058     shellexportscrapcallback exportroutine;
00059     } tyscraprecord;
00060 
00061 
00062 tyscraprecord shellscrap;
00063 
00064 
00065 
00066 
00067 static void shelldisposeinternalscrap (void) {
00068     
00069     /*
00070     5.0a16 dmb: this used to be shelldisposescrap
00071     */
00072 
00073     if (shellscrap.hscrap)
00074         (*shellscrap.disposeroutine) (shellscrap.hscrap);
00075     
00076     shellscrap.hscrap = nil;
00077     
00078     shellscrap.fldirty = false;
00079     } /*shelldisposeinternalscrap*/
00080 
00081 
00082 #ifdef WIN95VERSION
00083 
00084 void shelldisposescrap (void) {
00085     
00086     if (!shellscrap.fllocked) {
00087         
00088         shelldisposeinternalscrap ();
00089         
00090         handlescrapdisposed ();
00091         }
00092     } /*shelldisposescrap*/
00093 
00094 #endif
00095 
00096 
00097 static boolean shellsetinternalscrap (void * hscrap, tyscraptype type, shelldisposescrapcallback disposeroutine, shellexportscrapcallback exportroutine) {
00098     
00099     /*
00100     5.0a16 dmb: this used to be shellsetscrap
00101     */
00102 
00103     if (hscrap == nil)
00104         return (false);
00105     
00106     shelldisposeinternalscrap (); /*get rid of whatever's there*/
00107     
00108     shellscrap.hscrap = hscrap;
00109     
00110     shellscrap.fldirty = true;
00111     
00112     shellscrap.type = type;
00113     
00114     shellscrap.disposeroutine = disposeroutine;
00115     
00116     shellscrap.exportroutine = exportroutine;
00117     
00118     shellscrap.count = getscrapcount (); /*we're newer than desk scrap*/
00119 
00120     return (true);
00121     } /*shellsetinternalscrap*/
00122 
00123 
00124 boolean shellsetscrap (void * hscrap, tyscraptype type, shelldisposescrapcallback disposeroutine, shellexportscrapcallback exportroutine) {
00125     
00126     /*
00127     5.0a16 dmb: do it right, use delayed rendering for Win. Calling 
00128     putscrap should result in the disposal of the current private clipboard
00129     */
00130     
00131     #ifdef WIN95VERSION
00132         if (openclipboard ()) { // set this scrap type for delayed rendering
00133             
00134             if (resetscrap ()) {
00135 
00136                 putscrap (type, NULL);
00137                 
00138                 if ((type != textscraptype) && (type != pictscraptype))
00139                     putscrap (textscraptype, NULL);
00140                 }
00141 
00142             closeclipboard ();
00143             }
00144     #endif
00145     
00146     return (shellsetinternalscrap (hscrap,type, disposeroutine, exportroutine));
00147     } /*shellsetscrap*/
00148 
00149 
00150 boolean shellgetscrap (Handle *hscrap, tyscraptype *type) {
00151         
00152     *hscrap = shellscrap.hscrap;
00153     
00154     *type = shellscrap.type;
00155     
00156     return (shellscrap.hscrap != nil);
00157     } /*shellgetscrap*/
00158 
00159 
00160 static boolean exportshellscrap (tyscraptype type, Handle *hscrap, boolean *flconverted) {
00161     
00162     /*
00163     5.0b10 dmb: if exportroutine is nil, but script is right type, 
00164     return true with unconverted data
00165 
00166     5.0.2 dmb: if hscrap is nil, return false
00167     */
00168     
00169     if (shellscrap.hscrap == nil)
00170         return (false);
00171 
00172     if (shellscrap.exportroutine)
00173         return ((*shellscrap.exportroutine) (shellscrap.hscrap, type, hscrap, flconverted));
00174     
00175     if (shellscrap.type == type) {
00176         
00177         *hscrap = shellscrap.hscrap;
00178         
00179         *flconverted = false; /*we're returning a handle to the actual scrap*/
00180         
00181         return (true);
00182         }
00183     
00184     return (false);
00185     } /*exportshellscrap*/
00186 
00187 
00188 boolean shellconvertscrap (tyscraptype type, Handle *hscrap, boolean *flconverted) {
00189     
00190     /*
00191     if the scrap is of the given type, simply return a handle to it with
00192     no conversion.
00193     otherwise, return a handle to a converted version of the scrap contents.
00194     */
00195     
00196     if (!shellscrap.hscrap)
00197         return (false);
00198     
00199     if (shellscrap.type == type) {
00200         
00201         *hscrap = shellscrap.hscrap;
00202         
00203         *flconverted = false; /*we're returning a handle to the actual scrap*/
00204         
00205         return (true);
00206         }
00207     
00208     return (exportshellscrap (type, hscrap, flconverted));
00209     } /*shellconvertscrap*/
00210 
00211 
00212 boolean shellreadscrap (void) {
00213     
00214     /*
00215     return true if something was loaded from the desk scrap, false 
00216     otherwise.
00217     
00218     dmb: since we don't display the script (i.e. no Clipboard window), 
00219     this doesn't need to be called every time the desk scrap may have changed.
00220     we only need to check the desk scrap when we're about to do a paste.
00221     
00222     12/28/90 dmb: handle pict
00223     
00224     4.1b7 dmb: added floptionkey checking so that same scrap can be 
00225     imported again if the state of the Option key changes
00226     */
00227     
00228     short scrapcount;
00229     Handle hscrap;
00230     
00231     scrapcount = getscrapcount ();
00232     
00233     if ((scrapcount <= shellscrap.count) && /*desk scrap isn't newer*/
00234         (keyboardstatus.floptionkey == shellscrap.floptionkey)) /*same key status*/
00235         return (false);
00236     
00237     shellscrap.count = scrapcount; /*remember for next time*/
00238     
00239     shellscrap.floptionkey = keyboardstatus.floptionkey; /*4.1b7*/
00240     
00241     if (!newemptyhandle (&hscrap)) /*this is where the result of GetScrap is to be put*/
00242         return (false);
00243     
00244     openclipboard ();
00245 
00246     if (shellcallscraphooks (hscrap)) { /*none of the hooks set the scrap*/
00247         
00248         register tyscraptype scraptype = noscraptype;
00249         
00250     #ifndef WIN95VERSION
00251         if (getscrap ('PICT', hscrap))
00252             scraptype = pictscraptype;
00253         
00254         else { 
00255     #endif
00256             if (getscrap ('TEXT', hscrap))
00257                 scraptype = textscraptype;
00258         
00259             else {
00260                 
00261                 scraptype = getscraptype (); /*whatever's there*/
00262                 
00263                 if (!getscrap (scraptype, hscrap))
00264                     scraptype = noscraptype;
00265                 }
00266         #ifndef WIN95VERSION
00267             }
00268         #endif
00269         
00270         if (scraptype != noscraptype)
00271             shellsetinternalscrap (hscrap, scraptype, (shelldisposescrapcallback) disposehandle, nil);
00272         }
00273     
00274     shellscrap.fldirty = false; /*doesn't need to be exported*/
00275     
00276     closeclipboard ();
00277 
00278     return (true);
00279     } /*shellreadscrap*/
00280 
00281 
00282 #ifdef WIN95VERSION
00283 
00284 static boolean cr2crlfhandle (Handle htext) {
00285     
00286     byte bscr[] = "\x01\r";
00287     byte bscrlf[] = "\x02\r\n";
00288     Handle hcr = nil;
00289     Handle hcrlf = nil;
00290     boolean fl;
00291     
00292     if (!newtexthandle (bscr, &hcr))
00293         return (false);
00294 
00295     fl = newtexthandle (bscrlf, &hcrlf);
00296 
00297     if (fl)
00298         fl = textfindreplace (hcr, hcrlf, htext, true, false);
00299 
00300     disposehandle (hcr);
00301     
00302     disposehandle (hcrlf);
00303 
00304     return (fl);
00305     } /*cr2crlfhandle*/
00306 
00307 #endif
00308 
00309 
00310 boolean shellexportscrap (tyscraptype scraptype) {
00311     
00312     /*
00313     7/21/91 dmb: new convention: asking the export routine to export to its 
00314     own type means we want a contigous version of its structure to put on the 
00315     system clipboard
00316     
00317     5.0a16 dmb: add linefeeds when exporting converted text.
00318     
00319     5.0a23 dmb: call shellconvertscrap, not exportshellscrap
00320     
00321     5.0b9 dmb: no, that was wrong. we must allow the exportscrap routine to 
00322     create an export version of the current type, which convertscrap doesn't do
00323     */
00324     
00325     Handle hscrap = nil;
00326     boolean flconverted = false;
00327     boolean fl = false;
00328     
00329     if (!exportshellscrap (scraptype, &hscrap, &flconverted)) //couldn't create this type
00330         return (false);
00331     
00332     #ifdef WIN95VERSION
00333     
00334         if (scraptype == textscraptype) {
00335             
00336             if (flconverted)            
00337                 cr2crlfhandle (hscrap);
00338             }
00339         
00340     #endif
00341     
00342     fl = putscrap (scraptype, hscrap);
00343     
00344     if (flconverted)
00345         disposehandle (hscrap);
00346     
00347     return (fl);
00348     } /*shellexportscrap*/
00349 
00350 
00351 boolean shellwritescrap (tyscraptype type) {
00352     
00353     /*
00354     export our internal scrap.  if it's a picture or text, export it as is.  
00355     otherwise convert to pict or text and export that.
00356     
00357     6/5/91 dmb: now put internal form of scrap onto clipboard as well 
00358     as converted material
00359     
00360     2.1b3 dmb: added type parameter so caller can cause scrap to be 
00361     written only if it's a certain type. if the caller passes textscraptype, 
00362     we'll accept any scrap smaller than 256 bytes.
00363     
00364     5.0d14 dmb: can't count on handle size of non-text scrap predicting the 
00365     size of an exported text scrap. so we don't enforce the 255-char limit 
00366     here. The caller must do so.
00367 
00368     5.0a16 dmb: Win-ized, scrap exporting occurs on demand. we only 
00369     do something if type is allscraptypes, meaning we're responding to a 
00370     WM_RENDERALLFORMATS message
00371     */
00372     
00373     boolean fl = false;
00374     
00375     #ifdef WIN95VERSION
00376         if (type != allscraptypes)
00377             return (true);
00378     #endif
00379 
00380     #ifdef MACVERSION
00381         if (!shellscrap.fldirty)
00382             return (false);
00383         
00384         if ((type != anyscraptype) && (type != shellscrap.type)) {
00385             
00386             if ((type != textscraptype) /*|| (gethandlesize (shellscrap.hscrap) > lenbigstring)*/)
00387                 return (false);
00388             }
00389     #endif
00390     
00391     shellscrap.fldirty = false;
00392     
00393     if (!shellscrap.hscrap)
00394         return (false);
00395     
00396     if (!openclipboard ())
00397         return (false);
00398     
00399     ++shellscrap.fllocked; //so resetscrap won't touch it
00400 
00401     if (!resetscrap ())
00402         goto exit;
00403     
00404     shellscrap.count = getscrapcount ();
00405     
00406     switch (shellscrap.type) {
00407         
00408         #if TARGET_API_MAC_CARBON == 1
00409             case hashscraptype: /*7.0b48 PBS: can't export this type*/
00410             
00411                 fl = false;
00412                 
00413                 break;
00414         #endif
00415             
00416         case pictscraptype:
00417             fl = shellexportscrap (pictscraptype);
00418             
00419             break;
00420 
00421         case textscraptype:
00422             fl = shellexportscrap (textscraptype);
00423 
00424             break;
00425         
00426         default:
00427             if (shellexportscrap (shellscrap.type))
00428                 fl = true;
00429             
00430             if (shellexportscrap (pictscraptype))
00431                 fl = true;
00432             
00433             if (shellexportscrap (textscraptype))
00434                 fl = true;
00435             
00436             break;
00437         }
00438 
00439 exit:
00440     --shellscrap.fllocked;
00441 
00442     closeclipboard ();
00443 
00444     return (fl);
00445     } /*shellwritescrap*/
00446 
00447 
00448 void initscrap (void) {
00449     
00450     initclipboard ();
00451 
00452     clearbytes (&shellscrap, sizeof (shellscrap));
00453     
00454     shellscrap.count = getscrapcount () - 1; /*make sure they're different*/
00455     } /*initscrap*/

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