This commit is contained in:
gferg 2005-06-17 23:13:49 +00:00
parent 86753d3183
commit a371fae577
38 changed files with 3185 additions and 3148 deletions

View File

@ -1,7 +1,7 @@
<!--Title : NCurses Programming Howto"
Author : Pradeep Padala
E-Mail : p_padala@yahoo.com
Web-Page : http://www.cise.ufl.edu/~ppadala
E-Mail : ppadala@gmail.com
Web-Page : http://www.eecs.umich.edu/~ppadala
-->
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
@ -13,11 +13,21 @@
<firstname> Pradeep </firstname>
<surname> Padala </surname>
<affiliation>
<address><email>p_padala@yahoo.com</email></address>
<address><email>ppadala@gmail.com</email></address>
</affiliation>
</author>
<revhistory>
<revision>
<revnumber>1.8</revnumber>
<date>2005-06-17</date>
<authorinitials>ppadala</authorinitials>
<revremark>Lots of updates. Added references and perl examples.
Changes to examples. Many grammatical and stylistic changes to the
content. Changes to Ncurses history.
</revremark>
</revision>
<revision>
<revnumber>1.7.1</revnumber>
<date>2002-06-25</date>
@ -31,7 +41,7 @@
<revnumber>1.7</revnumber>
<date>2002-06-25</date>
<authorinitials>ppadala</authorinitials>
<revremark>Added "Other formats" section and made lot of fancy
<revremark>Added "Other formats" section and made a lot of fancy
changes to the programs. Inlining of programs is gone.
</revremark>
</revision>
@ -48,7 +58,7 @@
<revnumber>1.6</revnumber>
<date>2002-02-16</date>
<authorinitials>ppadala</authorinitials>
<revremark>Corrected lot of spelling mistakes, added ACS variables
<revremark>Corrected a lot of spelling mistakes, added ACS variables
section </revremark>
</revision>
@ -71,8 +81,8 @@
<revnumber>1.3</revnumber>
<date>2001-07-24</date>
<authorinitials>ppadala</authorinitials>
<revremark>Added copyright notice(LDP license) to main document,
Put copyright notice (GPL) for programs as well, Corrected
<revremark>Added copyright notices to main document (LDP license)
and programs (GPL), Corrected
printw_example.
</revremark>
</revision>
@ -95,16 +105,14 @@
</revision>
</revhistory>
<pubdate>v1.7.1, 2002-06-25</pubdate>
<pubdate>v1.8, 2005-06-17</pubdate>
<abstract>
<para>
<emphasis>
This document is intended to be an "All in One" guide for programming with
ncurses and its sister libraries. We graduate from a simple "Hello World"
program to more complex form manipulation. No prior experience in ncurses is
assumed. Latest version of the document can always be found at
<ulink url="http://www.cise.ufl.edu/~ppadala/ncurses/NCURSES_HOWTO/">my web site
</ulink>. Send comments to <ulink url="mailto:p_padala@yahoo.com">this address
assumed. Send comments to <ulink url="mailto:ppadala@gmail.com">this address
</ulink>
</emphasis>
</para>
@ -116,13 +124,13 @@ assumed. Latest version of the document can always be found at
<para>
In the olden days of teletype terminals, terminals were away from computers and
were connected to them through serial cables. The terminals could be configured
by sending a series of bytes to each of them. All the capabilities (such as
by sending a series of bytes. All the capabilities (such as
moving the cursor to a new location, erasing part of the screen, scrolling the
screen, changing modes, changing appearance, colors, brightness, blinking,
underlining, reverse video etc.) of terminals could be accessed through these
series of bytes which are usually called escape sequences because they start
screen, changing modes etc.) of terminals could be accessed through these
series of bytes. These control seeuqnces are usually called escape sequences,
because they start
with an escape(0x1B) character. Even today, with proper emulation, we can send
escape sequences to the emulator and achieve the same effect on the terminal
escape sequences to the emulator and achieve the same effect on a terminal
window.
</para>
@ -136,7 +144,7 @@ echo "^[[0;31;40mIn Color"
<para>
The first character is an escape character, which looks like two characters ^
and [. To be able to print that you have to press CTRL+V and then the ESC key.
and [. To be able to print it, you have to press CTRL+V and then the ESC key.
All the others are normal printable characters. You should be able to see the
string "In Color" in red. It stays that way and to revert back to the original
mode type this.
@ -147,28 +155,28 @@ echo "^[[0;37;40m"
</programlisting>
<para>
Now, what do those magic characters mean? Difficult to comprehend? They might
Now, what do these magic characters mean? Difficult to comprehend? They might
even be different for different terminals. So the designers of UNIX invented a
mechanism named <literal remap="tt">termcap</literal>. It is a file that
lists all the capabilities of a particular terminal, along with the escape
sequences needed to achieve a particular effect. In the later years, this was
replaced by <literal remap="tt"> terminfo</literal>. Without delving too
much into details, the concept of the mechanism is to allow application
programs query the terminfo database and obtain the control characters to be
sent to the terminal or terminal emulator.
replaced by <literal remap="tt">terminfo</literal>. Without delving too
much into details, this mechanism allows application
programs to query the terminfo database and obtain the control characters to be
sent to a terminal or terminal emulator.
</para>
<sect2 id="whatis"> <title> What is NCURSES? </title>
<para>
You might be wondering, what the import of all this technical gibberish is. In
the above scenario, every application program is supposed to query the terminfo
and do the necessary stuff(sending control characters etc.). It soon became
and perform the necessary stuff (sending control characters etc.). It soon became
difficult to manage this complexity and this gave birth to 'CURSES'. Curses is
a pun on the name "cursor optimization". The Curses library forms a wrapper
over working with raw terminal codes, and provides highly flexible and
efficient API (Application Programming Interface). It provides functions to
move the cursor, create windows, produce colors, play with mouse etc. The
Application programs need not worry about the underlying terminal capabilities.
application programs need not worry about the underlying terminal capabilities.
</para>
<para>
@ -180,21 +188,10 @@ document, the terms curses and ncurses are used interchangeably.
</para>
<para>
The <literal remap="tt">ncurses</literal> package was originated by Pavel
Curtis. The original maintainer of this package is <ulink
url="mailto:zmbenhal@netcom.com">Zeyd Ben-Halim</ulink>
<email>zmbenhal@netcom.com</email>. <ulink
url="mailto:esr@snark.thyrsus.com">Eric S. Raymond</ulink>
<email>esr@snark.thyrsus.com</email> wrote many of the new features in versions
after 1.8.1. <ulink url="mailto:juergen.pfeifer@gmx.net">J&uuml;rgen
Pfeifer</ulink> wrote all of the menu and forms code as well as the <ulink
url="http://www.adahome.com">Ada95</ulink> binding. Ongoing work is being done
by <ulink url="mailto:dickey@herndon4.his.com">Thomas Dickey</ulink> and
<ulink url="mailto:juergen.pfeifer@gmx.net">J&uuml;rgen Pfeifer</ulink>.
<ulink url="mailto:florian@gnu.org">Florian La Roche</ulink> acts as the
maintainer for the Free Software Foundation, which holds the copyright on
ncurses. Contact the current maintainers at <ulink url=
"mailto:bug-ncurses@gnu.org">bug-ncurses@gnu.org</ulink>.
A detailed history of Ncurses can be found in the NEWS file from the source
distribution. The current package is maintained by
<ulink url="mailto:dickey@his.com">Thomas Dickey</ulink>.
You can contact the maintainers at <ulink url="mailto:bug-ncurses@gnu.org">bug-ncurses@gnu.org</ulink>.
</para>
</sect2>
@ -238,8 +235,7 @@ Ncurses can be obtained from <ulink url=
"ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz">
ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</ulink> or any of the ftp
sites mentioned in <ulink url="http://www.gnu.org/order/ftp.html">
http://www.gnu.org/order/ftp.html</ulink>. The latest stable release is 5.2
20001021.
http://www.gnu.org/order/ftp.html</ulink>.
</para>
<para>
@ -275,14 +271,15 @@ command after becoming root.
This document is intended to be a "All in One" guide for programming with
ncurses and its sister libraries. We graduate from a simple "Hello World"
program to more complex form manipulation. No prior experience in ncurses is
assumed.
assumed. The writing is informal, but a lot of detail is provided for
each of the examples.
</para>
</sect2>
<sect2 id="aboutprograms"><title>About the Programs</title>
<para>
All the programs in the document are available in zipped form
<ulink url="http://www.linuxdoc.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz">
<ulink url="http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz">
here</ulink>. Unzip and untar it. The directory structure looks like this.
</para>
@ -297,6 +294,8 @@ ncurses
|----&gt; forms -- programs related to form library
|----&gt; menus -- programs related to menus library
|----&gt; panels -- programs related to panels library
|----&gt; perl -- perl equivalents of the examples (contributed
| by Anuradha Ratnaweera)
|----&gt; Makefile -- the top level Makefile
|----&gt; README -- the top level README file. contains instructions
|----&gt; COPYING -- copyright notice
@ -364,6 +363,9 @@ JustForFun
|----&gt; panel_hide.c -- Hiding and Un hiding of panels
|----&gt; panel_resize.c -- Moving and resizing of panels
|----&gt; panel_simple.c -- A simple panel example
perl
|----&gt; 01-10.pl -- Perl equivalents of first ten example programs
</programlisting>
<para>
@ -374,8 +376,8 @@ contains a README file explaining the purpose of each c file in the directory.
</para>
<para>
For every example I have given the path name for the file relative to the
ncurses directory.
For every example, I have included path name for the file relative to the
examples directory.
</para>
<simpara> If you prefer you can browse individual programs at <ulink
@ -385,8 +387,7 @@ http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/
<para>
All the programs are released under <ulink url=
"http://www.gnu.org/copyleft/gpl.txt">GPL</ulink> and you can use them for any
thing you like.
"http://www.gnu.org/copyleft/gpl.txt">GPL</ulink>.
</para>
</sect2>
@ -460,7 +461,7 @@ If above links are broken or if you want to experiment with sgml read on.
<para>
See <ulink url="http://www.tldp.org/LDP/LDP-Author-Guide/">
LDP Author guide</ulink> for more details. If all else failes, mail me at
<ulink url= "p_padala@yahoo.com">p_padala@yahoo.com</ulink>
<ulink url= "ppadala@gmail.com">ppadala@gmail.com</ulink>
</para>
</sect3>
@ -475,12 +476,16 @@ I rewrote it with few excerpts taken from his initial work. Emre helped in
writing printw and scanw sections.
</para>
<para>
Perl equivalents of the example programs are contributed by <ulink
url="mailto:Aratnaweera@virtusa.com">Anuradha Ratnaweera</ulink>.
</para>
<para>
Then comes <ulink url="mailto:parimi@ece.arizona.edu">Ravi Parimi</ulink>, my
dearest friend, who has been on this project before even one line was written.
He constantly bombarded me with suggestions and patiently reviewed the whole
text. He also checked each program on Linux and Solaris. See his notes to check
on your problems.
text. He also checked each program on Linux and Solaris.
</para>
</sect2>
@ -489,22 +494,21 @@ on your problems.
<para>
This is the wish list, in the order of priority. If you have a wish or you want
to work on completing the wish, mail <ulink url="mailto:p_padala@yahoo.com">me
to work on completing the wish, mail <ulink url="mailto:ppadala@gmail.com">me
</ulink>.
</para>
<itemizedlist>
<listitem>
<para>Add examples to last parts of forms section. (I am working on it) </para>
<para>Add examples to last parts of forms section.</para>
</listitem>
<listitem>
<para>
Prepare a Demo showing all the programs and allow the user to browse through
description of each program. Let the user compile and see the program in action.
A dialog based interface is preferred. (My friend <ulink
url="mailto:ashoknn@cisco.com">N.N.Ashok</ulink> is working on it)
A dialog based interface is preferred.
</para>
</listitem>
@ -518,15 +522,15 @@ package.</para>
</listitem>
<listitem><para>Working on two terminals simultaneously.</para></listitem>
<listitem><para>Add things in miscellaneous section.</para></listitem>
<listitem><para>Add more stuff to miscellaneous section.</para></listitem>
</itemizedlist>
</sect2>
<sect2 id="copyright"><title>Copyright</title>
<para>
Copyright (c) 2001 by Pradeep Padala. This document may be distributed under the
Copyright &copy; 2001 by Pradeep Padala. This document may be distributed under the
terms set forth in the LDP license at <ulink url=
"http://www.linuxdoc.org/COPYRIGHT.html">linuxdoc.org/COPYRIGHT.html</ulink>.
"http://www.tldp.org/COPYRIGHT.html">tldp.org/COPYRIGHT.html</ulink>.
</para>
<para>
@ -540,20 +544,20 @@ without even the implied warranty of <emphasis remap="bf">merchantability
</sect2>
</sect1>
<sect1 id="helloworld"><title>The Hello World Program</title>
<sect1 id="helloworld"><title>Hello World !!!</title>
<para>
Welcome to the world of curses. Before we plunge into the library and look into
its various features, bells and whistles, let's write a simple program and say
its various features, let's write a simple program and say
hello to the world.
</para>
<sect2 id="compilecurses"><title> Compiling With the Ncurses Library </title>
<para>
To use ncurses library functions, you have to include ncurses.h and to link the
program with ncurses library the flag -lncurses should be added. ncurses.h
already includes stdio.h.
To use ncurses library functions, you have to include ncurses.h in your
programs. To link the
program with ncurses the flag -lncurses should be added.
</para>
<programlisting>
@ -583,16 +587,16 @@ end curses mode. Let's dissect it line by line.
<sect3 id="about-initscr"><title>About initscr()</title>
<para>
The function initscr() initializes the terminal in curses mode. In some
implementations it clears the screen and presents a blank screen. To do any
implementations, it clears the screen and presents a blank screen. To do any
screen manipulation using curses package this has to be called first. This
function initializes the curses system and allocates memory for our present
window which is called 'stdscr' and some other data-structures. Under extreme
window (called <literal remap="tt">stdscr</literal>) and some other data-structures. Under extreme
cases this function might fail due to insufficient memory to allocate memory
for curses library's data structures.
</para>
<para>
After this is done we can do a variety of initializations to customize
After this is done, we can do a variety of initializations to customize
our curses settings. These details will be explained <link linkend="init">
later </link>.
</para>
@ -604,17 +608,17 @@ later </link>.
<para>
The next line printw prints the string "Hello World !!!" on to the screen. This
function is analogous to normal printf in all respects except that it prints
the data in a window called stdscr at the current (y,x) co-ordinates. Since our
the data on a window called stdscr at the current (y,x) co-ordinates. Since our
present co-ordinates are at 0,0 the string is printed at the left hand corner
of the window.
</para>
<para>
This brings us to that mysterious refresh(). Well, when we did printw actually
the data is written to an imaginary window called stdscr, which is not updated
This brings us to that mysterious refresh(). Well, when we called printw
the data is actually written to an imaginary window, which is not updated
on the screen yet. The job of printw is to update a few flags
and data structures and write the data to a buffer corresponding to stdscr.
In order to bring it to the screen we need to call refresh() and tell the
In order to show it on the screen, we need to call refresh() and tell the
curses system to dump the contents on the screen.
</para>
@ -622,7 +626,7 @@ curses system to dump the contents on the screen.
The philosophy behind all this is to allow the programmer to do multiple updates
on the imaginary screen or windows and do a refresh once all his screen update
is done. refresh() checks the window and updates only the portion which has been
changed. This gives good response and offers greater flexibility too. But it is
changed. This improves performance and offers greater flexibility too. But, it is
sometimes frustrating to beginners. A common mistake committed by beginners is
to forget to call refresh() after they did some update through printw() class of
functions. I still forget to add it sometimes :-)
@ -633,7 +637,7 @@ functions. I still forget to add it sometimes :-)
<para>
And finally don't forget to end the curses mode. Otherwise your terminal might
behave strangely after the program quits. endwin() frees the memory taken by
curses sub-system and it's data structures and puts the terminal in normal
curses sub-system and its data structures and puts the terminal in normal
mode. This function must be called after you are done with the curses mode.
</para>
</sect3>
@ -660,7 +664,7 @@ in raw mode or initialize color or initialize the mouse etc.. Let's discuss some
of the functions that are normally called immediately after initscr();
</para>
<sect2 id="aboutinit"><title>About Initialization functions like raw() etc...</title>
<sect2 id="aboutinit"><title>Initialization functions</title>
<para> </para>
</sect2>
@ -815,7 +819,7 @@ with stdscr as the window parameter.
</para>
</sect1>
<sect1 id="printw"><title>About output functions like printw() </title>
<sect1 id="printw"><title>Output functions</title>
<para>
I guess you can't wait any more to see some action. Back to our odyssey of
@ -1012,7 +1016,7 @@ in the <link linkend="windows"> windows </link> section.
</sect2>
</sect1>
<sect1 id="scanw"><title>About input functions like scanw() </title>
<sect1 id="scanw"><title>Input functions</title>
<para>
Well, printing without taking input, is boring. Let's see functions which
@ -1272,7 +1276,7 @@ explained in detail later. Use 0 for no color.
</sect2>
</sect1>
<sect1 id="windows"><title>All about window functions</title>
<sect1 id="windows"><title>Windows</title>
<para>
Windows form the most important concept in curses. You have seen the standard
window stdscr above where all the functions implicitly operated on this window.
@ -1353,14 +1357,14 @@ it produces some thing like
</para>
<programlisting>
+-----------------------+
+------------+
| |
| |
| |
| |
| |
| |
+-----------------------+
+------------+
</programlisting>
</sect2>
@ -1398,7 +1402,7 @@ length at the specified position.
</sect2>
</sect1>
<sect1 id="color"><title>All about color</title>
<sect1 id="color"><title>Colors</title>
<sect2 id="colorbasics"><title> The basics </title>
<para>
Life seems dull with no colors. Curses has a nice mechanism to handle colors.
@ -1413,8 +1417,8 @@ Let's get into the thick of the things with a small program.
<para>
As you can see, to start using color, you should first call the function
<literal remap="tt"> start_color()</literal>. After that you can use color
capabilities of your terminals by various functions. To find out whether
<literal remap="tt"> start_color()</literal>. After that, you can use color
capabilities of your terminals using various functions. To find out whether a
terminal has color capabilities or not, you can use
<literal remap="tt">has_colors()</literal> function, which returns FALSE if
the terminal does not support color.
@ -1486,8 +1490,7 @@ content and foreground, background combination for the pair.
</sect2>
</sect1>
<sect1 id="keys"> <title>Key management. How to read function keys, arrow keys etc..
</title>
<sect1 id="keys"> <title>Interfacing with the key board</title>
<sect2 id="keysbasics"><title>The Basics </title>
@ -3558,7 +3561,7 @@ The following requests support editing the field and changing the edit mode:
Clear to end of line. </para> </listitem>
<listitem><para><emphasis>REQ_CLR_EOF</emphasis>
Clear to end of field. </para> </listitem>
<listitem><para><emphasis>REQ_CLEAR_FIELD</emphasis>
<listitem><para><emphasis>REQ_CLR_FIELD</emphasis>
Clear entire field. </para> </listitem>
</itemizedlist>
@ -3680,8 +3683,8 @@ The kit provides some useful widgets, which can be used in your programs
directly. It's pretty well written and the documentation is very good. The
examples in the examples directory can be a good place to start for beginners.
The CDK can be downloaded from <ulink
url="http://www.vexus.ca/release/cdk.tar.gz">
http://www.vexus.ca/release/cdk.tar.gz </ulink>. Follow the instructions in
url="http://invisible-island.net/cdk/">http://invisible-island.net/cdk/</ulink>
. Follow the instructions in
README file to install it.
</para>
@ -3737,6 +3740,10 @@ Viewer This is a file/information viewer. Very useful
when you need to display loads of information.
===========================================================================
</programlisting>
<para>
A few of the widgets are modified by Thomas Dickey in recent versions.
</para>
</sect3>
<sect3 id="cdkattract"><title> Some Attractive Features </title>
@ -3793,9 +3800,8 @@ examples of how and where it can be used
<para>
As he explains, dialog is a real gem in making professional-looking dialog boxes
with ease. It creates a variety of dialog boxes, menus, check lists etc.. It is
usually installed by default. If not, you can find it at <ulink
url="http://www.ibiblio.org/pub/Linux/utils/shell/dialog-0.61a.tar.gz"> ibiblio
linux archive</ulink>.
usually installed by default. If not, you can download it from <ulink
url="http://invisible-island.net/dialog/">Thomas Dickey</ulink>'s site.
</para>
<para>
@ -3828,6 +3834,11 @@ functionality. Though the widgets provided are very rudimentary, these modules
provide good access to curses library from perl.
</para>
<para>
Some of my code examples are converted to perl by Anuradha Ratnaweera and they
are available in the <literal remap="tt">perl</literal> directory.
</para>
<para>
For more information see man pages Curses(3) , Curses::Form(3) and
Curses::Widgets(3). These pages are installed only when the above modules are
@ -3843,7 +3854,7 @@ This section contains few programs written by me just for fun. They don't
signify a better programming practice or the best way of using ncurses. They are
provided here so as to allow beginners to get ideas and add more programs to
this section. If you have written a couple of nice, simple programs in curses
and want them to included here, contact <ulink url="mailto:p_padala@yahoo.com">
and want them to included here, contact <ulink url="mailto:ppadala@gmail.com">
me</ulink>.
</para>
@ -3945,6 +3956,23 @@ can be helpful.
</para>
</sect2>
</sect1>
<sect1 id="ref"> <title>References</title>
<itemizedlist>
<listitem><para>NCurses man pages </para> </listitem>
<listitem><para>NCurses FAQ at <ulink
url="http://invisible-island.net/ncurses/ncurses.faq.html">
http://invisible-island.net/ncurses/ncurses.faq.html</ulink>
</para> </listitem>
<listitem><para>Writing programs with NCURSES by Eric Raymond and Zeyd M.
Ben-Halim at
<ulink
url="http://invisible-island.net/ncurses/ncurses-intro.html">http://invisible-island.net/ncurses/ncurses-intro.html</ulink> - somewhat
obsolete. I was inspired by this document and the structure of this HOWTO
follows from the original document</para></listitem>
</itemizedlist>
</sect1>
</article>
<!-- Keep this comment at the end of the file

