Events
I have a problem with my the mouse down events in my program. It does not quit the program when the close box in the window is clicked.It also does not work if you click in the drag region. Please Help me:
Code:
int main(int argc, char* argv[])
{
WindowRef window;
WindowRef window1;
EventRecord Event;
OSStatus err;
IBNibRef nibRef;
EventRef EvntRef;
EventHandlerUPP handlerUPP;
Boolean gotEvent;
err= CreateNibReference(CFSTR("main"), &nibRef);
err = SetMenuBarFromNib(nibRef,CFSTR("MenuBar"));
err = CreateWindowFromNib(nibRef, CFSTR("Window2"), &window1);
err= CreateWindowFromNib(nibRef,CFSTR("MainWindow"),&window);
ShowWindow(window);
ShowWindow(window1);
SetPortWindowPort(window1);
gQuit=false;
Item item(10,"Hello","Jan 30 2005",1);
SetUpFile("Stock.txt");
WriteItem(item);
while(!gQuit)
{
gotEvent = WaitNextEvent(everyEvent,&Event,MAX_UNIT32,NULL);
if(gotEvent)
{
doEvents(&Event);
}
}
return 0;
}
void doEvents(EventRecord * Event)
{
SInt32 menuChoice;
MenuID menuID;
MenuItemIndex MenuItem;
switch(Event->what)
{
case mouseDown:
doMouseDown(Event);
std::cout << "Mouse Down Event"<<std::endl;
break;
case keyDown:
if ((Event->modifiers & cmdKey) !=0)
{
menuChoice=MenuEvent(Event);
menuID =HiWord(menuChoice);
MenuItem = LoWord(menuChoice);
if (menuID==mFile && MenuItem== iQuit)
gQuit=true;
}
break;
case updateEvt:
break;
case autoKey:
break;
case activateEvt:
break;
case osEvt:
break;
case mouseUp:
break;
}
}
void doMouseDown(EventRecord *EvtRec)
{
WindowPartCode partCode;
WindowRef windowRef;
SInt32 menuChoice;
Rect constraintRect, mainScreenRect;
Point standardStateHeightAndWidth;
partCode= FindWindow(EvtRec->where,&windowRef);
switch (partCode)
{
case inMenuBar:
menuChoice=MenuSelect(EvtRec->where);
doMenuChoice(menuChoice);
break;
case inContent:
if (windowRef!=FrontWindow())
SelectWindow(windowRef);
break;
case inDrag:
DragWindow(windowRef,EvtRec->where,NULL);
gQuit=true;
break;
case inGoAway:
if (TrackGoAway(windowRef,EvtRec->where))
gQuit=true;
break;
case inGrow:
constraintRect.top =75;
constraintRect.left =205;
constraintRect.bottom = constraintRect.right = 32767;
ResizeWindow(windowRef,EvtRec->where,&constraintRect,NULL);
gQuit=true;
break;
}
}
Good grief, it's a long time since I've seen any ancient code like that!
These days you should be using RunApplicationEventLoop rather than writing your own from scratch, then all these things will "just work".
These days you should be using RunApplicationEventLoop rather than writing your own from scratch, then all these things will "just work".
you install event handlers for the ones you care about.
documentation is here: http://developer.apple.com/documentation...index.html
documentation is here: http://developer.apple.com/documentation...index.html
Yup. But you shouldn't care about OS9. That has destroyed my life.
lol. THe thing i dont understand is how the Carbon Event Manager sends the information about where the event happens and such. Like when you have a mouse down event with WaitNextEvent() you can take all the information needed from the EventRecord such as the regoin it is in?
when you register to receive a type of event, you give it a function to call. When that function is called, one of the things it's passed is an EventRef containing all the information you'll ever need.
seriously, read the docs.
seriously, read the docs.
I have read through the document but i con't get an understanding of Event Classes and Kinds. If any body could explain it it would be of great help.
class is just a general grouping, kind is the specific kind of event.
For example, the event class kEventClassKeyboard contains the event kind kEventRawKeyDown. For a full list see /System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/Headers/CarbonEvents.h (or it's probably in the docs, too).
For example, the event class kEventClassKeyboard contains the event kind kEventRawKeyDown. For a full list see /System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/Headers/CarbonEvents.h (or it's probably in the docs, too).
OOOOO i understand. each class has a group of constants that represent different event kinds.
Thank you So much for your help i am starting to understand the carbon event manger alot better now. Can you Help me with one problem with buttons? This Program does not issue an erro but then quits when you run it and issues this error:
TestApp has exited due to signal 10 (SIGBUS).
Thank you ver Much for all your help.
Thank you So much for your help i am starting to understand the carbon event manger alot better now. Can you Help me with one problem with buttons? This Program does not issue an erro but then quits when you run it and issues this error:
TestApp has exited due to signal 10 (SIGBUS).
Code:
#include <Carbon/Carbon.h>
#include <iostream.h>
void doControlHit(WindowRef &,EventRef &);
OSStatus doGetControls(WindowRef& windowRef);
typedef struct
{
ControlRef Button;
ControlRef Button1;
}DocStruct,**DocStructHandle;
OSStatus WindowEventHandler(EventHandlerCallRef inHandlerCallRef,
EventRef inEvent,
void* inUserData);
void doNewWindow(WindowRef &);
int main()
{
IBNibRef nibRef;
WindowRef window;
OSStatus err;
err = CreateNibReference(CFSTR("main"), &nibRef);
require_noerr( err, CantGetNibRef );
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
require_noerr( err, CantSetMenuBar );
doNewWindow(window);
DisposeNibReference(nibRef);
// The window was created hidden so show it.
ShowWindow( window );
// Call the event loop
RunApplicationEventLoop();
CantCreateWindow:
CantSetMenuBar:
CantGetNibRef:
return err;
}
void doNewWindow(WindowRef & window)
{
OSStatus err;
IBNibRef nibRef;
EventHandlerUPP handlerUPP;
EventTypeSpec eventType[]= { { kEventClassWindow , kEventWindowClose },
{ kEventClassControl , kEventControlHit } };
handlerUPP=NewEventHandlerUPP(WindowEventHandler);
err = CreateNibReference(CFSTR("main"), &nibRef);
require_noerr( err, CantGetNibRef );
err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
require_noerr( err, CantCreateWindow );
err = InstallWindowEventHandler(window, handlerUPP,
GetEventTypeCount(eventType), eventType,
NULL, NULL);
require_noerr(err , CantInstallHandler);
err=doGetControls(window);
require_noerr(err , CantCreateControls);
CantCreateControls:
CantGetNibRef:
CantCreateWindow:
CantInstallHandler:
return;
}
OSStatus WindowEventHandler
(EventHandlerCallRef inHandlerCallRef,
EventRef inEvent,
void* inUserData)
{
OSStatus err;
UInt32 EventClass=GetEventClass(inEvent);
UInt32 EventKind=GetEventKind(inEvent);
EventRecord EventRec;
Point MouseLocation;
ControlPartCode controlPartCode;
ControlRef controlRef;
ConvertEventRefToEventRecord(inEvent,&EventRec);
WindowRef windowRef;
switch (EventClass)
{
case kEventClassWindow:
switch(EventKind)
{
case kEventWindowClose:
QuitApplicationEventLoop();
break;
}
break;
case kEventClassControl:
switch(EventKind)
{
case kEventControlHit :
std::cout << "\nHit Control!\n";
GetEventParameter(inEvent,kEventParamWindowRef,typeWindowRef,NULL,
sizeof(windowRef),NULL,&windowRef);
GetEventParameter(inEvent,kEventParamDirectObject,typeControlRef,NULL,
sizeof(controlRef),NULL,&controlRef);
DocStructHandle docStruct=(DocStructHandle) (GetWRefCon(windowRef));
if (controlRef==(*docStruct)->Button)
{
std::cout << "\nWorked!\n";
}
break;
}
break;
}
}
OSStatus doGetControls(WindowRef& windowRef)
{
ControlRef controlRef;
DocStructHandle docStructHDL= (DocStructHandle) (GetWRefCon(windowRef));
OSStatus err;
Rect ControlRect;
SetRect(&ControlRect,63,213,132,233);
err = CreatePushButtonControl(windowRef,&ControlRect,CFSTR("Cancel"),
&(*docStructHDL)->Button);
std::cerr << "\nError!";
SetRect(&ControlRect,144,213,213,233);
err = CreatePushButtonControl(windowRef,&ControlRect,CFSTR("OK"),
&(*docStructHDL)->Button1);
std::cerr << "\nError!";
return err;
}Thank you ver Much for all your help.

