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