[comp.sys.mac.programmer] Windows Menu Code

djnowak@luna.dpl.scg.hac.com (David Nowak) (09/08/89)

Does anyone out there have a segment of code to create a Windows menu (like 
Microsoft Word and other programs have)?  I need an example.  I can figure
out how to add the names of the Windows items to the windows menu, but
when I close the windows, the names do not go away.  I am using the Toolbox
call FrontWindow to get a windowptr to the top window and CloseWindow to
close the window that windowptr points to.  However, the list of windows
under my Windows menu is not properly reflecting the window that was closed.

Thanks in advance.

David Nowak
djnowak@luna.dpl.scg.hac.com
.

earleh@eleazar.dartmouth.edu (Earle R. Horton) (09/08/89)

In article <4848@hacgate.UUCP> djnowak@luna.dpl.scg.hac.com (David Nowak) writes:
>Does anyone out there have a segment of code to create a Windows menu (like 
>Microsoft Word and other programs have)?  I need an example.  I can figure
>out how to add the names of the Windows items to the windows menu, but
>when I close the windows, the names do not go away.

You have to do something to make the contents of the windows menu
track the windows whose names you want in it.  One approach is to
rebuild the windows menu every time you call MenuSelect, just before
calling the trap, from scratch.  I am guessing that your current code
is sufficient here, but that you don't rebuild the windows menu often
enough.  This is the easiest way to make sure that the contents of the
menu are correct, and it doesn't noticeably affect performance unless
you have a large number of names in the menu ("large" is maybe two
dozen or more).  This approach is good to use if your windows menu
ordering follows the front-to-back ordering of the windows, and if you
desire to present an unembellished list.

It is also possible to update the windows menu every time you add or
remove a window, or to devise some scheme whereby you keep the windows
menu current at all times.  This approach seems at first glance to be
more complicated, but it may have advantages if the order you wish to
use differs from the order in which the windows appear on the screen.
You might want to have modeless dialogs appear first, documents
second, or you might want to sort document windows according to the
order in which they were opened.  If you have more than one document
type, then it seems appropriate to group window names by associated
document type first and by some other criterion second.  If you use
such an approach, then it may be more natural to update the windows
menu when you manage your windows and not at MenuSelect time.

There is one pitfall to avoid when maintaining a dynamic menu whose
item titles are defined by the user.  Assume that any character may
appear in a document or window name.  This includes menu meta
characters and leading hyphens.  Never use AppendMenu to add a window
name to the menu directly.  Rather, add an item containing an
innocuous string using AppendMenu, then set the text of the new item
to the target string using SetItem.  This allows for direct insertion
of meta characters without interpretation.  If there is any
possibility that the desired name begins with a hyphen ('-') then you
can build a menu item string consisting of the concatenation of a
string containing a NUL byte and the desired name.  Otherwise, the
name appears as a meaningless gray line.

Earle R. Horton