Home | History | Annotate | Line # | Download | only in libcurses
line.c revision 1.5
      1 /*	$NetBSD: line.c,v 1.5 2007/05/28 15:01:56 blymn Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998-1999 Brett Lymn
      5  *						 (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
      6  * All rights reserved.
      7  *
      8  * This code has been donated to The NetBSD Foundation by the Author.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  *
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: line.c,v 1.5 2007/05/28 15:01:56 blymn Exp $");
     35 #endif				/* not lint */
     36 
     37 #include <string.h>
     38 
     39 #include "curses.h"
     40 #include "curses_private.h"
     41 
     42 /*
     43  * hline --
     44  *	Draw a horizontal line of character c on stdscr.
     45  */
     46 int
     47 hline(chtype ch, int count)
     48 {
     49 	return whline(stdscr, ch, count);
     50 }
     51 
     52 /*
     53  * mvhline --
     54  *	Move to location (y, x) and draw a horizontal line of character c
     55  *	on stdscr.
     56  */
     57 int
     58 mvhline(int y, int x, chtype ch, int count)
     59 {
     60 	return mvwhline(stdscr, y, x, ch, count);
     61 }
     62 
     63 /*
     64  * mvwhline --
     65  *	Move to location (y, x) and draw a horizontal line of character c
     66  *	in the given window.
     67  */
     68 int
     69 mvwhline(WINDOW *win, int y, int x, chtype ch, int count)
     70 {
     71 	if (wmove(win, y, x) == ERR)
     72 		return ERR;
     73 
     74 	return whline(win, ch, count);
     75 }
     76 
     77 /*
     78  * whline --
     79  *	Draw a horizontal line of character c in the given window moving
     80  *	towards the rightmost column.  At most count characters are drawn
     81  *	or until the edge of the screen, whichever comes first.
     82  */
     83 int
     84 whline(WINDOW *win, chtype ch, int count)
     85 {
     86 	int ocurx, n, i;
     87 
     88 	n = min(count, win->maxx - win->curx);
     89 	ocurx = win->curx;
     90 
     91 	if (!(ch & __CHARTEXT))
     92 		ch |= ACS_HLINE;
     93 	for (i = 0; i < n; i++)
     94 		mvwaddch(win, win->cury, ocurx + i, ch);
     95 
     96 	wmove(win, win->cury, ocurx);
     97 	return OK;
     98 }
     99 
    100 /*
    101  * vline --
    102  *	Draw a vertical line of character ch on stdscr.
    103  */
    104 int
    105 vline(chtype ch, int count)
    106 {
    107 	return wvline(stdscr, ch, count);
    108 }
    109 
    110 /*
    111  * mvvline --
    112  *	Move to the given location an draw a vertical line of character ch.
    113  */
    114 int
    115 mvvline(int y, int x, chtype ch, int count)
    116 {
    117 	return mvwvline(stdscr, y, x, ch, count);
    118 }
    119 
    120 /*
    121  * mvwvline --
    122  *	Move to the given location and draw a vertical line of character ch
    123  *	on the given window.
    124  */
    125 int
    126 mvwvline(WINDOW *win, int y, int x, chtype ch, int count)
    127 {
    128 	if (wmove(win, y, x) == ERR)
    129 		return ERR;
    130 
    131 	return wvline(win, ch, count);
    132 }
    133 
    134 /*
    135  * wvline --
    136  *	Draw a vertical line of character ch in the given window moving
    137  *	towards the bottom of the screen.  At most count characters are drawn
    138  *	or until the edge of the screen, whichever comes first.
    139  */
    140 int
    141 wvline(WINDOW *win, chtype ch, int count)
    142 {
    143 	int ocury, ocurx, n, i;
    144 
    145 	n = min(count, win->maxy - win->cury);
    146 	ocury = win->cury;
    147 	ocurx = win->curx;
    148 
    149 	if (!(ch & __CHARTEXT))
    150 		ch |= ACS_VLINE;
    151 	for (i = 0; i < n; i++)
    152 		mvwaddch(win, ocury + i, ocurx, ch);
    153 
    154 	wmove(win, ocury, ocurx);
    155 	return OK;
    156 }
    157 
    158 int hline_set(const cchar_t *wch, int n)
    159 {
    160 #ifndef HAVE_WCHAR
    161 	return ERR;
    162 #else
    163 	return whline_set( stdscr, wch, n );
    164 #endif /* HAVE_WCHAR */
    165 }
    166 
    167 int mvhline_set(int y, int x, const cchar_t *wch, int n)
    168 {
    169 #ifndef HAVE_WCHAR
    170 	return ERR;
    171 #else
    172 	return mvwhline_set( stdscr, y, x, wch, n );
    173 #endif /* HAVE_WCHAR */
    174 }
    175 
    176 int mvwhline_set(WINDOW *win, int y, int x, const cchar_t *wch, int n)
    177 {
    178 #ifndef HAVE_WCHAR
    179 	return ERR;
    180 #else
    181 	if ( wmove( win, y , x ) == ERR )
    182 		return ERR;
    183 
    184 	return whline_set( win, wch, n );
    185 #endif /* HAVE_WCHAR */
    186 }
    187 
    188 int whline_set(WINDOW *win, const cchar_t *wch, int n)
    189 {
    190 #ifndef HAVE_WCHAR
    191 	return ERR;
    192 #else
    193 	int ocurx, wcn, i, cw;
    194 	cchar_t cc;
    195 
    196 	cw = wcwidth( wch->vals[ 0 ]);
    197 	if ( ( win->maxx - win->curx ) < cw )
    198 		return ERR;
    199 	wcn = min( n, ( win->maxx - win->curx ) / cw );
    200 #ifdef DEBUG
    201 	__CTRACE(__CTRACE_LINE, "whline_set: line of %d\n", wcn);
    202 #endif /* DEBUG */
    203 	ocurx = win->curx;
    204 
    205 	memcpy( &cc, wch, sizeof( cchar_t ));
    206 	if (!(wch->vals[ 0 ]))
    207 		cc.vals[ 0 ] |= WACS_HLINE;
    208 	for (i = 0; i < wcn; i++ ) {
    209 #ifdef DEBUG
    210 		__CTRACE(__CTRACE_LINE, "whline_set: (%d,%d)\n",
    211 		   win->cury, ocurx + i * cw);
    212 #endif /* DEBUG */
    213 		mvwadd_wch(win, win->cury, ocurx + i * cw, &cc);
    214 	}
    215 
    216 	wmove(win, win->cury, ocurx);
    217 	return OK;
    218 #endif /* HAVE_WCHAR */
    219 }
    220 
    221 int vline_set(const cchar_t *wch, int n)
    222 {
    223 #ifndef HAVE_WCHAR
    224 	return ERR;
    225 #else
    226 	return wvline_set( stdscr, wch, n );
    227 #endif /* HAVE_WCHAR */
    228 }
    229 
    230 int mvvline_set(int y, int x, const cchar_t *wch, int n)
    231 {
    232 #ifndef HAVE_WCHAR
    233 	return ERR;
    234 #else
    235 	return mvwvline_set( stdscr, y, x, wch, n );
    236 #endif /* HAVE_WCHAR */
    237 }
    238 
    239 int mvwvline_set(WINDOW *win, int y, int x, const cchar_t *wch, int n)
    240 {
    241 #ifndef HAVE_WCHAR
    242 	return ERR;
    243 #else
    244 	if ( wmove( win, y, x ) == ERR )
    245 		return ERR;
    246 
    247 	return wvline_set( win, wch, n );
    248 #endif /* HAVE_WCHAR */
    249 }
    250 
    251 int wvline_set(WINDOW *win, const cchar_t *wch, int n)
    252 {
    253 #ifndef HAVE_WCHAR
    254 	return ERR;
    255 #else
    256 	int ocury, ocurx, wcn, i;
    257 	cchar_t cc;
    258 
    259 	wcn = min( n, win->maxy - win->cury);
    260 #ifdef DEBUG
    261 	__CTRACE(__CTRACE_LINE, "wvline_set: line of %d\n", wcn);
    262 #endif /* DEBUG */
    263 	ocury = win->cury;
    264 	ocurx = win->curx;
    265 
    266 	memcpy( &cc, wch, sizeof( cchar_t ));
    267 	if (!(wch->vals[ 0 ]))
    268 		cc.vals[ 0 ] |= WACS_VLINE;
    269 	for (i = 0; i < wcn; i++) {
    270 		mvwadd_wch(win, ocury + i, ocurx, &cc);
    271 #ifdef DEBUG
    272 		__CTRACE(__CTRACE_LINE, "wvline_set: (%d,%d)\n",
    273 		    ocury + i, ocurx);
    274 #endif /* DEBUG */
    275 	}
    276 	wmove(win, ocury, ocurx);
    277 	return OK;
    278 #endif /* HAVE_WCHAR */
    279 }
    280