services.c

Go to the documentation of this file.
00001 
00002 /*  $Id: services.c 1199 2006-04-05 22:17:27Z karstenw $    */
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 services.c -- be a services client on OS X.
00030 
00031 Apple docs URL on setting up a Carbon app to use Services:
00032 http://developer.apple.com/techpubs/macosx/Carbon/HumanInterfaceToolbox/MenuManager/appservices/intro/index.html
00033 
00034 7.1b42 PBS 12/14/01
00035 */ 
00036 
00037 #include "frontier.h"
00038 #include "standard.h"
00039 
00040 #include "menu.h"
00041 #include "strings.h"
00042 #include "cancoon.h"
00043 #include "launch.h"
00044 #include "tablestructure.h"
00045 #include "popup.h"
00046 #include "meprograms.h"
00047 #include "dockmenu.h"
00048 #include "opinternal.h"
00049 #include "services.h"
00050 
00051 
00052 /*These aren't in the Universal Interfaces included with CW 7.
00053 They'll probably be in the next version, so be prepared
00054 to undefine them.
00055 
00056 #define kEventServiceCopy 1
00057 #define kEventServicePaste 2
00058 #define kEventServiceGetTypes 3
00059 
00060 #define kEventClassService 'serv'
00061 
00062 enum {
00063   kEventParamScrapRef           = 'scrp',   //    typeScrapRef
00064   kEventParamServiceCopyTypes   = 'svsd',   //    typeCFMutableArrayRef
00065   kEventParamServicePasteTypes  = 'svpt',   //    typeCFMutableArrayRef
00066   kEventParamServiceMessageName = 'svmg',   //    typeCFStringRef
00067   kEventParamServiceUserData    = 'svud',   //    typeCFStringRef
00068   typeScrapRef                  = 'scrp',   //    ScrapRef
00069   typeCFMutableArrayRef         = 'cfma'    //    CFMutableArrayRef
00070 };
00071 
00072 extern CFStringRef CreateTypeStringWithOSType (OSType inType);
00073 */
00074 
00075 
00076 /*Prototypes*/
00077 
00078 pascal OSStatus serviceshandlercopy (EventHandlerCallRef nextHandler, EventRef inEvent, void* userData);
00079 pascal OSStatus serviceshandlerpaste (EventHandlerCallRef nextHandler, EventRef inEvent, void* userData);
00080 pascal OSStatus serviceshandlergettypes (EventHandlerCallRef nextHandler, EventRef inEvent, void* userData);
00081   
00082 static const OSType servicesdatatypes [] = {'TEXT'}; /*support TEXT only*/
00083 
00084 
00085 /*Functions*/
00086 
00087 
00088 pascal OSStatus serviceshandlercopy (EventHandlerCallRef nextHandler, EventRef inEvent, void* userData) {
00089 #pragma unused(userData, nextHandler)
00090 
00091     /*
00092     Copy the current selection to the Services-specific scrap.
00093     
00094     2002-10-13 AR: Removed variable declarations for count and ix
00095     to eliminate a compiler warning about unused variables.
00096     */
00097     
00098     ScrapRef servicesscrap;
00099     Handle hscrap;
00100     boolean flconverted = false;
00101     long bytecount;
00102 
00103     
00104     if (shellwindowinfo == nil)
00105         return (eventNotHandledErr);
00106     
00107     if (!newclearhandle (256, &hscrap))
00108         return (eventNotHandledErr);
00109         
00110     shellpushwindowglobals (shellwindowinfo);
00111     
00112     (*shellglobals.copyroutine) ();
00113     
00114     shellconvertscrap ('TEXT', &hscrap, &flconverted); /*Okay, this is just 'TEXT'-specific right here.*/
00115     
00116     if (!flconverted) {
00117         
00118         disposehandle (hscrap);
00119         
00120         return (eventNotHandledErr);
00121         } /*if*/
00122     
00123     shellpopglobals ();
00124     
00125     GetEventParameter (inEvent, kEventParamScrapRef, typeScrapRef, nil, sizeof (ScrapRef), nil, &servicesscrap);
00126     
00127     bytecount = gethandlesize (hscrap);
00128     
00129     lockhandle (hscrap);
00130     
00131     PutScrapFlavor (servicesscrap, 'TEXT', 0, bytecount, (*hscrap));
00132     
00133     unlockhandle (hscrap);
00134     
00135     disposehandle (hscrap);
00136     
00137     return (noErr);
00138     } /*serviceshandlercopy*/
00139 
00140 
00141 pascal OSStatus serviceshandlerpaste (EventHandlerCallRef nextHandler, EventRef inEvent, void* userData) {
00142 #pragma unused(userData, nextHandler)
00143 
00144     ScrapRef servicesscrap, currentscrap;
00145     long bytecount;
00146     OSErr err;
00147     Handle hscrap;
00148 
00149     if (shellwindowinfo == nil)
00150         return (eventNotHandledErr);
00151 
00152     GetEventParameter (inEvent, kEventParamScrapRef, typeScrapRef, nil, sizeof (ScrapRef), nil, &servicesscrap);
00153     
00154     ClearCurrentScrap ();
00155     
00156     GetCurrentScrap (&currentscrap);
00157 
00158     err = GetScrapFlavorSize (servicesscrap, 'TEXT', &bytecount);
00159     
00160     if (err != noErr)
00161         return (eventNotHandledErr);
00162     
00163     if (!newclearhandle (bytecount, &hscrap))
00164         return (eventNotHandledErr);
00165 
00166     lockhandle (hscrap);
00167     
00168     err = GetScrapFlavorData (servicesscrap, 'TEXT', &bytecount, *hscrap);
00169     
00170     unlockhandle (hscrap);
00171     
00172     if (err != noErr) {
00173     
00174         disposehandle (hscrap);
00175         
00176         return (eventNotHandledErr);
00177         } /*if*/
00178     
00179     lockhandle (hscrap);
00180     
00181     PutScrapFlavor (currentscrap, 'TEXT', 0, bytecount, *hscrap);
00182  
00183     unlockhandle (hscrap);
00184     
00185     disposehandle (hscrap);
00186     
00187     shellpushwindowglobals (shellwindowinfo);
00188 
00189     (*shellglobals.pasteroutine) ();
00190     
00191     shellpopglobals ();
00192  
00193     return (noErr);
00194     } /*serviceshandlerpaste*/
00195 
00196 
00197 pascal OSStatus serviceshandlergettypes (EventHandlerCallRef nextHandler, EventRef inEvent, void* userData) {
00198 #pragma unused(userData, nextHandler)
00199 
00200     CFMutableArrayRef copytypes, pastetypes;
00201     short ix, count;
00202     
00203     GetEventParameter (
00204             inEvent,
00205             kEventParamServiceCopyTypes,
00206             typeCFMutableArrayRef,
00207             NULL,
00208             sizeof (CFMutableArrayRef),
00209             NULL,
00210             &copytypes);
00211 
00212     GetEventParameter (
00213             inEvent,
00214             kEventParamServicePasteTypes,
00215             typeCFMutableArrayRef,
00216             NULL,
00217             sizeof (CFMutableArrayRef),
00218             NULL,
00219             &pastetypes);
00220 
00221     count = sizeof (servicesdatatypes) / sizeof (OSType);
00222 
00223     /*Place our data types in the copytypes and pastetypes arrays. This code is generalized
00224     so it's possible to support more than just 'TEXT' -- see servicesdatatype array at
00225     the top of this file.*/
00226     
00227     for (ix = 0; ix < count; ix++) {
00228 
00229         CFStringRef type = CreateTypeStringWithOSType (servicesdatatypes [ix]);
00230         
00231         if (type) {
00232 
00233             CFArrayAppendValue (copytypes, type);
00234                 
00235             CFArrayAppendValue (pastetypes, type);
00236 
00237             CFRelease (type);
00238             } /*if*/
00239         } /*for*/
00240 
00241     return (noErr);
00242     } /*serviceshandlergettypes*/
00243 
00244 
00245 static void servicesinstallhandlers (void) {
00246     
00247     /*
00248     Install the Services Carbon Events handler.
00249     Return values aren't checked, because if this doesn't work,
00250     it's not fatal.
00251     
00252     These are all installed as application-level handlers.
00253     
00254     I could have done one handler that does a switch to see what the event
00255     type is, but I prefer multiple handlers, it feels cleaner.
00256     */
00257     
00258     const EventTypeSpec gettypeevent = {kEventClassService, kEventServiceGetTypes};
00259     const EventTypeSpec copyevent = {kEventClassService, kEventServiceCopy};
00260     const EventTypeSpec pasteevent = {kEventClassService, kEventServicePaste};
00261     
00262     InstallApplicationEventHandler (NewEventHandlerUPP (serviceshandlergettypes), 1, &gettypeevent, 0, NULL);
00263 
00264     InstallApplicationEventHandler (NewEventHandlerUPP (serviceshandlercopy), 1, &copyevent, 0, NULL);
00265     
00266     InstallApplicationEventHandler (NewEventHandlerUPP (serviceshandlerpaste), 1, &pasteevent, 0, NULL);
00267     } /*servicesinstallhandlers*/
00268 
00269 
00270 void initservices (void) {
00271 
00272     /*
00273     Called from shell.c.
00274     */
00275     
00276     servicesinstallhandlers ();
00277     } /*initservices*/

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