File: scrollmenu.c

package info (click to toggle)
gramofile 1.6-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 840 kB
  • ctags: 538
  • sloc: ansic: 10,238; makefile: 55
file content (125 lines) | stat: -rw-r--r-- 2,594 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Scrolling Menus

 * Copyright (C) 1998 J.A. Bezemer
 *
 * Licensed under the terms of the GNU General Public License.
 * ABSOLUTELY NO WARRANTY.
 * See the file `COPYING' in this directory.
 */

#include "scrollmenu.h"
#include <stdlib.h>
#ifndef OLD_CURSES
#include <ncurses.h>
#else
#include <curses.h>
#endif


void
scrollmenu_display (scrollmenu_t * menu)
{
  int i, j, x1, y1;

  if (menu->selected >= menu->number)
    menu->selected = menu->number - 1;
  /* May get selected=-1 if number==0... */
  if (menu->selected < 0)	/* ...so this one must be last! */
    menu->selected = 0;
  if (menu->selected < menu->firstonscreen)
    menu->firstonscreen = menu->selected;
  if (menu->selected >= menu->firstonscreen + menu->h)
    menu->firstonscreen = menu->selected - menu->h + 1;

  for (i = 0; i < menu->h; i++)
    {
      if (menu->firstonscreen + i == menu->selected && menu->hasfocus)
	attron (A_STANDOUT);

      if (menu->firstonscreen + i < menu->number)
	mvaddnstr (menu->y + i,
		   menu->x,
		   menu->items[menu->firstonscreen + i],
		   menu->w);
      else
	move (menu->y + i, menu->x);

      getyx (stdscr, y1, x1);

      for (j = x1; j < menu->x + menu->w; j++)
	addch (' ');

      if (menu->firstonscreen + i == menu->selected && menu->hasfocus)
	attroff (A_STANDOUT);
    }

  move (menu->y + menu->selected - menu->firstonscreen, menu->x);
}

int
scrollmenu_stdkeys (int key, scrollmenu_t * menu)
/* Returns >0: item was selected;
   Returns -1 if nothing serious has happened.
 */

{
  int returnval = -1;

  switch (key)
    {
    case 'k':			/* Well, `less'-keys here also... */
    case 'K':
    case KEY_UP:
      (menu->selected)--;
      break;

    case 'j':
    case 'J':
    case KEY_DOWN:
      (menu->selected)++;
      break;

    case KEY_NPAGE:
      if (menu->selected < menu->firstonscreen +
	  menu->h - 1)
	menu->selected = menu->firstonscreen +
	  menu->h - 1;
      else
	menu->selected += menu->h - 1;
      break;

    case KEY_PPAGE:
      if (menu->selected > menu->firstonscreen)
	menu->selected = menu->firstonscreen;
      else
	menu->selected -= (menu->h - 1);
      break;

    case 13:
    case KEY_ENTER:
      returnval = menu->selected;
      break;
    }

  if (menu->selected < 0)
    menu->selected = 0;
  if (menu->selected >= menu->number)
    menu->selected = menu->number - 1;

  return returnval;
}

void
scrollmenu_delete_menu (scrollmenu_t * menu)
{
  int i;

  if (menu->items != NULL)
    {
      for (i = 0; i < menu->number; i++)
	free (menu->items[i]);

      free (menu->items);
      menu->items = NULL;
    }
}