View File

@ -1,107 +1,107 @@
#include <curses.h>
int STARTX = 0;
int STARTY = 0;
int ENDX = 79;
int ENDY = 24;
#define CELL_CHAR '#'
#define TIME_OUT 300
typedef struct _state {
int oldstate;
int newstate;
}state;
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy);
void calc(state **area, int x, int y);
void update_state(state **area, int startx, int starty, int endx, int endy);
int main()
{ state **workarea;
int i, j;
initscr();
cbreak();
timeout(TIME_OUT);
keypad(stdscr, TRUE);
ENDX = COLS - 1;
ENDY = LINES - 1;
workarea = (state **)calloc(COLS, sizeof(state *));
for(i = 0;i < COLS; ++i)
workarea[i] = (state *)calloc(LINES, sizeof(state));
/* For inverted U */
workarea[39][15].newstate = TRUE;
workarea[40][15].newstate = TRUE;
workarea[41][15].newstate = TRUE;
workarea[39][16].newstate = TRUE;
workarea[39][17].newstate = TRUE;
workarea[41][16].newstate = TRUE;
workarea[41][17].newstate = TRUE;
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
/* For block */
/*
workarea[37][13].newstate = TRUE;
workarea[37][14].newstate = TRUE;
workarea[38][13].newstate = TRUE;
workarea[38][14].newstate = TRUE;
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
*/
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
while(getch() != KEY_F(1))
{ for(i = STARTX; i <= ENDX; ++i)
for(j = STARTY; j <= ENDY; ++j)
calc(workarea, i, j);
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
}
endwin();
return 0;
}
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy)
{ int i, j;
wclear(win);
for(i = startx; i <= endx; ++i)
for(j = starty;j <= endy; ++j)
if(area[i][j].newstate == TRUE)
mvwaddch(win, j, i, CELL_CHAR);
wrefresh(win);
}
void calc(state **area, int i, int j)
{ int neighbours;
int newstate;
neighbours =
area[(i - 1 + COLS) % COLS][j].oldstate +
area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate +
area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate +
area[(i + 1) % COLS][j].oldstate +
area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate +
area[(i + 1) % COLS][(j + 1) % LINES].oldstate +
area[i][(j - 1 + LINES) % LINES].oldstate +
area[i][(j + 1) % LINES].oldstate;
newstate = FALSE;
if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3))
newstate = TRUE;
else
if(area[i][j].oldstate == FALSE && neighbours == 3)
newstate = TRUE;
area[i][j].newstate = newstate;
}
void update_state(state **area, int startx, int starty, int endx, int endy)
{ int i, j;
for(i = startx; i <= endx; ++i)
for(j = starty; j <= endy; ++j)
area[i][j].oldstate = area[i][j].newstate;
}
#include <curses.h>
int STARTX = 0;
int STARTY = 0;
int ENDX = 79;
int ENDY = 24;
#define CELL_CHAR '#'
#define TIME_OUT 300
typedef struct _state {
int oldstate;
int newstate;
}state;
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy);
void calc(state **area, int x, int y);
void update_state(state **area, int startx, int starty, int endx, int endy);
int main()
{ state **workarea;
int i, j;
initscr();
cbreak();
timeout(TIME_OUT);
keypad(stdscr, TRUE);
ENDX = COLS - 1;
ENDY = LINES - 1;
workarea = (state **)calloc(COLS, sizeof(state *));
for(i = 0;i < COLS; ++i)
workarea[i] = (state *)calloc(LINES, sizeof(state));
/* For inverted U */
workarea[39][15].newstate = TRUE;
workarea[40][15].newstate = TRUE;
workarea[41][15].newstate = TRUE;
workarea[39][16].newstate = TRUE;
workarea[39][17].newstate = TRUE;
workarea[41][16].newstate = TRUE;
workarea[41][17].newstate = TRUE;
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
/* For block */
/*
workarea[37][13].newstate = TRUE;
workarea[37][14].newstate = TRUE;
workarea[38][13].newstate = TRUE;
workarea[38][14].newstate = TRUE;
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
*/
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
while(getch() != KEY_F(1))
{ for(i = STARTX; i <= ENDX; ++i)
for(j = STARTY; j <= ENDY; ++j)
calc(workarea, i, j);
update_state(workarea, STARTX, STARTY, ENDX, ENDY);
display(stdscr, workarea, STARTX, STARTY, ENDX, ENDY);
}
endwin();
return 0;
}
void display(WINDOW *win, state **area, int startx, int starty, int endx, int endy)
{ int i, j;
wclear(win);
for(i = startx; i <= endx; ++i)
for(j = starty;j <= endy; ++j)
if(area[i][j].newstate == TRUE)
mvwaddch(win, j, i, CELL_CHAR);
wrefresh(win);
}
void calc(state **area, int i, int j)
{ int neighbours;
int newstate;
neighbours =
area[(i - 1 + COLS) % COLS][j].oldstate +
area[(i - 1 + COLS) % COLS][(j - 1 + LINES) % LINES].oldstate +
area[(i - 1 + COLS) % COLS][(j + 1) % LINES].oldstate +
area[(i + 1) % COLS][j].oldstate +
area[(i + 1) % COLS][(j - 1 + LINES) % LINES].oldstate +
area[(i + 1) % COLS][(j + 1) % LINES].oldstate +
area[i][(j - 1 + LINES) % LINES].oldstate +
area[i][(j + 1) % LINES].oldstate;
newstate = FALSE;
if(area[i][j].oldstate == TRUE && (neighbours == 2 || neighbours == 3))
newstate = TRUE;
else
if(area[i][j].oldstate == FALSE && neighbours == 3)
newstate = TRUE;
area[i][j].newstate = newstate;
}
void update_state(state **area, int startx, int starty, int endx, int endy)
{ int i, j;
for(i = startx; i <= endx; ++i)
for(j = starty; j <= endy; ++j)
area[i][j].oldstate = area[i][j].newstate;
}

