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