Blame tutorial.sgml

Packit 4d380f
Packit 4d380f
Packit 4d380f
<article>
Packit 4d380f
Packit 4d380f
<artheader>
Packit 4d380f
<title>Writing Programs Using <literal remap="tt">newt</literal></title>
Packit 4d380f
<author> 
Packit 4d380f
<firstname>Erik Troan, <ewt@redhat.com></firstname>
Packit 4d380f
</author>
Packit 4d380f
Packit 4d380f
<pubdate>v0.31, 2003-Jan-06</pubdate>
Packit 4d380f
Packit 4d380f
<abstract>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The <literal remap="tt">newt</literal> windowing system is a terminal-based window and widget
Packit 4d380f
library designed for writing applications with a simple, but user-friendly,
Packit 4d380f
interface. While <literal remap="tt">newt</literal> is not intended to provide the rich feature
Packit 4d380f
set advanced applications may require, it has proven to be flexible enough
Packit 4d380f
for a wide range of applications (most notably, Red Hat's installation
Packit 4d380f
process). This tutorial explains the design philosophy behind <literal remap="tt">newt</literal> and
Packit 4d380f
how to use <literal remap="tt">newt</literal> from your programs.
Packit 4d380f
</para>
Packit 4d380f
Packit 4d380f
</abstract>
Packit 4d380f
</artheader>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect1><title>Introduction</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
<literal remap="tt">Newt</literal> has a definite design philosophy behind it, and knowing that design
Packit 4d380f
makes it significantly easier to craft robust <literal remap="tt">newt</literal> applications. This
Packit 4d380f
tutorial documents <literal remap="tt">newt</literal> 0.30 --- older versions of <literal remap="tt">newt</literal> had
Packit 4d380f
annoying inconsistencies in it (which writing this tutorial pointed out),
Packit 4d380f
which were removed while this tutorial was written. The latest version of
Packit 4d380f
<literal remap="tt">newt</literal> is always available from Red Hat.</para>
Packit 4d380f
Packit 4d380f
<sect2><title>Background</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
<literal remap="tt">Newt</literal> was originally designed for use in the install code for
Packit 4d380f
Red Hat Linux. As this install code runs in an environment with limited
Packit 4d380f
resources (most importantly limited filesystem space), <literal remap="tt">newt</literal>'s size
Packit 4d380f
was immediately an issue. To help minimize its size, the following design
Packit 4d380f
decisions were made early in its implementation:
Packit 4d380f
</para>
Packit 4d380f
Packit 4d380f
<itemizedlist>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 <literal remap="tt">newt</literal> does not use an event-driven architecture.
Packit 4d380f
</para>
Packit 4d380f
</listitem>
Packit 4d380f
<listitem>
Packit 4d380f
<para>
Packit 4d380f
 <literal remap="tt">newt</literal> is written in C, not C++. While there has been interest
Packit 4d380f
in constructing C++ wrapper classes around the <literal remap="tt">newt</literal> API, nothing has
Packit 4d380f
yet come of those ideas.</para>
Packit 4d380f
</listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 Windows must be created and destroyed as a stack (in other words, all
Packit 4d380f
<literal remap="tt">newt</literal> windows behave as modal dialogs). This is probably
Packit 4d380f
the greatest functionality restriction of <literal remap="tt">newt</literal>.</para>
Packit 4d380f
</listitem>
Packit 4d380f
<listitem>
Packit 4d380f
<para>
Packit 4d380f
 The tty keyboard is the only supported input device.</para>
Packit 4d380f
</listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 Many behaviours, such as widget traversal order, are difficult
Packit 4d380f
or impossible to change.
Packit 4d380f
</para>
Packit 4d380f
</listitem>
Packit 4d380f
</itemizedlist>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
While <literal remap="tt">newt</literal> provides a complete API, it does not handle the low-level
Packit 4d380f
screen drawing itself. Instead, <literal remap="tt">newt</literal> is layered on top of the screen
Packit 4d380f
management capabilities of John E. Davis's 
Packit 4d380f
<ulink url="ftp://space.mit.edu/pub/davis/slang/">S-Lang</ulink> library.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Designing <literal remap="tt">newt</literal> applications</title>
Packit 4d380f
<para>
Packit 4d380f
As <literal remap="tt">newt</literal> is not event driven and forces modal windows (forcing window
Packit 4d380f
order to behave like a stack), newt applications tend to look quite like
Packit 4d380f
other text-mode programs. It is quite straightforward to convert a command
Packit 4d380f
line program which uses simple user prompts into a <literal remap="tt">newt</literal> application.
Packit 4d380f
Some of the programs run as part of the Red Hat installation process
Packit 4d380f
(such as <literal remap="tt">Xconfigurator</literal> and <literal remap="tt">mouseconfig</literal>) were originally written
Packit 4d380f
as simple terminal mode programs which used line-oriented menus to get
Packit 4d380f
input from the user and were later converted into <literal remap="tt">newt</literal> applications
Packit 4d380f
(through a process affectionately known as newtering). Such a conversion
Packit 4d380f
does not require changes to the control flow of most applications.
Packit 4d380f
Packit 4d380f
Programming <literal remap="tt">newt</literal> is dramatically different from writing programs for
Packit 4d380f
most other windowing systems as <literal remap="tt">newt</literal>'s API is not event driven. This
Packit 4d380f
means that <literal remap="tt">newt</literal> applications look dramatically different from programs
Packit 4d380f
written for event-driven architectures such as Motif, <literal remap="tt">gtk</literal>, or even
Packit 4d380f
Borland's old TurboVision libraries.
Packit 4d380f
Packit 4d380f
When you're designing your <literal remap="tt">newt</literal> program, keep this differentiation
Packit 4d380f
in mind. As long as you plan your application to call a function to
Packit 4d380f
get input and then continue (rather then having your program called
Packit 4d380f
when input is ready), programming with the newt libraries should be
Packit 4d380f
simple.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Components</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Displayable items in <literal remap="tt">newt</literal> are known as <emphasis remap="bf">components</emphasis>, which are
Packit 4d380f
analogous to the widgets provided by most Unix widget sets. There are
Packit 4d380f
two main types of components in <literal remap="tt">newt</literal>, forms and everything else.
Packit 4d380f
Forms logically group components into functional sets. When an application
Packit 4d380f
is ready to get input from a user, it ``runs a form'', which makes the
Packit 4d380f
form active and lets the user enter information into the components the
Packit 4d380f
form contains. A form may contain any other component, including other
Packit 4d380f
forms. Using subforms in this manner lets the application change the details
Packit 4d380f
of how the user tabs between components on the form, scroll regions of the
Packit 4d380f
screen, and control background colors for portions of windows.
Packit 4d380f
Packit 4d380f
Every component is of type <literal remap="tt">newtComponent</literal>, which is an opaque type. It's
Packit 4d380f
guaranteed to be a pointer though, which lets applications move it through
Packit 4d380f
void pointers if the need arises. Variables of type <literal remap="tt">newtComponent</literal> should
Packit 4d380f
never be directly manipulated -- they should only be passed to <literal remap="tt">newt</literal>
Packit 4d380f
functions. As <literal remap="tt">newtComponent</literal> variables are pointers, remember that
Packit 4d380f
they are always passed by value -- if you pass a <literal remap="tt">newtComponent</literal> to
Packit 4d380f
a function which manipulates it, that component is manipulated everywhere,
Packit 4d380f
not just inside of that function (which is nearly always the behaviour
Packit 4d380f
you want).</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Conventions</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
<literal remap="tt">Newt</literal> uses a number of conventions to make it easier for programmers
Packit 4d380f
to use. 
Packit 4d380f
Packit 4d380f
Packit 4d380f
<itemizedlist>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 All functions which manipulate data structures take the data
Packit 4d380f
structure being modified as their first parameter. For example, all
Packit 4d380f
of the functions which manipulate forms expect the <literal remap="tt">newtComponent</literal>
Packit 4d380f
for that form to be the first parameter.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 As <literal remap="tt">newt</literal> is loosely typed (forcing all of the components into
Packit 4d380f
a single variable makes coding easier, but nullifies the value of type
Packit 4d380f
checking), <literal remap="tt">newt</literal> functions include the name of the type they are
Packit 4d380f
manipulating. An example of this is <literal remap="tt">newtFormAddComponent()</literal>, which
Packit 4d380f
adds a component to a form. Note that the first parameter to this function
Packit 4d380f
is a form, as the name would suggest.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 When screen coordinates are passed into a function, the 
Packit 4d380f
x location precedes the y location. To help keep this clear,
Packit 4d380f
we'll use the words ``left'' and ``top'' to describe those indicators (with
Packit 4d380f
left corresponding to the x position).</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 When box sizes are passed, the horizontal width precedes the vertical
Packit 4d380f
width.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 When both a screen location and a box size are being passed, the
Packit 4d380f
screen location precedes the box size.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 When any component other then a form is created, the first two
Packit 4d380f
parameters are always the (left, right) location.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 Many functions take a set of flags as the final parameter. These
Packit 4d380f
flags may be logically ORed together to pass more then one flag at a time.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
 <literal remap="tt">Newt</literal> uses <emphasis remap="bf">callback</emphasis> functions to convey certain events to