View File

@ -1,160 +1,161 @@
#include <curses.h>
#define STARTX 9
#define STARTY 3
#define WIDTH 6
#define HEIGHT 4
#define TRACE_VALUE TRACE_MAXIMUM
void board( WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
void magic(int **, int);
void print(int **, int);
void magic_board(int **a,int n);
int main(int argc, char *argv[])
{
int **a,n,i;
if(argc != 2)
{ printf("Usage: %s <magic sqaure order>\n", argv[0]);
exit(0);
}
n = atoi(argv[1]);
if(n % 2 == 0)
{ printf("Sorry !!! I don't know how to create magic square of even order\n");
printf("The order should be an odd number\n");
exit(0);
}
a = (int **) malloc(n * sizeof(int*));
for(i = 0;i < n;++i)
a[i] = (int *)malloc(n * sizeof(int));
magic(a,n);
initscr();
curs_set(0);
noecho();
magic_board(a,n);
getch();
endwin();
return;
}
void magic(int **a, int n)
{
int i,j,k;
int row,col;
for(i = 0;i < n;++i)
for(j = 0;j < n;++j)
a[i][j] = -1;
row = 0;
col = n / 2;
k = 1;
a[row][col] = k;
while(k != n * n)
{
if(row == 0 && col != n - 1)
{ row = n - 1;
col ++;
a[row][col] = ++k;
}
else if(row != 0 && col != n - 1)
{ if(a[row - 1][col + 1] == -1)
{ row --;
col ++;
a[row][col] = ++k;
}
else
{
row ++;
a[row][col] = ++k;
}
}
else if(row != 0 && col == n - 1)
{
row --;
col = 0;
a[row][col] = ++k;
}
else if(row == 0 && col == n - 1)
{ row ++;
a[row][col] = ++k;
}
}
return;
}
void print(int **a,int n)
{ int i,j;
int x,y;
x = STARTX;
y = STARTY;
mvprintw(1,30,"MAGIC SQUARE");
for(i = 0;i < n;++i)
{ for(j = 0;j < n;++j)
{ mvprintw(y,x,"%d",a[i][j]);
if(n > 9)
x += 4;
else
x += 6;
}
x = STARTX;
if(n > 7)
y += 2;
else
y += 3;
}
refresh();
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;
endy = starty + lines * tile_height;
endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
void magic_board(int **a,int n)
{ int i,j, deltax, deltay;
int startx, starty;
starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for(i = 0;i < n; ++i)
for(j = 0; j < n; ++j)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%d", a[i][j]);
}
#include <curses.h>
#include <stdlib.h>
#define STARTX 9
#define STARTY 3
#define WIDTH 6
#define HEIGHT 4
#define TRACE_VALUE TRACE_MAXIMUM
void board( WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
void magic(int **, int);
void print(int **, int);
void magic_board(int **a,int n);
int main(int argc, char *argv[])
{
int **a,n,i;
if(argc != 2)
{ printf("Usage: %s <magic sqaure order>\n", argv[0]);
exit(0);
}
n = atoi(argv[1]);
if(n % 2 == 0)
{ printf("Sorry !!! I don't know how to create magic square of even order\n");
printf("The order should be an odd number\n");
exit(0);
}
a = (int **) malloc(n * sizeof(int*));
for(i = 0;i < n;++i)
a[i] = (int *)malloc(n * sizeof(int));
magic(a,n);
initscr();
curs_set(0);
noecho();
magic_board(a,n);
getch();
endwin();
return;
}
void magic(int **a, int n)
{
int i,j,k;
int row,col;
for(i = 0;i < n;++i)
for(j = 0;j < n;++j)
a[i][j] = -1;
row = 0;
col = n / 2;
k = 1;
a[row][col] = k;
while(k != n * n)
{
if(row == 0 && col != n - 1)
{ row = n - 1;
col ++;
a[row][col] = ++k;
}
else if(row != 0 && col != n - 1)
{ if(a[row - 1][col + 1] == -1)
{ row --;
col ++;
a[row][col] = ++k;
}
else
{
row ++;
a[row][col] = ++k;
}
}
else if(row != 0 && col == n - 1)
{
row --;
col = 0;
a[row][col] = ++k;
}
else if(row == 0 && col == n - 1)
{ row ++;
a[row][col] = ++k;
}
}
return;
}
void print(int **a,int n)
{ int i,j;
int x,y;
x = STARTX;
y = STARTY;
mvprintw(1,30,"MAGIC SQUARE");
for(i = 0;i < n;++i)
{ for(j = 0;j < n;++j)
{ mvprintw(y,x,"%d",a[i][j]);
if(n > 9)
x += 4;
else
x += 6;
}
x = STARTX;
if(n > 7)
y += 2;
else
y += 3;
}
refresh();
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;
endy = starty + lines * tile_height;
endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
void magic_board(int **a,int n)
{ int i,j, deltax, deltay;
int startx, starty;
starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for(i = 0;i < n; ++i)
for(j = 0; j < n; ++j)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%d", a[i][j]);
}

View File

@ -1,122 +1,122 @@
#include <stdio.h>
#include <curses.h>
#define QUEEN_CHAR '*'
int *nqueens(int num);
int place(int current, int *position);
int print(int *positions, int num_queens);
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
int main(int argc, char *argv[])
{
int num_queens, *positions, count;
if(argc != 2)
{ printf("Usage: %s <number of queens (chess board order)>\n", argv[0]);
exit(1);
}
num_queens = atoi(argv[1]);
initscr();
cbreak();
keypad(stdscr, TRUE);
positions = nqueens(num_queens);
free(positions);
endwin();
return 0;
}
int *nqueens(int num)
{
int current, *position, num_solutions = 0;
position = (int *) calloc(num + 1, sizeof(int));
position[1] = 0;
current = 1; /* current queen is being checked */
/* position[current] is the coloumn*/
while(current > 0){
position[current] += 1;
while(position[current] <= num && !place(current, position) )
position[current] += 1;
if(position[current] <= num){
if(current == num) {
++num_solutions;
print(position, num);
}
else {
current += 1;
position[current] = 0;
}
}
else current -= 1; /* backtrack */
}
printf("Total Number of Solutions : %d\n", num_solutions);
return(position);
}
int place(int current, int *position)
{
int i;
if(current == 1) return(1);
for(i = 1; i < current; ++i)
if(position[i] == position[current]) return(0);
else if(abs(position[i] - position[current]) ==
abs(i - current))
return(0);
return(1);
}
int print(int *positions, int num_queens)
{ int count;
int y = 2, x = 2, w = 4, h = 2;
static int solution = 1;
mvprintw(0, 0, "Solution No: %d", solution++);
board(stdscr, y, x, num_queens, num_queens, w, h);
for(count = 1; count <= num_queens; ++count)
{ int tempy = y + (count - 1) * h + h / 2;
int tempx = x + (positions[count] - 1) * w + w / 2;
mvaddch(tempy, tempx, QUEEN_CHAR);
}
refresh();
mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)");
if(getch() == KEY_F(1))
{ endwin();
exit(0);
}
clear();
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;
endy = starty + lines * tile_height;
endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
#include <stdio.h>
#include <curses.h>
#define QUEEN_CHAR '*'
int *nqueens(int num);
int place(int current, int *position);
int print(int *positions, int num_queens);
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
int main(int argc, char *argv[])
{
int num_queens, *positions, count;
if(argc != 2)
{ printf("Usage: %s <number of queens (chess board order)>\n", argv[0]);
exit(1);
}
num_queens = atoi(argv[1]);
initscr();
cbreak();
keypad(stdscr, TRUE);
positions = nqueens(num_queens);
free(positions);
endwin();
return 0;
}
int *nqueens(int num)
{
int current, *position, num_solutions = 0;
position = (int *) calloc(num + 1, sizeof(int));
position[1] = 0;
current = 1; /* current queen is being checked */
/* position[current] is the coloumn*/
while(current > 0){
position[current] += 1;
while(position[current] <= num && !place(current, position) )
position[current] += 1;
if(position[current] <= num){
if(current == num) {
++num_solutions;
print(position, num);
}
else {
current += 1;
position[current] = 0;
}
}
else current -= 1; /* backtrack */
}
printf("Total Number of Solutions : %d\n", num_solutions);
return(position);
}
int place(int current, int *position)
{
int i;
if(current == 1) return(1);
for(i = 1; i < current; ++i)
if(position[i] == position[current]) return(0);
else if(abs(position[i] - position[current]) ==
abs(i - current))
return(0);
return(1);
}
int print(int *positions, int num_queens)
{ int count;
int y = 2, x = 2, w = 4, h = 2;
static int solution = 1;
mvprintw(0, 0, "Solution No: %d", solution++);
board(stdscr, y, x, num_queens, num_queens, w, h);
for(count = 1; count <= num_queens; ++count)
{ int tempy = y + (count - 1) * h + h / 2;
int tempx = x + (positions[count] - 1) * w + w / 2;
mvaddch(tempy, tempx, QUEEN_CHAR);
}
refresh();
mvprintw(LINES - 2, 0, "Press Any Key to See next solution (F1 to Exit)");
if(getch() == KEY_F(1))
{ endwin();
exit(0);
}
clear();
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;
endy = starty + lines * tile_height;
endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}

View File

@ -1,205 +1,205 @@
#include <curses.h>
#define STARTX 9
#define STARTY 3
#define WIDTH 6
#define HEIGHT 4
#define BLANK 0
typedef struct _tile {
int x;
int y;
}tile;
void init_board(int **board, int n, tile *blank);
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
void shuffle_board(int **board, int n);
void move_blank(int direction, int **s_board, int n, tile *blank);
int check_win(int **s_board, int n, tile *blank);
enum { LEFT, RIGHT, UP, DOWN };
int main(int argc, char *argv[])
{ int **s_board;
int n, i, ch;
tile blank;
if(argc != 2)
{ printf("Usage: %s <shuffle board order>\n", argv[0]);
exit(1);
}
n = atoi(argv[1]);
s_board = (int **)calloc(n, sizeof(int *));
for(i = 0;i < n; ++i)
s_board[i] = (int *)calloc(n, sizeof(int));
init_board(s_board, n, &blank);
initscr();
keypad(stdscr, TRUE);
cbreak();
shuffle_board(s_board, n);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
move_blank(RIGHT, s_board, n, &blank);
break;
case KEY_RIGHT:
move_blank(LEFT, s_board, n, &blank);
break;
case KEY_UP:
move_blank(DOWN, s_board, n, &blank);
break;
case KEY_DOWN:
move_blank(UP, s_board, n, &blank);
break;
}
shuffle_board(s_board, n);
if(check_win(s_board, n, &blank) == TRUE)
{ mvprintw(24, 0, "You Win !!!\n");
refresh();
break;
}
}
endwin();
return 0;
}
void move_blank(int direction, int **s_board, int n, tile *blank)
{ int temp;
switch(direction)
{ case LEFT:
{ if(blank->x != 0)
{ --blank->x;
temp = s_board[blank->x][blank->y];
s_board[blank->x + 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case RIGHT:
{ if(blank->x != n - 1)
{ ++blank->x;
temp = s_board[blank->x][blank->y];
s_board[blank->x - 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case UP:
{ if(blank->y != 0)
{ --blank->y;
temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y + 1] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case DOWN:
{ if(blank->y != n - 1)
{ ++blank->y;
temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y - 1] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
}
}
int check_win(int **s_board, int n, tile *blank)
{ int i, j;
s_board[blank->x][blank->y] = n * n;
for(i = 0;i < n; ++i)
for(j = 0;j < n; ++j)
if(s_board[i][j] != j * n + i + 1)
{ s_board[blank->x][blank->y] = BLANK;
return FALSE;
}
s_board[blank->x][blank->y] = BLANK;
return TRUE;
}
void init_board(int **s_board, int n, tile *blank)
{ int i, j, k;
int *temp_board;
temp_board = (int *)calloc(n * n, sizeof(int));
srand(time(NULL));
for(i = 0;i < n * n; ++i)
{
repeat :
k = rand() % (n * n);
for(j = 0;j <= i - 1; ++j)
if (k == temp_board[j])
goto repeat;
else
temp_board[i] = k;
}
k = 0;
for (i = 0;i < n;++i)
for(j = 0;j < n; ++j,++k)
{ if(temp_board[k] == 0)
{ blank->x = i;
blank->y = j;
}
s_board[i][j] = temp_board[k];
}
free(temp_board);
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;
endy = starty + lines * tile_height;
endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
void shuffle_board(int **s_board, int n)
{ int i,j, deltax, deltay;
int startx, starty;
starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
clear();
mvprintw(24, 0, "Press F1 to Exit");
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for(j = 0; j < n; ++j)
for(i = 0;i < n; ++i)
if(s_board[i][j] != BLANK)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%-2d", s_board[i][j]);
refresh();
}
#include <curses.h>
#define STARTX 9
#define STARTY 3
#define WIDTH 6
#define HEIGHT 4
#define BLANK 0
typedef struct _tile {
int x;
int y;
}tile;
void init_board(int **board, int n, tile *blank);
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height);
void shuffle_board(int **board, int n);
void move_blank(int direction, int **s_board, int n, tile *blank);
int check_win(int **s_board, int n, tile *blank);
enum { LEFT, RIGHT, UP, DOWN };
int main(int argc, char *argv[])
{ int **s_board;
int n, i, ch;
tile blank;
if(argc != 2)
{ printf("Usage: %s <shuffle board order>\n", argv[0]);
exit(1);
}
n = atoi(argv[1]);
s_board = (int **)calloc(n, sizeof(int *));
for(i = 0;i < n; ++i)
s_board[i] = (int *)calloc(n, sizeof(int));
init_board(s_board, n, &blank);
initscr();
keypad(stdscr, TRUE);
cbreak();
shuffle_board(s_board, n);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
move_blank(RIGHT, s_board, n, &blank);
break;
case KEY_RIGHT:
move_blank(LEFT, s_board, n, &blank);
break;
case KEY_UP:
move_blank(DOWN, s_board, n, &blank);
break;
case KEY_DOWN:
move_blank(UP, s_board, n, &blank);
break;
}
shuffle_board(s_board, n);
if(check_win(s_board, n, &blank) == TRUE)
{ mvprintw(24, 0, "You Win !!!\n");
refresh();
break;
}
}
endwin();
return 0;
}
void move_blank(int direction, int **s_board, int n, tile *blank)
{ int temp;
switch(direction)
{ case LEFT:
{ if(blank->x != 0)
{ --blank->x;
temp = s_board[blank->x][blank->y];
s_board[blank->x + 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case RIGHT:
{ if(blank->x != n - 1)
{ ++blank->x;
temp = s_board[blank->x][blank->y];
s_board[blank->x - 1][blank->y] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case UP:
{ if(blank->y != 0)
{ --blank->y;
temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y + 1] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
case DOWN:
{ if(blank->y != n - 1)
{ ++blank->y;
temp = s_board[blank->x][blank->y];
s_board[blank->x][blank->y - 1] = temp;
s_board[blank->x][blank->y] = BLANK;
}
}
break;
}
}
int check_win(int **s_board, int n, tile *blank)
{ int i, j;
s_board[blank->x][blank->y] = n * n;
for(i = 0;i < n; ++i)
for(j = 0;j < n; ++j)
if(s_board[i][j] != j * n + i + 1)
{ s_board[blank->x][blank->y] = BLANK;
return FALSE;
}
s_board[blank->x][blank->y] = BLANK;
return TRUE;
}
void init_board(int **s_board, int n, tile *blank)
{ int i, j, k;
int *temp_board;
temp_board = (int *)calloc(n * n, sizeof(int));
srand(time(NULL));
for(i = 0;i < n * n; ++i)
{
repeat :
k = rand() % (n * n);
for(j = 0;j <= i - 1; ++j)
if (k == temp_board[j])
goto repeat;
else
temp_board[i] = k;
}
k = 0;
for (i = 0;i < n;++i)
for(j = 0;j < n; ++j,++k)
{ if(temp_board[k] == 0)
{ blank->x = i;
blank->y = j;
}
s_board[i][j] = temp_board[k];
}
free(temp_board);
}
void board(WINDOW *win, int starty, int startx, int lines, int cols,
int tile_width, int tile_height)
{ int endy, endx, i, j;
endy = starty + lines * tile_height;
endx = startx + cols * tile_width;
for(j = starty; j <= endy; j += tile_height)
for(i = startx; i <= endx; ++i)
mvwaddch(win, j, i, ACS_HLINE);
for(i = startx; i <= endx; i += tile_width)
for(j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for(j = starty + tile_height; j <= endy - tile_height; j += tile_height)
{ mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for(i = startx + tile_width; i <= endx - tile_width; i += tile_width)
{ mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
void shuffle_board(int **s_board, int n)
{ int i,j, deltax, deltay;
int startx, starty;
starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
clear();
mvprintw(24, 0, "Press F1 to Exit");
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for(j = 0; j < n; ++j)
for(i = 0;i < n; ++i)
if(s_board[i][j] != BLANK)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%-2d", s_board[i][j]);
refresh();
}

View File

@ -1,223 +1,223 @@
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define HSIZE 60
#define LENGTH 75
#define WIDTH 10
#define STARTX 1
#define STARTY 5
#define STATUSX 1
#define STATUSY 25
#define KEY_F1 265
int print_menu();
void print_byebye();
void create_test_string();
void print_time(time_t startt, time_t endt, int mistakes);
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win);
char *groups[] = { "`123456" ,
"7890-=" ,
"~!@#$%^" ,
"&*()_+" ,
"<>?" ,
",./\\" ,
"asdfg",
"jkl;'",
"qwer",
"uiop",
"tyur",
"zxcv",
"bnm",
};
int n_groups;
int main()
{ int choice, i;
char *test_array;
int ch = KEY_F1;
int mistakes;
int x, y;
time_t start_t, end_t;
WINDOW *typing_win;
char string[80];
string[0] = '\0';
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
intrflush(stdscr, FALSE);
srandom(time(NULL));
n_groups = sizeof(groups) / sizeof(char *);
test_array = (char *)calloc(HSIZE + 1, sizeof(char));
while(1)
{
if(ch == KEY_F1)
{ choice = print_menu();
choice -= 1;
if(choice == n_groups)
{ print_byebye();
free(test_array);
endwin();
exit(0);
}
}
clear();
strcpy(string, "Typing window");
print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL);
attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu");
refresh();
attroff(A_REVERSE);
create_test_string(test_array, choice);
typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX);
keypad(typing_win, TRUE);
intrflush(typing_win, FALSE);
box(typing_win, 0, 0);
x = 1;
y = 1;
mvwprintw(typing_win, y, x, "%s", test_array);
wrefresh(typing_win);
y += 1;
mistakes = 0;
i = 0;
time(&start_t);
wmove(typing_win, y, x);
wrefresh(typing_win);
ch = 0;
while(ch != KEY_F1 && i != HSIZE + 1)
{ ch = wgetch(typing_win);
mvwprintw(typing_win, y, x, "%c", ch);
wrefresh(typing_win);
++x;
if(ch == test_array[i])
{ ++i;
continue;
}
else
{ ++mistakes;
++i;
}
}
time(&end_t);
print_time(start_t, end_t, mistakes);
}
free(test_array);
endwin();
return 0;
}
int print_menu()
{ int choice, i;
choice = 0;
while(1)
{ clear();
printw("\n\n");
print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL);
printw("\n\n\n");
for(i = 0;i <= n_groups - 1; ++i)
printw("\t%3d: \tPractice %s\n", i + 1, groups[i]);
printw("\t%3d: \tExit\n", i + 1);
printw("\n\n\tChoice: ");
refresh();
echo();
scanw("%d", &choice);
noecho();
if(choice >= 1 && choice <= n_groups + 1)
break;
else
{ attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue");
attroff(A_REVERSE);
getch();
}
}
return choice;
}
void create_test_string(char *test_array, int choice)
{ int i, index, length;
length = strlen(groups[choice]);
for(i = 0;i <= HSIZE - 1; ++i)
{ if(i%5 == 0)
test_array[i] = ' ';
else
{ index = (int)(random() % length);
test_array[i] = groups[choice][index];
}
}
test_array[i] = '\0';
}
void print_byebye()
{ printw("\n");
print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL);
print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL);
refresh();
}
void print_time(time_t start_t, time_t end_t, int mistakes)
{ long int diff;
int h,m,s;
float wpm;
diff = end_t - start_t;
wpm = ((HSIZE / 5)/(double)diff)*60;
h = (int)(diff / 3600);
diff -= h * 3600;
m = (int)(diff / 60);
diff -= m * 60;
s = (int)diff;
attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm);
attroff(A_REVERSE);
refresh();
getch();
}
/* ---------------------------------------------------------------- *
* startx = 0 means at present x *
* starty = 0 means at present y *
* win = NULL means take stdscr *
* ---------------------------------------------------------------- */
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define HSIZE 60
#define LENGTH 75
#define WIDTH 10
#define STARTX 1
#define STARTY 5
#define STATUSX 1
#define STATUSY 25
#define KEY_F1 265
int print_menu();
void print_byebye();
void create_test_string();
void print_time(time_t startt, time_t endt, int mistakes);
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win);
char *groups[] = { "`123456" ,
"7890-=" ,
"~!@#$%^" ,
"&*()_+" ,
"<>?" ,
",./\\" ,
"asdfg",
"jkl;'",
"qwer",
"uiop",
"tyur",
"zxcv",
"bnm",
};
int n_groups;
int main()
{ int choice, i;
char *test_array;
int ch = KEY_F1;
int mistakes;
int x, y;
time_t start_t, end_t;
WINDOW *typing_win;
char string[80];
string[0] = '\0';
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
intrflush(stdscr, FALSE);
srandom(time(NULL));
n_groups = sizeof(groups) / sizeof(char *);
test_array = (char *)calloc(HSIZE + 1, sizeof(char));
while(1)
{
if(ch == KEY_F1)
{ choice = print_menu();
choice -= 1;
if(choice == n_groups)
{ print_byebye();
free(test_array);
endwin();
exit(0);
}
}
clear();
strcpy(string, "Typing window");
print_in_middle(STARTX, STARTY - 2, LENGTH, string, NULL);
attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Press F1 to Main Menu");
refresh();
attroff(A_REVERSE);
create_test_string(test_array, choice);
typing_win = newwin(WIDTH, LENGTH, STARTY, STARTX);
keypad(typing_win, TRUE);
intrflush(typing_win, FALSE);
box(typing_win, 0, 0);
x = 1;
y = 1;
mvwprintw(typing_win, y, x, "%s", test_array);
wrefresh(typing_win);
y += 1;
mistakes = 0;
i = 0;
time(&start_t);
wmove(typing_win, y, x);
wrefresh(typing_win);
ch = 0;
while(ch != KEY_F1 && i != HSIZE + 1)
{ ch = wgetch(typing_win);
mvwprintw(typing_win, y, x, "%c", ch);
wrefresh(typing_win);
++x;
if(ch == test_array[i])
{ ++i;
continue;
}
else
{ ++mistakes;
++i;
}
}
time(&end_t);
print_time(start_t, end_t, mistakes);
}
free(test_array);
endwin();
return 0;
}
int print_menu()
{ int choice, i;
choice = 0;
while(1)
{ clear();
printw("\n\n");
print_in_middle(1, 1, 0, "* * * Welcome to typing practice (Version 1.0) * * * ", NULL);
printw("\n\n\n");
for(i = 0;i <= n_groups - 1; ++i)
printw("\t%3d: \tPractice %s\n", i + 1, groups[i]);
printw("\t%3d: \tExit\n", i + 1);
printw("\n\n\tChoice: ");
refresh();
echo();
scanw("%d", &choice);
noecho();
if(choice >= 1 && choice <= n_groups + 1)
break;
else
{ attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Wrong choice\tPress any key to continue");
attroff(A_REVERSE);
getch();
}
}
return choice;
}
void create_test_string(char *test_array, int choice)
{ int i, index, length;
length = strlen(groups[choice]);
for(i = 0;i <= HSIZE - 1; ++i)
{ if(i%5 == 0)
test_array[i] = ' ';
else
{ index = (int)(random() % length);
test_array[i] = groups[choice][index];
}
}
test_array[i] = '\0';
}
void print_byebye()
{ printw("\n");
print_in_middle(0,0,0,"Thank you for using my typing tutor\n", NULL);
print_in_middle(0,0,0,"Bye Bye ! ! !\n", NULL);
refresh();
}
void print_time(time_t start_t, time_t end_t, int mistakes)
{ long int diff;
int h,m,s;
float wpm;
diff = end_t - start_t;
wpm = ((HSIZE / 5)/(double)diff)*60;
h = (int)(diff / 3600);
diff -= h * 3600;
m = (int)(diff / 60);
diff -= m * 60;
s = (int)diff;
attron(A_REVERSE);
mvprintw(STATUSY, STATUSX, "Mistakes made : %d time taken: %d:%d:%d WPM : %.2f Press any Key to continue", mistakes, h, m, s, wpm);
attroff(A_REVERSE);
refresh();
getch();
}
/* ---------------------------------------------------------------- *
* startx = 0 means at present x *
* starty = 0 means at present y *
* win = NULL means take stdscr *
* ---------------------------------------------------------------- */
void print_in_middle(int startx, int starty, int width, char *string, WINDOW *win)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}

View File

@ -11,6 +11,8 @@ ncurses
|----> forms -- programs related to form library
|----> menus -- programs related to menus library
|----> panels -- programs related to panels library
|----> perl -- perl equivalents of the examples (contributed
| by Anuradha Ratnaweera)
|----> Makefile -- the top level Makefile
|----> README -- the top level README file. contains instructions
| -- to create executables for example programs
@ -80,3 +82,6 @@ JustForFun
|----> panel_hide.c -- Hiding and Un hiding of panels
|----> panel_resize.c -- Moving and resizing of panels
|----> panel_simple.c -- A simple panel example
perl
|----> 01-10.pl -- Perl equivalents of first ten example programs

View File

@ -1,44 +1,44 @@
#include <ncurses.h>
int main()
{
initscr();
printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n");
printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n");
printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n");
printw("Tee pointing right "); addch(ACS_LTEE); printw("\n");
printw("Tee pointing left "); addch(ACS_RTEE); printw("\n");
printw("Tee pointing up "); addch(ACS_BTEE); printw("\n");
printw("Tee pointing down "); addch(ACS_TTEE); printw("\n");
printw("Horizontal line "); addch(ACS_HLINE); printw("\n");
printw("Vertical line "); addch(ACS_VLINE); printw("\n");
printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n");
printw("Scan Line 1 "); addch(ACS_S1); printw("\n");
printw("Scan Line 3 "); addch(ACS_S3); printw("\n");
printw("Scan Line 7 "); addch(ACS_S7); printw("\n");
printw("Scan Line 9 "); addch(ACS_S9); printw("\n");
printw("Diamond "); addch(ACS_DIAMOND); printw("\n");
printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n");
printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n");
printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n");
printw("Bullet "); addch(ACS_BULLET); printw("\n");
printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n");
printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n");
printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n");
printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n");
printw("Board of squares "); addch(ACS_BOARD); printw("\n");
printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n");
printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n");
printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n");
printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n");
printw("Pi "); addch(ACS_PI); printw("\n");
printw("Not equal "); addch(ACS_NEQUAL); printw("\n");
printw("UK pound sign "); addch(ACS_STERLING); printw("\n");
refresh();
getch();
endwin();
return 0;
}
#include <ncurses.h>
int main()
{
initscr();
printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n");
printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n");
printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n");
printw("Tee pointing right "); addch(ACS_LTEE); printw("\n");
printw("Tee pointing left "); addch(ACS_RTEE); printw("\n");
printw("Tee pointing up "); addch(ACS_BTEE); printw("\n");
printw("Tee pointing down "); addch(ACS_TTEE); printw("\n");
printw("Horizontal line "); addch(ACS_HLINE); printw("\n");
printw("Vertical line "); addch(ACS_VLINE); printw("\n");
printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n");
printw("Scan Line 1 "); addch(ACS_S1); printw("\n");
printw("Scan Line 3 "); addch(ACS_S3); printw("\n");
printw("Scan Line 7 "); addch(ACS_S7); printw("\n");
printw("Scan Line 9 "); addch(ACS_S9); printw("\n");
printw("Diamond "); addch(ACS_DIAMOND); printw("\n");
printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n");
printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n");
printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n");
printw("Bullet "); addch(ACS_BULLET); printw("\n");
printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n");
printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n");
printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n");
printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n");
printw("Board of squares "); addch(ACS_BOARD); printw("\n");
printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n");
printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n");
printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n");
printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n");
printw("Pi "); addch(ACS_PI); printw("\n");
printw("Not equal "); addch(ACS_NEQUAL); printw("\n");
printw("UK pound sign "); addch(ACS_STERLING); printw("\n");
refresh();
getch();
endwin();
return 0;
}

View File

@ -1,12 +1,12 @@
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}

View File

@ -1,31 +1,31 @@
#include <ncurses.h>
int main()
{ int ch;
initscr(); /* Start curses mode */
raw(); /* Line buffering disabled */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
noecho(); /* Don't echo() while we do getch */
printw("Type any character to see it in bold\n");
ch = getch(); /* If raw() hadn't been called
* we have to press enter before it
* gets to the program */
if(ch == KEY_F(1)) /* Without keypad enabled this will */
printw("F1 Key pressed");/* not get to us either */
/* Without noecho() some ugly escape
* charachters might have been printed
* on screen */
else
{ printw("The pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}
#include <ncurses.h>
int main()
{ int ch;
initscr(); /* Start curses mode */
raw(); /* Line buffering disabled */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
noecho(); /* Don't echo() while we do getch */
printw("Type any character to see it in bold\n");
ch = getch(); /* If raw() hadn't been called
* we have to press enter before it
* gets to the program */
if(ch == KEY_F(1)) /* Without keypad enabled this will */
printw("F1 Key pressed");/* not get to us either */
/* Without noecho() some ugly escape
* charachters might have been printed
* on screen */
else
{ printw("The pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}

View File

@ -1,14 +1,14 @@
#include <ncurses.h>
int main()
{ int ch;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
ch = getch();
endwin();
printf("The key pressed is %d\n", ch);
}
#include <ncurses.h>
int main()
{ int ch;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
ch = getch();
endwin();
printf("The key pressed is %d\n", ch);
}

View File

@ -1,106 +1,106 @@
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = { "Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);
int main()
{ int c, choice = 0;
WINDOW *menu_win;
MEVENT event;
/* Initialize curses */
initscr();
clear();
noecho();
cbreak(); //Line buffering disabled. pass on everything
/* Try to put the window in the middle of screen */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh();
attroff(A_REVERSE);
/* Print the menu for the first time */
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1);
/* Get all the mouse events */
mousemask(ALL_MOUSE_EVENTS, NULL);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_MOUSE:
if(getmouse(&event) == OK)
{ /* When the user clicks left mouse button */
if(event.bstate & BUTTON1_PRESSED)
{ report_choice(event.x + 1, event.y + 1, &choice);
if(choice == -1) //Exit chosen
goto end;
mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
refresh();
}
}
print_menu(menu_win, choice);
break;
}
}
end:
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1)
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
/* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{ int i,j, choice;
i = startx + 2;
j = starty + 3;
for(choice = 0; choice < n_choices; ++choice)
if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
{ if(choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break;
}
}
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = { "Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);
int main()
{ int c, choice = 0;
WINDOW *menu_win;
MEVENT event;
/* Initialize curses */
initscr();
clear();
noecho();
cbreak(); //Line buffering disabled. pass on everything
/* Try to put the window in the middle of screen */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh();
attroff(A_REVERSE);
/* Print the menu for the first time */
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1);
/* Get all the mouse events */
mousemask(ALL_MOUSE_EVENTS, NULL);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_MOUSE:
if(getmouse(&event) == OK)
{ /* When the user clicks left mouse button */
if(event.bstate & BUTTON1_PRESSED)
{ report_choice(event.x + 1, event.y + 1, &choice);
if(choice == -1) //Exit chosen
goto end;
mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
refresh();
}
}
print_menu(menu_win, choice);
break;
}
}
end:
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1)
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
/* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{ int i,j, choice;
i = startx + 2;
j = starty + 3;
for(choice = 0; choice < n_choices; ++choice)
if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
{ if(choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break;
}
}

View File

@ -1,121 +1,120 @@
#include <ncurses.h>
typedef struct _win_border_struct {
chtype ls, rs, ts, bs,
tl, tr, bl, br;
}WIN_BORDER;
typedef struct _WIN_struct {
int startx, starty;
int height, width;
WIN_BORDER border;
}WIN;
void init_win_params(WIN *p_win);
void print_win_params(WIN *p_win);
void create_box(WIN *win, int bool);
int main(int argc, char *argv[])
{ WIN win;
int ch;
initscr(); /* Start curses mode */
start_color(); /* Start the color functionality */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */
noecho();
init_pair(1, COLOR_CYAN, COLOR_BLACK);
/* Initialize the window parameters */
init_win_params(&win);
print_win_params(&win);
attron(COLOR_PAIR(1));
printw("Press F1 to exit");
refresh();
attroff(COLOR_PAIR(1));
create_box(&win, TRUE);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
create_box(&win, FALSE);
--win.startx;
create_box(&win, TRUE);
break;
case KEY_RIGHT:
create_box(&win, FALSE);
++win.startx;
create_box(&win, TRUE);
break;
case KEY_UP:
create_box(&win, FALSE);
--win.starty;
create_box(&win, TRUE);
break;
case KEY_DOWN:
create_box(&win, FALSE);
++win.starty;
create_box(&win, TRUE);
break;
}
}
endwin(); /* End curses mode */
return 0;
}
void init_win_params(WIN *p_win)
{
p_win->height = 3;
p_win->width = 10;
p_win->starty = (LINES - p_win->height)/2;
p_win->startx = (COLS - p_win->width)/2;
p_win->border.ls = '|';
p_win->border.rs = '|';
p_win->border.ts = '-';
p_win->border.bs = '-';
p_win->border.tl = '+';
p_win->border.tr = '+';
p_win->border.bl = '+';
p_win->border.br = '+';
}
void print_win_params(WIN *p_win)
{
#ifdef _DEBUG
mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
p_win->width, p_win->height);
refresh();
#endif
}
void create_box(WIN *p_win, int bool)
{ int i, j;
int x, y, w, h;
x = p_win->startx;
y = p_win->starty;
w = p_win->width;
h = p_win->height;
if(bool == TRUE)
{ mvaddch(y, x, p_win->border.tl);
mvaddch(y, x + w, p_win->border.tr);
mvaddch(y + h, x, p_win->border.bl);
mvaddch(y + h, x + w, p_win->border.br);
mvhline(y, x + 1, p_win->border.ts, w - 1);
mvhline(y + h, x + 1, p_win->border.bs, w - 1);
mvvline(y + 1, x, p_win->border.ls, h - 1);
mvvline(y + 1, x + w, p_win->border.rs, h - 1);
}
else
for(j = y; j <= y + h; ++j)
for(i = x; i <= x + w; ++i)
mvaddch(j, i, ' ');
refresh();
}
#include <ncurses.h>
typedef struct _win_border_struct {
chtype ls, rs, ts, bs,
tl, tr, bl, br;
}WIN_BORDER;
typedef struct _WIN_struct {
int startx, starty;
int height, width;
WIN_BORDER border;
}WIN;
void init_win_params(WIN *p_win);
void print_win_params(WIN *p_win);
void create_box(WIN *win, bool flag);
int main(int argc, char *argv[])
{ WIN win;
int ch;
initscr(); /* Start curses mode */
start_color(); /* Start the color functionality */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */
noecho();
init_pair(1, COLOR_CYAN, COLOR_BLACK);
/* Initialize the window parameters */
init_win_params(&win);
print_win_params(&win);
attron(COLOR_PAIR(1));
printw("Press F1 to exit");
refresh();
attroff(COLOR_PAIR(1));
create_box(&win, TRUE);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
create_box(&win, FALSE);
--win.startx;
create_box(&win, TRUE);
break;
case KEY_RIGHT:
create_box(&win, FALSE);
++win.startx;
create_box(&win, TRUE);
break;
case KEY_UP:
create_box(&win, FALSE);
--win.starty;
create_box(&win, TRUE);
break;
case KEY_DOWN:
create_box(&win, FALSE);
++win.starty;
create_box(&win, TRUE);
break;
}
}
endwin(); /* End curses mode */
return 0;
}
void init_win_params(WIN *p_win)
{
p_win->height = 3;
p_win->width = 10;
p_win->starty = (LINES - p_win->height)/2;
p_win->startx = (COLS - p_win->width)/2;
p_win->border.ls = '|';
p_win->border.rs = '|';
p_win->border.ts = '-';
p_win->border.bs = '-';
p_win->border.tl = '+';
p_win->border.tr = '+';
p_win->border.bl = '+';
p_win->border.br = '+';
}
void print_win_params(WIN *p_win)
{
#ifdef _DEBUG
mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
p_win->width, p_win->height);
refresh();
#endif
}
void create_box(WIN *p_win, bool flag)
{ int i, j;
int x, y, w, h;
x = p_win->startx;
y = p_win->starty;
w = p_win->width;
h = p_win->height;
if(flag == TRUE)
{ mvaddch(y, x, p_win->border.tl);
mvaddch(y, x + w, p_win->border.tr);
mvaddch(y + h, x, p_win->border.bl);
mvaddch(y + h, x + w, p_win->border.br);
mvhline(y, x + 1, p_win->border.ts, w - 1);
mvhline(y + h, x + 1, p_win->border.bs, w - 1);
mvvline(y + 1, x, p_win->border.ls, h - 1);
mvvline(y + 1, x + w, p_win->border.rs, h - 1);
}
else
for(j = y; j <= y + h; ++j)
for(i = x; i <= x + w; ++i)
mvaddch(j, i, ' ');
refresh();
}

View File

@ -1,20 +1,20 @@
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>
int main()
{
char mesg[]="Just a string"; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
printw("Try resizing your window(if possible) and then run this program again");
refresh();
getch();
endwin();
return 0;
}
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>
int main()
{
char mesg[]="Just a string"; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
printw("Try resizing your window(if possible) and then run this program again");
refresh();
getch();
endwin();
return 0;
}

View File

@ -1,20 +1,20 @@
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>
int main()
{
char mesg[]="Enter a string: "; /* message to be appeared on the screen */
char str[80];
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
getstr(str);
mvprintw(LINES - 2, 0, "You Entered: %s", str);
getch();
endwin();
return 0;
}
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>
int main()
{
char mesg[]="Enter a string: "; /* message to be appeared on the screen */
char str[80];
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
getstr(str);
mvprintw(LINES - 2, 0, "You Entered: %s", str);
getch();
endwin();
return 0;
}

View File

@ -1,50 +1,54 @@
/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */
#include <ncurses.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int ch, prev;
FILE *fp;
int goto_prev = FALSE, y, x;
int ch, prev, row, col;
prev = EOF;
FILE *fp;
int y, x;
if(argc != 2)
{ printf("Usage: %s <a c file name>\n", argv[0]);
exit(1);
if(argc != 2)
{
printf("Usage: %s <a c file name>\n", argv[0]);
exit(1);
}
fp = fopen(argv[1], "r");
if(fp == NULL)
{
perror("Cannot open input file");
exit(1);
}
initscr(); /* Start curses mode */
getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */
while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */
{
getyx(stdscr, y, x); /* get the current curser position */
if(y == (row - 1)) /* are we are at the end of the screen */
{
printw("<-Press Any Key->"); /* tell the user to press a key */
getch();
clear(); /* clear the screen */
move(0, 0); /* start at the beginning of the screen */
}
fp = fopen(argv[1], "r");
if(fp == NULL)
{ perror("Cannot open input file");
exit(1);
if(prev == '/' && ch == '*') /* If it is / and * then only
* switch bold on */
{
attron(A_BOLD); /* cut bold on */
getyx(stdscr, y, x); /* get the current curser position */
move(y, x - 1); /* back up one space */
printw("%c%c", '/', ch); /* The actual printing is done here */
}
initscr(); /* Start curses mode */
prev = EOF;
while((ch = fgetc(fp)) != EOF)
{ if(prev == '/' && ch == '*') /* If it is / and * then olny
* switch bold on */
{ attron(A_BOLD);
goto_prev = TRUE; /* Go to previous char / and
* print it in BOLD */
}
if(goto_prev == TRUE)
{ getyx(stdscr, y, x);
move(y, x - 1);
printw("%c%c", '/', ch); /* The actual printing is done
* here */
ch = 'a'; /* 'a' is just a dummy
* character to prevent */
// "/*/" comments.
goto_prev = FALSE; /* Set it to FALSE or every
* thing from here will be / */
} else
printw("%c", ch);
refresh();
if(prev == '*' && ch == '/')
attroff(A_BOLD); /* Switch it off once we got *
and then / */
prev = ch;
}
getch();
endwin(); /* End curses mode */
return 0;
else
printw("%c", ch);
refresh();
if(prev == '*' && ch == '/')
attroff(A_BOLD); /* Switch it off once we got *
* and then / */
prev = ch;
}
endwin(); /* End curses mode */
fclose(fp);
return 0;
}

View File

@ -1,40 +1,40 @@
#include <ncurses.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
if(has_colors() == FALSE)
{ endwin();
printf("You terminal does not support color\n");
exit(1);
}
start_color(); /* Start color */
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1));
getch();
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}
#include <ncurses.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
if(has_colors() == FALSE)
{ endwin();
printf("Your terminal does not support color\n");
exit(1);
}
start_color(); /* Start color */
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1));
getch();
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}

