xref: /openbsd-src/lib/libmenu/m_cursor.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1*c7ef0cfcSnicm /* $OpenBSD: m_cursor.c,v 1.9 2023/10/17 09:52:10 nicm Exp $ */
29f1aa62bSmillert 
3457960bfSmillert /****************************************************************************
4*c7ef0cfcSnicm  * Copyright 2020,2021 Thomas E. Dickey                                     *
5*c7ef0cfcSnicm  * Copyright 1998-2009,2010 Free Software Foundation, Inc.                  *
6457960bfSmillert  *                                                                          *
7457960bfSmillert  * Permission is hereby granted, free of charge, to any person obtaining a  *
8457960bfSmillert  * copy of this software and associated documentation files (the            *
9457960bfSmillert  * "Software"), to deal in the Software without restriction, including      *
10457960bfSmillert  * without limitation the rights to use, copy, modify, merge, publish,      *
11457960bfSmillert  * distribute, distribute with modifications, sublicense, and/or sell       *
12457960bfSmillert  * copies of the Software, and to permit persons to whom the Software is    *
13457960bfSmillert  * furnished to do so, subject to the following conditions:                 *
14457960bfSmillert  *                                                                          *
15457960bfSmillert  * The above copyright notice and this permission notice shall be included  *
16457960bfSmillert  * in all copies or substantial portions of the Software.                   *
17457960bfSmillert  *                                                                          *
18457960bfSmillert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19457960bfSmillert  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20457960bfSmillert  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21457960bfSmillert  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22457960bfSmillert  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23457960bfSmillert  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24457960bfSmillert  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25457960bfSmillert  *                                                                          *
26457960bfSmillert  * Except as contained in this notice, the name(s) of the above copyright   *
27457960bfSmillert  * holders shall not be used in advertising or otherwise to promote the     *
28457960bfSmillert  * sale, use or other dealings in this Software without prior written       *
29457960bfSmillert  * authorization.                                                           *
30457960bfSmillert  ****************************************************************************/
31457960bfSmillert 
32457960bfSmillert /****************************************************************************
3381d8c4e1Snicm  *   Author:  Juergen Pfeifer, 1995,1997                                    *
34457960bfSmillert  ****************************************************************************/
35c166cd22Stholo 
36c166cd22Stholo /***************************************************************************
379f1aa62bSmillert * Module m_cursor                                                          *
3881d8c4e1Snicm * Correctly position a menu's cursor                                       *
39c166cd22Stholo ***************************************************************************/
40c166cd22Stholo 
41c166cd22Stholo #include "menu.priv.h"
42c166cd22Stholo 
43*c7ef0cfcSnicm MODULE_ID("$Id: m_cursor.c,v 1.9 2023/10/17 09:52:10 nicm Exp $")
445be68eb8Smillert 
455be68eb8Smillert /*---------------------------------------------------------------------------
465be68eb8Smillert |   Facility      :  libnmenu
475be68eb8Smillert |   Function      :  _nc_menu_cursor_pos
485be68eb8Smillert |
495be68eb8Smillert |   Description   :  Return position of logical cursor to current item
505be68eb8Smillert |
515be68eb8Smillert |   Return Values :  E_OK            - success
525be68eb8Smillert |                    E_BAD_ARGUMENT  - invalid menu
535be68eb8Smillert |                    E_NOT_POSTED    - Menu is not posted
545be68eb8Smillert +--------------------------------------------------------------------------*/
MENU_EXPORT(int)55*c7ef0cfcSnicm MENU_EXPORT(int)
5681d8c4e1Snicm _nc_menu_cursor_pos(const MENU *menu, const ITEM *item, int *pY, int *pX)
575be68eb8Smillert {
585be68eb8Smillert   if (!menu || !pX || !pY)
595be68eb8Smillert     return (E_BAD_ARGUMENT);
605be68eb8Smillert   else
615be68eb8Smillert     {
625be68eb8Smillert       if ((ITEM *)0 == item)
635be68eb8Smillert 	item = menu->curitem;
645be68eb8Smillert       assert(item != (ITEM *)0);
655be68eb8Smillert 
665be68eb8Smillert       if (!(menu->status & _POSTED))
675be68eb8Smillert 	return (E_NOT_POSTED);
685be68eb8Smillert 
695be68eb8Smillert       *pX = item->x * (menu->spc_cols + menu->itemlen);
705be68eb8Smillert       *pY = (item->y - menu->toprow) * menu->spc_rows;
715be68eb8Smillert     }
725be68eb8Smillert   return (E_OK);
735be68eb8Smillert }
740107aba4Smillert 
75c166cd22Stholo /*---------------------------------------------------------------------------
76c166cd22Stholo |   Facility      :  libnmenu
77c166cd22Stholo |   Function      :  pos_menu_cursor
78c166cd22Stholo |
79c166cd22Stholo |   Description   :  Position logical cursor to current item in menu
80c166cd22Stholo |
81c166cd22Stholo |   Return Values :  E_OK            - success
82c166cd22Stholo |                    E_BAD_ARGUMENT  - invalid menu
83c166cd22Stholo |                    E_NOT_POSTED    - Menu is not posted
84c166cd22Stholo +--------------------------------------------------------------------------*/
85*c7ef0cfcSnicm MENU_EXPORT(int)
pos_menu_cursor(const MENU * menu)8684af20ceSmillert pos_menu_cursor(const MENU *menu)
87c166cd22Stholo {
8881d8c4e1Snicm   int x = 0, y = 0;
895be68eb8Smillert   int err = _nc_menu_cursor_pos(menu, (ITEM *)0, &y, &x);
90c166cd22Stholo 
91*c7ef0cfcSnicm   T((T_CALLED("pos_menu_cursor(%p)"), (const void *)menu));
9281d8c4e1Snicm 
935be68eb8Smillert   if (E_OK == err)
945be68eb8Smillert     {
95*c7ef0cfcSnicm       WINDOW *win = Get_Menu_UserWin(menu);
96*c7ef0cfcSnicm       WINDOW *sub = menu->usersub ? menu->usersub : win;
97*c7ef0cfcSnicm 
98c166cd22Stholo       assert(win && sub);
99c166cd22Stholo 
100c166cd22Stholo       if ((menu->opt & O_SHOWMATCH) && (menu->pindex > 0))
101c166cd22Stholo 	x += (menu->pindex + menu->marklen - 1);
102c166cd22Stholo 
103c166cd22Stholo       wmove(sub, y, x);
104c166cd22Stholo 
105c166cd22Stholo       if (win != sub)
106c166cd22Stholo 	{
107c166cd22Stholo 	  wcursyncup(sub);
108c166cd22Stholo 	  wsyncup(sub);
109c166cd22Stholo 	  untouchwin(sub);
110c166cd22Stholo 	}
111c166cd22Stholo     }
1125be68eb8Smillert   RETURN(err);
113c166cd22Stholo }
114c166cd22Stholo 
115c166cd22Stholo /* m_cursor.c ends here */
116