How to access a graphics context for a view?

goldenmeg

Registered
I'm lost. I know this must be simple, but I can't find it. All the documentation and sample code I have looked at assumes that I will be drawing in a view after a kEventControlDraw event and the context is retrieved from the GetEventParameter. I want to simply draw in a window after the user selects a menu item. This generates a command event - not a draw event?

How can I draw to a window controled by a menu command event? I can definie an HIView - but how do I get the graphics context from the HIView?

Thanks, Meg
 
To handle menu selections in Carbon, you must write an event handler. The event handler calls GetEventParameter() to determine the menu command (menu item) the user selected.

The event handler's third argument is a pointer to data the event handler needs to do its job. You supply the data when installing the event handler. What you want to supply is a pointer to your window or a pointer to a data structure that lets you retrieve the window you want to draw to.

In your event handler code, you want to check for the menu command that tells the window to draw. When that command occurs, retrieve the window and draw to it.

For more information on Carbon events, go to the URL in my signature. I wrote an article about reading the keyboard with Carbon events. It provides a more in-depth explanation on Carbon events than I can provide in a forum post.
 
Thanks - this is helpful.

Once in the event handler, I get the window:
WindowRef window = (WindowRef)inUserData;

If it is a kEventCommandProcess I call my doCommand procedure.

in my doCommand procedure I get the command by using:
GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(HICommand), NULL, &command );

At the appropriate command.commandID:
Once I get my window in my event handler do I get the graphics context by using:

QDBeginCGContext(GetWindowPort(window), &ctx); ????

I tried using:
err = GetEventParameter (inEvent,
kEventParamCGContextRef,
typeCGContextRef,
NULL,
sizeof (CGContextRef),
NULL,
&ctx);
but this returns an error.

Can I call the GetEventParameter() as above in my doCommand loop or do I need to use the QDBeginCGContext() on the window. I ask this because this procedure is described in the QuickDraw reference and I thought the point was to drop QuickDraw and use Quartz. I just can't find a similar Quartz procedure that gets the context from a window?
 
goldenmeg said:
I tried using:
err = GetEventParameter (inEvent,
kEventParamCGContextRef,
typeCGContextRef,
NULL,
sizeof (CGContextRef),
NULL,
&ctx);
but this returns an error.

Can I call the GetEventParameter() as above in my doCommand loop
No because your doCommand loop handles only command events. Other events won't get handled.

You need to call the function HIViewSetNeedsDisplay(). This function tells the operating system that the view needs to be drawn. The operating system will generate a kEventControlDraw event for you. Your kEventControlDraw event handler will then do the drawing.

More information is available in the HIView Programming Guide and HIView Reference documents, which are part of Apple's Carbon documentation.
 
Back
Top