m_cursor.c revision 262629
1126261Smlaier/****************************************************************************
2145836Smlaier * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.              *
3126258Smlaier *                                                                          *
4126258Smlaier * Permission is hereby granted, free of charge, to any person obtaining a  *
5126258Smlaier * copy of this software and associated documentation files (the            *
6126258Smlaier * "Software"), to deal in the Software without restriction, including      *
7126258Smlaier * without limitation the rights to use, copy, modify, merge, publish,      *
8126258Smlaier * distribute, distribute with modifications, sublicense, and/or sell       *
9126258Smlaier * copies of the Software, and to permit persons to whom the Software is    *
10126258Smlaier * furnished to do so, subject to the following conditions:                 *
11126258Smlaier *                                                                          *
12126258Smlaier * The above copyright notice and this permission notice shall be included  *
13126258Smlaier * in all copies or substantial portions of the Software.                   *
14126258Smlaier *                                                                          *
15126258Smlaier * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16126258Smlaier * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17126258Smlaier * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18126258Smlaier * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19126258Smlaier * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20126258Smlaier * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21126258Smlaier * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22126258Smlaier *                                                                          *
23126258Smlaier * Except as contained in this notice, the name(s) of the above copyright   *
24126258Smlaier * holders shall not be used in advertising or otherwise to promote the     *
25126258Smlaier * sale, use or other dealings in this Software without prior written       *
26126258Smlaier * authorization.                                                           *
27126258Smlaier ****************************************************************************/
28126258Smlaier
29126258Smlaier/****************************************************************************
30126258Smlaier *   Author:  Juergen Pfeifer, 1995,1997                                    *
31126258Smlaier ****************************************************************************/
32126258Smlaier
33126258Smlaier/***************************************************************************
34126258Smlaier* Module m_cursor                                                          *
35126258Smlaier* Correctly position a menu's cursor                                       *
36126258Smlaier***************************************************************************/
37145836Smlaier
38126258Smlaier#include "menu.priv.h"
39126258Smlaier
40126258SmlaierMODULE_ID("$Id: m_cursor.c,v 1.22 2010/01/23 21:20:10 tom Exp $")
41126258Smlaier
42126258Smlaier/*---------------------------------------------------------------------------
43145836Smlaier|   Facility      :  libnmenu
44127145Smlaier|   Function      :  _nc_menu_cursor_pos
45130933Sbrooks|
46126261Smlaier|   Description   :  Return position of logical cursor to current item
47126261Smlaier|
48126258Smlaier|   Return Values :  E_OK            - success
49126261Smlaier|                    E_BAD_ARGUMENT  - invalid menu
50126261Smlaier|                    E_NOT_POSTED    - Menu is not posted
51127145Smlaier+--------------------------------------------------------------------------*/
52126261SmlaierNCURSES_EXPORT(int)
53126261Smlaier_nc_menu_cursor_pos(const MENU * menu, const ITEM * item, int *pY, int *pX)
54126261Smlaier{
55126258Smlaier  if (!menu || !pX || !pY)
56126258Smlaier    return (E_BAD_ARGUMENT);
57126258Smlaier  else
58135920Smlaier    {
59135920Smlaier      if ((ITEM *) 0 == item)
60135920Smlaier	item = menu->curitem;
61126258Smlaier      assert(item != (ITEM *) 0);
62126258Smlaier
63126258Smlaier      if (!(menu->status & _POSTED))
64126258Smlaier	return (E_NOT_POSTED);
65126258Smlaier
66130613Smlaier      *pX = item->x * (menu->spc_cols + menu->itemlen);
67145836Smlaier      *pY = (item->y - menu->toprow) * menu->spc_rows;
68126258Smlaier    }
69126258Smlaier  return (E_OK);
70126258Smlaier}
71126258Smlaier
72126258Smlaier/*---------------------------------------------------------------------------
73126258Smlaier|   Facility      :  libnmenu
74126258Smlaier|   Function      :  pos_menu_cursor
75126258Smlaier|
76126258Smlaier|   Description   :  Position logical cursor to current item in menu
77126258Smlaier|
78126258Smlaier|   Return Values :  E_OK            - success
79126258Smlaier|                    E_BAD_ARGUMENT  - invalid menu
80126258Smlaier|                    E_NOT_POSTED    - Menu is not posted
81126258Smlaier+--------------------------------------------------------------------------*/
82126258SmlaierNCURSES_EXPORT(int)
83126258Smlaierpos_menu_cursor(const MENU * menu)
84126258Smlaier{
85126258Smlaier  WINDOW *win, *sub;
86126258Smlaier  int x = 0, y = 0;
87130613Smlaier  int err = _nc_menu_cursor_pos(menu, (ITEM *) 0, &y, &x);
88145836Smlaier
89145836Smlaier  T((T_CALLED("pos_menu_cursor(%p)"), (const void *)menu));
90145836Smlaier
91145836Smlaier  if (E_OK == err)
92145836Smlaier    {
93145836Smlaier      win = Get_Menu_UserWin(menu);
94145836Smlaier      sub = menu->usersub ? menu->usersub : win;
95145836Smlaier      assert(win && sub);
96145836Smlaier
97145836Smlaier      if ((menu->opt & O_SHOWMATCH) && (menu->pindex > 0))
98145836Smlaier	x += (menu->pindex + menu->marklen - 1);
99145836Smlaier
100145836Smlaier      wmove(sub, y, x);
101145836Smlaier
102145836Smlaier      if (win != sub)
103145836Smlaier	{
104145836Smlaier	  wcursyncup(sub);
105145836Smlaier	  wsyncup(sub);
106145836Smlaier	  untouchwin(sub);
107145836Smlaier	}
108145836Smlaier    }
109145836Smlaier  RETURN(err);
110126258Smlaier}
111130613Smlaier
112126258Smlaier/* m_cursor.c ends here */
113126258Smlaier