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.
 
 
 
 
 
 

159 lines
4.4 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. if(cv == NULL)
  39. {
  40. printf("Can't created canvas\n");
  41. return -1;
  42. }
  43. dp = caca_create_display(cv);
  44. if(dp == NULL)
  45. {
  46. printf("Can't create display\n");
  47. return -1;
  48. }
  49. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  50. cucul_put_str(cv, 1, 1, "Text entries - press tab to cycle");
  51. for(i = 0; i < TEXT_ENTRIES; i++)
  52. {
  53. entries[i].buffer[0] = 0;
  54. entries[i].size = 0;
  55. entries[i].cursor = 0;
  56. }
  57. while(running)
  58. {
  59. caca_event_t ev;
  60. for(i = 0; i < TEXT_ENTRIES; i++)
  61. {
  62. unsigned int j, start, size;
  63. cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_LIGHTGRAY);
  64. cucul_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, ' ');
  65. start = 0;
  66. size = entries[i].size;
  67. for(j = 0; j < size; j++)
  68. {
  69. cucul_put_char(cv, 2 + j, 3 * i + 4,
  70. entries[i].buffer[start + j]);
  71. }
  72. }
  73. /* Put the cursor on the active textentry */
  74. cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_LIGHTRED);
  75. cucul_put_char(cv, 2 + entries[e].cursor, 3 * e + 4, ' ');
  76. caca_refresh_display(dp);
  77. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  78. continue;
  79. switch(ev.data.key.ch)
  80. {
  81. case CACA_KEY_ESCAPE:
  82. running = 0;
  83. break;
  84. case CACA_KEY_TAB:
  85. case CACA_KEY_RETURN:
  86. e = (e + 1) % TEXT_ENTRIES;
  87. break;
  88. case CACA_KEY_HOME:
  89. entries[e].cursor = 0;
  90. break;
  91. case CACA_KEY_END:
  92. entries[e].cursor = entries[e].size;
  93. break;
  94. case CACA_KEY_LEFT:
  95. if(entries[e].cursor)
  96. entries[e].cursor--;
  97. break;
  98. case CACA_KEY_RIGHT:
  99. if(entries[e].cursor < entries[e].size)
  100. entries[e].cursor++;
  101. break;
  102. case CACA_KEY_DELETE:
  103. if(entries[e].cursor < entries[e].size)
  104. {
  105. memmove(entries[e].buffer + entries[e].cursor,
  106. entries[e].buffer + entries[e].cursor + 1,
  107. (entries[e].size - entries[e].cursor + 1) * 4);
  108. entries[e].size--;
  109. }
  110. break;
  111. case CACA_KEY_BACKSPACE:
  112. if(entries[e].cursor)
  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].size--;
  118. entries[e].cursor--;
  119. }
  120. break;
  121. default:
  122. if(entries[e].size < BUFFER_SIZE)
  123. {
  124. memmove(entries[e].buffer + entries[e].cursor + 1,
  125. entries[e].buffer + entries[e].cursor,
  126. (entries[e].size - entries[e].cursor) * 4);
  127. entries[e].buffer[entries[e].cursor] = ev.data.key.utf32;
  128. entries[e].size++;
  129. entries[e].cursor++;
  130. }
  131. break;
  132. }
  133. }
  134. caca_free_display(dp);
  135. cucul_free_canvas(cv);
  136. return 0;
  137. }