PDA

View Full Version : Problem with Carbon Window Updating




Soulstorm
Mar 31, 2006, 05:13 AM
I have made a simple window in my nib file. And I want it to display a string and update the window every time it needs to.
here is my code:
#include <iostream>
#include <Carbon/Carbon.h>
using namespace std;


pascal OSStatus WindowEventHandler(EventHandlerCallRef handlerRef,
EventRef event, void *userData);
void UpdateWindow(WindowRef window);



//**********************************
int main(int argc, char* argv[]){
IBNibRef nibRef;
WindowRef window;
OSStatus err;

EventTargetRef target;
EventHandlerUPP handlerUPP;

EventTypeSpec windowEvent;
windowEvent.eventClass = kEventClassWindow;;
windowEvent.eventKind = kEventWindowDrawContent;

err = CreateNibReference(CFSTR("main"), &nibRef);
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
err = CreateWindowFromNib(nibRef,CFSTR("MainWindow"), &window);
DisposeNibReference(nibRef);

target = GetWindowEventTarget(window);
handlerUPP = NewEventHandlerUPP(WindowEventHandler);
InstallEventHandler(target, handlerUPP, 1, &windowEvent, (void *) window,NULL);
ShowWindow(window);

RunApplicationEventLoop();

return 0;
}

//**********************************************

pascal OSStatus WindowEventHandler(EventHandlerCallRef handlerRef,
EventRef event, void *userData)
{
cout << "entered the function\n";
OSStatus result = eventNotHandledErr;
UInt32 eventKind;
WindowRef window;

window = (WindowRef)userData;

eventKind = GetEventKind(event);

if(eventKind == kEventWindowDrawContent)
{
UpdateWindow(window);
}
return result;
}

void UpdateWindow(WindowRef window)
{
cout << "updatewindow\n";
FMFontFamily fontFamily;
SetPortWindowPort(window);

fontFamily = FMGetFontFamilyFromName("\pTimes");
TextFont(fontFamily);
TextFace(bold + italic);
TextSize(24);
MoveTo(30,60);
DrawString("\pThis is drawn from code!");
}



The problem is that no string is drawn inside the window. Xcode's console doesn't even display the messages I have put that show that the command event handler has been activated. Can someone show me what am I doing wrong?

I write this code from a book about carbon programming. Perhaps the fault lies in that the book is from 2001?



szymczyk
Mar 31, 2006, 12:49 PM
You haven't installed the standard window event handler. If you don't install the standard event handler, you won't get the kEventWindowDrawContent event so your UpdateWindow() function will never get called.

To install the standard event handler, call the function InstallStandardEventHandler().

err = InstallStandardEventHandler(target);

Soulstorm
Mar 31, 2006, 01:42 PM
#include <iostream>
#include <Carbon/Carbon.h>
using namespace std;


pascal OSStatus WindowEventHandler(EventHandlerCallRef handlerRef,
EventRef event, void *userData);
void UpdateWindow(WindowRef window);



//**********************************
int main(int argc, char* argv[]){
IBNibRef nibRef;
WindowRef window;
OSStatus err;

EventTargetRef target;
EventHandlerUPP handlerUPP;

EventTypeSpec windowEvent;
windowEvent.eventClass = kEventClassWindow;;
windowEvent.eventKind = kEventWindowDrawContent;

err = CreateNibReference(CFSTR("main"), &nibRef);
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
err = CreateWindowFromNib(nibRef,CFSTR("MainWindow"), &window);
DisposeNibReference(nibRef);

target = GetWindowEventTarget(window);
handlerUPP = NewEventHandlerUPP(WindowEventHandler);
err = InstallStandardEventHandler(target);

InstallEventHandler(target, handlerUPP, 1, &windowEvent, (void *) window,NULL);
ShowWindow(window);

RunApplicationEventLoop();

return 0;
}

//**********************************************

pascal OSStatus WindowEventHandler(EventHandlerCallRef handlerRef,
EventRef event, void *userData)
{
cout << "entered the function\n";
OSStatus result = eventNotHandledErr;
UInt32 eventKind;
WindowRef window;

window = (WindowRef)userData;

eventKind = GetEventKind(event);

if(eventKind == kEventWindowDrawContent)
{
UpdateWindow(window);
}
return result;
}

void UpdateWindow(WindowRef window)
{
cout << "updatewindow\n";
FMFontFamily fontFamily;
SetPortWindowPort(window);

fontFamily = FMGetFontFamilyFromName("\pTimes");
TextFont(fontFamily);
TextFace(bold + italic);
TextSize(24);
MoveTo(30,60);
DrawString("\pThis is drawn from code!");
}
Nope. It still doesn't work. Are you sure it works??

Also, my book doesn't mention that. Was it added in later versions? I have a book that is from 2001.

In what occasions must I use the function you told me?

szymczyk
Mar 31, 2006, 03:19 PM
Nope. It still doesn't work. Are you sure it works??

Also, my book doesn't mention that. Was it added in later versions? I have a book that is from 2001.

In what occasions must I use the function you told me?

You must call InstallStandardEventHandler() if you want to use Apple's standard event handlers, which you normally want to do. To receive kEventWindowDrawContent events, you must install the standard window event handler.

Looking at your WindowEventHandler() function, I noticed that the code does not change the value of the variable result. Your event handler always returns eventNotHandledErr. You should change result to noErr after calling UpdateWindow().

Is your UpdateWindow() function getting called? If it is, you have a problem in your text drawing code. I have never written any code to write text in a Mac window so I cannot help you solve that problem.

If the UpdateWindow() function is not being called, you have two possible problems. The first possible problem is a problem in the event handler, either in the code itself or the code to install the handler. The second possible problem is that your program is not generating kEventWindowDrawContent events.

If you need more help with Carbon Events, read this article (http://www.meandmark.com/keyboardpart1.html) I wrote. Although the article covers reading the keyboard, it provides an introduction to Carbon Events that may help you solve your problem.

Soulstorm
Mar 31, 2006, 04:25 PM
This is getting weirder and weirder.
Looking at your WindowEventHandler() function, I noticed that the code does not change the value of the variable result. Your event handler always returns eventNotHandledErr. You should change result to noErr after calling UpdateWindow().still doesn't work
Is your UpdateWindow() function getting called? If it is, you have a problem in your text drawing code. I have never written any code to write text in a Mac window so I cannot help you solve that problem.No it's not called.
If the UpdateWindow() function is not being called, you have two possible problems. The first possible problem is a problem in the event handler, either in the code itself or the code to install the handler. The second possible problem is that your program is not generating kEventWindowDrawContent events. I wish I knew how to fix that.

Anyway, can you provide me with any reference to Carbon development? I would appreciate it if you recommended me any book too!

szymczyk
Mar 31, 2006, 04:35 PM
Anyway, can you provide me with any reference to Carbon development? I would appreciate it if you recommended me any book too!
Unfortunately, there are no modern Carbon programming books. I would recommend reading Apple's Carbon documentation, especially the material on Carbon Events.

To find the source of your problem, set breakpoints at the start of your main, WindowEventHandler and UpdateWindow functions. Walk through your code line by line in the debugger. Your program is small enough that walking through your code will find the problem.