\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#264: Add Function to Retrieve the Interpreter of a Window}
\date{April 01, 2006}
\author{George Petasis}
\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 \#264}&\textbf{Add Function to Retrieve the Interpreter of a Window}\\\hline
Author&

George Petasis \tipmail{petasis}{iit.demokritos.gr}
\\
Created&Saturday, \(\text{1}^{\text{st}}\) April 2006\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.8 $\\
Keywords&Tk, C API\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes the addition of a new function in the Tk public API, for retrieving a pointer to the Tcl interpreter that was used for creating a window.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

During the development of a Tk extension that adds a ClientMessage handler under unix (tkdnd), I needed to get the pointer of the Tcl interpreter that is associated with a window. When the ClientMessage handler is called, only a Tk\_Window pointer is passed, for the window the ClientMessage is for. But if you want to execute Tcl code, you don't have a Tcl interpreter...

Of course, you can try use any (cached) interpreter, but this can lead to various problems, if it is not the interpreter that was used for creating the window. Since Tk already has this information, adding a function to return the associated interpreter for a Tk\_Window pointer will be relatively easy.

\section{Proposed Change}

A new public function (with signature \textit{Tcl\_Interp *} \textbf{Tk\_Interp}\textit{(Tk\_Window tkwin)}) is proposed to be added to the public C API of Tk. This function can be implemented as follows:
\begingroup\small\begin{verbatim}
 Tcl_Interp *
 Tk_Interp(Tk_Window tkwin) {
     if (tkwin != NULL && ((TkWindow *) tkwin)->mainPtr != NULL) {
         return ((TkWindow *) tkwin)->mainPtr->interp;
     }
     return NULL;
 }
\end{verbatim}
\endgroup

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
