\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#51: Native Menubutton on Macintosh}
\date{August 04, 2001}
\author{Mats Bengtsson}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#51}&\textbf{Native Menubutton on Macintosh}\\\hline
Author&

Mats Bengtsson \tipmail{matben}{privat.utfors.se}
\\
Created&Saturday, \(\text{4}^{\text{th}}\) August 2001\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Withdrawn\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This is a replacement for the menubutton on the Macintosh with a native implementation which is compliant with the Appearance Manager in Mac OS 8 and later.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

The present (in 8.3.3 and earlier) menubutton on the Macintosh is implemented using Tk drawing to draw something similar to the native menubutton on Mac how it looks on Pre Mac OS 8.0 systems, and therefore fails to give the correct appearance on Mac OS 8.0 systems and later. This TIP presents a step to increase the native appearance on the Macintosh (similar to \cite{tip25}.)

\begin{figure}[htbp]\begin{center}\leavevmode
\tipimage{51compare}{414pt}
\caption{Comparison of Native (to left) and Standard Menu Buttons.}
\end{center}\end{figure}

\section{Reference Implementation}

The proposed change is now implemented as a loadable extension (in C) on Macintosh, and can be downloaded [\url{http://hem.fyristorg.com/matben/download/MacMenuButton.sit}]. This implementation differs from the other buttons in Mac Tk (button, radiobutton, checkbutton), which use a mixture of native Apple drawing code and Tk drawing code, since it uses only native Apple code for drawing. This extension requires Tcl/Tk 8.3.2p1 or later due to the changed stub loading mechanism. The new implementation is not a complete replacement since it lacks the \textit{-bitmap} and \textit{-image} options, and a few other things, see below.

The changes necessary are:
\begin{itemize}
\item{}
Replace the \textit{tkMacMenubutton.c} file with the new one.
\item{}
Add a MENU resource item, which is included in the shared library, but needs to be added to the core.
\item{}
Modifications to \textit{tkMacFont.c} (see appendix). Put declaration so it can be used from any file. Possibly also add the new function to the stub table since it can be practical for other extension writers.
\item{}
Need to check for the presence of the Appearance manager:
\begingroup\small\begin{verbatim}
if (TkMacHaveAppearance()) 
   use native (new) menubutton 
else 
   use present menubutton
\end{verbatim}
\endgroup
\end{itemize}

All functionality from the documentation that is applicable is implemented in the extension, with some exceptions:
\begin{itemize}
\item{}
The \textit{-image} and \textit{-bitmap} options are not supported, yet.
\item{}
There is no button pressed (SELECTED) flag so it highlights when the mouse enters, just as a reminder that it must be fixed. (see appendix)
\item{}
Don't know which color to pick for the three pixels in each corner. It is now the \textit{-background} color, but the ordinary button uses \textit{-highlightbackground}?
\item{}
The position of the popup menu should be changed in order to conform better with standard Mac appearance..
\item{}
Something needs to be done so that we can get Mac native font stuffs from a \textit{Tk\_Font} object; I've included a crude hack in the appendix.
\item{}
It is compliant to the Appearance Manager which means that foreground and background colors are set via themes and not from command switches.
\item{}
Minor differences to comply with the Appearance Manager.
\end{itemize}

All these deviations are consistent with the look-and-feel of Mac OS 8.0 and on. Existing scripts using menubutton are compatible with the new menubutton.

Open questions: 
\begin{itemize}
\item{}
Option to use for the color of the corner pixels.
\item{}
If (and how) a SELECTED flag should be added to \textit{tkMenuButton.h}, and code to support it in \textit{tkMenuButton.c}.
\item{}
Implementation of the \textit{-bitmap} and \textit{-image} options.
\item{}
A \textit{-compound} option as described in TIP \#11.
\end{itemize}

\section{Copyright}

This document has been placed in the public domain.

\section{Appendix}
\begin{itemize}
\item{}
Addition to \textit{tkMenuButton.h}:
\begingroup\small\begin{verbatim}
#define SELECTED		8
\end{verbatim}
\endgroup

Other modifications to tkMenuButton.c must be made to support this flag.
\item{}
Addition to \textit{tkMacFont.c} (possibly add to exported functions):
\begingroup\small\begin{verbatim}
/*
 *---------------------------------------------------------------------------
 *
 * GetMacFontAttributes -- 
 *
 *      Takes a Tk_Font and gets the Mac font attributes faceNum, size, and style.
 *      Note that the Mac font size is in pixels while the Tk_Font size is
 *      in points. No need to do any UTF-8 translations since this is
 *      implicit in GetFamilyOrAliasNum().
 *      The code here is essentially a modified TkpGetFontFromAttributes() and
 *      InitFont(), both from tkMacFont.c.
 *
 * Results:
 *      Sets the Mac font attributes.
 *
 * Side effects:
 *      None.
 *
 *---------------------------------------------------------------------------
 */
void
GetMacFontAttributes(
        Tk_Window tkwin,        /* Tk window. (in) */
        Tk_Font tkFont,         /* Tk font. (in) */
        short *faceNumPtr,      /* Mac font face id. (out) */
        short *macSizePtr,      /* Mac font size in pixels. (out) */
        Style *stylePtr)        /* Mac font style specifier. (out) */
{
    int i, j;
    char *faceName, *fallback;
    char ***fallbacks;
    MacFont *fontPtr;
    const TkFontAttributes *faPtr;
    int size;           /* Size in points. */
        
    /*
     * This is just a macro to access the attribute struct member.
     */
     
    faPtr = GetFontAttributes(tkFont);

    /*
     * Algorithm to get the closest font to the one requested.
     *
     * try fontname
     * try all aliases for fontname
     * foreach fallback for fontname
     *      try the fallback
     *      try all aliases for the fallback
     */
     
    *faceNumPtr = 0;
    faceName = faPtr->family;
    if (faceName != NULL) {
        if (GetFamilyOrAliasNum(faceName, faceNumPtr) != 0) {
            goto found;
        }
        fallbacks = TkFontGetFallbacks();
        for (i = 0; fallbacks[i] != NULL; i++) {
            for (j = 0; (fallback = fallbacks[i][j]) != NULL; j++) {
                if (strcasecmp(faceName, fallback) == 0) {
                    for (j = 0; (fallback = fallbacks[i][j]) != NULL; j++) {
                        if (GetFamilyOrAliasNum(fallback, faceNumPtr)) {
                            goto found;
                        }
                    }
                }
                break;
            }
        }
    }
    
    found:    
    *stylePtr = 0;
    if (faPtr->weight != TK_FW_NORMAL) {
        *stylePtr |= bold;
    }
    if (faPtr->slant != TK_FS_ROMAN) {
        *stylePtr |= italic;
    }
    if (faPtr->underline) {
        *stylePtr |= underline;
    }
    if (faPtr->size == 0) {
        size = -GetDefFontSize();
    } else {
        size = faPtr->size;
    }
    *macSizePtr = (short) TkFontGetPixels(tkwin, size);
}
\end{verbatim}
\endgroup
\end{itemize}

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#25]{tip25}
Mats Bengtsson,
\emph{Native tk\_messageBox  on Macintosh},
on-line at \url{http://purl.org/tcl/tip/25.html}
\end{thebibliography}
\end{document}
