2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of Cat's Eye Technologies nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * $Id: curses_form.c,v 1.13 2005/02/08 22:56:06 cpressey Exp $
47 #define _(String) gettext (String)
49 #define _(String) (String)
52 #include "libaura/mem.h"
54 #include "libdfui/dump.h"
56 #include "curses_form.h"
57 #include "curses_widget.h"
58 #include "curses_util.h"
63 * Create a new curses form with the given title text.
66 curses_form_new(const char *title)
68 struct curses_form *cf;
70 AURA_MALLOC(cf, curses_form);
75 cf->widget_head = NULL;
76 cf->widget_tail = NULL;
77 cf->widget_focus = NULL;
79 cf->width = strlen(title) + 4;
87 cf->title = aura_strdup(title);
98 * Deallocate the memory used for the given curses form and all of the
99 * widgets it contains. Note that this only frees any data at the form's
100 * userdata pointer IFF cleanup is non-zero. Also, it does not cause the
101 * screen to be refreshed - call curses_form_refresh(NULL) afterwards to
102 * make the form disappear.
105 curses_form_free(struct curses_form *cf)
107 struct curses_widget *w, *t;
112 curses_widget_free(w);
116 if (cf->help_text != NULL) {
120 if (cf->cleanup && cf->userdata != NULL) {
124 if (cf->win != NULL) {
130 AURA_FREE(cf, curses_form);
134 * Prepare the widget for being placed in the form. This implements
135 * automatically positioning the widget and automatically resizing
136 * the form if the widget is too large to fit.
139 curses_form_widget_prepare(struct curses_form *cf, struct curses_widget *w)
142 * Link the widget to the form.
147 * Auto-position the widget to the center of the form,
150 if (w->flags & CURSES_WIDGET_CENTER)
151 w->x = (cf->width - w->width) / 2;
154 * If the widget's right edge exceeds the width of
155 * the form, expand the form.
157 dfui_debug("w->x=%d w->width=%d cf->width=%d : ",
158 w->x, w->width, cf->width);
159 if ((w->x + w->width + 1) > cf->width)
160 cf->width = w->x + w->width + 1;
161 dfui_debug("new cf->width=%d\n", cf->width);
165 * Create a new curses_widget and add it to an existing curses_form.
166 * If the width of the widget is larger than will fit on the form, the
167 * form will be expanded, unless it would be expanded larger than the
168 * screen, in which case the widget is shrunk.
170 struct curses_widget *
171 curses_form_widget_add(struct curses_form *cf,
172 unsigned int x, unsigned int y,
173 unsigned int width, widget_t type,
174 const char *text, unsigned int size,
177 struct curses_widget *w;
179 w = curses_widget_new(x, y, width, type, text, size, flags);
180 curses_form_widget_prepare(cf, w);
182 if (cf->widget_head == NULL) {
185 cf->widget_tail->next = w;
186 w->prev = cf->widget_tail;
194 * Create a new curses_widget and add it after an existing curses_widget
195 * in an existing curses_form.
197 struct curses_widget *
198 curses_form_widget_insert_after(struct curses_widget *cw,
199 unsigned int x, unsigned int y,
200 unsigned int width, widget_t type,
201 const char *text, unsigned int size,
204 struct curses_widget *w;
206 w = curses_widget_new(x, y, width, type, text, size, flags);
207 curses_form_widget_prepare(cw->form, w);
211 if (cw->next == NULL)
212 cw->form->widget_tail = w;
221 * Unlink a widget from a form. Does not deallocate the widget.
224 curses_form_widget_remove(struct curses_widget *w)
227 w->form->widget_head = w->next;
229 w->prev->next = w->next;
232 w->form->widget_tail = w->prev;
234 w->next->prev = w->prev;
242 curses_form_descriptive_labels_add(struct curses_form *cf, const char *text,
243 unsigned int x, unsigned int y,
250 line = aura_malloc(width + 1, "descriptive line");
252 done = extract_wrapped_line(text, line, width, &pos);
253 dfui_debug("line = `%s', done = %d, width = %d, form width = %d : ",
254 line, done, width, cf->width);
255 curses_form_widget_add(cf, x, y++, 0,
256 CURSES_LABEL, line, 0, CURSES_WIDGET_WIDEN);
257 dfui_debug("now %d\n", cf->width);
264 curses_form_finalize(struct curses_form *cf)
266 if (cf->widget_head != NULL) {
267 cf->widget_focus = cf->widget_head;
268 curses_form_focus_skip_forward(cf);
269 cf->want_x = cf->widget_focus->x + cf->widget_focus->width / 2;
270 cf->want_y = cf->widget_focus->y;
272 cf->widget_focus = NULL;
275 cf->left = (xmax - cf->width) / 2;
276 cf->top = (ymax - cf->height) / 2;
279 * Set the internal width and height.
282 cf->int_width = cf->width;
283 cf->int_height = cf->height;
286 * Limit form size to physical screen dimensions.
288 if (cf->width > (xmax - 2)) {
289 cf->width = xmax - 2;
292 if (cf->height > (ymax - 2)) {
293 cf->height = ymax - 2;
301 cf->win = newwin(cf->height + 2, cf->width + 2, cf->top - 1, cf->left - 1);
303 fprintf(stderr, "Could not allocate %dx%d window @ %d,%d\n",
304 cf->width + 2, cf->height + 2, cf->left - 1, cf->top - 1);
306 cf->pan = new_panel(cf->win);
310 * Render the given form (and all of the widgets it contains) in the
311 * curses backing store. Does not cause the form to be displayed.
314 curses_form_draw(struct curses_form *cf)
316 struct curses_widget *w;
317 float sb_factor = 0.0;
318 size_t sb_off = 0, sb_size = 0;
320 curses_colors_set(cf->win, CURSES_COLORS_NORMAL);
321 curses_window_blank(cf->win);
323 curses_colors_set(cf->win, CURSES_COLORS_BORDER);
324 /* draw_frame(cf->left - 1, cf->top - 1, cf->width + 2, cf->height + 2); */
325 wborder(cf->win, 0, 0, 0, 0, 0, 0, 0, 0);
328 * If the internal dimensions of the form exceed the physical
329 * dimensions, draw scrollbar(s) as appropriate.
331 if (cf->int_height > cf->height) {
332 sb_factor = (float)cf->height / (float)cf->int_height;
333 sb_size = cf->height * sb_factor;
334 if (sb_size == 0) sb_size = 1;
335 sb_off = cf->y_offset * sb_factor;
336 curses_colors_set(cf->win, CURSES_COLORS_SCROLLAREA);
337 mvwvline(cf->win, 1, cf->width + 1, ACS_CKBOARD, cf->height);
338 curses_colors_set(cf->win, CURSES_COLORS_SCROLLBAR);
339 mvwvline(cf->win, 1 + sb_off, cf->width + 1, ACS_BLOCK, sb_size);
342 if (cf->int_width > cf->width) {
343 sb_factor = (float)cf->width / (float)cf->int_width;
344 sb_size = cf->width * sb_factor;
345 if (sb_size == 0) sb_size = 1;
346 sb_off = cf->x_offset * sb_factor;
347 curses_colors_set(cf->win, CURSES_COLORS_SCROLLAREA);
348 mvwhline(cf->win, cf->height + 1, 1, ACS_CKBOARD, cf->width);
349 curses_colors_set(cf->win, CURSES_COLORS_SCROLLBAR);
350 mvwhline(cf->win, cf->height + 1, 1 + sb_off, ACS_BLOCK, sb_size);
353 curses_colors_set(cf->win, CURSES_COLORS_BORDER);
356 * Render the title bar text.
358 wmove(cf->win, 0, (cf->width - strlen(cf->title)) / 2 - 1);
359 waddch(cf->win, ACS_RTEE);
360 waddch(cf->win, ' ');
361 curses_colors_set(cf->win, CURSES_COLORS_FORMTITLE);
362 waddstr(cf->win, cf->title);
363 curses_colors_set(cf->win, CURSES_COLORS_BORDER);
364 waddch(cf->win, ' ');
365 waddch(cf->win, ACS_LTEE);
368 * Render a "how to get help" reminder.
370 if (cf->help_text != NULL) {
371 static const char *help_msg = "Press F1 for Help";
373 wmove(cf->win, cf->height + 1,
374 (cf->width - strlen(help_msg)) / 2 - 1);
375 waddch(cf->win, ACS_RTEE);
376 waddch(cf->win, ' ');
377 curses_colors_set(cf->win, CURSES_COLORS_FORMTITLE);
378 waddstr(cf->win, help_msg);
379 curses_colors_set(cf->win, CURSES_COLORS_BORDER);
380 waddch(cf->win, ' ');
381 waddch(cf->win, ACS_LTEE);
385 * Render the widgets.
387 for (w = cf->widget_head; w != NULL; w = w->next) {
388 curses_widget_draw(w);
391 /* to put the cursor there */
392 curses_widget_draw_tooltip(cf->widget_focus);
393 curses_widget_draw(cf->widget_focus);
397 * Cause the given form to be displayed (if it was not displayed previously)
398 * or refreshed (if it was previously displayed.) Passing NULL to this
399 * function causes all visible forms to be refreshed.
401 * (Implementation note: the argument is actually irrelevant - all visible
402 * forms will be refreshed when any form is displayed or refreshed - but
403 * client code should not rely on this behaviour.)
406 curses_form_refresh(struct curses_form *cf __unused)
413 curses_form_focus_skip_forward(struct curses_form *cf)
415 while (!curses_widget_can_take_focus(cf->widget_focus)) {
416 cf->widget_focus = cf->widget_focus->next;
417 if (cf->widget_focus == NULL)
418 cf->widget_focus = cf->widget_head;
420 curses_form_widget_ensure_visible(cf->widget_focus);
424 curses_form_focus_skip_backward(struct curses_form *cf)
426 while (!curses_widget_can_take_focus(cf->widget_focus)) {
427 cf->widget_focus = cf->widget_focus->prev;
428 if (cf->widget_focus == NULL)
429 cf->widget_focus = cf->widget_tail;
431 curses_form_widget_ensure_visible(cf->widget_focus);
435 curses_form_advance(struct curses_form *cf)
437 struct curses_widget *w;
439 w = cf->widget_focus;
440 cf->widget_focus = cf->widget_focus->next;
441 if (cf->widget_focus == NULL)
442 cf->widget_focus = cf->widget_head;
443 curses_form_focus_skip_forward(cf);
444 cf->want_x = cf->widget_focus->x + cf->widget_focus->width / 2;
445 cf->want_y = cf->widget_focus->y;
446 curses_widget_draw(w);
447 curses_widget_draw_tooltip(cf->widget_focus);
448 curses_widget_draw(cf->widget_focus);
449 curses_form_refresh(cf);
451 curses_debug_int(cf->widget_focus->user_id);
456 curses_form_retreat(struct curses_form *cf)
458 struct curses_widget *w;
460 w = cf->widget_focus;
461 cf->widget_focus = cf->widget_focus->prev;
462 if (cf->widget_focus == NULL)
463 cf->widget_focus = cf->widget_tail;
464 curses_form_focus_skip_backward(cf);
465 cf->want_x = cf->widget_focus->x + cf->widget_focus->width / 2;
466 cf->want_y = cf->widget_focus->y;
467 curses_widget_draw(w);
468 curses_widget_draw_tooltip(cf->widget_focus);
469 curses_widget_draw(cf->widget_focus);
470 curses_form_refresh(cf);
472 curses_debug_int(cf->widget_focus->user_id);
477 * Returns the widget at (x, y) within a form, or NULL if
478 * there is no widget at that location.
480 struct curses_widget *
481 curses_form_widget_at(struct curses_form *cf, unsigned int x, unsigned int y)
483 struct curses_widget *w;
485 for (w = cf->widget_head; w != NULL; w = w->next) {
486 if (y == w->y && x >= w->x && x <= (w->x + w->width))
494 * Returns the first (focusable) widget on
495 * the topmost row of the form.
498 curses_form_widget_first_row(struct curses_form *cf)
500 struct curses_widget *w;
502 for (w = cf->widget_head; w != NULL; w = w->next) {
503 if (curses_widget_can_take_focus(w))
511 * Returns the first (focusable) widget on
512 * the bottommost row of the form.
515 curses_form_widget_last_row(struct curses_form *cf)
517 struct curses_widget *w;
518 unsigned int best_y = 0;
520 for (w = cf->widget_head; w != NULL; w = w->next) {
521 if (curses_widget_can_take_focus(w) && w->y > best_y) {
530 * Returns the first (focusable) widget on row y.
532 struct curses_widget *
533 curses_form_widget_first_on_row(struct curses_form *cf, unsigned int y)
535 struct curses_widget *w;
537 for (w = cf->widget_head; w != NULL; w = w->next) {
538 if (curses_widget_can_take_focus(w) && y == w->y)
546 * Returns the (focusable) widget on row y closest to x.
548 struct curses_widget *
549 curses_form_widget_closest_on_row(struct curses_form *cf,
550 unsigned int x, unsigned int y)
552 struct curses_widget *w, *best = NULL;
553 int dist, best_dist = 999;
555 w = curses_form_widget_first_on_row(cf, y);
559 for (best = w; w != NULL && w->y == y; w = w->next) {
560 if (!curses_widget_can_take_focus(w))
562 dist = (w->x + w->width / 2) - x;
563 if (dist < 0) dist *= -1;
564 if (dist < best_dist) {
574 * Returns the number of (focusable) widgets with y values less than
575 * (above) the given widget.
578 curses_form_widget_count_above(struct curses_form *cf,
579 struct curses_widget *w)
581 struct curses_widget *lw;
584 for (lw = cf->widget_head; lw != NULL; lw = lw->next) {
585 if (curses_widget_can_take_focus(lw) && lw->y < w->y)
593 * Returns the number of (focusable) widgets with y values greater than
594 * (below) the given widget.
597 curses_form_widget_count_below(struct curses_form *cf,
598 struct curses_widget *w)
600 struct curses_widget *lw;
603 for (lw = cf->widget_head; lw != NULL; lw = lw->next) {
604 if (curses_widget_can_take_focus(lw) && lw->y > w->y)
612 * Move to the next widget whose y is greater than the
613 * current want_y, and whose x is closest to want_x.
616 curses_form_advance_row(struct curses_form *cf)
618 struct curses_widget *w, *c;
621 w = cf->widget_focus;
622 if (curses_form_widget_count_below(cf, w) > 0) {
625 wy = curses_form_widget_first_row(cf);
628 c = curses_form_widget_closest_on_row(cf,
632 cf->widget_focus = c;
633 curses_form_focus_skip_forward(cf);
634 cf->want_y = cf->widget_focus->y;
635 curses_widget_draw(w);
636 curses_widget_draw_tooltip(cf->widget_focus);
637 curses_widget_draw(cf->widget_focus);
638 curses_form_refresh(cf);
642 * Move to the next widget whose y is less than the
643 * current want_y, and whose x is closest to want_x.
646 curses_form_retreat_row(struct curses_form *cf)
648 struct curses_widget *w, *c;
651 w = cf->widget_focus;
652 if (curses_form_widget_count_above(cf, w) > 0) {
655 wy = curses_form_widget_last_row(cf);
658 c = curses_form_widget_closest_on_row(cf,
662 cf->widget_focus = c;
663 curses_form_focus_skip_backward(cf);
664 cf->want_y = cf->widget_focus->y;
665 curses_widget_draw(w);
666 curses_widget_draw_tooltip(cf->widget_focus);
667 curses_widget_draw(cf->widget_focus);
668 curses_form_refresh(cf);
672 curses_form_scroll_to(struct curses_form *cf,
673 unsigned int x_off, unsigned int y_off)
675 cf->x_offset = x_off;
676 cf->y_offset = y_off;
680 curses_form_scroll_delta(struct curses_form *cf, int dx, int dy)
682 unsigned int x_off, y_off;
684 if (dx < 0 && (unsigned int)-dx > cf->x_offset) {
687 x_off = cf->x_offset + dx;
689 if (x_off > (cf->int_width - cf->width))
690 x_off = cf->int_width - cf->width;
692 if (dy < 0 && (unsigned int)-dy > cf->y_offset) {
695 y_off = cf->y_offset + dy;
697 if (y_off > (cf->int_height - cf->height))
698 y_off = cf->int_height - cf->height;
700 curses_form_scroll_to(cf, x_off, y_off);
704 curses_form_refocus_after_scroll(struct curses_form *cf, int dx, int dy)
706 struct curses_widget *w;
708 w = curses_form_widget_closest_on_row(cf,
709 cf->widget_focus->x + dx, cf->widget_focus->y + dy);
712 cf->widget_focus = w;
713 cf->want_x = w->x + w->width / 2;
719 curses_form_widget_is_visible(struct curses_widget *w)
723 wy = w->y + 1 - w->form->y_offset;
725 if (wy < 1 || wy > w->form->height)
732 curses_form_widget_ensure_visible(struct curses_widget *w)
738 * If a textbox's offset is such that we can't see
739 * the cursor inside, adjust it.
741 if (w->type == CURSES_TEXTBOX) {
742 if (w->curpos - w->offset >= w->width - 2)
743 w->offset = w->curpos - (w->width - 3);
744 if (w->offset > w->curpos)
745 w->offset = w->curpos;
748 if (curses_form_widget_is_visible(w))
751 wy = w->y + 1 - w->form->y_offset;
755 else if (wy > w->form->height)
756 dy = (wy - w->form->height);
758 curses_form_scroll_delta(w->form, dx, dy);
759 curses_form_draw(w->form);
760 curses_form_refresh(w->form);
764 curses_form_show_help(const char *text)
766 struct curses_form *cf;
767 struct curses_widget *w;
769 cf = curses_form_new(_("Help"));
771 cf->height = curses_form_descriptive_labels_add(cf, text, 1, 1, 72);
773 w = curses_form_widget_add(cf, 0, cf->height++, 0,
774 CURSES_BUTTON, _("OK"), 0, CURSES_WIDGET_WIDEN);
775 curses_widget_set_click_cb(w, cb_click_close_form);
777 curses_form_finalize(cf);
779 curses_form_draw(cf);
780 curses_form_refresh(cf);
781 curses_form_frob(cf);
782 curses_form_free(cf);
785 #define CTRL(c) (char)(c - 'a' + 1)
787 struct curses_widget *
788 curses_form_frob(struct curses_form *cf)
798 curses_form_advance_row(cf);
802 curses_form_retreat_row(cf);
805 curses_form_advance(cf);
809 if (cf->widget_focus->type == CURSES_TEXTBOX) {
810 if (!curses_textbox_advance_char(cf->widget_focus))
811 curses_form_advance(cf);
813 curses_form_advance(cf);
817 if (cf->widget_focus->type == CURSES_TEXTBOX) {
818 if (!curses_textbox_retreat_char(cf->widget_focus))
819 curses_form_retreat(cf);
821 curses_form_retreat(cf);
825 if (cf->widget_focus->type == CURSES_TEXTBOX) {
826 switch (curses_widget_click(cf->widget_focus)) {
828 curses_form_advance(cf);
833 /* this would be pretty rare */
834 return(cf->widget_focus);
836 } else if (cf->widget_focus->type == CURSES_BUTTON) {
837 switch (curses_widget_click(cf->widget_focus)) {
844 return(cf->widget_focus);
846 } else if (cf->widget_focus->type == CURSES_CHECKBOX) {
847 curses_checkbox_toggle(cf->widget_focus);
854 case 127: /* why is this not KEY_BACKSPACE on xterm?? */
855 if (cf->widget_focus->type == CURSES_TEXTBOX) {
856 curses_textbox_backspace_char(cf->widget_focus);
863 if (cf->widget_focus->type == CURSES_TEXTBOX) {
864 curses_textbox_delete_char(cf->widget_focus);
871 if (cf->widget_focus->type == CURSES_TEXTBOX) {
872 curses_textbox_home(cf->widget_focus);
879 if (cf->widget_focus->type == CURSES_TEXTBOX) {
880 curses_textbox_end(cf->widget_focus);
887 curses_form_scroll_delta(cf, 0, cf->height - 1);
888 curses_form_refocus_after_scroll(cf, 0, cf->height - 1);
889 curses_form_draw(cf);
890 curses_form_refresh(cf);
894 curses_form_scroll_delta(cf, 0, -1 * (cf->height - 1));
895 curses_form_refocus_after_scroll(cf, 0, -1 * (cf->height - 1));
896 curses_form_draw(cf);
897 curses_form_refresh(cf);
900 if (cf->widget_focus->type == CURSES_TEXTBOX) {
901 /* XXX if non-editable, click it */
902 curses_textbox_insert_char(cf->widget_focus, ' ');
903 } else if (cf->widget_focus->type == CURSES_BUTTON) {
904 switch (curses_widget_click(cf->widget_focus)) {
911 return(cf->widget_focus);
913 } else if (cf->widget_focus->type == CURSES_CHECKBOX) {
914 curses_checkbox_toggle(cf->widget_focus);
919 case KEY_F(1): /* why does this not work in xterm??? */
921 if (cf->help_text != NULL) {
922 curses_form_show_help(cf->help_text);
923 curses_form_refresh(cf);
929 curses_form_refresh(NULL);
932 if (isprint(key) && cf->widget_focus->type == CURSES_TEXTBOX) {
933 curses_textbox_insert_char(cf->widget_focus, (char)key);
935 struct curses_widget *cw;
937 for (cw = cf->widget_head; cw != NULL; cw = cw->next) {
938 if (toupper(key) == cw->accel) {
943 cf->widget_focus = cw;
944 curses_form_widget_ensure_visible(cw);
945 curses_form_draw(cf);
946 curses_form_refresh(cf);
949 * To actually activate:
951 switch (curses_widget_click(cw)) {
965 curses_debug_key(key);
974 /*** GENERIC CALLBACKS ***/
977 * Callback to give to curses_button_set_click_cb, for buttons
978 * that simply close the form they are in when they are clicked.
979 * These usually map to dfui actions.
982 cb_click_close_form(struct curses_widget *w __unused)