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.
 
 
 
 
 
 

149 lines
4.3 KiB

  1. /*
  2. * input libcaca text input test program
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #include "common.h"
  16. #if !defined(__KERNEL__)
  17. # if defined(HAVE_INTTYPES_H)
  18. # include <inttypes.h>
  19. # endif
  20. # include <string.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "caca.h"
  24. #define BUFFER_SIZE 75
  25. #define TEXT_ENTRIES 5
  26. typedef struct textentry
  27. {
  28. uint32_t buffer[BUFFER_SIZE + 1];
  29. unsigned int size, cursor;
  30. } textentry;
  31. int main(int argc, char *argv[])
  32. {
  33. textentry entries[TEXT_ENTRIES];
  34. cucul_canvas_t *cv;
  35. caca_display_t *dp;
  36. unsigned int i, e = 0, running = 1;
  37. cv = cucul_create_canvas(0, 0);
  38. dp = caca_create_display(cv);
  39. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  40. cucul_put_str(cv, 1, 1, "Text entries - press tab to cycle");
  41. for(i = 0; i < TEXT_ENTRIES; i++)
  42. {
  43. entries[i].buffer[0] = 0;
  44. entries[i].size = 0;
  45. entries[i].cursor = 0;
  46. }
  47. while(running)
  48. {
  49. caca_event_t ev;
  50. for(i = 0; i < TEXT_ENTRIES; i++)
  51. {
  52. unsigned int j, start, size;
  53. cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_LIGHTGRAY);
  54. cucul_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, ' ');
  55. start = 0;
  56. size = entries[i].size;
  57. for(j = 0; j < size; j++)
  58. {
  59. cucul_put_char(cv, 2 + j, 3 * i + 4,
  60. entries[i].buffer[start + j]);
  61. }
  62. }
  63. /* Put the cursor on the active textentry */
  64. cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_LIGHTRED);
  65. cucul_put_char(cv, 2 + entries[e].cursor, 3 * e + 4, ' ');
  66. caca_refresh_display(dp);
  67. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  68. continue;
  69. switch(ev.data.key.ch)
  70. {
  71. case CACA_KEY_ESCAPE:
  72. running = 0;
  73. break;
  74. case CACA_KEY_TAB:
  75. case CACA_KEY_RETURN:
  76. e = (e + 1) % TEXT_ENTRIES;
  77. break;
  78. case CACA_KEY_HOME:
  79. entries[e].cursor = 0;
  80. break;
  81. case CACA_KEY_END:
  82. entries[e].cursor = entries[e].size;
  83. break;
  84. case CACA_KEY_LEFT:
  85. if(entries[e].cursor)
  86. entries[e].cursor--;
  87. break;
  88. case CACA_KEY_RIGHT:
  89. if(entries[e].cursor < entries[e].size)
  90. entries[e].cursor++;
  91. break;
  92. case CACA_KEY_DELETE:
  93. if(entries[e].cursor < entries[e].size)
  94. {
  95. memmove(entries[e].buffer + entries[e].cursor,
  96. entries[e].buffer + entries[e].cursor + 1,
  97. (entries[e].size - entries[e].cursor + 1) * 4);
  98. entries[e].size--;
  99. }
  100. break;
  101. case CACA_KEY_BACKSPACE:
  102. if(entries[e].cursor)
  103. {
  104. memmove(entries[e].buffer + entries[e].cursor - 1,
  105. entries[e].buffer + entries[e].cursor,
  106. (entries[e].size - entries[e].cursor) * 4);
  107. entries[e].size--;
  108. entries[e].cursor--;
  109. }
  110. break;
  111. default:
  112. if(entries[e].size < BUFFER_SIZE)
  113. {
  114. memmove(entries[e].buffer + entries[e].cursor + 1,
  115. entries[e].buffer + entries[e].cursor,
  116. (entries[e].size - entries[e].cursor) * 4);
  117. entries[e].buffer[entries[e].cursor] = ev.data.key.utf32;
  118. entries[e].size++;
  119. entries[e].cursor++;
  120. }
  121. break;
  122. }
  123. }
  124. caca_free_display(dp);
  125. cucul_free_canvas(cv);
  126. return 0;
  127. }