View File

@ -1,92 +1,92 @@
#include <stdio.h>
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
int main()
{ WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_choices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_choices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
refresh();
break;
}
print_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
clrtoeol();
refresh();
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
#include <stdio.h>
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
int main()
{ WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_choices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_choices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
refresh();
break;
}
print_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
clrtoeol();
refresh();
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}

View File

@ -1,20 +1,20 @@
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!\n"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
def_prog_mode(); /* Save the tty modes */
endwin(); /* End curses mode temporarily */
system("/bin/sh"); /* Do whatever you like in cooked mode */
reset_prog_mode(); /* Return to the previous tty mode*/
/* stored by def_prog_mode() */
refresh(); /* Do refresh() to restore the */
/* Screen contents */
printw("Another String\n"); /* Back to curses use the full */
refresh(); /* capabilities of curses */
endwin(); /* End curses mode */
return 0;
}
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!\n"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
def_prog_mode(); /* Save the tty modes */
endwin(); /* End curses mode temporarily */
system("/bin/sh"); /* Do whatever you like in cooked mode */
reset_prog_mode(); /* Return to the previous tty mode*/
/* stored by def_prog_mode() */
refresh(); /* Do refresh() to restore the */
/* Screen contents */
printw("Another String\n"); /* Back to curses use the full */
refresh(); /* capabilities of curses */
endwin(); /* End curses mode */
return 0;
}

