June 95 - Macintosh Q & A
Macintosh Q & A
Q I'm using the TPopup class in MacApp 3.0.1 in my window and I want to underline
the title string of a pop-up menu programmatically. The title text style is stored in a
field in the class but is used only when the pop-up menu is first created. How can I
change the text style of a pop-up menu title after it has been created?
AMacApp's TPopup class is basically just a wrapper around System 7's Popup CDEF
(with its own CDEF for pre-System 7) and so is subject to the same limitations as
normal System 7 pop-up menus. You're correct that the title style is stored in the
TPopup class and referenced only once, when the pop-up control is created. What
happens is that when a pop-up menu is created with NewControl, the Popup CDEF
interprets the value parameter to NewControl to be the title style of the pop-up menu
control. Thereafter, the value of the control is equal to the currently selected menu
item. TPopup::CreateCMgrControl calls NewControl as follows:
ControlHandle aCMgrControl = NewControl(itsPort, qdArea, itsTitle,
FALSE, (short) this->GetPopupTitleStyle(), fMenuID,
fItemOffset, this->GetProcID(), fUseAddResMenuResType);
The important setting is the title style: notice the TPopup::GetPopupTitleStyle call,
which returns a short integer corresponding to the text style settings. The problem is
that there's no way of defining this title style after the control has been created, so you
have to recreate the control when you want to change the title style. This may seem a
bit much, but it takes only a few lines of code. The important thing to remember is that
most of the information you need is already part of TPopup; all you're doing is
recreating the control.
Dispose of the old control, set the fTitleStyle field to the title style you want, and then
call CreateCMgrControl to create a new control with this title style, using all the
characteristics already set in your TPopup object. Here's the code to do this:
CStr255 itsLabel;
short itsVal;
/* First free the old control. */
DisposeControl(myPopup->fCMgrControl);
myPopup->fCMgrControl = NULL;
/* Now set the pop-up title style to underline. */
myPopup->fTitleStyle = myPopup->fTitleStyle + underline;
/* Get title and current value to send to CreateCMgrControl. */
myPopup->GetMenuLabel(itsLabel);
itsVal = myPopUp->GetCurrentItem();
/* Now create a new control with the desired text style. */
myPopup->CreateCMgrControl(itsLabel, itsVal, 0, 0, 0);
Q I'm having a problem with Balloon Help, getting HMCompareItem to work properly.
I've got several menu items that can change dynamically, and while HMCompareItem
successfully finds the first item, all other items have no balloons. What's the problem
here?
AThe problem is that the match string isn't exact. HMCompareItem only finds exact
matches for the actual menu items. (A common case to look out for is ellipsis (...)
versus three periods (...): always use an ellipsis in menus.)
If you can't determine the exact match ahead of time, we suggest that you use a
different technique: modify the help string on the fly. A method that other developers
have used is to store the current menu state in their preferences file along with the
current menu help string; then, as the application changes menu items, they modify
the 'STR ' resource that the help item refers to on the fly.
Q I'm writing a QuickDraw GX printer driver and need to get the text size of a shape.
I've tried GXGetStylePenSize and GXGetShapePenSize, but these continuously send back
12 no matter what the real size is. I've looked through the shapes in GraphicsBug, and
12 is there for the text size. What can I do to get the correct size?
AQuickDraw GX has three different shapes to handle typography -- text, glyph, and
layout -- and each one stores the typographic style objects (which are what you need)
differently. (There's a good discussion of the three types of typographic shapes in
QuickDraw GX: Programmer's Overview on pages 97 through 115.)
The important thing to remember is that simple text shapes can have only one type
style (attached to the style attribute of the object), so they're fairly easy to work
with. However, glyph and layout shapes can have one or more runs of type styles
(attached to the style list attribute of the object's geometry), so they can be more
complex to work with. Only if a glyph or layout shape doesn't have a style list attached
to its geometry is the style attribute of the object itself used. For shapes with multiple
style runs, there's no simple answer to the question "What is the text size of this
object?
For glyph and layout shapes, you'll need to write a "GetSizes" function that's capable of
returning one or more sizes. This routine should get the style list by calling
GXGetGlyphShapeParts or GXGetLayoutShapeParts. If the style list is nil, return the
default size in the style attribute of the object itself; otherwise, return an array of
each size in the list of styles, or whatever is appropriate for your application.