summaryrefslogtreecommitdiff
path: root/aewl/listbox.c
blob: e839fee6f5e73f88c08c175c7a93fb7ae4f6b97d (plain)
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
/*
Copyright (c) 2003 Bruno T. C. de Oliveira

LICENSE INFORMATION:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Copyright (c) 2002 Bruno T. C. de Oliveira

INFORMA��ES DE LICEN�A:
Este programa � um software de livre distribui��o; voc� pode
redistribu�-lo e/ou modific�-lo sob os termos da GNU General
Public License, conforme publicado pela Free Software Foundation,
pela vers�o 2 da licen�a ou qualquer vers�o posterior.

Este programa � distribu�do na esperan�a de que ele ser� �til
aos seus usu�rios, por�m, SEM QUAISQUER GARANTIAS; sem sequer
a garantia impl�cita de COMERCIABILIDADE ou DE ADEQUA��O A
QUALQUER FINALIDADE ESPEC�FICA. Consulte a GNU General Public
License para obter mais detalhes (uma c�pia acompanha este
programa, armazenada no arquivo COPYING).
*/

#include "aewl.h"
#include "listbox.h"
#include "util.h"
#include <string.h>
#include <stdlib.h>

#define DEFAULT_CAPACITY 20
#define CAPACITY_INCREMENT 20

AewlWidget *aewl_listbox_create(int x, int y, int width, int height,
                             int shortcut, const char *caption, int result) {
   AewlListBox *lb = malloc(sizeof(AewlListBox));
   AewlWidget *base = &lb->base;
   if (!lb) return NULL;
   memset(lb, 0, sizeof(AewlListBox));

   aewl_widget_fill_defaults(&(lb->base));
   base->x = x;
   base->y = y;
   base->width = width;
   base->height = height;
   base->focusable = true;
   base->shortcut = shortcut;

   base->fn_repaint = aewl_listbox_repaint;
   base->fn_destroy = aewl_listbox_destroy;
   base->fn_handlekey = aewl_listbox_handlekey;

   lb->caption = caption ? strdup(caption) : NULL;
   lb->result = result;
   lb->sel = 0;
   lb->iat = 0;

   lb->item_count = 0;
   lb->capacity = DEFAULT_CAPACITY;
   lb->items = malloc(sizeof(char*) * DEFAULT_CAPACITY);
   if (!lb->items) {
      free(lb);
      return NULL;
   }

   return (AewlWidget*) lb;
}


void aewl_listbox_clear(AewlListBox *lb) {
   int i;
   for (i = 0; i < lb->item_count; i++) 
      if (lb->items[i]) free(lb->items[i]);
   lb->item_count = 0;
   lb->iat = 0;
   lb->sel = 0;
}

void aewl_listbox_add(AewlListBox *lb, const char *text) {
   if (lb->capacity <= lb->item_count) {
      lb->capacity += CAPACITY_INCREMENT;
      lb->items = realloc(lb->items, sizeof(char*) * lb->capacity);
      if (!lb->items) abort();
   }

   /* add new item to end of list now that we are sure there is
    * capacity for it */
   if (! (lb->items[lb->item_count++] = strdup(text)) ) abort();
}

const char *aewl_listbox_get(AewlListBox *lb, int i) {
   if (i < 0 || i >= lb->item_count) return NULL;
   return lb->items[i];
}

bool aewl_listbox_set(AewlListBox *lb, int i, const char *newvalue) {
   if (i < 0 || i >= lb->item_count) return false;
   if (lb->items[i]) free(lb->items[i]);
   if (! (lb->items[i] = strdup(newvalue)) ) abort();
   return true;
}

void aewl_listbox_destroy(AewlWidget *listbox) {
   AewlListBox *lb = (AewlListBox*) listbox;
   aewl_listbox_clear(lb);
   free(lb->items);
   if (lb->caption) free(lb->caption);
   free(lb);
}

void aewl_listbox_repaint(AewlWidget *listbox, bool focused) {
   AewlListBox *lb = (AewlListBox*) listbox;
   AewlWidget *base = &lb->base;
   int i, y;

   /* erase and draw bounding box */
   wattrset(base->win, AEWL_ATTR(frame, focused, false));
   aewl_util_erase(base->win, base->x, base->y, base->width, base->height);
   aewl_util_drawbox(base->win, base->x, base->y, base->width, base->height);

   /* draw caption across top, if any */
   if (lb->caption) 
      aewl_util_mvwaddstr(base->win, base->y, base->x + 2, 
         AEWL_ATTR(frame, focused, false),
         AEWL_ATTR(frame, focused, true), lb->caption);

   /* draw items, if there are any */
   if (lb->item_count) {
      for (                y = base->y + 1,                i = lb->iat; 
                           y < base->y + base->height - 1 && i < lb->item_count;
                           i++, y++) {

         wmove(base->win, y, base->x + 1);
         wattrset(base->win, AEWL_ATTR(field, focused, i == lb->sel));
         aewl_util_addnstr(base->win, base->width - 2, lb->items[i]);
      }
   }

   /* if there are more items than can be seen, show an indication */
   wattrset(base->win, AEWL_ATTR(frame, focused, true));

   if (lb->iat > 0)
      mvwaddch(base->win, base->y + 1,  base->x + base->width - 1, '^');
   if (lb->iat + base->height - 2 < lb->item_count)
      mvwaddch(base->win, base->y + base->height - 2, 
                          base->x + base->width - 1, 'v');

   /* move the cursor to the selected item */
   wmove(base->win, base->y + 1 + (lb->sel - lb->iat), base->x + 1);
}

int aewl_listbox_handlekey(AewlWidget *listbox, int ch) {
   AewlListBox *lb = (AewlListBox*) listbox;
   AewlWidget *base = &lb->base;

   switch (ch) {
      case KEY_UP:    lb->sel--; break;
      case KEY_DOWN:  lb->sel++; break;
      case KEY_PPAGE: lb->sel -= base->height - 2; break;
      case KEY_NPAGE: lb->sel += base->height - 2; break;
      case 10: return lb->result; /* action key */
   }

   /* apply corrections */
   if (lb->sel < 0) lb->sel = 0;
   if (lb->sel >= lb->item_count) lb->sel = lb->item_count - 1;

   /* ensure visibility of selected item */
   if (lb->sel < lb->iat) lb->iat = lb->sel;
   if (lb->sel >= lb->iat + (base->height - 2))
      lb->iat = lb->sel - (base->height - 2) + 1;

   return 0;
}

int aewl_listbox_get_sel(AewlListBox *lb) {
   if (lb->sel >= 0 && lb->sel < lb->item_count) return lb->sel;
   return -1;
}

static int _compar(const void *p1, const void *p2) {
   const char **a = (const char**) p1;
   const char **b = (const char**) p2;
   return strcmp(*a, *b);
}

void aewl_listbox_sort(AewlListBox *lb) {
   qsort(lb->items, lb->item_count, sizeof(const char*), _compar);
}