View File

@ -1,82 +1,82 @@
#include <ncurses.h>
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[])
{ WINDOW *my_win;
int startx, starty, width, height;
int ch;
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */
height = 3;
width = 10;
starty = (LINES - height) / 2; /* Calculating for a center placement */
startx = (COLS - width) / 2; /* of the window */
printw("Press F1 to exit");
refresh();
my_win = create_newwin(height, width, starty, startx);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,--startx);
break;
case KEY_RIGHT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,++startx);
break;
case KEY_UP:
destroy_win(my_win);
my_win = create_newwin(height, width, --starty,startx);
break;
case KEY_DOWN:
destroy_win(my_win);
my_win = create_newwin(height, width, ++starty,startx);
break;
}
}
endwin(); /* End curses mode */
return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
wrefresh(local_win); /* Show that box */
return local_win;
}
void destroy_win(WINDOW *local_win)
{
/* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window.
*/
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are
* 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window
* 3. rs: character to be used for the right side of the window
* 4. ts: character to be used for the top side of the window
* 5. bs: character to be used for the bottom side of the window
* 6. tl: character to be used for the top left corner of the window
* 7. tr: character to be used for the top right corner of the window
* 8. bl: character to be used for the bottom left corner of the window
* 9. br: character to be used for the bottom right corner of the window
*/
wrefresh(local_win);
delwin(local_win);
}
#include <ncurses.h>
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[])
{ WINDOW *my_win;
int startx, starty, width, height;
int ch;
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */
height = 3;
width = 10;
starty = (LINES - height) / 2; /* Calculating for a center placement */
startx = (COLS - width) / 2; /* of the window */
printw("Press F1 to exit");
refresh();
my_win = create_newwin(height, width, starty, startx);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,--startx);
break;
case KEY_RIGHT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,++startx);
break;
case KEY_UP:
destroy_win(my_win);
my_win = create_newwin(height, width, --starty,startx);
break;
case KEY_DOWN:
destroy_win(my_win);
my_win = create_newwin(height, width, ++starty,startx);
break;
}
}
endwin(); /* End curses mode */
return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
wrefresh(local_win); /* Show that box */
return local_win;
}
void destroy_win(WINDOW *local_win)
{
/* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window.
*/
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are
* 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window
* 3. rs: character to be used for the right side of the window
* 4. ts: character to be used for the top side of the window
* 5. bs: character to be used for the bottom side of the window
* 6. tl: character to be used for the top left corner of the window
* 7. tr: character to be used for the top right corner of the window
* 8. bl: character to be used for the bottom left corner of the window
* 9. br: character to be used for the bottom right corner of the window
*/
wrefresh(local_win);
delwin(local_win);
}

