# Error in Learning Carbon Book?



## Mac Osxtopus (May 25, 2003)

I was struggling to grasp the aspect of carbon programming in the GUI using interface builder(i don't know the raw-code way yet, and don't correct me on that stance with technicalities please  ), and I got to the point where the "moon travel planner" can calculate all the different modes of transport. Instead all except the "apollo spacecraft" mode give an answer except 0. The apollo spacecraft one gives the value of 2483.0, which isn't correct. The code for the apollo spacecraft sets the time to 4. Anyone who learned how to program in carbon using O'reillys Learning Carbon book would be ESPECIALLY helpful.
Errors I ask for you to help fix:
Radio buttons don't refresh, once you make one "activated" when you choose another they are both "on".
Calculations don't work. 


Here is the code typed in main.c, the only file except for main.nib for the interface builder  stuff:
// alligators taken out to ensure text readable on forum
#include Carbon/Carbon.h
#define kMTPApplicationSignature	'MTPP'
#define kMTPComputeCommand	'tRav'
#define kMTPTravelTimeFieldID 	129
#define kMTPModeOfTransportButtonGroupID	130

//define c onstant to use in the MT time computeation
#define kMTPHoursPerDay 24
#define kMTPDistanceToMoon 384467 //kilometers

//Define constants to identify the mode of transportation.
#define kMTPFootMode	1
#define kMTPCareMode	2
#define kMTPCommercialJetMode	3
#define kMTPApolloSpacecraftMode	4

//define a window referenc to the main window
WindowRef	gMainWindow;

//Function declaration for the window event handlers
pascal OSStatus MTPMainWindowEventHandler (EventHandlerCallRef myHandler, EventRef event, void *userData);
int main(int argc, char* argv[])
{
    IBNibRef 		nibRef;
    //WindowRef 		window;

    OSStatus		err;
    EventTypeSpec	mainSpec= {kEventClassCommand, kEventCommandProcess};
    // 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);
    require_noerr( err, CantGetNibRef );

    // 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"));
    require_noerr( err, CantSetMenuBar );

    // 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"), &gMainWindow);
    require_noerr( err, CantCreateWindow );

    // We don't need the nib reference anymore.
    DisposeNibReference(nibRef);
    InstallWindowEventHandler(gMainWindow,NewEventHandlerUPP(MTPMainWindowEventHandler),1,&mainSpec,(void *) gMainWindow, NULL);
    // The window was created hidden so show it.
    ShowWindow( gMainWindow );

    // Call the event loop
    RunApplicationEventLoop();

CantCreateWindow:
CantSetMenuBar:
CantGetNibRef:
	return err;
}

pascal OSStatus MTPMainWindowEventHandler (EventHandlerCallRef myHandler, EventRef event, void *userData)
{

    OSStatus	result = eventNotHandledErr;//1
    HICommand	command;

    GetEventParameter(event, kEventParamDirectObject, typeHICommand, NULL, sizeof (HICommand), NULL, &command); // 2 ^

    switch(command.commandID)
    {
     case kMTPComputeCommand://3
        MTPComputerCommandHandler ((WindowRef) userData);
        result=noErr;//4 ^
        break;
    }
return result;
}

pascal void MTPComputerCommandHandler(WindowRef window)
{
    ControlHandle	modeOfTransportButtonGroup, // 1
                        travelTimeField;
    ControlID	modeOfTransportControlID= {kMTPApplicationSignature, // 2
    kMTPModeOfTransportButtonGroupID};
    ControlID travelTimeControlID = {kMTPApplicationSignature, kMTPTravelTimeFieldID };

CFStringRef text;
double travelTime;
SInt32	transportModeValue;
OSErr	status;

GetControlByID (window, &modeOfTransportControlID, //3
&modeOfTransportButtonGroup);
GetControlByID (window, &travelTimeControlID, &travelTimeField);
transportModeValue=GetControl32BitValue (modeOfTransportButtonGroup);//4
switch(transportModeValue)
{
    case kMTPFootMode:
    //Foot - Good walking time is 4 miles/hour
    //travelTime=(kMTPDistanceToMoon/(4.0/0.62))/kMTPHoursPerDay;
    travelTime=(384467/(4.0/0.62))/kMTPHoursPerDay;
    break;
    case kMTPCareMode:
    // Care - 70 miles per hour on the highway, no speed limit in space!
    travelTime=(384467/(70/0.62))/kMTPHoursPerDay;
    break;
    case kMTPCommercialJetMode:
    //Commercial Jet - 600 Miles per hour.
    travelTime=(384467/(70/0.62))/kMTPHoursPerDay;
    break;
    case kMTPApolloSpacecraftMode:
    //Apollo 11 took 4 days to get to he moon
    travelTime=4;
    break;
    default:
    travelTime=0;
    break;
    }
    text=CFStringCreateWithFormat (NULL, NULL, CFSTR("%2.1f"), travelTime);//6
    status=SetControlData(travelTimeField, kControlEntireControl, 
    kControlEditTextCFStringTag, sizeof(CFStringRef), &text); // 7
    CFRelease(text);
    DrawOneControl(travelTimeField);
    }


----------



## Mac Osxtopus (Jun 5, 2003)

ok, no one even responded T_T

Well, i found out i was using the wrong button type, but regardless I'm surprised no one replied with ANYTHING.


----------



## Captain Code (Jun 6, 2003)

Maybe noone here programs in Carbon any more 
I know I don't.


----------



## Mac Osxtopus (Jun 12, 2003)

Carbon is the only "real" language for big games (Warcraft III in cocoa would lag on an innumerable scale) and i can't find any information on how to do the GUI/Graphics/ other stuff for games in Carbon  . Please if you have any help on that (OTHER THAN THE APPLE DEV SITE, it's useless  ) I would greatly appreciate it.


----------

