Team Fortress 2 Source Code as on 22/4/2020
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.

84 lines
3.4 KiB

  1. // I haven't actually tested this yet, this is just to make sure it compiles
  2. #include <stdlib.h>
  3. #include <string.h> // memmove
  4. #include <ctype.h> // isspace
  5. #define STB_TEXTEDIT_CHARTYPE char
  6. #define STB_TEXTEDIT_STRING text_control
  7. // get the base type
  8. #include "stb_textedit.h"
  9. // define our editor structure
  10. typedef struct
  11. {
  12. char *string;
  13. int stringlen;
  14. STB_TexteditState state;
  15. } text_control;
  16. // define the functions we need
  17. void layout_func(StbTexteditRow *row, STB_TEXTEDIT_STRING *str, int start_i)
  18. {
  19. int remaining_chars = str->stringlen - start_i;
  20. row->num_chars = remaining_chars > 20 ? 20 : remaining_chars; // should do real word wrap here
  21. row->x0 = 0;
  22. row->x1 = 20; // need to account for actual size of characters
  23. row->baseline_y_delta = 1.25;
  24. row->ymin = -1;
  25. row->ymax = 0;
  26. }
  27. int delete_chars(STB_TEXTEDIT_STRING *str, int pos, int num)
  28. {
  29. memmove(&str->string[pos], &str->string[pos+num], str->stringlen - (pos+num));
  30. str->stringlen -= num;
  31. return 1; // always succeeds
  32. }
  33. int insert_chars(STB_TEXTEDIT_STRING *str, int pos, STB_TEXTEDIT_CHARTYPE *newtext, int num)
  34. {
  35. str->string = realloc(str->string, str->stringlen + num);
  36. memmove(&str->string[pos+num], &str->string[pos], str->stringlen - pos);
  37. memcpy(&str->string[pos], newtext, num);
  38. str->stringlen += num;
  39. return 1; // always succeeds
  40. }
  41. // define all the #defines needed
  42. #define KEYDOWN_BIT 0x80000000
  43. #define STB_TEXTEDIT_STRINGLEN(tc) ((tc)->stringlen)
  44. #define STB_TEXTEDIT_LAYOUTROW layout_func
  45. #define STB_TEXTEDIT_GETWIDTH(tc,n,i) (1) // quick hack for monospaced
  46. #define STB_TEXTEDIT_KEYTOTEXT(key) (((key) & KEYDOWN_BIT) ? 0 : (key))
  47. #define STB_TEXTEDIT_GETCHAR(tc,i) ((tc)->string[i])
  48. #define STB_TEXTEDIT_NEWLINE '\n'
  49. #define STB_TEXTEDIT_IS_SPACE(ch) isspace(ch)
  50. #define STB_TEXTEDIT_DELETECHARS delete_chars
  51. #define STB_TEXTEDIT_INSERTCHARS insert_chars
  52. #define STB_TEXTEDIT_K_SHIFT 0x40000000
  53. #define STB_TEXTEDIT_K_CONTROL 0x20000000
  54. #define STB_TEXTEDIT_K_LEFT (KEYDOWN_BIT | 1) // actually use VK_LEFT, SDLK_LEFT, etc
  55. #define STB_TEXTEDIT_K_RIGHT (KEYDOWN_BIT | 2) // VK_RIGHT
  56. #define STB_TEXTEDIT_K_UP (KEYDOWN_BIT | 3) // VK_UP
  57. #define STB_TEXTEDIT_K_DOWN (KEYDOWN_BIT | 4) // VK_DOWN
  58. #define STB_TEXTEDIT_K_LINESTART (KEYDOWN_BIT | 5) // VK_HOME
  59. #define STB_TEXTEDIT_K_LINEEND (KEYDOWN_BIT | 6) // VK_END
  60. #define STB_TEXTEDIT_K_TEXTSTART (STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_CONTROL)
  61. #define STB_TEXTEDIT_K_TEXTEND (STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_CONTROL)
  62. #define STB_TEXTEDIT_K_DELETE (KEYDOWN_BIT | 7) // VK_DELETE
  63. #define STB_TEXTEDIT_K_BACKSPACE (KEYDOWN_BIT | 8) // VK_BACKSPACE
  64. #define STB_TEXTEDIT_K_UNDO (KEYDOWN_BIT | STB_TEXTEDIT_K_CONTROL | 'z')
  65. #define STB_TEXTEDIT_K_REDO (KEYDOWN_BIT | STB_TEXTEDIT_K_CONTROL | 'y')
  66. #define STB_TEXTEDIT_K_INSERT (KEYDOWN_BIT | 9) // VK_INSERT
  67. #define STB_TEXTEDIT_K_WORDLEFT (STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_CONTROL)
  68. #define STB_TEXTEDIT_K_WORDRIGHT (STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_CONTROL)
  69. #define STB_TEXTEDIT_K_PGUP (KEYDOWN_BIT | 10) // VK_PGUP -- not implemented
  70. #define STB_TEXTEDIT_K_PGDOWN (KEYDOWN_BIT | 11) // VK_PGDOWN -- not implemented
  71. #define STB_TEXTEDIT_IMPLEMENTATION
  72. #include "stb_textedit.h"