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

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