Displaying ONE window using Carbon

Daz

Registered
Hi all,
I intend to port a win32 application in C to Mac, using Carbon. I'd like to begin by a little training and I can't get myself displaying a simple window. I don't want any Nib files, I wanna create everything from scratch (no resource file neither).
Here is my code :
#include <stdio.h>
#include <Carbon/Carbon.h>

int main (/*int argc, const char * argv[]*/) {

WindowRef theWindow;
Rect contentRect;
OSStatus err;

printf("Start main!\n");
SetRect(&contentRect, 100,100,100,100);
err=CreateNewWindow(kDocumentWindowClass,kWindowStandardDocumentAttributes,&contentRect, &theWindow);
if(err==FALSE)printf("Error in CreateNewWindow\n");

ShowWindow(theWindow);
printf("End main!\n");
return 0;
}

I've read the tutorial on Apple.com by it doesn't help, and when I use the debugger, I see that CreateNewWindow returns an error, but WHAT ??

All I want is a simple window with Hello in there.
Pretty simple, and then I'll build upon that.

Can anyone help me ?
I don't know what to do with this "CreateNewWindow"...In Carbon, we can also use NewCWindow, but I can't get this working neither. I've got a good background with Java, but how do you dump the stack in C ?

Thanks

@ + Daz
 
Most of it looks okay, but I wonder about the window coordinates. It looks like you're making a window that's 0 x 0 pixels, and that might not work.
 
Well, yeah, I got the answer from the Carbon-Development site. I'm actually passing a Java-type coordinates to the rectangle. that's a first mistake. Then, I should work on better the error message, so that I can pinpoint better the problem.
Would you know why I can't display a FloatingWindow ? I mean, when I create 2 windows of document type, they both appear correctly, and if I just change the type of one window to floating, only one appears ...
Why ???

Thanks

@ + Daz
 
Hereby you can find the code. I've commented the stuff in buildConsole(). If I use kDocumentWindowClass I see my console, otherwise I don't see it. Can you help me ?
(By the way, would you know how to get a handle to the console ?)

thanks

@ + Daz

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

#define DEBUG 1

//Window initial position
#define kWindowTop 100
#define kWindowLeft 600
#define kWindowRight 900
#define kWindowBottom 400
//Console initial position
#define kConsoleTop 100
#define kConsoleLeft 50
#define kConsoleRight 250
#define kConsoleBottom 250

#define kSleepTime 32767

//Global Variables Definition
WindowRef theConsole; // Reference to the Console
WindowRef theWindow; // Reference to the Window
Boolean gQuitFlag;
Boolean consoleShown;

/************Prototypes********************************************************************/
void Initialize(void);
void buildConsole();
void buildWindow();
void InitControls(WindowRef);
void CheckError(int,char*,int);
void EventLoop();
/*******************************************************************************************/


int main (int argc, const char * argv[]) {

Initialize();
EventLoop();
return 0;
}


void Initialize (void) // Do one-time-only initialization
{
OSStatus err;

buildWindow();
buildConsole();

ShowWindow(theWindow); // Display window on the screen
ShowWindow(theConsole); // Display console on the screen

//InitCursor(); // Set standard arrow cursor

} /* end Initialize */


void buildConsole()
{
WindowAttributes consoleAttrs; // Console attribute flags
Rect consoleRect; // Boundary of content region
OSStatus err;

consoleAttrs = kWindowStandardFloatingAttributes;
// consoleAttrs = kWindowStandardDocumentAttributes
// | kWindowHideOnSuspendAttribute; //Console will hide when the application is not selected
SetRect (&consoleRect, kConsoleLeft, kConsoleTop, // Set content rectangle
kConsoleRight, kConsoleBottom);
//err=CreateNewWindow (kDocumentWindowClass, consoleAttrs, &consoleRect, &theConsole);
err=CreateNewWindow (kFloatingWindowClass, consoleAttrs, &consoleRect, &theConsole);
CheckError(err,"Console Creation",1);
SetWindowTitleWithCFString (theConsole, CFSTR("VRT Console")); // Set title
}/* end buildConsole */

void buildWindow()
{
WindowAttributes windowAttrs; // Window attribute flags
Rect contentRect; // Boundary of content region
OSStatus err;

windowAttrs = kWindowStandardDocumentAttributes // Standard document window
| kWindowStandardHandlerAttribute; // Use standard event handler
SetRect (&contentRect, kWindowLeft, kWindowTop, // Set content rectangle
kWindowRight, kWindowBottom);
err=CreateNewWindow (kDocumentWindowClass, windowAttrs, // Create the window
&contentRect, &theWindow);

CheckError(err,"Window Creation",0);
SetWindowTitleWithCFString (theWindow, CFSTR("VRT Simulation")); // Set title

InitControls(theWindow);
} /* end buildWindow */


void InitControls(WindowRef win)
{
Rect button1Bounds,button2Bounds;
ControlRef rootControl;
ControlRef button1,button2;
OSStatus err;

SetRect(&button1Bounds,100,100,200,200);
SetRect(&button2Bounds,210,100,300,200);

err=CreateRootControl(win,&rootControl);
CheckError(err,"InitControl",0);
err=CreatePushButtonControl(win,&button1Bounds,CFSTR("Push me"),&button1);
CheckError(err,"InitControl",0);
err=CreatePushButtonControl(win,&button2Bounds,CFSTR("Quit"),&button2);
CheckError(err,"InitControl",0);
ShowControl(button1);
ShowControl(button2);
}/* end InitControls */

void CheckError(int theError,char* messageToPrepend,int force)
{
if((theError!=noErr && DEBUG) || force)
{
printf(messageToPrepend);
printf(" : Error %d occured\n",theError);
}
}/* end CheckError */

void EventLoop() //Terribly unefficient, but working for my purpose
//I'm not worrying at the moment about the event handling, but more about the design
{
Boolean gotEvent;
EventRecord event;

gQuitFlag = false;
int count=0;

do
{
gotEvent = WaitNextEvent(everyEvent, &event, kSleepTime, nil);

if (gotEvent)
printf("(%d) I'm supposed to do an event\n",++count);
if(count>10)gQuitFlag=true;
} while (!gQuitFlag);
printf("EventLoop exiting\n");
}
 
Back
Top