Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

149 řádky
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; 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_attr(cv, cucul_ansi_to_attr(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_attr(cv, cucul_ansi_to_attr(CUCUL_BLACK,
  53. 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_putchar(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_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTRED, CUCUL_LIGHTRED));
  65. cucul_putchar(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. }