View File

@ -1,24 +1,24 @@
#include <ncurses.h>
int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
start_color(); /* Start color functionality */
init_pair(1, COLOR_CYAN, COLOR_BLACK);
printw("A Big string which i didn't care to type fully ");
mvchgat(0, 0, -1, A_BLINK, 1, NULL);
/*
* First two parameters specify the position at which to start
* Third parameter number of characters to update. -1 means till
* end of line
* Forth parameter is the normal attribute you wanted to give
* to the charcter
* Fifth is the color index. It is the index given during init_pair()
* use 0 if you didn't want color
* Sixth one is always NULL
*/
refresh();
getch();
endwin(); /* End curses mode */
return 0;
}
#include <ncurses.h>
int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
start_color(); /* Start color functionality */
init_pair(1, COLOR_CYAN, COLOR_BLACK);
printw("A Big string which i didn't care to type fully ");
mvchgat(0, 0, -1, A_BLINK, 1, NULL);
/*
* First two parameters specify the position at which to start
* Third parameter number of characters to update. -1 means till
* end of line
* Forth parameter is the normal attribute you wanted to give
* to the charcter
* Fifth is the color index. It is the index given during init_pair()
* use 0 if you didn't want color
* Sixth one is always NULL
*/
refresh();
getch();
endwin(); /* End curses mode */
return 0;
}

