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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <ipxe/editbox.h>
  27. /** @file
  28. *
  29. * Editable text box widget
  30. *
  31. */
  32. #define EDITBOX_MIN_CHARS 3
  33. /**
  34. * Initialise text box widget
  35. *
  36. * @v box Editable text box widget
  37. * @v buf Text buffer
  38. * @v len Size of text buffer
  39. * @v win Containing window
  40. * @v row Row
  41. * @v col Starting column
  42. * @v width Width
  43. * @v flags Flags
  44. */
  45. void init_editbox ( struct edit_box *box, char *buf, size_t len,
  46. WINDOW *win, unsigned int row, unsigned int col,
  47. unsigned int width, unsigned int flags ) {
  48. memset ( box, 0, sizeof ( *box ) );
  49. init_editstring ( &box->string, buf, len );
  50. box->string.cursor = strlen ( buf );
  51. box->win = ( win ? win : stdscr );
  52. box->row = row;
  53. box->col = col;
  54. box->width = width;
  55. box->flags = flags;
  56. }
  57. /**
  58. * Draw text box widget
  59. *
  60. * @v box Editable text box widget
  61. *
  62. */
  63. void draw_editbox ( struct edit_box *box ) {
  64. size_t width = box->width;
  65. char buf[ width + 1 ];
  66. signed int cursor_offset, underflow, overflow, first;
  67. size_t len;
  68. /* Adjust starting offset so that cursor remains within box */
  69. cursor_offset = ( box->string.cursor - box->first );
  70. underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
  71. overflow = ( cursor_offset - ( width - 1 ) );
  72. first = box->first;
  73. if ( underflow > 0 ) {
  74. first -= underflow;
  75. if ( first < 0 )
  76. first = 0;
  77. } else if ( overflow > 0 ) {
  78. first += overflow;
  79. }
  80. box->first = first;
  81. cursor_offset = ( box->string.cursor - first );
  82. /* Construct underscore-padded string portion */
  83. memset ( buf, '_', width );
  84. buf[width] = '\0';
  85. len = ( strlen ( box->string.buf ) - first );
  86. if ( len > width )
  87. len = width;
  88. if ( box->flags & EDITBOX_STARS ) {
  89. memset ( buf, '*', len );
  90. } else {
  91. memcpy ( buf, ( box->string.buf + first ), len );
  92. }
  93. /* Print box content and move cursor */
  94. if ( ! box->win )
  95. box->win = stdscr;
  96. mvwprintw ( box->win, box->row, box->col, "%s", buf );
  97. wmove ( box->win, box->row, ( box->col + cursor_offset ) );
  98. }