You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editbox.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <string.h>
  19. #include <assert.h>
  20. #include <gpxe/editbox.h>
  21. /** @file
  22. *
  23. * Editable text box widget
  24. *
  25. */
  26. #define EDITBOX_MIN_CHARS 3
  27. /**
  28. * Initialise text box widget
  29. *
  30. * @v box Editable text box widget
  31. * @v buf Text buffer
  32. * @v len Size of text buffer
  33. * @v win Containing window
  34. * @v row Row
  35. * @v col Starting column
  36. * @v width Width
  37. * @v flags Flags
  38. */
  39. void init_editbox ( struct edit_box *box, char *buf, size_t len,
  40. WINDOW *win, unsigned int row, unsigned int col,
  41. unsigned int width, unsigned int flags ) {
  42. memset ( box, 0, sizeof ( *box ) );
  43. box->string.buf = buf;
  44. box->string.len = len;
  45. box->string.cursor = strlen ( buf );
  46. box->win = ( win ? win : stdscr );
  47. box->row = row;
  48. box->col = col;
  49. box->width = width;
  50. box->flags = flags;
  51. }
  52. /**
  53. * Draw text box widget
  54. *
  55. * @v box Editable text box widget
  56. *
  57. */
  58. void draw_editbox ( struct edit_box *box ) {
  59. size_t width = box->width;
  60. char buf[ width + 1 ];
  61. signed int cursor_offset, underflow, overflow, first;
  62. size_t len;
  63. /* Adjust starting offset so that cursor remains within box */
  64. cursor_offset = ( box->string.cursor - box->first );
  65. underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
  66. overflow = ( cursor_offset - ( width - 1 ) );
  67. first = box->first;
  68. if ( underflow > 0 ) {
  69. first -= underflow;
  70. if ( first < 0 )
  71. first = 0;
  72. } else if ( overflow > 0 ) {
  73. first += overflow;
  74. }
  75. box->first = first;
  76. cursor_offset = ( box->string.cursor - first );
  77. /* Construct underscore-padded string portion */
  78. memset ( buf, '_', width );
  79. buf[width] = '\0';
  80. len = ( strlen ( box->string.buf ) - first );
  81. if ( len > width )
  82. len = width;
  83. if ( box->flags & EDITBOX_STARS ) {
  84. memset ( buf, '*', len );
  85. } else {
  86. memcpy ( buf, ( box->string.buf + first ), len );
  87. }
  88. /* Print box content and move cursor */
  89. if ( ! box->win )
  90. box->win = stdscr;
  91. mvwprintw ( box->win, box->row, box->col, "%s", buf );
  92. wmove ( box->win, box->row, ( box->col + cursor_offset ) );
  93. }