]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_item_cur.c
ncurses 4.1
[ncurses.git] / menu / m_item_cur.c
1 /*-----------------------------------------------------------------------------+
2 |           The ncurses menu library is  Copyright (C) 1995-1997               |
3 |             by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de>                 |
4 |                          All Rights Reserved.                                |
5 |                                                                              |
6 | Permission to use, copy, modify, and distribute this software and its        |
7 | documentation for any purpose and without fee is hereby granted, provided    |
8 | that the above copyright notice appear in all copies and that both that      |
9 | copyright notice and this permission notice appear in supporting             |
10 | documentation, and that the name of the above listed copyright holder(s) not |
11 | be used in advertising or publicity pertaining to distribution of the        |
12 | software without specific, written prior permission.                         | 
13 |                                                                              |
14 | THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO  |
15 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-  |
16 | NESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR   |
17 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RE- |
18 | SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
19 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH    |
20 | THE USE OR PERFORMANCE OF THIS SOFTWARE.                                     |
21 +-----------------------------------------------------------------------------*/
22
23 /***************************************************************************
24 * Module menu_item_cur                                                     *
25 * Set and get current menus item                                           *
26 ***************************************************************************/
27
28 #include "menu.priv.h"
29
30 MODULE_ID("$Id: m_item_cur.c,v 1.7 1997/05/01 16:47:26 juergen Exp $")
31
32 /*---------------------------------------------------------------------------
33 |   Facility      :  libnmenu  
34 |   Function      :  int set_current_item(MENU *menu, const ITEM *item)
35 |   
36 |   Description   :  Make the item the current item
37 |
38 |   Return Values :  E_OK                - success
39 +--------------------------------------------------------------------------*/
40 int set_current_item(MENU * menu, ITEM * item)
41 {
42   if (menu && item && (item->imenu==menu))
43     {
44       if ( menu->status & _IN_DRIVER )
45         RETURN(E_BAD_STATE);
46       
47       assert( menu->curitem );
48       if (item != menu->curitem)
49         {
50           if (menu->status & _LINK_NEEDED)
51             {
52               /*
53                * Items are available, but they are not linked together.
54                * So we have to link here.
55                */
56               _nc_Link_Items(menu);
57             }
58           assert(menu->pattern);
59           Reset_Pattern(menu);
60           /* adjust the window to make item visible and update the menu */
61           Adjust_Current_Item(menu,menu->toprow,item);
62         }
63     }
64   else
65     RETURN(E_BAD_ARGUMENT);
66   
67   RETURN(E_OK);
68 }
69
70 /*---------------------------------------------------------------------------
71 |   Facility      :  libnmenu  
72 |   Function      :  ITEM *current_item(const MENU *menu)
73 |   
74 |   Description   :  Return the menus current item
75 |
76 |   Return Values :  Item pointer or NULL if failure
77 +--------------------------------------------------------------------------*/
78 ITEM *current_item(const MENU * menu) 
79 {
80   return (menu && menu->items) ? menu->curitem : (ITEM *)0;
81 }
82
83 /*---------------------------------------------------------------------------
84 |   Facility      :  libnmenu  
85 |   Function      :  int item_index(const ITEM *)
86 |   
87 |   Description   :  Return the logical index of this item.
88 |
89 |   Return Values :  The index or ERR if this is an invalid item pointer
90 +--------------------------------------------------------------------------*/
91 int item_index(const ITEM *item)
92 {
93   return (item && item->imenu) ? item->index : ERR;
94 }
95
96 /*---------------------------------------------------------------------------
97 |   Facility      :  libnmenu  
98 |   Function      :  int set_top_row(MENU *menu, int row)
99 |   
100 |   Description   :  Makes the speified row the top row in the menu
101 |
102 |   Return Values :  E_OK             - success
103 |                    E_BAD_ARGUMENT   - not a menu pointer or invalid row
104 |                    E_NOT_CONNECTED  - there are no items for the menu
105 +--------------------------------------------------------------------------*/
106 int set_top_row(MENU * menu, int row)
107 {
108   ITEM *item;
109   
110   if (menu)
111     {
112       if ( menu->status & _IN_DRIVER )
113         RETURN(E_BAD_STATE);
114       if (menu->items == (ITEM **)0)
115         RETURN(E_NOT_CONNECTED);
116       
117       if ((row<0) || (row > (menu->rows - menu->arows)))
118         RETURN(E_BAD_ARGUMENT);
119     }
120   else
121     RETURN(E_BAD_ARGUMENT);
122   
123   if (row != menu->toprow)
124     {
125       if (menu->status & _LINK_NEEDED) 
126         _nc_Link_Items(menu);
127       
128       item = menu->items[ (menu->opt&O_ROWMAJOR) ? (row*menu->cols) : row ];
129       assert(menu->pattern);
130       Reset_Pattern(menu);
131       _nc_New_TopRow_and_CurrentItem(menu, row, item);
132     }
133   
134     RETURN(E_OK);
135 }
136
137 /*---------------------------------------------------------------------------
138 |   Facility      :  libnmenu  
139 |   Function      :  int top_row(const MENU *)
140 |   
141 |   Description   :  Return the top row of the menu
142 |
143 |   Return Values :  The row number or ERR if there is no row
144 +--------------------------------------------------------------------------*/
145 int top_row(const MENU * menu)
146 {
147   if (menu && menu->items && *(menu->items))
148     {
149       assert( (menu->toprow>=0) && (menu->toprow < menu->rows) );
150       return menu->toprow;
151     }
152   else
153     return(ERR);
154 }
155
156 /* m_item_cur.c ends here */