< Previous PageNext Page > Hide TOC

Using Cocoa Functionality in a Carbon Application

This article describes how a Carbon application can use Cocoa functionality that is unrelated to the user interface. You can access Cocoa functionality in a Carbon application in Mac OS X version 10.1 and later. You need to perform two major tasks to use Cocoa functionality in a Carbon application:

The tasks described in the following sections are illustrated using sample code taken from a working application called Spelling Checker. The sample application uses Cocoa’s spell checking functionality. See “About the Spelling Checker Application” for a description of the application. You can download the code for SpellingChecker-CarbonCocoa.

Although a lot of the code from the Spelling Checker application is shown in the listings in this article, not all of the code is included or explained. For example, none of the code that handles the Carbon window has been included. To see exactly how the Carbon and Cocoa pieces fit together, you should download the project.

About the Spelling Checker Application

The sample Carbon application, Spelling Checker, provides spelling checking functionality for text typed into a window. The user interface is shown in Figure 1. The Spelling Checker window is a Carbon window, created with Interface Builder. The user can type text into the large text box on the left side of the window.

To check spelling, the user clicks the Check Spelling button. The first misspelled word is displayed below the button, as shown in Figure 1 (“clal”). Suggestions for a replacement word are shown in the Guesses list. The user can choose to:


Figure 1  The user interface for the Spelling Checker application

The user interface for the Spelling Checker application

Spelling checking functionality is provided by the Cocoa frameworks and accessed through C-callable wrapper functions, but called from the Carbon application.

Note: The text box in the sample application is a Unicode TextEdit control. For a more complex application, it is better to use the text editing capabilities provided by the Multilingual Text Engine (MLTE) API.

Writing the Cocoa Source

Writing the Cocoa source requires performing the tasks described in the following sections:

Creating a New Cocoa Source File Using Xcode

To make a Cocoa source file using Xcode, do the following:

  1. Open your Carbon project in Xcode.

  2. Choose File > New File.

  3. Select Empty File in Project in the New File window and click the Next button.

  4. Name the file so it has the appropriate .m extension. The sample code filename is SpellCheck.m.

    Recall from “Preprocessing Mixed-Language Code” that the .m extension indicates to the compiler that the code is Objective-C.

  5. Add the following statements to your new file:

    #include <Carbon/Carbon.h>
    #include <Cocoa/Cocoa.h>

As long as you create your source file using Xcode, you should not need to modify build settings and property list values.

Identifying Cocoa Methods

You need to identify the Cocoa methods that provide the functionality your Carbon application needs. For each of the methods you identify, you’ll need to write a C-callable wrapper function.

The Spelling Checker application requires the functionality provided by the following methods:

For additional information, see NSSpellChecker Class Reference.

You also need to identify any other methods that are needed to implement the Cocoa functionality. For example, the class method sharedSpellChecker returns an instance of NSSpellChecker.

Writing C-Callable Wrapper Functions

After you have identified the Cocoa methods that provide the functionality you want to use, you need to write C-callable wrapper functions for those methods.

For the Spelling Checker application, there are eight Cocoa methods (see “Identifying Cocoa Methods”) that provide functionality to manage and check spelling in a document. In order for the Carbon portion of the application to access the Cocoa methods, you need to write C-callable wrapper functions and put them in the Cocoa source file. You also need to declare the functions in a shared header file. Table 1 lists the names of the C-callable wrapper functions in the Spelling Checker application.

Table 1  C-callable wrapper functions for Cocoa methods

C-callable wrapper function

Cocoa method

UniqueSpellDocumentTag

uniqueSpellDocumentTag

CloseSpellDocumentWithTag

closeSpellDocumentWithTag:

CheckSpellingOfString

checkSpellingOfString:startingAt:

CheckSpellingOfStringWithOptions

checkSpellingOfString:startingAt: language:wrap:inSpellDocumentWithTag: wordCount:

IgnoreWord

ignoreWord:inSpellDocumentWithTag:

SetIgnoredWords

setIgnoredWords:inSpellDocumentWithTag:

CopyIgnoredWordsInSpellDocumentWithTag

ignoredWordsInSpellDocumentWithTag:

GuessesForWords

guessesForWord:

Listing 1 shows the C-callable wrapper function UniqueSpellDocumentTag. Note the code for the autorelease pool. For a Cocoa method used by a Carbon application, you must set up an autorelease pool each time it’s used.

Listing 1  A C-callable wrapper function for the uniqueSpellDocumentTag: method

int     UniqueSpellDocumentTag ()
{
    int  tag;
 
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    tag  = [NSSpellChecker uniqueSpellDocumentTag];
    [pool release];
 
    return (tag);
}

All the other C-callable wrapper functions for the Spelling Checker application are written in the same manner as shown in Listing 1, using these guidelines:

Listing 2  A C-callable wrapper function for the checkSpellingOfString:startingAt: method

CFRange  CheckSpellingOfString (CFStringRef stringToCheck,
                        int startingOffset)
{
    NSRange range = {0,0};
 
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    range = [[NSSpellChecker sharedSpellChecker]
                    checkSpellingOfString:(NSString *) stringToCheck
                    startingAt:startingOffset];
    [pool release];
    return ( *(CFRange*)&range );
}

You will also want to create a header file that contains the C-callable wrapper function declarations that can be included in the appropriate source files.

The code for the rest of the C-callable wrapper functions needed for the Spelling Checker application are in the SpellCheck.m Cocoa source file. You can download the code for SpellingChecker-CarbonCocoa.

Calling C-Wrapper Functions From Your Carbon Application

You can use the C-callable wrapper functions as needed in your Carbon application. Listing 3 shows how to call a C-callable wrapper function (CheckSpellingOfString) from your Carbon application’s event handler. (You can see this code in context by downloading the Spelling Checker application from the developer sample code website.) A detailed explanation of each numbered line of code appears following the listing.

Listing 3  Calling a C-wrapper function from your Carbon application

if (command.commandID == 'Spel') // 1
{
    GetControlByID (window, &controlID, &control); // 2
    err = GetControlData (control, 0,
               kControlStaticTextCFStringTag,
               sizeof(CFStringRef),
               &stringToSpellCheck,
               &count); // 3
    if (err == noErr)
    {
        windowInfo->range = CheckSpellingOfString (stringToSpellCheck, 0); // 4
        if (windowInfo->range.length > 0) // 5
            SetMisspelledWord (window, stringToSpellCheck, &windowInfo->range);
        else
            windowInfo->range.location = 0;
    }
}

Here’s what the code does:

  1. Checks to see if the command ID is the one that’s issued when the user clicks the Check Spelling button.

  2. Calls the Control Manager function GetControlByID to obtain the ControlRef of the Unicode TextEdit control. This is the text box that contains the text typed by the user that needs to have its spelling checked. See Figure 1.

  3. Calls the Control Manager function GetControlData to obtain the string that the user typed in the text box.

  4. Calls the C-wrapper function CheckSpellingOfString. Recall that this C-wrapper function wraps the Cocoa method checkSpellingOfString:startingAt:.

  5. Uses the location information returned from the C-wrapper function to set the location of the misspelled word if one is found.



< Previous PageNext Page > Hide TOC


© 2002, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.