Packit 4d380f
the application. While callbacks differ slightly in their parameters, most
Packit 4d380f
of them allow the application to specify an arbitrary argument to be passed
Packit 4d380f
to the callback when the callback is invoked. This argument is always a 
Packit 4d380f
<literal remap="tt">void *</literal>, which allows the application great flexibility.
Packit 4d380f
</para>
Packit 4d380f
</listitem>
Packit 4d380f
</itemizedlist>
Packit 4d380f
</para></sect2></sect1>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect1><title>Basic <literal remap="tt">Newt</literal> Functions</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
While most <literal remap="tt">newt</literal> functions are concerned with widgets or groups
Packit 4d380f
of widgets (called grids and forms), some parts of the <literal remap="tt">newt</literal> API
Packit 4d380f
deal with more global issues, such as initializing <literal remap="tt">newt</literal> or writing
Packit 4d380f
to the root window.</para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Starting and Ending <literal remap="tt">newt</literal> Services</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
There are three functions which nearly every <literal remap="tt">newt</literal> application use. The
Packit 4d380f
first two are used to initialize the system.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
int newtInit(void);
Packit 4d380f
void newtCls(void);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtInit()</literal> should be the first function called by every <literal remap="tt">newt</literal>
Packit 4d380f
program. It initializes internal data structures and places the terminal
Packit 4d380f
in raw mode. Most applications invoke <literal remap="tt">newtCls()</literal> immediately after
Packit 4d380f
<literal remap="tt">newtInit()</literal>, which causes the screen to be cleared. It's not
Packit 4d380f
necessary to call <literal remap="tt">newtCls()</literal> to use any of <literal remap="tt">newt</literal>'s features, but
Packit 4d380f
doing so will normally give a much neater appearance.
Packit 4d380f
Packit 4d380f
When a <literal remap="tt">newt</literal> program is ready to exit, it should call <literal remap="tt">newtFinished()</literal>.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
int newtFinished(void);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtFinished()</literal> restores the terminal to its appearance when
Packit 4d380f
<literal remap="tt">newtInit()</literal> was called (if possible -- on some terminals the cursor will
Packit 4d380f
be moved to the bottom, but it won't be possible to remember the original
Packit 4d380f
terminal contents) and places the terminal in its original input state.
Packit 4d380f
If this function isn't called, the terminal will probably need to be
Packit 4d380f
reset with the <literal remap="tt">reset</literal> command before it can be used easily.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Handling Keyboard Input</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Normally, <literal remap="tt">newt</literal> programs don't read input directly from the
Packit 4d380f
user. Instead, they let <literal remap="tt">newt</literal> read the input and hand it to the
Packit 4d380f
program in a semi-digested form. <literal remap="tt">Newt</literal> does provide a couple of simple
Packit 4d380f
functions which give programs (a bit of) control over the terminal.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtWaitForKey(void);
Packit 4d380f
void newtClearKeyBuffer(void);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The first of these, <literal remap="tt">newtWaitForKey()</literal>, doesn't return until a key
Packit 4d380f
has been pressed. The keystroke is then ignored. If a key is already in
Packit 4d380f
the terminal's buffer, <literal remap="tt">newtWaitForKey()</literal> discards a keystroke and
Packit 4d380f
returns immediately.
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtClearKeyBuffer()</literal> discards the contents of the terminal's input
Packit 4d380f
buffer without waiting for additional input.</para></sect2> 
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Drawing on the Root Window</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The background of the terminal's display (the part without any windows 
Packit 4d380f
covering it) is known as the <emphasis remap="bf">root window</emphasis> (it's the parent of all
Packit 4d380f
windows, just like the system's root directory is the parent of all 
Packit 4d380f
subdirectories). Normally, applications don't use the root window, instead
Packit 4d380f
drawing all of their text inside of windows (<literal remap="tt">newt</literal> doesn't require
Packit 4d380f
this though -- widgets may be placed directly on the root window without
Packit 4d380f
difficulty). It is often desirable to display some text, such as a
Packit 4d380f
program's name or copyright information, on the root window, however.
Packit 4d380f
<literal remap="tt">Newt</literal> provides two ways of displaying text on the root window. These
Packit 4d380f
functions may be called at any time. They are the only <literal remap="tt">newt</literal> functions
Packit 4d380f
which are meant to write outside of the current window.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtDrawRootText(int left, int top, const char * text);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
This function is straightforward. It displays the string <literal remap="tt">text</literal> at
Packit 4d380f
the position indicated. If either the <literal remap="tt">left</literal> or <literal remap="tt">top</literal> is
Packit 4d380f
negative, the position is measured from the opposite side of the
Packit 4d380f
screen. The final measurement will seem to be off by one though. For
Packit 4d380f
example, a <literal remap="tt">top</literal> of -1 indicates the last line on the screen, and
Packit 4d380f
one of -2 is the line above that.
Packit 4d380f
Packit 4d380f
As it's common to use the last line on the screen to display help information,
Packit 4d380f
<literal remap="tt">newt</literal> includes special support for doing exactly that. The last
Packit 4d380f
line on the display is known as the <emphasis remap="bf">help line</emphasis>, and is treated as a
Packit 4d380f
stack. As the value of the help line normally relates to the window
Packit 4d380f
currently displayed, using the same structure for window order and the
Packit 4d380f
help line is very natural. Two functions are provided to manipulate the
Packit 4d380f
help line.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtPushHelpLine(const char * text);
Packit 4d380f
void newtPopHelpLine(void);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The first function, <literal remap="tt">newtPushHelpLine()</literal>, saves the current help line
Packit 4d380f
on a stack (which is independent of the window stack) and displays the
Packit 4d380f
new line. If <literal remap="tt">text</literal> is <literal remap="tt">NULL</literal>, <literal remap="tt">newt</literal>'s default help line is
Packit 4d380f
displayed (which provides basic instructions on using <literal remap="tt">newt</literal>). If
Packit 4d380f
<literal remap="tt">text</literal> is a string of length 0, the help line is cleared. For all
Packit 4d380f
other values of <literal remap="tt">text</literal>, the passed string is displayed at the bottom,
Packit 4d380f
left-hand corner of the display. The space between the end of the displayed
Packit 4d380f
string the the right-hand edge of the terminal is cleared.
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtPopHelpLine()</literal> replaces the current help line with the one it
Packit 4d380f
replaced. It's important not to call tt/newtPopHelpLine()/ more then
Packit 4d380f
<literal remap="tt">newtPushHelpLine()</literal>!
Packit 4d380f
Packit 4d380f
<literal remap="tt">Suspending Newt Applications</literal>
Packit 4d380f
Packit 4d380f
By default, <literal remap="tt">newt</literal> programs cannot be suspended by the user (compare
Packit 4d380f
this to most Unix programs which can be suspended by pressing the suspend
Packit 4d380f
key (normally <literal remap="tt">^Z</literal>).  Instead, programs can specify a <emphasis remap="bf">callback</emphasis>
Packit 4d380f
function which gets invoked when the user presses the suspend key. 
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
typedef void (*newtSuspendCallback)(void);
Packit 4d380f
Packit 4d380f
void newtSetSuspendCallback(newtSuspendCallback cb);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The suspend function neither expects nor returns any value, and can
Packit 4d380f
do whatever it likes to when it is invoked. If no suspend callback
Packit 4d380f
is registered, the suspend keystroke is ignored.
Packit 4d380f
Packit 4d380f
If the application should suspend and continue like most user applications,
Packit 4d380f
the suspend callback needs two other <literal remap="tt">newt</literal> functions.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtSuspend(void);
Packit 4d380f
void newtResume(void);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtSuspend()</literal> tells <literal remap="tt">newt</literal> to return the terminal to its initial
Packit 4d380f
state. Once this is done, the application can suspend itself (by
Packit 4d380f
sending itself a <literal remap="tt">SIGTSTP</literal>, fork a child program, or do whatever
Packit 4d380f
else it likes. When it wants to resume using the <literal remap="tt">newt</literal> interface,
Packit 4d380f
it must call <literal remap="tt">newtResume</literal> before doing so. 
Packit 4d380f
Packit 4d380f
Note that suspend callbacks are not signal handlers. When <literal remap="tt">newtInit()</literal>
Packit 4d380f
takes over the terminal, it disables the part of the terminal interface
Packit 4d380f
which sends the suspend signal. Instead, if <literal remap="tt">newt</literal> sees the suspend
Packit 4d380f
keystroke during normal input processing, it immediately calls the suspend
Packit 4d380f
callback if one has been set. This means that suspending newt applications
Packit 4d380f
is not asynchronous.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Refreshing the Screen</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
To increase performance, S-Lang only updates the display when it needs
Packit 4d380f
to, not when the program tells S-Lang to write to the terminal. ``When it
Packit 4d380f
needs to'' is implemented as ``right before the we wait for the user to
Packit 4d380f
press a key''. While this allows for optimized screen displays most of
Packit 4d380f
the time, this optimization makes things difficult for programs which
Packit 4d380f
want to display progress messages without forcing the user to input 
Packit 4d380f
characters. Applications can force S-Lang to immediately update modified
Packit 4d380f
portions of the screen by calling <literal remap="tt">newtRefresh</literal>.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<orderedlist>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The program wants to display a progress message, without forcing
Packit 4d380f
for the user to enter any characters.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
A misfeature of the program causes part of the screen to be
Packit 4d380f
corrupted. Ideally, the program would be fixed, but that may not 
Packit 4d380f
always be practical.
Packit 4d380f
</para>
Packit 4d380f
</listitem></orderedlist>
Packit 4d380f
</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Other Miscellaneous Functions</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
As always, some function defy characterization. Two of <literal remap="tt">newt</literal>'s general
Packit 4d380f
function fit this oddball category.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtBell(void);
Packit 4d380f
void newtGetScreenSize(int * cols, int * rows);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The first sends a beep to the terminal. Depending on the terminal's
Packit 4d380f
settings, this been may or may not be audible. The second function,
Packit 4d380f
<literal remap="tt">newtGetScreenSize()</literal>, fills in the passed pointers with the
Packit 4d380f
current size of the terminal.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Basic <literal remap="tt">newt</literal> Example</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
To help illustrate the functions presented in this section here is a short
Packit 4d380f
sample <literal remap="tt">newt</literal> program which uses many of them. While it doesn't do
Packit 4d380f
anything interesting, it does show the basic structure of <literal remap="tt">newt</literal> programs.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
#include <newt.h>
Packit 4d380f
#include <stdlib.h>
Packit 4d380f
Packit 4d380f
int main(void) {
Packit 4d380f
    newtInit();
Packit 4d380f
    newtCls();
Packit 4d380f
Packit 4d380f
    newtDrawRootText(0, 0, "Some root text");
Packit 4d380f
    newtDrawRootText(-25, -2, "Root text in the other corner");
Packit 4d380f
Packit 4d380f
    newtPushHelpLine(NULL);
Packit 4d380f
    newtRefresh();
Packit 4d380f
    sleep(1);
Packit 4d380f
Packit 4d380f
    newtPushHelpLine("A help line");
Packit 4d380f
    newtRefresh();
Packit 4d380f
    sleep(1);
Packit 4d380f
Packit 4d380f
    newtPopHelpLine();
Packit 4d380f
    newtRefresh();
Packit 4d380f
    sleep(1);
Packit 4d380f
Packit 4d380f
    newtFinished();
Packit 4d380f
}
Packit 4d380f
</screen></para></sect2></sect1>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect1><title>Windows</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
While most <literal remap="tt">newt</literal> applications do use windows, <literal remap="tt">newt</literal>'s window
Packit 4d380f
support is actually extremely limited. Windows must be destroyed in the
Packit 4d380f
opposite of the order they were created, and only the topmost window may be
Packit 4d380f
active. Corollaries to this are:
Packit 4d380f
Packit 4d380f
Packit 4d380f
<itemizedlist>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The user may not switch between windows.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Only the top window may be destroyed.
Packit 4d380f
</para>
Packit 4d380f
</listitem></itemizedlist>
Packit 4d380f
Packit 4d380f
While this is quite a severe limitation, adopting it greatly simplifies
Packit 4d380f
both writing <literal remap="tt">newt</literal> applications and developing <literal remap="tt">newt</literal> itself, as it
Packit 4d380f
separates <literal remap="tt">newt</literal> from the world of event-driven programming. However,
Packit 4d380f
this tradeoff between function and simplicity may make <literal remap="tt">newt</literal>
Packit 4d380f
unsuitable for some tasks.
Packit 4d380f
</para>
Packit 4d380f
Packit 4d380f
<sect2><title>Creating Windows</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
There are two main ways of opening <literal remap="tt">newt</literal> windows: with or without
Packit 4d380f
explicit sizings. When grids (which will be introduced later in this
Packit 4d380f
tutorial) are used, a window may be made to just fit the grid. When
Packit 4d380f
grids are not used, explicit sizing must be given.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
int newtCenteredWindow(int width, int height, const char * title);
Packit 4d380f
int newtOpenWindow(int left, int top, int width, int height, 
Packit 4d380f
		   const char * title);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The first of these functions open a centered window of the specified
Packit 4d380f
size. The <literal remap="tt">title</literal> is optional -- if it is <literal remap="tt">NULL</literal>, then no title
Packit 4d380f
is used. <literal remap="tt">newtOpenWindow*(</literal> is similar, but it requires a specific
Packit 4d380f
location for the upper left-hand corner of the window.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Destroying Windows</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
All windows are destroyed in the same manner, no matter how the windows
Packit 4d380f
were originally created.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtPopWindow(void);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
This function removes the top window from the display, and redraws the
Packit 4d380f
display areas which the window overwrote.</para></sect2></sect1> 
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect1><title>Components</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Components are the basic user interface element <literal remap="tt">newt</literal> provides. A
Packit 4d380f
single component may be (for example) a listbox, push button checkbox,
Packit 4d380f
a collection of other components. Most components are used to display
Packit 4d380f
information in a window, provide a place for the user to enter data, or a
Packit 4d380f
combination of these two functions. 
Packit 4d380f
Packit 4d380f
Forms, however, are a component whose primary purpose is not noticed by
Packit 4d380f
the user at all. Forms are collections of components (a form may contain
Packit 4d380f
another form) which logically relate the components to one another. Once
Packit 4d380f
a form is created and had all of its constituent components added to it,
Packit 4d380f
applications normally then run the form.  This gives control of the
Packit 4d380f
application to the form, which then lets the user enter data onto the
Packit 4d380f
form. When the user is done (a number of different events qualify as
Packit 4d380f
``done''), the form returns control to the part of the application which
Packit 4d380f
invoked it. The application may then read the information the user provided
Packit 4d380f
and continue appropriately.
Packit 4d380f
Packit 4d380f
All <literal remap="tt">newt</literal> components are stored in a common data type, a
Packit 4d380f
<literal remap="tt">newtComponent</literal> (some of the particulars of <literal remap="tt">newtComponent</literal>s have
Packit 4d380f
already been mentioned. While this makes it easy for programmers to pass
Packit 4d380f
components around, it does force them to make sure they don't pass
Packit 4d380f
entry boxes to routines expecting push buttons, as the compiler can't
Packit 4d380f
ensure that for them.
Packit 4d380f
Packit 4d380f
We start off with a brief introduction to forms. While not terribly
Packit 4d380f
complete, this introduction is enough to let us illustrate the rest of
Packit 4d380f
the components with some sample code. We'll then discuss the remainder of
Packit 4d380f
the components, and end this section with a more exhaustive description of
Packit 4d380f
forms.</para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Introduction to Forms</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
As we've mentioned, forms are simply collections of components. As only one
Packit 4d380f
form can be active (or running) at a time, every component which the user
Packit 4d380f
should be able to access must be on the running form (or on a subform of
Packit 4d380f
the running form). A form is itself a component, which means forms are
Packit 4d380f
stored in <literal remap="tt">newtComponent</literal> data structures.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtForm(newtComponent vertBar, const char * help, int flags);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
To create a form, call <literal remap="tt">newtForm()</literal>. The first parameter is a vertical
Packit 4d380f
scrollbar which should be associated with the form. For now, that should
Packit 4d380f
always be <literal remap="tt">NULL</literal> (we'll discuss how to create scrolling forms later in
Packit 4d380f
this section). The second parameter, <literal remap="tt">help</literal>, is currently unused and
Packit 4d380f
should always be <literal remap="tt">NULL</literal>. The <literal remap="tt">flags</literal> is normally 0, and other values
Packit 4d380f
it can take will be discussed later. Now that we've waved away the
Packit 4d380f
complexity of this function, creating a form boils down to simply:
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent myForm;
Packit 4d380f
Packit 4d380f
myForm = newtForm(NULL, NULL, 0);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
After a form is created, components need to be added to it --- after all,
Packit 4d380f
an empty form isn't terribly useful. There are two functions which add
Packit 4d380f
components to a form.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtFormAddComponent(newtComponent form, newtComponent co);
Packit 4d380f
void newtFormAddComponents(newtComponent form, ...);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The first function, <literal remap="tt">newtFormAddComponent()</literal>, adds a single component
Packit 4d380f
to the form which is passed as the first parameter. The second function
Packit 4d380f
is simply a convenience function. After passing the form to
Packit 4d380f
<literal remap="tt">newtFormAddComponents()</literal>, an arbitrary number of components is then
Packit 4d380f
passed, followed by <literal remap="tt">NULL</literal>. Every component passed is added to the form.
Packit 4d380f
Packit 4d380f
Once a form has been created and components have been added to it, it's
Packit 4d380f
time to run the form.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtRunForm(newtComponent form);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
This function runs the form passed to it, and returns the component which
Packit 4d380f
caused the form to stop running. For now, we'll ignore the return value
Packit 4d380f
completely.
Packit 4d380f
Packit 4d380f
Notice that this function doesn't fit in with <literal remap="tt">newt</literal>'s normal
Packit 4d380f
naming convention. It is an older interface which will not work for all
Packit 4d380f
forms. It was left in <literal remap="tt">newt</literal> only for legacy applications. It is a
Packit 4d380f
simpler interface than the new <literal remap="tt">newtFormRun()</literal> though, and is still used
Packit 4d380f
quite often as a result.
Packit 4d380f
Packit 4d380f
When an application is done with a form, it destroys the form and
Packit 4d380f
all of the components the form contains.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtFormDestroy(newtComponent form);	
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
This function frees the memory resources used by the form and all of the
Packit 4d380f
components which have been added to the form (including those components
Packit 4d380f
which are on subforms). Once a form has been destroyed, none of the form's
Packit 4d380f
components can be used.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Components</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Non-form components are the most important user-interface component for
Packit 4d380f
users. They determine how users interact with <literal remap="tt">newt</literal> and how information
Packit 4d380f
is presented to them.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>General Component Manipulation</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
There are a couple of functions which work on more then one type of
Packit 4d380f
components. The description of each component indicates which (if any)
Packit 4d380f
of these functions are valid for that particular component.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
typedef void (*newtCallback)(newtComponent, void *);
Packit 4d380f
Packit 4d380f
void newtComponentAddCallback(newtComponent co, newtCallback f, void * data);
Packit 4d380f
void newtComponentTakesFocus(newtComponent co, int val);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The first registers a callback function for that component. A callback
Packit 4d380f
function is a function the application provides which <literal remap="tt">newt</literal> calls for a
Packit 4d380f
particular component. Exactly when (if ever) the callback is invoked
Packit 4d380f
depends on the type of component the callback is attached to, and will be
Packit 4d380f
discussed for the components which support callbacks.
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtComponentTakesFocus()</literal> works on all components. It allows the
Packit 4d380f
application to change which components the user is allowed to select as the
Packit 4d380f
current component, and hence provide input to. Components which do not
Packit 4d380f
take focus are skipped over during form traversal, but they are displayed
Packit 4d380f
on the terminal. Some components should never be set to take focus, such
Packit 4d380f
as those which display static text.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Buttons</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Nearly all forms contain at least one button. <literal remap="tt">Newt</literal> buttons come in two
Packit 4d380f
flavors, full buttons and compact buttons. Full buttons take up quit a bit
Packit 4d380f
of screen space, but look much better then the single-row compact buttons.
Packit 4d380f
Other then their size, both button styles behave identically. Different
Packit 4d380f
functions are used to create the two types of buttons.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtButton(int left, int top, const char * text);
Packit 4d380f
newtComponent newtCompactButton(int left, int top, const char * text);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Both functions take identical parameters. The first two parameters are the
Packit 4d380f
location of the upper left corner of the button, and the final parameter is
Packit 4d380f
the text which should be displayed in the button (such as ``Ok'' or
Packit 4d380f
``Cancel'').</para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect3><title>Button Example</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Here is a simple example of both full and compact buttons. It also
Packit 4d380f
illustrates opening and closing windows, as well a simple form.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
#include <newt.h>
Packit 4d380f
#include <stdlib.h>
Packit 4d380f
Packit 4d380f
void main(void) {
Packit 4d380f
    newtComponent form, b1, b2;
Packit 4d380f
    newtInit();
Packit 4d380f
    newtCls();
Packit 4d380f
Packit 4d380f
    newtOpenWindow(10, 5, 40, 6, "Button Sample");
Packit 4d380f
Packit 4d380f
    b1 = newtButton(10, 1, "Ok");
Packit 4d380f
    b2 = newtCompactButton(22, 2, "Cancel");
Packit 4d380f
    form = newtForm(NULL, NULL, 0);
Packit 4d380f
    newtFormAddComponents(form, b1, b2, NULL);
Packit 4d380f
Packit 4d380f
    newtRunForm(form);
Packit 4d380f
Packit 4d380f
    newtFormDestroy(form);
Packit 4d380f
    newtFinished();
Packit 4d380f
}
Packit 4d380f
</screen></para></sect3></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Labels</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Labels are <literal remap="tt">newt</literal>'s simplest component. They display some given text and
Packit 4d380f
don't allow any user input.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtLabel(int left, int top, const char * text);
Packit 4d380f
void newtLabelSetText(newtComponent co, const char * text);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Creating a label is just like creating a button; just pass the location of
Packit 4d380f
the label and the text it should display. Unlike buttons, labels do let the
Packit 4d380f
application change the text in the label with <literal remap="tt">newtLabelSetText</literal>. When
Packit 4d380f
the label's text is changed, the label automatically redraws itself. It
Packit 4d380f
does not clear out any old text which may be leftover from the previous
Packit 4d380f
time is was displayed, however, so be sure that the new text is at least
Packit 4d380f
as long as the old text.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Entry Boxes</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Entry boxes allow the user to enter a text string into the form which the
Packit 4d380f
application can later retrieve.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
typedef int (*newtEntryFilter)(newtComponent entry, void * data, int ch,
Packit 4d380f
			       int cursor);
Packit 4d380f
Packit 4d380f
newtComponent newtEntry(int left, int top, const char * initialValue, int width,
Packit 4d380f
			char ** resultPtr, int flags);
Packit 4d380f
void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd);
Packit 4d380f
char * newtEntryGetValue(newtComponent co);
Packit 4d380f
void newtEntrySetFilter(newtComponent co, newtEntryFilter filter, void * data);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
<literal remap="tt">newtEntry()</literal> creates a new entry box. After the location of the entry
Packit 4d380f
box, the initial value for the entry box is passed, which may be <literal remap="tt">NULL</literal>
Packit 4d380f
if the box should start off empty. Next, the width of the physical box is
Packit 4d380f
given. This width may or may not limit the length of the string the user is
Packit 4d380f
allowed to enter; that depends on the <literal remap="tt">flags</literal>. The <literal remap="tt">resultPtr</literal> must
Packit 4d380f
be the address of a <literal remap="tt">char *</literal>. Until the entry box is destroyed by
Packit 4d380f
<literal remap="tt">newtFormDestroy()</literal>, that <literal remap="tt">char *</literal> will point to the current value
Packit 4d380f
of the entry box. It's important that applications make a copy of that
Packit 4d380f
value before destroying the form if they need to use it later. The
Packit 4d380f
<literal remap="tt">resultPtr</literal> may be <literal remap="tt">NULL</literal>, in which case the user must use the
Packit 4d380f
<literal remap="tt">newtEntryGetValue()</literal> function to get the value of the entry box.
Packit 4d380f
Packit 4d380f
Entry boxes support a number of flags:
Packit 4d380f
Packit 4d380f
<variablelist>
Packit 4d380f
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_ENTRY_SCROLL</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>If this flag is not specified, the user cannot
Packit 4d380f
enter text into the entry box which is wider then the entry box itself.
Packit 4d380f
This flag removes this limitation, and lets the user enter data of an
Packit 4d380f
arbitrary length.</para></listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_HIDDEN</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>If this flag is specified, the value of the entry box
Packit 4d380f
is not displayed. This is useful when the application needs to read a
Packit 4d380f
password, for example.</para></listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_RETURNEXIT</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>When this flag is given, the entry box will cause
Packit 4d380f
the form to stop running if the user pressed return inside of the entry
Packit 4d380f
box. This can provide a nice shortcut for users.</para>
Packit 4d380f
</listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
</variablelist>
Packit 4d380f
Packit 4d380f
Packit 4d380f
After an entry box has been created, its contents can be set by
Packit 4d380f
<literal remap="tt">newtEntrySet()</literal>. After the entry box itself, the new string to place
Packit 4d380f
in the entry box is passed. The final parameter, <literal remap="tt">cursorAtEnd</literal>, controls
Packit 4d380f
where the cursor will appear in the entry box. If it is zero, the cursor
Packit 4d380f
remains at its present location; a nonzero value moves the cursor to the
Packit 4d380f
end of the entry box's new value.
Packit 4d380f
Packit 4d380f
While the simplest way to find the value of an entry box is by using a
Packit 4d380f
<literal remap="tt">resultPtr</literal>, doing so complicates some applications.
Packit 4d380f
<literal remap="tt">newtEntryGetValue()</literal> returns a pointer to the string which the entry
Packit 4d380f
box currently contains. The returned pointer may not be valid once the
Packit 4d380f
user further modifies the entry box, and will not be valid after the 
Packit 4d380f
entry box has been destroyed, so be sure to save its value in a more
Packit 4d380f
permanent location if necessary.
Packit 4d380f
Packit 4d380f
Entry boxes allow applications to filter characters as they are entered.
Packit 4d380f
This allows programs to ignore characters which are invalid (such as
Packit 4d380f
entering a ^ in the middle of a phone number) and provide intelligent aids
Packit 4d380f
to the user (such as automatically adding a '.' after the user has typed in
Packit 4d380f
the first three numbers in an IP address). 
Packit 4d380f
Packit 4d380f
When a filter is registered through <literal remap="tt">newtEntrySetFilter()</literal>, both the
Packit 4d380f
filter itself and an arbitrary <literal remap="tt">void *</literal>, which passed to the filter
Packit 4d380f
whenever it is invoked, are recorded. This data pointer isn't used for any
Packit 4d380f
other purpose, and may be <literal remap="tt">NULL</literal>. Entry filters take four arguments.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<orderedlist>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The entry box which had data entered into it</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The data pointer which was registered along with the filter</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The new character which <literal remap="tt">newt</literal> is considering inserting into the
Packit 4d380f
entry box</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The current cursor position (0 is the leftmost position)
Packit 4d380f
</para>
Packit 4d380f
</listitem>
Packit 4d380f
</orderedlist>
Packit 4d380f
Packit 4d380f
The filter returns 0 if the character should be ignored, or the value of
Packit 4d380f
the character which should be inserted into the entry box. Filter functions
Packit 4d380f
which want to do complex manipulations of the string should use
Packit 4d380f
<literal remap="tt">newtEntrySet()</literal> to update the entry box and then return 0 to prevent
Packit 4d380f
the new character from being inserted.
Packit 4d380f
Packit 4d380f
When a callback is attached to a entry box, the callback is invoked
Packit 4d380f
whenever the user moves off of the callback and on to another component.
Packit 4d380f
Packit 4d380f
Here is a sample program which illustrates the use of both labels and
Packit 4d380f
entry boxes.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
#include <newt.h>
Packit 4d380f
#include <stdlib.h>
Packit 4d380f
#include <stdio.h>
Packit 4d380f
Packit 4d380f
void main(void) {
Packit 4d380f
    newtComponent form, label, entry, button;
Packit 4d380f
    char * entryValue;
Packit 4d380f
Packit 4d380f
    newtInit();
Packit 4d380f
    newtCls();
Packit 4d380f
Packit 4d380f
    newtOpenWindow(10, 5, 40, 8, "Entry and Label Sample");
Packit 4d380f
Packit 4d380f
    label = newtLabel(1, 1, "Enter a string");
Packit 4d380f
    entry = newtEntry(16, 1, "sample", 20, &entryValue, 
Packit 4d380f
		      NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
Packit 4d380f
    button = newtButton(17, 3, "Ok");
Packit 4d380f
    form = newtForm(NULL, NULL, 0);
Packit 4d380f
    newtFormAddComponents(form, label, entry, button, NULL);
Packit 4d380f
Packit 4d380f
    newtRunForm(form);
Packit 4d380f
Packit 4d380f
    newtFinished();
Packit 4d380f
Packit 4d380f
    printf("Final string was: %s\n", entryValue);
Packit 4d380f
Packit 4d380f
    /* We cannot destroy the form until after we've used the value
Packit 4d380f
       from the entry widget. */
Packit 4d380f
    newtFormDestroy(form);
Packit 4d380f
}
Packit 4d380f
</screen></para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Checkboxes</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Most widget sets include checkboxes which toggle between two value (checked
Packit 4d380f
or not checked). <literal remap="tt">Newt</literal> checkboxes are more flexible. When the user
Packit 4d380f
presses the space bar on a checkbox, the checkbox's value changes to the
Packit 4d380f
next value in an arbitrary sequence (which wraps). Most checkboxes have
Packit 4d380f
two items in that sequence, checked or not, but <literal remap="tt">newt</literal> allows an
Packit 4d380f
arbitrary number of value. This is useful when the user must pick from a
Packit 4d380f
limited number of choices.
Packit 4d380f
Packit 4d380f
Each item in the sequence is a single character, and the sequence itself is
Packit 4d380f
represented as a string. The checkbox components displays the character
Packit 4d380f
which currently represents its value the left of a text label, and returns
Packit 4d380f
the same character as its current value. The default sequence for
Packit 4d380f
checkboxes is <literal remap="tt">" *"</literal>, with <literal remap="tt">' '</literal> indicating false and <literal remap="tt">'*'</literal> true.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtCheckbox(int left, int top, const char * text, char defValue,
Packit 4d380f
			   const char * seq, char * result);
Packit 4d380f
char newtCheckboxGetValue(newtComponent co);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Like most components, the position of the checkbox is the first thing
Packit 4d380f
passed to the function that creates one. The next parameter, <literal remap="tt">text</literal>, is
Packit 4d380f
the text which is displayed to the right of the area which is checked.  The
Packit 4d380f
<literal remap="tt">defValue</literal> is the initial value for the checkbox, and <literal remap="tt">seq</literal> is the
Packit 4d380f
sequence which the checkbox should go through (<literal remap="tt">defValue</literal> must be
Packit 4d380f
in <literal remap="tt">seq</literal>. <literal remap="tt">seq</literal> may be <literal remap="tt">NULL</literal>, in which case <literal remap="tt">" *"</literal> is used.
Packit 4d380f
The final parameter, <literal remap="tt">result</literal>, should point to a character which the
Packit 4d380f
checkbox should always record its current value in. If <literal remap="tt">result</literal> is
Packit 4d380f
<literal remap="tt">NULL</literal>, <literal remap="tt">newtCheckboxGetValue()</literal> must be used to get the current
Packit 4d380f
value of the checkbox.
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtCheckboxGetValue()</literal> is straightforward, returning the character
Packit 4d380f
in the sequence which indicates the current value of the checkbox
Packit 4d380f
Packit 4d380f
If a callback is attached to a checkbox, the callback is invoked whenever
Packit 4d380f
the checkbox responds to a user's keystroke. The entry box may respond by
Packit 4d380f
taking focus or giving up focus, as well as by changing its current value.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Radio Buttons</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Radio buttons look very similar to checkboxes. The key difference between
Packit 4d380f
the two is that radio buttons are grouped into sets, and exactly one radio
Packit 4d380f
button in that set may be turned on. If another radio button is selected,
Packit 4d380f
the button which was selected is automatically deselected.
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtRadiobutton(int left, int top, const char * text, 
Packit 4d380f
			      int isDefault, newtComponent prevButton);
Packit 4d380f
newtComponent newtRadioGetCurrent(newtComponent setMember);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Each radio button is created by calling <literal remap="tt">newtRadiobutton()</literal>. After
Packit 4d380f
the position of the radio button, the text displayed with the button
Packit 4d380f
is passed. <literal remap="tt">isDefault</literal> should be nonzero if the radio button is to
Packit 4d380f
be turned on by default. The final parameter, <literal remap="tt">prevMember</literal> is used
Packit 4d380f
to group radio buttons into sets. If <literal remap="tt">prevMember</literal> is <literal remap="tt">NULL</literal>, the 
Packit 4d380f
radio button is assigned to a new set. If the radio button should belong
Packit 4d380f
to a preexisting set, <literal remap="tt">prevMember</literal> must be the previous radio button
Packit 4d380f
added to that set.
Packit 4d380f
Packit 4d380f
Discovering which radio button in a set is currently selected necessitates
Packit 4d380f
<literal remap="tt">newtRadioGetCurrent()</literal>. It may be passed any radio button in the set
Packit 4d380f
you're interested in, and it returns the radio button component currently
Packit 4d380f
selected.
Packit 4d380f
Packit 4d380f
Here is an example of both checkboxes and radio buttons.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
#include <newt.h>
Packit 4d380f
#include <stdlib.h>
Packit 4d380f
#include <stdio.h>
Packit 4d380f
Packit 4d380f
void main(void) {
Packit 4d380f
    newtComponent form, checkbox, rb[3], button;
Packit 4d380f
    char cbValue;
Packit 4d380f
    int i;
Packit 4d380f
Packit 4d380f
    newtInit();
Packit 4d380f
    newtCls();
Packit 4d380f
Packit 4d380f
    newtOpenWindow(10, 5, 40, 11, "Checkboxes and Radio buttons");
Packit 4d380f
Packit 4d380f
    checkbox = newtCheckbox(1, 1, "A checkbox", ' ', " *X", &cbValue);
Packit 4d380f
Packit 4d380f
    rb[0] = newtRadiobutton(1, 3, "Choice 1", 1, NULL);
Packit 4d380f
    rb[1] = newtRadiobutton(1, 4, "Choice 2", 0, rb[0]);
Packit 4d380f
    rb[2] = newtRadiobutton(1, 5, "Choice 3", 0, rb[1]);
Packit 4d380f
Packit 4d380f
    button = newtButton(1, 7, "Ok");
Packit 4d380f
Packit 4d380f
    form = newtForm(NULL, NULL, 0);
Packit 4d380f
    newtFormAddComponent(form, checkbox);
Packit 4d380f
    for (i = 0; i < 3; i++)
Packit 4d380f
	newtFormAddComponent(form, rb[i]);
Packit 4d380f
    newtFormAddComponent(form, button);
Packit 4d380f
Packit 4d380f
    newtRunForm(form);
Packit 4d380f
    newtFinished();
Packit 4d380f
Packit 4d380f
    /* We cannot destroy the form until after we've found the current
Packit 4d380f
       radio button */
Packit 4d380f
Packit 4d380f
    for (i = 0; i < 3; i++)
Packit 4d380f
	if (newtRadioGetCurrent(rb[0]) == rb[i])
Packit 4d380f
	    printf("radio button picked: %d\n", i);
Packit 4d380f
    newtFormDestroy(form);
Packit 4d380f
Packit 4d380f
    /* But the checkbox's value is stored locally */
Packit 4d380f
    printf("checkbox value: '%c'\n", cbValue);
Packit 4d380f
}
Packit 4d380f
</screen></para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Scales</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
It's common for programs to need to display a progress meter on the
Packit 4d380f
terminal while it performs some length operation (it behaves like an
Packit 4d380f
anesthetic). The scale component is a simple way of doing this. It
Packit 4d380f
displays a horizontal bar graph which the application can update as the
Packit 4d380f
operation continues.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtScale(int left, int top, int width, long long fullValue);
Packit 4d380f
void newtScaleSet(newtComponent co, unsigned long long amount);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
When the scale is created with <literal remap="tt">newtScale</literal>, it is given the width of the
Packit 4d380f
scale itself as well as the value which means that the scale should be
Packit 4d380f
drawn as full. When the position of the scale is set with
Packit 4d380f
<literal remap="tt">newtScaleSet()</literal>, the scale is told the amount of the scale which should
Packit 4d380f
be filled in relative to the <literal remap="tt">fullAmount</literal>. For example, if the
Packit 4d380f
application is copying a file, <literal remap="tt">fullValue</literal> could be the number of bytes
Packit 4d380f
in the file, and when the scale is updated <literal remap="tt">newtScaleSet()</literal> would be
Packit 4d380f
passed the number of bytes which have been copied so far.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Textboxes</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Textboxes display a block of text on the terminal, and is appropriate for
Packit 4d380f
display large amounts of text. 
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtTextbox(int left, int top, int width, int height, int flags);
Packit 4d380f
void newtTextboxSetText(newtComponent co, const char * text);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtTextbox()</literal> creates a new textbox, but does not fill it with data.
Packit 4d380f
The function is passed the location for the textbox on the screen, the
Packit 4d380f
width and height of the textbox (in characters), and zero or more of the
Packit 4d380f
following flags:
Packit 4d380f
Packit 4d380f
<variablelist>
Packit 4d380f
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_WRAP</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>All text in the textbox should be wrapped to fit
Packit 4d380f
the width of the textbox. If this flag is not specified, each newline
Packit 4d380f
delimited line in the text is truncated if it is too long to fit.
Packit 4d380f
Packit 4d380f
When <literal remap="tt">newt</literal> wraps text, it tries not to break lines on spaces or tabs.
Packit 4d380f
Literal newline characters are respected, and may be used to force line
Packit 4d380f
breaks.</para>
Packit 4d380f
<variablelist>
Packit 4d380f
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_SCROLL</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>The text box should be scrollable. When this option
Packit 4d380f
is used, the scrollbar which is added increases the width of the area used
Packit 4d380f
by the textbox by 2 characters; that is the textbox is 2 characters wider
Packit 4d380f
then the width passed to <literal remap="tt">newtTextbox()</literal>.
Packit 4d380f
</para></listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
</variablelist>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
After a textbox has been created, text may be added to it through
Packit 4d380f
<literal remap="tt">newtTextboxSetText()</literal>, which takes only the textbox and the new text as
Packit 4d380f
parameters. If the textbox already contained text, that text is replaced by
Packit 4d380f
the new text. The textbox makes its own copy of the passed text, so these
Packit 4d380f
is no need to keep the original around unless it's convenient.</para></listitem></varlistentry></variablelist></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect3><title>Reflowing Text</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
When applications need to display large amounts of text, it's common not to
Packit 4d380f
know exactly where the linebreaks should go. While textboxes are quite
Packit 4d380f
willing to scroll the text, the programmer still must know what width the
Packit 4d380f
text will look ``best'' at (where ``best'' means most exactly rectangular;
Packit 4d380f
no lines much shorter or much longer then the rest). This common is
Packit 4d380f
especially prevalent in internationalized programs, which need to make a
Packit 4d380f
wide variety of message string look god on a screen.
Packit 4d380f
Packit 4d380f
To help with this, <literal remap="tt">newt</literal> provides routines to reformat text to look
Packit 4d380f
good. It tries different widths to figure out which one will look ``best''
Packit 4d380f
to the user. As these commons are almost always used to format text for
Packit 4d380f
textbox components, <literal remap="tt">newt</literal> makes it easy to construct a textbox with
Packit 4d380f
reflowed text.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
char * newtReflowText(char * text, int width, int flexDown, int flexUp,
Packit 4d380f
		      int * actualWidth, int * actualHeight);
Packit 4d380f
newtComponent newtTextboxReflowed(int left, int top, char * text, int width,
Packit 4d380f
				  int flexDown, int flexUp, int flags);
Packit 4d380f
int newtTextboxGetNumLines(newtComponent co);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
<literal remap="tt">newtReflowText()</literal> reflows the <literal remap="tt">text</literal> to a target width of
Packit 4d380f
<literal remap="tt">width</literal>. The actual width of the longest line in the returned string is
Packit 4d380f
between <literal remap="tt">width - flexDown</literal> and <literal remap="tt">width + flexUp</literal>; the actual maximum
Packit 4d380f
line length is chosen to make the displayed check look rectangular. 
Packit 4d380f
The <literal remap="tt">int</literal>s pointed to by <literal remap="tt">actualWidth</literal> and <literal remap="tt">actualHeight</literal> are set
Packit 4d380f
to the width of the longest line and the number of lines in in the
Packit 4d380f
returned text, respectively. Either one may be <literal remap="tt">NULL</literal>. The return
Packit 4d380f
value points to the reflowed text, and is allocated through <literal remap="tt">malloc()</literal>.
Packit 4d380f
Packit 4d380f
When the reflowed text is being placed in a textbox it may be easier to use
Packit 4d380f
<literal remap="tt">newtTextboxReflowed()</literal>, which creates a textbox, reflows the text, and
Packit 4d380f
places the reflowed text in the listbox. It's parameters consist of the
Packit 4d380f
position of the final textbox, the width and flex values for the text
Packit 4d380f
(which are identical to the parameters passed to <literal remap="tt">newtReflowText()</literal>,
Packit 4d380f
and the flags for the textbox (which are the same as the flags for
Packit 4d380f
<literal remap="tt">newtTextbox()</literal>. This function does not let you limit the height of the
Packit 4d380f
textbox, however, making limiting it's use to constructing textboxes which
Packit 4d380f
don't need to scroll.
Packit 4d380f
Packit 4d380f
To find out how tall the textbox created by <literal remap="tt">newtTextboxReflowed()</literal> is, 
Packit 4d380f
use <literal remap="tt">newtTextboxGetNumLines()</literal>, which returns the number of lines in the
Packit 4d380f
textbox. For textboxes created by <literal remap="tt">newtTextboxReflowed()</literal>, this is
Packit 4d380f
always the same as the height of the textbox.
Packit 4d380f
Packit 4d380f
Here's a simple program which uses a textbox to display a message.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
#include <newt.h>
Packit 4d380f
#include <stdlib.h>
Packit 4d380f
Packit 4d380f
char message[] = "This is a pretty long message. It will be displayed "
Packit 4d380f
		 "in a newt textbox, and illustrates how to construct "
Packit 4d380f
		 "a textbox from arbitrary text which may not have "
Packit 4d380f
		 "very good line breaks.\n\n"
Packit 4d380f
		 "Notice how literal \\n characters are respected, and "
Packit 4d380f
		 "may be used to force line breaks and blank lines.";
Packit 4d380f
Packit 4d380f
void main(void) {
Packit 4d380f
    newtComponent form, text, button;
Packit 4d380f
Packit 4d380f
    newtInit();
Packit 4d380f
    newtCls();
Packit 4d380f
Packit 4d380f
    text = newtTextboxReflowed(1, 1, message, 30, 5, 5, 0);
Packit 4d380f
    button = newtButton(12, newtTextboxGetNumLines(text) + 2, "Ok");
Packit 4d380f
Packit 4d380f
    newtOpenWindow(10, 5, 37,
Packit 4d380f
		   newtTextboxGetNumLines(text) + 7, "Textboxes");
Packit 4d380f
Packit 4d380f
    form = newtForm(NULL, NULL, 0);
Packit 4d380f
    newtFormAddComponents(form, text, button, NULL);
Packit 4d380f
Packit 4d380f
    newtRunForm(form);
Packit 4d380f
    newtFormDestroy(form);
Packit 4d380f
    newtFinished();
Packit 4d380f
}
Packit 4d380f
</screen></para></sect3></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Scrollbars</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Scrollbars (which, currently, are always vertical in <literal remap="tt">newt</literal>), may be
Packit 4d380f
attached to forms to let them contain more data then they have space for.
Packit 4d380f
While the actual process of making scrolling forms is discussed at the end 
Packit 4d380f
of this section, we'll go ahead and introduce scrollbars now so you'll be
Packit 4d380f
ready.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtVerticalScrollbar(int left, int top, int height,
Packit 4d380f
				    int normalColorset, int thumbColorset);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
When a scrollbar is created, it is given a position on the screen, a
Packit 4d380f
height, and two colors. The first color is the color used for drawing the
Packit 4d380f
scrollbar, and the second color is used for drawing the thumb. This is the
Packit 4d380f
only place in newt where an application specifically sets colors for a
Packit 4d380f
component. It's done here to let the colors a scrollbar use match the
Packit 4d380f
colors of the component the scrollbar is mated too. When a scrollbar is
Packit 4d380f
being used with a form, <literal remap="tt">normalColorset</literal> is often
Packit 4d380f
<literal remap="tt">NEWT_COLORSET_WINDOW</literal> and <literal remap="tt">thumbColorset</literal>
Packit 4d380f
<literal remap="tt">NEWT_COLORSET_ACTCHECKBOX</literal>. Of course, feel free to peruse
Packit 4d380f
<literal remap="tt"><newt.h></literal> and pick your own colors.
Packit 4d380f
Packit 4d380f
As the scrollbar is normally updated by the component it is mated with,
Packit 4d380f
there is no public interface for moving the thumb.</para></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Listboxes</title>
Packit 4d380f
<para>
Packit 4d380f
Listboxes are the most complicated components 
Packit 4d380f
<literal remap="tt">newt</literal> provides. They can
Packit 4d380f
allow a single selection or multiple selection, and are easy to update.
Packit 4d380f
Unfortunately, their API is also the least consistent of <literal remap="tt">newt</literal>'s
Packit 4d380f
components. Each entry in a listbox is a ordered pair of the text which should be
Packit 4d380f
displayed for that item and a <emphasis remap="bf">key</emphasis>, which is a <literal remap="tt">void *</literal> that
Packit 4d380f
uniquely identifies that listbox item. Many applications pass integers in
Packit 4d380f
as keys, but using arbitrary pointers makes many applications significantly
Packit 4d380f
easier to code.</para>
Packit 4d380f
Packit 4d380f
<sect3><title>Basic Listboxes</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Let's start off by looking at the most important listbox functions.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtListbox(int left, int top, int height, int flags);
Packit 4d380f
int newtListboxAppendEntry(newtComponent co, const char * text, 
Packit 4d380f
			   const void * data);
Packit 4d380f
void * newtListboxGetCurrent(newtComponent co);
Packit 4d380f
void newtListboxSetWidth(newtComponent co, int width);
Packit 4d380f
void newtListboxSetCurrent(newtComponent co, int num);
Packit 4d380f
void newtListboxSetCurrentByKey(newtComponent co, void * key);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
A listbox is created at a certain position and a given height. The
Packit 4d380f
<literal remap="tt">height</literal> is used for two things. First of all, it is the minimum
Packit 4d380f
height the listbox will use. If there are less items in the listbox then
Packit 4d380f
the height, suggests the listbox will still take up that minimum amount
Packit 4d380f
of space. Secondly, if the listbox is set to be scrollable (by setting
Packit 4d380f
the <literal remap="tt">NEWT_FLAG_SCROLL flag</literal>, the <literal remap="tt">height</literal> is also the maximum height
Packit 4d380f
of the listbox. If the listbox may not scroll, it increases its height to
Packit 4d380f
display all of its items.
Packit 4d380f
Packit 4d380f
The following flags may be used when creating a listbox:
Packit 4d380f
Packit 4d380f
<variablelist>
Packit 4d380f
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_SCROLL</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>The listbox should scroll to display all of the
Packit 4d380f
items it contains.</para></listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_RETURNEXIT</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>When the user presses return on an item in the
Packit 4d380f
list, the form should return.</para></listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_BORDER</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>A frame is drawn around the listbox, which can make
Packit 4d380f
it easier to see which listbox has the focus when a form contains multiple
Packit 4d380f
listboxes.</para></listitem>
Packit 4d380f
</varlistentry>
Packit 4d380f
<varlistentry>
Packit 4d380f
<term>NEWT_FLAG_MULTIPLE</term>
Packit 4d380f
<listitem>
Packit 4d380f
<para>By default, a listbox only lets the user select
Packit 4d380f
one item in the list at a time. When this flag is specified, they may
Packit 4d380f
select multiple items from the list.</para></listitem></varlistentry>
Packit 4d380f
</variablelist></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Once a listbox has been created, items are added to it by invoking
Packit 4d380f
<literal remap="tt">newtListboxAppendEntry()</literal>, which adds new items to the end of the list.
Packit 4d380f
In addition to the listbox component, <literal remap="tt">newtListboxAppendEntry()</literal> needs
Packit 4d380f
both elements of the (text, key) ordered pair. 
Packit 4d380f
Packit 4d380f
For lists which only allow a single selection, <literal remap="tt">newtListboxGetCurrent()</literal>
Packit 4d380f
should be used to find out which listbox item is currently selected. It
Packit 4d380f
returns the key of the currently selected item.
Packit 4d380f
Packit 4d380f
Normally, a listbox is as wide as its widest element, plus space for a
Packit 4d380f
scrollbar if the listbox is supposed to have one. To make the listbox
Packit 4d380f
any larger then that, use <literal remap="tt">newtListboxSetWidth()</literal>, which overrides the
Packit 4d380f
natural list of the listbox. Once the width has been set, it's fixed. The
Packit 4d380f
listbox will no longer grow to accommodate new entries, so bad things may
Packit 4d380f
happen! 
Packit 4d380f
Packit 4d380f
An application can change the current position of the listbox (where the
Packit 4d380f
selection bar is displayed) by calling <literal remap="tt">newtListboxSetCurrent()</literal> or
Packit 4d380f
<literal remap="tt">newtListboxSetCurrentByKey()</literal>. The first sets the current position to the
Packit 4d380f
entry number which is passed as the second argument, with 0 indicating
Packit 4d380f
the first entry. <literal remap="tt">newtListboxSetCurrentByKey()</literal> sets the current position
Packit 4d380f
to the entry whose <literal remap="tt">key</literal> is passed into the function.</para></sect3>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect3><title>Manipulating Listbox Contents</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
While the contents of many listboxes never need to change, some applications
Packit 4d380f
need to change the contents of listboxes regularly. <literal remap="tt">Newt</literal> includes
Packit 4d380f
complete support for updating listboxes. These new functions are in
Packit 4d380f
addition to <literal remap="tt">newtListboxAppendEntry()</literal>, which was already discussed.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtListboxSetEntry(newtComponent co, void * key, const char * text);
Packit 4d380f
int newtListboxInsertEntry(newtComponent co, const char * text, 
Packit 4d380f
                           const void * data, void * key);
Packit 4d380f
int newtListboxDeleteEntry(newtComponent co, void * key);
Packit 4d380f
void newtListboxClear(newtComponent co);
Packit 4d380f
</screen></para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
The first of these, <literal remap="tt">newtListboxSetEntry()</literal>, updates the text for a
Packit 4d380f
key which is already in the listbox. The <literal remap="tt">key</literal> specifies which listbox
Packit 4d380f
entry should be modified, and <literal remap="tt">text</literal> becomes the new text for that entry
Packit 4d380f
in the listbox.
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtListboxInsertEntry()</literal> inserts a new listbox entry <emphasis remap="bf">after</emphasis> an
Packit 4d380f
already existing entry, which is specified by the <literal remap="tt">key</literal> parameter.
Packit 4d380f
The <literal remap="tt">text</literal> and <literal remap="tt">data</literal> parameters specify the new entry which should
Packit 4d380f
be added.
Packit 4d380f
Packit 4d380f
Already-existing entries are removed from a listbox with
Packit 4d380f
<literal remap="tt">newtListboxDeleteEntry()</literal>. It removes the listbox entry with the
Packit 4d380f
specified <literal remap="tt">key</literal>. If you want to remove all of the entries from a
Packit 4d380f
listbox, use <literal remap="tt">newtListboxClear()</literal>.</para></sect3> 
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect3><title>Multiple Selections</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
When a listbox is created with <literal remap="tt">NEWT_FLAG_MULTIPLE</literal>, the user can select
Packit 4d380f
multiple items from the list. When this option is used, a different set of
Packit 4d380f
functions must be used to manipulate the listbox selection.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtListboxClearSelection(newtComponent co);
Packit 4d380f
void **newtListboxGetSelection(newtComponent co, int *numitems);
Packit 4d380f
void newtListboxSelectItem(newtComponent co, const void * key,
Packit 4d380f
	                   enum newtFlagsSense sense);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
The simplest of these is <literal remap="tt">newtListboxClearSelection()</literal>, which deselects
Packit 4d380f
all of the items in the list (listboxes which allow multiple selections
Packit 4d380f
also allow zero selections). <literal remap="tt">newtListboxGetSelection()</literal> returns a
Packit 4d380f
pointer to an array which contains the keys for all of the items in the
Packit 4d380f
listbox currently selected. The <literal remap="tt">int</literal> pointed to by <literal remap="tt">numitems</literal> is
Packit 4d380f
set to the number of items currently selected (and hence the number of
Packit 4d380f
items in the returned array). The returned array is dynamically allocated,
Packit 4d380f
and must be released through <literal remap="tt">free()</literal>.
Packit 4d380f
Packit 4d380f
<literal remap="tt">newtListboxSelectItem()</literal> lets the program select and deselect specific
Packit 4d380f
listbox entries. The <literal remap="tt">key</literal> of the listbox entry is being affected is
Packit 4d380f
passed, and <literal remap="tt">sense</literal> is one of <literal remap="tt">NEWT_FLAGS_RESET</literal>, which deselects
Packit 4d380f
the entry, <literal remap="tt">NEWT_FLAGS_SET</literal>, which selects the entry, or
Packit 4d380f
<literal remap="tt">NEWT_FLAGS_TOGGLE</literal>, which reverses the current selection status.</para></sect3></sect2>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect2><title>Advanced Forms</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Forms, which tie components together, are quite important in the world of
Packit 4d380f
<literal remap="tt">newt</literal>. While we've already discussed the basics of forms, we've omitted
Packit 4d380f
many of the details.</para>
Packit 4d380f
Packit 4d380f
Packit 4d380f
<sect3><title>Exiting From Forms</title>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Forms return control to the application for a number of reasons:
Packit 4d380f
Packit 4d380f
Packit 4d380f
<itemizedlist>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
A component can force the form to exit. Buttons do this whenever they
Packit 4d380f
are pushed, and other components exit when <literal remap="tt">NEWT_FLAG_RETURNEXIT</literal> has
Packit 4d380f
been specified.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
Applications can setup hot keys which cause the form to exit when
Packit 4d380f
they are pressed.</para></listitem>
Packit 4d380f
<listitem>
Packit 4d380f
Packit 4d380f
<para>
Packit 4d380f
<literal remap="tt">Newt</literal> can exit when file descriptors are ready to be read or
Packit 4d380f
ready to be written to.
Packit 4d380f
</para>
Packit 4d380f
</listitem></itemizedlist>
Packit 4d380f
Packit 4d380f
By default, <literal remap="tt">newt</literal> forms exit when the F12 key is pressed (F12 is setup
Packit 4d380f
as a hot key by default). <literal remap="tt">Newt</literal> applications should treat F12 as an
Packit 4d380f
``Ok'' button. If applications don't want F12 to exit the form, they can
Packit 4d380f
specify <literal remap="tt">NEWT_FLAG_NOF12</literal> as flag when creating the form with 
Packit 4d380f
<literal remap="tt">newtForm</literal>.
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtFormAddHotKey(newtComponent co, int key);
Packit 4d380f
void newtFormWatchFd(newtComponent form, int fd, int fdFlags);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
void newtDrawForm(newtComponent form);
Packit 4d380f
newtComponent newtFormGetCurrent(newtComponent co);
Packit 4d380f
void newtFormSetCurrent(newtComponent co, newtComponent subco);
Packit 4d380f
void newtFormRun(newtComponent co, struct newtExitStruct * es);
Packit 4d380f
</screen>
Packit 4d380f
Packit 4d380f
Packit 4d380f
Packit 4d380f
<screen>
Packit 4d380f
newtComponent newtForm(newtComponent vertBar, const char * help, int flags);
Packit 4d380f
void newtFormSetBackground(newtComponent co, int color);
Packit 4d380f
void newtFormSetHeight(newtComponent co, int height);
Packit 4d380f
void newtFormSetWidth(newtComponent co, int width);
Packit 4d380f
</screen>
Packit 4d380f
</para>
Packit 4d380f
</sect3>
Packit 4d380f
</sect2>
Packit 4d380f
</sect1>
Packit 4d380f
</article>
Packit 4d380f