xref: /openbsd/lib/libmenu/m_cursor.c (revision 09467b48)
1 /* $OpenBSD: m_cursor.c,v 1.8 2010/01/12 23:22:07 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *   Author:  Juergen Pfeifer, 1995,1997                                    *
33  ****************************************************************************/
34 
35 /***************************************************************************
36 * Module m_cursor                                                          *
37 * Correctly position a menu's cursor                                       *
38 ***************************************************************************/
39 
40 #include "menu.priv.h"
41 
42 MODULE_ID("$Id: m_cursor.c,v 1.8 2010/01/12 23:22:07 nicm Exp $")
43 
44 /*---------------------------------------------------------------------------
45 |   Facility      :  libnmenu
46 |   Function      :  _nc_menu_cursor_pos
47 |
48 |   Description   :  Return position of logical cursor to current item
49 |
50 |   Return Values :  E_OK            - success
51 |                    E_BAD_ARGUMENT  - invalid menu
52 |                    E_NOT_POSTED    - Menu is not posted
53 +--------------------------------------------------------------------------*/
54 NCURSES_EXPORT(int)
55 _nc_menu_cursor_pos(const MENU * menu, const ITEM * item, int *pY, int *pX)
56 {
57   if (!menu || !pX || !pY)
58     return (E_BAD_ARGUMENT);
59   else
60     {
61       if ((ITEM *) 0 == item)
62 	item = menu->curitem;
63       assert(item != (ITEM *) 0);
64 
65       if (!(menu->status & _POSTED))
66 	return (E_NOT_POSTED);
67 
68       *pX = item->x * (menu->spc_cols + menu->itemlen);
69       *pY = (item->y - menu->toprow) * menu->spc_rows;
70     }
71   return (E_OK);
72 }
73 
74 /*---------------------------------------------------------------------------
75 |   Facility      :  libnmenu
76 |   Function      :  pos_menu_cursor
77 |
78 |   Description   :  Position logical cursor to current item in menu
79 |
80 |   Return Values :  E_OK            - success
81 |                    E_BAD_ARGUMENT  - invalid menu
82 |                    E_NOT_POSTED    - Menu is not posted
83 +--------------------------------------------------------------------------*/
84 NCURSES_EXPORT(int)
85 pos_menu_cursor(const MENU * menu)
86 {
87   WINDOW *win, *sub;
88   int x = 0, y = 0;
89   int err = _nc_menu_cursor_pos(menu, (ITEM *) 0, &y, &x);
90 
91   T((T_CALLED("pos_menu_cursor(%p)"), menu));
92 
93   if (E_OK == err)
94     {
95       win = menu->userwin ? menu->userwin : stdscr;
96       sub = menu->usersub ? menu->usersub : win;
97       assert(win && sub);
98 
99       if ((menu->opt & O_SHOWMATCH) && (menu->pindex > 0))
100 	x += (menu->pindex + menu->marklen - 1);
101 
102       wmove(sub, y, x);
103 
104       if (win != sub)
105 	{
106 	  wcursyncup(sub);
107 	  wsyncup(sub);
108 	  untouchwin(sub);
109 	}
110     }
111   RETURN(err);
112 }
113 
114 /* m_cursor.c ends here */
115