View File

@ -1,75 +1,75 @@
#include <form.h>
int main()
{ FIELD *field[3];
FORM *my_form;
int ch;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize few color pairs */
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
/* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;
/* Set field options */
set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */
set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
/* are printed in white */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
set_current_field(my_form, field[0]); /* Set focus to the colored field */
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
#include <form.h>
int main()
{ FIELD *field[3];
FORM *my_form;
int ch;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize few color pairs */
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
/* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;
/* Set field options */
set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */
set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
/* are printed in white */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
set_current_field(my_form, field[0]); /* Set focus to the colored field */
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}

View File

@ -1,76 +1,76 @@
#include <form.h>
#define STARTX 15
#define STARTY 4
#define WIDTH 25
#define N_FIELDS 3
int main()
{ FIELD *field[N_FIELDS];
FORM *my_form;
int ch, i;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize the fields */
for(i = 0; i < N_FIELDS - 1; ++i)
field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
field[N_FIELDS - 1] = NULL;
/* Set field options */
set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */
field_opts_off(field[0], O_ACTIVE); /* This field is a static label */
field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/
field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
/* after last character is entered */
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
set_field_buffer(field[0], 0, "This is a static Field");
/* Initialize the field */
mvprintw(STARTY, STARTX - 10, "Field 1:");
mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
refresh();
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
#include <form.h>
#define STARTX 15
#define STARTY 4
#define WIDTH 25
#define N_FIELDS 3
int main()
{ FIELD *field[N_FIELDS];
FORM *my_form;
int ch, i;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize the fields */
for(i = 0; i < N_FIELDS - 1; ++i)
field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
field[N_FIELDS - 1] = NULL;
/* Set field options */
set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */
field_opts_off(field[0], O_ACTIVE); /* This field is a static label */
field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/
field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
/* after last character is entered */
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
set_field_buffer(field[0], 0, "This is a static Field");
/* Initialize the field */
mvprintw(STARTY, STARTX - 10, "Field 1:");
mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
refresh();
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}

View File

@ -1,66 +1,66 @@
#include <form.h>
int main()
{ FIELD *field[3];
FORM *my_form;
int ch;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;
/* Set field options */
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
refresh();
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
#include <form.h>
int main()
{ FIELD *field[3];
FORM *my_form;
int ch;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize the fields */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;
/* Set field options */
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
refresh();
/* Loop through to get user requests */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}

View File

@ -1,112 +1,112 @@
#include <form.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{
FIELD *field[3];
FORM *my_form;
WINDOW *my_form_win;
int ch, rows, cols;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize few color pairs */
init_pair(1, COLOR_RED, COLOR_BLACK);
/* Initialize the fields */
field[0] = new_field(1, 10, 6, 1, 0, 0);
field[1] = new_field(1, 10, 8, 1, 0, 0);
field[2] = NULL;
/* Set field options */
set_field_back(field[0], A_UNDERLINE);
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
/* Calculate the area required for the form */
scale_form(my_form, &rows, &cols);
/* Create the window to be associated with the form */
my_form_win = newwin(rows + 4, cols + 4, 4, 4);
keypad(my_form_win, TRUE);
/* Set main window and sub window */
set_form_win(my_form, my_form_win);
set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
/* Print a border around the main window and print a title */
box(my_form_win, 0, 0);
print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
post_form(my_form);
wrefresh(my_form_win);
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();
/* Loop through to get user requests */
while((ch = wgetch(my_form_win)) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
#include <form.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{
FIELD *field[3];
FORM *my_form;
WINDOW *my_form_win;
int ch, rows, cols;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize few color pairs */
init_pair(1, COLOR_RED, COLOR_BLACK);
/* Initialize the fields */
field[0] = new_field(1, 10, 6, 1, 0, 0);
field[1] = new_field(1, 10, 8, 1, 0, 0);
field[2] = NULL;
/* Set field options */
set_field_back(field[0], A_UNDERLINE);
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
/* Calculate the area required for the form */
scale_form(my_form, &rows, &cols);
/* Create the window to be associated with the form */
my_form_win = newwin(rows + 4, cols + 4, 4, 4);
keypad(my_form_win, TRUE);
/* Set main window and sub window */
set_form_win(my_form, my_form_win);
set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
/* Print a border around the main window and print a title */
box(my_form_win, 0, 0);
print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
post_form(my_form);
wrefresh(my_form_win);
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();
/* Loop through to get user requests */
while((ch = wgetch(my_form_win)) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

View File

@ -1,80 +1,80 @@
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* Initialize items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
item_opts_off(my_items[3], O_SELECTABLE);
item_opts_off(my_items[6], O_SELECTABLE);
/* Create menu */
my_menu = new_menu((ITEM **)my_items);
/* Set fore ground and back ground of the menu */
set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
set_menu_back(my_menu, COLOR_PAIR(2));
set_menu_grey(my_menu, COLOR_PAIR(3));
/* Post the menu */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
move(20, 0);
clrtoeol();
mvprintw(20, 0, "Item selected is : %s",
item_name(current_item(my_menu)));
pos_menu_cursor(my_menu);
break;
}
}
unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
free_menu(my_menu);
endwin();
}
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* Initialize items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
item_opts_off(my_items[3], O_SELECTABLE);
item_opts_off(my_items[6], O_SELECTABLE);
/* Create menu */
my_menu = new_menu((ITEM **)my_items);
/* Set fore ground and back ground of the menu */
set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
set_menu_back(my_menu, COLOR_PAIR(2));
set_menu_grey(my_menu, COLOR_PAIR(3));
/* Post the menu */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
move(20, 0);
clrtoeol();
mvprintw(20, 0, "Item selected is : %s",
item_name(current_item(my_menu)));
pos_menu_cursor(my_menu);
break;
}
}
unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
free_menu(my_menu);
endwin();
}

View File

@ -1,66 +1,66 @@
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
cur_item = current_item(my_menu);
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s",
item_index(cur_item) + 1, item_name(cur_item),
item_description(cur_item));
refresh();
pos_menu_cursor(my_menu);
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
cur_item = current_item(my_menu);
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s",
item_index(cur_item) + 1, item_name(cur_item),
item_description(cur_item));
refresh();
pos_menu_cursor(my_menu);
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}

View File

@ -1,97 +1,97 @@
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Exit",
(char *)NULL,
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Set menu option not to show the description */
menu_opts_off(my_menu, O_SHOWDESC);
/* Create the window to be associated with the menu */
my_menu_win = newwin(10, 70, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
set_menu_format(my_menu, 5, 3);
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
attron(COLOR_PAIR(2));
mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_LEFT:
menu_driver(my_menu, REQ_LEFT_ITEM);
break;
case KEY_RIGHT:
menu_driver(my_menu, REQ_RIGHT_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
}
wrefresh(my_menu_win);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Exit",
(char *)NULL,
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Set menu option not to show the description */
menu_opts_off(my_menu, O_SHOWDESC);
/* Create the window to be associated with the menu */
my_menu_win = newwin(10, 70, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
set_menu_format(my_menu, 5, 3);
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
attron(COLOR_PAIR(2));
mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_LEFT:
menu_driver(my_menu, REQ_LEFT_ITEM);
break;
case KEY_RIGHT:
menu_driver(my_menu, REQ_RIGHT_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
}
wrefresh(my_menu_win);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}

View File

@ -1,124 +1,124 @@
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Choice 8",
"Choice 9",
"Choice 10",
"Exit",
(char *)NULL,
};
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */
my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
set_menu_format(my_menu, 5, 1);
/* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
attron(COLOR_PAIR(2));
mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items");
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
}
wrefresh(my_menu_win);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Choice 8",
"Choice 9",
"Choice 10",
"Exit",
(char *)NULL,
};
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */
my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
set_menu_format(my_menu, 5, 1);
/* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
attron(COLOR_PAIR(2));
mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items");
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
}
wrefresh(my_menu_win);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

View File

@ -1,56 +1,56 @@
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
mvprintw(LINES - 2, 0, "F1 to Exit");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
mvprintw(LINES - 2, 0, "F1 to Exit");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}

View File

@ -1,84 +1,84 @@
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
/* Make the menu multi valued */
menu_opts_off(my_menu, O_ONEVALUE);
mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item.");
mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case ' ':
menu_driver(my_menu, REQ_TOGGLE_ITEM);
break;
case 10: /* Enter */
{ char temp[200];
ITEM **items;
items = menu_items(my_menu);
temp[0] = '\0';
for(i = 0; i < item_count(my_menu); ++i)
if(item_value(items[i]) == TRUE)
{ strcat(temp, item_name(items[i]));
strcat(temp, " ");
}
move(20, 0);
clrtoeol();
mvprintw(20, 0, temp);
refresh();
}
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
/* Make the menu multi valued */
menu_opts_off(my_menu, O_ONEVALUE);
mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item.");
mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case ' ':
menu_driver(my_menu, REQ_TOGGLE_ITEM);
break;
case 10: /* Enter */
{ char temp[200];
ITEM **items;
items = menu_items(my_menu);
temp[0] = '\0';
for(i = 0; i < item_count(my_menu); ++i)
if(item_value(items[i]) == TRUE)
{ strcat(temp, item_name(items[i]));
strcat(temp, " ");
}
move(20, 0);
clrtoeol();
mvprintw(20, 0, temp);
refresh();
}
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}

