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.
 
 
 
 
 
 

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