aboutsummaryrefslogtreecommitdiffstats
path: root/list_window.c
blob: 21ee9119fc3f5b95fdf5c2a0c2bdd9acadeb23ce (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>
#include <ncurses.h>

#include "support.h"
#include "command.h"
#include "list_window.h"

list_window_t *
list_window_init(WINDOW *w, int width, int height)
{
  list_window_t *lw;

  lw = malloc(sizeof(list_window_t));
  memset(lw, 0, sizeof(list_window_t));
  lw->w = w;
  lw->cols = width;
  lw->rows = height;
  lw->clear = 1;
  return lw;
}

list_window_t *
list_window_free(list_window_t *lw)
{
  if( lw )
    {
      memset(lw, 0, sizeof(list_window_t));
      free(lw);
    }
  return NULL;
}

void
list_window_reset(list_window_t *lw)
{
  lw->selected = 0;
  lw->start = 0;
  lw->clear = 1;
}

void 
list_window_set_selected(list_window_t *lw, int n)
{
  lw->selected=n;
}

void
list_window_next(list_window_t *lw, int length)
{
  if( lw->selected < length-1 )
    lw->selected++;
}

void
list_window_previous(list_window_t *lw)
{
  if( lw->selected > 0 )
    lw->selected--;
}

void
list_window_first(list_window_t *lw)
{
  lw->selected = 0;
}

void
list_window_last(list_window_t *lw, int length)
{
  lw->selected = length-1;
}

void
list_window_next_page(list_window_t *lw, int length)
{
  int step = lw->rows-1;
  if( step<= 0 )
    return;
  if( lw->selected+step < length-1 )
    lw->selected+=step;
  else
    return list_window_last(lw,length);
}

void
list_window_previous_page(list_window_t *lw)
{
  int step = lw->rows-1;
  if( step<= 0 )
    return;
  if( lw->selected-step > 0 )
    lw->selected-=step;
  else
    list_window_first(lw);
}


void 
list_window_paint(list_window_t *lw,
		  list_window_callback_fn_t callback,
		  void *callback_data)
{
  int i;

  while( lw->selected < lw->start )
    {
      lw->start--;
      lw->clear=1;
    }
  while( lw->selected >= lw->start+lw->rows )
    {
      lw->start++;
      lw->clear=1;
    }
  if( lw->clear )
    {
      wclear(lw->w);
      lw->clear=0;
    }

  for(i=0; i<lw->rows; i++)
    {
      int highlight;
      char *label;

      label = (callback) (lw->start+i, &highlight, callback_data);
      if( label )
	{
	  wmove(lw->w, i, 0);
	  if( highlight )
	    wattron(lw->w, A_BOLD);
	  if( lw->start+i == lw->selected )
	    wattron(lw->w, A_REVERSE);
	  
	  waddnstr(lw->w, label, lw->cols);

	  if( highlight )
	    wattroff(lw->w, A_BOLD);
	  if( lw->start+i == lw->selected )
	    wattroff(lw->w, A_REVERSE);
	}
    }
}


int
list_window_find(list_window_t *lw, 
		 list_window_callback_fn_t callback,
		 void *callback_data,
		 char *str)
{
  int h;
  int i = lw->selected+1;
  char *label;

  while( (label=(callback) (i,&h,callback_data)) )
    {
      if( str && label && strcasestr(label, str) )
	{
	  lw->selected = i;
	  return 0;
	}
      i++;
    }
  return 1;
}


/* perform basic list window commands (movement) */
int 
list_window_cmd(list_window_t *lw, int rows, command_t cmd)
{
  switch(cmd)
    {
    case CMD_LIST_PREVIOUS:
      list_window_previous(lw);
      lw->repaint=1;
      break;
    case CMD_LIST_NEXT:
      list_window_next(lw, rows);
      lw->repaint=1;
      break;
    case CMD_LIST_FIRST:
      list_window_first(lw);
      lw->repaint  = 1;
      break;
    case CMD_LIST_LAST:
      list_window_last(lw, rows);
      lw->repaint = 1;
      break;
    case CMD_LIST_NEXT_PAGE:
      list_window_next_page(lw, rows);
      lw->repaint  = 1;
      break;
    case CMD_LIST_PREVIOUS_PAGE:
      list_window_previous_page(lw);
      lw->repaint  = 1;
      break;
    default:
      return 0;
    }
  return 1;
}