View File

@ -1,87 +1,87 @@
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
void func(char *name);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* Initialize items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
{ my_items[i] = new_item(choices[i], choices[i]);
/* Set the user pointer */
set_item_userptr(my_items[i], func);
}
my_items[n_choices] = (ITEM *)NULL;
/* Create menu */
my_menu = new_menu((ITEM **)my_items);
/* Post the menu */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
{ ITEM *cur;
void (*p)(char *);
cur = current_item(my_menu);
p = item_userptr(cur);
p((char *)item_name(cur));
pos_menu_cursor(my_menu);
break;
}
break;
}
}
unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
free_menu(my_menu);
endwin();
}
void func(char *name)
{ move(20, 0);
clrtoeol();
mvprintw(20, 0, "Item selected is : %s", name);
}
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
void func(char *name);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* Initialize items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
{ my_items[i] = new_item(choices[i], choices[i]);
/* Set the user pointer */
set_item_userptr(my_items[i], func);
}
my_items[n_choices] = (ITEM *)NULL;
/* Create menu */
my_menu = new_menu((ITEM **)my_items);
/* Post the menu */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
{ ITEM *cur;
void (*p)(char *);
cur = current_item(my_menu);
p = item_userptr(cur);
p((char *)item_name(cur));
pos_menu_cursor(my_menu);
break;
}
break;
}
}
unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
free_menu(my_menu);
endwin();
}
void func(char *name)
{ move(20, 0);
clrtoeol();
mvprintw(20, 0, "Item selected is : %s", name);
}

View File

@ -1,105 +1,105 @@
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */
my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
/* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
mvprintw(LINES - 2, 0, "F1 to exit");
refresh();
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
wrefresh(my_menu_win);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */
my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
/* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
mvprintw(LINES - 2, 0, "F1 to exit");
refresh();
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
wrefresh(my_menu_win);
}
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

View File

@ -1,117 +1,117 @@
#include <panel.h>
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL *top;
int ch;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Set up the user pointers to the next panel */
set_panel_userptr(my_panels[0], my_panels[1]);
set_panel_userptr(my_panels[1], my_panels[2]);
set_panel_userptr(my_panels[2], my_panels[0]);
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();
top = my_panels[2];
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9:
top = (PANEL *)panel_userptr(top);
top_panel(top);
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
#include <panel.h>
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL *top;
int ch;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Set up the user pointers to the next panel */
set_panel_userptr(my_panels[0], my_panels[1]);
set_panel_userptr(my_panels[1], my_panels[2]);
set_panel_userptr(my_panels[2], my_panels[0]);
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();
top = my_panels[2];
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9:
top = (PANEL *)panel_userptr(top);
top_panel(top);
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

View File

@ -1,156 +1,156 @@
#include <panel.h>
typedef struct _PANEL_DATA {
int hide; /* TRUE if panel is hidden */
}PANEL_DATA;
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL_DATA panel_datas[3];
PANEL_DATA *temp;
int ch;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Initialize panel datas saying that nothing is hidden */
panel_datas[0].hide = FALSE;
panel_datas[1].hide = FALSE;
panel_datas[2].hide = FALSE;
set_panel_userptr(my_panels[0], &panel_datas[0]);
set_panel_userptr(my_panels[1], &panel_datas[1]);
set_panel_userptr(my_panels[2], &panel_datas[2]);
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)");
mvprintw(LINES - 2, 0, "F1 to Exit");
attroff(COLOR_PAIR(4));
doupdate();
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 'a':
temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[0]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[0]);
temp->hide = FALSE;
}
break;
case 'b':
temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[1]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[1]);
temp->hide = FALSE;
}
break;
case 'c':
temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[2]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[2]);
temp->hide = FALSE;
}
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
#include <panel.h>
typedef struct _PANEL_DATA {
int hide; /* TRUE if panel is hidden */
}PANEL_DATA;
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL_DATA panel_datas[3];
PANEL_DATA *temp;
int ch;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Initialize panel datas saying that nothing is hidden */
panel_datas[0].hide = FALSE;
panel_datas[1].hide = FALSE;
panel_datas[2].hide = FALSE;
set_panel_userptr(my_panels[0], &panel_datas[0]);
set_panel_userptr(my_panels[1], &panel_datas[1]);
set_panel_userptr(my_panels[2], &panel_datas[2]);
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)");
mvprintw(LINES - 2, 0, "F1 to Exit");
attroff(COLOR_PAIR(4));
doupdate();
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 'a':
temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[0]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[0]);
temp->hide = FALSE;
}
break;
case 'b':
temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[1]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[1]);
temp->hide = FALSE;
}
break;
case 'c':
temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[2]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[2]);
temp->hide = FALSE;
}
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

View File

@ -1,234 +1,234 @@
#include <panel.h>
typedef struct _PANEL_DATA {
int x, y, w, h;
char label[80];
int label_color;
PANEL *next;
}PANEL_DATA;
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
void set_user_ptrs(PANEL **panels, int n);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL_DATA *top;
PANEL *stack_top;
WINDOW *temp_win, *old_win;
int ch;
int newx, newy, neww, newh;
int size = FALSE, move = FALSE;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
set_user_ptrs(my_panels, 3);
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();
stack_top = my_panels[2];
top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x;
newy = top->y;
neww = top->w;
newh = top->h;
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9: /* Tab */
top = (PANEL_DATA *)panel_userptr(stack_top);
top_panel(top->next);
stack_top = top->next;
top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x;
newy = top->y;
neww = top->w;
newh = top->h;
break;
case 'r': /* Re-Size*/
size = TRUE;
attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing");
refresh();
attroff(COLOR_PAIR(4));
break;
case 'm': /* Move */
attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving");
refresh();
attroff(COLOR_PAIR(4));
move = TRUE;
break;
case KEY_LEFT:
if(size == TRUE)
{ --newx;
++neww;
}
if(move == TRUE)
--newx;
break;
case KEY_RIGHT:
if(size == TRUE)
{ ++newx;
--neww;
}
if(move == TRUE)
++newx;
break;
case KEY_UP:
if(size == TRUE)
{ --newy;
++newh;
}
if(move == TRUE)
--newy;
break;
case KEY_DOWN:
if(size == TRUE)
{ ++newy;
--newh;
}
if(move == TRUE)
++newy;
break;
case 10: /* Enter */
move(LINES - 4, 0);
clrtoeol();
refresh();
if(size == TRUE)
{ old_win = panel_window(stack_top);
temp_win = newwin(newh, neww, newy, newx);
replace_panel(stack_top, temp_win);
win_show(temp_win, top->label, top->label_color);
delwin(old_win);
size = FALSE;
}
if(move == TRUE)
{ move_panel(stack_top, newy, newx);
move = FALSE;
}
break;
}
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
refresh();
update_panels();
doupdate();
}
endwin();
return 0;
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* Set the PANEL_DATA structures for individual panels */
void set_user_ptrs(PANEL **panels, int n)
{ PANEL_DATA *ptrs;
WINDOW *win;
int x, y, w, h, i;
char temp[80];
ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
for(i = 0;i < n; ++i)
{ win = panel_window(panels[i]);
getbegyx(win, y, x);
getmaxyx(win, h, w);
ptrs[i].x = x;
ptrs[i].y = y;
ptrs[i].w = w;
ptrs[i].h = h;
sprintf(temp, "Window Number %d", i + 1);
strcpy(ptrs[i].label, temp);
ptrs[i].label_color = i + 1;
if(i + 1 == n)
ptrs[i].next = panels[0];
else
ptrs[i].next = panels[i + 1];
set_panel_userptr(panels[i], &ptrs[i]);
}
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
#include <panel.h>
typedef struct _PANEL_DATA {
int x, y, w, h;
char label[80];
int label_color;
PANEL *next;
}PANEL_DATA;
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
void set_user_ptrs(PANEL **panels, int n);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL_DATA *top;
PANEL *stack_top;
WINDOW *temp_win, *old_win;
int ch;
int newx, newy, neww, newh;
int size = FALSE, move = FALSE;
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* Initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
set_user_ptrs(my_panels, 3);
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();
stack_top = my_panels[2];
top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x;
newy = top->y;
neww = top->w;
newh = top->h;
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9: /* Tab */
top = (PANEL_DATA *)panel_userptr(stack_top);
top_panel(top->next);
stack_top = top->next;
top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x;
newy = top->y;
neww = top->w;
newh = top->h;
break;
case 'r': /* Re-Size*/
size = TRUE;
attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing");
refresh();
attroff(COLOR_PAIR(4));
break;
case 'm': /* Move */
attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving");
refresh();
attroff(COLOR_PAIR(4));
move = TRUE;
break;
case KEY_LEFT:
if(size == TRUE)
{ --newx;
++neww;
}
if(move == TRUE)
--newx;
break;
case KEY_RIGHT:
if(size == TRUE)
{ ++newx;
--neww;
}
if(move == TRUE)
++newx;
break;
case KEY_UP:
if(size == TRUE)
{ --newy;
++newh;
}
if(move == TRUE)
--newy;
break;
case KEY_DOWN:
if(size == TRUE)
{ ++newy;
--newh;
}
if(move == TRUE)
++newy;
break;
case 10: /* Enter */
move(LINES - 4, 0);
clrtoeol();
refresh();
if(size == TRUE)
{ old_win = panel_window(stack_top);
temp_win = newwin(newh, neww, newy, newx);
replace_panel(stack_top, temp_win);
win_show(temp_win, top->label, top->label_color);
delwin(old_win);
size = FALSE;
}
if(move == TRUE)
{ move_panel(stack_top, newy, newx);
move = FALSE;
}
break;
}
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
refresh();
update_panels();
doupdate();
}
endwin();
return 0;
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* Set the PANEL_DATA structures for individual panels */
void set_user_ptrs(PANEL **panels, int n)
{ PANEL_DATA *ptrs;
WINDOW *win;
int x, y, w, h, i;
char temp[80];
ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
for(i = 0;i < n; ++i)
{ win = panel_window(panels[i]);
getbegyx(win, y, x);
getmaxyx(win, h, w);
ptrs[i].x = x;
ptrs[i].y = y;
ptrs[i].w = w;
ptrs[i].h = h;
sprintf(temp, "Window Number %d", i + 1);
strcpy(ptrs[i].label, temp);
ptrs[i].label_color = i + 1;
if(i + 1 == n)
ptrs[i].next = panels[0];
else
ptrs[i].next = panels[i + 1];
set_panel_userptr(panels[i], &ptrs[i]);
}
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

View File

@ -1,38 +1,38 @@
#include <panel.h>
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
int lines = 10, cols = 40, y = 2, x = 4, i;
initscr();
cbreak();
noecho();
/* Create windows for the panels */
my_wins[0] = newwin(lines, cols, y, x);
my_wins[1] = newwin(lines, cols, y + 1, x + 5);
my_wins[2] = newwin(lines, cols, y + 2, x + 10);
/*
* Create borders around the windows so that you can see the effect
* of panels
*/
for(i = 0; i < 3; +++i)
box(my_wins[i], 0, 0);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
doupdate();
getch();
endwin();
}
#include <panel.h>
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
int lines = 10, cols = 40, y = 2, x = 4, i;
initscr();
cbreak();
noecho();
/* Create windows for the panels */
my_wins[0] = newwin(lines, cols, y, x);
my_wins[1] = newwin(lines, cols, y + 1, x + 5);
my_wins[2] = newwin(lines, cols, y + 2, x + 10);
/*
* Create borders around the windows so that you can see the effect
* of panels
*/
for(i = 0; i < 3; ++i)
box(my_wins[i], 0, 0);
/* Attach a panel to each window */ /* Order is bottom up */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
/* Update the stacking order. 2nd panel will be on top */
update_panels();
/* Show it on the screen */
doupdate();
getch();
endwin();
}