main.c

/*
    File:       main.c
    
    Description:
        Yet Another Scrolling Text (YAST) Control.
            Yast, it lets you edit Unicode text.
 
    Author:     JM
 
    Copyright:  © Copyright 2003 Apple Computer, Inc. All rights reserved.
    
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                
    Change History (most recent first):
        Fri, Jan 28, 2000 -- created
        Fri, Jan 14, 2003 -- carbon event based, removed wne codes
        Fri, Apr 17, 2003 -- added data accessors
*/
 
#include <Carbon/Carbon.h>
 
#include "YASTControl.h"
 
 
static pascal OSStatus MyWindowCursorHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) {
    OSStatus returnedResult;
    UInt32 eclass, ekind;
        /* set up locals */
    eclass = GetEventClass(inEvent);
    ekind = GetEventKind(inEvent);
        /* default result */
    returnedResult = eventNotHandledErr;
        /* dispatch the event */
    if (eclass == kEventClassWindow && ekind == kEventWindowClose) {
    
            /* exit from RunApplicationEventLoop */ 
        QuitApplicationEventLoop();
        
            /* returnedResult = noErr -- we don't do this here on purpose
            because we want the event to fall through and be handled by the
            standard handler that will actually close the window. */
    }
    return returnedResult;
}
 
 
static OSStatus StartupApplicationAndGetWindow(WindowRef *mainWindow) {
    IBNibRef nibRef;
    OSStatus err;
    EventTypeSpec myEvSpec[] = { { kEventClassWindow, kEventWindowClose } };
    err = TXNInitTextension(NULL,  0, 0);
    if (err == noErr) {
            /*  Create a Nib reference passing the name of the nib
            file (without the .nib extension) CreateNibReference only
            searches into the application bundle. */
        err = CreateNibReference(CFSTR("main"), &nibRef);
        if (err == noErr) {
        
                /* Once the nib reference is created, set the menu bar.
                "MainMenu" is the name of the menu bar object. This name
                is set in InterfaceBuilder when the nib is created. */
            err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
            if (err == noErr) {
            
                    /* Then create a window. "MainWindow" is the name of the
                    window object. This name is set in InterfaceBuilder when
                    the nib is created. */
                err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), mainWindow);
                if (err == noErr) {
            
                        /* Install a carbon event handler on the main window. */
                    err = InstallWindowEventHandler(*mainWindow, NewEventHandlerUPP(MyWindowCursorHandler),
                        (sizeof(myEvSpec)/sizeof(EventTypeSpec)), myEvSpec, NULL, NULL);
                }
            }
                /* We don't need the nib reference anymore. */
            DisposeNibReference(nibRef);
        }
    }
    return err;
}
 
 
int main(int argc, char* argv[])
{
    WindowRef       window;
    ControlRef      control;
    SInt32 id;
    OSStatus        err;
 
        /* startup the application */
    err = StartupApplicationAndGetWindow(&window);
    if (err == noErr) {
 
            /* add our text box controls */
        for (id=0; id<5; id++) {
            ControlID controlID;
            
                /* retrieve one of the 'PANE' controls */
            controlID.id = id;
            controlID.signature = 'PANE';
            err = GetControlByID( window, &controlID, &control );
            if (err == noErr) {
            
                    /* attach a TXN Editing field to it */
                err = YASTControlAttachToExistingControl(control);
                if (err == noErr) {
                    CFStringRef theString;
                    
                        /* create a string to put in the control */
                    theString = CFStringCreateWithFormat(NULL, NULL, CFSTR("Text Field Number %d"), id);
                    if (theString == NULL) {
                        err = memFullErr;
                    } else {
                    
                            /* display the string in the control */
                        err = SetControlData( control, kControlEntireControl,
                                    kYASTControlAllUnicodeTextTag, sizeof(theString), &theString);
                        CFRelease(theString);
                    }
                    if ((id&1) != 0) {
                        Boolean readonly = true;
                        err = SetControlData( control, kControlEntireControl,
                                    kYASTControlReadOnlyTag, sizeof(Boolean), &readonly);
                    } else {
                        Boolean tad = false;
                        SInt16 tabs = 42;
                        err = SetControlData( control, kControlEntireControl,
                                    kYASTControlTabSizeTag, sizeof(tabs), &tabs);
                        err = SetControlData( control, kControlEntireControl,
                                    kYASTControlTabsAdvanceFocusTag, sizeof(tad), &tad);
                    }
                }
            }
            if (err != noErr) break;
        }
        
        if (err == noErr) {
                /* display the window */
            ShowWindow( window );
            
                /* run the application */
            RunApplicationEventLoop();
        }
    
    }
    return err;
}